blob: 1c31831ee0e18d15e97f9c9d8c63e5b40e61d688 [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>
9
10#include <base/basictypes.h>
Darin Petkove9d12e02011-07-27 15:09:37 -070011#include <gtest/gtest_prod.h> // for FRIEND_TEST
Chris Masone3bd3c8c2011-06-13 08:20:26 -070012
Darin Petkovc5f56562011-08-06 16:40:05 -070013#include "shill/dbus_properties.h"
Chris Masone3bd3c8c2011-06-13 08:20:26 -070014#include "shill/device.h"
Paul Stewart26b327e2011-10-19 11:38:09 -070015#include "shill/event_dispatcher.h"
Darin Petkovc5f56562011-08-06 16:40:05 -070016#include "shill/modem_proxy_interface.h"
Chris Masone2b105542011-06-22 10:58:09 -070017#include "shill/refptr_types.h"
Chris Masone3bd3c8c2011-06-13 08:20:26 -070018
Darin Petkov137884a2011-10-26 18:52:47 +020019struct mobile_provider_db;
20
Chris Masone3bd3c8c2011-06-13 08:20:26 -070021namespace shill {
22
Darin Petkovdaf43862011-10-27 11:37:28 +020023class CellularCapability;
Darin Petkov4d6d9412011-08-24 13:19:54 -070024class Error;
Darin Petkovab565bb2011-10-06 02:55:51 -070025class ProxyFactory;
Darin Petkove9d12e02011-07-27 15:09:37 -070026
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050027class Cellular : public Device {
Chris Masone3bd3c8c2011-06-13 08:20:26 -070028 public:
Darin Petkove9d12e02011-07-27 15:09:37 -070029 enum Type {
30 kTypeGSM,
David Rochbergfa1d31d2012-03-20 10:38:07 -040031 kTypeCDMA,
32 kTypeUniversal, // ModemManager1
33 kTypeInvalid,
Darin Petkove9d12e02011-07-27 15:09:37 -070034 };
35
Darin Petkov0828f5f2011-08-11 10:18:52 -070036 // The device states progress linearly from Disabled to Linked.
Darin Petkove9d12e02011-07-27 15:09:37 -070037 enum State {
Darin Petkov0828f5f2011-08-11 10:18:52 -070038 // This is the initial state of the modem and indicates that the modem radio
39 // is not turned on.
Darin Petkove9d12e02011-07-27 15:09:37 -070040 kStateDisabled,
Darin Petkov0828f5f2011-08-11 10:18:52 -070041 // This state indicates that the modem radio is turned on, and it should be
42 // possible to measure signal strength.
Darin Petkove9d12e02011-07-27 15:09:37 -070043 kStateEnabled,
Darin Petkov0828f5f2011-08-11 10:18:52 -070044 // The modem has registered with a network and has signal quality
Darin Petkov51489002011-08-18 13:13:20 -070045 // measurements. A cellular service object is created.
Darin Petkove9d12e02011-07-27 15:09:37 -070046 kStateRegistered,
Darin Petkov0828f5f2011-08-11 10:18:52 -070047 // The modem has connected to a network.
Darin Petkove9d12e02011-07-27 15:09:37 -070048 kStateConnected,
Darin Petkov0828f5f2011-08-11 10:18:52 -070049 // The network interface is UP.
50 kStateLinked,
Darin Petkove9d12e02011-07-27 15:09:37 -070051 };
52
Darin Petkovbac96002011-08-09 13:22:00 -070053 enum ModemState {
54 kModemStateUnknown = 0,
David Rochbergfa1d31d2012-03-20 10:38:07 -040055 kModemStateDisabled = 1,
56 kModemStateInitializing = 2,
57 kModemStateLocked = 3,
58 kModemStateDisabling = 4,
59 kModemStateEnabling = 5,
60 kModemStateEnabled = 6,
61 kModemStateSearching = 7,
62 kModemStateRegistered = 8,
63 kModemStateDisconnecting = 9,
64 kModemStateConnecting = 10,
65 kModemStateConnected = 11,
Darin Petkovbac96002011-08-09 13:22:00 -070066 };
67
Darin Petkov3335b372011-08-22 11:05:32 -070068 class Operator {
69 public:
70 Operator();
71 ~Operator();
72
73 void CopyFrom(const Operator &oper);
Darin Petkov9cb02682012-01-28 00:17:38 +010074 bool Equals(const Operator &oper) const { return dict_ == oper.dict_; }
Darin Petkov3335b372011-08-22 11:05:32 -070075
76 const std::string &GetName() const;
77 void SetName(const std::string &name);
78
79 const std::string &GetCode() const;
80 void SetCode(const std::string &code);
81
82 const std::string &GetCountry() const;
83 void SetCountry(const std::string &country);
84
85 const Stringmap &ToDict() const;
86
87 private:
88 Stringmap dict_;
89
90 DISALLOW_COPY_AND_ASSIGN(Operator);
91 };
92
Darin Petkove9d12e02011-07-27 15:09:37 -070093 // |owner| is the ModemManager DBus service owner (e.g., ":1.17"). |path| is
94 // the ModemManager.Modem DBus object path (e.g.,
95 // "/org/chromium/ModemManager/Gobi/0").
Chris Masone3bd3c8c2011-06-13 08:20:26 -070096 Cellular(ControlInterface *control_interface,
97 EventDispatcher *dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -080098 Metrics *metrics,
Chris Masone3bd3c8c2011-06-13 08:20:26 -070099 Manager *manager,
Darin Petkove9d12e02011-07-27 15:09:37 -0700100 const std::string &link_name,
Chris Masone626719f2011-08-18 16:58:48 -0700101 const std::string &address,
Darin Petkove9d12e02011-07-27 15:09:37 -0700102 int interface_index,
103 Type type,
104 const std::string &owner,
Darin Petkov137884a2011-10-26 18:52:47 +0200105 const std::string &path,
106 mobile_provider_db *provider_db);
Darin Petkove9d12e02011-07-27 15:09:37 -0700107 virtual ~Cellular();
108
Darin Petkov4d6d9412011-08-24 13:19:54 -0700109 // Asynchronously connects the modem to the network. Populates |error| on
110 // failure, leaves it unchanged otherwise.
111 void Connect(Error *error);
Darin Petkovc5f56562011-08-06 16:40:05 -0700112
Darin Petkovfb0625e2012-01-16 13:05:56 +0100113 // Asynchronously disconnects the modem from the network. Populates |error| on
114 // failure, leaves it unchanged otherwise.
115 void Disconnect(Error *error);
116
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500117 // Asynchronously activates the modem. Returns an error on failure.
Eric Shienbrood9a245532012-03-07 14:20:39 -0500118 void Activate(const std::string &carrier, Error *error,
119 const ResultCallback &callback);
Darin Petkovc408e692011-08-17 13:47:15 -0700120
Darin Petkov3e509242011-11-10 14:46:44 +0100121 const CellularServiceRefPtr &service() const { return service_; }
122
Darin Petkova3d3be52011-11-14 21:34:16 +0100123 static std::string GetStateString(State state);
124
Darin Petkovac635a82012-01-10 16:51:58 +0100125 std::string CreateFriendlyServiceName();
126
Darin Petkova3d3be52011-11-14 21:34:16 +0100127 State state() const { return state_; }
128
Darin Petkovbac96002011-08-09 13:22:00 -0700129 void set_modem_state(ModemState state) { modem_state_ = state; }
130 ModemState modem_state() const { return modem_state_; }
131
Darin Petkov1272a432011-11-10 15:53:37 +0100132 mobile_provider_db *provider_db() const { return provider_db_; }
133
Darin Petkovdaf43862011-10-27 11:37:28 +0200134 const std::string &dbus_owner() const { return dbus_owner_; }
135 const std::string &dbus_path() const { return dbus_path_; }
136
Darin Petkovae0c64e2011-11-15 15:50:27 +0100137 const Operator &home_provider() const { return home_provider_; }
138 void set_home_provider(const Operator &oper);
Darin Petkov184c54e2011-11-15 12:44:39 +0100139
Darin Petkov3e509242011-11-10 14:46:44 +0100140 void HandleNewSignalQuality(uint32 strength);
141
Darin Petkov184c54e2011-11-15 12:44:39 +0100142 // Processes a change in the modem registration state, possibly creating,
143 // destroying or updating the CellularService.
144 void HandleNewRegistrationState();
145
David Rochbergfa1d31d2012-03-20 10:38:07 -0400146 virtual void OnModemManagerPropertiesChanged(
147 const DBusPropertiesMap &properties);
Darin Petkov184c54e2011-11-15 12:44:39 +0100148
Darin Petkove9d12e02011-07-27 15:09:37 -0700149 // Inherited from Device.
Eric Shienbrood9a245532012-03-07 14:20:39 -0500150 virtual void Start(Error *error, const EnabledStateChangedCallback &callback);
151 virtual void Stop(Error *error, const EnabledStateChangedCallback &callback);
Paul Stewartfdd16072011-09-16 12:41:35 -0700152 virtual bool TechnologyIs(Technology::Identifier type) const;
Darin Petkov0828f5f2011-08-11 10:18:52 -0700153 virtual void LinkEvent(unsigned int flags, unsigned int change);
Darin Petkovc0865312011-09-16 15:31:20 -0700154 virtual void Scan(Error *error);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500155 virtual void RegisterOnNetwork(const std::string &network_id,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500156 Error *error,
157 const ResultCallback &callback);
158 virtual void RequirePIN(const std::string &pin, bool require,
159 Error *error, const ResultCallback &callback);
160 virtual void EnterPIN(const std::string &pin,
161 Error *error, const ResultCallback &callback);
Darin Petkove42e1012011-08-31 12:35:04 -0700162 virtual void UnblockPIN(const std::string &unblock_code,
163 const std::string &pin,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500164 Error *error, const ResultCallback &callback);
Darin Petkove42e1012011-08-31 12:35:04 -0700165 virtual void ChangePIN(const std::string &old_pin,
166 const std::string &new_pin,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500167 Error *error, const ResultCallback &callback);
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700168
Eric Shienbrood9a245532012-03-07 14:20:39 -0500169 void OnModemStarted(const EnabledStateChangedCallback &callback,
170 const Error &error);
171 void OnModemStopped(const EnabledStateChangedCallback &callback,
172 const Error &error);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500173 void OnConnected();
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400174 void OnConnectFailed(const Error &error);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500175 void OnDisconnected();
176 void OnDisconnectFailed();
Eric Shienbrood0db6a9b2012-03-30 16:11:39 -0400177 std::string GetTechnologyFamily(Error *error);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500178
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700179 private:
Darin Petkovab565bb2011-10-06 02:55:51 -0700180 friend class CellularTest;
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500181 friend class CellularCapabilityTest;
Darin Petkov20c13ec2011-11-09 15:07:15 +0100182 friend class CellularCapabilityCDMATest;
183 friend class CellularCapabilityGSMTest;
Darin Petkov721ac932011-11-16 15:43:09 +0100184 friend class ModemTest;
Darin Petkovac635a82012-01-10 16:51:58 +0100185 FRIEND_TEST(CellularCapabilityCDMATest, CreateFriendlyServiceName);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500186 FRIEND_TEST(CellularCapabilityCDMATest, GetRegistrationState);
Darin Petkovac635a82012-01-10 16:51:58 +0100187 FRIEND_TEST(CellularCapabilityGSMTest, CreateFriendlyServiceName);
188 FRIEND_TEST(CellularServiceTest, FriendlyName);
Darin Petkov3335b372011-08-22 11:05:32 -0700189 FRIEND_TEST(CellularTest, CreateService);
Darin Petkovc5f56562011-08-06 16:40:05 -0700190 FRIEND_TEST(CellularTest, Connect);
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400191 FRIEND_TEST(CellularTest, ConnectFailure);
Darin Petkovfb0625e2012-01-16 13:05:56 +0100192 FRIEND_TEST(CellularTest, DisableModem);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500193 FRIEND_TEST(CellularTest, Disconnect);
Darin Petkovbac96002011-08-09 13:22:00 -0700194 FRIEND_TEST(CellularTest, StartConnected);
Darin Petkova1e0a1c2011-08-25 15:08:33 -0700195 FRIEND_TEST(CellularTest, StartCDMARegister);
Darin Petkov9bac6fe2011-08-26 12:49:05 -0700196 FRIEND_TEST(CellularTest, StartGSMRegister);
Darin Petkov0828f5f2011-08-11 10:18:52 -0700197 FRIEND_TEST(CellularTest, StartLinked);
Darin Petkov9c1dcef2012-02-07 15:58:26 +0100198 FRIEND_TEST(CellularCapabilityTest, AllowRoaming);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500199 FRIEND_TEST(CellularCapabilityTest, EnableModemFail);
200 FRIEND_TEST(CellularCapabilityTest, EnableModemSucceed);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500201 FRIEND_TEST(CellularCapabilityTest, FinishEnable);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500202 FRIEND_TEST(CellularCapabilityTest, GetModemInfo);
203 FRIEND_TEST(CellularCapabilityTest, GetModemStatus);
Darin Petkove9d12e02011-07-27 15:09:37 -0700204
Darin Petkov0828f5f2011-08-11 10:18:52 -0700205 void SetState(State state);
206
Darin Petkovbac96002011-08-09 13:22:00 -0700207 // Invoked when the modem is connected to the cellular network to transition
208 // to the network-connected state and bring the network interface up.
209 void EstablishLink();
210
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500211 void InitCapability(Type type, ProxyFactory *proxy_factory);
Darin Petkovf5f61e02011-07-29 11:35:40 -0700212
Darin Petkovd9661952011-08-03 16:25:42 -0700213 void CreateService();
Darin Petkov2c377382012-01-11 11:40:43 +0100214 void DestroyService();
Darin Petkovd9661952011-08-03 16:25:42 -0700215
Eric Shienbrood0db6a9b2012-03-30 16:11:39 -0400216 // HelpRegisterDerived*: Expose a property over RPC, with the name |name|.
217 //
218 // Reads of the property will be handled by invoking |get|.
219 // Writes to the property will be handled by invoking |set|.
220 // Clearing the property will be handled by PropertyStore.
221 void HelpRegisterDerivedString(
222 const std::string &name,
223 std::string(Cellular::*get)(Error *error),
224 void(Cellular::*set)(const std::string &value, Error *error));
225
Darin Petkove9d12e02011-07-27 15:09:37 -0700226 State state_;
Darin Petkovbac96002011-08-09 13:22:00 -0700227 ModemState modem_state_;
Darin Petkove9d12e02011-07-27 15:09:37 -0700228
Darin Petkovdaf43862011-10-27 11:37:28 +0200229 scoped_ptr<CellularCapability> capability_;
230
Darin Petkove9d12e02011-07-27 15:09:37 -0700231 const std::string dbus_owner_; // ModemManager.Modem
232 const std::string dbus_path_; // ModemManager.Modem
Darin Petkovbec79a22011-08-01 14:47:17 -0700233
Darin Petkov137884a2011-10-26 18:52:47 +0200234 mobile_provider_db *provider_db_;
235
Darin Petkovd9661952011-08-03 16:25:42 -0700236 CellularServiceRefPtr service_;
Chris Masone853b81b2011-06-24 14:11:41 -0700237
Chris Masoneb925cc82011-06-22 15:39:57 -0700238 // Properties
Darin Petkov3335b372011-08-22 11:05:32 -0700239 Operator home_provider_;
Chris Masoneb925cc82011-06-22 15:39:57 -0700240
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700241 DISALLOW_COPY_AND_ASSIGN(Cellular);
242};
243
244} // namespace shill
245
246#endif // SHILL_CELLULAR_