Darin Petkov | c64fe5e | 2012-01-11 12:46:13 +0100 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Darin Petkov | daf4386 | 2011-10-27 11:37:28 +0200 | [diff] [blame] | 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_capability_gsm.h" |
| 6 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 7 | #include <base/bind.h> |
Darin Petkov | daf4386 | 2011-10-27 11:37:28 +0200 | [diff] [blame] | 8 | #include <base/logging.h> |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 9 | #include <base/stl_util.h> |
Darin Petkov | 1272a43 | 2011-11-10 15:53:37 +0100 | [diff] [blame] | 10 | #include <base/string_number_conversions.h> |
Darin Petkov | ac635a8 | 2012-01-10 16:51:58 +0100 | [diff] [blame] | 11 | #include <base/stringprintf.h> |
Darin Petkov | 20c13ec | 2011-11-09 15:07:15 +0100 | [diff] [blame] | 12 | #include <chromeos/dbus/service_constants.h> |
| 13 | #include <mm/mm-modem.h> |
Darin Petkov | 1272a43 | 2011-11-10 15:53:37 +0100 | [diff] [blame] | 14 | #include <mobile_provider.h> |
Darin Petkov | daf4386 | 2011-10-27 11:37:28 +0200 | [diff] [blame] | 15 | |
Darin Petkov | 3cfbf21 | 2011-11-21 16:02:09 +0100 | [diff] [blame] | 16 | #include "shill/adaptor_interfaces.h" |
Darin Petkov | ae0c64e | 2011-11-15 15:50:27 +0100 | [diff] [blame] | 17 | #include "shill/cellular_service.h" |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 18 | #include "shill/error.h" |
Darin Petkov | 721ac93 | 2011-11-16 15:43:09 +0100 | [diff] [blame] | 19 | #include "shill/property_accessor.h" |
Darin Petkov | daf4386 | 2011-10-27 11:37:28 +0200 | [diff] [blame] | 20 | #include "shill/proxy_factory.h" |
| 21 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 22 | using base::Bind; |
Darin Petkov | b05315f | 2011-11-07 10:14:25 +0100 | [diff] [blame] | 23 | using std::string; |
| 24 | |
Darin Petkov | daf4386 | 2011-10-27 11:37:28 +0200 | [diff] [blame] | 25 | namespace shill { |
| 26 | |
Darin Petkov | ac635a8 | 2012-01-10 16:51:58 +0100 | [diff] [blame] | 27 | // static |
| 28 | unsigned int CellularCapabilityGSM::friendly_service_name_id_ = 0; |
| 29 | |
Darin Petkov | 1272a43 | 2011-11-10 15:53:37 +0100 | [diff] [blame] | 30 | const char CellularCapabilityGSM::kNetworkPropertyAccessTechnology[] = |
| 31 | "access-tech"; |
| 32 | const char CellularCapabilityGSM::kNetworkPropertyID[] = "operator-num"; |
| 33 | const char CellularCapabilityGSM::kNetworkPropertyLongName[] = "operator-long"; |
| 34 | const char CellularCapabilityGSM::kNetworkPropertyShortName[] = |
| 35 | "operator-short"; |
| 36 | const char CellularCapabilityGSM::kNetworkPropertyStatus[] = "status"; |
Darin Petkov | ae0c64e | 2011-11-15 15:50:27 +0100 | [diff] [blame] | 37 | const char CellularCapabilityGSM::kPhoneNumber[] = "*99#"; |
| 38 | const char CellularCapabilityGSM::kPropertyAccessTechnology[] = |
| 39 | "AccessTechnology"; |
Darin Petkov | 63138a9 | 2012-02-06 14:09:15 +0100 | [diff] [blame] | 40 | const char CellularCapabilityGSM::kPropertyEnabledFacilityLocks[] = |
| 41 | "EnabledFacilityLocks"; |
Darin Petkov | 721ac93 | 2011-11-16 15:43:09 +0100 | [diff] [blame] | 42 | const char CellularCapabilityGSM::kPropertyUnlockRequired[] = "UnlockRequired"; |
| 43 | const char CellularCapabilityGSM::kPropertyUnlockRetries[] = "UnlockRetries"; |
Darin Petkov | 1272a43 | 2011-11-10 15:53:37 +0100 | [diff] [blame] | 44 | |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 45 | CellularCapabilityGSM::CellularCapabilityGSM(Cellular *cellular, |
| 46 | ProxyFactory *proxy_factory) |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 47 | : CellularCapabilityClassic(cellular, proxy_factory), |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 48 | weak_ptr_factory_(this), |
Darin Petkov | 184c54e | 2011-11-15 12:44:39 +0100 | [diff] [blame] | 49 | registration_state_(MM_MODEM_GSM_NETWORK_REG_STATUS_UNKNOWN), |
Darin Petkov | ae0c64e | 2011-11-15 15:50:27 +0100 | [diff] [blame] | 50 | access_technology_(MM_MODEM_GSM_ACCESS_TECH_UNKNOWN), |
Darin Petkov | 0a4dfeb | 2011-11-18 19:36:13 +0100 | [diff] [blame] | 51 | home_provider_(NULL), |
Darin Petkov | 1272a43 | 2011-11-10 15:53:37 +0100 | [diff] [blame] | 52 | scanning_(false), |
| 53 | scan_interval_(0) { |
Darin Petkov | 5f316f6 | 2011-11-18 12:10:26 +0100 | [diff] [blame] | 54 | VLOG(2) << "Cellular capability constructed: GSM"; |
Darin Petkov | 1272a43 | 2011-11-10 15:53:37 +0100 | [diff] [blame] | 55 | PropertyStore *store = cellular->mutable_store(); |
Darin Petkov | 184c54e | 2011-11-15 12:44:39 +0100 | [diff] [blame] | 56 | store->RegisterConstString(flimflam::kSelectedNetworkProperty, |
| 57 | &selected_network_); |
Darin Petkov | 1272a43 | 2011-11-10 15:53:37 +0100 | [diff] [blame] | 58 | store->RegisterConstStringmaps(flimflam::kFoundNetworksProperty, |
| 59 | &found_networks_); |
| 60 | store->RegisterConstBool(flimflam::kScanningProperty, &scanning_); |
| 61 | store->RegisterUint16(flimflam::kScanIntervalProperty, &scan_interval_); |
Darin Petkov | 63138a9 | 2012-02-06 14:09:15 +0100 | [diff] [blame] | 62 | HelpRegisterDerivedKeyValueStore( |
| 63 | flimflam::kSIMLockStatusProperty, |
| 64 | &CellularCapabilityGSM::SimLockStatusToProperty, |
| 65 | NULL); |
Darin Petkov | 3cfbf21 | 2011-11-21 16:02:09 +0100 | [diff] [blame] | 66 | store->RegisterConstStringmaps(flimflam::kCellularApnListProperty, |
| 67 | &apn_list_); |
Darin Petkov | 0d06f7d | 2012-02-03 13:08:19 +0100 | [diff] [blame] | 68 | scanning_supported_ = true; |
Darin Petkov | 1272a43 | 2011-11-10 15:53:37 +0100 | [diff] [blame] | 69 | } |
Darin Petkov | daf4386 | 2011-10-27 11:37:28 +0200 | [diff] [blame] | 70 | |
Darin Petkov | 63138a9 | 2012-02-06 14:09:15 +0100 | [diff] [blame] | 71 | KeyValueStore CellularCapabilityGSM::SimLockStatusToProperty(Error */*error*/) { |
| 72 | KeyValueStore status; |
| 73 | status.SetBool(flimflam::kSIMLockEnabledProperty, sim_lock_status_.enabled); |
| 74 | status.SetString(flimflam::kSIMLockTypeProperty, sim_lock_status_.lock_type); |
| 75 | status.SetUint(flimflam::kSIMLockRetriesLeftProperty, |
| 76 | sim_lock_status_.retries_left); |
| 77 | return status; |
Darin Petkov | 721ac93 | 2011-11-16 15:43:09 +0100 | [diff] [blame] | 78 | } |
| 79 | |
Darin Petkov | 63138a9 | 2012-02-06 14:09:15 +0100 | [diff] [blame] | 80 | void CellularCapabilityGSM::HelpRegisterDerivedKeyValueStore( |
Darin Petkov | 721ac93 | 2011-11-16 15:43:09 +0100 | [diff] [blame] | 81 | const string &name, |
Darin Petkov | 63138a9 | 2012-02-06 14:09:15 +0100 | [diff] [blame] | 82 | KeyValueStore(CellularCapabilityGSM::*get)(Error *error), |
| 83 | void(CellularCapabilityGSM::*set)( |
| 84 | const KeyValueStore &value, Error *error)) { |
| 85 | cellular()->mutable_store()->RegisterDerivedKeyValueStore( |
Darin Petkov | 721ac93 | 2011-11-16 15:43:09 +0100 | [diff] [blame] | 86 | name, |
Darin Petkov | 63138a9 | 2012-02-06 14:09:15 +0100 | [diff] [blame] | 87 | KeyValueStoreAccessor( |
| 88 | new CustomAccessor<CellularCapabilityGSM, KeyValueStore>( |
Darin Petkov | 721ac93 | 2011-11-16 15:43:09 +0100 | [diff] [blame] | 89 | this, get, set))); |
| 90 | } |
| 91 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 92 | void CellularCapabilityGSM::InitProxies() { |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 93 | CellularCapabilityClassic::InitProxies(); |
Darin Petkov | cb54773 | 2011-11-09 13:55:26 +0100 | [diff] [blame] | 94 | card_proxy_.reset( |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 95 | proxy_factory()->CreateModemGSMCardProxy(cellular()->dbus_path(), |
Darin Petkov | daf4386 | 2011-10-27 11:37:28 +0200 | [diff] [blame] | 96 | cellular()->dbus_owner())); |
Darin Petkov | 184c54e | 2011-11-15 12:44:39 +0100 | [diff] [blame] | 97 | network_proxy_.reset( |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 98 | proxy_factory()->CreateModemGSMNetworkProxy(cellular()->dbus_path(), |
Darin Petkov | daf4386 | 2011-10-27 11:37:28 +0200 | [diff] [blame] | 99 | cellular()->dbus_owner())); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 100 | network_proxy_->set_signal_quality_callback( |
| 101 | Bind(&CellularCapabilityGSM::OnSignalQualitySignal, |
| 102 | weak_ptr_factory_.GetWeakPtr())); |
| 103 | network_proxy_->set_network_mode_callback( |
| 104 | Bind(&CellularCapabilityGSM::OnNetworkModeSignal, |
| 105 | weak_ptr_factory_.GetWeakPtr())); |
| 106 | network_proxy_->set_registration_info_callback( |
| 107 | Bind(&CellularCapabilityGSM::OnRegistrationInfoSignal, |
| 108 | weak_ptr_factory_.GetWeakPtr())); |
Darin Petkov | daf4386 | 2011-10-27 11:37:28 +0200 | [diff] [blame] | 109 | } |
| 110 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 111 | void CellularCapabilityGSM::StartModem(Error *error, |
| 112 | const ResultCallback &callback) { |
| 113 | InitProxies(); |
| 114 | |
| 115 | CellularTaskList *tasks = new CellularTaskList(); |
| 116 | ResultCallback cb = |
| 117 | Bind(&CellularCapabilityGSM::StepCompletedCallback, |
Thieu Le | 923006b | 2012-04-05 16:32:58 -0700 | [diff] [blame] | 118 | weak_ptr_factory_.GetWeakPtr(), callback, false, tasks); |
| 119 | ResultCallback cb_ignore_error = |
| 120 | Bind(&CellularCapabilityGSM::StepCompletedCallback, |
| 121 | weak_ptr_factory_.GetWeakPtr(), callback, true, tasks); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 122 | tasks->push_back(Bind(&CellularCapabilityGSM::EnableModem, |
| 123 | weak_ptr_factory_.GetWeakPtr(), cb)); |
| 124 | tasks->push_back(Bind(&CellularCapabilityGSM::Register, |
| 125 | weak_ptr_factory_.GetWeakPtr(), cb)); |
| 126 | tasks->push_back(Bind(&CellularCapabilityGSM::GetModemStatus, |
| 127 | weak_ptr_factory_.GetWeakPtr(), cb)); |
| 128 | tasks->push_back(Bind(&CellularCapabilityGSM::GetIMEI, |
| 129 | weak_ptr_factory_.GetWeakPtr(), cb)); |
| 130 | tasks->push_back(Bind(&CellularCapabilityGSM::GetIMSI, |
| 131 | weak_ptr_factory_.GetWeakPtr(), cb)); |
| 132 | tasks->push_back(Bind(&CellularCapabilityGSM::GetSPN, |
Thieu Le | 923006b | 2012-04-05 16:32:58 -0700 | [diff] [blame] | 133 | weak_ptr_factory_.GetWeakPtr(), cb_ignore_error)); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 134 | tasks->push_back(Bind(&CellularCapabilityGSM::GetMSISDN, |
Thieu Le | 923006b | 2012-04-05 16:32:58 -0700 | [diff] [blame] | 135 | weak_ptr_factory_.GetWeakPtr(), cb_ignore_error)); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 136 | tasks->push_back(Bind(&CellularCapabilityGSM::GetProperties, |
| 137 | weak_ptr_factory_.GetWeakPtr(), cb)); |
| 138 | tasks->push_back(Bind(&CellularCapabilityGSM::GetModemInfo, |
| 139 | weak_ptr_factory_.GetWeakPtr(), cb)); |
| 140 | tasks->push_back(Bind(&CellularCapabilityGSM::FinishEnable, |
| 141 | weak_ptr_factory_.GetWeakPtr(), cb)); |
| 142 | |
| 143 | RunNextStep(tasks); |
| 144 | } |
| 145 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 146 | void CellularCapabilityGSM::ReleaseProxies() { |
| 147 | VLOG(2) << __func__; |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 148 | CellularCapabilityClassic::ReleaseProxies(); |
Darin Petkov | 721ac93 | 2011-11-16 15:43:09 +0100 | [diff] [blame] | 149 | card_proxy_.reset(); |
| 150 | network_proxy_.reset(); |
| 151 | } |
| 152 | |
Darin Petkov | 5f316f6 | 2011-11-18 12:10:26 +0100 | [diff] [blame] | 153 | void CellularCapabilityGSM::OnServiceCreated() { |
Darin Petkov | 3133241 | 2012-01-28 01:50:02 +0100 | [diff] [blame] | 154 | // If IMSI is available, base the service's storage identifier on it. |
| 155 | if (!imsi_.empty()) { |
| 156 | cellular()->service()->SetStorageIdentifier( |
Darin Petkov | dd3e866 | 2012-02-03 13:16:20 +0100 | [diff] [blame] | 157 | string(flimflam::kTypeCellular) + "_" + |
| 158 | cellular()->address() + "_" + imsi_); |
Darin Petkov | 3133241 | 2012-01-28 01:50:02 +0100 | [diff] [blame] | 159 | } |
Darin Petkov | b9c9933 | 2012-01-12 13:13:00 +0100 | [diff] [blame] | 160 | cellular()->service()->SetActivationState( |
Darin Petkov | 5f316f6 | 2011-11-18 12:10:26 +0100 | [diff] [blame] | 161 | flimflam::kActivationStateActivated); |
| 162 | UpdateServingOperator(); |
| 163 | } |
| 164 | |
Darin Petkov | ae0c64e | 2011-11-15 15:50:27 +0100 | [diff] [blame] | 165 | void CellularCapabilityGSM::UpdateStatus(const DBusPropertiesMap &properties) { |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 166 | if (ContainsKey(properties, kPropertyIMSI)) { |
Darin Petkov | 0a4dfeb | 2011-11-18 19:36:13 +0100 | [diff] [blame] | 167 | SetHomeProvider(); |
Darin Petkov | ae0c64e | 2011-11-15 15:50:27 +0100 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | |
Eric Shienbrood | 30bc0ec | 2012-03-21 18:19:46 -0400 | [diff] [blame] | 171 | // Create the list of APNs to try, in the following order: |
| 172 | // - last APN that resulted in a successful connection attempt on the |
| 173 | // current network (if any) |
| 174 | // - the APN, if any, that was set by the user |
| 175 | // - the list of APNs found in the mobile broadband provider DB for the |
| 176 | // home provider associated with the current SIM |
| 177 | // - as a last resort, attempt to connect with no APN |
| 178 | void CellularCapabilityGSM::SetupApnTryList() { |
| 179 | apn_try_list_.clear(); |
| 180 | |
| 181 | DCHECK(cellular()->service().get()); |
| 182 | const Stringmap *apn_info = cellular()->service()->GetLastGoodApn(); |
| 183 | if (apn_info) |
| 184 | apn_try_list_.push_back(*apn_info); |
| 185 | |
| 186 | apn_info = cellular()->service()->GetUserSpecifiedApn(); |
| 187 | if (apn_info) |
| 188 | apn_try_list_.push_back(*apn_info); |
| 189 | |
| 190 | apn_try_list_.insert(apn_try_list_.end(), apn_list_.begin(), apn_list_.end()); |
| 191 | } |
| 192 | |
Darin Petkov | ae0c64e | 2011-11-15 15:50:27 +0100 | [diff] [blame] | 193 | void CellularCapabilityGSM::SetupConnectProperties( |
| 194 | DBusPropertiesMap *properties) { |
Eric Shienbrood | 30bc0ec | 2012-03-21 18:19:46 -0400 | [diff] [blame] | 195 | SetupApnTryList(); |
| 196 | FillConnectPropertyMap(properties); |
| 197 | } |
| 198 | |
| 199 | void CellularCapabilityGSM::FillConnectPropertyMap( |
| 200 | DBusPropertiesMap *properties) { |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 201 | (*properties)[kConnectPropertyPhoneNumber].writer().append_string( |
Darin Petkov | ae0c64e | 2011-11-15 15:50:27 +0100 | [diff] [blame] | 202 | kPhoneNumber); |
Eric Shienbrood | 30bc0ec | 2012-03-21 18:19:46 -0400 | [diff] [blame] | 203 | |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 204 | if (!AllowRoaming()) |
Eric Shienbrood | 30bc0ec | 2012-03-21 18:19:46 -0400 | [diff] [blame] | 205 | (*properties)[kConnectPropertyHomeOnly].writer().append_bool(true); |
| 206 | |
| 207 | if (!apn_try_list_.empty()) { |
| 208 | // Leave the APN at the front of the list, so that it can be recorded |
| 209 | // if the connect attempt succeeds. |
| 210 | Stringmap apn_info = apn_try_list_.front(); |
| 211 | VLOG(2) << __func__ << ": Using APN " << apn_info[flimflam::kApnProperty]; |
| 212 | (*properties)[kConnectPropertyApn].writer().append_string( |
| 213 | apn_info[flimflam::kApnProperty].c_str()); |
| 214 | if (ContainsKey(apn_info, flimflam::kApnUsernameProperty)) |
| 215 | (*properties)[kConnectPropertyApnUsername].writer().append_string( |
| 216 | apn_info[flimflam::kApnUsernameProperty].c_str()); |
| 217 | if (ContainsKey(apn_info, flimflam::kApnPasswordProperty)) |
| 218 | (*properties)[kConnectPropertyApnPassword].writer().append_string( |
| 219 | apn_info[flimflam::kApnPasswordProperty].c_str()); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | void CellularCapabilityGSM::OnConnectReply(const ResultCallback &callback, |
| 224 | const Error &error) { |
| 225 | if (error.IsFailure()) { |
| 226 | cellular()->service()->ClearLastGoodApn(); |
| 227 | // The APN that was just tried (and failed) is still at the |
| 228 | // front of the list, about to be removed. If the list is empty |
| 229 | // after that, try one last time without an APN. This may succeed |
| 230 | // with some modems in some cases. |
| 231 | if (error.type() == Error::kInvalidApn && !apn_try_list_.empty()) { |
| 232 | apn_try_list_.pop_front(); |
| 233 | VLOG(2) << "Connect failed with invalid APN, " << apn_try_list_.size() |
| 234 | << " remaining APNs to try"; |
| 235 | DBusPropertiesMap props; |
| 236 | FillConnectPropertyMap(&props); |
| 237 | Error error; |
| 238 | Connect(props, &error, callback); |
| 239 | return; |
| 240 | } |
| 241 | } else if (!apn_try_list_.empty()) { |
| 242 | cellular()->service()->SetLastGoodApn(apn_try_list_.front()); |
| 243 | apn_try_list_.clear(); |
| 244 | } |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 245 | CellularCapabilityClassic::OnConnectReply(callback, error); |
| 246 | } |
| 247 | |
| 248 | bool CellularCapabilityGSM::AllowRoaming() { |
| 249 | bool requires_roaming = |
| 250 | home_provider_ ? home_provider_->requires_roaming : false; |
| 251 | return requires_roaming || allow_roaming_property(); |
Darin Petkov | ae0c64e | 2011-11-15 15:50:27 +0100 | [diff] [blame] | 252 | } |
| 253 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 254 | // always called from an async context |
| 255 | void CellularCapabilityGSM::GetIMEI(const ResultCallback &callback) { |
Darin Petkov | cb54773 | 2011-11-09 13:55:26 +0100 | [diff] [blame] | 256 | VLOG(2) << __func__; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 257 | CHECK(!callback.is_null()); |
| 258 | Error error; |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 259 | if (imei_.empty()) { |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 260 | GSMIdentifierCallback cb = Bind(&CellularCapabilityGSM::OnGetIMEIReply, |
| 261 | weak_ptr_factory_.GetWeakPtr(), callback); |
| 262 | card_proxy_->GetIMEI(&error, cb, kTimeoutDefault); |
| 263 | if (error.IsFailure()) |
| 264 | callback.Run(error); |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 265 | } else { |
| 266 | VLOG(2) << "Already have IMEI " << imei_; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 267 | callback.Run(error); |
Darin Petkov | cb54773 | 2011-11-09 13:55:26 +0100 | [diff] [blame] | 268 | } |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 269 | } |
| 270 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 271 | // always called from an async context |
| 272 | void CellularCapabilityGSM::GetIMSI(const ResultCallback &callback) { |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 273 | VLOG(2) << __func__; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 274 | CHECK(!callback.is_null()); |
| 275 | Error error; |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 276 | if (imsi_.empty()) { |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 277 | GSMIdentifierCallback cb = Bind(&CellularCapabilityGSM::OnGetIMSIReply, |
| 278 | weak_ptr_factory_.GetWeakPtr(), |
| 279 | callback); |
| 280 | card_proxy_->GetIMSI(&error, cb, kTimeoutDefault); |
| 281 | if (error.IsFailure()) |
| 282 | callback.Run(error); |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 283 | } else { |
| 284 | VLOG(2) << "Already have IMSI " << imsi_; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 285 | callback.Run(error); |
Darin Petkov | cb54773 | 2011-11-09 13:55:26 +0100 | [diff] [blame] | 286 | } |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 287 | } |
| 288 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 289 | // always called from an async context |
| 290 | void CellularCapabilityGSM::GetSPN(const ResultCallback &callback) { |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 291 | VLOG(2) << __func__; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 292 | CHECK(!callback.is_null()); |
| 293 | Error error; |
Darin Petkov | ae0c64e | 2011-11-15 15:50:27 +0100 | [diff] [blame] | 294 | if (spn_.empty()) { |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 295 | GSMIdentifierCallback cb = Bind(&CellularCapabilityGSM::OnGetSPNReply, |
| 296 | weak_ptr_factory_.GetWeakPtr(), |
| 297 | callback); |
| 298 | card_proxy_->GetSPN(&error, cb, kTimeoutDefault); |
| 299 | if (error.IsFailure()) |
| 300 | callback.Run(error); |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 301 | } else { |
| 302 | VLOG(2) << "Already have SPN " << spn_; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 303 | callback.Run(error); |
Darin Petkov | cb54773 | 2011-11-09 13:55:26 +0100 | [diff] [blame] | 304 | } |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 305 | } |
| 306 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 307 | // always called from an async context |
| 308 | void CellularCapabilityGSM::GetMSISDN(const ResultCallback &callback) { |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 309 | VLOG(2) << __func__; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 310 | CHECK(!callback.is_null()); |
| 311 | Error error; |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 312 | if (mdn_.empty()) { |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 313 | GSMIdentifierCallback cb = Bind(&CellularCapabilityGSM::OnGetMSISDNReply, |
| 314 | weak_ptr_factory_.GetWeakPtr(), |
| 315 | callback); |
| 316 | card_proxy_->GetMSISDN(&error, cb, kTimeoutDefault); |
| 317 | if (error.IsFailure()) |
| 318 | callback.Run(error); |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 319 | } else { |
| 320 | VLOG(2) << "Already have MSISDN " << mdn_; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 321 | callback.Run(error); |
Darin Petkov | cb54773 | 2011-11-09 13:55:26 +0100 | [diff] [blame] | 322 | } |
| 323 | } |
| 324 | |
Darin Petkov | 3e50924 | 2011-11-10 14:46:44 +0100 | [diff] [blame] | 325 | void CellularCapabilityGSM::GetSignalQuality() { |
| 326 | VLOG(2) << __func__; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 327 | SignalQualityCallback callback = |
| 328 | Bind(&CellularCapabilityGSM::OnGetSignalQualityReply, |
| 329 | weak_ptr_factory_.GetWeakPtr()); |
| 330 | network_proxy_->GetSignalQuality(NULL, callback, kTimeoutDefault); |
Darin Petkov | 3e50924 | 2011-11-10 14:46:44 +0100 | [diff] [blame] | 331 | } |
| 332 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 333 | void CellularCapabilityGSM::GetRegistrationState() { |
Darin Petkov | 184c54e | 2011-11-15 12:44:39 +0100 | [diff] [blame] | 334 | VLOG(2) << __func__; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 335 | RegistrationInfoCallback callback = |
| 336 | Bind(&CellularCapabilityGSM::OnGetRegistrationInfoReply, |
| 337 | weak_ptr_factory_.GetWeakPtr()); |
| 338 | network_proxy_->GetRegistrationInfo(NULL, callback, kTimeoutDefault); |
Darin Petkov | 184c54e | 2011-11-15 12:44:39 +0100 | [diff] [blame] | 339 | } |
| 340 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 341 | void CellularCapabilityGSM::GetProperties(const ResultCallback &callback) { |
Darin Petkov | 184c54e | 2011-11-15 12:44:39 +0100 | [diff] [blame] | 342 | VLOG(2) << __func__; |
Darin Petkov | 63138a9 | 2012-02-06 14:09:15 +0100 | [diff] [blame] | 343 | |
Darin Petkov | 184c54e | 2011-11-15 12:44:39 +0100 | [diff] [blame] | 344 | // TODO(petkov): Switch to asynchronous calls (crosbug.com/17583). |
| 345 | uint32 tech = network_proxy_->AccessTechnology(); |
Darin Petkov | ae0c64e | 2011-11-15 15:50:27 +0100 | [diff] [blame] | 346 | SetAccessTechnology(tech); |
Darin Petkov | 184c54e | 2011-11-15 12:44:39 +0100 | [diff] [blame] | 347 | VLOG(2) << "GSM AccessTechnology: " << tech; |
Darin Petkov | 63138a9 | 2012-02-06 14:09:15 +0100 | [diff] [blame] | 348 | |
| 349 | // TODO(petkov): Switch to asynchronous calls (crosbug.com/17583). |
| 350 | uint32 locks = card_proxy_->EnabledFacilityLocks(); |
| 351 | sim_lock_status_.enabled = locks & MM_MODEM_GSM_FACILITY_SIM; |
| 352 | VLOG(2) << "GSM EnabledFacilityLocks: " << locks; |
| 353 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 354 | callback.Run(Error()); |
Darin Petkov | 184c54e | 2011-11-15 12:44:39 +0100 | [diff] [blame] | 355 | } |
| 356 | |
Darin Petkov | ac635a8 | 2012-01-10 16:51:58 +0100 | [diff] [blame] | 357 | string CellularCapabilityGSM::CreateFriendlyServiceName() { |
| 358 | VLOG(2) << __func__; |
| 359 | if (registration_state_ == MM_MODEM_GSM_NETWORK_REG_STATUS_HOME && |
| 360 | !cellular()->home_provider().GetName().empty()) { |
| 361 | return cellular()->home_provider().GetName(); |
| 362 | } |
| 363 | if (!serving_operator_.GetName().empty()) { |
| 364 | return serving_operator_.GetName(); |
| 365 | } |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 366 | if (!carrier_.empty()) { |
| 367 | return carrier_; |
Darin Petkov | ac635a8 | 2012-01-10 16:51:58 +0100 | [diff] [blame] | 368 | } |
| 369 | if (!serving_operator_.GetCode().empty()) { |
| 370 | return "cellular_" + serving_operator_.GetCode(); |
| 371 | } |
| 372 | return base::StringPrintf("GSMNetwork%u", friendly_service_name_id_++); |
| 373 | } |
| 374 | |
Darin Petkov | 0a4dfeb | 2011-11-18 19:36:13 +0100 | [diff] [blame] | 375 | void CellularCapabilityGSM::SetHomeProvider() { |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 376 | VLOG(2) << __func__ << "(IMSI: " << imsi_ |
Darin Petkov | 0a4dfeb | 2011-11-18 19:36:13 +0100 | [diff] [blame] | 377 | << " SPN: " << spn_ << ")"; |
| 378 | // TODO(petkov): The test for NULL provider_db should be done by |
| 379 | // mobile_provider_lookup_best_match. |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 380 | if (imsi_.empty() || !cellular()->provider_db()) { |
Darin Petkov | 0a4dfeb | 2011-11-18 19:36:13 +0100 | [diff] [blame] | 381 | return; |
| 382 | } |
| 383 | mobile_provider *provider = |
| 384 | mobile_provider_lookup_best_match( |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 385 | cellular()->provider_db(), spn_.c_str(), imsi_.c_str()); |
Darin Petkov | 0a4dfeb | 2011-11-18 19:36:13 +0100 | [diff] [blame] | 386 | if (!provider) { |
| 387 | VLOG(2) << "GSM provider not found."; |
| 388 | return; |
| 389 | } |
| 390 | home_provider_ = provider; |
| 391 | Cellular::Operator oper; |
| 392 | if (provider->networks) { |
| 393 | oper.SetCode(provider->networks[0]); |
| 394 | } |
| 395 | if (provider->country) { |
| 396 | oper.SetCountry(provider->country); |
| 397 | } |
| 398 | if (spn_.empty()) { |
| 399 | const char *name = mobile_provider_get_name(provider); |
| 400 | if (name) { |
| 401 | oper.SetName(name); |
| 402 | } |
| 403 | } else { |
| 404 | oper.SetName(spn_); |
| 405 | } |
| 406 | cellular()->set_home_provider(oper); |
Darin Petkov | 3cfbf21 | 2011-11-21 16:02:09 +0100 | [diff] [blame] | 407 | InitAPNList(); |
Darin Petkov | 0a4dfeb | 2011-11-18 19:36:13 +0100 | [diff] [blame] | 408 | } |
| 409 | |
Darin Petkov | ae0c64e | 2011-11-15 15:50:27 +0100 | [diff] [blame] | 410 | void CellularCapabilityGSM::UpdateOperatorInfo() { |
| 411 | VLOG(2) << __func__; |
Darin Petkov | 0a4dfeb | 2011-11-18 19:36:13 +0100 | [diff] [blame] | 412 | const string &network_id = serving_operator_.GetCode(); |
| 413 | if (!network_id.empty()) { |
| 414 | VLOG(2) << "Looking up network id: " << network_id; |
Darin Petkov | ae0c64e | 2011-11-15 15:50:27 +0100 | [diff] [blame] | 415 | mobile_provider *provider = |
| 416 | mobile_provider_lookup_by_network(cellular()->provider_db(), |
Darin Petkov | 0a4dfeb | 2011-11-18 19:36:13 +0100 | [diff] [blame] | 417 | network_id.c_str()); |
Darin Petkov | ae0c64e | 2011-11-15 15:50:27 +0100 | [diff] [blame] | 418 | if (provider) { |
| 419 | const char *provider_name = mobile_provider_get_name(provider); |
| 420 | if (provider_name && *provider_name) { |
Darin Petkov | 0a4dfeb | 2011-11-18 19:36:13 +0100 | [diff] [blame] | 421 | serving_operator_.SetName(provider_name); |
| 422 | if (provider->country) { |
| 423 | serving_operator_.SetCountry(provider->country); |
| 424 | } |
| 425 | VLOG(2) << "Operator name: " << serving_operator_.GetName() |
| 426 | << ", country: " << serving_operator_.GetCountry(); |
Darin Petkov | ae0c64e | 2011-11-15 15:50:27 +0100 | [diff] [blame] | 427 | } |
| 428 | } else { |
| 429 | VLOG(2) << "GSM provider not found."; |
| 430 | } |
| 431 | } |
| 432 | UpdateServingOperator(); |
| 433 | } |
| 434 | |
| 435 | void CellularCapabilityGSM::UpdateServingOperator() { |
| 436 | VLOG(2) << __func__; |
Darin Petkov | 0a4dfeb | 2011-11-18 19:36:13 +0100 | [diff] [blame] | 437 | if (cellular()->service().get()) { |
Darin Petkov | 9cb0268 | 2012-01-28 00:17:38 +0100 | [diff] [blame] | 438 | cellular()->service()->SetServingOperator(serving_operator_); |
Darin Petkov | ae0c64e | 2011-11-15 15:50:27 +0100 | [diff] [blame] | 439 | } |
Darin Petkov | ae0c64e | 2011-11-15 15:50:27 +0100 | [diff] [blame] | 440 | } |
| 441 | |
Darin Petkov | 3cfbf21 | 2011-11-21 16:02:09 +0100 | [diff] [blame] | 442 | void CellularCapabilityGSM::InitAPNList() { |
| 443 | VLOG(2) << __func__; |
| 444 | if (!home_provider_) { |
| 445 | return; |
| 446 | } |
| 447 | apn_list_.clear(); |
| 448 | for (int i = 0; i < home_provider_->num_apns; ++i) { |
| 449 | Stringmap props; |
| 450 | mobile_apn *apn = home_provider_->apns[i]; |
| 451 | if (apn->value) { |
| 452 | props[flimflam::kApnProperty] = apn->value; |
| 453 | } |
| 454 | if (apn->username) { |
| 455 | props[flimflam::kApnUsernameProperty] = apn->username; |
| 456 | } |
| 457 | if (apn->password) { |
| 458 | props[flimflam::kApnPasswordProperty] = apn->password; |
| 459 | } |
| 460 | // Find the first localized and non-localized name, if any. |
| 461 | const localized_name *lname = NULL; |
| 462 | const localized_name *name = NULL; |
| 463 | for (int j = 0; j < apn->num_names; ++j) { |
| 464 | if (apn->names[j]->lang) { |
| 465 | if (!lname) { |
| 466 | lname = apn->names[j]; |
| 467 | } |
| 468 | } else if (!name) { |
| 469 | name = apn->names[j]; |
| 470 | } |
| 471 | } |
| 472 | if (name) { |
| 473 | props[flimflam::kApnNameProperty] = name->name; |
| 474 | } |
| 475 | if (lname) { |
| 476 | props[flimflam::kApnLocalizedNameProperty] = lname->name; |
| 477 | props[flimflam::kApnLanguageProperty] = lname->lang; |
| 478 | } |
| 479 | apn_list_.push_back(props); |
| 480 | } |
| 481 | cellular()->adaptor()->EmitStringmapsChanged( |
| 482 | flimflam::kCellularApnListProperty, apn_list_); |
| 483 | } |
| 484 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 485 | // always called from an async context |
| 486 | void CellularCapabilityGSM::Register(const ResultCallback &callback) { |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 487 | VLOG(2) << __func__ << " \"" << selected_network_ << "\""; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 488 | CHECK(!callback.is_null()); |
| 489 | Error error; |
| 490 | ResultCallback cb = Bind(&CellularCapabilityGSM::OnRegisterReply, |
| 491 | weak_ptr_factory_.GetWeakPtr(), callback); |
| 492 | network_proxy_->Register(selected_network_, &error, cb, kTimeoutRegister); |
| 493 | if (error.IsFailure()) |
| 494 | callback.Run(error); |
Darin Petkov | 184c54e | 2011-11-15 12:44:39 +0100 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | void CellularCapabilityGSM::RegisterOnNetwork( |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 498 | const string &network_id, |
| 499 | Error *error, |
| 500 | const ResultCallback &callback) { |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 501 | VLOG(2) << __func__ << "(" << network_id << ")"; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 502 | CHECK(error); |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 503 | desired_network_ = network_id; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 504 | ResultCallback cb = Bind(&CellularCapabilityGSM::OnRegisterReply, |
| 505 | weak_ptr_factory_.GetWeakPtr(), callback); |
| 506 | network_proxy_->Register(network_id, error, cb, kTimeoutRegister); |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 507 | } |
| 508 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 509 | void CellularCapabilityGSM::OnRegisterReply(const ResultCallback &callback, |
| 510 | const Error &error) { |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 511 | VLOG(2) << __func__ << "(" << error << ")"; |
| 512 | |
| 513 | if (error.IsSuccess()) { |
| 514 | selected_network_ = desired_network_; |
| 515 | desired_network_.clear(); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 516 | callback.Run(error); |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 517 | return; |
| 518 | } |
| 519 | // If registration on the desired network failed, |
| 520 | // try to register on the home network. |
| 521 | if (!desired_network_.empty()) { |
| 522 | desired_network_.clear(); |
| 523 | selected_network_.clear(); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 524 | LOG(INFO) << "Couldn't register on selected network, trying home network"; |
| 525 | Register(callback); |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 526 | return; |
| 527 | } |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 528 | callback.Run(error); |
Darin Petkov | 184c54e | 2011-11-15 12:44:39 +0100 | [diff] [blame] | 529 | } |
| 530 | |
Darin Petkov | b72cf40 | 2011-11-22 14:51:39 +0100 | [diff] [blame] | 531 | bool CellularCapabilityGSM::IsRegistered() { |
| 532 | return (registration_state_ == MM_MODEM_GSM_NETWORK_REG_STATUS_HOME || |
| 533 | registration_state_ == MM_MODEM_GSM_NETWORK_REG_STATUS_ROAMING); |
| 534 | } |
| 535 | |
Darin Petkov | b05315f | 2011-11-07 10:14:25 +0100 | [diff] [blame] | 536 | void CellularCapabilityGSM::RequirePIN( |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 537 | const std::string &pin, bool require, |
| 538 | Error *error, const ResultCallback &callback) { |
| 539 | CHECK(error); |
| 540 | card_proxy_->EnablePIN(pin, require, error, callback, kTimeoutDefault); |
Darin Petkov | b05315f | 2011-11-07 10:14:25 +0100 | [diff] [blame] | 541 | } |
| 542 | |
Darin Petkov | e5bc2cb | 2011-12-07 14:47:32 +0100 | [diff] [blame] | 543 | void CellularCapabilityGSM::EnterPIN(const string &pin, |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 544 | Error *error, |
| 545 | const ResultCallback &callback) { |
| 546 | CHECK(error); |
| 547 | card_proxy_->SendPIN(pin, error, callback, kTimeoutDefault); |
Darin Petkov | b05315f | 2011-11-07 10:14:25 +0100 | [diff] [blame] | 548 | } |
| 549 | |
Darin Petkov | c64fe5e | 2012-01-11 12:46:13 +0100 | [diff] [blame] | 550 | void CellularCapabilityGSM::UnblockPIN(const string &unblock_code, |
| 551 | const string &pin, |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 552 | Error *error, |
| 553 | const ResultCallback &callback) { |
| 554 | CHECK(error); |
| 555 | card_proxy_->SendPUK(unblock_code, pin, error, callback, kTimeoutDefault); |
Darin Petkov | b05315f | 2011-11-07 10:14:25 +0100 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | void CellularCapabilityGSM::ChangePIN( |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 559 | const string &old_pin, const string &new_pin, |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 560 | Error *error, const ResultCallback &callback) { |
| 561 | CHECK(error); |
| 562 | card_proxy_->ChangePIN(old_pin, new_pin, error, callback, kTimeoutDefault); |
Darin Petkov | b05315f | 2011-11-07 10:14:25 +0100 | [diff] [blame] | 563 | } |
| 564 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 565 | void CellularCapabilityGSM::Scan(Error *error, const ResultCallback &callback) { |
Darin Petkov | 1272a43 | 2011-11-10 15:53:37 +0100 | [diff] [blame] | 566 | VLOG(2) << __func__; |
| 567 | // TODO(petkov): Defer scan requests if a scan is in progress already. |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 568 | CHECK(error); |
| 569 | ScanResultsCallback cb = Bind(&CellularCapabilityGSM::OnScanReply, |
| 570 | weak_ptr_factory_.GetWeakPtr(), callback); |
| 571 | network_proxy_->Scan(error, cb, kTimeoutScan); |
Darin Petkov | 1272a43 | 2011-11-10 15:53:37 +0100 | [diff] [blame] | 572 | } |
| 573 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 574 | void CellularCapabilityGSM::OnScanReply(const ResultCallback &callback, |
| 575 | const GSMScanResults &results, |
| 576 | const Error &error) { |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 577 | VLOG(2) << __func__; |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 578 | |
| 579 | // Error handling is weak. The current expectation is that on any |
| 580 | // error, found_networks_ should be cleared and a property change |
| 581 | // notification sent out. |
| 582 | // |
| 583 | // TODO(jglasgow): fix error handling |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 584 | found_networks_.clear(); |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 585 | if (!error.IsFailure()) { |
| 586 | for (GSMScanResults::const_iterator it = results.begin(); |
| 587 | it != results.end(); ++it) { |
| 588 | found_networks_.push_back(ParseScanResult(*it)); |
| 589 | } |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 590 | } |
Darin Petkov | b7828b0 | 2012-02-03 12:34:30 +0100 | [diff] [blame] | 591 | cellular()->adaptor()->EmitStringmapsChanged(flimflam::kFoundNetworksProperty, |
| 592 | found_networks_); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 593 | callback.Run(error); |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | Stringmap CellularCapabilityGSM::ParseScanResult(const GSMScanResult &result) { |
Darin Petkov | 1272a43 | 2011-11-10 15:53:37 +0100 | [diff] [blame] | 597 | Stringmap parsed; |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 598 | for (GSMScanResult::const_iterator it = result.begin(); |
| 599 | it != result.end(); ++it) { |
Darin Petkov | 1272a43 | 2011-11-10 15:53:37 +0100 | [diff] [blame] | 600 | // TODO(petkov): Define these in system_api/service_constants.h. The |
| 601 | // numerical values are taken from 3GPP TS 27.007 Section 7.3. |
| 602 | static const char * const kStatusString[] = { |
| 603 | "unknown", |
| 604 | "available", |
| 605 | "current", |
| 606 | "forbidden", |
| 607 | }; |
| 608 | static const char * const kTechnologyString[] = { |
| 609 | flimflam::kNetworkTechnologyGsm, |
| 610 | "GSM Compact", |
| 611 | flimflam::kNetworkTechnologyUmts, |
| 612 | flimflam::kNetworkTechnologyEdge, |
| 613 | "HSDPA", |
| 614 | "HSUPA", |
| 615 | flimflam::kNetworkTechnologyHspa, |
| 616 | }; |
| 617 | VLOG(2) << "Network property: " << it->first << " = " << it->second; |
| 618 | if (it->first == kNetworkPropertyStatus) { |
| 619 | int status = 0; |
| 620 | if (base::StringToInt(it->second, &status) && |
| 621 | status >= 0 && |
| 622 | status < static_cast<int>(arraysize(kStatusString))) { |
| 623 | parsed[flimflam::kStatusProperty] = kStatusString[status]; |
| 624 | } else { |
| 625 | LOG(ERROR) << "Unexpected status value: " << it->second; |
| 626 | } |
| 627 | } else if (it->first == kNetworkPropertyID) { |
| 628 | parsed[flimflam::kNetworkIdProperty] = it->second; |
| 629 | } else if (it->first == kNetworkPropertyLongName) { |
| 630 | parsed[flimflam::kLongNameProperty] = it->second; |
| 631 | } else if (it->first == kNetworkPropertyShortName) { |
| 632 | parsed[flimflam::kShortNameProperty] = it->second; |
| 633 | } else if (it->first == kNetworkPropertyAccessTechnology) { |
| 634 | int tech = 0; |
| 635 | if (base::StringToInt(it->second, &tech) && |
| 636 | tech >= 0 && |
| 637 | tech < static_cast<int>(arraysize(kTechnologyString))) { |
| 638 | parsed[flimflam::kTechnologyProperty] = kTechnologyString[tech]; |
| 639 | } else { |
| 640 | LOG(ERROR) << "Unexpected technology value: " << it->second; |
| 641 | } |
| 642 | } else { |
| 643 | LOG(WARNING) << "Unknown network property ignored: " << it->first; |
| 644 | } |
| 645 | } |
| 646 | // If the long name is not available but the network ID is, look up the long |
| 647 | // name in the mobile provider database. |
| 648 | if ((!ContainsKey(parsed, flimflam::kLongNameProperty) || |
| 649 | parsed[flimflam::kLongNameProperty].empty()) && |
| 650 | ContainsKey(parsed, flimflam::kNetworkIdProperty)) { |
| 651 | mobile_provider *provider = |
| 652 | mobile_provider_lookup_by_network( |
| 653 | cellular()->provider_db(), |
| 654 | parsed[flimflam::kNetworkIdProperty].c_str()); |
| 655 | if (provider) { |
| 656 | const char *long_name = mobile_provider_get_name(provider); |
| 657 | if (long_name && *long_name) { |
| 658 | parsed[flimflam::kLongNameProperty] = long_name; |
| 659 | } |
| 660 | } |
| 661 | } |
| 662 | return parsed; |
| 663 | } |
| 664 | |
Darin Petkov | ae0c64e | 2011-11-15 15:50:27 +0100 | [diff] [blame] | 665 | void CellularCapabilityGSM::SetAccessTechnology(uint32 access_technology) { |
| 666 | access_technology_ = access_technology; |
| 667 | if (cellular()->service().get()) { |
Darin Petkov | b72cf40 | 2011-11-22 14:51:39 +0100 | [diff] [blame] | 668 | cellular()->service()->SetNetworkTechnology(GetNetworkTechnologyString()); |
Darin Petkov | ae0c64e | 2011-11-15 15:50:27 +0100 | [diff] [blame] | 669 | } |
| 670 | } |
| 671 | |
Darin Petkov | 20c13ec | 2011-11-09 15:07:15 +0100 | [diff] [blame] | 672 | string CellularCapabilityGSM::GetNetworkTechnologyString() const { |
Darin Petkov | b72cf40 | 2011-11-22 14:51:39 +0100 | [diff] [blame] | 673 | switch (access_technology_) { |
| 674 | case MM_MODEM_GSM_ACCESS_TECH_GSM: |
| 675 | case MM_MODEM_GSM_ACCESS_TECH_GSM_COMPACT: |
| 676 | return flimflam::kNetworkTechnologyGsm; |
| 677 | case MM_MODEM_GSM_ACCESS_TECH_GPRS: |
| 678 | return flimflam::kNetworkTechnologyGprs; |
| 679 | case MM_MODEM_GSM_ACCESS_TECH_EDGE: |
| 680 | return flimflam::kNetworkTechnologyEdge; |
| 681 | case MM_MODEM_GSM_ACCESS_TECH_UMTS: |
| 682 | return flimflam::kNetworkTechnologyUmts; |
| 683 | case MM_MODEM_GSM_ACCESS_TECH_HSDPA: |
| 684 | case MM_MODEM_GSM_ACCESS_TECH_HSUPA: |
| 685 | case MM_MODEM_GSM_ACCESS_TECH_HSPA: |
| 686 | return flimflam::kNetworkTechnologyHspa; |
| 687 | case MM_MODEM_GSM_ACCESS_TECH_HSPA_PLUS: |
| 688 | return flimflam::kNetworkTechnologyHspaPlus; |
| 689 | default: |
| 690 | break; |
Darin Petkov | 20c13ec | 2011-11-09 15:07:15 +0100 | [diff] [blame] | 691 | } |
| 692 | return ""; |
| 693 | } |
| 694 | |
| 695 | string CellularCapabilityGSM::GetRoamingStateString() const { |
Darin Petkov | 184c54e | 2011-11-15 12:44:39 +0100 | [diff] [blame] | 696 | switch (registration_state_) { |
Darin Petkov | 20c13ec | 2011-11-09 15:07:15 +0100 | [diff] [blame] | 697 | case MM_MODEM_GSM_NETWORK_REG_STATUS_HOME: |
| 698 | return flimflam::kRoamingStateHome; |
| 699 | case MM_MODEM_GSM_NETWORK_REG_STATUS_ROAMING: |
| 700 | return flimflam::kRoamingStateRoaming; |
| 701 | default: |
| 702 | break; |
| 703 | } |
| 704 | return flimflam::kRoamingStateUnknown; |
| 705 | } |
| 706 | |
Darin Petkov | ae0c64e | 2011-11-15 15:50:27 +0100 | [diff] [blame] | 707 | void CellularCapabilityGSM::OnModemManagerPropertiesChanged( |
| 708 | const DBusPropertiesMap &properties) { |
| 709 | uint32 access_technology = MM_MODEM_GSM_ACCESS_TECH_UNKNOWN; |
| 710 | if (DBusProperties::GetUint32(properties, |
| 711 | kPropertyAccessTechnology, |
| 712 | &access_technology)) { |
| 713 | SetAccessTechnology(access_technology); |
| 714 | } |
Darin Petkov | 63138a9 | 2012-02-06 14:09:15 +0100 | [diff] [blame] | 715 | bool emit = false; |
| 716 | uint32 locks = 0; |
| 717 | if (DBusProperties::GetUint32( |
| 718 | properties, kPropertyEnabledFacilityLocks, &locks)) { |
| 719 | sim_lock_status_.enabled = locks & MM_MODEM_GSM_FACILITY_SIM; |
| 720 | emit = true; |
| 721 | } |
| 722 | if (DBusProperties::GetString( |
| 723 | properties, kPropertyUnlockRequired, &sim_lock_status_.lock_type)) { |
| 724 | emit = true; |
| 725 | } |
| 726 | if (DBusProperties::GetUint32( |
| 727 | properties, kPropertyUnlockRetries, &sim_lock_status_.retries_left)) { |
| 728 | emit = true; |
| 729 | } |
| 730 | if (emit) { |
| 731 | cellular()->adaptor()->EmitKeyValueStoreChanged( |
| 732 | flimflam::kSIMLockStatusProperty, SimLockStatusToProperty(NULL)); |
| 733 | } |
Darin Petkov | ae0c64e | 2011-11-15 15:50:27 +0100 | [diff] [blame] | 734 | } |
| 735 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 736 | void CellularCapabilityGSM::OnNetworkModeSignal(uint32 /*mode*/) { |
Darin Petkov | 184c54e | 2011-11-15 12:44:39 +0100 | [diff] [blame] | 737 | // TODO(petkov): Implement this. |
| 738 | NOTIMPLEMENTED(); |
| 739 | } |
| 740 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 741 | void CellularCapabilityGSM::OnRegistrationInfoSignal( |
| 742 | uint32 status, const string &operator_code, const string &operator_name) { |
| 743 | VLOG(2) << __func__ << ": regstate=" << status |
| 744 | << ", opercode=" << operator_code |
| 745 | << ", opername=" << operator_name; |
| 746 | registration_state_ = status; |
| 747 | serving_operator_.SetCode(operator_code); |
| 748 | serving_operator_.SetName(operator_name); |
| 749 | UpdateOperatorInfo(); |
| 750 | cellular()->HandleNewRegistrationState(); |
Darin Petkov | 184c54e | 2011-11-15 12:44:39 +0100 | [diff] [blame] | 751 | } |
| 752 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 753 | void CellularCapabilityGSM::OnSignalQualitySignal(uint32 quality) { |
Darin Petkov | 184c54e | 2011-11-15 12:44:39 +0100 | [diff] [blame] | 754 | cellular()->HandleNewSignalQuality(quality); |
| 755 | } |
| 756 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 757 | void CellularCapabilityGSM::OnGetRegistrationInfoReply( |
| 758 | uint32 status, const string &operator_code, const string &operator_name, |
| 759 | const Error &error) { |
| 760 | if (error.IsSuccess()) |
| 761 | OnRegistrationInfoSignal(status, operator_code, operator_name); |
| 762 | } |
| 763 | |
| 764 | void CellularCapabilityGSM::OnGetSignalQualityReply(uint32 quality, |
| 765 | const Error &error) { |
| 766 | if (error.IsSuccess()) |
| 767 | OnSignalQualitySignal(quality); |
| 768 | } |
| 769 | |
| 770 | void CellularCapabilityGSM::OnGetIMEIReply(const ResultCallback &callback, |
| 771 | const string &imei, |
| 772 | const Error &error) { |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 773 | if (error.IsSuccess()) { |
| 774 | VLOG(2) << "IMEI: " << imei; |
| 775 | imei_ = imei; |
| 776 | } else { |
| 777 | VLOG(2) << "GetIMEI failed - " << error; |
| 778 | } |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 779 | callback.Run(error); |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 780 | } |
| 781 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 782 | void CellularCapabilityGSM::OnGetIMSIReply(const ResultCallback &callback, |
| 783 | const string &imsi, |
| 784 | const Error &error) { |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 785 | if (error.IsSuccess()) { |
| 786 | VLOG(2) << "IMSI: " << imsi; |
| 787 | imsi_ = imsi; |
| 788 | SetHomeProvider(); |
| 789 | } else { |
| 790 | VLOG(2) << "GetIMSI failed - " << error; |
| 791 | } |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 792 | callback.Run(error); |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 793 | } |
| 794 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 795 | void CellularCapabilityGSM::OnGetSPNReply(const ResultCallback &callback, |
| 796 | const string &spn, |
| 797 | const Error &error) { |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 798 | if (error.IsSuccess()) { |
| 799 | VLOG(2) << "SPN: " << spn; |
| 800 | spn_ = spn; |
| 801 | SetHomeProvider(); |
| 802 | } else { |
| 803 | VLOG(2) << "GetSPN failed - " << error; |
| 804 | } |
Thieu Le | 923006b | 2012-04-05 16:32:58 -0700 | [diff] [blame] | 805 | callback.Run(error); |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 806 | } |
| 807 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 808 | void CellularCapabilityGSM::OnGetMSISDNReply(const ResultCallback &callback, |
| 809 | const string &msisdn, |
| 810 | const Error &error) { |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 811 | if (error.IsSuccess()) { |
| 812 | VLOG(2) << "MSISDN: " << msisdn; |
| 813 | mdn_ = msisdn; |
| 814 | } else { |
| 815 | VLOG(2) << "GetMSISDN failed - " << error; |
| 816 | } |
Thieu Le | 923006b | 2012-04-05 16:32:58 -0700 | [diff] [blame] | 817 | callback.Run(error); |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 818 | } |
| 819 | |
Darin Petkov | daf4386 | 2011-10-27 11:37:28 +0200 | [diff] [blame] | 820 | } // namespace shill |