blob: 5dadff0b73c9a8807e9fa39fcd4640b2ffe3eacd [file] [log] [blame]
Chris Masone3bd3c8c2011-06-13 08:20:26 -07001// Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
2// 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"
Darin Petkovd9661952011-08-03 16:25:42 -070016#include "shill/modem_cdma_proxy_interface.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#include "shill/shill_event.h"
20
21namespace shill {
22
Darin Petkove604f702011-07-28 15:51:17 -070023class ModemSimpleProxyInterface;
Darin Petkove9d12e02011-07-27 15:09:37 -070024
Darin Petkovd9661952011-08-03 16:25:42 -070025class Cellular : public Device,
Darin Petkovc5f56562011-08-06 16:40:05 -070026 public ModemCDMAProxyListener,
27 public ModemProxyListener {
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
43 // measurements.
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
Chris Masone889666b2011-07-03 12:58:50 -070065 class Network {
66 public:
67 Network();
Darin Petkove9d12e02011-07-27 15:09:37 -070068 ~Network();
Chris Masone889666b2011-07-03 12:58:50 -070069
70 const std::string &GetStatus() const;
71 void SetStatus(const std::string &status);
72
73 const std::string &GetId() const;
74 void SetId(const std::string &id);
75
76 const std::string &GetShortName() const;
77 void SetShortName(const std::string &name);
78
79 const std::string &GetLongName() const;
80 void SetLongName(const std::string &name);
81
82 const std::string &GetTechnology() const;
83 void SetTechnology(const std::string &technology);
84
85 const Stringmap &ToDict() const;
86
87 private:
88 Stringmap dict_;
Darin Petkove9d12e02011-07-27 15:09:37 -070089
Chris Masone889666b2011-07-03 12:58:50 -070090 DISALLOW_COPY_AND_ASSIGN(Network);
91 };
92
Darin Petkovbec79a22011-08-01 14:47:17 -070093 struct CDMA {
94 CDMA();
95
96 uint32 registration_state_evdo;
97 uint32 registration_state_1x;
98 uint32 activation_state;
99 };
100
Chris Masone889666b2011-07-03 12:58:50 -0700101 struct SimLockStatus {
102 public:
Darin Petkove9d12e02011-07-27 15:09:37 -0700103 SimLockStatus() : retries_left(0) {}
104
Chris Masone889666b2011-07-03 12:58:50 -0700105 std::string lock_type;
106 uint32 retries_left;
107 };
108
Darin Petkovc5f56562011-08-06 16:40:05 -0700109 static const char kConnectPropertyPhoneNumber[];
110
Darin Petkove9d12e02011-07-27 15:09:37 -0700111 // |owner| is the ModemManager DBus service owner (e.g., ":1.17"). |path| is
112 // the ModemManager.Modem DBus object path (e.g.,
113 // "/org/chromium/ModemManager/Gobi/0").
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700114 Cellular(ControlInterface *control_interface,
115 EventDispatcher *dispatcher,
116 Manager *manager,
Darin Petkove9d12e02011-07-27 15:09:37 -0700117 const std::string &link_name,
118 int interface_index,
119 Type type,
120 const std::string &owner,
121 const std::string &path);
122 virtual ~Cellular();
123
Darin Petkovc5f56562011-08-06 16:40:05 -0700124 // Asynchronously connects the modem to the network.
125 void Connect();
126
Darin Petkovbac96002011-08-09 13:22:00 -0700127 void set_modem_state(ModemState state) { modem_state_ = state; }
128 ModemState modem_state() const { return modem_state_; }
129
Darin Petkove9d12e02011-07-27 15:09:37 -0700130 // Inherited from Device.
131 virtual void Start();
132 virtual void Stop();
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700133 virtual bool TechnologyIs(Technology type) const;
Darin Petkov0828f5f2011-08-11 10:18:52 -0700134 virtual void LinkEvent(unsigned int flags, unsigned int change);
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700135
136 private:
Darin Petkovc5f56562011-08-06 16:40:05 -0700137 FRIEND_TEST(CellularTest, Connect);
Darin Petkovbec79a22011-08-01 14:47:17 -0700138 FRIEND_TEST(CellularTest, GetCDMARegistrationState);
Darin Petkovd9661952011-08-03 16:25:42 -0700139 FRIEND_TEST(CellularTest, GetCDMASignalQuality);
Darin Petkovceb68172011-07-29 14:47:48 -0700140 FRIEND_TEST(CellularTest, GetModemInfo);
141 FRIEND_TEST(CellularTest, GetModemStatus);
Darin Petkove9d12e02011-07-27 15:09:37 -0700142 FRIEND_TEST(CellularTest, GetStateString);
143 FRIEND_TEST(CellularTest, GetTypeString);
Darin Petkovbec79a22011-08-01 14:47:17 -0700144 FRIEND_TEST(CellularTest, InitProxiesCDMA);
145 FRIEND_TEST(CellularTest, InitProxiesGSM);
Darin Petkove9d12e02011-07-27 15:09:37 -0700146 FRIEND_TEST(CellularTest, Start);
Darin Petkovbac96002011-08-09 13:22:00 -0700147 FRIEND_TEST(CellularTest, StartConnected);
Darin Petkov0828f5f2011-08-11 10:18:52 -0700148 FRIEND_TEST(CellularTest, StartLinked);
Darin Petkovbac96002011-08-09 13:22:00 -0700149 FRIEND_TEST(CellularTest, StartRegister);
150
151 static const char kPhoneNumberCDMA[];
152 static const char kPhoneNumberGSM[];
Darin Petkove9d12e02011-07-27 15:09:37 -0700153
Darin Petkov0828f5f2011-08-11 10:18:52 -0700154 void SetState(State state);
155
Darin Petkovc5f56562011-08-06 16:40:05 -0700156 void ConnectTask(const DBusPropertiesMap &properties);
157
Darin Petkovbac96002011-08-09 13:22:00 -0700158 // Invoked when the modem is connected to the cellular network to transition
159 // to the network-connected state and bring the network interface up.
160 void EstablishLink();
161
Chris Masone889666b2011-07-03 12:58:50 -0700162 Stringmaps EnumerateNetworks();
163 StrIntPair SimLockStatusToProperty();
164 void HelpRegisterDerivedStringmaps(const std::string &name,
165 Stringmaps(Cellular::*get)(void),
166 bool(Cellular::*set)(const Stringmaps&));
167 void HelpRegisterDerivedStrIntPair(const std::string &name,
168 StrIntPair(Cellular::*get)(void),
169 bool(Cellular::*set)(const StrIntPair&));
170
Darin Petkovbec79a22011-08-01 14:47:17 -0700171 void InitProxies();
172
Darin Petkove9d12e02011-07-27 15:09:37 -0700173 std::string GetTypeString();
174 std::string GetStateString();
175
Darin Petkovf5f61e02011-07-29 11:35:40 -0700176 void EnableModem();
177 void GetModemStatus();
Darin Petkovceb68172011-07-29 14:47:48 -0700178 void GetGSMProperties();
179 void RegisterGSMModem();
Darin Petkovceb68172011-07-29 14:47:48 -0700180
181 // Obtains the modem identifiers: MEID for CDMA; IMEI, IMSI, SPN and MSISDN
182 // for GSM.
183 void GetModemIdentifiers();
184
Darin Petkovd9661952011-08-03 16:25:42 -0700185 // Obtains modem's manufacturer, model ID, and hardware revision.
Darin Petkovceb68172011-07-29 14:47:48 -0700186 void GetModemInfo();
Darin Petkovf5f61e02011-07-29 11:35:40 -0700187
Darin Petkovbec79a22011-08-01 14:47:17 -0700188 void GetModemRegistrationState();
189 void GetCDMARegistrationState();
190 void GetGSMRegistrationState();
191
Darin Petkovd9661952011-08-03 16:25:42 -0700192 // Processes a change in the modem registration state, possibly creating,
193 // destroying or updating the CellularService.
194 void HandleNewRegistrationState();
Darin Petkov0828f5f2011-08-11 10:18:52 -0700195 void HandleNewRegistrationStateTask();
Darin Petkovd9661952011-08-03 16:25:42 -0700196
197 void CreateService();
198
199 void GetModemSignalQuality();
200 uint32 GetCDMASignalQuality();
201 uint32 GetGSMSignalQuality();
202
203 void HandleNewSignalQuality(uint32 strength);
204
205 // Returns true if the modem is registered. Note that this method looks at the
206 // latest CDMA/GSM registration info obtained from the modem rather than the
207 // device |state_|.
208 bool IsModemRegistered();
209
Darin Petkovc5f56562011-08-06 16:40:05 -0700210 // Signal callbacks inherited from ModemCDMAProxyListener.
Darin Petkovd9661952011-08-03 16:25:42 -0700211 virtual void OnCDMARegistrationStateChanged(uint32 state_1x,
212 uint32 state_evdo);
213 virtual void OnCDMASignalQualityChanged(uint32 strength);
214
Darin Petkovc5f56562011-08-06 16:40:05 -0700215 // Signal callbacks inherited from ModemProxyListener.
216 virtual void OnModemStateChanged(uint32 old_state,
217 uint32 new_state,
218 uint32 reason);
219
Darin Petkove9d12e02011-07-27 15:09:37 -0700220 Type type_;
221 State state_;
Darin Petkovbac96002011-08-09 13:22:00 -0700222 ModemState modem_state_;
Darin Petkove9d12e02011-07-27 15:09:37 -0700223
224 const std::string dbus_owner_; // ModemManager.Modem
225 const std::string dbus_path_; // ModemManager.Modem
226 scoped_ptr<ModemProxyInterface> proxy_;
Darin Petkove604f702011-07-28 15:51:17 -0700227 scoped_ptr<ModemSimpleProxyInterface> simple_proxy_;
Darin Petkovbec79a22011-08-01 14:47:17 -0700228 scoped_ptr<ModemCDMAProxyInterface> cdma_proxy_;
229
230 CDMA cdma_;
Darin Petkove9d12e02011-07-27 15:09:37 -0700231
Darin Petkovd9661952011-08-03 16:25:42 -0700232 CellularServiceRefPtr service_;
Chris Masone853b81b2011-06-24 14:11:41 -0700233
Darin Petkovc5f56562011-08-06 16:40:05 -0700234 ScopedRunnableMethodFactory<Cellular> task_factory_;
235
Chris Masoneb925cc82011-06-22 15:39:57 -0700236 // Properties
237 bool allow_roaming_;
238 std::string carrier_;
239 std::string meid_;
240 std::string imei_;
241 std::string imsi_;
242 std::string esn_;
243 std::string mdn_;
244 std::string min_;
245 std::string model_id_;
246 std::string manufacturer_;
247 std::string firmware_revision_;
248 std::string hardware_revision_;
Darin Petkovceb68172011-07-29 14:47:48 -0700249 uint16 prl_version_;
Chris Masoneb925cc82011-06-22 15:39:57 -0700250 bool scanning_;
251 uint16 scan_interval_;
Chris Masone889666b2011-07-03 12:58:50 -0700252 std::vector<Network> found_networks_;
253 SimLockStatus sim_lock_status_;
Chris Masoneb925cc82011-06-22 15:39:57 -0700254
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700255 DISALLOW_COPY_AND_ASSIGN(Cellular);
256};
257
258} // namespace shill
259
260#endif // SHILL_CELLULAR_