blob: 578f32c065a2525755135223ae227b18e5550f85 [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 Petkovc5f56562011-08-06 16:40:05 -070011#include <base/task.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,
32 kTypeCDMA
33 };
34
Darin Petkov0828f5f2011-08-11 10:18:52 -070035 // The device states progress linearly from Disabled to Linked.
Darin Petkove9d12e02011-07-27 15:09:37 -070036 enum State {
Darin Petkov0828f5f2011-08-11 10:18:52 -070037 // This is the initial state of the modem and indicates that the modem radio
38 // is not turned on.
Darin Petkove9d12e02011-07-27 15:09:37 -070039 kStateDisabled,
Darin Petkov0828f5f2011-08-11 10:18:52 -070040 // This state indicates that the modem radio is turned on, and it should be
41 // possible to measure signal strength.
Darin Petkove9d12e02011-07-27 15:09:37 -070042 kStateEnabled,
Darin Petkov0828f5f2011-08-11 10:18:52 -070043 // The modem has registered with a network and has signal quality
Darin Petkov51489002011-08-18 13:13:20 -070044 // measurements. A cellular service object is created.
Darin Petkove9d12e02011-07-27 15:09:37 -070045 kStateRegistered,
Darin Petkov0828f5f2011-08-11 10:18:52 -070046 // The modem has connected to a network.
Darin Petkove9d12e02011-07-27 15:09:37 -070047 kStateConnected,
Darin Petkov0828f5f2011-08-11 10:18:52 -070048 // The network interface is UP.
49 kStateLinked,
Darin Petkove9d12e02011-07-27 15:09:37 -070050 };
51
Darin Petkovbac96002011-08-09 13:22:00 -070052 // These should be kept in sync with ModemManager's mm-modem.h:MMModemState.
53 enum ModemState {
54 kModemStateUnknown = 0,
55 kModemStateDisabled = 10,
56 kModemStateDisabling = 20,
57 kModemStateEnabling = 30,
58 kModemStateEnabled = 40,
59 kModemStateSearching = 50,
60 kModemStateRegistered = 60,
61 kModemStateDisconnecting = 70,
62 kModemStateConnecting = 80,
63 kModemStateConnected = 90,
64 };
65
Darin Petkov3335b372011-08-22 11:05:32 -070066 class Operator {
67 public:
68 Operator();
69 ~Operator();
70
71 void CopyFrom(const Operator &oper);
Darin Petkov9cb02682012-01-28 00:17:38 +010072 bool Equals(const Operator &oper) const { return dict_ == oper.dict_; }
Darin Petkov3335b372011-08-22 11:05:32 -070073
74 const std::string &GetName() const;
75 void SetName(const std::string &name);
76
77 const std::string &GetCode() const;
78 void SetCode(const std::string &code);
79
80 const std::string &GetCountry() const;
81 void SetCountry(const std::string &country);
82
83 const Stringmap &ToDict() const;
84
85 private:
86 Stringmap dict_;
87
88 DISALLOW_COPY_AND_ASSIGN(Operator);
89 };
90
Darin Petkove9d12e02011-07-27 15:09:37 -070091 // |owner| is the ModemManager DBus service owner (e.g., ":1.17"). |path| is
92 // the ModemManager.Modem DBus object path (e.g.,
93 // "/org/chromium/ModemManager/Gobi/0").
Chris Masone3bd3c8c2011-06-13 08:20:26 -070094 Cellular(ControlInterface *control_interface,
95 EventDispatcher *dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -080096 Metrics *metrics,
Chris Masone3bd3c8c2011-06-13 08:20:26 -070097 Manager *manager,
Darin Petkove9d12e02011-07-27 15:09:37 -070098 const std::string &link_name,
Chris Masone626719f2011-08-18 16:58:48 -070099 const std::string &address,
Darin Petkove9d12e02011-07-27 15:09:37 -0700100 int interface_index,
101 Type type,
102 const std::string &owner,
Darin Petkov137884a2011-10-26 18:52:47 +0200103 const std::string &path,
104 mobile_provider_db *provider_db);
Darin Petkove9d12e02011-07-27 15:09:37 -0700105 virtual ~Cellular();
106
Darin Petkov4d6d9412011-08-24 13:19:54 -0700107 // Asynchronously connects the modem to the network. Populates |error| on
108 // failure, leaves it unchanged otherwise.
109 void Connect(Error *error);
Darin Petkovc5f56562011-08-06 16:40:05 -0700110
Darin Petkovfb0625e2012-01-16 13:05:56 +0100111 // Asynchronously disconnects the modem from the network. Populates |error| on
112 // failure, leaves it unchanged otherwise.
113 void Disconnect(Error *error);
114
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500115 // Asynchronously activates the modem. Returns an error on failure.
116 void Activate(const std::string &carrier, ReturnerInterface *returner);
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.
146 virtual void Start();
147 virtual void Stop();
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,
152 ReturnerInterface *returner);
Darin Petkovc64fe5e2012-01-11 12:46:13 +0100153 virtual void RequirePIN(
154 const std::string &pin, bool require, ReturnerInterface *returner);
Darin Petkove5bc2cb2011-12-07 14:47:32 +0100155 virtual void EnterPIN(const std::string &pin, ReturnerInterface *returner);
Darin Petkove42e1012011-08-31 12:35:04 -0700156 virtual void UnblockPIN(const std::string &unblock_code,
157 const std::string &pin,
Darin Petkovc64fe5e2012-01-11 12:46:13 +0100158 ReturnerInterface *returner);
Darin Petkove42e1012011-08-31 12:35:04 -0700159 virtual void ChangePIN(const std::string &old_pin,
160 const std::string &new_pin,
Darin Petkovc64fe5e2012-01-11 12:46:13 +0100161 ReturnerInterface *returner);
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700162
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500163 void OnModemEnabled();
164 void OnModemDisabled();
165 void OnConnected();
166 void OnConnectFailed();
167 void OnDisconnected();
168 void OnDisconnectFailed();
169
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700170 private:
Darin Petkovab565bb2011-10-06 02:55:51 -0700171 friend class CellularTest;
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500172 friend class CellularCapabilityTest;
Darin Petkov20c13ec2011-11-09 15:07:15 +0100173 friend class CellularCapabilityCDMATest;
174 friend class CellularCapabilityGSMTest;
Darin Petkov721ac932011-11-16 15:43:09 +0100175 friend class ModemTest;
Darin Petkovac635a82012-01-10 16:51:58 +0100176 FRIEND_TEST(CellularCapabilityCDMATest, CreateFriendlyServiceName);
177 FRIEND_TEST(CellularCapabilityGSMTest, CreateFriendlyServiceName);
178 FRIEND_TEST(CellularServiceTest, FriendlyName);
Darin Petkov3335b372011-08-22 11:05:32 -0700179 FRIEND_TEST(CellularTest, CreateService);
Darin Petkovc5f56562011-08-06 16:40:05 -0700180 FRIEND_TEST(CellularTest, Connect);
Darin Petkovfb0625e2012-01-16 13:05:56 +0100181 FRIEND_TEST(CellularTest, DisableModem);
182 FRIEND_TEST(CellularTest, DisconnectModem);
Darin Petkovbac96002011-08-09 13:22:00 -0700183 FRIEND_TEST(CellularTest, StartConnected);
Darin Petkova1e0a1c2011-08-25 15:08:33 -0700184 FRIEND_TEST(CellularTest, StartCDMARegister);
Darin Petkov9bac6fe2011-08-26 12:49:05 -0700185 FRIEND_TEST(CellularTest, StartGSMRegister);
Darin Petkov0828f5f2011-08-11 10:18:52 -0700186 FRIEND_TEST(CellularTest, StartLinked);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500187 FRIEND_TEST(CellularCapabilityTest, EnableModemFail);
188 FRIEND_TEST(CellularCapabilityTest, EnableModemSucceed);
189 FRIEND_TEST(CellularCapabilityTest, GetModemInfo);
190 FRIEND_TEST(CellularCapabilityTest, GetModemStatus);
Darin Petkove9d12e02011-07-27 15:09:37 -0700191
Darin Petkov0828f5f2011-08-11 10:18:52 -0700192 void SetState(State state);
193
Darin Petkovc5f56562011-08-06 16:40:05 -0700194 void ConnectTask(const DBusPropertiesMap &properties);
Darin Petkovfb0625e2012-01-16 13:05:56 +0100195 void DisconnectTask();
196 void DisconnectModem();
Darin Petkovc5f56562011-08-06 16:40:05 -0700197
Darin Petkovbac96002011-08-09 13:22:00 -0700198 // Invoked when the modem is connected to the cellular network to transition
199 // to the network-connected state and bring the network interface up.
200 void EstablishLink();
201
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500202 void InitCapability(Type type, ProxyFactory *proxy_factory);
Darin Petkovf5f61e02011-07-29 11:35:40 -0700203
Darin Petkov0828f5f2011-08-11 10:18:52 -0700204 void HandleNewRegistrationStateTask();
Darin Petkovd9661952011-08-03 16:25:42 -0700205
206 void CreateService();
Darin Petkov2c377382012-01-11 11:40:43 +0100207 void DestroyService();
Darin Petkovd9661952011-08-03 16:25:42 -0700208
Darin Petkove9d12e02011-07-27 15:09:37 -0700209 State state_;
Darin Petkovbac96002011-08-09 13:22:00 -0700210 ModemState modem_state_;
Darin Petkove9d12e02011-07-27 15:09:37 -0700211
Darin Petkovdaf43862011-10-27 11:37:28 +0200212 scoped_ptr<CellularCapability> capability_;
213
Darin Petkove9d12e02011-07-27 15:09:37 -0700214 const std::string dbus_owner_; // ModemManager.Modem
215 const std::string dbus_path_; // ModemManager.Modem
Darin Petkovbec79a22011-08-01 14:47:17 -0700216
Darin Petkov137884a2011-10-26 18:52:47 +0200217 mobile_provider_db *provider_db_;
218
Darin Petkovd9661952011-08-03 16:25:42 -0700219 CellularServiceRefPtr service_;
Chris Masone853b81b2011-06-24 14:11:41 -0700220
Darin Petkovc5f56562011-08-06 16:40:05 -0700221 ScopedRunnableMethodFactory<Cellular> task_factory_;
222
Chris Masoneb925cc82011-06-22 15:39:57 -0700223 // Properties
Darin Petkov3335b372011-08-22 11:05:32 -0700224 Operator home_provider_;
Chris Masoneb925cc82011-06-22 15:39:57 -0700225
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700226 DISALLOW_COPY_AND_ASSIGN(Cellular);
227};
228
229} // namespace shill
230
231#endif // SHILL_CELLULAR_