blob: acf5f8b1f1e6bae93fb651a290cdb3197eb57a59 [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"
Paul Stewart26b327e2011-10-19 11:38:09 -070016#include "shill/event_dispatcher.h"
Darin Petkovd9661952011-08-03 16:25:42 -070017#include "shill/modem_cdma_proxy_interface.h"
Darin Petkov975b5e72011-08-30 11:48:08 -070018#include "shill/modem_gsm_card_proxy_interface.h"
Darin Petkova1e0a1c2011-08-25 15:08:33 -070019#include "shill/modem_gsm_network_proxy_interface.h"
Darin Petkovc5f56562011-08-06 16:40:05 -070020#include "shill/modem_proxy_interface.h"
Chris Masone2b105542011-06-22 10:58:09 -070021#include "shill/refptr_types.h"
Chris Masone3bd3c8c2011-06-13 08:20:26 -070022
23namespace shill {
24
Darin Petkov4d6d9412011-08-24 13:19:54 -070025class Error;
Darin Petkove604f702011-07-28 15:51:17 -070026class ModemSimpleProxyInterface;
Darin Petkovab565bb2011-10-06 02:55:51 -070027class ProxyFactory;
Darin Petkove9d12e02011-07-27 15:09:37 -070028
Darin Petkovd9661952011-08-03 16:25:42 -070029class Cellular : public Device,
Darin Petkovc5f56562011-08-06 16:40:05 -070030 public ModemCDMAProxyListener,
Darin Petkov975b5e72011-08-30 11:48:08 -070031 public ModemGSMCardProxyListener,
Darin Petkova1e0a1c2011-08-25 15:08:33 -070032 public ModemGSMNetworkProxyListener,
Darin Petkovc5f56562011-08-06 16:40:05 -070033 public ModemProxyListener {
Chris Masone3bd3c8c2011-06-13 08:20:26 -070034 public:
Darin Petkove9d12e02011-07-27 15:09:37 -070035 enum Type {
36 kTypeGSM,
37 kTypeCDMA
38 };
39
Darin Petkov0828f5f2011-08-11 10:18:52 -070040 // The device states progress linearly from Disabled to Linked.
Darin Petkove9d12e02011-07-27 15:09:37 -070041 enum State {
Darin Petkov0828f5f2011-08-11 10:18:52 -070042 // This is the initial state of the modem and indicates that the modem radio
43 // is not turned on.
Darin Petkove9d12e02011-07-27 15:09:37 -070044 kStateDisabled,
Darin Petkov0828f5f2011-08-11 10:18:52 -070045 // This state indicates that the modem radio is turned on, and it should be
46 // possible to measure signal strength.
Darin Petkove9d12e02011-07-27 15:09:37 -070047 kStateEnabled,
Darin Petkov0828f5f2011-08-11 10:18:52 -070048 // The modem has registered with a network and has signal quality
Darin Petkov51489002011-08-18 13:13:20 -070049 // measurements. A cellular service object is created.
Darin Petkove9d12e02011-07-27 15:09:37 -070050 kStateRegistered,
Darin Petkov0828f5f2011-08-11 10:18:52 -070051 // The modem has connected to a network.
Darin Petkove9d12e02011-07-27 15:09:37 -070052 kStateConnected,
Darin Petkov0828f5f2011-08-11 10:18:52 -070053 // The network interface is UP.
54 kStateLinked,
Darin Petkove9d12e02011-07-27 15:09:37 -070055 };
56
Darin Petkovbac96002011-08-09 13:22:00 -070057 // These should be kept in sync with ModemManager's mm-modem.h:MMModemState.
58 enum ModemState {
59 kModemStateUnknown = 0,
60 kModemStateDisabled = 10,
61 kModemStateDisabling = 20,
62 kModemStateEnabling = 30,
63 kModemStateEnabled = 40,
64 kModemStateSearching = 50,
65 kModemStateRegistered = 60,
66 kModemStateDisconnecting = 70,
67 kModemStateConnecting = 80,
68 kModemStateConnected = 90,
69 };
70
Darin Petkov3335b372011-08-22 11:05:32 -070071 class Operator {
72 public:
73 Operator();
74 ~Operator();
75
76 void CopyFrom(const Operator &oper);
77
78 const std::string &GetName() const;
79 void SetName(const std::string &name);
80
81 const std::string &GetCode() const;
82 void SetCode(const std::string &code);
83
84 const std::string &GetCountry() const;
85 void SetCountry(const std::string &country);
86
87 const Stringmap &ToDict() const;
88
89 private:
90 Stringmap dict_;
91
92 DISALLOW_COPY_AND_ASSIGN(Operator);
93 };
94
Chris Masone889666b2011-07-03 12:58:50 -070095 struct SimLockStatus {
96 public:
Darin Petkove9d12e02011-07-27 15:09:37 -070097 SimLockStatus() : retries_left(0) {}
Darin Petkov48a511a2011-09-15 10:33:37 -070098 SimLockStatus(const std::string &in_lock_type, uint32 in_retries_left)
99 : lock_type(in_lock_type),
100 retries_left(in_retries_left) {}
Darin Petkove9d12e02011-07-27 15:09:37 -0700101
Chris Masone889666b2011-07-03 12:58:50 -0700102 std::string lock_type;
103 uint32 retries_left;
104 };
105
Darin Petkovc5f56562011-08-06 16:40:05 -0700106 static const char kConnectPropertyPhoneNumber[];
107
Darin Petkove9d12e02011-07-27 15:09:37 -0700108 // |owner| is the ModemManager DBus service owner (e.g., ":1.17"). |path| is
109 // the ModemManager.Modem DBus object path (e.g.,
110 // "/org/chromium/ModemManager/Gobi/0").
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700111 Cellular(ControlInterface *control_interface,
112 EventDispatcher *dispatcher,
113 Manager *manager,
Darin Petkove9d12e02011-07-27 15:09:37 -0700114 const std::string &link_name,
Chris Masone626719f2011-08-18 16:58:48 -0700115 const std::string &address,
Darin Petkove9d12e02011-07-27 15:09:37 -0700116 int interface_index,
117 Type type,
118 const std::string &owner,
119 const std::string &path);
120 virtual ~Cellular();
121
Darin Petkov4d6d9412011-08-24 13:19:54 -0700122 // Asynchronously connects the modem to the network. Populates |error| on
123 // failure, leaves it unchanged otherwise.
124 void Connect(Error *error);
Darin Petkovc5f56562011-08-06 16:40:05 -0700125
Darin Petkovb100ae72011-08-24 16:19:45 -0700126 // Asynchronously activates the modem. Populates |error| on failure, leaves it
127 // unchanged otherwise.
128 void Activate(const std::string &carrier, Error *error);
Darin Petkovc408e692011-08-17 13:47:15 -0700129
Darin Petkovbac96002011-08-09 13:22:00 -0700130 void set_modem_state(ModemState state) { modem_state_ = state; }
131 ModemState modem_state() const { return modem_state_; }
132
Darin Petkov48a511a2011-09-15 10:33:37 -0700133 const SimLockStatus &sim_lock_status() const { return sim_lock_status_; }
134 void set_sim_lock_status(const SimLockStatus &s) { sim_lock_status_ = s; }
135
136 void SetGSMAccessTechnology(uint32 access_technology);
137
Darin Petkove9d12e02011-07-27 15:09:37 -0700138 // Inherited from Device.
139 virtual void Start();
140 virtual void Stop();
Paul Stewartfdd16072011-09-16 12:41:35 -0700141 virtual bool TechnologyIs(Technology::Identifier type) const;
Darin Petkov0828f5f2011-08-11 10:18:52 -0700142 virtual void LinkEvent(unsigned int flags, unsigned int change);
Darin Petkovc0865312011-09-16 15:31:20 -0700143 virtual void Scan(Error *error);
Darin Petkov9ae310f2011-08-30 15:41:13 -0700144 virtual void RegisterOnNetwork(const std::string &network_id, Error *error);
Darin Petkove42e1012011-08-31 12:35:04 -0700145 virtual void RequirePIN(const std::string &pin, bool require, Error *error);
146 virtual void EnterPIN(const std::string &pin, Error *error);
147 virtual void UnblockPIN(const std::string &unblock_code,
148 const std::string &pin,
149 Error *error);
150 virtual void ChangePIN(const std::string &old_pin,
151 const std::string &new_pin,
152 Error *error);
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700153
154 private:
Darin Petkovab565bb2011-10-06 02:55:51 -0700155 friend class CellularTest;
Darin Petkovc408e692011-08-17 13:47:15 -0700156 FRIEND_TEST(CellularTest, Activate);
Darin Petkov51489002011-08-18 13:13:20 -0700157 FRIEND_TEST(CellularTest, ActivateError);
Darin Petkov3335b372011-08-22 11:05:32 -0700158 FRIEND_TEST(CellularTest, CreateService);
Darin Petkove42e1012011-08-31 12:35:04 -0700159 FRIEND_TEST(CellularTest, ChangePIN);
160 FRIEND_TEST(CellularTest, ChangePINError);
Darin Petkovc5f56562011-08-06 16:40:05 -0700161 FRIEND_TEST(CellularTest, Connect);
Darin Petkovc408e692011-08-17 13:47:15 -0700162 FRIEND_TEST(CellularTest, GetCDMAActivationStateString);
Darin Petkov51489002011-08-18 13:13:20 -0700163 FRIEND_TEST(CellularTest, GetCDMAActivationErrorString);
Darin Petkov975b5e72011-08-30 11:48:08 -0700164 FRIEND_TEST(CellularTest, GetCDMAIdentifiers);
Darin Petkov9bac6fe2011-08-26 12:49:05 -0700165 FRIEND_TEST(CellularTest, GetCDMANetworkTechnologyString);
Darin Petkovbec79a22011-08-01 14:47:17 -0700166 FRIEND_TEST(CellularTest, GetCDMARegistrationState);
Darin Petkov9bac6fe2011-08-26 12:49:05 -0700167 FRIEND_TEST(CellularTest, GetCDMARoamingStateString);
Darin Petkovd9661952011-08-03 16:25:42 -0700168 FRIEND_TEST(CellularTest, GetCDMASignalQuality);
Darin Petkov975b5e72011-08-30 11:48:08 -0700169 FRIEND_TEST(CellularTest, GetGSMIdentifiers);
Darin Petkov9bac6fe2011-08-26 12:49:05 -0700170 FRIEND_TEST(CellularTest, GetGSMNetworkTechnologyString);
171 FRIEND_TEST(CellularTest, GetGSMRoamingStateString);
Darin Petkov22b72bf2011-08-29 14:01:20 -0700172 FRIEND_TEST(CellularTest, GetGSMSignalQuality);
Darin Petkovceb68172011-07-29 14:47:48 -0700173 FRIEND_TEST(CellularTest, GetModemInfo);
174 FRIEND_TEST(CellularTest, GetModemStatus);
Darin Petkove9d12e02011-07-27 15:09:37 -0700175 FRIEND_TEST(CellularTest, GetStateString);
176 FRIEND_TEST(CellularTest, GetTypeString);
Darin Petkove42e1012011-08-31 12:35:04 -0700177 FRIEND_TEST(CellularTest, EnterPIN);
178 FRIEND_TEST(CellularTest, EnterPINError);
Darin Petkovbec79a22011-08-01 14:47:17 -0700179 FRIEND_TEST(CellularTest, InitProxiesCDMA);
180 FRIEND_TEST(CellularTest, InitProxiesGSM);
Darin Petkovc0865312011-09-16 15:31:20 -0700181 FRIEND_TEST(CellularTest, ParseScanResult);
Darin Petkov9ae310f2011-08-30 15:41:13 -0700182 FRIEND_TEST(CellularTest, RegisterOnNetwork);
183 FRIEND_TEST(CellularTest, RegisterOnNetworkError);
Darin Petkove42e1012011-08-31 12:35:04 -0700184 FRIEND_TEST(CellularTest, RequirePIN);
185 FRIEND_TEST(CellularTest, RequirePINError);
Darin Petkov48a511a2011-09-15 10:33:37 -0700186 FRIEND_TEST(CellularTest, SetGSMAccessTechnology);
Darin Petkovc0865312011-09-16 15:31:20 -0700187 FRIEND_TEST(CellularTest, Scan);
Darin Petkovbac96002011-08-09 13:22:00 -0700188 FRIEND_TEST(CellularTest, StartConnected);
Darin Petkova1e0a1c2011-08-25 15:08:33 -0700189 FRIEND_TEST(CellularTest, StartCDMARegister);
Darin Petkov9bac6fe2011-08-26 12:49:05 -0700190 FRIEND_TEST(CellularTest, StartGSMRegister);
Darin Petkov0828f5f2011-08-11 10:18:52 -0700191 FRIEND_TEST(CellularTest, StartLinked);
Darin Petkove42e1012011-08-31 12:35:04 -0700192 FRIEND_TEST(CellularTest, UnblockPIN);
193 FRIEND_TEST(CellularTest, UnblockPINError);
Darin Petkovbac96002011-08-09 13:22:00 -0700194
Darin Petkov9bac6fe2011-08-26 12:49:05 -0700195 struct CDMA {
196 CDMA();
197
198 uint32 registration_state_evdo;
199 uint32 registration_state_1x;
200 uint32 activation_state;
201
202 uint16 prl_version;
203 std::string payment_url;
204 std::string usage_url;
205 };
206
207 struct GSM {
208 GSM();
209
210 uint32 registration_state;
211 uint32 access_technology;
212 std::string network_id;
213 std::string operator_name;
Darin Petkov975b5e72011-08-30 11:48:08 -0700214 std::string spn;
Darin Petkov9bac6fe2011-08-26 12:49:05 -0700215 };
216
Darin Petkovc0865312011-09-16 15:31:20 -0700217 static const char kNetworkPropertyAccessTechnology[];
218 static const char kNetworkPropertyID[];
219 static const char kNetworkPropertyLongName[];
220 static const char kNetworkPropertyShortName[];
221 static const char kNetworkPropertyStatus[];
Darin Petkovbac96002011-08-09 13:22:00 -0700222 static const char kPhoneNumberCDMA[];
223 static const char kPhoneNumberGSM[];
Darin Petkove9d12e02011-07-27 15:09:37 -0700224
Darin Petkov0828f5f2011-08-11 10:18:52 -0700225 void SetState(State state);
226
Darin Petkovc5f56562011-08-06 16:40:05 -0700227 void ConnectTask(const DBusPropertiesMap &properties);
Darin Petkovc0865312011-09-16 15:31:20 -0700228 void ScanTask();
Darin Petkovc408e692011-08-17 13:47:15 -0700229 void ActivateTask(const std::string &carrier);
Darin Petkov9ae310f2011-08-30 15:41:13 -0700230 void RegisterOnNetworkTask(const std::string &network_id);
Darin Petkove42e1012011-08-31 12:35:04 -0700231 void RequirePINTask(const std::string &pin, bool require);
232 void EnterPINTask(const std::string &pin);
233 void UnblockPINTask(const std::string &unblock_code, const std::string &pin);
234 void ChangePINTask(const std::string &old_pin, const std::string &new_pin);
Darin Petkovc5f56562011-08-06 16:40:05 -0700235
Darin Petkovbac96002011-08-09 13:22:00 -0700236 // Invoked when the modem is connected to the cellular network to transition
237 // to the network-connected state and bring the network interface up.
238 void EstablishLink();
239
Chris Masone889666b2011-07-03 12:58:50 -0700240 StrIntPair SimLockStatusToProperty();
Darin Petkov48a511a2011-09-15 10:33:37 -0700241
mukesh agrawalffa3d042011-10-06 15:26:10 -0700242 void HelpRegisterDerivedStrIntPair(
243 const std::string &name,
244 StrIntPair(Cellular::*get)(void),
245 void(Cellular::*set)(const StrIntPair&, Error *));
Chris Masone889666b2011-07-03 12:58:50 -0700246
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 Petkovab565bb2011-10-06 02:55:51 -0700317 // Store cached copies of singletons for speed/ease of testing.
318 ProxyFactory *proxy_factory_;
319
Darin Petkove9d12e02011-07-27 15:09:37 -0700320 Type type_;
321 State state_;
Darin Petkovbac96002011-08-09 13:22:00 -0700322 ModemState modem_state_;
Darin Petkove9d12e02011-07-27 15:09:37 -0700323
324 const std::string dbus_owner_; // ModemManager.Modem
325 const std::string dbus_path_; // ModemManager.Modem
326 scoped_ptr<ModemProxyInterface> proxy_;
Darin Petkove604f702011-07-28 15:51:17 -0700327 scoped_ptr<ModemSimpleProxyInterface> simple_proxy_;
Darin Petkovbec79a22011-08-01 14:47:17 -0700328 scoped_ptr<ModemCDMAProxyInterface> cdma_proxy_;
Darin Petkov975b5e72011-08-30 11:48:08 -0700329 scoped_ptr<ModemGSMCardProxyInterface> gsm_card_proxy_;
Darin Petkova1e0a1c2011-08-25 15:08:33 -0700330 scoped_ptr<ModemGSMNetworkProxyInterface> gsm_network_proxy_;
Darin Petkovbec79a22011-08-01 14:47:17 -0700331
332 CDMA cdma_;
Darin Petkov9bac6fe2011-08-26 12:49:05 -0700333 GSM gsm_;
Darin Petkove9d12e02011-07-27 15:09:37 -0700334
Darin Petkovd9661952011-08-03 16:25:42 -0700335 CellularServiceRefPtr service_;
Chris Masone853b81b2011-06-24 14:11:41 -0700336
Darin Petkovc5f56562011-08-06 16:40:05 -0700337 ScopedRunnableMethodFactory<Cellular> task_factory_;
338
Chris Masoneb925cc82011-06-22 15:39:57 -0700339 // Properties
340 bool allow_roaming_;
341 std::string carrier_;
342 std::string meid_;
343 std::string imei_;
344 std::string imsi_;
345 std::string esn_;
346 std::string mdn_;
347 std::string min_;
348 std::string model_id_;
349 std::string manufacturer_;
350 std::string firmware_revision_;
351 std::string hardware_revision_;
Chris Masoneb925cc82011-06-22 15:39:57 -0700352 bool scanning_;
353 uint16 scan_interval_;
Darin Petkova1e0a1c2011-08-25 15:08:33 -0700354 std::string selected_network_;
Darin Petkovc0865312011-09-16 15:31:20 -0700355 Stringmaps found_networks_;
Chris Masone889666b2011-07-03 12:58:50 -0700356 SimLockStatus sim_lock_status_;
Darin Petkov3335b372011-08-22 11:05:32 -0700357 Operator home_provider_;
Chris Masoneb925cc82011-06-22 15:39:57 -0700358
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700359 DISALLOW_COPY_AND_ASSIGN(Cellular);
360};
361
362} // namespace shill
363
364#endif // SHILL_CELLULAR_