blob: d4cf969a8d77d944d1775110620c6b14018a06b0 [file] [log] [blame]
Darin Petkovc64fe5e2012-01-11 12:46:13 +01001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Darin Petkovdaf43862011-10-27 11:37:28 +02002// 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_CAPABILITY_
6#define SHILL_CELLULAR_CAPABILITY_
7
Darin Petkovb05315f2011-11-07 10:14:25 +01008#include <string>
Eric Shienbrood3e20a232012-02-16 11:35:56 -05009#include <vector>
Darin Petkovb05315f2011-11-07 10:14:25 +010010
Darin Petkovdaf43862011-10-27 11:37:28 +020011#include <base/basictypes.h>
Eric Shienbrood3e20a232012-02-16 11:35:56 -050012#include <base/callback.h>
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050013#include <base/memory/scoped_ptr.h>
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050014#include <gtest/gtest_prod.h> // for FRIEND_TEST
Darin Petkovdaf43862011-10-27 11:37:28 +020015
Jason Glasgow82f9ab32012-04-04 14:27:19 -040016#include "shill/callbacks.h"
Darin Petkovae0c64e2011-11-15 15:50:27 +010017#include "shill/dbus_properties.h"
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050018
Darin Petkovdaf43862011-10-27 11:37:28 +020019namespace shill {
20
21class Cellular;
Darin Petkovb05315f2011-11-07 10:14:25 +010022class Error;
Darin Petkovdaf43862011-10-27 11:37:28 +020023class ProxyFactory;
24
Eric Shienbrood9a245532012-03-07 14:20:39 -050025typedef std::vector<base::Closure> CellularTaskList;
26
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050027// Cellular devices instantiate subclasses of CellularCapability that
28// handle the specific modem technologies and capabilities.
Jason Glasgow82f9ab32012-04-04 14:27:19 -040029//
30// The CellularCapability is directly subclassed by:
31// * CelllularCapabilityUniversal which handles all modems managed by
32// a modem manager using the the org.chromium.ModemManager1 DBUS
33// interface
34// * CellularCapabilityClassic which handles all modems managed by a
35// modem manager using the older org.chromium.ModemManager DBUS
36// interface. This class is further subclassed to represent CDMA
37// and GSM modems
38//
39// Pictorially:
40//
41// CellularCapability
42// |
43// |-- CellularCapabilityUniversal
44// |
45// |-- CellularCapabilityClassic
46// |
47// |-- CellularCapabilityGSM
48// |
49// |-- CellularCapabilityCDMA
50//
Eric Shienbrood9a245532012-03-07 14:20:39 -050051class CellularCapability {
Darin Petkovdaf43862011-10-27 11:37:28 +020052 public:
Jason Glasgow82f9ab32012-04-04 14:27:19 -040053
54 // SimLockStatus represents the fields in the Cellular.SIMLockStatus
55 // DBUS property of the shill device.
56 struct SimLockStatus {
57 SimLockStatus() : enabled(false), retries_left(0) {}
58
59 bool enabled;
60 std::string lock_type;
61 uint32 retries_left;
62 };
63
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050064 static const int kTimeoutActivate;
65 static const int kTimeoutConnect;
66 static const int kTimeoutDefault;
Eric Shienbrood9a245532012-03-07 14:20:39 -050067 static const int kTimeoutEnable;
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050068 static const int kTimeoutRegister;
69 static const int kTimeoutScan;
70
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050071 static const char kPropertyIMSI[];
72
Darin Petkovdaf43862011-10-27 11:37:28 +020073 // |cellular| is the parent Cellular device.
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050074 CellularCapability(Cellular *cellular, ProxyFactory *proxy_factory);
Darin Petkovdaf43862011-10-27 11:37:28 +020075 virtual ~CellularCapability();
76
77 Cellular *cellular() const { return cellular_; }
78 ProxyFactory *proxy_factory() const { return proxy_factory_; }
Darin Petkov5f316f62011-11-18 12:10:26 +010079
80 // Invoked by the parent Cellular device when a new service is created.
81 virtual void OnServiceCreated() = 0;
Darin Petkovdaf43862011-10-27 11:37:28 +020082
Darin Petkovae0c64e2011-11-15 15:50:27 +010083 virtual void UpdateStatus(const DBusPropertiesMap &properties) = 0;
84
85 virtual void SetupConnectProperties(DBusPropertiesMap *properties) = 0;
86
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050087 // StartModem attempts to put the modem in a state in which it is
88 // usable for creating services and establishing connections (if
89 // network conditions permit). It potentially consists of multiple
90 // non-blocking calls to the modem-manager server. After each call,
91 // control is passed back up to the main loop. Each time a reply to
92 // a non-blocking call is received, the operation advances to the next
93 // step, until either an error occurs in one of them, or all the steps
94 // have been completed, at which point StartModem() is finished.
Eric Shienbrood9a245532012-03-07 14:20:39 -050095 virtual void StartModem(Error *error,
96 const ResultCallback &callback) = 0;
Thieu Le923006b2012-04-05 16:32:58 -070097 // StopModem disconnects and disables a modem asynchronously.
98 // |callback| is invoked when this completes and the result is passed
99 // to the callback.
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400100 virtual void StopModem(Error *error, const ResultCallback &callback) = 0;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500101 virtual void Connect(const DBusPropertiesMap &properties, Error *error,
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400102 const ResultCallback &callback) = 0;
103 virtual void Disconnect(Error *error, const ResultCallback &callback) = 0;
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500104
105 // Activates the modem. Returns an Error on failure.
106 // The default implementation fails by returning a kNotSupported error
107 // to the caller.
108 virtual void Activate(const std::string &carrier,
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400109 Error *error, const ResultCallback &callback) = 0;
Darin Petkova3d3be52011-11-14 21:34:16 +0100110
Darin Petkov184c54e2011-11-15 12:44:39 +0100111 // Network registration.
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500112 virtual void RegisterOnNetwork(const std::string &network_id,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500113 Error *error,
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400114 const ResultCallback &callback) = 0;
Darin Petkovb72cf402011-11-22 14:51:39 +0100115 virtual bool IsRegistered() = 0;
Darin Petkov184c54e2011-11-15 12:44:39 +0100116
Darin Petkovac635a82012-01-10 16:51:58 +0100117 virtual std::string CreateFriendlyServiceName() = 0;
118
Darin Petkovc64fe5e2012-01-11 12:46:13 +0100119 // PIN management. The default implementation fails by returning an error.
Eric Shienbrood9a245532012-03-07 14:20:39 -0500120 virtual void RequirePIN(const std::string &pin, bool require,
121 Error *error, const ResultCallback &callback);
122 virtual void EnterPIN(const std::string &pin,
123 Error *error, const ResultCallback &callback);
Darin Petkovb05315f2011-11-07 10:14:25 +0100124 virtual void UnblockPIN(const std::string &unblock_code,
125 const std::string &pin,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500126 Error *error, const ResultCallback &callback);
Darin Petkovb05315f2011-11-07 10:14:25 +0100127 virtual void ChangePIN(const std::string &old_pin,
128 const std::string &new_pin,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500129 Error *error, const ResultCallback &callback);
Darin Petkovb05315f2011-11-07 10:14:25 +0100130
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400131 // Asks the modem to scan for networks.
132 //
133 // The default implementation fails by filling error with
134 // kNotSupported.
135 //
136 // Subclasses should implement this by fetching scan results
137 // asynchronously. When the results are ready, update the
138 // flimflam::kFoundNetworksProperty and send a property change
139 // notification. Finally, callback must be invoked to inform the
140 // caller that the scan has completed.
141 //
142 // Errors are not generally reported, but on error the
143 // kFoundNetworksProperty should be cleared and a property change
144 // notification sent out.
145 //
146 // TODO(jglasgow): Refactor to reuse code by putting notification
147 // logic into Cellular or CellularCapability.
148 //
149 // TODO(jglasgow): Implement real error handling.
Eric Shienbrood9a245532012-03-07 14:20:39 -0500150 virtual void Scan(Error *error, const ResultCallback &callback);
Darin Petkov1272a432011-11-10 15:53:37 +0100151
Darin Petkov20c13ec2011-11-09 15:07:15 +0100152 // Returns an empty string if the network technology is unknown.
153 virtual std::string GetNetworkTechnologyString() const = 0;
154
155 virtual std::string GetRoamingStateString() const = 0;
156
Eric Shienbrood9a245532012-03-07 14:20:39 -0500157 virtual void GetSignalQuality() = 0;
158
Eric Shienbrood0db6a9b2012-03-30 16:11:39 -0400159 virtual std::string GetTypeString() const = 0;
160
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400161 virtual void OnDBusPropertiesChanged(
162 const std::string &interface,
163 const DBusPropertiesMap &changed_properties,
164 const std::vector<std::string> &invalidated_properties) = 0;
Darin Petkovae0c64e2011-11-15 15:50:27 +0100165 virtual void OnModemManagerPropertiesChanged(
166 const DBusPropertiesMap &properties) = 0;
167
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400168 // Should this device allow roaming?
169 // The decision to allow roaming or not is based on the home
170 // provider as well as on the user modifiable "allow_roaming"
171 // property.
172 virtual bool AllowRoaming() = 0;
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500173
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400174 protected:
Eric Shienbrood9a245532012-03-07 14:20:39 -0500175 virtual void GetRegistrationState() = 0;
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500176
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400177 // Releases all proxies held by the object. This is most useful
178 // during unit tests.
179 virtual void ReleaseProxies() = 0;
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500180
Eric Shienbrood9a245532012-03-07 14:20:39 -0500181 static void OnUnsupportedOperation(const char *operation, Error *error);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500182
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400183 // Runs the next task in a list.
Eric Shienbrood9a245532012-03-07 14:20:39 -0500184 // Precondition: |tasks| is not empty.
185 void RunNextStep(CellularTaskList *tasks);
Thieu Le923006b2012-04-05 16:32:58 -0700186 // StepCompletedCallback is called after a task completes.
187 // |callback| is the original callback that needs to be invoked when all of
188 // the tasks complete or if there is a failure. |ignore_error| will be set
189 // to true if the next task should be run regardless of the result of the
190 // just-completed task. |tasks| is the list of tasks remaining. |error| is
191 // the result of the just-completed task.
192 void StepCompletedCallback(const ResultCallback &callback, bool ignore_error,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500193 CellularTaskList *tasks, const Error &error);
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400194
195 // accessor for subclasses to read the allow roaming property
196 bool allow_roaming_property() const { return allow_roaming_; }
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500197
Darin Petkovdaf43862011-10-27 11:37:28 +0200198 private:
Thieu Le923006b2012-04-05 16:32:58 -0700199 friend class CellularCapabilityGSMTest;
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500200 friend class CellularCapabilityTest;
Jason Glasgowef965562012-04-10 16:12:35 -0400201 friend class CellularCapabilityUniversalTest;
Thieu Le923006b2012-04-05 16:32:58 -0700202 friend class CellularTest;
Darin Petkov9c1dcef2012-02-07 15:58:26 +0100203 FRIEND_TEST(CellularCapabilityTest, AllowRoaming);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500204 FRIEND_TEST(CellularTest, Connect);
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400205 FRIEND_TEST(CellularTest, TearDown);
Darin Petkov721ac932011-11-16 15:43:09 +0100206
Darin Petkov9c1dcef2012-02-07 15:58:26 +0100207 void HelpRegisterDerivedBool(
208 const std::string &name,
209 bool(CellularCapability::*get)(Error *error),
210 void(CellularCapability::*set)(const bool &value, Error *error));
211
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400212 // DBUS accessors to read/modify the allow roaming property
Darin Petkov9c1dcef2012-02-07 15:58:26 +0100213 bool GetAllowRoaming(Error */*error*/) { return allow_roaming_; }
214 void SetAllowRoaming(const bool &value, Error *error);
215
Darin Petkovdaf43862011-10-27 11:37:28 +0200216 Cellular *cellular_;
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500217
218 // Store cached copies of singletons for speed/ease of testing.
Darin Petkovdaf43862011-10-27 11:37:28 +0200219 ProxyFactory *proxy_factory_;
220
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400221 // User preference to allow or disallow roaming
222 bool allow_roaming_;
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500223
Darin Petkovdaf43862011-10-27 11:37:28 +0200224 DISALLOW_COPY_AND_ASSIGN(CellularCapability);
225};
226
227} // namespace shill
228
229#endif // SHILL_CELLULAR_CAPABILITY_