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