blob: 8567d50ff5c2aae096fcc6cfe425d4c094511e0d [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 Petkovc5f56562011-08-06 16:40:05 -070017#include "shill/modem_proxy_interface.h"
Chris Masone2b105542011-06-22 10:58:09 -070018#include "shill/refptr_types.h"
Chris Masone3bd3c8c2011-06-13 08:20:26 -070019
Darin Petkov137884a2011-10-26 18:52:47 +020020struct mobile_provider_db;
21
Chris Masone3bd3c8c2011-06-13 08:20:26 -070022namespace shill {
23
Darin Petkovdaf43862011-10-27 11:37:28 +020024class CellularCapability;
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 Petkov580c7af2011-10-24 12:32:50 +020030 public ModemProxyDelegate {
Chris Masone3bd3c8c2011-06-13 08:20:26 -070031 public:
Darin Petkove9d12e02011-07-27 15:09:37 -070032 enum Type {
33 kTypeGSM,
34 kTypeCDMA
35 };
36
Darin Petkov0828f5f2011-08-11 10:18:52 -070037 // The device states progress linearly from Disabled to Linked.
Darin Petkove9d12e02011-07-27 15:09:37 -070038 enum State {
Darin Petkov0828f5f2011-08-11 10:18:52 -070039 // This is the initial state of the modem and indicates that the modem radio
40 // is not turned on.
Darin Petkove9d12e02011-07-27 15:09:37 -070041 kStateDisabled,
Darin Petkov0828f5f2011-08-11 10:18:52 -070042 // This state indicates that the modem radio is turned on, and it should be
43 // possible to measure signal strength.
Darin Petkove9d12e02011-07-27 15:09:37 -070044 kStateEnabled,
Darin Petkov0828f5f2011-08-11 10:18:52 -070045 // The modem has registered with a network and has signal quality
Darin Petkov51489002011-08-18 13:13:20 -070046 // measurements. A cellular service object is created.
Darin Petkove9d12e02011-07-27 15:09:37 -070047 kStateRegistered,
Darin Petkov0828f5f2011-08-11 10:18:52 -070048 // The modem has connected to a network.
Darin Petkove9d12e02011-07-27 15:09:37 -070049 kStateConnected,
Darin Petkov0828f5f2011-08-11 10:18:52 -070050 // The network interface is UP.
51 kStateLinked,
Darin Petkove9d12e02011-07-27 15:09:37 -070052 };
53
Darin Petkovbac96002011-08-09 13:22:00 -070054 // These should be kept in sync with ModemManager's mm-modem.h:MMModemState.
55 enum ModemState {
56 kModemStateUnknown = 0,
57 kModemStateDisabled = 10,
58 kModemStateDisabling = 20,
59 kModemStateEnabling = 30,
60 kModemStateEnabled = 40,
61 kModemStateSearching = 50,
62 kModemStateRegistered = 60,
63 kModemStateDisconnecting = 70,
64 kModemStateConnecting = 80,
65 kModemStateConnected = 90,
66 };
67
Darin Petkov3335b372011-08-22 11:05:32 -070068 class Operator {
69 public:
70 Operator();
71 ~Operator();
72
73 void CopyFrom(const Operator &oper);
74
75 const std::string &GetName() const;
76 void SetName(const std::string &name);
77
78 const std::string &GetCode() const;
79 void SetCode(const std::string &code);
80
81 const std::string &GetCountry() const;
82 void SetCountry(const std::string &country);
83
84 const Stringmap &ToDict() const;
85
86 private:
87 Stringmap dict_;
88
89 DISALLOW_COPY_AND_ASSIGN(Operator);
90 };
91
Chris Masone889666b2011-07-03 12:58:50 -070092 struct SimLockStatus {
93 public:
Darin Petkove9d12e02011-07-27 15:09:37 -070094 SimLockStatus() : retries_left(0) {}
Darin Petkov48a511a2011-09-15 10:33:37 -070095 SimLockStatus(const std::string &in_lock_type, uint32 in_retries_left)
96 : lock_type(in_lock_type),
97 retries_left(in_retries_left) {}
Darin Petkove9d12e02011-07-27 15:09:37 -070098
Chris Masone889666b2011-07-03 12:58:50 -070099 std::string lock_type;
100 uint32 retries_left;
101 };
102
Darin Petkovc5f56562011-08-06 16:40:05 -0700103 static const char kConnectPropertyPhoneNumber[];
104
Darin Petkove9d12e02011-07-27 15:09:37 -0700105 // |owner| is the ModemManager DBus service owner (e.g., ":1.17"). |path| is
106 // the ModemManager.Modem DBus object path (e.g.,
107 // "/org/chromium/ModemManager/Gobi/0").
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700108 Cellular(ControlInterface *control_interface,
109 EventDispatcher *dispatcher,
110 Manager *manager,
Darin Petkove9d12e02011-07-27 15:09:37 -0700111 const std::string &link_name,
Chris Masone626719f2011-08-18 16:58:48 -0700112 const std::string &address,
Darin Petkove9d12e02011-07-27 15:09:37 -0700113 int interface_index,
114 Type type,
115 const std::string &owner,
Darin Petkov137884a2011-10-26 18:52:47 +0200116 const std::string &path,
117 mobile_provider_db *provider_db);
Darin Petkove9d12e02011-07-27 15:09:37 -0700118 virtual ~Cellular();
119
Darin Petkov4d6d9412011-08-24 13:19:54 -0700120 // Asynchronously connects the modem to the network. Populates |error| on
121 // failure, leaves it unchanged otherwise.
122 void Connect(Error *error);
Darin Petkovc5f56562011-08-06 16:40:05 -0700123
Darin Petkovb100ae72011-08-24 16:19:45 -0700124 // Asynchronously activates the modem. Populates |error| on failure, leaves it
125 // unchanged otherwise.
126 void Activate(const std::string &carrier, Error *error);
Darin Petkovc408e692011-08-17 13:47:15 -0700127
Darin Petkov3e509242011-11-10 14:46:44 +0100128 const CellularServiceRefPtr &service() const { return service_; }
129
Darin Petkova3d3be52011-11-14 21:34:16 +0100130 static std::string GetStateString(State state);
131
132 State state() const { return state_; }
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 Petkov184c54e2011-11-15 12:44:39 +0100145 void set_gsm_network_id(const std::string &id) { gsm_.network_id = id; }
146 void set_gsm_operator_name(const std::string &name) {
147 gsm_.operator_name = name;
148 }
Darin Petkov20c13ec2011-11-09 15:07:15 +0100149
Darin Petkov184c54e2011-11-15 12:44:39 +0100150 uint32 gsm_access_technology() const { return gsm_.access_technology; }
Darin Petkov20c13ec2011-11-09 15:07:15 +0100151
Darin Petkova3d3be52011-11-14 21:34:16 +0100152 uint32 cdma_activation_state() const { return cdma_.activation_state; }
153 void set_cdma_activation_state(uint32 state) {
154 cdma_.activation_state = state;
155 }
156
Darin Petkov184c54e2011-11-15 12:44:39 +0100157 void set_cdma_payment_url(const std::string &url) { cdma_.payment_url = url; }
158
Darin Petkovcb547732011-11-09 13:55:26 +0100159 const std::string &meid() const { return meid_; }
160 void set_meid(const std::string &meid) { meid_ = meid; }
161
162 const std::string &imei() const { return imei_; }
163 void set_imei(const std::string &imei) { imei_ = imei; }
164
165 const std::string &imsi() const { return imsi_; }
166 void set_imsi(const std::string &imsi) { imsi_ = imsi; }
167
168 const std::string &spn() const { return gsm_.spn; }
169 void set_spn(const std::string &spn) { gsm_.spn = spn; }
170
171 const std::string &mdn() const { return mdn_; }
172 void set_mdn(const std::string &mdn) { mdn_ = mdn; }
173
Darin Petkov184c54e2011-11-15 12:44:39 +0100174 const std::string &min() const { return min_; }
175 void set_min(const std::string &min) { min_ = min; }
Darin Petkovdaf43862011-10-27 11:37:28 +0200176
Darin Petkov184c54e2011-11-15 12:44:39 +0100177 ProxyFactory *proxy_factory() const { return proxy_factory_; }
Darin Petkovdaf43862011-10-27 11:37:28 +0200178
Darin Petkov48a511a2011-09-15 10:33:37 -0700179 void SetGSMAccessTechnology(uint32 access_technology);
180
Darin Petkov3e509242011-11-10 14:46:44 +0100181 void HandleNewSignalQuality(uint32 strength);
182
Darin Petkov184c54e2011-11-15 12:44:39 +0100183 // Processes a change in the modem registration state, possibly creating,
184 // destroying or updating the CellularService.
185 void HandleNewRegistrationState();
186
Darin Petkova3d3be52011-11-14 21:34:16 +0100187 void HandleNewCDMAActivationState(uint32 error);
188
Darin Petkov184c54e2011-11-15 12:44:39 +0100189 // Updates the GSM operator name and country based on a newly obtained network
190 // id.
191 void UpdateGSMOperatorInfo();
192
Darin Petkove9d12e02011-07-27 15:09:37 -0700193 // Inherited from Device.
194 virtual void Start();
195 virtual void Stop();
Paul Stewartfdd16072011-09-16 12:41:35 -0700196 virtual bool TechnologyIs(Technology::Identifier type) const;
Darin Petkov0828f5f2011-08-11 10:18:52 -0700197 virtual void LinkEvent(unsigned int flags, unsigned int change);
Darin Petkovc0865312011-09-16 15:31:20 -0700198 virtual void Scan(Error *error);
Darin Petkov9ae310f2011-08-30 15:41:13 -0700199 virtual void RegisterOnNetwork(const std::string &network_id, Error *error);
Darin Petkove42e1012011-08-31 12:35:04 -0700200 virtual void RequirePIN(const std::string &pin, bool require, Error *error);
201 virtual void EnterPIN(const std::string &pin, Error *error);
202 virtual void UnblockPIN(const std::string &unblock_code,
203 const std::string &pin,
204 Error *error);
205 virtual void ChangePIN(const std::string &old_pin,
206 const std::string &new_pin,
207 Error *error);
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700208
209 private:
Darin Petkovab565bb2011-10-06 02:55:51 -0700210 friend class CellularTest;
Darin Petkov20c13ec2011-11-09 15:07:15 +0100211 friend class CellularCapabilityCDMATest;
212 friend class CellularCapabilityGSMTest;
Darin Petkov3335b372011-08-22 11:05:32 -0700213 FRIEND_TEST(CellularTest, CreateService);
Darin Petkovc5f56562011-08-06 16:40:05 -0700214 FRIEND_TEST(CellularTest, Connect);
Darin Petkovc408e692011-08-17 13:47:15 -0700215 FRIEND_TEST(CellularTest, GetCDMAActivationStateString);
Darin Petkov51489002011-08-18 13:13:20 -0700216 FRIEND_TEST(CellularTest, GetCDMAActivationErrorString);
Darin Petkovceb68172011-07-29 14:47:48 -0700217 FRIEND_TEST(CellularTest, GetModemInfo);
218 FRIEND_TEST(CellularTest, GetModemStatus);
Darin Petkove9d12e02011-07-27 15:09:37 -0700219 FRIEND_TEST(CellularTest, GetTypeString);
Darin Petkovbec79a22011-08-01 14:47:17 -0700220 FRIEND_TEST(CellularTest, InitProxiesCDMA);
221 FRIEND_TEST(CellularTest, InitProxiesGSM);
Darin Petkov48a511a2011-09-15 10:33:37 -0700222 FRIEND_TEST(CellularTest, SetGSMAccessTechnology);
Darin Petkovbac96002011-08-09 13:22:00 -0700223 FRIEND_TEST(CellularTest, StartConnected);
Darin Petkova1e0a1c2011-08-25 15:08:33 -0700224 FRIEND_TEST(CellularTest, StartCDMARegister);
Darin Petkov9bac6fe2011-08-26 12:49:05 -0700225 FRIEND_TEST(CellularTest, StartGSMRegister);
Darin Petkov0828f5f2011-08-11 10:18:52 -0700226 FRIEND_TEST(CellularTest, StartLinked);
Darin Petkov137884a2011-10-26 18:52:47 +0200227 FRIEND_TEST(CellularTest, UpdateGSMOperatorInfo);
Darin Petkovbac96002011-08-09 13:22:00 -0700228
Darin Petkov9bac6fe2011-08-26 12:49:05 -0700229 struct CDMA {
230 CDMA();
231
Darin Petkov9bac6fe2011-08-26 12:49:05 -0700232 uint32 activation_state;
233
234 uint16 prl_version;
235 std::string payment_url;
236 std::string usage_url;
237 };
238
239 struct GSM {
240 GSM();
241
Darin Petkov9bac6fe2011-08-26 12:49:05 -0700242 uint32 access_technology;
243 std::string network_id;
244 std::string operator_name;
Darin Petkov137884a2011-10-26 18:52:47 +0200245 std::string operator_country;
Darin Petkov975b5e72011-08-30 11:48:08 -0700246 std::string spn;
Darin Petkov9bac6fe2011-08-26 12:49:05 -0700247 };
248
Darin Petkovbac96002011-08-09 13:22:00 -0700249 static const char kPhoneNumberCDMA[];
250 static const char kPhoneNumberGSM[];
Darin Petkove9d12e02011-07-27 15:09:37 -0700251
Darin Petkov0828f5f2011-08-11 10:18:52 -0700252 void SetState(State state);
253
Darin Petkovc5f56562011-08-06 16:40:05 -0700254 void ConnectTask(const DBusPropertiesMap &properties);
255
Darin Petkovbac96002011-08-09 13:22:00 -0700256 // Invoked when the modem is connected to the cellular network to transition
257 // to the network-connected state and bring the network interface up.
258 void EstablishLink();
259
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800260 StrIntPair SimLockStatusToProperty(Error *error);
Darin Petkov48a511a2011-09-15 10:33:37 -0700261
mukesh agrawalffa3d042011-10-06 15:26:10 -0700262 void HelpRegisterDerivedStrIntPair(
263 const std::string &name,
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800264 StrIntPair(Cellular::*get)(Error *),
mukesh agrawalffa3d042011-10-06 15:26:10 -0700265 void(Cellular::*set)(const StrIntPair&, Error *));
Chris Masone889666b2011-07-03 12:58:50 -0700266
Darin Petkovdaf43862011-10-27 11:37:28 +0200267 void InitCapability();
Darin Petkovbec79a22011-08-01 14:47:17 -0700268 void InitProxies();
269
Darin Petkovcc044422011-08-17 13:30:06 -0700270 std::string GetTypeString() const;
Darin Petkove9d12e02011-07-27 15:09:37 -0700271
Darin Petkovc408e692011-08-17 13:47:15 -0700272 static std::string GetCDMAActivationStateString(uint32 state);
Darin Petkov51489002011-08-18 13:13:20 -0700273 static std::string GetCDMAActivationErrorString(uint32 error);
Darin Petkovc408e692011-08-17 13:47:15 -0700274
Darin Petkovf5f61e02011-07-29 11:35:40 -0700275 void EnableModem();
276 void GetModemStatus();
Darin Petkovceb68172011-07-29 14:47:48 -0700277
Darin Petkovd9661952011-08-03 16:25:42 -0700278 // Obtains modem's manufacturer, model ID, and hardware revision.
Darin Petkovceb68172011-07-29 14:47:48 -0700279 void GetModemInfo();
Darin Petkovf5f61e02011-07-29 11:35:40 -0700280
Darin Petkov0828f5f2011-08-11 10:18:52 -0700281 void HandleNewRegistrationStateTask();
Darin Petkovd9661952011-08-03 16:25:42 -0700282
283 void CreateService();
284
Darin Petkov137884a2011-10-26 18:52:47 +0200285 // Updates the serving operator on the active service.
286 void UpdateServingOperator();
287
Darin Petkov580c7af2011-10-24 12:32:50 +0200288 // Signal callbacks inherited from ModemProxyDelegate.
Darin Petkovc5f56562011-08-06 16:40:05 -0700289 virtual void OnModemStateChanged(uint32 old_state,
290 uint32 new_state,
291 uint32 reason);
292
Darin Petkovab565bb2011-10-06 02:55:51 -0700293 // Store cached copies of singletons for speed/ease of testing.
294 ProxyFactory *proxy_factory_;
295
Darin Petkove9d12e02011-07-27 15:09:37 -0700296 Type type_;
297 State state_;
Darin Petkovbac96002011-08-09 13:22:00 -0700298 ModemState modem_state_;
Darin Petkove9d12e02011-07-27 15:09:37 -0700299
Darin Petkovdaf43862011-10-27 11:37:28 +0200300 scoped_ptr<CellularCapability> capability_;
301
Darin Petkove9d12e02011-07-27 15:09:37 -0700302 const std::string dbus_owner_; // ModemManager.Modem
303 const std::string dbus_path_; // ModemManager.Modem
304 scoped_ptr<ModemProxyInterface> proxy_;
Darin Petkove604f702011-07-28 15:51:17 -0700305 scoped_ptr<ModemSimpleProxyInterface> simple_proxy_;
Darin Petkovbec79a22011-08-01 14:47:17 -0700306
Darin Petkov137884a2011-10-26 18:52:47 +0200307 mobile_provider_db *provider_db_;
308
Darin Petkovbec79a22011-08-01 14:47:17 -0700309 CDMA cdma_;
Darin Petkov9bac6fe2011-08-26 12:49:05 -0700310 GSM gsm_;
Darin Petkove9d12e02011-07-27 15:09:37 -0700311
Darin Petkovd9661952011-08-03 16:25:42 -0700312 CellularServiceRefPtr service_;
Chris Masone853b81b2011-06-24 14:11:41 -0700313
Darin Petkovc5f56562011-08-06 16:40:05 -0700314 ScopedRunnableMethodFactory<Cellular> task_factory_;
315
Chris Masoneb925cc82011-06-22 15:39:57 -0700316 // Properties
317 bool allow_roaming_;
318 std::string carrier_;
319 std::string meid_;
320 std::string imei_;
321 std::string imsi_;
322 std::string esn_;
323 std::string mdn_;
324 std::string min_;
325 std::string model_id_;
326 std::string manufacturer_;
327 std::string firmware_revision_;
328 std::string hardware_revision_;
Chris Masone889666b2011-07-03 12:58:50 -0700329 SimLockStatus sim_lock_status_;
Darin Petkov3335b372011-08-22 11:05:32 -0700330 Operator home_provider_;
Chris Masoneb925cc82011-06-22 15:39:57 -0700331
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700332 DISALLOW_COPY_AND_ASSIGN(Cellular);
333};
334
335} // namespace shill
336
337#endif // SHILL_CELLULAR_