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 | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 18 | #include "shill/control_interface.h" |
Chris Masone | d7732e4 | 2011-05-20 11:08:56 -0700 | [diff] [blame] | 19 | #include "shill/device_dbus_adaptor.h" |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 20 | #include "shill/dhcp_config.h" |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 21 | #include "shill/dhcp_provider.h" |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 22 | #include "shill/error.h" |
| 23 | #include "shill/manager.h" |
Chris Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame] | 24 | #include "shill/property_accessor.h" |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 25 | #include "shill/refptr_types.h" |
Paul Stewart | c39f113 | 2011-06-22 12:02:28 -0700 | [diff] [blame] | 26 | #include "shill/rtnl_handler.h" |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 27 | #include "shill/service.h" |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 28 | #include "shill/shill_event.h" |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 29 | #include "shill/store_interface.h" |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 30 | |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 31 | using base::StringPrintf; |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 32 | using std::string; |
| 33 | using std::vector; |
| 34 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 35 | namespace shill { |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 36 | |
| 37 | // static |
| 38 | const char Device::kStoragePowered[] = "Powered"; |
| 39 | |
| 40 | // static |
| 41 | const char Device::kStorageIPConfigs[] = "IPConfigs"; |
| 42 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 43 | Device::Device(ControlInterface *control_interface, |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 44 | EventDispatcher *dispatcher, |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 45 | Manager *manager, |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 46 | const string &link_name, |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 47 | int interface_index) |
Chris Masone | 853b81b | 2011-06-24 14:11:41 -0700 | [diff] [blame] | 48 | : powered_(true), |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 49 | reconnect_(true), |
mukesh agrawal | f60e406 | 2011-05-27 13:13:41 -0700 | [diff] [blame] | 50 | interface_index_(interface_index), |
| 51 | running_(false), |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 52 | link_name_(link_name), |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 53 | unique_id_(link_name), |
Darin Petkov | d966195 | 2011-08-03 16:25:42 -0700 | [diff] [blame] | 54 | control_interface_(control_interface), |
| 55 | dispatcher_(dispatcher), |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 56 | manager_(manager), |
mukesh agrawal | f60e406 | 2011-05-27 13:13:41 -0700 | [diff] [blame] | 57 | adaptor_(control_interface->CreateDeviceAdaptor(this)) { |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 58 | |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 59 | store_.RegisterConstString(flimflam::kAddressProperty, &hardware_address_); |
Chris Masone | 4d42df8 | 2011-07-02 17:09:39 -0700 | [diff] [blame] | 60 | |
| 61 | // flimflam::kBgscanMethodProperty: Registered in WiFi |
| 62 | // flimflam::kBgscanShortIntervalProperty: Registered in WiFi |
| 63 | // flimflam::kBgscanSignalThresholdProperty: Registered in WiFi |
| 64 | |
| 65 | // flimflam::kCellularAllowRoamingProperty: Registered in Cellular |
| 66 | // flimflam::kCarrierProperty: Registered in Cellular |
| 67 | // flimflam::kEsnProperty: Registered in Cellular |
| 68 | // flimflam::kImeiProperty: Registered in Cellular |
| 69 | // flimflam::kImsiProperty: Registered in Cellular |
| 70 | // flimflam::kManufacturerProperty: Registered in Cellular |
| 71 | // flimflam::kMdnProperty: Registered in Cellular |
| 72 | // flimflam::kMeidProperty: Registered in Cellular |
| 73 | // flimflam::kMinProperty: Registered in Cellular |
| 74 | // flimflam::kModelIDProperty: Registered in Cellular |
| 75 | // flimflam::kFirmwareRevisionProperty: Registered in Cellular |
| 76 | // flimflam::kHardwareRevisionProperty: Registered in Cellular |
| 77 | // flimflam::kPRLVersionProperty: Registered in Cellular |
| 78 | // flimflam::kSIMLockStatusProperty: Registered in Cellular |
| 79 | // flimflam::kFoundNetworksProperty: Registered in Cellular |
Darin Petkov | e9d12e0 | 2011-07-27 15:09:37 -0700 | [diff] [blame] | 80 | // flimflam::kDBusConnectionProperty: Registered in Cellular |
| 81 | // flimflam::kDBusObjectProperty: Register in Cellular |
Chris Masone | 4d42df8 | 2011-07-02 17:09:39 -0700 | [diff] [blame] | 82 | |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 83 | // TODO(cmasone): Chrome doesn't use this...does anyone? |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 84 | // store_.RegisterConstString(flimflam::kInterfaceProperty, &link_name_); |
| 85 | HelpRegisterDerivedStrings(flimflam::kIPConfigsProperty, |
| 86 | &Device::AvailableIPConfigs, |
| 87 | NULL); |
| 88 | store_.RegisterConstString(flimflam::kNameProperty, &link_name_); |
| 89 | store_.RegisterBool(flimflam::kPoweredProperty, &powered_); |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 90 | // TODO(cmasone): Chrome doesn't use this...does anyone? |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 91 | // store_.RegisterConstBool(flimflam::kReconnectProperty, &reconnect_); |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 92 | |
Chris Masone | 4e85161 | 2011-07-01 10:46:53 -0700 | [diff] [blame] | 93 | // TODO(cmasone): Figure out what shill concept maps to flimflam's "Network". |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 94 | // known_properties_.push_back(flimflam::kNetworksProperty); |
| 95 | |
Chris Masone | 4d42df8 | 2011-07-02 17:09:39 -0700 | [diff] [blame] | 96 | // flimflam::kScanningProperty: Registered in WiFi, Cellular |
| 97 | // flimflam::kScanIntervalProperty: Registered in WiFi, Cellular |
| 98 | |
| 99 | // TODO(pstew): Initialize Interface monitor, so we can detect new interfaces |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 100 | VLOG(2) << "Device " << link_name_ << " index " << interface_index; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | Device::~Device() { |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 104 | VLOG(2) << "Device " << link_name_ << " destroyed."; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | void Device::Start() { |
| 108 | running_ = true; |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 109 | VLOG(2) << "Device " << link_name_ << " starting."; |
Chris Masone | 413a319 | 2011-05-09 17:10:05 -0700 | [diff] [blame] | 110 | adaptor_->UpdateEnabled(); |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | void Device::Stop() { |
| 114 | running_ = false; |
Chris Masone | 413a319 | 2011-05-09 17:10:05 -0700 | [diff] [blame] | 115 | adaptor_->UpdateEnabled(); |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 118 | bool Device::TechnologyIs(const Technology type) { |
| 119 | return false; |
| 120 | } |
| 121 | |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 122 | void Device::LinkEvent(unsigned flags, unsigned change) { |
| 123 | VLOG(2) << "Device " << link_name_ << " flags " << flags << " changed " |
| 124 | << change; |
| 125 | } |
| 126 | |
| 127 | void Device::Scan() { |
| 128 | VLOG(2) << "Device " << link_name_ << " scan requested."; |
| 129 | } |
| 130 | |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 131 | string Device::GetRpcIdentifier() { |
| 132 | return adaptor_->GetRpcIdentifier(); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 135 | string Device::GetStorageIdentifier() { |
| 136 | string id = GetRpcIdentifier(); |
| 137 | ControlInterface::RpcIdToStorageId(&id); |
| 138 | return id; |
| 139 | } |
| 140 | |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 141 | const string& Device::FriendlyName() const { |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 142 | return link_name_; |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 145 | const string& Device::UniqueName() const { |
| 146 | return unique_id_; |
| 147 | } |
| 148 | |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 149 | bool Device::Load(StoreInterface *storage) { |
| 150 | const string id = GetStorageIdentifier(); |
| 151 | if (!storage->ContainsGroup(id)) { |
| 152 | LOG(WARNING) << "Device is not available in the persistent store: " << id; |
| 153 | return false; |
| 154 | } |
| 155 | storage->GetBool(id, kStoragePowered, &powered_); |
| 156 | // TODO(cmasone): What does it mean to load an IPConfig identifier?? |
| 157 | return true; |
| 158 | } |
| 159 | |
| 160 | bool Device::Save(StoreInterface *storage) { |
| 161 | const string id = GetStorageIdentifier(); |
| 162 | storage->SetBool(id, kStoragePowered, powered_); |
| 163 | if (ipconfig_.get()) |
| 164 | storage->SetString(id, kStorageIPConfigs, SerializeIPConfigsForStorage()); |
| 165 | return true; |
| 166 | } |
| 167 | |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 168 | void Device::DestroyIPConfig() { |
| 169 | if (ipconfig_.get()) { |
Paul Stewart | c39f113 | 2011-06-22 12:02:28 -0700 | [diff] [blame] | 170 | RTNLHandler::GetInstance()->RemoveInterfaceAddress(interface_index_, |
| 171 | *ipconfig_); |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 172 | ipconfig_->ReleaseIP(); |
| 173 | ipconfig_ = NULL; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | bool Device::AcquireDHCPConfig() { |
| 178 | DestroyIPConfig(); |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 179 | ipconfig_ = DHCPProvider::GetInstance()->CreateConfig(link_name_); |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 180 | ipconfig_->RegisterUpdateCallback( |
| 181 | NewCallback(this, &Device::IPConfigUpdatedCallback)); |
| 182 | return ipconfig_->RequestIP(); |
| 183 | } |
| 184 | |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 185 | void Device::HelpRegisterDerivedStrings(const string &name, |
Chris Masone | 889666b | 2011-07-03 12:58:50 -0700 | [diff] [blame] | 186 | Strings(Device::*get)(void), |
| 187 | bool(Device::*set)(const Strings&)) { |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 188 | store_.RegisterDerivedStrings( |
| 189 | name, |
| 190 | StringsAccessor(new CustomAccessor<Device, Strings>(this, get, set))); |
Chris Masone | 4e85161 | 2011-07-01 10:46:53 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 193 | void Device::IPConfigUpdatedCallback(const IPConfigRefPtr &ipconfig, |
| 194 | bool success) { |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 195 | // TODO(petkov): Use DeviceInfo to configure IP, etc. -- maybe through |
| 196 | // ConfigIP? Also, maybe allow forwarding the callback to interested listeners |
| 197 | // (e.g., the Manager). |
Paul Stewart | c39f113 | 2011-06-22 12:02:28 -0700 | [diff] [blame] | 198 | if (success) { |
| 199 | RTNLHandler::GetInstance()->AddInterfaceAddress(interface_index_, |
| 200 | *ipconfig); |
| 201 | } |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 202 | } |
| 203 | |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 204 | string Device::SerializeIPConfigsForStorage() { |
| 205 | return StringPrintf("%s:%s", |
| 206 | ipconfig_->GetStorageIdentifier().c_str(), |
| 207 | ipconfig_->type().c_str()); |
| 208 | } |
| 209 | |
Chris Masone | 4e85161 | 2011-07-01 10:46:53 -0700 | [diff] [blame] | 210 | vector<string> Device::AvailableIPConfigs() { |
| 211 | string id = (ipconfig_.get() ? ipconfig_->GetRpcIdentifier() : string()); |
| 212 | return vector<string>(1, id); |
| 213 | } |
| 214 | |
| 215 | string Device::GetRpcConnectionIdentifier() { |
| 216 | return adaptor_->GetRpcConnectionIdentifier(); |
| 217 | } |
| 218 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 219 | } // namespace shill |