blob: eba49563e8b387cb7cc38302109a30b33c254232 [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#include "shill/cellular.h"
6
7#include <string>
Chris Masone889666b2011-07-03 12:58:50 -07008#include <utility>
9#include <vector>
Chris Masone3bd3c8c2011-06-13 08:20:26 -070010
11#include <base/logging.h>
Darin Petkove9d12e02011-07-27 15:09:37 -070012#include <base/stringprintf.h>
Chris Masoneb925cc82011-06-22 15:39:57 -070013#include <chromeos/dbus/service_constants.h>
Chris Masone3bd3c8c2011-06-13 08:20:26 -070014
15#include "shill/cellular_service.h"
16#include "shill/control_interface.h"
17#include "shill/device.h"
18#include "shill/device_info.h"
19#include "shill/manager.h"
Darin Petkove9d12e02011-07-27 15:09:37 -070020#include "shill/modem_proxy_interface.h"
Chris Masone7aa5f902011-07-11 11:13:35 -070021#include "shill/profile.h"
Chris Masone889666b2011-07-03 12:58:50 -070022#include "shill/property_accessor.h"
Darin Petkove9d12e02011-07-27 15:09:37 -070023#include "shill/proxy_factory.h"
Chris Masone3bd3c8c2011-06-13 08:20:26 -070024#include "shill/shill_event.h"
25
Chris Masone889666b2011-07-03 12:58:50 -070026using std::make_pair;
Chris Masone3bd3c8c2011-06-13 08:20:26 -070027using std::string;
Chris Masone889666b2011-07-03 12:58:50 -070028using std::vector;
Chris Masone3bd3c8c2011-06-13 08:20:26 -070029
30namespace shill {
31
Chris Masone889666b2011-07-03 12:58:50 -070032Cellular::Network::Network() {
33 dict_[flimflam::kStatusProperty] = "";
34 dict_[flimflam::kNetworkIdProperty] = "";
35 dict_[flimflam::kShortNameProperty] = "";
36 dict_[flimflam::kLongNameProperty] = "";
37 dict_[flimflam::kTechnologyProperty] = "";
38}
39
40Cellular::Network::~Network() {}
41
42const std::string &Cellular::Network::GetStatus() const {
43 return dict_.find(flimflam::kStatusProperty)->second;
44}
45
46void Cellular::Network::SetStatus(const std::string &status) {
47 dict_[flimflam::kStatusProperty] = status;
48}
49
50const std::string &Cellular::Network::GetId() const {
51 return dict_.find(flimflam::kNetworkIdProperty)->second;
52}
53
54void Cellular::Network::SetId(const std::string &id) {
55 dict_[flimflam::kNetworkIdProperty] = id;
56}
57
58const std::string &Cellular::Network::GetShortName() const {
59 return dict_.find(flimflam::kShortNameProperty)->second;
60}
61
62void Cellular::Network::SetShortName(const std::string &name) {
63 dict_[flimflam::kShortNameProperty] = name;
64}
65
66const std::string &Cellular::Network::GetLongName() const {
67 return dict_.find(flimflam::kLongNameProperty)->second;
68}
69
70void Cellular::Network::SetLongName(const std::string &name) {
71 dict_[flimflam::kLongNameProperty] = name;
72}
73
74const std::string &Cellular::Network::GetTechnology() const {
75 return dict_.find(flimflam::kTechnologyProperty)->second;
76}
77
78void Cellular::Network::SetTechnology(const std::string &technology) {
79 dict_[flimflam::kTechnologyProperty] = technology;
80}
81
82const Stringmap &Cellular::Network::ToDict() const {
83 return dict_;
84}
85
Chris Masone3bd3c8c2011-06-13 08:20:26 -070086Cellular::Cellular(ControlInterface *control_interface,
87 EventDispatcher *dispatcher,
88 Manager *manager,
Darin Petkove9d12e02011-07-27 15:09:37 -070089 const string &link_name,
90 int interface_index,
91 Type type,
92 const string &owner,
93 const string &path)
Chris Masone3bd3c8c2011-06-13 08:20:26 -070094 : Device(control_interface,
95 dispatcher,
96 manager,
Darin Petkove9d12e02011-07-27 15:09:37 -070097 link_name,
Chris Masone3bd3c8c2011-06-13 08:20:26 -070098 interface_index),
Darin Petkove9d12e02011-07-27 15:09:37 -070099 type_(type),
100 state_(kStateDisabled),
101 dbus_owner_(owner),
102 dbus_path_(path),
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700103 service_(new CellularService(control_interface,
104 dispatcher,
Chris Masone6791a432011-07-12 13:23:19 -0700105 manager,
mukesh agrawal51a7e932011-07-27 16:18:26 -0700106 this)),
Darin Petkove9d12e02011-07-27 15:09:37 -0700107 service_registered_(false),
108 allow_roaming_(false),
109 prl_version_(0),
110 scanning_(false),
111 scan_interval_(0) {
112 store_.RegisterConstString(flimflam::kDBusConnectionProperty, &dbus_owner_);
113 store_.RegisterConstString(flimflam::kDBusObjectProperty, &dbus_path_);
Chris Masone27c4aa52011-07-02 13:10:14 -0700114 store_.RegisterConstString(flimflam::kCarrierProperty, &carrier_);
Chris Masone4d42df82011-07-02 17:09:39 -0700115 store_.RegisterBool(flimflam::kCellularAllowRoamingProperty, &allow_roaming_);
Chris Masone27c4aa52011-07-02 13:10:14 -0700116 store_.RegisterConstString(flimflam::kEsnProperty, &esn_);
Chris Masone27c4aa52011-07-02 13:10:14 -0700117 store_.RegisterConstString(flimflam::kFirmwareRevisionProperty,
118 &firmware_revision_);
119 store_.RegisterConstString(flimflam::kHardwareRevisionProperty,
120 &hardware_revision_);
Chris Masone4d42df82011-07-02 17:09:39 -0700121 store_.RegisterConstString(flimflam::kImeiProperty, &imei_);
122 store_.RegisterConstString(flimflam::kImsiProperty, &imsi_);
123 store_.RegisterConstString(flimflam::kManufacturerProperty, &manufacturer_);
124 store_.RegisterConstString(flimflam::kMdnProperty, &mdn_);
125 store_.RegisterConstString(flimflam::kMeidProperty, &meid_);
126 store_.RegisterConstString(flimflam::kMinProperty, &min_);
127 store_.RegisterConstString(flimflam::kModelIDProperty, &model_id_);
Chris Masone27c4aa52011-07-02 13:10:14 -0700128 store_.RegisterConstInt16(flimflam::kPRLVersionProperty, &prl_version_);
Chris Masoneb925cc82011-06-22 15:39:57 -0700129
Chris Masone889666b2011-07-03 12:58:50 -0700130 HelpRegisterDerivedStrIntPair(flimflam::kSIMLockStatusProperty,
131 &Cellular::SimLockStatusToProperty,
132 NULL);
133 HelpRegisterDerivedStringmaps(flimflam::kFoundNetworksProperty,
134 &Cellular::EnumerateNetworks,
135 NULL);
Chris Masoneb925cc82011-06-22 15:39:57 -0700136
Chris Masone4d42df82011-07-02 17:09:39 -0700137 store_.RegisterConstBool(flimflam::kScanningProperty, &scanning_);
138 store_.RegisterUint16(flimflam::kScanIntervalProperty, &scan_interval_);
139
Darin Petkove9d12e02011-07-27 15:09:37 -0700140 VLOG(2) << "Cellular device " << link_name_ << " initialized: "
141 << GetTypeString();
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700142}
143
Darin Petkove9d12e02011-07-27 15:09:37 -0700144Cellular::~Cellular() {}
145
146std::string Cellular::GetTypeString() {
147 switch (type_) {
148 case kTypeGSM: return "CellularTypeGSM";
149 case kTypeCDMA: return "CellularTypeCDMA";
150 default: return StringPrintf("CellularTypeUnknown-%d", type_);
151 }
152}
153
154std::string Cellular::GetStateString() {
155 switch (state_) {
156 case kStateDisabled: return "CellularStateDisabled";
157 case kStateEnabled: return "CellularStateEnabled";
158 case kStateRegistered: return "CellularStateRegistered";
159 case kStateConnected: return "CellularStateConnected";
160 default: return StringPrintf("CellularStateUnknown-%d", state_);
161 }
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700162}
163
164void Cellular::Start() {
Darin Petkove9d12e02011-07-27 15:09:37 -0700165 VLOG(2) << __func__ << ": " << GetStateString();
166 CHECK_EQ(kStateDisabled, state_);
167 proxy_.reset(
168 ProxyFactory::factory()->CreateModemProxy(dbus_path_, dbus_owner_));
169 // TODO(petkov): Switch to asynchronous calls (crosbug.com/17583).
170 proxy_->Enable(true);
171 state_ = kStateEnabled;
172 // TODO(petkov): Device::Start();
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700173}
174
175void Cellular::Stop() {
Darin Petkove9d12e02011-07-27 15:09:37 -0700176 proxy_.reset();
Chris Masone2b105542011-06-22 10:58:09 -0700177 manager_->DeregisterService(service_);
Darin Petkove9d12e02011-07-27 15:09:37 -0700178 service_ = NULL; // Breaks a reference cycle.
179 // TODO(petkov): Device::Stop();
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700180}
181
182bool Cellular::TechnologyIs(const Device::Technology type) {
183 return type == Device::kCellular;
184}
185
Chris Masone889666b2011-07-03 12:58:50 -0700186Stringmaps Cellular::EnumerateNetworks() {
187 Stringmaps to_return;
188 for (vector<Network>::const_iterator it = found_networks_.begin();
189 it != found_networks_.end();
190 ++it) {
191 to_return.push_back(it->ToDict());
192 }
193 return to_return;
194}
195
196StrIntPair Cellular::SimLockStatusToProperty() {
197 return StrIntPair(make_pair(flimflam::kSIMLockTypeProperty,
198 sim_lock_status_.lock_type),
199 make_pair(flimflam::kSIMLockRetriesLeftProperty,
200 sim_lock_status_.retries_left));
201}
202
203void Cellular::HelpRegisterDerivedStringmaps(
204 const string &name,
205 Stringmaps(Cellular::*get)(void),
206 bool(Cellular::*set)(const Stringmaps&)) {
207 store_.RegisterDerivedStringmaps(
208 name,
209 StringmapsAccessor(
210 new CustomAccessor<Cellular, Stringmaps>(this, get, set)));
211}
212
213void Cellular::HelpRegisterDerivedStrIntPair(
214 const string &name,
215 StrIntPair(Cellular::*get)(void),
216 bool(Cellular::*set)(const StrIntPair&)) {
217 store_.RegisterDerivedStrIntPair(
218 name,
219 StrIntPairAccessor(
220 new CustomAccessor<Cellular, StrIntPair>(this, get, set)));
221}
222
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700223} // namespace shill