blob: 6d1fa1d7ca279f54c127131d0decb9dbc972242f [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 Petkova1e0a1c2011-08-25 15:08:33 -070017#include "shill/modem_gsm_network_proxy_interface.h"
Darin Petkovc5f56562011-08-06 16:40:05 -070018#include "shill/modem_proxy_interface.h"
Chris Masone2b105542011-06-22 10:58:09 -070019#include "shill/refptr_types.h"
Chris Masone3bd3c8c2011-06-13 08:20:26 -070020#include "shill/shill_event.h"
21
22namespace shill {
23
Darin Petkov4d6d9412011-08-24 13:19:54 -070024class Error;
Darin Petkove604f702011-07-28 15:51:17 -070025class ModemSimpleProxyInterface;
Darin Petkove9d12e02011-07-27 15:09:37 -070026
Darin Petkovd9661952011-08-03 16:25:42 -070027class Cellular : public Device,
Darin Petkovc5f56562011-08-06 16:40:05 -070028 public ModemCDMAProxyListener,
Darin Petkova1e0a1c2011-08-25 15:08:33 -070029 public ModemGSMNetworkProxyListener,
Darin Petkovc5f56562011-08-06 16:40:05 -070030 public ModemProxyListener {
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 class Network {
93 public:
94 Network();
Darin Petkove9d12e02011-07-27 15:09:37 -070095 ~Network();
Chris Masone889666b2011-07-03 12:58:50 -070096
97 const std::string &GetStatus() const;
98 void SetStatus(const std::string &status);
99
100 const std::string &GetId() const;
101 void SetId(const std::string &id);
102
103 const std::string &GetShortName() const;
104 void SetShortName(const std::string &name);
105
106 const std::string &GetLongName() const;
107 void SetLongName(const std::string &name);
108
109 const std::string &GetTechnology() const;
110 void SetTechnology(const std::string &technology);
111
112 const Stringmap &ToDict() const;
113
114 private:
115 Stringmap dict_;
Darin Petkove9d12e02011-07-27 15:09:37 -0700116
Chris Masone889666b2011-07-03 12:58:50 -0700117 DISALLOW_COPY_AND_ASSIGN(Network);
118 };
119
120 struct SimLockStatus {
121 public:
Darin Petkove9d12e02011-07-27 15:09:37 -0700122 SimLockStatus() : retries_left(0) {}
123
Chris Masone889666b2011-07-03 12:58:50 -0700124 std::string lock_type;
125 uint32 retries_left;
126 };
127
Darin Petkovc5f56562011-08-06 16:40:05 -0700128 static const char kConnectPropertyPhoneNumber[];
129
Darin Petkove9d12e02011-07-27 15:09:37 -0700130 // |owner| is the ModemManager DBus service owner (e.g., ":1.17"). |path| is
131 // the ModemManager.Modem DBus object path (e.g.,
132 // "/org/chromium/ModemManager/Gobi/0").
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700133 Cellular(ControlInterface *control_interface,
134 EventDispatcher *dispatcher,
135 Manager *manager,
Darin Petkove9d12e02011-07-27 15:09:37 -0700136 const std::string &link_name,
Chris Masone626719f2011-08-18 16:58:48 -0700137 const std::string &address,
Darin Petkove9d12e02011-07-27 15:09:37 -0700138 int interface_index,
139 Type type,
140 const std::string &owner,
141 const std::string &path);
142 virtual ~Cellular();
143
Darin Petkov4d6d9412011-08-24 13:19:54 -0700144 // Asynchronously connects the modem to the network. Populates |error| on
145 // failure, leaves it unchanged otherwise.
146 void Connect(Error *error);
Darin Petkovc5f56562011-08-06 16:40:05 -0700147
Darin Petkovb100ae72011-08-24 16:19:45 -0700148 // Asynchronously activates the modem. Populates |error| on failure, leaves it
149 // unchanged otherwise.
150 void Activate(const std::string &carrier, Error *error);
Darin Petkovc408e692011-08-17 13:47:15 -0700151
Darin Petkovbac96002011-08-09 13:22:00 -0700152 void set_modem_state(ModemState state) { modem_state_ = state; }
153 ModemState modem_state() const { return modem_state_; }
154
Darin Petkove9d12e02011-07-27 15:09:37 -0700155 // Inherited from Device.
156 virtual void Start();
157 virtual void Stop();
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700158 virtual bool TechnologyIs(Technology type) const;
Darin Petkov0828f5f2011-08-11 10:18:52 -0700159 virtual void LinkEvent(unsigned int flags, unsigned int change);
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700160
161 private:
Darin Petkovc408e692011-08-17 13:47:15 -0700162 FRIEND_TEST(CellularTest, Activate);
Darin Petkov51489002011-08-18 13:13:20 -0700163 FRIEND_TEST(CellularTest, ActivateError);
Darin Petkov3335b372011-08-22 11:05:32 -0700164 FRIEND_TEST(CellularTest, CreateService);
Darin Petkovc5f56562011-08-06 16:40:05 -0700165 FRIEND_TEST(CellularTest, Connect);
Darin Petkovc408e692011-08-17 13:47:15 -0700166 FRIEND_TEST(CellularTest, GetCDMAActivationStateString);
Darin Petkov51489002011-08-18 13:13:20 -0700167 FRIEND_TEST(CellularTest, GetCDMAActivationErrorString);
Darin Petkov9bac6fe2011-08-26 12:49:05 -0700168 FRIEND_TEST(CellularTest, GetCDMANetworkTechnologyString);
Darin Petkovbec79a22011-08-01 14:47:17 -0700169 FRIEND_TEST(CellularTest, GetCDMARegistrationState);
Darin Petkov9bac6fe2011-08-26 12:49:05 -0700170 FRIEND_TEST(CellularTest, GetCDMARoamingStateString);
Darin Petkovd9661952011-08-03 16:25:42 -0700171 FRIEND_TEST(CellularTest, GetCDMASignalQuality);
Darin Petkov9bac6fe2011-08-26 12:49:05 -0700172 FRIEND_TEST(CellularTest, GetGSMNetworkTechnologyString);
173 FRIEND_TEST(CellularTest, GetGSMRoamingStateString);
Darin Petkovceb68172011-07-29 14:47:48 -0700174 FRIEND_TEST(CellularTest, GetModemInfo);
175 FRIEND_TEST(CellularTest, GetModemStatus);
Darin Petkove9d12e02011-07-27 15:09:37 -0700176 FRIEND_TEST(CellularTest, GetStateString);
177 FRIEND_TEST(CellularTest, GetTypeString);
Darin Petkovbec79a22011-08-01 14:47:17 -0700178 FRIEND_TEST(CellularTest, InitProxiesCDMA);
179 FRIEND_TEST(CellularTest, InitProxiesGSM);
Darin Petkovbac96002011-08-09 13:22:00 -0700180 FRIEND_TEST(CellularTest, StartConnected);
Darin Petkova1e0a1c2011-08-25 15:08:33 -0700181 FRIEND_TEST(CellularTest, StartCDMARegister);
Darin Petkov9bac6fe2011-08-26 12:49:05 -0700182 FRIEND_TEST(CellularTest, StartGSMRegister);
Darin Petkov0828f5f2011-08-11 10:18:52 -0700183 FRIEND_TEST(CellularTest, StartLinked);
Darin Petkovbac96002011-08-09 13:22:00 -0700184
Darin Petkov9bac6fe2011-08-26 12:49:05 -0700185 struct CDMA {
186 CDMA();
187
188 uint32 registration_state_evdo;
189 uint32 registration_state_1x;
190 uint32 activation_state;
191
192 uint16 prl_version;
193 std::string payment_url;
194 std::string usage_url;
195 };
196
197 struct GSM {
198 GSM();
199
200 uint32 registration_state;
201 uint32 access_technology;
202 std::string network_id;
203 std::string operator_name;
204 };
205
Darin Petkovbac96002011-08-09 13:22:00 -0700206 static const char kPhoneNumberCDMA[];
207 static const char kPhoneNumberGSM[];
Darin Petkove9d12e02011-07-27 15:09:37 -0700208
Darin Petkov0828f5f2011-08-11 10:18:52 -0700209 void SetState(State state);
210
Darin Petkovc5f56562011-08-06 16:40:05 -0700211 void ConnectTask(const DBusPropertiesMap &properties);
Darin Petkovc408e692011-08-17 13:47:15 -0700212 void ActivateTask(const std::string &carrier);
Darin Petkovc5f56562011-08-06 16:40:05 -0700213
Darin Petkovbac96002011-08-09 13:22:00 -0700214 // Invoked when the modem is connected to the cellular network to transition
215 // to the network-connected state and bring the network interface up.
216 void EstablishLink();
217
Chris Masone889666b2011-07-03 12:58:50 -0700218 Stringmaps EnumerateNetworks();
219 StrIntPair SimLockStatusToProperty();
220 void HelpRegisterDerivedStringmaps(const std::string &name,
221 Stringmaps(Cellular::*get)(void),
222 bool(Cellular::*set)(const Stringmaps&));
223 void HelpRegisterDerivedStrIntPair(const std::string &name,
224 StrIntPair(Cellular::*get)(void),
225 bool(Cellular::*set)(const StrIntPair&));
226
Darin Petkovbec79a22011-08-01 14:47:17 -0700227 void InitProxies();
228
Darin Petkovcc044422011-08-17 13:30:06 -0700229 std::string GetTypeString() const;
230 static std::string GetStateString(State state);
Darin Petkove9d12e02011-07-27 15:09:37 -0700231
Darin Petkovd2045802011-08-23 11:09:25 -0700232 // Returns an empty string if the network technology is unknown.
233 std::string GetNetworkTechnologyString() const;
234
235 std::string GetRoamingStateString() const;
236
Darin Petkovc408e692011-08-17 13:47:15 -0700237 static std::string GetCDMAActivationStateString(uint32 state);
Darin Petkov51489002011-08-18 13:13:20 -0700238 static std::string GetCDMAActivationErrorString(uint32 error);
Darin Petkovc408e692011-08-17 13:47:15 -0700239
Darin Petkovf5f61e02011-07-29 11:35:40 -0700240 void EnableModem();
241 void GetModemStatus();
Darin Petkovceb68172011-07-29 14:47:48 -0700242 void GetGSMProperties();
243 void RegisterGSMModem();
Darin Petkovceb68172011-07-29 14:47:48 -0700244
245 // Obtains the modem identifiers: MEID for CDMA; IMEI, IMSI, SPN and MSISDN
246 // for GSM.
247 void GetModemIdentifiers();
248
Darin Petkovd9661952011-08-03 16:25:42 -0700249 // Obtains modem's manufacturer, model ID, and hardware revision.
Darin Petkovceb68172011-07-29 14:47:48 -0700250 void GetModemInfo();
Darin Petkovf5f61e02011-07-29 11:35:40 -0700251
Darin Petkovbec79a22011-08-01 14:47:17 -0700252 void GetModemRegistrationState();
253 void GetCDMARegistrationState();
254 void GetGSMRegistrationState();
255
Darin Petkovd9661952011-08-03 16:25:42 -0700256 // Processes a change in the modem registration state, possibly creating,
257 // destroying or updating the CellularService.
258 void HandleNewRegistrationState();
Darin Petkov0828f5f2011-08-11 10:18:52 -0700259 void HandleNewRegistrationStateTask();
Darin Petkovd9661952011-08-03 16:25:42 -0700260
261 void CreateService();
262
263 void GetModemSignalQuality();
264 uint32 GetCDMASignalQuality();
265 uint32 GetGSMSignalQuality();
266
267 void HandleNewSignalQuality(uint32 strength);
268
Darin Petkovc408e692011-08-17 13:47:15 -0700269 void HandleNewCDMAActivationState(uint32 error);
270
Darin Petkovc5f56562011-08-06 16:40:05 -0700271 // Signal callbacks inherited from ModemCDMAProxyListener.
Darin Petkovb27e5442011-08-16 14:36:45 -0700272 virtual void OnCDMAActivationStateChanged(
273 uint32 activation_state,
274 uint32 activation_error,
275 const DBusPropertiesMap &status_changes);
Darin Petkovd9661952011-08-03 16:25:42 -0700276 virtual void OnCDMARegistrationStateChanged(uint32 state_1x,
277 uint32 state_evdo);
278 virtual void OnCDMASignalQualityChanged(uint32 strength);
279
Darin Petkova1e0a1c2011-08-25 15:08:33 -0700280 // Signal callbacks inherited from ModemGSMNetworkProxyListener.
281 virtual void OnGSMNetworkModeChanged(uint32 mode);
282 virtual void OnGSMRegistrationInfoChanged(uint32 status,
283 const std::string &operator_code,
284 const std::string &operator_name);
285 virtual void OnGSMSignalQualityChanged(uint32 quality);
286
Darin Petkovc5f56562011-08-06 16:40:05 -0700287 // Signal callbacks inherited from ModemProxyListener.
288 virtual void OnModemStateChanged(uint32 old_state,
289 uint32 new_state,
290 uint32 reason);
291
Darin Petkove9d12e02011-07-27 15:09:37 -0700292 Type type_;
293 State state_;
Darin Petkovbac96002011-08-09 13:22:00 -0700294 ModemState modem_state_;
Darin Petkove9d12e02011-07-27 15:09:37 -0700295
296 const std::string dbus_owner_; // ModemManager.Modem
297 const std::string dbus_path_; // ModemManager.Modem
298 scoped_ptr<ModemProxyInterface> proxy_;
Darin Petkove604f702011-07-28 15:51:17 -0700299 scoped_ptr<ModemSimpleProxyInterface> simple_proxy_;
Darin Petkovbec79a22011-08-01 14:47:17 -0700300 scoped_ptr<ModemCDMAProxyInterface> cdma_proxy_;
Darin Petkova1e0a1c2011-08-25 15:08:33 -0700301 scoped_ptr<ModemGSMNetworkProxyInterface> gsm_network_proxy_;
Darin Petkovbec79a22011-08-01 14:47:17 -0700302
303 CDMA cdma_;
Darin Petkov9bac6fe2011-08-26 12:49:05 -0700304 GSM gsm_;
Darin Petkove9d12e02011-07-27 15:09:37 -0700305
Darin Petkovd9661952011-08-03 16:25:42 -0700306 CellularServiceRefPtr service_;
Chris Masone853b81b2011-06-24 14:11:41 -0700307
Darin Petkovc5f56562011-08-06 16:40:05 -0700308 ScopedRunnableMethodFactory<Cellular> task_factory_;
309
Chris Masoneb925cc82011-06-22 15:39:57 -0700310 // Properties
311 bool allow_roaming_;
312 std::string carrier_;
313 std::string meid_;
314 std::string imei_;
315 std::string imsi_;
316 std::string esn_;
317 std::string mdn_;
318 std::string min_;
319 std::string model_id_;
320 std::string manufacturer_;
321 std::string firmware_revision_;
322 std::string hardware_revision_;
Chris Masoneb925cc82011-06-22 15:39:57 -0700323 bool scanning_;
324 uint16 scan_interval_;
Darin Petkova1e0a1c2011-08-25 15:08:33 -0700325 std::string selected_network_;
Chris Masone889666b2011-07-03 12:58:50 -0700326 std::vector<Network> found_networks_;
327 SimLockStatus sim_lock_status_;
Darin Petkov3335b372011-08-22 11:05:32 -0700328 Operator home_provider_;
Chris Masoneb925cc82011-06-22 15:39:57 -0700329
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700330 DISALLOW_COPY_AND_ASSIGN(Cellular);
331};
332
333} // namespace shill
334
335#endif // SHILL_CELLULAR_