blob: a273d8aa9c3820c3883daef336a4dd135f11b74d [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
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 Petkov51489002011-08-18 13:13:20 -0700145 FRIEND_TEST(CellularTest, ActivateError);
Darin Petkovc5f56562011-08-06 16:40:05 -0700146 FRIEND_TEST(CellularTest, Connect);
Darin Petkovc408e692011-08-17 13:47:15 -0700147 FRIEND_TEST(CellularTest, GetCDMAActivationStateString);
Darin Petkov51489002011-08-18 13:13:20 -0700148 FRIEND_TEST(CellularTest, GetCDMAActivationErrorString);
Darin Petkovbec79a22011-08-01 14:47:17 -0700149 FRIEND_TEST(CellularTest, GetCDMARegistrationState);
Darin Petkovd9661952011-08-03 16:25:42 -0700150 FRIEND_TEST(CellularTest, GetCDMASignalQuality);
Darin Petkovceb68172011-07-29 14:47:48 -0700151 FRIEND_TEST(CellularTest, GetModemInfo);
152 FRIEND_TEST(CellularTest, GetModemStatus);
Darin Petkove9d12e02011-07-27 15:09:37 -0700153 FRIEND_TEST(CellularTest, GetStateString);
154 FRIEND_TEST(CellularTest, GetTypeString);
Darin Petkovbec79a22011-08-01 14:47:17 -0700155 FRIEND_TEST(CellularTest, InitProxiesCDMA);
156 FRIEND_TEST(CellularTest, InitProxiesGSM);
Darin Petkove9d12e02011-07-27 15:09:37 -0700157 FRIEND_TEST(CellularTest, Start);
Darin Petkovbac96002011-08-09 13:22:00 -0700158 FRIEND_TEST(CellularTest, StartConnected);
Darin Petkov0828f5f2011-08-11 10:18:52 -0700159 FRIEND_TEST(CellularTest, StartLinked);
Darin Petkovbac96002011-08-09 13:22:00 -0700160 FRIEND_TEST(CellularTest, StartRegister);
161
162 static const char kPhoneNumberCDMA[];
163 static const char kPhoneNumberGSM[];
Darin Petkove9d12e02011-07-27 15:09:37 -0700164
Darin Petkov0828f5f2011-08-11 10:18:52 -0700165 void SetState(State state);
166
Darin Petkovc5f56562011-08-06 16:40:05 -0700167 void ConnectTask(const DBusPropertiesMap &properties);
Darin Petkovc408e692011-08-17 13:47:15 -0700168 void ActivateTask(const std::string &carrier);
Darin Petkovc5f56562011-08-06 16:40:05 -0700169
Darin Petkovbac96002011-08-09 13:22:00 -0700170 // Invoked when the modem is connected to the cellular network to transition
171 // to the network-connected state and bring the network interface up.
172 void EstablishLink();
173
Chris Masone889666b2011-07-03 12:58:50 -0700174 Stringmaps EnumerateNetworks();
175 StrIntPair SimLockStatusToProperty();
176 void HelpRegisterDerivedStringmaps(const std::string &name,
177 Stringmaps(Cellular::*get)(void),
178 bool(Cellular::*set)(const Stringmaps&));
179 void HelpRegisterDerivedStrIntPair(const std::string &name,
180 StrIntPair(Cellular::*get)(void),
181 bool(Cellular::*set)(const StrIntPair&));
182
Darin Petkovbec79a22011-08-01 14:47:17 -0700183 void InitProxies();
184
Darin Petkovcc044422011-08-17 13:30:06 -0700185 std::string GetTypeString() const;
186 static std::string GetStateString(State state);
Darin Petkove9d12e02011-07-27 15:09:37 -0700187
Darin Petkovc408e692011-08-17 13:47:15 -0700188 static std::string GetCDMAActivationStateString(uint32 state);
Darin Petkov51489002011-08-18 13:13:20 -0700189 static std::string GetCDMAActivationErrorString(uint32 error);
Darin Petkovc408e692011-08-17 13:47:15 -0700190
Darin Petkovf5f61e02011-07-29 11:35:40 -0700191 void EnableModem();
192 void GetModemStatus();
Darin Petkovceb68172011-07-29 14:47:48 -0700193 void GetGSMProperties();
194 void RegisterGSMModem();
Darin Petkovceb68172011-07-29 14:47:48 -0700195
196 // Obtains the modem identifiers: MEID for CDMA; IMEI, IMSI, SPN and MSISDN
197 // for GSM.
198 void GetModemIdentifiers();
199
Darin Petkovd9661952011-08-03 16:25:42 -0700200 // Obtains modem's manufacturer, model ID, and hardware revision.
Darin Petkovceb68172011-07-29 14:47:48 -0700201 void GetModemInfo();
Darin Petkovf5f61e02011-07-29 11:35:40 -0700202
Darin Petkovbec79a22011-08-01 14:47:17 -0700203 void GetModemRegistrationState();
204 void GetCDMARegistrationState();
205 void GetGSMRegistrationState();
206
Darin Petkovd9661952011-08-03 16:25:42 -0700207 // Processes a change in the modem registration state, possibly creating,
208 // destroying or updating the CellularService.
209 void HandleNewRegistrationState();
Darin Petkov0828f5f2011-08-11 10:18:52 -0700210 void HandleNewRegistrationStateTask();
Darin Petkovd9661952011-08-03 16:25:42 -0700211
212 void CreateService();
213
214 void GetModemSignalQuality();
215 uint32 GetCDMASignalQuality();
216 uint32 GetGSMSignalQuality();
217
218 void HandleNewSignalQuality(uint32 strength);
219
Darin Petkovc408e692011-08-17 13:47:15 -0700220 void HandleNewCDMAActivationState(uint32 error);
221
Darin Petkovd9661952011-08-03 16:25:42 -0700222 // Returns true if the modem is registered. Note that this method looks at the
223 // latest CDMA/GSM registration info obtained from the modem rather than the
224 // device |state_|.
225 bool IsModemRegistered();
226
Darin Petkovc5f56562011-08-06 16:40:05 -0700227 // Signal callbacks inherited from ModemCDMAProxyListener.
Darin Petkovb27e5442011-08-16 14:36:45 -0700228 virtual void OnCDMAActivationStateChanged(
229 uint32 activation_state,
230 uint32 activation_error,
231 const DBusPropertiesMap &status_changes);
Darin Petkovd9661952011-08-03 16:25:42 -0700232 virtual void OnCDMARegistrationStateChanged(uint32 state_1x,
233 uint32 state_evdo);
234 virtual void OnCDMASignalQualityChanged(uint32 strength);
235
Darin Petkovc5f56562011-08-06 16:40:05 -0700236 // Signal callbacks inherited from ModemProxyListener.
237 virtual void OnModemStateChanged(uint32 old_state,
238 uint32 new_state,
239 uint32 reason);
240
Darin Petkove9d12e02011-07-27 15:09:37 -0700241 Type type_;
242 State state_;
Darin Petkovbac96002011-08-09 13:22:00 -0700243 ModemState modem_state_;
Darin Petkove9d12e02011-07-27 15:09:37 -0700244
245 const std::string dbus_owner_; // ModemManager.Modem
246 const std::string dbus_path_; // ModemManager.Modem
247 scoped_ptr<ModemProxyInterface> proxy_;
Darin Petkove604f702011-07-28 15:51:17 -0700248 scoped_ptr<ModemSimpleProxyInterface> simple_proxy_;
Darin Petkovbec79a22011-08-01 14:47:17 -0700249 scoped_ptr<ModemCDMAProxyInterface> cdma_proxy_;
250
251 CDMA cdma_;
Darin Petkove9d12e02011-07-27 15:09:37 -0700252
Darin Petkovd9661952011-08-03 16:25:42 -0700253 CellularServiceRefPtr service_;
Chris Masone853b81b2011-06-24 14:11:41 -0700254
Darin Petkovc5f56562011-08-06 16:40:05 -0700255 ScopedRunnableMethodFactory<Cellular> task_factory_;
256
Chris Masoneb925cc82011-06-22 15:39:57 -0700257 // Properties
258 bool allow_roaming_;
259 std::string carrier_;
260 std::string meid_;
261 std::string imei_;
262 std::string imsi_;
263 std::string esn_;
264 std::string mdn_;
265 std::string min_;
266 std::string model_id_;
267 std::string manufacturer_;
268 std::string firmware_revision_;
269 std::string hardware_revision_;
Chris Masoneb925cc82011-06-22 15:39:57 -0700270 bool scanning_;
271 uint16 scan_interval_;
Chris Masone889666b2011-07-03 12:58:50 -0700272 std::vector<Network> found_networks_;
273 SimLockStatus sim_lock_status_;
Chris Masoneb925cc82011-06-22 15:39:57 -0700274
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700275 DISALLOW_COPY_AND_ASSIGN(Cellular);
276};
277
278} // namespace shill
279
280#endif // SHILL_CELLULAR_