blob: 90b915e5266db96231d833ecf4972a518020c61d [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
43 // measurements.
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
Chris Masone889666b2011-07-03 12:58:50 -070065 class Network {
66 public:
67 Network();
Darin Petkove9d12e02011-07-27 15:09:37 -070068 ~Network();
Chris Masone889666b2011-07-03 12:58:50 -070069
70 const std::string &GetStatus() const;
71 void SetStatus(const std::string &status);
72
73 const std::string &GetId() const;
74 void SetId(const std::string &id);
75
76 const std::string &GetShortName() const;
77 void SetShortName(const std::string &name);
78
79 const std::string &GetLongName() const;
80 void SetLongName(const std::string &name);
81
82 const std::string &GetTechnology() const;
83 void SetTechnology(const std::string &technology);
84
85 const Stringmap &ToDict() const;
86
87 private:
88 Stringmap dict_;
Darin Petkove9d12e02011-07-27 15:09:37 -070089
Chris Masone889666b2011-07-03 12:58:50 -070090 DISALLOW_COPY_AND_ASSIGN(Network);
91 };
92
Darin Petkovbec79a22011-08-01 14:47:17 -070093 struct CDMA {
94 CDMA();
95
96 uint32 registration_state_evdo;
97 uint32 registration_state_1x;
98 uint32 activation_state;
Darin Petkovcc044422011-08-17 13:30:06 -070099
100 uint16 prl_version;
101 std::string payment_url;
102 std::string usage_url;
Darin Petkovbec79a22011-08-01 14:47:17 -0700103 };
104
Chris Masone889666b2011-07-03 12:58:50 -0700105 struct SimLockStatus {
106 public:
Darin Petkove9d12e02011-07-27 15:09:37 -0700107 SimLockStatus() : retries_left(0) {}
108
Chris Masone889666b2011-07-03 12:58:50 -0700109 std::string lock_type;
110 uint32 retries_left;
111 };
112
Darin Petkovc5f56562011-08-06 16:40:05 -0700113 static const char kConnectPropertyPhoneNumber[];
114
Darin Petkove9d12e02011-07-27 15:09:37 -0700115 // |owner| is the ModemManager DBus service owner (e.g., ":1.17"). |path| is
116 // the ModemManager.Modem DBus object path (e.g.,
117 // "/org/chromium/ModemManager/Gobi/0").
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700118 Cellular(ControlInterface *control_interface,
119 EventDispatcher *dispatcher,
120 Manager *manager,
Darin Petkove9d12e02011-07-27 15:09:37 -0700121 const std::string &link_name,
122 int interface_index,
123 Type type,
124 const std::string &owner,
125 const std::string &path);
126 virtual ~Cellular();
127
Darin Petkovc5f56562011-08-06 16:40:05 -0700128 // Asynchronously connects the modem to the network.
129 void Connect();
130
Darin Petkovc408e692011-08-17 13:47:15 -0700131 // Asynchronously activates the modem.
132 void Activate(const std::string &carrier);
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 Petkove9d12e02011-07-27 15:09:37 -0700137 // Inherited from Device.
138 virtual void Start();
139 virtual void Stop();
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700140 virtual bool TechnologyIs(Technology type) const;
Darin Petkov0828f5f2011-08-11 10:18:52 -0700141 virtual void LinkEvent(unsigned int flags, unsigned int change);
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700142
143 private:
Darin Petkovc408e692011-08-17 13:47:15 -0700144 FRIEND_TEST(CellularTest, Activate);
Darin Petkovc5f56562011-08-06 16:40:05 -0700145 FRIEND_TEST(CellularTest, Connect);
Darin Petkovc408e692011-08-17 13:47:15 -0700146 FRIEND_TEST(CellularTest, GetCDMAActivationStateString);
Darin Petkovbec79a22011-08-01 14:47:17 -0700147 FRIEND_TEST(CellularTest, GetCDMARegistrationState);
Darin Petkovd9661952011-08-03 16:25:42 -0700148 FRIEND_TEST(CellularTest, GetCDMASignalQuality);
Darin Petkovceb68172011-07-29 14:47:48 -0700149 FRIEND_TEST(CellularTest, GetModemInfo);
150 FRIEND_TEST(CellularTest, GetModemStatus);
Darin Petkove9d12e02011-07-27 15:09:37 -0700151 FRIEND_TEST(CellularTest, GetStateString);
152 FRIEND_TEST(CellularTest, GetTypeString);
Darin Petkovbec79a22011-08-01 14:47:17 -0700153 FRIEND_TEST(CellularTest, InitProxiesCDMA);
154 FRIEND_TEST(CellularTest, InitProxiesGSM);
Darin Petkove9d12e02011-07-27 15:09:37 -0700155 FRIEND_TEST(CellularTest, Start);
Darin Petkovbac96002011-08-09 13:22:00 -0700156 FRIEND_TEST(CellularTest, StartConnected);
Darin Petkov0828f5f2011-08-11 10:18:52 -0700157 FRIEND_TEST(CellularTest, StartLinked);
Darin Petkovbac96002011-08-09 13:22:00 -0700158 FRIEND_TEST(CellularTest, StartRegister);
159
Darin Petkovc408e692011-08-17 13:47:15 -0700160 static const char kActivationStateActivated[];
161 static const char kActivationStateActivating[];
162 static const char kActivationStateNotActivated[];
163 static const char kActivationStatePartiallyActivated[];
164 static const char kActivationStateUnknown[];
Darin Petkovbac96002011-08-09 13:22:00 -0700165 static const char kPhoneNumberCDMA[];
166 static const char kPhoneNumberGSM[];
Darin Petkove9d12e02011-07-27 15:09:37 -0700167
Darin Petkov0828f5f2011-08-11 10:18:52 -0700168 void SetState(State state);
169
Darin Petkovc5f56562011-08-06 16:40:05 -0700170 void ConnectTask(const DBusPropertiesMap &properties);
Darin Petkovc408e692011-08-17 13:47:15 -0700171 void ActivateTask(const std::string &carrier);
Darin Petkovc5f56562011-08-06 16:40:05 -0700172
Darin Petkovbac96002011-08-09 13:22:00 -0700173 // Invoked when the modem is connected to the cellular network to transition
174 // to the network-connected state and bring the network interface up.
175 void EstablishLink();
176
Chris Masone889666b2011-07-03 12:58:50 -0700177 Stringmaps EnumerateNetworks();
178 StrIntPair SimLockStatusToProperty();
179 void HelpRegisterDerivedStringmaps(const std::string &name,
180 Stringmaps(Cellular::*get)(void),
181 bool(Cellular::*set)(const Stringmaps&));
182 void HelpRegisterDerivedStrIntPair(const std::string &name,
183 StrIntPair(Cellular::*get)(void),
184 bool(Cellular::*set)(const StrIntPair&));
185
Darin Petkovbec79a22011-08-01 14:47:17 -0700186 void InitProxies();
187
Darin Petkovcc044422011-08-17 13:30:06 -0700188 std::string GetTypeString() const;
189 static std::string GetStateString(State state);
Darin Petkove9d12e02011-07-27 15:09:37 -0700190
Darin Petkovc408e692011-08-17 13:47:15 -0700191 static std::string GetCDMAActivationStateString(uint32 state);
192
Darin Petkovf5f61e02011-07-29 11:35:40 -0700193 void EnableModem();
194 void GetModemStatus();
Darin Petkovceb68172011-07-29 14:47:48 -0700195 void GetGSMProperties();
196 void RegisterGSMModem();
Darin Petkovceb68172011-07-29 14:47:48 -0700197
198 // Obtains the modem identifiers: MEID for CDMA; IMEI, IMSI, SPN and MSISDN
199 // for GSM.
200 void GetModemIdentifiers();
201
Darin Petkovd9661952011-08-03 16:25:42 -0700202 // Obtains modem's manufacturer, model ID, and hardware revision.
Darin Petkovceb68172011-07-29 14:47:48 -0700203 void GetModemInfo();
Darin Petkovf5f61e02011-07-29 11:35:40 -0700204
Darin Petkovbec79a22011-08-01 14:47:17 -0700205 void GetModemRegistrationState();
206 void GetCDMARegistrationState();
207 void GetGSMRegistrationState();
208
Darin Petkovd9661952011-08-03 16:25:42 -0700209 // Processes a change in the modem registration state, possibly creating,
210 // destroying or updating the CellularService.
211 void HandleNewRegistrationState();
Darin Petkov0828f5f2011-08-11 10:18:52 -0700212 void HandleNewRegistrationStateTask();
Darin Petkovd9661952011-08-03 16:25:42 -0700213
214 void CreateService();
215
216 void GetModemSignalQuality();
217 uint32 GetCDMASignalQuality();
218 uint32 GetGSMSignalQuality();
219
220 void HandleNewSignalQuality(uint32 strength);
221
Darin Petkovc408e692011-08-17 13:47:15 -0700222 void HandleNewCDMAActivationState(uint32 error);
223
Darin Petkovd9661952011-08-03 16:25:42 -0700224 // Returns true if the modem is registered. Note that this method looks at the
225 // latest CDMA/GSM registration info obtained from the modem rather than the
226 // device |state_|.
227 bool IsModemRegistered();
228
Darin Petkovc5f56562011-08-06 16:40:05 -0700229 // Signal callbacks inherited from ModemCDMAProxyListener.
Darin Petkovb27e5442011-08-16 14:36:45 -0700230 virtual void OnCDMAActivationStateChanged(
231 uint32 activation_state,
232 uint32 activation_error,
233 const DBusPropertiesMap &status_changes);
Darin Petkovd9661952011-08-03 16:25:42 -0700234 virtual void OnCDMARegistrationStateChanged(uint32 state_1x,
235 uint32 state_evdo);
236 virtual void OnCDMASignalQualityChanged(uint32 strength);
237
Darin Petkovc5f56562011-08-06 16:40:05 -0700238 // Signal callbacks inherited from ModemProxyListener.
239 virtual void OnModemStateChanged(uint32 old_state,
240 uint32 new_state,
241 uint32 reason);
242
Darin Petkove9d12e02011-07-27 15:09:37 -0700243 Type type_;
244 State state_;
Darin Petkovbac96002011-08-09 13:22:00 -0700245 ModemState modem_state_;
Darin Petkove9d12e02011-07-27 15:09:37 -0700246
247 const std::string dbus_owner_; // ModemManager.Modem
248 const std::string dbus_path_; // ModemManager.Modem
249 scoped_ptr<ModemProxyInterface> proxy_;
Darin Petkove604f702011-07-28 15:51:17 -0700250 scoped_ptr<ModemSimpleProxyInterface> simple_proxy_;
Darin Petkovbec79a22011-08-01 14:47:17 -0700251 scoped_ptr<ModemCDMAProxyInterface> cdma_proxy_;
252
253 CDMA cdma_;
Darin Petkove9d12e02011-07-27 15:09:37 -0700254
Darin Petkovd9661952011-08-03 16:25:42 -0700255 CellularServiceRefPtr service_;
Chris Masone853b81b2011-06-24 14:11:41 -0700256
Darin Petkovc5f56562011-08-06 16:40:05 -0700257 ScopedRunnableMethodFactory<Cellular> task_factory_;
258
Chris Masoneb925cc82011-06-22 15:39:57 -0700259 // Properties
260 bool allow_roaming_;
261 std::string carrier_;
262 std::string meid_;
263 std::string imei_;
264 std::string imsi_;
265 std::string esn_;
266 std::string mdn_;
267 std::string min_;
268 std::string model_id_;
269 std::string manufacturer_;
270 std::string firmware_revision_;
271 std::string hardware_revision_;
Chris Masoneb925cc82011-06-22 15:39:57 -0700272 bool scanning_;
273 uint16 scan_interval_;
Chris Masone889666b2011-07-03 12:58:50 -0700274 std::vector<Network> found_networks_;
275 SimLockStatus sim_lock_status_;
Chris Masoneb925cc82011-06-22 15:39:57 -0700276
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700277 DISALLOW_COPY_AND_ASSIGN(Cellular);
278};
279
280} // namespace shill
281
282#endif // SHILL_CELLULAR_