blob: 54f1aaa2f4ffb8e80f218d4a8765f2fb8cd3bb72 [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,
Chris Masone626719f2011-08-18 16:58:48 -0700122 const std::string &address,
Darin Petkove9d12e02011-07-27 15:09:37 -0700123 int interface_index,
124 Type type,
125 const std::string &owner,
126 const std::string &path);
127 virtual ~Cellular();
128
Darin Petkovc5f56562011-08-06 16:40:05 -0700129 // Asynchronously connects the modem to the network.
130 void Connect();
131
Darin Petkovc408e692011-08-17 13:47:15 -0700132 // Asynchronously activates the modem.
133 void Activate(const std::string &carrier);
134
Darin Petkovbac96002011-08-09 13:22:00 -0700135 void set_modem_state(ModemState state) { modem_state_ = state; }
136 ModemState modem_state() const { return modem_state_; }
137
Darin Petkove9d12e02011-07-27 15:09:37 -0700138 // Inherited from Device.
139 virtual void Start();
140 virtual void Stop();
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700141 virtual bool TechnologyIs(Technology type) const;
Darin Petkov0828f5f2011-08-11 10:18:52 -0700142 virtual void LinkEvent(unsigned int flags, unsigned int change);
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700143
144 private:
Darin Petkovc408e692011-08-17 13:47:15 -0700145 FRIEND_TEST(CellularTest, Activate);
Darin Petkov51489002011-08-18 13:13:20 -0700146 FRIEND_TEST(CellularTest, ActivateError);
Darin Petkovc5f56562011-08-06 16:40:05 -0700147 FRIEND_TEST(CellularTest, Connect);
Darin Petkovc408e692011-08-17 13:47:15 -0700148 FRIEND_TEST(CellularTest, GetCDMAActivationStateString);
Darin Petkov51489002011-08-18 13:13:20 -0700149 FRIEND_TEST(CellularTest, GetCDMAActivationErrorString);
Darin Petkovbec79a22011-08-01 14:47:17 -0700150 FRIEND_TEST(CellularTest, GetCDMARegistrationState);
Darin Petkovd9661952011-08-03 16:25:42 -0700151 FRIEND_TEST(CellularTest, GetCDMASignalQuality);
Darin Petkovceb68172011-07-29 14:47:48 -0700152 FRIEND_TEST(CellularTest, GetModemInfo);
153 FRIEND_TEST(CellularTest, GetModemStatus);
Darin Petkove9d12e02011-07-27 15:09:37 -0700154 FRIEND_TEST(CellularTest, GetStateString);
155 FRIEND_TEST(CellularTest, GetTypeString);
Darin Petkovbec79a22011-08-01 14:47:17 -0700156 FRIEND_TEST(CellularTest, InitProxiesCDMA);
157 FRIEND_TEST(CellularTest, InitProxiesGSM);
Darin Petkove9d12e02011-07-27 15:09:37 -0700158 FRIEND_TEST(CellularTest, Start);
Darin Petkovbac96002011-08-09 13:22:00 -0700159 FRIEND_TEST(CellularTest, StartConnected);
Darin Petkov0828f5f2011-08-11 10:18:52 -0700160 FRIEND_TEST(CellularTest, StartLinked);
Darin Petkovbac96002011-08-09 13:22:00 -0700161 FRIEND_TEST(CellularTest, StartRegister);
162
163 static const char kPhoneNumberCDMA[];
164 static const char kPhoneNumberGSM[];
Darin Petkove9d12e02011-07-27 15:09:37 -0700165
Darin Petkov0828f5f2011-08-11 10:18:52 -0700166 void SetState(State state);
167
Darin Petkovc5f56562011-08-06 16:40:05 -0700168 void ConnectTask(const DBusPropertiesMap &properties);
Darin Petkovc408e692011-08-17 13:47:15 -0700169 void ActivateTask(const std::string &carrier);
Darin Petkovc5f56562011-08-06 16:40:05 -0700170
Darin Petkovbac96002011-08-09 13:22:00 -0700171 // Invoked when the modem is connected to the cellular network to transition
172 // to the network-connected state and bring the network interface up.
173 void EstablishLink();
174
Chris Masone889666b2011-07-03 12:58:50 -0700175 Stringmaps EnumerateNetworks();
176 StrIntPair SimLockStatusToProperty();
177 void HelpRegisterDerivedStringmaps(const std::string &name,
178 Stringmaps(Cellular::*get)(void),
179 bool(Cellular::*set)(const Stringmaps&));
180 void HelpRegisterDerivedStrIntPair(const std::string &name,
181 StrIntPair(Cellular::*get)(void),
182 bool(Cellular::*set)(const StrIntPair&));
183
Darin Petkovbec79a22011-08-01 14:47:17 -0700184 void InitProxies();
185
Darin Petkovcc044422011-08-17 13:30:06 -0700186 std::string GetTypeString() const;
187 static std::string GetStateString(State state);
Darin Petkove9d12e02011-07-27 15:09:37 -0700188
Darin Petkovc408e692011-08-17 13:47:15 -0700189 static std::string GetCDMAActivationStateString(uint32 state);
Darin Petkov51489002011-08-18 13:13:20 -0700190 static std::string GetCDMAActivationErrorString(uint32 error);
Darin Petkovc408e692011-08-17 13:47:15 -0700191
Darin Petkovf5f61e02011-07-29 11:35:40 -0700192 void EnableModem();
193 void GetModemStatus();
Darin Petkovceb68172011-07-29 14:47:48 -0700194 void GetGSMProperties();
195 void RegisterGSMModem();
Darin Petkovceb68172011-07-29 14:47:48 -0700196
197 // Obtains the modem identifiers: MEID for CDMA; IMEI, IMSI, SPN and MSISDN
198 // for GSM.
199 void GetModemIdentifiers();
200
Darin Petkovd9661952011-08-03 16:25:42 -0700201 // Obtains modem's manufacturer, model ID, and hardware revision.
Darin Petkovceb68172011-07-29 14:47:48 -0700202 void GetModemInfo();
Darin Petkovf5f61e02011-07-29 11:35:40 -0700203
Darin Petkovbec79a22011-08-01 14:47:17 -0700204 void GetModemRegistrationState();
205 void GetCDMARegistrationState();
206 void GetGSMRegistrationState();
207
Darin Petkovd9661952011-08-03 16:25:42 -0700208 // Processes a change in the modem registration state, possibly creating,
209 // destroying or updating the CellularService.
210 void HandleNewRegistrationState();
Darin Petkov0828f5f2011-08-11 10:18:52 -0700211 void HandleNewRegistrationStateTask();
Darin Petkovd9661952011-08-03 16:25:42 -0700212
213 void CreateService();
214
215 void GetModemSignalQuality();
216 uint32 GetCDMASignalQuality();
217 uint32 GetGSMSignalQuality();
218
219 void HandleNewSignalQuality(uint32 strength);
220
Darin Petkovc408e692011-08-17 13:47:15 -0700221 void HandleNewCDMAActivationState(uint32 error);
222
Darin Petkovd9661952011-08-03 16:25:42 -0700223 // Returns true if the modem is registered. Note that this method looks at the
224 // latest CDMA/GSM registration info obtained from the modem rather than the
225 // device |state_|.
226 bool IsModemRegistered();
227
Darin Petkovc5f56562011-08-06 16:40:05 -0700228 // Signal callbacks inherited from ModemCDMAProxyListener.
Darin Petkovb27e5442011-08-16 14:36:45 -0700229 virtual void OnCDMAActivationStateChanged(
230 uint32 activation_state,
231 uint32 activation_error,
232 const DBusPropertiesMap &status_changes);
Darin Petkovd9661952011-08-03 16:25:42 -0700233 virtual void OnCDMARegistrationStateChanged(uint32 state_1x,
234 uint32 state_evdo);
235 virtual void OnCDMASignalQualityChanged(uint32 strength);
236
Darin Petkovc5f56562011-08-06 16:40:05 -0700237 // Signal callbacks inherited from ModemProxyListener.
238 virtual void OnModemStateChanged(uint32 old_state,
239 uint32 new_state,
240 uint32 reason);
241
Darin Petkove9d12e02011-07-27 15:09:37 -0700242 Type type_;
243 State state_;
Darin Petkovbac96002011-08-09 13:22:00 -0700244 ModemState modem_state_;
Darin Petkove9d12e02011-07-27 15:09:37 -0700245
246 const std::string dbus_owner_; // ModemManager.Modem
247 const std::string dbus_path_; // ModemManager.Modem
248 scoped_ptr<ModemProxyInterface> proxy_;
Darin Petkove604f702011-07-28 15:51:17 -0700249 scoped_ptr<ModemSimpleProxyInterface> simple_proxy_;
Darin Petkovbec79a22011-08-01 14:47:17 -0700250 scoped_ptr<ModemCDMAProxyInterface> cdma_proxy_;
251
252 CDMA cdma_;
Darin Petkove9d12e02011-07-27 15:09:37 -0700253
Darin Petkovd9661952011-08-03 16:25:42 -0700254 CellularServiceRefPtr service_;
Chris Masone853b81b2011-06-24 14:11:41 -0700255
Darin Petkovc5f56562011-08-06 16:40:05 -0700256 ScopedRunnableMethodFactory<Cellular> task_factory_;
257
Chris Masoneb925cc82011-06-22 15:39:57 -0700258 // Properties
259 bool allow_roaming_;
260 std::string carrier_;
261 std::string meid_;
262 std::string imei_;
263 std::string imsi_;
264 std::string esn_;
265 std::string mdn_;
266 std::string min_;
267 std::string model_id_;
268 std::string manufacturer_;
269 std::string firmware_revision_;
270 std::string hardware_revision_;
Chris Masoneb925cc82011-06-22 15:39:57 -0700271 bool scanning_;
272 uint16 scan_interval_;
Chris Masone889666b2011-07-03 12:58:50 -0700273 std::vector<Network> found_networks_;
274 SimLockStatus sim_lock_status_;
Chris Masoneb925cc82011-06-22 15:39:57 -0700275
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700276 DISALLOW_COPY_AND_ASSIGN(Cellular);
277};
278
279} // namespace shill
280
281#endif // SHILL_CELLULAR_