blob: 3ebcbffd39a34c9314d414de8855d896cc2b2111 [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
Darin Petkovc37a9c42012-09-06 15:28:22 +02005#ifndef SHILL_CELLULAR_CAPABILITY_H_
6#define SHILL_CELLULAR_CAPABILITY_H_
Darin Petkovdaf43862011-10-27 11:37:28 +02007
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"
Eric Shienbrood7fce52c2012-04-13 19:11:02 -040017#include "shill/cellular.h"
Darin Petkovae0c64e2011-11-15 15:50:27 +010018#include "shill/dbus_properties.h"
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050019
Darin Petkovdaf43862011-10-27 11:37:28 +020020namespace shill {
21
22class Cellular;
Darin Petkovb05315f2011-11-07 10:14:25 +010023class Error;
Darin Petkovdaf43862011-10-27 11:37:28 +020024class ProxyFactory;
25
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050026// Cellular devices instantiate subclasses of CellularCapability that
27// handle the specific modem technologies and capabilities.
Jason Glasgow82f9ab32012-04-04 14:27:19 -040028//
29// The CellularCapability is directly subclassed by:
30// * CelllularCapabilityUniversal which handles all modems managed by
31// a modem manager using the the org.chromium.ModemManager1 DBUS
32// interface
33// * CellularCapabilityClassic which handles all modems managed by a
34// modem manager using the older org.chromium.ModemManager DBUS
35// interface. This class is further subclassed to represent CDMA
36// and GSM modems
37//
38// Pictorially:
39//
40// CellularCapability
41// |
42// |-- CellularCapabilityUniversal
43// |
44// |-- CellularCapabilityClassic
45// |
46// |-- CellularCapabilityGSM
47// |
48// |-- CellularCapabilityCDMA
49//
Eric Shienbrood9a245532012-03-07 14:20:39 -050050class CellularCapability {
Darin Petkovdaf43862011-10-27 11:37:28 +020051 public:
Jason Glasgow82f9ab32012-04-04 14:27:19 -040052 // SimLockStatus represents the fields in the Cellular.SIMLockStatus
53 // DBUS property of the shill device.
54 struct SimLockStatus {
55 SimLockStatus() : enabled(false), retries_left(0) {}
56
57 bool enabled;
58 std::string lock_type;
59 uint32 retries_left;
60 };
61
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050062 static const int kTimeoutActivate;
63 static const int kTimeoutConnect;
64 static const int kTimeoutDefault;
Thieu Le049adb52012-11-12 17:14:51 -080065 static const int kTimeoutDisconnect;
Eric Shienbrood9a245532012-03-07 14:20:39 -050066 static const int kTimeoutEnable;
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050067 static const int kTimeoutRegister;
68 static const int kTimeoutScan;
69
Eric Shienbrood7fce52c2012-04-13 19:11:02 -040070 static const char kModemPropertyIMSI[];
71 static const char kModemPropertyState[];
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050072
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 SetupConnectProperties(DBusPropertiesMap *properties) = 0;
84
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050085 // StartModem attempts to put the modem in a state in which it is
86 // usable for creating services and establishing connections (if
87 // network conditions permit). It potentially consists of multiple
88 // non-blocking calls to the modem-manager server. After each call,
89 // control is passed back up to the main loop. Each time a reply to
90 // a non-blocking call is received, the operation advances to the next
91 // step, until either an error occurs in one of them, or all the steps
92 // have been completed, at which point StartModem() is finished.
Eric Shienbrood9a245532012-03-07 14:20:39 -050093 virtual void StartModem(Error *error,
94 const ResultCallback &callback) = 0;
Thieu Le923006b2012-04-05 16:32:58 -070095 // StopModem disconnects and disables a modem asynchronously.
96 // |callback| is invoked when this completes and the result is passed
97 // to the callback.
Jason Glasgow82f9ab32012-04-04 14:27:19 -040098 virtual void StopModem(Error *error, const ResultCallback &callback) = 0;
Eric Shienbrood9a245532012-03-07 14:20:39 -050099 virtual void Connect(const DBusPropertiesMap &properties, Error *error,
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400100 const ResultCallback &callback) = 0;
101 virtual void Disconnect(Error *error, const ResultCallback &callback) = 0;
Christopher Wiley8a468902012-11-30 11:52:38 -0800102 // Called when a disconnect completes, successful or not.
103 virtual void DisconnectCleanup() = 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
Ben Chan15786032012-11-04 21:28:02 -0800111 // Returns true if service activation is required. Returns false by default
112 // in this base class.
113 virtual bool IsServiceActivationRequired() const;
114
Darin Petkov184c54e2011-11-15 12:44:39 +0100115 // Network registration.
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500116 virtual void RegisterOnNetwork(const std::string &network_id,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500117 Error *error,
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400118 const ResultCallback &callback) = 0;
Darin Petkovb72cf402011-11-22 14:51:39 +0100119 virtual bool IsRegistered() = 0;
Eric Shienbrood7fce52c2012-04-13 19:11:02 -0400120 // If we are informed by means of something other than a signal indicating
121 // a registration state change that the modem has unregistered from the
122 // network, we need to update the network-type-specific capability object.
123 virtual void SetUnregistered(bool searching) = 0;
Darin Petkov184c54e2011-11-15 12:44:39 +0100124
Darin Petkovac635a82012-01-10 16:51:58 +0100125 virtual std::string CreateFriendlyServiceName() = 0;
126
Darin Petkovc64fe5e2012-01-11 12:46:13 +0100127 // PIN management. The default implementation fails by returning an error.
Eric Shienbrood9a245532012-03-07 14:20:39 -0500128 virtual void RequirePIN(const std::string &pin, bool require,
129 Error *error, const ResultCallback &callback);
130 virtual void EnterPIN(const std::string &pin,
131 Error *error, const ResultCallback &callback);
Darin Petkovb05315f2011-11-07 10:14:25 +0100132 virtual void UnblockPIN(const std::string &unblock_code,
133 const std::string &pin,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500134 Error *error, const ResultCallback &callback);
Darin Petkovb05315f2011-11-07 10:14:25 +0100135 virtual void ChangePIN(const std::string &old_pin,
136 const std::string &new_pin,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500137 Error *error, const ResultCallback &callback);
Darin Petkovb05315f2011-11-07 10:14:25 +0100138
Darin Petkovc37a9c42012-09-06 15:28:22 +0200139 // The default implementation fails by returning an error.
140 virtual void SetCarrier(const std::string &carrier,
141 Error *error, const ResultCallback &callback);
142
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400143 // Asks the modem to scan for networks.
144 //
145 // The default implementation fails by filling error with
146 // kNotSupported.
147 //
148 // Subclasses should implement this by fetching scan results
149 // asynchronously. When the results are ready, update the
150 // flimflam::kFoundNetworksProperty and send a property change
151 // notification. Finally, callback must be invoked to inform the
152 // caller that the scan has completed.
153 //
154 // Errors are not generally reported, but on error the
155 // kFoundNetworksProperty should be cleared and a property change
156 // notification sent out.
157 //
158 // TODO(jglasgow): Refactor to reuse code by putting notification
159 // logic into Cellular or CellularCapability.
160 //
161 // TODO(jglasgow): Implement real error handling.
Eric Shienbrood9a245532012-03-07 14:20:39 -0500162 virtual void Scan(Error *error, const ResultCallback &callback);
Darin Petkov1272a432011-11-10 15:53:37 +0100163
Darin Petkov20c13ec2011-11-09 15:07:15 +0100164 // Returns an empty string if the network technology is unknown.
165 virtual std::string GetNetworkTechnologyString() const = 0;
166
167 virtual std::string GetRoamingStateString() const = 0;
168
Eric Shienbrood9a245532012-03-07 14:20:39 -0500169 virtual void GetSignalQuality() = 0;
170
Eric Shienbrood0db6a9b2012-03-30 16:11:39 -0400171 virtual std::string GetTypeString() const = 0;
172
Jason Glasgow4c0724a2012-04-17 15:47:40 -0400173 // Called when ModemManager has sent a property change notification
174 // signal over DBUS.
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400175 virtual void OnDBusPropertiesChanged(
176 const std::string &interface,
177 const DBusPropertiesMap &changed_properties,
178 const std::vector<std::string> &invalidated_properties) = 0;
Darin Petkovae0c64e2011-11-15 15:50:27 +0100179
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400180 // Should this device allow roaming?
181 // The decision to allow roaming or not is based on the home
182 // provider as well as on the user modifiable "allow_roaming"
183 // property.
184 virtual bool AllowRoaming() = 0;
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500185
Christopher Wiley1582bdd2012-11-15 11:31:14 -0800186 virtual bool IsActivating() const;
187
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400188 protected:
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400189 // Releases all proxies held by the object. This is most useful
190 // during unit tests.
191 virtual void ReleaseProxies() = 0;
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500192
Eric Shienbrood9a245532012-03-07 14:20:39 -0500193 static void OnUnsupportedOperation(const char *operation, Error *error);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500194
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400195 // accessor for subclasses to read the allow roaming property
Jason Glasgow7b461df2012-05-01 16:38:45 -0400196 bool allow_roaming_property() const {
197 return cellular_->allow_roaming_property();
198 }
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500199
Darin Petkovdaf43862011-10-27 11:37:28 +0200200 private:
Thieu Le923006b2012-04-05 16:32:58 -0700201 friend class CellularCapabilityGSMTest;
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500202 friend class CellularCapabilityTest;
Jason Glasgowef965562012-04-10 16:12:35 -0400203 friend class CellularCapabilityUniversalTest;
Thieu Le923006b2012-04-05 16:32:58 -0700204 friend class CellularTest;
Darin Petkov9c1dcef2012-02-07 15:58:26 +0100205 FRIEND_TEST(CellularCapabilityTest, AllowRoaming);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500206 FRIEND_TEST(CellularTest, Connect);
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400207 FRIEND_TEST(CellularTest, TearDown);
Darin Petkov721ac932011-11-16 15:43:09 +0100208
Darin Petkovdaf43862011-10-27 11:37:28 +0200209 Cellular *cellular_;
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500210
211 // Store cached copies of singletons for speed/ease of testing.
Darin Petkovdaf43862011-10-27 11:37:28 +0200212 ProxyFactory *proxy_factory_;
213
Darin Petkovdaf43862011-10-27 11:37:28 +0200214 DISALLOW_COPY_AND_ASSIGN(CellularCapability);
215};
216
217} // namespace shill
218
Darin Petkovc37a9c42012-09-06 15:28:22 +0200219#endif // SHILL_CELLULAR_CAPABILITY_H_