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 | |
Arman Uguray | f84a424 | 2013-04-09 20:01:07 -0700 | [diff] [blame] | 21 | #include "shill/async_connection.h" |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 22 | #include "shill/connection.h" |
Arman Uguray | f84a424 | 2013-04-09 20:01:07 -0700 | [diff] [blame] | 23 | #include "shill/connection_health_checker.h" |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 24 | #include "shill/control_interface.h" |
Chris Masone | d7732e4 | 2011-05-20 11:08:56 -0700 | [diff] [blame] | 25 | #include "shill/device_dbus_adaptor.h" |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 26 | #include "shill/dhcp_config.h" |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 27 | #include "shill/dhcp_provider.h" |
Arman Uguray | f84a424 | 2013-04-09 20:01:07 -0700 | [diff] [blame] | 28 | #include "shill/dns_client.h" |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 29 | #include "shill/error.h" |
Paul Stewart | 26b327e | 2011-10-19 11:38:09 -0700 | [diff] [blame] | 30 | #include "shill/event_dispatcher.h" |
Gaurav Shah | 6d2c72d | 2012-10-16 16:30:44 -0700 | [diff] [blame] | 31 | #include "shill/geolocation_info.h" |
Paul Stewart | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 32 | #include "shill/http_proxy.h" |
Arman Uguray | f84a424 | 2013-04-09 20:01:07 -0700 | [diff] [blame] | 33 | #include "shill/ip_address.h" |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 34 | #include "shill/link_monitor.h" |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 35 | #include "shill/logging.h" |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 36 | #include "shill/manager.h" |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 37 | #include "shill/metrics.h" |
Chris Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame] | 38 | #include "shill/property_accessor.h" |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 39 | #include "shill/refptr_types.h" |
Paul Stewart | c39f113 | 2011-06-22 12:02:28 -0700 | [diff] [blame] | 40 | #include "shill/rtnl_handler.h" |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 41 | #include "shill/service.h" |
Arman Uguray | f84a424 | 2013-04-09 20:01:07 -0700 | [diff] [blame] | 42 | #include "shill/socket_info_reader.h" |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 43 | #include "shill/store_interface.h" |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 44 | #include "shill/technology.h" |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 45 | #include "shill/traffic_monitor.h" |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 46 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 47 | using base::Bind; |
Albert Chaulk | 0e1cdea | 2013-02-27 15:32:55 -0800 | [diff] [blame] | 48 | using base::FilePath; |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 49 | using base::StringPrintf; |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 50 | using std::string; |
| 51 | using std::vector; |
| 52 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 53 | namespace shill { |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 54 | |
| 55 | // static |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 56 | const char Device::kIPFlagTemplate[] = "/proc/sys/net/%s/conf/%s/%s"; |
| 57 | // static |
| 58 | const char Device::kIPFlagVersion4[] = "ipv4"; |
| 59 | // static |
| 60 | const char Device::kIPFlagVersion6[] = "ipv6"; |
| 61 | // static |
| 62 | const char Device::kIPFlagDisableIPv6[] = "disable_ipv6"; |
| 63 | // static |
| 64 | const char Device::kIPFlagUseTempAddr[] = "use_tempaddr"; |
| 65 | // static |
| 66 | const char Device::kIPFlagUseTempAddrUsedAndDefault[] = "2"; |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 67 | // static |
| 68 | const char Device::kIPFlagReversePathFilter[] = "rp_filter"; |
| 69 | // static |
| 70 | const char Device::kIPFlagReversePathFilterEnabled[] = "1"; |
| 71 | // static |
| 72 | const char Device::kIPFlagReversePathFilterLooseMode[] = "2"; |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 73 | // static |
Paul Stewart | 41a071e | 2013-04-26 07:56:04 -0700 | [diff] [blame] | 74 | const char Device::kStorageIPConfigs[] = "IPConfigs"; |
| 75 | // static |
Prathmesh Prabhu | ba99b59 | 2013-04-17 15:13:14 -0700 | [diff] [blame] | 76 | const char Device::kStoragePowered[] = "Powered"; |
| 77 | // static |
Paul Stewart | 6ff27f5 | 2012-07-11 06:51:41 -0700 | [diff] [blame] | 78 | const char Device::kStorageReceiveByteCount[] = "ReceiveByteCount"; |
| 79 | // static |
| 80 | const char Device::kStorageTransmitByteCount[] = "TransmitByteCount"; |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 81 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 82 | Device::Device(ControlInterface *control_interface, |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 83 | EventDispatcher *dispatcher, |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 84 | Metrics *metrics, |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 85 | Manager *manager, |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 86 | const string &link_name, |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 87 | const string &address, |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 88 | int interface_index, |
| 89 | Technology::Identifier technology) |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 90 | : enabled_(false), |
| 91 | enabled_persistent_(true), |
| 92 | enabled_pending_(enabled_), |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 93 | reconnect_(true), |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 94 | hardware_address_(address), |
mukesh agrawal | f60e406 | 2011-05-27 13:13:41 -0700 | [diff] [blame] | 95 | interface_index_(interface_index), |
| 96 | running_(false), |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 97 | link_name_(link_name), |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 98 | unique_id_(link_name), |
Darin Petkov | d966195 | 2011-08-03 16:25:42 -0700 | [diff] [blame] | 99 | control_interface_(control_interface), |
| 100 | dispatcher_(dispatcher), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 101 | metrics_(metrics), |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 102 | manager_(manager), |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 103 | weak_ptr_factory_(this), |
Darin Petkov | 77cb681 | 2011-08-15 16:19:41 -0700 | [diff] [blame] | 104 | adaptor_(control_interface->CreateDeviceAdaptor(this)), |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 105 | traffic_monitor_enabled_(false), |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 106 | portal_detector_callback_(Bind(&Device::PortalDetectorCallback, |
| 107 | weak_ptr_factory_.GetWeakPtr())), |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 108 | technology_(technology), |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 109 | portal_attempts_to_online_(0), |
Paul Stewart | 6ff27f5 | 2012-07-11 06:51:41 -0700 | [diff] [blame] | 110 | receive_byte_offset_(0), |
| 111 | transmit_byte_offset_(0), |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 112 | dhcp_provider_(DHCPProvider::GetInstance()), |
| 113 | rtnl_handler_(RTNLHandler::GetInstance()) { |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 114 | store_.RegisterConstString(flimflam::kAddressProperty, &hardware_address_); |
Chris Masone | 4d42df8 | 2011-07-02 17:09:39 -0700 | [diff] [blame] | 115 | |
| 116 | // flimflam::kBgscanMethodProperty: Registered in WiFi |
| 117 | // flimflam::kBgscanShortIntervalProperty: Registered in WiFi |
| 118 | // flimflam::kBgscanSignalThresholdProperty: Registered in WiFi |
| 119 | |
| 120 | // flimflam::kCellularAllowRoamingProperty: Registered in Cellular |
| 121 | // flimflam::kCarrierProperty: Registered in Cellular |
| 122 | // flimflam::kEsnProperty: Registered in Cellular |
Darin Petkov | 3335b37 | 2011-08-22 11:05:32 -0700 | [diff] [blame] | 123 | // flimflam::kHomeProviderProperty: Registered in Cellular |
Chris Masone | 4d42df8 | 2011-07-02 17:09:39 -0700 | [diff] [blame] | 124 | // flimflam::kImeiProperty: Registered in Cellular |
Ben Chan | a5e2708 | 2012-07-31 14:30:28 -0700 | [diff] [blame] | 125 | // flimflam::kIccidProperty: Registered in Cellular |
Chris Masone | 4d42df8 | 2011-07-02 17:09:39 -0700 | [diff] [blame] | 126 | // flimflam::kImsiProperty: Registered in Cellular |
| 127 | // flimflam::kManufacturerProperty: Registered in Cellular |
| 128 | // flimflam::kMdnProperty: Registered in Cellular |
| 129 | // flimflam::kMeidProperty: Registered in Cellular |
| 130 | // flimflam::kMinProperty: Registered in Cellular |
| 131 | // flimflam::kModelIDProperty: Registered in Cellular |
| 132 | // flimflam::kFirmwareRevisionProperty: Registered in Cellular |
| 133 | // flimflam::kHardwareRevisionProperty: Registered in Cellular |
| 134 | // flimflam::kPRLVersionProperty: Registered in Cellular |
| 135 | // flimflam::kSIMLockStatusProperty: Registered in Cellular |
| 136 | // flimflam::kFoundNetworksProperty: Registered in Cellular |
Darin Petkov | e9d12e0 | 2011-07-27 15:09:37 -0700 | [diff] [blame] | 137 | // flimflam::kDBusConnectionProperty: Registered in Cellular |
| 138 | // flimflam::kDBusObjectProperty: Register in Cellular |
Chris Masone | 4d42df8 | 2011-07-02 17:09:39 -0700 | [diff] [blame] | 139 | |
Jason Glasgow | e8334fd | 2012-03-30 16:02:37 -0400 | [diff] [blame] | 140 | store_.RegisterConstString(flimflam::kInterfaceProperty, &link_name_); |
Jason Glasgow | 08afdff | 2012-04-03 10:22:26 -0400 | [diff] [blame] | 141 | HelpRegisterConstDerivedRpcIdentifiers(flimflam::kIPConfigsProperty, |
| 142 | &Device::AvailableIPConfigs); |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 143 | store_.RegisterConstString(flimflam::kNameProperty, &link_name_); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 144 | store_.RegisterConstBool(flimflam::kPoweredProperty, &enabled_); |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 145 | HelpRegisterConstDerivedString(flimflam::kTypeProperty, |
| 146 | &Device::GetTechnologyString); |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 147 | HelpRegisterConstDerivedUint64(shill::kLinkMonitorResponseTimeProperty, |
| 148 | &Device::GetLinkMonitorResponseTime); |
Jason Glasgow | b579005 | 2012-01-27 01:03:52 -0500 | [diff] [blame] | 149 | |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 150 | // TODO(cmasone): Chrome doesn't use this...does anyone? |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 151 | // store_.RegisterConstBool(flimflam::kReconnectProperty, &reconnect_); |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 152 | |
Chris Masone | 4e85161 | 2011-07-01 10:46:53 -0700 | [diff] [blame] | 153 | // TODO(cmasone): Figure out what shill concept maps to flimflam's "Network". |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 154 | // known_properties_.push_back(flimflam::kNetworksProperty); |
| 155 | |
Chris Masone | 4d42df8 | 2011-07-02 17:09:39 -0700 | [diff] [blame] | 156 | // flimflam::kScanningProperty: Registered in WiFi, Cellular |
| 157 | // flimflam::kScanIntervalProperty: Registered in WiFi, Cellular |
Paul Stewart | 6ff27f5 | 2012-07-11 06:51:41 -0700 | [diff] [blame] | 158 | |
| 159 | if (manager_ && manager_->device_info()) { // Unit tests may not have these. |
| 160 | manager_->device_info()->GetByteCounts( |
| 161 | interface_index_, &receive_byte_offset_, &transmit_byte_offset_); |
| 162 | HelpRegisterConstDerivedUint64(shill::kReceiveByteCountProperty, |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 163 | &Device::GetReceiveByteCountProperty); |
Paul Stewart | 6ff27f5 | 2012-07-11 06:51:41 -0700 | [diff] [blame] | 164 | HelpRegisterConstDerivedUint64(shill::kTransmitByteCountProperty, |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 165 | &Device::GetTransmitByteCountProperty); |
Paul Stewart | 6ff27f5 | 2012-07-11 06:51:41 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Darin Petkov | a0a0efe | 2012-06-27 12:50:01 +0200 | [diff] [blame] | 168 | LOG(INFO) << "Device created: " << link_name_ |
| 169 | << " index " << interface_index_; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | Device::~Device() { |
Darin Petkov | a0a0efe | 2012-06-27 12:50:01 +0200 | [diff] [blame] | 173 | LOG(INFO) << "Device destructed: " << link_name_ |
| 174 | << " index " << interface_index_; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 177 | void Device::LinkEvent(unsigned flags, unsigned change) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 178 | SLOG(Device, 2) << "Device " << link_name_ |
| 179 | << std::showbase << std::hex |
| 180 | << " flags " << flags << " changed " << change |
| 181 | << std::dec << std::noshowbase; |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Wade Guthrie | 68d4109 | 2013-04-02 12:56:02 -0700 | [diff] [blame] | 184 | void Device::Scan(ScanType scan_type, Error *error) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 185 | SLOG(Device, 2) << "Device " << link_name_ << " scan requested."; |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame] | 186 | Error::PopulateAndLog(error, Error::kNotSupported, |
| 187 | "Device doesn't support scan."); |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 190 | void Device::RegisterOnNetwork(const std::string &/*network_id*/, Error *error, |
| 191 | const ResultCallback &/*callback*/) { |
| 192 | Error::PopulateAndLog(error, Error::kNotSupported, |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame] | 193 | "Device doesn't support network registration."); |
Darin Petkov | 9ae310f | 2011-08-30 15:41:13 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Darin Petkov | c64fe5e | 2012-01-11 12:46:13 +0100 | [diff] [blame] | 196 | void Device::RequirePIN( |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 197 | const string &/*pin*/, bool /*require*/, |
| 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 RequirePIN."); |
Darin Petkov | e42e101 | 2011-08-31 12:35:04 -0700 | [diff] [blame] | 202 | } |
| 203 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 204 | void Device::EnterPIN(const string &/*pin*/, |
| 205 | Error *error, const ResultCallback &/*callback*/) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 206 | SLOG(Device, 2) << __func__; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 207 | Error::PopulateAndLog(error, Error::kNotSupported, |
| 208 | "Device doesn't support EnterPIN."); |
Darin Petkov | e42e101 | 2011-08-31 12:35:04 -0700 | [diff] [blame] | 209 | } |
| 210 | |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 211 | void Device::UnblockPIN(const string &/*unblock_code*/, |
| 212 | const string &/*pin*/, |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 213 | Error *error, const ResultCallback &/*callback*/) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 214 | SLOG(Device, 2) << __func__; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 215 | Error::PopulateAndLog(error, Error::kNotSupported, |
| 216 | "Device doesn't support UnblockPIN."); |
Darin Petkov | e42e101 | 2011-08-31 12:35:04 -0700 | [diff] [blame] | 217 | } |
| 218 | |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 219 | void Device::ChangePIN(const string &/*old_pin*/, |
| 220 | const string &/*new_pin*/, |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 221 | Error *error, const ResultCallback &/*callback*/) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 222 | SLOG(Device, 2) << __func__; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 223 | Error::PopulateAndLog(error, Error::kNotSupported, |
| 224 | "Device doesn't support ChangePIN."); |
Darin Petkov | e42e101 | 2011-08-31 12:35:04 -0700 | [diff] [blame] | 225 | } |
| 226 | |
Ben Chan | ad663e1 | 2013-01-08 01:58:47 -0800 | [diff] [blame] | 227 | void Device::Reset(Error *error, const ResultCallback &/*callback*/) { |
| 228 | SLOG(Device, 2) << __func__; |
| 229 | Error::PopulateAndLog(error, Error::kNotSupported, |
| 230 | "Device doesn't support Reset."); |
| 231 | } |
| 232 | |
Darin Petkov | c37a9c4 | 2012-09-06 15:28:22 +0200 | [diff] [blame] | 233 | void Device::SetCarrier(const string &/*carrier*/, |
| 234 | Error *error, const ResultCallback &/*callback*/) { |
| 235 | SLOG(Device, 2) << __func__; |
| 236 | Error::PopulateAndLog(error, Error::kNotSupported, |
| 237 | "Device doesn't support SetCarrier."); |
| 238 | } |
| 239 | |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 240 | void Device::DisableIPv6() { |
| 241 | SetIPFlag(IPAddress::kFamilyIPv6, kIPFlagDisableIPv6, "1"); |
| 242 | } |
| 243 | |
| 244 | void Device::EnableIPv6() { |
| 245 | SetIPFlag(IPAddress::kFamilyIPv6, kIPFlagDisableIPv6, "0"); |
| 246 | } |
| 247 | |
| 248 | void Device::EnableIPv6Privacy() { |
| 249 | SetIPFlag(IPAddress::kFamilyIPv6, kIPFlagUseTempAddr, |
| 250 | kIPFlagUseTempAddrUsedAndDefault); |
| 251 | } |
| 252 | |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 253 | void Device::DisableReversePathFilter() { |
| 254 | // TODO(pstew): Current kernel doesn't offer reverse-path filtering flag |
Paul Stewart | ee6b3d7 | 2013-07-12 16:07:51 -0700 | [diff] [blame] | 255 | // for IPv6. crbug.com/207193 |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 256 | SetIPFlag(IPAddress::kFamilyIPv4, kIPFlagReversePathFilter, |
| 257 | kIPFlagReversePathFilterLooseMode); |
| 258 | } |
| 259 | |
| 260 | void Device::EnableReversePathFilter() { |
| 261 | SetIPFlag(IPAddress::kFamilyIPv4, kIPFlagReversePathFilter, |
| 262 | kIPFlagReversePathFilterEnabled); |
| 263 | } |
| 264 | |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 265 | bool Device::IsConnected() const { |
| 266 | if (selected_service_) |
| 267 | return selected_service_->IsConnected(); |
| 268 | return false; |
| 269 | } |
| 270 | |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 271 | bool Device::IsConnectedToService(const ServiceRefPtr &service) const { |
| 272 | return service == selected_service_ && IsConnected(); |
| 273 | } |
| 274 | |
mukesh agrawal | f6b3209 | 2013-04-10 15:49:55 -0700 | [diff] [blame] | 275 | string Device::GetRpcIdentifier() const { |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 276 | return adaptor_->GetRpcIdentifier(); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 277 | } |
| 278 | |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 279 | string Device::GetStorageIdentifier() { |
| 280 | string id = GetRpcIdentifier(); |
| 281 | ControlInterface::RpcIdToStorageId(&id); |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 282 | size_t needle = id.find('_'); |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 283 | DLOG_IF(ERROR, needle == string::npos) << "No _ in storage id?!?!"; |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 284 | id.replace(id.begin() + needle + 1, id.end(), hardware_address_); |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 285 | return id; |
| 286 | } |
| 287 | |
Gaurav Shah | 6d2c72d | 2012-10-16 16:30:44 -0700 | [diff] [blame] | 288 | vector<GeolocationInfo> Device::GetGeolocationObjects() const { |
| 289 | return vector<GeolocationInfo>(); |
| 290 | }; |
| 291 | |
Jason Glasgow | b579005 | 2012-01-27 01:03:52 -0500 | [diff] [blame] | 292 | string Device::GetTechnologyString(Error */*error*/) { |
| 293 | return Technology::NameFromIdentifier(technology()); |
| 294 | } |
| 295 | |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 296 | const string& Device::FriendlyName() const { |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 297 | return link_name_; |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 298 | } |
| 299 | |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 300 | const string& Device::UniqueName() const { |
| 301 | return unique_id_; |
| 302 | } |
| 303 | |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 304 | bool Device::Load(StoreInterface *storage) { |
| 305 | const string id = GetStorageIdentifier(); |
| 306 | if (!storage->ContainsGroup(id)) { |
| 307 | LOG(WARNING) << "Device is not available in the persistent store: " << id; |
| 308 | return false; |
| 309 | } |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 310 | enabled_persistent_ = true; |
| 311 | storage->GetBool(id, kStoragePowered, &enabled_persistent_); |
Paul Stewart | 6ff27f5 | 2012-07-11 06:51:41 -0700 | [diff] [blame] | 312 | uint64 rx_byte_count = 0, tx_byte_count = 0; |
| 313 | |
| 314 | manager_->device_info()->GetByteCounts( |
| 315 | interface_index_, &rx_byte_count, &tx_byte_count); |
| 316 | // If there is a byte-count present in the profile, the return value |
| 317 | // of Device::Get*ByteCount() should be the this stored value plus |
| 318 | // whatever additional bytes we receive since time-of-load. We |
| 319 | // accomplish this by the subtractions below, which can validly |
| 320 | // roll over "negative" in the subtractions below and in Get*ByteCount. |
| 321 | uint64 profile_byte_count; |
| 322 | if (storage->GetUint64(id, kStorageReceiveByteCount, &profile_byte_count)) { |
| 323 | receive_byte_offset_ = rx_byte_count - profile_byte_count; |
| 324 | } |
| 325 | if (storage->GetUint64(id, kStorageTransmitByteCount, &profile_byte_count)) { |
| 326 | transmit_byte_offset_ = tx_byte_count - profile_byte_count; |
| 327 | } |
| 328 | |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 329 | return true; |
| 330 | } |
| 331 | |
| 332 | bool Device::Save(StoreInterface *storage) { |
| 333 | const string id = GetStorageIdentifier(); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 334 | storage->SetBool(id, kStoragePowered, enabled_persistent_); |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 335 | if (ipconfig_.get()) { |
| 336 | // The _0 is an index into the list of IPConfigs that this device might |
| 337 | // have. We only have one IPConfig right now, and I hope to never have |
| 338 | // to support more, as sleffler indicates that associating IPConfigs |
| 339 | // with devices is wrong and due to be changed in flimflam anyhow. |
| 340 | string suffix = hardware_address_ + "_0"; |
| 341 | ipconfig_->Save(storage, suffix); |
| 342 | storage->SetString(id, kStorageIPConfigs, SerializeIPConfigs(suffix)); |
| 343 | } |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 344 | storage->SetUint64(id, kStorageReceiveByteCount, GetReceiveByteCount()); |
| 345 | storage->SetUint64(id, kStorageTransmitByteCount, GetTransmitByteCount()); |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 346 | return true; |
| 347 | } |
| 348 | |
mukesh agrawal | 784566d | 2012-08-08 18:32:58 -0700 | [diff] [blame] | 349 | void Device::OnBeforeSuspend() { |
| 350 | // Nothing to be done in the general case. |
| 351 | } |
| 352 | |
| 353 | void Device::OnAfterResume() { |
| 354 | if (ipconfig_) { |
| 355 | SLOG(Device, 3) << "Renewing IP address on resume."; |
| 356 | ipconfig_->RenewIP(); |
| 357 | } |
| 358 | } |
| 359 | |
Darin Petkov | 2b8e44e | 2012-06-25 15:13:26 +0200 | [diff] [blame] | 360 | void Device::DropConnection() { |
| 361 | SLOG(Device, 2) << __func__; |
| 362 | DestroyIPConfig(); |
| 363 | SelectService(NULL); |
| 364 | } |
| 365 | |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 366 | void Device::DestroyIPConfig() { |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 367 | DisableIPv6(); |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 368 | if (ipconfig_.get()) { |
Paul Stewart | 217c61d | 2013-06-13 15:12:02 -0700 | [diff] [blame] | 369 | ipconfig_->ReleaseIP(IPConfig::kReleaseReasonDisconnect); |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 370 | ipconfig_ = NULL; |
| 371 | } |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 372 | DestroyConnection(); |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 373 | } |
| 374 | |
Arman Uguray | ed8e610 | 2012-11-29 14:47:20 -0800 | [diff] [blame] | 375 | bool Device::ShouldUseArpGateway() const { |
| 376 | return false; |
| 377 | } |
| 378 | |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 379 | bool Device::AcquireIPConfig() { |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 380 | return AcquireIPConfigWithLeaseName(string()); |
| 381 | } |
| 382 | |
| 383 | bool Device::AcquireIPConfigWithLeaseName(const string &lease_name) { |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 384 | DestroyIPConfig(); |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 385 | EnableIPv6(); |
Arman Uguray | ed8e610 | 2012-11-29 14:47:20 -0800 | [diff] [blame] | 386 | bool arp_gateway = manager_->GetArpGateway() && ShouldUseArpGateway(); |
Paul Stewart | d408fdf | 2012-05-07 17:15:57 -0700 | [diff] [blame] | 387 | ipconfig_ = dhcp_provider_->CreateConfig(link_name_, |
| 388 | manager_->GetHostName(), |
| 389 | lease_name, |
Arman Uguray | ed8e610 | 2012-11-29 14:47:20 -0800 | [diff] [blame] | 390 | arp_gateway); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 391 | ipconfig_->RegisterUpdateCallback(Bind(&Device::OnIPConfigUpdated, |
| 392 | weak_ptr_factory_.GetWeakPtr())); |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 393 | dispatcher_->PostTask(Bind(&Device::ConfigureStaticIPTask, |
| 394 | weak_ptr_factory_.GetWeakPtr())); |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 395 | return ipconfig_->RequestIP(); |
| 396 | } |
| 397 | |
Albert Chaulk | 0e1cdea | 2013-02-27 15:32:55 -0800 | [diff] [blame] | 398 | void Device::DestroyIPConfigLease(const string &name) { |
| 399 | dhcp_provider_->DestroyLease(name); |
| 400 | } |
| 401 | |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 402 | void Device::HelpRegisterConstDerivedString( |
Jason Glasgow | b579005 | 2012-01-27 01:03:52 -0500 | [diff] [blame] | 403 | const string &name, |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 404 | string(Device::*get)(Error *error)) { |
Jason Glasgow | b579005 | 2012-01-27 01:03:52 -0500 | [diff] [blame] | 405 | store_.RegisterDerivedString( |
| 406 | name, |
mukesh agrawal | bebf1b8 | 2013-04-23 15:06:33 -0700 | [diff] [blame] | 407 | StringAccessor(new CustomAccessor<Device, string>(this, get, NULL))); |
Chris Masone | 4e85161 | 2011-07-01 10:46:53 -0700 | [diff] [blame] | 408 | } |
| 409 | |
Jason Glasgow | 08afdff | 2012-04-03 10:22:26 -0400 | [diff] [blame] | 410 | void Device::HelpRegisterConstDerivedRpcIdentifiers( |
| 411 | const string &name, |
| 412 | RpcIdentifiers(Device::*get)(Error *)) { |
| 413 | store_.RegisterDerivedRpcIdentifiers( |
| 414 | name, |
| 415 | RpcIdentifiersAccessor( |
| 416 | new CustomAccessor<Device, RpcIdentifiers>(this, get, NULL))); |
| 417 | } |
| 418 | |
Paul Stewart | 6ff27f5 | 2012-07-11 06:51:41 -0700 | [diff] [blame] | 419 | void Device::HelpRegisterConstDerivedUint64( |
| 420 | const string &name, |
| 421 | uint64(Device::*get)(Error *)) { |
| 422 | store_.RegisterDerivedUint64( |
| 423 | name, |
| 424 | Uint64Accessor( |
| 425 | new CustomAccessor<Device, uint64>(this, get, NULL))); |
| 426 | } |
| 427 | |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 428 | void Device::ConfigureStaticIPTask() { |
| 429 | SLOG(Device, 2) << __func__ << " selected_service " << selected_service_.get() |
| 430 | << " ipconfig " << ipconfig_.get(); |
| 431 | |
| 432 | if (!selected_service_ || !ipconfig_) { |
| 433 | return; |
| 434 | } |
| 435 | |
| 436 | const StaticIPParameters &static_ip_parameters = |
| 437 | selected_service_->static_ip_parameters(); |
| 438 | if (static_ip_parameters.ContainsAddress()) { |
| 439 | SLOG(Device, 2) << __func__ << " " << " configuring static IP parameters."; |
| 440 | // If the parameters contain an IP address, apply them now and bring |
| 441 | // the interface up. When DHCP information arrives, it will supplement |
| 442 | // the static information. |
| 443 | OnIPConfigUpdated(ipconfig_, true); |
| 444 | } else { |
| 445 | SLOG(Device, 2) << __func__ << " " << " no static IP address."; |
| 446 | } |
| 447 | } |
| 448 | |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 449 | void Device::OnIPConfigUpdated(const IPConfigRefPtr &ipconfig, bool success) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 450 | SLOG(Device, 2) << __func__ << " " << " success: " << success; |
Paul Stewart | c39f113 | 2011-06-22 12:02:28 -0700 | [diff] [blame] | 451 | if (success) { |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 452 | CreateConnection(); |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 453 | if (selected_service_) { |
| 454 | ipconfig->ApplyStaticIPParameters( |
Paul Stewart | def189e | 2012-08-02 20:12:09 -0700 | [diff] [blame] | 455 | selected_service_->mutable_static_ip_parameters()); |
Paul Stewart | 217c61d | 2013-06-13 15:12:02 -0700 | [diff] [blame] | 456 | if (selected_service_->static_ip_parameters().ContainsAddress()) { |
| 457 | // If we are using a statically configured IP address instead |
| 458 | // of a leased IP address, release any acquired lease so it may |
| 459 | // be used by others. This allows us to merge other non-leased |
| 460 | // parameters (like DNS) when they're available from a DHCP server |
| 461 | // and not overridden by static parameters, but at the same time |
| 462 | // we avoid taking up a dynamic IP address the DHCP server could |
| 463 | // assign to someone else who might actually use it. |
| 464 | ipconfig->ReleaseIP(IPConfig::kReleaseReasonStaticIP); |
| 465 | } |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 466 | } |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 467 | connection_->UpdateFromIPConfig(ipconfig); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 468 | // SetConnection must occur after the UpdateFromIPConfig so the |
| 469 | // service can use the values derived from the connection. |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 470 | if (selected_service_) { |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 471 | selected_service_->SetConnection(connection_); |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 472 | } |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 473 | // The service state change needs to happen last, so that at the |
| 474 | // time we report the state change to the manager, the service |
| 475 | // has its connection. |
| 476 | SetServiceState(Service::kStateConnected); |
Christopher Wiley | 5519e9e | 2013-01-08 16:55:56 -0800 | [diff] [blame] | 477 | OnConnected(); |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 478 | portal_attempts_to_online_ = 0; |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 479 | // Subtle: Start portal detection after transitioning the service |
| 480 | // to the Connected state because this call may immediately transition |
| 481 | // to the Online state. |
Thieu Le | d176092 | 2012-09-11 14:15:35 -0700 | [diff] [blame] | 482 | if (selected_service_) { |
| 483 | StartPortalDetection(); |
| 484 | } |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 485 | StartLinkMonitor(); |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 486 | StartTrafficMonitor(); |
Prathmesh Prabhu | ba99b59 | 2013-04-17 15:13:14 -0700 | [diff] [blame] | 487 | SetupConnectionHealthChecker(); |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 488 | } else { |
mukesh agrawal | cc0fded | 2012-05-09 13:40:58 -0700 | [diff] [blame] | 489 | // TODO(pstew): This logic gets yet more complex when multiple |
| 490 | // IPConfig types are run in parallel (e.g. DHCP and DHCP6) |
| 491 | if (selected_service_ && |
| 492 | selected_service_->static_ip_parameters().ContainsAddress()) { |
| 493 | // Consider three cases: |
| 494 | // |
| 495 | // 1. We're here because DHCP failed while starting up. There |
| 496 | // are two subcases: |
| 497 | // a. DHCP has failed, and Static IP config has _not yet_ |
| 498 | // completed. It's fine to do nothing, because we'll |
| 499 | // apply the static config shortly. |
| 500 | // b. DHCP has failed, and Static IP config has _already_ |
| 501 | // completed. It's fine to do nothing, because we can |
| 502 | // continue to use the static config that's already |
| 503 | // been applied. |
| 504 | // |
| 505 | // 2. We're here because a previously valid DHCP configuration |
| 506 | // is no longer valid. There's still a static IP config, |
| 507 | // because the condition in the if clause evaluated to true. |
| 508 | // Furthermore, the static config includes an IP address for |
| 509 | // us to use. |
| 510 | // |
| 511 | // The current configuration may include some DHCP |
| 512 | // parameters, overriden by any static parameters |
| 513 | // provided. We continue to use this configuration, because |
| 514 | // the only configuration element that is leased to us (IP |
| 515 | // address) will be overriden by a static parameter. |
| 516 | return; |
| 517 | } |
| 518 | |
Paul Stewart | f6f9648 | 2013-07-12 12:49:15 -0700 | [diff] [blame] | 519 | OnIPConfigFailure(); |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 520 | DestroyConnection(); |
Paul Stewart | c39f113 | 2011-06-22 12:02:28 -0700 | [diff] [blame] | 521 | } |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 522 | } |
| 523 | |
Paul Stewart | f6f9648 | 2013-07-12 12:49:15 -0700 | [diff] [blame] | 524 | void Device::OnIPConfigFailure() { |
| 525 | if (selected_service_) { |
| 526 | Error error; |
| 527 | selected_service_->DisconnectWithFailure(Service::kFailureDHCP, &error); |
| 528 | } |
| 529 | } |
| 530 | |
Christopher Wiley | 5519e9e | 2013-01-08 16:55:56 -0800 | [diff] [blame] | 531 | void Device::OnConnected() {} |
| 532 | |
Paul Stewart | 8596f9f | 2013-03-14 07:58:26 -0700 | [diff] [blame] | 533 | void Device::OnConnectionUpdated() { |
| 534 | if (selected_service_) { |
| 535 | manager_->UpdateService(selected_service_); |
| 536 | } |
| 537 | } |
| 538 | |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 539 | void Device::CreateConnection() { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 540 | SLOG(Device, 2) << __func__; |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 541 | if (!connection_.get()) { |
mukesh agrawal | 23ac6b7 | 2013-01-31 18:52:37 -0800 | [diff] [blame] | 542 | connection_ = new Connection(interface_index_, |
| 543 | link_name_, |
| 544 | technology_, |
| 545 | manager_->device_info()); |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 546 | } |
| 547 | } |
| 548 | |
| 549 | void Device::DestroyConnection() { |
mukesh agrawal | 6813e76 | 2013-07-10 19:05:08 -0700 | [diff] [blame] | 550 | SLOG(Device, 2) << __func__ << " on " << link_name_; |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 551 | StopPortalDetection(); |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 552 | StopLinkMonitor(); |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 553 | StopTrafficMonitor(); |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 554 | if (selected_service_.get()) { |
mukesh agrawal | 6813e76 | 2013-07-10 19:05:08 -0700 | [diff] [blame] | 555 | SLOG(Device, 3) << "Clearing connection of service " |
| 556 | << selected_service_->unique_name(); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 557 | selected_service_->SetConnection(NULL); |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 558 | } |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 559 | connection_ = NULL; |
Arman Uguray | f84a424 | 2013-04-09 20:01:07 -0700 | [diff] [blame] | 560 | health_checker_.reset(); |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 561 | } |
| 562 | |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 563 | void Device::SelectService(const ServiceRefPtr &service) { |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 564 | SLOG(Device, 2) << __func__ << ": service " |
mukesh agrawal | 6813e76 | 2013-07-10 19:05:08 -0700 | [diff] [blame] | 565 | << (service ? service->unique_name() : "*reset*") |
| 566 | << " on " << link_name_; |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 567 | |
| 568 | if (selected_service_.get() == service.get()) { |
| 569 | // No change to |selected_service_|. Return early to avoid |
| 570 | // changing its state. |
| 571 | return; |
| 572 | } |
| 573 | |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 574 | if (selected_service_.get()) { |
| 575 | if (selected_service_->state() != Service::kStateFailure) { |
| 576 | selected_service_->SetState(Service::kStateIdle); |
| 577 | } |
Paul Stewart | 20b0a09 | 2012-05-22 20:39:57 -0700 | [diff] [blame] | 578 | // Just in case the Device subclass has not already done so, make |
| 579 | // sure the previously selected service has its connection removed. |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 580 | selected_service_->SetConnection(NULL); |
Paul Stewart | c886061 | 2012-09-28 07:36:21 -0700 | [diff] [blame] | 581 | StopLinkMonitor(); |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 582 | StopTrafficMonitor(); |
Paul Stewart | c886061 | 2012-09-28 07:36:21 -0700 | [diff] [blame] | 583 | StopPortalDetection(); |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 584 | } |
| 585 | selected_service_ = service; |
| 586 | } |
| 587 | |
| 588 | void Device::SetServiceState(Service::ConnectState state) { |
| 589 | if (selected_service_.get()) { |
| 590 | selected_service_->SetState(state); |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | void Device::SetServiceFailure(Service::ConnectFailure failure_state) { |
| 595 | if (selected_service_.get()) { |
| 596 | selected_service_->SetFailure(failure_state); |
| 597 | } |
| 598 | } |
| 599 | |
Eric Shienbrood | cc95c5d | 2012-03-30 15:25:49 -0400 | [diff] [blame] | 600 | void Device::SetServiceFailureSilent(Service::ConnectFailure failure_state) { |
| 601 | if (selected_service_.get()) { |
| 602 | selected_service_->SetFailureSilent(failure_state); |
| 603 | } |
| 604 | } |
| 605 | |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 606 | string Device::SerializeIPConfigs(const string &suffix) { |
| 607 | return StringPrintf("%s:%s", suffix.c_str(), ipconfig_->type().c_str()); |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 608 | } |
| 609 | |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 610 | bool Device::SetIPFlag(IPAddress::Family family, const string &flag, |
| 611 | const string &value) { |
| 612 | string ip_version; |
| 613 | if (family == IPAddress::kFamilyIPv4) { |
| 614 | ip_version = kIPFlagVersion4; |
| 615 | } else if (family == IPAddress::kFamilyIPv6) { |
| 616 | ip_version = kIPFlagVersion6; |
| 617 | } else { |
| 618 | NOTIMPLEMENTED(); |
| 619 | } |
| 620 | FilePath flag_file(StringPrintf(kIPFlagTemplate, ip_version.c_str(), |
| 621 | link_name_.c_str(), flag.c_str())); |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 622 | SLOG(Device, 2) << "Writing " << value << " to flag file " |
| 623 | << flag_file.value(); |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 624 | if (file_util::WriteFile(flag_file, value.c_str(), value.length()) != 1) { |
| 625 | LOG(ERROR) << StringPrintf("IP flag write failed: %s to %s", |
| 626 | value.c_str(), flag_file.value().c_str()); |
| 627 | return false; |
| 628 | } |
| 629 | return true; |
| 630 | } |
| 631 | |
Paul Stewart | 6ff27f5 | 2012-07-11 06:51:41 -0700 | [diff] [blame] | 632 | void Device::ResetByteCounters() { |
| 633 | manager_->device_info()->GetByteCounts( |
| 634 | interface_index_, &receive_byte_offset_, &transmit_byte_offset_); |
| 635 | manager_->UpdateDevice(this); |
| 636 | } |
| 637 | |
Prathmesh Prabhu | ba99b59 | 2013-04-17 15:13:14 -0700 | [diff] [blame] | 638 | void Device::SetupConnectionHealthChecker() { |
| 639 | DCHECK(connection_); |
| 640 | if (!health_checker_.get()) { |
| 641 | health_checker_.reset(new ConnectionHealthChecker( |
| 642 | connection_, |
| 643 | dispatcher_, |
| 644 | manager_->health_checker_remote_ips(), |
| 645 | Bind(&Device::OnConnectionHealthCheckerResult, |
| 646 | weak_ptr_factory_.GetWeakPtr()))); |
| 647 | } else { |
| 648 | health_checker_->SetConnection(connection_); |
| 649 | } |
| 650 | // Add URL in either case because a connection reset could have dropped past |
| 651 | // DNS queries. |
| 652 | health_checker_->AddRemoteURL(manager_->GetPortalCheckURL()); |
| 653 | } |
| 654 | |
Arman Uguray | f84a424 | 2013-04-09 20:01:07 -0700 | [diff] [blame] | 655 | void Device::RequestConnectionHealthCheck() { |
| 656 | if (!health_checker_.get()) { |
| 657 | SLOG(Device, 2) << "No health checker exists, cannot request " |
| 658 | << "health check."; |
| 659 | return; |
| 660 | } |
| 661 | if (health_checker_->health_check_in_progress()) { |
| 662 | SLOG(Device, 2) << "Health check already in progress."; |
| 663 | return; |
| 664 | } |
| 665 | health_checker_->Start(); |
| 666 | } |
| 667 | |
| 668 | void Device::OnConnectionHealthCheckerResult( |
| 669 | ConnectionHealthChecker::Result result) { |
Prathmesh Prabhu | 5489b7a | 2013-04-10 13:33:59 -0700 | [diff] [blame] | 670 | SLOG(Device, 2) |
| 671 | << FriendlyName() |
| 672 | << ": ConnectionHealthChecker result: " |
| 673 | << ConnectionHealthChecker::ResultToString(result); |
Arman Uguray | f84a424 | 2013-04-09 20:01:07 -0700 | [diff] [blame] | 674 | } |
| 675 | |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 676 | bool Device::RestartPortalDetection() { |
| 677 | StopPortalDetection(); |
| 678 | return StartPortalDetection(); |
| 679 | } |
| 680 | |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 681 | bool Device::RequestPortalDetection() { |
| 682 | if (!selected_service_) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 683 | SLOG(Device, 2) << FriendlyName() |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 684 | << ": No selected service, so no need for portal check."; |
| 685 | return false; |
| 686 | } |
| 687 | |
| 688 | if (!connection_.get()) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 689 | SLOG(Device, 2) << FriendlyName() |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 690 | << ": No connection, so no need for portal check."; |
| 691 | return false; |
| 692 | } |
| 693 | |
| 694 | if (selected_service_->state() != Service::kStatePortal) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 695 | SLOG(Device, 2) << FriendlyName() |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 696 | << ": Service is not in portal state. No need to start check."; |
| 697 | return false; |
| 698 | } |
| 699 | |
| 700 | if (!connection_->is_default()) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 701 | SLOG(Device, 2) << FriendlyName() |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 702 | << ": Service is not the default connection. Don't start check."; |
| 703 | return false; |
| 704 | } |
| 705 | |
| 706 | if (portal_detector_.get() && portal_detector_->IsInProgress()) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 707 | SLOG(Device, 2) << FriendlyName() |
| 708 | << ": Portal detection is already running."; |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 709 | return true; |
| 710 | } |
| 711 | |
| 712 | return StartPortalDetection(); |
| 713 | } |
| 714 | |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 715 | bool Device::StartPortalDetection() { |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 716 | DCHECK(selected_service_); |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 717 | if (selected_service_->IsPortalDetectionDisabled()) { |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 718 | SLOG(Device, 2) << "Service " << selected_service_->unique_name() |
Paul Stewart | d215af6 | 2012-04-24 23:25:50 -0700 | [diff] [blame] | 719 | << ": Portal detection is disabled; " |
| 720 | << "marking service online."; |
| 721 | SetServiceConnectedState(Service::kStateOnline); |
| 722 | return false; |
| 723 | } |
| 724 | |
| 725 | if (selected_service_->IsPortalDetectionAuto() && |
| 726 | !manager_->IsPortalDetectionEnabled(technology())) { |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 727 | // If portal detection is disabled for this technology, immediately set |
| 728 | // the service state to "Online". |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 729 | SLOG(Device, 2) << "Device " << FriendlyName() |
| 730 | << ": Portal detection is disabled; " |
| 731 | << "marking service online."; |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 732 | SetServiceConnectedState(Service::kStateOnline); |
| 733 | return false; |
| 734 | } |
| 735 | |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 736 | if (selected_service_->HasProxyConfig()) { |
| 737 | // Services with HTTP proxy configurations should not be checked by the |
| 738 | // connection manager, since we don't have the ability to evaluate |
| 739 | // arbitrary proxy configs and their possible credentials. |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 740 | SLOG(Device, 2) << "Device " << FriendlyName() |
| 741 | << ": Service has proxy config; marking it online."; |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 742 | SetServiceConnectedState(Service::kStateOnline); |
| 743 | return false; |
| 744 | } |
| 745 | |
| 746 | portal_detector_.reset(new PortalDetector(connection_, |
| 747 | dispatcher_, |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 748 | portal_detector_callback_)); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 749 | if (!portal_detector_->Start(manager_->GetPortalCheckURL())) { |
| 750 | LOG(ERROR) << "Device " << FriendlyName() |
| 751 | << ": Portal detection failed to start: likely bad URL: " |
| 752 | << manager_->GetPortalCheckURL(); |
| 753 | SetServiceConnectedState(Service::kStateOnline); |
| 754 | return false; |
| 755 | } |
| 756 | |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 757 | SLOG(Device, 2) << "Device " << FriendlyName() |
| 758 | << ": Portal detection has started."; |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 759 | return true; |
| 760 | } |
| 761 | |
| 762 | void Device::StopPortalDetection() { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 763 | SLOG(Device, 2) << "Device " << FriendlyName() |
| 764 | << ": Portal detection has stopped."; |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 765 | portal_detector_.reset(); |
| 766 | } |
| 767 | |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 768 | void Device::set_link_monitor(LinkMonitor *link_monitor) { |
| 769 | link_monitor_.reset(link_monitor); |
| 770 | } |
| 771 | |
| 772 | bool Device::StartLinkMonitor() { |
| 773 | if (!manager_->IsTechnologyLinkMonitorEnabled(technology())) { |
| 774 | SLOG(Device, 2) << "Device " << FriendlyName() |
| 775 | << ": Link Monitoring is disabled."; |
| 776 | return false; |
| 777 | } |
| 778 | |
| 779 | if (!link_monitor()) { |
| 780 | set_link_monitor( |
| 781 | new LinkMonitor( |
| 782 | connection_, dispatcher_, metrics(), manager_->device_info(), |
| 783 | Bind(&Device::OnLinkMonitorFailure, weak_ptr_factory_.GetWeakPtr()))); |
| 784 | } |
| 785 | |
| 786 | SLOG(Device, 2) << "Device " << FriendlyName() |
| 787 | << ": Link Monitor starting."; |
| 788 | return link_monitor_->Start(); |
| 789 | } |
| 790 | |
| 791 | void Device::StopLinkMonitor() { |
| 792 | SLOG(Device, 2) << "Device " << FriendlyName() |
| 793 | << ": Link Monitor stopping."; |
| 794 | link_monitor_.reset(); |
| 795 | } |
| 796 | |
| 797 | void Device::OnLinkMonitorFailure() { |
| 798 | LOG(ERROR) << "Device " << FriendlyName() |
| 799 | << ": Link Monitor indicates failure."; |
| 800 | } |
| 801 | |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 802 | void Device::set_traffic_monitor(TrafficMonitor *traffic_monitor) { |
| 803 | traffic_monitor_.reset(traffic_monitor); |
| 804 | } |
| 805 | |
| 806 | bool Device::StartTrafficMonitor() { |
Thieu Le | 0302666 | 2013-04-04 10:45:11 -0700 | [diff] [blame] | 807 | SLOG(Device, 2) << __func__; |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 808 | if (!traffic_monitor_enabled_) { |
| 809 | SLOG(Device, 2) << "Device " << FriendlyName() |
| 810 | << ": Traffic Monitoring is disabled."; |
| 811 | return false; |
| 812 | } |
| 813 | |
| 814 | if (!traffic_monitor_.get()) { |
| 815 | traffic_monitor_.reset(new TrafficMonitor(this, dispatcher_)); |
Thieu Le | 0302666 | 2013-04-04 10:45:11 -0700 | [diff] [blame] | 816 | traffic_monitor_->set_tcp_out_traffic_not_routed_callback( |
| 817 | Bind(&Device::OnNoNetworkRouting, weak_ptr_factory_.GetWeakPtr())); |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 818 | } |
| 819 | |
| 820 | SLOG(Device, 2) << "Device " << FriendlyName() |
| 821 | << ": Traffic Monitor starting."; |
| 822 | traffic_monitor_->Start(); |
| 823 | return true; |
| 824 | } |
| 825 | |
| 826 | void Device::StopTrafficMonitor() { |
Thieu Le | 0302666 | 2013-04-04 10:45:11 -0700 | [diff] [blame] | 827 | SLOG(Device, 2) << __func__; |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 828 | SLOG(Device, 2) << "Device " << FriendlyName() |
| 829 | << ": Traffic Monitor stopping."; |
| 830 | traffic_monitor_.reset(); |
| 831 | } |
| 832 | |
Thieu Le | 0302666 | 2013-04-04 10:45:11 -0700 | [diff] [blame] | 833 | void Device::OnNoNetworkRouting() { |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 834 | SLOG(Device, 2) << "Device " << FriendlyName() |
Arman Uguray | f84a424 | 2013-04-09 20:01:07 -0700 | [diff] [blame] | 835 | << ": Traffic Monitor detects network congestion."; |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 836 | } |
| 837 | |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 838 | void Device::SetServiceConnectedState(Service::ConnectState state) { |
| 839 | DCHECK(selected_service_.get()); |
| 840 | |
| 841 | if (!selected_service_.get()) { |
| 842 | LOG(ERROR) << FriendlyName() << ": " |
| 843 | << "Portal detection completed but no selected service exists!"; |
| 844 | return; |
| 845 | } |
| 846 | |
| 847 | if (!selected_service_->IsConnected()) { |
| 848 | LOG(ERROR) << FriendlyName() << ": " |
| 849 | << "Portal detection completed but selected service " |
Darin Petkov | 457728b | 2013-01-09 09:49:08 +0100 | [diff] [blame] | 850 | << selected_service_->unique_name() |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 851 | << " is in non-connected state."; |
| 852 | return; |
| 853 | } |
| 854 | |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 855 | if (state == Service::kStatePortal && connection_->is_default() && |
| 856 | manager_->GetPortalCheckInterval() != 0) { |
| 857 | CHECK(portal_detector_.get()); |
| 858 | if (!portal_detector_->StartAfterDelay( |
| 859 | manager_->GetPortalCheckURL(), |
| 860 | manager_->GetPortalCheckInterval())) { |
| 861 | LOG(ERROR) << "Device " << FriendlyName() |
| 862 | << ": Portal detection failed to restart: likely bad URL: " |
| 863 | << manager_->GetPortalCheckURL(); |
| 864 | SetServiceState(Service::kStateOnline); |
| 865 | portal_detector_.reset(); |
| 866 | return; |
| 867 | } |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 868 | SLOG(Device, 2) << "Device " << FriendlyName() |
| 869 | << ": Portal detection retrying."; |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 870 | } else { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 871 | SLOG(Device, 2) << "Device " << FriendlyName() |
| 872 | << ": Portal will not retry."; |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 873 | portal_detector_.reset(); |
| 874 | } |
| 875 | |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 876 | SetServiceState(state); |
| 877 | } |
| 878 | |
| 879 | void Device::PortalDetectorCallback(const PortalDetector::Result &result) { |
| 880 | if (!result.final) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 881 | SLOG(Device, 2) << "Device " << FriendlyName() |
| 882 | << ": Received non-final status: " |
| 883 | << PortalDetector::StatusToString(result.status); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 884 | return; |
| 885 | } |
| 886 | |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 887 | SLOG(Device, 2) << "Device " << FriendlyName() |
| 888 | << ": Received final status: " |
| 889 | << PortalDetector::StatusToString(result.status); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 890 | |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 891 | portal_attempts_to_online_ += result.num_attempts; |
| 892 | |
| 893 | metrics()->SendEnumToUMA( |
| 894 | metrics()->GetFullMetricName(Metrics::kMetricPortalResult, technology()), |
| 895 | Metrics::PortalDetectionResultToEnum(result), |
| 896 | Metrics::kPortalResultMax); |
| 897 | |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 898 | if (result.status == PortalDetector::kStatusSuccess) { |
| 899 | SetServiceConnectedState(Service::kStateOnline); |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 900 | |
| 901 | metrics()->SendToUMA( |
| 902 | metrics()->GetFullMetricName( |
| 903 | Metrics::kMetricPortalAttemptsToOnline, technology()), |
| 904 | portal_attempts_to_online_, |
| 905 | Metrics::kMetricPortalAttemptsToOnlineMin, |
| 906 | Metrics::kMetricPortalAttemptsToOnlineMax, |
| 907 | Metrics::kMetricPortalAttemptsToOnlineNumBuckets); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 908 | } else { |
| 909 | SetServiceConnectedState(Service::kStatePortal); |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 910 | |
| 911 | metrics()->SendToUMA( |
| 912 | metrics()->GetFullMetricName( |
| 913 | Metrics::kMetricPortalAttempts, technology()), |
| 914 | result.num_attempts, |
| 915 | Metrics::kMetricPortalAttemptsMin, |
| 916 | Metrics::kMetricPortalAttemptsMax, |
| 917 | Metrics::kMetricPortalAttemptsNumBuckets); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 918 | } |
| 919 | } |
| 920 | |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 921 | vector<string> Device::AvailableIPConfigs(Error */*error*/) { |
Jason Glasgow | 08afdff | 2012-04-03 10:22:26 -0400 | [diff] [blame] | 922 | if (ipconfig_.get()) { |
| 923 | string id = ipconfig_->GetRpcIdentifier(); |
| 924 | return vector<string>(1, id); |
| 925 | } |
| 926 | return vector<string>(); |
Chris Masone | 4e85161 | 2011-07-01 10:46:53 -0700 | [diff] [blame] | 927 | } |
| 928 | |
| 929 | string Device::GetRpcConnectionIdentifier() { |
| 930 | return adaptor_->GetRpcConnectionIdentifier(); |
| 931 | } |
| 932 | |
Paul Stewart | 036dba0 | 2012-08-07 12:34:41 -0700 | [diff] [blame] | 933 | uint64 Device::GetLinkMonitorResponseTime(Error *error) { |
| 934 | if (!link_monitor_.get()) { |
| 935 | // It is not strictly an error that the link monitor does not |
| 936 | // exist, but returning an error here allows the GetProperties |
| 937 | // call in our Adaptor to omit this parameter. |
| 938 | error->Populate(Error::kNotFound, "Device is not running LinkMonitor"); |
| 939 | return 0; |
| 940 | } |
| 941 | return link_monitor_->GetResponseTimeMilliseconds(); |
| 942 | } |
| 943 | |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 944 | uint64 Device::GetReceiveByteCount() { |
Paul Stewart | 6ff27f5 | 2012-07-11 06:51:41 -0700 | [diff] [blame] | 945 | uint64 rx_byte_count = 0, tx_byte_count = 0; |
| 946 | manager_->device_info()->GetByteCounts( |
| 947 | interface_index_, &rx_byte_count, &tx_byte_count); |
| 948 | return rx_byte_count - receive_byte_offset_; |
| 949 | } |
| 950 | |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 951 | uint64 Device::GetTransmitByteCount() { |
Paul Stewart | 6ff27f5 | 2012-07-11 06:51:41 -0700 | [diff] [blame] | 952 | uint64 rx_byte_count = 0, tx_byte_count = 0; |
| 953 | manager_->device_info()->GetByteCounts( |
| 954 | interface_index_, &rx_byte_count, &tx_byte_count); |
| 955 | return tx_byte_count - transmit_byte_offset_; |
| 956 | } |
| 957 | |
Ben Chan | b061f89 | 2013-02-27 17:46:55 -0800 | [diff] [blame] | 958 | uint64 Device::GetReceiveByteCountProperty(Error */*error*/) { |
| 959 | return GetReceiveByteCount(); |
| 960 | } |
| 961 | |
| 962 | uint64 Device::GetTransmitByteCountProperty(Error */*error*/) { |
| 963 | return GetTransmitByteCount(); |
| 964 | } |
| 965 | |
Eric Shienbrood | 7fce52c | 2012-04-13 19:11:02 -0400 | [diff] [blame] | 966 | bool Device::IsUnderlyingDeviceEnabled() const { |
| 967 | return false; |
| 968 | } |
| 969 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 970 | // callback |
| 971 | void Device::OnEnabledStateChanged(const ResultCallback &callback, |
| 972 | const Error &error) { |
mukesh agrawal | 46c27cc | 2013-07-10 16:39:10 -0700 | [diff] [blame] | 973 | SLOG(Device, 2) << __func__ |
| 974 | << " (target: " << enabled_pending_ << "," |
| 975 | << " success: " << error.IsSuccess() << ")" |
| 976 | << " on " << link_name_; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 977 | if (error.IsSuccess()) { |
| 978 | enabled_ = enabled_pending_; |
| 979 | manager_->UpdateEnabledTechnologies(); |
| 980 | adaptor_->EmitBoolChanged(flimflam::kPoweredProperty, enabled_); |
| 981 | adaptor_->UpdateEnabled(); |
| 982 | } |
Gary Morain | baeefdf | 2012-04-30 14:53:35 -0700 | [diff] [blame] | 983 | enabled_pending_ = enabled_; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 984 | if (!callback.is_null()) |
| 985 | callback.Run(error); |
| 986 | } |
| 987 | |
| 988 | void Device::SetEnabled(bool enable) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 989 | SLOG(Device, 2) << __func__ << "(" << enable << ")"; |
Jason Glasgow | 4a49079 | 2012-04-10 15:02:05 -0400 | [diff] [blame] | 990 | Error error; |
| 991 | SetEnabledInternal(enable, false, &error, ResultCallback()); |
Jason Glasgow | 7234ec3 | 2012-05-23 16:01:21 -0400 | [diff] [blame] | 992 | LOG_IF(ERROR, error.IsFailure() && !error.IsOngoing()) |
Jason Glasgow | 4a49079 | 2012-04-10 15:02:05 -0400 | [diff] [blame] | 993 | << "Enabled failed, but no way to report the failure."; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 994 | } |
| 995 | |
| 996 | void Device::SetEnabledPersistent(bool enable, |
| 997 | Error *error, |
| 998 | const ResultCallback &callback) { |
| 999 | SetEnabledInternal(enable, true, error, callback); |
| 1000 | } |
| 1001 | |
| 1002 | void Device::SetEnabledInternal(bool enable, |
| 1003 | bool persist, |
| 1004 | Error *error, |
| 1005 | const ResultCallback &callback) { |
Jason Glasgow | 4a49079 | 2012-04-10 15:02:05 -0400 | [diff] [blame] | 1006 | DCHECK(error); |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 1007 | SLOG(Device, 2) << "Device " << link_name_ << " " |
| 1008 | << (enable ? "starting" : "stopping"); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 1009 | if (enable == enabled_) { |
Jason Glasgow | 4a49079 | 2012-04-10 15:02:05 -0400 | [diff] [blame] | 1010 | error->Reset(); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 1011 | return; |
| 1012 | } |
| 1013 | |
| 1014 | if (enabled_pending_ == enable) { |
Jason Glasgow | 4a49079 | 2012-04-10 15:02:05 -0400 | [diff] [blame] | 1015 | Error::PopulateAndLog(error, Error::kInProgress, |
| 1016 | "Enable operation already in progress"); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 1017 | return; |
| 1018 | } |
| 1019 | |
| 1020 | if (persist) { |
| 1021 | enabled_persistent_ = enable; |
Darin Petkov | e7c6ad3 | 2012-06-29 10:22:09 +0200 | [diff] [blame] | 1022 | manager_->UpdateDevice(this); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 1023 | } |
| 1024 | |
| 1025 | enabled_pending_ = enable; |
| 1026 | EnabledStateChangedCallback enabled_callback = |
| 1027 | Bind(&Device::OnEnabledStateChanged, |
| 1028 | weak_ptr_factory_.GetWeakPtr(), callback); |
| 1029 | if (enable) { |
| 1030 | running_ = true; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 1031 | Start(error, enabled_callback); |
| 1032 | } else { |
| 1033 | running_ = false; |
| 1034 | DestroyIPConfig(); // breaks a reference cycle |
| 1035 | SelectService(NULL); // breaks a reference cycle |
| 1036 | rtnl_handler_->SetInterfaceFlags(interface_index(), 0, IFF_UP); |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 1037 | SLOG(Device, 3) << "Device " << link_name_ << " ipconfig_ " |
| 1038 | << (ipconfig_ ? "is set." : "is not set."); |
| 1039 | SLOG(Device, 3) << "Device " << link_name_ << " connection_ " |
| 1040 | << (connection_ ? "is set." : "is not set."); |
| 1041 | SLOG(Device, 3) << "Device " << link_name_ << " selected_service_ " |
| 1042 | << (selected_service_ ? "is set." : "is not set."); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 1043 | Stop(error, enabled_callback); |
| 1044 | } |
| 1045 | } |
| 1046 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 1047 | } // namespace shill |