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> |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 15 | #include <base/logging.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" |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 30 | #include "shill/manager.h" |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 31 | #include "shill/profile.h" |
Chris Masone | 889666b | 2011-07-03 12:58:50 -0700 | [diff] [blame] | 32 | #include "shill/property_accessor.h" |
Darin Petkov | e9d12e0 | 2011-07-27 15:09:37 -0700 | [diff] [blame] | 33 | #include "shill/proxy_factory.h" |
Darin Petkov | 0828f5f | 2011-08-11 10:18:52 -0700 | [diff] [blame] | 34 | #include "shill/rtnl_handler.h" |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 35 | #include "shill/technology.h" |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 36 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 37 | using base::Bind; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 38 | using std::string; |
Chris Masone | 889666b | 2011-07-03 12:58:50 -0700 | [diff] [blame] | 39 | using std::vector; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 40 | |
| 41 | namespace shill { |
| 42 | |
Darin Petkov | 3335b37 | 2011-08-22 11:05:32 -0700 | [diff] [blame] | 43 | Cellular::Operator::Operator() { |
| 44 | SetName(""); |
| 45 | SetCode(""); |
| 46 | SetCountry(""); |
| 47 | } |
| 48 | |
| 49 | Cellular::Operator::~Operator() {} |
| 50 | |
| 51 | void Cellular::Operator::CopyFrom(const Operator &oper) { |
| 52 | dict_ = oper.dict_; |
| 53 | } |
| 54 | |
| 55 | const string &Cellular::Operator::GetName() const { |
| 56 | return dict_.find(flimflam::kOperatorNameKey)->second; |
| 57 | } |
| 58 | |
| 59 | void Cellular::Operator::SetName(const string &name) { |
| 60 | dict_[flimflam::kOperatorNameKey] = name; |
| 61 | } |
| 62 | |
| 63 | const string &Cellular::Operator::GetCode() const { |
| 64 | return dict_.find(flimflam::kOperatorCodeKey)->second; |
| 65 | } |
| 66 | |
| 67 | void Cellular::Operator::SetCode(const string &code) { |
| 68 | dict_[flimflam::kOperatorCodeKey] = code; |
| 69 | } |
| 70 | |
| 71 | const string &Cellular::Operator::GetCountry() const { |
| 72 | return dict_.find(flimflam::kOperatorCountryKey)->second; |
| 73 | } |
| 74 | |
| 75 | void Cellular::Operator::SetCountry(const string &country) { |
| 76 | dict_[flimflam::kOperatorCountryKey] = country; |
| 77 | } |
| 78 | |
| 79 | const Stringmap &Cellular::Operator::ToDict() const { |
| 80 | return dict_; |
| 81 | } |
| 82 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 83 | Cellular::Cellular(ControlInterface *control_interface, |
| 84 | EventDispatcher *dispatcher, |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 85 | Metrics *metrics, |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 86 | Manager *manager, |
Darin Petkov | e9d12e0 | 2011-07-27 15:09:37 -0700 | [diff] [blame] | 87 | const string &link_name, |
Darin Petkov | 3335b37 | 2011-08-22 11:05:32 -0700 | [diff] [blame] | 88 | const string &address, |
Darin Petkov | e9d12e0 | 2011-07-27 15:09:37 -0700 | [diff] [blame] | 89 | int interface_index, |
| 90 | Type type, |
| 91 | const string &owner, |
Darin Petkov | 137884a | 2011-10-26 18:52:47 +0200 | [diff] [blame] | 92 | const string &path, |
| 93 | mobile_provider_db *provider_db) |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 94 | : Device(control_interface, |
| 95 | dispatcher, |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 96 | metrics, |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 97 | manager, |
Darin Petkov | e9d12e0 | 2011-07-27 15:09:37 -0700 | [diff] [blame] | 98 | link_name, |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 99 | address, |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 100 | interface_index, |
| 101 | Technology::kCellular), |
Darin Petkov | e9d12e0 | 2011-07-27 15:09:37 -0700 | [diff] [blame] | 102 | state_(kStateDisabled), |
Darin Petkov | bac9600 | 2011-08-09 13:22:00 -0700 | [diff] [blame] | 103 | modem_state_(kModemStateUnknown), |
Darin Petkov | e9d12e0 | 2011-07-27 15:09:37 -0700 | [diff] [blame] | 104 | dbus_owner_(owner), |
| 105 | dbus_path_(path), |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 106 | provider_db_(provider_db) { |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 107 | PropertyStore *store = this->mutable_store(); |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 108 | store->RegisterConstString(flimflam::kDBusConnectionProperty, &dbus_owner_); |
| 109 | store->RegisterConstString(flimflam::kDBusObjectProperty, &dbus_path_); |
Eric Shienbrood | 0db6a9b | 2012-03-30 16:11:39 -0400 | [diff] [blame] | 110 | HelpRegisterDerivedString(flimflam::kTechnologyFamilyProperty, |
| 111 | &Cellular::GetTechnologyFamily, |
| 112 | NULL); |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 113 | store->RegisterConstStringmap(flimflam::kHomeProviderProperty, |
Darin Petkov | 3335b37 | 2011-08-22 11:05:32 -0700 | [diff] [blame] | 114 | &home_provider_.ToDict()); |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 115 | // For now, only a single capability is supported. |
| 116 | InitCapability(type, ProxyFactory::GetInstance()); |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 117 | |
Darin Petkov | 5f316f6 | 2011-11-18 12:10:26 +0100 | [diff] [blame] | 118 | VLOG(2) << "Cellular device " << this->link_name() << " initialized."; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 121 | Cellular::~Cellular() { |
| 122 | } |
Darin Petkov | e9d12e0 | 2011-07-27 15:09:37 -0700 | [diff] [blame] | 123 | |
Darin Petkov | cc04442 | 2011-08-17 13:30:06 -0700 | [diff] [blame] | 124 | // static |
| 125 | string Cellular::GetStateString(State state) { |
| 126 | switch (state) { |
Darin Petkov | e9d12e0 | 2011-07-27 15:09:37 -0700 | [diff] [blame] | 127 | case kStateDisabled: return "CellularStateDisabled"; |
| 128 | case kStateEnabled: return "CellularStateEnabled"; |
| 129 | case kStateRegistered: return "CellularStateRegistered"; |
| 130 | case kStateConnected: return "CellularStateConnected"; |
Darin Petkov | 0828f5f | 2011-08-11 10:18:52 -0700 | [diff] [blame] | 131 | case kStateLinked: return "CellularStateLinked"; |
| 132 | default: NOTREACHED(); |
Darin Petkov | e9d12e0 | 2011-07-27 15:09:37 -0700 | [diff] [blame] | 133 | } |
Darin Petkov | cc04442 | 2011-08-17 13:30:06 -0700 | [diff] [blame] | 134 | return StringPrintf("CellularStateUnknown-%d", state); |
Darin Petkov | 0828f5f | 2011-08-11 10:18:52 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Eric Shienbrood | 0db6a9b | 2012-03-30 16:11:39 -0400 | [diff] [blame] | 137 | string Cellular::GetTechnologyFamily(Error *error) { |
| 138 | return capability_->GetTypeString(); |
| 139 | } |
| 140 | |
Darin Petkov | 0828f5f | 2011-08-11 10:18:52 -0700 | [diff] [blame] | 141 | void Cellular::SetState(State state) { |
Darin Petkov | cc04442 | 2011-08-17 13:30:06 -0700 | [diff] [blame] | 142 | VLOG(2) << GetStateString(state_) << " -> " << GetStateString(state); |
Darin Petkov | 0828f5f | 2011-08-11 10:18:52 -0700 | [diff] [blame] | 143 | state_ = state; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Eric Shienbrood | 0db6a9b | 2012-03-30 16:11:39 -0400 | [diff] [blame] | 146 | void Cellular::HelpRegisterDerivedString( |
| 147 | const string &name, |
| 148 | string(Cellular::*get)(Error *), |
| 149 | void(Cellular::*set)(const string&, Error *)) { |
| 150 | mutable_store()->RegisterDerivedString( |
| 151 | name, |
| 152 | StringAccessor(new CustomAccessor<Cellular, string>(this, get, set))); |
| 153 | } |
| 154 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 155 | void Cellular::Start(Error *error, |
| 156 | const EnabledStateChangedCallback &callback) { |
Jason Glasgow | 4a49079 | 2012-04-10 15:02:05 -0400 | [diff] [blame] | 157 | DCHECK(error); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 158 | VLOG(2) << __func__ << ": " << GetStateString(state_); |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 159 | if (state_ != kStateDisabled) { |
| 160 | return; |
| 161 | } |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 162 | if (modem_state_ == kModemStateEnabled) { |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 163 | // Modem already enabled. Make sure shill |
| 164 | // state matches ModemManager state. |
| 165 | SetState(kStateEnabled); |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 166 | return; |
| 167 | } |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 168 | capability_->StartModem(error, |
| 169 | Bind(&Cellular::OnModemStarted, this, callback)); |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 172 | void Cellular::Stop(Error *error, |
| 173 | const EnabledStateChangedCallback &callback) { |
| 174 | VLOG(2) << __func__ << ": " << GetStateString(state_); |
| 175 | if (service_) { |
| 176 | // TODO(ers): See whether we can/should do DestroyService() here. |
| 177 | manager()->DeregisterService(service_); |
| 178 | service_ = NULL; |
| 179 | } |
| 180 | capability_->StopModem(error, |
| 181 | Bind(&Cellular::OnModemStopped, this, callback)); |
| 182 | } |
| 183 | |
| 184 | void Cellular::OnModemStarted(const EnabledStateChangedCallback &callback, |
| 185 | const Error &error) { |
| 186 | VLOG(2) << __func__ << ": " << GetStateString(state_); |
| 187 | if (state_ == kStateDisabled) |
| 188 | SetState(kStateEnabled); |
| 189 | callback.Run(error); |
| 190 | } |
| 191 | |
| 192 | void Cellular::OnModemStopped(const EnabledStateChangedCallback &callback, |
| 193 | const Error &error) { |
| 194 | VLOG(2) << __func__ << ": " << GetStateString(state_); |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 195 | if (state_ != kStateDisabled) |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 196 | SetState(kStateDisabled); |
| 197 | callback.Run(error); |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 200 | void Cellular::InitCapability(Type type, ProxyFactory *proxy_factory) { |
Darin Petkov | 5f316f6 | 2011-11-18 12:10:26 +0100 | [diff] [blame] | 201 | // TODO(petkov): Consider moving capability construction into a factory that's |
| 202 | // external to the Cellular class. |
| 203 | VLOG(2) << __func__ << "(" << type << ")"; |
| 204 | switch (type) { |
Darin Petkov | daf4386 | 2011-10-27 11:37:28 +0200 | [diff] [blame] | 205 | case kTypeGSM: |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 206 | capability_.reset(new CellularCapabilityGSM(this, proxy_factory)); |
Darin Petkov | daf4386 | 2011-10-27 11:37:28 +0200 | [diff] [blame] | 207 | break; |
| 208 | case kTypeCDMA: |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 209 | capability_.reset(new CellularCapabilityCDMA(this, proxy_factory)); |
Darin Petkov | daf4386 | 2011-10-27 11:37:28 +0200 | [diff] [blame] | 210 | break; |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 211 | case kTypeUniversal: |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 212 | capability_.reset(new CellularCapabilityUniversal(this, proxy_factory)); |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 213 | break; |
Darin Petkov | daf4386 | 2011-10-27 11:37:28 +0200 | [diff] [blame] | 214 | default: NOTREACHED(); |
| 215 | } |
| 216 | } |
| 217 | |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 218 | void Cellular::Activate(const string &carrier, |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 219 | Error *error, const ResultCallback &callback) { |
| 220 | capability_->Activate(carrier, error, callback); |
Darin Petkov | 9ae310f | 2011-08-30 15:41:13 -0700 | [diff] [blame] | 221 | } |
| 222 | |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 223 | void Cellular::RegisterOnNetwork(const string &network_id, |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 224 | Error *error, |
| 225 | const ResultCallback &callback) { |
| 226 | capability_->RegisterOnNetwork(network_id, error, callback); |
Darin Petkov | a3d3be5 | 2011-11-14 21:34:16 +0100 | [diff] [blame] | 227 | } |
| 228 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 229 | void Cellular::RequirePIN(const string &pin, bool require, |
| 230 | Error *error, const ResultCallback &callback) { |
| 231 | VLOG(2) << __func__ << "(" << require << ")"; |
| 232 | capability_->RequirePIN(pin, require, error, callback); |
Darin Petkov | e42e101 | 2011-08-31 12:35:04 -0700 | [diff] [blame] | 233 | } |
| 234 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 235 | void Cellular::EnterPIN(const string &pin, |
| 236 | Error *error, const ResultCallback &callback) { |
| 237 | VLOG(2) << __func__; |
| 238 | capability_->EnterPIN(pin, error, callback); |
Darin Petkov | e42e101 | 2011-08-31 12:35:04 -0700 | [diff] [blame] | 239 | } |
| 240 | |
Darin Petkov | c64fe5e | 2012-01-11 12:46:13 +0100 | [diff] [blame] | 241 | void Cellular::UnblockPIN(const string &unblock_code, |
| 242 | const string &pin, |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 243 | Error *error, const ResultCallback &callback) { |
| 244 | VLOG(2) << __func__; |
| 245 | capability_->UnblockPIN(unblock_code, pin, error, callback); |
Darin Petkov | e42e101 | 2011-08-31 12:35:04 -0700 | [diff] [blame] | 246 | } |
| 247 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 248 | void Cellular::ChangePIN(const string &old_pin, const string &new_pin, |
| 249 | Error *error, const ResultCallback &callback) { |
| 250 | VLOG(2) << __func__; |
| 251 | capability_->ChangePIN(old_pin, new_pin, error, callback); |
Darin Petkov | e42e101 | 2011-08-31 12:35:04 -0700 | [diff] [blame] | 252 | } |
| 253 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 254 | void Cellular::Scan(Error *error) { |
| 255 | // TODO(ers): for now report immediate success or failure. |
| 256 | capability_->Scan(error, ResultCallback()); |
Darin Petkov | ceb6817 | 2011-07-29 14:47:48 -0700 | [diff] [blame] | 257 | } |
| 258 | |
Darin Petkov | d966195 | 2011-08-03 16:25:42 -0700 | [diff] [blame] | 259 | void Cellular::HandleNewRegistrationState() { |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 260 | VLOG(2) << __func__ << ": " << GetStateString(state_); |
Darin Petkov | b72cf40 | 2011-11-22 14:51:39 +0100 | [diff] [blame] | 261 | if (!capability_->IsRegistered()) { |
Darin Petkov | 2c37738 | 2012-01-11 11:40:43 +0100 | [diff] [blame] | 262 | DestroyService(); |
Darin Petkov | 0828f5f | 2011-08-11 10:18:52 -0700 | [diff] [blame] | 263 | if (state_ == kStateLinked || |
| 264 | state_ == kStateConnected || |
| 265 | state_ == kStateRegistered) { |
| 266 | SetState(kStateEnabled); |
Darin Petkov | d966195 | 2011-08-03 16:25:42 -0700 | [diff] [blame] | 267 | } |
| 268 | return; |
| 269 | } |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 270 | // In Disabled state, defer creating a service until fully |
| 271 | // enabled. UI will ignore the appearance of a new service |
| 272 | // on a disabled device. |
| 273 | if (state_ == kStateDisabled) { |
| 274 | return; |
| 275 | } |
Darin Petkov | d966195 | 2011-08-03 16:25:42 -0700 | [diff] [blame] | 276 | if (state_ == kStateEnabled) { |
Darin Petkov | 0828f5f | 2011-08-11 10:18:52 -0700 | [diff] [blame] | 277 | SetState(kStateRegistered); |
Darin Petkov | d966195 | 2011-08-03 16:25:42 -0700 | [diff] [blame] | 278 | } |
| 279 | if (!service_.get()) { |
Darin Petkov | d966195 | 2011-08-03 16:25:42 -0700 | [diff] [blame] | 280 | CreateService(); |
| 281 | } |
Darin Petkov | 3e50924 | 2011-11-10 14:46:44 +0100 | [diff] [blame] | 282 | capability_->GetSignalQuality(); |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 283 | if (state_ == kStateRegistered && modem_state_ == kModemStateConnected) |
| 284 | OnConnected(); |
Darin Petkov | b72cf40 | 2011-11-22 14:51:39 +0100 | [diff] [blame] | 285 | service_->SetNetworkTechnology(capability_->GetNetworkTechnologyString()); |
| 286 | service_->SetRoamingState(capability_->GetRoamingStateString()); |
Darin Petkov | d966195 | 2011-08-03 16:25:42 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Darin Petkov | d966195 | 2011-08-03 16:25:42 -0700 | [diff] [blame] | 289 | void Cellular::HandleNewSignalQuality(uint32 strength) { |
| 290 | VLOG(2) << "Signal strength: " << strength; |
Darin Petkov | d78ee7e | 2012-01-12 11:21:10 +0100 | [diff] [blame] | 291 | if (service_) { |
| 292 | service_->SetStrength(strength); |
Darin Petkov | d966195 | 2011-08-03 16:25:42 -0700 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | |
| 296 | void Cellular::CreateService() { |
Darin Petkov | 0828f5f | 2011-08-11 10:18:52 -0700 | [diff] [blame] | 297 | VLOG(2) << __func__; |
Darin Petkov | d966195 | 2011-08-03 16:25:42 -0700 | [diff] [blame] | 298 | CHECK(!service_.get()); |
| 299 | service_ = |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 300 | new CellularService(control_interface(), dispatcher(), metrics(), |
| 301 | manager(), this); |
Darin Petkov | ae0c64e | 2011-11-15 15:50:27 +0100 | [diff] [blame] | 302 | capability_->OnServiceCreated(); |
Darin Petkov | 3133241 | 2012-01-28 01:50:02 +0100 | [diff] [blame] | 303 | manager()->RegisterService(service_); |
Darin Petkov | f5f61e0 | 2011-07-29 11:35:40 -0700 | [diff] [blame] | 304 | } |
| 305 | |
Darin Petkov | 2c37738 | 2012-01-11 11:40:43 +0100 | [diff] [blame] | 306 | void Cellular::DestroyService() { |
| 307 | VLOG(2) << __func__; |
| 308 | DestroyIPConfig(); |
| 309 | if (service_) { |
| 310 | manager()->DeregisterService(service_); |
| 311 | service_ = NULL; |
| 312 | } |
| 313 | SelectService(NULL); |
| 314 | } |
| 315 | |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 316 | bool Cellular::TechnologyIs(const Technology::Identifier type) const { |
| 317 | return type == Technology::kCellular; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 318 | } |
| 319 | |
Darin Petkov | 4d6d941 | 2011-08-24 13:19:54 -0700 | [diff] [blame] | 320 | void Cellular::Connect(Error *error) { |
Darin Petkov | c5f5656 | 2011-08-06 16:40:05 -0700 | [diff] [blame] | 321 | VLOG(2) << __func__; |
Eric Shienbrood | 30bc0ec | 2012-03-21 18:19:46 -0400 | [diff] [blame] | 322 | if (state_ == kStateConnected || state_ == kStateLinked) { |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame] | 323 | Error::PopulateAndLog(error, Error::kAlreadyConnected, |
| 324 | "Already connected; connection request ignored."); |
Darin Petkov | c5f5656 | 2011-08-06 16:40:05 -0700 | [diff] [blame] | 325 | return; |
| 326 | } |
| 327 | CHECK_EQ(kStateRegistered, state_); |
Darin Petkov | d204580 | 2011-08-23 11:09:25 -0700 | [diff] [blame] | 328 | |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 329 | if (!capability_->AllowRoaming() && |
Darin Petkov | d204580 | 2011-08-23 11:09:25 -0700 | [diff] [blame] | 330 | service_->roaming_state() == flimflam::kRoamingStateRoaming) { |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame] | 331 | Error::PopulateAndLog(error, Error::kNotOnHomeNetwork, |
| 332 | "Roaming disallowed; connection request ignored."); |
Darin Petkov | d204580 | 2011-08-23 11:09:25 -0700 | [diff] [blame] | 333 | return; |
| 334 | } |
| 335 | |
Darin Petkov | c5f5656 | 2011-08-06 16:40:05 -0700 | [diff] [blame] | 336 | DBusPropertiesMap properties; |
Darin Petkov | ae0c64e | 2011-11-15 15:50:27 +0100 | [diff] [blame] | 337 | capability_->SetupConnectProperties(&properties); |
Eric Shienbrood | cc95c5d | 2012-03-30 15:25:49 -0400 | [diff] [blame] | 338 | service_->SetState(Service::kStateAssociating); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 339 | // TODO(ers): use null callback until Connect is made fully asynchronous |
| 340 | capability_->Connect(properties, error, ResultCallback()); |
Darin Petkov | c5f5656 | 2011-08-06 16:40:05 -0700 | [diff] [blame] | 341 | } |
| 342 | |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 343 | void Cellular::OnConnected() { |
Darin Petkov | c5f5656 | 2011-08-06 16:40:05 -0700 | [diff] [blame] | 344 | VLOG(2) << __func__; |
Darin Petkov | 0828f5f | 2011-08-11 10:18:52 -0700 | [diff] [blame] | 345 | SetState(kStateConnected); |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 346 | if (!capability_->AllowRoaming() && |
Darin Petkov | 9c1dcef | 2012-02-07 15:58:26 +0100 | [diff] [blame] | 347 | service_->roaming_state() == flimflam::kRoamingStateRoaming) { |
| 348 | Disconnect(NULL); |
| 349 | } else { |
| 350 | EstablishLink(); |
| 351 | } |
Darin Petkov | bac9600 | 2011-08-09 13:22:00 -0700 | [diff] [blame] | 352 | } |
| 353 | |
Eric Shienbrood | 30bc0ec | 2012-03-21 18:19:46 -0400 | [diff] [blame] | 354 | void Cellular::OnConnectFailed(const Error &error) { |
| 355 | service()->SetFailure(Service::kFailureUnknown); |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 356 | } |
| 357 | |
Darin Petkov | fb0625e | 2012-01-16 13:05:56 +0100 | [diff] [blame] | 358 | void Cellular::Disconnect(Error *error) { |
| 359 | VLOG(2) << __func__; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 360 | if (state_ != kStateConnected && state_ != kStateLinked) { |
Darin Petkov | fb0625e | 2012-01-16 13:05:56 +0100 | [diff] [blame] | 361 | Error::PopulateAndLog( |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 362 | error, Error::kNotConnected, "Not connected; request ignored."); |
Darin Petkov | fb0625e | 2012-01-16 13:05:56 +0100 | [diff] [blame] | 363 | return; |
| 364 | } |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 365 | // TODO(ers): use null callback until Disconnect is made fully asynchronous |
| 366 | capability_->Disconnect(error, ResultCallback()); |
Darin Petkov | fb0625e | 2012-01-16 13:05:56 +0100 | [diff] [blame] | 367 | } |
| 368 | |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 369 | void Cellular::OnDisconnected() { |
Darin Petkov | fb0625e | 2012-01-16 13:05:56 +0100 | [diff] [blame] | 370 | VLOG(2) << __func__; |
Eric Shienbrood | cc95c5d | 2012-03-30 15:25:49 -0400 | [diff] [blame] | 371 | if (state_ == kStateConnected || state_ == kStateLinked) { |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 372 | SetState(kStateRegistered); |
Eric Shienbrood | cc95c5d | 2012-03-30 15:25:49 -0400 | [diff] [blame] | 373 | SetServiceFailureSilent(Service::kFailureUnknown); |
| 374 | } else { |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 375 | LOG(WARNING) << "Disconnect occurred while in state " |
| 376 | << GetStateString(state_); |
Eric Shienbrood | cc95c5d | 2012-03-30 15:25:49 -0400 | [diff] [blame] | 377 | } |
Eric Shienbrood | 5de44ab | 2011-12-05 10:46:27 -0500 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | void Cellular::OnDisconnectFailed() { |
| 381 | // TODO(ers): Signal failure. |
Darin Petkov | fb0625e | 2012-01-16 13:05:56 +0100 | [diff] [blame] | 382 | } |
| 383 | |
Darin Petkov | bac9600 | 2011-08-09 13:22:00 -0700 | [diff] [blame] | 384 | void Cellular::EstablishLink() { |
| 385 | VLOG(2) << __func__; |
Darin Petkov | 0828f5f | 2011-08-11 10:18:52 -0700 | [diff] [blame] | 386 | CHECK_EQ(kStateConnected, state_); |
| 387 | unsigned int flags = 0; |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 388 | if (manager()->device_info()->GetFlags(interface_index(), &flags) && |
Darin Petkov | 0828f5f | 2011-08-11 10:18:52 -0700 | [diff] [blame] | 389 | (flags & IFF_UP) != 0) { |
| 390 | LinkEvent(flags, IFF_UP); |
| 391 | return; |
| 392 | } |
| 393 | // TODO(petkov): Provide a timeout for a failed link-up request. |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 394 | rtnl_handler()->SetInterfaceFlags(interface_index(), IFF_UP, IFF_UP); |
Darin Petkov | 0828f5f | 2011-08-11 10:18:52 -0700 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | void Cellular::LinkEvent(unsigned int flags, unsigned int change) { |
| 398 | Device::LinkEvent(flags, change); |
| 399 | if ((flags & IFF_UP) != 0 && state_ == kStateConnected) { |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 400 | LOG(INFO) << link_name() << " is up."; |
Darin Petkov | 0828f5f | 2011-08-11 10:18:52 -0700 | [diff] [blame] | 401 | SetState(kStateLinked); |
Darin Petkov | 0828f5f | 2011-08-11 10:18:52 -0700 | [diff] [blame] | 402 | // TODO(petkov): For GSM, remember the APN. |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 403 | if (AcquireIPConfig()) { |
Darin Petkov | 60b8c3b | 2011-08-25 11:03:20 -0700 | [diff] [blame] | 404 | SelectService(service_); |
| 405 | SetServiceState(Service::kStateConfiguring); |
| 406 | } else { |
| 407 | LOG(ERROR) << "Unable to acquire DHCP config."; |
| 408 | } |
Darin Petkov | 0828f5f | 2011-08-11 10:18:52 -0700 | [diff] [blame] | 409 | } else if ((flags & IFF_UP) == 0 && state_ == kStateLinked) { |
| 410 | SetState(kStateConnected); |
Darin Petkov | 2c37738 | 2012-01-11 11:40:43 +0100 | [diff] [blame] | 411 | DestroyService(); |
Darin Petkov | 0828f5f | 2011-08-11 10:18:52 -0700 | [diff] [blame] | 412 | } |
Darin Petkov | c5f5656 | 2011-08-06 16:40:05 -0700 | [diff] [blame] | 413 | } |
| 414 | |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 415 | void Cellular::OnDBusPropertiesChanged( |
| 416 | const string &interface, |
| 417 | const DBusPropertiesMap &changed_properties, |
| 418 | const vector<string> &invalidated_properties) { |
| 419 | capability_->OnDBusPropertiesChanged(interface, |
| 420 | changed_properties, |
| 421 | invalidated_properties); |
| 422 | } |
| 423 | |
Darin Petkov | ae0c64e | 2011-11-15 15:50:27 +0100 | [diff] [blame] | 424 | void Cellular::OnModemManagerPropertiesChanged( |
| 425 | const DBusPropertiesMap &properties) { |
Darin Petkov | 721ac93 | 2011-11-16 15:43:09 +0100 | [diff] [blame] | 426 | capability_->OnModemManagerPropertiesChanged(properties); |
Chris Masone | 889666b | 2011-07-03 12:58:50 -0700 | [diff] [blame] | 427 | } |
| 428 | |
Darin Petkov | ae0c64e | 2011-11-15 15:50:27 +0100 | [diff] [blame] | 429 | void Cellular::set_home_provider(const Operator &oper) { |
| 430 | home_provider_.CopyFrom(oper); |
| 431 | } |
| 432 | |
Darin Petkov | ac635a8 | 2012-01-10 16:51:58 +0100 | [diff] [blame] | 433 | string Cellular::CreateFriendlyServiceName() { |
| 434 | VLOG(2) << __func__; |
| 435 | return capability_.get() ? capability_->CreateFriendlyServiceName() : ""; |
| 436 | } |
| 437 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 438 | } // namespace shill |