blob: 838f311e842ff2cdf1afadf3d7b261733b992e13 [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 Masone5dec5f42011-07-22 14:07:55 -070015#include <base/stringprintf.h>
Chris Masone3bd3c8c2011-06-13 08:20:26 -070016#include <chromeos/dbus/service_constants.h>
Chris Masoneee929b72011-05-10 10:02:18 -070017
Paul Stewarte6132022011-08-16 09:11:02 -070018#include "shill/connection.h"
Paul Stewart75897df2011-04-27 09:05:53 -070019#include "shill/control_interface.h"
Chris Masoned7732e42011-05-20 11:08:56 -070020#include "shill/device_dbus_adaptor.h"
Chris Masone2b105542011-06-22 10:58:09 -070021#include "shill/dhcp_config.h"
Darin Petkovafa6fc42011-06-21 16:21:08 -070022#include "shill/dhcp_provider.h"
Chris Masone8fe2c7e2011-06-09 15:51:19 -070023#include "shill/error.h"
24#include "shill/manager.h"
Chris Masone95207da2011-06-29 16:50:49 -070025#include "shill/property_accessor.h"
Chris Masone2b105542011-06-22 10:58:09 -070026#include "shill/refptr_types.h"
Paul Stewartc39f1132011-06-22 12:02:28 -070027#include "shill/rtnl_handler.h"
Chris Masone2b105542011-06-22 10:58:09 -070028#include "shill/service.h"
Chris Masone0e1d1042011-05-09 18:07:03 -070029#include "shill/shill_event.h"
Chris Masone5dec5f42011-07-22 14:07:55 -070030#include "shill/store_interface.h"
Paul Stewart75897df2011-04-27 09:05:53 -070031
Chris Masone5dec5f42011-07-22 14:07:55 -070032using base::StringPrintf;
Chris Masone8fe2c7e2011-06-09 15:51:19 -070033using std::string;
34using std::vector;
35
Paul Stewart75897df2011-04-27 09:05:53 -070036namespace shill {
Chris Masone5dec5f42011-07-22 14:07:55 -070037
38// static
39const char Device::kStoragePowered[] = "Powered";
40
41// static
42const char Device::kStorageIPConfigs[] = "IPConfigs";
43
Paul Stewart75897df2011-04-27 09:05:53 -070044Device::Device(ControlInterface *control_interface,
Paul Stewartb50f0b92011-05-16 16:31:42 -070045 EventDispatcher *dispatcher,
Paul Stewartf1ce5d22011-05-19 13:10:20 -070046 Manager *manager,
Darin Petkovafa6fc42011-06-21 16:21:08 -070047 const string &link_name,
Chris Masone626719f2011-08-18 16:58:48 -070048 const string &address,
Paul Stewartb50f0b92011-05-16 16:31:42 -070049 int interface_index)
Chris Masone853b81b2011-06-24 14:11:41 -070050 : powered_(true),
Chris Masoneb925cc82011-06-22 15:39:57 -070051 reconnect_(true),
Chris Masone626719f2011-08-18 16:58:48 -070052 hardware_address_(address),
mukesh agrawalf60e4062011-05-27 13:13:41 -070053 interface_index_(interface_index),
54 running_(false),
Darin Petkovafa6fc42011-06-21 16:21:08 -070055 link_name_(link_name),
Chris Masone19e30402011-07-19 15:48:47 -070056 unique_id_(link_name),
Darin Petkovd9661952011-08-03 16:25:42 -070057 control_interface_(control_interface),
58 dispatcher_(dispatcher),
Chris Masone7df0c672011-07-15 10:24:54 -070059 manager_(manager),
Darin Petkov77cb6812011-08-15 16:19:41 -070060 adaptor_(control_interface->CreateDeviceAdaptor(this)),
61 dhcp_provider_(DHCPProvider::GetInstance()) {
Chris Masone27c4aa52011-07-02 13:10:14 -070062 store_.RegisterConstString(flimflam::kAddressProperty, &hardware_address_);
Chris Masone4d42df82011-07-02 17:09:39 -070063
64 // flimflam::kBgscanMethodProperty: Registered in WiFi
65 // flimflam::kBgscanShortIntervalProperty: Registered in WiFi
66 // flimflam::kBgscanSignalThresholdProperty: Registered in WiFi
67
68 // flimflam::kCellularAllowRoamingProperty: Registered in Cellular
69 // flimflam::kCarrierProperty: Registered in Cellular
70 // flimflam::kEsnProperty: Registered in Cellular
71 // flimflam::kImeiProperty: Registered in Cellular
72 // flimflam::kImsiProperty: Registered in Cellular
73 // flimflam::kManufacturerProperty: Registered in Cellular
74 // flimflam::kMdnProperty: Registered in Cellular
75 // flimflam::kMeidProperty: Registered in Cellular
76 // flimflam::kMinProperty: Registered in Cellular
77 // flimflam::kModelIDProperty: Registered in Cellular
78 // flimflam::kFirmwareRevisionProperty: Registered in Cellular
79 // flimflam::kHardwareRevisionProperty: Registered in Cellular
80 // flimflam::kPRLVersionProperty: Registered in Cellular
81 // flimflam::kSIMLockStatusProperty: Registered in Cellular
82 // flimflam::kFoundNetworksProperty: Registered in Cellular
Darin Petkove9d12e02011-07-27 15:09:37 -070083 // flimflam::kDBusConnectionProperty: Registered in Cellular
84 // flimflam::kDBusObjectProperty: Register in Cellular
Chris Masone4d42df82011-07-02 17:09:39 -070085
Chris Masoneb925cc82011-06-22 15:39:57 -070086 // TODO(cmasone): Chrome doesn't use this...does anyone?
Chris Masone27c4aa52011-07-02 13:10:14 -070087 // store_.RegisterConstString(flimflam::kInterfaceProperty, &link_name_);
88 HelpRegisterDerivedStrings(flimflam::kIPConfigsProperty,
89 &Device::AvailableIPConfigs,
90 NULL);
91 store_.RegisterConstString(flimflam::kNameProperty, &link_name_);
92 store_.RegisterBool(flimflam::kPoweredProperty, &powered_);
Chris Masoneb925cc82011-06-22 15:39:57 -070093 // TODO(cmasone): Chrome doesn't use this...does anyone?
Chris Masone27c4aa52011-07-02 13:10:14 -070094 // store_.RegisterConstBool(flimflam::kReconnectProperty, &reconnect_);
Chris Masoneb925cc82011-06-22 15:39:57 -070095
Chris Masone4e851612011-07-01 10:46:53 -070096 // TODO(cmasone): Figure out what shill concept maps to flimflam's "Network".
Chris Masoneb925cc82011-06-22 15:39:57 -070097 // known_properties_.push_back(flimflam::kNetworksProperty);
98
Chris Masone4d42df82011-07-02 17:09:39 -070099 // flimflam::kScanningProperty: Registered in WiFi, Cellular
100 // flimflam::kScanIntervalProperty: Registered in WiFi, Cellular
101
102 // TODO(pstew): Initialize Interface monitor, so we can detect new interfaces
Paul Stewartb50f0b92011-05-16 16:31:42 -0700103 VLOG(2) << "Device " << link_name_ << " index " << interface_index;
Paul Stewart75897df2011-04-27 09:05:53 -0700104}
105
106Device::~Device() {
Paul Stewartb50f0b92011-05-16 16:31:42 -0700107 VLOG(2) << "Device " << link_name_ << " destroyed.";
Paul Stewart75897df2011-04-27 09:05:53 -0700108}
109
110void Device::Start() {
111 running_ = true;
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700112 VLOG(2) << "Device " << link_name_ << " starting.";
Chris Masone413a3192011-05-09 17:10:05 -0700113 adaptor_->UpdateEnabled();
Paul Stewart75897df2011-04-27 09:05:53 -0700114}
115
116void Device::Stop() {
117 running_ = false;
Chris Masone413a3192011-05-09 17:10:05 -0700118 adaptor_->UpdateEnabled();
Paul Stewart75897df2011-04-27 09:05:53 -0700119}
120
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700121bool Device::TechnologyIs(const Technology type) const {
Darin Petkovafa6fc42011-06-21 16:21:08 -0700122 return false;
123}
124
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700125void Device::LinkEvent(unsigned flags, unsigned change) {
126 VLOG(2) << "Device " << link_name_ << " flags " << flags << " changed "
127 << change;
128}
129
130void Device::Scan() {
131 VLOG(2) << "Device " << link_name_ << " scan requested.";
132}
133
Chris Masone27c4aa52011-07-02 13:10:14 -0700134string Device::GetRpcIdentifier() {
135 return adaptor_->GetRpcIdentifier();
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700136}
137
Chris Masone5dec5f42011-07-22 14:07:55 -0700138string Device::GetStorageIdentifier() {
139 string id = GetRpcIdentifier();
140 ControlInterface::RpcIdToStorageId(&id);
Chris Masone626719f2011-08-18 16:58:48 -0700141 size_t needle = id.find('_');
142 LOG_IF(ERROR, needle == string::npos) << "No _ in storage id?!?!";
143 id.replace(id.begin() + needle + 1, id.end(), hardware_address_);
Chris Masone5dec5f42011-07-22 14:07:55 -0700144 return id;
145}
146
Chris Masone19e30402011-07-19 15:48:47 -0700147const string& Device::FriendlyName() const {
Chris Masone7df0c672011-07-15 10:24:54 -0700148 return link_name_;
Darin Petkovafa6fc42011-06-21 16:21:08 -0700149}
150
Chris Masone19e30402011-07-19 15:48:47 -0700151const string& Device::UniqueName() const {
152 return unique_id_;
153}
154
Chris Masone5dec5f42011-07-22 14:07:55 -0700155bool Device::Load(StoreInterface *storage) {
156 const string id = GetStorageIdentifier();
157 if (!storage->ContainsGroup(id)) {
158 LOG(WARNING) << "Device is not available in the persistent store: " << id;
159 return false;
160 }
161 storage->GetBool(id, kStoragePowered, &powered_);
162 // TODO(cmasone): What does it mean to load an IPConfig identifier??
163 return true;
164}
165
166bool Device::Save(StoreInterface *storage) {
167 const string id = GetStorageIdentifier();
168 storage->SetBool(id, kStoragePowered, powered_);
169 if (ipconfig_.get())
170 storage->SetString(id, kStorageIPConfigs, SerializeIPConfigsForStorage());
171 return true;
172}
173
Darin Petkovafa6fc42011-06-21 16:21:08 -0700174void Device::DestroyIPConfig() {
175 if (ipconfig_.get()) {
Paul Stewarte6132022011-08-16 09:11:02 -0700176 // TODO(pstew): Instead we should do this in DestroyConnection(), which
177 // should have a facility at its disposal that returns all addresses
178 // assigned to the interface.
Paul Stewartc39f1132011-06-22 12:02:28 -0700179 RTNLHandler::GetInstance()->RemoveInterfaceAddress(interface_index_,
180 *ipconfig_);
Darin Petkovafa6fc42011-06-21 16:21:08 -0700181 ipconfig_->ReleaseIP();
182 ipconfig_ = NULL;
183 }
Paul Stewarte6132022011-08-16 09:11:02 -0700184 DestroyConnection();
Darin Petkovafa6fc42011-06-21 16:21:08 -0700185}
186
187bool Device::AcquireDHCPConfig() {
188 DestroyIPConfig();
Darin Petkov77cb6812011-08-15 16:19:41 -0700189 ipconfig_ = dhcp_provider_->CreateConfig(link_name_);
Darin Petkovafa6fc42011-06-21 16:21:08 -0700190 ipconfig_->RegisterUpdateCallback(
191 NewCallback(this, &Device::IPConfigUpdatedCallback));
192 return ipconfig_->RequestIP();
193}
194
Chris Masone27c4aa52011-07-02 13:10:14 -0700195void Device::HelpRegisterDerivedStrings(const string &name,
Chris Masone889666b2011-07-03 12:58:50 -0700196 Strings(Device::*get)(void),
197 bool(Device::*set)(const Strings&)) {
Chris Masone27c4aa52011-07-02 13:10:14 -0700198 store_.RegisterDerivedStrings(
199 name,
200 StringsAccessor(new CustomAccessor<Device, Strings>(this, get, set)));
Chris Masone4e851612011-07-01 10:46:53 -0700201}
202
Chris Masone2b105542011-06-22 10:58:09 -0700203void Device::IPConfigUpdatedCallback(const IPConfigRefPtr &ipconfig,
204 bool success) {
Paul Stewarte6132022011-08-16 09:11:02 -0700205 VLOG(2) << __func__ << " " << " success: " << success;
Paul Stewartc39f1132011-06-22 12:02:28 -0700206 if (success) {
Paul Stewarte6132022011-08-16 09:11:02 -0700207 CreateConnection();
208 connection_->UpdateFromIPConfig(ipconfig);
209 } else {
210 // TODO(pstew): This logic gets more complex when multiple IPConfig types
211 // are run in parallel (e.g. DHCP and DHCP6)
212 DestroyConnection();
Paul Stewartc39f1132011-06-22 12:02:28 -0700213 }
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700214}
215
Paul Stewarte6132022011-08-16 09:11:02 -0700216void Device::CreateConnection() {
217 VLOG(2) << __func__;
218 if (!connection_.get()) {
219 connection_ = new Connection(interface_index_, link_name_);
220 }
221}
222
223void Device::DestroyConnection() {
224 VLOG(2) << __func__;
225 connection_ = NULL;
226}
227
Chris Masone5dec5f42011-07-22 14:07:55 -0700228string Device::SerializeIPConfigsForStorage() {
229 return StringPrintf("%s:%s",
230 ipconfig_->GetStorageIdentifier().c_str(),
231 ipconfig_->type().c_str());
232}
233
Chris Masone4e851612011-07-01 10:46:53 -0700234vector<string> Device::AvailableIPConfigs() {
235 string id = (ipconfig_.get() ? ipconfig_->GetRpcIdentifier() : string());
236 return vector<string>(1, id);
237}
238
239string Device::GetRpcConnectionIdentifier() {
240 return adaptor_->GetRpcConnectionIdentifier();
241}
242
Paul Stewart75897df2011-04-27 09:05:53 -0700243} // namespace shill