blob: 2341dc9fe36fe8c728b029f6f4e0266cbabfd655 [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 Petkov975b5e72011-08-30 11:48:08 -070017#include "shill/modem_gsm_card_proxy_interface.h"
Darin Petkova1e0a1c2011-08-25 15:08:33 -070018#include "shill/modem_gsm_network_proxy_interface.h"
Darin Petkovc5f56562011-08-06 16:40:05 -070019#include "shill/modem_proxy_interface.h"
Chris Masone2b105542011-06-22 10:58:09 -070020#include "shill/refptr_types.h"
Chris Masone3bd3c8c2011-06-13 08:20:26 -070021#include "shill/shill_event.h"
22
23namespace shill {
24
Darin Petkov4d6d9412011-08-24 13:19:54 -070025class Error;
Darin Petkove604f702011-07-28 15:51:17 -070026class ModemSimpleProxyInterface;
Darin Petkove9d12e02011-07-27 15:09:37 -070027
Darin Petkovd9661952011-08-03 16:25:42 -070028class Cellular : public Device,
Darin Petkovc5f56562011-08-06 16:40:05 -070029 public ModemCDMAProxyListener,
Darin Petkov975b5e72011-08-30 11:48:08 -070030 public ModemGSMCardProxyListener,
Darin Petkova1e0a1c2011-08-25 15:08:33 -070031 public ModemGSMNetworkProxyListener,
Darin Petkovc5f56562011-08-06 16:40:05 -070032 public ModemProxyListener {
Chris Masone3bd3c8c2011-06-13 08:20:26 -070033 public:
Darin Petkove9d12e02011-07-27 15:09:37 -070034 enum Type {
35 kTypeGSM,
36 kTypeCDMA
37 };
38
Darin Petkov0828f5f2011-08-11 10:18:52 -070039 // The device states progress linearly from Disabled to Linked.
Darin Petkove9d12e02011-07-27 15:09:37 -070040 enum State {
Darin Petkov0828f5f2011-08-11 10:18:52 -070041 // This is the initial state of the modem and indicates that the modem radio
42 // is not turned on.
Darin Petkove9d12e02011-07-27 15:09:37 -070043 kStateDisabled,
Darin Petkov0828f5f2011-08-11 10:18:52 -070044 // This state indicates that the modem radio is turned on, and it should be
45 // possible to measure signal strength.
Darin Petkove9d12e02011-07-27 15:09:37 -070046 kStateEnabled,
Darin Petkov0828f5f2011-08-11 10:18:52 -070047 // The modem has registered with a network and has signal quality
Darin Petkov51489002011-08-18 13:13:20 -070048 // measurements. A cellular service object is created.
Darin Petkove9d12e02011-07-27 15:09:37 -070049 kStateRegistered,
Darin Petkov0828f5f2011-08-11 10:18:52 -070050 // The modem has connected to a network.
Darin Petkove9d12e02011-07-27 15:09:37 -070051 kStateConnected,
Darin Petkov0828f5f2011-08-11 10:18:52 -070052 // The network interface is UP.
53 kStateLinked,
Darin Petkove9d12e02011-07-27 15:09:37 -070054 };
55
Darin Petkovbac96002011-08-09 13:22:00 -070056 // These should be kept in sync with ModemManager's mm-modem.h:MMModemState.
57 enum ModemState {
58 kModemStateUnknown = 0,
59 kModemStateDisabled = 10,
60 kModemStateDisabling = 20,
61 kModemStateEnabling = 30,
62 kModemStateEnabled = 40,
63 kModemStateSearching = 50,
64 kModemStateRegistered = 60,
65 kModemStateDisconnecting = 70,
66 kModemStateConnecting = 80,
67 kModemStateConnected = 90,
68 };
69
Darin Petkov3335b372011-08-22 11:05:32 -070070 class Operator {
71 public:
72 Operator();
73 ~Operator();
74
75 void CopyFrom(const Operator &oper);
76
77 const std::string &GetName() const;
78 void SetName(const std::string &name);
79
80 const std::string &GetCode() const;
81 void SetCode(const std::string &code);
82
83 const std::string &GetCountry() const;
84 void SetCountry(const std::string &country);
85
86 const Stringmap &ToDict() const;
87
88 private:
89 Stringmap dict_;
90
91 DISALLOW_COPY_AND_ASSIGN(Operator);
92 };
93
Chris Masone889666b2011-07-03 12:58:50 -070094 class Network {
95 public:
96 Network();
Darin Petkove9d12e02011-07-27 15:09:37 -070097 ~Network();
Chris Masone889666b2011-07-03 12:58:50 -070098
99 const std::string &GetStatus() const;
100 void SetStatus(const std::string &status);
101
102 const std::string &GetId() const;
103 void SetId(const std::string &id);
104
105 const std::string &GetShortName() const;
106 void SetShortName(const std::string &name);
107
108 const std::string &GetLongName() const;
109 void SetLongName(const std::string &name);
110
111 const std::string &GetTechnology() const;
112 void SetTechnology(const std::string &technology);
113
114 const Stringmap &ToDict() const;
115
116 private:
117 Stringmap dict_;
Darin Petkove9d12e02011-07-27 15:09:37 -0700118
Chris Masone889666b2011-07-03 12:58:50 -0700119 DISALLOW_COPY_AND_ASSIGN(Network);
120 };
121
122 struct SimLockStatus {
123 public:
Darin Petkove9d12e02011-07-27 15:09:37 -0700124 SimLockStatus() : retries_left(0) {}
125
Chris Masone889666b2011-07-03 12:58:50 -0700126 std::string lock_type;
127 uint32 retries_left;
128 };
129
Darin Petkovc5f56562011-08-06 16:40:05 -0700130 static const char kConnectPropertyPhoneNumber[];
131
Darin Petkove9d12e02011-07-27 15:09:37 -0700132 // |owner| is the ModemManager DBus service owner (e.g., ":1.17"). |path| is
133 // the ModemManager.Modem DBus object path (e.g.,
134 // "/org/chromium/ModemManager/Gobi/0").
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700135 Cellular(ControlInterface *control_interface,
136 EventDispatcher *dispatcher,
137 Manager *manager,
Darin Petkove9d12e02011-07-27 15:09:37 -0700138 const std::string &link_name,
Chris Masone626719f2011-08-18 16:58:48 -0700139 const std::string &address,
Darin Petkove9d12e02011-07-27 15:09:37 -0700140 int interface_index,
141 Type type,
142 const std::string &owner,
143 const std::string &path);
144 virtual ~Cellular();
145
Darin Petkov4d6d9412011-08-24 13:19:54 -0700146 // Asynchronously connects the modem to the network. Populates |error| on
147 // failure, leaves it unchanged otherwise.
148 void Connect(Error *error);
Darin Petkovc5f56562011-08-06 16:40:05 -0700149
Darin Petkovb100ae72011-08-24 16:19:45 -0700150 // Asynchronously activates the modem. Populates |error| on failure, leaves it
151 // unchanged otherwise.
152 void Activate(const std::string &carrier, Error *error);
Darin Petkovc408e692011-08-17 13:47:15 -0700153
Darin Petkovbac96002011-08-09 13:22:00 -0700154 void set_modem_state(ModemState state) { modem_state_ = state; }
155 ModemState modem_state() const { return modem_state_; }
156
Darin Petkove9d12e02011-07-27 15:09:37 -0700157 // Inherited from Device.
158 virtual void Start();
159 virtual void Stop();
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700160 virtual bool TechnologyIs(Technology type) const;
Darin Petkov0828f5f2011-08-11 10:18:52 -0700161 virtual void LinkEvent(unsigned int flags, unsigned int change);
Darin Petkov9ae310f2011-08-30 15:41:13 -0700162 virtual void RegisterOnNetwork(const std::string &network_id, Error *error);
Darin Petkove42e1012011-08-31 12:35:04 -0700163 virtual void RequirePIN(const std::string &pin, bool require, Error *error);
164 virtual void EnterPIN(const std::string &pin, Error *error);
165 virtual void UnblockPIN(const std::string &unblock_code,
166 const std::string &pin,
167 Error *error);
168 virtual void ChangePIN(const std::string &old_pin,
169 const std::string &new_pin,
170 Error *error);
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700171
172 private:
Darin Petkovc408e692011-08-17 13:47:15 -0700173 FRIEND_TEST(CellularTest, Activate);
Darin Petkov51489002011-08-18 13:13:20 -0700174 FRIEND_TEST(CellularTest, ActivateError);
Darin Petkov3335b372011-08-22 11:05:32 -0700175 FRIEND_TEST(CellularTest, CreateService);
Darin Petkove42e1012011-08-31 12:35:04 -0700176 FRIEND_TEST(CellularTest, ChangePIN);
177 FRIEND_TEST(CellularTest, ChangePINError);
Darin Petkovc5f56562011-08-06 16:40:05 -0700178 FRIEND_TEST(CellularTest, Connect);
Darin Petkovc408e692011-08-17 13:47:15 -0700179 FRIEND_TEST(CellularTest, GetCDMAActivationStateString);
Darin Petkov51489002011-08-18 13:13:20 -0700180 FRIEND_TEST(CellularTest, GetCDMAActivationErrorString);
Darin Petkov975b5e72011-08-30 11:48:08 -0700181 FRIEND_TEST(CellularTest, GetCDMAIdentifiers);
Darin Petkov9bac6fe2011-08-26 12:49:05 -0700182 FRIEND_TEST(CellularTest, GetCDMANetworkTechnologyString);
Darin Petkovbec79a22011-08-01 14:47:17 -0700183 FRIEND_TEST(CellularTest, GetCDMARegistrationState);
Darin Petkov9bac6fe2011-08-26 12:49:05 -0700184 FRIEND_TEST(CellularTest, GetCDMARoamingStateString);
Darin Petkovd9661952011-08-03 16:25:42 -0700185 FRIEND_TEST(CellularTest, GetCDMASignalQuality);
Darin Petkov975b5e72011-08-30 11:48:08 -0700186 FRIEND_TEST(CellularTest, GetGSMIdentifiers);
Darin Petkov9bac6fe2011-08-26 12:49:05 -0700187 FRIEND_TEST(CellularTest, GetGSMNetworkTechnologyString);
188 FRIEND_TEST(CellularTest, GetGSMRoamingStateString);
Darin Petkov22b72bf2011-08-29 14:01:20 -0700189 FRIEND_TEST(CellularTest, GetGSMSignalQuality);
Darin Petkovceb68172011-07-29 14:47:48 -0700190 FRIEND_TEST(CellularTest, GetModemInfo);
191 FRIEND_TEST(CellularTest, GetModemStatus);
Darin Petkove9d12e02011-07-27 15:09:37 -0700192 FRIEND_TEST(CellularTest, GetStateString);
193 FRIEND_TEST(CellularTest, GetTypeString);
Darin Petkove42e1012011-08-31 12:35:04 -0700194 FRIEND_TEST(CellularTest, EnterPIN);
195 FRIEND_TEST(CellularTest, EnterPINError);
Darin Petkovbec79a22011-08-01 14:47:17 -0700196 FRIEND_TEST(CellularTest, InitProxiesCDMA);
197 FRIEND_TEST(CellularTest, InitProxiesGSM);
Darin Petkov9ae310f2011-08-30 15:41:13 -0700198 FRIEND_TEST(CellularTest, RegisterOnNetwork);
199 FRIEND_TEST(CellularTest, RegisterOnNetworkError);
Darin Petkove42e1012011-08-31 12:35:04 -0700200 FRIEND_TEST(CellularTest, RequirePIN);
201 FRIEND_TEST(CellularTest, RequirePINError);
Darin Petkovbac96002011-08-09 13:22:00 -0700202 FRIEND_TEST(CellularTest, StartConnected);
Darin Petkova1e0a1c2011-08-25 15:08:33 -0700203 FRIEND_TEST(CellularTest, StartCDMARegister);
Darin Petkov9bac6fe2011-08-26 12:49:05 -0700204 FRIEND_TEST(CellularTest, StartGSMRegister);
Darin Petkov0828f5f2011-08-11 10:18:52 -0700205 FRIEND_TEST(CellularTest, StartLinked);
Darin Petkove42e1012011-08-31 12:35:04 -0700206 FRIEND_TEST(CellularTest, UnblockPIN);
207 FRIEND_TEST(CellularTest, UnblockPINError);
Darin Petkovbac96002011-08-09 13:22:00 -0700208
Darin Petkov9bac6fe2011-08-26 12:49:05 -0700209 struct CDMA {
210 CDMA();
211
212 uint32 registration_state_evdo;
213 uint32 registration_state_1x;
214 uint32 activation_state;
215
216 uint16 prl_version;
217 std::string payment_url;
218 std::string usage_url;
219 };
220
221 struct GSM {
222 GSM();
223
224 uint32 registration_state;
225 uint32 access_technology;
226 std::string network_id;
227 std::string operator_name;
Darin Petkov975b5e72011-08-30 11:48:08 -0700228 std::string spn;
Darin Petkov9bac6fe2011-08-26 12:49:05 -0700229 };
230
Darin Petkovbac96002011-08-09 13:22:00 -0700231 static const char kPhoneNumberCDMA[];
232 static const char kPhoneNumberGSM[];
Darin Petkove9d12e02011-07-27 15:09:37 -0700233
Darin Petkov0828f5f2011-08-11 10:18:52 -0700234 void SetState(State state);
235
Darin Petkovc5f56562011-08-06 16:40:05 -0700236 void ConnectTask(const DBusPropertiesMap &properties);
Darin Petkovc408e692011-08-17 13:47:15 -0700237 void ActivateTask(const std::string &carrier);
Darin Petkov9ae310f2011-08-30 15:41:13 -0700238 void RegisterOnNetworkTask(const std::string &network_id);
Darin Petkove42e1012011-08-31 12:35:04 -0700239 void RequirePINTask(const std::string &pin, bool require);
240 void EnterPINTask(const std::string &pin);
241 void UnblockPINTask(const std::string &unblock_code, const std::string &pin);
242 void ChangePINTask(const std::string &old_pin, const std::string &new_pin);
Darin Petkovc5f56562011-08-06 16:40:05 -0700243
Darin Petkovbac96002011-08-09 13:22:00 -0700244 // Invoked when the modem is connected to the cellular network to transition
245 // to the network-connected state and bring the network interface up.
246 void EstablishLink();
247
Chris Masone889666b2011-07-03 12:58:50 -0700248 Stringmaps EnumerateNetworks();
249 StrIntPair SimLockStatusToProperty();
250 void HelpRegisterDerivedStringmaps(const std::string &name,
251 Stringmaps(Cellular::*get)(void),
252 bool(Cellular::*set)(const Stringmaps&));
253 void HelpRegisterDerivedStrIntPair(const std::string &name,
254 StrIntPair(Cellular::*get)(void),
255 bool(Cellular::*set)(const StrIntPair&));
256
Darin Petkovbec79a22011-08-01 14:47:17 -0700257 void InitProxies();
258
Darin Petkovcc044422011-08-17 13:30:06 -0700259 std::string GetTypeString() const;
260 static std::string GetStateString(State state);
Darin Petkove9d12e02011-07-27 15:09:37 -0700261
Darin Petkovd2045802011-08-23 11:09:25 -0700262 // Returns an empty string if the network technology is unknown.
263 std::string GetNetworkTechnologyString() const;
264
265 std::string GetRoamingStateString() const;
266
Darin Petkovc408e692011-08-17 13:47:15 -0700267 static std::string GetCDMAActivationStateString(uint32 state);
Darin Petkov51489002011-08-18 13:13:20 -0700268 static std::string GetCDMAActivationErrorString(uint32 error);
Darin Petkovc408e692011-08-17 13:47:15 -0700269
Darin Petkovf5f61e02011-07-29 11:35:40 -0700270 void EnableModem();
271 void GetModemStatus();
Darin Petkovceb68172011-07-29 14:47:48 -0700272 void GetGSMProperties();
273 void RegisterGSMModem();
Darin Petkovceb68172011-07-29 14:47:48 -0700274
275 // Obtains the modem identifiers: MEID for CDMA; IMEI, IMSI, SPN and MSISDN
276 // for GSM.
277 void GetModemIdentifiers();
Darin Petkov975b5e72011-08-30 11:48:08 -0700278 void GetCDMAIdentifiers();
279 void GetGSMIdentifiers();
Darin Petkovceb68172011-07-29 14:47:48 -0700280
Darin Petkovd9661952011-08-03 16:25:42 -0700281 // Obtains modem's manufacturer, model ID, and hardware revision.
Darin Petkovceb68172011-07-29 14:47:48 -0700282 void GetModemInfo();
Darin Petkovf5f61e02011-07-29 11:35:40 -0700283
Darin Petkovbec79a22011-08-01 14:47:17 -0700284 void GetModemRegistrationState();
285 void GetCDMARegistrationState();
286 void GetGSMRegistrationState();
287
Darin Petkovd9661952011-08-03 16:25:42 -0700288 // Processes a change in the modem registration state, possibly creating,
289 // destroying or updating the CellularService.
290 void HandleNewRegistrationState();
Darin Petkov0828f5f2011-08-11 10:18:52 -0700291 void HandleNewRegistrationStateTask();
Darin Petkovd9661952011-08-03 16:25:42 -0700292
293 void CreateService();
294
295 void GetModemSignalQuality();
296 uint32 GetCDMASignalQuality();
297 uint32 GetGSMSignalQuality();
298
299 void HandleNewSignalQuality(uint32 strength);
300
Darin Petkovc408e692011-08-17 13:47:15 -0700301 void HandleNewCDMAActivationState(uint32 error);
302
Darin Petkovc5f56562011-08-06 16:40:05 -0700303 // Signal callbacks inherited from ModemCDMAProxyListener.
Darin Petkovb27e5442011-08-16 14:36:45 -0700304 virtual void OnCDMAActivationStateChanged(
305 uint32 activation_state,
306 uint32 activation_error,
307 const DBusPropertiesMap &status_changes);
Darin Petkovd9661952011-08-03 16:25:42 -0700308 virtual void OnCDMARegistrationStateChanged(uint32 state_1x,
309 uint32 state_evdo);
310 virtual void OnCDMASignalQualityChanged(uint32 strength);
311
Darin Petkova1e0a1c2011-08-25 15:08:33 -0700312 // Signal callbacks inherited from ModemGSMNetworkProxyListener.
313 virtual void OnGSMNetworkModeChanged(uint32 mode);
314 virtual void OnGSMRegistrationInfoChanged(uint32 status,
315 const std::string &operator_code,
316 const std::string &operator_name);
317 virtual void OnGSMSignalQualityChanged(uint32 quality);
318
Darin Petkovc5f56562011-08-06 16:40:05 -0700319 // Signal callbacks inherited from ModemProxyListener.
320 virtual void OnModemStateChanged(uint32 old_state,
321 uint32 new_state,
322 uint32 reason);
323
Darin Petkove9d12e02011-07-27 15:09:37 -0700324 Type type_;
325 State state_;
Darin Petkovbac96002011-08-09 13:22:00 -0700326 ModemState modem_state_;
Darin Petkove9d12e02011-07-27 15:09:37 -0700327
328 const std::string dbus_owner_; // ModemManager.Modem
329 const std::string dbus_path_; // ModemManager.Modem
330 scoped_ptr<ModemProxyInterface> proxy_;
Darin Petkove604f702011-07-28 15:51:17 -0700331 scoped_ptr<ModemSimpleProxyInterface> simple_proxy_;
Darin Petkovbec79a22011-08-01 14:47:17 -0700332 scoped_ptr<ModemCDMAProxyInterface> cdma_proxy_;
Darin Petkov975b5e72011-08-30 11:48:08 -0700333 scoped_ptr<ModemGSMCardProxyInterface> gsm_card_proxy_;
Darin Petkova1e0a1c2011-08-25 15:08:33 -0700334 scoped_ptr<ModemGSMNetworkProxyInterface> gsm_network_proxy_;
Darin Petkovbec79a22011-08-01 14:47:17 -0700335
336 CDMA cdma_;
Darin Petkov9bac6fe2011-08-26 12:49:05 -0700337 GSM gsm_;
Darin Petkove9d12e02011-07-27 15:09:37 -0700338
Darin Petkovd9661952011-08-03 16:25:42 -0700339 CellularServiceRefPtr service_;
Chris Masone853b81b2011-06-24 14:11:41 -0700340
Darin Petkovc5f56562011-08-06 16:40:05 -0700341 ScopedRunnableMethodFactory<Cellular> task_factory_;
342
Chris Masoneb925cc82011-06-22 15:39:57 -0700343 // Properties
344 bool allow_roaming_;
345 std::string carrier_;
346 std::string meid_;
347 std::string imei_;
348 std::string imsi_;
349 std::string esn_;
350 std::string mdn_;
351 std::string min_;
352 std::string model_id_;
353 std::string manufacturer_;
354 std::string firmware_revision_;
355 std::string hardware_revision_;
Chris Masoneb925cc82011-06-22 15:39:57 -0700356 bool scanning_;
357 uint16 scan_interval_;
Darin Petkova1e0a1c2011-08-25 15:08:33 -0700358 std::string selected_network_;
Chris Masone889666b2011-07-03 12:58:50 -0700359 std::vector<Network> found_networks_;
360 SimLockStatus sim_lock_status_;
Darin Petkov3335b372011-08-22 11:05:32 -0700361 Operator home_provider_;
Chris Masoneb925cc82011-06-22 15:39:57 -0700362
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700363 DISALLOW_COPY_AND_ASSIGN(Cellular);
364};
365
366} // namespace shill
367
368#endif // SHILL_CELLULAR_