blob: 16bf12f13309248f96346bff663f5a3b6c092439 [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
Chris Masone2b105542011-06-22 10:58:09 -07005#include "shill/device.h"
6
Paul Stewart75897df2011-04-27 09:05:53 -07007#include <time.h>
Paul Stewart75897df2011-04-27 09:05:53 -07008#include <stdio.h>
Chris Masoneee929b72011-05-10 10:02:18 -07009
Paul Stewart75897df2011-04-27 09:05:53 -070010#include <string>
Chris Masone8fe2c7e2011-06-09 15:51:19 -070011#include <vector>
Paul Stewart75897df2011-04-27 09:05:53 -070012
Chris Masoneee929b72011-05-10 10:02:18 -070013#include <base/logging.h>
Chris Masone487b8bf2011-05-13 16:27:57 -070014#include <base/memory/ref_counted.h>
Chris Masone3bd3c8c2011-06-13 08:20:26 -070015#include <chromeos/dbus/service_constants.h>
Chris Masoneee929b72011-05-10 10:02:18 -070016
Paul Stewart75897df2011-04-27 09:05:53 -070017#include "shill/control_interface.h"
Chris Masoned7732e42011-05-20 11:08:56 -070018#include "shill/device_dbus_adaptor.h"
Chris Masone2b105542011-06-22 10:58:09 -070019#include "shill/dhcp_config.h"
Darin Petkovafa6fc42011-06-21 16:21:08 -070020#include "shill/dhcp_provider.h"
Chris Masone8fe2c7e2011-06-09 15:51:19 -070021#include "shill/error.h"
22#include "shill/manager.h"
Chris Masone95207da2011-06-29 16:50:49 -070023#include "shill/property_accessor.h"
Chris Masone2b105542011-06-22 10:58:09 -070024#include "shill/refptr_types.h"
Paul Stewartc39f1132011-06-22 12:02:28 -070025#include "shill/rtnl_handler.h"
Chris Masone2b105542011-06-22 10:58:09 -070026#include "shill/service.h"
Chris Masone0e1d1042011-05-09 18:07:03 -070027#include "shill/shill_event.h"
Paul Stewart75897df2011-04-27 09:05:53 -070028
Chris Masone8fe2c7e2011-06-09 15:51:19 -070029using std::string;
30using std::vector;
31
Paul Stewart75897df2011-04-27 09:05:53 -070032namespace shill {
33Device::Device(ControlInterface *control_interface,
Paul Stewartb50f0b92011-05-16 16:31:42 -070034 EventDispatcher *dispatcher,
Paul Stewartf1ce5d22011-05-19 13:10:20 -070035 Manager *manager,
Darin Petkovafa6fc42011-06-21 16:21:08 -070036 const string &link_name,
Paul Stewartb50f0b92011-05-16 16:31:42 -070037 int interface_index)
Chris Masone853b81b2011-06-24 14:11:41 -070038 : powered_(true),
Chris Masoneb925cc82011-06-22 15:39:57 -070039 reconnect_(true),
mukesh agrawalf60e4062011-05-27 13:13:41 -070040 interface_index_(interface_index),
41 running_(false),
Darin Petkovafa6fc42011-06-21 16:21:08 -070042 link_name_(link_name),
Chris Masone7df0c672011-07-15 10:24:54 -070043 manager_(manager),
mukesh agrawalf60e4062011-05-27 13:13:41 -070044 adaptor_(control_interface->CreateDeviceAdaptor(this)) {
Chris Masoneb925cc82011-06-22 15:39:57 -070045
Chris Masone27c4aa52011-07-02 13:10:14 -070046 store_.RegisterConstString(flimflam::kAddressProperty, &hardware_address_);
Chris Masone4d42df82011-07-02 17:09:39 -070047
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 Masone27c4aa52011-07-02 13:10:14 -070068 HelpRegisterDerivedString(flimflam::kDBusConnectionProperty,
69 &Device::GetRpcConnectionIdentifier,
70 NULL);
71 HelpRegisterDerivedString(flimflam::kDBusObjectProperty,
72 &Device::GetRpcIdentifier,
73 NULL);
Chris Masoneb925cc82011-06-22 15:39:57 -070074 // TODO(cmasone): Chrome doesn't use this...does anyone?
Chris Masone27c4aa52011-07-02 13:10:14 -070075 // 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 Masoneb925cc82011-06-22 15:39:57 -070081 // TODO(cmasone): Chrome doesn't use this...does anyone?
Chris Masone27c4aa52011-07-02 13:10:14 -070082 // store_.RegisterConstBool(flimflam::kReconnectProperty, &reconnect_);
Chris Masoneb925cc82011-06-22 15:39:57 -070083
Chris Masone4e851612011-07-01 10:46:53 -070084 // TODO(cmasone): Figure out what shill concept maps to flimflam's "Network".
Chris Masoneb925cc82011-06-22 15:39:57 -070085 // known_properties_.push_back(flimflam::kNetworksProperty);
86
Chris Masone4d42df82011-07-02 17:09:39 -070087 // 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 Stewartb50f0b92011-05-16 16:31:42 -070091 VLOG(2) << "Device " << link_name_ << " index " << interface_index;
Paul Stewart75897df2011-04-27 09:05:53 -070092}
93
94Device::~Device() {
Paul Stewartb50f0b92011-05-16 16:31:42 -070095 VLOG(2) << "Device " << link_name_ << " destroyed.";
Paul Stewart75897df2011-04-27 09:05:53 -070096}
97
98void Device::Start() {
99 running_ = true;
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700100 VLOG(2) << "Device " << link_name_ << " starting.";
Chris Masone413a3192011-05-09 17:10:05 -0700101 adaptor_->UpdateEnabled();
Paul Stewart75897df2011-04-27 09:05:53 -0700102}
103
104void Device::Stop() {
105 running_ = false;
Chris Masone413a3192011-05-09 17:10:05 -0700106 adaptor_->UpdateEnabled();
Paul Stewart75897df2011-04-27 09:05:53 -0700107}
108
Darin Petkovafa6fc42011-06-21 16:21:08 -0700109bool Device::TechnologyIs(const Technology type) {
110 return false;
111}
112
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700113void Device::LinkEvent(unsigned flags, unsigned change) {
114 VLOG(2) << "Device " << link_name_ << " flags " << flags << " changed "
115 << change;
116}
117
118void Device::Scan() {
119 VLOG(2) << "Device " << link_name_ << " scan requested.";
120}
121
Chris Masone27c4aa52011-07-02 13:10:14 -0700122string Device::GetRpcIdentifier() {
123 return adaptor_->GetRpcIdentifier();
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700124}
125
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700126const string& Device::UniqueName() const {
127 // TODO(pstew): link_name is only run-time unique and won't persist
Chris Masone7df0c672011-07-15 10:24:54 -0700128 return link_name_;
Darin Petkovafa6fc42011-06-21 16:21:08 -0700129}
130
131void Device::DestroyIPConfig() {
132 if (ipconfig_.get()) {
Paul Stewartc39f1132011-06-22 12:02:28 -0700133 RTNLHandler::GetInstance()->RemoveInterfaceAddress(interface_index_,
134 *ipconfig_);
Darin Petkovafa6fc42011-06-21 16:21:08 -0700135 ipconfig_->ReleaseIP();
136 ipconfig_ = NULL;
137 }
138}
139
140bool Device::AcquireDHCPConfig() {
141 DestroyIPConfig();
Chris Masone7df0c672011-07-15 10:24:54 -0700142 ipconfig_ = DHCPProvider::GetInstance()->CreateConfig(link_name_);
Darin Petkovafa6fc42011-06-21 16:21:08 -0700143 ipconfig_->RegisterUpdateCallback(
144 NewCallback(this, &Device::IPConfigUpdatedCallback));
145 return ipconfig_->RequestIP();
146}
147
Chris Masone27c4aa52011-07-02 13:10:14 -0700148void Device::HelpRegisterDerivedString(const string &name,
Chris Masone889666b2011-07-03 12:58:50 -0700149 string(Device::*get)(void),
150 bool(Device::*set)(const string&)) {
Chris Masone27c4aa52011-07-02 13:10:14 -0700151 store_.RegisterDerivedString(
152 name,
153 StringAccessor(new CustomAccessor<Device, string>(this, get, set)));
Chris Masone95207da2011-06-29 16:50:49 -0700154}
155
Chris Masone27c4aa52011-07-02 13:10:14 -0700156void Device::HelpRegisterDerivedStrings(const string &name,
Chris Masone889666b2011-07-03 12:58:50 -0700157 Strings(Device::*get)(void),
158 bool(Device::*set)(const Strings&)) {
Chris Masone27c4aa52011-07-02 13:10:14 -0700159 store_.RegisterDerivedStrings(
160 name,
161 StringsAccessor(new CustomAccessor<Device, Strings>(this, get, set)));
Chris Masone4e851612011-07-01 10:46:53 -0700162}
163
Chris Masone2b105542011-06-22 10:58:09 -0700164void Device::IPConfigUpdatedCallback(const IPConfigRefPtr &ipconfig,
165 bool success) {
Darin Petkovafa6fc42011-06-21 16:21:08 -0700166 // 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 Stewartc39f1132011-06-22 12:02:28 -0700169 if (success) {
170 RTNLHandler::GetInstance()->AddInterfaceAddress(interface_index_,
171 *ipconfig);
172 }
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700173}
174
Chris Masone4e851612011-07-01 10:46:53 -0700175vector<string> Device::AvailableIPConfigs() {
176 string id = (ipconfig_.get() ? ipconfig_->GetRpcIdentifier() : string());
177 return vector<string>(1, id);
178}
179
180string Device::GetRpcConnectionIdentifier() {
181 return adaptor_->GetRpcConnectionIdentifier();
182}
183
Paul Stewart75897df2011-04-27 09:05:53 -0700184} // namespace shill