blob: 9bfd3c86f9f0cf91e5305fe3a42e867894d8e2e0 [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
Eric Shienbrood3e20a232012-02-16 11:35:56 -050015#include <base/bind.h>
Paul Stewart2bf1d352011-12-06 15:02:55 -080016#include <base/file_util.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"
Christopher Wileyb691efd2012-08-09 13:51:51 -070029#include "shill/logging.h"
Chris Masone8fe2c7e2011-06-09 15:51:19 -070030#include "shill/manager.h"
Thieu Le85e050b2012-03-13 15:04:38 -070031#include "shill/metrics.h"
Chris Masone95207da2011-06-29 16:50:49 -070032#include "shill/property_accessor.h"
Chris Masone2b105542011-06-22 10:58:09 -070033#include "shill/refptr_types.h"
Paul Stewartc39f1132011-06-22 12:02:28 -070034#include "shill/rtnl_handler.h"
Chris Masone2b105542011-06-22 10:58:09 -070035#include "shill/service.h"
Chris Masone5dec5f42011-07-22 14:07:55 -070036#include "shill/store_interface.h"
Gaurav Shah435de2c2011-11-17 19:01:07 -080037#include "shill/technology.h"
Paul Stewart75897df2011-04-27 09:05:53 -070038
Eric Shienbrood3e20a232012-02-16 11:35:56 -050039using base::Bind;
Chris Masone5dec5f42011-07-22 14:07:55 -070040using base::StringPrintf;
Chris Masone8fe2c7e2011-06-09 15:51:19 -070041using std::string;
42using std::vector;
43
Paul Stewart75897df2011-04-27 09:05:53 -070044namespace shill {
Chris Masone5dec5f42011-07-22 14:07:55 -070045
46// static
Paul Stewart2bf1d352011-12-06 15:02:55 -080047const char Device::kIPFlagTemplate[] = "/proc/sys/net/%s/conf/%s/%s";
48// static
49const char Device::kIPFlagVersion4[] = "ipv4";
50// static
51const char Device::kIPFlagVersion6[] = "ipv6";
52// static
53const char Device::kIPFlagDisableIPv6[] = "disable_ipv6";
54// static
55const char Device::kIPFlagUseTempAddr[] = "use_tempaddr";
56// static
57const char Device::kIPFlagUseTempAddrUsedAndDefault[] = "2";
Paul Stewartc8f4bef2011-12-13 09:45:51 -080058// static
59const char Device::kIPFlagReversePathFilter[] = "rp_filter";
60// static
61const char Device::kIPFlagReversePathFilterEnabled[] = "1";
62// static
63const char Device::kIPFlagReversePathFilterLooseMode[] = "2";
Paul Stewart2bf1d352011-12-06 15:02:55 -080064// static
Chris Masone5dec5f42011-07-22 14:07:55 -070065const char Device::kStoragePowered[] = "Powered";
Chris Masone5dec5f42011-07-22 14:07:55 -070066// static
67const char Device::kStorageIPConfigs[] = "IPConfigs";
Paul Stewart6ff27f52012-07-11 06:51:41 -070068// static
69const char Device::kStorageReceiveByteCount[] = "ReceiveByteCount";
70// static
71const char Device::kStorageTransmitByteCount[] = "TransmitByteCount";
Chris Masone5dec5f42011-07-22 14:07:55 -070072
Paul Stewart75897df2011-04-27 09:05:53 -070073Device::Device(ControlInterface *control_interface,
Paul Stewartb50f0b92011-05-16 16:31:42 -070074 EventDispatcher *dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -080075 Metrics *metrics,
Paul Stewartf1ce5d22011-05-19 13:10:20 -070076 Manager *manager,
Darin Petkovafa6fc42011-06-21 16:21:08 -070077 const string &link_name,
Chris Masone626719f2011-08-18 16:58:48 -070078 const string &address,
Gaurav Shah435de2c2011-11-17 19:01:07 -080079 int interface_index,
80 Technology::Identifier technology)
Eric Shienbrood9a245532012-03-07 14:20:39 -050081 : enabled_(false),
82 enabled_persistent_(true),
83 enabled_pending_(enabled_),
Chris Masoneb925cc82011-06-22 15:39:57 -070084 reconnect_(true),
Chris Masone626719f2011-08-18 16:58:48 -070085 hardware_address_(address),
mukesh agrawalf60e4062011-05-27 13:13:41 -070086 interface_index_(interface_index),
87 running_(false),
Darin Petkovafa6fc42011-06-21 16:21:08 -070088 link_name_(link_name),
Chris Masone19e30402011-07-19 15:48:47 -070089 unique_id_(link_name),
Darin Petkovd9661952011-08-03 16:25:42 -070090 control_interface_(control_interface),
91 dispatcher_(dispatcher),
Thieu Le3426c8f2012-01-11 17:35:11 -080092 metrics_(metrics),
Chris Masone7df0c672011-07-15 10:24:54 -070093 manager_(manager),
Eric Shienbrood9a245532012-03-07 14:20:39 -050094 weak_ptr_factory_(this),
Darin Petkov77cb6812011-08-15 16:19:41 -070095 adaptor_(control_interface->CreateDeviceAdaptor(this)),
Eric Shienbrood9a245532012-03-07 14:20:39 -050096 portal_detector_callback_(Bind(&Device::PortalDetectorCallback,
97 weak_ptr_factory_.GetWeakPtr())),
Gaurav Shah435de2c2011-11-17 19:01:07 -080098 technology_(technology),
Thieu Le85e050b2012-03-13 15:04:38 -070099 portal_attempts_to_online_(0),
Paul Stewart6ff27f52012-07-11 06:51:41 -0700100 receive_byte_offset_(0),
101 transmit_byte_offset_(0),
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700102 dhcp_provider_(DHCPProvider::GetInstance()),
103 rtnl_handler_(RTNLHandler::GetInstance()) {
Chris Masone27c4aa52011-07-02 13:10:14 -0700104 store_.RegisterConstString(flimflam::kAddressProperty, &hardware_address_);
Chris Masone4d42df82011-07-02 17:09:39 -0700105
106 // flimflam::kBgscanMethodProperty: Registered in WiFi
107 // flimflam::kBgscanShortIntervalProperty: Registered in WiFi
108 // flimflam::kBgscanSignalThresholdProperty: Registered in WiFi
109
110 // flimflam::kCellularAllowRoamingProperty: Registered in Cellular
111 // flimflam::kCarrierProperty: Registered in Cellular
112 // flimflam::kEsnProperty: Registered in Cellular
Darin Petkov3335b372011-08-22 11:05:32 -0700113 // flimflam::kHomeProviderProperty: Registered in Cellular
Chris Masone4d42df82011-07-02 17:09:39 -0700114 // flimflam::kImeiProperty: Registered in Cellular
Ben Chana5e27082012-07-31 14:30:28 -0700115 // flimflam::kIccidProperty: Registered in Cellular
Chris Masone4d42df82011-07-02 17:09:39 -0700116 // flimflam::kImsiProperty: Registered in Cellular
117 // flimflam::kManufacturerProperty: Registered in Cellular
118 // flimflam::kMdnProperty: Registered in Cellular
119 // flimflam::kMeidProperty: Registered in Cellular
120 // flimflam::kMinProperty: Registered in Cellular
121 // flimflam::kModelIDProperty: Registered in Cellular
122 // flimflam::kFirmwareRevisionProperty: Registered in Cellular
123 // flimflam::kHardwareRevisionProperty: Registered in Cellular
124 // flimflam::kPRLVersionProperty: Registered in Cellular
125 // flimflam::kSIMLockStatusProperty: Registered in Cellular
126 // flimflam::kFoundNetworksProperty: Registered in Cellular
Darin Petkove9d12e02011-07-27 15:09:37 -0700127 // flimflam::kDBusConnectionProperty: Registered in Cellular
128 // flimflam::kDBusObjectProperty: Register in Cellular
Chris Masone4d42df82011-07-02 17:09:39 -0700129
Jason Glasgowe8334fd2012-03-30 16:02:37 -0400130 store_.RegisterConstString(flimflam::kInterfaceProperty, &link_name_);
Jason Glasgow08afdff2012-04-03 10:22:26 -0400131 HelpRegisterConstDerivedRpcIdentifiers(flimflam::kIPConfigsProperty,
132 &Device::AvailableIPConfigs);
Chris Masone27c4aa52011-07-02 13:10:14 -0700133 store_.RegisterConstString(flimflam::kNameProperty, &link_name_);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500134 store_.RegisterConstBool(flimflam::kPoweredProperty, &enabled_);
Jason Glasgowb5790052012-01-27 01:03:52 -0500135 HelpRegisterDerivedString(flimflam::kTypeProperty,
136 &Device::GetTechnologyString,
137 NULL);
138
Chris Masoneb925cc82011-06-22 15:39:57 -0700139 // TODO(cmasone): Chrome doesn't use this...does anyone?
Chris Masone27c4aa52011-07-02 13:10:14 -0700140 // store_.RegisterConstBool(flimflam::kReconnectProperty, &reconnect_);
Chris Masoneb925cc82011-06-22 15:39:57 -0700141
Chris Masone4e851612011-07-01 10:46:53 -0700142 // TODO(cmasone): Figure out what shill concept maps to flimflam's "Network".
Chris Masoneb925cc82011-06-22 15:39:57 -0700143 // known_properties_.push_back(flimflam::kNetworksProperty);
144
Chris Masone4d42df82011-07-02 17:09:39 -0700145 // flimflam::kScanningProperty: Registered in WiFi, Cellular
146 // flimflam::kScanIntervalProperty: Registered in WiFi, Cellular
Paul Stewart6ff27f52012-07-11 06:51:41 -0700147
148 if (manager_ && manager_->device_info()) { // Unit tests may not have these.
149 manager_->device_info()->GetByteCounts(
150 interface_index_, &receive_byte_offset_, &transmit_byte_offset_);
151 HelpRegisterConstDerivedUint64(shill::kReceiveByteCountProperty,
152 &Device::GetReceiveByteCount);
153 HelpRegisterConstDerivedUint64(shill::kTransmitByteCountProperty,
154 &Device::GetTransmitByteCount);
155 }
156
Darin Petkova0a0efe2012-06-27 12:50:01 +0200157 LOG(INFO) << "Device created: " << link_name_
158 << " index " << interface_index_;
Paul Stewart75897df2011-04-27 09:05:53 -0700159}
160
161Device::~Device() {
Darin Petkova0a0efe2012-06-27 12:50:01 +0200162 LOG(INFO) << "Device destructed: " << link_name_
163 << " index " << interface_index_;
Paul Stewart75897df2011-04-27 09:05:53 -0700164}
165
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700166void Device::LinkEvent(unsigned flags, unsigned change) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700167 SLOG(Device, 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) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700174 SLOG(Device, 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
Eric Shienbrood9a245532012-03-07 14:20:39 -0500179void Device::RegisterOnNetwork(const std::string &/*network_id*/, Error *error,
180 const ResultCallback &/*callback*/) {
181 Error::PopulateAndLog(error, Error::kNotSupported,
Paul Stewartbe005172011-11-02 18:10:29 -0700182 "Device doesn't support network registration.");
Darin Petkov9ae310f2011-08-30 15:41:13 -0700183}
184
Darin Petkovc64fe5e2012-01-11 12:46:13 +0100185void Device::RequirePIN(
Eric Shienbrood9a245532012-03-07 14:20:39 -0500186 const string &/*pin*/, bool /*require*/,
187 Error *error, const ResultCallback &/*callback*/) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700188 SLOG(Device, 2) << __func__;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500189 Error::PopulateAndLog(error, Error::kNotSupported,
190 "Device doesn't support RequirePIN.");
Darin Petkove42e1012011-08-31 12:35:04 -0700191}
192
Eric Shienbrood9a245532012-03-07 14:20:39 -0500193void Device::EnterPIN(const string &/*pin*/,
194 Error *error, const ResultCallback &/*callback*/) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700195 SLOG(Device, 2) << __func__;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500196 Error::PopulateAndLog(error, Error::kNotSupported,
197 "Device doesn't support EnterPIN.");
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*/,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500202 Error *error, const ResultCallback &/*callback*/) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700203 SLOG(Device, 2) << __func__;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500204 Error::PopulateAndLog(error, Error::kNotSupported,
205 "Device doesn't support UnblockPIN.");
Darin Petkove42e1012011-08-31 12:35:04 -0700206}
207
mukesh agrawal1830fa12011-09-26 14:31:40 -0700208void Device::ChangePIN(const string &/*old_pin*/,
209 const string &/*new_pin*/,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500210 Error *error, const ResultCallback &/*callback*/) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700211 SLOG(Device, 2) << __func__;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500212 Error::PopulateAndLog(error, Error::kNotSupported,
213 "Device doesn't support ChangePIN.");
Darin Petkove42e1012011-08-31 12:35:04 -0700214}
215
Paul Stewart2bf1d352011-12-06 15:02:55 -0800216void Device::DisableIPv6() {
217 SetIPFlag(IPAddress::kFamilyIPv6, kIPFlagDisableIPv6, "1");
218}
219
220void Device::EnableIPv6() {
221 SetIPFlag(IPAddress::kFamilyIPv6, kIPFlagDisableIPv6, "0");
222}
223
224void Device::EnableIPv6Privacy() {
225 SetIPFlag(IPAddress::kFamilyIPv6, kIPFlagUseTempAddr,
226 kIPFlagUseTempAddrUsedAndDefault);
227}
228
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800229void Device::DisableReversePathFilter() {
230 // TODO(pstew): Current kernel doesn't offer reverse-path filtering flag
231 // for IPv6. crosbug.com/24228
232 SetIPFlag(IPAddress::kFamilyIPv4, kIPFlagReversePathFilter,
233 kIPFlagReversePathFilterLooseMode);
234}
235
236void Device::EnableReversePathFilter() {
237 SetIPFlag(IPAddress::kFamilyIPv4, kIPFlagReversePathFilter,
238 kIPFlagReversePathFilterEnabled);
239}
240
Gaurav Shah435de2c2011-11-17 19:01:07 -0800241bool Device::IsConnected() const {
242 if (selected_service_)
243 return selected_service_->IsConnected();
244 return false;
245}
246
Paul Stewartd215af62012-04-24 23:25:50 -0700247bool Device::IsConnectedToService(const ServiceRefPtr &service) const {
248 return service == selected_service_ && IsConnected();
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
Jason Glasgowb5790052012-01-27 01:03:52 -0500264string Device::GetTechnologyString(Error */*error*/) {
265 return Technology::NameFromIdentifier(technology());
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 }
Eric Shienbrood9a245532012-03-07 14:20:39 -0500282 enabled_persistent_ = true;
283 storage->GetBool(id, kStoragePowered, &enabled_persistent_);
Paul Stewart6ff27f52012-07-11 06:51:41 -0700284 uint64 rx_byte_count = 0, tx_byte_count = 0;
285
286 manager_->device_info()->GetByteCounts(
287 interface_index_, &rx_byte_count, &tx_byte_count);
288 // If there is a byte-count present in the profile, the return value
289 // of Device::Get*ByteCount() should be the this stored value plus
290 // whatever additional bytes we receive since time-of-load. We
291 // accomplish this by the subtractions below, which can validly
292 // roll over "negative" in the subtractions below and in Get*ByteCount.
293 uint64 profile_byte_count;
294 if (storage->GetUint64(id, kStorageReceiveByteCount, &profile_byte_count)) {
295 receive_byte_offset_ = rx_byte_count - profile_byte_count;
296 }
297 if (storage->GetUint64(id, kStorageTransmitByteCount, &profile_byte_count)) {
298 transmit_byte_offset_ = tx_byte_count - profile_byte_count;
299 }
300
Chris Masone5dec5f42011-07-22 14:07:55 -0700301 return true;
302}
303
304bool Device::Save(StoreInterface *storage) {
305 const string id = GetStorageIdentifier();
Eric Shienbrood9a245532012-03-07 14:20:39 -0500306 storage->SetBool(id, kStoragePowered, enabled_persistent_);
Chris Masone34af2182011-08-22 11:59:36 -0700307 if (ipconfig_.get()) {
308 // The _0 is an index into the list of IPConfigs that this device might
309 // have. We only have one IPConfig right now, and I hope to never have
310 // to support more, as sleffler indicates that associating IPConfigs
311 // with devices is wrong and due to be changed in flimflam anyhow.
312 string suffix = hardware_address_ + "_0";
313 ipconfig_->Save(storage, suffix);
314 storage->SetString(id, kStorageIPConfigs, SerializeIPConfigs(suffix));
315 }
Paul Stewart6ff27f52012-07-11 06:51:41 -0700316 storage->SetUint64(id, kStorageReceiveByteCount, GetReceiveByteCount(NULL));
317 storage->SetUint64(id, kStorageTransmitByteCount, GetTransmitByteCount(NULL));
Chris Masone5dec5f42011-07-22 14:07:55 -0700318 return true;
319}
320
mukesh agrawal784566d2012-08-08 18:32:58 -0700321void Device::OnBeforeSuspend() {
322 // Nothing to be done in the general case.
323}
324
325void Device::OnAfterResume() {
326 if (ipconfig_) {
327 SLOG(Device, 3) << "Renewing IP address on resume.";
328 ipconfig_->RenewIP();
329 }
330}
331
Darin Petkov2b8e44e2012-06-25 15:13:26 +0200332void Device::DropConnection() {
333 SLOG(Device, 2) << __func__;
334 DestroyIPConfig();
335 SelectService(NULL);
336}
337
Darin Petkovafa6fc42011-06-21 16:21:08 -0700338void Device::DestroyIPConfig() {
Paul Stewart2bf1d352011-12-06 15:02:55 -0800339 DisableIPv6();
Darin Petkovafa6fc42011-06-21 16:21:08 -0700340 if (ipconfig_.get()) {
341 ipconfig_->ReleaseIP();
342 ipconfig_ = NULL;
343 }
Paul Stewarte6132022011-08-16 09:11:02 -0700344 DestroyConnection();
Darin Petkovafa6fc42011-06-21 16:21:08 -0700345}
346
Paul Stewart2bf1d352011-12-06 15:02:55 -0800347bool Device::AcquireIPConfig() {
Paul Stewartd408fdf2012-05-07 17:15:57 -0700348 return AcquireIPConfigWithLeaseName(string());
349}
350
351bool Device::AcquireIPConfigWithLeaseName(const string &lease_name) {
Darin Petkovafa6fc42011-06-21 16:21:08 -0700352 DestroyIPConfig();
Paul Stewart2bf1d352011-12-06 15:02:55 -0800353 EnableIPv6();
Paul Stewartd408fdf2012-05-07 17:15:57 -0700354 ipconfig_ = dhcp_provider_->CreateConfig(link_name_,
355 manager_->GetHostName(),
356 lease_name,
357 manager_->GetArpGateway());
Eric Shienbrood9a245532012-03-07 14:20:39 -0500358 ipconfig_->RegisterUpdateCallback(Bind(&Device::OnIPConfigUpdated,
359 weak_ptr_factory_.GetWeakPtr()));
Paul Stewart1062d9d2012-04-27 10:42:27 -0700360 dispatcher_->PostTask(Bind(&Device::ConfigureStaticIPTask,
361 weak_ptr_factory_.GetWeakPtr()));
Darin Petkovafa6fc42011-06-21 16:21:08 -0700362 return ipconfig_->RequestIP();
363}
364
Jason Glasgowb5790052012-01-27 01:03:52 -0500365void Device::HelpRegisterDerivedString(
366 const string &name,
367 string(Device::*get)(Error *error),
368 void(Device::*set)(const string &value, Error *error)) {
369 store_.RegisterDerivedString(
370 name,
371 StringAccessor(new CustomAccessor<Device, string>(this, get, set)));
372}
373
mukesh agrawalffa3d042011-10-06 15:26:10 -0700374void Device::HelpRegisterDerivedStrings(
375 const string &name,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500376 Strings(Device::*get)(Error *error),
377 void(Device::*set)(const Strings &value, Error *error)) {
Chris Masone27c4aa52011-07-02 13:10:14 -0700378 store_.RegisterDerivedStrings(
379 name,
380 StringsAccessor(new CustomAccessor<Device, Strings>(this, get, set)));
Chris Masone4e851612011-07-01 10:46:53 -0700381}
382
Jason Glasgow08afdff2012-04-03 10:22:26 -0400383void Device::HelpRegisterConstDerivedRpcIdentifiers(
384 const string &name,
385 RpcIdentifiers(Device::*get)(Error *)) {
386 store_.RegisterDerivedRpcIdentifiers(
387 name,
388 RpcIdentifiersAccessor(
389 new CustomAccessor<Device, RpcIdentifiers>(this, get, NULL)));
390}
391
Paul Stewart6ff27f52012-07-11 06:51:41 -0700392void Device::HelpRegisterConstDerivedUint64(
393 const string &name,
394 uint64(Device::*get)(Error *)) {
395 store_.RegisterDerivedUint64(
396 name,
397 Uint64Accessor(
398 new CustomAccessor<Device, uint64>(this, get, NULL)));
399}
400
Paul Stewart1062d9d2012-04-27 10:42:27 -0700401void Device::ConfigureStaticIPTask() {
402 SLOG(Device, 2) << __func__ << " selected_service " << selected_service_.get()
403 << " ipconfig " << ipconfig_.get();
404
405 if (!selected_service_ || !ipconfig_) {
406 return;
407 }
408
409 const StaticIPParameters &static_ip_parameters =
410 selected_service_->static_ip_parameters();
411 if (static_ip_parameters.ContainsAddress()) {
412 SLOG(Device, 2) << __func__ << " " << " configuring static IP parameters.";
413 // If the parameters contain an IP address, apply them now and bring
414 // the interface up. When DHCP information arrives, it will supplement
415 // the static information.
416 OnIPConfigUpdated(ipconfig_, true);
417 } else {
418 SLOG(Device, 2) << __func__ << " " << " no static IP address.";
419 }
420}
421
Darin Petkov79d74c92012-03-07 17:20:32 +0100422void Device::OnIPConfigUpdated(const IPConfigRefPtr &ipconfig, bool success) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700423 SLOG(Device, 2) << __func__ << " " << " success: " << success;
Paul Stewartc39f1132011-06-22 12:02:28 -0700424 if (success) {
Paul Stewarte6132022011-08-16 09:11:02 -0700425 CreateConnection();
Paul Stewart1062d9d2012-04-27 10:42:27 -0700426 if (selected_service_) {
427 ipconfig->ApplyStaticIPParameters(
Paul Stewartdef189e2012-08-02 20:12:09 -0700428 selected_service_->mutable_static_ip_parameters());
Paul Stewart1062d9d2012-04-27 10:42:27 -0700429 }
Paul Stewarte6132022011-08-16 09:11:02 -0700430 connection_->UpdateFromIPConfig(ipconfig);
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800431 // SetConnection must occur after the UpdateFromIPConfig so the
432 // service can use the values derived from the connection.
Paul Stewart1062d9d2012-04-27 10:42:27 -0700433 if (selected_service_) {
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800434 selected_service_->SetConnection(connection_);
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800435 }
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800436 // The service state change needs to happen last, so that at the
437 // time we report the state change to the manager, the service
438 // has its connection.
439 SetServiceState(Service::kStateConnected);
Thieu Le85e050b2012-03-13 15:04:38 -0700440 portal_attempts_to_online_ = 0;
Paul Stewart20088d82012-02-16 06:58:55 -0800441 // Subtle: Start portal detection after transitioning the service
442 // to the Connected state because this call may immediately transition
443 // to the Online state.
444 StartPortalDetection();
Paul Stewarte6132022011-08-16 09:11:02 -0700445 } else {
mukesh agrawalcc0fded2012-05-09 13:40:58 -0700446 // TODO(pstew): This logic gets yet more complex when multiple
447 // IPConfig types are run in parallel (e.g. DHCP and DHCP6)
448 if (selected_service_ &&
449 selected_service_->static_ip_parameters().ContainsAddress()) {
450 // Consider three cases:
451 //
452 // 1. We're here because DHCP failed while starting up. There
453 // are two subcases:
454 // a. DHCP has failed, and Static IP config has _not yet_
455 // completed. It's fine to do nothing, because we'll
456 // apply the static config shortly.
457 // b. DHCP has failed, and Static IP config has _already_
458 // completed. It's fine to do nothing, because we can
459 // continue to use the static config that's already
460 // been applied.
461 //
462 // 2. We're here because a previously valid DHCP configuration
463 // is no longer valid. There's still a static IP config,
464 // because the condition in the if clause evaluated to true.
465 // Furthermore, the static config includes an IP address for
466 // us to use.
467 //
468 // The current configuration may include some DHCP
469 // parameters, overriden by any static parameters
470 // provided. We continue to use this configuration, because
471 // the only configuration element that is leased to us (IP
472 // address) will be overriden by a static parameter.
473 return;
474 }
475
Paul Stewart03dba0b2011-08-22 16:32:45 -0700476 SetServiceState(Service::kStateDisconnected);
Paul Stewarte6132022011-08-16 09:11:02 -0700477 DestroyConnection();
Paul Stewartc39f1132011-06-22 12:02:28 -0700478 }
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700479}
480
Paul Stewarte6132022011-08-16 09:11:02 -0700481void Device::CreateConnection() {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700482 SLOG(Device, 2) << __func__;
Paul Stewarte6132022011-08-16 09:11:02 -0700483 if (!connection_.get()) {
Paul Stewartbf667612012-06-29 14:49:54 -0700484 connection_ = new Connection(
485 interface_index_,
486 link_name_,
487 technology_,
488 manager_->device_info(),
489 manager_->IsTechnologyShortDNSTimeoutEnabled(technology_));
Paul Stewarte6132022011-08-16 09:11:02 -0700490 }
491}
492
493void Device::DestroyConnection() {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700494 SLOG(Device, 2) << __func__;
Paul Stewart20088d82012-02-16 06:58:55 -0800495 StopPortalDetection();
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800496 if (selected_service_.get()) {
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800497 selected_service_->SetConnection(NULL);
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800498 }
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800499 connection_ = NULL;
Paul Stewarte6132022011-08-16 09:11:02 -0700500}
501
Paul Stewart03dba0b2011-08-22 16:32:45 -0700502void Device::SelectService(const ServiceRefPtr &service) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700503 SLOG(Device, 2) << __func__ << ": "
504 << (service.get() ?
505 StringPrintf("%s (%s)",
506 service->UniqueName().c_str(),
507 service->friendly_name().c_str()) :
508 "*reset*");
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000509
510 if (selected_service_.get() == service.get()) {
511 // No change to |selected_service_|. Return early to avoid
512 // changing its state.
513 return;
514 }
515
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800516 if (selected_service_.get()) {
517 if (selected_service_->state() != Service::kStateFailure) {
518 selected_service_->SetState(Service::kStateIdle);
519 }
Paul Stewart20b0a092012-05-22 20:39:57 -0700520 // Just in case the Device subclass has not already done so, make
521 // sure the previously selected service has its connection removed.
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800522 selected_service_->SetConnection(NULL);
Paul Stewart03dba0b2011-08-22 16:32:45 -0700523 }
524 selected_service_ = service;
525}
526
527void Device::SetServiceState(Service::ConnectState state) {
528 if (selected_service_.get()) {
529 selected_service_->SetState(state);
530 }
531}
532
533void Device::SetServiceFailure(Service::ConnectFailure failure_state) {
534 if (selected_service_.get()) {
535 selected_service_->SetFailure(failure_state);
536 }
537}
538
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400539void Device::SetServiceFailureSilent(Service::ConnectFailure failure_state) {
540 if (selected_service_.get()) {
541 selected_service_->SetFailureSilent(failure_state);
542 }
543}
544
Chris Masone34af2182011-08-22 11:59:36 -0700545string Device::SerializeIPConfigs(const string &suffix) {
546 return StringPrintf("%s:%s", suffix.c_str(), ipconfig_->type().c_str());
Chris Masone5dec5f42011-07-22 14:07:55 -0700547}
548
Paul Stewart2bf1d352011-12-06 15:02:55 -0800549bool Device::SetIPFlag(IPAddress::Family family, const string &flag,
550 const string &value) {
551 string ip_version;
552 if (family == IPAddress::kFamilyIPv4) {
553 ip_version = kIPFlagVersion4;
554 } else if (family == IPAddress::kFamilyIPv6) {
555 ip_version = kIPFlagVersion6;
556 } else {
557 NOTIMPLEMENTED();
558 }
559 FilePath flag_file(StringPrintf(kIPFlagTemplate, ip_version.c_str(),
560 link_name_.c_str(), flag.c_str()));
Ben Chanfad4a0b2012-04-18 15:49:59 -0700561 SLOG(Device, 2) << "Writing " << value << " to flag file "
562 << flag_file.value();
Paul Stewart2bf1d352011-12-06 15:02:55 -0800563 if (file_util::WriteFile(flag_file, value.c_str(), value.length()) != 1) {
564 LOG(ERROR) << StringPrintf("IP flag write failed: %s to %s",
565 value.c_str(), flag_file.value().c_str());
566 return false;
567 }
568 return true;
569}
570
Paul Stewart6ff27f52012-07-11 06:51:41 -0700571void Device::ResetByteCounters() {
572 manager_->device_info()->GetByteCounts(
573 interface_index_, &receive_byte_offset_, &transmit_byte_offset_);
574 manager_->UpdateDevice(this);
575}
576
Paul Stewartd215af62012-04-24 23:25:50 -0700577bool Device::RestartPortalDetection() {
578 StopPortalDetection();
579 return StartPortalDetection();
580}
581
Paul Stewartc681fa02012-03-02 19:40:04 -0800582bool Device::RequestPortalDetection() {
583 if (!selected_service_) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700584 SLOG(Device, 2) << FriendlyName()
Paul Stewartc681fa02012-03-02 19:40:04 -0800585 << ": No selected service, so no need for portal check.";
586 return false;
587 }
588
589 if (!connection_.get()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700590 SLOG(Device, 2) << FriendlyName()
Paul Stewartc681fa02012-03-02 19:40:04 -0800591 << ": No connection, so no need for portal check.";
592 return false;
593 }
594
595 if (selected_service_->state() != Service::kStatePortal) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700596 SLOG(Device, 2) << FriendlyName()
Paul Stewartc681fa02012-03-02 19:40:04 -0800597 << ": Service is not in portal state. No need to start check.";
598 return false;
599 }
600
601 if (!connection_->is_default()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700602 SLOG(Device, 2) << FriendlyName()
Paul Stewartc681fa02012-03-02 19:40:04 -0800603 << ": Service is not the default connection. Don't start check.";
604 return false;
605 }
606
607 if (portal_detector_.get() && portal_detector_->IsInProgress()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700608 SLOG(Device, 2) << FriendlyName()
609 << ": Portal detection is already running.";
Paul Stewartc681fa02012-03-02 19:40:04 -0800610 return true;
611 }
612
613 return StartPortalDetection();
614}
615
Paul Stewart20088d82012-02-16 06:58:55 -0800616bool Device::StartPortalDetection() {
Paul Stewartd215af62012-04-24 23:25:50 -0700617 DCHECK(selected_service_.get());
618 if (selected_service_->IsPortalDetectionDisabled()) {
619 SLOG(Device, 2) << "Service " << selected_service_->friendly_name()
620 << ": Portal detection is disabled; "
621 << "marking service online.";
622 SetServiceConnectedState(Service::kStateOnline);
623 return false;
624 }
625
626 if (selected_service_->IsPortalDetectionAuto() &&
627 !manager_->IsPortalDetectionEnabled(technology())) {
Paul Stewart20088d82012-02-16 06:58:55 -0800628 // If portal detection is disabled for this technology, immediately set
629 // the service state to "Online".
Ben Chanfad4a0b2012-04-18 15:49:59 -0700630 SLOG(Device, 2) << "Device " << FriendlyName()
631 << ": Portal detection is disabled; "
632 << "marking service online.";
Paul Stewart20088d82012-02-16 06:58:55 -0800633 SetServiceConnectedState(Service::kStateOnline);
634 return false;
635 }
636
Paul Stewart20088d82012-02-16 06:58:55 -0800637 if (selected_service_->HasProxyConfig()) {
638 // Services with HTTP proxy configurations should not be checked by the
639 // connection manager, since we don't have the ability to evaluate
640 // arbitrary proxy configs and their possible credentials.
Ben Chanfad4a0b2012-04-18 15:49:59 -0700641 SLOG(Device, 2) << "Device " << FriendlyName()
642 << ": Service has proxy config; marking it online.";
Paul Stewart20088d82012-02-16 06:58:55 -0800643 SetServiceConnectedState(Service::kStateOnline);
644 return false;
645 }
646
647 portal_detector_.reset(new PortalDetector(connection_,
648 dispatcher_,
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500649 portal_detector_callback_));
Paul Stewart20088d82012-02-16 06:58:55 -0800650 if (!portal_detector_->Start(manager_->GetPortalCheckURL())) {
651 LOG(ERROR) << "Device " << FriendlyName()
652 << ": Portal detection failed to start: likely bad URL: "
653 << manager_->GetPortalCheckURL();
654 SetServiceConnectedState(Service::kStateOnline);
655 return false;
656 }
657
Ben Chanfad4a0b2012-04-18 15:49:59 -0700658 SLOG(Device, 2) << "Device " << FriendlyName()
659 << ": Portal detection has started.";
Paul Stewart20088d82012-02-16 06:58:55 -0800660 return true;
661}
662
663void Device::StopPortalDetection() {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700664 SLOG(Device, 2) << "Device " << FriendlyName()
665 << ": Portal detection has stopped.";
Paul Stewart20088d82012-02-16 06:58:55 -0800666 portal_detector_.reset();
667}
668
669void Device::SetServiceConnectedState(Service::ConnectState state) {
670 DCHECK(selected_service_.get());
671
672 if (!selected_service_.get()) {
673 LOG(ERROR) << FriendlyName() << ": "
674 << "Portal detection completed but no selected service exists!";
675 return;
676 }
677
678 if (!selected_service_->IsConnected()) {
679 LOG(ERROR) << FriendlyName() << ": "
680 << "Portal detection completed but selected service "
681 << selected_service_->UniqueName()
682 << " is in non-connected state.";
683 return;
684 }
685
Paul Stewartc681fa02012-03-02 19:40:04 -0800686 if (state == Service::kStatePortal && connection_->is_default() &&
687 manager_->GetPortalCheckInterval() != 0) {
688 CHECK(portal_detector_.get());
689 if (!portal_detector_->StartAfterDelay(
690 manager_->GetPortalCheckURL(),
691 manager_->GetPortalCheckInterval())) {
692 LOG(ERROR) << "Device " << FriendlyName()
693 << ": Portal detection failed to restart: likely bad URL: "
694 << manager_->GetPortalCheckURL();
695 SetServiceState(Service::kStateOnline);
696 portal_detector_.reset();
697 return;
698 }
Ben Chanfad4a0b2012-04-18 15:49:59 -0700699 SLOG(Device, 2) << "Device " << FriendlyName()
700 << ": Portal detection retrying.";
Paul Stewartc681fa02012-03-02 19:40:04 -0800701 } else {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700702 SLOG(Device, 2) << "Device " << FriendlyName()
703 << ": Portal will not retry.";
Paul Stewartc681fa02012-03-02 19:40:04 -0800704 portal_detector_.reset();
705 }
706
Paul Stewart20088d82012-02-16 06:58:55 -0800707 SetServiceState(state);
708}
709
710void Device::PortalDetectorCallback(const PortalDetector::Result &result) {
711 if (!result.final) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700712 SLOG(Device, 2) << "Device " << FriendlyName()
713 << ": Received non-final status: "
714 << PortalDetector::StatusToString(result.status);
Paul Stewart20088d82012-02-16 06:58:55 -0800715 return;
716 }
717
Ben Chanfad4a0b2012-04-18 15:49:59 -0700718 SLOG(Device, 2) << "Device " << FriendlyName()
719 << ": Received final status: "
720 << PortalDetector::StatusToString(result.status);
Paul Stewart20088d82012-02-16 06:58:55 -0800721
Thieu Le85e050b2012-03-13 15:04:38 -0700722 portal_attempts_to_online_ += result.num_attempts;
723
724 metrics()->SendEnumToUMA(
725 metrics()->GetFullMetricName(Metrics::kMetricPortalResult, technology()),
726 Metrics::PortalDetectionResultToEnum(result),
727 Metrics::kPortalResultMax);
728
Paul Stewart20088d82012-02-16 06:58:55 -0800729 if (result.status == PortalDetector::kStatusSuccess) {
730 SetServiceConnectedState(Service::kStateOnline);
Thieu Le85e050b2012-03-13 15:04:38 -0700731
732 metrics()->SendToUMA(
733 metrics()->GetFullMetricName(
734 Metrics::kMetricPortalAttemptsToOnline, technology()),
735 portal_attempts_to_online_,
736 Metrics::kMetricPortalAttemptsToOnlineMin,
737 Metrics::kMetricPortalAttemptsToOnlineMax,
738 Metrics::kMetricPortalAttemptsToOnlineNumBuckets);
Paul Stewart20088d82012-02-16 06:58:55 -0800739 } else {
740 SetServiceConnectedState(Service::kStatePortal);
Thieu Le85e050b2012-03-13 15:04:38 -0700741
742 metrics()->SendToUMA(
743 metrics()->GetFullMetricName(
744 Metrics::kMetricPortalAttempts, technology()),
745 result.num_attempts,
746 Metrics::kMetricPortalAttemptsMin,
747 Metrics::kMetricPortalAttemptsMax,
748 Metrics::kMetricPortalAttemptsNumBuckets);
Paul Stewart20088d82012-02-16 06:58:55 -0800749 }
750}
751
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800752vector<string> Device::AvailableIPConfigs(Error */*error*/) {
Jason Glasgow08afdff2012-04-03 10:22:26 -0400753 if (ipconfig_.get()) {
754 string id = ipconfig_->GetRpcIdentifier();
755 return vector<string>(1, id);
756 }
757 return vector<string>();
Chris Masone4e851612011-07-01 10:46:53 -0700758}
759
760string Device::GetRpcConnectionIdentifier() {
761 return adaptor_->GetRpcConnectionIdentifier();
762}
763
Paul Stewart6ff27f52012-07-11 06:51:41 -0700764uint64 Device::GetReceiveByteCount(Error */*error*/) {
765 uint64 rx_byte_count = 0, tx_byte_count = 0;
766 manager_->device_info()->GetByteCounts(
767 interface_index_, &rx_byte_count, &tx_byte_count);
768 return rx_byte_count - receive_byte_offset_;
769}
770
771uint64 Device::GetTransmitByteCount(Error */*error*/) {
772 uint64 rx_byte_count = 0, tx_byte_count = 0;
773 manager_->device_info()->GetByteCounts(
774 interface_index_, &rx_byte_count, &tx_byte_count);
775 return tx_byte_count - transmit_byte_offset_;
776}
777
Eric Shienbrood7fce52c2012-04-13 19:11:02 -0400778bool Device::IsUnderlyingDeviceEnabled() const {
779 return false;
780}
781
Eric Shienbrood9a245532012-03-07 14:20:39 -0500782// callback
783void Device::OnEnabledStateChanged(const ResultCallback &callback,
784 const Error &error) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700785 SLOG(Device, 2) << __func__ << "(" << enabled_pending_ << ")";
Eric Shienbrood9a245532012-03-07 14:20:39 -0500786 if (error.IsSuccess()) {
787 enabled_ = enabled_pending_;
788 manager_->UpdateEnabledTechnologies();
789 adaptor_->EmitBoolChanged(flimflam::kPoweredProperty, enabled_);
790 adaptor_->UpdateEnabled();
791 }
Gary Morainbaeefdf2012-04-30 14:53:35 -0700792 enabled_pending_ = enabled_;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500793 if (!callback.is_null())
794 callback.Run(error);
795}
796
797void Device::SetEnabled(bool enable) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700798 SLOG(Device, 2) << __func__ << "(" << enable << ")";
Jason Glasgow4a490792012-04-10 15:02:05 -0400799 Error error;
800 SetEnabledInternal(enable, false, &error, ResultCallback());
Jason Glasgow7234ec32012-05-23 16:01:21 -0400801 LOG_IF(ERROR, error.IsFailure() && !error.IsOngoing())
Jason Glasgow4a490792012-04-10 15:02:05 -0400802 << "Enabled failed, but no way to report the failure.";
Eric Shienbrood9a245532012-03-07 14:20:39 -0500803}
804
805void Device::SetEnabledPersistent(bool enable,
806 Error *error,
807 const ResultCallback &callback) {
808 SetEnabledInternal(enable, true, error, callback);
809}
810
811void Device::SetEnabledInternal(bool enable,
812 bool persist,
813 Error *error,
814 const ResultCallback &callback) {
Jason Glasgow4a490792012-04-10 15:02:05 -0400815 DCHECK(error);
Ben Chanfad4a0b2012-04-18 15:49:59 -0700816 SLOG(Device, 2) << "Device " << link_name_ << " "
817 << (enable ? "starting" : "stopping");
Eric Shienbrood9a245532012-03-07 14:20:39 -0500818 if (enable == enabled_) {
Jason Glasgow4a490792012-04-10 15:02:05 -0400819 error->Reset();
Eric Shienbrood9a245532012-03-07 14:20:39 -0500820 return;
821 }
822
823 if (enabled_pending_ == enable) {
Jason Glasgow4a490792012-04-10 15:02:05 -0400824 Error::PopulateAndLog(error, Error::kInProgress,
825 "Enable operation already in progress");
Eric Shienbrood9a245532012-03-07 14:20:39 -0500826 return;
827 }
828
829 if (persist) {
830 enabled_persistent_ = enable;
Darin Petkove7c6ad32012-06-29 10:22:09 +0200831 manager_->UpdateDevice(this);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500832 }
833
834 enabled_pending_ = enable;
835 EnabledStateChangedCallback enabled_callback =
836 Bind(&Device::OnEnabledStateChanged,
837 weak_ptr_factory_.GetWeakPtr(), callback);
838 if (enable) {
839 running_ = true;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500840 Start(error, enabled_callback);
841 } else {
842 running_ = false;
843 DestroyIPConfig(); // breaks a reference cycle
844 SelectService(NULL); // breaks a reference cycle
845 rtnl_handler_->SetInterfaceFlags(interface_index(), 0, IFF_UP);
Ben Chanfad4a0b2012-04-18 15:49:59 -0700846 SLOG(Device, 3) << "Device " << link_name_ << " ipconfig_ "
847 << (ipconfig_ ? "is set." : "is not set.");
848 SLOG(Device, 3) << "Device " << link_name_ << " connection_ "
849 << (connection_ ? "is set." : "is not set.");
850 SLOG(Device, 3) << "Device " << link_name_ << " selected_service_ "
851 << (selected_service_ ? "is set." : "is not set.");
Eric Shienbrood9a245532012-03-07 14:20:39 -0500852 Stop(error, enabled_callback);
853 }
854}
855
Paul Stewart75897df2011-04-27 09:05:53 -0700856} // namespace shill