blob: 5f280cd1b7cae0dfa5705f7417f9e4c007a27fe6 [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 Petkovbac96002011-08-09 13:22:00 -0700134 void set_modem_state(ModemState state) { modem_state_ = state; }
135 ModemState modem_state() const { return modem_state_; }
136
Darin Petkov48a511a2011-09-15 10:33:37 -0700137 const SimLockStatus &sim_lock_status() const { return sim_lock_status_; }
138 void set_sim_lock_status(const SimLockStatus &s) { sim_lock_status_ = s; }
139
Darin Petkov1272a432011-11-10 15:53:37 +0100140 mobile_provider_db *provider_db() const { return provider_db_; }
141
Darin Petkovdaf43862011-10-27 11:37:28 +0200142 const std::string &dbus_owner() const { return dbus_owner_; }
143 const std::string &dbus_path() const { return dbus_path_; }
144
Darin Petkov20c13ec2011-11-09 15:07:15 +0100145 uint32 gsm_registration_state() const { return gsm_.registration_state; }
146 uint32 gsm_access_technology() const { return gsm_.access_technology; }
147
148 uint32 cdma_registration_state_evdo() const {
149 return cdma_.registration_state_evdo;
150 }
151 uint32 cdma_registration_state_1x() const {
152 return cdma_.registration_state_1x;
153 }
154
Darin Petkovcb547732011-11-09 13:55:26 +0100155 const std::string &meid() const { return meid_; }
156 void set_meid(const std::string &meid) { meid_ = meid; }
157
158 const std::string &imei() const { return imei_; }
159 void set_imei(const std::string &imei) { imei_ = imei; }
160
161 const std::string &imsi() const { return imsi_; }
162 void set_imsi(const std::string &imsi) { imsi_ = imsi; }
163
164 const std::string &spn() const { return gsm_.spn; }
165 void set_spn(const std::string &spn) { gsm_.spn = spn; }
166
167 const std::string &mdn() const { return mdn_; }
168 void set_mdn(const std::string &mdn) { mdn_ = mdn; }
169
Darin Petkovdaf43862011-10-27 11:37:28 +0200170 ProxyFactory *proxy_factory() const { return proxy_factory_; }
171
Darin Petkovb05315f2011-11-07 10:14:25 +0100172 ModemCDMAProxyInterface *modem_cdma_proxy() const {
173 return cdma_proxy_.get();
174 }
Darin Petkovdaf43862011-10-27 11:37:28 +0200175 void set_modem_cdma_proxy(ModemCDMAProxyInterface *proxy) {
176 cdma_proxy_.reset(proxy);
177 }
Darin Petkovb05315f2011-11-07 10:14:25 +0100178 ModemGSMNetworkProxyInterface *modem_gsm_network_proxy() const {
179 return gsm_network_proxy_.get();
180 }
Darin Petkovdaf43862011-10-27 11:37:28 +0200181 void set_modem_gsm_network_proxy(ModemGSMNetworkProxyInterface *proxy) {
182 gsm_network_proxy_.reset(proxy);
183 }
184
Darin Petkov48a511a2011-09-15 10:33:37 -0700185 void SetGSMAccessTechnology(uint32 access_technology);
186
Darin Petkov3e509242011-11-10 14:46:44 +0100187 void HandleNewSignalQuality(uint32 strength);
188
Darin Petkove9d12e02011-07-27 15:09:37 -0700189 // Inherited from Device.
190 virtual void Start();
191 virtual void Stop();
Paul Stewartfdd16072011-09-16 12:41:35 -0700192 virtual bool TechnologyIs(Technology::Identifier type) const;
Darin Petkov0828f5f2011-08-11 10:18:52 -0700193 virtual void LinkEvent(unsigned int flags, unsigned int change);
Darin Petkovc0865312011-09-16 15:31:20 -0700194 virtual void Scan(Error *error);
Darin Petkov9ae310f2011-08-30 15:41:13 -0700195 virtual void RegisterOnNetwork(const std::string &network_id, Error *error);
Darin Petkove42e1012011-08-31 12:35:04 -0700196 virtual void RequirePIN(const std::string &pin, bool require, Error *error);
197 virtual void EnterPIN(const std::string &pin, Error *error);
198 virtual void UnblockPIN(const std::string &unblock_code,
199 const std::string &pin,
200 Error *error);
201 virtual void ChangePIN(const std::string &old_pin,
202 const std::string &new_pin,
203 Error *error);
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700204
205 private:
Darin Petkovab565bb2011-10-06 02:55:51 -0700206 friend class CellularTest;
Darin Petkov20c13ec2011-11-09 15:07:15 +0100207 friend class CellularCapabilityCDMATest;
208 friend class CellularCapabilityGSMTest;
Darin Petkovc408e692011-08-17 13:47:15 -0700209 FRIEND_TEST(CellularTest, Activate);
Darin Petkov51489002011-08-18 13:13:20 -0700210 FRIEND_TEST(CellularTest, ActivateError);
Darin Petkov3335b372011-08-22 11:05:32 -0700211 FRIEND_TEST(CellularTest, CreateService);
Darin Petkovc5f56562011-08-06 16:40:05 -0700212 FRIEND_TEST(CellularTest, Connect);
Darin Petkovc408e692011-08-17 13:47:15 -0700213 FRIEND_TEST(CellularTest, GetCDMAActivationStateString);
Darin Petkov51489002011-08-18 13:13:20 -0700214 FRIEND_TEST(CellularTest, GetCDMAActivationErrorString);
Darin Petkovbec79a22011-08-01 14:47:17 -0700215 FRIEND_TEST(CellularTest, GetCDMARegistrationState);
Darin Petkovceb68172011-07-29 14:47:48 -0700216 FRIEND_TEST(CellularTest, GetModemInfo);
217 FRIEND_TEST(CellularTest, GetModemStatus);
Darin Petkove9d12e02011-07-27 15:09:37 -0700218 FRIEND_TEST(CellularTest, GetStateString);
219 FRIEND_TEST(CellularTest, GetTypeString);
Darin Petkovbec79a22011-08-01 14:47:17 -0700220 FRIEND_TEST(CellularTest, InitProxiesCDMA);
221 FRIEND_TEST(CellularTest, InitProxiesGSM);
Darin Petkov9ae310f2011-08-30 15:41:13 -0700222 FRIEND_TEST(CellularTest, RegisterOnNetwork);
223 FRIEND_TEST(CellularTest, RegisterOnNetworkError);
Darin Petkov48a511a2011-09-15 10:33:37 -0700224 FRIEND_TEST(CellularTest, SetGSMAccessTechnology);
Darin Petkovbac96002011-08-09 13:22:00 -0700225 FRIEND_TEST(CellularTest, StartConnected);
Darin Petkova1e0a1c2011-08-25 15:08:33 -0700226 FRIEND_TEST(CellularTest, StartCDMARegister);
Darin Petkov9bac6fe2011-08-26 12:49:05 -0700227 FRIEND_TEST(CellularTest, StartGSMRegister);
Darin Petkov0828f5f2011-08-11 10:18:52 -0700228 FRIEND_TEST(CellularTest, StartLinked);
Darin Petkov137884a2011-10-26 18:52:47 +0200229 FRIEND_TEST(CellularTest, UpdateGSMOperatorInfo);
Darin Petkovbac96002011-08-09 13:22:00 -0700230
Darin Petkov9bac6fe2011-08-26 12:49:05 -0700231 struct CDMA {
232 CDMA();
233
234 uint32 registration_state_evdo;
235 uint32 registration_state_1x;
236 uint32 activation_state;
237
238 uint16 prl_version;
239 std::string payment_url;
240 std::string usage_url;
241 };
242
243 struct GSM {
244 GSM();
245
246 uint32 registration_state;
247 uint32 access_technology;
248 std::string network_id;
249 std::string operator_name;
Darin Petkov137884a2011-10-26 18:52:47 +0200250 std::string operator_country;
Darin Petkov975b5e72011-08-30 11:48:08 -0700251 std::string spn;
Darin Petkov9bac6fe2011-08-26 12:49:05 -0700252 };
253
Darin Petkovbac96002011-08-09 13:22:00 -0700254 static const char kPhoneNumberCDMA[];
255 static const char kPhoneNumberGSM[];
Darin Petkove9d12e02011-07-27 15:09:37 -0700256
Darin Petkov0828f5f2011-08-11 10:18:52 -0700257 void SetState(State state);
258
Darin Petkovc5f56562011-08-06 16:40:05 -0700259 void ConnectTask(const DBusPropertiesMap &properties);
Darin Petkovc408e692011-08-17 13:47:15 -0700260 void ActivateTask(const std::string &carrier);
Darin Petkov9ae310f2011-08-30 15:41:13 -0700261 void RegisterOnNetworkTask(const std::string &network_id);
Darin Petkovc5f56562011-08-06 16:40:05 -0700262
Darin Petkovbac96002011-08-09 13:22:00 -0700263 // Invoked when the modem is connected to the cellular network to transition
264 // to the network-connected state and bring the network interface up.
265 void EstablishLink();
266
Chris Masone889666b2011-07-03 12:58:50 -0700267 StrIntPair SimLockStatusToProperty();
Darin Petkov48a511a2011-09-15 10:33:37 -0700268
mukesh agrawalffa3d042011-10-06 15:26:10 -0700269 void HelpRegisterDerivedStrIntPair(
270 const std::string &name,
271 StrIntPair(Cellular::*get)(void),
272 void(Cellular::*set)(const StrIntPair&, Error *));
Chris Masone889666b2011-07-03 12:58:50 -0700273
Darin Petkovdaf43862011-10-27 11:37:28 +0200274 void InitCapability();
Darin Petkovbec79a22011-08-01 14:47:17 -0700275 void InitProxies();
276
Darin Petkovcc044422011-08-17 13:30:06 -0700277 std::string GetTypeString() const;
278 static std::string GetStateString(State state);
Darin Petkove9d12e02011-07-27 15:09:37 -0700279
Darin Petkovc408e692011-08-17 13:47:15 -0700280 static std::string GetCDMAActivationStateString(uint32 state);
Darin Petkov51489002011-08-18 13:13:20 -0700281 static std::string GetCDMAActivationErrorString(uint32 error);
Darin Petkovc408e692011-08-17 13:47:15 -0700282
Darin Petkovf5f61e02011-07-29 11:35:40 -0700283 void EnableModem();
284 void GetModemStatus();
Darin Petkovceb68172011-07-29 14:47:48 -0700285 void GetGSMProperties();
286 void RegisterGSMModem();
Darin Petkovceb68172011-07-29 14:47:48 -0700287
Darin Petkovd9661952011-08-03 16:25:42 -0700288 // Obtains modem's manufacturer, model ID, and hardware revision.
Darin Petkovceb68172011-07-29 14:47:48 -0700289 void GetModemInfo();
Darin Petkovf5f61e02011-07-29 11:35:40 -0700290
Darin Petkovbec79a22011-08-01 14:47:17 -0700291 void GetModemRegistrationState();
292 void GetCDMARegistrationState();
293 void GetGSMRegistrationState();
294
Darin Petkovd9661952011-08-03 16:25:42 -0700295 // Processes a change in the modem registration state, possibly creating,
296 // destroying or updating the CellularService.
297 void HandleNewRegistrationState();
Darin Petkov0828f5f2011-08-11 10:18:52 -0700298 void HandleNewRegistrationStateTask();
Darin Petkovd9661952011-08-03 16:25:42 -0700299
300 void CreateService();
301
Darin Petkovc408e692011-08-17 13:47:15 -0700302 void HandleNewCDMAActivationState(uint32 error);
303
Darin Petkov137884a2011-10-26 18:52:47 +0200304 // Updates the GSM operator name and country based on a newly obtained network
305 // id.
306 void UpdateGSMOperatorInfo();
307
308 // Updates the serving operator on the active service.
309 void UpdateServingOperator();
310
Darin Petkov580c7af2011-10-24 12:32:50 +0200311 // Signal callbacks inherited from ModemCDMAProxyDelegate.
Darin Petkovb27e5442011-08-16 14:36:45 -0700312 virtual void OnCDMAActivationStateChanged(
313 uint32 activation_state,
314 uint32 activation_error,
315 const DBusPropertiesMap &status_changes);
Darin Petkovd9661952011-08-03 16:25:42 -0700316 virtual void OnCDMARegistrationStateChanged(uint32 state_1x,
317 uint32 state_evdo);
318 virtual void OnCDMASignalQualityChanged(uint32 strength);
319
Darin Petkov580c7af2011-10-24 12:32:50 +0200320 // Signal callbacks inherited from ModemGSMNetworkProxyDelegate.
Darin Petkova1e0a1c2011-08-25 15:08:33 -0700321 virtual void OnGSMNetworkModeChanged(uint32 mode);
322 virtual void OnGSMRegistrationInfoChanged(uint32 status,
323 const std::string &operator_code,
324 const std::string &operator_name);
325 virtual void OnGSMSignalQualityChanged(uint32 quality);
326
Darin Petkov580c7af2011-10-24 12:32:50 +0200327 // Signal callbacks inherited from ModemProxyDelegate.
Darin Petkovc5f56562011-08-06 16:40:05 -0700328 virtual void OnModemStateChanged(uint32 old_state,
329 uint32 new_state,
330 uint32 reason);
331
Darin Petkovab565bb2011-10-06 02:55:51 -0700332 // Store cached copies of singletons for speed/ease of testing.
333 ProxyFactory *proxy_factory_;
334
Darin Petkove9d12e02011-07-27 15:09:37 -0700335 Type type_;
336 State state_;
Darin Petkovbac96002011-08-09 13:22:00 -0700337 ModemState modem_state_;
Darin Petkove9d12e02011-07-27 15:09:37 -0700338
Darin Petkovdaf43862011-10-27 11:37:28 +0200339 scoped_ptr<CellularCapability> capability_;
340
Darin Petkove9d12e02011-07-27 15:09:37 -0700341 const std::string dbus_owner_; // ModemManager.Modem
342 const std::string dbus_path_; // ModemManager.Modem
343 scoped_ptr<ModemProxyInterface> proxy_;
Darin Petkove604f702011-07-28 15:51:17 -0700344 scoped_ptr<ModemSimpleProxyInterface> simple_proxy_;
Darin Petkovbec79a22011-08-01 14:47:17 -0700345 scoped_ptr<ModemCDMAProxyInterface> cdma_proxy_;
Darin Petkova1e0a1c2011-08-25 15:08:33 -0700346 scoped_ptr<ModemGSMNetworkProxyInterface> gsm_network_proxy_;
Darin Petkovbec79a22011-08-01 14:47:17 -0700347
Darin Petkov137884a2011-10-26 18:52:47 +0200348 mobile_provider_db *provider_db_;
349
Darin Petkovbec79a22011-08-01 14:47:17 -0700350 CDMA cdma_;
Darin Petkov9bac6fe2011-08-26 12:49:05 -0700351 GSM gsm_;
Darin Petkove9d12e02011-07-27 15:09:37 -0700352
Darin Petkovd9661952011-08-03 16:25:42 -0700353 CellularServiceRefPtr service_;
Chris Masone853b81b2011-06-24 14:11:41 -0700354
Darin Petkovc5f56562011-08-06 16:40:05 -0700355 ScopedRunnableMethodFactory<Cellular> task_factory_;
356
Chris Masoneb925cc82011-06-22 15:39:57 -0700357 // Properties
358 bool allow_roaming_;
359 std::string carrier_;
360 std::string meid_;
361 std::string imei_;
362 std::string imsi_;
363 std::string esn_;
364 std::string mdn_;
365 std::string min_;
366 std::string model_id_;
367 std::string manufacturer_;
368 std::string firmware_revision_;
369 std::string hardware_revision_;
Darin Petkova1e0a1c2011-08-25 15:08:33 -0700370 std::string selected_network_;
Chris Masone889666b2011-07-03 12:58:50 -0700371 SimLockStatus sim_lock_status_;
Darin Petkov3335b372011-08-22 11:05:32 -0700372 Operator home_provider_;
Chris Masoneb925cc82011-06-22 15:39:57 -0700373
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700374 DISALLOW_COPY_AND_ASSIGN(Cellular);
375};
376
377} // namespace shill
378
379#endif // SHILL_CELLULAR_