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