blob: 68ab81b0c03b1578f1e42a4c59d3fa7f212f4a4b [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
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
mukesh agrawal1830fa12011-09-26 14:31:40 -0700183void Device::RequirePIN(const string &/*pin*/, bool /*require*/, Error *error) {
Paul Stewartbe005172011-11-02 18:10:29 -0700184 Error::PopulateAndLog(error, Error::kNotSupported,
185 "Device doesn't support RequirePIN.");
Darin Petkove42e1012011-08-31 12:35:04 -0700186}
187
Darin Petkove5bc2cb2011-12-07 14:47:32 +0100188void Device::EnterPIN(const string &/*pin*/, ReturnerInterface *returner) {
189 VLOG(2) << __func__;
190 Error error;
191 Error::PopulateAndLog(&error, Error::kNotSupported,
Paul Stewartbe005172011-11-02 18:10:29 -0700192 "Device doesn't support EnterPIN.");
Darin Petkove5bc2cb2011-12-07 14:47:32 +0100193 returner->ReturnError(error);
Darin Petkove42e1012011-08-31 12:35:04 -0700194}
195
mukesh agrawal1830fa12011-09-26 14:31:40 -0700196void Device::UnblockPIN(const string &/*unblock_code*/,
197 const string &/*pin*/,
Darin Petkove42e1012011-08-31 12:35:04 -0700198 Error *error) {
Paul Stewartbe005172011-11-02 18:10:29 -0700199 Error::PopulateAndLog(error, Error::kNotSupported,
200 "Device doesn't support UnblockPIN.");
Darin Petkove42e1012011-08-31 12:35:04 -0700201}
202
mukesh agrawal1830fa12011-09-26 14:31:40 -0700203void Device::ChangePIN(const string &/*old_pin*/,
204 const string &/*new_pin*/,
Darin Petkove42e1012011-08-31 12:35:04 -0700205 Error *error) {
Paul Stewartbe005172011-11-02 18:10:29 -0700206 Error::PopulateAndLog(error, Error::kNotSupported,
207 "Device doesn't support ChangePIN.");
Darin Petkove42e1012011-08-31 12:35:04 -0700208}
209
Paul Stewart2bf1d352011-12-06 15:02:55 -0800210void Device::DisableIPv6() {
211 SetIPFlag(IPAddress::kFamilyIPv6, kIPFlagDisableIPv6, "1");
212}
213
214void Device::EnableIPv6() {
215 SetIPFlag(IPAddress::kFamilyIPv6, kIPFlagDisableIPv6, "0");
216}
217
218void Device::EnableIPv6Privacy() {
219 SetIPFlag(IPAddress::kFamilyIPv6, kIPFlagUseTempAddr,
220 kIPFlagUseTempAddrUsedAndDefault);
221}
222
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800223void Device::DisableReversePathFilter() {
224 // TODO(pstew): Current kernel doesn't offer reverse-path filtering flag
225 // for IPv6. crosbug.com/24228
226 SetIPFlag(IPAddress::kFamilyIPv4, kIPFlagReversePathFilter,
227 kIPFlagReversePathFilterLooseMode);
228}
229
230void Device::EnableReversePathFilter() {
231 SetIPFlag(IPAddress::kFamilyIPv4, kIPFlagReversePathFilter,
232 kIPFlagReversePathFilterEnabled);
233}
234
Gaurav Shah435de2c2011-11-17 19:01:07 -0800235bool Device::IsConnected() const {
236 if (selected_service_)
237 return selected_service_->IsConnected();
238 return false;
239}
240
Chris Masone27c4aa52011-07-02 13:10:14 -0700241string Device::GetRpcIdentifier() {
242 return adaptor_->GetRpcIdentifier();
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700243}
244
Chris Masone5dec5f42011-07-22 14:07:55 -0700245string Device::GetStorageIdentifier() {
246 string id = GetRpcIdentifier();
247 ControlInterface::RpcIdToStorageId(&id);
Chris Masone626719f2011-08-18 16:58:48 -0700248 size_t needle = id.find('_');
Chris Masone34af2182011-08-22 11:59:36 -0700249 DLOG_IF(ERROR, needle == string::npos) << "No _ in storage id?!?!";
Chris Masone626719f2011-08-18 16:58:48 -0700250 id.replace(id.begin() + needle + 1, id.end(), hardware_address_);
Chris Masone5dec5f42011-07-22 14:07:55 -0700251 return id;
252}
253
Chris Masone19e30402011-07-19 15:48:47 -0700254const string& Device::FriendlyName() const {
Chris Masone7df0c672011-07-15 10:24:54 -0700255 return link_name_;
Darin Petkovafa6fc42011-06-21 16:21:08 -0700256}
257
Chris Masone19e30402011-07-19 15:48:47 -0700258const string& Device::UniqueName() const {
259 return unique_id_;
260}
261
Chris Masone5dec5f42011-07-22 14:07:55 -0700262bool Device::Load(StoreInterface *storage) {
263 const string id = GetStorageIdentifier();
264 if (!storage->ContainsGroup(id)) {
265 LOG(WARNING) << "Device is not available in the persistent store: " << id;
266 return false;
267 }
268 storage->GetBool(id, kStoragePowered, &powered_);
269 // TODO(cmasone): What does it mean to load an IPConfig identifier??
270 return true;
271}
272
273bool Device::Save(StoreInterface *storage) {
274 const string id = GetStorageIdentifier();
275 storage->SetBool(id, kStoragePowered, powered_);
Chris Masone34af2182011-08-22 11:59:36 -0700276 if (ipconfig_.get()) {
277 // The _0 is an index into the list of IPConfigs that this device might
278 // have. We only have one IPConfig right now, and I hope to never have
279 // to support more, as sleffler indicates that associating IPConfigs
280 // with devices is wrong and due to be changed in flimflam anyhow.
281 string suffix = hardware_address_ + "_0";
282 ipconfig_->Save(storage, suffix);
283 storage->SetString(id, kStorageIPConfigs, SerializeIPConfigs(suffix));
284 }
Chris Masone5dec5f42011-07-22 14:07:55 -0700285 return true;
286}
287
Darin Petkovafa6fc42011-06-21 16:21:08 -0700288void Device::DestroyIPConfig() {
Paul Stewart2bf1d352011-12-06 15:02:55 -0800289 DisableIPv6();
Darin Petkovafa6fc42011-06-21 16:21:08 -0700290 if (ipconfig_.get()) {
291 ipconfig_->ReleaseIP();
292 ipconfig_ = NULL;
293 }
Paul Stewarte6132022011-08-16 09:11:02 -0700294 DestroyConnection();
Darin Petkovafa6fc42011-06-21 16:21:08 -0700295}
296
Paul Stewart2bf1d352011-12-06 15:02:55 -0800297bool Device::AcquireIPConfig() {
Darin Petkovafa6fc42011-06-21 16:21:08 -0700298 DestroyIPConfig();
Paul Stewart2bf1d352011-12-06 15:02:55 -0800299 EnableIPv6();
Darin Petkov77cb6812011-08-15 16:19:41 -0700300 ipconfig_ = dhcp_provider_->CreateConfig(link_name_);
Darin Petkovafa6fc42011-06-21 16:21:08 -0700301 ipconfig_->RegisterUpdateCallback(
302 NewCallback(this, &Device::IPConfigUpdatedCallback));
303 return ipconfig_->RequestIP();
304}
305
mukesh agrawalffa3d042011-10-06 15:26:10 -0700306void Device::HelpRegisterDerivedStrings(
307 const string &name,
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800308 Strings(Device::*get)(Error *),
mukesh agrawalffa3d042011-10-06 15:26:10 -0700309 void(Device::*set)(const Strings&, Error *)) {
Chris Masone27c4aa52011-07-02 13:10:14 -0700310 store_.RegisterDerivedStrings(
311 name,
312 StringsAccessor(new CustomAccessor<Device, Strings>(this, get, set)));
Chris Masone4e851612011-07-01 10:46:53 -0700313}
314
Chris Masone2b105542011-06-22 10:58:09 -0700315void Device::IPConfigUpdatedCallback(const IPConfigRefPtr &ipconfig,
316 bool success) {
Paul Stewarte6132022011-08-16 09:11:02 -0700317 VLOG(2) << __func__ << " " << " success: " << success;
Paul Stewartc39f1132011-06-22 12:02:28 -0700318 if (success) {
Paul Stewarte6132022011-08-16 09:11:02 -0700319 CreateConnection();
320 connection_->UpdateFromIPConfig(ipconfig);
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800321 // SetConnection must occur after the UpdateFromIPConfig so the
322 // service can use the values derived from the connection.
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800323 if (selected_service_.get()) {
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800324 selected_service_->SetConnection(connection_);
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800325 }
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800326 // The service state change needs to happen last, so that at the
327 // time we report the state change to the manager, the service
328 // has its connection.
329 SetServiceState(Service::kStateConnected);
Paul Stewarte6132022011-08-16 09:11:02 -0700330 } else {
331 // TODO(pstew): This logic gets more complex when multiple IPConfig types
332 // are run in parallel (e.g. DHCP and DHCP6)
Paul Stewart03dba0b2011-08-22 16:32:45 -0700333 SetServiceState(Service::kStateDisconnected);
Paul Stewarte6132022011-08-16 09:11:02 -0700334 DestroyConnection();
Paul Stewartc39f1132011-06-22 12:02:28 -0700335 }
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700336}
337
Paul Stewarte6132022011-08-16 09:11:02 -0700338void Device::CreateConnection() {
339 VLOG(2) << __func__;
340 if (!connection_.get()) {
Paul Stewart9a908082011-08-31 12:18:48 -0700341 connection_ = new Connection(interface_index_,
342 link_name_,
343 manager_->device_info());
Paul Stewarte6132022011-08-16 09:11:02 -0700344 }
345}
346
347void Device::DestroyConnection() {
348 VLOG(2) << __func__;
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800349 if (selected_service_.get()) {
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800350 selected_service_->SetConnection(NULL);
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800351 }
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800352 connection_ = NULL;
Paul Stewarte6132022011-08-16 09:11:02 -0700353}
354
Paul Stewart03dba0b2011-08-22 16:32:45 -0700355void Device::SelectService(const ServiceRefPtr &service) {
Paul Stewarta2b8cd12011-08-25 14:55:32 -0700356 VLOG(2) << __func__ << ": "
mukesh agrawal15908392011-11-16 18:29:25 +0000357 << (service.get() ?
358 StringPrintf("%s (%s)",
359 service->UniqueName().c_str(),
360 service->friendly_name().c_str()) :
361 "*reset*");
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800362 if (selected_service_.get()) {
363 if (selected_service_->state() != Service::kStateFailure) {
364 selected_service_->SetState(Service::kStateIdle);
365 }
366 // TODO(pstew): We need to revisit the model here: should the Device
367 // subclass be responsible for calling DestroyIPConfig() (which would
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800368 // trigger DestroyConnection() and Service::SetConnection(NULL))?
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800369 // Ethernet does, but WiFi currently does not. crosbug.com/23929
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800370 selected_service_->SetConnection(NULL);
Paul Stewart03dba0b2011-08-22 16:32:45 -0700371 }
372 selected_service_ = service;
373}
374
375void Device::SetServiceState(Service::ConnectState state) {
376 if (selected_service_.get()) {
377 selected_service_->SetState(state);
378 }
379}
380
381void Device::SetServiceFailure(Service::ConnectFailure failure_state) {
382 if (selected_service_.get()) {
383 selected_service_->SetFailure(failure_state);
384 }
385}
386
Chris Masone34af2182011-08-22 11:59:36 -0700387string Device::SerializeIPConfigs(const string &suffix) {
388 return StringPrintf("%s:%s", suffix.c_str(), ipconfig_->type().c_str());
Chris Masone5dec5f42011-07-22 14:07:55 -0700389}
390
Paul Stewart2bf1d352011-12-06 15:02:55 -0800391bool Device::SetIPFlag(IPAddress::Family family, const string &flag,
392 const string &value) {
393 string ip_version;
394 if (family == IPAddress::kFamilyIPv4) {
395 ip_version = kIPFlagVersion4;
396 } else if (family == IPAddress::kFamilyIPv6) {
397 ip_version = kIPFlagVersion6;
398 } else {
399 NOTIMPLEMENTED();
400 }
401 FilePath flag_file(StringPrintf(kIPFlagTemplate, ip_version.c_str(),
402 link_name_.c_str(), flag.c_str()));
403 VLOG(2) << "Writing " << value << " to flag file " << flag_file.value();
404 if (file_util::WriteFile(flag_file, value.c_str(), value.length()) != 1) {
405 LOG(ERROR) << StringPrintf("IP flag write failed: %s to %s",
406 value.c_str(), flag_file.value().c_str());
407 return false;
408 }
409 return true;
410}
411
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800412vector<string> Device::AvailableIPConfigs(Error */*error*/) {
Chris Masone4e851612011-07-01 10:46:53 -0700413 string id = (ipconfig_.get() ? ipconfig_->GetRpcIdentifier() : string());
414 return vector<string>(1, id);
415}
416
417string Device::GetRpcConnectionIdentifier() {
418 return adaptor_->GetRpcConnectionIdentifier();
419}
420
Paul Stewart75897df2011-04-27 09:05:53 -0700421} // namespace shill