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