Darin Petkov | c64fe5e | 2012-01-11 12:46:13 +0100 | [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.h" |
| 6 | |
Darin Petkov | 0828f5f | 2011-08-11 10:18:52 -0700 | [diff] [blame] | 7 | #include <netinet/in.h> |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 8 | #include <linux/if.h> // Needs definitions from netinet/in.h |
Darin Petkov | 0828f5f | 2011-08-11 10:18:52 -0700 | [diff] [blame] | 9 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 10 | #include <string> |
Chris Masone | 889666b | 2011-07-03 12:58:50 -0700 | [diff] [blame] | 11 | #include <utility> |
| 12 | #include <vector> |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 13 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 14 | #include <base/bind.h> |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame] | 15 | #include <base/callback.h> |
Darin Petkov | e9d12e0 | 2011-07-27 15:09:37 -0700 | [diff] [blame] | 16 | #include <base/stringprintf.h> |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 17 | #include <chromeos/dbus/service_constants.h> |
Darin Petkov | 137884a | 2011-10-26 18:52:47 +0200 | [diff] [blame] | 18 | #include <mobile_provider.h> |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 19 | |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 20 | #include "shill/adaptor_interfaces.h" |
Darin Petkov | daf4386 | 2011-10-27 11:37:28 +0200 | [diff] [blame] | 21 | #include "shill/cellular_capability_cdma.h" |
| 22 | #include "shill/cellular_capability_gsm.h" |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 23 | #include "shill/cellular_capability_universal.h" |
Arman Uguray | 72fab6a | 2013-01-10 19:32:42 -0800 | [diff] [blame] | 24 | #include "shill/cellular_capability_universal_cdma.h" |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 25 | #include "shill/cellular_service.h" |
| 26 | #include "shill/control_interface.h" |
| 27 | #include "shill/device.h" |
| 28 | #include "shill/device_info.h" |
Darin Petkov | 4d6d941 | 2011-08-24 13:19:54 -0700 | [diff] [blame] | 29 | #include "shill/error.h" |
Paul Stewart | 26b327e | 2011-10-19 11:38:09 -0700 | [diff] [blame] | 30 | #include "shill/event_dispatcher.h" |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 31 | #include "shill/logging.h" |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 32 | #include "shill/manager.h" |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 33 | #include "shill/profile.h" |
Chris Masone | 889666b | 2011-07-03 12:58:50 -0700 | [diff] [blame] | 34 | #include "shill/property_accessor.h" |
Darin Petkov | e9d12e0 | 2011-07-27 15:09:37 -0700 | [diff] [blame] | 35 | #include "shill/proxy_factory.h" |
Darin Petkov | 0828f5f | 2011-08-11 10:18:52 -0700 | [diff] [blame] | 36 | #include "shill/rtnl_handler.h" |
Jason Glasgow | 7b461df | 2012-05-01 16:38:45 -0400 | [diff] [blame] | 37 | #include "shill/store_interface.h" |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 38 | #include "shill/technology.h" |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 39 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 40 | using base::Bind; |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame] | 41 | using base::Closure; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 42 | using std::string; |
Chris Masone | 889666b | 2011-07-03 12:58:50 -0700 | [diff] [blame] | 43 | using std::vector; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 44 | |
| 45 | namespace shill { |
| 46 | |
Jason Glasgow | 7b461df | 2012-05-01 16:38:45 -0400 | [diff] [blame] | 47 | // static |
| 48 | const char Cellular::kAllowRoaming[] = "AllowRoaming"; |
| 49 | |
Darin Petkov | 3335b37 | 2011-08-22 11:05:32 -0700 | [diff] [blame] | 50 | Cellular::Operator::Operator() { |
| 51 | SetName(""); |
| 52 | SetCode(""); |
| 53 | SetCountry(""); |
| 54 | } |
| 55 | |
| 56 | Cellular::Operator::~Operator() {} |
| 57 | |
| 58 | void Cellular::Operator::CopyFrom(const Operator &oper) { |
| 59 | dict_ = oper.dict_; |
| 60 | } |
| 61 | |
| 62 | const string &Cellular::Operator::GetName() const { |
| 63 | return dict_.find(flimflam::kOperatorNameKey)->second; |
| 64 | } |
| 65 | |
| 66 | void Cellular::Operator::SetName(const string &name) { |
| 67 | dict_[flimflam::kOperatorNameKey] = name; |
| 68 | } |
| 69 | |
| 70 | const string &Cellular::Operator::GetCode() const { |
| 71 | return dict_.find(flimflam::kOperatorCodeKey)->second; |
| 72 | } |
| 73 | |
| 74 | void Cellular::Operator::SetCode(const string &code) { |
| 75 | dict_[flimflam::kOperatorCodeKey] = code; |
| 76 | } |
| 77 | |
| 78 | const string &Cellular::Operator::GetCountry() const { |
| 79 | return dict_.find(flimflam::kOperatorCountryKey)->second; |
| 80 | } |
| 81 | |
| 82 | void Cellular::Operator::SetCountry(const string &country) { |
| 83 | dict_[flimflam::kOperatorCountryKey] = country; |
| 84 | } |
| 85 | |
| 86 | const Stringmap &Cellular::Operator::ToDict() const { |
| 87 | return dict_; |
| 88 | } |
| 89 | |
Prathmesh Prabhu | 27526f1 | 2013-03-25 19:42:18 -0700 | [diff] [blame] | 90 | Cellular::Cellular(ModemInfo *modem_info, |
Darin Petkov | e9d12e0 | 2011-07-27 15:09:37 -0700 | [diff] [blame] | 91 | const string &link_name, |
Darin Petkov | 3335b37 | 2011-08-22 11:05:32 -0700 | [diff] [blame] | 92 | const string &address, |
Darin Petkov | e9d12e0 | 2011-07-27 15:09:37 -0700 | [diff] [blame] | 93 | int interface_index, |
| 94 | Type type, |
| 95 | const string &owner, |
Jason Glasgow | a585fc3 | 2012-06-06 11:04:09 -0400 | [diff] [blame] | 96 | const string &service, |
Darin Petkov | 137884a | 2011-10-26 18:52:47 +0200 | [diff] [blame] | 97 | const string &path, |
Ben Chan | 3ecdf82 | 2012-08-06 12:29:23 -0700 | [diff] [blame] | 98 | ProxyFactory *proxy_factory) |
Prathmesh Prabhu | 27526f1 | 2013-03-25 19:42:18 -0700 | [diff] [blame] | 99 | : Device(modem_info->control_interface(), |
| 100 | modem_info->dispatcher(), |
| 101 | modem_info->metrics(), |
| 102 | modem_info->manager(), |
Darin Petkov | e9d12e0 | 2011-07-27 15:09:37 -0700 | [diff] [blame] | 103 | link_name, |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 104 | address, |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 105 | interface_index, |
| 106 | Technology::kCellular), |
Thieu Le | 64b0fe5 | 2012-08-08 14:57:36 -0700 | [diff] [blame] | 107 | weak_ptr_factory_(this), |
Darin Petkov | e9d12e0 | 2011-07-27 15:09:37 -0700 | [diff] [blame] | 108 | state_(kStateDisabled), |
Darin Petkov | bac9600 | 2011-08-09 13:22:00 -0700 | [diff] [blame] | 109 | modem_state_(kModemStateUnknown), |
Darin Petkov | e9d12e0 | 2011-07-27 15:09:37 -0700 | [diff] [blame] | 110 | dbus_owner_(owner), |
Jason Glasgow | a585fc3 | 2012-06-06 11:04:09 -0400 | [diff] [blame] | 111 | dbus_service_(service), |
Darin Petkov | e9d12e0 | 2011-07-27 15:09:37 -0700 | [diff] [blame] | 112 | dbus_path_(path), |
Prathmesh Prabhu | 27526f1 | 2013-03-25 19:42:18 -0700 | [diff] [blame] | 113 | modem_info_(modem_info), |
Ben Chan | 3ecdf82 | 2012-08-06 12:29:23 -0700 | [diff] [blame] | 114 | proxy_factory_(proxy_factory), |
Thieu Le | 26fc01b | 2013-01-28 12:08:48 -0800 | [diff] [blame] | 115 | allow_roaming_(false), |
| 116 | explicit_disconnect_(false) { |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 117 | PropertyStore *store = this->mutable_store(); |
Jason Glasgow | a585fc3 | 2012-06-06 11:04:09 -0400 | [diff] [blame] | 118 | // TODO(jglasgow): kDBusConnectionProperty is deprecated. |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 119 | store->RegisterConstString(flimflam::kDBusConnectionProperty, &dbus_owner_); |
Jason Glasgow | a585fc3 | 2012-06-06 11:04:09 -0400 | [diff] [blame] | 120 | store->RegisterConstString(flimflam::kDBusServiceProperty, &dbus_service_); |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 121 | store->RegisterConstString(flimflam::kDBusObjectProperty, &dbus_path_); |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 122 | HelpRegisterConstDerivedString(flimflam::kTechnologyFamilyProperty, |
| 123 | &Cellular::GetTechnologyFamily); |
Jason Glasgow | 7b461df | 2012-05-01 16:38:45 -0400 | [diff] [blame] | 124 | HelpRegisterDerivedBool(flimflam::kCellularAllowRoamingProperty, |
| 125 | &Cellular::GetAllowRoaming, |
| 126 | &Cellular::SetAllowRoaming); |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 127 | store->RegisterConstStringmap(flimflam::kHomeProviderProperty, |
Darin Petkov | 3335b37 | 2011-08-22 11:05:32 -0700 | [diff] [blame] | 128 | &home_provider_.ToDict()); |
Arman Uguray | c7b1560 | 2013-02-16 00:56:18 -0800 | [diff] [blame] | 129 | |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 130 | // For now, only a single capability is supported. |
Ben Chan | 3ecdf82 | 2012-08-06 12:29:23 -0700 | [diff] [blame] | 131 | InitCapability(type); |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 132 | |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 133 | SLOG(Cellular, 2) << "Cellular device " << this->link_name() |
| 134 | << " initialized."; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 137 | Cellular::~Cellular() { |
| 138 | } |
Darin Petkov | e9d12e0 | 2011-07-27 15:09:37 -0700 | [diff] [blame] | 139 | |
Jason Glasgow | 7b461df | 2012-05-01 16:38:45 -0400 | [diff] [blame] | 140 | bool Cellular::Load(StoreInterface *storage) { |
| 141 | const string id = GetStorageIdentifier(); |
| 142 | if (!storage->ContainsGroup(id)) { |
| 143 | LOG(WARNING) << "Device is not available in the persistent store: " << id; |
| 144 | return false; |
| 145 | } |
| 146 | storage->GetBool(id, kAllowRoaming, &allow_roaming_); |
| 147 | return Device::Load(storage); |
| 148 | } |
| 149 | |
| 150 | bool Cellular::Save(StoreInterface *storage) { |
| 151 | const string id = GetStorageIdentifier(); |
| 152 | storage->SetBool(id, kAllowRoaming, allow_roaming_); |
| 153 | return Device::Save(storage); |
| 154 | } |
| 155 | |
Darin Petkov | cc04442 | 2011-08-17 13:30:06 -0700 | [diff] [blame] | 156 | // static |
| 157 | string Cellular::GetStateString(State state) { |
| 158 | switch (state) { |
Darin Petkov | e9d12e0 | 2011-07-27 15:09:37 -0700 | [diff] [blame] | 159 | case kStateDisabled: return "CellularStateDisabled"; |
| 160 | case kStateEnabled: return "CellularStateEnabled"; |
| 161 | case kStateRegistered: return "CellularStateRegistered"; |
| 162 | case kStateConnected: return "CellularStateConnected"; |
Darin Petkov | 0828f5f | 2011-08-11 10:18:52 -0700 | [diff] [blame] | 163 | case kStateLinked: return "CellularStateLinked"; |
| 164 | default: NOTREACHED(); |
Darin Petkov | e9d12e0 | 2011-07-27 15:09:37 -0700 | [diff] [blame] | 165 | } |
Darin Petkov | cc04442 | 2011-08-17 13:30:06 -0700 | [diff] [blame] | 166 | return StringPrintf("CellularStateUnknown-%d", state); |
Darin Petkov | 0828f5f | 2011-08-11 10:18:52 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Eric Shienbrood | 0db6a9b | 2012-03-30 16:11:39 -0400 | [diff] [blame] | 169 | string Cellular::GetTechnologyFamily(Error *error) { |
| 170 | return capability_->GetTypeString(); |
| 171 | } |
| 172 | |
Darin Petkov | 0828f5f | 2011-08-11 10:18:52 -0700 | [diff] [blame] | 173 | void Cellular::SetState(State state) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 174 | SLOG(Cellular, 2) << GetStateString(state_) << " -> " |
| 175 | << GetStateString(state); |
Darin Petkov | 0828f5f | 2011-08-11 10:18:52 -0700 | [diff] [blame] | 176 | state_ = state; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Jason Glasgow | 7b461df | 2012-05-01 16:38:45 -0400 | [diff] [blame] | 179 | void Cellular::HelpRegisterDerivedBool( |
| 180 | const string &name, |
| 181 | bool(Cellular::*get)(Error *error), |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 182 | bool(Cellular::*set)(const bool &value, Error *error)) { |
Jason Glasgow | 7b461df | 2012-05-01 16:38:45 -0400 | [diff] [blame] | 183 | mutable_store()->RegisterDerivedBool( |
| 184 | name, |
| 185 | BoolAccessor( |
| 186 | new CustomAccessor<Cellular, bool>(this, get, set))); |
| 187 | } |
| 188 | |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 189 | void Cellular::HelpRegisterConstDerivedString( |
Eric Shienbrood | 0db6a9b | 2012-03-30 16:11:39 -0400 | [diff] [blame] | 190 | const string &name, |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 191 | string(Cellular::*get)(Error *)) { |
Eric Shienbrood | 0db6a9b | 2012-03-30 16:11:39 -0400 | [diff] [blame] | 192 | mutable_store()->RegisterDerivedString( |
| 193 | name, |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 194 | StringAccessor(new CustomAccessor<Cellular, string>(this, get, NULL))); |
Eric Shienbrood | 0db6a9b | 2012-03-30 16:11:39 -0400 | [diff] [blame] | 195 | } |
| 196 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 197 | void Cellular::Start(Error *error, |
| 198 | const EnabledStateChangedCallback &callback) { |
Jason Glasgow | 4a49079 | 2012-04-10 15:02:05 -0400 | [diff] [blame] | 199 | DCHECK(error); |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 200 | SLOG(Cellular, 2) << __func__ << ": " << GetStateString(state_); |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 201 | if (state_ != kStateDisabled) { |
| 202 | return; |
| 203 | } |
Thieu Le | 64b0fe5 | 2012-08-08 14:57:36 -0700 | [diff] [blame] | 204 | ResultCallback cb = Bind(&Cellular::StartModemCallback, |
| 205 | weak_ptr_factory_.GetWeakPtr(), |
| 206 | callback); |
| 207 | capability_->StartModem(error, cb); |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 210 | void Cellular::Stop(Error *error, |
| 211 | const EnabledStateChangedCallback &callback) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 212 | SLOG(Cellular, 2) << __func__ << ": " << GetStateString(state_); |
Thieu Le | 26fc01b | 2013-01-28 12:08:48 -0800 | [diff] [blame] | 213 | explicit_disconnect_ = true; |
Thieu Le | 64b0fe5 | 2012-08-08 14:57:36 -0700 | [diff] [blame] | 214 | ResultCallback cb = Bind(&Cellular::StopModemCallback, |
| 215 | weak_ptr_factory_.GetWeakPtr(), |
| 216 | callback); |
| 217 | capability_->StopModem(error, cb); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 218 | } |
| 219 | |
Eric Shienbrood | 7fce52c | 2012-04-13 19:11:02 -0400 | [diff] [blame] | 220 | bool Cellular::IsUnderlyingDeviceEnabled() const { |
| 221 | return IsEnabledModemState(modem_state_); |
| 222 | } |
| 223 | |
Thieu Le | d001205 | 2012-07-25 16:09:09 -0700 | [diff] [blame] | 224 | bool Cellular::IsModemRegistered() const { |
| 225 | return (modem_state_ == Cellular::kModemStateRegistered || |
| 226 | modem_state_ == Cellular::kModemStateConnecting || |
| 227 | modem_state_ == Cellular::kModemStateConnected); |
| 228 | } |
| 229 | |
Eric Shienbrood | 7fce52c | 2012-04-13 19:11:02 -0400 | [diff] [blame] | 230 | // static |
| 231 | bool Cellular::IsEnabledModemState(ModemState state) { |
| 232 | switch (state) { |
| 233 | case kModemStateUnknown: |
| 234 | case kModemStateDisabled: |
| 235 | case kModemStateInitializing: |
| 236 | case kModemStateLocked: |
| 237 | case kModemStateDisabling: |
| 238 | case kModemStateEnabling: |
| 239 | return false; |
| 240 | case kModemStateEnabled: |
| 241 | case kModemStateSearching: |
| 242 | case kModemStateRegistered: |
| 243 | case kModemStateDisconnecting: |
| 244 | case kModemStateConnecting: |
| 245 | case kModemStateConnected: |
| 246 | return true; |
| 247 | } |
| 248 | return false; |
| 249 | } |
| 250 | |
Thieu Le | 37b9003 | 2012-05-15 15:18:41 -0700 | [diff] [blame] | 251 | void Cellular::StartModemCallback(const EnabledStateChangedCallback &callback, |
| 252 | const Error &error) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 253 | SLOG(Cellular, 2) << __func__ << ": " << GetStateString(state_); |
Gary Morain | baeefdf | 2012-04-30 14:53:35 -0700 | [diff] [blame] | 254 | if (error.IsSuccess() && (state_ == kStateDisabled)) { |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 255 | SetState(kStateEnabled); |
Jason Glasgow | 4380f0d | 2012-05-03 18:05:04 -0400 | [diff] [blame] | 256 | // Registration state updates may have been ignored while the |
| 257 | // modem was not yet marked enabled. |
| 258 | HandleNewRegistrationState(); |
Arman Uguray | f84a424 | 2013-04-09 20:01:07 -0700 | [diff] [blame] | 259 | if (capability_->ShouldDetectOutOfCredit()) |
Arman Uguray | d42d8ec | 2013-04-08 19:28:21 -0700 | [diff] [blame] | 260 | set_traffic_monitor_enabled(true); |
Jason Glasgow | 4380f0d | 2012-05-03 18:05:04 -0400 | [diff] [blame] | 261 | } |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 262 | callback.Run(error); |
| 263 | } |
| 264 | |
Thieu Le | 37b9003 | 2012-05-15 15:18:41 -0700 | [diff] [blame] | 265 | void Cellular::StopModemCallback(const EnabledStateChangedCallback &callback, |
| 266 | const Error &error) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 267 | SLOG(Cellular, 2) << __func__ << ": " << GetStateString(state_); |
Thieu Le | 26fc01b | 2013-01-28 12:08:48 -0800 | [diff] [blame] | 268 | explicit_disconnect_ = false; |
Thieu Le | 37b9003 | 2012-05-15 15:18:41 -0700 | [diff] [blame] | 269 | // Destroy the cellular service regardless of any errors that occur during |
| 270 | // the stop process since we do not know the state of the modem at this |
| 271 | // point. |
| 272 | DestroyService(); |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 273 | if (state_ != kStateDisabled) |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 274 | SetState(kStateDisabled); |
Arman Uguray | d42d8ec | 2013-04-08 19:28:21 -0700 | [diff] [blame] | 275 | set_traffic_monitor_enabled(false); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 276 | callback.Run(error); |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 277 | } |
| 278 | |
Ben Chan | 3ecdf82 | 2012-08-06 12:29:23 -0700 | [diff] [blame] | 279 | void Cellular::InitCapability(Type type) { |
Darin Petkov | 5f316f6 | 2011-11-18 12:10:26 +0100 | [diff] [blame] | 280 | // TODO(petkov): Consider moving capability construction into a factory that's |
| 281 | // external to the Cellular class. |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 282 | SLOG(Cellular, 2) << __func__ << "(" << type << ")"; |
Darin Petkov | 5f316f6 | 2011-11-18 12:10:26 +0100 | [diff] [blame] | 283 | switch (type) { |
Darin Petkov | daf4386 | 2011-10-27 11:37:28 +0200 | [diff] [blame] | 284 | case kTypeGSM: |
Thieu Le | ce4483e | 2013-01-23 15:12:03 -0800 | [diff] [blame] | 285 | capability_.reset(new CellularCapabilityGSM(this, |
| 286 | proxy_factory_, |
Prathmesh Prabhu | 27526f1 | 2013-03-25 19:42:18 -0700 | [diff] [blame] | 287 | modem_info_)); |
Darin Petkov | daf4386 | 2011-10-27 11:37:28 +0200 | [diff] [blame] | 288 | break; |
| 289 | case kTypeCDMA: |
Thieu Le | ce4483e | 2013-01-23 15:12:03 -0800 | [diff] [blame] | 290 | capability_.reset(new CellularCapabilityCDMA(this, |
| 291 | proxy_factory_, |
Prathmesh Prabhu | 27526f1 | 2013-03-25 19:42:18 -0700 | [diff] [blame] | 292 | modem_info_)); |
Darin Petkov | daf4386 | 2011-10-27 11:37:28 +0200 | [diff] [blame] | 293 | break; |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 294 | case kTypeUniversal: |
Arman Uguray | c7b1560 | 2013-02-16 00:56:18 -0800 | [diff] [blame] | 295 | capability_.reset(new CellularCapabilityUniversal( |
| 296 | this, |
| 297 | proxy_factory_, |
Prathmesh Prabhu | 27526f1 | 2013-03-25 19:42:18 -0700 | [diff] [blame] | 298 | modem_info_)); |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 299 | break; |
Arman Uguray | 72fab6a | 2013-01-10 19:32:42 -0800 | [diff] [blame] | 300 | case kTypeUniversalCDMA: |
| 301 | capability_.reset(new CellularCapabilityUniversalCDMA( |
| 302 | this, |
| 303 | proxy_factory_, |
| 304 | modem_info_)); |
| 305 | break; |
Darin Petkov | daf4386 | 2011-10-27 11:37:28 +0200 | [diff] [blame] | 306 | default: NOTREACHED(); |
| 307 | } |
| 308 | } |
| 309 | |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 310 | void Cellular::Activate(const string &carrier, |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 311 | Error *error, const ResultCallback &callback) { |
| 312 | capability_->Activate(carrier, error, callback); |
Darin Petkov | 9ae310f | 2011-08-30 15:41:13 -0700 | [diff] [blame] | 313 | } |
| 314 | |
Arman Uguray | c7b1560 | 2013-02-16 00:56:18 -0800 | [diff] [blame] | 315 | void Cellular::CompleteActivation(Error *error) { |
| 316 | capability_->CompleteActivation(error); |
| 317 | } |
| 318 | |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 319 | void Cellular::RegisterOnNetwork(const string &network_id, |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 320 | Error *error, |
| 321 | const ResultCallback &callback) { |
| 322 | capability_->RegisterOnNetwork(network_id, error, callback); |
Darin Petkov | a3d3be5 | 2011-11-14 21:34:16 +0100 | [diff] [blame] | 323 | } |
| 324 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 325 | void Cellular::RequirePIN(const string &pin, bool require, |
| 326 | Error *error, const ResultCallback &callback) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 327 | SLOG(Cellular, 2) << __func__ << "(" << require << ")"; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 328 | capability_->RequirePIN(pin, require, error, callback); |
Darin Petkov | e42e101 | 2011-08-31 12:35:04 -0700 | [diff] [blame] | 329 | } |
| 330 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 331 | void Cellular::EnterPIN(const string &pin, |
| 332 | Error *error, const ResultCallback &callback) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 333 | SLOG(Cellular, 2) << __func__; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 334 | capability_->EnterPIN(pin, error, callback); |
Darin Petkov | e42e101 | 2011-08-31 12:35:04 -0700 | [diff] [blame] | 335 | } |
| 336 | |
Darin Petkov | c64fe5e | 2012-01-11 12:46:13 +0100 | [diff] [blame] | 337 | void Cellular::UnblockPIN(const string &unblock_code, |
| 338 | const string &pin, |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 339 | Error *error, const ResultCallback &callback) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 340 | SLOG(Cellular, 2) << __func__; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 341 | capability_->UnblockPIN(unblock_code, pin, error, callback); |
Darin Petkov | e42e101 | 2011-08-31 12:35:04 -0700 | [diff] [blame] | 342 | } |
| 343 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 344 | void Cellular::ChangePIN(const string &old_pin, const string &new_pin, |
| 345 | Error *error, const ResultCallback &callback) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 346 | SLOG(Cellular, 2) << __func__; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 347 | capability_->ChangePIN(old_pin, new_pin, error, callback); |
Darin Petkov | e42e101 | 2011-08-31 12:35:04 -0700 | [diff] [blame] | 348 | } |
| 349 | |
Ben Chan | 5d0d32c | 2013-01-08 02:05:29 -0800 | [diff] [blame] | 350 | void Cellular::Reset(Error *error, const ResultCallback &callback) { |
| 351 | SLOG(Cellular, 2) << __func__; |
| 352 | capability_->Reset(error, callback); |
| 353 | } |
| 354 | |
Darin Petkov | c37a9c4 | 2012-09-06 15:28:22 +0200 | [diff] [blame] | 355 | void Cellular::SetCarrier(const string &carrier, |
| 356 | Error *error, const ResultCallback &callback) { |
| 357 | SLOG(Cellular, 2) << __func__ << "(" << carrier << ")"; |
| 358 | capability_->SetCarrier(carrier, error, callback); |
| 359 | } |
| 360 | |
Arman Uguray | d42d8ec | 2013-04-08 19:28:21 -0700 | [diff] [blame] | 361 | void Cellular::OnNoNetworkRouting() { |
| 362 | SLOG(Cellular, 2) << __func__; |
| 363 | Device::OnNoNetworkRouting(); |
Arman Uguray | f84a424 | 2013-04-09 20:01:07 -0700 | [diff] [blame] | 364 | |
| 365 | SLOG(Cellular, 2) << "Requesting active probe for out-of-credit detection."; |
| 366 | RequestConnectionHealthCheck(); |
| 367 | } |
| 368 | |
| 369 | void Cellular::OnConnectionHealthCheckerResult( |
| 370 | ConnectionHealthChecker::Result result) { |
Prathmesh Prabhu | 5489b7a | 2013-04-10 13:33:59 -0700 | [diff] [blame] | 371 | SLOG(Cellular, 2) << __func__ << "(Result = " |
| 372 | << ConnectionHealthChecker::ResultToString(result) << ")"; |
Arman Uguray | f84a424 | 2013-04-09 20:01:07 -0700 | [diff] [blame] | 373 | |
Prathmesh Prabhu | 3fd54bd | 2013-05-08 16:36:57 -0700 | [diff] [blame] | 374 | if (result == ConnectionHealthChecker::kResultCongestedTxQueue) { |
Arman Uguray | f84a424 | 2013-04-09 20:01:07 -0700 | [diff] [blame] | 375 | SLOG(Cellular, 2) << "Active probe determined possible out-of-credits " |
| 376 | << "scenario."; |
| 377 | if (service().get()) { |
Thieu Le | 91fccf6 | 2013-04-22 15:23:16 -0700 | [diff] [blame] | 378 | Metrics::CellularOutOfCreditsReason reason = |
| 379 | (result == ConnectionHealthChecker::kResultCongestedTxQueue) ? |
| 380 | Metrics::kCellularOutOfCreditsReasonTxCongested : |
| 381 | Metrics::kCellularOutOfCreditsReasonElongatedTimeWait; |
| 382 | metrics()->NotifyCellularOutOfCredits(reason); |
| 383 | |
Arman Uguray | f84a424 | 2013-04-09 20:01:07 -0700 | [diff] [blame] | 384 | service()->SetOutOfCredits(true); |
| 385 | SLOG(Cellular, 2) << "Disconnecting due to out-of-credit scenario."; |
| 386 | Error error; |
| 387 | service()->Disconnect(&error); |
| 388 | } |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | void Cellular::PortalDetectorCallback(const PortalDetector::Result &result) { |
| 393 | Device::PortalDetectorCallback(result); |
| 394 | if (result.status != PortalDetector::kStatusSuccess && |
| 395 | capability_->ShouldDetectOutOfCredit()) { |
| 396 | SLOG(Cellular, 2) << "Portal detection failed. Launching active probe for " |
| 397 | << "out-of-credit detection."; |
| 398 | RequestConnectionHealthCheck(); |
| 399 | } |
Arman Uguray | d42d8ec | 2013-04-08 19:28:21 -0700 | [diff] [blame] | 400 | } |
| 401 | |
Wade Guthrie | 68d4109 | 2013-04-02 12:56:02 -0700 | [diff] [blame] | 402 | void Cellular::Scan(ScanType scan_type, Error *error) { |
| 403 | // |scan_type| is ignored because Cellular only does a full scan. |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 404 | // TODO(ers): for now report immediate success or failure. |
| 405 | capability_->Scan(error, ResultCallback()); |
Darin Petkov | ceb6817 | 2011-07-29 14:47:48 -0700 | [diff] [blame] | 406 | } |
| 407 | |
Darin Petkov | d966195 | 2011-08-03 16:25:42 -0700 | [diff] [blame] | 408 | void Cellular::HandleNewRegistrationState() { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 409 | SLOG(Cellular, 2) << __func__ << ": " << GetStateString(state_); |
Ben Chan | 09fa2a0 | 2012-11-07 22:09:09 -0800 | [diff] [blame] | 410 | if (capability_->IsServiceActivationRequired()) { |
| 411 | if (state_ == kStateEnabled && !service_.get()) { |
| 412 | CreateService(); |
| 413 | } |
| 414 | return; |
| 415 | } |
Darin Petkov | b72cf40 | 2011-11-22 14:51:39 +0100 | [diff] [blame] | 416 | if (!capability_->IsRegistered()) { |
Thieu Le | 26fc01b | 2013-01-28 12:08:48 -0800 | [diff] [blame] | 417 | if (!explicit_disconnect_ && |
| 418 | (state_ == kStateLinked || state_ == kStateConnected) && |
| 419 | service_.get()) |
| 420 | metrics()->NotifyCellularDeviceDrop( |
Thieu Le | 6c1e3bb | 2013-02-06 15:20:35 -0800 | [diff] [blame] | 421 | interface_index(), |
| 422 | capability_->GetNetworkTechnologyString(), |
| 423 | service_->strength()); |
Darin Petkov | 2c37738 | 2012-01-11 11:40:43 +0100 | [diff] [blame] | 424 | DestroyService(); |
Darin Petkov | 0828f5f | 2011-08-11 10:18:52 -0700 | [diff] [blame] | 425 | if (state_ == kStateLinked || |
| 426 | state_ == kStateConnected || |
| 427 | state_ == kStateRegistered) { |
| 428 | SetState(kStateEnabled); |
Darin Petkov | d966195 | 2011-08-03 16:25:42 -0700 | [diff] [blame] | 429 | } |
| 430 | return; |
| 431 | } |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 432 | // In Disabled state, defer creating a service until fully |
| 433 | // enabled. UI will ignore the appearance of a new service |
| 434 | // on a disabled device. |
| 435 | if (state_ == kStateDisabled) { |
| 436 | return; |
| 437 | } |
Darin Petkov | d966195 | 2011-08-03 16:25:42 -0700 | [diff] [blame] | 438 | if (state_ == kStateEnabled) { |
Darin Petkov | 0828f5f | 2011-08-11 10:18:52 -0700 | [diff] [blame] | 439 | SetState(kStateRegistered); |
Darin Petkov | d966195 | 2011-08-03 16:25:42 -0700 | [diff] [blame] | 440 | } |
| 441 | if (!service_.get()) { |
Thieu Le | 18c1107 | 2013-01-28 17:21:37 -0800 | [diff] [blame] | 442 | metrics()->NotifyDeviceScanFinished(interface_index()); |
Darin Petkov | d966195 | 2011-08-03 16:25:42 -0700 | [diff] [blame] | 443 | CreateService(); |
| 444 | } |
Darin Petkov | 3e50924 | 2011-11-10 14:46:44 +0100 | [diff] [blame] | 445 | capability_->GetSignalQuality(); |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 446 | if (state_ == kStateRegistered && modem_state_ == kModemStateConnected) |
| 447 | OnConnected(); |
Darin Petkov | b72cf40 | 2011-11-22 14:51:39 +0100 | [diff] [blame] | 448 | service_->SetNetworkTechnology(capability_->GetNetworkTechnologyString()); |
| 449 | service_->SetRoamingState(capability_->GetRoamingStateString()); |
Christopher Wiley | 9169d25 | 2012-11-30 15:13:39 -0800 | [diff] [blame] | 450 | manager()->UpdateService(service_); |
Darin Petkov | d966195 | 2011-08-03 16:25:42 -0700 | [diff] [blame] | 451 | } |
| 452 | |
Darin Petkov | d966195 | 2011-08-03 16:25:42 -0700 | [diff] [blame] | 453 | void Cellular::HandleNewSignalQuality(uint32 strength) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 454 | SLOG(Cellular, 2) << "Signal strength: " << strength; |
Darin Petkov | d78ee7e | 2012-01-12 11:21:10 +0100 | [diff] [blame] | 455 | if (service_) { |
| 456 | service_->SetStrength(strength); |
Darin Petkov | d966195 | 2011-08-03 16:25:42 -0700 | [diff] [blame] | 457 | } |
| 458 | } |
| 459 | |
| 460 | void Cellular::CreateService() { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 461 | SLOG(Cellular, 2) << __func__; |
Darin Petkov | d966195 | 2011-08-03 16:25:42 -0700 | [diff] [blame] | 462 | CHECK(!service_.get()); |
Prathmesh Prabhu | 0d36b4f | 2013-04-01 11:45:54 -0700 | [diff] [blame] | 463 | service_ = new CellularService(modem_info_, this); |
Darin Petkov | ae0c64e | 2011-11-15 15:50:27 +0100 | [diff] [blame] | 464 | capability_->OnServiceCreated(); |
Darin Petkov | 3133241 | 2012-01-28 01:50:02 +0100 | [diff] [blame] | 465 | manager()->RegisterService(service_); |
Darin Petkov | f5f61e0 | 2011-07-29 11:35:40 -0700 | [diff] [blame] | 466 | } |
| 467 | |
Darin Petkov | 2c37738 | 2012-01-11 11:40:43 +0100 | [diff] [blame] | 468 | void Cellular::DestroyService() { |
Darin Petkov | 0f5dfa0 | 2012-11-14 16:35:32 +0100 | [diff] [blame] | 469 | DropConnection(); |
Darin Petkov | 2c37738 | 2012-01-11 11:40:43 +0100 | [diff] [blame] | 470 | if (service_) { |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 471 | LOG(INFO) << "Deregistering cellular service " << service_->unique_name() |
Darin Petkov | 0f5dfa0 | 2012-11-14 16:35:32 +0100 | [diff] [blame] | 472 | << " for device " << link_name(); |
Darin Petkov | 2c37738 | 2012-01-11 11:40:43 +0100 | [diff] [blame] | 473 | manager()->DeregisterService(service_); |
| 474 | service_ = NULL; |
| 475 | } |
Darin Petkov | 2c37738 | 2012-01-11 11:40:43 +0100 | [diff] [blame] | 476 | } |
| 477 | |
Darin Petkov | 4d6d941 | 2011-08-24 13:19:54 -0700 | [diff] [blame] | 478 | void Cellular::Connect(Error *error) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 479 | SLOG(Cellular, 2) << __func__; |
Eric Shienbrood | 30bc0ec | 2012-03-21 18:19:46 -0400 | [diff] [blame] | 480 | if (state_ == kStateConnected || state_ == kStateLinked) { |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame] | 481 | Error::PopulateAndLog(error, Error::kAlreadyConnected, |
| 482 | "Already connected; connection request ignored."); |
Darin Petkov | c5f5656 | 2011-08-06 16:40:05 -0700 | [diff] [blame] | 483 | return; |
Thieu Le | c7d8cd1 | 2013-02-13 11:38:14 -0800 | [diff] [blame] | 484 | } else if (state_ != kStateRegistered) { |
| 485 | Error::PopulateAndLog(error, Error::kNotRegistered, |
| 486 | "Modem not registered; connection request ignored."); |
| 487 | return; |
Darin Petkov | c5f5656 | 2011-08-06 16:40:05 -0700 | [diff] [blame] | 488 | } |
Darin Petkov | d204580 | 2011-08-23 11:09:25 -0700 | [diff] [blame] | 489 | |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 490 | if (!capability_->AllowRoaming() && |
Darin Petkov | d204580 | 2011-08-23 11:09:25 -0700 | [diff] [blame] | 491 | service_->roaming_state() == flimflam::kRoamingStateRoaming) { |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame] | 492 | Error::PopulateAndLog(error, Error::kNotOnHomeNetwork, |
| 493 | "Roaming disallowed; connection request ignored."); |
Darin Petkov | d204580 | 2011-08-23 11:09:25 -0700 | [diff] [blame] | 494 | return; |
| 495 | } |
| 496 | |
Darin Petkov | c5f5656 | 2011-08-06 16:40:05 -0700 | [diff] [blame] | 497 | DBusPropertiesMap properties; |
Darin Petkov | ae0c64e | 2011-11-15 15:50:27 +0100 | [diff] [blame] | 498 | capability_->SetupConnectProperties(&properties); |
Thieu Le | 64b0fe5 | 2012-08-08 14:57:36 -0700 | [diff] [blame] | 499 | ResultCallback cb = Bind(&Cellular::OnConnectReply, |
| 500 | weak_ptr_factory_.GetWeakPtr()); |
Eric Shienbrood | 7fce52c | 2012-04-13 19:11:02 -0400 | [diff] [blame] | 501 | OnConnecting(); |
Nathan Williams | b54974f | 2012-04-19 11:16:30 -0400 | [diff] [blame] | 502 | capability_->Connect(properties, error, cb); |
Thieu Le | 7cf36b0 | 2013-01-30 17:15:56 -0800 | [diff] [blame] | 503 | if (!error->IsSuccess()) |
| 504 | return; |
| 505 | |
| 506 | bool is_auto_connecting = service_.get() && service_->is_auto_connecting(); |
| 507 | metrics()->NotifyDeviceConnectStarted(interface_index(), is_auto_connecting); |
Nathan Williams | b54974f | 2012-04-19 11:16:30 -0400 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | // Note that there's no ResultCallback argument to this, |
| 511 | // since Connect() isn't yet passed one. |
| 512 | void Cellular::OnConnectReply(const Error &error) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 513 | SLOG(Cellular, 2) << __func__ << "(" << error << ")"; |
Thieu Le | cdb5a21 | 2013-01-25 11:17:18 -0800 | [diff] [blame] | 514 | if (error.IsSuccess()) { |
| 515 | metrics()->NotifyDeviceConnectFinished(interface_index()); |
Nathan Williams | b54974f | 2012-04-19 11:16:30 -0400 | [diff] [blame] | 516 | OnConnected(); |
Thieu Le | cdb5a21 | 2013-01-25 11:17:18 -0800 | [diff] [blame] | 517 | } else { |
Thieu Le | b7aa5f7 | 2013-01-31 15:57:48 -0800 | [diff] [blame] | 518 | metrics()->NotifyCellularDeviceFailure(error); |
Nathan Williams | b54974f | 2012-04-19 11:16:30 -0400 | [diff] [blame] | 519 | OnConnectFailed(error); |
Thieu Le | cdb5a21 | 2013-01-25 11:17:18 -0800 | [diff] [blame] | 520 | } |
Darin Petkov | c5f5656 | 2011-08-06 16:40:05 -0700 | [diff] [blame] | 521 | } |
| 522 | |
Eric Shienbrood | 7fce52c | 2012-04-13 19:11:02 -0400 | [diff] [blame] | 523 | void Cellular::OnConnecting() { |
| 524 | if (service_) |
| 525 | service_->SetState(Service::kStateAssociating); |
| 526 | } |
| 527 | |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 528 | void Cellular::OnConnected() { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 529 | SLOG(Cellular, 2) << __func__; |
Eric Shienbrood | 7fce52c | 2012-04-13 19:11:02 -0400 | [diff] [blame] | 530 | if (state_ == kStateConnected || state_ == kStateLinked) { |
Ben Chan | 09fa2a0 | 2012-11-07 22:09:09 -0800 | [diff] [blame] | 531 | SLOG(Cellular, 2) << "Already connected"; |
Eric Shienbrood | 7fce52c | 2012-04-13 19:11:02 -0400 | [diff] [blame] | 532 | return; |
| 533 | } |
Darin Petkov | 1c01bef | 2012-09-13 15:27:17 +0200 | [diff] [blame] | 534 | Closure start_cb = Bind(&Cellular::StartTermination, |
| 535 | weak_ptr_factory_.GetWeakPtr()); |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame] | 536 | manager()->AddTerminationAction(FriendlyName(), start_cb); |
Darin Petkov | 0828f5f | 2011-08-11 10:18:52 -0700 | [diff] [blame] | 537 | SetState(kStateConnected); |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 538 | if (!capability_->AllowRoaming() && |
Darin Petkov | 9c1dcef | 2012-02-07 15:58:26 +0100 | [diff] [blame] | 539 | service_->roaming_state() == flimflam::kRoamingStateRoaming) { |
Arman Uguray | 539c423 | 2012-09-11 10:00:22 -0700 | [diff] [blame] | 540 | LOG(INFO) << "Disconnecting due to roaming."; |
Darin Petkov | 9c1dcef | 2012-02-07 15:58:26 +0100 | [diff] [blame] | 541 | Disconnect(NULL); |
| 542 | } else { |
| 543 | EstablishLink(); |
| 544 | } |
Darin Petkov | bac9600 | 2011-08-09 13:22:00 -0700 | [diff] [blame] | 545 | } |
| 546 | |
Eric Shienbrood | 30bc0ec | 2012-03-21 18:19:46 -0400 | [diff] [blame] | 547 | void Cellular::OnConnectFailed(const Error &error) { |
Thieu Le | b5954a2 | 2012-05-18 10:37:34 -0700 | [diff] [blame] | 548 | if (service_) |
| 549 | service_->SetFailure(Service::kFailureUnknown); |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 550 | } |
| 551 | |
Christopher Wiley | 8a46890 | 2012-11-30 11:52:38 -0800 | [diff] [blame] | 552 | void Cellular::Disconnect(Error *error) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 553 | SLOG(Cellular, 2) << __func__; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 554 | if (state_ != kStateConnected && state_ != kStateLinked) { |
Darin Petkov | fb0625e | 2012-01-16 13:05:56 +0100 | [diff] [blame] | 555 | Error::PopulateAndLog( |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 556 | error, Error::kNotConnected, "Not connected; request ignored."); |
Darin Petkov | fb0625e | 2012-01-16 13:05:56 +0100 | [diff] [blame] | 557 | return; |
| 558 | } |
Thieu Le | 26fc01b | 2013-01-28 12:08:48 -0800 | [diff] [blame] | 559 | explicit_disconnect_ = true; |
Thieu Le | 64b0fe5 | 2012-08-08 14:57:36 -0700 | [diff] [blame] | 560 | ResultCallback cb = Bind(&Cellular::OnDisconnectReply, |
Christopher Wiley | 8a46890 | 2012-11-30 11:52:38 -0800 | [diff] [blame] | 561 | weak_ptr_factory_.GetWeakPtr()); |
Nathan Williams | b54974f | 2012-04-19 11:16:30 -0400 | [diff] [blame] | 562 | capability_->Disconnect(error, cb); |
| 563 | } |
| 564 | |
Christopher Wiley | 8a46890 | 2012-11-30 11:52:38 -0800 | [diff] [blame] | 565 | void Cellular::OnDisconnectReply(const Error &error) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 566 | SLOG(Cellular, 2) << __func__ << "(" << error << ")"; |
Thieu Le | 26fc01b | 2013-01-28 12:08:48 -0800 | [diff] [blame] | 567 | explicit_disconnect_ = false; |
Thieu Le | b7aa5f7 | 2013-01-31 15:57:48 -0800 | [diff] [blame] | 568 | if (error.IsSuccess()) { |
Nathan Williams | b54974f | 2012-04-19 11:16:30 -0400 | [diff] [blame] | 569 | OnDisconnected(); |
Thieu Le | b7aa5f7 | 2013-01-31 15:57:48 -0800 | [diff] [blame] | 570 | } else { |
| 571 | metrics()->NotifyCellularDeviceFailure(error); |
Nathan Williams | b54974f | 2012-04-19 11:16:30 -0400 | [diff] [blame] | 572 | OnDisconnectFailed(); |
Thieu Le | b7aa5f7 | 2013-01-31 15:57:48 -0800 | [diff] [blame] | 573 | } |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame] | 574 | manager()->TerminationActionComplete(FriendlyName()); |
| 575 | manager()->RemoveTerminationAction(FriendlyName()); |
Darin Petkov | fb0625e | 2012-01-16 13:05:56 +0100 | [diff] [blame] | 576 | } |
| 577 | |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 578 | void Cellular::OnDisconnected() { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 579 | SLOG(Cellular, 2) << __func__; |
Arman Uguray | 539c423 | 2012-09-11 10:00:22 -0700 | [diff] [blame] | 580 | if (!DisconnectCleanup()) { |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 581 | LOG(WARNING) << "Disconnect occurred while in state " |
| 582 | << GetStateString(state_); |
Eric Shienbrood | cc95c5d | 2012-03-30 15:25:49 -0400 | [diff] [blame] | 583 | } |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | void Cellular::OnDisconnectFailed() { |
Arman Uguray | 539c423 | 2012-09-11 10:00:22 -0700 | [diff] [blame] | 587 | SLOG(Cellular, 2) << __func__; |
| 588 | // If the modem is in the disconnecting state, then |
| 589 | // the disconnect should eventually succeed, so do |
| 590 | // nothing. |
| 591 | if (modem_state_ == kModemStateDisconnecting) { |
| 592 | LOG(WARNING) << "Ignoring failed disconnect while modem is disconnecting."; |
| 593 | return; |
| 594 | } |
| 595 | |
| 596 | // OnDisconnectFailed got called because no bearers |
| 597 | // to disconnect were found. Which means that we shouldn't |
| 598 | // really remain in the connected/linked state if we |
| 599 | // are in one of those. |
| 600 | if (!DisconnectCleanup()) { |
| 601 | // otherwise, no-op |
| 602 | LOG(WARNING) << "Ignoring failed disconnect while in state " |
| 603 | << GetStateString(state_); |
| 604 | } |
| 605 | |
| 606 | // TODO(armansito): In either case, shill ends up thinking |
| 607 | // that it's disconnected, while for some reason the underlying |
| 608 | // modem might still actually be connected. In that case the UI |
| 609 | // would be reflecting an incorrect state and a further connection |
| 610 | // request would fail. We should perhaps tear down the modem and |
| 611 | // restart it here. |
Darin Petkov | fb0625e | 2012-01-16 13:05:56 +0100 | [diff] [blame] | 612 | } |
| 613 | |
Darin Petkov | bac9600 | 2011-08-09 13:22:00 -0700 | [diff] [blame] | 614 | void Cellular::EstablishLink() { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 615 | SLOG(Cellular, 2) << __func__; |
Darin Petkov | 0828f5f | 2011-08-11 10:18:52 -0700 | [diff] [blame] | 616 | CHECK_EQ(kStateConnected, state_); |
| 617 | unsigned int flags = 0; |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 618 | if (manager()->device_info()->GetFlags(interface_index(), &flags) && |
Darin Petkov | 0828f5f | 2011-08-11 10:18:52 -0700 | [diff] [blame] | 619 | (flags & IFF_UP) != 0) { |
| 620 | LinkEvent(flags, IFF_UP); |
| 621 | return; |
| 622 | } |
| 623 | // TODO(petkov): Provide a timeout for a failed link-up request. |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 624 | rtnl_handler()->SetInterfaceFlags(interface_index(), IFF_UP, IFF_UP); |
Arman Uguray | 32c7640 | 2012-11-27 14:01:13 -0800 | [diff] [blame] | 625 | |
| 626 | // Set state to associating. |
| 627 | OnConnecting(); |
Darin Petkov | 0828f5f | 2011-08-11 10:18:52 -0700 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | void Cellular::LinkEvent(unsigned int flags, unsigned int change) { |
| 631 | Device::LinkEvent(flags, change); |
| 632 | if ((flags & IFF_UP) != 0 && state_ == kStateConnected) { |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 633 | LOG(INFO) << link_name() << " is up."; |
Darin Petkov | 0828f5f | 2011-08-11 10:18:52 -0700 | [diff] [blame] | 634 | SetState(kStateLinked); |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 635 | if (AcquireIPConfig()) { |
Darin Petkov | 60b8c3b | 2011-08-25 11:03:20 -0700 | [diff] [blame] | 636 | SelectService(service_); |
| 637 | SetServiceState(Service::kStateConfiguring); |
| 638 | } else { |
| 639 | LOG(ERROR) << "Unable to acquire DHCP config."; |
| 640 | } |
Darin Petkov | 0828f5f | 2011-08-11 10:18:52 -0700 | [diff] [blame] | 641 | } else if ((flags & IFF_UP) == 0 && state_ == kStateLinked) { |
Arman Uguray | 32c7640 | 2012-11-27 14:01:13 -0800 | [diff] [blame] | 642 | LOG(INFO) << link_name() << " is down."; |
Darin Petkov | 0828f5f | 2011-08-11 10:18:52 -0700 | [diff] [blame] | 643 | SetState(kStateConnected); |
Arman Uguray | 32c7640 | 2012-11-27 14:01:13 -0800 | [diff] [blame] | 644 | DropConnection(); |
Darin Petkov | 0828f5f | 2011-08-11 10:18:52 -0700 | [diff] [blame] | 645 | } |
Darin Petkov | c5f5656 | 2011-08-06 16:40:05 -0700 | [diff] [blame] | 646 | } |
| 647 | |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 648 | void Cellular::OnDBusPropertiesChanged( |
| 649 | const string &interface, |
| 650 | const DBusPropertiesMap &changed_properties, |
| 651 | const vector<string> &invalidated_properties) { |
| 652 | capability_->OnDBusPropertiesChanged(interface, |
| 653 | changed_properties, |
| 654 | invalidated_properties); |
| 655 | } |
| 656 | |
Darin Petkov | ae0c64e | 2011-11-15 15:50:27 +0100 | [diff] [blame] | 657 | void Cellular::set_home_provider(const Operator &oper) { |
| 658 | home_provider_.CopyFrom(oper); |
| 659 | } |
| 660 | |
Darin Petkov | ac635a8 | 2012-01-10 16:51:58 +0100 | [diff] [blame] | 661 | string Cellular::CreateFriendlyServiceName() { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 662 | SLOG(Cellular, 2) << __func__; |
Darin Petkov | ac635a8 | 2012-01-10 16:51:58 +0100 | [diff] [blame] | 663 | return capability_.get() ? capability_->CreateFriendlyServiceName() : ""; |
| 664 | } |
| 665 | |
Eric Shienbrood | 7fce52c | 2012-04-13 19:11:02 -0400 | [diff] [blame] | 666 | void Cellular::OnModemStateChanged(ModemState old_state, |
| 667 | ModemState new_state, |
| 668 | uint32 /*reason*/) { |
| 669 | if (old_state == new_state) { |
| 670 | return; |
| 671 | } |
| 672 | set_modem_state(new_state); |
Thieu Le | 5218cf2 | 2012-11-26 11:52:57 -0800 | [diff] [blame] | 673 | if (old_state >= kModemStateRegistered && |
| 674 | new_state < kModemStateRegistered) { |
| 675 | capability_->SetUnregistered(new_state == kModemStateSearching); |
| 676 | HandleNewRegistrationState(); |
| 677 | } |
Eric Shienbrood | 7fce52c | 2012-04-13 19:11:02 -0400 | [diff] [blame] | 678 | switch (new_state) { |
| 679 | case kModemStateDisabled: |
| 680 | SetEnabled(false); |
| 681 | break; |
| 682 | case kModemStateEnabled: |
Thieu Le | 5218cf2 | 2012-11-26 11:52:57 -0800 | [diff] [blame] | 683 | // Transition from Disabled to Enabled is handled in the |
| 684 | // DBusPropertiesChanged handler. |
| 685 | SLOG(Cellular, 2) << __func__ << ": Ignoring state change to Enabled"; |
Thieu Le | d001205 | 2012-07-25 16:09:09 -0700 | [diff] [blame] | 686 | // Intentionally falls through. |
Thieu Le | 5218cf2 | 2012-11-26 11:52:57 -0800 | [diff] [blame] | 687 | case kModemStateSearching: |
Eric Shienbrood | 7fce52c | 2012-04-13 19:11:02 -0400 | [diff] [blame] | 688 | case kModemStateRegistered: |
Thieu Le | d001205 | 2012-07-25 16:09:09 -0700 | [diff] [blame] | 689 | // If the modem state changes from Connecting/Connected/Disconnecting |
| 690 | // to Registered/Enabled/Searching, then it's an indication that the |
| 691 | // modem has been disconnected or got disconnected by the network. |
Eric Shienbrood | 7fce52c | 2012-04-13 19:11:02 -0400 | [diff] [blame] | 692 | if (old_state == kModemStateConnected || |
| 693 | old_state == kModemStateConnecting || |
| 694 | old_state == kModemStateDisconnecting) |
| 695 | OnDisconnected(); |
| 696 | break; |
| 697 | case kModemStateConnecting: |
| 698 | OnConnecting(); |
| 699 | break; |
| 700 | case kModemStateConnected: |
Thieu Le | d001205 | 2012-07-25 16:09:09 -0700 | [diff] [blame] | 701 | if (old_state == kModemStateConnecting) |
| 702 | OnConnected(); |
| 703 | else |
| 704 | SLOG(Cellular, 2) << __func__ << ": Ignoring state change to Connected"; |
Eric Shienbrood | 7fce52c | 2012-04-13 19:11:02 -0400 | [diff] [blame] | 705 | break; |
| 706 | default: |
| 707 | break; |
| 708 | } |
| 709 | } |
| 710 | |
Christopher Wiley | 1582bdd | 2012-11-15 11:31:14 -0800 | [diff] [blame] | 711 | bool Cellular::IsActivating() const { |
| 712 | return capability_->IsActivating(); |
| 713 | } |
| 714 | |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 715 | bool Cellular::SetAllowRoaming(const bool &value, Error */*error*/) { |
Jason Glasgow | 7b461df | 2012-05-01 16:38:45 -0400 | [diff] [blame] | 716 | SLOG(Cellular, 2) << __func__ |
| 717 | << "(" << allow_roaming_ << "->" << value << ")"; |
| 718 | if (allow_roaming_ == value) { |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 719 | return false; |
Jason Glasgow | 7b461df | 2012-05-01 16:38:45 -0400 | [diff] [blame] | 720 | } |
| 721 | allow_roaming_ = value; |
Darin Petkov | e7c6ad3 | 2012-06-29 10:22:09 +0200 | [diff] [blame] | 722 | manager()->UpdateDevice(this); |
Jason Glasgow | 7b461df | 2012-05-01 16:38:45 -0400 | [diff] [blame] | 723 | |
| 724 | // Use AllowRoaming() instead of allow_roaming_ in order to |
| 725 | // incorporate provider preferences when evaluating if a disconnect |
| 726 | // is required. |
| 727 | if (!capability_->AllowRoaming() && |
| 728 | capability_->GetRoamingStateString() == flimflam::kRoamingStateRoaming) { |
| 729 | Error error; |
| 730 | Disconnect(&error); |
| 731 | } |
| 732 | adaptor()->EmitBoolChanged(flimflam::kCellularAllowRoamingProperty, value); |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 733 | return true; |
Jason Glasgow | 7b461df | 2012-05-01 16:38:45 -0400 | [diff] [blame] | 734 | } |
| 735 | |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame] | 736 | void Cellular::StartTermination() { |
Darin Petkov | 3ec5534 | 2012-09-28 14:04:44 +0200 | [diff] [blame] | 737 | LOG(INFO) << __func__; |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame] | 738 | Error error; |
| 739 | Disconnect(&error); |
| 740 | } |
| 741 | |
Arman Uguray | 539c423 | 2012-09-11 10:00:22 -0700 | [diff] [blame] | 742 | bool Cellular::DisconnectCleanup() { |
Christopher Wiley | 8a46890 | 2012-11-30 11:52:38 -0800 | [diff] [blame] | 743 | bool succeeded = false; |
Arman Uguray | 539c423 | 2012-09-11 10:00:22 -0700 | [diff] [blame] | 744 | if (state_ == kStateConnected || state_ == kStateLinked) { |
| 745 | SetState(kStateRegistered); |
| 746 | SetServiceFailureSilent(Service::kFailureUnknown); |
| 747 | DestroyIPConfig(); |
Christopher Wiley | 8a46890 | 2012-11-30 11:52:38 -0800 | [diff] [blame] | 748 | succeeded = true; |
Arman Uguray | 539c423 | 2012-09-11 10:00:22 -0700 | [diff] [blame] | 749 | } |
Christopher Wiley | 8a46890 | 2012-11-30 11:52:38 -0800 | [diff] [blame] | 750 | capability_->DisconnectCleanup(); |
| 751 | return succeeded; |
Arman Uguray | 539c423 | 2012-09-11 10:00:22 -0700 | [diff] [blame] | 752 | } |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame] | 753 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 754 | } // namespace shill |