blob: 62e6061430bec463fdf84a56fb92949c41b576ac [file] [log] [blame]
mukesh agrawal8a3188d2011-12-01 20:56:44 +00001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Paul Stewart75897df2011-04-27 09:05:53 -07002// 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
Paul Stewart2bf1d352011-12-06 15:02:55 -080015#include <base/file_util.h>
Chris Masoneee929b72011-05-10 10:02:18 -070016#include <base/logging.h>
Chris Masone487b8bf2011-05-13 16:27:57 -070017#include <base/memory/ref_counted.h>
Chris Masone5dec5f42011-07-22 14:07:55 -070018#include <base/stringprintf.h>
Chris Masone3bd3c8c2011-06-13 08:20:26 -070019#include <chromeos/dbus/service_constants.h>
Chris Masoneee929b72011-05-10 10:02:18 -070020
Paul Stewarte6132022011-08-16 09:11:02 -070021#include "shill/connection.h"
Paul Stewart75897df2011-04-27 09:05:53 -070022#include "shill/control_interface.h"
Chris Masoned7732e42011-05-20 11:08:56 -070023#include "shill/device_dbus_adaptor.h"
Chris Masone2b105542011-06-22 10:58:09 -070024#include "shill/dhcp_config.h"
Darin Petkovafa6fc42011-06-21 16:21:08 -070025#include "shill/dhcp_provider.h"
Chris Masone8fe2c7e2011-06-09 15:51:19 -070026#include "shill/error.h"
Paul Stewart26b327e2011-10-19 11:38:09 -070027#include "shill/event_dispatcher.h"
Paul Stewartf65320c2011-10-13 14:34:52 -070028#include "shill/http_proxy.h"
Chris Masone8fe2c7e2011-06-09 15:51:19 -070029#include "shill/manager.h"
Chris Masone95207da2011-06-29 16:50:49 -070030#include "shill/property_accessor.h"
Chris Masone2b105542011-06-22 10:58:09 -070031#include "shill/refptr_types.h"
Paul Stewartc39f1132011-06-22 12:02:28 -070032#include "shill/rtnl_handler.h"
Chris Masone2b105542011-06-22 10:58:09 -070033#include "shill/service.h"
Chris Masone5dec5f42011-07-22 14:07:55 -070034#include "shill/store_interface.h"
Gaurav Shah435de2c2011-11-17 19:01:07 -080035#include "shill/technology.h"
Paul Stewart75897df2011-04-27 09:05:53 -070036
Chris Masone5dec5f42011-07-22 14:07:55 -070037using base::StringPrintf;
Chris Masone8fe2c7e2011-06-09 15:51:19 -070038using std::string;
39using std::vector;
40
Paul Stewart75897df2011-04-27 09:05:53 -070041namespace shill {
Chris Masone5dec5f42011-07-22 14:07:55 -070042
43// static
Paul Stewart2bf1d352011-12-06 15:02:55 -080044const char Device::kIPFlagTemplate[] = "/proc/sys/net/%s/conf/%s/%s";
45// static
46const char Device::kIPFlagVersion4[] = "ipv4";
47// static
48const char Device::kIPFlagVersion6[] = "ipv6";
49// static
50const char Device::kIPFlagDisableIPv6[] = "disable_ipv6";
51// static
52const char Device::kIPFlagUseTempAddr[] = "use_tempaddr";
53// static
54const char Device::kIPFlagUseTempAddrUsedAndDefault[] = "2";
Paul Stewartc8f4bef2011-12-13 09:45:51 -080055// static
56const char Device::kIPFlagReversePathFilter[] = "rp_filter";
57// static
58const char Device::kIPFlagReversePathFilterEnabled[] = "1";
59// static
60const char Device::kIPFlagReversePathFilterLooseMode[] = "2";
Paul Stewart2bf1d352011-12-06 15:02:55 -080061
62// static
Chris Masone5dec5f42011-07-22 14:07:55 -070063const char Device::kStoragePowered[] = "Powered";
64
65// static
66const char Device::kStorageIPConfigs[] = "IPConfigs";
67
Paul Stewart75897df2011-04-27 09:05:53 -070068Device::Device(ControlInterface *control_interface,
Paul Stewartb50f0b92011-05-16 16:31:42 -070069 EventDispatcher *dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -080070 Metrics *metrics,
Paul Stewartf1ce5d22011-05-19 13:10:20 -070071 Manager *manager,
Darin Petkovafa6fc42011-06-21 16:21:08 -070072 const string &link_name,
Chris Masone626719f2011-08-18 16:58:48 -070073 const string &address,
Gaurav Shah435de2c2011-11-17 19:01:07 -080074 int interface_index,
75 Technology::Identifier technology)
Chris Masone853b81b2011-06-24 14:11:41 -070076 : powered_(true),
Chris Masoneb925cc82011-06-22 15:39:57 -070077 reconnect_(true),
Chris Masone626719f2011-08-18 16:58:48 -070078 hardware_address_(address),
mukesh agrawalf60e4062011-05-27 13:13:41 -070079 interface_index_(interface_index),
80 running_(false),
Darin Petkovafa6fc42011-06-21 16:21:08 -070081 link_name_(link_name),
Chris Masone19e30402011-07-19 15:48:47 -070082 unique_id_(link_name),
Darin Petkovd9661952011-08-03 16:25:42 -070083 control_interface_(control_interface),
84 dispatcher_(dispatcher),
Thieu Le3426c8f2012-01-11 17:35:11 -080085 metrics_(metrics),
Chris Masone7df0c672011-07-15 10:24:54 -070086 manager_(manager),
Darin Petkov77cb6812011-08-15 16:19:41 -070087 adaptor_(control_interface->CreateDeviceAdaptor(this)),
Gaurav Shah435de2c2011-11-17 19:01:07 -080088 technology_(technology),
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -070089 dhcp_provider_(DHCPProvider::GetInstance()),
90 rtnl_handler_(RTNLHandler::GetInstance()) {
Chris Masone27c4aa52011-07-02 13:10:14 -070091 store_.RegisterConstString(flimflam::kAddressProperty, &hardware_address_);
Chris Masone4d42df82011-07-02 17:09:39 -070092
93 // flimflam::kBgscanMethodProperty: Registered in WiFi
94 // flimflam::kBgscanShortIntervalProperty: Registered in WiFi
95 // flimflam::kBgscanSignalThresholdProperty: Registered in WiFi
96
97 // flimflam::kCellularAllowRoamingProperty: Registered in Cellular
98 // flimflam::kCarrierProperty: Registered in Cellular
99 // flimflam::kEsnProperty: Registered in Cellular
Darin Petkov3335b372011-08-22 11:05:32 -0700100 // flimflam::kHomeProviderProperty: Registered in Cellular
Chris Masone4d42df82011-07-02 17:09:39 -0700101 // flimflam::kImeiProperty: Registered in Cellular
102 // flimflam::kImsiProperty: Registered in Cellular
103 // flimflam::kManufacturerProperty: Registered in Cellular
104 // flimflam::kMdnProperty: Registered in Cellular
105 // flimflam::kMeidProperty: Registered in Cellular
106 // flimflam::kMinProperty: Registered in Cellular
107 // flimflam::kModelIDProperty: Registered in Cellular
108 // flimflam::kFirmwareRevisionProperty: Registered in Cellular
109 // flimflam::kHardwareRevisionProperty: Registered in Cellular
110 // flimflam::kPRLVersionProperty: Registered in Cellular
111 // flimflam::kSIMLockStatusProperty: Registered in Cellular
112 // flimflam::kFoundNetworksProperty: Registered in Cellular
Darin Petkove9d12e02011-07-27 15:09:37 -0700113 // flimflam::kDBusConnectionProperty: Registered in Cellular
114 // flimflam::kDBusObjectProperty: Register in Cellular
Chris Masone4d42df82011-07-02 17:09:39 -0700115
Chris Masoneb925cc82011-06-22 15:39:57 -0700116 // TODO(cmasone): Chrome doesn't use this...does anyone?
Chris Masone27c4aa52011-07-02 13:10:14 -0700117 // store_.RegisterConstString(flimflam::kInterfaceProperty, &link_name_);
118 HelpRegisterDerivedStrings(flimflam::kIPConfigsProperty,
119 &Device::AvailableIPConfigs,
120 NULL);
121 store_.RegisterConstString(flimflam::kNameProperty, &link_name_);
122 store_.RegisterBool(flimflam::kPoweredProperty, &powered_);
Chris Masoneb925cc82011-06-22 15:39:57 -0700123 // TODO(cmasone): Chrome doesn't use this...does anyone?
Chris Masone27c4aa52011-07-02 13:10:14 -0700124 // store_.RegisterConstBool(flimflam::kReconnectProperty, &reconnect_);
Chris Masoneb925cc82011-06-22 15:39:57 -0700125
Chris Masone4e851612011-07-01 10:46:53 -0700126 // TODO(cmasone): Figure out what shill concept maps to flimflam's "Network".
Chris Masoneb925cc82011-06-22 15:39:57 -0700127 // known_properties_.push_back(flimflam::kNetworksProperty);
128
Chris Masone4d42df82011-07-02 17:09:39 -0700129 // flimflam::kScanningProperty: Registered in WiFi, Cellular
130 // flimflam::kScanIntervalProperty: Registered in WiFi, Cellular
131
132 // TODO(pstew): Initialize Interface monitor, so we can detect new interfaces
Paul Stewartb50f0b92011-05-16 16:31:42 -0700133 VLOG(2) << "Device " << link_name_ << " index " << interface_index;
Paul Stewart75897df2011-04-27 09:05:53 -0700134}
135
136Device::~Device() {
Paul Stewartb50f0b92011-05-16 16:31:42 -0700137 VLOG(2) << "Device " << link_name_ << " destroyed.";
Paul Stewart75897df2011-04-27 09:05:53 -0700138}
139
140void Device::Start() {
141 running_ = true;
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700142 VLOG(2) << "Device " << link_name_ << " starting.";
Chris Masone413a3192011-05-09 17:10:05 -0700143 adaptor_->UpdateEnabled();
Paul Stewart75897df2011-04-27 09:05:53 -0700144}
145
146void Device::Stop() {
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700147 VLOG(2) << "Device " << link_name_ << " stopping.";
Paul Stewart75897df2011-04-27 09:05:53 -0700148 running_ = false;
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700149 DestroyIPConfig(); // breaks a reference cycle
150 SelectService(NULL); // breaks a reference cycle
Chris Masone413a3192011-05-09 17:10:05 -0700151 adaptor_->UpdateEnabled();
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700152 rtnl_handler_->SetInterfaceFlags(interface_index(), 0, IFF_UP);
153
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700154 VLOG(3) << "Device " << link_name_ << " ipconfig_ "
155 << (ipconfig_.get() ? "is set." : "is not set.");
156 VLOG(3) << "Device " << link_name_ << " connection_ "
157 << (connection_.get() ? "is set." : "is not set.");
158 VLOG(3) << "Device " << link_name_ << " selected_service_ "
159 << (selected_service_.get() ? "is set." : "is not set.");
Paul Stewart75897df2011-04-27 09:05:53 -0700160}
161
mukesh agrawal1830fa12011-09-26 14:31:40 -0700162bool Device::TechnologyIs(const Technology::Identifier /*type*/) const {
Darin Petkovafa6fc42011-06-21 16:21:08 -0700163 return false;
164}
165
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700166void Device::LinkEvent(unsigned flags, unsigned change) {
mukesh agrawal47009f82011-08-25 14:07:35 -0700167 VLOG(2) << "Device " << link_name_
168 << std::showbase << std::hex
169 << " flags " << flags << " changed " << change
170 << std::dec << std::noshowbase;
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700171}
172
Darin Petkovc0865312011-09-16 15:31:20 -0700173void Device::Scan(Error *error) {
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700174 VLOG(2) << "Device " << link_name_ << " scan requested.";
Paul Stewartbe005172011-11-02 18:10:29 -0700175 Error::PopulateAndLog(error, Error::kNotSupported,
176 "Device doesn't support scan.");
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700177}
178
mukesh agrawal1830fa12011-09-26 14:31:40 -0700179void Device::RegisterOnNetwork(const std::string &/*network_id*/,
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500180 ReturnerInterface *returner) {
181 Error error;
182 Error::PopulateAndLog(&error, Error::kNotSupported,
Paul Stewartbe005172011-11-02 18:10:29 -0700183 "Device doesn't support network registration.");
Eric Shienbrood5de44ab2011-12-05 10:46:27 -0500184 returner->ReturnError(error);
Darin Petkov9ae310f2011-08-30 15:41:13 -0700185}
186
Darin Petkovc64fe5e2012-01-11 12:46:13 +0100187void Device::RequirePIN(
188 const string &/*pin*/, bool /*require*/, ReturnerInterface *returner) {
189 VLOG(2) << __func__;
190 Error error;
191 Error::PopulateAndLog(
192 &error, Error::kNotSupported, "Device doesn't support RequirePIN.");
193 returner->ReturnError(error);
Darin Petkove42e1012011-08-31 12:35:04 -0700194}
195
Darin Petkove5bc2cb2011-12-07 14:47:32 +0100196void Device::EnterPIN(const string &/*pin*/, ReturnerInterface *returner) {
197 VLOG(2) << __func__;
198 Error error;
Darin Petkovc64fe5e2012-01-11 12:46:13 +0100199 Error::PopulateAndLog(
200 &error, Error::kNotSupported, "Device doesn't support EnterPIN.");
Darin Petkove5bc2cb2011-12-07 14:47:32 +0100201 returner->ReturnError(error);
Darin Petkove42e1012011-08-31 12:35:04 -0700202}
203
mukesh agrawal1830fa12011-09-26 14:31:40 -0700204void Device::UnblockPIN(const string &/*unblock_code*/,
205 const string &/*pin*/,
Darin Petkovc64fe5e2012-01-11 12:46:13 +0100206 ReturnerInterface *returner) {
207 VLOG(2) << __func__;
208 Error error;
209 Error::PopulateAndLog(
210 &error, Error::kNotSupported, "Device doesn't support UnblockPIN.");
211 returner->ReturnError(error);
Darin Petkove42e1012011-08-31 12:35:04 -0700212}
213
mukesh agrawal1830fa12011-09-26 14:31:40 -0700214void Device::ChangePIN(const string &/*old_pin*/,
215 const string &/*new_pin*/,
Darin Petkovc64fe5e2012-01-11 12:46:13 +0100216 ReturnerInterface *returner) {
217 VLOG(2) << __func__;
218 Error error;
219 Error::PopulateAndLog(
220 &error, Error::kNotSupported, "Device doesn't support ChangePIN.");
221 returner->ReturnError(error);
Darin Petkove42e1012011-08-31 12:35:04 -0700222}
223
Paul Stewart2bf1d352011-12-06 15:02:55 -0800224void Device::DisableIPv6() {
225 SetIPFlag(IPAddress::kFamilyIPv6, kIPFlagDisableIPv6, "1");
226}
227
228void Device::EnableIPv6() {
229 SetIPFlag(IPAddress::kFamilyIPv6, kIPFlagDisableIPv6, "0");
230}
231
232void Device::EnableIPv6Privacy() {
233 SetIPFlag(IPAddress::kFamilyIPv6, kIPFlagUseTempAddr,
234 kIPFlagUseTempAddrUsedAndDefault);
235}
236
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800237void Device::DisableReversePathFilter() {
238 // TODO(pstew): Current kernel doesn't offer reverse-path filtering flag
239 // for IPv6. crosbug.com/24228
240 SetIPFlag(IPAddress::kFamilyIPv4, kIPFlagReversePathFilter,
241 kIPFlagReversePathFilterLooseMode);
242}
243
244void Device::EnableReversePathFilter() {
245 SetIPFlag(IPAddress::kFamilyIPv4, kIPFlagReversePathFilter,
246 kIPFlagReversePathFilterEnabled);
247}
248
Gaurav Shah435de2c2011-11-17 19:01:07 -0800249bool Device::IsConnected() const {
250 if (selected_service_)
251 return selected_service_->IsConnected();
252 return false;
253}
254
Chris Masone27c4aa52011-07-02 13:10:14 -0700255string Device::GetRpcIdentifier() {
256 return adaptor_->GetRpcIdentifier();
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700257}
258
Chris Masone5dec5f42011-07-22 14:07:55 -0700259string Device::GetStorageIdentifier() {
260 string id = GetRpcIdentifier();
261 ControlInterface::RpcIdToStorageId(&id);
Chris Masone626719f2011-08-18 16:58:48 -0700262 size_t needle = id.find('_');
Chris Masone34af2182011-08-22 11:59:36 -0700263 DLOG_IF(ERROR, needle == string::npos) << "No _ in storage id?!?!";
Chris Masone626719f2011-08-18 16:58:48 -0700264 id.replace(id.begin() + needle + 1, id.end(), hardware_address_);
Chris Masone5dec5f42011-07-22 14:07:55 -0700265 return id;
266}
267
Chris Masone19e30402011-07-19 15:48:47 -0700268const string& Device::FriendlyName() const {
Chris Masone7df0c672011-07-15 10:24:54 -0700269 return link_name_;
Darin Petkovafa6fc42011-06-21 16:21:08 -0700270}
271
Chris Masone19e30402011-07-19 15:48:47 -0700272const string& Device::UniqueName() const {
273 return unique_id_;
274}
275
Chris Masone5dec5f42011-07-22 14:07:55 -0700276bool Device::Load(StoreInterface *storage) {
277 const string id = GetStorageIdentifier();
278 if (!storage->ContainsGroup(id)) {
279 LOG(WARNING) << "Device is not available in the persistent store: " << id;
280 return false;
281 }
282 storage->GetBool(id, kStoragePowered, &powered_);
283 // TODO(cmasone): What does it mean to load an IPConfig identifier??
284 return true;
285}
286
287bool Device::Save(StoreInterface *storage) {
288 const string id = GetStorageIdentifier();
289 storage->SetBool(id, kStoragePowered, powered_);
Chris Masone34af2182011-08-22 11:59:36 -0700290 if (ipconfig_.get()) {
291 // The _0 is an index into the list of IPConfigs that this device might
292 // have. We only have one IPConfig right now, and I hope to never have
293 // to support more, as sleffler indicates that associating IPConfigs
294 // with devices is wrong and due to be changed in flimflam anyhow.
295 string suffix = hardware_address_ + "_0";
296 ipconfig_->Save(storage, suffix);
297 storage->SetString(id, kStorageIPConfigs, SerializeIPConfigs(suffix));
298 }
Chris Masone5dec5f42011-07-22 14:07:55 -0700299 return true;
300}
301
Darin Petkovafa6fc42011-06-21 16:21:08 -0700302void Device::DestroyIPConfig() {
Paul Stewart2bf1d352011-12-06 15:02:55 -0800303 DisableIPv6();
Darin Petkovafa6fc42011-06-21 16:21:08 -0700304 if (ipconfig_.get()) {
305 ipconfig_->ReleaseIP();
306 ipconfig_ = NULL;
307 }
Paul Stewarte6132022011-08-16 09:11:02 -0700308 DestroyConnection();
Darin Petkovafa6fc42011-06-21 16:21:08 -0700309}
310
Paul Stewart2bf1d352011-12-06 15:02:55 -0800311bool Device::AcquireIPConfig() {
Darin Petkovafa6fc42011-06-21 16:21:08 -0700312 DestroyIPConfig();
Paul Stewart2bf1d352011-12-06 15:02:55 -0800313 EnableIPv6();
Paul Stewartd32f4842012-01-11 16:08:13 -0800314 ipconfig_ = dhcp_provider_->CreateConfig(link_name_, manager_->GetHostName());
Darin Petkovafa6fc42011-06-21 16:21:08 -0700315 ipconfig_->RegisterUpdateCallback(
316 NewCallback(this, &Device::IPConfigUpdatedCallback));
317 return ipconfig_->RequestIP();
318}
319
mukesh agrawalffa3d042011-10-06 15:26:10 -0700320void Device::HelpRegisterDerivedStrings(
321 const string &name,
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800322 Strings(Device::*get)(Error *),
mukesh agrawalffa3d042011-10-06 15:26:10 -0700323 void(Device::*set)(const Strings&, Error *)) {
Chris Masone27c4aa52011-07-02 13:10:14 -0700324 store_.RegisterDerivedStrings(
325 name,
326 StringsAccessor(new CustomAccessor<Device, Strings>(this, get, set)));
Chris Masone4e851612011-07-01 10:46:53 -0700327}
328
Chris Masone2b105542011-06-22 10:58:09 -0700329void Device::IPConfigUpdatedCallback(const IPConfigRefPtr &ipconfig,
330 bool success) {
Paul Stewarte6132022011-08-16 09:11:02 -0700331 VLOG(2) << __func__ << " " << " success: " << success;
Paul Stewartc39f1132011-06-22 12:02:28 -0700332 if (success) {
Paul Stewarte6132022011-08-16 09:11:02 -0700333 CreateConnection();
334 connection_->UpdateFromIPConfig(ipconfig);
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800335 // SetConnection must occur after the UpdateFromIPConfig so the
336 // service can use the values derived from the connection.
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800337 if (selected_service_.get()) {
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800338 selected_service_->SetConnection(connection_);
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800339 }
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800340 // The service state change needs to happen last, so that at the
341 // time we report the state change to the manager, the service
342 // has its connection.
343 SetServiceState(Service::kStateConnected);
Paul Stewarte6132022011-08-16 09:11:02 -0700344 } else {
345 // TODO(pstew): This logic gets more complex when multiple IPConfig types
346 // are run in parallel (e.g. DHCP and DHCP6)
Paul Stewart03dba0b2011-08-22 16:32:45 -0700347 SetServiceState(Service::kStateDisconnected);
Paul Stewarte6132022011-08-16 09:11:02 -0700348 DestroyConnection();
Paul Stewartc39f1132011-06-22 12:02:28 -0700349 }
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700350}
351
Paul Stewarte6132022011-08-16 09:11:02 -0700352void Device::CreateConnection() {
353 VLOG(2) << __func__;
354 if (!connection_.get()) {
Paul Stewart9a908082011-08-31 12:18:48 -0700355 connection_ = new Connection(interface_index_,
356 link_name_,
357 manager_->device_info());
Paul Stewarte6132022011-08-16 09:11:02 -0700358 }
359}
360
361void Device::DestroyConnection() {
362 VLOG(2) << __func__;
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800363 if (selected_service_.get()) {
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800364 selected_service_->SetConnection(NULL);
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800365 }
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800366 connection_ = NULL;
Paul Stewarte6132022011-08-16 09:11:02 -0700367}
368
Paul Stewart03dba0b2011-08-22 16:32:45 -0700369void Device::SelectService(const ServiceRefPtr &service) {
Paul Stewarta2b8cd12011-08-25 14:55:32 -0700370 VLOG(2) << __func__ << ": "
mukesh agrawal15908392011-11-16 18:29:25 +0000371 << (service.get() ?
372 StringPrintf("%s (%s)",
373 service->UniqueName().c_str(),
374 service->friendly_name().c_str()) :
375 "*reset*");
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000376
377 if (selected_service_.get() == service.get()) {
378 // No change to |selected_service_|. Return early to avoid
379 // changing its state.
380 return;
381 }
382
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800383 if (selected_service_.get()) {
384 if (selected_service_->state() != Service::kStateFailure) {
385 selected_service_->SetState(Service::kStateIdle);
386 }
387 // TODO(pstew): We need to revisit the model here: should the Device
388 // subclass be responsible for calling DestroyIPConfig() (which would
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800389 // trigger DestroyConnection() and Service::SetConnection(NULL))?
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800390 // Ethernet does, but WiFi currently does not. crosbug.com/23929
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800391 selected_service_->SetConnection(NULL);
Paul Stewart03dba0b2011-08-22 16:32:45 -0700392 }
393 selected_service_ = service;
394}
395
396void Device::SetServiceState(Service::ConnectState state) {
397 if (selected_service_.get()) {
398 selected_service_->SetState(state);
399 }
400}
401
402void Device::SetServiceFailure(Service::ConnectFailure failure_state) {
403 if (selected_service_.get()) {
404 selected_service_->SetFailure(failure_state);
405 }
406}
407
Chris Masone34af2182011-08-22 11:59:36 -0700408string Device::SerializeIPConfigs(const string &suffix) {
409 return StringPrintf("%s:%s", suffix.c_str(), ipconfig_->type().c_str());
Chris Masone5dec5f42011-07-22 14:07:55 -0700410}
411
Paul Stewart2bf1d352011-12-06 15:02:55 -0800412bool Device::SetIPFlag(IPAddress::Family family, const string &flag,
413 const string &value) {
414 string ip_version;
415 if (family == IPAddress::kFamilyIPv4) {
416 ip_version = kIPFlagVersion4;
417 } else if (family == IPAddress::kFamilyIPv6) {
418 ip_version = kIPFlagVersion6;
419 } else {
420 NOTIMPLEMENTED();
421 }
422 FilePath flag_file(StringPrintf(kIPFlagTemplate, ip_version.c_str(),
423 link_name_.c_str(), flag.c_str()));
424 VLOG(2) << "Writing " << value << " to flag file " << flag_file.value();
425 if (file_util::WriteFile(flag_file, value.c_str(), value.length()) != 1) {
426 LOG(ERROR) << StringPrintf("IP flag write failed: %s to %s",
427 value.c_str(), flag_file.value().c_str());
428 return false;
429 }
430 return true;
431}
432
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800433vector<string> Device::AvailableIPConfigs(Error */*error*/) {
Chris Masone4e851612011-07-01 10:46:53 -0700434 string id = (ipconfig_.get() ? ipconfig_->GetRpcIdentifier() : string());
435 return vector<string>(1, id);
436}
437
438string Device::GetRpcConnectionIdentifier() {
439 return adaptor_->GetRpcConnectionIdentifier();
440}
441
Paul Stewart75897df2011-04-27 09:05:53 -0700442} // namespace shill