blob: ad9e7661872902e7724c18d91058bdb3463c59d0 [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"
Paul Stewart26b327e2011-10-19 11:38:09 -070026#include "shill/event_dispatcher.h"
Chris Masone8fe2c7e2011-06-09 15:51:19 -070027#include "shill/manager.h"
Chris Masone95207da2011-06-29 16:50:49 -070028#include "shill/property_accessor.h"
Chris Masone2b105542011-06-22 10:58:09 -070029#include "shill/refptr_types.h"
Paul Stewartc39f1132011-06-22 12:02:28 -070030#include "shill/rtnl_handler.h"
Chris Masone2b105542011-06-22 10:58:09 -070031#include "shill/service.h"
Chris Masone5dec5f42011-07-22 14:07:55 -070032#include "shill/store_interface.h"
Gaurav Shah435de2c2011-11-17 19:01:07 -080033#include "shill/technology.h"
Paul Stewart75897df2011-04-27 09:05:53 -070034
Chris Masone5dec5f42011-07-22 14:07:55 -070035using base::StringPrintf;
Chris Masone8fe2c7e2011-06-09 15:51:19 -070036using std::string;
37using std::vector;
38
Paul Stewart75897df2011-04-27 09:05:53 -070039namespace shill {
Chris Masone5dec5f42011-07-22 14:07:55 -070040
41// static
42const char Device::kStoragePowered[] = "Powered";
43
44// static
45const char Device::kStorageIPConfigs[] = "IPConfigs";
46
Paul Stewart75897df2011-04-27 09:05:53 -070047Device::Device(ControlInterface *control_interface,
Paul Stewartb50f0b92011-05-16 16:31:42 -070048 EventDispatcher *dispatcher,
Paul Stewartf1ce5d22011-05-19 13:10:20 -070049 Manager *manager,
Darin Petkovafa6fc42011-06-21 16:21:08 -070050 const string &link_name,
Chris Masone626719f2011-08-18 16:58:48 -070051 const string &address,
Gaurav Shah435de2c2011-11-17 19:01:07 -080052 int interface_index,
53 Technology::Identifier technology)
Chris Masone853b81b2011-06-24 14:11:41 -070054 : powered_(true),
Chris Masoneb925cc82011-06-22 15:39:57 -070055 reconnect_(true),
Chris Masone626719f2011-08-18 16:58:48 -070056 hardware_address_(address),
mukesh agrawalf60e4062011-05-27 13:13:41 -070057 interface_index_(interface_index),
58 running_(false),
Darin Petkovafa6fc42011-06-21 16:21:08 -070059 link_name_(link_name),
Chris Masone19e30402011-07-19 15:48:47 -070060 unique_id_(link_name),
Darin Petkovd9661952011-08-03 16:25:42 -070061 control_interface_(control_interface),
62 dispatcher_(dispatcher),
Chris Masone7df0c672011-07-15 10:24:54 -070063 manager_(manager),
Darin Petkov77cb6812011-08-15 16:19:41 -070064 adaptor_(control_interface->CreateDeviceAdaptor(this)),
Gaurav Shah435de2c2011-11-17 19:01:07 -080065 technology_(technology),
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -070066 dhcp_provider_(DHCPProvider::GetInstance()),
67 rtnl_handler_(RTNLHandler::GetInstance()) {
Chris Masone27c4aa52011-07-02 13:10:14 -070068 store_.RegisterConstString(flimflam::kAddressProperty, &hardware_address_);
Chris Masone4d42df82011-07-02 17:09:39 -070069
70 // flimflam::kBgscanMethodProperty: Registered in WiFi
71 // flimflam::kBgscanShortIntervalProperty: Registered in WiFi
72 // flimflam::kBgscanSignalThresholdProperty: Registered in WiFi
73
74 // flimflam::kCellularAllowRoamingProperty: Registered in Cellular
75 // flimflam::kCarrierProperty: Registered in Cellular
76 // flimflam::kEsnProperty: Registered in Cellular
Darin Petkov3335b372011-08-22 11:05:32 -070077 // flimflam::kHomeProviderProperty: Registered in Cellular
Chris Masone4d42df82011-07-02 17:09:39 -070078 // flimflam::kImeiProperty: Registered in Cellular
79 // flimflam::kImsiProperty: Registered in Cellular
80 // flimflam::kManufacturerProperty: Registered in Cellular
81 // flimflam::kMdnProperty: Registered in Cellular
82 // flimflam::kMeidProperty: Registered in Cellular
83 // flimflam::kMinProperty: Registered in Cellular
84 // flimflam::kModelIDProperty: Registered in Cellular
85 // flimflam::kFirmwareRevisionProperty: Registered in Cellular
86 // flimflam::kHardwareRevisionProperty: Registered in Cellular
87 // flimflam::kPRLVersionProperty: Registered in Cellular
88 // flimflam::kSIMLockStatusProperty: Registered in Cellular
89 // flimflam::kFoundNetworksProperty: Registered in Cellular
Darin Petkove9d12e02011-07-27 15:09:37 -070090 // flimflam::kDBusConnectionProperty: Registered in Cellular
91 // flimflam::kDBusObjectProperty: Register in Cellular
Chris Masone4d42df82011-07-02 17:09:39 -070092
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_.RegisterConstString(flimflam::kInterfaceProperty, &link_name_);
95 HelpRegisterDerivedStrings(flimflam::kIPConfigsProperty,
96 &Device::AvailableIPConfigs,
97 NULL);
98 store_.RegisterConstString(flimflam::kNameProperty, &link_name_);
99 store_.RegisterBool(flimflam::kPoweredProperty, &powered_);
Chris Masoneb925cc82011-06-22 15:39:57 -0700100 // TODO(cmasone): Chrome doesn't use this...does anyone?
Chris Masone27c4aa52011-07-02 13:10:14 -0700101 // store_.RegisterConstBool(flimflam::kReconnectProperty, &reconnect_);
Chris Masoneb925cc82011-06-22 15:39:57 -0700102
Chris Masone4e851612011-07-01 10:46:53 -0700103 // TODO(cmasone): Figure out what shill concept maps to flimflam's "Network".
Chris Masoneb925cc82011-06-22 15:39:57 -0700104 // known_properties_.push_back(flimflam::kNetworksProperty);
105
Chris Masone4d42df82011-07-02 17:09:39 -0700106 // flimflam::kScanningProperty: Registered in WiFi, Cellular
107 // flimflam::kScanIntervalProperty: Registered in WiFi, Cellular
108
109 // TODO(pstew): Initialize Interface monitor, so we can detect new interfaces
Paul Stewartb50f0b92011-05-16 16:31:42 -0700110 VLOG(2) << "Device " << link_name_ << " index " << interface_index;
Paul Stewart75897df2011-04-27 09:05:53 -0700111}
112
113Device::~Device() {
Paul Stewartb50f0b92011-05-16 16:31:42 -0700114 VLOG(2) << "Device " << link_name_ << " destroyed.";
Paul Stewart75897df2011-04-27 09:05:53 -0700115}
116
117void Device::Start() {
118 running_ = true;
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700119 VLOG(2) << "Device " << link_name_ << " starting.";
Chris Masone413a3192011-05-09 17:10:05 -0700120 adaptor_->UpdateEnabled();
Paul Stewart75897df2011-04-27 09:05:53 -0700121}
122
123void Device::Stop() {
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700124 VLOG(2) << "Device " << link_name_ << " stopping.";
Paul Stewart75897df2011-04-27 09:05:53 -0700125 running_ = false;
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700126 DestroyIPConfig(); // breaks a reference cycle
127 SelectService(NULL); // breaks a reference cycle
Chris Masone413a3192011-05-09 17:10:05 -0700128 adaptor_->UpdateEnabled();
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700129 rtnl_handler_->SetInterfaceFlags(interface_index(), 0, IFF_UP);
130
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700131 VLOG(3) << "Device " << link_name_ << " ipconfig_ "
132 << (ipconfig_.get() ? "is set." : "is not set.");
133 VLOG(3) << "Device " << link_name_ << " connection_ "
134 << (connection_.get() ? "is set." : "is not set.");
135 VLOG(3) << "Device " << link_name_ << " selected_service_ "
136 << (selected_service_.get() ? "is set." : "is not set.");
Paul Stewart75897df2011-04-27 09:05:53 -0700137}
138
mukesh agrawal1830fa12011-09-26 14:31:40 -0700139bool Device::TechnologyIs(const Technology::Identifier /*type*/) const {
Darin Petkovafa6fc42011-06-21 16:21:08 -0700140 return false;
141}
142
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700143void Device::LinkEvent(unsigned flags, unsigned change) {
mukesh agrawal47009f82011-08-25 14:07:35 -0700144 VLOG(2) << "Device " << link_name_
145 << std::showbase << std::hex
146 << " flags " << flags << " changed " << change
147 << std::dec << std::noshowbase;
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700148}
149
Darin Petkovc0865312011-09-16 15:31:20 -0700150void Device::Scan(Error *error) {
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700151 VLOG(2) << "Device " << link_name_ << " scan requested.";
Paul Stewartbe005172011-11-02 18:10:29 -0700152 Error::PopulateAndLog(error, Error::kNotSupported,
153 "Device doesn't support scan.");
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700154}
155
mukesh agrawal1830fa12011-09-26 14:31:40 -0700156void Device::RegisterOnNetwork(const std::string &/*network_id*/,
157 Error *error) {
Paul Stewartbe005172011-11-02 18:10:29 -0700158 Error::PopulateAndLog(error, Error::kNotSupported,
159 "Device doesn't support network registration.");
Darin Petkov9ae310f2011-08-30 15:41:13 -0700160}
161
mukesh agrawal1830fa12011-09-26 14:31:40 -0700162void Device::RequirePIN(const string &/*pin*/, bool /*require*/, Error *error) {
Paul Stewartbe005172011-11-02 18:10:29 -0700163 Error::PopulateAndLog(error, Error::kNotSupported,
164 "Device doesn't support RequirePIN.");
Darin Petkove42e1012011-08-31 12:35:04 -0700165}
166
mukesh agrawal1830fa12011-09-26 14:31:40 -0700167void Device::EnterPIN(const string &/*pin*/, Error *error) {
Paul Stewartbe005172011-11-02 18:10:29 -0700168 Error::PopulateAndLog(error, Error::kNotSupported,
169 "Device doesn't support EnterPIN.");
Darin Petkove42e1012011-08-31 12:35:04 -0700170}
171
mukesh agrawal1830fa12011-09-26 14:31:40 -0700172void Device::UnblockPIN(const string &/*unblock_code*/,
173 const string &/*pin*/,
Darin Petkove42e1012011-08-31 12:35:04 -0700174 Error *error) {
Paul Stewartbe005172011-11-02 18:10:29 -0700175 Error::PopulateAndLog(error, Error::kNotSupported,
176 "Device doesn't support UnblockPIN.");
Darin Petkove42e1012011-08-31 12:35:04 -0700177}
178
mukesh agrawal1830fa12011-09-26 14:31:40 -0700179void Device::ChangePIN(const string &/*old_pin*/,
180 const string &/*new_pin*/,
Darin Petkove42e1012011-08-31 12:35:04 -0700181 Error *error) {
Paul Stewartbe005172011-11-02 18:10:29 -0700182 Error::PopulateAndLog(error, Error::kNotSupported,
183 "Device doesn't support ChangePIN.");
Darin Petkove42e1012011-08-31 12:35:04 -0700184}
185
Gaurav Shah435de2c2011-11-17 19:01:07 -0800186bool Device::IsConnected() const {
187 if (selected_service_)
188 return selected_service_->IsConnected();
189 return false;
190}
191
Chris Masone27c4aa52011-07-02 13:10:14 -0700192string Device::GetRpcIdentifier() {
193 return adaptor_->GetRpcIdentifier();
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700194}
195
Chris Masone5dec5f42011-07-22 14:07:55 -0700196string Device::GetStorageIdentifier() {
197 string id = GetRpcIdentifier();
198 ControlInterface::RpcIdToStorageId(&id);
Chris Masone626719f2011-08-18 16:58:48 -0700199 size_t needle = id.find('_');
Chris Masone34af2182011-08-22 11:59:36 -0700200 DLOG_IF(ERROR, needle == string::npos) << "No _ in storage id?!?!";
Chris Masone626719f2011-08-18 16:58:48 -0700201 id.replace(id.begin() + needle + 1, id.end(), hardware_address_);
Chris Masone5dec5f42011-07-22 14:07:55 -0700202 return id;
203}
204
Chris Masone19e30402011-07-19 15:48:47 -0700205const string& Device::FriendlyName() const {
Chris Masone7df0c672011-07-15 10:24:54 -0700206 return link_name_;
Darin Petkovafa6fc42011-06-21 16:21:08 -0700207}
208
Chris Masone19e30402011-07-19 15:48:47 -0700209const string& Device::UniqueName() const {
210 return unique_id_;
211}
212
Chris Masone5dec5f42011-07-22 14:07:55 -0700213bool Device::Load(StoreInterface *storage) {
214 const string id = GetStorageIdentifier();
215 if (!storage->ContainsGroup(id)) {
216 LOG(WARNING) << "Device is not available in the persistent store: " << id;
217 return false;
218 }
219 storage->GetBool(id, kStoragePowered, &powered_);
220 // TODO(cmasone): What does it mean to load an IPConfig identifier??
221 return true;
222}
223
224bool Device::Save(StoreInterface *storage) {
225 const string id = GetStorageIdentifier();
226 storage->SetBool(id, kStoragePowered, powered_);
Chris Masone34af2182011-08-22 11:59:36 -0700227 if (ipconfig_.get()) {
228 // The _0 is an index into the list of IPConfigs that this device might
229 // have. We only have one IPConfig right now, and I hope to never have
230 // to support more, as sleffler indicates that associating IPConfigs
231 // with devices is wrong and due to be changed in flimflam anyhow.
232 string suffix = hardware_address_ + "_0";
233 ipconfig_->Save(storage, suffix);
234 storage->SetString(id, kStorageIPConfigs, SerializeIPConfigs(suffix));
235 }
Chris Masone5dec5f42011-07-22 14:07:55 -0700236 return true;
237}
238
Darin Petkovafa6fc42011-06-21 16:21:08 -0700239void Device::DestroyIPConfig() {
240 if (ipconfig_.get()) {
241 ipconfig_->ReleaseIP();
242 ipconfig_ = NULL;
243 }
Paul Stewarte6132022011-08-16 09:11:02 -0700244 DestroyConnection();
Darin Petkovafa6fc42011-06-21 16:21:08 -0700245}
246
247bool Device::AcquireDHCPConfig() {
248 DestroyIPConfig();
Darin Petkov77cb6812011-08-15 16:19:41 -0700249 ipconfig_ = dhcp_provider_->CreateConfig(link_name_);
Darin Petkovafa6fc42011-06-21 16:21:08 -0700250 ipconfig_->RegisterUpdateCallback(
251 NewCallback(this, &Device::IPConfigUpdatedCallback));
252 return ipconfig_->RequestIP();
253}
254
mukesh agrawalffa3d042011-10-06 15:26:10 -0700255void Device::HelpRegisterDerivedStrings(
256 const string &name,
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800257 Strings(Device::*get)(Error *),
mukesh agrawalffa3d042011-10-06 15:26:10 -0700258 void(Device::*set)(const Strings&, Error *)) {
Chris Masone27c4aa52011-07-02 13:10:14 -0700259 store_.RegisterDerivedStrings(
260 name,
261 StringsAccessor(new CustomAccessor<Device, Strings>(this, get, set)));
Chris Masone4e851612011-07-01 10:46:53 -0700262}
263
Chris Masone2b105542011-06-22 10:58:09 -0700264void Device::IPConfigUpdatedCallback(const IPConfigRefPtr &ipconfig,
265 bool success) {
Paul Stewarte6132022011-08-16 09:11:02 -0700266 VLOG(2) << __func__ << " " << " success: " << success;
Paul Stewartc39f1132011-06-22 12:02:28 -0700267 if (success) {
Paul Stewarte6132022011-08-16 09:11:02 -0700268 CreateConnection();
269 connection_->UpdateFromIPConfig(ipconfig);
Paul Stewart03dba0b2011-08-22 16:32:45 -0700270 SetServiceState(Service::kStateConnected);
Paul Stewarte6132022011-08-16 09:11:02 -0700271 } else {
272 // TODO(pstew): This logic gets more complex when multiple IPConfig types
273 // are run in parallel (e.g. DHCP and DHCP6)
Paul Stewart03dba0b2011-08-22 16:32:45 -0700274 SetServiceState(Service::kStateDisconnected);
Paul Stewarte6132022011-08-16 09:11:02 -0700275 DestroyConnection();
Paul Stewartc39f1132011-06-22 12:02:28 -0700276 }
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700277}
278
Paul Stewarte6132022011-08-16 09:11:02 -0700279void Device::CreateConnection() {
280 VLOG(2) << __func__;
281 if (!connection_.get()) {
Paul Stewart9a908082011-08-31 12:18:48 -0700282 connection_ = new Connection(interface_index_,
283 link_name_,
284 manager_->device_info());
Paul Stewarte6132022011-08-16 09:11:02 -0700285 }
286}
287
288void Device::DestroyConnection() {
289 VLOG(2) << __func__;
290 connection_ = NULL;
291}
292
Paul Stewart03dba0b2011-08-22 16:32:45 -0700293void Device::SelectService(const ServiceRefPtr &service) {
Paul Stewarta2b8cd12011-08-25 14:55:32 -0700294 VLOG(2) << __func__ << ": "
mukesh agrawal15908392011-11-16 18:29:25 +0000295 << (service.get() ?
296 StringPrintf("%s (%s)",
297 service->UniqueName().c_str(),
298 service->friendly_name().c_str()) :
299 "*reset*");
Paul Stewart03dba0b2011-08-22 16:32:45 -0700300 if (selected_service_.get() &&
301 selected_service_->state() != Service::kStateFailure) {
302 selected_service_->SetState(Service::kStateIdle);
303 }
304 selected_service_ = service;
305}
306
307void Device::SetServiceState(Service::ConnectState state) {
308 if (selected_service_.get()) {
309 selected_service_->SetState(state);
310 }
311}
312
313void Device::SetServiceFailure(Service::ConnectFailure failure_state) {
314 if (selected_service_.get()) {
315 selected_service_->SetFailure(failure_state);
316 }
317}
318
Chris Masone34af2182011-08-22 11:59:36 -0700319string Device::SerializeIPConfigs(const string &suffix) {
320 return StringPrintf("%s:%s", suffix.c_str(), ipconfig_->type().c_str());
Chris Masone5dec5f42011-07-22 14:07:55 -0700321}
322
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800323vector<string> Device::AvailableIPConfigs(Error */*error*/) {
Chris Masone4e851612011-07-01 10:46:53 -0700324 string id = (ipconfig_.get() ? ipconfig_->GetRpcIdentifier() : string());
325 return vector<string>(1, id);
326}
327
328string Device::GetRpcConnectionIdentifier() {
329 return adaptor_->GetRpcConnectionIdentifier();
330}
331
Paul Stewart75897df2011-04-27 09:05:53 -0700332} // namespace shill