blob: e030aaa2f1fcced447c5089ae73adefe41dfda17 [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
Ben Chanad663e12013-01-08 01:58:47 -0800220void Device::Reset(Error *error, const ResultCallback &/*callback*/) {
221 SLOG(Device, 2) << __func__;
222 Error::PopulateAndLog(error, Error::kNotSupported,
223 "Device doesn't support Reset.");
224}
225
Darin Petkovc37a9c42012-09-06 15:28:22 +0200226void Device::SetCarrier(const string &/*carrier*/,
227 Error *error, const ResultCallback &/*callback*/) {
228 SLOG(Device, 2) << __func__;
229 Error::PopulateAndLog(error, Error::kNotSupported,
230 "Device doesn't support SetCarrier.");
231}
232
Paul Stewart2bf1d352011-12-06 15:02:55 -0800233void Device::DisableIPv6() {
234 SetIPFlag(IPAddress::kFamilyIPv6, kIPFlagDisableIPv6, "1");
235}
236
237void Device::EnableIPv6() {
238 SetIPFlag(IPAddress::kFamilyIPv6, kIPFlagDisableIPv6, "0");
239}
240
241void Device::EnableIPv6Privacy() {
242 SetIPFlag(IPAddress::kFamilyIPv6, kIPFlagUseTempAddr,
243 kIPFlagUseTempAddrUsedAndDefault);
244}
245
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800246void Device::DisableReversePathFilter() {
247 // TODO(pstew): Current kernel doesn't offer reverse-path filtering flag
248 // for IPv6. crosbug.com/24228
249 SetIPFlag(IPAddress::kFamilyIPv4, kIPFlagReversePathFilter,
250 kIPFlagReversePathFilterLooseMode);
251}
252
253void Device::EnableReversePathFilter() {
254 SetIPFlag(IPAddress::kFamilyIPv4, kIPFlagReversePathFilter,
255 kIPFlagReversePathFilterEnabled);
256}
257
Gaurav Shah435de2c2011-11-17 19:01:07 -0800258bool Device::IsConnected() const {
259 if (selected_service_)
260 return selected_service_->IsConnected();
261 return false;
262}
263
Paul Stewartd215af62012-04-24 23:25:50 -0700264bool Device::IsConnectedToService(const ServiceRefPtr &service) const {
265 return service == selected_service_ && IsConnected();
266}
267
Chris Masone27c4aa52011-07-02 13:10:14 -0700268string Device::GetRpcIdentifier() {
269 return adaptor_->GetRpcIdentifier();
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700270}
271
Chris Masone5dec5f42011-07-22 14:07:55 -0700272string Device::GetStorageIdentifier() {
273 string id = GetRpcIdentifier();
274 ControlInterface::RpcIdToStorageId(&id);
Chris Masone626719f2011-08-18 16:58:48 -0700275 size_t needle = id.find('_');
Chris Masone34af2182011-08-22 11:59:36 -0700276 DLOG_IF(ERROR, needle == string::npos) << "No _ in storage id?!?!";
Chris Masone626719f2011-08-18 16:58:48 -0700277 id.replace(id.begin() + needle + 1, id.end(), hardware_address_);
Chris Masone5dec5f42011-07-22 14:07:55 -0700278 return id;
279}
280
Gaurav Shah6d2c72d2012-10-16 16:30:44 -0700281vector<GeolocationInfo> Device::GetGeolocationObjects() const {
282 return vector<GeolocationInfo>();
283};
284
Jason Glasgowb5790052012-01-27 01:03:52 -0500285string Device::GetTechnologyString(Error */*error*/) {
286 return Technology::NameFromIdentifier(technology());
287}
288
Chris Masone19e30402011-07-19 15:48:47 -0700289const string& Device::FriendlyName() const {
Chris Masone7df0c672011-07-15 10:24:54 -0700290 return link_name_;
Darin Petkovafa6fc42011-06-21 16:21:08 -0700291}
292
Chris Masone19e30402011-07-19 15:48:47 -0700293const string& Device::UniqueName() const {
294 return unique_id_;
295}
296
Chris Masone5dec5f42011-07-22 14:07:55 -0700297bool Device::Load(StoreInterface *storage) {
298 const string id = GetStorageIdentifier();
299 if (!storage->ContainsGroup(id)) {
300 LOG(WARNING) << "Device is not available in the persistent store: " << id;
301 return false;
302 }
Eric Shienbrood9a245532012-03-07 14:20:39 -0500303 enabled_persistent_ = true;
304 storage->GetBool(id, kStoragePowered, &enabled_persistent_);
Paul Stewart6ff27f52012-07-11 06:51:41 -0700305 uint64 rx_byte_count = 0, tx_byte_count = 0;
306
307 manager_->device_info()->GetByteCounts(
308 interface_index_, &rx_byte_count, &tx_byte_count);
309 // If there is a byte-count present in the profile, the return value
310 // of Device::Get*ByteCount() should be the this stored value plus
311 // whatever additional bytes we receive since time-of-load. We
312 // accomplish this by the subtractions below, which can validly
313 // roll over "negative" in the subtractions below and in Get*ByteCount.
314 uint64 profile_byte_count;
315 if (storage->GetUint64(id, kStorageReceiveByteCount, &profile_byte_count)) {
316 receive_byte_offset_ = rx_byte_count - profile_byte_count;
317 }
318 if (storage->GetUint64(id, kStorageTransmitByteCount, &profile_byte_count)) {
319 transmit_byte_offset_ = tx_byte_count - profile_byte_count;
320 }
321
Chris Masone5dec5f42011-07-22 14:07:55 -0700322 return true;
323}
324
325bool Device::Save(StoreInterface *storage) {
326 const string id = GetStorageIdentifier();
Eric Shienbrood9a245532012-03-07 14:20:39 -0500327 storage->SetBool(id, kStoragePowered, enabled_persistent_);
Chris Masone34af2182011-08-22 11:59:36 -0700328 if (ipconfig_.get()) {
329 // The _0 is an index into the list of IPConfigs that this device might
330 // have. We only have one IPConfig right now, and I hope to never have
331 // to support more, as sleffler indicates that associating IPConfigs
332 // with devices is wrong and due to be changed in flimflam anyhow.
333 string suffix = hardware_address_ + "_0";
334 ipconfig_->Save(storage, suffix);
335 storage->SetString(id, kStorageIPConfigs, SerializeIPConfigs(suffix));
336 }
Paul Stewart6ff27f52012-07-11 06:51:41 -0700337 storage->SetUint64(id, kStorageReceiveByteCount, GetReceiveByteCount(NULL));
338 storage->SetUint64(id, kStorageTransmitByteCount, GetTransmitByteCount(NULL));
Chris Masone5dec5f42011-07-22 14:07:55 -0700339 return true;
340}
341
mukesh agrawal784566d2012-08-08 18:32:58 -0700342void Device::OnBeforeSuspend() {
343 // Nothing to be done in the general case.
344}
345
346void Device::OnAfterResume() {
347 if (ipconfig_) {
348 SLOG(Device, 3) << "Renewing IP address on resume.";
349 ipconfig_->RenewIP();
350 }
351}
352
Darin Petkov2b8e44e2012-06-25 15:13:26 +0200353void Device::DropConnection() {
354 SLOG(Device, 2) << __func__;
355 DestroyIPConfig();
356 SelectService(NULL);
357}
358
Darin Petkovafa6fc42011-06-21 16:21:08 -0700359void Device::DestroyIPConfig() {
Paul Stewart2bf1d352011-12-06 15:02:55 -0800360 DisableIPv6();
Darin Petkovafa6fc42011-06-21 16:21:08 -0700361 if (ipconfig_.get()) {
362 ipconfig_->ReleaseIP();
363 ipconfig_ = NULL;
364 }
Paul Stewarte6132022011-08-16 09:11:02 -0700365 DestroyConnection();
Darin Petkovafa6fc42011-06-21 16:21:08 -0700366}
367
Arman Ugurayed8e6102012-11-29 14:47:20 -0800368bool Device::ShouldUseArpGateway() const {
369 return false;
370}
371
Paul Stewart2bf1d352011-12-06 15:02:55 -0800372bool Device::AcquireIPConfig() {
Paul Stewartd408fdf2012-05-07 17:15:57 -0700373 return AcquireIPConfigWithLeaseName(string());
374}
375
376bool Device::AcquireIPConfigWithLeaseName(const string &lease_name) {
Darin Petkovafa6fc42011-06-21 16:21:08 -0700377 DestroyIPConfig();
Paul Stewart2bf1d352011-12-06 15:02:55 -0800378 EnableIPv6();
Arman Ugurayed8e6102012-11-29 14:47:20 -0800379 bool arp_gateway = manager_->GetArpGateway() && ShouldUseArpGateway();
Paul Stewartd408fdf2012-05-07 17:15:57 -0700380 ipconfig_ = dhcp_provider_->CreateConfig(link_name_,
381 manager_->GetHostName(),
382 lease_name,
Arman Ugurayed8e6102012-11-29 14:47:20 -0800383 arp_gateway);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500384 ipconfig_->RegisterUpdateCallback(Bind(&Device::OnIPConfigUpdated,
385 weak_ptr_factory_.GetWeakPtr()));
Paul Stewart1062d9d2012-04-27 10:42:27 -0700386 dispatcher_->PostTask(Bind(&Device::ConfigureStaticIPTask,
387 weak_ptr_factory_.GetWeakPtr()));
Darin Petkovafa6fc42011-06-21 16:21:08 -0700388 return ipconfig_->RequestIP();
389}
390
Jason Glasgowb5790052012-01-27 01:03:52 -0500391void Device::HelpRegisterDerivedString(
392 const string &name,
393 string(Device::*get)(Error *error),
394 void(Device::*set)(const string &value, Error *error)) {
395 store_.RegisterDerivedString(
396 name,
397 StringAccessor(new CustomAccessor<Device, string>(this, get, set)));
398}
399
mukesh agrawalffa3d042011-10-06 15:26:10 -0700400void Device::HelpRegisterDerivedStrings(
401 const string &name,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500402 Strings(Device::*get)(Error *error),
403 void(Device::*set)(const Strings &value, Error *error)) {
Chris Masone27c4aa52011-07-02 13:10:14 -0700404 store_.RegisterDerivedStrings(
405 name,
406 StringsAccessor(new CustomAccessor<Device, Strings>(this, get, set)));
Chris Masone4e851612011-07-01 10:46:53 -0700407}
408
Jason Glasgow08afdff2012-04-03 10:22:26 -0400409void Device::HelpRegisterConstDerivedRpcIdentifiers(
410 const string &name,
411 RpcIdentifiers(Device::*get)(Error *)) {
412 store_.RegisterDerivedRpcIdentifiers(
413 name,
414 RpcIdentifiersAccessor(
415 new CustomAccessor<Device, RpcIdentifiers>(this, get, NULL)));
416}
417
Paul Stewart6ff27f52012-07-11 06:51:41 -0700418void Device::HelpRegisterConstDerivedUint64(
419 const string &name,
420 uint64(Device::*get)(Error *)) {
421 store_.RegisterDerivedUint64(
422 name,
423 Uint64Accessor(
424 new CustomAccessor<Device, uint64>(this, get, NULL)));
425}
426
Paul Stewart1062d9d2012-04-27 10:42:27 -0700427void Device::ConfigureStaticIPTask() {
428 SLOG(Device, 2) << __func__ << " selected_service " << selected_service_.get()
429 << " ipconfig " << ipconfig_.get();
430
431 if (!selected_service_ || !ipconfig_) {
432 return;
433 }
434
435 const StaticIPParameters &static_ip_parameters =
436 selected_service_->static_ip_parameters();
437 if (static_ip_parameters.ContainsAddress()) {
438 SLOG(Device, 2) << __func__ << " " << " configuring static IP parameters.";
439 // If the parameters contain an IP address, apply them now and bring
440 // the interface up. When DHCP information arrives, it will supplement
441 // the static information.
442 OnIPConfigUpdated(ipconfig_, true);
443 } else {
444 SLOG(Device, 2) << __func__ << " " << " no static IP address.";
445 }
446}
447
Darin Petkov79d74c92012-03-07 17:20:32 +0100448void Device::OnIPConfigUpdated(const IPConfigRefPtr &ipconfig, bool success) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700449 SLOG(Device, 2) << __func__ << " " << " success: " << success;
Paul Stewartc39f1132011-06-22 12:02:28 -0700450 if (success) {
Paul Stewarte6132022011-08-16 09:11:02 -0700451 CreateConnection();
Paul Stewart1062d9d2012-04-27 10:42:27 -0700452 if (selected_service_) {
453 ipconfig->ApplyStaticIPParameters(
Paul Stewartdef189e2012-08-02 20:12:09 -0700454 selected_service_->mutable_static_ip_parameters());
Paul Stewart1062d9d2012-04-27 10:42:27 -0700455 }
Paul Stewarte6132022011-08-16 09:11:02 -0700456 connection_->UpdateFromIPConfig(ipconfig);
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800457 // SetConnection must occur after the UpdateFromIPConfig so the
458 // service can use the values derived from the connection.
Paul Stewart1062d9d2012-04-27 10:42:27 -0700459 if (selected_service_) {
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800460 selected_service_->SetConnection(connection_);
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800461 }
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800462 // The service state change needs to happen last, so that at the
463 // time we report the state change to the manager, the service
464 // has its connection.
465 SetServiceState(Service::kStateConnected);
Thieu Le85e050b2012-03-13 15:04:38 -0700466 portal_attempts_to_online_ = 0;
Paul Stewart20088d82012-02-16 06:58:55 -0800467 // Subtle: Start portal detection after transitioning the service
468 // to the Connected state because this call may immediately transition
469 // to the Online state.
Thieu Led1760922012-09-11 14:15:35 -0700470 if (selected_service_) {
471 StartPortalDetection();
472 }
Paul Stewart036dba02012-08-07 12:34:41 -0700473 StartLinkMonitor();
Paul Stewarte6132022011-08-16 09:11:02 -0700474 } else {
mukesh agrawalcc0fded2012-05-09 13:40:58 -0700475 // TODO(pstew): This logic gets yet more complex when multiple
476 // IPConfig types are run in parallel (e.g. DHCP and DHCP6)
477 if (selected_service_ &&
478 selected_service_->static_ip_parameters().ContainsAddress()) {
479 // Consider three cases:
480 //
481 // 1. We're here because DHCP failed while starting up. There
482 // are two subcases:
483 // a. DHCP has failed, and Static IP config has _not yet_
484 // completed. It's fine to do nothing, because we'll
485 // apply the static config shortly.
486 // b. DHCP has failed, and Static IP config has _already_
487 // completed. It's fine to do nothing, because we can
488 // continue to use the static config that's already
489 // been applied.
490 //
491 // 2. We're here because a previously valid DHCP configuration
492 // is no longer valid. There's still a static IP config,
493 // because the condition in the if clause evaluated to true.
494 // Furthermore, the static config includes an IP address for
495 // us to use.
496 //
497 // The current configuration may include some DHCP
498 // parameters, overriden by any static parameters
499 // provided. We continue to use this configuration, because
500 // the only configuration element that is leased to us (IP
501 // address) will be overriden by a static parameter.
502 return;
503 }
504
Christopher Wileyabd3b502012-09-26 13:08:52 -0700505 if (selected_service_) {
506 Error error;
507 selected_service_->DisconnectWithFailure(Service::kFailureDHCP, &error);
508 }
Paul Stewarte6132022011-08-16 09:11:02 -0700509 DestroyConnection();
Paul Stewartc39f1132011-06-22 12:02:28 -0700510 }
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700511}
512
Paul Stewarte6132022011-08-16 09:11:02 -0700513void Device::CreateConnection() {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700514 SLOG(Device, 2) << __func__;
Paul Stewarte6132022011-08-16 09:11:02 -0700515 if (!connection_.get()) {
Paul Stewartbf667612012-06-29 14:49:54 -0700516 connection_ = new Connection(
517 interface_index_,
518 link_name_,
519 technology_,
520 manager_->device_info(),
521 manager_->IsTechnologyShortDNSTimeoutEnabled(technology_));
Paul Stewarte6132022011-08-16 09:11:02 -0700522 }
523}
524
525void Device::DestroyConnection() {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700526 SLOG(Device, 2) << __func__;
Paul Stewart20088d82012-02-16 06:58:55 -0800527 StopPortalDetection();
Paul Stewart036dba02012-08-07 12:34:41 -0700528 StopLinkMonitor();
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800529 if (selected_service_.get()) {
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800530 selected_service_->SetConnection(NULL);
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800531 }
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800532 connection_ = NULL;
Paul Stewarte6132022011-08-16 09:11:02 -0700533}
534
Paul Stewart03dba0b2011-08-22 16:32:45 -0700535void Device::SelectService(const ServiceRefPtr &service) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700536 SLOG(Device, 2) << __func__ << ": "
537 << (service.get() ?
538 StringPrintf("%s (%s)",
539 service->UniqueName().c_str(),
540 service->friendly_name().c_str()) :
541 "*reset*");
mukesh agrawal8a3188d2011-12-01 20:56:44 +0000542
543 if (selected_service_.get() == service.get()) {
544 // No change to |selected_service_|. Return early to avoid
545 // changing its state.
546 return;
547 }
548
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800549 if (selected_service_.get()) {
550 if (selected_service_->state() != Service::kStateFailure) {
551 selected_service_->SetState(Service::kStateIdle);
552 }
Paul Stewart20b0a092012-05-22 20:39:57 -0700553 // Just in case the Device subclass has not already done so, make
554 // sure the previously selected service has its connection removed.
Paul Stewartc1dec4d2011-12-08 15:25:28 -0800555 selected_service_->SetConnection(NULL);
Paul Stewartc8860612012-09-28 07:36:21 -0700556 StopLinkMonitor();
557 StopPortalDetection();
Paul Stewart03dba0b2011-08-22 16:32:45 -0700558 }
559 selected_service_ = service;
560}
561
562void Device::SetServiceState(Service::ConnectState state) {
563 if (selected_service_.get()) {
564 selected_service_->SetState(state);
565 }
566}
567
568void Device::SetServiceFailure(Service::ConnectFailure failure_state) {
569 if (selected_service_.get()) {
570 selected_service_->SetFailure(failure_state);
571 }
572}
573
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400574void Device::SetServiceFailureSilent(Service::ConnectFailure failure_state) {
575 if (selected_service_.get()) {
576 selected_service_->SetFailureSilent(failure_state);
577 }
578}
579
Chris Masone34af2182011-08-22 11:59:36 -0700580string Device::SerializeIPConfigs(const string &suffix) {
581 return StringPrintf("%s:%s", suffix.c_str(), ipconfig_->type().c_str());
Chris Masone5dec5f42011-07-22 14:07:55 -0700582}
583
Paul Stewart2bf1d352011-12-06 15:02:55 -0800584bool Device::SetIPFlag(IPAddress::Family family, const string &flag,
585 const string &value) {
586 string ip_version;
587 if (family == IPAddress::kFamilyIPv4) {
588 ip_version = kIPFlagVersion4;
589 } else if (family == IPAddress::kFamilyIPv6) {
590 ip_version = kIPFlagVersion6;
591 } else {
592 NOTIMPLEMENTED();
593 }
594 FilePath flag_file(StringPrintf(kIPFlagTemplate, ip_version.c_str(),
595 link_name_.c_str(), flag.c_str()));
Ben Chanfad4a0b2012-04-18 15:49:59 -0700596 SLOG(Device, 2) << "Writing " << value << " to flag file "
597 << flag_file.value();
Paul Stewart2bf1d352011-12-06 15:02:55 -0800598 if (file_util::WriteFile(flag_file, value.c_str(), value.length()) != 1) {
599 LOG(ERROR) << StringPrintf("IP flag write failed: %s to %s",
600 value.c_str(), flag_file.value().c_str());
601 return false;
602 }
603 return true;
604}
605
Paul Stewart6ff27f52012-07-11 06:51:41 -0700606void Device::ResetByteCounters() {
607 manager_->device_info()->GetByteCounts(
608 interface_index_, &receive_byte_offset_, &transmit_byte_offset_);
609 manager_->UpdateDevice(this);
610}
611
Paul Stewartd215af62012-04-24 23:25:50 -0700612bool Device::RestartPortalDetection() {
613 StopPortalDetection();
614 return StartPortalDetection();
615}
616
Paul Stewartc681fa02012-03-02 19:40:04 -0800617bool Device::RequestPortalDetection() {
618 if (!selected_service_) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700619 SLOG(Device, 2) << FriendlyName()
Paul Stewartc681fa02012-03-02 19:40:04 -0800620 << ": No selected service, so no need for portal check.";
621 return false;
622 }
623
624 if (!connection_.get()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700625 SLOG(Device, 2) << FriendlyName()
Paul Stewartc681fa02012-03-02 19:40:04 -0800626 << ": No connection, so no need for portal check.";
627 return false;
628 }
629
630 if (selected_service_->state() != Service::kStatePortal) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700631 SLOG(Device, 2) << FriendlyName()
Paul Stewartc681fa02012-03-02 19:40:04 -0800632 << ": Service is not in portal state. No need to start check.";
633 return false;
634 }
635
636 if (!connection_->is_default()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700637 SLOG(Device, 2) << FriendlyName()
Paul Stewartc681fa02012-03-02 19:40:04 -0800638 << ": Service is not the default connection. Don't start check.";
639 return false;
640 }
641
642 if (portal_detector_.get() && portal_detector_->IsInProgress()) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700643 SLOG(Device, 2) << FriendlyName()
644 << ": Portal detection is already running.";
Paul Stewartc681fa02012-03-02 19:40:04 -0800645 return true;
646 }
647
648 return StartPortalDetection();
649}
650
Paul Stewart20088d82012-02-16 06:58:55 -0800651bool Device::StartPortalDetection() {
Paul Stewartd215af62012-04-24 23:25:50 -0700652 DCHECK(selected_service_.get());
653 if (selected_service_->IsPortalDetectionDisabled()) {
654 SLOG(Device, 2) << "Service " << selected_service_->friendly_name()
655 << ": Portal detection is disabled; "
656 << "marking service online.";
657 SetServiceConnectedState(Service::kStateOnline);
658 return false;
659 }
660
661 if (selected_service_->IsPortalDetectionAuto() &&
662 !manager_->IsPortalDetectionEnabled(technology())) {
Paul Stewart20088d82012-02-16 06:58:55 -0800663 // If portal detection is disabled for this technology, immediately set
664 // the service state to "Online".
Ben Chanfad4a0b2012-04-18 15:49:59 -0700665 SLOG(Device, 2) << "Device " << FriendlyName()
666 << ": Portal detection is disabled; "
667 << "marking service online.";
Paul Stewart20088d82012-02-16 06:58:55 -0800668 SetServiceConnectedState(Service::kStateOnline);
669 return false;
670 }
671
Paul Stewart20088d82012-02-16 06:58:55 -0800672 if (selected_service_->HasProxyConfig()) {
673 // Services with HTTP proxy configurations should not be checked by the
674 // connection manager, since we don't have the ability to evaluate
675 // arbitrary proxy configs and their possible credentials.
Ben Chanfad4a0b2012-04-18 15:49:59 -0700676 SLOG(Device, 2) << "Device " << FriendlyName()
677 << ": Service has proxy config; marking it online.";
Paul Stewart20088d82012-02-16 06:58:55 -0800678 SetServiceConnectedState(Service::kStateOnline);
679 return false;
680 }
681
682 portal_detector_.reset(new PortalDetector(connection_,
683 dispatcher_,
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500684 portal_detector_callback_));
Paul Stewart20088d82012-02-16 06:58:55 -0800685 if (!portal_detector_->Start(manager_->GetPortalCheckURL())) {
686 LOG(ERROR) << "Device " << FriendlyName()
687 << ": Portal detection failed to start: likely bad URL: "
688 << manager_->GetPortalCheckURL();
689 SetServiceConnectedState(Service::kStateOnline);
690 return false;
691 }
692
Ben Chanfad4a0b2012-04-18 15:49:59 -0700693 SLOG(Device, 2) << "Device " << FriendlyName()
694 << ": Portal detection has started.";
Paul Stewart20088d82012-02-16 06:58:55 -0800695 return true;
696}
697
698void Device::StopPortalDetection() {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700699 SLOG(Device, 2) << "Device " << FriendlyName()
700 << ": Portal detection has stopped.";
Paul Stewart20088d82012-02-16 06:58:55 -0800701 portal_detector_.reset();
702}
703
Paul Stewart036dba02012-08-07 12:34:41 -0700704void Device::set_link_monitor(LinkMonitor *link_monitor) {
705 link_monitor_.reset(link_monitor);
706}
707
708bool Device::StartLinkMonitor() {
709 if (!manager_->IsTechnologyLinkMonitorEnabled(technology())) {
710 SLOG(Device, 2) << "Device " << FriendlyName()
711 << ": Link Monitoring is disabled.";
712 return false;
713 }
714
715 if (!link_monitor()) {
716 set_link_monitor(
717 new LinkMonitor(
718 connection_, dispatcher_, metrics(), manager_->device_info(),
719 Bind(&Device::OnLinkMonitorFailure, weak_ptr_factory_.GetWeakPtr())));
720 }
721
722 SLOG(Device, 2) << "Device " << FriendlyName()
723 << ": Link Monitor starting.";
724 return link_monitor_->Start();
725}
726
727void Device::StopLinkMonitor() {
728 SLOG(Device, 2) << "Device " << FriendlyName()
729 << ": Link Monitor stopping.";
730 link_monitor_.reset();
731}
732
733void Device::OnLinkMonitorFailure() {
734 LOG(ERROR) << "Device " << FriendlyName()
735 << ": Link Monitor indicates failure.";
736}
737
Paul Stewart20088d82012-02-16 06:58:55 -0800738void Device::SetServiceConnectedState(Service::ConnectState state) {
739 DCHECK(selected_service_.get());
740
741 if (!selected_service_.get()) {
742 LOG(ERROR) << FriendlyName() << ": "
743 << "Portal detection completed but no selected service exists!";
744 return;
745 }
746
747 if (!selected_service_->IsConnected()) {
748 LOG(ERROR) << FriendlyName() << ": "
749 << "Portal detection completed but selected service "
750 << selected_service_->UniqueName()
751 << " is in non-connected state.";
752 return;
753 }
754
Paul Stewartc681fa02012-03-02 19:40:04 -0800755 if (state == Service::kStatePortal && connection_->is_default() &&
756 manager_->GetPortalCheckInterval() != 0) {
757 CHECK(portal_detector_.get());
758 if (!portal_detector_->StartAfterDelay(
759 manager_->GetPortalCheckURL(),
760 manager_->GetPortalCheckInterval())) {
761 LOG(ERROR) << "Device " << FriendlyName()
762 << ": Portal detection failed to restart: likely bad URL: "
763 << manager_->GetPortalCheckURL();
764 SetServiceState(Service::kStateOnline);
765 portal_detector_.reset();
766 return;
767 }
Ben Chanfad4a0b2012-04-18 15:49:59 -0700768 SLOG(Device, 2) << "Device " << FriendlyName()
769 << ": Portal detection retrying.";
Paul Stewartc681fa02012-03-02 19:40:04 -0800770 } else {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700771 SLOG(Device, 2) << "Device " << FriendlyName()
772 << ": Portal will not retry.";
Paul Stewartc681fa02012-03-02 19:40:04 -0800773 portal_detector_.reset();
774 }
775
Paul Stewart20088d82012-02-16 06:58:55 -0800776 SetServiceState(state);
777}
778
779void Device::PortalDetectorCallback(const PortalDetector::Result &result) {
780 if (!result.final) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700781 SLOG(Device, 2) << "Device " << FriendlyName()
782 << ": Received non-final status: "
783 << PortalDetector::StatusToString(result.status);
Paul Stewart20088d82012-02-16 06:58:55 -0800784 return;
785 }
786
Ben Chanfad4a0b2012-04-18 15:49:59 -0700787 SLOG(Device, 2) << "Device " << FriendlyName()
788 << ": Received final status: "
789 << PortalDetector::StatusToString(result.status);
Paul Stewart20088d82012-02-16 06:58:55 -0800790
Thieu Le85e050b2012-03-13 15:04:38 -0700791 portal_attempts_to_online_ += result.num_attempts;
792
793 metrics()->SendEnumToUMA(
794 metrics()->GetFullMetricName(Metrics::kMetricPortalResult, technology()),
795 Metrics::PortalDetectionResultToEnum(result),
796 Metrics::kPortalResultMax);
797
Paul Stewart20088d82012-02-16 06:58:55 -0800798 if (result.status == PortalDetector::kStatusSuccess) {
799 SetServiceConnectedState(Service::kStateOnline);
Thieu Le85e050b2012-03-13 15:04:38 -0700800
801 metrics()->SendToUMA(
802 metrics()->GetFullMetricName(
803 Metrics::kMetricPortalAttemptsToOnline, technology()),
804 portal_attempts_to_online_,
805 Metrics::kMetricPortalAttemptsToOnlineMin,
806 Metrics::kMetricPortalAttemptsToOnlineMax,
807 Metrics::kMetricPortalAttemptsToOnlineNumBuckets);
Paul Stewart20088d82012-02-16 06:58:55 -0800808 } else {
809 SetServiceConnectedState(Service::kStatePortal);
Thieu Le85e050b2012-03-13 15:04:38 -0700810
811 metrics()->SendToUMA(
812 metrics()->GetFullMetricName(
813 Metrics::kMetricPortalAttempts, technology()),
814 result.num_attempts,
815 Metrics::kMetricPortalAttemptsMin,
816 Metrics::kMetricPortalAttemptsMax,
817 Metrics::kMetricPortalAttemptsNumBuckets);
Paul Stewart20088d82012-02-16 06:58:55 -0800818 }
819}
820
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800821vector<string> Device::AvailableIPConfigs(Error */*error*/) {
Jason Glasgow08afdff2012-04-03 10:22:26 -0400822 if (ipconfig_.get()) {
823 string id = ipconfig_->GetRpcIdentifier();
824 return vector<string>(1, id);
825 }
826 return vector<string>();
Chris Masone4e851612011-07-01 10:46:53 -0700827}
828
829string Device::GetRpcConnectionIdentifier() {
830 return adaptor_->GetRpcConnectionIdentifier();
831}
832
Paul Stewart036dba02012-08-07 12:34:41 -0700833uint64 Device::GetLinkMonitorResponseTime(Error *error) {
834 if (!link_monitor_.get()) {
835 // It is not strictly an error that the link monitor does not
836 // exist, but returning an error here allows the GetProperties
837 // call in our Adaptor to omit this parameter.
838 error->Populate(Error::kNotFound, "Device is not running LinkMonitor");
839 return 0;
840 }
841 return link_monitor_->GetResponseTimeMilliseconds();
842}
843
Paul Stewart6ff27f52012-07-11 06:51:41 -0700844uint64 Device::GetReceiveByteCount(Error */*error*/) {
845 uint64 rx_byte_count = 0, tx_byte_count = 0;
846 manager_->device_info()->GetByteCounts(
847 interface_index_, &rx_byte_count, &tx_byte_count);
848 return rx_byte_count - receive_byte_offset_;
849}
850
851uint64 Device::GetTransmitByteCount(Error */*error*/) {
852 uint64 rx_byte_count = 0, tx_byte_count = 0;
853 manager_->device_info()->GetByteCounts(
854 interface_index_, &rx_byte_count, &tx_byte_count);
855 return tx_byte_count - transmit_byte_offset_;
856}
857
Eric Shienbrood7fce52c2012-04-13 19:11:02 -0400858bool Device::IsUnderlyingDeviceEnabled() const {
859 return false;
860}
861
Eric Shienbrood9a245532012-03-07 14:20:39 -0500862// callback
863void Device::OnEnabledStateChanged(const ResultCallback &callback,
864 const Error &error) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700865 SLOG(Device, 2) << __func__ << "(" << enabled_pending_ << ")";
Eric Shienbrood9a245532012-03-07 14:20:39 -0500866 if (error.IsSuccess()) {
867 enabled_ = enabled_pending_;
868 manager_->UpdateEnabledTechnologies();
869 adaptor_->EmitBoolChanged(flimflam::kPoweredProperty, enabled_);
870 adaptor_->UpdateEnabled();
871 }
Gary Morainbaeefdf2012-04-30 14:53:35 -0700872 enabled_pending_ = enabled_;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500873 if (!callback.is_null())
874 callback.Run(error);
875}
876
877void Device::SetEnabled(bool enable) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700878 SLOG(Device, 2) << __func__ << "(" << enable << ")";
Jason Glasgow4a490792012-04-10 15:02:05 -0400879 Error error;
880 SetEnabledInternal(enable, false, &error, ResultCallback());
Jason Glasgow7234ec32012-05-23 16:01:21 -0400881 LOG_IF(ERROR, error.IsFailure() && !error.IsOngoing())
Jason Glasgow4a490792012-04-10 15:02:05 -0400882 << "Enabled failed, but no way to report the failure.";
Eric Shienbrood9a245532012-03-07 14:20:39 -0500883}
884
885void Device::SetEnabledPersistent(bool enable,
886 Error *error,
887 const ResultCallback &callback) {
888 SetEnabledInternal(enable, true, error, callback);
889}
890
891void Device::SetEnabledInternal(bool enable,
892 bool persist,
893 Error *error,
894 const ResultCallback &callback) {
Jason Glasgow4a490792012-04-10 15:02:05 -0400895 DCHECK(error);
Ben Chanfad4a0b2012-04-18 15:49:59 -0700896 SLOG(Device, 2) << "Device " << link_name_ << " "
897 << (enable ? "starting" : "stopping");
Eric Shienbrood9a245532012-03-07 14:20:39 -0500898 if (enable == enabled_) {
Jason Glasgow4a490792012-04-10 15:02:05 -0400899 error->Reset();
Eric Shienbrood9a245532012-03-07 14:20:39 -0500900 return;
901 }
902
903 if (enabled_pending_ == enable) {
Jason Glasgow4a490792012-04-10 15:02:05 -0400904 Error::PopulateAndLog(error, Error::kInProgress,
905 "Enable operation already in progress");
Eric Shienbrood9a245532012-03-07 14:20:39 -0500906 return;
907 }
908
909 if (persist) {
910 enabled_persistent_ = enable;
Darin Petkove7c6ad32012-06-29 10:22:09 +0200911 manager_->UpdateDevice(this);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500912 }
913
914 enabled_pending_ = enable;
915 EnabledStateChangedCallback enabled_callback =
916 Bind(&Device::OnEnabledStateChanged,
917 weak_ptr_factory_.GetWeakPtr(), callback);
918 if (enable) {
919 running_ = true;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500920 Start(error, enabled_callback);
921 } else {
922 running_ = false;
923 DestroyIPConfig(); // breaks a reference cycle
924 SelectService(NULL); // breaks a reference cycle
925 rtnl_handler_->SetInterfaceFlags(interface_index(), 0, IFF_UP);
Ben Chanfad4a0b2012-04-18 15:49:59 -0700926 SLOG(Device, 3) << "Device " << link_name_ << " ipconfig_ "
927 << (ipconfig_ ? "is set." : "is not set.");
928 SLOG(Device, 3) << "Device " << link_name_ << " connection_ "
929 << (connection_ ? "is set." : "is not set.");
930 SLOG(Device, 3) << "Device " << link_name_ << " selected_service_ "
931 << (selected_service_ ? "is set." : "is not set.");
Eric Shienbrood9a245532012-03-07 14:20:39 -0500932 Stop(error, enabled_callback);
933 }
934}
935
Paul Stewart75897df2011-04-27 09:05:53 -0700936} // namespace shill