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 | |
| 5 | #include <time.h> |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 6 | #include <stdio.h> |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 7 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 8 | #include <string> |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 9 | #include <vector> |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 10 | |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 11 | #include <base/logging.h> |
Chris Masone | 487b8bf | 2011-05-13 16:27:57 -0700 | [diff] [blame] | 12 | #include <base/memory/ref_counted.h> |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 13 | #include <base/stl_util-inl.h> |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 14 | #include <chromeos/dbus/service_constants.h> |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 15 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 16 | #include "shill/control_interface.h" |
| 17 | #include "shill/device.h" |
Chris Masone | d7732e4 | 2011-05-20 11:08:56 -0700 | [diff] [blame] | 18 | #include "shill/device_dbus_adaptor.h" |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 19 | #include "shill/dhcp_provider.h" |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 20 | #include "shill/error.h" |
| 21 | #include "shill/manager.h" |
Paul Stewart | c39f113 | 2011-06-22 12:02:28 -0700 | [diff] [blame] | 22 | #include "shill/rtnl_handler.h" |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 23 | #include "shill/shill_event.h" |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 24 | |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 25 | using std::string; |
| 26 | using std::vector; |
| 27 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 28 | namespace shill { |
| 29 | Device::Device(ControlInterface *control_interface, |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 30 | EventDispatcher *dispatcher, |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 31 | Manager *manager, |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 32 | const string &link_name, |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 33 | int interface_index) |
Chris Masone | 853b81b | 2011-06-24 14:11:41 -0700 | [diff] [blame^] | 34 | : powered_(true), |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 35 | reconnect_(true), |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 36 | manager_(manager), |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 37 | link_name_(link_name), |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 38 | adaptor_(control_interface->CreateDeviceAdaptor(this)), |
| 39 | interface_index_(interface_index), |
| 40 | running_(false) { |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 41 | |
| 42 | RegisterConstString(flimflam::kAddressProperty, &hardware_address_); |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 43 | // TODO(cmasone): Chrome doesn't use this...does anyone? |
| 44 | // RegisterConstString(flimflam::kInterfaceProperty, &link_name_); |
| 45 | RegisterConstString(flimflam::kNameProperty, &link_name_); |
| 46 | RegisterBool(flimflam::kPoweredProperty, &powered_); |
| 47 | // TODO(cmasone): Chrome doesn't use this...does anyone? |
| 48 | // RegisterConstBool(flimflam::kReconnectProperty, &reconnect_); |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 49 | |
| 50 | // TODO(cmasone): Add support for R/O properties that return DBus object paths |
| 51 | // known_properties_.push_back(flimflam::kDBusConnectionProperty); |
| 52 | // known_properties_.push_back(flimflam::kDBusObjectProperty); |
| 53 | // known_properties_.push_back(flimflam::kIPConfigsProperty); |
| 54 | // known_properties_.push_back(flimflam::kNetworksProperty); |
| 55 | |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 56 | // Initialize Interface monitor, so we can detect new interfaces |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 57 | VLOG(2) << "Device " << link_name_ << " index " << interface_index; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | Device::~Device() { |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 61 | VLOG(2) << "Device " << link_name_ << " destroyed."; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | void Device::Start() { |
| 65 | running_ = true; |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 66 | VLOG(2) << "Device " << link_name_ << " starting."; |
Chris Masone | 413a319 | 2011-05-09 17:10:05 -0700 | [diff] [blame] | 67 | adaptor_->UpdateEnabled(); |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | void Device::Stop() { |
| 71 | running_ = false; |
Chris Masone | 413a319 | 2011-05-09 17:10:05 -0700 | [diff] [blame] | 72 | adaptor_->UpdateEnabled(); |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 75 | bool Device::TechnologyIs(const Technology type) { |
| 76 | return false; |
| 77 | } |
| 78 | |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 79 | void Device::LinkEvent(unsigned flags, unsigned change) { |
| 80 | VLOG(2) << "Device " << link_name_ << " flags " << flags << " changed " |
| 81 | << change; |
| 82 | } |
| 83 | |
| 84 | void Device::Scan() { |
| 85 | VLOG(2) << "Device " << link_name_ << " scan requested."; |
| 86 | } |
| 87 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 88 | bool Device::SetBoolProperty(const string& name, bool value, Error *error) { |
| 89 | VLOG(2) << "Setting " << name << " as a bool."; |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 90 | bool set = (ContainsKey(bool_properties_, name) && |
| 91 | bool_properties_[name]->Set(value)); |
| 92 | if (!set && error) |
| 93 | error->Populate(Error::kInvalidArguments, name + " is not a R/W bool."); |
| 94 | return set; |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | bool Device::SetInt32Property(const std::string& name, |
| 98 | int32 value, |
| 99 | Error *error) { |
| 100 | VLOG(2) << "Setting " << name << " as an int32."; |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 101 | bool set = (ContainsKey(int32_properties_, name) && |
| 102 | int32_properties_[name]->Set(value)); |
| 103 | if (!set && error) |
| 104 | error->Populate(Error::kInvalidArguments, name + " is not a R/W int32."); |
| 105 | return set; |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 108 | bool Device::SetUint16Property(const std::string& name, |
| 109 | uint16 value, |
| 110 | Error *error) { |
| 111 | VLOG(2) << "Setting " << name << " as a uint16."; |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 112 | bool set = (ContainsKey(uint16_properties_, name) && |
| 113 | uint16_properties_[name]->Set(value)); |
| 114 | if (!set && error) |
| 115 | error->Populate(Error::kInvalidArguments, name + " is not a R/W uint16."); |
| 116 | return set; |
| 117 | } |
| 118 | |
| 119 | bool Device::SetStringProperty(const string& name, |
| 120 | const string& value, |
| 121 | Error *error) { |
| 122 | VLOG(2) << "Setting " << name << " as a string."; |
| 123 | bool set = (ContainsKey(string_properties_, name) && |
| 124 | string_properties_[name]->Set(value)); |
| 125 | if (!set && error) |
| 126 | error->Populate(Error::kInvalidArguments, name + " is not a R/W string."); |
| 127 | return set; |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 130 | const string& Device::UniqueName() const { |
| 131 | // 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] | 132 | return link_name(); |
| 133 | } |
| 134 | |
| 135 | void Device::DestroyIPConfig() { |
| 136 | if (ipconfig_.get()) { |
Paul Stewart | c39f113 | 2011-06-22 12:02:28 -0700 | [diff] [blame] | 137 | RTNLHandler::GetInstance()->RemoveInterfaceAddress(interface_index_, |
| 138 | *ipconfig_); |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 139 | ipconfig_->ReleaseIP(); |
| 140 | ipconfig_ = NULL; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | bool Device::AcquireDHCPConfig() { |
| 145 | DestroyIPConfig(); |
| 146 | ipconfig_ = DHCPProvider::GetInstance()->CreateConfig(link_name()); |
| 147 | ipconfig_->RegisterUpdateCallback( |
| 148 | NewCallback(this, &Device::IPConfigUpdatedCallback)); |
| 149 | return ipconfig_->RequestIP(); |
| 150 | } |
| 151 | |
| 152 | void Device::IPConfigUpdatedCallback(IPConfigRefPtr ipconfig, bool success) { |
| 153 | // TODO(petkov): Use DeviceInfo to configure IP, etc. -- maybe through |
| 154 | // ConfigIP? Also, maybe allow forwarding the callback to interested listeners |
| 155 | // (e.g., the Manager). |
Paul Stewart | c39f113 | 2011-06-22 12:02:28 -0700 | [diff] [blame] | 156 | if (success) { |
| 157 | RTNLHandler::GetInstance()->AddInterfaceAddress(interface_index_, |
| 158 | *ipconfig); |
| 159 | } |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 162 | } // namespace shill |