blob: 8d7851ced41782ff63088fe8d503228effe9698f [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
Darin Petkov3335b372011-08-22 11:05:32 -070071 // flimflam::kHomeProviderProperty: Registered in Cellular
Chris Masone4d42df82011-07-02 17:09:39 -070072 // flimflam::kImeiProperty: Registered in Cellular
73 // flimflam::kImsiProperty: Registered in Cellular
74 // flimflam::kManufacturerProperty: Registered in Cellular
75 // flimflam::kMdnProperty: Registered in Cellular
76 // flimflam::kMeidProperty: Registered in Cellular
77 // flimflam::kMinProperty: Registered in Cellular
78 // flimflam::kModelIDProperty: Registered in Cellular
79 // flimflam::kFirmwareRevisionProperty: Registered in Cellular
80 // flimflam::kHardwareRevisionProperty: Registered in Cellular
81 // flimflam::kPRLVersionProperty: Registered in Cellular
82 // flimflam::kSIMLockStatusProperty: Registered in Cellular
83 // flimflam::kFoundNetworksProperty: Registered in Cellular
Darin Petkove9d12e02011-07-27 15:09:37 -070084 // flimflam::kDBusConnectionProperty: Registered in Cellular
85 // flimflam::kDBusObjectProperty: Register in Cellular
Chris Masone4d42df82011-07-02 17:09:39 -070086
Chris Masoneb925cc82011-06-22 15:39:57 -070087 // TODO(cmasone): Chrome doesn't use this...does anyone?
Chris Masone27c4aa52011-07-02 13:10:14 -070088 // store_.RegisterConstString(flimflam::kInterfaceProperty, &link_name_);
89 HelpRegisterDerivedStrings(flimflam::kIPConfigsProperty,
90 &Device::AvailableIPConfigs,
91 NULL);
92 store_.RegisterConstString(flimflam::kNameProperty, &link_name_);
93 store_.RegisterBool(flimflam::kPoweredProperty, &powered_);
Chris Masoneb925cc82011-06-22 15:39:57 -070094 // TODO(cmasone): Chrome doesn't use this...does anyone?
Chris Masone27c4aa52011-07-02 13:10:14 -070095 // store_.RegisterConstBool(flimflam::kReconnectProperty, &reconnect_);
Chris Masoneb925cc82011-06-22 15:39:57 -070096
Chris Masone4e851612011-07-01 10:46:53 -070097 // TODO(cmasone): Figure out what shill concept maps to flimflam's "Network".
Chris Masoneb925cc82011-06-22 15:39:57 -070098 // known_properties_.push_back(flimflam::kNetworksProperty);
99
Chris Masone4d42df82011-07-02 17:09:39 -0700100 // flimflam::kScanningProperty: Registered in WiFi, Cellular
101 // flimflam::kScanIntervalProperty: Registered in WiFi, Cellular
102
103 // TODO(pstew): Initialize Interface monitor, so we can detect new interfaces
Paul Stewartb50f0b92011-05-16 16:31:42 -0700104 VLOG(2) << "Device " << link_name_ << " index " << interface_index;
Paul Stewart75897df2011-04-27 09:05:53 -0700105}
106
107Device::~Device() {
Paul Stewartb50f0b92011-05-16 16:31:42 -0700108 VLOG(2) << "Device " << link_name_ << " destroyed.";
Paul Stewart75897df2011-04-27 09:05:53 -0700109}
110
111void Device::Start() {
112 running_ = true;
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700113 VLOG(2) << "Device " << link_name_ << " starting.";
Chris Masone413a3192011-05-09 17:10:05 -0700114 adaptor_->UpdateEnabled();
Paul Stewart75897df2011-04-27 09:05:53 -0700115}
116
117void Device::Stop() {
118 running_ = false;
Chris Masone413a3192011-05-09 17:10:05 -0700119 adaptor_->UpdateEnabled();
Paul Stewart75897df2011-04-27 09:05:53 -0700120}
121
Darin Petkov6f9eaa32011-08-09 15:26:44 -0700122bool Device::TechnologyIs(const Technology type) const {
Darin Petkovafa6fc42011-06-21 16:21:08 -0700123 return false;
124}
125
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700126void Device::LinkEvent(unsigned flags, unsigned change) {
127 VLOG(2) << "Device " << link_name_ << " flags " << flags << " changed "
128 << change;
129}
130
131void Device::Scan() {
132 VLOG(2) << "Device " << link_name_ << " scan requested.";
133}
134
Chris Masone27c4aa52011-07-02 13:10:14 -0700135string Device::GetRpcIdentifier() {
136 return adaptor_->GetRpcIdentifier();
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700137}
138
Chris Masone5dec5f42011-07-22 14:07:55 -0700139string Device::GetStorageIdentifier() {
140 string id = GetRpcIdentifier();
141 ControlInterface::RpcIdToStorageId(&id);
Chris Masone626719f2011-08-18 16:58:48 -0700142 size_t needle = id.find('_');
143 LOG_IF(ERROR, needle == string::npos) << "No _ in storage id?!?!";
144 id.replace(id.begin() + needle + 1, id.end(), hardware_address_);
Chris Masone5dec5f42011-07-22 14:07:55 -0700145 return id;
146}
147
Chris Masone19e30402011-07-19 15:48:47 -0700148const string& Device::FriendlyName() const {
Chris Masone7df0c672011-07-15 10:24:54 -0700149 return link_name_;
Darin Petkovafa6fc42011-06-21 16:21:08 -0700150}
151
Chris Masone19e30402011-07-19 15:48:47 -0700152const string& Device::UniqueName() const {
153 return unique_id_;
154}
155
Chris Masone5dec5f42011-07-22 14:07:55 -0700156bool Device::Load(StoreInterface *storage) {
157 const string id = GetStorageIdentifier();
158 if (!storage->ContainsGroup(id)) {
159 LOG(WARNING) << "Device is not available in the persistent store: " << id;
160 return false;
161 }
162 storage->GetBool(id, kStoragePowered, &powered_);
163 // TODO(cmasone): What does it mean to load an IPConfig identifier??
164 return true;
165}
166
167bool Device::Save(StoreInterface *storage) {
168 const string id = GetStorageIdentifier();
169 storage->SetBool(id, kStoragePowered, powered_);
170 if (ipconfig_.get())
171 storage->SetString(id, kStorageIPConfigs, SerializeIPConfigsForStorage());
172 return true;
173}
174
Darin Petkovafa6fc42011-06-21 16:21:08 -0700175void Device::DestroyIPConfig() {
176 if (ipconfig_.get()) {
Paul Stewarte6132022011-08-16 09:11:02 -0700177 // TODO(pstew): Instead we should do this in DestroyConnection(), which
178 // should have a facility at its disposal that returns all addresses
179 // assigned to the interface.
Paul Stewartc39f1132011-06-22 12:02:28 -0700180 RTNLHandler::GetInstance()->RemoveInterfaceAddress(interface_index_,
181 *ipconfig_);
Darin Petkovafa6fc42011-06-21 16:21:08 -0700182 ipconfig_->ReleaseIP();
183 ipconfig_ = NULL;
184 }
Paul Stewarte6132022011-08-16 09:11:02 -0700185 DestroyConnection();
Darin Petkovafa6fc42011-06-21 16:21:08 -0700186}
187
188bool Device::AcquireDHCPConfig() {
189 DestroyIPConfig();
Darin Petkov77cb6812011-08-15 16:19:41 -0700190 ipconfig_ = dhcp_provider_->CreateConfig(link_name_);
Darin Petkovafa6fc42011-06-21 16:21:08 -0700191 ipconfig_->RegisterUpdateCallback(
192 NewCallback(this, &Device::IPConfigUpdatedCallback));
193 return ipconfig_->RequestIP();
194}
195
Chris Masone27c4aa52011-07-02 13:10:14 -0700196void Device::HelpRegisterDerivedStrings(const string &name,
Chris Masone889666b2011-07-03 12:58:50 -0700197 Strings(Device::*get)(void),
198 bool(Device::*set)(const Strings&)) {
Chris Masone27c4aa52011-07-02 13:10:14 -0700199 store_.RegisterDerivedStrings(
200 name,
201 StringsAccessor(new CustomAccessor<Device, Strings>(this, get, set)));
Chris Masone4e851612011-07-01 10:46:53 -0700202}
203
Chris Masone2b105542011-06-22 10:58:09 -0700204void Device::IPConfigUpdatedCallback(const IPConfigRefPtr &ipconfig,
205 bool success) {
Paul Stewarte6132022011-08-16 09:11:02 -0700206 VLOG(2) << __func__ << " " << " success: " << success;
Paul Stewartc39f1132011-06-22 12:02:28 -0700207 if (success) {
Paul Stewarte6132022011-08-16 09:11:02 -0700208 CreateConnection();
209 connection_->UpdateFromIPConfig(ipconfig);
210 } else {
211 // TODO(pstew): This logic gets more complex when multiple IPConfig types
212 // are run in parallel (e.g. DHCP and DHCP6)
213 DestroyConnection();
Paul Stewartc39f1132011-06-22 12:02:28 -0700214 }
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700215}
216
Paul Stewarte6132022011-08-16 09:11:02 -0700217void Device::CreateConnection() {
218 VLOG(2) << __func__;
219 if (!connection_.get()) {
220 connection_ = new Connection(interface_index_, link_name_);
221 }
222}
223
224void Device::DestroyConnection() {
225 VLOG(2) << __func__;
226 connection_ = NULL;
227}
228
Chris Masone5dec5f42011-07-22 14:07:55 -0700229string Device::SerializeIPConfigsForStorage() {
230 return StringPrintf("%s:%s",
231 ipconfig_->GetStorageIdentifier().c_str(),
232 ipconfig_->type().c_str());
233}
234
Chris Masone4e851612011-07-01 10:46:53 -0700235vector<string> Device::AvailableIPConfigs() {
236 string id = (ipconfig_.get() ? ipconfig_->GetRpcIdentifier() : string());
237 return vector<string>(1, id);
238}
239
240string Device::GetRpcConnectionIdentifier() {
241 return adaptor_->GetRpcConnectionIdentifier();
242}
243
Paul Stewart75897df2011-04-27 09:05:53 -0700244} // namespace shill