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