blob: c3ac21f8771b470dadeb6707d95b383e8733d51d [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 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#include "shill/shill_event.h"
20
21namespace shill {
22
Darin Petkove604f702011-07-28 15:51:17 -070023class ModemSimpleProxyInterface;
Darin Petkove9d12e02011-07-27 15:09:37 -070024
Darin Petkovd9661952011-08-03 16:25:42 -070025class Cellular : public Device,
Darin Petkovc5f56562011-08-06 16:40:05 -070026 public ModemCDMAProxyListener,
27 public ModemProxyListener {
Chris Masone3bd3c8c2011-06-13 08:20:26 -070028 public:
Darin Petkove9d12e02011-07-27 15:09:37 -070029 enum Type {
30 kTypeGSM,
31 kTypeCDMA
32 };
33
Darin Petkov0828f5f2011-08-11 10:18:52 -070034 // The device states progress linearly from Disabled to Linked.
Darin Petkove9d12e02011-07-27 15:09:37 -070035 enum State {
Darin Petkov0828f5f2011-08-11 10:18:52 -070036 // This is the initial state of the modem and indicates that the modem radio
37 // is not turned on.
Darin Petkove9d12e02011-07-27 15:09:37 -070038 kStateDisabled,
Darin Petkov0828f5f2011-08-11 10:18:52 -070039 // This state indicates that the modem radio is turned on, and it should be
40 // possible to measure signal strength.
Darin Petkove9d12e02011-07-27 15:09:37 -070041 kStateEnabled,
Darin Petkov0828f5f2011-08-11 10:18:52 -070042 // The modem has registered with a network and has signal quality
Darin Petkov51489002011-08-18 13:13:20 -070043 // measurements. A cellular service object is created.
Darin Petkove9d12e02011-07-27 15:09:37 -070044 kStateRegistered,
Darin Petkov0828f5f2011-08-11 10:18:52 -070045 // The modem has connected to a network.
Darin Petkove9d12e02011-07-27 15:09:37 -070046 kStateConnected,
Darin Petkov0828f5f2011-08-11 10:18:52 -070047 // The network interface is UP.
48 kStateLinked,
Darin Petkove9d12e02011-07-27 15:09:37 -070049 };
50
Darin Petkovbac96002011-08-09 13:22:00 -070051 // These should be kept in sync with ModemManager's mm-modem.h:MMModemState.
52 enum ModemState {
53 kModemStateUnknown = 0,
54 kModemStateDisabled = 10,
55 kModemStateDisabling = 20,
56 kModemStateEnabling = 30,
57 kModemStateEnabled = 40,
58 kModemStateSearching = 50,
59 kModemStateRegistered = 60,
60 kModemStateDisconnecting = 70,
61 kModemStateConnecting = 80,
62 kModemStateConnected = 90,
63 };
64
Darin Petkov3335b372011-08-22 11:05:32 -070065 class Operator {
66 public:
67 Operator();
68 ~Operator();
69
70 void CopyFrom(const Operator &oper);
71
72 const std::string &GetName() const;
73 void SetName(const std::string &name);
74
75 const std::string &GetCode() const;
76 void SetCode(const std::string &code);
77
78 const std::string &GetCountry() const;
79 void SetCountry(const std::string &country);
80
81 const Stringmap &ToDict() const;
82
83 private:
84 Stringmap dict_;
85
86 DISALLOW_COPY_AND_ASSIGN(Operator);
87 };
88
Chris Masone889666b2011-07-03 12:58:50 -070089 class Network {
90 public:
91 Network();
Darin Petkove9d12e02011-07-27 15:09:37 -070092 ~Network();
Chris Masone889666b2011-07-03 12:58:50 -070093
94 const std::string &GetStatus() const;
95 void SetStatus(const std::string &status);
96
97 const std::string &GetId() const;
98 void SetId(const std::string &id);
99
100 const std::string &GetShortName() const;
101 void SetShortName(const std::string &name);
102
103 const std::string &GetLongName() const;
104 void SetLongName(const std::string &name);
105
106 const std::string &GetTechnology() const;
107 void SetTechnology(const std::string &technology);
108
109 const Stringmap &ToDict() const;
110
111 private:
112 Stringmap dict_;
Darin Petkove9d12e02011-07-27 15:09:37 -0700113
Chris Masone889666b2011-07-03 12:58:50 -0700114 DISALLOW_COPY_AND_ASSIGN(Network);
115 };
116
Darin Petkovbec79a22011-08-01 14:47:17 -0700117 struct CDMA {
118 CDMA();
119
120 uint32 registration_state_evdo;
121 uint32 registration_state_1x;
122 uint32 activation_state;
Darin Petkovcc044422011-08-17 13:30:06 -0700123
124 uint16 prl_version;
125 std::string payment_url;
126 std::string usage_url;
Darin Petkovbec79a22011-08-01 14:47:17 -0700127 };
128
Chris Masone889666b2011-07-03 12:58:50 -0700129 struct SimLockStatus {
130 public:
Darin Petkove9d12e02011-07-27 15:09:37 -0700131 SimLockStatus() : retries_left(0) {}
132
Chris Masone889666b2011-07-03 12:58:50 -0700133 std::string lock_type;
134 uint32 retries_left;
135 };
136
Darin Petkovc5f56562011-08-06 16:40:05 -0700137 static const char kConnectPropertyPhoneNumber[];
138
Darin Petkove9d12e02011-07-27 15:09:37 -0700139 // |owner| is the ModemManager DBus service owner (e.g., ":1.17"). |path| is
140 // the ModemManager.Modem DBus object path (e.g.,
141 // "/org/chromium/ModemManager/Gobi/0").
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700142 Cellular(ControlInterface *control_interface,
143 EventDispatcher *dispatcher,
144 Manager *manager,
Darin Petkove9d12e02011-07-27 15:09:37 -0700145 const std::string &link_name,
Chris Masone626719f2011-08-18 16:58:48 -0700146 const std::string &address,
Darin Petkove9d12e02011-07-27 15:09:37 -0700147 int interface_index,
148 Type type,
149 const std::string &owner,
150 const std::string &path);
151 virtual ~Cellular();
152
Darin Petkovc5f56562011-08-06 16:40:05 -0700153 // Asynchronously connects the modem to the network.
154 void Connect();
155
Darin Petkovc408e692011-08-17 13:47:15 -0700156 // Asynchronously activates the modem.
157 void Activate(const std::string &carrier);
158
Darin Petkovbac96002011-08-09 13:22:00 -0700159 void set_modem_state(ModemState state) { modem_state_ = state; }
160 ModemState modem_state() const { return modem_state_; }
161
Darin Petkove9d12e02011-07-27 15:09:37 -0700162 // Inherited from Device.
163 virtual void Start();
164 virtual void Stop();
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700165 virtual bool TechnologyIs(Technology type) const;
Darin Petkov0828f5f2011-08-11 10:18:52 -0700166 virtual void LinkEvent(unsigned int flags, unsigned int change);
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700167
168 private:
Darin Petkovc408e692011-08-17 13:47:15 -0700169 FRIEND_TEST(CellularTest, Activate);
Darin Petkov51489002011-08-18 13:13:20 -0700170 FRIEND_TEST(CellularTest, ActivateError);
Darin Petkov3335b372011-08-22 11:05:32 -0700171 FRIEND_TEST(CellularTest, CreateService);
Darin Petkovc5f56562011-08-06 16:40:05 -0700172 FRIEND_TEST(CellularTest, Connect);
Darin Petkovc408e692011-08-17 13:47:15 -0700173 FRIEND_TEST(CellularTest, GetCDMAActivationStateString);
Darin Petkov51489002011-08-18 13:13:20 -0700174 FRIEND_TEST(CellularTest, GetCDMAActivationErrorString);
Darin Petkovbec79a22011-08-01 14:47:17 -0700175 FRIEND_TEST(CellularTest, GetCDMARegistrationState);
Darin Petkovd9661952011-08-03 16:25:42 -0700176 FRIEND_TEST(CellularTest, GetCDMASignalQuality);
Darin Petkovceb68172011-07-29 14:47:48 -0700177 FRIEND_TEST(CellularTest, GetModemInfo);
178 FRIEND_TEST(CellularTest, GetModemStatus);
Darin Petkove9d12e02011-07-27 15:09:37 -0700179 FRIEND_TEST(CellularTest, GetStateString);
180 FRIEND_TEST(CellularTest, GetTypeString);
Darin Petkovbec79a22011-08-01 14:47:17 -0700181 FRIEND_TEST(CellularTest, InitProxiesCDMA);
182 FRIEND_TEST(CellularTest, InitProxiesGSM);
Darin Petkove9d12e02011-07-27 15:09:37 -0700183 FRIEND_TEST(CellularTest, Start);
Darin Petkovbac96002011-08-09 13:22:00 -0700184 FRIEND_TEST(CellularTest, StartConnected);
Darin Petkov0828f5f2011-08-11 10:18:52 -0700185 FRIEND_TEST(CellularTest, StartLinked);
Darin Petkovbac96002011-08-09 13:22:00 -0700186 FRIEND_TEST(CellularTest, StartRegister);
187
188 static const char kPhoneNumberCDMA[];
189 static const char kPhoneNumberGSM[];
Darin Petkove9d12e02011-07-27 15:09:37 -0700190
Darin Petkov0828f5f2011-08-11 10:18:52 -0700191 void SetState(State state);
192
Darin Petkovc5f56562011-08-06 16:40:05 -0700193 void ConnectTask(const DBusPropertiesMap &properties);
Darin Petkovc408e692011-08-17 13:47:15 -0700194 void ActivateTask(const std::string &carrier);
Darin Petkovc5f56562011-08-06 16:40:05 -0700195
Darin Petkovbac96002011-08-09 13:22:00 -0700196 // Invoked when the modem is connected to the cellular network to transition
197 // to the network-connected state and bring the network interface up.
198 void EstablishLink();
199
Chris Masone889666b2011-07-03 12:58:50 -0700200 Stringmaps EnumerateNetworks();
201 StrIntPair SimLockStatusToProperty();
202 void HelpRegisterDerivedStringmaps(const std::string &name,
203 Stringmaps(Cellular::*get)(void),
204 bool(Cellular::*set)(const Stringmaps&));
205 void HelpRegisterDerivedStrIntPair(const std::string &name,
206 StrIntPair(Cellular::*get)(void),
207 bool(Cellular::*set)(const StrIntPair&));
208
Darin Petkovbec79a22011-08-01 14:47:17 -0700209 void InitProxies();
210
Darin Petkovcc044422011-08-17 13:30:06 -0700211 std::string GetTypeString() const;
212 static std::string GetStateString(State state);
Darin Petkove9d12e02011-07-27 15:09:37 -0700213
Darin Petkovc408e692011-08-17 13:47:15 -0700214 static std::string GetCDMAActivationStateString(uint32 state);
Darin Petkov51489002011-08-18 13:13:20 -0700215 static std::string GetCDMAActivationErrorString(uint32 error);
Darin Petkovc408e692011-08-17 13:47:15 -0700216
Darin Petkovf5f61e02011-07-29 11:35:40 -0700217 void EnableModem();
218 void GetModemStatus();
Darin Petkovceb68172011-07-29 14:47:48 -0700219 void GetGSMProperties();
220 void RegisterGSMModem();
Darin Petkovceb68172011-07-29 14:47:48 -0700221
222 // Obtains the modem identifiers: MEID for CDMA; IMEI, IMSI, SPN and MSISDN
223 // for GSM.
224 void GetModemIdentifiers();
225
Darin Petkovd9661952011-08-03 16:25:42 -0700226 // Obtains modem's manufacturer, model ID, and hardware revision.
Darin Petkovceb68172011-07-29 14:47:48 -0700227 void GetModemInfo();
Darin Petkovf5f61e02011-07-29 11:35:40 -0700228
Darin Petkovbec79a22011-08-01 14:47:17 -0700229 void GetModemRegistrationState();
230 void GetCDMARegistrationState();
231 void GetGSMRegistrationState();
232
Darin Petkovd9661952011-08-03 16:25:42 -0700233 // Processes a change in the modem registration state, possibly creating,
234 // destroying or updating the CellularService.
235 void HandleNewRegistrationState();
Darin Petkov0828f5f2011-08-11 10:18:52 -0700236 void HandleNewRegistrationStateTask();
Darin Petkovd9661952011-08-03 16:25:42 -0700237
238 void CreateService();
239
240 void GetModemSignalQuality();
241 uint32 GetCDMASignalQuality();
242 uint32 GetGSMSignalQuality();
243
244 void HandleNewSignalQuality(uint32 strength);
245
Darin Petkovc408e692011-08-17 13:47:15 -0700246 void HandleNewCDMAActivationState(uint32 error);
247
Darin Petkovd9661952011-08-03 16:25:42 -0700248 // Returns true if the modem is registered. Note that this method looks at the
249 // latest CDMA/GSM registration info obtained from the modem rather than the
250 // device |state_|.
251 bool IsModemRegistered();
252
Darin Petkovc5f56562011-08-06 16:40:05 -0700253 // Signal callbacks inherited from ModemCDMAProxyListener.
Darin Petkovb27e5442011-08-16 14:36:45 -0700254 virtual void OnCDMAActivationStateChanged(
255 uint32 activation_state,
256 uint32 activation_error,
257 const DBusPropertiesMap &status_changes);
Darin Petkovd9661952011-08-03 16:25:42 -0700258 virtual void OnCDMARegistrationStateChanged(uint32 state_1x,
259 uint32 state_evdo);
260 virtual void OnCDMASignalQualityChanged(uint32 strength);
261
Darin Petkovc5f56562011-08-06 16:40:05 -0700262 // Signal callbacks inherited from ModemProxyListener.
263 virtual void OnModemStateChanged(uint32 old_state,
264 uint32 new_state,
265 uint32 reason);
266
Darin Petkove9d12e02011-07-27 15:09:37 -0700267 Type type_;
268 State state_;
Darin Petkovbac96002011-08-09 13:22:00 -0700269 ModemState modem_state_;
Darin Petkove9d12e02011-07-27 15:09:37 -0700270
271 const std::string dbus_owner_; // ModemManager.Modem
272 const std::string dbus_path_; // ModemManager.Modem
273 scoped_ptr<ModemProxyInterface> proxy_;
Darin Petkove604f702011-07-28 15:51:17 -0700274 scoped_ptr<ModemSimpleProxyInterface> simple_proxy_;
Darin Petkovbec79a22011-08-01 14:47:17 -0700275 scoped_ptr<ModemCDMAProxyInterface> cdma_proxy_;
276
277 CDMA cdma_;
Darin Petkove9d12e02011-07-27 15:09:37 -0700278
Darin Petkovd9661952011-08-03 16:25:42 -0700279 CellularServiceRefPtr service_;
Chris Masone853b81b2011-06-24 14:11:41 -0700280
Darin Petkovc5f56562011-08-06 16:40:05 -0700281 ScopedRunnableMethodFactory<Cellular> task_factory_;
282
Chris Masoneb925cc82011-06-22 15:39:57 -0700283 // Properties
284 bool allow_roaming_;
285 std::string carrier_;
286 std::string meid_;
287 std::string imei_;
288 std::string imsi_;
289 std::string esn_;
290 std::string mdn_;
291 std::string min_;
292 std::string model_id_;
293 std::string manufacturer_;
294 std::string firmware_revision_;
295 std::string hardware_revision_;
Chris Masoneb925cc82011-06-22 15:39:57 -0700296 bool scanning_;
297 uint16 scan_interval_;
Chris Masone889666b2011-07-03 12:58:50 -0700298 std::vector<Network> found_networks_;
299 SimLockStatus sim_lock_status_;
Darin Petkov3335b372011-08-22 11:05:32 -0700300 Operator home_provider_;
Chris Masoneb925cc82011-06-22 15:39:57 -0700301
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700302 DISALLOW_COPY_AND_ASSIGN(Cellular);
303};
304
305} // namespace shill
306
307#endif // SHILL_CELLULAR_