blob: cbe21979f93d7929b9d76eaf6c53d53687a85193 [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,
31 kTypeCDMA
32 };
33
Darin Petkov0828f5f2011-08-11 10:18:52 -070034 // The device states progress linearly from Disabled to Linked.
Darin Petkove9d12e02011-07-27 15:09:37 -070035 enum State {
Darin Petkov0828f5f2011-08-11 10:18:52 -070036 // This is the initial state of the modem and indicates that the modem radio
37 // is not turned on.
Darin Petkove9d12e02011-07-27 15:09:37 -070038 kStateDisabled,
Darin Petkov0828f5f2011-08-11 10:18:52 -070039 // This state indicates that the modem radio is turned on, and it should be
40 // possible to measure signal strength.
Darin Petkove9d12e02011-07-27 15:09:37 -070041 kStateEnabled,
Darin Petkov0828f5f2011-08-11 10:18:52 -070042 // The modem has registered with a network and has signal quality
Darin Petkov51489002011-08-18 13:13:20 -070043 // measurements. A cellular service object is created.
Darin Petkove9d12e02011-07-27 15:09:37 -070044 kStateRegistered,
Darin Petkov0828f5f2011-08-11 10:18:52 -070045 // The modem has connected to a network.
Darin Petkove9d12e02011-07-27 15:09:37 -070046 kStateConnected,
Darin Petkov0828f5f2011-08-11 10:18:52 -070047 // The network interface is UP.
48 kStateLinked,
Darin Petkove9d12e02011-07-27 15:09:37 -070049 };
50
Darin Petkovbac96002011-08-09 13:22:00 -070051 // These should be kept in sync with ModemManager's mm-modem.h:MMModemState.
52 enum ModemState {
53 kModemStateUnknown = 0,
54 kModemStateDisabled = 10,
55 kModemStateDisabling = 20,
56 kModemStateEnabling = 30,
57 kModemStateEnabled = 40,
58 kModemStateSearching = 50,
59 kModemStateRegistered = 60,
60 kModemStateDisconnecting = 70,
61 kModemStateConnecting = 80,
62 kModemStateConnected = 90,
63 };
64
Darin Petkov3335b372011-08-22 11:05:32 -070065 class Operator {
66 public:
67 Operator();
68 ~Operator();
69
70 void CopyFrom(const Operator &oper);
Darin Petkov9cb02682012-01-28 00:17:38 +010071 bool Equals(const Operator &oper) const { return dict_ == oper.dict_; }
Darin Petkov3335b372011-08-22 11:05:32 -070072
73 const std::string &GetName() const;
74 void SetName(const std::string &name);
75
76 const std::string &GetCode() const;
77 void SetCode(const std::string &code);
78
79 const std::string &GetCountry() const;
80 void SetCountry(const std::string &country);
81
82 const Stringmap &ToDict() const;
83
84 private:
85 Stringmap dict_;
86
87 DISALLOW_COPY_AND_ASSIGN(Operator);
88 };
89
Darin Petkove9d12e02011-07-27 15:09:37 -070090 // |owner| is the ModemManager DBus service owner (e.g., ":1.17"). |path| is
91 // the ModemManager.Modem DBus object path (e.g.,
92 // "/org/chromium/ModemManager/Gobi/0").
Chris Masone3bd3c8c2011-06-13 08:20:26 -070093 Cellular(ControlInterface *control_interface,
94 EventDispatcher *dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -080095 Metrics *metrics,
Chris Masone3bd3c8c2011-06-13 08:20:26 -070096 Manager *manager,
Darin Petkove9d12e02011-07-27 15:09:37 -070097 const std::string &link_name,
Chris Masone626719f2011-08-18 16:58:48 -070098 const std::string &address,
Darin Petkove9d12e02011-07-27 15:09:37 -070099 int interface_index,
100 Type type,
101 const std::string &owner,
Darin Petkov137884a2011-10-26 18:52:47 +0200102 const std::string &path,
103 mobile_provider_db *provider_db);
Darin Petkove9d12e02011-07-27 15:09:37 -0700104 virtual ~Cellular();
105
Darin Petkov4d6d9412011-08-24 13:19:54 -0700106 // Asynchronously connects the modem to the network. Populates |error| on
107 // failure, leaves it unchanged otherwise.
108 void Connect(Error *error);
Darin Petkovc5f56562011-08-06 16:40:05 -0700109
Darin Petkovfb0625e2012-01-16 13:05:56 +0100110 // Asynchronously disconnects the modem from the network. Populates |error| on
111 // failure, leaves it unchanged otherwise.
112 void Disconnect(Error *error);
113
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500114 // Asynchronously activates the modem. Returns an error on failure.
Eric Shienbrood9a245532012-03-07 14:20:39 -0500115 void Activate(const std::string &carrier, Error *error,
116 const ResultCallback &callback);
Darin Petkovc408e692011-08-17 13:47:15 -0700117
Darin Petkov3e509242011-11-10 14:46:44 +0100118 const CellularServiceRefPtr &service() const { return service_; }
119
Darin Petkova3d3be52011-11-14 21:34:16 +0100120 static std::string GetStateString(State state);
121
Darin Petkovac635a82012-01-10 16:51:58 +0100122 std::string CreateFriendlyServiceName();
123
Darin Petkova3d3be52011-11-14 21:34:16 +0100124 State state() const { return state_; }
125
Darin Petkovbac96002011-08-09 13:22:00 -0700126 void set_modem_state(ModemState state) { modem_state_ = state; }
127 ModemState modem_state() const { return modem_state_; }
128
Darin Petkov1272a432011-11-10 15:53:37 +0100129 mobile_provider_db *provider_db() const { return provider_db_; }
130
Darin Petkovdaf43862011-10-27 11:37:28 +0200131 const std::string &dbus_owner() const { return dbus_owner_; }
132 const std::string &dbus_path() const { return dbus_path_; }
133
Darin Petkovae0c64e2011-11-15 15:50:27 +0100134 const Operator &home_provider() const { return home_provider_; }
135 void set_home_provider(const Operator &oper);
Darin Petkov184c54e2011-11-15 12:44:39 +0100136
Darin Petkov3e509242011-11-10 14:46:44 +0100137 void HandleNewSignalQuality(uint32 strength);
138
Darin Petkov184c54e2011-11-15 12:44:39 +0100139 // Processes a change in the modem registration state, possibly creating,
140 // destroying or updating the CellularService.
141 void HandleNewRegistrationState();
142
Darin Petkovae0c64e2011-11-15 15:50:27 +0100143 void OnModemManagerPropertiesChanged(const DBusPropertiesMap &properties);
Darin Petkov184c54e2011-11-15 12:44:39 +0100144
Darin Petkove9d12e02011-07-27 15:09:37 -0700145 // Inherited from Device.
Eric Shienbrood9a245532012-03-07 14:20:39 -0500146 virtual void Start(Error *error, const EnabledStateChangedCallback &callback);
147 virtual void Stop(Error *error, const EnabledStateChangedCallback &callback);
Paul Stewartfdd16072011-09-16 12:41:35 -0700148 virtual bool TechnologyIs(Technology::Identifier type) const;
Darin Petkov0828f5f2011-08-11 10:18:52 -0700149 virtual void LinkEvent(unsigned int flags, unsigned int change);
Darin Petkovc0865312011-09-16 15:31:20 -0700150 virtual void Scan(Error *error);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500151 virtual void RegisterOnNetwork(const std::string &network_id,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500152 Error *error,
153 const ResultCallback &callback);
154 virtual void RequirePIN(const std::string &pin, bool require,
155 Error *error, const ResultCallback &callback);
156 virtual void EnterPIN(const std::string &pin,
157 Error *error, const ResultCallback &callback);
Darin Petkove42e1012011-08-31 12:35:04 -0700158 virtual void UnblockPIN(const std::string &unblock_code,
159 const std::string &pin,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500160 Error *error, const ResultCallback &callback);
Darin Petkove42e1012011-08-31 12:35:04 -0700161 virtual void ChangePIN(const std::string &old_pin,
162 const std::string &new_pin,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500163 Error *error, const ResultCallback &callback);
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700164
Eric Shienbrood9a245532012-03-07 14:20:39 -0500165 void OnModemStarted(const EnabledStateChangedCallback &callback,
166 const Error &error);
167 void OnModemStopped(const EnabledStateChangedCallback &callback,
168 const Error &error);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500169 void OnConnected();
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400170 void OnConnectFailed(const Error &error);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500171 void OnDisconnected();
172 void OnDisconnectFailed();
173
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700174 private:
Darin Petkovab565bb2011-10-06 02:55:51 -0700175 friend class CellularTest;
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500176 friend class CellularCapabilityTest;
Darin Petkov20c13ec2011-11-09 15:07:15 +0100177 friend class CellularCapabilityCDMATest;
178 friend class CellularCapabilityGSMTest;
Darin Petkov721ac932011-11-16 15:43:09 +0100179 friend class ModemTest;
Darin Petkovac635a82012-01-10 16:51:58 +0100180 FRIEND_TEST(CellularCapabilityCDMATest, CreateFriendlyServiceName);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500181 FRIEND_TEST(CellularCapabilityCDMATest, GetRegistrationState);
Darin Petkovac635a82012-01-10 16:51:58 +0100182 FRIEND_TEST(CellularCapabilityGSMTest, CreateFriendlyServiceName);
183 FRIEND_TEST(CellularServiceTest, FriendlyName);
Darin Petkov3335b372011-08-22 11:05:32 -0700184 FRIEND_TEST(CellularTest, CreateService);
Darin Petkovc5f56562011-08-06 16:40:05 -0700185 FRIEND_TEST(CellularTest, Connect);
Darin Petkovfb0625e2012-01-16 13:05:56 +0100186 FRIEND_TEST(CellularTest, DisableModem);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500187 FRIEND_TEST(CellularTest, Disconnect);
Darin Petkovbac96002011-08-09 13:22:00 -0700188 FRIEND_TEST(CellularTest, StartConnected);
Darin Petkova1e0a1c2011-08-25 15:08:33 -0700189 FRIEND_TEST(CellularTest, StartCDMARegister);
Darin Petkov9bac6fe2011-08-26 12:49:05 -0700190 FRIEND_TEST(CellularTest, StartGSMRegister);
Darin Petkov0828f5f2011-08-11 10:18:52 -0700191 FRIEND_TEST(CellularTest, StartLinked);
Darin Petkov9c1dcef2012-02-07 15:58:26 +0100192 FRIEND_TEST(CellularCapabilityTest, AllowRoaming);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500193 FRIEND_TEST(CellularCapabilityTest, EnableModemFail);
194 FRIEND_TEST(CellularCapabilityTest, EnableModemSucceed);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500195 FRIEND_TEST(CellularCapabilityTest, FinishEnable);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500196 FRIEND_TEST(CellularCapabilityTest, GetModemInfo);
197 FRIEND_TEST(CellularCapabilityTest, GetModemStatus);
Darin Petkove9d12e02011-07-27 15:09:37 -0700198
Darin Petkov0828f5f2011-08-11 10:18:52 -0700199 void SetState(State state);
200
Darin Petkovbac96002011-08-09 13:22:00 -0700201 // Invoked when the modem is connected to the cellular network to transition
202 // to the network-connected state and bring the network interface up.
203 void EstablishLink();
204
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500205 void InitCapability(Type type, ProxyFactory *proxy_factory);
Darin Petkovf5f61e02011-07-29 11:35:40 -0700206
Darin Petkovd9661952011-08-03 16:25:42 -0700207 void CreateService();
Darin Petkov2c377382012-01-11 11:40:43 +0100208 void DestroyService();
Darin Petkovd9661952011-08-03 16:25:42 -0700209
Darin Petkove9d12e02011-07-27 15:09:37 -0700210 State state_;
Darin Petkovbac96002011-08-09 13:22:00 -0700211 ModemState modem_state_;
Darin Petkove9d12e02011-07-27 15:09:37 -0700212
Darin Petkovdaf43862011-10-27 11:37:28 +0200213 scoped_ptr<CellularCapability> capability_;
214
Darin Petkove9d12e02011-07-27 15:09:37 -0700215 const std::string dbus_owner_; // ModemManager.Modem
216 const std::string dbus_path_; // ModemManager.Modem
Darin Petkovbec79a22011-08-01 14:47:17 -0700217
Darin Petkov137884a2011-10-26 18:52:47 +0200218 mobile_provider_db *provider_db_;
219
Darin Petkovd9661952011-08-03 16:25:42 -0700220 CellularServiceRefPtr service_;
Chris Masone853b81b2011-06-24 14:11:41 -0700221
Chris Masoneb925cc82011-06-22 15:39:57 -0700222 // Properties
Darin Petkov3335b372011-08-22 11:05:32 -0700223 Operator home_provider_;
Chris Masoneb925cc82011-06-22 15:39:57 -0700224
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700225 DISALLOW_COPY_AND_ASSIGN(Cellular);
226};
227
228} // namespace shill
229
230#endif // SHILL_CELLULAR_