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 | |
| 14 | #include <base/logging.h> |
Darin Petkov | e9d12e0 | 2011-07-27 15:09:37 -0700 | [diff] [blame] | 15 | #include <base/stringprintf.h> |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 16 | #include <chromeos/dbus/service_constants.h> |
Darin Petkov | bec79a2 | 2011-08-01 14:47:17 -0700 | [diff] [blame] | 17 | #include <mm/mm-modem.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 | |
Darin Petkov | daf4386 | 2011-10-27 11:37:28 +0200 | [diff] [blame] | 20 | #include "shill/cellular_capability_cdma.h" |
| 21 | #include "shill/cellular_capability_gsm.h" |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 22 | #include "shill/cellular_service.h" |
| 23 | #include "shill/control_interface.h" |
| 24 | #include "shill/device.h" |
| 25 | #include "shill/device_info.h" |
Darin Petkov | 4d6d941 | 2011-08-24 13:19:54 -0700 | [diff] [blame] | 26 | #include "shill/error.h" |
Paul Stewart | 26b327e | 2011-10-19 11:38:09 -0700 | [diff] [blame] | 27 | #include "shill/event_dispatcher.h" |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 28 | #include "shill/manager.h" |
Darin Petkov | e604f70 | 2011-07-28 15:51:17 -0700 | [diff] [blame] | 29 | #include "shill/modem_simple_proxy_interface.h" |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 30 | #include "shill/profile.h" |
Chris Masone | 889666b | 2011-07-03 12:58:50 -0700 | [diff] [blame] | 31 | #include "shill/property_accessor.h" |
Darin Petkov | e9d12e0 | 2011-07-27 15:09:37 -0700 | [diff] [blame] | 32 | #include "shill/proxy_factory.h" |
Darin Petkov | 0828f5f | 2011-08-11 10:18:52 -0700 | [diff] [blame] | 33 | #include "shill/rtnl_handler.h" |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 34 | #include "shill/technology.h" |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 35 | |
Darin Petkov | c086531 | 2011-09-16 15:31:20 -0700 | [diff] [blame] | 36 | using std::map; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 37 | using std::string; |
Chris Masone | 889666b | 2011-07-03 12:58:50 -0700 | [diff] [blame] | 38 | using std::vector; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 39 | |
| 40 | namespace shill { |
| 41 | |
Darin Petkov | c5f5656 | 2011-08-06 16:40:05 -0700 | [diff] [blame] | 42 | const char Cellular::kConnectPropertyPhoneNumber[] = "number"; |
Darin Petkov | 0a4dfeb | 2011-11-18 19:36:13 +0100 | [diff] [blame] | 43 | const char Cellular::kPropertyIMSI[] = "imsi"; |
Darin Petkov | c5f5656 | 2011-08-06 16:40:05 -0700 | [diff] [blame] | 44 | |
Darin Petkov | 3335b37 | 2011-08-22 11:05:32 -0700 | [diff] [blame] | 45 | Cellular::Operator::Operator() { |
| 46 | SetName(""); |
| 47 | SetCode(""); |
| 48 | SetCountry(""); |
| 49 | } |
| 50 | |
| 51 | Cellular::Operator::~Operator() {} |
| 52 | |
| 53 | void Cellular::Operator::CopyFrom(const Operator &oper) { |
| 54 | dict_ = oper.dict_; |
| 55 | } |
| 56 | |
| 57 | const string &Cellular::Operator::GetName() const { |
| 58 | return dict_.find(flimflam::kOperatorNameKey)->second; |
| 59 | } |
| 60 | |
| 61 | void Cellular::Operator::SetName(const string &name) { |
| 62 | dict_[flimflam::kOperatorNameKey] = name; |
| 63 | } |
| 64 | |
| 65 | const string &Cellular::Operator::GetCode() const { |
| 66 | return dict_.find(flimflam::kOperatorCodeKey)->second; |
| 67 | } |
| 68 | |
| 69 | void Cellular::Operator::SetCode(const string &code) { |
| 70 | dict_[flimflam::kOperatorCodeKey] = code; |
| 71 | } |
| 72 | |
| 73 | const string &Cellular::Operator::GetCountry() const { |
| 74 | return dict_.find(flimflam::kOperatorCountryKey)->second; |
| 75 | } |
| 76 | |
| 77 | void Cellular::Operator::SetCountry(const string &country) { |
| 78 | dict_[flimflam::kOperatorCountryKey] = country; |
| 79 | } |
| 80 | |
| 81 | const Stringmap &Cellular::Operator::ToDict() const { |
| 82 | return dict_; |
| 83 | } |
| 84 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 85 | Cellular::Cellular(ControlInterface *control_interface, |
| 86 | EventDispatcher *dispatcher, |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 87 | Metrics *metrics, |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 88 | Manager *manager, |
Darin Petkov | e9d12e0 | 2011-07-27 15:09:37 -0700 | [diff] [blame] | 89 | const string &link_name, |
Darin Petkov | 3335b37 | 2011-08-22 11:05:32 -0700 | [diff] [blame] | 90 | const string &address, |
Darin Petkov | e9d12e0 | 2011-07-27 15:09:37 -0700 | [diff] [blame] | 91 | int interface_index, |
| 92 | Type type, |
| 93 | const string &owner, |
Darin Petkov | 137884a | 2011-10-26 18:52:47 +0200 | [diff] [blame] | 94 | const string &path, |
| 95 | mobile_provider_db *provider_db) |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 96 | : Device(control_interface, |
| 97 | dispatcher, |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 98 | metrics, |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 99 | manager, |
Darin Petkov | e9d12e0 | 2011-07-27 15:09:37 -0700 | [diff] [blame] | 100 | link_name, |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 101 | address, |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 102 | interface_index, |
| 103 | Technology::kCellular), |
Darin Petkov | ab565bb | 2011-10-06 02:55:51 -0700 | [diff] [blame] | 104 | proxy_factory_(ProxyFactory::GetInstance()), |
Darin Petkov | e9d12e0 | 2011-07-27 15:09:37 -0700 | [diff] [blame] | 105 | state_(kStateDisabled), |
Darin Petkov | bac9600 | 2011-08-09 13:22:00 -0700 | [diff] [blame] | 106 | modem_state_(kModemStateUnknown), |
Darin Petkov | e9d12e0 | 2011-07-27 15:09:37 -0700 | [diff] [blame] | 107 | dbus_owner_(owner), |
| 108 | dbus_path_(path), |
Darin Petkov | 137884a | 2011-10-26 18:52:47 +0200 | [diff] [blame] | 109 | provider_db_(provider_db), |
Darin Petkov | c5f5656 | 2011-08-06 16:40:05 -0700 | [diff] [blame] | 110 | task_factory_(this), |
Darin Petkov | 1272a43 | 2011-11-10 15:53:37 +0100 | [diff] [blame] | 111 | allow_roaming_(false) { |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 112 | PropertyStore *store = this->mutable_store(); |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 113 | store->RegisterConstString(flimflam::kCarrierProperty, &carrier_); |
| 114 | store->RegisterConstString(flimflam::kDBusConnectionProperty, &dbus_owner_); |
| 115 | store->RegisterConstString(flimflam::kDBusObjectProperty, &dbus_path_); |
| 116 | store->RegisterBool(flimflam::kCellularAllowRoamingProperty, &allow_roaming_); |
| 117 | store->RegisterConstString(flimflam::kEsnProperty, &esn_); |
| 118 | store->RegisterConstString(flimflam::kFirmwareRevisionProperty, |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 119 | &firmware_revision_); |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 120 | store->RegisterConstString(flimflam::kHardwareRevisionProperty, |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 121 | &hardware_revision_); |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 122 | store->RegisterConstStringmap(flimflam::kHomeProviderProperty, |
Darin Petkov | 3335b37 | 2011-08-22 11:05:32 -0700 | [diff] [blame] | 123 | &home_provider_.ToDict()); |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 124 | store->RegisterConstString(flimflam::kImeiProperty, &imei_); |
| 125 | store->RegisterConstString(flimflam::kImsiProperty, &imsi_); |
| 126 | store->RegisterConstString(flimflam::kManufacturerProperty, &manufacturer_); |
| 127 | store->RegisterConstString(flimflam::kMdnProperty, &mdn_); |
| 128 | store->RegisterConstString(flimflam::kMeidProperty, &meid_); |
| 129 | store->RegisterConstString(flimflam::kMinProperty, &min_); |
| 130 | store->RegisterConstString(flimflam::kModelIDProperty, &model_id_); |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 131 | |
Darin Petkov | 5f316f6 | 2011-11-18 12:10:26 +0100 | [diff] [blame] | 132 | InitCapability(type); // For now, only a single capability is supported. |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 133 | |
Darin Petkov | 5f316f6 | 2011-11-18 12:10:26 +0100 | [diff] [blame] | 134 | VLOG(2) << "Cellular device " << this->link_name() << " initialized."; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Darin Petkov | e9d12e0 | 2011-07-27 15:09:37 -0700 | [diff] [blame] | 137 | Cellular::~Cellular() {} |
| 138 | |
Darin Petkov | cc04442 | 2011-08-17 13:30:06 -0700 | [diff] [blame] | 139 | // static |
| 140 | string Cellular::GetStateString(State state) { |
| 141 | switch (state) { |
Darin Petkov | e9d12e0 | 2011-07-27 15:09:37 -0700 | [diff] [blame] | 142 | case kStateDisabled: return "CellularStateDisabled"; |
| 143 | case kStateEnabled: return "CellularStateEnabled"; |
| 144 | case kStateRegistered: return "CellularStateRegistered"; |
| 145 | case kStateConnected: return "CellularStateConnected"; |
Darin Petkov | 0828f5f | 2011-08-11 10:18:52 -0700 | [diff] [blame] | 146 | case kStateLinked: return "CellularStateLinked"; |
| 147 | default: NOTREACHED(); |
Darin Petkov | e9d12e0 | 2011-07-27 15:09:37 -0700 | [diff] [blame] | 148 | } |
Darin Petkov | cc04442 | 2011-08-17 13:30:06 -0700 | [diff] [blame] | 149 | return StringPrintf("CellularStateUnknown-%d", state); |
Darin Petkov | 0828f5f | 2011-08-11 10:18:52 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | void Cellular::SetState(State state) { |
Darin Petkov | cc04442 | 2011-08-17 13:30:06 -0700 | [diff] [blame] | 153 | VLOG(2) << GetStateString(state_) << " -> " << GetStateString(state); |
Darin Petkov | 0828f5f | 2011-08-11 10:18:52 -0700 | [diff] [blame] | 154 | state_ = state; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | void Cellular::Start() { |
Darin Petkov | cc04442 | 2011-08-17 13:30:06 -0700 | [diff] [blame] | 158 | LOG(INFO) << __func__ << ": " << GetStateString(state_); |
Darin Petkov | 3335b37 | 2011-08-22 11:05:32 -0700 | [diff] [blame] | 159 | Device::Start(); |
Darin Petkov | 5f316f6 | 2011-11-18 12:10:26 +0100 | [diff] [blame] | 160 | capability_->OnDeviceStarted(); |
Darin Petkov | bec79a2 | 2011-08-01 14:47:17 -0700 | [diff] [blame] | 161 | InitProxies(); |
Darin Petkov | f5f61e0 | 2011-07-29 11:35:40 -0700 | [diff] [blame] | 162 | EnableModem(); |
Darin Petkov | 184c54e | 2011-11-15 12:44:39 +0100 | [diff] [blame] | 163 | capability_->Register(); |
Darin Petkov | f5f61e0 | 2011-07-29 11:35:40 -0700 | [diff] [blame] | 164 | GetModemStatus(); |
Darin Petkov | cb54773 | 2011-11-09 13:55:26 +0100 | [diff] [blame] | 165 | capability_->GetIdentifiers(); |
Darin Petkov | 184c54e | 2011-11-15 12:44:39 +0100 | [diff] [blame] | 166 | capability_->GetProperties(); |
Darin Petkov | ceb6817 | 2011-07-29 14:47:48 -0700 | [diff] [blame] | 167 | GetModemInfo(); |
Darin Petkov | 184c54e | 2011-11-15 12:44:39 +0100 | [diff] [blame] | 168 | capability_->GetRegistrationState(); |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | void Cellular::Stop() { |
Darin Petkov | 5f316f6 | 2011-11-18 12:10:26 +0100 | [diff] [blame] | 172 | capability_->OnDeviceStopped(); |
Darin Petkov | fb0625e | 2012-01-16 13:05:56 +0100 | [diff] [blame] | 173 | DestroyService(); |
| 174 | DisconnectModem(); |
| 175 | DisableModem(); |
Darin Petkov | e9d12e0 | 2011-07-27 15:09:37 -0700 | [diff] [blame] | 176 | proxy_.reset(); |
Darin Petkov | e604f70 | 2011-07-28 15:51:17 -0700 | [diff] [blame] | 177 | simple_proxy_.reset(); |
Darin Petkov | 3335b37 | 2011-08-22 11:05:32 -0700 | [diff] [blame] | 178 | Device::Stop(); |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 179 | } |
| 180 | |
Darin Petkov | 5f316f6 | 2011-11-18 12:10:26 +0100 | [diff] [blame] | 181 | void Cellular::InitCapability(Type type) { |
| 182 | // TODO(petkov): Consider moving capability construction into a factory that's |
| 183 | // external to the Cellular class. |
| 184 | VLOG(2) << __func__ << "(" << type << ")"; |
| 185 | switch (type) { |
Darin Petkov | daf4386 | 2011-10-27 11:37:28 +0200 | [diff] [blame] | 186 | case kTypeGSM: |
| 187 | capability_.reset(new CellularCapabilityGSM(this)); |
| 188 | break; |
| 189 | case kTypeCDMA: |
| 190 | capability_.reset(new CellularCapabilityCDMA(this)); |
| 191 | break; |
| 192 | default: NOTREACHED(); |
| 193 | } |
| 194 | } |
| 195 | |
Darin Petkov | bec79a2 | 2011-08-01 14:47:17 -0700 | [diff] [blame] | 196 | void Cellular::InitProxies() { |
Darin Petkov | cc04442 | 2011-08-17 13:30:06 -0700 | [diff] [blame] | 197 | VLOG(2) << __func__; |
Darin Petkov | ab565bb | 2011-10-06 02:55:51 -0700 | [diff] [blame] | 198 | proxy_.reset(proxy_factory_->CreateModemProxy(this, dbus_path_, dbus_owner_)); |
Darin Petkov | bec79a2 | 2011-08-01 14:47:17 -0700 | [diff] [blame] | 199 | simple_proxy_.reset( |
Darin Petkov | ab565bb | 2011-10-06 02:55:51 -0700 | [diff] [blame] | 200 | proxy_factory_->CreateModemSimpleProxy(dbus_path_, dbus_owner_)); |
Darin Petkov | bec79a2 | 2011-08-01 14:47:17 -0700 | [diff] [blame] | 201 | } |
| 202 | |
Darin Petkov | f5f61e0 | 2011-07-29 11:35:40 -0700 | [diff] [blame] | 203 | void Cellular::EnableModem() { |
| 204 | CHECK_EQ(kStateDisabled, state_); |
| 205 | // TODO(petkov): Switch to asynchronous calls (crosbug.com/17583). |
Darin Petkov | e5bc2cb | 2011-12-07 14:47:32 +0100 | [diff] [blame] | 206 | try { |
| 207 | proxy_->Enable(true); |
| 208 | } catch (const DBus::Error e) { |
| 209 | LOG(WARNING) << "Enable failed: " << e.what(); |
| 210 | } |
Darin Petkov | 0828f5f | 2011-08-11 10:18:52 -0700 | [diff] [blame] | 211 | SetState(kStateEnabled); |
Darin Petkov | f5f61e0 | 2011-07-29 11:35:40 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Darin Petkov | fb0625e | 2012-01-16 13:05:56 +0100 | [diff] [blame] | 214 | void Cellular::DisableModem() { |
| 215 | VLOG(2) << __func__; |
| 216 | if (state_ == kStateDisabled) { |
| 217 | return; |
| 218 | } |
| 219 | // TODO(petkov): Switch to asynchronous calls (crosbug.com/17583). Note, |
| 220 | // however, that this is invoked by the Stop method, so some extra refactoring |
| 221 | // may be needed to prevent the device instance from being destroyed before |
| 222 | // the modem manager calls are complete. Maybe Disconnect and Disable need to |
| 223 | // be handled by the parent Modem class. |
| 224 | try { |
| 225 | proxy_->Enable(false); |
| 226 | } catch (const DBus::Error e) { |
| 227 | LOG(WARNING) << "Disable failed: " << e.what(); |
| 228 | } |
| 229 | SetState(kStateDisabled); |
| 230 | } |
| 231 | |
Darin Petkov | f5f61e0 | 2011-07-29 11:35:40 -0700 | [diff] [blame] | 232 | void Cellular::GetModemStatus() { |
Darin Petkov | ceb6817 | 2011-07-29 14:47:48 -0700 | [diff] [blame] | 233 | CHECK_EQ(kStateEnabled, state_); |
Darin Petkov | f5f61e0 | 2011-07-29 11:35:40 -0700 | [diff] [blame] | 234 | // TODO(petkov): Switch to asynchronous calls (crosbug.com/17583). |
| 235 | DBusPropertiesMap properties = simple_proxy_->GetStatus(); |
Darin Petkov | ae0c64e | 2011-11-15 15:50:27 +0100 | [diff] [blame] | 236 | DBusProperties::GetString(properties, "carrier", &carrier_); |
Darin Petkov | ceb6817 | 2011-07-29 14:47:48 -0700 | [diff] [blame] | 237 | DBusProperties::GetString(properties, "meid", &meid_); |
| 238 | DBusProperties::GetString(properties, "imei", &imei_); |
Darin Petkov | 0a4dfeb | 2011-11-18 19:36:13 +0100 | [diff] [blame] | 239 | DBusProperties::GetString(properties, kPropertyIMSI, &imsi_); |
Darin Petkov | ceb6817 | 2011-07-29 14:47:48 -0700 | [diff] [blame] | 240 | DBusProperties::GetString(properties, "esn", &esn_); |
| 241 | DBusProperties::GetString(properties, "mdn", &mdn_); |
| 242 | DBusProperties::GetString(properties, "min", &min_); |
Darin Petkov | ceb6817 | 2011-07-29 14:47:48 -0700 | [diff] [blame] | 243 | DBusProperties::GetString( |
| 244 | properties, "firmware_revision", &firmware_revision_); |
Darin Petkov | b27e544 | 2011-08-16 14:36:45 -0700 | [diff] [blame] | 245 | |
Darin Petkov | d204580 | 2011-08-23 11:09:25 -0700 | [diff] [blame] | 246 | uint32 state = 0; |
| 247 | if (DBusProperties::GetUint32(properties, "state", &state)) { |
| 248 | modem_state_ = static_cast<ModemState>(state); |
| 249 | } |
Darin Petkov | b27e544 | 2011-08-16 14:36:45 -0700 | [diff] [blame] | 250 | |
Darin Petkov | ae0c64e | 2011-11-15 15:50:27 +0100 | [diff] [blame] | 251 | capability_->UpdateStatus(properties); |
Darin Petkov | ceb6817 | 2011-07-29 14:47:48 -0700 | [diff] [blame] | 252 | } |
| 253 | |
Darin Petkov | 184c54e | 2011-11-15 12:44:39 +0100 | [diff] [blame] | 254 | void Cellular::Activate(const std::string &carrier, Error *error) { |
| 255 | capability_->Activate(carrier, error); |
Darin Petkov | 9ae310f | 2011-08-30 15:41:13 -0700 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | void Cellular::RegisterOnNetwork(const string &network_id, Error *error) { |
Darin Petkov | 184c54e | 2011-11-15 12:44:39 +0100 | [diff] [blame] | 259 | capability_->RegisterOnNetwork(network_id, error); |
Darin Petkov | a3d3be5 | 2011-11-14 21:34:16 +0100 | [diff] [blame] | 260 | } |
| 261 | |
Darin Petkov | c64fe5e | 2012-01-11 12:46:13 +0100 | [diff] [blame] | 262 | void Cellular::RequirePIN( |
| 263 | const string &pin, bool require, ReturnerInterface *returner) { |
| 264 | VLOG(2) << __func__ << "(" << returner << ")"; |
| 265 | capability_->RequirePIN(pin, require, returner); |
Darin Petkov | e42e101 | 2011-08-31 12:35:04 -0700 | [diff] [blame] | 266 | } |
| 267 | |
Darin Petkov | e5bc2cb | 2011-12-07 14:47:32 +0100 | [diff] [blame] | 268 | void Cellular::EnterPIN(const string &pin, ReturnerInterface *returner) { |
| 269 | VLOG(2) << __func__ << "(" << returner << ")"; |
| 270 | capability_->EnterPIN(pin, returner); |
Darin Petkov | e42e101 | 2011-08-31 12:35:04 -0700 | [diff] [blame] | 271 | } |
| 272 | |
Darin Petkov | c64fe5e | 2012-01-11 12:46:13 +0100 | [diff] [blame] | 273 | void Cellular::UnblockPIN(const string &unblock_code, |
| 274 | const string &pin, |
| 275 | ReturnerInterface *returner) { |
| 276 | VLOG(2) << __func__ << "(" << returner << ")"; |
| 277 | capability_->UnblockPIN(unblock_code, pin, returner); |
Darin Petkov | e42e101 | 2011-08-31 12:35:04 -0700 | [diff] [blame] | 278 | } |
| 279 | |
Darin Petkov | b05315f | 2011-11-07 10:14:25 +0100 | [diff] [blame] | 280 | void Cellular::ChangePIN( |
Darin Petkov | c64fe5e | 2012-01-11 12:46:13 +0100 | [diff] [blame] | 281 | const string &old_pin, const string &new_pin, ReturnerInterface *returner) { |
| 282 | VLOG(2) << __func__ << "(" << returner << ")"; |
| 283 | capability_->ChangePIN(old_pin, new_pin, returner); |
Darin Petkov | e42e101 | 2011-08-31 12:35:04 -0700 | [diff] [blame] | 284 | } |
| 285 | |
Darin Petkov | 1272a43 | 2011-11-10 15:53:37 +0100 | [diff] [blame] | 286 | void Cellular::Scan(Error *error) { |
| 287 | capability_->Scan(error); |
| 288 | } |
| 289 | |
Darin Petkov | ceb6817 | 2011-07-29 14:47:48 -0700 | [diff] [blame] | 290 | void Cellular::GetModemInfo() { |
Darin Petkov | d204580 | 2011-08-23 11:09:25 -0700 | [diff] [blame] | 291 | // TODO(petkov): Switch to asynchronous calls (crosbug.com/17583). |
Darin Petkov | ceb6817 | 2011-07-29 14:47:48 -0700 | [diff] [blame] | 292 | ModemProxyInterface::Info info = proxy_->GetInfo(); |
| 293 | manufacturer_ = info._1; |
| 294 | model_id_ = info._2; |
| 295 | hardware_revision_ = info._3; |
| 296 | VLOG(2) << "ModemInfo: " << manufacturer_ << ", " << model_id_ << ", " |
| 297 | << hardware_revision_; |
| 298 | } |
| 299 | |
Darin Petkov | d966195 | 2011-08-03 16:25:42 -0700 | [diff] [blame] | 300 | void Cellular::HandleNewRegistrationState() { |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 301 | dispatcher()->PostTask( |
Darin Petkov | 0828f5f | 2011-08-11 10:18:52 -0700 | [diff] [blame] | 302 | task_factory_.NewRunnableMethod( |
| 303 | &Cellular::HandleNewRegistrationStateTask)); |
| 304 | } |
| 305 | |
| 306 | void Cellular::HandleNewRegistrationStateTask() { |
Darin Petkov | d966195 | 2011-08-03 16:25:42 -0700 | [diff] [blame] | 307 | VLOG(2) << __func__; |
Darin Petkov | b72cf40 | 2011-11-22 14:51:39 +0100 | [diff] [blame] | 308 | if (!capability_->IsRegistered()) { |
Darin Petkov | 2c37738 | 2012-01-11 11:40:43 +0100 | [diff] [blame] | 309 | DestroyService(); |
Darin Petkov | 0828f5f | 2011-08-11 10:18:52 -0700 | [diff] [blame] | 310 | if (state_ == kStateLinked || |
| 311 | state_ == kStateConnected || |
| 312 | state_ == kStateRegistered) { |
| 313 | SetState(kStateEnabled); |
Darin Petkov | d966195 | 2011-08-03 16:25:42 -0700 | [diff] [blame] | 314 | } |
| 315 | return; |
| 316 | } |
Darin Petkov | d966195 | 2011-08-03 16:25:42 -0700 | [diff] [blame] | 317 | if (state_ == kStateEnabled) { |
Darin Petkov | 0828f5f | 2011-08-11 10:18:52 -0700 | [diff] [blame] | 318 | SetState(kStateRegistered); |
Darin Petkov | d966195 | 2011-08-03 16:25:42 -0700 | [diff] [blame] | 319 | } |
| 320 | if (!service_.get()) { |
| 321 | // For now, no endpoint is created. Revisit if necessary. |
| 322 | CreateService(); |
| 323 | } |
Darin Petkov | 3e50924 | 2011-11-10 14:46:44 +0100 | [diff] [blame] | 324 | capability_->GetSignalQuality(); |
Darin Petkov | 0828f5f | 2011-08-11 10:18:52 -0700 | [diff] [blame] | 325 | if (state_ == kStateRegistered && modem_state_ == kModemStateConnected) { |
| 326 | SetState(kStateConnected); |
| 327 | EstablishLink(); |
| 328 | } |
Darin Petkov | b72cf40 | 2011-11-22 14:51:39 +0100 | [diff] [blame] | 329 | service_->SetNetworkTechnology(capability_->GetNetworkTechnologyString()); |
| 330 | service_->SetRoamingState(capability_->GetRoamingStateString()); |
Darin Petkov | d966195 | 2011-08-03 16:25:42 -0700 | [diff] [blame] | 331 | } |
| 332 | |
Darin Petkov | d966195 | 2011-08-03 16:25:42 -0700 | [diff] [blame] | 333 | void Cellular::HandleNewSignalQuality(uint32 strength) { |
| 334 | VLOG(2) << "Signal strength: " << strength; |
Darin Petkov | d78ee7e | 2012-01-12 11:21:10 +0100 | [diff] [blame] | 335 | if (service_) { |
| 336 | service_->SetStrength(strength); |
Darin Petkov | d966195 | 2011-08-03 16:25:42 -0700 | [diff] [blame] | 337 | } |
| 338 | } |
| 339 | |
| 340 | void Cellular::CreateService() { |
Darin Petkov | 0828f5f | 2011-08-11 10:18:52 -0700 | [diff] [blame] | 341 | VLOG(2) << __func__; |
Darin Petkov | d966195 | 2011-08-03 16:25:42 -0700 | [diff] [blame] | 342 | CHECK(!service_.get()); |
| 343 | service_ = |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 344 | new CellularService(control_interface(), dispatcher(), metrics(), |
| 345 | manager(), this); |
Eric Shienbrood | 04e6358 | 2012-01-19 13:41:12 -0500 | [diff] [blame^] | 346 | manager()->RegisterService(service_); |
Darin Petkov | ae0c64e | 2011-11-15 15:50:27 +0100 | [diff] [blame] | 347 | capability_->OnServiceCreated(); |
Darin Petkov | f5f61e0 | 2011-07-29 11:35:40 -0700 | [diff] [blame] | 348 | } |
| 349 | |
Darin Petkov | 2c37738 | 2012-01-11 11:40:43 +0100 | [diff] [blame] | 350 | void Cellular::DestroyService() { |
| 351 | VLOG(2) << __func__; |
| 352 | DestroyIPConfig(); |
| 353 | if (service_) { |
| 354 | manager()->DeregisterService(service_); |
| 355 | service_ = NULL; |
| 356 | } |
| 357 | SelectService(NULL); |
| 358 | } |
| 359 | |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 360 | bool Cellular::TechnologyIs(const Technology::Identifier type) const { |
| 361 | return type == Technology::kCellular; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 362 | } |
| 363 | |
Darin Petkov | 4d6d941 | 2011-08-24 13:19:54 -0700 | [diff] [blame] | 364 | void Cellular::Connect(Error *error) { |
Darin Petkov | c5f5656 | 2011-08-06 16:40:05 -0700 | [diff] [blame] | 365 | VLOG(2) << __func__; |
Darin Petkov | 0828f5f | 2011-08-11 10:18:52 -0700 | [diff] [blame] | 366 | if (state_ == kStateConnected || |
| 367 | state_ == kStateLinked) { |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame] | 368 | Error::PopulateAndLog(error, Error::kAlreadyConnected, |
| 369 | "Already connected; connection request ignored."); |
Darin Petkov | c5f5656 | 2011-08-06 16:40:05 -0700 | [diff] [blame] | 370 | return; |
| 371 | } |
| 372 | CHECK_EQ(kStateRegistered, state_); |
Darin Petkov | d204580 | 2011-08-23 11:09:25 -0700 | [diff] [blame] | 373 | |
| 374 | if (!allow_roaming_ && |
| 375 | service_->roaming_state() == flimflam::kRoamingStateRoaming) { |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame] | 376 | Error::PopulateAndLog(error, Error::kNotOnHomeNetwork, |
| 377 | "Roaming disallowed; connection request ignored."); |
Darin Petkov | d204580 | 2011-08-23 11:09:25 -0700 | [diff] [blame] | 378 | return; |
| 379 | } |
| 380 | |
Darin Petkov | c5f5656 | 2011-08-06 16:40:05 -0700 | [diff] [blame] | 381 | DBusPropertiesMap properties; |
Darin Petkov | ae0c64e | 2011-11-15 15:50:27 +0100 | [diff] [blame] | 382 | capability_->SetupConnectProperties(&properties); |
Darin Petkov | c5f5656 | 2011-08-06 16:40:05 -0700 | [diff] [blame] | 383 | |
| 384 | // Defer connect because we may be in a dbus-c++ callback. |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 385 | dispatcher()->PostTask( |
Darin Petkov | c5f5656 | 2011-08-06 16:40:05 -0700 | [diff] [blame] | 386 | task_factory_.NewRunnableMethod(&Cellular::ConnectTask, properties)); |
| 387 | } |
| 388 | |
| 389 | void Cellular::ConnectTask(const DBusPropertiesMap &properties) { |
| 390 | VLOG(2) << __func__; |
Darin Petkov | bac9600 | 2011-08-09 13:22:00 -0700 | [diff] [blame] | 391 | // TODO(petkov): Switch to asynchronous calls (crosbug.com/17583). |
Darin Petkov | c5f5656 | 2011-08-06 16:40:05 -0700 | [diff] [blame] | 392 | simple_proxy_->Connect(properties); |
Darin Petkov | 0828f5f | 2011-08-11 10:18:52 -0700 | [diff] [blame] | 393 | SetState(kStateConnected); |
Darin Petkov | bac9600 | 2011-08-09 13:22:00 -0700 | [diff] [blame] | 394 | EstablishLink(); |
| 395 | } |
| 396 | |
Darin Petkov | fb0625e | 2012-01-16 13:05:56 +0100 | [diff] [blame] | 397 | void Cellular::Disconnect(Error *error) { |
| 398 | VLOG(2) << __func__; |
| 399 | if (state_ != kStateConnected && |
| 400 | state_ != kStateLinked) { |
| 401 | Error::PopulateAndLog( |
| 402 | error, Error::kInProgress, "Not connected; request ignored."); |
| 403 | return; |
| 404 | } |
| 405 | // Defer because we may be in a dbus-c++ callback. |
| 406 | dispatcher()->PostTask( |
| 407 | task_factory_.NewRunnableMethod(&Cellular::DisconnectTask)); |
| 408 | } |
| 409 | |
| 410 | void Cellular::DisconnectTask() { |
| 411 | VLOG(2) << __func__; |
| 412 | DisconnectModem(); |
| 413 | } |
| 414 | |
| 415 | void Cellular::DisconnectModem() { |
| 416 | VLOG(2) << __func__; |
| 417 | if (state_ != kStateConnected && |
| 418 | state_ != kStateLinked) { |
| 419 | return; |
| 420 | } |
| 421 | // TODO(petkov): Switch to asynchronous calls (crosbug.com/17583). Note, |
| 422 | // however, that this is invoked by the Stop method, so some extra refactoring |
| 423 | // may be needed to prevent the device instance from being destroyed before |
| 424 | // the modem manager calls are complete. Maybe Disconnect and Disable need to |
| 425 | // be handled by the parent Modem class. |
| 426 | try { |
| 427 | proxy_->Disconnect(); |
| 428 | } catch (const DBus::Error e) { |
| 429 | LOG(WARNING) << "Disconnect failed: " << e.what(); |
| 430 | } |
| 431 | SetState(kStateRegistered); |
| 432 | } |
| 433 | |
Darin Petkov | bac9600 | 2011-08-09 13:22:00 -0700 | [diff] [blame] | 434 | void Cellular::EstablishLink() { |
| 435 | VLOG(2) << __func__; |
Darin Petkov | 0828f5f | 2011-08-11 10:18:52 -0700 | [diff] [blame] | 436 | CHECK_EQ(kStateConnected, state_); |
| 437 | unsigned int flags = 0; |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 438 | if (manager()->device_info()->GetFlags(interface_index(), &flags) && |
Darin Petkov | 0828f5f | 2011-08-11 10:18:52 -0700 | [diff] [blame] | 439 | (flags & IFF_UP) != 0) { |
| 440 | LinkEvent(flags, IFF_UP); |
| 441 | return; |
| 442 | } |
| 443 | // TODO(petkov): Provide a timeout for a failed link-up request. |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 444 | rtnl_handler()->SetInterfaceFlags(interface_index(), IFF_UP, IFF_UP); |
Darin Petkov | 0828f5f | 2011-08-11 10:18:52 -0700 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | void Cellular::LinkEvent(unsigned int flags, unsigned int change) { |
| 448 | Device::LinkEvent(flags, change); |
| 449 | if ((flags & IFF_UP) != 0 && state_ == kStateConnected) { |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 450 | LOG(INFO) << link_name() << " is up."; |
Darin Petkov | 0828f5f | 2011-08-11 10:18:52 -0700 | [diff] [blame] | 451 | SetState(kStateLinked); |
Darin Petkov | 0828f5f | 2011-08-11 10:18:52 -0700 | [diff] [blame] | 452 | // TODO(petkov): For GSM, remember the APN. |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 453 | if (AcquireIPConfig()) { |
Darin Petkov | 60b8c3b | 2011-08-25 11:03:20 -0700 | [diff] [blame] | 454 | SelectService(service_); |
| 455 | SetServiceState(Service::kStateConfiguring); |
| 456 | } else { |
| 457 | LOG(ERROR) << "Unable to acquire DHCP config."; |
| 458 | } |
Darin Petkov | 0828f5f | 2011-08-11 10:18:52 -0700 | [diff] [blame] | 459 | } else if ((flags & IFF_UP) == 0 && state_ == kStateLinked) { |
| 460 | SetState(kStateConnected); |
Darin Petkov | 2c37738 | 2012-01-11 11:40:43 +0100 | [diff] [blame] | 461 | DestroyService(); |
Darin Petkov | 0828f5f | 2011-08-11 10:18:52 -0700 | [diff] [blame] | 462 | } |
Darin Petkov | c5f5656 | 2011-08-06 16:40:05 -0700 | [diff] [blame] | 463 | } |
| 464 | |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 465 | void Cellular::OnModemStateChanged(uint32 /*old_state*/, |
| 466 | uint32 /*new_state*/, |
| 467 | uint32 /*reason*/) { |
Darin Petkov | c5f5656 | 2011-08-06 16:40:05 -0700 | [diff] [blame] | 468 | // TODO(petkov): Implement this. |
| 469 | NOTIMPLEMENTED(); |
| 470 | } |
| 471 | |
Darin Petkov | ae0c64e | 2011-11-15 15:50:27 +0100 | [diff] [blame] | 472 | void Cellular::OnModemManagerPropertiesChanged( |
| 473 | const DBusPropertiesMap &properties) { |
Darin Petkov | 721ac93 | 2011-11-16 15:43:09 +0100 | [diff] [blame] | 474 | capability_->OnModemManagerPropertiesChanged(properties); |
Chris Masone | 889666b | 2011-07-03 12:58:50 -0700 | [diff] [blame] | 475 | } |
| 476 | |
Darin Petkov | ae0c64e | 2011-11-15 15:50:27 +0100 | [diff] [blame] | 477 | void Cellular::set_home_provider(const Operator &oper) { |
| 478 | home_provider_.CopyFrom(oper); |
| 479 | } |
| 480 | |
Darin Petkov | ac635a8 | 2012-01-10 16:51:58 +0100 | [diff] [blame] | 481 | string Cellular::CreateFriendlyServiceName() { |
| 482 | VLOG(2) << __func__; |
| 483 | return capability_.get() ? capability_->CreateFriendlyServiceName() : ""; |
| 484 | } |
| 485 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 486 | } // namespace shill |