mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 5 | #include "shill/device.h" |
| 6 | |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 7 | #include <netinet/in.h> |
| 8 | #include <linux/if.h> // Needs definitions from netinet/in.h |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 9 | #include <stdio.h> |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 10 | #include <time.h> |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 11 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 12 | #include <string> |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 13 | #include <vector> |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 14 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 15 | #include <base/bind.h> |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 16 | #include <base/file_util.h> |
Chris Masone | 487b8bf | 2011-05-13 16:27:57 -0700 | [diff] [blame] | 17 | #include <base/memory/ref_counted.h> |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 18 | #include <base/stringprintf.h> |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 19 | #include <chromeos/dbus/service_constants.h> |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 20 | |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 21 | #include "shill/connection.h" |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 22 | #include "shill/control_interface.h" |
Chris Masone | d7732e4 | 2011-05-20 11:08:56 -0700 | [diff] [blame] | 23 | #include "shill/device_dbus_adaptor.h" |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 24 | #include "shill/dhcp_config.h" |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 25 | #include "shill/dhcp_provider.h" |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 26 | #include "shill/error.h" |
Paul Stewart | 26b327e | 2011-10-19 11:38:09 -0700 | [diff] [blame] | 27 | #include "shill/event_dispatcher.h" |
Gaurav Shah | 6d2c72d | 2012-10-16 16:30:44 -0700 | [diff] [blame] | 28 | #include "shill/geolocation_info.h" |
Paul Stewart | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 29 | #include "shill/http_proxy.h" |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 30 | #include "shill/link_monitor.h" |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 31 | #include "shill/logging.h" |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 32 | #include "shill/manager.h" |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 33 | #include "shill/metrics.h" |
Chris Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame] | 34 | #include "shill/property_accessor.h" |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 35 | #include "shill/refptr_types.h" |
Paul Stewart | c39f113 | 2011-06-22 12:02:28 -0700 | [diff] [blame] | 36 | #include "shill/rtnl_handler.h" |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 37 | #include "shill/service.h" |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 38 | #include "shill/store_interface.h" |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 39 | #include "shill/technology.h" |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 40 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 41 | using base::Bind; |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 42 | using base::StringPrintf; |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 43 | using std::string; |
| 44 | using std::vector; |
| 45 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 46 | namespace shill { |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 47 | |
| 48 | // static |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 49 | const char Device::kIPFlagTemplate[] = "/proc/sys/net/%s/conf/%s/%s"; |
| 50 | // static |
| 51 | const char Device::kIPFlagVersion4[] = "ipv4"; |
| 52 | // static |
| 53 | const char Device::kIPFlagVersion6[] = "ipv6"; |
| 54 | // static |
| 55 | const char Device::kIPFlagDisableIPv6[] = "disable_ipv6"; |
| 56 | // static |
| 57 | const char Device::kIPFlagUseTempAddr[] = "use_tempaddr"; |
| 58 | // static |
| 59 | const char Device::kIPFlagUseTempAddrUsedAndDefault[] = "2"; |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 60 | // static |
| 61 | const char Device::kIPFlagReversePathFilter[] = "rp_filter"; |
| 62 | // static |
| 63 | const char Device::kIPFlagReversePathFilterEnabled[] = "1"; |
| 64 | // static |
| 65 | const char Device::kIPFlagReversePathFilterLooseMode[] = "2"; |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 66 | // static |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 67 | const char Device::kStoragePowered[] = "Powered"; |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 68 | // static |
| 69 | const char Device::kStorageIPConfigs[] = "IPConfigs"; |
Paul Stewart | 6ff27f5 | 2012-07-11 06:51:41 -0700 | [diff] [blame] | 70 | // static |
| 71 | const char Device::kStorageReceiveByteCount[] = "ReceiveByteCount"; |
| 72 | // static |
| 73 | const char Device::kStorageTransmitByteCount[] = "TransmitByteCount"; |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 74 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 75 | Device::Device(ControlInterface *control_interface, |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 76 | EventDispatcher *dispatcher, |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 77 | Metrics *metrics, |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 78 | Manager *manager, |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 79 | const string &link_name, |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 80 | const string &address, |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 81 | int interface_index, |
| 82 | Technology::Identifier technology) |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 83 | : enabled_(false), |
| 84 | enabled_persistent_(true), |
| 85 | enabled_pending_(enabled_), |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 86 | reconnect_(true), |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 87 | hardware_address_(address), |
mukesh agrawal | f60e406 | 2011-05-27 13:13:41 -0700 | [diff] [blame] | 88 | interface_index_(interface_index), |
| 89 | running_(false), |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 90 | link_name_(link_name), |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 91 | unique_id_(link_name), |
Darin Petkov | d966195 | 2011-08-03 16:25:42 -0700 | [diff] [blame] | 92 | control_interface_(control_interface), |
| 93 | dispatcher_(dispatcher), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 94 | metrics_(metrics), |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 95 | manager_(manager), |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 96 | weak_ptr_factory_(this), |
Darin Petkov | 77cb681 | 2011-08-15 16:19:41 -0700 | [diff] [blame] | 97 | adaptor_(control_interface->CreateDeviceAdaptor(this)), |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 98 | portal_detector_callback_(Bind(&Device::PortalDetectorCallback, |
| 99 | weak_ptr_factory_.GetWeakPtr())), |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 100 | technology_(technology), |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 101 | portal_attempts_to_online_(0), |
Paul Stewart | 6ff27f5 | 2012-07-11 06:51:41 -0700 | [diff] [blame] | 102 | receive_byte_offset_(0), |
| 103 | transmit_byte_offset_(0), |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 104 | dhcp_provider_(DHCPProvider::GetInstance()), |
| 105 | rtnl_handler_(RTNLHandler::GetInstance()) { |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 106 | store_.RegisterConstString(flimflam::kAddressProperty, &hardware_address_); |
Chris Masone | 4d42df8 | 2011-07-02 17:09:39 -0700 | [diff] [blame] | 107 | |
| 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 Petkov | 3335b37 | 2011-08-22 11:05:32 -0700 | [diff] [blame] | 115 | // flimflam::kHomeProviderProperty: Registered in Cellular |
Chris Masone | 4d42df8 | 2011-07-02 17:09:39 -0700 | [diff] [blame] | 116 | // flimflam::kImeiProperty: Registered in Cellular |
Ben Chan | a5e2708 | 2012-07-31 14:30:28 -0700 | [diff] [blame] | 117 | // flimflam::kIccidProperty: Registered in Cellular |
Chris Masone | 4d42df8 | 2011-07-02 17:09:39 -0700 | [diff] [blame] | 118 | // 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 Petkov | e9d12e0 | 2011-07-27 15:09:37 -0700 | [diff] [blame] | 129 | // flimflam::kDBusConnectionProperty: Registered in Cellular |
| 130 | // flimflam::kDBusObjectProperty: Register in Cellular |
Chris Masone | 4d42df8 | 2011-07-02 17:09:39 -0700 | [diff] [blame] | 131 | |
Jason Glasgow | e8334fd | 2012-03-30 16:02:37 -0400 | [diff] [blame] | 132 | store_.RegisterConstString(flimflam::kInterfaceProperty, &link_name_); |
Jason Glasgow | 08afdff | 2012-04-03 10:22:26 -0400 | [diff] [blame] | 133 | HelpRegisterConstDerivedRpcIdentifiers(flimflam::kIPConfigsProperty, |
| 134 | &Device::AvailableIPConfigs); |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 135 | store_.RegisterConstString(flimflam::kNameProperty, &link_name_); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 136 | store_.RegisterConstBool(flimflam::kPoweredProperty, &enabled_); |
Jason Glasgow | b579005 | 2012-01-27 01:03:52 -0500 | [diff] [blame] | 137 | HelpRegisterDerivedString(flimflam::kTypeProperty, |
| 138 | &Device::GetTechnologyString, |
| 139 | NULL); |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 140 | HelpRegisterConstDerivedUint64(shill::kLinkMonitorResponseTimeProperty, |
| 141 | &Device::GetLinkMonitorResponseTime); |
Jason Glasgow | b579005 | 2012-01-27 01:03:52 -0500 | [diff] [blame] | 142 | |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 143 | // TODO(cmasone): Chrome doesn't use this...does anyone? |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 144 | // store_.RegisterConstBool(flimflam::kReconnectProperty, &reconnect_); |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 145 | |
Chris Masone | 4e85161 | 2011-07-01 10:46:53 -0700 | [diff] [blame] | 146 | // TODO(cmasone): Figure out what shill concept maps to flimflam's "Network". |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 147 | // known_properties_.push_back(flimflam::kNetworksProperty); |
| 148 | |
Chris Masone | 4d42df8 | 2011-07-02 17:09:39 -0700 | [diff] [blame] | 149 | // flimflam::kScanningProperty: Registered in WiFi, Cellular |
| 150 | // flimflam::kScanIntervalProperty: Registered in WiFi, Cellular |
Paul Stewart | 6ff27f5 | 2012-07-11 06:51:41 -0700 | [diff] [blame] | 151 | |
| 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 Petkov | a0a0efe | 2012-06-27 12:50:01 +0200 | [diff] [blame] | 161 | LOG(INFO) << "Device created: " << link_name_ |
| 162 | << " index " << interface_index_; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | Device::~Device() { |
Darin Petkov | a0a0efe | 2012-06-27 12:50:01 +0200 | [diff] [blame] | 166 | LOG(INFO) << "Device destructed: " << link_name_ |
| 167 | << " index " << interface_index_; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 170 | void Device::LinkEvent(unsigned flags, unsigned change) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 171 | SLOG(Device, 2) << "Device " << link_name_ |
| 172 | << std::showbase << std::hex |
| 173 | << " flags " << flags << " changed " << change |
| 174 | << std::dec << std::noshowbase; |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Darin Petkov | c086531 | 2011-09-16 15:31:20 -0700 | [diff] [blame] | 177 | void Device::Scan(Error *error) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 178 | SLOG(Device, 2) << "Device " << link_name_ << " scan requested."; |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame] | 179 | Error::PopulateAndLog(error, Error::kNotSupported, |
| 180 | "Device doesn't support scan."); |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 183 | void Device::RegisterOnNetwork(const std::string &/*network_id*/, Error *error, |
| 184 | const ResultCallback &/*callback*/) { |
| 185 | Error::PopulateAndLog(error, Error::kNotSupported, |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame] | 186 | "Device doesn't support network registration."); |
Darin Petkov | 9ae310f | 2011-08-30 15:41:13 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Darin Petkov | c64fe5e | 2012-01-11 12:46:13 +0100 | [diff] [blame] | 189 | void Device::RequirePIN( |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 190 | const string &/*pin*/, bool /*require*/, |
| 191 | Error *error, const ResultCallback &/*callback*/) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 192 | SLOG(Device, 2) << __func__; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 193 | Error::PopulateAndLog(error, Error::kNotSupported, |
| 194 | "Device doesn't support RequirePIN."); |
Darin Petkov | e42e101 | 2011-08-31 12:35:04 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 197 | void Device::EnterPIN(const string &/*pin*/, |
| 198 | Error *error, const ResultCallback &/*callback*/) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 199 | SLOG(Device, 2) << __func__; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 200 | Error::PopulateAndLog(error, Error::kNotSupported, |
| 201 | "Device doesn't support EnterPIN."); |
Darin Petkov | e42e101 | 2011-08-31 12:35:04 -0700 | [diff] [blame] | 202 | } |
| 203 | |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 204 | void Device::UnblockPIN(const string &/*unblock_code*/, |
| 205 | const string &/*pin*/, |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 206 | Error *error, const ResultCallback &/*callback*/) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 207 | SLOG(Device, 2) << __func__; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 208 | Error::PopulateAndLog(error, Error::kNotSupported, |
| 209 | "Device doesn't support UnblockPIN."); |
Darin Petkov | e42e101 | 2011-08-31 12:35:04 -0700 | [diff] [blame] | 210 | } |
| 211 | |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 212 | void Device::ChangePIN(const string &/*old_pin*/, |
| 213 | const string &/*new_pin*/, |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 214 | Error *error, const ResultCallback &/*callback*/) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 215 | SLOG(Device, 2) << __func__; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 216 | Error::PopulateAndLog(error, Error::kNotSupported, |
| 217 | "Device doesn't support ChangePIN."); |
Darin Petkov | e42e101 | 2011-08-31 12:35:04 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Ben Chan | ad663e1 | 2013-01-08 01:58:47 -0800 | [diff] [blame] | 220 | void 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 Petkov | c37a9c4 | 2012-09-06 15:28:22 +0200 | [diff] [blame] | 226 | void 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 Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 233 | void Device::DisableIPv6() { |
| 234 | SetIPFlag(IPAddress::kFamilyIPv6, kIPFlagDisableIPv6, "1"); |
| 235 | } |
| 236 | |
| 237 | void Device::EnableIPv6() { |
| 238 | SetIPFlag(IPAddress::kFamilyIPv6, kIPFlagDisableIPv6, "0"); |
| 239 | } |
| 240 | |
| 241 | void Device::EnableIPv6Privacy() { |
| 242 | SetIPFlag(IPAddress::kFamilyIPv6, kIPFlagUseTempAddr, |
| 243 | kIPFlagUseTempAddrUsedAndDefault); |
| 244 | } |
| 245 | |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 246 | void 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 | |
| 253 | void Device::EnableReversePathFilter() { |
| 254 | SetIPFlag(IPAddress::kFamilyIPv4, kIPFlagReversePathFilter, |
| 255 | kIPFlagReversePathFilterEnabled); |
| 256 | } |
| 257 | |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 258 | bool Device::IsConnected() const { |
| 259 | if (selected_service_) |
| 260 | return selected_service_->IsConnected(); |
| 261 | return false; |
| 262 | } |
| 263 | |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 264 | bool Device::IsConnectedToService(const ServiceRefPtr &service) const { |
| 265 | return service == selected_service_ && IsConnected(); |
| 266 | } |
| 267 | |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 268 | string Device::GetRpcIdentifier() { |
| 269 | return adaptor_->GetRpcIdentifier(); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 270 | } |
| 271 | |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 272 | string Device::GetStorageIdentifier() { |
| 273 | string id = GetRpcIdentifier(); |
| 274 | ControlInterface::RpcIdToStorageId(&id); |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 275 | size_t needle = id.find('_'); |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 276 | DLOG_IF(ERROR, needle == string::npos) << "No _ in storage id?!?!"; |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 277 | id.replace(id.begin() + needle + 1, id.end(), hardware_address_); |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 278 | return id; |
| 279 | } |
| 280 | |
Gaurav Shah | 6d2c72d | 2012-10-16 16:30:44 -0700 | [diff] [blame] | 281 | vector<GeolocationInfo> Device::GetGeolocationObjects() const { |
| 282 | return vector<GeolocationInfo>(); |
| 283 | }; |
| 284 | |
Jason Glasgow | b579005 | 2012-01-27 01:03:52 -0500 | [diff] [blame] | 285 | string Device::GetTechnologyString(Error */*error*/) { |
| 286 | return Technology::NameFromIdentifier(technology()); |
| 287 | } |
| 288 | |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 289 | const string& Device::FriendlyName() const { |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 290 | return link_name_; |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 291 | } |
| 292 | |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 293 | const string& Device::UniqueName() const { |
| 294 | return unique_id_; |
| 295 | } |
| 296 | |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 297 | bool 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 Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 303 | enabled_persistent_ = true; |
| 304 | storage->GetBool(id, kStoragePowered, &enabled_persistent_); |
Paul Stewart | 6ff27f5 | 2012-07-11 06:51:41 -0700 | [diff] [blame] | 305 | 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 Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 322 | return true; |
| 323 | } |
| 324 | |
| 325 | bool Device::Save(StoreInterface *storage) { |
| 326 | const string id = GetStorageIdentifier(); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 327 | storage->SetBool(id, kStoragePowered, enabled_persistent_); |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 328 | 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 Stewart | 6ff27f5 | 2012-07-11 06:51:41 -0700 | [diff] [blame] | 337 | storage->SetUint64(id, kStorageReceiveByteCount, GetReceiveByteCount(NULL)); |
| 338 | storage->SetUint64(id, kStorageTransmitByteCount, GetTransmitByteCount(NULL)); |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 339 | return true; |
| 340 | } |
| 341 | |
mukesh agrawal | 784566d | 2012-08-08 18:32:58 -0700 | [diff] [blame] | 342 | void Device::OnBeforeSuspend() { |
| 343 | // Nothing to be done in the general case. |
| 344 | } |
| 345 | |
| 346 | void Device::OnAfterResume() { |
| 347 | if (ipconfig_) { |
| 348 | SLOG(Device, 3) << "Renewing IP address on resume."; |
| 349 | ipconfig_->RenewIP(); |
| 350 | } |
| 351 | } |
| 352 | |
Darin Petkov | 2b8e44e | 2012-06-25 15:13:26 +0200 | [diff] [blame] | 353 | void Device::DropConnection() { |
| 354 | SLOG(Device, 2) << __func__; |
| 355 | DestroyIPConfig(); |
| 356 | SelectService(NULL); |
| 357 | } |
| 358 | |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 359 | void Device::DestroyIPConfig() { |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 360 | DisableIPv6(); |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 361 | if (ipconfig_.get()) { |
| 362 | ipconfig_->ReleaseIP(); |
| 363 | ipconfig_ = NULL; |
| 364 | } |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 365 | DestroyConnection(); |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 366 | } |
| 367 | |
Arman Uguray | ed8e610 | 2012-11-29 14:47:20 -0800 | [diff] [blame] | 368 | bool Device::ShouldUseArpGateway() const { |
| 369 | return false; |
| 370 | } |
| 371 | |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 372 | bool Device::AcquireIPConfig() { |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 373 | return AcquireIPConfigWithLeaseName(string()); |
| 374 | } |
| 375 | |
| 376 | bool Device::AcquireIPConfigWithLeaseName(const string &lease_name) { |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 377 | DestroyIPConfig(); |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 378 | EnableIPv6(); |
Arman Uguray | ed8e610 | 2012-11-29 14:47:20 -0800 | [diff] [blame] | 379 | bool arp_gateway = manager_->GetArpGateway() && ShouldUseArpGateway(); |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 380 | ipconfig_ = dhcp_provider_->CreateConfig(link_name_, |
| 381 | manager_->GetHostName(), |
| 382 | lease_name, |
Arman Uguray | ed8e610 | 2012-11-29 14:47:20 -0800 | [diff] [blame] | 383 | arp_gateway); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 384 | ipconfig_->RegisterUpdateCallback(Bind(&Device::OnIPConfigUpdated, |
| 385 | weak_ptr_factory_.GetWeakPtr())); |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 386 | dispatcher_->PostTask(Bind(&Device::ConfigureStaticIPTask, |
| 387 | weak_ptr_factory_.GetWeakPtr())); |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 388 | return ipconfig_->RequestIP(); |
| 389 | } |
| 390 | |
Jason Glasgow | b579005 | 2012-01-27 01:03:52 -0500 | [diff] [blame] | 391 | void 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 agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 400 | void Device::HelpRegisterDerivedStrings( |
| 401 | const string &name, |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 402 | Strings(Device::*get)(Error *error), |
| 403 | void(Device::*set)(const Strings &value, Error *error)) { |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 404 | store_.RegisterDerivedStrings( |
| 405 | name, |
| 406 | StringsAccessor(new CustomAccessor<Device, Strings>(this, get, set))); |
Chris Masone | 4e85161 | 2011-07-01 10:46:53 -0700 | [diff] [blame] | 407 | } |
| 408 | |
Jason Glasgow | 08afdff | 2012-04-03 10:22:26 -0400 | [diff] [blame] | 409 | void 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 Stewart | 6ff27f5 | 2012-07-11 06:51:41 -0700 | [diff] [blame] | 418 | void 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 Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 427 | void 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 Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 448 | void Device::OnIPConfigUpdated(const IPConfigRefPtr &ipconfig, bool success) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 449 | SLOG(Device, 2) << __func__ << " " << " success: " << success; |
Paul Stewart | c39f113 | 2011-06-22 12:02:28 -0700 | [diff] [blame] | 450 | if (success) { |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 451 | CreateConnection(); |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 452 | if (selected_service_) { |
| 453 | ipconfig->ApplyStaticIPParameters( |
Paul Stewart | def189e | 2012-08-02 20:12:09 -0700 | [diff] [blame] | 454 | selected_service_->mutable_static_ip_parameters()); |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 455 | } |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 456 | connection_->UpdateFromIPConfig(ipconfig); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 457 | // SetConnection must occur after the UpdateFromIPConfig so the |
| 458 | // service can use the values derived from the connection. |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 459 | if (selected_service_) { |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 460 | selected_service_->SetConnection(connection_); |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 461 | } |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 462 | // 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); |
Christopher Wiley | 5519e9e | 2013-01-08 16:55:56 -0800 | [diff] [blame] | 466 | OnConnected(); |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 467 | portal_attempts_to_online_ = 0; |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 468 | // Subtle: Start portal detection after transitioning the service |
| 469 | // to the Connected state because this call may immediately transition |
| 470 | // to the Online state. |
Thieu Le | d176092 | 2012-09-11 14:15:35 -0700 | [diff] [blame] | 471 | if (selected_service_) { |
| 472 | StartPortalDetection(); |
| 473 | } |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 474 | StartLinkMonitor(); |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 475 | } else { |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 476 | // TODO(pstew): This logic gets yet more complex when multiple |
| 477 | // IPConfig types are run in parallel (e.g. DHCP and DHCP6) |
| 478 | if (selected_service_ && |
| 479 | selected_service_->static_ip_parameters().ContainsAddress()) { |
| 480 | // Consider three cases: |
| 481 | // |
| 482 | // 1. We're here because DHCP failed while starting up. There |
| 483 | // are two subcases: |
| 484 | // a. DHCP has failed, and Static IP config has _not yet_ |
| 485 | // completed. It's fine to do nothing, because we'll |
| 486 | // apply the static config shortly. |
| 487 | // b. DHCP has failed, and Static IP config has _already_ |
| 488 | // completed. It's fine to do nothing, because we can |
| 489 | // continue to use the static config that's already |
| 490 | // been applied. |
| 491 | // |
| 492 | // 2. We're here because a previously valid DHCP configuration |
| 493 | // is no longer valid. There's still a static IP config, |
| 494 | // because the condition in the if clause evaluated to true. |
| 495 | // Furthermore, the static config includes an IP address for |
| 496 | // us to use. |
| 497 | // |
| 498 | // The current configuration may include some DHCP |
| 499 | // parameters, overriden by any static parameters |
| 500 | // provided. We continue to use this configuration, because |
| 501 | // the only configuration element that is leased to us (IP |
| 502 | // address) will be overriden by a static parameter. |
| 503 | return; |
| 504 | } |
| 505 | |
Christopher Wiley | abd3b50 | 2012-09-26 13:08:52 -0700 | [diff] [blame] | 506 | if (selected_service_) { |
| 507 | Error error; |
| 508 | selected_service_->DisconnectWithFailure(Service::kFailureDHCP, &error); |
| 509 | } |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 510 | DestroyConnection(); |
Paul Stewart | c39f113 | 2011-06-22 12:02:28 -0700 | [diff] [blame] | 511 | } |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 512 | } |
| 513 | |
Christopher Wiley | 5519e9e | 2013-01-08 16:55:56 -0800 | [diff] [blame] | 514 | void Device::OnConnected() {} |
| 515 | |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 516 | void Device::CreateConnection() { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 517 | SLOG(Device, 2) << __func__; |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 518 | if (!connection_.get()) { |
Paul Stewart | bf66761 | 2012-06-29 14:49:54 -0700 | [diff] [blame] | 519 | connection_ = new Connection( |
| 520 | interface_index_, |
| 521 | link_name_, |
| 522 | technology_, |
| 523 | manager_->device_info(), |
| 524 | manager_->IsTechnologyShortDNSTimeoutEnabled(technology_)); |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 525 | } |
| 526 | } |
| 527 | |
| 528 | void Device::DestroyConnection() { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 529 | SLOG(Device, 2) << __func__; |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 530 | StopPortalDetection(); |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 531 | StopLinkMonitor(); |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 532 | if (selected_service_.get()) { |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 533 | selected_service_->SetConnection(NULL); |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 534 | } |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 535 | connection_ = NULL; |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 536 | } |
| 537 | |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 538 | void Device::SelectService(const ServiceRefPtr &service) { |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 539 | SLOG(Device, 2) << __func__ << ": service " |
| 540 | << (service ? service->unique_name() : "*reset*"); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 541 | |
| 542 | if (selected_service_.get() == service.get()) { |
| 543 | // No change to |selected_service_|. Return early to avoid |
| 544 | // changing its state. |
| 545 | return; |
| 546 | } |
| 547 | |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 548 | if (selected_service_.get()) { |
| 549 | if (selected_service_->state() != Service::kStateFailure) { |
| 550 | selected_service_->SetState(Service::kStateIdle); |
| 551 | } |
Paul Stewart | 20b0a09 | 2012-05-22 20:39:57 -0700 | [diff] [blame] | 552 | // Just in case the Device subclass has not already done so, make |
| 553 | // sure the previously selected service has its connection removed. |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 554 | selected_service_->SetConnection(NULL); |
Paul Stewart | c886061 | 2012-09-28 07:36:21 -0700 | [diff] [blame] | 555 | StopLinkMonitor(); |
| 556 | StopPortalDetection(); |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 557 | } |
| 558 | selected_service_ = service; |
| 559 | } |
| 560 | |
| 561 | void Device::SetServiceState(Service::ConnectState state) { |
| 562 | if (selected_service_.get()) { |
| 563 | selected_service_->SetState(state); |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | void Device::SetServiceFailure(Service::ConnectFailure failure_state) { |
| 568 | if (selected_service_.get()) { |
| 569 | selected_service_->SetFailure(failure_state); |
| 570 | } |
| 571 | } |
| 572 | |
Eric Shienbrood | cc95c5d | 2012-03-30 15:25:49 -0400 | [diff] [blame] | 573 | void Device::SetServiceFailureSilent(Service::ConnectFailure failure_state) { |
| 574 | if (selected_service_.get()) { |
| 575 | selected_service_->SetFailureSilent(failure_state); |
| 576 | } |
| 577 | } |
| 578 | |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 579 | string Device::SerializeIPConfigs(const string &suffix) { |
| 580 | return StringPrintf("%s:%s", suffix.c_str(), ipconfig_->type().c_str()); |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 581 | } |
| 582 | |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 583 | bool Device::SetIPFlag(IPAddress::Family family, const string &flag, |
| 584 | const string &value) { |
| 585 | string ip_version; |
| 586 | if (family == IPAddress::kFamilyIPv4) { |
| 587 | ip_version = kIPFlagVersion4; |
| 588 | } else if (family == IPAddress::kFamilyIPv6) { |
| 589 | ip_version = kIPFlagVersion6; |
| 590 | } else { |
| 591 | NOTIMPLEMENTED(); |
| 592 | } |
| 593 | FilePath flag_file(StringPrintf(kIPFlagTemplate, ip_version.c_str(), |
| 594 | link_name_.c_str(), flag.c_str())); |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 595 | SLOG(Device, 2) << "Writing " << value << " to flag file " |
| 596 | << flag_file.value(); |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 597 | if (file_util::WriteFile(flag_file, value.c_str(), value.length()) != 1) { |
| 598 | LOG(ERROR) << StringPrintf("IP flag write failed: %s to %s", |
| 599 | value.c_str(), flag_file.value().c_str()); |
| 600 | return false; |
| 601 | } |
| 602 | return true; |
| 603 | } |
| 604 | |
Paul Stewart | 6ff27f5 | 2012-07-11 06:51:41 -0700 | [diff] [blame] | 605 | void Device::ResetByteCounters() { |
| 606 | manager_->device_info()->GetByteCounts( |
| 607 | interface_index_, &receive_byte_offset_, &transmit_byte_offset_); |
| 608 | manager_->UpdateDevice(this); |
| 609 | } |
| 610 | |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 611 | bool Device::RestartPortalDetection() { |
| 612 | StopPortalDetection(); |
| 613 | return StartPortalDetection(); |
| 614 | } |
| 615 | |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 616 | bool Device::RequestPortalDetection() { |
| 617 | if (!selected_service_) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 618 | SLOG(Device, 2) << FriendlyName() |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 619 | << ": No selected service, so no need for portal check."; |
| 620 | return false; |
| 621 | } |
| 622 | |
| 623 | if (!connection_.get()) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 624 | SLOG(Device, 2) << FriendlyName() |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 625 | << ": No connection, so no need for portal check."; |
| 626 | return false; |
| 627 | } |
| 628 | |
| 629 | if (selected_service_->state() != Service::kStatePortal) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 630 | SLOG(Device, 2) << FriendlyName() |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 631 | << ": Service is not in portal state. No need to start check."; |
| 632 | return false; |
| 633 | } |
| 634 | |
| 635 | if (!connection_->is_default()) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 636 | SLOG(Device, 2) << FriendlyName() |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 637 | << ": Service is not the default connection. Don't start check."; |
| 638 | return false; |
| 639 | } |
| 640 | |
| 641 | if (portal_detector_.get() && portal_detector_->IsInProgress()) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 642 | SLOG(Device, 2) << FriendlyName() |
| 643 | << ": Portal detection is already running."; |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 644 | return true; |
| 645 | } |
| 646 | |
| 647 | return StartPortalDetection(); |
| 648 | } |
| 649 | |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 650 | bool Device::StartPortalDetection() { |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 651 | DCHECK(selected_service_); |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 652 | if (selected_service_->IsPortalDetectionDisabled()) { |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 653 | SLOG(Device, 2) << "Service " << selected_service_->unique_name() |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 654 | << ": Portal detection is disabled; " |
| 655 | << "marking service online."; |
| 656 | SetServiceConnectedState(Service::kStateOnline); |
| 657 | return false; |
| 658 | } |
| 659 | |
| 660 | if (selected_service_->IsPortalDetectionAuto() && |
| 661 | !manager_->IsPortalDetectionEnabled(technology())) { |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 662 | // If portal detection is disabled for this technology, immediately set |
| 663 | // the service state to "Online". |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 664 | SLOG(Device, 2) << "Device " << FriendlyName() |
| 665 | << ": Portal detection is disabled; " |
| 666 | << "marking service online."; |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 667 | SetServiceConnectedState(Service::kStateOnline); |
| 668 | return false; |
| 669 | } |
| 670 | |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 671 | if (selected_service_->HasProxyConfig()) { |
| 672 | // Services with HTTP proxy configurations should not be checked by the |
| 673 | // connection manager, since we don't have the ability to evaluate |
| 674 | // arbitrary proxy configs and their possible credentials. |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 675 | SLOG(Device, 2) << "Device " << FriendlyName() |
| 676 | << ": Service has proxy config; marking it online."; |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 677 | SetServiceConnectedState(Service::kStateOnline); |
| 678 | return false; |
| 679 | } |
| 680 | |
| 681 | portal_detector_.reset(new PortalDetector(connection_, |
| 682 | dispatcher_, |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 683 | portal_detector_callback_)); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 684 | if (!portal_detector_->Start(manager_->GetPortalCheckURL())) { |
| 685 | LOG(ERROR) << "Device " << FriendlyName() |
| 686 | << ": Portal detection failed to start: likely bad URL: " |
| 687 | << manager_->GetPortalCheckURL(); |
| 688 | SetServiceConnectedState(Service::kStateOnline); |
| 689 | return false; |
| 690 | } |
| 691 | |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 692 | SLOG(Device, 2) << "Device " << FriendlyName() |
| 693 | << ": Portal detection has started."; |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 694 | return true; |
| 695 | } |
| 696 | |
| 697 | void Device::StopPortalDetection() { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 698 | SLOG(Device, 2) << "Device " << FriendlyName() |
| 699 | << ": Portal detection has stopped."; |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 700 | portal_detector_.reset(); |
| 701 | } |
| 702 | |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 703 | void Device::set_link_monitor(LinkMonitor *link_monitor) { |
| 704 | link_monitor_.reset(link_monitor); |
| 705 | } |
| 706 | |
| 707 | bool Device::StartLinkMonitor() { |
| 708 | if (!manager_->IsTechnologyLinkMonitorEnabled(technology())) { |
| 709 | SLOG(Device, 2) << "Device " << FriendlyName() |
| 710 | << ": Link Monitoring is disabled."; |
| 711 | return false; |
| 712 | } |
| 713 | |
| 714 | if (!link_monitor()) { |
| 715 | set_link_monitor( |
| 716 | new LinkMonitor( |
| 717 | connection_, dispatcher_, metrics(), manager_->device_info(), |
| 718 | Bind(&Device::OnLinkMonitorFailure, weak_ptr_factory_.GetWeakPtr()))); |
| 719 | } |
| 720 | |
| 721 | SLOG(Device, 2) << "Device " << FriendlyName() |
| 722 | << ": Link Monitor starting."; |
| 723 | return link_monitor_->Start(); |
| 724 | } |
| 725 | |
| 726 | void Device::StopLinkMonitor() { |
| 727 | SLOG(Device, 2) << "Device " << FriendlyName() |
| 728 | << ": Link Monitor stopping."; |
| 729 | link_monitor_.reset(); |
| 730 | } |
| 731 | |
| 732 | void Device::OnLinkMonitorFailure() { |
| 733 | LOG(ERROR) << "Device " << FriendlyName() |
| 734 | << ": Link Monitor indicates failure."; |
| 735 | } |
| 736 | |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 737 | void Device::SetServiceConnectedState(Service::ConnectState state) { |
| 738 | DCHECK(selected_service_.get()); |
| 739 | |
| 740 | if (!selected_service_.get()) { |
| 741 | LOG(ERROR) << FriendlyName() << ": " |
| 742 | << "Portal detection completed but no selected service exists!"; |
| 743 | return; |
| 744 | } |
| 745 | |
| 746 | if (!selected_service_->IsConnected()) { |
| 747 | LOG(ERROR) << FriendlyName() << ": " |
| 748 | << "Portal detection completed but selected service " |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 749 | << selected_service_->unique_name() |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 750 | << " is in non-connected state."; |
| 751 | return; |
| 752 | } |
| 753 | |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 754 | if (state == Service::kStatePortal && connection_->is_default() && |
| 755 | manager_->GetPortalCheckInterval() != 0) { |
| 756 | CHECK(portal_detector_.get()); |
| 757 | if (!portal_detector_->StartAfterDelay( |
| 758 | manager_->GetPortalCheckURL(), |
| 759 | manager_->GetPortalCheckInterval())) { |
| 760 | LOG(ERROR) << "Device " << FriendlyName() |
| 761 | << ": Portal detection failed to restart: likely bad URL: " |
| 762 | << manager_->GetPortalCheckURL(); |
| 763 | SetServiceState(Service::kStateOnline); |
| 764 | portal_detector_.reset(); |
| 765 | return; |
| 766 | } |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 767 | SLOG(Device, 2) << "Device " << FriendlyName() |
| 768 | << ": Portal detection retrying."; |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 769 | } else { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 770 | SLOG(Device, 2) << "Device " << FriendlyName() |
| 771 | << ": Portal will not retry."; |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 772 | portal_detector_.reset(); |
| 773 | } |
| 774 | |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 775 | SetServiceState(state); |
| 776 | } |
| 777 | |
| 778 | void Device::PortalDetectorCallback(const PortalDetector::Result &result) { |
| 779 | if (!result.final) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 780 | SLOG(Device, 2) << "Device " << FriendlyName() |
| 781 | << ": Received non-final status: " |
| 782 | << PortalDetector::StatusToString(result.status); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 783 | return; |
| 784 | } |
| 785 | |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 786 | SLOG(Device, 2) << "Device " << FriendlyName() |
| 787 | << ": Received final status: " |
| 788 | << PortalDetector::StatusToString(result.status); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 789 | |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 790 | portal_attempts_to_online_ += result.num_attempts; |
| 791 | |
| 792 | metrics()->SendEnumToUMA( |
| 793 | metrics()->GetFullMetricName(Metrics::kMetricPortalResult, technology()), |
| 794 | Metrics::PortalDetectionResultToEnum(result), |
| 795 | Metrics::kPortalResultMax); |
| 796 | |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 797 | if (result.status == PortalDetector::kStatusSuccess) { |
| 798 | SetServiceConnectedState(Service::kStateOnline); |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 799 | |
| 800 | metrics()->SendToUMA( |
| 801 | metrics()->GetFullMetricName( |
| 802 | Metrics::kMetricPortalAttemptsToOnline, technology()), |
| 803 | portal_attempts_to_online_, |
| 804 | Metrics::kMetricPortalAttemptsToOnlineMin, |
| 805 | Metrics::kMetricPortalAttemptsToOnlineMax, |
| 806 | Metrics::kMetricPortalAttemptsToOnlineNumBuckets); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 807 | } else { |
| 808 | SetServiceConnectedState(Service::kStatePortal); |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 809 | |
| 810 | metrics()->SendToUMA( |
| 811 | metrics()->GetFullMetricName( |
| 812 | Metrics::kMetricPortalAttempts, technology()), |
| 813 | result.num_attempts, |
| 814 | Metrics::kMetricPortalAttemptsMin, |
| 815 | Metrics::kMetricPortalAttemptsMax, |
| 816 | Metrics::kMetricPortalAttemptsNumBuckets); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 817 | } |
| 818 | } |
| 819 | |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 820 | vector<string> Device::AvailableIPConfigs(Error */*error*/) { |
Jason Glasgow | 08afdff | 2012-04-03 10:22:26 -0400 | [diff] [blame] | 821 | if (ipconfig_.get()) { |
| 822 | string id = ipconfig_->GetRpcIdentifier(); |
| 823 | return vector<string>(1, id); |
| 824 | } |
| 825 | return vector<string>(); |
Chris Masone | 4e85161 | 2011-07-01 10:46:53 -0700 | [diff] [blame] | 826 | } |
| 827 | |
| 828 | string Device::GetRpcConnectionIdentifier() { |
| 829 | return adaptor_->GetRpcConnectionIdentifier(); |
| 830 | } |
| 831 | |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 832 | uint64 Device::GetLinkMonitorResponseTime(Error *error) { |
| 833 | if (!link_monitor_.get()) { |
| 834 | // It is not strictly an error that the link monitor does not |
| 835 | // exist, but returning an error here allows the GetProperties |
| 836 | // call in our Adaptor to omit this parameter. |
| 837 | error->Populate(Error::kNotFound, "Device is not running LinkMonitor"); |
| 838 | return 0; |
| 839 | } |
| 840 | return link_monitor_->GetResponseTimeMilliseconds(); |
| 841 | } |
| 842 | |
Paul Stewart | 6ff27f5 | 2012-07-11 06:51:41 -0700 | [diff] [blame] | 843 | uint64 Device::GetReceiveByteCount(Error */*error*/) { |
| 844 | uint64 rx_byte_count = 0, tx_byte_count = 0; |
| 845 | manager_->device_info()->GetByteCounts( |
| 846 | interface_index_, &rx_byte_count, &tx_byte_count); |
| 847 | return rx_byte_count - receive_byte_offset_; |
| 848 | } |
| 849 | |
| 850 | uint64 Device::GetTransmitByteCount(Error */*error*/) { |
| 851 | uint64 rx_byte_count = 0, tx_byte_count = 0; |
| 852 | manager_->device_info()->GetByteCounts( |
| 853 | interface_index_, &rx_byte_count, &tx_byte_count); |
| 854 | return tx_byte_count - transmit_byte_offset_; |
| 855 | } |
| 856 | |
Eric Shienbrood | 7fce52c | 2012-04-13 19:11:02 -0400 | [diff] [blame] | 857 | bool Device::IsUnderlyingDeviceEnabled() const { |
| 858 | return false; |
| 859 | } |
| 860 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 861 | // callback |
| 862 | void Device::OnEnabledStateChanged(const ResultCallback &callback, |
| 863 | const Error &error) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 864 | SLOG(Device, 2) << __func__ << "(" << enabled_pending_ << ")"; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 865 | if (error.IsSuccess()) { |
| 866 | enabled_ = enabled_pending_; |
| 867 | manager_->UpdateEnabledTechnologies(); |
| 868 | adaptor_->EmitBoolChanged(flimflam::kPoweredProperty, enabled_); |
| 869 | adaptor_->UpdateEnabled(); |
| 870 | } |
Gary Morain | baeefdf | 2012-04-30 14:53:35 -0700 | [diff] [blame] | 871 | enabled_pending_ = enabled_; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 872 | if (!callback.is_null()) |
| 873 | callback.Run(error); |
| 874 | } |
| 875 | |
| 876 | void Device::SetEnabled(bool enable) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 877 | SLOG(Device, 2) << __func__ << "(" << enable << ")"; |
Jason Glasgow | 4a49079 | 2012-04-10 15:02:05 -0400 | [diff] [blame] | 878 | Error error; |
| 879 | SetEnabledInternal(enable, false, &error, ResultCallback()); |
Jason Glasgow | 7234ec3 | 2012-05-23 16:01:21 -0400 | [diff] [blame] | 880 | LOG_IF(ERROR, error.IsFailure() && !error.IsOngoing()) |
Jason Glasgow | 4a49079 | 2012-04-10 15:02:05 -0400 | [diff] [blame] | 881 | << "Enabled failed, but no way to report the failure."; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 882 | } |
| 883 | |
| 884 | void Device::SetEnabledPersistent(bool enable, |
| 885 | Error *error, |
| 886 | const ResultCallback &callback) { |
| 887 | SetEnabledInternal(enable, true, error, callback); |
| 888 | } |
| 889 | |
| 890 | void Device::SetEnabledInternal(bool enable, |
| 891 | bool persist, |
| 892 | Error *error, |
| 893 | const ResultCallback &callback) { |
Jason Glasgow | 4a49079 | 2012-04-10 15:02:05 -0400 | [diff] [blame] | 894 | DCHECK(error); |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 895 | SLOG(Device, 2) << "Device " << link_name_ << " " |
| 896 | << (enable ? "starting" : "stopping"); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 897 | if (enable == enabled_) { |
Jason Glasgow | 4a49079 | 2012-04-10 15:02:05 -0400 | [diff] [blame] | 898 | error->Reset(); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 899 | return; |
| 900 | } |
| 901 | |
| 902 | if (enabled_pending_ == enable) { |
Jason Glasgow | 4a49079 | 2012-04-10 15:02:05 -0400 | [diff] [blame] | 903 | Error::PopulateAndLog(error, Error::kInProgress, |
| 904 | "Enable operation already in progress"); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 905 | return; |
| 906 | } |
| 907 | |
| 908 | if (persist) { |
| 909 | enabled_persistent_ = enable; |
Darin Petkov | e7c6ad3 | 2012-06-29 10:22:09 +0200 | [diff] [blame] | 910 | manager_->UpdateDevice(this); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 911 | } |
| 912 | |
| 913 | enabled_pending_ = enable; |
| 914 | EnabledStateChangedCallback enabled_callback = |
| 915 | Bind(&Device::OnEnabledStateChanged, |
| 916 | weak_ptr_factory_.GetWeakPtr(), callback); |
| 917 | if (enable) { |
| 918 | running_ = true; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 919 | Start(error, enabled_callback); |
| 920 | } else { |
| 921 | running_ = false; |
| 922 | DestroyIPConfig(); // breaks a reference cycle |
| 923 | SelectService(NULL); // breaks a reference cycle |
| 924 | rtnl_handler_->SetInterfaceFlags(interface_index(), 0, IFF_UP); |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 925 | SLOG(Device, 3) << "Device " << link_name_ << " ipconfig_ " |
| 926 | << (ipconfig_ ? "is set." : "is not set."); |
| 927 | SLOG(Device, 3) << "Device " << link_name_ << " connection_ " |
| 928 | << (connection_ ? "is set." : "is not set."); |
| 929 | SLOG(Device, 3) << "Device " << link_name_ << " selected_service_ " |
| 930 | << (selected_service_ ? "is set." : "is not set."); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 931 | Stop(error, enabled_callback); |
| 932 | } |
| 933 | } |
| 934 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 935 | } // namespace shill |