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 |
| 71 | // flimflam::kImeiProperty: Registered in Cellular |
| 72 | // flimflam::kImsiProperty: Registered in Cellular |
| 73 | // flimflam::kManufacturerProperty: Registered in Cellular |
| 74 | // flimflam::kMdnProperty: Registered in Cellular |
| 75 | // flimflam::kMeidProperty: Registered in Cellular |
| 76 | // flimflam::kMinProperty: Registered in Cellular |
| 77 | // flimflam::kModelIDProperty: Registered in Cellular |
| 78 | // flimflam::kFirmwareRevisionProperty: Registered in Cellular |
| 79 | // flimflam::kHardwareRevisionProperty: Registered in Cellular |
| 80 | // flimflam::kPRLVersionProperty: Registered in Cellular |
| 81 | // flimflam::kSIMLockStatusProperty: Registered in Cellular |
| 82 | // flimflam::kFoundNetworksProperty: Registered in Cellular |
Darin Petkov | e9d12e0 | 2011-07-27 15:09:37 -0700 | [diff] [blame] | 83 | // flimflam::kDBusConnectionProperty: Registered in Cellular |
| 84 | // flimflam::kDBusObjectProperty: Register in Cellular |
Chris Masone | 4d42df8 | 2011-07-02 17:09:39 -0700 | [diff] [blame] | 85 | |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 86 | // TODO(cmasone): Chrome doesn't use this...does anyone? |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 87 | // store_.RegisterConstString(flimflam::kInterfaceProperty, &link_name_); |
| 88 | HelpRegisterDerivedStrings(flimflam::kIPConfigsProperty, |
| 89 | &Device::AvailableIPConfigs, |
| 90 | NULL); |
| 91 | store_.RegisterConstString(flimflam::kNameProperty, &link_name_); |
| 92 | store_.RegisterBool(flimflam::kPoweredProperty, &powered_); |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 93 | // TODO(cmasone): Chrome doesn't use this...does anyone? |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 94 | // store_.RegisterConstBool(flimflam::kReconnectProperty, &reconnect_); |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 95 | |
Chris Masone | 4e85161 | 2011-07-01 10:46:53 -0700 | [diff] [blame] | 96 | // TODO(cmasone): Figure out what shill concept maps to flimflam's "Network". |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 97 | // known_properties_.push_back(flimflam::kNetworksProperty); |
| 98 | |
Chris Masone | 4d42df8 | 2011-07-02 17:09:39 -0700 | [diff] [blame] | 99 | // flimflam::kScanningProperty: Registered in WiFi, Cellular |
| 100 | // flimflam::kScanIntervalProperty: Registered in WiFi, Cellular |
| 101 | |
| 102 | // TODO(pstew): Initialize Interface monitor, so we can detect new interfaces |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 103 | VLOG(2) << "Device " << link_name_ << " index " << interface_index; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | Device::~Device() { |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 107 | VLOG(2) << "Device " << link_name_ << " destroyed."; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | void Device::Start() { |
| 111 | running_ = true; |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 112 | VLOG(2) << "Device " << link_name_ << " starting."; |
Chris Masone | 413a319 | 2011-05-09 17:10:05 -0700 | [diff] [blame] | 113 | adaptor_->UpdateEnabled(); |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | void Device::Stop() { |
| 117 | running_ = false; |
Chris Masone | 413a319 | 2011-05-09 17:10:05 -0700 | [diff] [blame] | 118 | adaptor_->UpdateEnabled(); |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Darin Petkov | 6f9eaa3 | 2011-08-09 15:26:44 -0700 | [diff] [blame] | 121 | bool Device::TechnologyIs(const Technology type) const { |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 122 | return false; |
| 123 | } |
| 124 | |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 125 | void Device::LinkEvent(unsigned flags, unsigned change) { |
| 126 | VLOG(2) << "Device " << link_name_ << " flags " << flags << " changed " |
| 127 | << change; |
| 128 | } |
| 129 | |
| 130 | void Device::Scan() { |
| 131 | VLOG(2) << "Device " << link_name_ << " scan requested."; |
| 132 | } |
| 133 | |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 134 | string Device::GetRpcIdentifier() { |
| 135 | return adaptor_->GetRpcIdentifier(); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 138 | string Device::GetStorageIdentifier() { |
| 139 | string id = GetRpcIdentifier(); |
| 140 | ControlInterface::RpcIdToStorageId(&id); |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame^] | 141 | size_t needle = id.find('_'); |
| 142 | LOG_IF(ERROR, needle == string::npos) << "No _ in storage id?!?!"; |
| 143 | id.replace(id.begin() + needle + 1, id.end(), hardware_address_); |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 144 | return id; |
| 145 | } |
| 146 | |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 147 | const string& Device::FriendlyName() const { |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 148 | return link_name_; |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 151 | const string& Device::UniqueName() const { |
| 152 | return unique_id_; |
| 153 | } |
| 154 | |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 155 | bool Device::Load(StoreInterface *storage) { |
| 156 | const string id = GetStorageIdentifier(); |
| 157 | if (!storage->ContainsGroup(id)) { |
| 158 | LOG(WARNING) << "Device is not available in the persistent store: " << id; |
| 159 | return false; |
| 160 | } |
| 161 | storage->GetBool(id, kStoragePowered, &powered_); |
| 162 | // TODO(cmasone): What does it mean to load an IPConfig identifier?? |
| 163 | return true; |
| 164 | } |
| 165 | |
| 166 | bool Device::Save(StoreInterface *storage) { |
| 167 | const string id = GetStorageIdentifier(); |
| 168 | storage->SetBool(id, kStoragePowered, powered_); |
| 169 | if (ipconfig_.get()) |
| 170 | storage->SetString(id, kStorageIPConfigs, SerializeIPConfigsForStorage()); |
| 171 | return true; |
| 172 | } |
| 173 | |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 174 | void Device::DestroyIPConfig() { |
| 175 | if (ipconfig_.get()) { |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 176 | // TODO(pstew): Instead we should do this in DestroyConnection(), which |
| 177 | // should have a facility at its disposal that returns all addresses |
| 178 | // assigned to the interface. |
Paul Stewart | c39f113 | 2011-06-22 12:02:28 -0700 | [diff] [blame] | 179 | RTNLHandler::GetInstance()->RemoveInterfaceAddress(interface_index_, |
| 180 | *ipconfig_); |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 181 | ipconfig_->ReleaseIP(); |
| 182 | ipconfig_ = NULL; |
| 183 | } |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 184 | DestroyConnection(); |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | bool Device::AcquireDHCPConfig() { |
| 188 | DestroyIPConfig(); |
Darin Petkov | 77cb681 | 2011-08-15 16:19:41 -0700 | [diff] [blame] | 189 | ipconfig_ = dhcp_provider_->CreateConfig(link_name_); |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 190 | ipconfig_->RegisterUpdateCallback( |
| 191 | NewCallback(this, &Device::IPConfigUpdatedCallback)); |
| 192 | return ipconfig_->RequestIP(); |
| 193 | } |
| 194 | |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 195 | void Device::HelpRegisterDerivedStrings(const string &name, |
Chris Masone | 889666b | 2011-07-03 12:58:50 -0700 | [diff] [blame] | 196 | Strings(Device::*get)(void), |
| 197 | bool(Device::*set)(const Strings&)) { |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 198 | store_.RegisterDerivedStrings( |
| 199 | name, |
| 200 | StringsAccessor(new CustomAccessor<Device, Strings>(this, get, set))); |
Chris Masone | 4e85161 | 2011-07-01 10:46:53 -0700 | [diff] [blame] | 201 | } |
| 202 | |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 203 | void Device::IPConfigUpdatedCallback(const IPConfigRefPtr &ipconfig, |
| 204 | bool success) { |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 205 | VLOG(2) << __func__ << " " << " success: " << success; |
Paul Stewart | c39f113 | 2011-06-22 12:02:28 -0700 | [diff] [blame] | 206 | if (success) { |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 207 | CreateConnection(); |
| 208 | connection_->UpdateFromIPConfig(ipconfig); |
| 209 | } else { |
| 210 | // TODO(pstew): This logic gets more complex when multiple IPConfig types |
| 211 | // are run in parallel (e.g. DHCP and DHCP6) |
| 212 | DestroyConnection(); |
Paul Stewart | c39f113 | 2011-06-22 12:02:28 -0700 | [diff] [blame] | 213 | } |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 216 | void Device::CreateConnection() { |
| 217 | VLOG(2) << __func__; |
| 218 | if (!connection_.get()) { |
| 219 | connection_ = new Connection(interface_index_, link_name_); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | void Device::DestroyConnection() { |
| 224 | VLOG(2) << __func__; |
| 225 | connection_ = NULL; |
| 226 | } |
| 227 | |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 228 | string Device::SerializeIPConfigsForStorage() { |
| 229 | return StringPrintf("%s:%s", |
| 230 | ipconfig_->GetStorageIdentifier().c_str(), |
| 231 | ipconfig_->type().c_str()); |
| 232 | } |
| 233 | |
Chris Masone | 4e85161 | 2011-07-01 10:46:53 -0700 | [diff] [blame] | 234 | vector<string> Device::AvailableIPConfigs() { |
| 235 | string id = (ipconfig_.get() ? ipconfig_->GetRpcIdentifier() : string()); |
| 236 | return vector<string>(1, id); |
| 237 | } |
| 238 | |
| 239 | string Device::GetRpcConnectionIdentifier() { |
| 240 | return adaptor_->GetRpcConnectionIdentifier(); |
| 241 | } |
| 242 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 243 | } // namespace shill |