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