blob: 1419b0ecb3e01bdf4ee03b067085f117d79fcdce [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
34 enum State {
35 kStateDisabled,
36 kStateEnabled,
37 kStateRegistered,
38 kStateConnected,
39 };
40
Darin Petkovbac96002011-08-09 13:22:00 -070041 // These should be kept in sync with ModemManager's mm-modem.h:MMModemState.
42 enum ModemState {
43 kModemStateUnknown = 0,
44 kModemStateDisabled = 10,
45 kModemStateDisabling = 20,
46 kModemStateEnabling = 30,
47 kModemStateEnabled = 40,
48 kModemStateSearching = 50,
49 kModemStateRegistered = 60,
50 kModemStateDisconnecting = 70,
51 kModemStateConnecting = 80,
52 kModemStateConnected = 90,
53 };
54
Chris Masone889666b2011-07-03 12:58:50 -070055 class Network {
56 public:
57 Network();
Darin Petkove9d12e02011-07-27 15:09:37 -070058 ~Network();
Chris Masone889666b2011-07-03 12:58:50 -070059
60 const std::string &GetStatus() const;
61 void SetStatus(const std::string &status);
62
63 const std::string &GetId() const;
64 void SetId(const std::string &id);
65
66 const std::string &GetShortName() const;
67 void SetShortName(const std::string &name);
68
69 const std::string &GetLongName() const;
70 void SetLongName(const std::string &name);
71
72 const std::string &GetTechnology() const;
73 void SetTechnology(const std::string &technology);
74
75 const Stringmap &ToDict() const;
76
77 private:
78 Stringmap dict_;
Darin Petkove9d12e02011-07-27 15:09:37 -070079
Chris Masone889666b2011-07-03 12:58:50 -070080 DISALLOW_COPY_AND_ASSIGN(Network);
81 };
82
Darin Petkovbec79a22011-08-01 14:47:17 -070083 struct CDMA {
84 CDMA();
85
86 uint32 registration_state_evdo;
87 uint32 registration_state_1x;
88 uint32 activation_state;
89 };
90
Chris Masone889666b2011-07-03 12:58:50 -070091 struct SimLockStatus {
92 public:
Darin Petkove9d12e02011-07-27 15:09:37 -070093 SimLockStatus() : retries_left(0) {}
94
Chris Masone889666b2011-07-03 12:58:50 -070095 std::string lock_type;
96 uint32 retries_left;
97 };
98
Darin Petkovc5f56562011-08-06 16:40:05 -070099 static const char kConnectPropertyPhoneNumber[];
100
Darin Petkove9d12e02011-07-27 15:09:37 -0700101 // |owner| is the ModemManager DBus service owner (e.g., ":1.17"). |path| is
102 // the ModemManager.Modem DBus object path (e.g.,
103 // "/org/chromium/ModemManager/Gobi/0").
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700104 Cellular(ControlInterface *control_interface,
105 EventDispatcher *dispatcher,
106 Manager *manager,
Darin Petkove9d12e02011-07-27 15:09:37 -0700107 const std::string &link_name,
108 int interface_index,
109 Type type,
110 const std::string &owner,
111 const std::string &path);
112 virtual ~Cellular();
113
Darin Petkovc5f56562011-08-06 16:40:05 -0700114 // Asynchronously connects the modem to the network.
115 void Connect();
116
Darin Petkovbac96002011-08-09 13:22:00 -0700117 void set_modem_state(ModemState state) { modem_state_ = state; }
118 ModemState modem_state() const { return modem_state_; }
119
Darin Petkove9d12e02011-07-27 15:09:37 -0700120 // Inherited from Device.
121 virtual void Start();
122 virtual void Stop();
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700123 virtual bool TechnologyIs(Technology type) const;
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700124
125 private:
Darin Petkovc5f56562011-08-06 16:40:05 -0700126 FRIEND_TEST(CellularTest, Connect);
Darin Petkovbec79a22011-08-01 14:47:17 -0700127 FRIEND_TEST(CellularTest, GetCDMARegistrationState);
Darin Petkovd9661952011-08-03 16:25:42 -0700128 FRIEND_TEST(CellularTest, GetCDMASignalQuality);
Darin Petkovceb68172011-07-29 14:47:48 -0700129 FRIEND_TEST(CellularTest, GetModemInfo);
130 FRIEND_TEST(CellularTest, GetModemStatus);
Darin Petkove9d12e02011-07-27 15:09:37 -0700131 FRIEND_TEST(CellularTest, GetStateString);
132 FRIEND_TEST(CellularTest, GetTypeString);
Darin Petkovbec79a22011-08-01 14:47:17 -0700133 FRIEND_TEST(CellularTest, InitProxiesCDMA);
134 FRIEND_TEST(CellularTest, InitProxiesGSM);
Darin Petkove9d12e02011-07-27 15:09:37 -0700135 FRIEND_TEST(CellularTest, Start);
Darin Petkovbac96002011-08-09 13:22:00 -0700136 FRIEND_TEST(CellularTest, StartConnected);
137 FRIEND_TEST(CellularTest, StartRegister);
138
139 static const char kPhoneNumberCDMA[];
140 static const char kPhoneNumberGSM[];
Darin Petkove9d12e02011-07-27 15:09:37 -0700141
Darin Petkovc5f56562011-08-06 16:40:05 -0700142 void ConnectTask(const DBusPropertiesMap &properties);
143
Darin Petkovbac96002011-08-09 13:22:00 -0700144 // Invoked when the modem is connected to the cellular network to transition
145 // to the network-connected state and bring the network interface up.
146 void EstablishLink();
147
Chris Masone889666b2011-07-03 12:58:50 -0700148 Stringmaps EnumerateNetworks();
149 StrIntPair SimLockStatusToProperty();
150 void HelpRegisterDerivedStringmaps(const std::string &name,
151 Stringmaps(Cellular::*get)(void),
152 bool(Cellular::*set)(const Stringmaps&));
153 void HelpRegisterDerivedStrIntPair(const std::string &name,
154 StrIntPair(Cellular::*get)(void),
155 bool(Cellular::*set)(const StrIntPair&));
156
Darin Petkovbec79a22011-08-01 14:47:17 -0700157 void InitProxies();
158
Darin Petkove9d12e02011-07-27 15:09:37 -0700159 std::string GetTypeString();
160 std::string GetStateString();
161
Darin Petkovf5f61e02011-07-29 11:35:40 -0700162 void EnableModem();
163 void GetModemStatus();
Darin Petkovceb68172011-07-29 14:47:48 -0700164 void GetGSMProperties();
165 void RegisterGSMModem();
Darin Petkovceb68172011-07-29 14:47:48 -0700166
167 // Obtains the modem identifiers: MEID for CDMA; IMEI, IMSI, SPN and MSISDN
168 // for GSM.
169 void GetModemIdentifiers();
170
Darin Petkovd9661952011-08-03 16:25:42 -0700171 // Obtains modem's manufacturer, model ID, and hardware revision.
Darin Petkovceb68172011-07-29 14:47:48 -0700172 void GetModemInfo();
Darin Petkovf5f61e02011-07-29 11:35:40 -0700173
Darin Petkovbec79a22011-08-01 14:47:17 -0700174 void GetModemRegistrationState();
175 void GetCDMARegistrationState();
176 void GetGSMRegistrationState();
177
Darin Petkovd9661952011-08-03 16:25:42 -0700178 // Processes a change in the modem registration state, possibly creating,
179 // destroying or updating the CellularService.
180 void HandleNewRegistrationState();
181
182 void CreateService();
183
184 void GetModemSignalQuality();
185 uint32 GetCDMASignalQuality();
186 uint32 GetGSMSignalQuality();
187
188 void HandleNewSignalQuality(uint32 strength);
189
190 // Returns true if the modem is registered. Note that this method looks at the
191 // latest CDMA/GSM registration info obtained from the modem rather than the
192 // device |state_|.
193 bool IsModemRegistered();
194
Darin Petkovc5f56562011-08-06 16:40:05 -0700195 // Signal callbacks inherited from ModemCDMAProxyListener.
Darin Petkovd9661952011-08-03 16:25:42 -0700196 virtual void OnCDMARegistrationStateChanged(uint32 state_1x,
197 uint32 state_evdo);
198 virtual void OnCDMASignalQualityChanged(uint32 strength);
199
Darin Petkovc5f56562011-08-06 16:40:05 -0700200 // Signal callbacks inherited from ModemProxyListener.
201 virtual void OnModemStateChanged(uint32 old_state,
202 uint32 new_state,
203 uint32 reason);
204
Darin Petkove9d12e02011-07-27 15:09:37 -0700205 Type type_;
206 State state_;
Darin Petkovbac96002011-08-09 13:22:00 -0700207 ModemState modem_state_;
Darin Petkove9d12e02011-07-27 15:09:37 -0700208
209 const std::string dbus_owner_; // ModemManager.Modem
210 const std::string dbus_path_; // ModemManager.Modem
211 scoped_ptr<ModemProxyInterface> proxy_;
Darin Petkove604f702011-07-28 15:51:17 -0700212 scoped_ptr<ModemSimpleProxyInterface> simple_proxy_;
Darin Petkovbec79a22011-08-01 14:47:17 -0700213 scoped_ptr<ModemCDMAProxyInterface> cdma_proxy_;
214
215 CDMA cdma_;
Darin Petkove9d12e02011-07-27 15:09:37 -0700216
Darin Petkovd9661952011-08-03 16:25:42 -0700217 CellularServiceRefPtr service_;
Chris Masone853b81b2011-06-24 14:11:41 -0700218 bool service_registered_;
219
Darin Petkovc5f56562011-08-06 16:40:05 -0700220 ScopedRunnableMethodFactory<Cellular> task_factory_;
221
Chris Masoneb925cc82011-06-22 15:39:57 -0700222 // Properties
223 bool allow_roaming_;
224 std::string carrier_;
225 std::string meid_;
226 std::string imei_;
227 std::string imsi_;
228 std::string esn_;
229 std::string mdn_;
230 std::string min_;
231 std::string model_id_;
232 std::string manufacturer_;
233 std::string firmware_revision_;
234 std::string hardware_revision_;
Darin Petkovceb68172011-07-29 14:47:48 -0700235 uint16 prl_version_;
Chris Masoneb925cc82011-06-22 15:39:57 -0700236 bool scanning_;
237 uint16 scan_interval_;
Chris Masone889666b2011-07-03 12:58:50 -0700238 std::vector<Network> found_networks_;
239 SimLockStatus sim_lock_status_;
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_