blob: ba59a76fc36f57a6b5b4922022250394f37c086e [file] [log] [blame]
Darin Petkovc64fe5e2012-01-11 12:46:13 +01001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Chris Masone3bd3c8c2011-06-13 08:20:26 -07002// 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>
Jason Glasgow82f9ab32012-04-04 14:27:19 -04009#include <vector>
Chris Masone3bd3c8c2011-06-13 08:20:26 -070010
11#include <base/basictypes.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"
Paul Stewart26b327e2011-10-19 11:38:09 -070016#include "shill/event_dispatcher.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
Darin Petkov137884a2011-10-26 18:52:47 +020020struct mobile_provider_db;
21
Chris Masone3bd3c8c2011-06-13 08:20:26 -070022namespace shill {
23
Darin Petkovdaf43862011-10-27 11:37:28 +020024class CellularCapability;
Darin Petkov4d6d9412011-08-24 13:19:54 -070025class Error;
Darin Petkovab565bb2011-10-06 02:55:51 -070026class ProxyFactory;
Darin Petkove9d12e02011-07-27 15:09:37 -070027
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050028class Cellular : public Device {
Chris Masone3bd3c8c2011-06-13 08:20:26 -070029 public:
Darin Petkove9d12e02011-07-27 15:09:37 -070030 enum Type {
31 kTypeGSM,
David Rochbergfa1d31d2012-03-20 10:38:07 -040032 kTypeCDMA,
33 kTypeUniversal, // ModemManager1
34 kTypeInvalid,
Darin Petkove9d12e02011-07-27 15:09:37 -070035 };
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 enum ModemState {
55 kModemStateUnknown = 0,
Eric Shienbrood7fce52c2012-04-13 19:11:02 -040056 kModemStateInitializing = 1,
57 kModemStateLocked = 2,
58 kModemStateDisabled = 3,
David Rochbergfa1d31d2012-03-20 10:38:07 -040059 kModemStateDisabling = 4,
60 kModemStateEnabling = 5,
61 kModemStateEnabled = 6,
62 kModemStateSearching = 7,
63 kModemStateRegistered = 8,
64 kModemStateDisconnecting = 9,
65 kModemStateConnecting = 10,
66 kModemStateConnected = 11,
Darin Petkovbac96002011-08-09 13:22:00 -070067 };
68
Darin Petkov3335b372011-08-22 11:05:32 -070069 class Operator {
70 public:
71 Operator();
72 ~Operator();
73
74 void CopyFrom(const Operator &oper);
Darin Petkov9cb02682012-01-28 00:17:38 +010075 bool Equals(const Operator &oper) const { return dict_ == oper.dict_; }
Darin Petkov3335b372011-08-22 11:05:32 -070076
77 const std::string &GetName() const;
78 void SetName(const std::string &name);
79
80 const std::string &GetCode() const;
81 void SetCode(const std::string &code);
82
83 const std::string &GetCountry() const;
84 void SetCountry(const std::string &country);
85
86 const Stringmap &ToDict() const;
87
88 private:
89 Stringmap dict_;
90
91 DISALLOW_COPY_AND_ASSIGN(Operator);
92 };
93
Jason Glasgowa585fc32012-06-06 11:04:09 -040094 // |owner| is the ModemManager DBus service owner (e.g., ":1.17").
95 // |path| is the ModemManager.Modem DBus object path (e.g.,
Darin Petkove9d12e02011-07-27 15:09:37 -070096 // "/org/chromium/ModemManager/Gobi/0").
Jason Glasgowa585fc32012-06-06 11:04:09 -040097 // |service| is the modem mananager service name (e.g.,
98 // /org/freeDesktop/ModemManager, /org/freedesktop/ModemManager1
99 // or /org/chromium/ModemManager).
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700100 Cellular(ControlInterface *control_interface,
101 EventDispatcher *dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -0800102 Metrics *metrics,
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700103 Manager *manager,
Darin Petkove9d12e02011-07-27 15:09:37 -0700104 const std::string &link_name,
Chris Masone626719f2011-08-18 16:58:48 -0700105 const std::string &address,
Darin Petkove9d12e02011-07-27 15:09:37 -0700106 int interface_index,
107 Type type,
108 const std::string &owner,
Jason Glasgowa585fc32012-06-06 11:04:09 -0400109 const std::string &service,
Darin Petkov137884a2011-10-26 18:52:47 +0200110 const std::string &path,
111 mobile_provider_db *provider_db);
Darin Petkove9d12e02011-07-27 15:09:37 -0700112 virtual ~Cellular();
113
Jason Glasgow7b461df2012-05-01 16:38:45 -0400114 // Load configuration for the device from |storage|.
115 virtual bool Load(StoreInterface *storage);
116
117 // Save configuration for the device to |storage|.
118 virtual bool Save(StoreInterface *storage);
119
Darin Petkov4d6d9412011-08-24 13:19:54 -0700120 // Asynchronously connects the modem to the network. Populates |error| on
121 // failure, leaves it unchanged otherwise.
122 void Connect(Error *error);
Darin Petkovc5f56562011-08-06 16:40:05 -0700123
Darin Petkovfb0625e2012-01-16 13:05:56 +0100124 // Asynchronously disconnects the modem from the network. Populates |error| on
125 // failure, leaves it unchanged otherwise.
126 void Disconnect(Error *error);
127
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500128 // Asynchronously activates the modem. Returns an error on failure.
Eric Shienbrood9a245532012-03-07 14:20:39 -0500129 void Activate(const std::string &carrier, Error *error,
130 const ResultCallback &callback);
Darin Petkovc408e692011-08-17 13:47:15 -0700131
Darin Petkov3e509242011-11-10 14:46:44 +0100132 const CellularServiceRefPtr &service() const { return service_; }
133
Darin Petkova0a0efe2012-06-27 12:50:01 +0200134 // Deregisters and destructs the current service and destroys the connection,
135 // if any. This also eliminates the circular references between this device
136 // and the associated service, allowing eventual device destruction.
137 virtual void DestroyService();
138
Darin Petkova3d3be52011-11-14 21:34:16 +0100139 static std::string GetStateString(State state);
140
Darin Petkovac635a82012-01-10 16:51:58 +0100141 std::string CreateFriendlyServiceName();
142
Darin Petkova3d3be52011-11-14 21:34:16 +0100143 State state() const { return state_; }
144
Darin Petkovbac96002011-08-09 13:22:00 -0700145 void set_modem_state(ModemState state) { modem_state_ = state; }
146 ModemState modem_state() const { return modem_state_; }
Eric Shienbrood7fce52c2012-04-13 19:11:02 -0400147 bool IsUnderlyingDeviceEnabled() const;
Thieu Led0012052012-07-25 16:09:09 -0700148 bool IsModemRegistered() const;
Eric Shienbrood7fce52c2012-04-13 19:11:02 -0400149 static bool IsEnabledModemState(ModemState state);
Darin Petkovbac96002011-08-09 13:22:00 -0700150
Darin Petkov1272a432011-11-10 15:53:37 +0100151 mobile_provider_db *provider_db() const { return provider_db_; }
152
Darin Petkovdaf43862011-10-27 11:37:28 +0200153 const std::string &dbus_owner() const { return dbus_owner_; }
154 const std::string &dbus_path() const { return dbus_path_; }
155
Darin Petkovae0c64e2011-11-15 15:50:27 +0100156 const Operator &home_provider() const { return home_provider_; }
157 void set_home_provider(const Operator &oper);
Darin Petkov184c54e2011-11-15 12:44:39 +0100158
Darin Petkov3e509242011-11-10 14:46:44 +0100159 void HandleNewSignalQuality(uint32 strength);
160
Darin Petkov184c54e2011-11-15 12:44:39 +0100161 // Processes a change in the modem registration state, possibly creating,
162 // destroying or updating the CellularService.
163 void HandleNewRegistrationState();
164
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400165 virtual void OnDBusPropertiesChanged(
166 const std::string &interface,
167 const DBusPropertiesMap &changed_properties,
168 const std::vector<std::string> &invalidated_properties);
Darin Petkov184c54e2011-11-15 12:44:39 +0100169
Darin Petkove9d12e02011-07-27 15:09:37 -0700170 // Inherited from Device.
Eric Shienbrood9a245532012-03-07 14:20:39 -0500171 virtual void Start(Error *error, const EnabledStateChangedCallback &callback);
172 virtual void Stop(Error *error, const EnabledStateChangedCallback &callback);
Darin Petkov0828f5f2011-08-11 10:18:52 -0700173 virtual void LinkEvent(unsigned int flags, unsigned int change);
Darin Petkovc0865312011-09-16 15:31:20 -0700174 virtual void Scan(Error *error);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500175 virtual void RegisterOnNetwork(const std::string &network_id,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500176 Error *error,
177 const ResultCallback &callback);
178 virtual void RequirePIN(const std::string &pin, bool require,
179 Error *error, const ResultCallback &callback);
180 virtual void EnterPIN(const std::string &pin,
181 Error *error, const ResultCallback &callback);
Darin Petkove42e1012011-08-31 12:35:04 -0700182 virtual void UnblockPIN(const std::string &unblock_code,
183 const std::string &pin,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500184 Error *error, const ResultCallback &callback);
Darin Petkove42e1012011-08-31 12:35:04 -0700185 virtual void ChangePIN(const std::string &old_pin,
186 const std::string &new_pin,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500187 Error *error, const ResultCallback &callback);
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700188
Thieu Le37b90032012-05-15 15:18:41 -0700189 void StartModemCallback(const EnabledStateChangedCallback &callback,
190 const Error &error);
191 void StopModemCallback(const EnabledStateChangedCallback &callback,
192 const Error &error);
Eric Shienbrood7fce52c2012-04-13 19:11:02 -0400193 void OnConnecting();
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500194 void OnConnected();
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400195 void OnConnectFailed(const Error &error);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500196 void OnDisconnected();
197 void OnDisconnectFailed();
Eric Shienbrood0db6a9b2012-03-30 16:11:39 -0400198 std::string GetTechnologyFamily(Error *error);
Eric Shienbrood7fce52c2012-04-13 19:11:02 -0400199 void OnModemStateChanged(ModemState old_state,
200 ModemState new_state,
201 uint32 reason);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500202
Jason Glasgow7b461df2012-05-01 16:38:45 -0400203 // accessor to read the allow roaming property
204 bool allow_roaming_property() const { return allow_roaming_; }
205
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700206 private:
Darin Petkovab565bb2011-10-06 02:55:51 -0700207 friend class CellularTest;
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500208 friend class CellularCapabilityTest;
Darin Petkov20c13ec2011-11-09 15:07:15 +0100209 friend class CellularCapabilityCDMATest;
210 friend class CellularCapabilityGSMTest;
Jason Glasgowef965562012-04-10 16:12:35 -0400211 friend class CellularCapabilityUniversalTest;
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400212 friend class CellularServiceTest;
Darin Petkov721ac932011-11-16 15:43:09 +0100213 friend class ModemTest;
Darin Petkovac635a82012-01-10 16:51:58 +0100214 FRIEND_TEST(CellularCapabilityCDMATest, CreateFriendlyServiceName);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500215 FRIEND_TEST(CellularCapabilityCDMATest, GetRegistrationState);
Darin Petkovac635a82012-01-10 16:51:58 +0100216 FRIEND_TEST(CellularCapabilityGSMTest, CreateFriendlyServiceName);
217 FRIEND_TEST(CellularServiceTest, FriendlyName);
Darin Petkov3335b372011-08-22 11:05:32 -0700218 FRIEND_TEST(CellularTest, CreateService);
Darin Petkovc5f56562011-08-06 16:40:05 -0700219 FRIEND_TEST(CellularTest, Connect);
Gary Moraina9fb3252012-05-31 12:05:31 -0700220 FRIEND_TEST(CellularTest, ConnectAddsTerminationAction);
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400221 FRIEND_TEST(CellularTest, ConnectFailure);
Thieu Leb5954a22012-05-18 10:37:34 -0700222 FRIEND_TEST(CellularTest, ConnectFailureNoService);
Darin Petkovfb0625e2012-01-16 13:05:56 +0100223 FRIEND_TEST(CellularTest, DisableModem);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500224 FRIEND_TEST(CellularTest, Disconnect);
Eric Shienbrood7fce52c2012-04-13 19:11:02 -0400225 FRIEND_TEST(CellularTest, ModemStateChangeDisable);
226 FRIEND_TEST(CellularTest, ModemStateChangeEnable);
Thieu Led0012052012-07-25 16:09:09 -0700227 FRIEND_TEST(CellularTest, ModemStateChangeStaleConnected);
228 FRIEND_TEST(CellularTest, ModemStateChangeValidConnected);
Darin Petkove7c6ad32012-06-29 10:22:09 +0200229 FRIEND_TEST(CellularTest, SetAllowRoaming);
Thieu Le37b90032012-05-15 15:18:41 -0700230 FRIEND_TEST(CellularTest, StartModemCallback);
231 FRIEND_TEST(CellularTest, StartModemCallbackFail);
232 FRIEND_TEST(CellularTest, StopModemCallback);
233 FRIEND_TEST(CellularTest, StopModemCallbackFail);
Darin Petkovbac96002011-08-09 13:22:00 -0700234 FRIEND_TEST(CellularTest, StartConnected);
Darin Petkova1e0a1c2011-08-25 15:08:33 -0700235 FRIEND_TEST(CellularTest, StartCDMARegister);
Darin Petkov9bac6fe2011-08-26 12:49:05 -0700236 FRIEND_TEST(CellularTest, StartGSMRegister);
Darin Petkov0828f5f2011-08-11 10:18:52 -0700237 FRIEND_TEST(CellularTest, StartLinked);
Darin Petkov9c1dcef2012-02-07 15:58:26 +0100238 FRIEND_TEST(CellularCapabilityTest, AllowRoaming);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500239 FRIEND_TEST(CellularCapabilityTest, EnableModemFail);
240 FRIEND_TEST(CellularCapabilityTest, EnableModemSucceed);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500241 FRIEND_TEST(CellularCapabilityTest, FinishEnable);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500242 FRIEND_TEST(CellularCapabilityTest, GetModemInfo);
243 FRIEND_TEST(CellularCapabilityTest, GetModemStatus);
Jason Glasgow02401cc2012-05-16 10:35:37 -0400244 FRIEND_TEST(CellularCapabilityUniversalTest, StopModemConnected);
Jason Glasgow14521872012-05-07 19:12:15 -0400245 FRIEND_TEST(CellularCapabilityUniversalTest, Connect);
Darin Petkove9d12e02011-07-27 15:09:37 -0700246
Jason Glasgow7b461df2012-05-01 16:38:45 -0400247 // Names of properties in storage
248 static const char kAllowRoaming[];
249
Darin Petkov0828f5f2011-08-11 10:18:52 -0700250 void SetState(State state);
251
Darin Petkovbac96002011-08-09 13:22:00 -0700252 // Invoked when the modem is connected to the cellular network to transition
253 // to the network-connected state and bring the network interface up.
254 void EstablishLink();
255
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500256 void InitCapability(Type type, ProxyFactory *proxy_factory);
Darin Petkovf5f61e02011-07-29 11:35:40 -0700257
Darin Petkovd9661952011-08-03 16:25:42 -0700258 void CreateService();
259
Eric Shienbrood0db6a9b2012-03-30 16:11:39 -0400260 // HelpRegisterDerived*: Expose a property over RPC, with the name |name|.
261 //
262 // Reads of the property will be handled by invoking |get|.
263 // Writes to the property will be handled by invoking |set|.
264 // Clearing the property will be handled by PropertyStore.
Jason Glasgow7b461df2012-05-01 16:38:45 -0400265 void HelpRegisterDerivedBool(
266 const std::string &name,
267 bool(Cellular::*get)(Error *error),
268 void(Cellular::*set)(const bool &value, Error *error));
Eric Shienbrood0db6a9b2012-03-30 16:11:39 -0400269 void HelpRegisterDerivedString(
270 const std::string &name,
271 std::string(Cellular::*get)(Error *error),
272 void(Cellular::*set)(const std::string &value, Error *error));
273
Nathan Williamsb54974f2012-04-19 11:16:30 -0400274 void OnConnectReply(const Error &error);
275 void OnDisconnectReply(const Error &error);
276
Jason Glasgow7b461df2012-05-01 16:38:45 -0400277 // DBUS accessors to read/modify the allow roaming property
278 bool GetAllowRoaming(Error */*error*/) { return allow_roaming_; }
279 void SetAllowRoaming(const bool &value, Error *error);
280
Gary Moraina9fb3252012-05-31 12:05:31 -0700281 // When shill terminates or ChromeOS suspends, this function is called to
282 // disconnect from the cellular network.
283 void StartTermination();
284
Darin Petkove9d12e02011-07-27 15:09:37 -0700285 State state_;
Darin Petkovbac96002011-08-09 13:22:00 -0700286 ModemState modem_state_;
Darin Petkove9d12e02011-07-27 15:09:37 -0700287
Darin Petkovdaf43862011-10-27 11:37:28 +0200288 scoped_ptr<CellularCapability> capability_;
289
Jason Glasgowa585fc32012-06-06 11:04:09 -0400290 const std::string dbus_owner_; // :x.y
291 const std::string dbus_service_; // org.*.ModemManager*
Darin Petkove9d12e02011-07-27 15:09:37 -0700292 const std::string dbus_path_; // ModemManager.Modem
Darin Petkovbec79a22011-08-01 14:47:17 -0700293
Darin Petkov137884a2011-10-26 18:52:47 +0200294 mobile_provider_db *provider_db_;
295
Darin Petkovd9661952011-08-03 16:25:42 -0700296 CellularServiceRefPtr service_;
Chris Masone853b81b2011-06-24 14:11:41 -0700297
Chris Masoneb925cc82011-06-22 15:39:57 -0700298 // Properties
Darin Petkov3335b372011-08-22 11:05:32 -0700299 Operator home_provider_;
Chris Masoneb925cc82011-06-22 15:39:57 -0700300
Jason Glasgow7b461df2012-05-01 16:38:45 -0400301 // User preference to allow or disallow roaming
302 bool allow_roaming_;
303
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700304 DISALLOW_COPY_AND_ASSIGN(Cellular);
305};
306
307} // namespace shill
308
309#endif // SHILL_CELLULAR_