blob: 00462385584e38eeac4464c6b79ead66f2e71598 [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 struct SimLockStatus {
95 public:
Darin Petkove9d12e02011-07-27 15:09:37 -070096 SimLockStatus() : retries_left(0) {}
Darin Petkov48a511a2011-09-15 10:33:37 -070097 SimLockStatus(const std::string &in_lock_type, uint32 in_retries_left)
98 : lock_type(in_lock_type),
99 retries_left(in_retries_left) {}
Darin Petkove9d12e02011-07-27 15:09:37 -0700100
Chris Masone889666b2011-07-03 12:58:50 -0700101 std::string lock_type;
102 uint32 retries_left;
103 };
104
Darin Petkovc5f56562011-08-06 16:40:05 -0700105 static const char kConnectPropertyPhoneNumber[];
106
Darin Petkove9d12e02011-07-27 15:09:37 -0700107 // |owner| is the ModemManager DBus service owner (e.g., ":1.17"). |path| is
108 // the ModemManager.Modem DBus object path (e.g.,
109 // "/org/chromium/ModemManager/Gobi/0").
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700110 Cellular(ControlInterface *control_interface,
111 EventDispatcher *dispatcher,
112 Manager *manager,
Darin Petkove9d12e02011-07-27 15:09:37 -0700113 const std::string &link_name,
Chris Masone626719f2011-08-18 16:58:48 -0700114 const std::string &address,
Darin Petkove9d12e02011-07-27 15:09:37 -0700115 int interface_index,
116 Type type,
117 const std::string &owner,
118 const std::string &path);
119 virtual ~Cellular();
120
Darin Petkov4d6d9412011-08-24 13:19:54 -0700121 // Asynchronously connects the modem to the network. Populates |error| on
122 // failure, leaves it unchanged otherwise.
123 void Connect(Error *error);
Darin Petkovc5f56562011-08-06 16:40:05 -0700124
Darin Petkovb100ae72011-08-24 16:19:45 -0700125 // Asynchronously activates the modem. Populates |error| on failure, leaves it
126 // unchanged otherwise.
127 void Activate(const std::string &carrier, Error *error);
Darin Petkovc408e692011-08-17 13:47:15 -0700128
Darin Petkovbac96002011-08-09 13:22:00 -0700129 void set_modem_state(ModemState state) { modem_state_ = state; }
130 ModemState modem_state() const { return modem_state_; }
131
Darin Petkov48a511a2011-09-15 10:33:37 -0700132 const SimLockStatus &sim_lock_status() const { return sim_lock_status_; }
133 void set_sim_lock_status(const SimLockStatus &s) { sim_lock_status_ = s; }
134
135 void SetGSMAccessTechnology(uint32 access_technology);
136
Darin Petkove9d12e02011-07-27 15:09:37 -0700137 // Inherited from Device.
138 virtual void Start();
139 virtual void Stop();
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700140 virtual bool TechnologyIs(Technology type) const;
Darin Petkov0828f5f2011-08-11 10:18:52 -0700141 virtual void LinkEvent(unsigned int flags, unsigned int change);
Darin Petkovc0865312011-09-16 15:31:20 -0700142 virtual void Scan(Error *error);
Darin Petkov9ae310f2011-08-30 15:41:13 -0700143 virtual void RegisterOnNetwork(const std::string &network_id, Error *error);
Darin Petkove42e1012011-08-31 12:35:04 -0700144 virtual void RequirePIN(const std::string &pin, bool require, Error *error);
145 virtual void EnterPIN(const std::string &pin, Error *error);
146 virtual void UnblockPIN(const std::string &unblock_code,
147 const std::string &pin,
148 Error *error);
149 virtual void ChangePIN(const std::string &old_pin,
150 const std::string &new_pin,
151 Error *error);
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700152
153 private:
Darin Petkovc408e692011-08-17 13:47:15 -0700154 FRIEND_TEST(CellularTest, Activate);
Darin Petkov51489002011-08-18 13:13:20 -0700155 FRIEND_TEST(CellularTest, ActivateError);
Darin Petkov3335b372011-08-22 11:05:32 -0700156 FRIEND_TEST(CellularTest, CreateService);
Darin Petkove42e1012011-08-31 12:35:04 -0700157 FRIEND_TEST(CellularTest, ChangePIN);
158 FRIEND_TEST(CellularTest, ChangePINError);
Darin Petkovc5f56562011-08-06 16:40:05 -0700159 FRIEND_TEST(CellularTest, Connect);
Darin Petkovc408e692011-08-17 13:47:15 -0700160 FRIEND_TEST(CellularTest, GetCDMAActivationStateString);
Darin Petkov51489002011-08-18 13:13:20 -0700161 FRIEND_TEST(CellularTest, GetCDMAActivationErrorString);
Darin Petkov975b5e72011-08-30 11:48:08 -0700162 FRIEND_TEST(CellularTest, GetCDMAIdentifiers);
Darin Petkov9bac6fe2011-08-26 12:49:05 -0700163 FRIEND_TEST(CellularTest, GetCDMANetworkTechnologyString);
Darin Petkovbec79a22011-08-01 14:47:17 -0700164 FRIEND_TEST(CellularTest, GetCDMARegistrationState);
Darin Petkov9bac6fe2011-08-26 12:49:05 -0700165 FRIEND_TEST(CellularTest, GetCDMARoamingStateString);
Darin Petkovd9661952011-08-03 16:25:42 -0700166 FRIEND_TEST(CellularTest, GetCDMASignalQuality);
Darin Petkov975b5e72011-08-30 11:48:08 -0700167 FRIEND_TEST(CellularTest, GetGSMIdentifiers);
Darin Petkov9bac6fe2011-08-26 12:49:05 -0700168 FRIEND_TEST(CellularTest, GetGSMNetworkTechnologyString);
169 FRIEND_TEST(CellularTest, GetGSMRoamingStateString);
Darin Petkov22b72bf2011-08-29 14:01:20 -0700170 FRIEND_TEST(CellularTest, GetGSMSignalQuality);
Darin Petkovceb68172011-07-29 14:47:48 -0700171 FRIEND_TEST(CellularTest, GetModemInfo);
172 FRIEND_TEST(CellularTest, GetModemStatus);
Darin Petkove9d12e02011-07-27 15:09:37 -0700173 FRIEND_TEST(CellularTest, GetStateString);
174 FRIEND_TEST(CellularTest, GetTypeString);
Darin Petkove42e1012011-08-31 12:35:04 -0700175 FRIEND_TEST(CellularTest, EnterPIN);
176 FRIEND_TEST(CellularTest, EnterPINError);
Darin Petkovbec79a22011-08-01 14:47:17 -0700177 FRIEND_TEST(CellularTest, InitProxiesCDMA);
178 FRIEND_TEST(CellularTest, InitProxiesGSM);
Darin Petkovc0865312011-09-16 15:31:20 -0700179 FRIEND_TEST(CellularTest, ParseScanResult);
Darin Petkov9ae310f2011-08-30 15:41:13 -0700180 FRIEND_TEST(CellularTest, RegisterOnNetwork);
181 FRIEND_TEST(CellularTest, RegisterOnNetworkError);
Darin Petkove42e1012011-08-31 12:35:04 -0700182 FRIEND_TEST(CellularTest, RequirePIN);
183 FRIEND_TEST(CellularTest, RequirePINError);
Darin Petkov48a511a2011-09-15 10:33:37 -0700184 FRIEND_TEST(CellularTest, SetGSMAccessTechnology);
Darin Petkovc0865312011-09-16 15:31:20 -0700185 FRIEND_TEST(CellularTest, Scan);
Darin Petkovbac96002011-08-09 13:22:00 -0700186 FRIEND_TEST(CellularTest, StartConnected);
Darin Petkova1e0a1c2011-08-25 15:08:33 -0700187 FRIEND_TEST(CellularTest, StartCDMARegister);
Darin Petkov9bac6fe2011-08-26 12:49:05 -0700188 FRIEND_TEST(CellularTest, StartGSMRegister);
Darin Petkov0828f5f2011-08-11 10:18:52 -0700189 FRIEND_TEST(CellularTest, StartLinked);
Darin Petkove42e1012011-08-31 12:35:04 -0700190 FRIEND_TEST(CellularTest, UnblockPIN);
191 FRIEND_TEST(CellularTest, UnblockPINError);
Darin Petkovbac96002011-08-09 13:22:00 -0700192
Darin Petkov9bac6fe2011-08-26 12:49:05 -0700193 struct CDMA {
194 CDMA();
195
196 uint32 registration_state_evdo;
197 uint32 registration_state_1x;
198 uint32 activation_state;
199
200 uint16 prl_version;
201 std::string payment_url;
202 std::string usage_url;
203 };
204
205 struct GSM {
206 GSM();
207
208 uint32 registration_state;
209 uint32 access_technology;
210 std::string network_id;
211 std::string operator_name;
Darin Petkov975b5e72011-08-30 11:48:08 -0700212 std::string spn;
Darin Petkov9bac6fe2011-08-26 12:49:05 -0700213 };
214
Darin Petkovc0865312011-09-16 15:31:20 -0700215 static const char kNetworkPropertyAccessTechnology[];
216 static const char kNetworkPropertyID[];
217 static const char kNetworkPropertyLongName[];
218 static const char kNetworkPropertyShortName[];
219 static const char kNetworkPropertyStatus[];
Darin Petkovbac96002011-08-09 13:22:00 -0700220 static const char kPhoneNumberCDMA[];
221 static const char kPhoneNumberGSM[];
Darin Petkove9d12e02011-07-27 15:09:37 -0700222
Darin Petkov0828f5f2011-08-11 10:18:52 -0700223 void SetState(State state);
224
Darin Petkovc5f56562011-08-06 16:40:05 -0700225 void ConnectTask(const DBusPropertiesMap &properties);
Darin Petkovc0865312011-09-16 15:31:20 -0700226 void ScanTask();
Darin Petkovc408e692011-08-17 13:47:15 -0700227 void ActivateTask(const std::string &carrier);
Darin Petkov9ae310f2011-08-30 15:41:13 -0700228 void RegisterOnNetworkTask(const std::string &network_id);
Darin Petkove42e1012011-08-31 12:35:04 -0700229 void RequirePINTask(const std::string &pin, bool require);
230 void EnterPINTask(const std::string &pin);
231 void UnblockPINTask(const std::string &unblock_code, const std::string &pin);
232 void ChangePINTask(const std::string &old_pin, const std::string &new_pin);
Darin Petkovc5f56562011-08-06 16:40:05 -0700233
Darin Petkovbac96002011-08-09 13:22:00 -0700234 // Invoked when the modem is connected to the cellular network to transition
235 // to the network-connected state and bring the network interface up.
236 void EstablishLink();
237
Chris Masone889666b2011-07-03 12:58:50 -0700238 StrIntPair SimLockStatusToProperty();
Darin Petkov48a511a2011-09-15 10:33:37 -0700239
Chris Masone889666b2011-07-03 12:58:50 -0700240 void HelpRegisterDerivedStringmaps(const std::string &name,
241 Stringmaps(Cellular::*get)(void),
242 bool(Cellular::*set)(const Stringmaps&));
243 void HelpRegisterDerivedStrIntPair(const std::string &name,
244 StrIntPair(Cellular::*get)(void),
245 bool(Cellular::*set)(const StrIntPair&));
246
Darin Petkovbec79a22011-08-01 14:47:17 -0700247 void InitProxies();
248
Darin Petkovcc044422011-08-17 13:30:06 -0700249 std::string GetTypeString() const;
250 static std::string GetStateString(State state);
Darin Petkove9d12e02011-07-27 15:09:37 -0700251
Darin Petkovd2045802011-08-23 11:09:25 -0700252 // Returns an empty string if the network technology is unknown.
253 std::string GetNetworkTechnologyString() const;
254
255 std::string GetRoamingStateString() const;
256
Darin Petkovc408e692011-08-17 13:47:15 -0700257 static std::string GetCDMAActivationStateString(uint32 state);
Darin Petkov51489002011-08-18 13:13:20 -0700258 static std::string GetCDMAActivationErrorString(uint32 error);
Darin Petkovc408e692011-08-17 13:47:15 -0700259
Darin Petkovf5f61e02011-07-29 11:35:40 -0700260 void EnableModem();
261 void GetModemStatus();
Darin Petkovceb68172011-07-29 14:47:48 -0700262 void GetGSMProperties();
263 void RegisterGSMModem();
Darin Petkovceb68172011-07-29 14:47:48 -0700264
265 // Obtains the modem identifiers: MEID for CDMA; IMEI, IMSI, SPN and MSISDN
266 // for GSM.
267 void GetModemIdentifiers();
Darin Petkov975b5e72011-08-30 11:48:08 -0700268 void GetCDMAIdentifiers();
269 void GetGSMIdentifiers();
Darin Petkovceb68172011-07-29 14:47:48 -0700270
Darin Petkovd9661952011-08-03 16:25:42 -0700271 // Obtains modem's manufacturer, model ID, and hardware revision.
Darin Petkovceb68172011-07-29 14:47:48 -0700272 void GetModemInfo();
Darin Petkovf5f61e02011-07-29 11:35:40 -0700273
Darin Petkovbec79a22011-08-01 14:47:17 -0700274 void GetModemRegistrationState();
275 void GetCDMARegistrationState();
276 void GetGSMRegistrationState();
277
Darin Petkovd9661952011-08-03 16:25:42 -0700278 // Processes a change in the modem registration state, possibly creating,
279 // destroying or updating the CellularService.
280 void HandleNewRegistrationState();
Darin Petkov0828f5f2011-08-11 10:18:52 -0700281 void HandleNewRegistrationStateTask();
Darin Petkovd9661952011-08-03 16:25:42 -0700282
283 void CreateService();
284
285 void GetModemSignalQuality();
286 uint32 GetCDMASignalQuality();
287 uint32 GetGSMSignalQuality();
288
289 void HandleNewSignalQuality(uint32 strength);
290
Darin Petkovc408e692011-08-17 13:47:15 -0700291 void HandleNewCDMAActivationState(uint32 error);
292
Darin Petkovc0865312011-09-16 15:31:20 -0700293 Stringmap ParseScanResult(
294 const ModemGSMNetworkProxyInterface::ScanResult &result);
295
Darin Petkovc5f56562011-08-06 16:40:05 -0700296 // Signal callbacks inherited from ModemCDMAProxyListener.
Darin Petkovb27e5442011-08-16 14:36:45 -0700297 virtual void OnCDMAActivationStateChanged(
298 uint32 activation_state,
299 uint32 activation_error,
300 const DBusPropertiesMap &status_changes);
Darin Petkovd9661952011-08-03 16:25:42 -0700301 virtual void OnCDMARegistrationStateChanged(uint32 state_1x,
302 uint32 state_evdo);
303 virtual void OnCDMASignalQualityChanged(uint32 strength);
304
Darin Petkova1e0a1c2011-08-25 15:08:33 -0700305 // Signal callbacks inherited from ModemGSMNetworkProxyListener.
306 virtual void OnGSMNetworkModeChanged(uint32 mode);
307 virtual void OnGSMRegistrationInfoChanged(uint32 status,
308 const std::string &operator_code,
309 const std::string &operator_name);
310 virtual void OnGSMSignalQualityChanged(uint32 quality);
311
Darin Petkovc5f56562011-08-06 16:40:05 -0700312 // Signal callbacks inherited from ModemProxyListener.
313 virtual void OnModemStateChanged(uint32 old_state,
314 uint32 new_state,
315 uint32 reason);
316
Darin Petkove9d12e02011-07-27 15:09:37 -0700317 Type type_;
318 State state_;
Darin Petkovbac96002011-08-09 13:22:00 -0700319 ModemState modem_state_;
Darin Petkove9d12e02011-07-27 15:09:37 -0700320
321 const std::string dbus_owner_; // ModemManager.Modem
322 const std::string dbus_path_; // ModemManager.Modem
323 scoped_ptr<ModemProxyInterface> proxy_;
Darin Petkove604f702011-07-28 15:51:17 -0700324 scoped_ptr<ModemSimpleProxyInterface> simple_proxy_;
Darin Petkovbec79a22011-08-01 14:47:17 -0700325 scoped_ptr<ModemCDMAProxyInterface> cdma_proxy_;
Darin Petkov975b5e72011-08-30 11:48:08 -0700326 scoped_ptr<ModemGSMCardProxyInterface> gsm_card_proxy_;
Darin Petkova1e0a1c2011-08-25 15:08:33 -0700327 scoped_ptr<ModemGSMNetworkProxyInterface> gsm_network_proxy_;
Darin Petkovbec79a22011-08-01 14:47:17 -0700328
329 CDMA cdma_;
Darin Petkov9bac6fe2011-08-26 12:49:05 -0700330 GSM gsm_;
Darin Petkove9d12e02011-07-27 15:09:37 -0700331
Darin Petkovd9661952011-08-03 16:25:42 -0700332 CellularServiceRefPtr service_;
Chris Masone853b81b2011-06-24 14:11:41 -0700333
Darin Petkovc5f56562011-08-06 16:40:05 -0700334 ScopedRunnableMethodFactory<Cellular> task_factory_;
335
Chris Masoneb925cc82011-06-22 15:39:57 -0700336 // Properties
337 bool allow_roaming_;
338 std::string carrier_;
339 std::string meid_;
340 std::string imei_;
341 std::string imsi_;
342 std::string esn_;
343 std::string mdn_;
344 std::string min_;
345 std::string model_id_;
346 std::string manufacturer_;
347 std::string firmware_revision_;
348 std::string hardware_revision_;
Chris Masoneb925cc82011-06-22 15:39:57 -0700349 bool scanning_;
350 uint16 scan_interval_;
Darin Petkova1e0a1c2011-08-25 15:08:33 -0700351 std::string selected_network_;
Darin Petkovc0865312011-09-16 15:31:20 -0700352 Stringmaps found_networks_;
Chris Masone889666b2011-07-03 12:58:50 -0700353 SimLockStatus sim_lock_status_;
Darin Petkov3335b372011-08-22 11:05:32 -0700354 Operator home_provider_;
Chris Masoneb925cc82011-06-22 15:39:57 -0700355
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700356 DISALLOW_COPY_AND_ASSIGN(Cellular);
357};
358
359} // namespace shill
360
361#endif // SHILL_CELLULAR_