blob: 7f72cdfba95f128eff77e9719c307f8d2bdf7f16 [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
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -07007#include <netinet/in.h>
8#include <linux/if.h> // Needs definitions from netinet/in.h
Paul Stewart75897df2011-04-27 09:05:53 -07009#include <stdio.h>
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -070010#include <time.h>
Chris Masoneee929b72011-05-10 10:02:18 -070011
Paul Stewart75897df2011-04-27 09:05:53 -070012#include <string>
Chris Masone8fe2c7e2011-06-09 15:51:19 -070013#include <vector>
Paul Stewart75897df2011-04-27 09:05:53 -070014
Chris Masoneee929b72011-05-10 10:02:18 -070015#include <base/logging.h>
Chris Masone487b8bf2011-05-13 16:27:57 -070016#include <base/memory/ref_counted.h>
Chris Masone5dec5f42011-07-22 14:07:55 -070017#include <base/stringprintf.h>
Chris Masone3bd3c8c2011-06-13 08:20:26 -070018#include <chromeos/dbus/service_constants.h>
Chris Masoneee929b72011-05-10 10:02:18 -070019
Paul Stewarte6132022011-08-16 09:11:02 -070020#include "shill/connection.h"
Paul Stewart75897df2011-04-27 09:05:53 -070021#include "shill/control_interface.h"
Chris Masoned7732e42011-05-20 11:08:56 -070022#include "shill/device_dbus_adaptor.h"
Chris Masone2b105542011-06-22 10:58:09 -070023#include "shill/dhcp_config.h"
Darin Petkovafa6fc42011-06-21 16:21:08 -070024#include "shill/dhcp_provider.h"
Chris Masone8fe2c7e2011-06-09 15:51:19 -070025#include "shill/error.h"
26#include "shill/manager.h"
Chris Masone95207da2011-06-29 16:50:49 -070027#include "shill/property_accessor.h"
Chris Masone2b105542011-06-22 10:58:09 -070028#include "shill/refptr_types.h"
Paul Stewartc39f1132011-06-22 12:02:28 -070029#include "shill/rtnl_handler.h"
Chris Masone2b105542011-06-22 10:58:09 -070030#include "shill/service.h"
Chris Masone0e1d1042011-05-09 18:07:03 -070031#include "shill/shill_event.h"
Chris Masone5dec5f42011-07-22 14:07:55 -070032#include "shill/store_interface.h"
Paul Stewart75897df2011-04-27 09:05:53 -070033
Chris Masone5dec5f42011-07-22 14:07:55 -070034using base::StringPrintf;
Chris Masone8fe2c7e2011-06-09 15:51:19 -070035using std::string;
36using std::vector;
37
Paul Stewart75897df2011-04-27 09:05:53 -070038namespace shill {
Chris Masone5dec5f42011-07-22 14:07:55 -070039
40// static
41const char Device::kStoragePowered[] = "Powered";
42
43// static
44const char Device::kStorageIPConfigs[] = "IPConfigs";
45
Paul Stewart75897df2011-04-27 09:05:53 -070046Device::Device(ControlInterface *control_interface,
Paul Stewartb50f0b92011-05-16 16:31:42 -070047 EventDispatcher *dispatcher,
Paul Stewartf1ce5d22011-05-19 13:10:20 -070048 Manager *manager,
Darin Petkovafa6fc42011-06-21 16:21:08 -070049 const string &link_name,
Chris Masone626719f2011-08-18 16:58:48 -070050 const string &address,
Paul Stewartb50f0b92011-05-16 16:31:42 -070051 int interface_index)
Chris Masone853b81b2011-06-24 14:11:41 -070052 : powered_(true),
Chris Masoneb925cc82011-06-22 15:39:57 -070053 reconnect_(true),
Chris Masone626719f2011-08-18 16:58:48 -070054 hardware_address_(address),
mukesh agrawalf60e4062011-05-27 13:13:41 -070055 interface_index_(interface_index),
56 running_(false),
Darin Petkovafa6fc42011-06-21 16:21:08 -070057 link_name_(link_name),
Chris Masone19e30402011-07-19 15:48:47 -070058 unique_id_(link_name),
Darin Petkovd9661952011-08-03 16:25:42 -070059 control_interface_(control_interface),
60 dispatcher_(dispatcher),
Chris Masone7df0c672011-07-15 10:24:54 -070061 manager_(manager),
Darin Petkov77cb6812011-08-15 16:19:41 -070062 adaptor_(control_interface->CreateDeviceAdaptor(this)),
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -070063 dhcp_provider_(DHCPProvider::GetInstance()),
64 rtnl_handler_(RTNLHandler::GetInstance()) {
Chris Masone27c4aa52011-07-02 13:10:14 -070065 store_.RegisterConstString(flimflam::kAddressProperty, &hardware_address_);
Chris Masone4d42df82011-07-02 17:09:39 -070066
67 // flimflam::kBgscanMethodProperty: Registered in WiFi
68 // flimflam::kBgscanShortIntervalProperty: Registered in WiFi
69 // flimflam::kBgscanSignalThresholdProperty: Registered in WiFi
70
71 // flimflam::kCellularAllowRoamingProperty: Registered in Cellular
72 // flimflam::kCarrierProperty: Registered in Cellular
73 // flimflam::kEsnProperty: Registered in Cellular
Darin Petkov3335b372011-08-22 11:05:32 -070074 // flimflam::kHomeProviderProperty: Registered in Cellular
Chris Masone4d42df82011-07-02 17:09:39 -070075 // flimflam::kImeiProperty: Registered in Cellular
76 // flimflam::kImsiProperty: Registered in Cellular
77 // flimflam::kManufacturerProperty: Registered in Cellular
78 // flimflam::kMdnProperty: Registered in Cellular
79 // flimflam::kMeidProperty: Registered in Cellular
80 // flimflam::kMinProperty: Registered in Cellular
81 // flimflam::kModelIDProperty: Registered in Cellular
82 // flimflam::kFirmwareRevisionProperty: Registered in Cellular
83 // flimflam::kHardwareRevisionProperty: Registered in Cellular
84 // flimflam::kPRLVersionProperty: Registered in Cellular
85 // flimflam::kSIMLockStatusProperty: Registered in Cellular
86 // flimflam::kFoundNetworksProperty: Registered in Cellular
Darin Petkove9d12e02011-07-27 15:09:37 -070087 // flimflam::kDBusConnectionProperty: Registered in Cellular
88 // flimflam::kDBusObjectProperty: Register in Cellular
Chris Masone4d42df82011-07-02 17:09:39 -070089
Chris Masoneb925cc82011-06-22 15:39:57 -070090 // TODO(cmasone): Chrome doesn't use this...does anyone?
Chris Masone27c4aa52011-07-02 13:10:14 -070091 // store_.RegisterConstString(flimflam::kInterfaceProperty, &link_name_);
92 HelpRegisterDerivedStrings(flimflam::kIPConfigsProperty,
93 &Device::AvailableIPConfigs,
94 NULL);
95 store_.RegisterConstString(flimflam::kNameProperty, &link_name_);
96 store_.RegisterBool(flimflam::kPoweredProperty, &powered_);
Chris Masoneb925cc82011-06-22 15:39:57 -070097 // TODO(cmasone): Chrome doesn't use this...does anyone?
Chris Masone27c4aa52011-07-02 13:10:14 -070098 // store_.RegisterConstBool(flimflam::kReconnectProperty, &reconnect_);
Chris Masoneb925cc82011-06-22 15:39:57 -070099
Chris Masone4e851612011-07-01 10:46:53 -0700100 // TODO(cmasone): Figure out what shill concept maps to flimflam's "Network".
Chris Masoneb925cc82011-06-22 15:39:57 -0700101 // known_properties_.push_back(flimflam::kNetworksProperty);
102
Chris Masone4d42df82011-07-02 17:09:39 -0700103 // flimflam::kScanningProperty: Registered in WiFi, Cellular
104 // flimflam::kScanIntervalProperty: Registered in WiFi, Cellular
105
106 // TODO(pstew): Initialize Interface monitor, so we can detect new interfaces
Paul Stewartb50f0b92011-05-16 16:31:42 -0700107 VLOG(2) << "Device " << link_name_ << " index " << interface_index;
Paul Stewart75897df2011-04-27 09:05:53 -0700108}
109
110Device::~Device() {
Paul Stewartb50f0b92011-05-16 16:31:42 -0700111 VLOG(2) << "Device " << link_name_ << " destroyed.";
Paul Stewart75897df2011-04-27 09:05:53 -0700112}
113
114void Device::Start() {
115 running_ = true;
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700116 VLOG(2) << "Device " << link_name_ << " starting.";
Chris Masone413a3192011-05-09 17:10:05 -0700117 adaptor_->UpdateEnabled();
Paul Stewart75897df2011-04-27 09:05:53 -0700118}
119
120void Device::Stop() {
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700121 VLOG(2) << "Device " << link_name_ << " stopping.";
Paul Stewart75897df2011-04-27 09:05:53 -0700122 running_ = false;
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700123 DestroyIPConfig(); // breaks a reference cycle
124 SelectService(NULL); // breaks a reference cycle
Chris Masone413a3192011-05-09 17:10:05 -0700125 adaptor_->UpdateEnabled();
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700126 rtnl_handler_->SetInterfaceFlags(interface_index(), 0, IFF_UP);
127
128 VLOG(3) << "Device " << link_name_ << " has " << services_.size()
129 << " remaining services.";
130 VLOG(3) << "Device " << link_name_ << " ipconfig_ "
131 << (ipconfig_.get() ? "is set." : "is not set.");
132 VLOG(3) << "Device " << link_name_ << " connection_ "
133 << (connection_.get() ? "is set." : "is not set.");
134 VLOG(3) << "Device " << link_name_ << " selected_service_ "
135 << (selected_service_.get() ? "is set." : "is not set.");
Paul Stewart75897df2011-04-27 09:05:53 -0700136}
137
Paul Stewartfdd16072011-09-16 12:41:35 -0700138bool Device::TechnologyIs(const Technology::Identifier type) const {
Darin Petkovafa6fc42011-06-21 16:21:08 -0700139 return false;
140}
141
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700142void Device::LinkEvent(unsigned flags, unsigned change) {
mukesh agrawal47009f82011-08-25 14:07:35 -0700143 VLOG(2) << "Device " << link_name_
144 << std::showbase << std::hex
145 << " flags " << flags << " changed " << change
146 << std::dec << std::noshowbase;
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700147}
148
Darin Petkovc0865312011-09-16 15:31:20 -0700149void Device::Scan(Error *error) {
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700150 VLOG(2) << "Device " << link_name_ << " scan requested.";
Darin Petkovc0865312011-09-16 15:31:20 -0700151 const string kMessage = "Device doesn't support scan.";
152 LOG(ERROR) << kMessage;
153 CHECK(error);
154 error->Populate(Error::kNotSupported, kMessage);
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700155}
156
Darin Petkov9ae310f2011-08-30 15:41:13 -0700157void Device::RegisterOnNetwork(const std::string &network_id, Error *error) {
158 const string kMessage = "Device doesn't support network registration.";
159 LOG(ERROR) << kMessage;
160 CHECK(error);
161 error->Populate(Error::kNotSupported, kMessage);
162}
163
Darin Petkove42e1012011-08-31 12:35:04 -0700164void Device::RequirePIN(const string &pin, bool require, Error *error) {
165 const string kMessage = "Device doesn't support RequirePIN.";
166 LOG(ERROR) << kMessage;
167 CHECK(error);
168 error->Populate(Error::kNotSupported, kMessage);
169}
170
171void Device::EnterPIN(const string &pin, Error *error) {
172 const string kMessage = "Device doesn't support EnterPIN.";
173 LOG(ERROR) << kMessage;
174 CHECK(error);
175 error->Populate(Error::kNotSupported, kMessage);
176}
177
178void Device::UnblockPIN(const string &unblock_code,
179 const string &pin,
180 Error *error) {
181 const string kMessage = "Device doesn't support UnblockPIN.";
182 LOG(ERROR) << kMessage;
183 CHECK(error);
184 error->Populate(Error::kNotSupported, kMessage);
185}
186
187void Device::ChangePIN(const string &old_pin,
188 const string &new_pin,
189 Error *error) {
190 const string kMessage = "Device doesn't support ChangePIN.";
191 LOG(ERROR) << kMessage;
192 CHECK(error);
193 error->Populate(Error::kNotSupported, kMessage);
194}
195
Chris Masone27c4aa52011-07-02 13:10:14 -0700196string Device::GetRpcIdentifier() {
197 return adaptor_->GetRpcIdentifier();
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700198}
199
Chris Masone5dec5f42011-07-22 14:07:55 -0700200string Device::GetStorageIdentifier() {
201 string id = GetRpcIdentifier();
202 ControlInterface::RpcIdToStorageId(&id);
Chris Masone626719f2011-08-18 16:58:48 -0700203 size_t needle = id.find('_');
Chris Masone34af2182011-08-22 11:59:36 -0700204 DLOG_IF(ERROR, needle == string::npos) << "No _ in storage id?!?!";
Chris Masone626719f2011-08-18 16:58:48 -0700205 id.replace(id.begin() + needle + 1, id.end(), hardware_address_);
Chris Masone5dec5f42011-07-22 14:07:55 -0700206 return id;
207}
208
Chris Masone19e30402011-07-19 15:48:47 -0700209const string& Device::FriendlyName() const {
Chris Masone7df0c672011-07-15 10:24:54 -0700210 return link_name_;
Darin Petkovafa6fc42011-06-21 16:21:08 -0700211}
212
Chris Masone19e30402011-07-19 15:48:47 -0700213const string& Device::UniqueName() const {
214 return unique_id_;
215}
216
Chris Masone5dec5f42011-07-22 14:07:55 -0700217bool Device::Load(StoreInterface *storage) {
218 const string id = GetStorageIdentifier();
219 if (!storage->ContainsGroup(id)) {
220 LOG(WARNING) << "Device is not available in the persistent store: " << id;
221 return false;
222 }
223 storage->GetBool(id, kStoragePowered, &powered_);
224 // TODO(cmasone): What does it mean to load an IPConfig identifier??
225 return true;
226}
227
228bool Device::Save(StoreInterface *storage) {
229 const string id = GetStorageIdentifier();
230 storage->SetBool(id, kStoragePowered, powered_);
Chris Masone34af2182011-08-22 11:59:36 -0700231 if (ipconfig_.get()) {
232 // The _0 is an index into the list of IPConfigs that this device might
233 // have. We only have one IPConfig right now, and I hope to never have
234 // to support more, as sleffler indicates that associating IPConfigs
235 // with devices is wrong and due to be changed in flimflam anyhow.
236 string suffix = hardware_address_ + "_0";
237 ipconfig_->Save(storage, suffix);
238 storage->SetString(id, kStorageIPConfigs, SerializeIPConfigs(suffix));
239 }
Chris Masone5dec5f42011-07-22 14:07:55 -0700240 return true;
241}
242
Darin Petkovafa6fc42011-06-21 16:21:08 -0700243void Device::DestroyIPConfig() {
244 if (ipconfig_.get()) {
245 ipconfig_->ReleaseIP();
246 ipconfig_ = NULL;
247 }
Paul Stewarte6132022011-08-16 09:11:02 -0700248 DestroyConnection();
Darin Petkovafa6fc42011-06-21 16:21:08 -0700249}
250
251bool Device::AcquireDHCPConfig() {
252 DestroyIPConfig();
Darin Petkov77cb6812011-08-15 16:19:41 -0700253 ipconfig_ = dhcp_provider_->CreateConfig(link_name_);
Darin Petkovafa6fc42011-06-21 16:21:08 -0700254 ipconfig_->RegisterUpdateCallback(
255 NewCallback(this, &Device::IPConfigUpdatedCallback));
256 return ipconfig_->RequestIP();
257}
258
Chris Masone27c4aa52011-07-02 13:10:14 -0700259void Device::HelpRegisterDerivedStrings(const string &name,
Chris Masone889666b2011-07-03 12:58:50 -0700260 Strings(Device::*get)(void),
261 bool(Device::*set)(const Strings&)) {
Chris Masone27c4aa52011-07-02 13:10:14 -0700262 store_.RegisterDerivedStrings(
263 name,
264 StringsAccessor(new CustomAccessor<Device, Strings>(this, get, set)));
Chris Masone4e851612011-07-01 10:46:53 -0700265}
266
Chris Masone2b105542011-06-22 10:58:09 -0700267void Device::IPConfigUpdatedCallback(const IPConfigRefPtr &ipconfig,
268 bool success) {
Paul Stewarte6132022011-08-16 09:11:02 -0700269 VLOG(2) << __func__ << " " << " success: " << success;
Paul Stewartc39f1132011-06-22 12:02:28 -0700270 if (success) {
Paul Stewarte6132022011-08-16 09:11:02 -0700271 CreateConnection();
272 connection_->UpdateFromIPConfig(ipconfig);
Paul Stewart03dba0b2011-08-22 16:32:45 -0700273 SetServiceState(Service::kStateConnected);
Paul Stewarte6132022011-08-16 09:11:02 -0700274 } else {
275 // TODO(pstew): This logic gets more complex when multiple IPConfig types
276 // are run in parallel (e.g. DHCP and DHCP6)
Paul Stewart03dba0b2011-08-22 16:32:45 -0700277 SetServiceState(Service::kStateDisconnected);
Paul Stewarte6132022011-08-16 09:11:02 -0700278 DestroyConnection();
Paul Stewartc39f1132011-06-22 12:02:28 -0700279 }
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700280}
281
Paul Stewarte6132022011-08-16 09:11:02 -0700282void Device::CreateConnection() {
283 VLOG(2) << __func__;
284 if (!connection_.get()) {
Paul Stewart9a908082011-08-31 12:18:48 -0700285 connection_ = new Connection(interface_index_,
286 link_name_,
287 manager_->device_info());
Paul Stewarte6132022011-08-16 09:11:02 -0700288 }
289}
290
291void Device::DestroyConnection() {
292 VLOG(2) << __func__;
293 connection_ = NULL;
294}
295
Paul Stewart03dba0b2011-08-22 16:32:45 -0700296void Device::SelectService(const ServiceRefPtr &service) {
Paul Stewarta2b8cd12011-08-25 14:55:32 -0700297 VLOG(2) << __func__ << ": "
298 << (service.get() ? service->UniqueName() : "*reset*");
Paul Stewart03dba0b2011-08-22 16:32:45 -0700299 if (selected_service_.get() &&
300 selected_service_->state() != Service::kStateFailure) {
301 selected_service_->SetState(Service::kStateIdle);
302 }
303 selected_service_ = service;
304}
305
306void Device::SetServiceState(Service::ConnectState state) {
307 if (selected_service_.get()) {
308 selected_service_->SetState(state);
309 }
310}
311
312void Device::SetServiceFailure(Service::ConnectFailure failure_state) {
313 if (selected_service_.get()) {
314 selected_service_->SetFailure(failure_state);
315 }
316}
317
Chris Masone34af2182011-08-22 11:59:36 -0700318string Device::SerializeIPConfigs(const string &suffix) {
319 return StringPrintf("%s:%s", suffix.c_str(), ipconfig_->type().c_str());
Chris Masone5dec5f42011-07-22 14:07:55 -0700320}
321
Chris Masone4e851612011-07-01 10:46:53 -0700322vector<string> Device::AvailableIPConfigs() {
323 string id = (ipconfig_.get() ? ipconfig_->GetRpcIdentifier() : string());
324 return vector<string>(1, id);
325}
326
327string Device::GetRpcConnectionIdentifier() {
328 return adaptor_->GetRpcConnectionIdentifier();
329}
330
Paul Stewart75897df2011-04-27 09:05:53 -0700331} // namespace shill