blob: 5437f1f82ad72bba2d1fd7fb42539796c4b59850 [file] [log] [blame]
Paul Stewart75897df2011-04-27 09:05:53 -07001// 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 Stewart75897df2011-04-27 09:05:53 -07006#include <stdio.h>
Chris Masoneee929b72011-05-10 10:02:18 -07007
Paul Stewart75897df2011-04-27 09:05:53 -07008#include <string>
Chris Masone8fe2c7e2011-06-09 15:51:19 -07009#include <vector>
Paul Stewart75897df2011-04-27 09:05:53 -070010
Chris Masoneee929b72011-05-10 10:02:18 -070011#include <base/logging.h>
Chris Masone487b8bf2011-05-13 16:27:57 -070012#include <base/memory/ref_counted.h>
Chris Masoneb925cc82011-06-22 15:39:57 -070013#include <base/stl_util-inl.h>
Chris Masone3bd3c8c2011-06-13 08:20:26 -070014#include <chromeos/dbus/service_constants.h>
Chris Masoneee929b72011-05-10 10:02:18 -070015
Paul Stewart75897df2011-04-27 09:05:53 -070016#include "shill/control_interface.h"
17#include "shill/device.h"
Chris Masoned7732e42011-05-20 11:08:56 -070018#include "shill/device_dbus_adaptor.h"
Darin Petkovafa6fc42011-06-21 16:21:08 -070019#include "shill/dhcp_provider.h"
Chris Masone8fe2c7e2011-06-09 15:51:19 -070020#include "shill/error.h"
21#include "shill/manager.h"
Paul Stewartc39f1132011-06-22 12:02:28 -070022#include "shill/rtnl_handler.h"
Chris Masone0e1d1042011-05-09 18:07:03 -070023#include "shill/shill_event.h"
Paul Stewart75897df2011-04-27 09:05:53 -070024
Chris Masone8fe2c7e2011-06-09 15:51:19 -070025using std::string;
26using std::vector;
27
Paul Stewart75897df2011-04-27 09:05:53 -070028namespace shill {
29Device::Device(ControlInterface *control_interface,
Paul Stewartb50f0b92011-05-16 16:31:42 -070030 EventDispatcher *dispatcher,
Paul Stewartf1ce5d22011-05-19 13:10:20 -070031 Manager *manager,
Darin Petkovafa6fc42011-06-21 16:21:08 -070032 const string &link_name,
Paul Stewartb50f0b92011-05-16 16:31:42 -070033 int interface_index)
Chris Masone853b81b2011-06-24 14:11:41 -070034 : powered_(true),
Chris Masoneb925cc82011-06-22 15:39:57 -070035 reconnect_(true),
Chris Masoneb925cc82011-06-22 15:39:57 -070036 manager_(manager),
Darin Petkovafa6fc42011-06-21 16:21:08 -070037 link_name_(link_name),
Paul Stewartf1ce5d22011-05-19 13:10:20 -070038 adaptor_(control_interface->CreateDeviceAdaptor(this)),
39 interface_index_(interface_index),
40 running_(false) {
Chris Masoneb925cc82011-06-22 15:39:57 -070041
42 RegisterConstString(flimflam::kAddressProperty, &hardware_address_);
Chris Masoneb925cc82011-06-22 15:39:57 -070043 // 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 Masoneb925cc82011-06-22 15:39:57 -070049
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 Masoneee929b72011-05-10 10:02:18 -070056 // Initialize Interface monitor, so we can detect new interfaces
Paul Stewartb50f0b92011-05-16 16:31:42 -070057 VLOG(2) << "Device " << link_name_ << " index " << interface_index;
Paul Stewart75897df2011-04-27 09:05:53 -070058}
59
60Device::~Device() {
Paul Stewartb50f0b92011-05-16 16:31:42 -070061 VLOG(2) << "Device " << link_name_ << " destroyed.";
Paul Stewart75897df2011-04-27 09:05:53 -070062}
63
64void Device::Start() {
65 running_ = true;
Paul Stewartf1ce5d22011-05-19 13:10:20 -070066 VLOG(2) << "Device " << link_name_ << " starting.";
Chris Masone413a3192011-05-09 17:10:05 -070067 adaptor_->UpdateEnabled();
Paul Stewart75897df2011-04-27 09:05:53 -070068}
69
70void Device::Stop() {
71 running_ = false;
Chris Masone413a3192011-05-09 17:10:05 -070072 adaptor_->UpdateEnabled();
Paul Stewart75897df2011-04-27 09:05:53 -070073}
74
Darin Petkovafa6fc42011-06-21 16:21:08 -070075bool Device::TechnologyIs(const Technology type) {
76 return false;
77}
78
Paul Stewartf1ce5d22011-05-19 13:10:20 -070079void Device::LinkEvent(unsigned flags, unsigned change) {
80 VLOG(2) << "Device " << link_name_ << " flags " << flags << " changed "
81 << change;
82}
83
84void Device::Scan() {
85 VLOG(2) << "Device " << link_name_ << " scan requested.";
86}
87
Chris Masone3bd3c8c2011-06-13 08:20:26 -070088bool Device::SetBoolProperty(const string& name, bool value, Error *error) {
89 VLOG(2) << "Setting " << name << " as a bool.";
Chris Masoneb925cc82011-06-22 15:39:57 -070090 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 Masone8fe2c7e2011-06-09 15:51:19 -070095}
96
97bool Device::SetInt32Property(const std::string& name,
98 int32 value,
99 Error *error) {
100 VLOG(2) << "Setting " << name << " as an int32.";
Chris Masoneb925cc82011-06-22 15:39:57 -0700101 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 Masone8fe2c7e2011-06-09 15:51:19 -0700106}
107
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700108bool Device::SetUint16Property(const std::string& name,
109 uint16 value,
110 Error *error) {
111 VLOG(2) << "Setting " << name << " as a uint16.";
Chris Masoneb925cc82011-06-22 15:39:57 -0700112 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
119bool 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 Masone8fe2c7e2011-06-09 15:51:19 -0700128}
129
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700130const string& Device::UniqueName() const {
131 // TODO(pstew): link_name is only run-time unique and won't persist
Darin Petkovafa6fc42011-06-21 16:21:08 -0700132 return link_name();
133}
134
135void Device::DestroyIPConfig() {
136 if (ipconfig_.get()) {
Paul Stewartc39f1132011-06-22 12:02:28 -0700137 RTNLHandler::GetInstance()->RemoveInterfaceAddress(interface_index_,
138 *ipconfig_);
Darin Petkovafa6fc42011-06-21 16:21:08 -0700139 ipconfig_->ReleaseIP();
140 ipconfig_ = NULL;
141 }
142}
143
144bool 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
152void 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 Stewartc39f1132011-06-22 12:02:28 -0700156 if (success) {
157 RTNLHandler::GetInstance()->AddInterfaceAddress(interface_index_,
158 *ipconfig);
159 }
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700160}
161
Paul Stewart75897df2011-04-27 09:05:53 -0700162} // namespace shill