blob: 9ad0b3238d2d535c933608a735958f7390cb7de2 [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"
Gaurav Shah6d2c72d2012-10-16 16:30:44 -070028#include "shill/geolocation_info.h"
Paul Stewartf65320c2011-10-13 14:34:52 -070029#include "shill/http_proxy.h"
Paul Stewart036dba02012-08-07 12:34:41 -070030#include "shill/link_monitor.h"
Christopher Wileyb691efd2012-08-09 13:51:51 -070031#include "shill/logging.h"
Chris Masone8fe2c7e2011-06-09 15:51:19 -070032#include "shill/manager.h"
Thieu Le85e050b2012-03-13 15:04:38 -070033#include "shill/metrics.h"
Chris Masone95207da2011-06-29 16:50:49 -070034#include "shill/property_accessor.h"
Chris Masone2b105542011-06-22 10:58:09 -070035#include "shill/refptr_types.h"
Paul Stewartc39f1132011-06-22 12:02:28 -070036#include "shill/rtnl_handler.h"
Chris Masone2b105542011-06-22 10:58:09 -070037#include "shill/service.h"
Chris Masone5dec5f42011-07-22 14:07:55 -070038#include "shill/store_interface.h"
Gaurav Shah435de2c2011-11-17 19:01:07 -080039#include "shill/technology.h"
Paul Stewart75897df2011-04-27 09:05:53 -070040
Eric Shienbrood3e20a232012-02-16 11:35:56 -050041using base::Bind;
Chris Masone5dec5f42011-07-22 14:07:55 -070042using base::StringPrintf;
Chris Masone8fe2c7e2011-06-09 15:51:19 -070043using std::string;
44using std::vector;
45
Paul Stewart75897df2011-04-27 09:05:53 -070046namespace shill {
Chris Masone5dec5f42011-07-22 14:07:55 -070047
48// static
Paul Stewart2bf1d352011-12-06 15:02:55 -080049const char Device::kIPFlagTemplate[] = "/proc/sys/net/%s/conf/%s/%s";
50// static
51const char Device::kIPFlagVersion4[] = "ipv4";
52// static
53const char Device::kIPFlagVersion6[] = "ipv6";
54// static
55const char Device::kIPFlagDisableIPv6[] = "disable_ipv6";
56// static
57const char Device::kIPFlagUseTempAddr[] = "use_tempaddr";
58// static
59const char Device::kIPFlagUseTempAddrUsedAndDefault[] = "2";
Paul Stewartc8f4bef2011-12-13 09:45:51 -080060// static
61const char Device::kIPFlagReversePathFilter[] = "rp_filter";
62// static
63const char Device::kIPFlagReversePathFilterEnabled[] = "1";
64// static
65const char Device::kIPFlagReversePathFilterLooseMode[] = "2";
Paul Stewart2bf1d352011-12-06 15:02:55 -080066// static
Chris Masone5dec5f42011-07-22 14:07:55 -070067const char Device::kStoragePowered[] = "Powered";
Chris Masone5dec5f42011-07-22 14:07:55 -070068// static
69const char Device::kStorageIPConfigs[] = "IPConfigs";
Paul Stewart6ff27f52012-07-11 06:51:41 -070070// static
71const char Device::kStorageReceiveByteCount[] = "ReceiveByteCount";
72// static
73const char Device::kStorageTransmitByteCount[] = "TransmitByteCount";
Chris Masone5dec5f42011-07-22 14:07:55 -070074
Paul Stewart75897df2011-04-27 09:05:53 -070075Device::Device(ControlInterface *control_interface,
Paul Stewartb50f0b92011-05-16 16:31:42 -070076 EventDispatcher *dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -080077 Metrics *metrics,
Paul Stewartf1ce5d22011-05-19 13:10:20 -070078 Manager *manager,
Darin Petkovafa6fc42011-06-21 16:21:08 -070079 const string &link_name,
Chris Masone626719f2011-08-18 16:58:48 -070080 const string &address,
Gaurav Shah435de2c2011-11-17 19:01:07 -080081 int interface_index,
82 Technology::Identifier technology)
Eric Shienbrood9a245532012-03-07 14:20:39 -050083 : enabled_(false),
84 enabled_persistent_(true),
85 enabled_pending_(enabled_),
Chris Masoneb925cc82011-06-22 15:39:57 -070086 reconnect_(true),
Chris Masone626719f2011-08-18 16:58:48 -070087 hardware_address_(address),
mukesh agrawalf60e4062011-05-27 13:13:41 -070088 interface_index_(interface_index),
89 running_(false),
Darin Petkovafa6fc42011-06-21 16:21:08 -070090 link_name_(link_name),
Chris Masone19e30402011-07-19 15:48:47 -070091 unique_id_(link_name),
Darin Petkovd9661952011-08-03 16:25:42 -070092 control_interface_(control_interface),
93 dispatcher_(dispatcher),
Thieu Le3426c8f2012-01-11 17:35:11 -080094 metrics_(metrics),
Chris Masone7df0c672011-07-15 10:24:54 -070095 manager_(manager),
Eric Shienbrood9a245532012-03-07 14:20:39 -050096 weak_ptr_factory_(this),
Darin Petkov77cb6812011-08-15 16:19:41 -070097 adaptor_(control_interface->CreateDeviceAdaptor(this)),
Eric Shienbrood9a245532012-03-07 14:20:39 -050098 portal_detector_callback_(Bind(&Device::PortalDetectorCallback,
99 weak_ptr_factory_.GetWeakPtr())),
Gaurav Shah435de2c2011-11-17 19:01:07 -0800100 technology_(technology),
Thieu Le85e050b2012-03-13 15:04:38 -0700101 portal_attempts_to_online_(0),
Paul Stewart6ff27f52012-07-11 06:51:41 -0700102 receive_byte_offset_(0),
103 transmit_byte_offset_(0),
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700104 dhcp_provider_(DHCPProvider::GetInstance()),
105 rtnl_handler_(RTNLHandler::GetInstance()) {
Chris Masone27c4aa52011-07-02 13:10:14 -0700106 store_.RegisterConstString(flimflam::kAddressProperty, &hardware_address_);
Chris Masone4d42df82011-07-02 17:09:39 -0700107
108 // flimflam::kBgscanMethodProperty: Registered in WiFi
109 // flimflam::kBgscanShortIntervalProperty: Registered in WiFi
110 // flimflam::kBgscanSignalThresholdProperty: Registered in WiFi
111
112 // flimflam::kCellularAllowRoamingProperty: Registered in Cellular
113 // flimflam::kCarrierProperty: Registered in Cellular
114 // flimflam::kEsnProperty: Registered in Cellular
Darin Petkov3335b372011-08-22 11:05:32 -0700115 // flimflam::kHomeProviderProperty: Registered in Cellular
Chris Masone4d42df82011-07-02 17:09:39 -0700116 // flimflam::kImeiProperty: Registered in Cellular
Ben Chana5e27082012-07-31 14:30:28 -0700117 // flimflam::kIccidProperty: Registered in Cellular
Chris Masone4d42df82011-07-02 17:09:39 -0700118 // flimflam::kImsiProperty: Registered in Cellular
119 // flimflam::kManufacturerProperty: Registered in Cellular
120 // flimflam::kMdnProperty: Registered in Cellular
121 // flimflam::kMeidProperty: Registered in Cellular
122 // flimflam::kMinProperty: Registered in Cellular
123 // flimflam::kModelIDProperty: Registered in Cellular
124 // flimflam::kFirmwareRevisionProperty: Registered in Cellular
125 // flimflam::kHardwareRevisionProperty: Registered in Cellular
126 // flimflam::kPRLVersionProperty: Registered in Cellular
127 // flimflam::kSIMLockStatusProperty: Registered in Cellular
128 // flimflam::kFoundNetworksProperty: Registered in Cellular
Darin Petkove9d12e02011-07-27 15:09:37 -0700129 // flimflam::kDBusConnectionProperty: Registered in Cellular
130 // flimflam::kDBusObjectProperty: Register in Cellular
Chris Masone4d42df82011-07-02 17:09:39 -0700131
Jason Glasgowe8334fd2012-03-30 16:02:37 -0400132 store_.RegisterConstString(flimflam::kInterfaceProperty, &link_name_);
Jason Glasgow08afdff2012-04-03 10:22:26 -0400133 HelpRegisterConstDerivedRpcIdentifiers(flimflam::kIPConfigsProperty,
134 &Device::AvailableIPConfigs);
Chris Masone27c4aa52011-07-02 13:10:14 -0700135 store_.RegisterConstString(flimflam::kNameProperty, &link_name_);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500136 store_.RegisterConstBool(flimflam::kPoweredProperty, &enabled_);
Jason Glasgowb5790052012-01-27 01:03:52 -0500137 HelpRegisterDerivedString(flimflam::kTypeProperty,
138 &Device::GetTechnologyString,
139 NULL);
Paul Stewart036dba02012-08-07 12:34:41 -0700140 HelpRegisterConstDerivedUint64(shill::kLinkMonitorResponseTimeProperty,
141 &Device::GetLinkMonitorResponseTime);
Jason Glasgowb5790052012-01-27 01:03:52 -0500142
Chris Masoneb925cc82011-06-22 15:39:57 -0700143 // TODO(cmasone): Chrome doesn't use this...does anyone?
Chris Masone27c4aa52011-07-02 13:10:14 -0700144 // store_.RegisterConstBool(flimflam::kReconnectProperty, &reconnect_);
Chris Masoneb925cc82011-06-22 15:39:57 -0700145
Chris Masone4e851612011-07-01 10:46:53 -0700146 // TODO(cmasone): Figure out what shill concept maps to flimflam's "Network".
Chris Masoneb925cc82011-06-22 15:39:57 -0700147 // known_properties_.push_back(flimflam::kNetworksProperty);
148
Chris Masone4d42df82011-07-02 17:09:39 -0700149 // flimflam::kScanningProperty: Registered in WiFi, Cellular
150 // flimflam::kScanIntervalProperty: Registered in WiFi, Cellular
Paul Stewart6ff27f52012-07-11 06:51:41 -0700151
152 if (manager_ && manager_->device_info()) { // Unit tests may not have these.
153 manager_->device_info()->GetByteCounts(
154 interface_index_, &receive_byte_offset_, &transmit_byte_offset_);
155 HelpRegisterConstDerivedUint64(shill::kReceiveByteCountProperty,
156 &Device::GetReceiveByteCount);
157 HelpRegisterConstDerivedUint64(shill::kTransmitByteCountProperty,
158 &Device::GetTransmitByteCount);
159 }
160
Darin Petkova0a0efe2012-06-27 12:50:01 +0200161 LOG(INFO) << "Device created: " << link_name_
162 << " index " << interface_index_;
Paul Stewart75897df2011-04-27 09:05:53 -0700163}
164
165Device::~Device() {
Darin Petkova0a0efe2012-06-27 12:50:01 +0200166 LOG(INFO) << "Device destructed: " << link_name_
167 << " index " << interface_index_;
Paul Stewart75897df2011-04-27 09:05:53 -0700168}
169
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700170void Device::LinkEvent(unsigned flags, unsigned change) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700171 SLOG(Device, 2) << "Device " << link_name_
172 << std::showbase << std::hex
173 << " flags " << flags << " changed " << change
174 << std::dec << std::noshowbase;
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700175}
176
Darin Petkovc0865312011-09-16 15:31:20 -0700177void Device::Scan(Error *error) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700178 SLOG(Device, 2) << "Device " << link_name_ << " scan requested.";
Paul Stewartbe005172011-11-02 18:10:29 -0700179 Error::PopulateAndLog(error, Error::kNotSupported,
180 "Device doesn't support scan.");
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700181}
182
Eric Shienbrood9a245532012-03-07 14:20:39 -0500183void Device::RegisterOnNetwork(const std::string &/*network_id*/, Error *error,
184 const ResultCallback &/*callback*/) {
185 Error::PopulateAndLog(error, Error::kNotSupported,
Paul Stewartbe005172011-11-02 18:10:29 -0700186 "Device doesn't support network registration.");
Darin Petkov9ae310f2011-08-30 15:41:13 -0700187}
188
Darin Petkovc64fe5e2012-01-11 12:46:13 +0100189void Device::RequirePIN(
Eric Shienbrood9a245532012-03-07 14:20:39 -0500190 const string &/*pin*/, bool /*require*/,
191 Error *error, const ResultCallback &/*callback*/) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700192 SLOG(Device, 2) << __func__;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500193 Error::PopulateAndLog(error, Error::kNotSupported,
194 "Device doesn't support RequirePIN.");
Darin Petkove42e1012011-08-31 12:35:04 -0700195}
196
Eric Shienbrood9a245532012-03-07 14:20:39 -0500197void Device::EnterPIN(const string &/*pin*/,
198 Error *error, const ResultCallback &/*callback*/) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700199 SLOG(Device, 2) << __func__;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500200 Error::PopulateAndLog(error, Error::kNotSupported,
201 "Device doesn't support EnterPIN.");
Darin Petkove42e1012011-08-31 12:35:04 -0700202}
203
mukesh agrawal1830fa12011-09-26 14:31:40 -0700204void Device::UnblockPIN(const string &/*unblock_code*/,
205 const string &/*pin*/,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500206 Error *error, const ResultCallback &/*callback*/) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700207 SLOG(Device, 2) << __func__;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500208 Error::PopulateAndLog(error, Error::kNotSupported,
209 "Device doesn't support UnblockPIN.");
Darin Petkove42e1012011-08-31 12:35:04 -0700210}
211
mukesh agrawal1830fa12011-09-26 14:31:40 -0700212void Device::ChangePIN(const string &/*old_pin*/,
213 const string &/*new_pin*/,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500214 Error *error, const ResultCallback &/*callback*/) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700215 SLOG(Device, 2) << __func__;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500216 Error::PopulateAndLog(error, Error::kNotSupported,
217 "Device doesn't support ChangePIN.");
Darin Petkove42e1012011-08-31 12:35:04 -0700218}
219
Darin Petkovc37a9c42012-09-06 15:28:22 +0200220void Device::SetCarrier(const string &/*carrier*/,
221 Error *error, const ResultCallback &/*callback*/) {
222 SLOG(Device, 2) << __func__;
223 Error::PopulateAndLog(error, Error::kNotSupported,
224 "Device doesn't support SetCarrier.");
225}
226
Paul Stewart2bf1d352011-12-06 15:02:55 -0800227void Device::DisableIPv6() {
228 SetIPFlag(IPAddress::kFamilyIPv6, kIPFlagDisableIPv6, "1");
229}
230
231void Device::EnableIPv6() {
232 SetIPFlag(IPAddress::kFamilyIPv6, kIPFlagDisableIPv6, "0");
233}
234
235void Device::EnableIPv6Privacy() {
236 SetIPFlag(IPAddress::kFamilyIPv6, kIPFlagUseTempAddr,
237 kIPFlagUseTempAddrUsedAndDefault);
238}
239
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800240void Device::DisableReversePathFilter() {
241 // TODO(pstew): Current kernel doesn't offer reverse-path filtering flag
242 // for IPv6. crosbug.com/24228
243 SetIPFlag(IPAddress::kFamilyIPv4, kIPFlagReversePathFilter,
244 kIPFlagReversePathFilterLooseMode);
245}
246
247void Device::EnableReversePathFilter() {
248 SetIPFlag(IPAddress::kFamilyIPv4, kIPFlagReversePathFilter,
249 kIPFlagReversePathFilterEnabled);
250}
251
Gaurav Shah435de2c2011-11-17 19:01:07 -0800252bool Device::IsConnected() const {
253 if (selected_service_)
254 return selected_service_->IsConnected();
255 return false;
256}
257
Paul Stewartd215af62012-04-24 23:25:50 -0700258bool Device::IsConnectedToService(const ServiceRefPtr &service) const {
259 return service == selected_service_ && IsConnected();
260}
261
Chris Masone27c4aa52011-07-02 13:10:14 -0700262string Device::GetRpcIdentifier() {
263 return adaptor_->GetRpcIdentifier();
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700264}
265
Chris Masone5dec5f42011-07-22 14:07:55 -0700266string Device::GetStorageIdentifier() {
267 string id = GetRpcIdentifier();
268 ControlInterface::RpcIdToStorageId(&id);
Chris Masone626719f2011-08-18 16:58:48 -0700269 size_t needle = id.find('_');
Chris Masone34af2182011-08-22 11:59:36 -0700270 DLOG_IF(ERROR, needle == string::npos) << "No _ in storage id?!?!";
Chris Masone626719f2011-08-18 16:58:48 -0700271 id.replace(id.begin() + needle + 1, id.end(), hardware_address_);
Chris Masone5dec5f42011-07-22 14:07:55 -0700272 return id;
273}
274
Gaurav Shah6d2c72d2012-10-16 16:30:44 -0700275vector<GeolocationInfo> Device::GetGeolocationObjects() const {
276 return vector<GeolocationInfo>();
277};
278
Jason Glasgowb5790052012-01-27 01:03:52 -0500279string Device::GetTechnologyString(Error */*error*/) {
280 return Technology::NameFromIdentifier(technology());
281}
282
Chris Masone19e30402011-07-19 15:48:47 -0700283const string& Device::FriendlyName() const {
Chris Masone7df0c672011-07-15 10:24:54 -0700284 return link_name_;
Darin Petkovafa6fc42011-06-21 16:21:08 -0700285}
286
Chris Masone19e30402011-07-19 15:48:47 -0700287const string& Device::UniqueName() const {
288 return unique_id_;
289}
290
Chris Masone5dec5f42011-07-22 14:07:55 -0700291bool Device::Load(StoreInterface *storage) {
292 const string id = GetStorageIdentifier();
293 if (!storage->ContainsGroup(id)) {
294 LOG(WARNING) << "Device is not available in the persistent store: " << id;
295 return false;
296 }
Eric Shienbrood9a245532012-03-07 14:20:39 -0500297 enabled_persistent_ = true;
298 storage->GetBool(id, kStoragePowered, &enabled_persistent_);
Paul Stewart6ff27f52012-07-11 06:51:41 -0700299 uint64 rx_byte_count = 0, tx_byte_count = 0;
300
301 manager_->device_info()->GetByteCounts(
302 interface_index_, &rx_byte_count, &tx_byte_count);
303 // If there is a byte-count present in the profile, the return value
304 // of Device::Get*ByteCount() should be the this stored value plus
305 // whatever additional bytes we receive since time-of-load. We
306 // accomplish this by the subtractions below, which can validly
307 // roll over "negative" in the subtractions below and in Get*ByteCount.
308 uint64 profile_byte_count;
309 if (storage->GetUint64(id, kStorageReceiveByteCount, &profile_byte_count)) {
310 receive_byte_offset_ = rx_byte_count - profile_byte_count;
311 }
312 if (storage->GetUint64(id, kStorageTransmitByteCount, &profile_byte_count)) {
313 transmit_byte_offset_ = tx_byte_count - profile_byte_count;
314 }
315
Chris Masone5dec5f42011-07-22 14:07:55 -0700316 return true;
317}
318
319bool Device::Save(StoreInterface *storage) {
320 const string id = GetStorageIdentifier();
Eric Shienbrood9a245532012-03-07 14:20:39 -0500321 storage->SetBool(id, kStoragePowered, enabled_persistent_);
Chris Masone34af2182011-08-22 11:59:36 -0700322 if (ipconfig_.get()) {
323 // The _0 is an index into the list of IPConfigs that this device might
324 // have. We only have one IPConfig right now, and I hope to never have
325 // to support more, as sleffler indicates that associating IPConfigs
326 // with devices is wrong and due to be changed in flimflam anyhow.
327 string suffix = hardware_address_ + "_0";
328 ipconfig_->Save(storage, suffix);
329 storage->SetString(id, kStorageIPConfigs, SerializeIPConfigs(suffix));
330 }
Paul Stewart6ff27f52012-07-11 06:51:41 -0700331 storage->SetUint64(id, kStorageReceiveByteCount, GetReceiveByteCount(NULL));
332 storage->SetUint64(id, kStorageTransmitByteCount, GetTransmitByteCount(NULL));
Chris Masone5dec5f42011-07-22 14:07:55 -0700333 return true;
334}
335
mukesh agrawal784566d2012-08-08 18:32:58 -0700336void Device::OnBeforeSuspend() {
337 // Nothing to be done in the general case.
338}
339
340void Device::OnAfterResume() {
341 if (ipconfig_) {
342 SLOG(Device, 3) << "Renewing IP address on resume.";
343 ipconfig_->RenewIP();
344 }
345}
346
Darin Petkov2b8e44e2012-06-25 15:13:26 +0200347void Device::DropConnection() {
348 SLOG(Device, 2) << __func__;
349 DestroyIPConfig();
350 SelectService(NULL);
351}
352
Darin Petkovafa6fc42011-06-21 16:21:08 -0700353void Device::DestroyIPConfig() {
Paul Stewart2bf1d352011-12-06 15:02:55 -0800354 DisableIPv6();
Darin Petkovafa6fc42011-06-21 16:21:08 -0700355 if (ipconfig_.get()) {
356 ipconfig_->ReleaseIP();
357 ipconfig_ = NULL;
358 }
Paul Stewarte6132022011-08-16 09:11:02 -0700359 DestroyConnection();
Darin Petkovafa6fc42011-06-21 16:21:08 -0700360}
361
Paul Stewart2bf1d352011-12-06 15:02:55 -0800362bool Device::AcquireIPConfig() {
Paul Stewartd408fdf2012-05-07 17:15:57 -0700363 return AcquireIPConfigWithLeaseName(string());
364}
365
366bool Device::AcquireIPConfigWithLeaseName(const string &lease_name) {
Darin Petkovafa6fc42011-06-21 16:21:08 -0700367 DestroyIPConfig();
Paul Stewart2bf1d352011-12-06 15:02:55 -0800368 EnableIPv6();
Paul Stewartd408fdf2012-05-07 17:15:57 -0700369 ipconfig_ = dhcp_provider_->CreateConfig(link_name_,
370 manager_->GetHostName(),
371 lease_name,
372 manager_->GetArpGateway());
Eric Shienbrood9a245532012-03-07 14:20:39 -0500373 ipconfig_->RegisterUpdateCallback(Bind(&Device::OnIPConfigUpdated,
374 weak_ptr_factory_.GetWeakPtr()));
Paul Stewart1062d9d2012-04-27 10:42:27 -0700375 dispatcher_->PostTask(Bind(&Device::ConfigureStaticIPTask,
376 weak_ptr_factory_.GetWeakPtr()));
Darin Petkovafa6fc42011-06-21 16:21:08 -0700377 return ipconfig_->RequestIP();
378}
379
Jason Glasgowb5790052012-01-27 01:03:52 -0500380void Device::HelpRegisterDerivedString(
381 const string &name,
382 string(Device::*get)(Error *error),
383 void(Device::*set)(const string &value, Error *error)) {
384 store_.RegisterDerivedString(
385 name,
386 StringAccessor(new CustomAccessor<Device, string>(this, get, set)));
387}
388
mukesh agrawalffa3d042011-10-06 15:26:10 -0700389void Device::HelpRegisterDerivedStrings(
390 const string &name,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500391 Strings(Device::*get)(Error *error),
392 void(Device::*set)(const Strings &value, Error *error)) {
Chris Masone27c4aa52011-07-02 13:10:14 -0700393 store_.RegisterDerivedStrings(
394 name,
395 StringsAccessor(new CustomAccessor<Device, Strings>(this, get, set)));
Chris Masone4e851612011-07-01 10:46:53 -0700396}
397
Jason Glasgow08afdff2012-04-03 10:22:26 -0400398void Device::HelpRegisterConstDerivedRpcIdentifiers(
399 const string &name,
400 RpcIdentifiers(Device::*get)(Error *)) {
401 store_.RegisterDerivedRpcIdentifiers(
402 name,
403 RpcIdentifiersAccessor(
404 new CustomAccessor<Device, RpcIdentifiers>(this, get, NULL)));
405}
406
Paul Stewart6ff27f52012-07-11 06:51:41 -0700407void Device::HelpRegisterConstDerivedUint64(
408 const string &name,
409 uint64(Device::*get)(Error *)) {
410 store_.RegisterDerivedUint64(
411 name,
412 Uint64Accessor(
413 new CustomAccessor<Device, uint64>(this, get, NULL)));
414}
415
Paul Stewart1062d9d2012-04-27 10:42:27 -0700416void Device::ConfigureStaticIPTask() {
417 SLOG(Device, 2) << __func__ << " selected_service " << selected_service_.get()
418 << " ipconfig " << ipconfig_.get();
419
420 if (!selected_service_ || !ipconfig_) {
421 return;
422 }
423
424 const StaticIPParameters &static_ip_parameters =
425 selected_service_->static_ip_parameters();
426 if (static_ip_parameters.ContainsAddress()) {
427 SLOG(Device, 2) << __func__ << " " << " configuring static IP parameters.";
428 // If the parameters contain an IP address, apply them now and bring
429 // the interface up. When DHCP information arrives, it will supplement
430 // the static information.
431 OnIPConfigUpdated(ipconfig_, true);
432 } else {
433 SLOG(Device, 2) << __func__ << " " << " no static IP address.";
434 }
435}
436
Darin Petkov79d74c92012-03-07 17:20:32 +0100437void Device::OnIPConfigUpdated(const IPConfigRefPtr &ipconfig, bool success) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700438 SLOG(Device, 2) << __func__ << " " << " success: " << success;
Paul Stewartc39f1132011-06-22 12:02:28 -0700439 if (success) {
Paul Stewarte6132022011-08-16 09:11:02 -0700440 CreateConnection();
Paul Stewart1062d9d2012-04-27 10:42:27 -0700441 if (selected_service_) {
442 ipconfig->ApplyStaticIPParameters(
Paul Stewartdef189e2012-08-02 20:12:09 -0700443 selected_service_->mutable_static_ip_parameters());
Paul Stewart1062d9d2012-04-27 10:42:27 -0700444 }
Paul Stewarte6132022011-08-16 09:11:02 -0700445 connection_->UpdateFromIPConfig(ipconfig);
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800446 // SetConnection must occur after the UpdateFromIPConfig so the
447 // service can use the values derived from the connection.
Paul Stewart1062d9d2012-04-27 10:42:27 -0700448 if (selected_service_) {
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800449 selected_service_->SetConnection(connection_);
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800450 }
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800451 // The service state change needs to happen last, so that at the
452 // time we report the state change to the manager, the service
453 // has its connection.
454 SetServiceState(Service::kStateConnected);
Thieu Le85e050b2012-03-13 15:04:38 -0700455 portal_attempts_to_online_ = 0;
Paul Stewart20088d82012-02-16 06:58:55 -0800456 // Subtle: Start portal detection after transitioning the service
457 // to the Connected state because this call may immediately transition
458 // to the Online state.
Thieu Led1760922012-09-11 14:15:35 -0700459 if (selected_service_) {
460 StartPortalDetection();
461 }
Paul Stewart036dba02012-08-07 12:34:41 -0700462 StartLinkMonitor();
Paul Stewarte6132022011-08-16 09:11:02 -0700463 } else {
mukesh agrawalcc0fded2012-05-09 13:40:58 -0700464 // TODO(pstew): This logic gets yet more complex when multiple
465 // IPConfig types are run in parallel (e.g. DHCP and DHCP6)
466 if (selected_service_ &&
467 selected_service_->static_ip_parameters().ContainsAddress()) {
468 // Consider three cases:
469 //
470 // 1. We're here because DHCP failed while starting up. There
471 // are two subcases:
472 // a. DHCP has failed, and Static IP config has _not yet_
473 // completed. It's fine to do nothing, because we'll
474 // apply the static config shortly.
475 // b. DHCP has failed, and Static IP config has _already_
476 // completed. It's fine to do nothing, because we can
477 // continue to use the static config that's already
478 // been applied.
479 //
480 // 2. We're here because a previously valid DHCP configuration
481 // is no longer valid. There's still a static IP config,
482 // because the condition in the if clause evaluated to true.
483 // Furthermore, the static config includes an IP address for
484 // us to use.
485 //
486 // The current configuration may include some DHCP
487 // parameters, overriden by any static parameters
488 // provided. We continue to use this configuration, because
489 // the only configuration element that is leased to us (IP
490 // address) will be overriden by a static parameter.
491 return;
492 }
493
Christopher Wileyabd3b502012-09-26 13:08:52 -0700494 if (selected_service_) {
495 Error error;
496 selected_service_->DisconnectWithFailure(Service::kFailureDHCP, &error);
497 }
Paul Stewarte6132022011-08-16 09:11:02 -0700498 DestroyConnection();
Paul Stewartc39f1132011-06-22 12:02:28 -0700499 }
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700500}
501
Paul Stewarte6132022011-08-16 09:11:02 -0700502void Device::CreateConnection() {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700503 SLOG(Device, 2) << __func__;
Paul Stewarte6132022011-08-16 09:11:02 -0700504 if (!connection_.get()) {
Paul Stewartbf667612012-06-29 14:49:54 -0700505 connection_ = new Connection(
506 interface_index_,
507 link_name_,
508 technology_,
509 manager_->device_info(),
510 manager_->IsTechnologyShortDNSTimeoutEnabled(technology_));
Paul Stewarte6132022011-08-16 09:11:02 -0700511 }
512}
513
514void Device::DestroyConnection() {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700515 SLOG(Device, 2) << __func__;
Paul Stewart20088d82012-02-16 06:58:55 -0800516 StopPortalDetection();
Paul Stewart036dba02012-08-07 12:34:41 -0700517 StopLinkMonitor();
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800518 if (selected_service_.get()) {
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800519 selected_service_->SetConnection(NULL);
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800520 }
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800521 connection_ = NULL;
Paul Stewarte6132022011-08-16 09:11:02 -0700522}
523
Paul Stewart03dba0b2011-08-22 16:32:45 -0700524void Device::SelectService(const ServiceRefPtr &service) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700525 SLOG(Device, 2) << __func__ << ": "
526 << (service.get() ?
527 StringPrintf("%s (%s)",
528 service->UniqueName().c_str(),
529 service->friendly_name().c_str()) :
530 "*reset*");
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000531
532 if (selected_service_.get() == service.get()) {
533 // No change to |selected_service_|. Return early to avoid
534 // changing its state.
535 return;
536 }
537
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800538 if (selected_service_.get()) {
539 if (selected_service_->state() != Service::kStateFailure) {
540 selected_service_->SetState(Service::kStateIdle);
541 }
Paul Stewart20b0a092012-05-22 20:39:57 -0700542 // Just in case the Device subclass has not already done so, make
543 // sure the previously selected service has its connection removed.
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800544 selected_service_->SetConnection(NULL);
Paul Stewartc8860612012-09-28 07:36:21 -0700545 StopLinkMonitor();
546 StopPortalDetection();
Paul Stewart03dba0b2011-08-22 16:32:45 -0700547 }
548 selected_service_ = service;
549}
550
551void Device::SetServiceState(Service::ConnectState state) {
552 if (selected_service_.get()) {
553 selected_service_->SetState(state);
554 }
555}
556
557void Device::SetServiceFailure(Service::ConnectFailure failure_state) {
558 if (selected_service_.get()) {
559 selected_service_->SetFailure(failure_state);
560 }
561}
562
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400563void Device::SetServiceFailureSilent(Service::ConnectFailure failure_state) {
564 if (selected_service_.get()) {
565 selected_service_->SetFailureSilent(failure_state);
566 }
567}
568
Chris Masone34af2182011-08-22 11:59:36 -0700569string Device::SerializeIPConfigs(const string &suffix) {
570 return StringPrintf("%s:%s", suffix.c_str(), ipconfig_->type().c_str());
Chris Masone5dec5f42011-07-22 14:07:55 -0700571}
572
Paul Stewart2bf1d352011-12-06 15:02:55 -0800573bool Device::SetIPFlag(IPAddress::Family family, const string &flag,
574 const string &value) {
575 string ip_version;
576 if (family == IPAddress::kFamilyIPv4) {
577 ip_version = kIPFlagVersion4;
578 } else if (family == IPAddress::kFamilyIPv6) {
579 ip_version = kIPFlagVersion6;
580 } else {
581 NOTIMPLEMENTED();
582 }
583 FilePath flag_file(StringPrintf(kIPFlagTemplate, ip_version.c_str(),
584 link_name_.c_str(), flag.c_str()));
Ben Chanfad4a0b2012-04-18 15:49:59 -0700585 SLOG(Device, 2) << "Writing " << value << " to flag file "
586 << flag_file.value();
Paul Stewart2bf1d352011-12-06 15:02:55 -0800587 if (file_util::WriteFile(flag_file, value.c_str(), value.length()) != 1) {
588 LOG(ERROR) << StringPrintf("IP flag write failed: %s to %s",
589 value.c_str(), flag_file.value().c_str());
590 return false;
591 }
592 return true;
593}
594
Paul Stewart6ff27f52012-07-11 06:51:41 -0700595void Device::ResetByteCounters() {
596 manager_->device_info()->GetByteCounts(
597 interface_index_, &receive_byte_offset_, &transmit_byte_offset_);
598 manager_->UpdateDevice(this);
599}
600
Paul Stewartd215af62012-04-24 23:25:50 -0700601bool Device::RestartPortalDetection() {
602 StopPortalDetection();
603 return StartPortalDetection();
604}
605
Paul Stewartc681fa02012-03-02 19:40:04 -0800606bool Device::RequestPortalDetection() {
607 if (!selected_service_) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700608 SLOG(Device, 2) << FriendlyName()
Paul Stewartc681fa02012-03-02 19:40:04 -0800609 << ": No selected service, so no need for portal check.";
610 return false;
611 }
612
613 if (!connection_.get()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700614 SLOG(Device, 2) << FriendlyName()
Paul Stewartc681fa02012-03-02 19:40:04 -0800615 << ": No connection, so no need for portal check.";
616 return false;
617 }
618
619 if (selected_service_->state() != Service::kStatePortal) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700620 SLOG(Device, 2) << FriendlyName()
Paul Stewartc681fa02012-03-02 19:40:04 -0800621 << ": Service is not in portal state. No need to start check.";
622 return false;
623 }
624
625 if (!connection_->is_default()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700626 SLOG(Device, 2) << FriendlyName()
Paul Stewartc681fa02012-03-02 19:40:04 -0800627 << ": Service is not the default connection. Don't start check.";
628 return false;
629 }
630
631 if (portal_detector_.get() && portal_detector_->IsInProgress()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700632 SLOG(Device, 2) << FriendlyName()
633 << ": Portal detection is already running.";
Paul Stewartc681fa02012-03-02 19:40:04 -0800634 return true;
635 }
636
637 return StartPortalDetection();
638}
639
Paul Stewart20088d82012-02-16 06:58:55 -0800640bool Device::StartPortalDetection() {
Paul Stewartd215af62012-04-24 23:25:50 -0700641 DCHECK(selected_service_.get());
642 if (selected_service_->IsPortalDetectionDisabled()) {
643 SLOG(Device, 2) << "Service " << selected_service_->friendly_name()
644 << ": Portal detection is disabled; "
645 << "marking service online.";
646 SetServiceConnectedState(Service::kStateOnline);
647 return false;
648 }
649
650 if (selected_service_->IsPortalDetectionAuto() &&
651 !manager_->IsPortalDetectionEnabled(technology())) {
Paul Stewart20088d82012-02-16 06:58:55 -0800652 // If portal detection is disabled for this technology, immediately set
653 // the service state to "Online".
Ben Chanfad4a0b2012-04-18 15:49:59 -0700654 SLOG(Device, 2) << "Device " << FriendlyName()
655 << ": Portal detection is disabled; "
656 << "marking service online.";
Paul Stewart20088d82012-02-16 06:58:55 -0800657 SetServiceConnectedState(Service::kStateOnline);
658 return false;
659 }
660
Paul Stewart20088d82012-02-16 06:58:55 -0800661 if (selected_service_->HasProxyConfig()) {
662 // Services with HTTP proxy configurations should not be checked by the
663 // connection manager, since we don't have the ability to evaluate
664 // arbitrary proxy configs and their possible credentials.
Ben Chanfad4a0b2012-04-18 15:49:59 -0700665 SLOG(Device, 2) << "Device " << FriendlyName()
666 << ": Service has proxy config; marking it online.";
Paul Stewart20088d82012-02-16 06:58:55 -0800667 SetServiceConnectedState(Service::kStateOnline);
668 return false;
669 }
670
671 portal_detector_.reset(new PortalDetector(connection_,
672 dispatcher_,
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500673 portal_detector_callback_));
Paul Stewart20088d82012-02-16 06:58:55 -0800674 if (!portal_detector_->Start(manager_->GetPortalCheckURL())) {
675 LOG(ERROR) << "Device " << FriendlyName()
676 << ": Portal detection failed to start: likely bad URL: "
677 << manager_->GetPortalCheckURL();
678 SetServiceConnectedState(Service::kStateOnline);
679 return false;
680 }
681
Ben Chanfad4a0b2012-04-18 15:49:59 -0700682 SLOG(Device, 2) << "Device " << FriendlyName()
683 << ": Portal detection has started.";
Paul Stewart20088d82012-02-16 06:58:55 -0800684 return true;
685}
686
687void Device::StopPortalDetection() {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700688 SLOG(Device, 2) << "Device " << FriendlyName()
689 << ": Portal detection has stopped.";
Paul Stewart20088d82012-02-16 06:58:55 -0800690 portal_detector_.reset();
691}
692
Paul Stewart036dba02012-08-07 12:34:41 -0700693void Device::set_link_monitor(LinkMonitor *link_monitor) {
694 link_monitor_.reset(link_monitor);
695}
696
697bool Device::StartLinkMonitor() {
698 if (!manager_->IsTechnologyLinkMonitorEnabled(technology())) {
699 SLOG(Device, 2) << "Device " << FriendlyName()
700 << ": Link Monitoring is disabled.";
701 return false;
702 }
703
704 if (!link_monitor()) {
705 set_link_monitor(
706 new LinkMonitor(
707 connection_, dispatcher_, metrics(), manager_->device_info(),
708 Bind(&Device::OnLinkMonitorFailure, weak_ptr_factory_.GetWeakPtr())));
709 }
710
711 SLOG(Device, 2) << "Device " << FriendlyName()
712 << ": Link Monitor starting.";
713 return link_monitor_->Start();
714}
715
716void Device::StopLinkMonitor() {
717 SLOG(Device, 2) << "Device " << FriendlyName()
718 << ": Link Monitor stopping.";
719 link_monitor_.reset();
720}
721
722void Device::OnLinkMonitorFailure() {
723 LOG(ERROR) << "Device " << FriendlyName()
724 << ": Link Monitor indicates failure.";
725}
726
Paul Stewart20088d82012-02-16 06:58:55 -0800727void Device::SetServiceConnectedState(Service::ConnectState state) {
728 DCHECK(selected_service_.get());
729
730 if (!selected_service_.get()) {
731 LOG(ERROR) << FriendlyName() << ": "
732 << "Portal detection completed but no selected service exists!";
733 return;
734 }
735
736 if (!selected_service_->IsConnected()) {
737 LOG(ERROR) << FriendlyName() << ": "
738 << "Portal detection completed but selected service "
739 << selected_service_->UniqueName()
740 << " is in non-connected state.";
741 return;
742 }
743
Paul Stewartc681fa02012-03-02 19:40:04 -0800744 if (state == Service::kStatePortal && connection_->is_default() &&
745 manager_->GetPortalCheckInterval() != 0) {
746 CHECK(portal_detector_.get());
747 if (!portal_detector_->StartAfterDelay(
748 manager_->GetPortalCheckURL(),
749 manager_->GetPortalCheckInterval())) {
750 LOG(ERROR) << "Device " << FriendlyName()
751 << ": Portal detection failed to restart: likely bad URL: "
752 << manager_->GetPortalCheckURL();
753 SetServiceState(Service::kStateOnline);
754 portal_detector_.reset();
755 return;
756 }
Ben Chanfad4a0b2012-04-18 15:49:59 -0700757 SLOG(Device, 2) << "Device " << FriendlyName()
758 << ": Portal detection retrying.";
Paul Stewartc681fa02012-03-02 19:40:04 -0800759 } else {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700760 SLOG(Device, 2) << "Device " << FriendlyName()
761 << ": Portal will not retry.";
Paul Stewartc681fa02012-03-02 19:40:04 -0800762 portal_detector_.reset();
763 }
764
Paul Stewart20088d82012-02-16 06:58:55 -0800765 SetServiceState(state);
766}
767
768void Device::PortalDetectorCallback(const PortalDetector::Result &result) {
769 if (!result.final) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700770 SLOG(Device, 2) << "Device " << FriendlyName()
771 << ": Received non-final status: "
772 << PortalDetector::StatusToString(result.status);
Paul Stewart20088d82012-02-16 06:58:55 -0800773 return;
774 }
775
Ben Chanfad4a0b2012-04-18 15:49:59 -0700776 SLOG(Device, 2) << "Device " << FriendlyName()
777 << ": Received final status: "
778 << PortalDetector::StatusToString(result.status);
Paul Stewart20088d82012-02-16 06:58:55 -0800779
Thieu Le85e050b2012-03-13 15:04:38 -0700780 portal_attempts_to_online_ += result.num_attempts;
781
782 metrics()->SendEnumToUMA(
783 metrics()->GetFullMetricName(Metrics::kMetricPortalResult, technology()),
784 Metrics::PortalDetectionResultToEnum(result),
785 Metrics::kPortalResultMax);
786
Paul Stewart20088d82012-02-16 06:58:55 -0800787 if (result.status == PortalDetector::kStatusSuccess) {
788 SetServiceConnectedState(Service::kStateOnline);
Thieu Le85e050b2012-03-13 15:04:38 -0700789
790 metrics()->SendToUMA(
791 metrics()->GetFullMetricName(
792 Metrics::kMetricPortalAttemptsToOnline, technology()),
793 portal_attempts_to_online_,
794 Metrics::kMetricPortalAttemptsToOnlineMin,
795 Metrics::kMetricPortalAttemptsToOnlineMax,
796 Metrics::kMetricPortalAttemptsToOnlineNumBuckets);
Paul Stewart20088d82012-02-16 06:58:55 -0800797 } else {
798 SetServiceConnectedState(Service::kStatePortal);
Thieu Le85e050b2012-03-13 15:04:38 -0700799
800 metrics()->SendToUMA(
801 metrics()->GetFullMetricName(
802 Metrics::kMetricPortalAttempts, technology()),
803 result.num_attempts,
804 Metrics::kMetricPortalAttemptsMin,
805 Metrics::kMetricPortalAttemptsMax,
806 Metrics::kMetricPortalAttemptsNumBuckets);
Paul Stewart20088d82012-02-16 06:58:55 -0800807 }
808}
809
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800810vector<string> Device::AvailableIPConfigs(Error */*error*/) {
Jason Glasgow08afdff2012-04-03 10:22:26 -0400811 if (ipconfig_.get()) {
812 string id = ipconfig_->GetRpcIdentifier();
813 return vector<string>(1, id);
814 }
815 return vector<string>();
Chris Masone4e851612011-07-01 10:46:53 -0700816}
817
818string Device::GetRpcConnectionIdentifier() {
819 return adaptor_->GetRpcConnectionIdentifier();
820}
821
Paul Stewart036dba02012-08-07 12:34:41 -0700822uint64 Device::GetLinkMonitorResponseTime(Error *error) {
823 if (!link_monitor_.get()) {
824 // It is not strictly an error that the link monitor does not
825 // exist, but returning an error here allows the GetProperties
826 // call in our Adaptor to omit this parameter.
827 error->Populate(Error::kNotFound, "Device is not running LinkMonitor");
828 return 0;
829 }
830 return link_monitor_->GetResponseTimeMilliseconds();
831}
832
Paul Stewart6ff27f52012-07-11 06:51:41 -0700833uint64 Device::GetReceiveByteCount(Error */*error*/) {
834 uint64 rx_byte_count = 0, tx_byte_count = 0;
835 manager_->device_info()->GetByteCounts(
836 interface_index_, &rx_byte_count, &tx_byte_count);
837 return rx_byte_count - receive_byte_offset_;
838}
839
840uint64 Device::GetTransmitByteCount(Error */*error*/) {
841 uint64 rx_byte_count = 0, tx_byte_count = 0;
842 manager_->device_info()->GetByteCounts(
843 interface_index_, &rx_byte_count, &tx_byte_count);
844 return tx_byte_count - transmit_byte_offset_;
845}
846
Eric Shienbrood7fce52c2012-04-13 19:11:02 -0400847bool Device::IsUnderlyingDeviceEnabled() const {
848 return false;
849}
850
Eric Shienbrood9a245532012-03-07 14:20:39 -0500851// callback
852void Device::OnEnabledStateChanged(const ResultCallback &callback,
853 const Error &error) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700854 SLOG(Device, 2) << __func__ << "(" << enabled_pending_ << ")";
Eric Shienbrood9a245532012-03-07 14:20:39 -0500855 if (error.IsSuccess()) {
856 enabled_ = enabled_pending_;
857 manager_->UpdateEnabledTechnologies();
858 adaptor_->EmitBoolChanged(flimflam::kPoweredProperty, enabled_);
859 adaptor_->UpdateEnabled();
860 }
Gary Morainbaeefdf2012-04-30 14:53:35 -0700861 enabled_pending_ = enabled_;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500862 if (!callback.is_null())
863 callback.Run(error);
864}
865
866void Device::SetEnabled(bool enable) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700867 SLOG(Device, 2) << __func__ << "(" << enable << ")";
Jason Glasgow4a490792012-04-10 15:02:05 -0400868 Error error;
869 SetEnabledInternal(enable, false, &error, ResultCallback());
Jason Glasgow7234ec32012-05-23 16:01:21 -0400870 LOG_IF(ERROR, error.IsFailure() && !error.IsOngoing())
Jason Glasgow4a490792012-04-10 15:02:05 -0400871 << "Enabled failed, but no way to report the failure.";
Eric Shienbrood9a245532012-03-07 14:20:39 -0500872}
873
874void Device::SetEnabledPersistent(bool enable,
875 Error *error,
876 const ResultCallback &callback) {
877 SetEnabledInternal(enable, true, error, callback);
878}
879
880void Device::SetEnabledInternal(bool enable,
881 bool persist,
882 Error *error,
883 const ResultCallback &callback) {
Jason Glasgow4a490792012-04-10 15:02:05 -0400884 DCHECK(error);
Ben Chanfad4a0b2012-04-18 15:49:59 -0700885 SLOG(Device, 2) << "Device " << link_name_ << " "
886 << (enable ? "starting" : "stopping");
Eric Shienbrood9a245532012-03-07 14:20:39 -0500887 if (enable == enabled_) {
Jason Glasgow4a490792012-04-10 15:02:05 -0400888 error->Reset();
Eric Shienbrood9a245532012-03-07 14:20:39 -0500889 return;
890 }
891
892 if (enabled_pending_ == enable) {
Jason Glasgow4a490792012-04-10 15:02:05 -0400893 Error::PopulateAndLog(error, Error::kInProgress,
894 "Enable operation already in progress");
Eric Shienbrood9a245532012-03-07 14:20:39 -0500895 return;
896 }
897
898 if (persist) {
899 enabled_persistent_ = enable;
Darin Petkove7c6ad32012-06-29 10:22:09 +0200900 manager_->UpdateDevice(this);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500901 }
902
903 enabled_pending_ = enable;
904 EnabledStateChangedCallback enabled_callback =
905 Bind(&Device::OnEnabledStateChanged,
906 weak_ptr_factory_.GetWeakPtr(), callback);
907 if (enable) {
908 running_ = true;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500909 Start(error, enabled_callback);
910 } else {
911 running_ = false;
912 DestroyIPConfig(); // breaks a reference cycle
913 SelectService(NULL); // breaks a reference cycle
914 rtnl_handler_->SetInterfaceFlags(interface_index(), 0, IFF_UP);
Ben Chanfad4a0b2012-04-18 15:49:59 -0700915 SLOG(Device, 3) << "Device " << link_name_ << " ipconfig_ "
916 << (ipconfig_ ? "is set." : "is not set.");
917 SLOG(Device, 3) << "Device " << link_name_ << " connection_ "
918 << (connection_ ? "is set." : "is not set.");
919 SLOG(Device, 3) << "Device " << link_name_ << " selected_service_ "
920 << (selected_service_ ? "is set." : "is not set.");
Eric Shienbrood9a245532012-03-07 14:20:39 -0500921 Stop(error, enabled_callback);
922 }
923}
924
Paul Stewart75897df2011-04-27 09:05:53 -0700925} // namespace shill