blob: 4445de9ca8ec65c744170d5b2eea648e42a12a70 [file] [log] [blame]
Darin Petkovc64fe5e2012-01-11 12:46:13 +01001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Darin Petkovdaf43862011-10-27 11:37:28 +02002// 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_capability_gsm.h"
6
Jason Glasgow4c0724a2012-04-17 15:47:40 -04007#include <string>
8#include <vector>
9
Eric Shienbrood3e20a232012-02-16 11:35:56 -050010#include <base/bind.h>
Eric Shienbrood3e20a232012-02-16 11:35:56 -050011#include <base/stl_util.h>
Darin Petkov1272a432011-11-10 15:53:37 +010012#include <base/string_number_conversions.h>
Darin Petkovac635a82012-01-10 16:51:58 +010013#include <base/stringprintf.h>
Darin Petkov20c13ec2011-11-09 15:07:15 +010014#include <chromeos/dbus/service_constants.h>
15#include <mm/mm-modem.h>
Darin Petkov1272a432011-11-10 15:53:37 +010016#include <mobile_provider.h>
Darin Petkovdaf43862011-10-27 11:37:28 +020017
Darin Petkov3cfbf212011-11-21 16:02:09 +010018#include "shill/adaptor_interfaces.h"
Darin Petkovae0c64e2011-11-15 15:50:27 +010019#include "shill/cellular_service.h"
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050020#include "shill/error.h"
Christopher Wileyb691efd2012-08-09 13:51:51 -070021#include "shill/logging.h"
Darin Petkov721ac932011-11-16 15:43:09 +010022#include "shill/property_accessor.h"
Darin Petkovdaf43862011-10-27 11:37:28 +020023#include "shill/proxy_factory.h"
24
Eric Shienbrood3e20a232012-02-16 11:35:56 -050025using base::Bind;
Darin Petkovb05315f2011-11-07 10:14:25 +010026using std::string;
Jason Glasgow4c0724a2012-04-17 15:47:40 -040027using std::vector;
Darin Petkovb05315f2011-11-07 10:14:25 +010028
Darin Petkovdaf43862011-10-27 11:37:28 +020029namespace shill {
30
Darin Petkovac635a82012-01-10 16:51:58 +010031// static
32unsigned int CellularCapabilityGSM::friendly_service_name_id_ = 0;
33
Darin Petkov1272a432011-11-10 15:53:37 +010034const char CellularCapabilityGSM::kNetworkPropertyAccessTechnology[] =
35 "access-tech";
36const char CellularCapabilityGSM::kNetworkPropertyID[] = "operator-num";
37const char CellularCapabilityGSM::kNetworkPropertyLongName[] = "operator-long";
38const char CellularCapabilityGSM::kNetworkPropertyShortName[] =
39 "operator-short";
40const char CellularCapabilityGSM::kNetworkPropertyStatus[] = "status";
Darin Petkovae0c64e2011-11-15 15:50:27 +010041const char CellularCapabilityGSM::kPhoneNumber[] = "*99#";
42const char CellularCapabilityGSM::kPropertyAccessTechnology[] =
43 "AccessTechnology";
Darin Petkov63138a92012-02-06 14:09:15 +010044const char CellularCapabilityGSM::kPropertyEnabledFacilityLocks[] =
45 "EnabledFacilityLocks";
Darin Petkov721ac932011-11-16 15:43:09 +010046const char CellularCapabilityGSM::kPropertyUnlockRequired[] = "UnlockRequired";
47const char CellularCapabilityGSM::kPropertyUnlockRetries[] = "UnlockRetries";
Darin Petkov1272a432011-11-10 15:53:37 +010048
Gary Morain82a31a02012-08-02 18:03:32 -070049const int CellularCapabilityGSM::kGetIMSIRetryLimit = 10;
50const int64 CellularCapabilityGSM::kGetIMSIRetryDelayMilliseconds = 200;
51
52
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050053CellularCapabilityGSM::CellularCapabilityGSM(Cellular *cellular,
54 ProxyFactory *proxy_factory)
Jason Glasgow82f9ab32012-04-04 14:27:19 -040055 : CellularCapabilityClassic(cellular, proxy_factory),
Eric Shienbrood3e20a232012-02-16 11:35:56 -050056 weak_ptr_factory_(this),
Darin Petkov184c54e2011-11-15 12:44:39 +010057 registration_state_(MM_MODEM_GSM_NETWORK_REG_STATUS_UNKNOWN),
Darin Petkovae0c64e2011-11-15 15:50:27 +010058 access_technology_(MM_MODEM_GSM_ACCESS_TECH_UNKNOWN),
Darin Petkov0a4dfeb2011-11-18 19:36:13 +010059 home_provider_(NULL),
Gary Morain82a31a02012-08-02 18:03:32 -070060 get_imsi_retries_(0),
61 get_imsi_retry_delay_milliseconds_(kGetIMSIRetryDelayMilliseconds),
Darin Petkov1272a432011-11-10 15:53:37 +010062 scanning_(false),
63 scan_interval_(0) {
Ben Chanfad4a0b2012-04-18 15:49:59 -070064 SLOG(Cellular, 2) << "Cellular capability constructed: GSM";
Darin Petkov1272a432011-11-10 15:53:37 +010065 PropertyStore *store = cellular->mutable_store();
Darin Petkov184c54e2011-11-15 12:44:39 +010066 store->RegisterConstString(flimflam::kSelectedNetworkProperty,
67 &selected_network_);
Darin Petkov1272a432011-11-10 15:53:37 +010068 store->RegisterConstStringmaps(flimflam::kFoundNetworksProperty,
69 &found_networks_);
70 store->RegisterConstBool(flimflam::kScanningProperty, &scanning_);
71 store->RegisterUint16(flimflam::kScanIntervalProperty, &scan_interval_);
Darin Petkov63138a92012-02-06 14:09:15 +010072 HelpRegisterDerivedKeyValueStore(
73 flimflam::kSIMLockStatusProperty,
74 &CellularCapabilityGSM::SimLockStatusToProperty,
75 NULL);
Darin Petkov3cfbf212011-11-21 16:02:09 +010076 store->RegisterConstStringmaps(flimflam::kCellularApnListProperty,
77 &apn_list_);
Darin Petkov0d06f7d2012-02-03 13:08:19 +010078 scanning_supported_ = true;
Ben Chan3ecdf822012-08-06 12:29:23 -070079
80 // TODO(benchan): This is a hack to initialize the GSM card proxy for GetIMSI
81 // before InitProxies is called. There are side-effects of calling InitProxies
82 // before the device is enabled. It's better to refactor InitProxies such that
83 // proxies can be created when the cellular device/capability is constructed,
84 // but callbacks for DBus signal updates are not set up until the device is
85 // enabled.
86 card_proxy_.reset(
87 proxy_factory->CreateModemGSMCardProxy(cellular->dbus_path(),
88 cellular->dbus_owner()));
89 // TODO(benchan): To allow unit testing using a mock proxy without further
90 // complicating the code, the test proxy factory is set up to return a NULL
91 // pointer when CellularCapabilityGSM is constructed. Refactor the code to
92 // avoid this hack.
93 if (card_proxy_.get())
94 InitProperties();
Darin Petkov1272a432011-11-10 15:53:37 +010095}
Darin Petkovdaf43862011-10-27 11:37:28 +020096
Darin Petkov63138a92012-02-06 14:09:15 +010097KeyValueStore CellularCapabilityGSM::SimLockStatusToProperty(Error */*error*/) {
98 KeyValueStore status;
99 status.SetBool(flimflam::kSIMLockEnabledProperty, sim_lock_status_.enabled);
100 status.SetString(flimflam::kSIMLockTypeProperty, sim_lock_status_.lock_type);
101 status.SetUint(flimflam::kSIMLockRetriesLeftProperty,
102 sim_lock_status_.retries_left);
103 return status;
Darin Petkov721ac932011-11-16 15:43:09 +0100104}
105
Darin Petkov63138a92012-02-06 14:09:15 +0100106void CellularCapabilityGSM::HelpRegisterDerivedKeyValueStore(
Darin Petkov721ac932011-11-16 15:43:09 +0100107 const string &name,
Darin Petkov63138a92012-02-06 14:09:15 +0100108 KeyValueStore(CellularCapabilityGSM::*get)(Error *error),
109 void(CellularCapabilityGSM::*set)(
110 const KeyValueStore &value, Error *error)) {
111 cellular()->mutable_store()->RegisterDerivedKeyValueStore(
Darin Petkov721ac932011-11-16 15:43:09 +0100112 name,
Darin Petkov63138a92012-02-06 14:09:15 +0100113 KeyValueStoreAccessor(
114 new CustomAccessor<CellularCapabilityGSM, KeyValueStore>(
Darin Petkov721ac932011-11-16 15:43:09 +0100115 this, get, set)));
116}
117
Eric Shienbrood9a245532012-03-07 14:20:39 -0500118void CellularCapabilityGSM::InitProxies() {
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400119 CellularCapabilityClassic::InitProxies();
Ben Chan3ecdf822012-08-06 12:29:23 -0700120 // TODO(benchan): Remove this check after refactoring the proxy
121 // initialization.
122 if (!card_proxy_.get()) {
123 card_proxy_.reset(
124 proxy_factory()->CreateModemGSMCardProxy(cellular()->dbus_path(),
125 cellular()->dbus_owner()));
126 }
Darin Petkov184c54e2011-11-15 12:44:39 +0100127 network_proxy_.reset(
Eric Shienbrood9a245532012-03-07 14:20:39 -0500128 proxy_factory()->CreateModemGSMNetworkProxy(cellular()->dbus_path(),
Darin Petkovdaf43862011-10-27 11:37:28 +0200129 cellular()->dbus_owner()));
Eric Shienbrood9a245532012-03-07 14:20:39 -0500130 network_proxy_->set_signal_quality_callback(
131 Bind(&CellularCapabilityGSM::OnSignalQualitySignal,
132 weak_ptr_factory_.GetWeakPtr()));
133 network_proxy_->set_network_mode_callback(
134 Bind(&CellularCapabilityGSM::OnNetworkModeSignal,
135 weak_ptr_factory_.GetWeakPtr()));
136 network_proxy_->set_registration_info_callback(
137 Bind(&CellularCapabilityGSM::OnRegistrationInfoSignal,
138 weak_ptr_factory_.GetWeakPtr()));
Darin Petkovdaf43862011-10-27 11:37:28 +0200139}
140
Ben Chan3ecdf822012-08-06 12:29:23 -0700141void CellularCapabilityGSM::InitProperties() {
142 CellularTaskList *tasks = new CellularTaskList();
143 ResultCallback cb_ignore_error =
144 Bind(&CellularCapabilityGSM::StepCompletedCallback,
145 weak_ptr_factory_.GetWeakPtr(), ResultCallback(), true, tasks);
146 // Chrome uses IMSI to determine if a SIM is present before allowing the
147 // modem to be enabled, so shill needs to obtain IMSI even before the device
148 // is enabled.
149 tasks->push_back(Bind(&CellularCapabilityGSM::GetIMSI,
150 weak_ptr_factory_.GetWeakPtr(), cb_ignore_error));
151 RunNextStep(tasks);
152}
153
Eric Shienbrood9a245532012-03-07 14:20:39 -0500154void CellularCapabilityGSM::StartModem(Error *error,
155 const ResultCallback &callback) {
156 InitProxies();
157
158 CellularTaskList *tasks = new CellularTaskList();
159 ResultCallback cb =
160 Bind(&CellularCapabilityGSM::StepCompletedCallback,
Thieu Le923006b2012-04-05 16:32:58 -0700161 weak_ptr_factory_.GetWeakPtr(), callback, false, tasks);
162 ResultCallback cb_ignore_error =
163 Bind(&CellularCapabilityGSM::StepCompletedCallback,
164 weak_ptr_factory_.GetWeakPtr(), callback, true, tasks);
Eric Shienbrood7fce52c2012-04-13 19:11:02 -0400165 if (!cellular()->IsUnderlyingDeviceEnabled())
166 tasks->push_back(Bind(&CellularCapabilityGSM::EnableModem,
167 weak_ptr_factory_.GetWeakPtr(), cb));
168 // If we're within range of the home network, the modem will try to
169 // register once it's enabled, or may be already registered if we
170 // started out enabled.
171 if (!IsUnderlyingDeviceRegistered() && !selected_network_.empty())
172 tasks->push_back(Bind(&CellularCapabilityGSM::Register,
173 weak_ptr_factory_.GetWeakPtr(), cb));
Eric Shienbrood9a245532012-03-07 14:20:39 -0500174 tasks->push_back(Bind(&CellularCapabilityGSM::GetIMEI,
175 weak_ptr_factory_.GetWeakPtr(), cb));
Gary Morain82a31a02012-08-02 18:03:32 -0700176 get_imsi_retries_ = 0;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500177 tasks->push_back(Bind(&CellularCapabilityGSM::GetIMSI,
178 weak_ptr_factory_.GetWeakPtr(), cb));
179 tasks->push_back(Bind(&CellularCapabilityGSM::GetSPN,
Thieu Le923006b2012-04-05 16:32:58 -0700180 weak_ptr_factory_.GetWeakPtr(), cb_ignore_error));
Eric Shienbrood9a245532012-03-07 14:20:39 -0500181 tasks->push_back(Bind(&CellularCapabilityGSM::GetMSISDN,
Thieu Le923006b2012-04-05 16:32:58 -0700182 weak_ptr_factory_.GetWeakPtr(), cb_ignore_error));
Eric Shienbrood9a245532012-03-07 14:20:39 -0500183 tasks->push_back(Bind(&CellularCapabilityGSM::GetProperties,
184 weak_ptr_factory_.GetWeakPtr(), cb));
185 tasks->push_back(Bind(&CellularCapabilityGSM::GetModemInfo,
Eric Shienbrood7fce52c2012-04-13 19:11:02 -0400186 weak_ptr_factory_.GetWeakPtr(), cb_ignore_error));
Eric Shienbrood9a245532012-03-07 14:20:39 -0500187 tasks->push_back(Bind(&CellularCapabilityGSM::FinishEnable,
188 weak_ptr_factory_.GetWeakPtr(), cb));
189
190 RunNextStep(tasks);
191}
192
Eric Shienbrood7fce52c2012-04-13 19:11:02 -0400193bool CellularCapabilityGSM::IsUnderlyingDeviceRegistered() const {
194 switch (cellular()->modem_state()) {
195 case Cellular::kModemStateUnknown:
196 case Cellular::kModemStateDisabled:
197 case Cellular::kModemStateInitializing:
198 case Cellular::kModemStateLocked:
199 case Cellular::kModemStateDisabling:
200 case Cellular::kModemStateEnabling:
201 case Cellular::kModemStateEnabled:
202 return false;
203 case Cellular::kModemStateSearching:
204 case Cellular::kModemStateRegistered:
205 case Cellular::kModemStateDisconnecting:
206 case Cellular::kModemStateConnecting:
207 case Cellular::kModemStateConnected:
208 return true;
209 }
210 return false;
211}
212
Eric Shienbrood9a245532012-03-07 14:20:39 -0500213void CellularCapabilityGSM::ReleaseProxies() {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700214 SLOG(Cellular, 2) << __func__;
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400215 CellularCapabilityClassic::ReleaseProxies();
Darin Petkov721ac932011-11-16 15:43:09 +0100216 card_proxy_.reset();
217 network_proxy_.reset();
218}
219
Darin Petkov5f316f62011-11-18 12:10:26 +0100220void CellularCapabilityGSM::OnServiceCreated() {
Darin Petkov31332412012-01-28 01:50:02 +0100221 // If IMSI is available, base the service's storage identifier on it.
222 if (!imsi_.empty()) {
223 cellular()->service()->SetStorageIdentifier(
Darin Petkovdd3e8662012-02-03 13:16:20 +0100224 string(flimflam::kTypeCellular) + "_" +
225 cellular()->address() + "_" + imsi_);
Darin Petkov31332412012-01-28 01:50:02 +0100226 }
Darin Petkovb9c99332012-01-12 13:13:00 +0100227 cellular()->service()->SetActivationState(
Darin Petkov5f316f62011-11-18 12:10:26 +0100228 flimflam::kActivationStateActivated);
229 UpdateServingOperator();
230}
231
Darin Petkovae0c64e2011-11-15 15:50:27 +0100232void CellularCapabilityGSM::UpdateStatus(const DBusPropertiesMap &properties) {
Eric Shienbrood7fce52c2012-04-13 19:11:02 -0400233 if (ContainsKey(properties, kModemPropertyIMSI)) {
Darin Petkov0a4dfeb2011-11-18 19:36:13 +0100234 SetHomeProvider();
Darin Petkovae0c64e2011-11-15 15:50:27 +0100235 }
236}
237
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400238// Create the list of APNs to try, in the following order:
239// - last APN that resulted in a successful connection attempt on the
240// current network (if any)
241// - the APN, if any, that was set by the user
242// - the list of APNs found in the mobile broadband provider DB for the
243// home provider associated with the current SIM
244// - as a last resort, attempt to connect with no APN
245void CellularCapabilityGSM::SetupApnTryList() {
246 apn_try_list_.clear();
247
248 DCHECK(cellular()->service().get());
249 const Stringmap *apn_info = cellular()->service()->GetLastGoodApn();
250 if (apn_info)
251 apn_try_list_.push_back(*apn_info);
252
253 apn_info = cellular()->service()->GetUserSpecifiedApn();
254 if (apn_info)
255 apn_try_list_.push_back(*apn_info);
256
257 apn_try_list_.insert(apn_try_list_.end(), apn_list_.begin(), apn_list_.end());
258}
259
Darin Petkovae0c64e2011-11-15 15:50:27 +0100260void CellularCapabilityGSM::SetupConnectProperties(
261 DBusPropertiesMap *properties) {
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400262 SetupApnTryList();
263 FillConnectPropertyMap(properties);
264}
265
266void CellularCapabilityGSM::FillConnectPropertyMap(
267 DBusPropertiesMap *properties) {
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500268 (*properties)[kConnectPropertyPhoneNumber].writer().append_string(
Darin Petkovae0c64e2011-11-15 15:50:27 +0100269 kPhoneNumber);
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400270
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400271 if (!AllowRoaming())
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400272 (*properties)[kConnectPropertyHomeOnly].writer().append_bool(true);
273
274 if (!apn_try_list_.empty()) {
275 // Leave the APN at the front of the list, so that it can be recorded
276 // if the connect attempt succeeds.
277 Stringmap apn_info = apn_try_list_.front();
Ben Chanfad4a0b2012-04-18 15:49:59 -0700278 SLOG(Cellular, 2) << __func__ << ": Using APN "
279 << apn_info[flimflam::kApnProperty];
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400280 (*properties)[kConnectPropertyApn].writer().append_string(
281 apn_info[flimflam::kApnProperty].c_str());
282 if (ContainsKey(apn_info, flimflam::kApnUsernameProperty))
283 (*properties)[kConnectPropertyApnUsername].writer().append_string(
284 apn_info[flimflam::kApnUsernameProperty].c_str());
285 if (ContainsKey(apn_info, flimflam::kApnPasswordProperty))
286 (*properties)[kConnectPropertyApnPassword].writer().append_string(
287 apn_info[flimflam::kApnPasswordProperty].c_str());
288 }
289}
290
Nathan Williamsb54974f2012-04-19 11:16:30 -0400291void CellularCapabilityGSM::Connect(const DBusPropertiesMap &properties,
292 Error *error,
293 const ResultCallback &callback) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700294 SLOG(Cellular, 2) << __func__;
Nathan Williamsb54974f2012-04-19 11:16:30 -0400295 ResultCallback cb = Bind(&CellularCapabilityGSM::OnConnectReply,
296 weak_ptr_factory_.GetWeakPtr(),
297 callback);
298 simple_proxy_->Connect(properties, error, cb, kTimeoutConnect);
299}
300
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400301void CellularCapabilityGSM::OnConnectReply(const ResultCallback &callback,
302 const Error &error) {
Thieu Leb5954a22012-05-18 10:37:34 -0700303 CellularServiceRefPtr service = cellular()->service();
304 if (!service) {
305 // The service could have been deleted before our Connect() request
306 // completes if the modem was enabled and then quickly disabled.
307 apn_try_list_.clear();
308 } else if (error.IsFailure()) {
309 service->ClearLastGoodApn();
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400310 // The APN that was just tried (and failed) is still at the
311 // front of the list, about to be removed. If the list is empty
312 // after that, try one last time without an APN. This may succeed
313 // with some modems in some cases.
314 if (error.type() == Error::kInvalidApn && !apn_try_list_.empty()) {
315 apn_try_list_.pop_front();
Ben Chanfad4a0b2012-04-18 15:49:59 -0700316 SLOG(Cellular, 2) << "Connect failed with invalid APN, "
317 << apn_try_list_.size() << " remaining APNs to try";
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400318 DBusPropertiesMap props;
319 FillConnectPropertyMap(&props);
320 Error error;
321 Connect(props, &error, callback);
322 return;
323 }
324 } else if (!apn_try_list_.empty()) {
Thieu Leb5954a22012-05-18 10:37:34 -0700325 service->SetLastGoodApn(apn_try_list_.front());
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400326 apn_try_list_.clear();
327 }
Nathan Williamsb54974f2012-04-19 11:16:30 -0400328 if (!callback.is_null())
329 callback.Run(error);
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400330}
331
332bool CellularCapabilityGSM::AllowRoaming() {
333 bool requires_roaming =
334 home_provider_ ? home_provider_->requires_roaming : false;
335 return requires_roaming || allow_roaming_property();
Darin Petkovae0c64e2011-11-15 15:50:27 +0100336}
337
Eric Shienbrood9a245532012-03-07 14:20:39 -0500338// always called from an async context
339void CellularCapabilityGSM::GetIMEI(const ResultCallback &callback) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700340 SLOG(Cellular, 2) << __func__;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500341 CHECK(!callback.is_null());
342 Error error;
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500343 if (imei_.empty()) {
Eric Shienbrood9a245532012-03-07 14:20:39 -0500344 GSMIdentifierCallback cb = Bind(&CellularCapabilityGSM::OnGetIMEIReply,
345 weak_ptr_factory_.GetWeakPtr(), callback);
346 card_proxy_->GetIMEI(&error, cb, kTimeoutDefault);
347 if (error.IsFailure())
348 callback.Run(error);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500349 } else {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700350 SLOG(Cellular, 2) << "Already have IMEI " << imei_;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500351 callback.Run(error);
Darin Petkovcb547732011-11-09 13:55:26 +0100352 }
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500353}
354
Eric Shienbrood9a245532012-03-07 14:20:39 -0500355// always called from an async context
356void CellularCapabilityGSM::GetIMSI(const ResultCallback &callback) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700357 SLOG(Cellular, 2) << __func__;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500358 CHECK(!callback.is_null());
359 Error error;
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500360 if (imsi_.empty()) {
Eric Shienbrood9a245532012-03-07 14:20:39 -0500361 GSMIdentifierCallback cb = Bind(&CellularCapabilityGSM::OnGetIMSIReply,
362 weak_ptr_factory_.GetWeakPtr(),
363 callback);
364 card_proxy_->GetIMSI(&error, cb, kTimeoutDefault);
365 if (error.IsFailure())
366 callback.Run(error);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500367 } else {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700368 SLOG(Cellular, 2) << "Already have IMSI " << imsi_;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500369 callback.Run(error);
Darin Petkovcb547732011-11-09 13:55:26 +0100370 }
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500371}
372
Eric Shienbrood9a245532012-03-07 14:20:39 -0500373// always called from an async context
374void CellularCapabilityGSM::GetSPN(const ResultCallback &callback) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700375 SLOG(Cellular, 2) << __func__;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500376 CHECK(!callback.is_null());
377 Error error;
Darin Petkovae0c64e2011-11-15 15:50:27 +0100378 if (spn_.empty()) {
Eric Shienbrood9a245532012-03-07 14:20:39 -0500379 GSMIdentifierCallback cb = Bind(&CellularCapabilityGSM::OnGetSPNReply,
380 weak_ptr_factory_.GetWeakPtr(),
381 callback);
382 card_proxy_->GetSPN(&error, cb, kTimeoutDefault);
383 if (error.IsFailure())
384 callback.Run(error);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500385 } else {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700386 SLOG(Cellular, 2) << "Already have SPN " << spn_;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500387 callback.Run(error);
Darin Petkovcb547732011-11-09 13:55:26 +0100388 }
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500389}
390
Eric Shienbrood9a245532012-03-07 14:20:39 -0500391// always called from an async context
392void CellularCapabilityGSM::GetMSISDN(const ResultCallback &callback) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700393 SLOG(Cellular, 2) << __func__;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500394 CHECK(!callback.is_null());
395 Error error;
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500396 if (mdn_.empty()) {
Eric Shienbrood9a245532012-03-07 14:20:39 -0500397 GSMIdentifierCallback cb = Bind(&CellularCapabilityGSM::OnGetMSISDNReply,
398 weak_ptr_factory_.GetWeakPtr(),
399 callback);
400 card_proxy_->GetMSISDN(&error, cb, kTimeoutDefault);
401 if (error.IsFailure())
402 callback.Run(error);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500403 } else {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700404 SLOG(Cellular, 2) << "Already have MSISDN " << mdn_;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500405 callback.Run(error);
Darin Petkovcb547732011-11-09 13:55:26 +0100406 }
407}
408
Darin Petkov3e509242011-11-10 14:46:44 +0100409void CellularCapabilityGSM::GetSignalQuality() {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700410 SLOG(Cellular, 2) << __func__;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500411 SignalQualityCallback callback =
412 Bind(&CellularCapabilityGSM::OnGetSignalQualityReply,
413 weak_ptr_factory_.GetWeakPtr());
414 network_proxy_->GetSignalQuality(NULL, callback, kTimeoutDefault);
Darin Petkov3e509242011-11-10 14:46:44 +0100415}
416
Eric Shienbrood9a245532012-03-07 14:20:39 -0500417void CellularCapabilityGSM::GetRegistrationState() {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700418 SLOG(Cellular, 2) << __func__;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500419 RegistrationInfoCallback callback =
420 Bind(&CellularCapabilityGSM::OnGetRegistrationInfoReply,
421 weak_ptr_factory_.GetWeakPtr());
422 network_proxy_->GetRegistrationInfo(NULL, callback, kTimeoutDefault);
Darin Petkov184c54e2011-11-15 12:44:39 +0100423}
424
Eric Shienbrood9a245532012-03-07 14:20:39 -0500425void CellularCapabilityGSM::GetProperties(const ResultCallback &callback) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700426 SLOG(Cellular, 2) << __func__;
Darin Petkov63138a92012-02-06 14:09:15 +0100427
Darin Petkov184c54e2011-11-15 12:44:39 +0100428 // TODO(petkov): Switch to asynchronous calls (crosbug.com/17583).
429 uint32 tech = network_proxy_->AccessTechnology();
Darin Petkovae0c64e2011-11-15 15:50:27 +0100430 SetAccessTechnology(tech);
Ben Chanfad4a0b2012-04-18 15:49:59 -0700431 SLOG(Cellular, 2) << "GSM AccessTechnology: " << tech;
Darin Petkov63138a92012-02-06 14:09:15 +0100432
433 // TODO(petkov): Switch to asynchronous calls (crosbug.com/17583).
434 uint32 locks = card_proxy_->EnabledFacilityLocks();
435 sim_lock_status_.enabled = locks & MM_MODEM_GSM_FACILITY_SIM;
Ben Chanfad4a0b2012-04-18 15:49:59 -0700436 SLOG(Cellular, 2) << "GSM EnabledFacilityLocks: " << locks;
Darin Petkov63138a92012-02-06 14:09:15 +0100437
Eric Shienbrood9a245532012-03-07 14:20:39 -0500438 callback.Run(Error());
Darin Petkov184c54e2011-11-15 12:44:39 +0100439}
440
Darin Petkovac635a82012-01-10 16:51:58 +0100441string CellularCapabilityGSM::CreateFriendlyServiceName() {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700442 SLOG(Cellular, 2) << __func__;
Darin Petkovac635a82012-01-10 16:51:58 +0100443 if (registration_state_ == MM_MODEM_GSM_NETWORK_REG_STATUS_HOME &&
444 !cellular()->home_provider().GetName().empty()) {
445 return cellular()->home_provider().GetName();
446 }
447 if (!serving_operator_.GetName().empty()) {
448 return serving_operator_.GetName();
449 }
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500450 if (!carrier_.empty()) {
451 return carrier_;
Darin Petkovac635a82012-01-10 16:51:58 +0100452 }
453 if (!serving_operator_.GetCode().empty()) {
454 return "cellular_" + serving_operator_.GetCode();
455 }
456 return base::StringPrintf("GSMNetwork%u", friendly_service_name_id_++);
457}
458
Darin Petkov0a4dfeb2011-11-18 19:36:13 +0100459void CellularCapabilityGSM::SetHomeProvider() {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700460 SLOG(Cellular, 2) << __func__ << "(IMSI: " << imsi_
461 << " SPN: " << spn_ << ")";
Darin Petkov0a4dfeb2011-11-18 19:36:13 +0100462 // TODO(petkov): The test for NULL provider_db should be done by
463 // mobile_provider_lookup_best_match.
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500464 if (imsi_.empty() || !cellular()->provider_db()) {
Darin Petkov0a4dfeb2011-11-18 19:36:13 +0100465 return;
466 }
467 mobile_provider *provider =
468 mobile_provider_lookup_best_match(
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500469 cellular()->provider_db(), spn_.c_str(), imsi_.c_str());
Darin Petkov0a4dfeb2011-11-18 19:36:13 +0100470 if (!provider) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700471 SLOG(Cellular, 2) << "GSM provider not found.";
Darin Petkov0a4dfeb2011-11-18 19:36:13 +0100472 return;
473 }
474 home_provider_ = provider;
475 Cellular::Operator oper;
Darin Petkovb4fccd22012-08-10 11:59:26 +0200476 if (provider->networks && provider->networks[0]) {
Darin Petkov0a4dfeb2011-11-18 19:36:13 +0100477 oper.SetCode(provider->networks[0]);
478 }
479 if (provider->country) {
480 oper.SetCountry(provider->country);
481 }
482 if (spn_.empty()) {
483 const char *name = mobile_provider_get_name(provider);
484 if (name) {
485 oper.SetName(name);
486 }
487 } else {
488 oper.SetName(spn_);
489 }
490 cellular()->set_home_provider(oper);
Darin Petkov3cfbf212011-11-21 16:02:09 +0100491 InitAPNList();
Darin Petkov0a4dfeb2011-11-18 19:36:13 +0100492}
493
Darin Petkovae0c64e2011-11-15 15:50:27 +0100494void CellularCapabilityGSM::UpdateOperatorInfo() {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700495 SLOG(Cellular, 2) << __func__;
Darin Petkov0a4dfeb2011-11-18 19:36:13 +0100496 const string &network_id = serving_operator_.GetCode();
497 if (!network_id.empty()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700498 SLOG(Cellular, 2) << "Looking up network id: " << network_id;
Darin Petkovae0c64e2011-11-15 15:50:27 +0100499 mobile_provider *provider =
500 mobile_provider_lookup_by_network(cellular()->provider_db(),
Darin Petkov0a4dfeb2011-11-18 19:36:13 +0100501 network_id.c_str());
Darin Petkovae0c64e2011-11-15 15:50:27 +0100502 if (provider) {
503 const char *provider_name = mobile_provider_get_name(provider);
504 if (provider_name && *provider_name) {
Darin Petkov0a4dfeb2011-11-18 19:36:13 +0100505 serving_operator_.SetName(provider_name);
506 if (provider->country) {
507 serving_operator_.SetCountry(provider->country);
508 }
Ben Chanfad4a0b2012-04-18 15:49:59 -0700509 SLOG(Cellular, 2) << "Operator name: " << serving_operator_.GetName()
510 << ", country: " << serving_operator_.GetCountry();
Darin Petkovae0c64e2011-11-15 15:50:27 +0100511 }
512 } else {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700513 SLOG(Cellular, 2) << "GSM provider not found.";
Darin Petkovae0c64e2011-11-15 15:50:27 +0100514 }
515 }
516 UpdateServingOperator();
517}
518
519void CellularCapabilityGSM::UpdateServingOperator() {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700520 SLOG(Cellular, 2) << __func__;
Darin Petkov0a4dfeb2011-11-18 19:36:13 +0100521 if (cellular()->service().get()) {
Darin Petkov9cb02682012-01-28 00:17:38 +0100522 cellular()->service()->SetServingOperator(serving_operator_);
Darin Petkovae0c64e2011-11-15 15:50:27 +0100523 }
Darin Petkovae0c64e2011-11-15 15:50:27 +0100524}
525
Darin Petkov3cfbf212011-11-21 16:02:09 +0100526void CellularCapabilityGSM::InitAPNList() {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700527 SLOG(Cellular, 2) << __func__;
Darin Petkov3cfbf212011-11-21 16:02:09 +0100528 if (!home_provider_) {
529 return;
530 }
531 apn_list_.clear();
532 for (int i = 0; i < home_provider_->num_apns; ++i) {
533 Stringmap props;
534 mobile_apn *apn = home_provider_->apns[i];
535 if (apn->value) {
536 props[flimflam::kApnProperty] = apn->value;
537 }
538 if (apn->username) {
539 props[flimflam::kApnUsernameProperty] = apn->username;
540 }
541 if (apn->password) {
542 props[flimflam::kApnPasswordProperty] = apn->password;
543 }
544 // Find the first localized and non-localized name, if any.
545 const localized_name *lname = NULL;
546 const localized_name *name = NULL;
547 for (int j = 0; j < apn->num_names; ++j) {
548 if (apn->names[j]->lang) {
549 if (!lname) {
550 lname = apn->names[j];
551 }
552 } else if (!name) {
553 name = apn->names[j];
554 }
555 }
556 if (name) {
557 props[flimflam::kApnNameProperty] = name->name;
558 }
559 if (lname) {
560 props[flimflam::kApnLocalizedNameProperty] = lname->name;
561 props[flimflam::kApnLanguageProperty] = lname->lang;
562 }
563 apn_list_.push_back(props);
564 }
Darin Petkovdb6083f2012-08-16 12:50:23 +0200565 if (cellular()->adaptor()) {
566 cellular()->adaptor()->EmitStringmapsChanged(
567 flimflam::kCellularApnListProperty, apn_list_);
568 } else {
569 LOG(ERROR) << "Null RPC service adaptor.";
570 }
Darin Petkov3cfbf212011-11-21 16:02:09 +0100571}
572
Eric Shienbrood9a245532012-03-07 14:20:39 -0500573// always called from an async context
574void CellularCapabilityGSM::Register(const ResultCallback &callback) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700575 SLOG(Cellular, 2) << __func__ << " \"" << selected_network_ << "\"";
Eric Shienbrood9a245532012-03-07 14:20:39 -0500576 CHECK(!callback.is_null());
577 Error error;
578 ResultCallback cb = Bind(&CellularCapabilityGSM::OnRegisterReply,
579 weak_ptr_factory_.GetWeakPtr(), callback);
580 network_proxy_->Register(selected_network_, &error, cb, kTimeoutRegister);
581 if (error.IsFailure())
582 callback.Run(error);
Darin Petkov184c54e2011-11-15 12:44:39 +0100583}
584
585void CellularCapabilityGSM::RegisterOnNetwork(
Eric Shienbrood9a245532012-03-07 14:20:39 -0500586 const string &network_id,
587 Error *error,
588 const ResultCallback &callback) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700589 SLOG(Cellular, 2) << __func__ << "(" << network_id << ")";
Eric Shienbrood9a245532012-03-07 14:20:39 -0500590 CHECK(error);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500591 desired_network_ = network_id;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500592 ResultCallback cb = Bind(&CellularCapabilityGSM::OnRegisterReply,
593 weak_ptr_factory_.GetWeakPtr(), callback);
594 network_proxy_->Register(network_id, error, cb, kTimeoutRegister);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500595}
596
Eric Shienbrood9a245532012-03-07 14:20:39 -0500597void CellularCapabilityGSM::OnRegisterReply(const ResultCallback &callback,
598 const Error &error) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700599 SLOG(Cellular, 2) << __func__ << "(" << error << ")";
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500600
601 if (error.IsSuccess()) {
602 selected_network_ = desired_network_;
603 desired_network_.clear();
Eric Shienbrood9a245532012-03-07 14:20:39 -0500604 callback.Run(error);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500605 return;
606 }
607 // If registration on the desired network failed,
608 // try to register on the home network.
609 if (!desired_network_.empty()) {
610 desired_network_.clear();
611 selected_network_.clear();
Eric Shienbrood9a245532012-03-07 14:20:39 -0500612 LOG(INFO) << "Couldn't register on selected network, trying home network";
613 Register(callback);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500614 return;
615 }
Eric Shienbrood9a245532012-03-07 14:20:39 -0500616 callback.Run(error);
Darin Petkov184c54e2011-11-15 12:44:39 +0100617}
618
Darin Petkovb72cf402011-11-22 14:51:39 +0100619bool CellularCapabilityGSM::IsRegistered() {
620 return (registration_state_ == MM_MODEM_GSM_NETWORK_REG_STATUS_HOME ||
621 registration_state_ == MM_MODEM_GSM_NETWORK_REG_STATUS_ROAMING);
622}
623
Eric Shienbrood7fce52c2012-04-13 19:11:02 -0400624void CellularCapabilityGSM::SetUnregistered(bool searching) {
625 // If we're already in some non-registered state, don't override that
626 if (registration_state_ == MM_MODEM_GSM_NETWORK_REG_STATUS_HOME ||
627 registration_state_ == MM_MODEM_GSM_NETWORK_REG_STATUS_ROAMING) {
628 registration_state_ =
629 (searching ? MM_MODEM_GSM_NETWORK_REG_STATUS_SEARCHING :
630 MM_MODEM_GSM_NETWORK_REG_STATUS_IDLE);
631 }
632}
633
Darin Petkovb05315f2011-11-07 10:14:25 +0100634void CellularCapabilityGSM::RequirePIN(
Eric Shienbrood9a245532012-03-07 14:20:39 -0500635 const std::string &pin, bool require,
636 Error *error, const ResultCallback &callback) {
637 CHECK(error);
638 card_proxy_->EnablePIN(pin, require, error, callback, kTimeoutDefault);
Darin Petkovb05315f2011-11-07 10:14:25 +0100639}
640
Darin Petkove5bc2cb2011-12-07 14:47:32 +0100641void CellularCapabilityGSM::EnterPIN(const string &pin,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500642 Error *error,
643 const ResultCallback &callback) {
644 CHECK(error);
645 card_proxy_->SendPIN(pin, error, callback, kTimeoutDefault);
Darin Petkovb05315f2011-11-07 10:14:25 +0100646}
647
Darin Petkovc64fe5e2012-01-11 12:46:13 +0100648void CellularCapabilityGSM::UnblockPIN(const string &unblock_code,
649 const string &pin,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500650 Error *error,
651 const ResultCallback &callback) {
652 CHECK(error);
653 card_proxy_->SendPUK(unblock_code, pin, error, callback, kTimeoutDefault);
Darin Petkovb05315f2011-11-07 10:14:25 +0100654}
655
656void CellularCapabilityGSM::ChangePIN(
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500657 const string &old_pin, const string &new_pin,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500658 Error *error, const ResultCallback &callback) {
659 CHECK(error);
660 card_proxy_->ChangePIN(old_pin, new_pin, error, callback, kTimeoutDefault);
Darin Petkovb05315f2011-11-07 10:14:25 +0100661}
662
Eric Shienbrood9a245532012-03-07 14:20:39 -0500663void CellularCapabilityGSM::Scan(Error *error, const ResultCallback &callback) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700664 SLOG(Cellular, 2) << __func__;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500665 CHECK(error);
Jason Glasgowcd0349c2012-05-03 23:32:15 -0400666 if (scanning_) {
667 Error::PopulateAndLog(error, Error::kInProgress, "Already scanning");
668 return;
669 }
Eric Shienbrood9a245532012-03-07 14:20:39 -0500670 ScanResultsCallback cb = Bind(&CellularCapabilityGSM::OnScanReply,
671 weak_ptr_factory_.GetWeakPtr(), callback);
672 network_proxy_->Scan(error, cb, kTimeoutScan);
Jason Glasgowcd0349c2012-05-03 23:32:15 -0400673 if (!error->IsFailure()) {
674 scanning_ = true;
675 cellular()->adaptor()->EmitBoolChanged(flimflam::kScanningProperty,
676 scanning_);
677 }
Darin Petkov1272a432011-11-10 15:53:37 +0100678}
679
Eric Shienbrood9a245532012-03-07 14:20:39 -0500680void CellularCapabilityGSM::OnScanReply(const ResultCallback &callback,
681 const GSMScanResults &results,
682 const Error &error) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700683 SLOG(Cellular, 2) << __func__;
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400684
685 // Error handling is weak. The current expectation is that on any
686 // error, found_networks_ should be cleared and a property change
687 // notification sent out.
688 //
689 // TODO(jglasgow): fix error handling
Jason Glasgowcd0349c2012-05-03 23:32:15 -0400690 scanning_ = false;
691 cellular()->adaptor()->EmitBoolChanged(flimflam::kScanningProperty,
692 scanning_);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500693 found_networks_.clear();
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400694 if (!error.IsFailure()) {
695 for (GSMScanResults::const_iterator it = results.begin();
696 it != results.end(); ++it) {
697 found_networks_.push_back(ParseScanResult(*it));
698 }
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500699 }
Darin Petkovb7828b02012-02-03 12:34:30 +0100700 cellular()->adaptor()->EmitStringmapsChanged(flimflam::kFoundNetworksProperty,
701 found_networks_);
Gary Moraina49062a2012-04-27 13:48:41 -0700702 if (!callback.is_null())
703 callback.Run(error);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500704}
705
706Stringmap CellularCapabilityGSM::ParseScanResult(const GSMScanResult &result) {
Darin Petkov1272a432011-11-10 15:53:37 +0100707 Stringmap parsed;
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500708 for (GSMScanResult::const_iterator it = result.begin();
709 it != result.end(); ++it) {
Darin Petkov1272a432011-11-10 15:53:37 +0100710 // TODO(petkov): Define these in system_api/service_constants.h. The
711 // numerical values are taken from 3GPP TS 27.007 Section 7.3.
712 static const char * const kStatusString[] = {
713 "unknown",
714 "available",
715 "current",
716 "forbidden",
717 };
718 static const char * const kTechnologyString[] = {
719 flimflam::kNetworkTechnologyGsm,
720 "GSM Compact",
721 flimflam::kNetworkTechnologyUmts,
722 flimflam::kNetworkTechnologyEdge,
723 "HSDPA",
724 "HSUPA",
725 flimflam::kNetworkTechnologyHspa,
726 };
Ben Chanfad4a0b2012-04-18 15:49:59 -0700727 SLOG(Cellular, 2) << "Network property: " << it->first << " = "
728 << it->second;
Darin Petkov1272a432011-11-10 15:53:37 +0100729 if (it->first == kNetworkPropertyStatus) {
730 int status = 0;
731 if (base::StringToInt(it->second, &status) &&
732 status >= 0 &&
733 status < static_cast<int>(arraysize(kStatusString))) {
734 parsed[flimflam::kStatusProperty] = kStatusString[status];
735 } else {
736 LOG(ERROR) << "Unexpected status value: " << it->second;
737 }
738 } else if (it->first == kNetworkPropertyID) {
739 parsed[flimflam::kNetworkIdProperty] = it->second;
740 } else if (it->first == kNetworkPropertyLongName) {
741 parsed[flimflam::kLongNameProperty] = it->second;
742 } else if (it->first == kNetworkPropertyShortName) {
743 parsed[flimflam::kShortNameProperty] = it->second;
744 } else if (it->first == kNetworkPropertyAccessTechnology) {
745 int tech = 0;
746 if (base::StringToInt(it->second, &tech) &&
747 tech >= 0 &&
748 tech < static_cast<int>(arraysize(kTechnologyString))) {
749 parsed[flimflam::kTechnologyProperty] = kTechnologyString[tech];
750 } else {
751 LOG(ERROR) << "Unexpected technology value: " << it->second;
752 }
753 } else {
754 LOG(WARNING) << "Unknown network property ignored: " << it->first;
755 }
756 }
757 // If the long name is not available but the network ID is, look up the long
758 // name in the mobile provider database.
759 if ((!ContainsKey(parsed, flimflam::kLongNameProperty) ||
760 parsed[flimflam::kLongNameProperty].empty()) &&
761 ContainsKey(parsed, flimflam::kNetworkIdProperty)) {
762 mobile_provider *provider =
763 mobile_provider_lookup_by_network(
764 cellular()->provider_db(),
765 parsed[flimflam::kNetworkIdProperty].c_str());
766 if (provider) {
767 const char *long_name = mobile_provider_get_name(provider);
768 if (long_name && *long_name) {
769 parsed[flimflam::kLongNameProperty] = long_name;
770 }
771 }
772 }
773 return parsed;
774}
775
Darin Petkovae0c64e2011-11-15 15:50:27 +0100776void CellularCapabilityGSM::SetAccessTechnology(uint32 access_technology) {
777 access_technology_ = access_technology;
778 if (cellular()->service().get()) {
Darin Petkovb72cf402011-11-22 14:51:39 +0100779 cellular()->service()->SetNetworkTechnology(GetNetworkTechnologyString());
Darin Petkovae0c64e2011-11-15 15:50:27 +0100780 }
781}
782
Darin Petkov20c13ec2011-11-09 15:07:15 +0100783string CellularCapabilityGSM::GetNetworkTechnologyString() const {
Darin Petkovb72cf402011-11-22 14:51:39 +0100784 switch (access_technology_) {
785 case MM_MODEM_GSM_ACCESS_TECH_GSM:
786 case MM_MODEM_GSM_ACCESS_TECH_GSM_COMPACT:
787 return flimflam::kNetworkTechnologyGsm;
788 case MM_MODEM_GSM_ACCESS_TECH_GPRS:
789 return flimflam::kNetworkTechnologyGprs;
790 case MM_MODEM_GSM_ACCESS_TECH_EDGE:
791 return flimflam::kNetworkTechnologyEdge;
792 case MM_MODEM_GSM_ACCESS_TECH_UMTS:
793 return flimflam::kNetworkTechnologyUmts;
794 case MM_MODEM_GSM_ACCESS_TECH_HSDPA:
795 case MM_MODEM_GSM_ACCESS_TECH_HSUPA:
796 case MM_MODEM_GSM_ACCESS_TECH_HSPA:
797 return flimflam::kNetworkTechnologyHspa;
798 case MM_MODEM_GSM_ACCESS_TECH_HSPA_PLUS:
799 return flimflam::kNetworkTechnologyHspaPlus;
800 default:
801 break;
Darin Petkov20c13ec2011-11-09 15:07:15 +0100802 }
803 return "";
804}
805
806string CellularCapabilityGSM::GetRoamingStateString() const {
Darin Petkov184c54e2011-11-15 12:44:39 +0100807 switch (registration_state_) {
Darin Petkov20c13ec2011-11-09 15:07:15 +0100808 case MM_MODEM_GSM_NETWORK_REG_STATUS_HOME:
809 return flimflam::kRoamingStateHome;
810 case MM_MODEM_GSM_NETWORK_REG_STATUS_ROAMING:
811 return flimflam::kRoamingStateRoaming;
812 default:
813 break;
814 }
815 return flimflam::kRoamingStateUnknown;
816}
817
Jason Glasgow4c0724a2012-04-17 15:47:40 -0400818void CellularCapabilityGSM::OnDBusPropertiesChanged(
819 const string &interface,
820 const DBusPropertiesMap &properties,
Eric Shienbrood7fce52c2012-04-13 19:11:02 -0400821 const vector<string> &invalidated_properties) {
822 CellularCapabilityClassic::OnDBusPropertiesChanged(interface,
823 properties,
824 invalidated_properties);
825 if (interface == MM_MODEM_GSM_NETWORK_INTERFACE) {
826 uint32 access_technology = MM_MODEM_GSM_ACCESS_TECH_UNKNOWN;
827 if (DBusProperties::GetUint32(properties,
828 kPropertyAccessTechnology,
829 &access_technology)) {
830 SetAccessTechnology(access_technology);
831 }
Gary Morainbaeefdf2012-04-30 14:53:35 -0700832 } else {
Eric Shienbrood7fce52c2012-04-13 19:11:02 -0400833 bool emit = false;
Gary Morainbaeefdf2012-04-30 14:53:35 -0700834 if (interface == MM_MODEM_GSM_CARD_INTERFACE) {
835 uint32 locks = 0;
836 if (DBusProperties::GetUint32(
837 properties, kPropertyEnabledFacilityLocks, &locks)) {
838 sim_lock_status_.enabled = locks & MM_MODEM_GSM_FACILITY_SIM;
839 emit = true;
840 }
841 } else if (interface == MM_MODEM_INTERFACE) {
842 if (DBusProperties::GetString(properties, kPropertyUnlockRequired,
843 &sim_lock_status_.lock_type)) {
844 emit = true;
845 }
846 if (DBusProperties::GetUint32(properties, kPropertyUnlockRetries,
847 &sim_lock_status_.retries_left)) {
848 emit = true;
849 }
Eric Shienbrood7fce52c2012-04-13 19:11:02 -0400850 }
851 if (emit) {
852 cellular()->adaptor()->EmitKeyValueStoreChanged(
853 flimflam::kSIMLockStatusProperty, SimLockStatusToProperty(NULL));
854 }
Darin Petkov63138a92012-02-06 14:09:15 +0100855 }
Darin Petkovae0c64e2011-11-15 15:50:27 +0100856}
857
Eric Shienbrood9a245532012-03-07 14:20:39 -0500858void CellularCapabilityGSM::OnNetworkModeSignal(uint32 /*mode*/) {
Darin Petkov184c54e2011-11-15 12:44:39 +0100859 // TODO(petkov): Implement this.
860 NOTIMPLEMENTED();
861}
862
Eric Shienbrood9a245532012-03-07 14:20:39 -0500863void CellularCapabilityGSM::OnRegistrationInfoSignal(
864 uint32 status, const string &operator_code, const string &operator_name) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700865 SLOG(Cellular, 2) << __func__ << ": regstate=" << status
866 << ", opercode=" << operator_code
867 << ", opername=" << operator_name;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500868 registration_state_ = status;
869 serving_operator_.SetCode(operator_code);
870 serving_operator_.SetName(operator_name);
871 UpdateOperatorInfo();
872 cellular()->HandleNewRegistrationState();
Darin Petkov184c54e2011-11-15 12:44:39 +0100873}
874
Eric Shienbrood9a245532012-03-07 14:20:39 -0500875void CellularCapabilityGSM::OnSignalQualitySignal(uint32 quality) {
Darin Petkov184c54e2011-11-15 12:44:39 +0100876 cellular()->HandleNewSignalQuality(quality);
877}
878
Eric Shienbrood9a245532012-03-07 14:20:39 -0500879void CellularCapabilityGSM::OnGetRegistrationInfoReply(
880 uint32 status, const string &operator_code, const string &operator_name,
881 const Error &error) {
882 if (error.IsSuccess())
883 OnRegistrationInfoSignal(status, operator_code, operator_name);
884}
885
886void CellularCapabilityGSM::OnGetSignalQualityReply(uint32 quality,
887 const Error &error) {
888 if (error.IsSuccess())
889 OnSignalQualitySignal(quality);
890}
891
892void CellularCapabilityGSM::OnGetIMEIReply(const ResultCallback &callback,
893 const string &imei,
894 const Error &error) {
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500895 if (error.IsSuccess()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700896 SLOG(Cellular, 2) << "IMEI: " << imei;
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500897 imei_ = imei;
898 } else {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700899 SLOG(Cellular, 2) << "GetIMEI failed - " << error;
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500900 }
Eric Shienbrood9a245532012-03-07 14:20:39 -0500901 callback.Run(error);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500902}
903
Eric Shienbrood9a245532012-03-07 14:20:39 -0500904void CellularCapabilityGSM::OnGetIMSIReply(const ResultCallback &callback,
905 const string &imsi,
906 const Error &error) {
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500907 if (error.IsSuccess()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700908 SLOG(Cellular, 2) << "IMSI: " << imsi;
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500909 imsi_ = imsi;
910 SetHomeProvider();
Gary Morain82a31a02012-08-02 18:03:32 -0700911 callback.Run(error);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500912 } else {
Gary Morain82a31a02012-08-02 18:03:32 -0700913 if (get_imsi_retries_++ < kGetIMSIRetryLimit) {
914 SLOG(Cellular, 2) << "GetIMSI failed - " << error << ". Retrying";
915 base::Callback<void(void)> retry_get_imsi_cb =
916 Bind(&CellularCapabilityGSM::GetIMSI,
917 weak_ptr_factory_.GetWeakPtr(), callback);
918 cellular()->dispatcher()->PostDelayedTask(
919 retry_get_imsi_cb,
920 get_imsi_retry_delay_milliseconds_);
921 } else {
922 LOG(INFO) << "GetIMSI failed - " << error;
923 callback.Run(error);
924 }
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500925 }
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500926}
927
Eric Shienbrood9a245532012-03-07 14:20:39 -0500928void CellularCapabilityGSM::OnGetSPNReply(const ResultCallback &callback,
929 const string &spn,
930 const Error &error) {
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500931 if (error.IsSuccess()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700932 SLOG(Cellular, 2) << "SPN: " << spn;
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500933 spn_ = spn;
934 SetHomeProvider();
935 } else {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700936 SLOG(Cellular, 2) << "GetSPN failed - " << error;
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500937 }
Thieu Le923006b2012-04-05 16:32:58 -0700938 callback.Run(error);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500939}
940
Eric Shienbrood9a245532012-03-07 14:20:39 -0500941void CellularCapabilityGSM::OnGetMSISDNReply(const ResultCallback &callback,
942 const string &msisdn,
943 const Error &error) {
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500944 if (error.IsSuccess()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700945 SLOG(Cellular, 2) << "MSISDN: " << msisdn;
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500946 mdn_ = msisdn;
947 } else {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700948 SLOG(Cellular, 2) << "GetMSISDN failed - " << error;
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500949 }
Thieu Le923006b2012-04-05 16:32:58 -0700950 callback.Run(error);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500951}
952
Darin Petkovdaf43862011-10-27 11:37:28 +0200953} // namespace shill