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