blob: 6086045a90cfc56e95db06c37fda434113fc13ef [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"
Paul Stewart036dba02012-08-07 12:34:41 -070029#include "shill/link_monitor.h"
Christopher Wileyb691efd2012-08-09 13:51:51 -070030#include "shill/logging.h"
Chris Masone8fe2c7e2011-06-09 15:51:19 -070031#include "shill/manager.h"
Thieu Le85e050b2012-03-13 15:04:38 -070032#include "shill/metrics.h"
Chris Masone95207da2011-06-29 16:50:49 -070033#include "shill/property_accessor.h"
Chris Masone2b105542011-06-22 10:58:09 -070034#include "shill/refptr_types.h"
Paul Stewartc39f1132011-06-22 12:02:28 -070035#include "shill/rtnl_handler.h"
Chris Masone2b105542011-06-22 10:58:09 -070036#include "shill/service.h"
Chris Masone5dec5f42011-07-22 14:07:55 -070037#include "shill/store_interface.h"
Gaurav Shah435de2c2011-11-17 19:01:07 -080038#include "shill/technology.h"
Paul Stewart75897df2011-04-27 09:05:53 -070039
Eric Shienbrood3e20a232012-02-16 11:35:56 -050040using base::Bind;
Chris Masone5dec5f42011-07-22 14:07:55 -070041using base::StringPrintf;
Chris Masone8fe2c7e2011-06-09 15:51:19 -070042using std::string;
43using std::vector;
44
Paul Stewart75897df2011-04-27 09:05:53 -070045namespace shill {
Chris Masone5dec5f42011-07-22 14:07:55 -070046
47// static
Paul Stewart2bf1d352011-12-06 15:02:55 -080048const char Device::kIPFlagTemplate[] = "/proc/sys/net/%s/conf/%s/%s";
49// static
50const char Device::kIPFlagVersion4[] = "ipv4";
51// static
52const char Device::kIPFlagVersion6[] = "ipv6";
53// static
54const char Device::kIPFlagDisableIPv6[] = "disable_ipv6";
55// static
56const char Device::kIPFlagUseTempAddr[] = "use_tempaddr";
57// static
58const char Device::kIPFlagUseTempAddrUsedAndDefault[] = "2";
Paul Stewartc8f4bef2011-12-13 09:45:51 -080059// static
60const char Device::kIPFlagReversePathFilter[] = "rp_filter";
61// static
62const char Device::kIPFlagReversePathFilterEnabled[] = "1";
63// static
64const char Device::kIPFlagReversePathFilterLooseMode[] = "2";
Paul Stewart2bf1d352011-12-06 15:02:55 -080065// static
Chris Masone5dec5f42011-07-22 14:07:55 -070066const char Device::kStoragePowered[] = "Powered";
Chris Masone5dec5f42011-07-22 14:07:55 -070067// static
68const char Device::kStorageIPConfigs[] = "IPConfigs";
Paul Stewart6ff27f52012-07-11 06:51:41 -070069// static
70const char Device::kStorageReceiveByteCount[] = "ReceiveByteCount";
71// static
72const char Device::kStorageTransmitByteCount[] = "TransmitByteCount";
Chris Masone5dec5f42011-07-22 14:07:55 -070073
Paul Stewart75897df2011-04-27 09:05:53 -070074Device::Device(ControlInterface *control_interface,
Paul Stewartb50f0b92011-05-16 16:31:42 -070075 EventDispatcher *dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -080076 Metrics *metrics,
Paul Stewartf1ce5d22011-05-19 13:10:20 -070077 Manager *manager,
Darin Petkovafa6fc42011-06-21 16:21:08 -070078 const string &link_name,
Chris Masone626719f2011-08-18 16:58:48 -070079 const string &address,
Gaurav Shah435de2c2011-11-17 19:01:07 -080080 int interface_index,
81 Technology::Identifier technology)
Eric Shienbrood9a245532012-03-07 14:20:39 -050082 : enabled_(false),
83 enabled_persistent_(true),
84 enabled_pending_(enabled_),
Chris Masoneb925cc82011-06-22 15:39:57 -070085 reconnect_(true),
Chris Masone626719f2011-08-18 16:58:48 -070086 hardware_address_(address),
mukesh agrawalf60e4062011-05-27 13:13:41 -070087 interface_index_(interface_index),
88 running_(false),
Darin Petkovafa6fc42011-06-21 16:21:08 -070089 link_name_(link_name),
Chris Masone19e30402011-07-19 15:48:47 -070090 unique_id_(link_name),
Darin Petkovd9661952011-08-03 16:25:42 -070091 control_interface_(control_interface),
92 dispatcher_(dispatcher),
Thieu Le3426c8f2012-01-11 17:35:11 -080093 metrics_(metrics),
Chris Masone7df0c672011-07-15 10:24:54 -070094 manager_(manager),
Eric Shienbrood9a245532012-03-07 14:20:39 -050095 weak_ptr_factory_(this),
Darin Petkov77cb6812011-08-15 16:19:41 -070096 adaptor_(control_interface->CreateDeviceAdaptor(this)),
Eric Shienbrood9a245532012-03-07 14:20:39 -050097 portal_detector_callback_(Bind(&Device::PortalDetectorCallback,
98 weak_ptr_factory_.GetWeakPtr())),
Gaurav Shah435de2c2011-11-17 19:01:07 -080099 technology_(technology),
Thieu Le85e050b2012-03-13 15:04:38 -0700100 portal_attempts_to_online_(0),
Paul Stewart6ff27f52012-07-11 06:51:41 -0700101 receive_byte_offset_(0),
102 transmit_byte_offset_(0),
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700103 dhcp_provider_(DHCPProvider::GetInstance()),
104 rtnl_handler_(RTNLHandler::GetInstance()) {
Chris Masone27c4aa52011-07-02 13:10:14 -0700105 store_.RegisterConstString(flimflam::kAddressProperty, &hardware_address_);
Chris Masone4d42df82011-07-02 17:09:39 -0700106
107 // flimflam::kBgscanMethodProperty: Registered in WiFi
108 // flimflam::kBgscanShortIntervalProperty: Registered in WiFi
109 // flimflam::kBgscanSignalThresholdProperty: Registered in WiFi
110
111 // flimflam::kCellularAllowRoamingProperty: Registered in Cellular
112 // flimflam::kCarrierProperty: Registered in Cellular
113 // flimflam::kEsnProperty: Registered in Cellular
Darin Petkov3335b372011-08-22 11:05:32 -0700114 // flimflam::kHomeProviderProperty: Registered in Cellular
Chris Masone4d42df82011-07-02 17:09:39 -0700115 // flimflam::kImeiProperty: Registered in Cellular
Ben Chana5e27082012-07-31 14:30:28 -0700116 // flimflam::kIccidProperty: Registered in Cellular
Chris Masone4d42df82011-07-02 17:09:39 -0700117 // flimflam::kImsiProperty: Registered in Cellular
118 // flimflam::kManufacturerProperty: Registered in Cellular
119 // flimflam::kMdnProperty: Registered in Cellular
120 // flimflam::kMeidProperty: Registered in Cellular
121 // flimflam::kMinProperty: Registered in Cellular
122 // flimflam::kModelIDProperty: Registered in Cellular
123 // flimflam::kFirmwareRevisionProperty: Registered in Cellular
124 // flimflam::kHardwareRevisionProperty: Registered in Cellular
125 // flimflam::kPRLVersionProperty: Registered in Cellular
126 // flimflam::kSIMLockStatusProperty: Registered in Cellular
127 // flimflam::kFoundNetworksProperty: Registered in Cellular
Darin Petkove9d12e02011-07-27 15:09:37 -0700128 // flimflam::kDBusConnectionProperty: Registered in Cellular
129 // flimflam::kDBusObjectProperty: Register in Cellular
Chris Masone4d42df82011-07-02 17:09:39 -0700130
Jason Glasgowe8334fd2012-03-30 16:02:37 -0400131 store_.RegisterConstString(flimflam::kInterfaceProperty, &link_name_);
Jason Glasgow08afdff2012-04-03 10:22:26 -0400132 HelpRegisterConstDerivedRpcIdentifiers(flimflam::kIPConfigsProperty,
133 &Device::AvailableIPConfigs);
Chris Masone27c4aa52011-07-02 13:10:14 -0700134 store_.RegisterConstString(flimflam::kNameProperty, &link_name_);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500135 store_.RegisterConstBool(flimflam::kPoweredProperty, &enabled_);
Jason Glasgowb5790052012-01-27 01:03:52 -0500136 HelpRegisterDerivedString(flimflam::kTypeProperty,
137 &Device::GetTechnologyString,
138 NULL);
Paul Stewart036dba02012-08-07 12:34:41 -0700139 HelpRegisterConstDerivedUint64(shill::kLinkMonitorResponseTimeProperty,
140 &Device::GetLinkMonitorResponseTime);
Jason Glasgowb5790052012-01-27 01:03:52 -0500141
Chris Masoneb925cc82011-06-22 15:39:57 -0700142 // TODO(cmasone): Chrome doesn't use this...does anyone?
Chris Masone27c4aa52011-07-02 13:10:14 -0700143 // store_.RegisterConstBool(flimflam::kReconnectProperty, &reconnect_);
Chris Masoneb925cc82011-06-22 15:39:57 -0700144
Chris Masone4e851612011-07-01 10:46:53 -0700145 // TODO(cmasone): Figure out what shill concept maps to flimflam's "Network".
Chris Masoneb925cc82011-06-22 15:39:57 -0700146 // known_properties_.push_back(flimflam::kNetworksProperty);
147
Chris Masone4d42df82011-07-02 17:09:39 -0700148 // flimflam::kScanningProperty: Registered in WiFi, Cellular
149 // flimflam::kScanIntervalProperty: Registered in WiFi, Cellular
Paul Stewart6ff27f52012-07-11 06:51:41 -0700150
151 if (manager_ && manager_->device_info()) { // Unit tests may not have these.
152 manager_->device_info()->GetByteCounts(
153 interface_index_, &receive_byte_offset_, &transmit_byte_offset_);
154 HelpRegisterConstDerivedUint64(shill::kReceiveByteCountProperty,
155 &Device::GetReceiveByteCount);
156 HelpRegisterConstDerivedUint64(shill::kTransmitByteCountProperty,
157 &Device::GetTransmitByteCount);
158 }
159
Darin Petkova0a0efe2012-06-27 12:50:01 +0200160 LOG(INFO) << "Device created: " << link_name_
161 << " index " << interface_index_;
Paul Stewart75897df2011-04-27 09:05:53 -0700162}
163
164Device::~Device() {
Darin Petkova0a0efe2012-06-27 12:50:01 +0200165 LOG(INFO) << "Device destructed: " << link_name_
166 << " index " << interface_index_;
Paul Stewart75897df2011-04-27 09:05:53 -0700167}
168
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700169void Device::LinkEvent(unsigned flags, unsigned change) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700170 SLOG(Device, 2) << "Device " << link_name_
171 << std::showbase << std::hex
172 << " flags " << flags << " changed " << change
173 << std::dec << std::noshowbase;
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700174}
175
Darin Petkovc0865312011-09-16 15:31:20 -0700176void Device::Scan(Error *error) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700177 SLOG(Device, 2) << "Device " << link_name_ << " scan requested.";
Paul Stewartbe005172011-11-02 18:10:29 -0700178 Error::PopulateAndLog(error, Error::kNotSupported,
179 "Device doesn't support scan.");
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700180}
181
Eric Shienbrood9a245532012-03-07 14:20:39 -0500182void Device::RegisterOnNetwork(const std::string &/*network_id*/, Error *error,
183 const ResultCallback &/*callback*/) {
184 Error::PopulateAndLog(error, Error::kNotSupported,
Paul Stewartbe005172011-11-02 18:10:29 -0700185 "Device doesn't support network registration.");
Darin Petkov9ae310f2011-08-30 15:41:13 -0700186}
187
Darin Petkovc64fe5e2012-01-11 12:46:13 +0100188void Device::RequirePIN(
Eric Shienbrood9a245532012-03-07 14:20:39 -0500189 const string &/*pin*/, bool /*require*/,
190 Error *error, const ResultCallback &/*callback*/) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700191 SLOG(Device, 2) << __func__;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500192 Error::PopulateAndLog(error, Error::kNotSupported,
193 "Device doesn't support RequirePIN.");
Darin Petkove42e1012011-08-31 12:35:04 -0700194}
195
Eric Shienbrood9a245532012-03-07 14:20:39 -0500196void Device::EnterPIN(const string &/*pin*/,
197 Error *error, const ResultCallback &/*callback*/) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700198 SLOG(Device, 2) << __func__;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500199 Error::PopulateAndLog(error, Error::kNotSupported,
200 "Device doesn't support EnterPIN.");
Darin Petkove42e1012011-08-31 12:35:04 -0700201}
202
mukesh agrawal1830fa12011-09-26 14:31:40 -0700203void Device::UnblockPIN(const string &/*unblock_code*/,
204 const string &/*pin*/,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500205 Error *error, const ResultCallback &/*callback*/) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700206 SLOG(Device, 2) << __func__;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500207 Error::PopulateAndLog(error, Error::kNotSupported,
208 "Device doesn't support UnblockPIN.");
Darin Petkove42e1012011-08-31 12:35:04 -0700209}
210
mukesh agrawal1830fa12011-09-26 14:31:40 -0700211void Device::ChangePIN(const string &/*old_pin*/,
212 const string &/*new_pin*/,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500213 Error *error, const ResultCallback &/*callback*/) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700214 SLOG(Device, 2) << __func__;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500215 Error::PopulateAndLog(error, Error::kNotSupported,
216 "Device doesn't support ChangePIN.");
Darin Petkove42e1012011-08-31 12:35:04 -0700217}
218
Paul Stewart2bf1d352011-12-06 15:02:55 -0800219void Device::DisableIPv6() {
220 SetIPFlag(IPAddress::kFamilyIPv6, kIPFlagDisableIPv6, "1");
221}
222
223void Device::EnableIPv6() {
224 SetIPFlag(IPAddress::kFamilyIPv6, kIPFlagDisableIPv6, "0");
225}
226
227void Device::EnableIPv6Privacy() {
228 SetIPFlag(IPAddress::kFamilyIPv6, kIPFlagUseTempAddr,
229 kIPFlagUseTempAddrUsedAndDefault);
230}
231
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800232void Device::DisableReversePathFilter() {
233 // TODO(pstew): Current kernel doesn't offer reverse-path filtering flag
234 // for IPv6. crosbug.com/24228
235 SetIPFlag(IPAddress::kFamilyIPv4, kIPFlagReversePathFilter,
236 kIPFlagReversePathFilterLooseMode);
237}
238
239void Device::EnableReversePathFilter() {
240 SetIPFlag(IPAddress::kFamilyIPv4, kIPFlagReversePathFilter,
241 kIPFlagReversePathFilterEnabled);
242}
243
Gaurav Shah435de2c2011-11-17 19:01:07 -0800244bool Device::IsConnected() const {
245 if (selected_service_)
246 return selected_service_->IsConnected();
247 return false;
248}
249
Paul Stewartd215af62012-04-24 23:25:50 -0700250bool Device::IsConnectedToService(const ServiceRefPtr &service) const {
251 return service == selected_service_ && IsConnected();
252}
253
Chris Masone27c4aa52011-07-02 13:10:14 -0700254string Device::GetRpcIdentifier() {
255 return adaptor_->GetRpcIdentifier();
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700256}
257
Chris Masone5dec5f42011-07-22 14:07:55 -0700258string Device::GetStorageIdentifier() {
259 string id = GetRpcIdentifier();
260 ControlInterface::RpcIdToStorageId(&id);
Chris Masone626719f2011-08-18 16:58:48 -0700261 size_t needle = id.find('_');
Chris Masone34af2182011-08-22 11:59:36 -0700262 DLOG_IF(ERROR, needle == string::npos) << "No _ in storage id?!?!";
Chris Masone626719f2011-08-18 16:58:48 -0700263 id.replace(id.begin() + needle + 1, id.end(), hardware_address_);
Chris Masone5dec5f42011-07-22 14:07:55 -0700264 return id;
265}
266
Jason Glasgowb5790052012-01-27 01:03:52 -0500267string Device::GetTechnologyString(Error */*error*/) {
268 return Technology::NameFromIdentifier(technology());
269}
270
Chris Masone19e30402011-07-19 15:48:47 -0700271const string& Device::FriendlyName() const {
Chris Masone7df0c672011-07-15 10:24:54 -0700272 return link_name_;
Darin Petkovafa6fc42011-06-21 16:21:08 -0700273}
274
Chris Masone19e30402011-07-19 15:48:47 -0700275const string& Device::UniqueName() const {
276 return unique_id_;
277}
278
Chris Masone5dec5f42011-07-22 14:07:55 -0700279bool Device::Load(StoreInterface *storage) {
280 const string id = GetStorageIdentifier();
281 if (!storage->ContainsGroup(id)) {
282 LOG(WARNING) << "Device is not available in the persistent store: " << id;
283 return false;
284 }
Eric Shienbrood9a245532012-03-07 14:20:39 -0500285 enabled_persistent_ = true;
286 storage->GetBool(id, kStoragePowered, &enabled_persistent_);
Paul Stewart6ff27f52012-07-11 06:51:41 -0700287 uint64 rx_byte_count = 0, tx_byte_count = 0;
288
289 manager_->device_info()->GetByteCounts(
290 interface_index_, &rx_byte_count, &tx_byte_count);
291 // If there is a byte-count present in the profile, the return value
292 // of Device::Get*ByteCount() should be the this stored value plus
293 // whatever additional bytes we receive since time-of-load. We
294 // accomplish this by the subtractions below, which can validly
295 // roll over "negative" in the subtractions below and in Get*ByteCount.
296 uint64 profile_byte_count;
297 if (storage->GetUint64(id, kStorageReceiveByteCount, &profile_byte_count)) {
298 receive_byte_offset_ = rx_byte_count - profile_byte_count;
299 }
300 if (storage->GetUint64(id, kStorageTransmitByteCount, &profile_byte_count)) {
301 transmit_byte_offset_ = tx_byte_count - profile_byte_count;
302 }
303
Chris Masone5dec5f42011-07-22 14:07:55 -0700304 return true;
305}
306
307bool Device::Save(StoreInterface *storage) {
308 const string id = GetStorageIdentifier();
Eric Shienbrood9a245532012-03-07 14:20:39 -0500309 storage->SetBool(id, kStoragePowered, enabled_persistent_);
Chris Masone34af2182011-08-22 11:59:36 -0700310 if (ipconfig_.get()) {
311 // The _0 is an index into the list of IPConfigs that this device might
312 // have. We only have one IPConfig right now, and I hope to never have
313 // to support more, as sleffler indicates that associating IPConfigs
314 // with devices is wrong and due to be changed in flimflam anyhow.
315 string suffix = hardware_address_ + "_0";
316 ipconfig_->Save(storage, suffix);
317 storage->SetString(id, kStorageIPConfigs, SerializeIPConfigs(suffix));
318 }
Paul Stewart6ff27f52012-07-11 06:51:41 -0700319 storage->SetUint64(id, kStorageReceiveByteCount, GetReceiveByteCount(NULL));
320 storage->SetUint64(id, kStorageTransmitByteCount, GetTransmitByteCount(NULL));
Chris Masone5dec5f42011-07-22 14:07:55 -0700321 return true;
322}
323
mukesh agrawal784566d2012-08-08 18:32:58 -0700324void Device::OnBeforeSuspend() {
325 // Nothing to be done in the general case.
326}
327
328void Device::OnAfterResume() {
329 if (ipconfig_) {
330 SLOG(Device, 3) << "Renewing IP address on resume.";
331 ipconfig_->RenewIP();
332 }
333}
334
Darin Petkov2b8e44e2012-06-25 15:13:26 +0200335void Device::DropConnection() {
336 SLOG(Device, 2) << __func__;
337 DestroyIPConfig();
338 SelectService(NULL);
339}
340
Darin Petkovafa6fc42011-06-21 16:21:08 -0700341void Device::DestroyIPConfig() {
Paul Stewart2bf1d352011-12-06 15:02:55 -0800342 DisableIPv6();
Darin Petkovafa6fc42011-06-21 16:21:08 -0700343 if (ipconfig_.get()) {
344 ipconfig_->ReleaseIP();
345 ipconfig_ = NULL;
346 }
Paul Stewarte6132022011-08-16 09:11:02 -0700347 DestroyConnection();
Darin Petkovafa6fc42011-06-21 16:21:08 -0700348}
349
Paul Stewart2bf1d352011-12-06 15:02:55 -0800350bool Device::AcquireIPConfig() {
Paul Stewartd408fdf2012-05-07 17:15:57 -0700351 return AcquireIPConfigWithLeaseName(string());
352}
353
354bool Device::AcquireIPConfigWithLeaseName(const string &lease_name) {
Darin Petkovafa6fc42011-06-21 16:21:08 -0700355 DestroyIPConfig();
Paul Stewart2bf1d352011-12-06 15:02:55 -0800356 EnableIPv6();
Paul Stewartd408fdf2012-05-07 17:15:57 -0700357 ipconfig_ = dhcp_provider_->CreateConfig(link_name_,
358 manager_->GetHostName(),
359 lease_name,
360 manager_->GetArpGateway());
Eric Shienbrood9a245532012-03-07 14:20:39 -0500361 ipconfig_->RegisterUpdateCallback(Bind(&Device::OnIPConfigUpdated,
362 weak_ptr_factory_.GetWeakPtr()));
Paul Stewart1062d9d2012-04-27 10:42:27 -0700363 dispatcher_->PostTask(Bind(&Device::ConfigureStaticIPTask,
364 weak_ptr_factory_.GetWeakPtr()));
Darin Petkovafa6fc42011-06-21 16:21:08 -0700365 return ipconfig_->RequestIP();
366}
367
Jason Glasgowb5790052012-01-27 01:03:52 -0500368void Device::HelpRegisterDerivedString(
369 const string &name,
370 string(Device::*get)(Error *error),
371 void(Device::*set)(const string &value, Error *error)) {
372 store_.RegisterDerivedString(
373 name,
374 StringAccessor(new CustomAccessor<Device, string>(this, get, set)));
375}
376
mukesh agrawalffa3d042011-10-06 15:26:10 -0700377void Device::HelpRegisterDerivedStrings(
378 const string &name,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500379 Strings(Device::*get)(Error *error),
380 void(Device::*set)(const Strings &value, Error *error)) {
Chris Masone27c4aa52011-07-02 13:10:14 -0700381 store_.RegisterDerivedStrings(
382 name,
383 StringsAccessor(new CustomAccessor<Device, Strings>(this, get, set)));
Chris Masone4e851612011-07-01 10:46:53 -0700384}
385
Jason Glasgow08afdff2012-04-03 10:22:26 -0400386void Device::HelpRegisterConstDerivedRpcIdentifiers(
387 const string &name,
388 RpcIdentifiers(Device::*get)(Error *)) {
389 store_.RegisterDerivedRpcIdentifiers(
390 name,
391 RpcIdentifiersAccessor(
392 new CustomAccessor<Device, RpcIdentifiers>(this, get, NULL)));
393}
394
Paul Stewart6ff27f52012-07-11 06:51:41 -0700395void Device::HelpRegisterConstDerivedUint64(
396 const string &name,
397 uint64(Device::*get)(Error *)) {
398 store_.RegisterDerivedUint64(
399 name,
400 Uint64Accessor(
401 new CustomAccessor<Device, uint64>(this, get, NULL)));
402}
403
Paul Stewart1062d9d2012-04-27 10:42:27 -0700404void Device::ConfigureStaticIPTask() {
405 SLOG(Device, 2) << __func__ << " selected_service " << selected_service_.get()
406 << " ipconfig " << ipconfig_.get();
407
408 if (!selected_service_ || !ipconfig_) {
409 return;
410 }
411
412 const StaticIPParameters &static_ip_parameters =
413 selected_service_->static_ip_parameters();
414 if (static_ip_parameters.ContainsAddress()) {
415 SLOG(Device, 2) << __func__ << " " << " configuring static IP parameters.";
416 // If the parameters contain an IP address, apply them now and bring
417 // the interface up. When DHCP information arrives, it will supplement
418 // the static information.
419 OnIPConfigUpdated(ipconfig_, true);
420 } else {
421 SLOG(Device, 2) << __func__ << " " << " no static IP address.";
422 }
423}
424
Darin Petkov79d74c92012-03-07 17:20:32 +0100425void Device::OnIPConfigUpdated(const IPConfigRefPtr &ipconfig, bool success) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700426 SLOG(Device, 2) << __func__ << " " << " success: " << success;
Paul Stewartc39f1132011-06-22 12:02:28 -0700427 if (success) {
Paul Stewarte6132022011-08-16 09:11:02 -0700428 CreateConnection();
Paul Stewart1062d9d2012-04-27 10:42:27 -0700429 if (selected_service_) {
430 ipconfig->ApplyStaticIPParameters(
Paul Stewartdef189e2012-08-02 20:12:09 -0700431 selected_service_->mutable_static_ip_parameters());
Paul Stewart1062d9d2012-04-27 10:42:27 -0700432 }
Paul Stewarte6132022011-08-16 09:11:02 -0700433 connection_->UpdateFromIPConfig(ipconfig);
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800434 // SetConnection must occur after the UpdateFromIPConfig so the
435 // service can use the values derived from the connection.
Paul Stewart1062d9d2012-04-27 10:42:27 -0700436 if (selected_service_) {
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800437 selected_service_->SetConnection(connection_);
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800438 }
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800439 // The service state change needs to happen last, so that at the
440 // time we report the state change to the manager, the service
441 // has its connection.
442 SetServiceState(Service::kStateConnected);
Thieu Le85e050b2012-03-13 15:04:38 -0700443 portal_attempts_to_online_ = 0;
Paul Stewart20088d82012-02-16 06:58:55 -0800444 // Subtle: Start portal detection after transitioning the service
445 // to the Connected state because this call may immediately transition
446 // to the Online state.
447 StartPortalDetection();
Paul Stewart036dba02012-08-07 12:34:41 -0700448 StartLinkMonitor();
Paul Stewarte6132022011-08-16 09:11:02 -0700449 } else {
mukesh agrawalcc0fded2012-05-09 13:40:58 -0700450 // TODO(pstew): This logic gets yet more complex when multiple
451 // IPConfig types are run in parallel (e.g. DHCP and DHCP6)
452 if (selected_service_ &&
453 selected_service_->static_ip_parameters().ContainsAddress()) {
454 // Consider three cases:
455 //
456 // 1. We're here because DHCP failed while starting up. There
457 // are two subcases:
458 // a. DHCP has failed, and Static IP config has _not yet_
459 // completed. It's fine to do nothing, because we'll
460 // apply the static config shortly.
461 // b. DHCP has failed, and Static IP config has _already_
462 // completed. It's fine to do nothing, because we can
463 // continue to use the static config that's already
464 // been applied.
465 //
466 // 2. We're here because a previously valid DHCP configuration
467 // is no longer valid. There's still a static IP config,
468 // because the condition in the if clause evaluated to true.
469 // Furthermore, the static config includes an IP address for
470 // us to use.
471 //
472 // The current configuration may include some DHCP
473 // parameters, overriden by any static parameters
474 // provided. We continue to use this configuration, because
475 // the only configuration element that is leased to us (IP
476 // address) will be overriden by a static parameter.
477 return;
478 }
479
Paul Stewart03dba0b2011-08-22 16:32:45 -0700480 SetServiceState(Service::kStateDisconnected);
Paul Stewarte6132022011-08-16 09:11:02 -0700481 DestroyConnection();
Paul Stewartc39f1132011-06-22 12:02:28 -0700482 }
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700483}
484
Paul Stewarte6132022011-08-16 09:11:02 -0700485void Device::CreateConnection() {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700486 SLOG(Device, 2) << __func__;
Paul Stewarte6132022011-08-16 09:11:02 -0700487 if (!connection_.get()) {
Paul Stewartbf667612012-06-29 14:49:54 -0700488 connection_ = new Connection(
489 interface_index_,
490 link_name_,
491 technology_,
492 manager_->device_info(),
493 manager_->IsTechnologyShortDNSTimeoutEnabled(technology_));
Paul Stewarte6132022011-08-16 09:11:02 -0700494 }
495}
496
497void Device::DestroyConnection() {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700498 SLOG(Device, 2) << __func__;
Paul Stewart20088d82012-02-16 06:58:55 -0800499 StopPortalDetection();
Paul Stewart036dba02012-08-07 12:34:41 -0700500 StopLinkMonitor();
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800501 if (selected_service_.get()) {
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800502 selected_service_->SetConnection(NULL);
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800503 }
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800504 connection_ = NULL;
Paul Stewarte6132022011-08-16 09:11:02 -0700505}
506
Paul Stewart03dba0b2011-08-22 16:32:45 -0700507void Device::SelectService(const ServiceRefPtr &service) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700508 SLOG(Device, 2) << __func__ << ": "
509 << (service.get() ?
510 StringPrintf("%s (%s)",
511 service->UniqueName().c_str(),
512 service->friendly_name().c_str()) :
513 "*reset*");
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000514
515 if (selected_service_.get() == service.get()) {
516 // No change to |selected_service_|. Return early to avoid
517 // changing its state.
518 return;
519 }
520
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800521 if (selected_service_.get()) {
522 if (selected_service_->state() != Service::kStateFailure) {
523 selected_service_->SetState(Service::kStateIdle);
524 }
Paul Stewart20b0a092012-05-22 20:39:57 -0700525 // Just in case the Device subclass has not already done so, make
526 // sure the previously selected service has its connection removed.
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800527 selected_service_->SetConnection(NULL);
Paul Stewart03dba0b2011-08-22 16:32:45 -0700528 }
529 selected_service_ = service;
530}
531
532void Device::SetServiceState(Service::ConnectState state) {
533 if (selected_service_.get()) {
534 selected_service_->SetState(state);
535 }
536}
537
538void Device::SetServiceFailure(Service::ConnectFailure failure_state) {
539 if (selected_service_.get()) {
540 selected_service_->SetFailure(failure_state);
541 }
542}
543
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400544void Device::SetServiceFailureSilent(Service::ConnectFailure failure_state) {
545 if (selected_service_.get()) {
546 selected_service_->SetFailureSilent(failure_state);
547 }
548}
549
Chris Masone34af2182011-08-22 11:59:36 -0700550string Device::SerializeIPConfigs(const string &suffix) {
551 return StringPrintf("%s:%s", suffix.c_str(), ipconfig_->type().c_str());
Chris Masone5dec5f42011-07-22 14:07:55 -0700552}
553
Paul Stewart2bf1d352011-12-06 15:02:55 -0800554bool Device::SetIPFlag(IPAddress::Family family, const string &flag,
555 const string &value) {
556 string ip_version;
557 if (family == IPAddress::kFamilyIPv4) {
558 ip_version = kIPFlagVersion4;
559 } else if (family == IPAddress::kFamilyIPv6) {
560 ip_version = kIPFlagVersion6;
561 } else {
562 NOTIMPLEMENTED();
563 }
564 FilePath flag_file(StringPrintf(kIPFlagTemplate, ip_version.c_str(),
565 link_name_.c_str(), flag.c_str()));
Ben Chanfad4a0b2012-04-18 15:49:59 -0700566 SLOG(Device, 2) << "Writing " << value << " to flag file "
567 << flag_file.value();
Paul Stewart2bf1d352011-12-06 15:02:55 -0800568 if (file_util::WriteFile(flag_file, value.c_str(), value.length()) != 1) {
569 LOG(ERROR) << StringPrintf("IP flag write failed: %s to %s",
570 value.c_str(), flag_file.value().c_str());
571 return false;
572 }
573 return true;
574}
575
Paul Stewart6ff27f52012-07-11 06:51:41 -0700576void Device::ResetByteCounters() {
577 manager_->device_info()->GetByteCounts(
578 interface_index_, &receive_byte_offset_, &transmit_byte_offset_);
579 manager_->UpdateDevice(this);
580}
581
Paul Stewartd215af62012-04-24 23:25:50 -0700582bool Device::RestartPortalDetection() {
583 StopPortalDetection();
584 return StartPortalDetection();
585}
586
Paul Stewartc681fa02012-03-02 19:40:04 -0800587bool Device::RequestPortalDetection() {
588 if (!selected_service_) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700589 SLOG(Device, 2) << FriendlyName()
Paul Stewartc681fa02012-03-02 19:40:04 -0800590 << ": No selected service, so no need for portal check.";
591 return false;
592 }
593
594 if (!connection_.get()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700595 SLOG(Device, 2) << FriendlyName()
Paul Stewartc681fa02012-03-02 19:40:04 -0800596 << ": No connection, so no need for portal check.";
597 return false;
598 }
599
600 if (selected_service_->state() != Service::kStatePortal) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700601 SLOG(Device, 2) << FriendlyName()
Paul Stewartc681fa02012-03-02 19:40:04 -0800602 << ": Service is not in portal state. No need to start check.";
603 return false;
604 }
605
606 if (!connection_->is_default()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700607 SLOG(Device, 2) << FriendlyName()
Paul Stewartc681fa02012-03-02 19:40:04 -0800608 << ": Service is not the default connection. Don't start check.";
609 return false;
610 }
611
612 if (portal_detector_.get() && portal_detector_->IsInProgress()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700613 SLOG(Device, 2) << FriendlyName()
614 << ": Portal detection is already running.";
Paul Stewartc681fa02012-03-02 19:40:04 -0800615 return true;
616 }
617
618 return StartPortalDetection();
619}
620
Paul Stewart20088d82012-02-16 06:58:55 -0800621bool Device::StartPortalDetection() {
Paul Stewartd215af62012-04-24 23:25:50 -0700622 DCHECK(selected_service_.get());
623 if (selected_service_->IsPortalDetectionDisabled()) {
624 SLOG(Device, 2) << "Service " << selected_service_->friendly_name()
625 << ": Portal detection is disabled; "
626 << "marking service online.";
627 SetServiceConnectedState(Service::kStateOnline);
628 return false;
629 }
630
631 if (selected_service_->IsPortalDetectionAuto() &&
632 !manager_->IsPortalDetectionEnabled(technology())) {
Paul Stewart20088d82012-02-16 06:58:55 -0800633 // If portal detection is disabled for this technology, immediately set
634 // the service state to "Online".
Ben Chanfad4a0b2012-04-18 15:49:59 -0700635 SLOG(Device, 2) << "Device " << FriendlyName()
636 << ": Portal detection is disabled; "
637 << "marking service online.";
Paul Stewart20088d82012-02-16 06:58:55 -0800638 SetServiceConnectedState(Service::kStateOnline);
639 return false;
640 }
641
Paul Stewart20088d82012-02-16 06:58:55 -0800642 if (selected_service_->HasProxyConfig()) {
643 // Services with HTTP proxy configurations should not be checked by the
644 // connection manager, since we don't have the ability to evaluate
645 // arbitrary proxy configs and their possible credentials.
Ben Chanfad4a0b2012-04-18 15:49:59 -0700646 SLOG(Device, 2) << "Device " << FriendlyName()
647 << ": Service has proxy config; marking it online.";
Paul Stewart20088d82012-02-16 06:58:55 -0800648 SetServiceConnectedState(Service::kStateOnline);
649 return false;
650 }
651
652 portal_detector_.reset(new PortalDetector(connection_,
653 dispatcher_,
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500654 portal_detector_callback_));
Paul Stewart20088d82012-02-16 06:58:55 -0800655 if (!portal_detector_->Start(manager_->GetPortalCheckURL())) {
656 LOG(ERROR) << "Device " << FriendlyName()
657 << ": Portal detection failed to start: likely bad URL: "
658 << manager_->GetPortalCheckURL();
659 SetServiceConnectedState(Service::kStateOnline);
660 return false;
661 }
662
Ben Chanfad4a0b2012-04-18 15:49:59 -0700663 SLOG(Device, 2) << "Device " << FriendlyName()
664 << ": Portal detection has started.";
Paul Stewart20088d82012-02-16 06:58:55 -0800665 return true;
666}
667
668void Device::StopPortalDetection() {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700669 SLOG(Device, 2) << "Device " << FriendlyName()
670 << ": Portal detection has stopped.";
Paul Stewart20088d82012-02-16 06:58:55 -0800671 portal_detector_.reset();
672}
673
Paul Stewart036dba02012-08-07 12:34:41 -0700674void Device::set_link_monitor(LinkMonitor *link_monitor) {
675 link_monitor_.reset(link_monitor);
676}
677
678bool Device::StartLinkMonitor() {
679 if (!manager_->IsTechnologyLinkMonitorEnabled(technology())) {
680 SLOG(Device, 2) << "Device " << FriendlyName()
681 << ": Link Monitoring is disabled.";
682 return false;
683 }
684
685 if (!link_monitor()) {
686 set_link_monitor(
687 new LinkMonitor(
688 connection_, dispatcher_, metrics(), manager_->device_info(),
689 Bind(&Device::OnLinkMonitorFailure, weak_ptr_factory_.GetWeakPtr())));
690 }
691
692 SLOG(Device, 2) << "Device " << FriendlyName()
693 << ": Link Monitor starting.";
694 return link_monitor_->Start();
695}
696
697void Device::StopLinkMonitor() {
698 SLOG(Device, 2) << "Device " << FriendlyName()
699 << ": Link Monitor stopping.";
700 link_monitor_.reset();
701}
702
703void Device::OnLinkMonitorFailure() {
704 LOG(ERROR) << "Device " << FriendlyName()
705 << ": Link Monitor indicates failure.";
706}
707
Paul Stewart20088d82012-02-16 06:58:55 -0800708void Device::SetServiceConnectedState(Service::ConnectState state) {
709 DCHECK(selected_service_.get());
710
711 if (!selected_service_.get()) {
712 LOG(ERROR) << FriendlyName() << ": "
713 << "Portal detection completed but no selected service exists!";
714 return;
715 }
716
717 if (!selected_service_->IsConnected()) {
718 LOG(ERROR) << FriendlyName() << ": "
719 << "Portal detection completed but selected service "
720 << selected_service_->UniqueName()
721 << " is in non-connected state.";
722 return;
723 }
724
Paul Stewartc681fa02012-03-02 19:40:04 -0800725 if (state == Service::kStatePortal && connection_->is_default() &&
726 manager_->GetPortalCheckInterval() != 0) {
727 CHECK(portal_detector_.get());
728 if (!portal_detector_->StartAfterDelay(
729 manager_->GetPortalCheckURL(),
730 manager_->GetPortalCheckInterval())) {
731 LOG(ERROR) << "Device " << FriendlyName()
732 << ": Portal detection failed to restart: likely bad URL: "
733 << manager_->GetPortalCheckURL();
734 SetServiceState(Service::kStateOnline);
735 portal_detector_.reset();
736 return;
737 }
Ben Chanfad4a0b2012-04-18 15:49:59 -0700738 SLOG(Device, 2) << "Device " << FriendlyName()
739 << ": Portal detection retrying.";
Paul Stewartc681fa02012-03-02 19:40:04 -0800740 } else {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700741 SLOG(Device, 2) << "Device " << FriendlyName()
742 << ": Portal will not retry.";
Paul Stewartc681fa02012-03-02 19:40:04 -0800743 portal_detector_.reset();
744 }
745
Paul Stewart20088d82012-02-16 06:58:55 -0800746 SetServiceState(state);
747}
748
749void Device::PortalDetectorCallback(const PortalDetector::Result &result) {
750 if (!result.final) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700751 SLOG(Device, 2) << "Device " << FriendlyName()
752 << ": Received non-final status: "
753 << PortalDetector::StatusToString(result.status);
Paul Stewart20088d82012-02-16 06:58:55 -0800754 return;
755 }
756
Ben Chanfad4a0b2012-04-18 15:49:59 -0700757 SLOG(Device, 2) << "Device " << FriendlyName()
758 << ": Received final status: "
759 << PortalDetector::StatusToString(result.status);
Paul Stewart20088d82012-02-16 06:58:55 -0800760
Thieu Le85e050b2012-03-13 15:04:38 -0700761 portal_attempts_to_online_ += result.num_attempts;
762
763 metrics()->SendEnumToUMA(
764 metrics()->GetFullMetricName(Metrics::kMetricPortalResult, technology()),
765 Metrics::PortalDetectionResultToEnum(result),
766 Metrics::kPortalResultMax);
767
Paul Stewart20088d82012-02-16 06:58:55 -0800768 if (result.status == PortalDetector::kStatusSuccess) {
769 SetServiceConnectedState(Service::kStateOnline);
Thieu Le85e050b2012-03-13 15:04:38 -0700770
771 metrics()->SendToUMA(
772 metrics()->GetFullMetricName(
773 Metrics::kMetricPortalAttemptsToOnline, technology()),
774 portal_attempts_to_online_,
775 Metrics::kMetricPortalAttemptsToOnlineMin,
776 Metrics::kMetricPortalAttemptsToOnlineMax,
777 Metrics::kMetricPortalAttemptsToOnlineNumBuckets);
Paul Stewart20088d82012-02-16 06:58:55 -0800778 } else {
779 SetServiceConnectedState(Service::kStatePortal);
Thieu Le85e050b2012-03-13 15:04:38 -0700780
781 metrics()->SendToUMA(
782 metrics()->GetFullMetricName(
783 Metrics::kMetricPortalAttempts, technology()),
784 result.num_attempts,
785 Metrics::kMetricPortalAttemptsMin,
786 Metrics::kMetricPortalAttemptsMax,
787 Metrics::kMetricPortalAttemptsNumBuckets);
Paul Stewart20088d82012-02-16 06:58:55 -0800788 }
789}
790
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800791vector<string> Device::AvailableIPConfigs(Error */*error*/) {
Jason Glasgow08afdff2012-04-03 10:22:26 -0400792 if (ipconfig_.get()) {
793 string id = ipconfig_->GetRpcIdentifier();
794 return vector<string>(1, id);
795 }
796 return vector<string>();
Chris Masone4e851612011-07-01 10:46:53 -0700797}
798
799string Device::GetRpcConnectionIdentifier() {
800 return adaptor_->GetRpcConnectionIdentifier();
801}
802
Paul Stewart036dba02012-08-07 12:34:41 -0700803uint64 Device::GetLinkMonitorResponseTime(Error *error) {
804 if (!link_monitor_.get()) {
805 // It is not strictly an error that the link monitor does not
806 // exist, but returning an error here allows the GetProperties
807 // call in our Adaptor to omit this parameter.
808 error->Populate(Error::kNotFound, "Device is not running LinkMonitor");
809 return 0;
810 }
811 return link_monitor_->GetResponseTimeMilliseconds();
812}
813
Paul Stewart6ff27f52012-07-11 06:51:41 -0700814uint64 Device::GetReceiveByteCount(Error */*error*/) {
815 uint64 rx_byte_count = 0, tx_byte_count = 0;
816 manager_->device_info()->GetByteCounts(
817 interface_index_, &rx_byte_count, &tx_byte_count);
818 return rx_byte_count - receive_byte_offset_;
819}
820
821uint64 Device::GetTransmitByteCount(Error */*error*/) {
822 uint64 rx_byte_count = 0, tx_byte_count = 0;
823 manager_->device_info()->GetByteCounts(
824 interface_index_, &rx_byte_count, &tx_byte_count);
825 return tx_byte_count - transmit_byte_offset_;
826}
827
Eric Shienbrood7fce52c2012-04-13 19:11:02 -0400828bool Device::IsUnderlyingDeviceEnabled() const {
829 return false;
830}
831
Eric Shienbrood9a245532012-03-07 14:20:39 -0500832// callback
833void Device::OnEnabledStateChanged(const ResultCallback &callback,
834 const Error &error) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700835 SLOG(Device, 2) << __func__ << "(" << enabled_pending_ << ")";
Eric Shienbrood9a245532012-03-07 14:20:39 -0500836 if (error.IsSuccess()) {
837 enabled_ = enabled_pending_;
838 manager_->UpdateEnabledTechnologies();
839 adaptor_->EmitBoolChanged(flimflam::kPoweredProperty, enabled_);
840 adaptor_->UpdateEnabled();
841 }
Gary Morainbaeefdf2012-04-30 14:53:35 -0700842 enabled_pending_ = enabled_;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500843 if (!callback.is_null())
844 callback.Run(error);
845}
846
847void Device::SetEnabled(bool enable) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700848 SLOG(Device, 2) << __func__ << "(" << enable << ")";
Jason Glasgow4a490792012-04-10 15:02:05 -0400849 Error error;
850 SetEnabledInternal(enable, false, &error, ResultCallback());
Jason Glasgow7234ec32012-05-23 16:01:21 -0400851 LOG_IF(ERROR, error.IsFailure() && !error.IsOngoing())
Jason Glasgow4a490792012-04-10 15:02:05 -0400852 << "Enabled failed, but no way to report the failure.";
Eric Shienbrood9a245532012-03-07 14:20:39 -0500853}
854
855void Device::SetEnabledPersistent(bool enable,
856 Error *error,
857 const ResultCallback &callback) {
858 SetEnabledInternal(enable, true, error, callback);
859}
860
861void Device::SetEnabledInternal(bool enable,
862 bool persist,
863 Error *error,
864 const ResultCallback &callback) {
Jason Glasgow4a490792012-04-10 15:02:05 -0400865 DCHECK(error);
Ben Chanfad4a0b2012-04-18 15:49:59 -0700866 SLOG(Device, 2) << "Device " << link_name_ << " "
867 << (enable ? "starting" : "stopping");
Eric Shienbrood9a245532012-03-07 14:20:39 -0500868 if (enable == enabled_) {
Jason Glasgow4a490792012-04-10 15:02:05 -0400869 error->Reset();
Eric Shienbrood9a245532012-03-07 14:20:39 -0500870 return;
871 }
872
873 if (enabled_pending_ == enable) {
Jason Glasgow4a490792012-04-10 15:02:05 -0400874 Error::PopulateAndLog(error, Error::kInProgress,
875 "Enable operation already in progress");
Eric Shienbrood9a245532012-03-07 14:20:39 -0500876 return;
877 }
878
879 if (persist) {
880 enabled_persistent_ = enable;
Darin Petkove7c6ad32012-06-29 10:22:09 +0200881 manager_->UpdateDevice(this);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500882 }
883
884 enabled_pending_ = enable;
885 EnabledStateChangedCallback enabled_callback =
886 Bind(&Device::OnEnabledStateChanged,
887 weak_ptr_factory_.GetWeakPtr(), callback);
888 if (enable) {
889 running_ = true;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500890 Start(error, enabled_callback);
891 } else {
892 running_ = false;
893 DestroyIPConfig(); // breaks a reference cycle
894 SelectService(NULL); // breaks a reference cycle
895 rtnl_handler_->SetInterfaceFlags(interface_index(), 0, IFF_UP);
Ben Chanfad4a0b2012-04-18 15:49:59 -0700896 SLOG(Device, 3) << "Device " << link_name_ << " ipconfig_ "
897 << (ipconfig_ ? "is set." : "is not set.");
898 SLOG(Device, 3) << "Device " << link_name_ << " connection_ "
899 << (connection_ ? "is set." : "is not set.");
900 SLOG(Device, 3) << "Device " << link_name_ << " selected_service_ "
901 << (selected_service_ ? "is set." : "is not set.");
Eric Shienbrood9a245532012-03-07 14:20:39 -0500902 Stop(error, enabled_callback);
903 }
904}
905
Paul Stewart75897df2011-04-27 09:05:53 -0700906} // namespace shill