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