blob: 995f2c96420f74babca32d67c7599b75355828a5 [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 Shienbrood9a245532012-03-07 14:20:39 -050014#include <base/memory/weak_ptr.h>
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050015#include <gtest/gtest_prod.h> // for FRIEND_TEST
Darin Petkovdaf43862011-10-27 11:37:28 +020016
Darin Petkovae0c64e2011-11-15 15:50:27 +010017#include "shill/dbus_properties.h"
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050018#include "shill/modem_proxy_interface.h"
19#include "shill/modem_simple_proxy_interface.h"
20
Darin Petkovdaf43862011-10-27 11:37:28 +020021namespace shill {
22
23class Cellular;
Darin Petkovb05315f2011-11-07 10:14:25 +010024class Error;
25class EventDispatcher;
Darin Petkovdaf43862011-10-27 11:37:28 +020026class ProxyFactory;
27
Eric Shienbrood9a245532012-03-07 14:20:39 -050028typedef std::vector<base::Closure> CellularTaskList;
29
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050030// Cellular devices instantiate subclasses of CellularCapability that
31// handle the specific modem technologies and capabilities.
Eric Shienbrood9a245532012-03-07 14:20:39 -050032class CellularCapability {
Darin Petkovdaf43862011-10-27 11:37:28 +020033 public:
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050034 static const int kTimeoutActivate;
35 static const int kTimeoutConnect;
36 static const int kTimeoutDefault;
Eric Shienbrood9a245532012-03-07 14:20:39 -050037 static const int kTimeoutEnable;
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050038 static const int kTimeoutRegister;
39 static const int kTimeoutScan;
40
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -040041 static const char kConnectPropertyApn[];
42 static const char kConnectPropertyApnUsername[];
43 static const char kConnectPropertyApnPassword[];
44 static const char kConnectPropertyHomeOnly[];
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050045 static const char kConnectPropertyPhoneNumber[];
46 static const char kPropertyIMSI[];
47
48
Darin Petkovdaf43862011-10-27 11:37:28 +020049 // |cellular| is the parent Cellular device.
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050050 CellularCapability(Cellular *cellular, ProxyFactory *proxy_factory);
Darin Petkovdaf43862011-10-27 11:37:28 +020051 virtual ~CellularCapability();
52
53 Cellular *cellular() const { return cellular_; }
54 ProxyFactory *proxy_factory() const { return proxy_factory_; }
Darin Petkov5f316f62011-11-18 12:10:26 +010055
56 // Invoked by the parent Cellular device when a new service is created.
57 virtual void OnServiceCreated() = 0;
Darin Petkovdaf43862011-10-27 11:37:28 +020058
Darin Petkovae0c64e2011-11-15 15:50:27 +010059 virtual void UpdateStatus(const DBusPropertiesMap &properties) = 0;
60
61 virtual void SetupConnectProperties(DBusPropertiesMap *properties) = 0;
62
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050063 bool allow_roaming() const { return allow_roaming_; }
64
65 // StartModem attempts to put the modem in a state in which it is
66 // usable for creating services and establishing connections (if
67 // network conditions permit). It potentially consists of multiple
68 // non-blocking calls to the modem-manager server. After each call,
69 // control is passed back up to the main loop. Each time a reply to
70 // a non-blocking call is received, the operation advances to the next
71 // step, until either an error occurs in one of them, or all the steps
72 // have been completed, at which point StartModem() is finished.
Eric Shienbrood9a245532012-03-07 14:20:39 -050073 virtual void StartModem(Error *error,
74 const ResultCallback &callback) = 0;
75 // StopModem is also a multi-step operation consisting of several
76 // non-blocking calls.
77 virtual void StopModem(Error *error, const ResultCallback &callback) = 0;
78 virtual void Connect(const DBusPropertiesMap &properties, Error *error,
79 const ResultCallback &callback);
80 virtual void Disconnect(Error *error, const ResultCallback &callback);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050081
82 // Activates the modem. Returns an Error on failure.
83 // The default implementation fails by returning a kNotSupported error
84 // to the caller.
85 virtual void Activate(const std::string &carrier,
Eric Shienbrood9a245532012-03-07 14:20:39 -050086 Error *error, const ResultCallback &callback);
Darin Petkova3d3be52011-11-14 21:34:16 +010087
Darin Petkov184c54e2011-11-15 12:44:39 +010088 // Network registration.
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050089 virtual void RegisterOnNetwork(const std::string &network_id,
Eric Shienbrood9a245532012-03-07 14:20:39 -050090 Error *error,
91 const ResultCallback &callback);
Darin Petkovb72cf402011-11-22 14:51:39 +010092 virtual bool IsRegistered() = 0;
Darin Petkov184c54e2011-11-15 12:44:39 +010093
Darin Petkovac635a82012-01-10 16:51:58 +010094 virtual std::string CreateFriendlyServiceName() = 0;
95
Darin Petkovc64fe5e2012-01-11 12:46:13 +010096 // PIN management. The default implementation fails by returning an error.
Eric Shienbrood9a245532012-03-07 14:20:39 -050097 virtual void RequirePIN(const std::string &pin, bool require,
98 Error *error, const ResultCallback &callback);
99 virtual void EnterPIN(const std::string &pin,
100 Error *error, const ResultCallback &callback);
Darin Petkovb05315f2011-11-07 10:14:25 +0100101 virtual void UnblockPIN(const std::string &unblock_code,
102 const std::string &pin,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500103 Error *error, const ResultCallback &callback);
Darin Petkovb05315f2011-11-07 10:14:25 +0100104 virtual void ChangePIN(const std::string &old_pin,
105 const std::string &new_pin,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500106 Error *error, const ResultCallback &callback);
Darin Petkovb05315f2011-11-07 10:14:25 +0100107
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500108 // Network scanning. The default implementation fails by invoking
109 // the reply handler with an error.
Eric Shienbrood9a245532012-03-07 14:20:39 -0500110 virtual void Scan(Error *error, const ResultCallback &callback);
Darin Petkov1272a432011-11-10 15:53:37 +0100111
Darin Petkov20c13ec2011-11-09 15:07:15 +0100112 // Returns an empty string if the network technology is unknown.
113 virtual std::string GetNetworkTechnologyString() const = 0;
114
115 virtual std::string GetRoamingStateString() const = 0;
116
Eric Shienbrood9a245532012-03-07 14:20:39 -0500117 virtual void GetSignalQuality() = 0;
118
Eric Shienbrood0db6a9b2012-03-30 16:11:39 -0400119 virtual std::string GetTypeString() const = 0;
120
Darin Petkovae0c64e2011-11-15 15:50:27 +0100121 virtual void OnModemManagerPropertiesChanged(
122 const DBusPropertiesMap &properties) = 0;
123
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500124 protected:
Eric Shienbrood9a245532012-03-07 14:20:39 -0500125 // The following five methods are only ever called as
126 // callbacks (from the main loop), which is why they
127 // don't take an Error * argument.
128 virtual void EnableModem(const ResultCallback &callback);
129 virtual void DisableModem(const ResultCallback &callback);
130 virtual void GetModemStatus(const ResultCallback &callback);
131 virtual void GetModemInfo(const ResultCallback &callback);
132 virtual void GetProperties(const ResultCallback &callback) = 0;
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500133
Eric Shienbrood9a245532012-03-07 14:20:39 -0500134 virtual void GetRegistrationState() = 0;
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500135
Eric Shienbrood9a245532012-03-07 14:20:39 -0500136 void FinishEnable(const ResultCallback &callback);
137 void FinishDisable(const ResultCallback &callback);
138 virtual void InitProxies();
139 virtual void ReleaseProxies();
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500140
Eric Shienbrood9a245532012-03-07 14:20:39 -0500141 static void OnUnsupportedOperation(const char *operation, Error *error);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500142
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400143 virtual void OnConnectReply(const ResultCallback &callback,
144 const Error &error);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500145 // Run the next task in a list.
146 // Precondition: |tasks| is not empty.
147 void RunNextStep(CellularTaskList *tasks);
148 void StepCompletedCallback(const ResultCallback &callback,
149 CellularTaskList *tasks, const Error &error);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500150 // Properties
151 bool allow_roaming_;
Darin Petkov0d06f7d2012-02-03 13:08:19 +0100152 bool scanning_supported_;
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500153 std::string carrier_;
154 std::string meid_;
155 std::string imei_;
156 std::string imsi_;
157 std::string esn_;
158 std::string mdn_;
159 std::string min_;
160 std::string model_id_;
161 std::string manufacturer_;
162 std::string firmware_revision_;
163 std::string hardware_revision_;
164
Darin Petkovdaf43862011-10-27 11:37:28 +0200165 private:
Darin Petkov721ac932011-11-16 15:43:09 +0100166 friend class CellularTest;
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500167 friend class CellularCapabilityTest;
Darin Petkov31332412012-01-28 01:50:02 +0100168 FRIEND_TEST(CellularCapabilityGSMTest, SetStorageIdentifier);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500169 FRIEND_TEST(CellularCapabilityGSMTest, UpdateStatus);
Darin Petkov9c1dcef2012-02-07 15:58:26 +0100170 FRIEND_TEST(CellularCapabilityTest, AllowRoaming);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500171 FRIEND_TEST(CellularCapabilityTest, EnableModemFail);
172 FRIEND_TEST(CellularCapabilityTest, EnableModemSucceed);
173 FRIEND_TEST(CellularCapabilityTest, FinishEnable);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500174 FRIEND_TEST(CellularCapabilityTest, GetModemInfo);
175 FRIEND_TEST(CellularCapabilityTest, GetModemStatus);
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400176 FRIEND_TEST(CellularCapabilityTest, TryApns);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500177 FRIEND_TEST(CellularServiceTest, FriendlyName);
178 FRIEND_TEST(CellularTest, StartCDMARegister);
179 FRIEND_TEST(CellularTest, StartConnected);
180 FRIEND_TEST(CellularTest, StartGSMRegister);
181 FRIEND_TEST(CellularTest, StartLinked);
182 FRIEND_TEST(CellularTest, Connect);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500183 FRIEND_TEST(CellularTest, Disconnect);
Darin Petkov721ac932011-11-16 15:43:09 +0100184
Darin Petkov9c1dcef2012-02-07 15:58:26 +0100185 void HelpRegisterDerivedBool(
186 const std::string &name,
187 bool(CellularCapability::*get)(Error *error),
188 void(CellularCapability::*set)(const bool &value, Error *error));
189
190 bool GetAllowRoaming(Error */*error*/) { return allow_roaming_; }
191 void SetAllowRoaming(const bool &value, Error *error);
192
Eric Shienbrood9a245532012-03-07 14:20:39 -0500193 // Method reply and signal callbacks from Modem interface
194 virtual void OnModemStateChangedSignal(
195 uint32 old_state, uint32 new_state, uint32 reason);
196 virtual void OnGetModemInfoReply(const ResultCallback &callback,
197 const ModemHardwareInfo &info,
198 const Error &error);
199
200 // Method reply callbacks from Modem.Simple interface
201 virtual void OnGetModemStatusReply(const ResultCallback &callback,
202 const DBusPropertiesMap &props,
203 const Error &error);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500204 virtual void OnDisconnectReply(const ResultCallback &callback,
205 const Error &error);
206
Darin Petkovdaf43862011-10-27 11:37:28 +0200207 Cellular *cellular_;
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500208
209 // Store cached copies of singletons for speed/ease of testing.
Darin Petkovdaf43862011-10-27 11:37:28 +0200210 ProxyFactory *proxy_factory_;
211
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500212 scoped_ptr<ModemProxyInterface> proxy_;
213 scoped_ptr<ModemSimpleProxyInterface> simple_proxy_;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500214 base::WeakPtrFactory<CellularCapability> weak_ptr_factory_;
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500215
Darin Petkovdaf43862011-10-27 11:37:28 +0200216 DISALLOW_COPY_AND_ASSIGN(CellularCapability);
217};
218
219} // namespace shill
220
221#endif // SHILL_CELLULAR_CAPABILITY_