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 | |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 9 | #include <base/stringprintf.h> |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 10 | #include <chromeos/dbus/service_constants.h> |
| 11 | |
Darin Petkov | b72cf40 | 2011-11-22 14:51:39 +0100 | [diff] [blame] | 12 | #include "shill/adaptor_interfaces.h" |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 13 | #include "shill/cellular.h" |
Eric Shienbrood | 30bc0ec | 2012-03-21 18:19:46 -0400 | [diff] [blame] | 14 | #include "shill/property_accessor.h" |
| 15 | #include "shill/store_interface.h" |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 16 | |
| 17 | using std::string; |
| 18 | |
| 19 | namespace shill { |
Darin Petkov | c5f5656 | 2011-08-06 16:40:05 -0700 | [diff] [blame] | 20 | |
Christopher Wiley | 1582bdd | 2012-11-15 11:31:14 -0800 | [diff] [blame] | 21 | const char CellularService::kAutoConnActivating[] = "activating"; |
Ben Chan | 19f8397 | 2012-10-03 23:25:56 -0700 | [diff] [blame] | 22 | const char CellularService::kAutoConnDeviceDisabled[] = "device disabled"; |
Thieu Le | 398b1da | 2013-03-11 17:31:10 -0700 | [diff] [blame] | 23 | const char CellularService::kAutoConnOutOfCredits[] = "device out of credits"; |
| 24 | const char CellularService::kAutoConnOutOfCreditsDetectionInProgress[] = |
| 25 | "device detecting out-of-credits"; |
| 26 | const int64 CellularService::kOutOfCreditsConnectionDropSeconds = 15; |
| 27 | const int CellularService::kOutOfCreditsMaxConnectAttempts = 3; |
Thieu Le | 99dc56d | 2013-04-01 14:22:21 -0700 | [diff] [blame] | 28 | const int64 CellularService::kOutOfCreditsResumeIgnoreSeconds = 5; |
Eric Shienbrood | 30bc0ec | 2012-03-21 18:19:46 -0400 | [diff] [blame] | 29 | const char CellularService::kStorageAPN[] = "Cellular.APN"; |
| 30 | const char CellularService::kStorageLastGoodAPN[] = "Cellular.LastGoodAPN"; |
| 31 | |
Darin Petkov | 381928f | 2012-02-02 23:00:12 +0100 | [diff] [blame] | 32 | // TODO(petkov): Add these to system_api/dbus/service_constants.h |
| 33 | namespace { |
| 34 | const char kKeyOLPURL[] = "url"; |
| 35 | const char kKeyOLPMethod[] = "method"; |
| 36 | const char kKeyOLPPostData[] = "postdata"; |
Ben Chan | 19f8397 | 2012-10-03 23:25:56 -0700 | [diff] [blame] | 37 | } // namespace |
Darin Petkov | 381928f | 2012-02-02 23:00:12 +0100 | [diff] [blame] | 38 | |
Eric Shienbrood | 30bc0ec | 2012-03-21 18:19:46 -0400 | [diff] [blame] | 39 | static bool GetNonEmptyField(const Stringmap &stringmap, |
| 40 | const string &fieldname, |
| 41 | string *value) { |
| 42 | Stringmap::const_iterator it = stringmap.find(fieldname); |
| 43 | if (it != stringmap.end() && !it->second.empty()) { |
| 44 | *value = it->second; |
| 45 | return true; |
| 46 | } |
| 47 | return false; |
| 48 | } |
| 49 | |
Darin Petkov | 381928f | 2012-02-02 23:00:12 +0100 | [diff] [blame] | 50 | CellularService::OLP::OLP() { |
| 51 | SetURL(""); |
| 52 | SetMethod(""); |
| 53 | SetPostData(""); |
| 54 | } |
| 55 | |
| 56 | CellularService::OLP::~OLP() {} |
| 57 | |
| 58 | void CellularService::OLP::CopyFrom(const OLP &olp) { |
| 59 | dict_ = olp.dict_; |
| 60 | } |
| 61 | |
| 62 | bool CellularService::OLP::Equals(const OLP &olp) const { |
| 63 | return dict_ == olp.dict_; |
| 64 | } |
| 65 | |
| 66 | const string &CellularService::OLP::GetURL() const { |
| 67 | return dict_.find(kKeyOLPURL)->second; |
| 68 | } |
| 69 | |
| 70 | void CellularService::OLP::SetURL(const string &url) { |
| 71 | dict_[kKeyOLPURL] = url; |
| 72 | } |
| 73 | |
| 74 | const string &CellularService::OLP::GetMethod() const { |
| 75 | return dict_.find(kKeyOLPMethod)->second; |
| 76 | } |
| 77 | |
| 78 | void CellularService::OLP::SetMethod(const string &method) { |
| 79 | dict_[kKeyOLPMethod] = method; |
| 80 | } |
| 81 | |
| 82 | const string &CellularService::OLP::GetPostData() const { |
| 83 | return dict_.find(kKeyOLPPostData)->second; |
| 84 | } |
| 85 | |
| 86 | void CellularService::OLP::SetPostData(const string &post_data) { |
| 87 | dict_[kKeyOLPPostData] = post_data; |
| 88 | } |
| 89 | |
| 90 | const Stringmap &CellularService::OLP::ToDict() const { |
| 91 | return dict_; |
| 92 | } |
| 93 | |
Prathmesh Prabhu | 0d36b4f | 2013-04-01 11:45:54 -0700 | [diff] [blame] | 94 | CellularService::CellularService(ModemInfo *modem_info, |
mukesh agrawal | 51a7e93 | 2011-07-27 16:18:26 -0700 | [diff] [blame] | 95 | const CellularRefPtr &device) |
Prathmesh Prabhu | 0d36b4f | 2013-04-01 11:45:54 -0700 | [diff] [blame] | 96 | : Service(modem_info->control_interface(), modem_info->dispatcher(), |
| 97 | modem_info->metrics(), modem_info->manager(), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 98 | Technology::kCellular), |
Thieu Le | 398b1da | 2013-03-11 17:31:10 -0700 | [diff] [blame] | 99 | weak_ptr_factory_(this), |
Ben Chan | 3d6de0e | 2012-12-10 12:01:34 -0800 | [diff] [blame] | 100 | activate_over_non_cellular_network_(false), |
Thieu Le | 7cf36b0 | 2013-01-30 17:15:56 -0800 | [diff] [blame] | 101 | cellular_(device), |
Thieu Le | 398b1da | 2013-03-11 17:31:10 -0700 | [diff] [blame] | 102 | is_auto_connecting_(false), |
| 103 | enforce_out_of_credits_detection_(false), |
| 104 | num_connect_attempts_(0), |
| 105 | out_of_credits_detection_in_progress_(false), |
| 106 | out_of_credits_(false) { |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 107 | SetConnectable(true); |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 108 | PropertyStore *store = this->mutable_store(); |
Ben Chan | 3d6de0e | 2012-12-10 12:01:34 -0800 | [diff] [blame] | 109 | store->RegisterConstBool(kActivateOverNonCellularNetworkProperty, |
| 110 | &activate_over_non_cellular_network_); |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 111 | store->RegisterConstString(flimflam::kActivationStateProperty, |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 112 | &activation_state_); |
Eric Shienbrood | 30bc0ec | 2012-03-21 18:19:46 -0400 | [diff] [blame] | 113 | HelpRegisterDerivedStringmap(flimflam::kCellularApnProperty, |
| 114 | &CellularService::GetApn, |
| 115 | &CellularService::SetApn); |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 116 | store->RegisterConstStringmap(flimflam::kCellularLastGoodApnProperty, |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 117 | &last_good_apn_info_); |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 118 | store->RegisterConstString(flimflam::kNetworkTechnologyProperty, |
Darin Petkov | b72cf40 | 2011-11-22 14:51:39 +0100 | [diff] [blame] | 119 | &network_technology_); |
Thieu Le | 398b1da | 2013-03-11 17:31:10 -0700 | [diff] [blame] | 120 | store->RegisterConstBool(kOutOfCreditsProperty, &out_of_credits_); |
Darin Petkov | 381928f | 2012-02-02 23:00:12 +0100 | [diff] [blame] | 121 | store->RegisterConstStringmap(flimflam::kPaymentPortalProperty, |
| 122 | &olp_.ToDict()); |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 123 | store->RegisterConstString(flimflam::kRoamingStateProperty, &roaming_state_); |
| 124 | store->RegisterConstStringmap(flimflam::kServingOperatorProperty, |
Darin Petkov | 3335b37 | 2011-08-22 11:05:32 -0700 | [diff] [blame] | 125 | &serving_operator_.ToDict()); |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 126 | store->RegisterConstString(flimflam::kUsageURLProperty, &usage_url_); |
Darin Petkov | ac635a8 | 2012-01-10 16:51:58 +0100 | [diff] [blame] | 127 | |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 128 | string name = device->CreateFriendlyServiceName(); |
| 129 | set_friendly_name(name); |
Darin Petkov | dd3e866 | 2012-02-03 13:16:20 +0100 | [diff] [blame] | 130 | SetStorageIdentifier(string(flimflam::kTypeCellular) + "_" + |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 131 | device->address() + "_" + name); |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | CellularService::~CellularService() { } |
| 135 | |
Ben Chan | 19f8397 | 2012-10-03 23:25:56 -0700 | [diff] [blame] | 136 | bool CellularService::IsAutoConnectable(const char **reason) const { |
| 137 | if (!cellular_->running()) { |
| 138 | *reason = kAutoConnDeviceDisabled; |
| 139 | return false; |
| 140 | } |
Christopher Wiley | 1582bdd | 2012-11-15 11:31:14 -0800 | [diff] [blame] | 141 | if (cellular_->IsActivating()) { |
| 142 | *reason = kAutoConnActivating; |
| 143 | return false; |
| 144 | } |
Thieu Le | 398b1da | 2013-03-11 17:31:10 -0700 | [diff] [blame] | 145 | if (out_of_credits_detection_in_progress_) { |
| 146 | *reason = kAutoConnOutOfCreditsDetectionInProgress; |
| 147 | return false; |
| 148 | } |
| 149 | if (out_of_credits_) { |
| 150 | *reason = kAutoConnOutOfCredits; |
| 151 | return false; |
| 152 | } |
Ben Chan | 19f8397 | 2012-10-03 23:25:56 -0700 | [diff] [blame] | 153 | return Service::IsAutoConnectable(reason); |
| 154 | } |
| 155 | |
Eric Shienbrood | 30bc0ec | 2012-03-21 18:19:46 -0400 | [diff] [blame] | 156 | void CellularService::HelpRegisterDerivedStringmap( |
| 157 | const string &name, |
| 158 | Stringmap(CellularService::*get)(Error *error), |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 159 | bool(CellularService::*set)( |
Eric Shienbrood | 30bc0ec | 2012-03-21 18:19:46 -0400 | [diff] [blame] | 160 | const Stringmap &value, Error *error)) { |
| 161 | mutable_store()->RegisterDerivedStringmap( |
| 162 | name, |
| 163 | StringmapAccessor( |
| 164 | new CustomAccessor<CellularService, Stringmap>(this, get, set))); |
| 165 | } |
| 166 | |
| 167 | Stringmap *CellularService::GetUserSpecifiedApn() { |
| 168 | Stringmap::iterator it = apn_info_.find(flimflam::kApnProperty); |
| 169 | if (it == apn_info_.end() || it->second.empty()) |
| 170 | return NULL; |
| 171 | return &apn_info_; |
| 172 | } |
| 173 | |
| 174 | Stringmap *CellularService::GetLastGoodApn() { |
| 175 | Stringmap::iterator it = |
| 176 | last_good_apn_info_.find(flimflam::kApnProperty); |
| 177 | if (it == last_good_apn_info_.end() || it->second.empty()) |
| 178 | return NULL; |
| 179 | return &last_good_apn_info_; |
| 180 | } |
| 181 | |
| 182 | Stringmap CellularService::GetApn(Error */*error*/) { |
| 183 | return apn_info_; |
| 184 | } |
| 185 | |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 186 | bool CellularService::SetApn(const Stringmap &value, Error *error) { |
Eric Shienbrood | 30bc0ec | 2012-03-21 18:19:46 -0400 | [diff] [blame] | 187 | // Only copy in the fields we care about, and validate the contents. |
Eric Shienbrood | c707330 | 2012-04-03 14:41:44 -0400 | [diff] [blame] | 188 | // If the "apn" field is missing or empty, the APN is cleared. |
Eric Shienbrood | 30bc0ec | 2012-03-21 18:19:46 -0400 | [diff] [blame] | 189 | string str; |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 190 | Stringmap new_apn_info; |
| 191 | if (GetNonEmptyField(value, flimflam::kApnProperty, &str)) { |
| 192 | new_apn_info[flimflam::kApnProperty] = str; |
Eric Shienbrood | c707330 | 2012-04-03 14:41:44 -0400 | [diff] [blame] | 193 | if (GetNonEmptyField(value, flimflam::kApnUsernameProperty, &str)) |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 194 | new_apn_info[flimflam::kApnUsernameProperty] = str; |
Eric Shienbrood | c707330 | 2012-04-03 14:41:44 -0400 | [diff] [blame] | 195 | if (GetNonEmptyField(value, flimflam::kApnPasswordProperty, &str)) |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 196 | new_apn_info[flimflam::kApnPasswordProperty] = str; |
| 197 | } |
| 198 | if (apn_info_ == new_apn_info) { |
| 199 | return false; |
| 200 | } |
| 201 | apn_info_ = new_apn_info; |
| 202 | if (ContainsKey(apn_info_, flimflam::kApnProperty)) { |
Eric Shienbrood | c707330 | 2012-04-03 14:41:44 -0400 | [diff] [blame] | 203 | // Clear the last good APN, otherwise the one the user just |
| 204 | // set won't be used, since LastGoodApn comes first in the |
| 205 | // search order when trying to connect. Only do this if a |
| 206 | // non-empty user APN has been supplied. If the user APN is |
| 207 | // being cleared, leave LastGoodApn alone. |
| 208 | ClearLastGoodApn(); |
Eric Shienbrood | 30bc0ec | 2012-03-21 18:19:46 -0400 | [diff] [blame] | 209 | } |
Eric Shienbrood | 30bc0ec | 2012-03-21 18:19:46 -0400 | [diff] [blame] | 210 | adaptor()->EmitStringmapChanged(flimflam::kCellularApnProperty, apn_info_); |
| 211 | SaveToCurrentProfile(); |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 212 | return true; |
Eric Shienbrood | 30bc0ec | 2012-03-21 18:19:46 -0400 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | void CellularService::SetLastGoodApn(const Stringmap &apn_info) { |
| 216 | last_good_apn_info_ = apn_info; |
| 217 | adaptor()->EmitStringmapChanged(flimflam::kCellularLastGoodApnProperty, |
| 218 | last_good_apn_info_); |
| 219 | SaveToCurrentProfile(); |
| 220 | } |
| 221 | |
| 222 | void CellularService::ClearLastGoodApn() { |
| 223 | last_good_apn_info_.clear(); |
| 224 | adaptor()->EmitStringmapChanged(flimflam::kCellularLastGoodApnProperty, |
| 225 | last_good_apn_info_); |
| 226 | SaveToCurrentProfile(); |
| 227 | } |
| 228 | |
Thieu Le | 99dc56d | 2013-04-01 14:22:21 -0700 | [diff] [blame] | 229 | void CellularService::OnAfterResume() { |
| 230 | Service::OnAfterResume(); |
| 231 | resume_start_time_ = base::Time::Now(); |
| 232 | } |
| 233 | |
Eric Shienbrood | 30bc0ec | 2012-03-21 18:19:46 -0400 | [diff] [blame] | 234 | bool CellularService::Load(StoreInterface *storage) { |
| 235 | // Load properties common to all Services. |
| 236 | if (!Service::Load(storage)) |
| 237 | return false; |
| 238 | |
| 239 | const string id = GetStorageIdentifier(); |
| 240 | LoadApn(storage, id, kStorageAPN, &apn_info_); |
| 241 | LoadApn(storage, id, kStorageLastGoodAPN, &last_good_apn_info_); |
| 242 | return true; |
| 243 | } |
| 244 | |
| 245 | void CellularService::LoadApn(StoreInterface *storage, |
| 246 | const string &storage_group, |
| 247 | const string &keytag, |
| 248 | Stringmap *apn_info) { |
| 249 | if (!LoadApnField(storage, storage_group, keytag, |
| 250 | flimflam::kApnProperty, apn_info)) |
| 251 | return; |
| 252 | LoadApnField(storage, storage_group, keytag, |
| 253 | flimflam::kApnUsernameProperty, apn_info); |
| 254 | LoadApnField(storage, storage_group, keytag, |
| 255 | flimflam::kApnPasswordProperty, apn_info); |
| 256 | } |
| 257 | |
| 258 | bool CellularService::LoadApnField(StoreInterface *storage, |
| 259 | const string &storage_group, |
| 260 | const string &keytag, |
| 261 | const string &apntag, |
| 262 | Stringmap *apn_info) { |
| 263 | string value; |
| 264 | if (storage->GetString(storage_group, keytag + "." + apntag, &value) && |
| 265 | !value.empty()) { |
| 266 | (*apn_info)[apntag] = value; |
| 267 | return true; |
| 268 | } |
| 269 | return false; |
| 270 | } |
| 271 | |
Thieu Le | 398b1da | 2013-03-11 17:31:10 -0700 | [diff] [blame] | 272 | void CellularService::PerformOutOfCreditsDetection(ConnectState curr_state, |
| 273 | ConnectState new_state) { |
| 274 | // WORKAROUND: |
| 275 | // Some modems on Verizon network does not properly redirect when a SIM |
| 276 | // runs out of credits. This workaround is used to detect an out-of-credits |
| 277 | // condition by by retrying a connect request if it was dropped within |
| 278 | // kOutOfCreditsConnectionDropSeconds. If the number of retries exceeds |
| 279 | // kOutOfCreditsMaxConnectAttempts, then the SIM is considered |
| 280 | // out-of-credits and the cellular service kOutOfCreditsProperty is set. |
| 281 | // This will signal Chrome to display the appropriate UX and also suppress |
| 282 | // auto-connect until the next time the user manually connects. |
| 283 | // |
| 284 | // TODO(thieule): Remove this workaround (crosbug.com/p/18169). |
Thieu Le | 3a8683d | 2013-04-17 13:57:24 -0700 | [diff] [blame] | 285 | if (out_of_credits_) { |
| 286 | SLOG(Cellular, 2) << __func__ |
| 287 | << ": Already out-of-credits, skipping check"; |
| 288 | return; |
| 289 | } |
Thieu Le | 398b1da | 2013-03-11 17:31:10 -0700 | [diff] [blame] | 290 | base::TimeDelta |
Thieu Le | 99dc56d | 2013-04-01 14:22:21 -0700 | [diff] [blame] | 291 | time_since_resume = base::Time::Now() - resume_start_time_; |
| 292 | if (time_since_resume.InSeconds() < kOutOfCreditsResumeIgnoreSeconds) { |
| 293 | // On platforms that power down the modem during suspend, make sure that |
| 294 | // we do not display a false out-of-credits warning to the user |
| 295 | // due to the sequence below by skipping out-of-credits detection |
| 296 | // immediately after a resume. |
| 297 | // 1. User suspends Chromebook. |
| 298 | // 2. Hardware turns off power to modem. |
| 299 | // 3. User resumes Chromebook. |
| 300 | // 4. Hardware restores power to modem. |
| 301 | // 5. ModemManager still has instance of old modem. |
| 302 | // ModemManager does not delete this instance until udev fires a |
| 303 | // device removed event. ModemManager does not detect new modem |
| 304 | // until udev fires a new device event. |
| 305 | // 6. Shill performs auto-connect against the old modem. |
| 306 | // Make sure at this step that we do not display a false |
| 307 | // out-of-credits warning. |
| 308 | // 7. Udev fires device removed event. |
| 309 | // 8. Udev fires new device event. |
| 310 | SLOG(Cellular, 2) << |
| 311 | "Skipping out-of-credits detection, too soon since resume."; |
| 312 | ResetOutOfCreditsState(); |
| 313 | return; |
| 314 | } |
| 315 | base::TimeDelta |
Thieu Le | 398b1da | 2013-03-11 17:31:10 -0700 | [diff] [blame] | 316 | time_since_connect = base::Time::Now() - connect_start_time_; |
| 317 | if (time_since_connect.InSeconds() > kOutOfCreditsConnectionDropSeconds) { |
| 318 | ResetOutOfCreditsState(); |
| 319 | return; |
| 320 | } |
| 321 | // Verizon can drop the connection in two ways: |
| 322 | // - Denies the connect request |
| 323 | // - Allows connect request but disconnects later |
| 324 | bool connection_dropped = |
| 325 | (IsConnectedState(curr_state) || IsConnectingState(curr_state)) && |
| 326 | (new_state == kStateFailure || new_state == kStateIdle); |
| 327 | if (!connection_dropped) |
| 328 | return; |
| 329 | if (explicitly_disconnected()) |
| 330 | return; |
| 331 | if (roaming_state_ == flimflam::kRoamingStateRoaming && |
| 332 | !cellular_->allow_roaming_property()) |
| 333 | return; |
| 334 | if (time_since_connect.InSeconds() <= kOutOfCreditsConnectionDropSeconds) { |
| 335 | if (num_connect_attempts_ < kOutOfCreditsMaxConnectAttempts) { |
| 336 | SLOG(Cellular, 2) << "Out-Of-Credits detection: Reconnecting " |
| 337 | << "(retry #" << num_connect_attempts_ << ")"; |
| 338 | // Prevent autoconnect logic from kicking in while we perform the |
| 339 | // out-of-credits detection. |
| 340 | out_of_credits_detection_in_progress_ = true; |
| 341 | dispatcher()->PostTask( |
| 342 | Bind(&CellularService::OutOfCreditsReconnect, |
| 343 | weak_ptr_factory_.GetWeakPtr())); |
| 344 | } else { |
| 345 | LOG(ERROR) << |
| 346 | "Out-Of-Credits detection: Marking service as out-of-credits"; |
Thieu Le | 91fccf6 | 2013-04-22 15:23:16 -0700 | [diff] [blame] | 347 | metrics()->NotifyCellularOutOfCredits( |
| 348 | Metrics::kCellularOutOfCreditsReasonConnectDisconnectLoop); |
Thieu Le | 398b1da | 2013-03-11 17:31:10 -0700 | [diff] [blame] | 349 | SetOutOfCredits(true); |
| 350 | ResetOutOfCreditsState(); |
| 351 | } |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | void CellularService::OutOfCreditsReconnect() { |
| 356 | Error error; |
mukesh agrawal | dc7b844 | 2012-09-27 13:48:14 -0700 | [diff] [blame] | 357 | Connect(&error, __func__); |
Thieu Le | 398b1da | 2013-03-11 17:31:10 -0700 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | void CellularService::ResetOutOfCreditsState() { |
| 361 | out_of_credits_detection_in_progress_ = false; |
| 362 | num_connect_attempts_ = 0; |
| 363 | } |
| 364 | |
Eric Shienbrood | 30bc0ec | 2012-03-21 18:19:46 -0400 | [diff] [blame] | 365 | bool CellularService::Save(StoreInterface *storage) { |
| 366 | // Save properties common to all Services. |
| 367 | if (!Service::Save(storage)) |
| 368 | return false; |
| 369 | |
| 370 | const string id = GetStorageIdentifier(); |
| 371 | SaveApn(storage, id, GetUserSpecifiedApn(), kStorageAPN); |
| 372 | SaveApn(storage, id, GetLastGoodApn(), kStorageLastGoodAPN); |
| 373 | return true; |
| 374 | } |
| 375 | |
| 376 | void CellularService::SaveApn(StoreInterface *storage, |
| 377 | const string &storage_group, |
| 378 | const Stringmap *apn_info, |
| 379 | const string &keytag) { |
| 380 | SaveApnField(storage, storage_group, apn_info, keytag, |
| 381 | flimflam::kApnProperty); |
| 382 | SaveApnField(storage, storage_group, apn_info, keytag, |
| 383 | flimflam::kApnUsernameProperty); |
| 384 | SaveApnField(storage, storage_group, apn_info, keytag, |
| 385 | flimflam::kApnPasswordProperty); |
| 386 | } |
| 387 | |
| 388 | void CellularService::SaveApnField(StoreInterface *storage, |
| 389 | const string &storage_group, |
| 390 | const Stringmap *apn_info, |
| 391 | const string &keytag, |
| 392 | const string &apntag) { |
| 393 | const string key = keytag + "." + apntag; |
| 394 | string str; |
| 395 | if (apn_info && GetNonEmptyField(*apn_info, apntag, &str)) |
| 396 | storage->SetString(storage_group, key, str); |
| 397 | else |
| 398 | storage->DeleteKey(storage_group, key); |
| 399 | } |
| 400 | |
Thieu Le | 7cf36b0 | 2013-01-30 17:15:56 -0800 | [diff] [blame] | 401 | void CellularService::AutoConnect() { |
| 402 | is_auto_connecting_ = true; |
| 403 | Service::AutoConnect(); |
| 404 | is_auto_connecting_ = false; |
| 405 | } |
| 406 | |
mukesh agrawal | dc7b844 | 2012-09-27 13:48:14 -0700 | [diff] [blame] | 407 | void CellularService::Connect(Error *error, const char *reason) { |
Thieu Le | 398b1da | 2013-03-11 17:31:10 -0700 | [diff] [blame] | 408 | if (num_connect_attempts_ == 0) |
| 409 | SetOutOfCredits(false); |
| 410 | connect_start_time_ = base::Time::Now(); |
| 411 | num_connect_attempts_++; |
mukesh agrawal | dc7b844 | 2012-09-27 13:48:14 -0700 | [diff] [blame] | 412 | Service::Connect(error, reason); |
Darin Petkov | 4d6d941 | 2011-08-24 13:19:54 -0700 | [diff] [blame] | 413 | cellular_->Connect(error); |
Thieu Le | 398b1da | 2013-03-11 17:31:10 -0700 | [diff] [blame] | 414 | if (error->IsFailure()) |
| 415 | ResetOutOfCreditsState(); |
Darin Petkov | c5f5656 | 2011-08-06 16:40:05 -0700 | [diff] [blame] | 416 | } |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 417 | |
Darin Petkov | fb0625e | 2012-01-16 13:05:56 +0100 | [diff] [blame] | 418 | void CellularService::Disconnect(Error *error) { |
| 419 | Service::Disconnect(error); |
| 420 | cellular_->Disconnect(error); |
| 421 | } |
| 422 | |
Darin Petkov | b100ae7 | 2011-08-24 16:19:45 -0700 | [diff] [blame] | 423 | void CellularService::ActivateCellularModem(const string &carrier, |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 424 | Error *error, |
| 425 | const ResultCallback &callback) { |
| 426 | cellular_->Activate(carrier, error, callback); |
Darin Petkov | c408e69 | 2011-08-17 13:47:15 -0700 | [diff] [blame] | 427 | } |
| 428 | |
Arman Uguray | c7b1560 | 2013-02-16 00:56:18 -0800 | [diff] [blame] | 429 | void CellularService::CompleteCellularActivation(Error *error) { |
| 430 | cellular_->CompleteActivation(error); |
| 431 | } |
| 432 | |
Thieu Le | 398b1da | 2013-03-11 17:31:10 -0700 | [diff] [blame] | 433 | void CellularService::SetState(ConnectState new_state) { |
| 434 | if (enforce_out_of_credits_detection_) |
| 435 | PerformOutOfCreditsDetection(state(), new_state); |
| 436 | Service::SetState(new_state); |
| 437 | } |
| 438 | |
Darin Petkov | 3133241 | 2012-01-28 01:50:02 +0100 | [diff] [blame] | 439 | void CellularService::SetStorageIdentifier(const string &identifier) { |
| 440 | storage_identifier_ = identifier; |
| 441 | std::replace_if(storage_identifier_.begin(), |
| 442 | storage_identifier_.end(), |
| 443 | &Service::IllegalChar, '_'); |
| 444 | } |
| 445 | |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 446 | string CellularService::GetStorageIdentifier() const { |
Darin Petkov | 3133241 | 2012-01-28 01:50:02 +0100 | [diff] [blame] | 447 | return storage_identifier_; |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 448 | } |
| 449 | |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 450 | string CellularService::GetDeviceRpcId(Error */*error*/) { |
Chris Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame] | 451 | return cellular_->GetRpcIdentifier(); |
| 452 | } |
| 453 | |
Ben Chan | 3d6de0e | 2012-12-10 12:01:34 -0800 | [diff] [blame] | 454 | void CellularService::SetActivateOverNonCellularNetwork(bool state) { |
| 455 | if (state == activate_over_non_cellular_network_) { |
| 456 | return; |
| 457 | } |
| 458 | activate_over_non_cellular_network_ = state; |
| 459 | adaptor()->EmitBoolChanged(kActivateOverNonCellularNetworkProperty, state); |
| 460 | } |
| 461 | |
Darin Petkov | b9c9933 | 2012-01-12 13:13:00 +0100 | [diff] [blame] | 462 | void CellularService::SetActivationState(const string &state) { |
| 463 | if (state == activation_state_) { |
| 464 | return; |
| 465 | } |
| 466 | activation_state_ = state; |
| 467 | adaptor()->EmitStringChanged(flimflam::kActivationStateProperty, state); |
mukesh agrawal | cbfb34e | 2013-04-17 19:33:25 -0700 | [diff] [blame] | 468 | SetConnectableFull(state != flimflam::kActivationStateNotActivated); |
Darin Petkov | b9c9933 | 2012-01-12 13:13:00 +0100 | [diff] [blame] | 469 | } |
| 470 | |
Darin Petkov | 381928f | 2012-02-02 23:00:12 +0100 | [diff] [blame] | 471 | void CellularService::SetOLP(const OLP &olp) { |
| 472 | if (olp_.Equals(olp)) { |
| 473 | return; |
| 474 | } |
| 475 | olp_.CopyFrom(olp); |
| 476 | adaptor()->EmitStringmapChanged(flimflam::kPaymentPortalProperty, |
| 477 | olp.ToDict()); |
| 478 | } |
| 479 | |
Arman Uguray | 2717a10 | 2013-01-29 23:36:06 -0800 | [diff] [blame] | 480 | void CellularService::SetUsageURL(const string &url) { |
Darin Petkov | 381928f | 2012-02-02 23:00:12 +0100 | [diff] [blame] | 481 | if (url == usage_url_) { |
| 482 | return; |
| 483 | } |
| 484 | usage_url_ = url; |
| 485 | adaptor()->EmitStringChanged(flimflam::kUsageURLProperty, url); |
| 486 | } |
| 487 | |
Darin Petkov | b72cf40 | 2011-11-22 14:51:39 +0100 | [diff] [blame] | 488 | void CellularService::SetNetworkTechnology(const string &technology) { |
| 489 | if (technology == network_technology_) { |
| 490 | return; |
| 491 | } |
| 492 | network_technology_ = technology; |
| 493 | adaptor()->EmitStringChanged(flimflam::kNetworkTechnologyProperty, |
| 494 | technology); |
| 495 | } |
| 496 | |
| 497 | void CellularService::SetRoamingState(const string &state) { |
| 498 | if (state == roaming_state_) { |
| 499 | return; |
| 500 | } |
| 501 | roaming_state_ = state; |
| 502 | adaptor()->EmitStringChanged(flimflam::kRoamingStateProperty, state); |
| 503 | } |
| 504 | |
Thieu Le | 398b1da | 2013-03-11 17:31:10 -0700 | [diff] [blame] | 505 | void CellularService::SetOutOfCredits(bool state) { |
| 506 | if (state == out_of_credits_) { |
| 507 | return; |
| 508 | } |
| 509 | out_of_credits_ = state; |
| 510 | adaptor()->EmitBoolChanged(kOutOfCreditsProperty, state); |
| 511 | } |
| 512 | |
Darin Petkov | 3335b37 | 2011-08-22 11:05:32 -0700 | [diff] [blame] | 513 | const Cellular::Operator &CellularService::serving_operator() const { |
| 514 | return serving_operator_; |
| 515 | } |
| 516 | |
Darin Petkov | 9cb0268 | 2012-01-28 00:17:38 +0100 | [diff] [blame] | 517 | void CellularService::SetServingOperator(const Cellular::Operator &oper) { |
| 518 | if (serving_operator_.Equals(oper)) { |
| 519 | return; |
| 520 | } |
Darin Petkov | 3335b37 | 2011-08-22 11:05:32 -0700 | [diff] [blame] | 521 | serving_operator_.CopyFrom(oper); |
Darin Petkov | 9cb0268 | 2012-01-28 00:17:38 +0100 | [diff] [blame] | 522 | adaptor()->EmitStringmapChanged(flimflam::kServingOperatorProperty, |
| 523 | oper.ToDict()); |
Darin Petkov | 3335b37 | 2011-08-22 11:05:32 -0700 | [diff] [blame] | 524 | } |
| 525 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 526 | } // namespace shill |