mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [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_service.h" |
| 6 | |
| 7 | #include <string> |
| 8 | |
| 9 | #include <base/logging.h> |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 10 | #include <base/stringprintf.h> |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 11 | #include <chromeos/dbus/service_constants.h> |
| 12 | |
Darin Petkov | b72cf40 | 2011-11-22 14:51:39 +0100 | [diff] [blame] | 13 | #include "shill/adaptor_interfaces.h" |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 14 | #include "shill/cellular.h" |
Eric Shienbrood | 30bc0ec | 2012-03-21 18:19:46 -0400 | [diff] [blame] | 15 | #include "shill/property_accessor.h" |
| 16 | #include "shill/store_interface.h" |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 17 | |
| 18 | using std::string; |
| 19 | |
| 20 | namespace shill { |
Darin Petkov | c5f5656 | 2011-08-06 16:40:05 -0700 | [diff] [blame] | 21 | |
Eric Shienbrood | 30bc0ec | 2012-03-21 18:19:46 -0400 | [diff] [blame] | 22 | const char CellularService::kStorageAPN[] = "Cellular.APN"; |
| 23 | const char CellularService::kStorageLastGoodAPN[] = "Cellular.LastGoodAPN"; |
| 24 | |
Darin Petkov | 381928f | 2012-02-02 23:00:12 +0100 | [diff] [blame] | 25 | // TODO(petkov): Add these to system_api/dbus/service_constants.h |
| 26 | namespace { |
| 27 | const char kKeyOLPURL[] = "url"; |
| 28 | const char kKeyOLPMethod[] = "method"; |
| 29 | const char kKeyOLPPostData[] = "postdata"; |
| 30 | } // namespace {} |
| 31 | |
Eric Shienbrood | 30bc0ec | 2012-03-21 18:19:46 -0400 | [diff] [blame] | 32 | static bool GetNonEmptyField(const Stringmap &stringmap, |
| 33 | const string &fieldname, |
| 34 | string *value) { |
| 35 | Stringmap::const_iterator it = stringmap.find(fieldname); |
| 36 | if (it != stringmap.end() && !it->second.empty()) { |
| 37 | *value = it->second; |
| 38 | return true; |
| 39 | } |
| 40 | return false; |
| 41 | } |
| 42 | |
Darin Petkov | 381928f | 2012-02-02 23:00:12 +0100 | [diff] [blame] | 43 | CellularService::OLP::OLP() { |
| 44 | SetURL(""); |
| 45 | SetMethod(""); |
| 46 | SetPostData(""); |
| 47 | } |
| 48 | |
| 49 | CellularService::OLP::~OLP() {} |
| 50 | |
| 51 | void CellularService::OLP::CopyFrom(const OLP &olp) { |
| 52 | dict_ = olp.dict_; |
| 53 | } |
| 54 | |
| 55 | bool CellularService::OLP::Equals(const OLP &olp) const { |
| 56 | return dict_ == olp.dict_; |
| 57 | } |
| 58 | |
| 59 | const string &CellularService::OLP::GetURL() const { |
| 60 | return dict_.find(kKeyOLPURL)->second; |
| 61 | } |
| 62 | |
| 63 | void CellularService::OLP::SetURL(const string &url) { |
| 64 | dict_[kKeyOLPURL] = url; |
| 65 | } |
| 66 | |
| 67 | const string &CellularService::OLP::GetMethod() const { |
| 68 | return dict_.find(kKeyOLPMethod)->second; |
| 69 | } |
| 70 | |
| 71 | void CellularService::OLP::SetMethod(const string &method) { |
| 72 | dict_[kKeyOLPMethod] = method; |
| 73 | } |
| 74 | |
| 75 | const string &CellularService::OLP::GetPostData() const { |
| 76 | return dict_.find(kKeyOLPPostData)->second; |
| 77 | } |
| 78 | |
| 79 | void CellularService::OLP::SetPostData(const string &post_data) { |
| 80 | dict_[kKeyOLPPostData] = post_data; |
| 81 | } |
| 82 | |
| 83 | const Stringmap &CellularService::OLP::ToDict() const { |
| 84 | return dict_; |
| 85 | } |
| 86 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 87 | CellularService::CellularService(ControlInterface *control_interface, |
| 88 | EventDispatcher *dispatcher, |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 89 | Metrics *metrics, |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 90 | Manager *manager, |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 91 | const CellularRefPtr &device) |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 92 | : Service(control_interface, dispatcher, metrics, manager, |
| 93 | Technology::kCellular), |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 94 | cellular_(device) { |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 95 | PropertyStore *store = this->mutable_store(); |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 96 | store->RegisterConstString(flimflam::kActivationStateProperty, |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 97 | &activation_state_); |
Eric Shienbrood | 30bc0ec | 2012-03-21 18:19:46 -0400 | [diff] [blame] | 98 | HelpRegisterDerivedStringmap(flimflam::kCellularApnProperty, |
| 99 | &CellularService::GetApn, |
| 100 | &CellularService::SetApn); |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 101 | store->RegisterConstStringmap(flimflam::kCellularLastGoodApnProperty, |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 102 | &last_good_apn_info_); |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 103 | store->RegisterConstString(flimflam::kNetworkTechnologyProperty, |
Darin Petkov | b72cf40 | 2011-11-22 14:51:39 +0100 | [diff] [blame] | 104 | &network_technology_); |
Darin Petkov | 381928f | 2012-02-02 23:00:12 +0100 | [diff] [blame] | 105 | store->RegisterConstStringmap(flimflam::kPaymentPortalProperty, |
| 106 | &olp_.ToDict()); |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 107 | store->RegisterConstString(flimflam::kRoamingStateProperty, &roaming_state_); |
| 108 | store->RegisterConstStringmap(flimflam::kServingOperatorProperty, |
Darin Petkov | 3335b37 | 2011-08-22 11:05:32 -0700 | [diff] [blame] | 109 | &serving_operator_.ToDict()); |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 110 | store->RegisterConstString(flimflam::kUsageURLProperty, &usage_url_); |
Darin Petkov | ac635a8 | 2012-01-10 16:51:58 +0100 | [diff] [blame] | 111 | |
| 112 | set_friendly_name(device->CreateFriendlyServiceName()); |
Darin Petkov | dd3e866 | 2012-02-03 13:16:20 +0100 | [diff] [blame] | 113 | SetStorageIdentifier(string(flimflam::kTypeCellular) + "_" + |
| 114 | device->address() + "_" + friendly_name()); |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | CellularService::~CellularService() { } |
| 118 | |
Eric Shienbrood | 30bc0ec | 2012-03-21 18:19:46 -0400 | [diff] [blame] | 119 | void CellularService::HelpRegisterDerivedStringmap( |
| 120 | const string &name, |
| 121 | Stringmap(CellularService::*get)(Error *error), |
| 122 | void(CellularService::*set)( |
| 123 | const Stringmap &value, Error *error)) { |
| 124 | mutable_store()->RegisterDerivedStringmap( |
| 125 | name, |
| 126 | StringmapAccessor( |
| 127 | new CustomAccessor<CellularService, Stringmap>(this, get, set))); |
| 128 | } |
| 129 | |
| 130 | Stringmap *CellularService::GetUserSpecifiedApn() { |
| 131 | Stringmap::iterator it = apn_info_.find(flimflam::kApnProperty); |
| 132 | if (it == apn_info_.end() || it->second.empty()) |
| 133 | return NULL; |
| 134 | return &apn_info_; |
| 135 | } |
| 136 | |
| 137 | Stringmap *CellularService::GetLastGoodApn() { |
| 138 | Stringmap::iterator it = |
| 139 | last_good_apn_info_.find(flimflam::kApnProperty); |
| 140 | if (it == last_good_apn_info_.end() || it->second.empty()) |
| 141 | return NULL; |
| 142 | return &last_good_apn_info_; |
| 143 | } |
| 144 | |
| 145 | Stringmap CellularService::GetApn(Error */*error*/) { |
| 146 | return apn_info_; |
| 147 | } |
| 148 | |
| 149 | void CellularService::SetApn(const Stringmap &value, Error *error) { |
| 150 | // Only copy in the fields we care about, and validate the contents. |
Eric Shienbrood | c707330 | 2012-04-03 14:41:44 -0400 | [diff] [blame] | 151 | // If the "apn" field is missing or empty, the APN is cleared. |
Eric Shienbrood | 30bc0ec | 2012-03-21 18:19:46 -0400 | [diff] [blame] | 152 | string str; |
| 153 | if (!GetNonEmptyField(value, flimflam::kApnProperty, &str)) { |
Eric Shienbrood | c707330 | 2012-04-03 14:41:44 -0400 | [diff] [blame] | 154 | apn_info_.clear(); |
| 155 | } else { |
| 156 | apn_info_[flimflam::kApnProperty] = str; |
| 157 | if (GetNonEmptyField(value, flimflam::kApnUsernameProperty, &str)) |
| 158 | apn_info_[flimflam::kApnUsernameProperty] = str; |
| 159 | if (GetNonEmptyField(value, flimflam::kApnPasswordProperty, &str)) |
| 160 | apn_info_[flimflam::kApnPasswordProperty] = str; |
| 161 | // Clear the last good APN, otherwise the one the user just |
| 162 | // set won't be used, since LastGoodApn comes first in the |
| 163 | // search order when trying to connect. Only do this if a |
| 164 | // non-empty user APN has been supplied. If the user APN is |
| 165 | // being cleared, leave LastGoodApn alone. |
| 166 | ClearLastGoodApn(); |
Eric Shienbrood | 30bc0ec | 2012-03-21 18:19:46 -0400 | [diff] [blame] | 167 | } |
Eric Shienbrood | 30bc0ec | 2012-03-21 18:19:46 -0400 | [diff] [blame] | 168 | adaptor()->EmitStringmapChanged(flimflam::kCellularApnProperty, apn_info_); |
| 169 | SaveToCurrentProfile(); |
| 170 | } |
| 171 | |
| 172 | void CellularService::SetLastGoodApn(const Stringmap &apn_info) { |
| 173 | last_good_apn_info_ = apn_info; |
| 174 | adaptor()->EmitStringmapChanged(flimflam::kCellularLastGoodApnProperty, |
| 175 | last_good_apn_info_); |
| 176 | SaveToCurrentProfile(); |
| 177 | } |
| 178 | |
| 179 | void CellularService::ClearLastGoodApn() { |
| 180 | last_good_apn_info_.clear(); |
| 181 | adaptor()->EmitStringmapChanged(flimflam::kCellularLastGoodApnProperty, |
| 182 | last_good_apn_info_); |
| 183 | SaveToCurrentProfile(); |
| 184 | } |
| 185 | |
| 186 | bool CellularService::Load(StoreInterface *storage) { |
| 187 | // Load properties common to all Services. |
| 188 | if (!Service::Load(storage)) |
| 189 | return false; |
| 190 | |
| 191 | const string id = GetStorageIdentifier(); |
| 192 | LoadApn(storage, id, kStorageAPN, &apn_info_); |
| 193 | LoadApn(storage, id, kStorageLastGoodAPN, &last_good_apn_info_); |
| 194 | return true; |
| 195 | } |
| 196 | |
| 197 | void CellularService::LoadApn(StoreInterface *storage, |
| 198 | const string &storage_group, |
| 199 | const string &keytag, |
| 200 | Stringmap *apn_info) { |
| 201 | if (!LoadApnField(storage, storage_group, keytag, |
| 202 | flimflam::kApnProperty, apn_info)) |
| 203 | return; |
| 204 | LoadApnField(storage, storage_group, keytag, |
| 205 | flimflam::kApnUsernameProperty, apn_info); |
| 206 | LoadApnField(storage, storage_group, keytag, |
| 207 | flimflam::kApnPasswordProperty, apn_info); |
| 208 | } |
| 209 | |
| 210 | bool CellularService::LoadApnField(StoreInterface *storage, |
| 211 | const string &storage_group, |
| 212 | const string &keytag, |
| 213 | const string &apntag, |
| 214 | Stringmap *apn_info) { |
| 215 | string value; |
| 216 | if (storage->GetString(storage_group, keytag + "." + apntag, &value) && |
| 217 | !value.empty()) { |
| 218 | (*apn_info)[apntag] = value; |
| 219 | return true; |
| 220 | } |
| 221 | return false; |
| 222 | } |
| 223 | |
| 224 | bool CellularService::Save(StoreInterface *storage) { |
| 225 | // Save properties common to all Services. |
| 226 | if (!Service::Save(storage)) |
| 227 | return false; |
| 228 | |
| 229 | const string id = GetStorageIdentifier(); |
| 230 | SaveApn(storage, id, GetUserSpecifiedApn(), kStorageAPN); |
| 231 | SaveApn(storage, id, GetLastGoodApn(), kStorageLastGoodAPN); |
| 232 | return true; |
| 233 | } |
| 234 | |
| 235 | void CellularService::SaveApn(StoreInterface *storage, |
| 236 | const string &storage_group, |
| 237 | const Stringmap *apn_info, |
| 238 | const string &keytag) { |
| 239 | SaveApnField(storage, storage_group, apn_info, keytag, |
| 240 | flimflam::kApnProperty); |
| 241 | SaveApnField(storage, storage_group, apn_info, keytag, |
| 242 | flimflam::kApnUsernameProperty); |
| 243 | SaveApnField(storage, storage_group, apn_info, keytag, |
| 244 | flimflam::kApnPasswordProperty); |
| 245 | } |
| 246 | |
| 247 | void CellularService::SaveApnField(StoreInterface *storage, |
| 248 | const string &storage_group, |
| 249 | const Stringmap *apn_info, |
| 250 | const string &keytag, |
| 251 | const string &apntag) { |
| 252 | const string key = keytag + "." + apntag; |
| 253 | string str; |
| 254 | if (apn_info && GetNonEmptyField(*apn_info, apntag, &str)) |
| 255 | storage->SetString(storage_group, key, str); |
| 256 | else |
| 257 | storage->DeleteKey(storage_group, key); |
| 258 | } |
| 259 | |
Darin Petkov | 4d6d941 | 2011-08-24 13:19:54 -0700 | [diff] [blame] | 260 | void CellularService::Connect(Error *error) { |
mukesh agrawal | adb6848 | 2012-01-17 16:31:51 -0800 | [diff] [blame] | 261 | Service::Connect(error); |
Darin Petkov | 4d6d941 | 2011-08-24 13:19:54 -0700 | [diff] [blame] | 262 | cellular_->Connect(error); |
Darin Petkov | c5f5656 | 2011-08-06 16:40:05 -0700 | [diff] [blame] | 263 | } |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 264 | |
Darin Petkov | fb0625e | 2012-01-16 13:05:56 +0100 | [diff] [blame] | 265 | void CellularService::Disconnect(Error *error) { |
| 266 | Service::Disconnect(error); |
| 267 | cellular_->Disconnect(error); |
| 268 | } |
| 269 | |
Darin Petkov | b100ae7 | 2011-08-24 16:19:45 -0700 | [diff] [blame] | 270 | void CellularService::ActivateCellularModem(const string &carrier, |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 271 | Error *error, |
| 272 | const ResultCallback &callback) { |
| 273 | cellular_->Activate(carrier, error, callback); |
Darin Petkov | c408e69 | 2011-08-17 13:47:15 -0700 | [diff] [blame] | 274 | } |
| 275 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 276 | bool CellularService::TechnologyIs(const Technology::Identifier type) const { |
| 277 | return cellular_->TechnologyIs(type); |
| 278 | } |
| 279 | |
Darin Petkov | 3133241 | 2012-01-28 01:50:02 +0100 | [diff] [blame] | 280 | void CellularService::SetStorageIdentifier(const string &identifier) { |
| 281 | storage_identifier_ = identifier; |
| 282 | std::replace_if(storage_identifier_.begin(), |
| 283 | storage_identifier_.end(), |
| 284 | &Service::IllegalChar, '_'); |
| 285 | } |
| 286 | |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 287 | string CellularService::GetStorageIdentifier() const { |
Darin Petkov | 3133241 | 2012-01-28 01:50:02 +0100 | [diff] [blame] | 288 | return storage_identifier_; |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 289 | } |
| 290 | |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 291 | string CellularService::GetDeviceRpcId(Error */*error*/) { |
Chris Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame] | 292 | return cellular_->GetRpcIdentifier(); |
| 293 | } |
| 294 | |
Darin Petkov | b9c9933 | 2012-01-12 13:13:00 +0100 | [diff] [blame] | 295 | void CellularService::SetActivationState(const string &state) { |
| 296 | if (state == activation_state_) { |
| 297 | return; |
| 298 | } |
| 299 | activation_state_ = state; |
| 300 | adaptor()->EmitStringChanged(flimflam::kActivationStateProperty, state); |
| 301 | } |
| 302 | |
Darin Petkov | 381928f | 2012-02-02 23:00:12 +0100 | [diff] [blame] | 303 | void CellularService::SetOLP(const OLP &olp) { |
| 304 | if (olp_.Equals(olp)) { |
| 305 | return; |
| 306 | } |
| 307 | olp_.CopyFrom(olp); |
| 308 | adaptor()->EmitStringmapChanged(flimflam::kPaymentPortalProperty, |
| 309 | olp.ToDict()); |
| 310 | } |
| 311 | |
| 312 | void CellularService::SetUsageURL(const std::string &url) { |
| 313 | if (url == usage_url_) { |
| 314 | return; |
| 315 | } |
| 316 | usage_url_ = url; |
| 317 | adaptor()->EmitStringChanged(flimflam::kUsageURLProperty, url); |
| 318 | } |
| 319 | |
Darin Petkov | b72cf40 | 2011-11-22 14:51:39 +0100 | [diff] [blame] | 320 | void CellularService::SetNetworkTechnology(const string &technology) { |
| 321 | if (technology == network_technology_) { |
| 322 | return; |
| 323 | } |
| 324 | network_technology_ = technology; |
| 325 | adaptor()->EmitStringChanged(flimflam::kNetworkTechnologyProperty, |
| 326 | technology); |
| 327 | } |
| 328 | |
| 329 | void CellularService::SetRoamingState(const string &state) { |
| 330 | if (state == roaming_state_) { |
| 331 | return; |
| 332 | } |
| 333 | roaming_state_ = state; |
| 334 | adaptor()->EmitStringChanged(flimflam::kRoamingStateProperty, state); |
| 335 | } |
| 336 | |
Darin Petkov | 3335b37 | 2011-08-22 11:05:32 -0700 | [diff] [blame] | 337 | const Cellular::Operator &CellularService::serving_operator() const { |
| 338 | return serving_operator_; |
| 339 | } |
| 340 | |
Darin Petkov | 9cb0268 | 2012-01-28 00:17:38 +0100 | [diff] [blame] | 341 | void CellularService::SetServingOperator(const Cellular::Operator &oper) { |
| 342 | if (serving_operator_.Equals(oper)) { |
| 343 | return; |
| 344 | } |
Darin Petkov | 3335b37 | 2011-08-22 11:05:32 -0700 | [diff] [blame] | 345 | serving_operator_.CopyFrom(oper); |
Darin Petkov | 9cb0268 | 2012-01-28 00:17:38 +0100 | [diff] [blame] | 346 | adaptor()->EmitStringmapChanged(flimflam::kServingOperatorProperty, |
| 347 | oper.ToDict()); |
Darin Petkov | 3335b37 | 2011-08-22 11:05:32 -0700 | [diff] [blame] | 348 | } |
| 349 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 350 | } // namespace shill |