blob: ef0060f32c175e756ca6551eaa5f3dcbf7cfbe2a [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;
Thieu Le923006b2012-04-05 16:32:58 -070075 // StopModem disconnects and disables a modem asynchronously.
76 // |callback| is invoked when this completes and the result is passed
77 // to the callback.
78 virtual void StopModem(Error *error, const ResultCallback &callback);
Eric Shienbrood9a245532012-03-07 14:20:39 -050079 virtual void Connect(const DBusPropertiesMap &properties, Error *error,
80 const ResultCallback &callback);
81 virtual void Disconnect(Error *error, const ResultCallback &callback);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050082
83 // Activates the modem. Returns an Error on failure.
84 // The default implementation fails by returning a kNotSupported error
85 // to the caller.
86 virtual void Activate(const std::string &carrier,
Eric Shienbrood9a245532012-03-07 14:20:39 -050087 Error *error, const ResultCallback &callback);
Darin Petkova3d3be52011-11-14 21:34:16 +010088
Darin Petkov184c54e2011-11-15 12:44:39 +010089 // Network registration.
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050090 virtual void RegisterOnNetwork(const std::string &network_id,
Eric Shienbrood9a245532012-03-07 14:20:39 -050091 Error *error,
92 const ResultCallback &callback);
Darin Petkovb72cf402011-11-22 14:51:39 +010093 virtual bool IsRegistered() = 0;
Darin Petkov184c54e2011-11-15 12:44:39 +010094
Darin Petkovac635a82012-01-10 16:51:58 +010095 virtual std::string CreateFriendlyServiceName() = 0;
96
Darin Petkovc64fe5e2012-01-11 12:46:13 +010097 // PIN management. The default implementation fails by returning an error.
Eric Shienbrood9a245532012-03-07 14:20:39 -050098 virtual void RequirePIN(const std::string &pin, bool require,
99 Error *error, const ResultCallback &callback);
100 virtual void EnterPIN(const std::string &pin,
101 Error *error, const ResultCallback &callback);
Darin Petkovb05315f2011-11-07 10:14:25 +0100102 virtual void UnblockPIN(const std::string &unblock_code,
103 const std::string &pin,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500104 Error *error, const ResultCallback &callback);
Darin Petkovb05315f2011-11-07 10:14:25 +0100105 virtual void ChangePIN(const std::string &old_pin,
106 const std::string &new_pin,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500107 Error *error, const ResultCallback &callback);
Darin Petkovb05315f2011-11-07 10:14:25 +0100108
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500109 // Network scanning. The default implementation fails by invoking
110 // the reply handler with an error.
Eric Shienbrood9a245532012-03-07 14:20:39 -0500111 virtual void Scan(Error *error, const ResultCallback &callback);
Darin Petkov1272a432011-11-10 15:53:37 +0100112
Darin Petkov20c13ec2011-11-09 15:07:15 +0100113 // Returns an empty string if the network technology is unknown.
114 virtual std::string GetNetworkTechnologyString() const = 0;
115
116 virtual std::string GetRoamingStateString() const = 0;
117
Eric Shienbrood9a245532012-03-07 14:20:39 -0500118 virtual void GetSignalQuality() = 0;
119
Eric Shienbrood0db6a9b2012-03-30 16:11:39 -0400120 virtual std::string GetTypeString() const = 0;
121
Darin Petkovae0c64e2011-11-15 15:50:27 +0100122 virtual void OnModemManagerPropertiesChanged(
123 const DBusPropertiesMap &properties) = 0;
124
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500125 protected:
Eric Shienbrood9a245532012-03-07 14:20:39 -0500126 // The following five methods are only ever called as
127 // callbacks (from the main loop), which is why they
128 // don't take an Error * argument.
129 virtual void EnableModem(const ResultCallback &callback);
130 virtual void DisableModem(const ResultCallback &callback);
131 virtual void GetModemStatus(const ResultCallback &callback);
132 virtual void GetModemInfo(const ResultCallback &callback);
133 virtual void GetProperties(const ResultCallback &callback) = 0;
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500134
Eric Shienbrood9a245532012-03-07 14:20:39 -0500135 virtual void GetRegistrationState() = 0;
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500136
Eric Shienbrood9a245532012-03-07 14:20:39 -0500137 void FinishEnable(const ResultCallback &callback);
138 void FinishDisable(const ResultCallback &callback);
139 virtual void InitProxies();
140 virtual void ReleaseProxies();
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500141
Eric Shienbrood9a245532012-03-07 14:20:39 -0500142 static void OnUnsupportedOperation(const char *operation, Error *error);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500143
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400144 virtual void OnConnectReply(const ResultCallback &callback,
145 const Error &error);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500146 // Run the next task in a list.
147 // Precondition: |tasks| is not empty.
148 void RunNextStep(CellularTaskList *tasks);
Thieu Le923006b2012-04-05 16:32:58 -0700149 // StepCompletedCallback is called after a task completes.
150 // |callback| is the original callback that needs to be invoked when all of
151 // the tasks complete or if there is a failure. |ignore_error| will be set
152 // to true if the next task should be run regardless of the result of the
153 // just-completed task. |tasks| is the list of tasks remaining. |error| is
154 // the result of the just-completed task.
155 void StepCompletedCallback(const ResultCallback &callback, bool ignore_error,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500156 CellularTaskList *tasks, const Error &error);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500157 // Properties
158 bool allow_roaming_;
Darin Petkov0d06f7d2012-02-03 13:08:19 +0100159 bool scanning_supported_;
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500160 std::string carrier_;
161 std::string meid_;
162 std::string imei_;
163 std::string imsi_;
164 std::string esn_;
165 std::string mdn_;
166 std::string min_;
167 std::string model_id_;
168 std::string manufacturer_;
169 std::string firmware_revision_;
170 std::string hardware_revision_;
171
Darin Petkovdaf43862011-10-27 11:37:28 +0200172 private:
Thieu Le923006b2012-04-05 16:32:58 -0700173 friend class CellularCapabilityGSMTest;
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500174 friend class CellularCapabilityTest;
Thieu Le923006b2012-04-05 16:32:58 -0700175 friend class CellularTest;
Darin Petkov31332412012-01-28 01:50:02 +0100176 FRIEND_TEST(CellularCapabilityGSMTest, SetStorageIdentifier);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500177 FRIEND_TEST(CellularCapabilityGSMTest, UpdateStatus);
Darin Petkov9c1dcef2012-02-07 15:58:26 +0100178 FRIEND_TEST(CellularCapabilityTest, AllowRoaming);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500179 FRIEND_TEST(CellularCapabilityTest, EnableModemFail);
180 FRIEND_TEST(CellularCapabilityTest, EnableModemSucceed);
181 FRIEND_TEST(CellularCapabilityTest, FinishEnable);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500182 FRIEND_TEST(CellularCapabilityTest, GetModemInfo);
183 FRIEND_TEST(CellularCapabilityTest, GetModemStatus);
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400184 FRIEND_TEST(CellularCapabilityTest, TryApns);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500185 FRIEND_TEST(CellularServiceTest, FriendlyName);
186 FRIEND_TEST(CellularTest, StartCDMARegister);
187 FRIEND_TEST(CellularTest, StartConnected);
188 FRIEND_TEST(CellularTest, StartGSMRegister);
189 FRIEND_TEST(CellularTest, StartLinked);
190 FRIEND_TEST(CellularTest, Connect);
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400191 FRIEND_TEST(CellularTest, ConnectFailure);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500192 FRIEND_TEST(CellularTest, Disconnect);
Darin Petkov721ac932011-11-16 15:43:09 +0100193
Darin Petkov9c1dcef2012-02-07 15:58:26 +0100194 void HelpRegisterDerivedBool(
195 const std::string &name,
196 bool(CellularCapability::*get)(Error *error),
197 void(CellularCapability::*set)(const bool &value, Error *error));
198
199 bool GetAllowRoaming(Error */*error*/) { return allow_roaming_; }
200 void SetAllowRoaming(const bool &value, Error *error);
201
Eric Shienbrood9a245532012-03-07 14:20:39 -0500202 // Method reply and signal callbacks from Modem interface
203 virtual void OnModemStateChangedSignal(
204 uint32 old_state, uint32 new_state, uint32 reason);
205 virtual void OnGetModemInfoReply(const ResultCallback &callback,
206 const ModemHardwareInfo &info,
207 const Error &error);
208
209 // Method reply callbacks from Modem.Simple interface
210 virtual void OnGetModemStatusReply(const ResultCallback &callback,
211 const DBusPropertiesMap &props,
212 const Error &error);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500213 virtual void OnDisconnectReply(const ResultCallback &callback,
214 const 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
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500221 scoped_ptr<ModemProxyInterface> proxy_;
222 scoped_ptr<ModemSimpleProxyInterface> simple_proxy_;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500223 base::WeakPtrFactory<CellularCapability> weak_ptr_factory_;
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500224
Darin Petkovdaf43862011-10-27 11:37:28 +0200225 DISALLOW_COPY_AND_ASSIGN(CellularCapability);
226};
227
228} // namespace shill
229
230#endif // SHILL_CELLULAR_CAPABILITY_