Darin Petkov | c64fe5e | 2012-01-11 12:46:13 +0100 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Darin Petkov | daf4386 | 2011-10-27 11:37:28 +0200 | [diff] [blame] | 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_CAPABILITY_ |
| 6 | #define SHILL_CELLULAR_CAPABILITY_ |
| 7 | |
Darin Petkov | b05315f | 2011-11-07 10:14:25 +0100 | [diff] [blame] | 8 | #include <string> |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 9 | #include <vector> |
Darin Petkov | b05315f | 2011-11-07 10:14:25 +0100 | [diff] [blame] | 10 | |
Darin Petkov | daf4386 | 2011-10-27 11:37:28 +0200 | [diff] [blame] | 11 | #include <base/basictypes.h> |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 12 | #include <base/callback.h> |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 13 | #include <base/memory/scoped_ptr.h> |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 14 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
Darin Petkov | daf4386 | 2011-10-27 11:37:28 +0200 | [diff] [blame] | 15 | |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 16 | #include "shill/callbacks.h" |
Eric Shienbrood | 7fce52c | 2012-04-13 19:11:02 -0400 | [diff] [blame] | 17 | #include "shill/cellular.h" |
Darin Petkov | ae0c64e | 2011-11-15 15:50:27 +0100 | [diff] [blame] | 18 | #include "shill/dbus_properties.h" |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 19 | |
Darin Petkov | daf4386 | 2011-10-27 11:37:28 +0200 | [diff] [blame] | 20 | namespace shill { |
| 21 | |
| 22 | class Cellular; |
Darin Petkov | b05315f | 2011-11-07 10:14:25 +0100 | [diff] [blame] | 23 | class Error; |
Darin Petkov | daf4386 | 2011-10-27 11:37:28 +0200 | [diff] [blame] | 24 | class ProxyFactory; |
| 25 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 26 | typedef std::vector<base::Closure> CellularTaskList; |
| 27 | |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 28 | // Cellular devices instantiate subclasses of CellularCapability that |
| 29 | // handle the specific modem technologies and capabilities. |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 30 | // |
| 31 | // The CellularCapability is directly subclassed by: |
| 32 | // * CelllularCapabilityUniversal which handles all modems managed by |
| 33 | // a modem manager using the the org.chromium.ModemManager1 DBUS |
| 34 | // interface |
| 35 | // * CellularCapabilityClassic which handles all modems managed by a |
| 36 | // modem manager using the older org.chromium.ModemManager DBUS |
| 37 | // interface. This class is further subclassed to represent CDMA |
| 38 | // and GSM modems |
| 39 | // |
| 40 | // Pictorially: |
| 41 | // |
| 42 | // CellularCapability |
| 43 | // | |
| 44 | // |-- CellularCapabilityUniversal |
| 45 | // | |
| 46 | // |-- CellularCapabilityClassic |
| 47 | // | |
| 48 | // |-- CellularCapabilityGSM |
| 49 | // | |
| 50 | // |-- CellularCapabilityCDMA |
| 51 | // |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 52 | class CellularCapability { |
Darin Petkov | daf4386 | 2011-10-27 11:37:28 +0200 | [diff] [blame] | 53 | public: |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 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 Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 64 | static const int kTimeoutActivate; |
| 65 | static const int kTimeoutConnect; |
| 66 | static const int kTimeoutDefault; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 67 | static const int kTimeoutEnable; |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 68 | static const int kTimeoutRegister; |
| 69 | static const int kTimeoutScan; |
| 70 | |
Eric Shienbrood | 7fce52c | 2012-04-13 19:11:02 -0400 | [diff] [blame] | 71 | static const char kModemPropertyIMSI[]; |
| 72 | static const char kModemPropertyState[]; |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 73 | |
Darin Petkov | daf4386 | 2011-10-27 11:37:28 +0200 | [diff] [blame] | 74 | // |cellular| is the parent Cellular device. |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 75 | CellularCapability(Cellular *cellular, ProxyFactory *proxy_factory); |
Darin Petkov | daf4386 | 2011-10-27 11:37:28 +0200 | [diff] [blame] | 76 | virtual ~CellularCapability(); |
| 77 | |
| 78 | Cellular *cellular() const { return cellular_; } |
| 79 | ProxyFactory *proxy_factory() const { return proxy_factory_; } |
Darin Petkov | 5f316f6 | 2011-11-18 12:10:26 +0100 | [diff] [blame] | 80 | |
| 81 | // Invoked by the parent Cellular device when a new service is created. |
| 82 | virtual void OnServiceCreated() = 0; |
Darin Petkov | daf4386 | 2011-10-27 11:37:28 +0200 | [diff] [blame] | 83 | |
Darin Petkov | ae0c64e | 2011-11-15 15:50:27 +0100 | [diff] [blame] | 84 | virtual void UpdateStatus(const DBusPropertiesMap &properties) = 0; |
| 85 | |
| 86 | virtual void SetupConnectProperties(DBusPropertiesMap *properties) = 0; |
| 87 | |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 88 | // StartModem attempts to put the modem in a state in which it is |
| 89 | // usable for creating services and establishing connections (if |
| 90 | // network conditions permit). It potentially consists of multiple |
| 91 | // non-blocking calls to the modem-manager server. After each call, |
| 92 | // control is passed back up to the main loop. Each time a reply to |
| 93 | // a non-blocking call is received, the operation advances to the next |
| 94 | // step, until either an error occurs in one of them, or all the steps |
| 95 | // have been completed, at which point StartModem() is finished. |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 96 | virtual void StartModem(Error *error, |
| 97 | const ResultCallback &callback) = 0; |
Thieu Le | 923006b | 2012-04-05 16:32:58 -0700 | [diff] [blame] | 98 | // StopModem disconnects and disables a modem asynchronously. |
| 99 | // |callback| is invoked when this completes and the result is passed |
| 100 | // to the callback. |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 101 | virtual void StopModem(Error *error, const ResultCallback &callback) = 0; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 102 | virtual void Connect(const DBusPropertiesMap &properties, Error *error, |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 103 | const ResultCallback &callback) = 0; |
| 104 | virtual void Disconnect(Error *error, const ResultCallback &callback) = 0; |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 105 | |
| 106 | // Activates the modem. Returns an Error on failure. |
| 107 | // The default implementation fails by returning a kNotSupported error |
| 108 | // to the caller. |
| 109 | virtual void Activate(const std::string &carrier, |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 110 | Error *error, const ResultCallback &callback) = 0; |
Darin Petkov | a3d3be5 | 2011-11-14 21:34:16 +0100 | [diff] [blame] | 111 | |
Darin Petkov | 184c54e | 2011-11-15 12:44:39 +0100 | [diff] [blame] | 112 | // Network registration. |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 113 | virtual void RegisterOnNetwork(const std::string &network_id, |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 114 | Error *error, |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 115 | const ResultCallback &callback) = 0; |
Darin Petkov | b72cf40 | 2011-11-22 14:51:39 +0100 | [diff] [blame] | 116 | virtual bool IsRegistered() = 0; |
Eric Shienbrood | 7fce52c | 2012-04-13 19:11:02 -0400 | [diff] [blame] | 117 | // If we are informed by means of something other than a signal indicating |
| 118 | // a registration state change that the modem has unregistered from the |
| 119 | // network, we need to update the network-type-specific capability object. |
| 120 | virtual void SetUnregistered(bool searching) = 0; |
Darin Petkov | 184c54e | 2011-11-15 12:44:39 +0100 | [diff] [blame] | 121 | |
Darin Petkov | ac635a8 | 2012-01-10 16:51:58 +0100 | [diff] [blame] | 122 | virtual std::string CreateFriendlyServiceName() = 0; |
| 123 | |
Darin Petkov | c64fe5e | 2012-01-11 12:46:13 +0100 | [diff] [blame] | 124 | // PIN management. The default implementation fails by returning an error. |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 125 | virtual void RequirePIN(const std::string &pin, bool require, |
| 126 | Error *error, const ResultCallback &callback); |
| 127 | virtual void EnterPIN(const std::string &pin, |
| 128 | Error *error, const ResultCallback &callback); |
Darin Petkov | b05315f | 2011-11-07 10:14:25 +0100 | [diff] [blame] | 129 | virtual void UnblockPIN(const std::string &unblock_code, |
| 130 | const std::string &pin, |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 131 | Error *error, const ResultCallback &callback); |
Darin Petkov | b05315f | 2011-11-07 10:14:25 +0100 | [diff] [blame] | 132 | virtual void ChangePIN(const std::string &old_pin, |
| 133 | const std::string &new_pin, |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 134 | Error *error, const ResultCallback &callback); |
Darin Petkov | b05315f | 2011-11-07 10:14:25 +0100 | [diff] [blame] | 135 | |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 136 | // Asks the modem to scan for networks. |
| 137 | // |
| 138 | // The default implementation fails by filling error with |
| 139 | // kNotSupported. |
| 140 | // |
| 141 | // Subclasses should implement this by fetching scan results |
| 142 | // asynchronously. When the results are ready, update the |
| 143 | // flimflam::kFoundNetworksProperty and send a property change |
| 144 | // notification. Finally, callback must be invoked to inform the |
| 145 | // caller that the scan has completed. |
| 146 | // |
| 147 | // Errors are not generally reported, but on error the |
| 148 | // kFoundNetworksProperty should be cleared and a property change |
| 149 | // notification sent out. |
| 150 | // |
| 151 | // TODO(jglasgow): Refactor to reuse code by putting notification |
| 152 | // logic into Cellular or CellularCapability. |
| 153 | // |
| 154 | // TODO(jglasgow): Implement real error handling. |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 155 | virtual void Scan(Error *error, const ResultCallback &callback); |
Darin Petkov | 1272a43 | 2011-11-10 15:53:37 +0100 | [diff] [blame] | 156 | |
Darin Petkov | 20c13ec | 2011-11-09 15:07:15 +0100 | [diff] [blame] | 157 | // Returns an empty string if the network technology is unknown. |
| 158 | virtual std::string GetNetworkTechnologyString() const = 0; |
| 159 | |
| 160 | virtual std::string GetRoamingStateString() const = 0; |
| 161 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 162 | virtual void GetSignalQuality() = 0; |
| 163 | |
Eric Shienbrood | 0db6a9b | 2012-03-30 16:11:39 -0400 | [diff] [blame] | 164 | virtual std::string GetTypeString() const = 0; |
| 165 | |
Jason Glasgow | 4c0724a | 2012-04-17 15:47:40 -0400 | [diff] [blame] | 166 | // Called when ModemManager has sent a property change notification |
| 167 | // signal over DBUS. |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 168 | virtual void OnDBusPropertiesChanged( |
| 169 | const std::string &interface, |
| 170 | const DBusPropertiesMap &changed_properties, |
| 171 | const std::vector<std::string> &invalidated_properties) = 0; |
Darin Petkov | ae0c64e | 2011-11-15 15:50:27 +0100 | [diff] [blame] | 172 | |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 173 | // Should this device allow roaming? |
| 174 | // The decision to allow roaming or not is based on the home |
| 175 | // provider as well as on the user modifiable "allow_roaming" |
| 176 | // property. |
| 177 | virtual bool AllowRoaming() = 0; |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 178 | |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 179 | protected: |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 180 | virtual void GetRegistrationState() = 0; |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 181 | |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 182 | // Releases all proxies held by the object. This is most useful |
| 183 | // during unit tests. |
| 184 | virtual void ReleaseProxies() = 0; |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 185 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 186 | static void OnUnsupportedOperation(const char *operation, Error *error); |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 187 | |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 188 | // Runs the next task in a list. |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 189 | // Precondition: |tasks| is not empty. |
| 190 | void RunNextStep(CellularTaskList *tasks); |
Thieu Le | 923006b | 2012-04-05 16:32:58 -0700 | [diff] [blame] | 191 | // StepCompletedCallback is called after a task completes. |
| 192 | // |callback| is the original callback that needs to be invoked when all of |
| 193 | // the tasks complete or if there is a failure. |ignore_error| will be set |
| 194 | // to true if the next task should be run regardless of the result of the |
| 195 | // just-completed task. |tasks| is the list of tasks remaining. |error| is |
| 196 | // the result of the just-completed task. |
| 197 | void StepCompletedCallback(const ResultCallback &callback, bool ignore_error, |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 198 | CellularTaskList *tasks, const Error &error); |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 199 | |
| 200 | // accessor for subclasses to read the allow roaming property |
| 201 | bool allow_roaming_property() const { return allow_roaming_; } |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 202 | |
Darin Petkov | daf4386 | 2011-10-27 11:37:28 +0200 | [diff] [blame] | 203 | private: |
Thieu Le | 923006b | 2012-04-05 16:32:58 -0700 | [diff] [blame] | 204 | friend class CellularCapabilityGSMTest; |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 205 | friend class CellularCapabilityTest; |
Jason Glasgow | ef96556 | 2012-04-10 16:12:35 -0400 | [diff] [blame] | 206 | friend class CellularCapabilityUniversalTest; |
Thieu Le | 923006b | 2012-04-05 16:32:58 -0700 | [diff] [blame] | 207 | friend class CellularTest; |
Darin Petkov | 9c1dcef | 2012-02-07 15:58:26 +0100 | [diff] [blame] | 208 | FRIEND_TEST(CellularCapabilityTest, AllowRoaming); |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 209 | FRIEND_TEST(CellularTest, Connect); |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 210 | FRIEND_TEST(CellularTest, TearDown); |
Darin Petkov | 721ac93 | 2011-11-16 15:43:09 +0100 | [diff] [blame] | 211 | |
Darin Petkov | 9c1dcef | 2012-02-07 15:58:26 +0100 | [diff] [blame] | 212 | void HelpRegisterDerivedBool( |
| 213 | const std::string &name, |
| 214 | bool(CellularCapability::*get)(Error *error), |
| 215 | void(CellularCapability::*set)(const bool &value, Error *error)); |
| 216 | |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 217 | // DBUS accessors to read/modify the allow roaming property |
Darin Petkov | 9c1dcef | 2012-02-07 15:58:26 +0100 | [diff] [blame] | 218 | bool GetAllowRoaming(Error */*error*/) { return allow_roaming_; } |
| 219 | void SetAllowRoaming(const bool &value, Error *error); |
| 220 | |
Darin Petkov | daf4386 | 2011-10-27 11:37:28 +0200 | [diff] [blame] | 221 | Cellular *cellular_; |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 222 | |
| 223 | // Store cached copies of singletons for speed/ease of testing. |
Darin Petkov | daf4386 | 2011-10-27 11:37:28 +0200 | [diff] [blame] | 224 | ProxyFactory *proxy_factory_; |
| 225 | |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 226 | // User preference to allow or disallow roaming |
| 227 | bool allow_roaming_; |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 228 | |
Darin Petkov | daf4386 | 2011-10-27 11:37:28 +0200 | [diff] [blame] | 229 | DISALLOW_COPY_AND_ASSIGN(CellularCapability); |
| 230 | }; |
| 231 | |
| 232 | } // namespace shill |
| 233 | |
| 234 | #endif // SHILL_CELLULAR_CAPABILITY_ |