Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
| 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> |
| 8 | #include <linux/if.h> |
| 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> |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 18 | |
| 19 | #include "shill/cellular_service.h" |
| 20 | #include "shill/control_interface.h" |
| 21 | #include "shill/device.h" |
| 22 | #include "shill/device_info.h" |
| 23 | #include "shill/manager.h" |
Darin Petkov | e604f70 | 2011-07-28 15:51:17 -0700 | [diff] [blame] | 24 | #include "shill/modem_simple_proxy_interface.h" |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 25 | #include "shill/profile.h" |
Chris Masone | 889666b | 2011-07-03 12:58:50 -0700 | [diff] [blame] | 26 | #include "shill/property_accessor.h" |
Darin Petkov | e9d12e0 | 2011-07-27 15:09:37 -0700 | [diff] [blame] | 27 | #include "shill/proxy_factory.h" |
Darin Petkov | 0828f5f | 2011-08-11 10:18:52 -0700 | [diff] [blame^] | 28 | #include "shill/rtnl_handler.h" |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 29 | #include "shill/shill_event.h" |
| 30 | |
Chris Masone | 889666b | 2011-07-03 12:58:50 -0700 | [diff] [blame] | 31 | using std::make_pair; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 32 | using std::string; |
Chris Masone | 889666b | 2011-07-03 12:58:50 -0700 | [diff] [blame] | 33 | using std::vector; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 34 | |
| 35 | namespace shill { |
| 36 | |
Darin Petkov | c5f5656 | 2011-08-06 16:40:05 -0700 | [diff] [blame] | 37 | const char Cellular::kConnectPropertyPhoneNumber[] = "number"; |
| 38 | const char Cellular::kPhoneNumberCDMA[] = "#777"; |
| 39 | const char Cellular::kPhoneNumberGSM[] = "*99#"; |
| 40 | |
Chris Masone | 889666b | 2011-07-03 12:58:50 -0700 | [diff] [blame] | 41 | Cellular::Network::Network() { |
| 42 | dict_[flimflam::kStatusProperty] = ""; |
| 43 | dict_[flimflam::kNetworkIdProperty] = ""; |
| 44 | dict_[flimflam::kShortNameProperty] = ""; |
| 45 | dict_[flimflam::kLongNameProperty] = ""; |
| 46 | dict_[flimflam::kTechnologyProperty] = ""; |
| 47 | } |
| 48 | |
| 49 | Cellular::Network::~Network() {} |
| 50 | |
| 51 | const std::string &Cellular::Network::GetStatus() const { |
| 52 | return dict_.find(flimflam::kStatusProperty)->second; |
| 53 | } |
| 54 | |
| 55 | void Cellular::Network::SetStatus(const std::string &status) { |
| 56 | dict_[flimflam::kStatusProperty] = status; |
| 57 | } |
| 58 | |
| 59 | const std::string &Cellular::Network::GetId() const { |
| 60 | return dict_.find(flimflam::kNetworkIdProperty)->second; |
| 61 | } |
| 62 | |
| 63 | void Cellular::Network::SetId(const std::string &id) { |
| 64 | dict_[flimflam::kNetworkIdProperty] = id; |
| 65 | } |
| 66 | |
| 67 | const std::string &Cellular::Network::GetShortName() const { |
| 68 | return dict_.find(flimflam::kShortNameProperty)->second; |
| 69 | } |
| 70 | |
| 71 | void Cellular::Network::SetShortName(const std::string &name) { |
| 72 | dict_[flimflam::kShortNameProperty] = name; |
| 73 | } |
| 74 | |
| 75 | const std::string &Cellular::Network::GetLongName() const { |
| 76 | return dict_.find(flimflam::kLongNameProperty)->second; |
| 77 | } |
| 78 | |
| 79 | void Cellular::Network::SetLongName(const std::string &name) { |
| 80 | dict_[flimflam::kLongNameProperty] = name; |
| 81 | } |
| 82 | |
| 83 | const std::string &Cellular::Network::GetTechnology() const { |
| 84 | return dict_.find(flimflam::kTechnologyProperty)->second; |
| 85 | } |
| 86 | |
| 87 | void Cellular::Network::SetTechnology(const std::string &technology) { |
| 88 | dict_[flimflam::kTechnologyProperty] = technology; |
| 89 | } |
| 90 | |
| 91 | const Stringmap &Cellular::Network::ToDict() const { |
| 92 | return dict_; |
| 93 | } |
| 94 | |
Darin Petkov | bec79a2 | 2011-08-01 14:47:17 -0700 | [diff] [blame] | 95 | Cellular::CDMA::CDMA() |
| 96 | : registration_state_evdo(MM_MODEM_CDMA_REGISTRATION_STATE_UNKNOWN), |
| 97 | registration_state_1x(MM_MODEM_CDMA_REGISTRATION_STATE_UNKNOWN), |
| 98 | activation_state(MM_MODEM_CDMA_ACTIVATION_STATE_NOT_ACTIVATED) {} |
| 99 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 100 | Cellular::Cellular(ControlInterface *control_interface, |
| 101 | EventDispatcher *dispatcher, |
| 102 | Manager *manager, |
Darin Petkov | e9d12e0 | 2011-07-27 15:09:37 -0700 | [diff] [blame] | 103 | const string &link_name, |
| 104 | int interface_index, |
| 105 | Type type, |
| 106 | const string &owner, |
| 107 | const string &path) |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 108 | : Device(control_interface, |
| 109 | dispatcher, |
| 110 | manager, |
Darin Petkov | e9d12e0 | 2011-07-27 15:09:37 -0700 | [diff] [blame] | 111 | link_name, |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 112 | interface_index), |
Darin Petkov | e9d12e0 | 2011-07-27 15:09:37 -0700 | [diff] [blame] | 113 | type_(type), |
| 114 | state_(kStateDisabled), |
Darin Petkov | bac9600 | 2011-08-09 13:22:00 -0700 | [diff] [blame] | 115 | modem_state_(kModemStateUnknown), |
Darin Petkov | e9d12e0 | 2011-07-27 15:09:37 -0700 | [diff] [blame] | 116 | dbus_owner_(owner), |
| 117 | dbus_path_(path), |
Darin Petkov | c5f5656 | 2011-08-06 16:40:05 -0700 | [diff] [blame] | 118 | task_factory_(this), |
Darin Petkov | e9d12e0 | 2011-07-27 15:09:37 -0700 | [diff] [blame] | 119 | allow_roaming_(false), |
| 120 | prl_version_(0), |
| 121 | scanning_(false), |
| 122 | scan_interval_(0) { |
| 123 | store_.RegisterConstString(flimflam::kDBusConnectionProperty, &dbus_owner_); |
| 124 | store_.RegisterConstString(flimflam::kDBusObjectProperty, &dbus_path_); |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 125 | store_.RegisterConstString(flimflam::kCarrierProperty, &carrier_); |
Chris Masone | 4d42df8 | 2011-07-02 17:09:39 -0700 | [diff] [blame] | 126 | store_.RegisterBool(flimflam::kCellularAllowRoamingProperty, &allow_roaming_); |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 127 | store_.RegisterConstString(flimflam::kEsnProperty, &esn_); |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 128 | store_.RegisterConstString(flimflam::kFirmwareRevisionProperty, |
| 129 | &firmware_revision_); |
| 130 | store_.RegisterConstString(flimflam::kHardwareRevisionProperty, |
| 131 | &hardware_revision_); |
Chris Masone | 4d42df8 | 2011-07-02 17:09:39 -0700 | [diff] [blame] | 132 | store_.RegisterConstString(flimflam::kImeiProperty, &imei_); |
| 133 | store_.RegisterConstString(flimflam::kImsiProperty, &imsi_); |
| 134 | store_.RegisterConstString(flimflam::kManufacturerProperty, &manufacturer_); |
| 135 | store_.RegisterConstString(flimflam::kMdnProperty, &mdn_); |
| 136 | store_.RegisterConstString(flimflam::kMeidProperty, &meid_); |
| 137 | store_.RegisterConstString(flimflam::kMinProperty, &min_); |
| 138 | store_.RegisterConstString(flimflam::kModelIDProperty, &model_id_); |
Darin Petkov | ceb6817 | 2011-07-29 14:47:48 -0700 | [diff] [blame] | 139 | store_.RegisterConstUint16(flimflam::kPRLVersionProperty, &prl_version_); |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 140 | |
Chris Masone | 889666b | 2011-07-03 12:58:50 -0700 | [diff] [blame] | 141 | HelpRegisterDerivedStrIntPair(flimflam::kSIMLockStatusProperty, |
| 142 | &Cellular::SimLockStatusToProperty, |
| 143 | NULL); |
| 144 | HelpRegisterDerivedStringmaps(flimflam::kFoundNetworksProperty, |
| 145 | &Cellular::EnumerateNetworks, |
| 146 | NULL); |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 147 | |
Chris Masone | 4d42df8 | 2011-07-02 17:09:39 -0700 | [diff] [blame] | 148 | store_.RegisterConstBool(flimflam::kScanningProperty, &scanning_); |
| 149 | store_.RegisterUint16(flimflam::kScanIntervalProperty, &scan_interval_); |
| 150 | |
Darin Petkov | e9d12e0 | 2011-07-27 15:09:37 -0700 | [diff] [blame] | 151 | VLOG(2) << "Cellular device " << link_name_ << " initialized: " |
| 152 | << GetTypeString(); |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Darin Petkov | e9d12e0 | 2011-07-27 15:09:37 -0700 | [diff] [blame] | 155 | Cellular::~Cellular() {} |
| 156 | |
Darin Petkov | 0828f5f | 2011-08-11 10:18:52 -0700 | [diff] [blame^] | 157 | string Cellular::GetTypeString() { |
Darin Petkov | e9d12e0 | 2011-07-27 15:09:37 -0700 | [diff] [blame] | 158 | switch (type_) { |
| 159 | case kTypeGSM: return "CellularTypeGSM"; |
| 160 | case kTypeCDMA: return "CellularTypeCDMA"; |
Darin Petkov | 0828f5f | 2011-08-11 10:18:52 -0700 | [diff] [blame^] | 161 | default: NOTREACHED(); |
Darin Petkov | e9d12e0 | 2011-07-27 15:09:37 -0700 | [diff] [blame] | 162 | } |
Darin Petkov | 0828f5f | 2011-08-11 10:18:52 -0700 | [diff] [blame^] | 163 | return StringPrintf("CellularTypeUnknown-%d", type_); |
Darin Petkov | e9d12e0 | 2011-07-27 15:09:37 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Darin Petkov | 0828f5f | 2011-08-11 10:18:52 -0700 | [diff] [blame^] | 166 | string Cellular::GetStateString() { |
Darin Petkov | e9d12e0 | 2011-07-27 15:09:37 -0700 | [diff] [blame] | 167 | switch (state_) { |
| 168 | case kStateDisabled: return "CellularStateDisabled"; |
| 169 | case kStateEnabled: return "CellularStateEnabled"; |
| 170 | case kStateRegistered: return "CellularStateRegistered"; |
| 171 | case kStateConnected: return "CellularStateConnected"; |
Darin Petkov | 0828f5f | 2011-08-11 10:18:52 -0700 | [diff] [blame^] | 172 | case kStateLinked: return "CellularStateLinked"; |
| 173 | default: NOTREACHED(); |
Darin Petkov | e9d12e0 | 2011-07-27 15:09:37 -0700 | [diff] [blame] | 174 | } |
Darin Petkov | 0828f5f | 2011-08-11 10:18:52 -0700 | [diff] [blame^] | 175 | return StringPrintf("CellularStateUnknown-%d", state_); |
| 176 | } |
| 177 | |
| 178 | void Cellular::SetState(State state) { |
| 179 | VLOG(2) << "Current state: " << GetStateString(); |
| 180 | state_ = state; |
| 181 | VLOG(2) << "New state: " << GetStateString(); |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | void Cellular::Start() { |
Darin Petkov | 0828f5f | 2011-08-11 10:18:52 -0700 | [diff] [blame^] | 185 | LOG(INFO) << "Start"; |
Darin Petkov | f5f61e0 | 2011-07-29 11:35:40 -0700 | [diff] [blame] | 186 | VLOG(2) << __func__ << ": " << GetStateString(); |
Darin Petkov | bec79a2 | 2011-08-01 14:47:17 -0700 | [diff] [blame] | 187 | InitProxies(); |
Darin Petkov | f5f61e0 | 2011-07-29 11:35:40 -0700 | [diff] [blame] | 188 | EnableModem(); |
Darin Petkov | ceb6817 | 2011-07-29 14:47:48 -0700 | [diff] [blame] | 189 | if (type_ == kTypeGSM) { |
| 190 | RegisterGSMModem(); |
| 191 | } |
Darin Petkov | f5f61e0 | 2011-07-29 11:35:40 -0700 | [diff] [blame] | 192 | GetModemStatus(); |
Darin Petkov | ceb6817 | 2011-07-29 14:47:48 -0700 | [diff] [blame] | 193 | GetModemIdentifiers(); |
| 194 | if (type_ == kTypeGSM) { |
| 195 | GetGSMProperties(); |
| 196 | } |
| 197 | GetModemInfo(); |
| 198 | GetModemRegistrationState(); |
Darin Petkov | e9d12e0 | 2011-07-27 15:09:37 -0700 | [diff] [blame] | 199 | // TODO(petkov): Device::Start(); |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | void Cellular::Stop() { |
Darin Petkov | e9d12e0 | 2011-07-27 15:09:37 -0700 | [diff] [blame] | 203 | proxy_.reset(); |
Darin Petkov | e604f70 | 2011-07-28 15:51:17 -0700 | [diff] [blame] | 204 | simple_proxy_.reset(); |
Darin Petkov | bec79a2 | 2011-08-01 14:47:17 -0700 | [diff] [blame] | 205 | cdma_proxy_.reset(); |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 206 | manager_->DeregisterService(service_); |
Darin Petkov | e9d12e0 | 2011-07-27 15:09:37 -0700 | [diff] [blame] | 207 | service_ = NULL; // Breaks a reference cycle. |
Darin Petkov | 0828f5f | 2011-08-11 10:18:52 -0700 | [diff] [blame^] | 208 | SetState(kStateDisabled); |
| 209 | RTNLHandler::GetInstance()->SetInterfaceFlags(interface_index_, 0, IFF_UP); |
Darin Petkov | e9d12e0 | 2011-07-27 15:09:37 -0700 | [diff] [blame] | 210 | // TODO(petkov): Device::Stop(); |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 211 | } |
| 212 | |
Darin Petkov | bec79a2 | 2011-08-01 14:47:17 -0700 | [diff] [blame] | 213 | void Cellular::InitProxies() { |
Darin Petkov | 0828f5f | 2011-08-11 10:18:52 -0700 | [diff] [blame^] | 214 | LOG(INFO) << "InitProxies"; |
Darin Petkov | bec79a2 | 2011-08-01 14:47:17 -0700 | [diff] [blame] | 215 | proxy_.reset( |
Darin Petkov | c5f5656 | 2011-08-06 16:40:05 -0700 | [diff] [blame] | 216 | ProxyFactory::factory()->CreateModemProxy(this, dbus_path_, dbus_owner_)); |
Darin Petkov | bec79a2 | 2011-08-01 14:47:17 -0700 | [diff] [blame] | 217 | simple_proxy_.reset( |
| 218 | ProxyFactory::factory()->CreateModemSimpleProxy( |
| 219 | dbus_path_, dbus_owner_)); |
| 220 | switch (type_) { |
| 221 | case kTypeGSM: |
| 222 | NOTIMPLEMENTED(); |
| 223 | break; |
| 224 | case kTypeCDMA: |
| 225 | cdma_proxy_.reset( |
| 226 | ProxyFactory::factory()->CreateModemCDMAProxy( |
Darin Petkov | d966195 | 2011-08-03 16:25:42 -0700 | [diff] [blame] | 227 | this, dbus_path_, dbus_owner_)); |
Darin Petkov | bec79a2 | 2011-08-01 14:47:17 -0700 | [diff] [blame] | 228 | break; |
| 229 | default: NOTREACHED(); |
| 230 | } |
| 231 | } |
| 232 | |
Darin Petkov | f5f61e0 | 2011-07-29 11:35:40 -0700 | [diff] [blame] | 233 | void Cellular::EnableModem() { |
| 234 | CHECK_EQ(kStateDisabled, state_); |
| 235 | // TODO(petkov): Switch to asynchronous calls (crosbug.com/17583). |
| 236 | proxy_->Enable(true); |
Darin Petkov | 0828f5f | 2011-08-11 10:18:52 -0700 | [diff] [blame^] | 237 | SetState(kStateEnabled); |
Darin Petkov | f5f61e0 | 2011-07-29 11:35:40 -0700 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | void Cellular::GetModemStatus() { |
Darin Petkov | ceb6817 | 2011-07-29 14:47:48 -0700 | [diff] [blame] | 241 | CHECK_EQ(kStateEnabled, state_); |
Darin Petkov | f5f61e0 | 2011-07-29 11:35:40 -0700 | [diff] [blame] | 242 | // TODO(petkov): Switch to asynchronous calls (crosbug.com/17583). |
| 243 | DBusPropertiesMap properties = simple_proxy_->GetStatus(); |
Darin Petkov | ceb6817 | 2011-07-29 14:47:48 -0700 | [diff] [blame] | 244 | if (DBusProperties::GetString(properties, "carrier", &carrier_) && |
| 245 | type_ == kTypeCDMA) { |
| 246 | // TODO(petkov): Set Cellular.FirmwareImageName and home_provider based on |
| 247 | // the carrier. |
Darin Petkov | f5f61e0 | 2011-07-29 11:35:40 -0700 | [diff] [blame] | 248 | } |
Darin Petkov | ceb6817 | 2011-07-29 14:47:48 -0700 | [diff] [blame] | 249 | DBusProperties::GetString(properties, "meid", &meid_); |
| 250 | DBusProperties::GetString(properties, "imei", &imei_); |
| 251 | if (DBusProperties::GetString(properties, "imsi", &imsi_) && |
| 252 | type_ == kTypeGSM) { |
| 253 | // TODO(petkov): Set GSM provider. |
| 254 | } |
| 255 | DBusProperties::GetString(properties, "esn", &esn_); |
| 256 | DBusProperties::GetString(properties, "mdn", &mdn_); |
| 257 | DBusProperties::GetString(properties, "min", &min_); |
| 258 | DBusProperties::GetUint16(properties, "prl_version", &prl_version_); |
| 259 | DBusProperties::GetString( |
| 260 | properties, "firmware_revision", &firmware_revision_); |
| 261 | // TODO(petkov): Get payment_url/olp_url/usage_url. For now, get these from |
| 262 | // ModemManager to match flimflam. In the future, provide a plugin API to get |
| 263 | // these directly from the modem driver. |
| 264 | if (type_ == kTypeCDMA) { |
| 265 | // TODO(petkov): Get activation_state. |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | void Cellular::GetModemIdentifiers() { |
| 270 | // TODO(petkov): Implement this. |
| 271 | NOTIMPLEMENTED(); |
| 272 | } |
| 273 | |
| 274 | void Cellular::GetGSMProperties() { |
| 275 | // TODO(petkov): Implement this. |
| 276 | NOTIMPLEMENTED(); |
| 277 | } |
| 278 | |
| 279 | void Cellular::RegisterGSMModem() { |
| 280 | // TODO(petkov): Invoke ModemManager.Modem.Gsm.Network.Register. |
| 281 | NOTIMPLEMENTED(); |
| 282 | } |
| 283 | |
| 284 | void Cellular::GetModemInfo() { |
| 285 | ModemProxyInterface::Info info = proxy_->GetInfo(); |
| 286 | manufacturer_ = info._1; |
| 287 | model_id_ = info._2; |
| 288 | hardware_revision_ = info._3; |
| 289 | VLOG(2) << "ModemInfo: " << manufacturer_ << ", " << model_id_ << ", " |
| 290 | << hardware_revision_; |
| 291 | } |
| 292 | |
| 293 | void Cellular::GetModemRegistrationState() { |
Darin Petkov | bec79a2 | 2011-08-01 14:47:17 -0700 | [diff] [blame] | 294 | switch (type_) { |
| 295 | case kTypeGSM: |
| 296 | GetGSMRegistrationState(); |
| 297 | break; |
| 298 | case kTypeCDMA: |
| 299 | GetCDMARegistrationState(); |
| 300 | break; |
| 301 | default: NOTREACHED(); |
| 302 | } |
Darin Petkov | d966195 | 2011-08-03 16:25:42 -0700 | [diff] [blame] | 303 | HandleNewRegistrationState(); |
Darin Petkov | bec79a2 | 2011-08-01 14:47:17 -0700 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | void Cellular::GetCDMARegistrationState() { |
| 307 | CHECK_EQ(kTypeCDMA, type_); |
| 308 | cdma_proxy_->GetRegistrationState(&cdma_.registration_state_1x, |
| 309 | &cdma_.registration_state_evdo); |
| 310 | VLOG(2) << "CDMA Registration: 1x(" << cdma_.registration_state_1x |
| 311 | << ") EVDO(" << cdma_.registration_state_evdo << ")"; |
| 312 | // TODO(petkov): handle_reported_connect? |
| 313 | } |
| 314 | |
| 315 | void Cellular::GetGSMRegistrationState() { |
Darin Petkov | ceb6817 | 2011-07-29 14:47:48 -0700 | [diff] [blame] | 316 | // TODO(petkov): Implement this. |
| 317 | NOTIMPLEMENTED(); |
| 318 | } |
| 319 | |
Darin Petkov | d966195 | 2011-08-03 16:25:42 -0700 | [diff] [blame] | 320 | bool Cellular::IsModemRegistered() { |
| 321 | return cdma_.registration_state_1x != |
| 322 | MM_MODEM_CDMA_REGISTRATION_STATE_UNKNOWN || |
| 323 | cdma_.registration_state_evdo != |
| 324 | MM_MODEM_CDMA_REGISTRATION_STATE_UNKNOWN; |
| 325 | // TODO(petkov): Handle GSM states. |
| 326 | } |
| 327 | |
| 328 | void Cellular::HandleNewRegistrationState() { |
Darin Petkov | 0828f5f | 2011-08-11 10:18:52 -0700 | [diff] [blame^] | 329 | dispatcher_->PostTask( |
| 330 | task_factory_.NewRunnableMethod( |
| 331 | &Cellular::HandleNewRegistrationStateTask)); |
| 332 | } |
| 333 | |
| 334 | void Cellular::HandleNewRegistrationStateTask() { |
Darin Petkov | d966195 | 2011-08-03 16:25:42 -0700 | [diff] [blame] | 335 | VLOG(2) << __func__; |
| 336 | if (!IsModemRegistered()) { |
| 337 | service_ = NULL; |
Darin Petkov | 0828f5f | 2011-08-11 10:18:52 -0700 | [diff] [blame^] | 338 | if (state_ == kStateLinked || |
| 339 | state_ == kStateConnected || |
| 340 | state_ == kStateRegistered) { |
| 341 | SetState(kStateEnabled); |
Darin Petkov | d966195 | 2011-08-03 16:25:42 -0700 | [diff] [blame] | 342 | } |
| 343 | return; |
| 344 | } |
Darin Petkov | d966195 | 2011-08-03 16:25:42 -0700 | [diff] [blame] | 345 | if (state_ == kStateEnabled) { |
Darin Petkov | 0828f5f | 2011-08-11 10:18:52 -0700 | [diff] [blame^] | 346 | SetState(kStateRegistered); |
Darin Petkov | d966195 | 2011-08-03 16:25:42 -0700 | [diff] [blame] | 347 | } |
| 348 | if (!service_.get()) { |
| 349 | // For now, no endpoint is created. Revisit if necessary. |
| 350 | CreateService(); |
| 351 | } |
| 352 | GetModemSignalQuality(); |
Darin Petkov | 0828f5f | 2011-08-11 10:18:52 -0700 | [diff] [blame^] | 353 | if (state_ == kStateRegistered && modem_state_ == kModemStateConnected) { |
| 354 | SetState(kStateConnected); |
| 355 | EstablishLink(); |
| 356 | } |
Darin Petkov | d966195 | 2011-08-03 16:25:42 -0700 | [diff] [blame] | 357 | // TODO(petkov): Update the service. |
| 358 | } |
| 359 | |
| 360 | void Cellular::GetModemSignalQuality() { |
Darin Petkov | 0828f5f | 2011-08-11 10:18:52 -0700 | [diff] [blame^] | 361 | VLOG(2) << __func__; |
Darin Petkov | d966195 | 2011-08-03 16:25:42 -0700 | [diff] [blame] | 362 | uint32 strength = 0; |
| 363 | switch (type_) { |
| 364 | case kTypeGSM: |
| 365 | strength = GetGSMSignalQuality(); |
| 366 | break; |
| 367 | case kTypeCDMA: |
| 368 | strength = GetCDMASignalQuality(); |
| 369 | break; |
| 370 | default: NOTREACHED(); |
| 371 | } |
| 372 | HandleNewSignalQuality(strength); |
| 373 | } |
| 374 | |
| 375 | uint32 Cellular::GetCDMASignalQuality() { |
Darin Petkov | 0828f5f | 2011-08-11 10:18:52 -0700 | [diff] [blame^] | 376 | VLOG(2) << __func__; |
Darin Petkov | d966195 | 2011-08-03 16:25:42 -0700 | [diff] [blame] | 377 | CHECK_EQ(kTypeCDMA, type_); |
| 378 | // TODO(petkov): Switch to asynchronous calls (crosbug.com/17583). |
| 379 | return cdma_proxy_->GetSignalQuality(); |
| 380 | } |
| 381 | |
| 382 | uint32 Cellular::GetGSMSignalQuality() { |
Darin Petkov | ceb6817 | 2011-07-29 14:47:48 -0700 | [diff] [blame] | 383 | // TODO(petkov): Implement this. |
| 384 | NOTIMPLEMENTED(); |
Darin Petkov | d966195 | 2011-08-03 16:25:42 -0700 | [diff] [blame] | 385 | return 0; |
| 386 | } |
| 387 | |
| 388 | void Cellular::HandleNewSignalQuality(uint32 strength) { |
| 389 | VLOG(2) << "Signal strength: " << strength; |
| 390 | if (service_.get()) { |
| 391 | service_->set_strength(strength); |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | void Cellular::CreateService() { |
Darin Petkov | 0828f5f | 2011-08-11 10:18:52 -0700 | [diff] [blame^] | 396 | VLOG(2) << __func__; |
Darin Petkov | d966195 | 2011-08-03 16:25:42 -0700 | [diff] [blame] | 397 | CHECK(!service_.get()); |
| 398 | service_ = |
| 399 | new CellularService(control_interface_, dispatcher_, manager_, this); |
| 400 | // TODO(petkov): Set activation_state. |
| 401 | // TODO(petkov): Set operator. |
| 402 | // TODO(petkov): Set old_url/usage_url. |
Darin Petkov | f5f61e0 | 2011-07-29 11:35:40 -0700 | [diff] [blame] | 403 | } |
| 404 | |
Darin Petkov | 6f9eaa3 | 2011-08-09 15:26:44 -0700 | [diff] [blame] | 405 | bool Cellular::TechnologyIs(const Device::Technology type) const { |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 406 | return type == Device::kCellular; |
| 407 | } |
| 408 | |
Darin Petkov | c5f5656 | 2011-08-06 16:40:05 -0700 | [diff] [blame] | 409 | void Cellular::Connect() { |
| 410 | VLOG(2) << __func__; |
Darin Petkov | 0828f5f | 2011-08-11 10:18:52 -0700 | [diff] [blame^] | 411 | if (state_ == kStateConnected || |
| 412 | state_ == kStateLinked) { |
Darin Petkov | c5f5656 | 2011-08-06 16:40:05 -0700 | [diff] [blame] | 413 | return; |
| 414 | } |
| 415 | CHECK_EQ(kStateRegistered, state_); |
| 416 | DBusPropertiesMap properties; |
| 417 | const char *phone_number = NULL; |
| 418 | switch (type_) { |
| 419 | case kTypeGSM: |
| 420 | phone_number = kPhoneNumberGSM; |
| 421 | break; |
| 422 | case kTypeCDMA: |
| 423 | phone_number = kPhoneNumberCDMA; |
| 424 | break; |
| 425 | default: NOTREACHED(); |
| 426 | } |
| 427 | properties[kConnectPropertyPhoneNumber].writer().append_string(phone_number); |
| 428 | // TODO(petkov): Setup apn and "home_only". |
| 429 | |
| 430 | // Defer connect because we may be in a dbus-c++ callback. |
| 431 | dispatcher_->PostTask( |
| 432 | task_factory_.NewRunnableMethod(&Cellular::ConnectTask, properties)); |
| 433 | } |
| 434 | |
| 435 | void Cellular::ConnectTask(const DBusPropertiesMap &properties) { |
| 436 | VLOG(2) << __func__; |
Darin Petkov | bac9600 | 2011-08-09 13:22:00 -0700 | [diff] [blame] | 437 | // TODO(petkov): Switch to asynchronous calls (crosbug.com/17583). |
Darin Petkov | c5f5656 | 2011-08-06 16:40:05 -0700 | [diff] [blame] | 438 | simple_proxy_->Connect(properties); |
Darin Petkov | 0828f5f | 2011-08-11 10:18:52 -0700 | [diff] [blame^] | 439 | SetState(kStateConnected); |
Darin Petkov | bac9600 | 2011-08-09 13:22:00 -0700 | [diff] [blame] | 440 | EstablishLink(); |
| 441 | } |
| 442 | |
| 443 | void Cellular::EstablishLink() { |
| 444 | VLOG(2) << __func__; |
Darin Petkov | 0828f5f | 2011-08-11 10:18:52 -0700 | [diff] [blame^] | 445 | CHECK_EQ(kStateConnected, state_); |
| 446 | unsigned int flags = 0; |
| 447 | if (manager_->device_info()->GetFlags(interface_index_, &flags) && |
| 448 | (flags & IFF_UP) != 0) { |
| 449 | LinkEvent(flags, IFF_UP); |
| 450 | return; |
| 451 | } |
| 452 | // TODO(petkov): Provide a timeout for a failed link-up request. |
| 453 | RTNLHandler::GetInstance()->SetInterfaceFlags( |
| 454 | interface_index_, IFF_UP, IFF_UP); |
| 455 | } |
| 456 | |
| 457 | void Cellular::LinkEvent(unsigned int flags, unsigned int change) { |
| 458 | Device::LinkEvent(flags, change); |
| 459 | if ((flags & IFF_UP) != 0 && state_ == kStateConnected) { |
| 460 | LOG(INFO) << link_name_ << " is up."; |
| 461 | SetState(kStateLinked); |
| 462 | manager_->RegisterService(service_); |
| 463 | // TODO(petkov): For GSM, remember the APN. |
| 464 | // TODO(petkov): Acquire IP. |
| 465 | } else if ((flags & IFF_UP) == 0 && state_ == kStateLinked) { |
| 466 | SetState(kStateConnected); |
| 467 | manager_->DeregisterService(service_); |
| 468 | // TODO(petkov): Release IP. |
| 469 | } |
Darin Petkov | c5f5656 | 2011-08-06 16:40:05 -0700 | [diff] [blame] | 470 | } |
| 471 | |
Darin Petkov | d966195 | 2011-08-03 16:25:42 -0700 | [diff] [blame] | 472 | void Cellular::OnCDMARegistrationStateChanged(uint32 state_1x, |
| 473 | uint32 state_evdo) { |
| 474 | CHECK_EQ(kTypeCDMA, type_); |
| 475 | cdma_.registration_state_1x = state_1x; |
| 476 | cdma_.registration_state_evdo = state_evdo; |
| 477 | HandleNewRegistrationState(); |
| 478 | } |
| 479 | |
| 480 | void Cellular::OnCDMASignalQualityChanged(uint32 strength) { |
| 481 | CHECK_EQ(kTypeCDMA, type_); |
| 482 | HandleNewSignalQuality(strength); |
| 483 | } |
| 484 | |
Darin Petkov | c5f5656 | 2011-08-06 16:40:05 -0700 | [diff] [blame] | 485 | void Cellular::OnModemStateChanged(uint32 old_state, |
| 486 | uint32 new_state, |
| 487 | uint32 reason) { |
| 488 | // TODO(petkov): Implement this. |
| 489 | NOTIMPLEMENTED(); |
| 490 | } |
| 491 | |
Chris Masone | 889666b | 2011-07-03 12:58:50 -0700 | [diff] [blame] | 492 | Stringmaps Cellular::EnumerateNetworks() { |
| 493 | Stringmaps to_return; |
| 494 | for (vector<Network>::const_iterator it = found_networks_.begin(); |
| 495 | it != found_networks_.end(); |
| 496 | ++it) { |
| 497 | to_return.push_back(it->ToDict()); |
| 498 | } |
| 499 | return to_return; |
| 500 | } |
| 501 | |
| 502 | StrIntPair Cellular::SimLockStatusToProperty() { |
| 503 | return StrIntPair(make_pair(flimflam::kSIMLockTypeProperty, |
| 504 | sim_lock_status_.lock_type), |
| 505 | make_pair(flimflam::kSIMLockRetriesLeftProperty, |
| 506 | sim_lock_status_.retries_left)); |
| 507 | } |
| 508 | |
| 509 | void Cellular::HelpRegisterDerivedStringmaps( |
| 510 | const string &name, |
| 511 | Stringmaps(Cellular::*get)(void), |
| 512 | bool(Cellular::*set)(const Stringmaps&)) { |
| 513 | store_.RegisterDerivedStringmaps( |
| 514 | name, |
| 515 | StringmapsAccessor( |
| 516 | new CustomAccessor<Cellular, Stringmaps>(this, get, set))); |
| 517 | } |
| 518 | |
| 519 | void Cellular::HelpRegisterDerivedStrIntPair( |
| 520 | const string &name, |
| 521 | StrIntPair(Cellular::*get)(void), |
| 522 | bool(Cellular::*set)(const StrIntPair&)) { |
| 523 | store_.RegisterDerivedStrIntPair( |
| 524 | name, |
| 525 | StrIntPairAccessor( |
| 526 | new CustomAccessor<Cellular, StrIntPair>(this, get, set))); |
| 527 | } |
| 528 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 529 | } // namespace shill |