blob: da5d5f8d84ab6a3bad53afb4da2b751afdc6bff3 [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
Darin Petkovbec79a22011-08-01 14:47:17 -0700120 struct CDMA {
121 CDMA();
122
123 uint32 registration_state_evdo;
124 uint32 registration_state_1x;
125 uint32 activation_state;
Darin Petkovcc044422011-08-17 13:30:06 -0700126
127 uint16 prl_version;
128 std::string payment_url;
129 std::string usage_url;
Darin Petkovbec79a22011-08-01 14:47:17 -0700130 };
131
Chris Masone889666b2011-07-03 12:58:50 -0700132 struct SimLockStatus {
133 public:
Darin Petkove9d12e02011-07-27 15:09:37 -0700134 SimLockStatus() : retries_left(0) {}
135
Chris Masone889666b2011-07-03 12:58:50 -0700136 std::string lock_type;
137 uint32 retries_left;
138 };
139
Darin Petkovc5f56562011-08-06 16:40:05 -0700140 static const char kConnectPropertyPhoneNumber[];
141
Darin Petkove9d12e02011-07-27 15:09:37 -0700142 // |owner| is the ModemManager DBus service owner (e.g., ":1.17"). |path| is
143 // the ModemManager.Modem DBus object path (e.g.,
144 // "/org/chromium/ModemManager/Gobi/0").
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700145 Cellular(ControlInterface *control_interface,
146 EventDispatcher *dispatcher,
147 Manager *manager,
Darin Petkove9d12e02011-07-27 15:09:37 -0700148 const std::string &link_name,
Chris Masone626719f2011-08-18 16:58:48 -0700149 const std::string &address,
Darin Petkove9d12e02011-07-27 15:09:37 -0700150 int interface_index,
151 Type type,
152 const std::string &owner,
153 const std::string &path);
154 virtual ~Cellular();
155
Darin Petkov4d6d9412011-08-24 13:19:54 -0700156 // Asynchronously connects the modem to the network. Populates |error| on
157 // failure, leaves it unchanged otherwise.
158 void Connect(Error *error);
Darin Petkovc5f56562011-08-06 16:40:05 -0700159
Darin Petkovb100ae72011-08-24 16:19:45 -0700160 // Asynchronously activates the modem. Populates |error| on failure, leaves it
161 // unchanged otherwise.
162 void Activate(const std::string &carrier, Error *error);
Darin Petkovc408e692011-08-17 13:47:15 -0700163
Darin Petkovbac96002011-08-09 13:22:00 -0700164 void set_modem_state(ModemState state) { modem_state_ = state; }
165 ModemState modem_state() const { return modem_state_; }
166
Darin Petkove9d12e02011-07-27 15:09:37 -0700167 // Inherited from Device.
168 virtual void Start();
169 virtual void Stop();
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700170 virtual bool TechnologyIs(Technology type) const;
Darin Petkov0828f5f2011-08-11 10:18:52 -0700171 virtual void LinkEvent(unsigned int flags, unsigned int change);
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700172
173 private:
Darin Petkovc408e692011-08-17 13:47:15 -0700174 FRIEND_TEST(CellularTest, Activate);
Darin Petkov51489002011-08-18 13:13:20 -0700175 FRIEND_TEST(CellularTest, ActivateError);
Darin Petkov3335b372011-08-22 11:05:32 -0700176 FRIEND_TEST(CellularTest, CreateService);
Darin Petkovc5f56562011-08-06 16:40:05 -0700177 FRIEND_TEST(CellularTest, Connect);
Darin Petkovc408e692011-08-17 13:47:15 -0700178 FRIEND_TEST(CellularTest, GetCDMAActivationStateString);
Darin Petkov51489002011-08-18 13:13:20 -0700179 FRIEND_TEST(CellularTest, GetCDMAActivationErrorString);
Darin Petkovbec79a22011-08-01 14:47:17 -0700180 FRIEND_TEST(CellularTest, GetCDMARegistrationState);
Darin Petkovd9661952011-08-03 16:25:42 -0700181 FRIEND_TEST(CellularTest, GetCDMASignalQuality);
Darin Petkovceb68172011-07-29 14:47:48 -0700182 FRIEND_TEST(CellularTest, GetModemInfo);
183 FRIEND_TEST(CellularTest, GetModemStatus);
Darin Petkovd2045802011-08-23 11:09:25 -0700184 FRIEND_TEST(CellularTest, GetNetworkTechnologyString);
185 FRIEND_TEST(CellularTest, GetRoamingStateString);
Darin Petkove9d12e02011-07-27 15:09:37 -0700186 FRIEND_TEST(CellularTest, GetStateString);
187 FRIEND_TEST(CellularTest, GetTypeString);
Darin Petkovbec79a22011-08-01 14:47:17 -0700188 FRIEND_TEST(CellularTest, InitProxiesCDMA);
189 FRIEND_TEST(CellularTest, InitProxiesGSM);
Darin Petkovbac96002011-08-09 13:22:00 -0700190 FRIEND_TEST(CellularTest, StartConnected);
Darin Petkova1e0a1c2011-08-25 15:08:33 -0700191 FRIEND_TEST(CellularTest, StartCDMARegister);
192 FRIEND_TEST(CellularTest, StartGSM);
Darin Petkov0828f5f2011-08-11 10:18:52 -0700193 FRIEND_TEST(CellularTest, StartLinked);
Darin Petkovbac96002011-08-09 13:22:00 -0700194
195 static const char kPhoneNumberCDMA[];
196 static const char kPhoneNumberGSM[];
Darin Petkove9d12e02011-07-27 15:09:37 -0700197
Darin Petkov0828f5f2011-08-11 10:18:52 -0700198 void SetState(State state);
199
Darin Petkovc5f56562011-08-06 16:40:05 -0700200 void ConnectTask(const DBusPropertiesMap &properties);
Darin Petkovc408e692011-08-17 13:47:15 -0700201 void ActivateTask(const std::string &carrier);
Darin Petkovc5f56562011-08-06 16:40:05 -0700202
Darin Petkovbac96002011-08-09 13:22:00 -0700203 // Invoked when the modem is connected to the cellular network to transition
204 // to the network-connected state and bring the network interface up.
205 void EstablishLink();
206
Chris Masone889666b2011-07-03 12:58:50 -0700207 Stringmaps EnumerateNetworks();
208 StrIntPair SimLockStatusToProperty();
209 void HelpRegisterDerivedStringmaps(const std::string &name,
210 Stringmaps(Cellular::*get)(void),
211 bool(Cellular::*set)(const Stringmaps&));
212 void HelpRegisterDerivedStrIntPair(const std::string &name,
213 StrIntPair(Cellular::*get)(void),
214 bool(Cellular::*set)(const StrIntPair&));
215
Darin Petkovbec79a22011-08-01 14:47:17 -0700216 void InitProxies();
217
Darin Petkovcc044422011-08-17 13:30:06 -0700218 std::string GetTypeString() const;
219 static std::string GetStateString(State state);
Darin Petkove9d12e02011-07-27 15:09:37 -0700220
Darin Petkovd2045802011-08-23 11:09:25 -0700221 // Returns an empty string if the network technology is unknown.
222 std::string GetNetworkTechnologyString() const;
223
224 std::string GetRoamingStateString() const;
225
Darin Petkovc408e692011-08-17 13:47:15 -0700226 static std::string GetCDMAActivationStateString(uint32 state);
Darin Petkov51489002011-08-18 13:13:20 -0700227 static std::string GetCDMAActivationErrorString(uint32 error);
Darin Petkovc408e692011-08-17 13:47:15 -0700228
Darin Petkovf5f61e02011-07-29 11:35:40 -0700229 void EnableModem();
230 void GetModemStatus();
Darin Petkovceb68172011-07-29 14:47:48 -0700231 void GetGSMProperties();
232 void RegisterGSMModem();
Darin Petkovceb68172011-07-29 14:47:48 -0700233
234 // Obtains the modem identifiers: MEID for CDMA; IMEI, IMSI, SPN and MSISDN
235 // for GSM.
236 void GetModemIdentifiers();
237
Darin Petkovd9661952011-08-03 16:25:42 -0700238 // Obtains modem's manufacturer, model ID, and hardware revision.
Darin Petkovceb68172011-07-29 14:47:48 -0700239 void GetModemInfo();
Darin Petkovf5f61e02011-07-29 11:35:40 -0700240
Darin Petkovbec79a22011-08-01 14:47:17 -0700241 void GetModemRegistrationState();
242 void GetCDMARegistrationState();
243 void GetGSMRegistrationState();
244
Darin Petkovd9661952011-08-03 16:25:42 -0700245 // Processes a change in the modem registration state, possibly creating,
246 // destroying or updating the CellularService.
247 void HandleNewRegistrationState();
Darin Petkov0828f5f2011-08-11 10:18:52 -0700248 void HandleNewRegistrationStateTask();
Darin Petkovd9661952011-08-03 16:25:42 -0700249
250 void CreateService();
251
252 void GetModemSignalQuality();
253 uint32 GetCDMASignalQuality();
254 uint32 GetGSMSignalQuality();
255
256 void HandleNewSignalQuality(uint32 strength);
257
Darin Petkovc408e692011-08-17 13:47:15 -0700258 void HandleNewCDMAActivationState(uint32 error);
259
Darin Petkovc5f56562011-08-06 16:40:05 -0700260 // Signal callbacks inherited from ModemCDMAProxyListener.
Darin Petkovb27e5442011-08-16 14:36:45 -0700261 virtual void OnCDMAActivationStateChanged(
262 uint32 activation_state,
263 uint32 activation_error,
264 const DBusPropertiesMap &status_changes);
Darin Petkovd9661952011-08-03 16:25:42 -0700265 virtual void OnCDMARegistrationStateChanged(uint32 state_1x,
266 uint32 state_evdo);
267 virtual void OnCDMASignalQualityChanged(uint32 strength);
268
Darin Petkova1e0a1c2011-08-25 15:08:33 -0700269 // Signal callbacks inherited from ModemGSMNetworkProxyListener.
270 virtual void OnGSMNetworkModeChanged(uint32 mode);
271 virtual void OnGSMRegistrationInfoChanged(uint32 status,
272 const std::string &operator_code,
273 const std::string &operator_name);
274 virtual void OnGSMSignalQualityChanged(uint32 quality);
275
Darin Petkovc5f56562011-08-06 16:40:05 -0700276 // Signal callbacks inherited from ModemProxyListener.
277 virtual void OnModemStateChanged(uint32 old_state,
278 uint32 new_state,
279 uint32 reason);
280
Darin Petkove9d12e02011-07-27 15:09:37 -0700281 Type type_;
282 State state_;
Darin Petkovbac96002011-08-09 13:22:00 -0700283 ModemState modem_state_;
Darin Petkove9d12e02011-07-27 15:09:37 -0700284
285 const std::string dbus_owner_; // ModemManager.Modem
286 const std::string dbus_path_; // ModemManager.Modem
287 scoped_ptr<ModemProxyInterface> proxy_;
Darin Petkove604f702011-07-28 15:51:17 -0700288 scoped_ptr<ModemSimpleProxyInterface> simple_proxy_;
Darin Petkovbec79a22011-08-01 14:47:17 -0700289 scoped_ptr<ModemCDMAProxyInterface> cdma_proxy_;
Darin Petkova1e0a1c2011-08-25 15:08:33 -0700290 scoped_ptr<ModemGSMNetworkProxyInterface> gsm_network_proxy_;
Darin Petkovbec79a22011-08-01 14:47:17 -0700291
292 CDMA cdma_;
Darin Petkove9d12e02011-07-27 15:09:37 -0700293
Darin Petkovd9661952011-08-03 16:25:42 -0700294 CellularServiceRefPtr service_;
Chris Masone853b81b2011-06-24 14:11:41 -0700295
Darin Petkovc5f56562011-08-06 16:40:05 -0700296 ScopedRunnableMethodFactory<Cellular> task_factory_;
297
Chris Masoneb925cc82011-06-22 15:39:57 -0700298 // Properties
299 bool allow_roaming_;
300 std::string carrier_;
301 std::string meid_;
302 std::string imei_;
303 std::string imsi_;
304 std::string esn_;
305 std::string mdn_;
306 std::string min_;
307 std::string model_id_;
308 std::string manufacturer_;
309 std::string firmware_revision_;
310 std::string hardware_revision_;
Chris Masoneb925cc82011-06-22 15:39:57 -0700311 bool scanning_;
312 uint16 scan_interval_;
Darin Petkova1e0a1c2011-08-25 15:08:33 -0700313 std::string selected_network_;
Chris Masone889666b2011-07-03 12:58:50 -0700314 std::vector<Network> found_networks_;
315 SimLockStatus sim_lock_status_;
Darin Petkov3335b372011-08-22 11:05:32 -0700316 Operator home_provider_;
Chris Masoneb925cc82011-06-22 15:39:57 -0700317
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700318 DISALLOW_COPY_AND_ASSIGN(Cellular);
319};
320
321} // namespace shill
322
323#endif // SHILL_CELLULAR_