blob: ee226e22710ffebd3b5f1e63c21e727f6e3733c7 [file] [log] [blame]
Chris Masone3bd3c8c2011-06-13 08:20:26 -07001// Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef SHILL_CELLULAR_
6#define SHILL_CELLULAR_
7
8#include <string>
9
10#include <base/basictypes.h>
Darin Petkovc5f56562011-08-06 16:40:05 -070011#include <base/task.h>
Darin Petkove9d12e02011-07-27 15:09:37 -070012#include <gtest/gtest_prod.h> // for FRIEND_TEST
Chris Masone3bd3c8c2011-06-13 08:20:26 -070013
Darin Petkovc5f56562011-08-06 16:40:05 -070014#include "shill/dbus_properties.h"
Chris Masone3bd3c8c2011-06-13 08:20:26 -070015#include "shill/device.h"
Darin Petkovd9661952011-08-03 16:25:42 -070016#include "shill/modem_cdma_proxy_interface.h"
Darin Petkovc5f56562011-08-06 16:40:05 -070017#include "shill/modem_proxy_interface.h"
Chris Masone2b105542011-06-22 10:58:09 -070018#include "shill/refptr_types.h"
Chris Masone3bd3c8c2011-06-13 08:20:26 -070019#include "shill/shill_event.h"
20
21namespace shill {
22
Darin Petkov4d6d9412011-08-24 13:19:54 -070023class Error;
Darin Petkove604f702011-07-28 15:51:17 -070024class ModemSimpleProxyInterface;
Darin Petkove9d12e02011-07-27 15:09:37 -070025
Darin Petkovd9661952011-08-03 16:25:42 -070026class Cellular : public Device,
Darin Petkovc5f56562011-08-06 16:40:05 -070027 public ModemCDMAProxyListener,
28 public ModemProxyListener {
Chris Masone3bd3c8c2011-06-13 08:20:26 -070029 public:
Darin Petkove9d12e02011-07-27 15:09:37 -070030 enum Type {
31 kTypeGSM,
32 kTypeCDMA
33 };
34
Darin Petkov0828f5f2011-08-11 10:18:52 -070035 // The device states progress linearly from Disabled to Linked.
Darin Petkove9d12e02011-07-27 15:09:37 -070036 enum State {
Darin Petkov0828f5f2011-08-11 10:18:52 -070037 // This is the initial state of the modem and indicates that the modem radio
38 // is not turned on.
Darin Petkove9d12e02011-07-27 15:09:37 -070039 kStateDisabled,
Darin Petkov0828f5f2011-08-11 10:18:52 -070040 // This state indicates that the modem radio is turned on, and it should be
41 // possible to measure signal strength.
Darin Petkove9d12e02011-07-27 15:09:37 -070042 kStateEnabled,
Darin Petkov0828f5f2011-08-11 10:18:52 -070043 // The modem has registered with a network and has signal quality
Darin Petkov51489002011-08-18 13:13:20 -070044 // measurements. A cellular service object is created.
Darin Petkove9d12e02011-07-27 15:09:37 -070045 kStateRegistered,
Darin Petkov0828f5f2011-08-11 10:18:52 -070046 // The modem has connected to a network.
Darin Petkove9d12e02011-07-27 15:09:37 -070047 kStateConnected,
Darin Petkov0828f5f2011-08-11 10:18:52 -070048 // The network interface is UP.
49 kStateLinked,
Darin Petkove9d12e02011-07-27 15:09:37 -070050 };
51
Darin Petkovbac96002011-08-09 13:22:00 -070052 // These should be kept in sync with ModemManager's mm-modem.h:MMModemState.
53 enum ModemState {
54 kModemStateUnknown = 0,
55 kModemStateDisabled = 10,
56 kModemStateDisabling = 20,
57 kModemStateEnabling = 30,
58 kModemStateEnabled = 40,
59 kModemStateSearching = 50,
60 kModemStateRegistered = 60,
61 kModemStateDisconnecting = 70,
62 kModemStateConnecting = 80,
63 kModemStateConnected = 90,
64 };
65
Darin Petkov3335b372011-08-22 11:05:32 -070066 class Operator {
67 public:
68 Operator();
69 ~Operator();
70
71 void CopyFrom(const Operator &oper);
72
73 const std::string &GetName() const;
74 void SetName(const std::string &name);
75
76 const std::string &GetCode() const;
77 void SetCode(const std::string &code);
78
79 const std::string &GetCountry() const;
80 void SetCountry(const std::string &country);
81
82 const Stringmap &ToDict() const;
83
84 private:
85 Stringmap dict_;
86
87 DISALLOW_COPY_AND_ASSIGN(Operator);
88 };
89
Chris Masone889666b2011-07-03 12:58:50 -070090 class Network {
91 public:
92 Network();
Darin Petkove9d12e02011-07-27 15:09:37 -070093 ~Network();
Chris Masone889666b2011-07-03 12:58:50 -070094
95 const std::string &GetStatus() const;
96 void SetStatus(const std::string &status);
97
98 const std::string &GetId() const;
99 void SetId(const std::string &id);
100
101 const std::string &GetShortName() const;
102 void SetShortName(const std::string &name);
103
104 const std::string &GetLongName() const;
105 void SetLongName(const std::string &name);
106
107 const std::string &GetTechnology() const;
108 void SetTechnology(const std::string &technology);
109
110 const Stringmap &ToDict() const;
111
112 private:
113 Stringmap dict_;
Darin Petkove9d12e02011-07-27 15:09:37 -0700114
Chris Masone889666b2011-07-03 12:58:50 -0700115 DISALLOW_COPY_AND_ASSIGN(Network);
116 };
117
Darin Petkovbec79a22011-08-01 14:47:17 -0700118 struct CDMA {
119 CDMA();
120
121 uint32 registration_state_evdo;
122 uint32 registration_state_1x;
123 uint32 activation_state;
Darin Petkovcc044422011-08-17 13:30:06 -0700124
125 uint16 prl_version;
126 std::string payment_url;
127 std::string usage_url;
Darin Petkovbec79a22011-08-01 14:47:17 -0700128 };
129
Chris Masone889666b2011-07-03 12:58:50 -0700130 struct SimLockStatus {
131 public:
Darin Petkove9d12e02011-07-27 15:09:37 -0700132 SimLockStatus() : retries_left(0) {}
133
Chris Masone889666b2011-07-03 12:58:50 -0700134 std::string lock_type;
135 uint32 retries_left;
136 };
137
Darin Petkovc5f56562011-08-06 16:40:05 -0700138 static const char kConnectPropertyPhoneNumber[];
139
Darin Petkove9d12e02011-07-27 15:09:37 -0700140 // |owner| is the ModemManager DBus service owner (e.g., ":1.17"). |path| is
141 // the ModemManager.Modem DBus object path (e.g.,
142 // "/org/chromium/ModemManager/Gobi/0").
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700143 Cellular(ControlInterface *control_interface,
144 EventDispatcher *dispatcher,
145 Manager *manager,
Darin Petkove9d12e02011-07-27 15:09:37 -0700146 const std::string &link_name,
Chris Masone626719f2011-08-18 16:58:48 -0700147 const std::string &address,
Darin Petkove9d12e02011-07-27 15:09:37 -0700148 int interface_index,
149 Type type,
150 const std::string &owner,
151 const std::string &path);
152 virtual ~Cellular();
153
Darin Petkov4d6d9412011-08-24 13:19:54 -0700154 // Asynchronously connects the modem to the network. Populates |error| on
155 // failure, leaves it unchanged otherwise.
156 void Connect(Error *error);
Darin Petkovc5f56562011-08-06 16:40:05 -0700157
Darin Petkovc408e692011-08-17 13:47:15 -0700158 // Asynchronously activates the modem.
159 void Activate(const std::string &carrier);
160
Darin Petkovbac96002011-08-09 13:22:00 -0700161 void set_modem_state(ModemState state) { modem_state_ = state; }
162 ModemState modem_state() const { return modem_state_; }
163
Darin Petkove9d12e02011-07-27 15:09:37 -0700164 // Inherited from Device.
165 virtual void Start();
166 virtual void Stop();
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700167 virtual bool TechnologyIs(Technology type) const;
Darin Petkov0828f5f2011-08-11 10:18:52 -0700168 virtual void LinkEvent(unsigned int flags, unsigned int change);
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700169
170 private:
Darin Petkovc408e692011-08-17 13:47:15 -0700171 FRIEND_TEST(CellularTest, Activate);
Darin Petkov51489002011-08-18 13:13:20 -0700172 FRIEND_TEST(CellularTest, ActivateError);
Darin Petkov3335b372011-08-22 11:05:32 -0700173 FRIEND_TEST(CellularTest, CreateService);
Darin Petkovc5f56562011-08-06 16:40:05 -0700174 FRIEND_TEST(CellularTest, Connect);
Darin Petkovc408e692011-08-17 13:47:15 -0700175 FRIEND_TEST(CellularTest, GetCDMAActivationStateString);
Darin Petkov51489002011-08-18 13:13:20 -0700176 FRIEND_TEST(CellularTest, GetCDMAActivationErrorString);
Darin Petkovbec79a22011-08-01 14:47:17 -0700177 FRIEND_TEST(CellularTest, GetCDMARegistrationState);
Darin Petkovd9661952011-08-03 16:25:42 -0700178 FRIEND_TEST(CellularTest, GetCDMASignalQuality);
Darin Petkovceb68172011-07-29 14:47:48 -0700179 FRIEND_TEST(CellularTest, GetModemInfo);
180 FRIEND_TEST(CellularTest, GetModemStatus);
Darin Petkovd2045802011-08-23 11:09:25 -0700181 FRIEND_TEST(CellularTest, GetNetworkTechnologyString);
182 FRIEND_TEST(CellularTest, GetRoamingStateString);
Darin Petkove9d12e02011-07-27 15:09:37 -0700183 FRIEND_TEST(CellularTest, GetStateString);
184 FRIEND_TEST(CellularTest, GetTypeString);
Darin Petkovbec79a22011-08-01 14:47:17 -0700185 FRIEND_TEST(CellularTest, InitProxiesCDMA);
186 FRIEND_TEST(CellularTest, InitProxiesGSM);
Darin Petkove9d12e02011-07-27 15:09:37 -0700187 FRIEND_TEST(CellularTest, Start);
Darin Petkovbac96002011-08-09 13:22:00 -0700188 FRIEND_TEST(CellularTest, StartConnected);
Darin Petkov0828f5f2011-08-11 10:18:52 -0700189 FRIEND_TEST(CellularTest, StartLinked);
Darin Petkovbac96002011-08-09 13:22:00 -0700190 FRIEND_TEST(CellularTest, StartRegister);
191
192 static const char kPhoneNumberCDMA[];
193 static const char kPhoneNumberGSM[];
Darin Petkove9d12e02011-07-27 15:09:37 -0700194
Darin Petkov0828f5f2011-08-11 10:18:52 -0700195 void SetState(State state);
196
Darin Petkovc5f56562011-08-06 16:40:05 -0700197 void ConnectTask(const DBusPropertiesMap &properties);
Darin Petkovc408e692011-08-17 13:47:15 -0700198 void ActivateTask(const std::string &carrier);
Darin Petkovc5f56562011-08-06 16:40:05 -0700199
Darin Petkovbac96002011-08-09 13:22:00 -0700200 // Invoked when the modem is connected to the cellular network to transition
201 // to the network-connected state and bring the network interface up.
202 void EstablishLink();
203
Chris Masone889666b2011-07-03 12:58:50 -0700204 Stringmaps EnumerateNetworks();
205 StrIntPair SimLockStatusToProperty();
206 void HelpRegisterDerivedStringmaps(const std::string &name,
207 Stringmaps(Cellular::*get)(void),
208 bool(Cellular::*set)(const Stringmaps&));
209 void HelpRegisterDerivedStrIntPair(const std::string &name,
210 StrIntPair(Cellular::*get)(void),
211 bool(Cellular::*set)(const StrIntPair&));
212
Darin Petkovbec79a22011-08-01 14:47:17 -0700213 void InitProxies();
214
Darin Petkovcc044422011-08-17 13:30:06 -0700215 std::string GetTypeString() const;
216 static std::string GetStateString(State state);
Darin Petkove9d12e02011-07-27 15:09:37 -0700217
Darin Petkovd2045802011-08-23 11:09:25 -0700218 // Returns an empty string if the network technology is unknown.
219 std::string GetNetworkTechnologyString() const;
220
221 std::string GetRoamingStateString() const;
222
Darin Petkovc408e692011-08-17 13:47:15 -0700223 static std::string GetCDMAActivationStateString(uint32 state);
Darin Petkov51489002011-08-18 13:13:20 -0700224 static std::string GetCDMAActivationErrorString(uint32 error);
Darin Petkovc408e692011-08-17 13:47:15 -0700225
Darin Petkovf5f61e02011-07-29 11:35:40 -0700226 void EnableModem();
227 void GetModemStatus();
Darin Petkovceb68172011-07-29 14:47:48 -0700228 void GetGSMProperties();
229 void RegisterGSMModem();
Darin Petkovceb68172011-07-29 14:47:48 -0700230
231 // Obtains the modem identifiers: MEID for CDMA; IMEI, IMSI, SPN and MSISDN
232 // for GSM.
233 void GetModemIdentifiers();
234
Darin Petkovd9661952011-08-03 16:25:42 -0700235 // Obtains modem's manufacturer, model ID, and hardware revision.
Darin Petkovceb68172011-07-29 14:47:48 -0700236 void GetModemInfo();
Darin Petkovf5f61e02011-07-29 11:35:40 -0700237
Darin Petkovbec79a22011-08-01 14:47:17 -0700238 void GetModemRegistrationState();
239 void GetCDMARegistrationState();
240 void GetGSMRegistrationState();
241
Darin Petkovd9661952011-08-03 16:25:42 -0700242 // Processes a change in the modem registration state, possibly creating,
243 // destroying or updating the CellularService.
244 void HandleNewRegistrationState();
Darin Petkov0828f5f2011-08-11 10:18:52 -0700245 void HandleNewRegistrationStateTask();
Darin Petkovd9661952011-08-03 16:25:42 -0700246
247 void CreateService();
248
249 void GetModemSignalQuality();
250 uint32 GetCDMASignalQuality();
251 uint32 GetGSMSignalQuality();
252
253 void HandleNewSignalQuality(uint32 strength);
254
Darin Petkovc408e692011-08-17 13:47:15 -0700255 void HandleNewCDMAActivationState(uint32 error);
256
Darin Petkovc5f56562011-08-06 16:40:05 -0700257 // Signal callbacks inherited from ModemCDMAProxyListener.
Darin Petkovb27e5442011-08-16 14:36:45 -0700258 virtual void OnCDMAActivationStateChanged(
259 uint32 activation_state,
260 uint32 activation_error,
261 const DBusPropertiesMap &status_changes);
Darin Petkovd9661952011-08-03 16:25:42 -0700262 virtual void OnCDMARegistrationStateChanged(uint32 state_1x,
263 uint32 state_evdo);
264 virtual void OnCDMASignalQualityChanged(uint32 strength);
265
Darin Petkovc5f56562011-08-06 16:40:05 -0700266 // Signal callbacks inherited from ModemProxyListener.
267 virtual void OnModemStateChanged(uint32 old_state,
268 uint32 new_state,
269 uint32 reason);
270
Darin Petkove9d12e02011-07-27 15:09:37 -0700271 Type type_;
272 State state_;
Darin Petkovbac96002011-08-09 13:22:00 -0700273 ModemState modem_state_;
Darin Petkove9d12e02011-07-27 15:09:37 -0700274
275 const std::string dbus_owner_; // ModemManager.Modem
276 const std::string dbus_path_; // ModemManager.Modem
277 scoped_ptr<ModemProxyInterface> proxy_;
Darin Petkove604f702011-07-28 15:51:17 -0700278 scoped_ptr<ModemSimpleProxyInterface> simple_proxy_;
Darin Petkovbec79a22011-08-01 14:47:17 -0700279 scoped_ptr<ModemCDMAProxyInterface> cdma_proxy_;
280
281 CDMA cdma_;
Darin Petkove9d12e02011-07-27 15:09:37 -0700282
Darin Petkovd9661952011-08-03 16:25:42 -0700283 CellularServiceRefPtr service_;
Chris Masone853b81b2011-06-24 14:11:41 -0700284
Darin Petkovc5f56562011-08-06 16:40:05 -0700285 ScopedRunnableMethodFactory<Cellular> task_factory_;
286
Chris Masoneb925cc82011-06-22 15:39:57 -0700287 // Properties
288 bool allow_roaming_;
289 std::string carrier_;
290 std::string meid_;
291 std::string imei_;
292 std::string imsi_;
293 std::string esn_;
294 std::string mdn_;
295 std::string min_;
296 std::string model_id_;
297 std::string manufacturer_;
298 std::string firmware_revision_;
299 std::string hardware_revision_;
Chris Masoneb925cc82011-06-22 15:39:57 -0700300 bool scanning_;
301 uint16 scan_interval_;
Chris Masone889666b2011-07-03 12:58:50 -0700302 std::vector<Network> found_networks_;
303 SimLockStatus sim_lock_status_;
Darin Petkov3335b372011-08-22 11:05:32 -0700304 Operator home_provider_;
Chris Masoneb925cc82011-06-22 15:39:57 -0700305
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700306 DISALLOW_COPY_AND_ASSIGN(Cellular);
307};
308
309} // namespace shill
310
311#endif // SHILL_CELLULAR_