blob: 4416499b340bea65c5b0d1e6669d14360049f2df [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>
Thieu Le64b0fe52012-08-08 14:57:36 -070012#include <base/memory/weak_ptr.h>
Darin Petkove9d12e02011-07-27 15:09:37 -070013#include <gtest/gtest_prod.h> // for FRIEND_TEST
Chris Masone3bd3c8c2011-06-13 08:20:26 -070014
Darin Petkovc5f56562011-08-06 16:40:05 -070015#include "shill/dbus_properties.h"
Chris Masone3bd3c8c2011-06-13 08:20:26 -070016#include "shill/device.h"
Paul Stewart26b327e2011-10-19 11:38:09 -070017#include "shill/event_dispatcher.h"
Darin Petkovc5f56562011-08-06 16:40:05 -070018#include "shill/modem_proxy_interface.h"
Chris Masone2b105542011-06-22 10:58:09 -070019#include "shill/refptr_types.h"
Chris Masone3bd3c8c2011-06-13 08:20:26 -070020
Darin Petkov137884a2011-10-26 18:52:47 +020021struct mobile_provider_db;
22
Chris Masone3bd3c8c2011-06-13 08:20:26 -070023namespace shill {
24
Darin Petkovdaf43862011-10-27 11:37:28 +020025class CellularCapability;
Darin Petkov4d6d9412011-08-24 13:19:54 -070026class Error;
Darin Petkovab565bb2011-10-06 02:55:51 -070027class ProxyFactory;
Darin Petkove9d12e02011-07-27 15:09:37 -070028
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050029class Cellular : public Device {
Chris Masone3bd3c8c2011-06-13 08:20:26 -070030 public:
Darin Petkove9d12e02011-07-27 15:09:37 -070031 enum Type {
32 kTypeGSM,
David Rochbergfa1d31d2012-03-20 10:38:07 -040033 kTypeCDMA,
34 kTypeUniversal, // ModemManager1
35 kTypeInvalid,
Darin Petkove9d12e02011-07-27 15:09:37 -070036 };
37
Darin Petkov0828f5f2011-08-11 10:18:52 -070038 // The device states progress linearly from Disabled to Linked.
Darin Petkove9d12e02011-07-27 15:09:37 -070039 enum State {
Darin Petkov0828f5f2011-08-11 10:18:52 -070040 // This is the initial state of the modem and indicates that the modem radio
41 // is not turned on.
Darin Petkove9d12e02011-07-27 15:09:37 -070042 kStateDisabled,
Darin Petkov0828f5f2011-08-11 10:18:52 -070043 // This state indicates that the modem radio is turned on, and it should be
44 // possible to measure signal strength.
Darin Petkove9d12e02011-07-27 15:09:37 -070045 kStateEnabled,
Darin Petkov0828f5f2011-08-11 10:18:52 -070046 // The modem has registered with a network and has signal quality
Darin Petkov51489002011-08-18 13:13:20 -070047 // measurements. A cellular service object is created.
Darin Petkove9d12e02011-07-27 15:09:37 -070048 kStateRegistered,
Darin Petkov0828f5f2011-08-11 10:18:52 -070049 // The modem has connected to a network.
Darin Petkove9d12e02011-07-27 15:09:37 -070050 kStateConnected,
Darin Petkov0828f5f2011-08-11 10:18:52 -070051 // The network interface is UP.
52 kStateLinked,
Darin Petkove9d12e02011-07-27 15:09:37 -070053 };
54
Darin Petkovbac96002011-08-09 13:22:00 -070055 enum ModemState {
56 kModemStateUnknown = 0,
Eric Shienbrood7fce52c2012-04-13 19:11:02 -040057 kModemStateInitializing = 1,
58 kModemStateLocked = 2,
59 kModemStateDisabled = 3,
David Rochbergfa1d31d2012-03-20 10:38:07 -040060 kModemStateDisabling = 4,
61 kModemStateEnabling = 5,
62 kModemStateEnabled = 6,
63 kModemStateSearching = 7,
64 kModemStateRegistered = 8,
65 kModemStateDisconnecting = 9,
66 kModemStateConnecting = 10,
67 kModemStateConnected = 11,
Darin Petkovbac96002011-08-09 13:22:00 -070068 };
69
Darin Petkov3335b372011-08-22 11:05:32 -070070 class Operator {
71 public:
72 Operator();
73 ~Operator();
74
75 void CopyFrom(const Operator &oper);
Darin Petkov9cb02682012-01-28 00:17:38 +010076 bool Equals(const Operator &oper) const { return dict_ == oper.dict_; }
Darin Petkov3335b372011-08-22 11:05:32 -070077
78 const std::string &GetName() const;
79 void SetName(const std::string &name);
80
81 const std::string &GetCode() const;
82 void SetCode(const std::string &code);
83
84 const std::string &GetCountry() const;
85 void SetCountry(const std::string &country);
86
87 const Stringmap &ToDict() const;
88
89 private:
90 Stringmap dict_;
91
92 DISALLOW_COPY_AND_ASSIGN(Operator);
93 };
94
Jason Glasgowa585fc32012-06-06 11:04:09 -040095 // |owner| is the ModemManager DBus service owner (e.g., ":1.17").
96 // |path| is the ModemManager.Modem DBus object path (e.g.,
Darin Petkove9d12e02011-07-27 15:09:37 -070097 // "/org/chromium/ModemManager/Gobi/0").
Jason Glasgowa585fc32012-06-06 11:04:09 -040098 // |service| is the modem mananager service name (e.g.,
99 // /org/freeDesktop/ModemManager, /org/freedesktop/ModemManager1
100 // or /org/chromium/ModemManager).
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700101 Cellular(ControlInterface *control_interface,
102 EventDispatcher *dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -0800103 Metrics *metrics,
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700104 Manager *manager,
Darin Petkove9d12e02011-07-27 15:09:37 -0700105 const std::string &link_name,
Chris Masone626719f2011-08-18 16:58:48 -0700106 const std::string &address,
Darin Petkove9d12e02011-07-27 15:09:37 -0700107 int interface_index,
108 Type type,
109 const std::string &owner,
Jason Glasgowa585fc32012-06-06 11:04:09 -0400110 const std::string &service,
Darin Petkov137884a2011-10-26 18:52:47 +0200111 const std::string &path,
Ben Chan3ecdf822012-08-06 12:29:23 -0700112 mobile_provider_db *provider_db,
113 ProxyFactory *proxy_factory);
Darin Petkove9d12e02011-07-27 15:09:37 -0700114 virtual ~Cellular();
115
Jason Glasgow7b461df2012-05-01 16:38:45 -0400116 // Load configuration for the device from |storage|.
117 virtual bool Load(StoreInterface *storage);
118
119 // Save configuration for the device to |storage|.
120 virtual bool Save(StoreInterface *storage);
121
Darin Petkov4d6d9412011-08-24 13:19:54 -0700122 // Asynchronously connects the modem to the network. Populates |error| on
123 // failure, leaves it unchanged otherwise.
124 void Connect(Error *error);
Darin Petkovc5f56562011-08-06 16:40:05 -0700125
Darin Petkovfb0625e2012-01-16 13:05:56 +0100126 // Asynchronously disconnects the modem from the network. Populates |error| on
127 // failure, leaves it unchanged otherwise.
128 void Disconnect(Error *error);
129
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500130 // Asynchronously activates the modem. Returns an error on failure.
Eric Shienbrood9a245532012-03-07 14:20:39 -0500131 void Activate(const std::string &carrier, Error *error,
132 const ResultCallback &callback);
Darin Petkovc408e692011-08-17 13:47:15 -0700133
Darin Petkov3e509242011-11-10 14:46:44 +0100134 const CellularServiceRefPtr &service() const { return service_; }
135
Darin Petkova0a0efe2012-06-27 12:50:01 +0200136 // Deregisters and destructs the current service and destroys the connection,
137 // if any. This also eliminates the circular references between this device
138 // and the associated service, allowing eventual device destruction.
139 virtual void DestroyService();
140
Darin Petkova3d3be52011-11-14 21:34:16 +0100141 static std::string GetStateString(State state);
142
Darin Petkovac635a82012-01-10 16:51:58 +0100143 std::string CreateFriendlyServiceName();
144
Darin Petkova3d3be52011-11-14 21:34:16 +0100145 State state() const { return state_; }
146
Darin Petkovbac96002011-08-09 13:22:00 -0700147 void set_modem_state(ModemState state) { modem_state_ = state; }
148 ModemState modem_state() const { return modem_state_; }
Eric Shienbrood7fce52c2012-04-13 19:11:02 -0400149 bool IsUnderlyingDeviceEnabled() const;
Thieu Led0012052012-07-25 16:09:09 -0700150 bool IsModemRegistered() const;
Eric Shienbrood7fce52c2012-04-13 19:11:02 -0400151 static bool IsEnabledModemState(ModemState state);
Darin Petkovbac96002011-08-09 13:22:00 -0700152
Darin Petkov1272a432011-11-10 15:53:37 +0100153 mobile_provider_db *provider_db() const { return provider_db_; }
154
Darin Petkovdaf43862011-10-27 11:37:28 +0200155 const std::string &dbus_owner() const { return dbus_owner_; }
156 const std::string &dbus_path() const { return dbus_path_; }
157
Darin Petkovae0c64e2011-11-15 15:50:27 +0100158 const Operator &home_provider() const { return home_provider_; }
159 void set_home_provider(const Operator &oper);
Darin Petkov184c54e2011-11-15 12:44:39 +0100160
Darin Petkov3e509242011-11-10 14:46:44 +0100161 void HandleNewSignalQuality(uint32 strength);
162
Darin Petkov184c54e2011-11-15 12:44:39 +0100163 // Processes a change in the modem registration state, possibly creating,
164 // destroying or updating the CellularService.
165 void HandleNewRegistrationState();
166
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400167 virtual void OnDBusPropertiesChanged(
168 const std::string &interface,
169 const DBusPropertiesMap &changed_properties,
170 const std::vector<std::string> &invalidated_properties);
Darin Petkov184c54e2011-11-15 12:44:39 +0100171
Darin Petkove9d12e02011-07-27 15:09:37 -0700172 // Inherited from Device.
Eric Shienbrood9a245532012-03-07 14:20:39 -0500173 virtual void Start(Error *error, const EnabledStateChangedCallback &callback);
174 virtual void Stop(Error *error, const EnabledStateChangedCallback &callback);
Darin Petkov0828f5f2011-08-11 10:18:52 -0700175 virtual void LinkEvent(unsigned int flags, unsigned int change);
Darin Petkovc0865312011-09-16 15:31:20 -0700176 virtual void Scan(Error *error);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500177 virtual void RegisterOnNetwork(const std::string &network_id,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500178 Error *error,
179 const ResultCallback &callback);
180 virtual void RequirePIN(const std::string &pin, bool require,
181 Error *error, const ResultCallback &callback);
182 virtual void EnterPIN(const std::string &pin,
183 Error *error, const ResultCallback &callback);
Darin Petkove42e1012011-08-31 12:35:04 -0700184 virtual void UnblockPIN(const std::string &unblock_code,
185 const std::string &pin,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500186 Error *error, const ResultCallback &callback);
Darin Petkove42e1012011-08-31 12:35:04 -0700187 virtual void ChangePIN(const std::string &old_pin,
188 const std::string &new_pin,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500189 Error *error, const ResultCallback &callback);
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700190
Thieu Le37b90032012-05-15 15:18:41 -0700191 void StartModemCallback(const EnabledStateChangedCallback &callback,
192 const Error &error);
193 void StopModemCallback(const EnabledStateChangedCallback &callback,
194 const Error &error);
Eric Shienbrood7fce52c2012-04-13 19:11:02 -0400195 void OnConnecting();
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500196 void OnConnected();
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400197 void OnConnectFailed(const Error &error);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500198 void OnDisconnected();
199 void OnDisconnectFailed();
Eric Shienbrood0db6a9b2012-03-30 16:11:39 -0400200 std::string GetTechnologyFamily(Error *error);
Eric Shienbrood7fce52c2012-04-13 19:11:02 -0400201 void OnModemStateChanged(ModemState old_state,
202 ModemState new_state,
203 uint32 reason);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500204
Jason Glasgow7b461df2012-05-01 16:38:45 -0400205 // accessor to read the allow roaming property
206 bool allow_roaming_property() const { return allow_roaming_; }
207
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700208 private:
Darin Petkovab565bb2011-10-06 02:55:51 -0700209 friend class CellularTest;
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500210 friend class CellularCapabilityTest;
Darin Petkov20c13ec2011-11-09 15:07:15 +0100211 friend class CellularCapabilityCDMATest;
212 friend class CellularCapabilityGSMTest;
Jason Glasgowef965562012-04-10 16:12:35 -0400213 friend class CellularCapabilityUniversalTest;
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400214 friend class CellularServiceTest;
Darin Petkov721ac932011-11-16 15:43:09 +0100215 friend class ModemTest;
Darin Petkovac635a82012-01-10 16:51:58 +0100216 FRIEND_TEST(CellularCapabilityCDMATest, CreateFriendlyServiceName);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500217 FRIEND_TEST(CellularCapabilityCDMATest, GetRegistrationState);
Darin Petkovac635a82012-01-10 16:51:58 +0100218 FRIEND_TEST(CellularCapabilityGSMTest, CreateFriendlyServiceName);
219 FRIEND_TEST(CellularServiceTest, FriendlyName);
Darin Petkov3335b372011-08-22 11:05:32 -0700220 FRIEND_TEST(CellularTest, CreateService);
Darin Petkovc5f56562011-08-06 16:40:05 -0700221 FRIEND_TEST(CellularTest, Connect);
Gary Moraina9fb3252012-05-31 12:05:31 -0700222 FRIEND_TEST(CellularTest, ConnectAddsTerminationAction);
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400223 FRIEND_TEST(CellularTest, ConnectFailure);
Thieu Leb5954a22012-05-18 10:37:34 -0700224 FRIEND_TEST(CellularTest, ConnectFailureNoService);
Darin Petkovfb0625e2012-01-16 13:05:56 +0100225 FRIEND_TEST(CellularTest, DisableModem);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500226 FRIEND_TEST(CellularTest, Disconnect);
Eric Shienbrood7fce52c2012-04-13 19:11:02 -0400227 FRIEND_TEST(CellularTest, ModemStateChangeDisable);
228 FRIEND_TEST(CellularTest, ModemStateChangeEnable);
Thieu Led0012052012-07-25 16:09:09 -0700229 FRIEND_TEST(CellularTest, ModemStateChangeStaleConnected);
230 FRIEND_TEST(CellularTest, ModemStateChangeValidConnected);
Darin Petkove7c6ad32012-06-29 10:22:09 +0200231 FRIEND_TEST(CellularTest, SetAllowRoaming);
Thieu Le37b90032012-05-15 15:18:41 -0700232 FRIEND_TEST(CellularTest, StartModemCallback);
233 FRIEND_TEST(CellularTest, StartModemCallbackFail);
234 FRIEND_TEST(CellularTest, StopModemCallback);
235 FRIEND_TEST(CellularTest, StopModemCallbackFail);
Darin Petkovbac96002011-08-09 13:22:00 -0700236 FRIEND_TEST(CellularTest, StartConnected);
Darin Petkova1e0a1c2011-08-25 15:08:33 -0700237 FRIEND_TEST(CellularTest, StartCDMARegister);
Darin Petkov9bac6fe2011-08-26 12:49:05 -0700238 FRIEND_TEST(CellularTest, StartGSMRegister);
Darin Petkov0828f5f2011-08-11 10:18:52 -0700239 FRIEND_TEST(CellularTest, StartLinked);
Darin Petkov9c1dcef2012-02-07 15:58:26 +0100240 FRIEND_TEST(CellularCapabilityTest, AllowRoaming);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500241 FRIEND_TEST(CellularCapabilityTest, EnableModemFail);
242 FRIEND_TEST(CellularCapabilityTest, EnableModemSucceed);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500243 FRIEND_TEST(CellularCapabilityTest, FinishEnable);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500244 FRIEND_TEST(CellularCapabilityTest, GetModemInfo);
245 FRIEND_TEST(CellularCapabilityTest, GetModemStatus);
Jason Glasgow02401cc2012-05-16 10:35:37 -0400246 FRIEND_TEST(CellularCapabilityUniversalTest, StopModemConnected);
Jason Glasgow14521872012-05-07 19:12:15 -0400247 FRIEND_TEST(CellularCapabilityUniversalTest, Connect);
Darin Petkove9d12e02011-07-27 15:09:37 -0700248
Jason Glasgow7b461df2012-05-01 16:38:45 -0400249 // Names of properties in storage
250 static const char kAllowRoaming[];
251
Darin Petkov0828f5f2011-08-11 10:18:52 -0700252 void SetState(State state);
253
Darin Petkovbac96002011-08-09 13:22:00 -0700254 // Invoked when the modem is connected to the cellular network to transition
255 // to the network-connected state and bring the network interface up.
256 void EstablishLink();
257
Ben Chan3ecdf822012-08-06 12:29:23 -0700258 void InitCapability(Type type);
Darin Petkovf5f61e02011-07-29 11:35:40 -0700259
Darin Petkovd9661952011-08-03 16:25:42 -0700260 void CreateService();
261
Eric Shienbrood0db6a9b2012-03-30 16:11:39 -0400262 // HelpRegisterDerived*: Expose a property over RPC, with the name |name|.
263 //
264 // Reads of the property will be handled by invoking |get|.
265 // Writes to the property will be handled by invoking |set|.
266 // Clearing the property will be handled by PropertyStore.
Jason Glasgow7b461df2012-05-01 16:38:45 -0400267 void HelpRegisterDerivedBool(
268 const std::string &name,
269 bool(Cellular::*get)(Error *error),
270 void(Cellular::*set)(const bool &value, Error *error));
Eric Shienbrood0db6a9b2012-03-30 16:11:39 -0400271 void HelpRegisterDerivedString(
272 const std::string &name,
273 std::string(Cellular::*get)(Error *error),
274 void(Cellular::*set)(const std::string &value, Error *error));
275
Nathan Williamsb54974f2012-04-19 11:16:30 -0400276 void OnConnectReply(const Error &error);
277 void OnDisconnectReply(const Error &error);
278
Jason Glasgow7b461df2012-05-01 16:38:45 -0400279 // DBUS accessors to read/modify the allow roaming property
280 bool GetAllowRoaming(Error */*error*/) { return allow_roaming_; }
281 void SetAllowRoaming(const bool &value, Error *error);
282
Gary Moraina9fb3252012-05-31 12:05:31 -0700283 // When shill terminates or ChromeOS suspends, this function is called to
284 // disconnect from the cellular network.
285 void StartTermination();
286
Thieu Le64b0fe52012-08-08 14:57:36 -0700287 base::WeakPtrFactory<Cellular> weak_ptr_factory_;
288
Darin Petkove9d12e02011-07-27 15:09:37 -0700289 State state_;
Darin Petkovbac96002011-08-09 13:22:00 -0700290 ModemState modem_state_;
Darin Petkove9d12e02011-07-27 15:09:37 -0700291
Darin Petkovdaf43862011-10-27 11:37:28 +0200292 scoped_ptr<CellularCapability> capability_;
293
Jason Glasgowa585fc32012-06-06 11:04:09 -0400294 const std::string dbus_owner_; // :x.y
295 const std::string dbus_service_; // org.*.ModemManager*
Darin Petkove9d12e02011-07-27 15:09:37 -0700296 const std::string dbus_path_; // ModemManager.Modem
Darin Petkovbec79a22011-08-01 14:47:17 -0700297
Darin Petkov137884a2011-10-26 18:52:47 +0200298 mobile_provider_db *provider_db_;
Ben Chan3ecdf822012-08-06 12:29:23 -0700299 ProxyFactory *proxy_factory_;
Darin Petkov137884a2011-10-26 18:52:47 +0200300
Darin Petkovd9661952011-08-03 16:25:42 -0700301 CellularServiceRefPtr service_;
Chris Masone853b81b2011-06-24 14:11:41 -0700302
Chris Masoneb925cc82011-06-22 15:39:57 -0700303 // Properties
Darin Petkov3335b372011-08-22 11:05:32 -0700304 Operator home_provider_;
Chris Masoneb925cc82011-06-22 15:39:57 -0700305
Jason Glasgow7b461df2012-05-01 16:38:45 -0400306 // User preference to allow or disallow roaming
307 bool allow_roaming_;
308
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700309 DISALLOW_COPY_AND_ASSIGN(Cellular);
310};
311
312} // namespace shill
313
314#endif // SHILL_CELLULAR_