blob: ee9683eaef6a3b3cf0f1b33d8f37ca312221912b [file] [log] [blame]
Darin Petkovc64fe5e2012-01-11 12:46:13 +01001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Chris Masone3bd3c8c2011-06-13 08:20:26 -07002// 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_
6#define SHILL_CELLULAR_
7
8#include <string>
Jason Glasgow82f9ab32012-04-04 14:27:19 -04009#include <vector>
Chris Masone3bd3c8c2011-06-13 08:20:26 -070010
11#include <base/basictypes.h>
Darin Petkove9d12e02011-07-27 15:09:37 -070012#include <gtest/gtest_prod.h> // for FRIEND_TEST
Chris Masone3bd3c8c2011-06-13 08:20:26 -070013
Darin Petkovc5f56562011-08-06 16:40:05 -070014#include "shill/dbus_properties.h"
Chris Masone3bd3c8c2011-06-13 08:20:26 -070015#include "shill/device.h"
Paul Stewart26b327e2011-10-19 11:38:09 -070016#include "shill/event_dispatcher.h"
Darin Petkovc5f56562011-08-06 16:40:05 -070017#include "shill/modem_proxy_interface.h"
Chris Masone2b105542011-06-22 10:58:09 -070018#include "shill/refptr_types.h"
Chris Masone3bd3c8c2011-06-13 08:20:26 -070019
Darin Petkov137884a2011-10-26 18:52:47 +020020struct mobile_provider_db;
21
Chris Masone3bd3c8c2011-06-13 08:20:26 -070022namespace shill {
23
Darin Petkovdaf43862011-10-27 11:37:28 +020024class CellularCapability;
Darin Petkov4d6d9412011-08-24 13:19:54 -070025class Error;
Darin Petkovab565bb2011-10-06 02:55:51 -070026class ProxyFactory;
Darin Petkove9d12e02011-07-27 15:09:37 -070027
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050028class Cellular : public Device {
Chris Masone3bd3c8c2011-06-13 08:20:26 -070029 public:
Darin Petkove9d12e02011-07-27 15:09:37 -070030 enum Type {
31 kTypeGSM,
David Rochbergfa1d31d2012-03-20 10:38:07 -040032 kTypeCDMA,
33 kTypeUniversal, // ModemManager1
34 kTypeInvalid,
Darin Petkove9d12e02011-07-27 15:09:37 -070035 };
36
Darin Petkov0828f5f2011-08-11 10:18:52 -070037 // The device states progress linearly from Disabled to Linked.
Darin Petkove9d12e02011-07-27 15:09:37 -070038 enum State {
Darin Petkov0828f5f2011-08-11 10:18:52 -070039 // This is the initial state of the modem and indicates that the modem radio
40 // is not turned on.
Darin Petkove9d12e02011-07-27 15:09:37 -070041 kStateDisabled,
Darin Petkov0828f5f2011-08-11 10:18:52 -070042 // This state indicates that the modem radio is turned on, and it should be
43 // possible to measure signal strength.
Darin Petkove9d12e02011-07-27 15:09:37 -070044 kStateEnabled,
Darin Petkov0828f5f2011-08-11 10:18:52 -070045 // The modem has registered with a network and has signal quality
Darin Petkov51489002011-08-18 13:13:20 -070046 // measurements. A cellular service object is created.
Darin Petkove9d12e02011-07-27 15:09:37 -070047 kStateRegistered,
Darin Petkov0828f5f2011-08-11 10:18:52 -070048 // The modem has connected to a network.
Darin Petkove9d12e02011-07-27 15:09:37 -070049 kStateConnected,
Darin Petkov0828f5f2011-08-11 10:18:52 -070050 // The network interface is UP.
51 kStateLinked,
Darin Petkove9d12e02011-07-27 15:09:37 -070052 };
53
Darin Petkovbac96002011-08-09 13:22:00 -070054 enum ModemState {
55 kModemStateUnknown = 0,
Eric Shienbrood7fce52c2012-04-13 19:11:02 -040056 kModemStateInitializing = 1,
57 kModemStateLocked = 2,
58 kModemStateDisabled = 3,
David Rochbergfa1d31d2012-03-20 10:38:07 -040059 kModemStateDisabling = 4,
60 kModemStateEnabling = 5,
61 kModemStateEnabled = 6,
62 kModemStateSearching = 7,
63 kModemStateRegistered = 8,
64 kModemStateDisconnecting = 9,
65 kModemStateConnecting = 10,
66 kModemStateConnected = 11,
Darin Petkovbac96002011-08-09 13:22:00 -070067 };
68
Darin Petkov3335b372011-08-22 11:05:32 -070069 class Operator {
70 public:
71 Operator();
72 ~Operator();
73
74 void CopyFrom(const Operator &oper);
Darin Petkov9cb02682012-01-28 00:17:38 +010075 bool Equals(const Operator &oper) const { return dict_ == oper.dict_; }
Darin Petkov3335b372011-08-22 11:05:32 -070076
77 const std::string &GetName() const;
78 void SetName(const std::string &name);
79
80 const std::string &GetCode() const;
81 void SetCode(const std::string &code);
82
83 const std::string &GetCountry() const;
84 void SetCountry(const std::string &country);
85
86 const Stringmap &ToDict() const;
87
88 private:
89 Stringmap dict_;
90
91 DISALLOW_COPY_AND_ASSIGN(Operator);
92 };
93
Darin Petkove9d12e02011-07-27 15:09:37 -070094 // |owner| is the ModemManager DBus service owner (e.g., ":1.17"). |path| is
95 // the ModemManager.Modem DBus object path (e.g.,
96 // "/org/chromium/ModemManager/Gobi/0").
Chris Masone3bd3c8c2011-06-13 08:20:26 -070097 Cellular(ControlInterface *control_interface,
98 EventDispatcher *dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -080099 Metrics *metrics,
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700100 Manager *manager,
Darin Petkove9d12e02011-07-27 15:09:37 -0700101 const std::string &link_name,
Chris Masone626719f2011-08-18 16:58:48 -0700102 const std::string &address,
Darin Petkove9d12e02011-07-27 15:09:37 -0700103 int interface_index,
104 Type type,
105 const std::string &owner,
Darin Petkov137884a2011-10-26 18:52:47 +0200106 const std::string &path,
107 mobile_provider_db *provider_db);
Darin Petkove9d12e02011-07-27 15:09:37 -0700108 virtual ~Cellular();
109
Darin Petkov4d6d9412011-08-24 13:19:54 -0700110 // Asynchronously connects the modem to the network. Populates |error| on
111 // failure, leaves it unchanged otherwise.
112 void Connect(Error *error);
Darin Petkovc5f56562011-08-06 16:40:05 -0700113
Darin Petkovfb0625e2012-01-16 13:05:56 +0100114 // Asynchronously disconnects the modem from the network. Populates |error| on
115 // failure, leaves it unchanged otherwise.
116 void Disconnect(Error *error);
117
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500118 // Asynchronously activates the modem. Returns an error on failure.
Eric Shienbrood9a245532012-03-07 14:20:39 -0500119 void Activate(const std::string &carrier, Error *error,
120 const ResultCallback &callback);
Darin Petkovc408e692011-08-17 13:47:15 -0700121
Darin Petkov3e509242011-11-10 14:46:44 +0100122 const CellularServiceRefPtr &service() const { return service_; }
123
Darin Petkova3d3be52011-11-14 21:34:16 +0100124 static std::string GetStateString(State state);
125
Darin Petkovac635a82012-01-10 16:51:58 +0100126 std::string CreateFriendlyServiceName();
127
Darin Petkova3d3be52011-11-14 21:34:16 +0100128 State state() const { return state_; }
129
Darin Petkovbac96002011-08-09 13:22:00 -0700130 void set_modem_state(ModemState state) { modem_state_ = state; }
131 ModemState modem_state() const { return modem_state_; }
Eric Shienbrood7fce52c2012-04-13 19:11:02 -0400132 bool IsUnderlyingDeviceEnabled() const;
133 static bool IsEnabledModemState(ModemState state);
Darin Petkovbac96002011-08-09 13:22:00 -0700134
Darin Petkov1272a432011-11-10 15:53:37 +0100135 mobile_provider_db *provider_db() const { return provider_db_; }
136
Darin Petkovdaf43862011-10-27 11:37:28 +0200137 const std::string &dbus_owner() const { return dbus_owner_; }
138 const std::string &dbus_path() const { return dbus_path_; }
139
Darin Petkovae0c64e2011-11-15 15:50:27 +0100140 const Operator &home_provider() const { return home_provider_; }
141 void set_home_provider(const Operator &oper);
Darin Petkov184c54e2011-11-15 12:44:39 +0100142
Darin Petkov3e509242011-11-10 14:46:44 +0100143 void HandleNewSignalQuality(uint32 strength);
144
Darin Petkov184c54e2011-11-15 12:44:39 +0100145 // Processes a change in the modem registration state, possibly creating,
146 // destroying or updating the CellularService.
147 void HandleNewRegistrationState();
148
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400149 virtual void OnDBusPropertiesChanged(
150 const std::string &interface,
151 const DBusPropertiesMap &changed_properties,
152 const std::vector<std::string> &invalidated_properties);
Darin Petkov184c54e2011-11-15 12:44:39 +0100153
Darin Petkove9d12e02011-07-27 15:09:37 -0700154 // Inherited from Device.
Eric Shienbrood9a245532012-03-07 14:20:39 -0500155 virtual void Start(Error *error, const EnabledStateChangedCallback &callback);
156 virtual void Stop(Error *error, const EnabledStateChangedCallback &callback);
Paul Stewartfdd16072011-09-16 12:41:35 -0700157 virtual bool TechnologyIs(Technology::Identifier type) const;
Darin Petkov0828f5f2011-08-11 10:18:52 -0700158 virtual void LinkEvent(unsigned int flags, unsigned int change);
Darin Petkovc0865312011-09-16 15:31:20 -0700159 virtual void Scan(Error *error);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500160 virtual void RegisterOnNetwork(const std::string &network_id,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500161 Error *error,
162 const ResultCallback &callback);
163 virtual void RequirePIN(const std::string &pin, bool require,
164 Error *error, const ResultCallback &callback);
165 virtual void EnterPIN(const std::string &pin,
166 Error *error, const ResultCallback &callback);
Darin Petkove42e1012011-08-31 12:35:04 -0700167 virtual void UnblockPIN(const std::string &unblock_code,
168 const std::string &pin,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500169 Error *error, const ResultCallback &callback);
Darin Petkove42e1012011-08-31 12:35:04 -0700170 virtual void ChangePIN(const std::string &old_pin,
171 const std::string &new_pin,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500172 Error *error, const ResultCallback &callback);
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700173
Eric Shienbrood9a245532012-03-07 14:20:39 -0500174 void OnModemStarted(const EnabledStateChangedCallback &callback,
175 const Error &error);
176 void OnModemStopped(const EnabledStateChangedCallback &callback,
177 const Error &error);
Eric Shienbrood7fce52c2012-04-13 19:11:02 -0400178 void OnConnecting();
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500179 void OnConnected();
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400180 void OnConnectFailed(const Error &error);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500181 void OnDisconnected();
182 void OnDisconnectFailed();
Eric Shienbrood0db6a9b2012-03-30 16:11:39 -0400183 std::string GetTechnologyFamily(Error *error);
Eric Shienbrood7fce52c2012-04-13 19:11:02 -0400184 void OnModemStateChanged(ModemState old_state,
185 ModemState new_state,
186 uint32 reason);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500187
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700188 private:
Darin Petkovab565bb2011-10-06 02:55:51 -0700189 friend class CellularTest;
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500190 friend class CellularCapabilityTest;
Darin Petkov20c13ec2011-11-09 15:07:15 +0100191 friend class CellularCapabilityCDMATest;
192 friend class CellularCapabilityGSMTest;
Jason Glasgowef965562012-04-10 16:12:35 -0400193 friend class CellularCapabilityUniversalTest;
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400194 friend class CellularServiceTest;
Darin Petkov721ac932011-11-16 15:43:09 +0100195 friend class ModemTest;
Darin Petkovac635a82012-01-10 16:51:58 +0100196 FRIEND_TEST(CellularCapabilityCDMATest, CreateFriendlyServiceName);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500197 FRIEND_TEST(CellularCapabilityCDMATest, GetRegistrationState);
Darin Petkovac635a82012-01-10 16:51:58 +0100198 FRIEND_TEST(CellularCapabilityGSMTest, CreateFriendlyServiceName);
199 FRIEND_TEST(CellularServiceTest, FriendlyName);
Darin Petkov3335b372011-08-22 11:05:32 -0700200 FRIEND_TEST(CellularTest, CreateService);
Darin Petkovc5f56562011-08-06 16:40:05 -0700201 FRIEND_TEST(CellularTest, Connect);
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400202 FRIEND_TEST(CellularTest, ConnectFailure);
Darin Petkovfb0625e2012-01-16 13:05:56 +0100203 FRIEND_TEST(CellularTest, DisableModem);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500204 FRIEND_TEST(CellularTest, Disconnect);
Eric Shienbrood7fce52c2012-04-13 19:11:02 -0400205 FRIEND_TEST(CellularTest, ModemStateChangeDisable);
206 FRIEND_TEST(CellularTest, ModemStateChangeEnable);
Darin Petkovbac96002011-08-09 13:22:00 -0700207 FRIEND_TEST(CellularTest, StartConnected);
Darin Petkova1e0a1c2011-08-25 15:08:33 -0700208 FRIEND_TEST(CellularTest, StartCDMARegister);
Darin Petkov9bac6fe2011-08-26 12:49:05 -0700209 FRIEND_TEST(CellularTest, StartGSMRegister);
Darin Petkov0828f5f2011-08-11 10:18:52 -0700210 FRIEND_TEST(CellularTest, StartLinked);
Darin Petkov9c1dcef2012-02-07 15:58:26 +0100211 FRIEND_TEST(CellularCapabilityTest, AllowRoaming);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500212 FRIEND_TEST(CellularCapabilityTest, EnableModemFail);
213 FRIEND_TEST(CellularCapabilityTest, EnableModemSucceed);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500214 FRIEND_TEST(CellularCapabilityTest, FinishEnable);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500215 FRIEND_TEST(CellularCapabilityTest, GetModemInfo);
216 FRIEND_TEST(CellularCapabilityTest, GetModemStatus);
Darin Petkove9d12e02011-07-27 15:09:37 -0700217
Darin Petkov0828f5f2011-08-11 10:18:52 -0700218 void SetState(State state);
219
Darin Petkovbac96002011-08-09 13:22:00 -0700220 // Invoked when the modem is connected to the cellular network to transition
221 // to the network-connected state and bring the network interface up.
222 void EstablishLink();
223
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500224 void InitCapability(Type type, ProxyFactory *proxy_factory);
Darin Petkovf5f61e02011-07-29 11:35:40 -0700225
Darin Petkovd9661952011-08-03 16:25:42 -0700226 void CreateService();
Darin Petkov2c377382012-01-11 11:40:43 +0100227 void DestroyService();
Darin Petkovd9661952011-08-03 16:25:42 -0700228
Eric Shienbrood0db6a9b2012-03-30 16:11:39 -0400229 // HelpRegisterDerived*: Expose a property over RPC, with the name |name|.
230 //
231 // Reads of the property will be handled by invoking |get|.
232 // Writes to the property will be handled by invoking |set|.
233 // Clearing the property will be handled by PropertyStore.
234 void HelpRegisterDerivedString(
235 const std::string &name,
236 std::string(Cellular::*get)(Error *error),
237 void(Cellular::*set)(const std::string &value, Error *error));
238
Nathan Williamsb54974f2012-04-19 11:16:30 -0400239 void OnConnectReply(const Error &error);
240 void OnDisconnectReply(const Error &error);
241
Darin Petkove9d12e02011-07-27 15:09:37 -0700242 State state_;
Darin Petkovbac96002011-08-09 13:22:00 -0700243 ModemState modem_state_;
Darin Petkove9d12e02011-07-27 15:09:37 -0700244
Darin Petkovdaf43862011-10-27 11:37:28 +0200245 scoped_ptr<CellularCapability> capability_;
246
Darin Petkove9d12e02011-07-27 15:09:37 -0700247 const std::string dbus_owner_; // ModemManager.Modem
248 const std::string dbus_path_; // ModemManager.Modem
Darin Petkovbec79a22011-08-01 14:47:17 -0700249
Darin Petkov137884a2011-10-26 18:52:47 +0200250 mobile_provider_db *provider_db_;
251
Darin Petkovd9661952011-08-03 16:25:42 -0700252 CellularServiceRefPtr service_;
Chris Masone853b81b2011-06-24 14:11:41 -0700253
Chris Masoneb925cc82011-06-22 15:39:57 -0700254 // Properties
Darin Petkov3335b372011-08-22 11:05:32 -0700255 Operator home_provider_;
Chris Masoneb925cc82011-06-22 15:39:57 -0700256
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700257 DISALLOW_COPY_AND_ASSIGN(Cellular);
258};
259
260} // namespace shill
261
262#endif // SHILL_CELLULAR_