blob: fd1cb584a4c725e5aa32327dc97be0f105d54835 [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
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050016#include "shill/async_call_handler.h"
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 Shienbrood5de44ab2011-12-05 10:46:27 -050028// Cellular devices instantiate subclasses of CellularCapability that
29// handle the specific modem technologies and capabilities.
30class CellularCapability : public ModemProxyDelegate,
31 public ModemSimpleProxyDelegate {
Darin Petkovdaf43862011-10-27 11:37:28 +020032 public:
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050033 static const int kTimeoutActivate;
34 static const int kTimeoutConnect;
35 static const int kTimeoutDefault;
36 static const int kTimeoutRegister;
37 static const int kTimeoutScan;
38
39 static const char kConnectPropertyPhoneNumber[];
40 static const char kPropertyIMSI[];
41
42
Darin Petkovdaf43862011-10-27 11:37:28 +020043 // |cellular| is the parent Cellular device.
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050044 CellularCapability(Cellular *cellular, ProxyFactory *proxy_factory);
Darin Petkovdaf43862011-10-27 11:37:28 +020045 virtual ~CellularCapability();
46
47 Cellular *cellular() const { return cellular_; }
48 ProxyFactory *proxy_factory() const { return proxy_factory_; }
Darin Petkov5f316f62011-11-18 12:10:26 +010049
50 // Invoked by the parent Cellular device when a new service is created.
51 virtual void OnServiceCreated() = 0;
Darin Petkovdaf43862011-10-27 11:37:28 +020052
Darin Petkovae0c64e2011-11-15 15:50:27 +010053 virtual void UpdateStatus(const DBusPropertiesMap &properties) = 0;
54
55 virtual void SetupConnectProperties(DBusPropertiesMap *properties) = 0;
56
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050057 bool allow_roaming() const { return allow_roaming_; }
58
59 // StartModem attempts to put the modem in a state in which it is
60 // usable for creating services and establishing connections (if
61 // network conditions permit). It potentially consists of multiple
62 // non-blocking calls to the modem-manager server. After each call,
63 // control is passed back up to the main loop. Each time a reply to
64 // a non-blocking call is received, the operation advances to the next
65 // step, until either an error occurs in one of them, or all the steps
66 // have been completed, at which point StartModem() is finished.
67 virtual void StartModem();
68 virtual void StopModem();
69 // EnableModem ensures that the modem device is powered.
70 virtual void EnableModem(AsyncCallHandler *call_handler);
71 virtual void DisableModem(AsyncCallHandler *call_handler);
72 virtual void GetModemStatus(AsyncCallHandler *call_handler);
73 virtual void GetModemInfo(AsyncCallHandler *call_handler);
74 virtual void Connect(const DBusPropertiesMap &properties);
75 virtual void Disconnect();
76
77 // Activates the modem. Returns an Error on failure.
78 // The default implementation fails by returning a kNotSupported error
79 // to the caller.
80 virtual void Activate(const std::string &carrier,
81 AsyncCallHandler *call_handler);
Darin Petkova3d3be52011-11-14 21:34:16 +010082
Darin Petkov184c54e2011-11-15 12:44:39 +010083 // Network registration.
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050084 virtual void RegisterOnNetwork(const std::string &network_id,
85 AsyncCallHandler *call_handler);
Darin Petkovb72cf402011-11-22 14:51:39 +010086 virtual bool IsRegistered() = 0;
Darin Petkov184c54e2011-11-15 12:44:39 +010087
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050088 virtual void GetProperties(AsyncCallHandler *call_handler) = 0;
Darin Petkov184c54e2011-11-15 12:44:39 +010089
Darin Petkov3e509242011-11-10 14:46:44 +010090 // Retrieves the current cellular signal strength.
91 virtual void GetSignalQuality() = 0;
92
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050093 virtual void GetRegistrationState(AsyncCallHandler *call_handler) = 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.
98 virtual void RequirePIN(
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050099 const std::string &pin, bool require, AsyncCallHandler *call_handler);
100 virtual void EnterPIN(const std::string &pin, AsyncCallHandler *call_handler);
Darin Petkovb05315f2011-11-07 10:14:25 +0100101 virtual void UnblockPIN(const std::string &unblock_code,
102 const std::string &pin,
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500103 AsyncCallHandler *call_handler);
Darin Petkovb05315f2011-11-07 10:14:25 +0100104 virtual void ChangePIN(const std::string &old_pin,
105 const std::string &new_pin,
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500106 AsyncCallHandler *call_handler);
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.
110 virtual void Scan(AsyncCallHandler *call_handler);
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
Darin Petkovae0c64e2011-11-15 15:50:27 +0100117 virtual void OnModemManagerPropertiesChanged(
118 const DBusPropertiesMap &properties) = 0;
119
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500120 // Method and signal callbacks inherited from ModemProxyDelegate.
121 virtual void OnModemStateChanged(
122 uint32 old_state, uint32 new_state, uint32 reason);
123 virtual void OnModemEnableCallback(const Error &error,
124 AsyncCallHandler *call_handler);
125 virtual void OnGetModemInfoCallback(const ModemHardwareInfo &info,
126 const Error &error,
127 AsyncCallHandler *call_handler);
128
129 // Method and signal callbacks inherited from ModemSimpleProxyDelegate.
130 virtual void OnGetModemStatusCallback(const DBusPropertiesMap &props,
131 const Error &error,
132 AsyncCallHandler *call_handler);
133 virtual void OnConnectCallback(const Error &error,
134 AsyncCallHandler *call_handler);
135 virtual void OnDisconnectCallback(const Error &error,
136 AsyncCallHandler *call_handler);
137
138 protected:
139 class MultiStepAsyncCallHandler : public AsyncCallHandler {
140 public:
141 explicit MultiStepAsyncCallHandler(EventDispatcher *dispatcher);
142 virtual ~MultiStepAsyncCallHandler();
143
144 // Override the non-error case
145 virtual bool CompleteOperation();
146
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500147 void AddTask(const base::Closure &task);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500148 void PostNextTask();
149
150 private:
151 EventDispatcher *dispatcher_;
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500152 std::vector<base::Closure> tasks_;
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500153
154 DISALLOW_COPY_AND_ASSIGN(MultiStepAsyncCallHandler);
155 };
156
157 static void CompleteOperation(AsyncCallHandler *reply_handler);
158 static void CompleteOperation(AsyncCallHandler *reply_handler,
159 const Error &error);
160 // Generic synthetic callback by which a Capability class can indicate
161 // that a requested operation is not supported.
162 void static OnUnsupportedOperation(const char *operation,
163 AsyncCallHandler *call_handler);
164
165 // Properties
166 bool allow_roaming_;
Darin Petkov0d06f7d2012-02-03 13:08:19 +0100167 bool scanning_supported_;
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500168 std::string carrier_;
169 std::string meid_;
170 std::string imei_;
171 std::string imsi_;
172 std::string esn_;
173 std::string mdn_;
174 std::string min_;
175 std::string model_id_;
176 std::string manufacturer_;
177 std::string firmware_revision_;
178 std::string hardware_revision_;
179
Darin Petkovdaf43862011-10-27 11:37:28 +0200180 private:
Darin Petkov721ac932011-11-16 15:43:09 +0100181 friend class CellularTest;
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500182 friend class CellularCapabilityTest;
Darin Petkov31332412012-01-28 01:50:02 +0100183 FRIEND_TEST(CellularCapabilityGSMTest, SetStorageIdentifier);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500184 FRIEND_TEST(CellularCapabilityGSMTest, UpdateStatus);
Darin Petkov9c1dcef2012-02-07 15:58:26 +0100185 FRIEND_TEST(CellularCapabilityTest, AllowRoaming);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500186 FRIEND_TEST(CellularCapabilityTest, GetModemInfo);
187 FRIEND_TEST(CellularCapabilityTest, GetModemStatus);
188 FRIEND_TEST(CellularServiceTest, FriendlyName);
189 FRIEND_TEST(CellularTest, StartCDMARegister);
190 FRIEND_TEST(CellularTest, StartConnected);
191 FRIEND_TEST(CellularTest, StartGSMRegister);
192 FRIEND_TEST(CellularTest, StartLinked);
193 FRIEND_TEST(CellularTest, Connect);
194 FRIEND_TEST(CellularTest, DisconnectModem);
Darin Petkov721ac932011-11-16 15:43:09 +0100195
Darin Petkov9c1dcef2012-02-07 15:58:26 +0100196 void HelpRegisterDerivedBool(
197 const std::string &name,
198 bool(CellularCapability::*get)(Error *error),
199 void(CellularCapability::*set)(const bool &value, Error *error));
200
201 bool GetAllowRoaming(Error */*error*/) { return allow_roaming_; }
202 void SetAllowRoaming(const bool &value, Error *error);
203
Darin Petkovdaf43862011-10-27 11:37:28 +0200204 Cellular *cellular_;
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500205
206 // Store cached copies of singletons for speed/ease of testing.
Darin Petkovdaf43862011-10-27 11:37:28 +0200207 ProxyFactory *proxy_factory_;
208
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500209 scoped_ptr<ModemProxyInterface> proxy_;
210 scoped_ptr<ModemSimpleProxyInterface> simple_proxy_;
211
Darin Petkovdaf43862011-10-27 11:37:28 +0200212 DISALLOW_COPY_AND_ASSIGN(CellularCapability);
213};
214
215} // namespace shill
216
217#endif // SHILL_CELLULAR_CAPABILITY_