blob: 6e8a04ce999771c18fd0bb9d7f98011fc18f7985 [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,
Paul Stewartf1ce5d22011-05-19 13:10:20 -070070 Manager *manager,
Darin Petkovafa6fc42011-06-21 16:21:08 -070071 const string &link_name,
Chris Masone626719f2011-08-18 16:58:48 -070072 const string &address,
Gaurav Shah435de2c2011-11-17 19:01:07 -080073 int interface_index,
74 Technology::Identifier technology)
Chris Masone853b81b2011-06-24 14:11:41 -070075 : powered_(true),
Chris Masoneb925cc82011-06-22 15:39:57 -070076 reconnect_(true),
Chris Masone626719f2011-08-18 16:58:48 -070077 hardware_address_(address),
mukesh agrawalf60e4062011-05-27 13:13:41 -070078 interface_index_(interface_index),
79 running_(false),
Darin Petkovafa6fc42011-06-21 16:21:08 -070080 link_name_(link_name),
Chris Masone19e30402011-07-19 15:48:47 -070081 unique_id_(link_name),
Darin Petkovd9661952011-08-03 16:25:42 -070082 control_interface_(control_interface),
83 dispatcher_(dispatcher),
Chris Masone7df0c672011-07-15 10:24:54 -070084 manager_(manager),
Darin Petkov77cb6812011-08-15 16:19:41 -070085 adaptor_(control_interface->CreateDeviceAdaptor(this)),
Gaurav Shah435de2c2011-11-17 19:01:07 -080086 technology_(technology),
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -070087 dhcp_provider_(DHCPProvider::GetInstance()),
88 rtnl_handler_(RTNLHandler::GetInstance()) {
Chris Masone27c4aa52011-07-02 13:10:14 -070089 store_.RegisterConstString(flimflam::kAddressProperty, &hardware_address_);
Chris Masone4d42df82011-07-02 17:09:39 -070090
91 // flimflam::kBgscanMethodProperty: Registered in WiFi
92 // flimflam::kBgscanShortIntervalProperty: Registered in WiFi
93 // flimflam::kBgscanSignalThresholdProperty: Registered in WiFi
94
95 // flimflam::kCellularAllowRoamingProperty: Registered in Cellular
96 // flimflam::kCarrierProperty: Registered in Cellular
97 // flimflam::kEsnProperty: Registered in Cellular
Darin Petkov3335b372011-08-22 11:05:32 -070098 // flimflam::kHomeProviderProperty: Registered in Cellular
Chris Masone4d42df82011-07-02 17:09:39 -070099 // flimflam::kImeiProperty: Registered in Cellular
100 // flimflam::kImsiProperty: Registered in Cellular
101 // flimflam::kManufacturerProperty: Registered in Cellular
102 // flimflam::kMdnProperty: Registered in Cellular
103 // flimflam::kMeidProperty: Registered in Cellular
104 // flimflam::kMinProperty: Registered in Cellular
105 // flimflam::kModelIDProperty: Registered in Cellular
106 // flimflam::kFirmwareRevisionProperty: Registered in Cellular
107 // flimflam::kHardwareRevisionProperty: Registered in Cellular
108 // flimflam::kPRLVersionProperty: Registered in Cellular
109 // flimflam::kSIMLockStatusProperty: Registered in Cellular
110 // flimflam::kFoundNetworksProperty: Registered in Cellular
Darin Petkove9d12e02011-07-27 15:09:37 -0700111 // flimflam::kDBusConnectionProperty: Registered in Cellular
112 // flimflam::kDBusObjectProperty: Register in Cellular
Chris Masone4d42df82011-07-02 17:09:39 -0700113
Chris Masoneb925cc82011-06-22 15:39:57 -0700114 // TODO(cmasone): Chrome doesn't use this...does anyone?
Chris Masone27c4aa52011-07-02 13:10:14 -0700115 // store_.RegisterConstString(flimflam::kInterfaceProperty, &link_name_);
116 HelpRegisterDerivedStrings(flimflam::kIPConfigsProperty,
117 &Device::AvailableIPConfigs,
118 NULL);
119 store_.RegisterConstString(flimflam::kNameProperty, &link_name_);
120 store_.RegisterBool(flimflam::kPoweredProperty, &powered_);
Chris Masoneb925cc82011-06-22 15:39:57 -0700121 // TODO(cmasone): Chrome doesn't use this...does anyone?
Chris Masone27c4aa52011-07-02 13:10:14 -0700122 // store_.RegisterConstBool(flimflam::kReconnectProperty, &reconnect_);
Chris Masoneb925cc82011-06-22 15:39:57 -0700123
Chris Masone4e851612011-07-01 10:46:53 -0700124 // TODO(cmasone): Figure out what shill concept maps to flimflam's "Network".
Chris Masoneb925cc82011-06-22 15:39:57 -0700125 // known_properties_.push_back(flimflam::kNetworksProperty);
126
Chris Masone4d42df82011-07-02 17:09:39 -0700127 // flimflam::kScanningProperty: Registered in WiFi, Cellular
128 // flimflam::kScanIntervalProperty: Registered in WiFi, Cellular
129
130 // TODO(pstew): Initialize Interface monitor, so we can detect new interfaces
Paul Stewartb50f0b92011-05-16 16:31:42 -0700131 VLOG(2) << "Device " << link_name_ << " index " << interface_index;
Paul Stewart75897df2011-04-27 09:05:53 -0700132}
133
134Device::~Device() {
Paul Stewartb50f0b92011-05-16 16:31:42 -0700135 VLOG(2) << "Device " << link_name_ << " destroyed.";
Paul Stewart75897df2011-04-27 09:05:53 -0700136}
137
138void Device::Start() {
139 running_ = true;
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700140 VLOG(2) << "Device " << link_name_ << " starting.";
Chris Masone413a3192011-05-09 17:10:05 -0700141 adaptor_->UpdateEnabled();
Paul Stewart75897df2011-04-27 09:05:53 -0700142}
143
144void Device::Stop() {
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700145 VLOG(2) << "Device " << link_name_ << " stopping.";
Paul Stewart75897df2011-04-27 09:05:53 -0700146 running_ = false;
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700147 DestroyIPConfig(); // breaks a reference cycle
148 SelectService(NULL); // breaks a reference cycle
Chris Masone413a3192011-05-09 17:10:05 -0700149 adaptor_->UpdateEnabled();
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700150 rtnl_handler_->SetInterfaceFlags(interface_index(), 0, IFF_UP);
151
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700152 VLOG(3) << "Device " << link_name_ << " ipconfig_ "
153 << (ipconfig_.get() ? "is set." : "is not set.");
154 VLOG(3) << "Device " << link_name_ << " connection_ "
155 << (connection_.get() ? "is set." : "is not set.");
156 VLOG(3) << "Device " << link_name_ << " selected_service_ "
157 << (selected_service_.get() ? "is set." : "is not set.");
Paul Stewart75897df2011-04-27 09:05:53 -0700158}
159
mukesh agrawal1830fa12011-09-26 14:31:40 -0700160bool Device::TechnologyIs(const Technology::Identifier /*type*/) const {
Darin Petkovafa6fc42011-06-21 16:21:08 -0700161 return false;
162}
163
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700164void Device::LinkEvent(unsigned flags, unsigned change) {
mukesh agrawal47009f82011-08-25 14:07:35 -0700165 VLOG(2) << "Device " << link_name_
166 << std::showbase << std::hex
167 << " flags " << flags << " changed " << change
168 << std::dec << std::noshowbase;
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700169}
170
Darin Petkovc0865312011-09-16 15:31:20 -0700171void Device::Scan(Error *error) {
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700172 VLOG(2) << "Device " << link_name_ << " scan requested.";
Paul Stewartbe005172011-11-02 18:10:29 -0700173 Error::PopulateAndLog(error, Error::kNotSupported,
174 "Device doesn't support scan.");
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700175}
176
mukesh agrawal1830fa12011-09-26 14:31:40 -0700177void Device::RegisterOnNetwork(const std::string &/*network_id*/,
178 Error *error) {
Paul Stewartbe005172011-11-02 18:10:29 -0700179 Error::PopulateAndLog(error, Error::kNotSupported,
180 "Device doesn't support network registration.");
Darin Petkov9ae310f2011-08-30 15:41:13 -0700181}
182
Darin Petkovc64fe5e2012-01-11 12:46:13 +0100183void Device::RequirePIN(
184 const string &/*pin*/, bool /*require*/, ReturnerInterface *returner) {
185 VLOG(2) << __func__;
186 Error error;
187 Error::PopulateAndLog(
188 &error, Error::kNotSupported, "Device doesn't support RequirePIN.");
189 returner->ReturnError(error);
Darin Petkove42e1012011-08-31 12:35:04 -0700190}
191
Darin Petkove5bc2cb2011-12-07 14:47:32 +0100192void Device::EnterPIN(const string &/*pin*/, ReturnerInterface *returner) {
193 VLOG(2) << __func__;
194 Error error;
Darin Petkovc64fe5e2012-01-11 12:46:13 +0100195 Error::PopulateAndLog(
196 &error, Error::kNotSupported, "Device doesn't support EnterPIN.");
Darin Petkove5bc2cb2011-12-07 14:47:32 +0100197 returner->ReturnError(error);
Darin Petkove42e1012011-08-31 12:35:04 -0700198}
199
mukesh agrawal1830fa12011-09-26 14:31:40 -0700200void Device::UnblockPIN(const string &/*unblock_code*/,
201 const string &/*pin*/,
Darin Petkovc64fe5e2012-01-11 12:46:13 +0100202 ReturnerInterface *returner) {
203 VLOG(2) << __func__;
204 Error error;
205 Error::PopulateAndLog(
206 &error, Error::kNotSupported, "Device doesn't support UnblockPIN.");
207 returner->ReturnError(error);
Darin Petkove42e1012011-08-31 12:35:04 -0700208}
209
mukesh agrawal1830fa12011-09-26 14:31:40 -0700210void Device::ChangePIN(const string &/*old_pin*/,
211 const string &/*new_pin*/,
Darin Petkovc64fe5e2012-01-11 12:46:13 +0100212 ReturnerInterface *returner) {
213 VLOG(2) << __func__;
214 Error error;
215 Error::PopulateAndLog(
216 &error, Error::kNotSupported, "Device doesn't support ChangePIN.");
217 returner->ReturnError(error);
Darin Petkove42e1012011-08-31 12:35:04 -0700218}
219
Paul Stewart2bf1d352011-12-06 15:02:55 -0800220void Device::DisableIPv6() {
221 SetIPFlag(IPAddress::kFamilyIPv6, kIPFlagDisableIPv6, "1");
222}
223
224void Device::EnableIPv6() {
225 SetIPFlag(IPAddress::kFamilyIPv6, kIPFlagDisableIPv6, "0");
226}
227
228void Device::EnableIPv6Privacy() {
229 SetIPFlag(IPAddress::kFamilyIPv6, kIPFlagUseTempAddr,
230 kIPFlagUseTempAddrUsedAndDefault);
231}
232
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800233void Device::DisableReversePathFilter() {
234 // TODO(pstew): Current kernel doesn't offer reverse-path filtering flag
235 // for IPv6. crosbug.com/24228
236 SetIPFlag(IPAddress::kFamilyIPv4, kIPFlagReversePathFilter,
237 kIPFlagReversePathFilterLooseMode);
238}
239
240void Device::EnableReversePathFilter() {
241 SetIPFlag(IPAddress::kFamilyIPv4, kIPFlagReversePathFilter,
242 kIPFlagReversePathFilterEnabled);
243}
244
Gaurav Shah435de2c2011-11-17 19:01:07 -0800245bool Device::IsConnected() const {
246 if (selected_service_)
247 return selected_service_->IsConnected();
248 return false;
249}
250
Chris Masone27c4aa52011-07-02 13:10:14 -0700251string Device::GetRpcIdentifier() {
252 return adaptor_->GetRpcIdentifier();
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700253}
254
Chris Masone5dec5f42011-07-22 14:07:55 -0700255string Device::GetStorageIdentifier() {
256 string id = GetRpcIdentifier();
257 ControlInterface::RpcIdToStorageId(&id);
Chris Masone626719f2011-08-18 16:58:48 -0700258 size_t needle = id.find('_');
Chris Masone34af2182011-08-22 11:59:36 -0700259 DLOG_IF(ERROR, needle == string::npos) << "No _ in storage id?!?!";
Chris Masone626719f2011-08-18 16:58:48 -0700260 id.replace(id.begin() + needle + 1, id.end(), hardware_address_);
Chris Masone5dec5f42011-07-22 14:07:55 -0700261 return id;
262}
263
Chris Masone19e30402011-07-19 15:48:47 -0700264const string& Device::FriendlyName() const {
Chris Masone7df0c672011-07-15 10:24:54 -0700265 return link_name_;
Darin Petkovafa6fc42011-06-21 16:21:08 -0700266}
267
Chris Masone19e30402011-07-19 15:48:47 -0700268const string& Device::UniqueName() const {
269 return unique_id_;
270}
271
Chris Masone5dec5f42011-07-22 14:07:55 -0700272bool Device::Load(StoreInterface *storage) {
273 const string id = GetStorageIdentifier();
274 if (!storage->ContainsGroup(id)) {
275 LOG(WARNING) << "Device is not available in the persistent store: " << id;
276 return false;
277 }
278 storage->GetBool(id, kStoragePowered, &powered_);
279 // TODO(cmasone): What does it mean to load an IPConfig identifier??
280 return true;
281}
282
283bool Device::Save(StoreInterface *storage) {
284 const string id = GetStorageIdentifier();
285 storage->SetBool(id, kStoragePowered, powered_);
Chris Masone34af2182011-08-22 11:59:36 -0700286 if (ipconfig_.get()) {
287 // The _0 is an index into the list of IPConfigs that this device might
288 // have. We only have one IPConfig right now, and I hope to never have
289 // to support more, as sleffler indicates that associating IPConfigs
290 // with devices is wrong and due to be changed in flimflam anyhow.
291 string suffix = hardware_address_ + "_0";
292 ipconfig_->Save(storage, suffix);
293 storage->SetString(id, kStorageIPConfigs, SerializeIPConfigs(suffix));
294 }
Chris Masone5dec5f42011-07-22 14:07:55 -0700295 return true;
296}
297
Darin Petkovafa6fc42011-06-21 16:21:08 -0700298void Device::DestroyIPConfig() {
Paul Stewart2bf1d352011-12-06 15:02:55 -0800299 DisableIPv6();
Darin Petkovafa6fc42011-06-21 16:21:08 -0700300 if (ipconfig_.get()) {
301 ipconfig_->ReleaseIP();
302 ipconfig_ = NULL;
303 }
Paul Stewarte6132022011-08-16 09:11:02 -0700304 DestroyConnection();
Darin Petkovafa6fc42011-06-21 16:21:08 -0700305}
306
Paul Stewart2bf1d352011-12-06 15:02:55 -0800307bool Device::AcquireIPConfig() {
Darin Petkovafa6fc42011-06-21 16:21:08 -0700308 DestroyIPConfig();
Paul Stewart2bf1d352011-12-06 15:02:55 -0800309 EnableIPv6();
Darin Petkov77cb6812011-08-15 16:19:41 -0700310 ipconfig_ = dhcp_provider_->CreateConfig(link_name_);
Darin Petkovafa6fc42011-06-21 16:21:08 -0700311 ipconfig_->RegisterUpdateCallback(
312 NewCallback(this, &Device::IPConfigUpdatedCallback));
313 return ipconfig_->RequestIP();
314}
315
mukesh agrawalffa3d042011-10-06 15:26:10 -0700316void Device::HelpRegisterDerivedStrings(
317 const string &name,
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800318 Strings(Device::*get)(Error *),
mukesh agrawalffa3d042011-10-06 15:26:10 -0700319 void(Device::*set)(const Strings&, Error *)) {
Chris Masone27c4aa52011-07-02 13:10:14 -0700320 store_.RegisterDerivedStrings(
321 name,
322 StringsAccessor(new CustomAccessor<Device, Strings>(this, get, set)));
Chris Masone4e851612011-07-01 10:46:53 -0700323}
324
Chris Masone2b105542011-06-22 10:58:09 -0700325void Device::IPConfigUpdatedCallback(const IPConfigRefPtr &ipconfig,
326 bool success) {
Paul Stewarte6132022011-08-16 09:11:02 -0700327 VLOG(2) << __func__ << " " << " success: " << success;
Paul Stewartc39f1132011-06-22 12:02:28 -0700328 if (success) {
Paul Stewarte6132022011-08-16 09:11:02 -0700329 CreateConnection();
330 connection_->UpdateFromIPConfig(ipconfig);
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800331 // SetConnection must occur after the UpdateFromIPConfig so the
332 // service can use the values derived from the connection.
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800333 if (selected_service_.get()) {
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800334 selected_service_->SetConnection(connection_);
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800335 }
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800336 // The service state change needs to happen last, so that at the
337 // time we report the state change to the manager, the service
338 // has its connection.
339 SetServiceState(Service::kStateConnected);
Paul Stewarte6132022011-08-16 09:11:02 -0700340 } else {
341 // TODO(pstew): This logic gets more complex when multiple IPConfig types
342 // are run in parallel (e.g. DHCP and DHCP6)
Paul Stewart03dba0b2011-08-22 16:32:45 -0700343 SetServiceState(Service::kStateDisconnected);
Paul Stewarte6132022011-08-16 09:11:02 -0700344 DestroyConnection();
Paul Stewartc39f1132011-06-22 12:02:28 -0700345 }
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700346}
347
Paul Stewarte6132022011-08-16 09:11:02 -0700348void Device::CreateConnection() {
349 VLOG(2) << __func__;
350 if (!connection_.get()) {
Paul Stewart9a908082011-08-31 12:18:48 -0700351 connection_ = new Connection(interface_index_,
352 link_name_,
353 manager_->device_info());
Paul Stewarte6132022011-08-16 09:11:02 -0700354 }
355}
356
357void Device::DestroyConnection() {
358 VLOG(2) << __func__;
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800359 if (selected_service_.get()) {
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800360 selected_service_->SetConnection(NULL);
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800361 }
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800362 connection_ = NULL;
Paul Stewarte6132022011-08-16 09:11:02 -0700363}
364
Paul Stewart03dba0b2011-08-22 16:32:45 -0700365void Device::SelectService(const ServiceRefPtr &service) {
Paul Stewarta2b8cd12011-08-25 14:55:32 -0700366 VLOG(2) << __func__ << ": "
mukesh agrawal15908392011-11-16 18:29:25 +0000367 << (service.get() ?
368 StringPrintf("%s (%s)",
369 service->UniqueName().c_str(),
370 service->friendly_name().c_str()) :
371 "*reset*");
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000372
373 if (selected_service_.get() == service.get()) {
374 // No change to |selected_service_|. Return early to avoid
375 // changing its state.
376 return;
377 }
378
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800379 if (selected_service_.get()) {
380 if (selected_service_->state() != Service::kStateFailure) {
381 selected_service_->SetState(Service::kStateIdle);
382 }
383 // TODO(pstew): We need to revisit the model here: should the Device
384 // subclass be responsible for calling DestroyIPConfig() (which would
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800385 // trigger DestroyConnection() and Service::SetConnection(NULL))?
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800386 // Ethernet does, but WiFi currently does not. crosbug.com/23929
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800387 selected_service_->SetConnection(NULL);
Paul Stewart03dba0b2011-08-22 16:32:45 -0700388 }
389 selected_service_ = service;
390}
391
392void Device::SetServiceState(Service::ConnectState state) {
393 if (selected_service_.get()) {
394 selected_service_->SetState(state);
395 }
396}
397
398void Device::SetServiceFailure(Service::ConnectFailure failure_state) {
399 if (selected_service_.get()) {
400 selected_service_->SetFailure(failure_state);
401 }
402}
403
Chris Masone34af2182011-08-22 11:59:36 -0700404string Device::SerializeIPConfigs(const string &suffix) {
405 return StringPrintf("%s:%s", suffix.c_str(), ipconfig_->type().c_str());
Chris Masone5dec5f42011-07-22 14:07:55 -0700406}
407
Paul Stewart2bf1d352011-12-06 15:02:55 -0800408bool Device::SetIPFlag(IPAddress::Family family, const string &flag,
409 const string &value) {
410 string ip_version;
411 if (family == IPAddress::kFamilyIPv4) {
412 ip_version = kIPFlagVersion4;
413 } else if (family == IPAddress::kFamilyIPv6) {
414 ip_version = kIPFlagVersion6;
415 } else {
416 NOTIMPLEMENTED();
417 }
418 FilePath flag_file(StringPrintf(kIPFlagTemplate, ip_version.c_str(),
419 link_name_.c_str(), flag.c_str()));
420 VLOG(2) << "Writing " << value << " to flag file " << flag_file.value();
421 if (file_util::WriteFile(flag_file, value.c_str(), value.length()) != 1) {
422 LOG(ERROR) << StringPrintf("IP flag write failed: %s to %s",
423 value.c_str(), flag_file.value().c_str());
424 return false;
425 }
426 return true;
427}
428
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800429vector<string> Device::AvailableIPConfigs(Error */*error*/) {
Chris Masone4e851612011-07-01 10:46:53 -0700430 string id = (ipconfig_.get() ? ipconfig_->GetRpcIdentifier() : string());
431 return vector<string>(1, id);
432}
433
434string Device::GetRpcConnectionIdentifier() {
435 return adaptor_->GetRpcConnectionIdentifier();
436}
437
Paul Stewart75897df2011-04-27 09:05:53 -0700438} // namespace shill