blob: c8996ccbb15e017223a47f5f18d6b6bd9447cf60 [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 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
Darin Petkov137884a2011-10-26 18:52:47 +020022struct mobile_provider_db;
23
Chris Masone3bd3c8c2011-06-13 08:20:26 -070024namespace shill {
25
Darin Petkovdaf43862011-10-27 11:37:28 +020026class CellularCapability;
Darin Petkov4d6d9412011-08-24 13:19:54 -070027class Error;
Darin Petkove604f702011-07-28 15:51:17 -070028class ModemSimpleProxyInterface;
Darin Petkovab565bb2011-10-06 02:55:51 -070029class ProxyFactory;
Darin Petkove9d12e02011-07-27 15:09:37 -070030
Darin Petkovd9661952011-08-03 16:25:42 -070031class Cellular : public Device,
Darin Petkov580c7af2011-10-24 12:32:50 +020032 public ModemCDMAProxyDelegate,
Darin Petkov580c7af2011-10-24 12:32:50 +020033 public ModemGSMNetworkProxyDelegate,
34 public ModemProxyDelegate {
Chris Masone3bd3c8c2011-06-13 08:20:26 -070035 public:
Darin Petkove9d12e02011-07-27 15:09:37 -070036 enum Type {
37 kTypeGSM,
38 kTypeCDMA
39 };
40
Darin Petkov0828f5f2011-08-11 10:18:52 -070041 // The device states progress linearly from Disabled to Linked.
Darin Petkove9d12e02011-07-27 15:09:37 -070042 enum State {
Darin Petkov0828f5f2011-08-11 10:18:52 -070043 // This is the initial state of the modem and indicates that the modem radio
44 // is not turned on.
Darin Petkove9d12e02011-07-27 15:09:37 -070045 kStateDisabled,
Darin Petkov0828f5f2011-08-11 10:18:52 -070046 // This state indicates that the modem radio is turned on, and it should be
47 // possible to measure signal strength.
Darin Petkove9d12e02011-07-27 15:09:37 -070048 kStateEnabled,
Darin Petkov0828f5f2011-08-11 10:18:52 -070049 // The modem has registered with a network and has signal quality
Darin Petkov51489002011-08-18 13:13:20 -070050 // measurements. A cellular service object is created.
Darin Petkove9d12e02011-07-27 15:09:37 -070051 kStateRegistered,
Darin Petkov0828f5f2011-08-11 10:18:52 -070052 // The modem has connected to a network.
Darin Petkove9d12e02011-07-27 15:09:37 -070053 kStateConnected,
Darin Petkov0828f5f2011-08-11 10:18:52 -070054 // The network interface is UP.
55 kStateLinked,
Darin Petkove9d12e02011-07-27 15:09:37 -070056 };
57
Darin Petkovbac96002011-08-09 13:22:00 -070058 // These should be kept in sync with ModemManager's mm-modem.h:MMModemState.
59 enum ModemState {
60 kModemStateUnknown = 0,
61 kModemStateDisabled = 10,
62 kModemStateDisabling = 20,
63 kModemStateEnabling = 30,
64 kModemStateEnabled = 40,
65 kModemStateSearching = 50,
66 kModemStateRegistered = 60,
67 kModemStateDisconnecting = 70,
68 kModemStateConnecting = 80,
69 kModemStateConnected = 90,
70 };
71
Darin Petkov3335b372011-08-22 11:05:32 -070072 class Operator {
73 public:
74 Operator();
75 ~Operator();
76
77 void CopyFrom(const Operator &oper);
78
79 const std::string &GetName() const;
80 void SetName(const std::string &name);
81
82 const std::string &GetCode() const;
83 void SetCode(const std::string &code);
84
85 const std::string &GetCountry() const;
86 void SetCountry(const std::string &country);
87
88 const Stringmap &ToDict() const;
89
90 private:
91 Stringmap dict_;
92
93 DISALLOW_COPY_AND_ASSIGN(Operator);
94 };
95
Chris Masone889666b2011-07-03 12:58:50 -070096 struct SimLockStatus {
97 public:
Darin Petkove9d12e02011-07-27 15:09:37 -070098 SimLockStatus() : retries_left(0) {}
Darin Petkov48a511a2011-09-15 10:33:37 -070099 SimLockStatus(const std::string &in_lock_type, uint32 in_retries_left)
100 : lock_type(in_lock_type),
101 retries_left(in_retries_left) {}
Darin Petkove9d12e02011-07-27 15:09:37 -0700102
Chris Masone889666b2011-07-03 12:58:50 -0700103 std::string lock_type;
104 uint32 retries_left;
105 };
106
Darin Petkovc5f56562011-08-06 16:40:05 -0700107 static const char kConnectPropertyPhoneNumber[];
108
Darin Petkove9d12e02011-07-27 15:09:37 -0700109 // |owner| is the ModemManager DBus service owner (e.g., ":1.17"). |path| is
110 // the ModemManager.Modem DBus object path (e.g.,
111 // "/org/chromium/ModemManager/Gobi/0").
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700112 Cellular(ControlInterface *control_interface,
113 EventDispatcher *dispatcher,
114 Manager *manager,
Darin Petkove9d12e02011-07-27 15:09:37 -0700115 const std::string &link_name,
Chris Masone626719f2011-08-18 16:58:48 -0700116 const std::string &address,
Darin Petkove9d12e02011-07-27 15:09:37 -0700117 int interface_index,
118 Type type,
119 const std::string &owner,
Darin Petkov137884a2011-10-26 18:52:47 +0200120 const std::string &path,
121 mobile_provider_db *provider_db);
Darin Petkove9d12e02011-07-27 15:09:37 -0700122 virtual ~Cellular();
123
Darin Petkov4d6d9412011-08-24 13:19:54 -0700124 // Asynchronously connects the modem to the network. Populates |error| on
125 // failure, leaves it unchanged otherwise.
126 void Connect(Error *error);
Darin Petkovc5f56562011-08-06 16:40:05 -0700127
Darin Petkovb100ae72011-08-24 16:19:45 -0700128 // Asynchronously activates the modem. Populates |error| on failure, leaves it
129 // unchanged otherwise.
130 void Activate(const std::string &carrier, Error *error);
Darin Petkovc408e692011-08-17 13:47:15 -0700131
Darin Petkov3e509242011-11-10 14:46:44 +0100132 const CellularServiceRefPtr &service() const { return service_; }
133
Darin Petkova3d3be52011-11-14 21:34:16 +0100134 static std::string GetStateString(State state);
135
136 State state() const { return state_; }
137
Darin Petkovbac96002011-08-09 13:22:00 -0700138 void set_modem_state(ModemState state) { modem_state_ = state; }
139 ModemState modem_state() const { return modem_state_; }
140
Darin Petkov48a511a2011-09-15 10:33:37 -0700141 const SimLockStatus &sim_lock_status() const { return sim_lock_status_; }
142 void set_sim_lock_status(const SimLockStatus &s) { sim_lock_status_ = s; }
143
Darin Petkov1272a432011-11-10 15:53:37 +0100144 mobile_provider_db *provider_db() const { return provider_db_; }
145
Darin Petkovdaf43862011-10-27 11:37:28 +0200146 const std::string &dbus_owner() const { return dbus_owner_; }
147 const std::string &dbus_path() const { return dbus_path_; }
148
Darin Petkov20c13ec2011-11-09 15:07:15 +0100149 uint32 gsm_registration_state() const { return gsm_.registration_state; }
150 uint32 gsm_access_technology() const { return gsm_.access_technology; }
151
152 uint32 cdma_registration_state_evdo() const {
153 return cdma_.registration_state_evdo;
154 }
155 uint32 cdma_registration_state_1x() const {
156 return cdma_.registration_state_1x;
157 }
158
Darin Petkova3d3be52011-11-14 21:34:16 +0100159 uint32 cdma_activation_state() const { return cdma_.activation_state; }
160 void set_cdma_activation_state(uint32 state) {
161 cdma_.activation_state = state;
162 }
163
Darin Petkovcb547732011-11-09 13:55:26 +0100164 const std::string &meid() const { return meid_; }
165 void set_meid(const std::string &meid) { meid_ = meid; }
166
167 const std::string &imei() const { return imei_; }
168 void set_imei(const std::string &imei) { imei_ = imei; }
169
170 const std::string &imsi() const { return imsi_; }
171 void set_imsi(const std::string &imsi) { imsi_ = imsi; }
172
173 const std::string &spn() const { return gsm_.spn; }
174 void set_spn(const std::string &spn) { gsm_.spn = spn; }
175
176 const std::string &mdn() const { return mdn_; }
177 void set_mdn(const std::string &mdn) { mdn_ = mdn; }
178
Darin Petkovdaf43862011-10-27 11:37:28 +0200179 ProxyFactory *proxy_factory() const { return proxy_factory_; }
180
Darin Petkovb05315f2011-11-07 10:14:25 +0100181 ModemCDMAProxyInterface *modem_cdma_proxy() const {
182 return cdma_proxy_.get();
183 }
Darin Petkovdaf43862011-10-27 11:37:28 +0200184 void set_modem_cdma_proxy(ModemCDMAProxyInterface *proxy) {
185 cdma_proxy_.reset(proxy);
186 }
Darin Petkovb05315f2011-11-07 10:14:25 +0100187 ModemGSMNetworkProxyInterface *modem_gsm_network_proxy() const {
188 return gsm_network_proxy_.get();
189 }
Darin Petkovdaf43862011-10-27 11:37:28 +0200190 void set_modem_gsm_network_proxy(ModemGSMNetworkProxyInterface *proxy) {
191 gsm_network_proxy_.reset(proxy);
192 }
193
Darin Petkov48a511a2011-09-15 10:33:37 -0700194 void SetGSMAccessTechnology(uint32 access_technology);
195
Darin Petkov3e509242011-11-10 14:46:44 +0100196 void HandleNewSignalQuality(uint32 strength);
197
Darin Petkova3d3be52011-11-14 21:34:16 +0100198 void HandleNewCDMAActivationState(uint32 error);
199
Darin Petkove9d12e02011-07-27 15:09:37 -0700200 // Inherited from Device.
201 virtual void Start();
202 virtual void Stop();
Paul Stewartfdd16072011-09-16 12:41:35 -0700203 virtual bool TechnologyIs(Technology::Identifier type) const;
Darin Petkov0828f5f2011-08-11 10:18:52 -0700204 virtual void LinkEvent(unsigned int flags, unsigned int change);
Darin Petkovc0865312011-09-16 15:31:20 -0700205 virtual void Scan(Error *error);
Darin Petkov9ae310f2011-08-30 15:41:13 -0700206 virtual void RegisterOnNetwork(const std::string &network_id, Error *error);
Darin Petkove42e1012011-08-31 12:35:04 -0700207 virtual void RequirePIN(const std::string &pin, bool require, Error *error);
208 virtual void EnterPIN(const std::string &pin, Error *error);
209 virtual void UnblockPIN(const std::string &unblock_code,
210 const std::string &pin,
211 Error *error);
212 virtual void ChangePIN(const std::string &old_pin,
213 const std::string &new_pin,
214 Error *error);
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700215
216 private:
Darin Petkovab565bb2011-10-06 02:55:51 -0700217 friend class CellularTest;
Darin Petkov20c13ec2011-11-09 15:07:15 +0100218 friend class CellularCapabilityCDMATest;
219 friend class CellularCapabilityGSMTest;
Darin Petkov3335b372011-08-22 11:05:32 -0700220 FRIEND_TEST(CellularTest, CreateService);
Darin Petkovc5f56562011-08-06 16:40:05 -0700221 FRIEND_TEST(CellularTest, Connect);
Darin Petkovc408e692011-08-17 13:47:15 -0700222 FRIEND_TEST(CellularTest, GetCDMAActivationStateString);
Darin Petkov51489002011-08-18 13:13:20 -0700223 FRIEND_TEST(CellularTest, GetCDMAActivationErrorString);
Darin Petkovbec79a22011-08-01 14:47:17 -0700224 FRIEND_TEST(CellularTest, GetCDMARegistrationState);
Darin Petkovceb68172011-07-29 14:47:48 -0700225 FRIEND_TEST(CellularTest, GetModemInfo);
226 FRIEND_TEST(CellularTest, GetModemStatus);
Darin Petkove9d12e02011-07-27 15:09:37 -0700227 FRIEND_TEST(CellularTest, GetTypeString);
Darin Petkovbec79a22011-08-01 14:47:17 -0700228 FRIEND_TEST(CellularTest, InitProxiesCDMA);
229 FRIEND_TEST(CellularTest, InitProxiesGSM);
Darin Petkov9ae310f2011-08-30 15:41:13 -0700230 FRIEND_TEST(CellularTest, RegisterOnNetwork);
231 FRIEND_TEST(CellularTest, RegisterOnNetworkError);
Darin Petkov48a511a2011-09-15 10:33:37 -0700232 FRIEND_TEST(CellularTest, SetGSMAccessTechnology);
Darin Petkovbac96002011-08-09 13:22:00 -0700233 FRIEND_TEST(CellularTest, StartConnected);
Darin Petkova1e0a1c2011-08-25 15:08:33 -0700234 FRIEND_TEST(CellularTest, StartCDMARegister);
Darin Petkov9bac6fe2011-08-26 12:49:05 -0700235 FRIEND_TEST(CellularTest, StartGSMRegister);
Darin Petkov0828f5f2011-08-11 10:18:52 -0700236 FRIEND_TEST(CellularTest, StartLinked);
Darin Petkov137884a2011-10-26 18:52:47 +0200237 FRIEND_TEST(CellularTest, UpdateGSMOperatorInfo);
Darin Petkovbac96002011-08-09 13:22:00 -0700238
Darin Petkov9bac6fe2011-08-26 12:49:05 -0700239 struct CDMA {
240 CDMA();
241
242 uint32 registration_state_evdo;
243 uint32 registration_state_1x;
244 uint32 activation_state;
245
246 uint16 prl_version;
247 std::string payment_url;
248 std::string usage_url;
249 };
250
251 struct GSM {
252 GSM();
253
254 uint32 registration_state;
255 uint32 access_technology;
256 std::string network_id;
257 std::string operator_name;
Darin Petkov137884a2011-10-26 18:52:47 +0200258 std::string operator_country;
Darin Petkov975b5e72011-08-30 11:48:08 -0700259 std::string spn;
Darin Petkov9bac6fe2011-08-26 12:49:05 -0700260 };
261
Darin Petkovbac96002011-08-09 13:22:00 -0700262 static const char kPhoneNumberCDMA[];
263 static const char kPhoneNumberGSM[];
Darin Petkove9d12e02011-07-27 15:09:37 -0700264
Darin Petkov0828f5f2011-08-11 10:18:52 -0700265 void SetState(State state);
266
Darin Petkovc5f56562011-08-06 16:40:05 -0700267 void ConnectTask(const DBusPropertiesMap &properties);
Darin Petkov9ae310f2011-08-30 15:41:13 -0700268 void RegisterOnNetworkTask(const std::string &network_id);
Darin Petkovc5f56562011-08-06 16:40:05 -0700269
Darin Petkovbac96002011-08-09 13:22:00 -0700270 // Invoked when the modem is connected to the cellular network to transition
271 // to the network-connected state and bring the network interface up.
272 void EstablishLink();
273
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800274 StrIntPair SimLockStatusToProperty(Error *error);
Darin Petkov48a511a2011-09-15 10:33:37 -0700275
mukesh agrawalffa3d042011-10-06 15:26:10 -0700276 void HelpRegisterDerivedStrIntPair(
277 const std::string &name,
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800278 StrIntPair(Cellular::*get)(Error *),
mukesh agrawalffa3d042011-10-06 15:26:10 -0700279 void(Cellular::*set)(const StrIntPair&, Error *));
Chris Masone889666b2011-07-03 12:58:50 -0700280
Darin Petkovdaf43862011-10-27 11:37:28 +0200281 void InitCapability();
Darin Petkovbec79a22011-08-01 14:47:17 -0700282 void InitProxies();
283
Darin Petkovcc044422011-08-17 13:30:06 -0700284 std::string GetTypeString() const;
Darin Petkove9d12e02011-07-27 15:09:37 -0700285
Darin Petkovc408e692011-08-17 13:47:15 -0700286 static std::string GetCDMAActivationStateString(uint32 state);
Darin Petkov51489002011-08-18 13:13:20 -0700287 static std::string GetCDMAActivationErrorString(uint32 error);
Darin Petkovc408e692011-08-17 13:47:15 -0700288
Darin Petkovf5f61e02011-07-29 11:35:40 -0700289 void EnableModem();
290 void GetModemStatus();
Darin Petkovceb68172011-07-29 14:47:48 -0700291 void GetGSMProperties();
292 void RegisterGSMModem();
Darin Petkovceb68172011-07-29 14:47:48 -0700293
Darin Petkovd9661952011-08-03 16:25:42 -0700294 // Obtains modem's manufacturer, model ID, and hardware revision.
Darin Petkovceb68172011-07-29 14:47:48 -0700295 void GetModemInfo();
Darin Petkovf5f61e02011-07-29 11:35:40 -0700296
Darin Petkovbec79a22011-08-01 14:47:17 -0700297 void GetModemRegistrationState();
298 void GetCDMARegistrationState();
299 void GetGSMRegistrationState();
300
Darin Petkovd9661952011-08-03 16:25:42 -0700301 // Processes a change in the modem registration state, possibly creating,
302 // destroying or updating the CellularService.
303 void HandleNewRegistrationState();
Darin Petkov0828f5f2011-08-11 10:18:52 -0700304 void HandleNewRegistrationStateTask();
Darin Petkovd9661952011-08-03 16:25:42 -0700305
306 void CreateService();
307
Darin Petkov137884a2011-10-26 18:52:47 +0200308 // Updates the GSM operator name and country based on a newly obtained network
309 // id.
310 void UpdateGSMOperatorInfo();
311
312 // Updates the serving operator on the active service.
313 void UpdateServingOperator();
314
Darin Petkov580c7af2011-10-24 12:32:50 +0200315 // Signal callbacks inherited from ModemCDMAProxyDelegate.
Darin Petkovb27e5442011-08-16 14:36:45 -0700316 virtual void OnCDMAActivationStateChanged(
317 uint32 activation_state,
318 uint32 activation_error,
319 const DBusPropertiesMap &status_changes);
Darin Petkovd9661952011-08-03 16:25:42 -0700320 virtual void OnCDMARegistrationStateChanged(uint32 state_1x,
321 uint32 state_evdo);
322 virtual void OnCDMASignalQualityChanged(uint32 strength);
323
Darin Petkov580c7af2011-10-24 12:32:50 +0200324 // Signal callbacks inherited from ModemGSMNetworkProxyDelegate.
Darin Petkova1e0a1c2011-08-25 15:08:33 -0700325 virtual void OnGSMNetworkModeChanged(uint32 mode);
326 virtual void OnGSMRegistrationInfoChanged(uint32 status,
327 const std::string &operator_code,
328 const std::string &operator_name);
329 virtual void OnGSMSignalQualityChanged(uint32 quality);
330
Darin Petkov580c7af2011-10-24 12:32:50 +0200331 // Signal callbacks inherited from ModemProxyDelegate.
Darin Petkovc5f56562011-08-06 16:40:05 -0700332 virtual void OnModemStateChanged(uint32 old_state,
333 uint32 new_state,
334 uint32 reason);
335
Darin Petkovab565bb2011-10-06 02:55:51 -0700336 // Store cached copies of singletons for speed/ease of testing.
337 ProxyFactory *proxy_factory_;
338
Darin Petkove9d12e02011-07-27 15:09:37 -0700339 Type type_;
340 State state_;
Darin Petkovbac96002011-08-09 13:22:00 -0700341 ModemState modem_state_;
Darin Petkove9d12e02011-07-27 15:09:37 -0700342
Darin Petkovdaf43862011-10-27 11:37:28 +0200343 scoped_ptr<CellularCapability> capability_;
344
Darin Petkove9d12e02011-07-27 15:09:37 -0700345 const std::string dbus_owner_; // ModemManager.Modem
346 const std::string dbus_path_; // ModemManager.Modem
347 scoped_ptr<ModemProxyInterface> proxy_;
Darin Petkove604f702011-07-28 15:51:17 -0700348 scoped_ptr<ModemSimpleProxyInterface> simple_proxy_;
Darin Petkovbec79a22011-08-01 14:47:17 -0700349 scoped_ptr<ModemCDMAProxyInterface> cdma_proxy_;
Darin Petkova1e0a1c2011-08-25 15:08:33 -0700350 scoped_ptr<ModemGSMNetworkProxyInterface> gsm_network_proxy_;
Darin Petkovbec79a22011-08-01 14:47:17 -0700351
Darin Petkov137884a2011-10-26 18:52:47 +0200352 mobile_provider_db *provider_db_;
353
Darin Petkovbec79a22011-08-01 14:47:17 -0700354 CDMA cdma_;
Darin Petkov9bac6fe2011-08-26 12:49:05 -0700355 GSM gsm_;
Darin Petkove9d12e02011-07-27 15:09:37 -0700356
Darin Petkovd9661952011-08-03 16:25:42 -0700357 CellularServiceRefPtr service_;
Chris Masone853b81b2011-06-24 14:11:41 -0700358
Darin Petkovc5f56562011-08-06 16:40:05 -0700359 ScopedRunnableMethodFactory<Cellular> task_factory_;
360
Chris Masoneb925cc82011-06-22 15:39:57 -0700361 // Properties
362 bool allow_roaming_;
363 std::string carrier_;
364 std::string meid_;
365 std::string imei_;
366 std::string imsi_;
367 std::string esn_;
368 std::string mdn_;
369 std::string min_;
370 std::string model_id_;
371 std::string manufacturer_;
372 std::string firmware_revision_;
373 std::string hardware_revision_;
Darin Petkova1e0a1c2011-08-25 15:08:33 -0700374 std::string selected_network_;
Chris Masone889666b2011-07-03 12:58:50 -0700375 SimLockStatus sim_lock_status_;
Darin Petkov3335b372011-08-22 11:05:32 -0700376 Operator home_provider_;
Chris Masoneb925cc82011-06-22 15:39:57 -0700377
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700378 DISALLOW_COPY_AND_ASSIGN(Cellular);
379};
380
381} // namespace shill
382
383#endif // SHILL_CELLULAR_