Paul Stewart | 75897df | 2011-04-27 09:05:53 -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 | |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 5 | #include "shill/device.h" |
| 6 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 7 | #include <time.h> |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 8 | #include <stdio.h> |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 9 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 10 | #include <string> |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 11 | #include <vector> |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 12 | |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 13 | #include <base/logging.h> |
Chris Masone | 487b8bf | 2011-05-13 16:27:57 -0700 | [diff] [blame] | 14 | #include <base/memory/ref_counted.h> |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 15 | #include <base/stringprintf.h> |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 16 | #include <chromeos/dbus/service_constants.h> |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 17 | |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 18 | #include "shill/connection.h" |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 19 | #include "shill/control_interface.h" |
Chris Masone | d7732e4 | 2011-05-20 11:08:56 -0700 | [diff] [blame] | 20 | #include "shill/device_dbus_adaptor.h" |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 21 | #include "shill/dhcp_config.h" |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 22 | #include "shill/dhcp_provider.h" |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 23 | #include "shill/error.h" |
| 24 | #include "shill/manager.h" |
Chris Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame] | 25 | #include "shill/property_accessor.h" |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 26 | #include "shill/refptr_types.h" |
Paul Stewart | c39f113 | 2011-06-22 12:02:28 -0700 | [diff] [blame] | 27 | #include "shill/rtnl_handler.h" |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 28 | #include "shill/service.h" |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 29 | #include "shill/shill_event.h" |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 30 | #include "shill/store_interface.h" |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 31 | |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 32 | using base::StringPrintf; |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 33 | using std::string; |
| 34 | using std::vector; |
| 35 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 36 | namespace shill { |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 37 | |
| 38 | // static |
| 39 | const char Device::kStoragePowered[] = "Powered"; |
| 40 | |
| 41 | // static |
| 42 | const char Device::kStorageIPConfigs[] = "IPConfigs"; |
| 43 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 44 | Device::Device(ControlInterface *control_interface, |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 45 | EventDispatcher *dispatcher, |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 46 | Manager *manager, |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 47 | const string &link_name, |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 48 | const string &address, |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 49 | int interface_index) |
Chris Masone | 853b81b | 2011-06-24 14:11:41 -0700 | [diff] [blame] | 50 | : powered_(true), |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 51 | reconnect_(true), |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 52 | hardware_address_(address), |
mukesh agrawal | f60e406 | 2011-05-27 13:13:41 -0700 | [diff] [blame] | 53 | interface_index_(interface_index), |
| 54 | running_(false), |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 55 | link_name_(link_name), |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 56 | unique_id_(link_name), |
Darin Petkov | d966195 | 2011-08-03 16:25:42 -0700 | [diff] [blame] | 57 | control_interface_(control_interface), |
| 58 | dispatcher_(dispatcher), |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 59 | manager_(manager), |
Darin Petkov | 77cb681 | 2011-08-15 16:19:41 -0700 | [diff] [blame] | 60 | adaptor_(control_interface->CreateDeviceAdaptor(this)), |
| 61 | dhcp_provider_(DHCPProvider::GetInstance()) { |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 62 | store_.RegisterConstString(flimflam::kAddressProperty, &hardware_address_); |
Chris Masone | 4d42df8 | 2011-07-02 17:09:39 -0700 | [diff] [blame] | 63 | |
| 64 | // flimflam::kBgscanMethodProperty: Registered in WiFi |
| 65 | // flimflam::kBgscanShortIntervalProperty: Registered in WiFi |
| 66 | // flimflam::kBgscanSignalThresholdProperty: Registered in WiFi |
| 67 | |
| 68 | // flimflam::kCellularAllowRoamingProperty: Registered in Cellular |
| 69 | // flimflam::kCarrierProperty: Registered in Cellular |
| 70 | // flimflam::kEsnProperty: Registered in Cellular |
Darin Petkov | 3335b37 | 2011-08-22 11:05:32 -0700 | [diff] [blame] | 71 | // flimflam::kHomeProviderProperty: Registered in Cellular |
Chris Masone | 4d42df8 | 2011-07-02 17:09:39 -0700 | [diff] [blame] | 72 | // flimflam::kImeiProperty: Registered in Cellular |
| 73 | // flimflam::kImsiProperty: Registered in Cellular |
| 74 | // flimflam::kManufacturerProperty: Registered in Cellular |
| 75 | // flimflam::kMdnProperty: Registered in Cellular |
| 76 | // flimflam::kMeidProperty: Registered in Cellular |
| 77 | // flimflam::kMinProperty: Registered in Cellular |
| 78 | // flimflam::kModelIDProperty: Registered in Cellular |
| 79 | // flimflam::kFirmwareRevisionProperty: Registered in Cellular |
| 80 | // flimflam::kHardwareRevisionProperty: Registered in Cellular |
| 81 | // flimflam::kPRLVersionProperty: Registered in Cellular |
| 82 | // flimflam::kSIMLockStatusProperty: Registered in Cellular |
| 83 | // flimflam::kFoundNetworksProperty: Registered in Cellular |
Darin Petkov | e9d12e0 | 2011-07-27 15:09:37 -0700 | [diff] [blame] | 84 | // flimflam::kDBusConnectionProperty: Registered in Cellular |
| 85 | // flimflam::kDBusObjectProperty: Register in Cellular |
Chris Masone | 4d42df8 | 2011-07-02 17:09:39 -0700 | [diff] [blame] | 86 | |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 87 | // TODO(cmasone): Chrome doesn't use this...does anyone? |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 88 | // store_.RegisterConstString(flimflam::kInterfaceProperty, &link_name_); |
| 89 | HelpRegisterDerivedStrings(flimflam::kIPConfigsProperty, |
| 90 | &Device::AvailableIPConfigs, |
| 91 | NULL); |
| 92 | store_.RegisterConstString(flimflam::kNameProperty, &link_name_); |
| 93 | store_.RegisterBool(flimflam::kPoweredProperty, &powered_); |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 94 | // TODO(cmasone): Chrome doesn't use this...does anyone? |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 95 | // store_.RegisterConstBool(flimflam::kReconnectProperty, &reconnect_); |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 96 | |
Chris Masone | 4e85161 | 2011-07-01 10:46:53 -0700 | [diff] [blame] | 97 | // TODO(cmasone): Figure out what shill concept maps to flimflam's "Network". |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 98 | // known_properties_.push_back(flimflam::kNetworksProperty); |
| 99 | |
Chris Masone | 4d42df8 | 2011-07-02 17:09:39 -0700 | [diff] [blame] | 100 | // flimflam::kScanningProperty: Registered in WiFi, Cellular |
| 101 | // flimflam::kScanIntervalProperty: Registered in WiFi, Cellular |
| 102 | |
| 103 | // TODO(pstew): Initialize Interface monitor, so we can detect new interfaces |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 104 | VLOG(2) << "Device " << link_name_ << " index " << interface_index; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | Device::~Device() { |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 108 | VLOG(2) << "Device " << link_name_ << " destroyed."; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | void Device::Start() { |
| 112 | running_ = true; |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 113 | VLOG(2) << "Device " << link_name_ << " starting."; |
Chris Masone | 413a319 | 2011-05-09 17:10:05 -0700 | [diff] [blame] | 114 | adaptor_->UpdateEnabled(); |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | void Device::Stop() { |
| 118 | running_ = false; |
Chris Masone | 413a319 | 2011-05-09 17:10:05 -0700 | [diff] [blame] | 119 | adaptor_->UpdateEnabled(); |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Darin Petkov | 6f9eaa3 | 2011-08-09 15:26:44 -0700 | [diff] [blame] | 122 | bool Device::TechnologyIs(const Technology type) const { |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 123 | return false; |
| 124 | } |
| 125 | |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 126 | void Device::LinkEvent(unsigned flags, unsigned change) { |
| 127 | VLOG(2) << "Device " << link_name_ << " flags " << flags << " changed " |
| 128 | << change; |
| 129 | } |
| 130 | |
| 131 | void Device::Scan() { |
| 132 | VLOG(2) << "Device " << link_name_ << " scan requested."; |
| 133 | } |
| 134 | |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 135 | string Device::GetRpcIdentifier() { |
| 136 | return adaptor_->GetRpcIdentifier(); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 139 | string Device::GetStorageIdentifier() { |
| 140 | string id = GetRpcIdentifier(); |
| 141 | ControlInterface::RpcIdToStorageId(&id); |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 142 | size_t needle = id.find('_'); |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 143 | DLOG_IF(ERROR, needle == string::npos) << "No _ in storage id?!?!"; |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 144 | id.replace(id.begin() + needle + 1, id.end(), hardware_address_); |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 145 | return id; |
| 146 | } |
| 147 | |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 148 | const string& Device::FriendlyName() const { |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 149 | return link_name_; |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 152 | const string& Device::UniqueName() const { |
| 153 | return unique_id_; |
| 154 | } |
| 155 | |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 156 | bool Device::Load(StoreInterface *storage) { |
| 157 | const string id = GetStorageIdentifier(); |
| 158 | if (!storage->ContainsGroup(id)) { |
| 159 | LOG(WARNING) << "Device is not available in the persistent store: " << id; |
| 160 | return false; |
| 161 | } |
| 162 | storage->GetBool(id, kStoragePowered, &powered_); |
| 163 | // TODO(cmasone): What does it mean to load an IPConfig identifier?? |
| 164 | return true; |
| 165 | } |
| 166 | |
| 167 | bool Device::Save(StoreInterface *storage) { |
| 168 | const string id = GetStorageIdentifier(); |
| 169 | storage->SetBool(id, kStoragePowered, powered_); |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 170 | if (ipconfig_.get()) { |
| 171 | // The _0 is an index into the list of IPConfigs that this device might |
| 172 | // have. We only have one IPConfig right now, and I hope to never have |
| 173 | // to support more, as sleffler indicates that associating IPConfigs |
| 174 | // with devices is wrong and due to be changed in flimflam anyhow. |
| 175 | string suffix = hardware_address_ + "_0"; |
| 176 | ipconfig_->Save(storage, suffix); |
| 177 | storage->SetString(id, kStorageIPConfigs, SerializeIPConfigs(suffix)); |
| 178 | } |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 179 | return true; |
| 180 | } |
| 181 | |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 182 | void Device::DestroyIPConfig() { |
| 183 | if (ipconfig_.get()) { |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 184 | // TODO(pstew): Instead we should do this in DestroyConnection(), which |
| 185 | // should have a facility at its disposal that returns all addresses |
| 186 | // assigned to the interface. |
Paul Stewart | c39f113 | 2011-06-22 12:02:28 -0700 | [diff] [blame] | 187 | RTNLHandler::GetInstance()->RemoveInterfaceAddress(interface_index_, |
| 188 | *ipconfig_); |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 189 | ipconfig_->ReleaseIP(); |
| 190 | ipconfig_ = NULL; |
| 191 | } |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 192 | DestroyConnection(); |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | bool Device::AcquireDHCPConfig() { |
| 196 | DestroyIPConfig(); |
Darin Petkov | 77cb681 | 2011-08-15 16:19:41 -0700 | [diff] [blame] | 197 | ipconfig_ = dhcp_provider_->CreateConfig(link_name_); |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 198 | ipconfig_->RegisterUpdateCallback( |
| 199 | NewCallback(this, &Device::IPConfigUpdatedCallback)); |
| 200 | return ipconfig_->RequestIP(); |
| 201 | } |
| 202 | |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 203 | void Device::HelpRegisterDerivedStrings(const string &name, |
Chris Masone | 889666b | 2011-07-03 12:58:50 -0700 | [diff] [blame] | 204 | Strings(Device::*get)(void), |
| 205 | bool(Device::*set)(const Strings&)) { |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 206 | store_.RegisterDerivedStrings( |
| 207 | name, |
| 208 | StringsAccessor(new CustomAccessor<Device, Strings>(this, get, set))); |
Chris Masone | 4e85161 | 2011-07-01 10:46:53 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 211 | void Device::IPConfigUpdatedCallback(const IPConfigRefPtr &ipconfig, |
| 212 | bool success) { |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 213 | VLOG(2) << __func__ << " " << " success: " << success; |
Paul Stewart | c39f113 | 2011-06-22 12:02:28 -0700 | [diff] [blame] | 214 | if (success) { |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 215 | CreateConnection(); |
| 216 | connection_->UpdateFromIPConfig(ipconfig); |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame^] | 217 | SetServiceState(Service::kStateConnected); |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 218 | } else { |
| 219 | // TODO(pstew): This logic gets more complex when multiple IPConfig types |
| 220 | // are run in parallel (e.g. DHCP and DHCP6) |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame^] | 221 | SetServiceState(Service::kStateDisconnected); |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 222 | DestroyConnection(); |
Paul Stewart | c39f113 | 2011-06-22 12:02:28 -0700 | [diff] [blame] | 223 | } |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 224 | } |
| 225 | |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 226 | void Device::CreateConnection() { |
| 227 | VLOG(2) << __func__; |
| 228 | if (!connection_.get()) { |
| 229 | connection_ = new Connection(interface_index_, link_name_); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | void Device::DestroyConnection() { |
| 234 | VLOG(2) << __func__; |
| 235 | connection_ = NULL; |
| 236 | } |
| 237 | |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame^] | 238 | void Device::SelectService(const ServiceRefPtr &service) { |
| 239 | VLOG(2) << __func__ << ": " << service->UniqueName(); |
| 240 | if (selected_service_.get() && |
| 241 | selected_service_->state() != Service::kStateFailure) { |
| 242 | selected_service_->SetState(Service::kStateIdle); |
| 243 | } |
| 244 | selected_service_ = service; |
| 245 | } |
| 246 | |
| 247 | void Device::SetServiceState(Service::ConnectState state) { |
| 248 | if (selected_service_.get()) { |
| 249 | selected_service_->SetState(state); |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | void Device::SetServiceFailure(Service::ConnectFailure failure_state) { |
| 254 | if (selected_service_.get()) { |
| 255 | selected_service_->SetFailure(failure_state); |
| 256 | } |
| 257 | } |
| 258 | |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 259 | string Device::SerializeIPConfigs(const string &suffix) { |
| 260 | return StringPrintf("%s:%s", suffix.c_str(), ipconfig_->type().c_str()); |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 261 | } |
| 262 | |
Chris Masone | 4e85161 | 2011-07-01 10:46:53 -0700 | [diff] [blame] | 263 | vector<string> Device::AvailableIPConfigs() { |
| 264 | string id = (ipconfig_.get() ? ipconfig_->GetRpcIdentifier() : string()); |
| 265 | return vector<string>(1, id); |
| 266 | } |
| 267 | |
| 268 | string Device::GetRpcConnectionIdentifier() { |
| 269 | return adaptor_->GetRpcConnectionIdentifier(); |
| 270 | } |
| 271 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 272 | } // namespace shill |