blob: fed0824e7c5241c57b62196ad8063467970997ff [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 Masone19e30402011-07-19 15:48:47 -070043 unique_id_(link_name),
Chris Masone7df0c672011-07-15 10:24:54 -070044 manager_(manager),
mukesh agrawalf60e4062011-05-27 13:13:41 -070045 adaptor_(control_interface->CreateDeviceAdaptor(this)) {
Chris Masoneb925cc82011-06-22 15:39:57 -070046
Chris Masone27c4aa52011-07-02 13:10:14 -070047 store_.RegisterConstString(flimflam::kAddressProperty, &hardware_address_);
Chris Masone4d42df82011-07-02 17:09:39 -070048
49 // flimflam::kBgscanMethodProperty: Registered in WiFi
50 // flimflam::kBgscanShortIntervalProperty: Registered in WiFi
51 // flimflam::kBgscanSignalThresholdProperty: Registered in WiFi
52
53 // flimflam::kCellularAllowRoamingProperty: Registered in Cellular
54 // flimflam::kCarrierProperty: Registered in Cellular
55 // flimflam::kEsnProperty: Registered in Cellular
56 // flimflam::kImeiProperty: Registered in Cellular
57 // flimflam::kImsiProperty: Registered in Cellular
58 // flimflam::kManufacturerProperty: Registered in Cellular
59 // flimflam::kMdnProperty: Registered in Cellular
60 // flimflam::kMeidProperty: Registered in Cellular
61 // flimflam::kMinProperty: Registered in Cellular
62 // flimflam::kModelIDProperty: Registered in Cellular
63 // flimflam::kFirmwareRevisionProperty: Registered in Cellular
64 // flimflam::kHardwareRevisionProperty: Registered in Cellular
65 // flimflam::kPRLVersionProperty: Registered in Cellular
66 // flimflam::kSIMLockStatusProperty: Registered in Cellular
67 // flimflam::kFoundNetworksProperty: Registered in Cellular
Darin Petkove9d12e02011-07-27 15:09:37 -070068 // flimflam::kDBusConnectionProperty: Registered in Cellular
69 // flimflam::kDBusObjectProperty: Register in Cellular
Chris Masone4d42df82011-07-02 17:09:39 -070070
Chris Masoneb925cc82011-06-22 15:39:57 -070071 // TODO(cmasone): Chrome doesn't use this...does anyone?
Chris Masone27c4aa52011-07-02 13:10:14 -070072 // store_.RegisterConstString(flimflam::kInterfaceProperty, &link_name_);
73 HelpRegisterDerivedStrings(flimflam::kIPConfigsProperty,
74 &Device::AvailableIPConfigs,
75 NULL);
76 store_.RegisterConstString(flimflam::kNameProperty, &link_name_);
77 store_.RegisterBool(flimflam::kPoweredProperty, &powered_);
Chris Masoneb925cc82011-06-22 15:39:57 -070078 // TODO(cmasone): Chrome doesn't use this...does anyone?
Chris Masone27c4aa52011-07-02 13:10:14 -070079 // store_.RegisterConstBool(flimflam::kReconnectProperty, &reconnect_);
Chris Masoneb925cc82011-06-22 15:39:57 -070080
Chris Masone4e851612011-07-01 10:46:53 -070081 // TODO(cmasone): Figure out what shill concept maps to flimflam's "Network".
Chris Masoneb925cc82011-06-22 15:39:57 -070082 // known_properties_.push_back(flimflam::kNetworksProperty);
83
Chris Masone4d42df82011-07-02 17:09:39 -070084 // flimflam::kScanningProperty: Registered in WiFi, Cellular
85 // flimflam::kScanIntervalProperty: Registered in WiFi, Cellular
86
87 // TODO(pstew): Initialize Interface monitor, so we can detect new interfaces
Paul Stewartb50f0b92011-05-16 16:31:42 -070088 VLOG(2) << "Device " << link_name_ << " index " << interface_index;
Paul Stewart75897df2011-04-27 09:05:53 -070089}
90
91Device::~Device() {
Paul Stewartb50f0b92011-05-16 16:31:42 -070092 VLOG(2) << "Device " << link_name_ << " destroyed.";
Paul Stewart75897df2011-04-27 09:05:53 -070093}
94
95void Device::Start() {
96 running_ = true;
Paul Stewartf1ce5d22011-05-19 13:10:20 -070097 VLOG(2) << "Device " << link_name_ << " starting.";
Chris Masone413a3192011-05-09 17:10:05 -070098 adaptor_->UpdateEnabled();
Paul Stewart75897df2011-04-27 09:05:53 -070099}
100
101void Device::Stop() {
102 running_ = false;
Chris Masone413a3192011-05-09 17:10:05 -0700103 adaptor_->UpdateEnabled();
Paul Stewart75897df2011-04-27 09:05:53 -0700104}
105
Darin Petkovafa6fc42011-06-21 16:21:08 -0700106bool Device::TechnologyIs(const Technology type) {
107 return false;
108}
109
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700110void Device::LinkEvent(unsigned flags, unsigned change) {
111 VLOG(2) << "Device " << link_name_ << " flags " << flags << " changed "
112 << change;
113}
114
115void Device::Scan() {
116 VLOG(2) << "Device " << link_name_ << " scan requested.";
117}
118
Chris Masone27c4aa52011-07-02 13:10:14 -0700119string Device::GetRpcIdentifier() {
120 return adaptor_->GetRpcIdentifier();
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700121}
122
Chris Masone19e30402011-07-19 15:48:47 -0700123const string& Device::FriendlyName() const {
Chris Masone7df0c672011-07-15 10:24:54 -0700124 return link_name_;
Darin Petkovafa6fc42011-06-21 16:21:08 -0700125}
126
Chris Masone19e30402011-07-19 15:48:47 -0700127const string& Device::UniqueName() const {
128 return unique_id_;
129}
130
Darin Petkovafa6fc42011-06-21 16:21:08 -0700131void 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::HelpRegisterDerivedStrings(const string &name,
Chris Masone889666b2011-07-03 12:58:50 -0700149 Strings(Device::*get)(void),
150 bool(Device::*set)(const Strings&)) {
Chris Masone27c4aa52011-07-02 13:10:14 -0700151 store_.RegisterDerivedStrings(
152 name,
153 StringsAccessor(new CustomAccessor<Device, Strings>(this, get, set)));
Chris Masone4e851612011-07-01 10:46:53 -0700154}
155
Chris Masone2b105542011-06-22 10:58:09 -0700156void Device::IPConfigUpdatedCallback(const IPConfigRefPtr &ipconfig,
157 bool success) {
Darin Petkovafa6fc42011-06-21 16:21:08 -0700158 // TODO(petkov): Use DeviceInfo to configure IP, etc. -- maybe through
159 // ConfigIP? Also, maybe allow forwarding the callback to interested listeners
160 // (e.g., the Manager).
Paul Stewartc39f1132011-06-22 12:02:28 -0700161 if (success) {
162 RTNLHandler::GetInstance()->AddInterfaceAddress(interface_index_,
163 *ipconfig);
164 }
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700165}
166
Chris Masone4e851612011-07-01 10:46:53 -0700167vector<string> Device::AvailableIPConfigs() {
168 string id = (ipconfig_.get() ? ipconfig_->GetRpcIdentifier() : string());
169 return vector<string>(1, id);
170}
171
172string Device::GetRpcConnectionIdentifier() {
173 return adaptor_->GetRpcConnectionIdentifier();
174}
175
Paul Stewart75897df2011-04-27 09:05:53 -0700176} // namespace shill