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 | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 17 | #include <base/logging.h> |
Chris Masone | 487b8bf | 2011-05-13 16:27:57 -0700 | [diff] [blame] | 18 | #include <base/memory/ref_counted.h> |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 19 | #include <base/stringprintf.h> |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 20 | #include <chromeos/dbus/service_constants.h> |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 21 | |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 22 | #include "shill/connection.h" |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 23 | #include "shill/control_interface.h" |
Chris Masone | d7732e4 | 2011-05-20 11:08:56 -0700 | [diff] [blame] | 24 | #include "shill/device_dbus_adaptor.h" |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 25 | #include "shill/dhcp_config.h" |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 26 | #include "shill/dhcp_provider.h" |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 27 | #include "shill/error.h" |
Paul Stewart | 26b327e | 2011-10-19 11:38:09 -0700 | [diff] [blame] | 28 | #include "shill/event_dispatcher.h" |
Paul Stewart | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 29 | #include "shill/http_proxy.h" |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 30 | #include "shill/manager.h" |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 31 | #include "shill/metrics.h" |
Chris Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame] | 32 | #include "shill/property_accessor.h" |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 33 | #include "shill/refptr_types.h" |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 34 | #include "shill/routing_table.h" |
Paul Stewart | c39f113 | 2011-06-22 12:02:28 -0700 | [diff] [blame] | 35 | #include "shill/rtnl_handler.h" |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 36 | #include "shill/service.h" |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 37 | #include "shill/store_interface.h" |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 38 | #include "shill/technology.h" |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 39 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 40 | using base::Bind; |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 41 | using base::StringPrintf; |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 42 | using std::string; |
| 43 | using std::vector; |
| 44 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 45 | namespace shill { |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 46 | |
| 47 | // static |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 48 | const char Device::kIPFlagTemplate[] = "/proc/sys/net/%s/conf/%s/%s"; |
| 49 | // static |
| 50 | const char Device::kIPFlagVersion4[] = "ipv4"; |
| 51 | // static |
| 52 | const char Device::kIPFlagVersion6[] = "ipv6"; |
| 53 | // static |
| 54 | const char Device::kIPFlagDisableIPv6[] = "disable_ipv6"; |
| 55 | // static |
| 56 | const char Device::kIPFlagUseTempAddr[] = "use_tempaddr"; |
| 57 | // static |
| 58 | const char Device::kIPFlagUseTempAddrUsedAndDefault[] = "2"; |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 59 | // static |
| 60 | const char Device::kIPFlagReversePathFilter[] = "rp_filter"; |
| 61 | // static |
| 62 | const char Device::kIPFlagReversePathFilterEnabled[] = "1"; |
| 63 | // static |
| 64 | const char Device::kIPFlagReversePathFilterLooseMode[] = "2"; |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 65 | |
| 66 | // static |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 67 | const char Device::kStoragePowered[] = "Powered"; |
| 68 | |
| 69 | // static |
| 70 | const char Device::kStorageIPConfigs[] = "IPConfigs"; |
| 71 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 72 | Device::Device(ControlInterface *control_interface, |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 73 | EventDispatcher *dispatcher, |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 74 | Metrics *metrics, |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 75 | Manager *manager, |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 76 | const string &link_name, |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 77 | const string &address, |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 78 | int interface_index, |
| 79 | Technology::Identifier technology) |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 80 | : enabled_(false), |
| 81 | enabled_persistent_(true), |
| 82 | enabled_pending_(enabled_), |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 83 | reconnect_(true), |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 84 | hardware_address_(address), |
mukesh agrawal | f60e406 | 2011-05-27 13:13:41 -0700 | [diff] [blame] | 85 | interface_index_(interface_index), |
| 86 | running_(false), |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 87 | link_name_(link_name), |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 88 | unique_id_(link_name), |
Darin Petkov | d966195 | 2011-08-03 16:25:42 -0700 | [diff] [blame] | 89 | control_interface_(control_interface), |
| 90 | dispatcher_(dispatcher), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 91 | metrics_(metrics), |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 92 | manager_(manager), |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 93 | weak_ptr_factory_(this), |
Darin Petkov | 77cb681 | 2011-08-15 16:19:41 -0700 | [diff] [blame] | 94 | adaptor_(control_interface->CreateDeviceAdaptor(this)), |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 95 | portal_detector_callback_(Bind(&Device::PortalDetectorCallback, |
| 96 | weak_ptr_factory_.GetWeakPtr())), |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 97 | technology_(technology), |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 98 | portal_attempts_to_online_(0), |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 99 | dhcp_provider_(DHCPProvider::GetInstance()), |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 100 | routing_table_(RoutingTable::GetInstance()), |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 101 | rtnl_handler_(RTNLHandler::GetInstance()) { |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 102 | store_.RegisterConstString(flimflam::kAddressProperty, &hardware_address_); |
Chris Masone | 4d42df8 | 2011-07-02 17:09:39 -0700 | [diff] [blame] | 103 | |
| 104 | // flimflam::kBgscanMethodProperty: Registered in WiFi |
| 105 | // flimflam::kBgscanShortIntervalProperty: Registered in WiFi |
| 106 | // flimflam::kBgscanSignalThresholdProperty: Registered in WiFi |
| 107 | |
| 108 | // flimflam::kCellularAllowRoamingProperty: Registered in Cellular |
| 109 | // flimflam::kCarrierProperty: Registered in Cellular |
| 110 | // flimflam::kEsnProperty: Registered in Cellular |
Darin Petkov | 3335b37 | 2011-08-22 11:05:32 -0700 | [diff] [blame] | 111 | // flimflam::kHomeProviderProperty: Registered in Cellular |
Chris Masone | 4d42df8 | 2011-07-02 17:09:39 -0700 | [diff] [blame] | 112 | // flimflam::kImeiProperty: Registered in Cellular |
| 113 | // flimflam::kImsiProperty: Registered in Cellular |
| 114 | // flimflam::kManufacturerProperty: Registered in Cellular |
| 115 | // flimflam::kMdnProperty: Registered in Cellular |
| 116 | // flimflam::kMeidProperty: Registered in Cellular |
| 117 | // flimflam::kMinProperty: Registered in Cellular |
| 118 | // flimflam::kModelIDProperty: Registered in Cellular |
| 119 | // flimflam::kFirmwareRevisionProperty: Registered in Cellular |
| 120 | // flimflam::kHardwareRevisionProperty: Registered in Cellular |
| 121 | // flimflam::kPRLVersionProperty: Registered in Cellular |
| 122 | // flimflam::kSIMLockStatusProperty: Registered in Cellular |
| 123 | // flimflam::kFoundNetworksProperty: Registered in Cellular |
Darin Petkov | e9d12e0 | 2011-07-27 15:09:37 -0700 | [diff] [blame] | 124 | // flimflam::kDBusConnectionProperty: Registered in Cellular |
| 125 | // flimflam::kDBusObjectProperty: Register in Cellular |
Chris Masone | 4d42df8 | 2011-07-02 17:09:39 -0700 | [diff] [blame] | 126 | |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 127 | // TODO(cmasone): Chrome doesn't use this...does anyone? |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 128 | // store_.RegisterConstString(flimflam::kInterfaceProperty, &link_name_); |
| 129 | HelpRegisterDerivedStrings(flimflam::kIPConfigsProperty, |
| 130 | &Device::AvailableIPConfigs, |
| 131 | NULL); |
| 132 | store_.RegisterConstString(flimflam::kNameProperty, &link_name_); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 133 | store_.RegisterConstBool(flimflam::kPoweredProperty, &enabled_); |
Jason Glasgow | b579005 | 2012-01-27 01:03:52 -0500 | [diff] [blame] | 134 | HelpRegisterDerivedString(flimflam::kTypeProperty, |
| 135 | &Device::GetTechnologyString, |
| 136 | NULL); |
| 137 | |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 138 | // TODO(cmasone): Chrome doesn't use this...does anyone? |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 139 | // store_.RegisterConstBool(flimflam::kReconnectProperty, &reconnect_); |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 140 | |
Chris Masone | 4e85161 | 2011-07-01 10:46:53 -0700 | [diff] [blame] | 141 | // TODO(cmasone): Figure out what shill concept maps to flimflam's "Network". |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 142 | // known_properties_.push_back(flimflam::kNetworksProperty); |
| 143 | |
Chris Masone | 4d42df8 | 2011-07-02 17:09:39 -0700 | [diff] [blame] | 144 | // flimflam::kScanningProperty: Registered in WiFi, Cellular |
| 145 | // flimflam::kScanIntervalProperty: Registered in WiFi, Cellular |
| 146 | |
| 147 | // TODO(pstew): Initialize Interface monitor, so we can detect new interfaces |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 148 | VLOG(2) << "Device " << link_name_ << " index " << interface_index; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | Device::~Device() { |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 152 | VLOG(2) << "Device " << link_name_ << " destroyed."; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 153 | } |
| 154 | |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 155 | bool Device::TechnologyIs(const Technology::Identifier /*type*/) const { |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 156 | return false; |
| 157 | } |
| 158 | |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 159 | void Device::LinkEvent(unsigned flags, unsigned change) { |
mukesh agrawal | 47009f8 | 2011-08-25 14:07:35 -0700 | [diff] [blame] | 160 | VLOG(2) << "Device " << link_name_ |
| 161 | << std::showbase << std::hex |
| 162 | << " flags " << flags << " changed " << change |
| 163 | << std::dec << std::noshowbase; |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Darin Petkov | c086531 | 2011-09-16 15:31:20 -0700 | [diff] [blame] | 166 | void Device::Scan(Error *error) { |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 167 | VLOG(2) << "Device " << link_name_ << " scan requested."; |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame] | 168 | Error::PopulateAndLog(error, Error::kNotSupported, |
| 169 | "Device doesn't support scan."); |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 172 | void Device::RegisterOnNetwork(const std::string &/*network_id*/, Error *error, |
| 173 | const ResultCallback &/*callback*/) { |
| 174 | Error::PopulateAndLog(error, Error::kNotSupported, |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame] | 175 | "Device doesn't support network registration."); |
Darin Petkov | 9ae310f | 2011-08-30 15:41:13 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Darin Petkov | c64fe5e | 2012-01-11 12:46:13 +0100 | [diff] [blame] | 178 | void Device::RequirePIN( |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 179 | const string &/*pin*/, bool /*require*/, |
| 180 | Error *error, const ResultCallback &/*callback*/) { |
Darin Petkov | c64fe5e | 2012-01-11 12:46:13 +0100 | [diff] [blame] | 181 | VLOG(2) << __func__; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 182 | Error::PopulateAndLog(error, Error::kNotSupported, |
| 183 | "Device doesn't support RequirePIN."); |
Darin Petkov | e42e101 | 2011-08-31 12:35:04 -0700 | [diff] [blame] | 184 | } |
| 185 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 186 | void Device::EnterPIN(const string &/*pin*/, |
| 187 | Error *error, const ResultCallback &/*callback*/) { |
Darin Petkov | e5bc2cb | 2011-12-07 14:47:32 +0100 | [diff] [blame] | 188 | VLOG(2) << __func__; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 189 | Error::PopulateAndLog(error, Error::kNotSupported, |
| 190 | "Device doesn't support EnterPIN."); |
Darin Petkov | e42e101 | 2011-08-31 12:35:04 -0700 | [diff] [blame] | 191 | } |
| 192 | |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 193 | void Device::UnblockPIN(const string &/*unblock_code*/, |
| 194 | const string &/*pin*/, |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 195 | Error *error, const ResultCallback &/*callback*/) { |
Darin Petkov | c64fe5e | 2012-01-11 12:46:13 +0100 | [diff] [blame] | 196 | VLOG(2) << __func__; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 197 | Error::PopulateAndLog(error, Error::kNotSupported, |
| 198 | "Device doesn't support UnblockPIN."); |
Darin Petkov | e42e101 | 2011-08-31 12:35:04 -0700 | [diff] [blame] | 199 | } |
| 200 | |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 201 | void Device::ChangePIN(const string &/*old_pin*/, |
| 202 | const string &/*new_pin*/, |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 203 | Error *error, const ResultCallback &/*callback*/) { |
Darin Petkov | c64fe5e | 2012-01-11 12:46:13 +0100 | [diff] [blame] | 204 | VLOG(2) << __func__; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 205 | Error::PopulateAndLog(error, Error::kNotSupported, |
| 206 | "Device doesn't support ChangePIN."); |
Darin Petkov | e42e101 | 2011-08-31 12:35:04 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 209 | void Device::DisableIPv6() { |
| 210 | SetIPFlag(IPAddress::kFamilyIPv6, kIPFlagDisableIPv6, "1"); |
| 211 | } |
| 212 | |
| 213 | void Device::EnableIPv6() { |
| 214 | SetIPFlag(IPAddress::kFamilyIPv6, kIPFlagDisableIPv6, "0"); |
| 215 | } |
| 216 | |
| 217 | void Device::EnableIPv6Privacy() { |
| 218 | SetIPFlag(IPAddress::kFamilyIPv6, kIPFlagUseTempAddr, |
| 219 | kIPFlagUseTempAddrUsedAndDefault); |
| 220 | } |
| 221 | |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 222 | void Device::DisableReversePathFilter() { |
| 223 | // TODO(pstew): Current kernel doesn't offer reverse-path filtering flag |
| 224 | // for IPv6. crosbug.com/24228 |
| 225 | SetIPFlag(IPAddress::kFamilyIPv4, kIPFlagReversePathFilter, |
| 226 | kIPFlagReversePathFilterLooseMode); |
| 227 | } |
| 228 | |
| 229 | void Device::EnableReversePathFilter() { |
| 230 | SetIPFlag(IPAddress::kFamilyIPv4, kIPFlagReversePathFilter, |
| 231 | kIPFlagReversePathFilterEnabled); |
| 232 | } |
| 233 | |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 234 | bool Device::IsConnected() const { |
| 235 | if (selected_service_) |
| 236 | return selected_service_->IsConnected(); |
| 237 | return false; |
| 238 | } |
| 239 | |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 240 | string Device::GetRpcIdentifier() { |
| 241 | return adaptor_->GetRpcIdentifier(); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 242 | } |
| 243 | |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 244 | string Device::GetStorageIdentifier() { |
| 245 | string id = GetRpcIdentifier(); |
| 246 | ControlInterface::RpcIdToStorageId(&id); |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 247 | size_t needle = id.find('_'); |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 248 | DLOG_IF(ERROR, needle == string::npos) << "No _ in storage id?!?!"; |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 249 | id.replace(id.begin() + needle + 1, id.end(), hardware_address_); |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 250 | return id; |
| 251 | } |
| 252 | |
Jason Glasgow | b579005 | 2012-01-27 01:03:52 -0500 | [diff] [blame] | 253 | string Device::GetTechnologyString(Error */*error*/) { |
| 254 | return Technology::NameFromIdentifier(technology()); |
| 255 | } |
| 256 | |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 257 | const string& Device::FriendlyName() const { |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 258 | return link_name_; |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 261 | const string& Device::UniqueName() const { |
| 262 | return unique_id_; |
| 263 | } |
| 264 | |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 265 | bool Device::Load(StoreInterface *storage) { |
| 266 | const string id = GetStorageIdentifier(); |
| 267 | if (!storage->ContainsGroup(id)) { |
| 268 | LOG(WARNING) << "Device is not available in the persistent store: " << id; |
| 269 | return false; |
| 270 | } |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 271 | enabled_persistent_ = true; |
| 272 | storage->GetBool(id, kStoragePowered, &enabled_persistent_); |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 273 | // TODO(cmasone): What does it mean to load an IPConfig identifier?? |
| 274 | return true; |
| 275 | } |
| 276 | |
| 277 | bool Device::Save(StoreInterface *storage) { |
| 278 | const string id = GetStorageIdentifier(); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 279 | storage->SetBool(id, kStoragePowered, enabled_persistent_); |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 280 | if (ipconfig_.get()) { |
| 281 | // The _0 is an index into the list of IPConfigs that this device might |
| 282 | // have. We only have one IPConfig right now, and I hope to never have |
| 283 | // to support more, as sleffler indicates that associating IPConfigs |
| 284 | // with devices is wrong and due to be changed in flimflam anyhow. |
| 285 | string suffix = hardware_address_ + "_0"; |
| 286 | ipconfig_->Save(storage, suffix); |
| 287 | storage->SetString(id, kStorageIPConfigs, SerializeIPConfigs(suffix)); |
| 288 | } |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 289 | return true; |
| 290 | } |
| 291 | |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 292 | void Device::DestroyIPConfig() { |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 293 | DisableIPv6(); |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 294 | if (ipconfig_.get()) { |
| 295 | ipconfig_->ReleaseIP(); |
| 296 | ipconfig_ = NULL; |
| 297 | } |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 298 | DestroyConnection(); |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 299 | } |
| 300 | |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 301 | bool Device::AcquireIPConfig() { |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 302 | DestroyIPConfig(); |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 303 | EnableIPv6(); |
Paul Stewart | d32f484 | 2012-01-11 16:08:13 -0800 | [diff] [blame] | 304 | ipconfig_ = dhcp_provider_->CreateConfig(link_name_, manager_->GetHostName()); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 305 | ipconfig_->RegisterUpdateCallback(Bind(&Device::OnIPConfigUpdated, |
| 306 | weak_ptr_factory_.GetWeakPtr())); |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 307 | return ipconfig_->RequestIP(); |
| 308 | } |
| 309 | |
Jason Glasgow | b579005 | 2012-01-27 01:03:52 -0500 | [diff] [blame] | 310 | void Device::HelpRegisterDerivedString( |
| 311 | const string &name, |
| 312 | string(Device::*get)(Error *error), |
| 313 | void(Device::*set)(const string &value, Error *error)) { |
| 314 | store_.RegisterDerivedString( |
| 315 | name, |
| 316 | StringAccessor(new CustomAccessor<Device, string>(this, get, set))); |
| 317 | } |
| 318 | |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 319 | void Device::HelpRegisterDerivedStrings( |
| 320 | const string &name, |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 321 | Strings(Device::*get)(Error *error), |
| 322 | void(Device::*set)(const Strings &value, Error *error)) { |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 323 | store_.RegisterDerivedStrings( |
| 324 | name, |
| 325 | StringsAccessor(new CustomAccessor<Device, Strings>(this, get, set))); |
Chris Masone | 4e85161 | 2011-07-01 10:46:53 -0700 | [diff] [blame] | 326 | } |
| 327 | |
Darin Petkov | 79d74c9 | 2012-03-07 17:20:32 +0100 | [diff] [blame] | 328 | void Device::OnIPConfigUpdated(const IPConfigRefPtr &ipconfig, bool success) { |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 329 | VLOG(2) << __func__ << " " << " success: " << success; |
Paul Stewart | c39f113 | 2011-06-22 12:02:28 -0700 | [diff] [blame] | 330 | if (success) { |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 331 | CreateConnection(); |
| 332 | connection_->UpdateFromIPConfig(ipconfig); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 333 | // SetConnection must occur after the UpdateFromIPConfig so the |
| 334 | // service can use the values derived from the connection. |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 335 | if (selected_service_.get()) { |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 336 | selected_service_->SetConnection(connection_); |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 337 | } |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 338 | // The service state change needs to happen last, so that at the |
| 339 | // time we report the state change to the manager, the service |
| 340 | // has its connection. |
| 341 | SetServiceState(Service::kStateConnected); |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 342 | portal_attempts_to_online_ = 0; |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 343 | // Subtle: Start portal detection after transitioning the service |
| 344 | // to the Connected state because this call may immediately transition |
| 345 | // to the Online state. |
| 346 | StartPortalDetection(); |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 347 | } else { |
| 348 | // TODO(pstew): This logic gets more complex when multiple IPConfig types |
| 349 | // are run in parallel (e.g. DHCP and DHCP6) |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 350 | SetServiceState(Service::kStateDisconnected); |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 351 | DestroyConnection(); |
Paul Stewart | c39f113 | 2011-06-22 12:02:28 -0700 | [diff] [blame] | 352 | } |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 353 | } |
| 354 | |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 355 | void Device::CreateConnection() { |
| 356 | VLOG(2) << __func__; |
| 357 | if (!connection_.get()) { |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 358 | connection_ = new Connection(interface_index_, |
| 359 | link_name_, |
Paul Stewart | e00600e | 2012-03-16 07:08:00 -0700 | [diff] [blame] | 360 | technology_, |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 361 | manager_->device_info()); |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 362 | } |
| 363 | } |
| 364 | |
| 365 | void Device::DestroyConnection() { |
| 366 | VLOG(2) << __func__; |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 367 | StopPortalDetection(); |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 368 | if (selected_service_.get()) { |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 369 | selected_service_->SetConnection(NULL); |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 370 | } |
Paul Stewart | c8f4bef | 2011-12-13 09:45:51 -0800 | [diff] [blame] | 371 | connection_ = NULL; |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 372 | } |
| 373 | |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 374 | void Device::SelectService(const ServiceRefPtr &service) { |
Paul Stewart | a2b8cd1 | 2011-08-25 14:55:32 -0700 | [diff] [blame] | 375 | VLOG(2) << __func__ << ": " |
mukesh agrawal | 1590839 | 2011-11-16 18:29:25 +0000 | [diff] [blame] | 376 | << (service.get() ? |
| 377 | StringPrintf("%s (%s)", |
| 378 | service->UniqueName().c_str(), |
| 379 | service->friendly_name().c_str()) : |
| 380 | "*reset*"); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 381 | |
| 382 | if (selected_service_.get() == service.get()) { |
| 383 | // No change to |selected_service_|. Return early to avoid |
| 384 | // changing its state. |
| 385 | return; |
| 386 | } |
| 387 | |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 388 | if (selected_service_.get()) { |
| 389 | if (selected_service_->state() != Service::kStateFailure) { |
| 390 | selected_service_->SetState(Service::kStateIdle); |
| 391 | } |
| 392 | // TODO(pstew): We need to revisit the model here: should the Device |
| 393 | // subclass be responsible for calling DestroyIPConfig() (which would |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 394 | // trigger DestroyConnection() and Service::SetConnection(NULL))? |
Paul Stewart | be5f5b3 | 2011-12-07 17:11:11 -0800 | [diff] [blame] | 395 | // Ethernet does, but WiFi currently does not. crosbug.com/23929 |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 396 | selected_service_->SetConnection(NULL); |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 397 | } |
| 398 | selected_service_ = service; |
| 399 | } |
| 400 | |
| 401 | void Device::SetServiceState(Service::ConnectState state) { |
| 402 | if (selected_service_.get()) { |
| 403 | selected_service_->SetState(state); |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | void Device::SetServiceFailure(Service::ConnectFailure failure_state) { |
| 408 | if (selected_service_.get()) { |
| 409 | selected_service_->SetFailure(failure_state); |
| 410 | } |
| 411 | } |
| 412 | |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 413 | string Device::SerializeIPConfigs(const string &suffix) { |
| 414 | return StringPrintf("%s:%s", suffix.c_str(), ipconfig_->type().c_str()); |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 415 | } |
| 416 | |
Paul Stewart | 2bf1d35 | 2011-12-06 15:02:55 -0800 | [diff] [blame] | 417 | bool Device::SetIPFlag(IPAddress::Family family, const string &flag, |
| 418 | const string &value) { |
| 419 | string ip_version; |
| 420 | if (family == IPAddress::kFamilyIPv4) { |
| 421 | ip_version = kIPFlagVersion4; |
| 422 | } else if (family == IPAddress::kFamilyIPv6) { |
| 423 | ip_version = kIPFlagVersion6; |
| 424 | } else { |
| 425 | NOTIMPLEMENTED(); |
| 426 | } |
| 427 | FilePath flag_file(StringPrintf(kIPFlagTemplate, ip_version.c_str(), |
| 428 | link_name_.c_str(), flag.c_str())); |
| 429 | VLOG(2) << "Writing " << value << " to flag file " << flag_file.value(); |
| 430 | if (file_util::WriteFile(flag_file, value.c_str(), value.length()) != 1) { |
| 431 | LOG(ERROR) << StringPrintf("IP flag write failed: %s to %s", |
| 432 | value.c_str(), flag_file.value().c_str()); |
| 433 | return false; |
| 434 | } |
| 435 | return true; |
| 436 | } |
| 437 | |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 438 | bool Device::RequestPortalDetection() { |
| 439 | if (!selected_service_) { |
| 440 | VLOG(2) << FriendlyName() |
| 441 | << ": No selected service, so no need for portal check."; |
| 442 | return false; |
| 443 | } |
| 444 | |
| 445 | if (!connection_.get()) { |
| 446 | VLOG(2) << FriendlyName() |
| 447 | << ": No connection, so no need for portal check."; |
| 448 | return false; |
| 449 | } |
| 450 | |
| 451 | if (selected_service_->state() != Service::kStatePortal) { |
| 452 | VLOG(2) << FriendlyName() |
| 453 | << ": Service is not in portal state. No need to start check."; |
| 454 | return false; |
| 455 | } |
| 456 | |
| 457 | if (!connection_->is_default()) { |
| 458 | VLOG(2) << FriendlyName() |
| 459 | << ": Service is not the default connection. Don't start check."; |
| 460 | return false; |
| 461 | } |
| 462 | |
| 463 | if (portal_detector_.get() && portal_detector_->IsInProgress()) { |
| 464 | VLOG(2) << FriendlyName() << ": Portal detection is already running."; |
| 465 | return true; |
| 466 | } |
| 467 | |
| 468 | return StartPortalDetection(); |
| 469 | } |
| 470 | |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 471 | bool Device::StartPortalDetection() { |
| 472 | if (!manager_->IsPortalDetectionEnabled(technology())) { |
| 473 | // If portal detection is disabled for this technology, immediately set |
| 474 | // the service state to "Online". |
| 475 | VLOG(2) << "Device " << FriendlyName() |
| 476 | << ": portal detection is disabled; marking service online."; |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 477 | SetServiceConnectedState(Service::kStateOnline); |
| 478 | return false; |
| 479 | } |
| 480 | |
| 481 | DCHECK(selected_service_.get()); |
| 482 | if (selected_service_->HasProxyConfig()) { |
| 483 | // Services with HTTP proxy configurations should not be checked by the |
| 484 | // connection manager, since we don't have the ability to evaluate |
| 485 | // arbitrary proxy configs and their possible credentials. |
| 486 | VLOG(2) << "Device " << FriendlyName() |
| 487 | << ": service has proxy config; marking it online."; |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 488 | SetServiceConnectedState(Service::kStateOnline); |
| 489 | return false; |
| 490 | } |
| 491 | |
| 492 | portal_detector_.reset(new PortalDetector(connection_, |
| 493 | dispatcher_, |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 494 | portal_detector_callback_)); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 495 | if (!portal_detector_->Start(manager_->GetPortalCheckURL())) { |
| 496 | LOG(ERROR) << "Device " << FriendlyName() |
| 497 | << ": Portal detection failed to start: likely bad URL: " |
| 498 | << manager_->GetPortalCheckURL(); |
| 499 | SetServiceConnectedState(Service::kStateOnline); |
| 500 | return false; |
| 501 | } |
| 502 | |
| 503 | VLOG(2) << "Device " << FriendlyName() << ": portal detection has started."; |
| 504 | return true; |
| 505 | } |
| 506 | |
| 507 | void Device::StopPortalDetection() { |
| 508 | VLOG(2) << "Device " << FriendlyName() << ": portal detection has stopped."; |
| 509 | portal_detector_.reset(); |
| 510 | } |
| 511 | |
| 512 | void Device::SetServiceConnectedState(Service::ConnectState state) { |
| 513 | DCHECK(selected_service_.get()); |
| 514 | |
| 515 | if (!selected_service_.get()) { |
| 516 | LOG(ERROR) << FriendlyName() << ": " |
| 517 | << "Portal detection completed but no selected service exists!"; |
| 518 | return; |
| 519 | } |
| 520 | |
| 521 | if (!selected_service_->IsConnected()) { |
| 522 | LOG(ERROR) << FriendlyName() << ": " |
| 523 | << "Portal detection completed but selected service " |
| 524 | << selected_service_->UniqueName() |
| 525 | << " is in non-connected state."; |
| 526 | return; |
| 527 | } |
| 528 | |
Paul Stewart | c681fa0 | 2012-03-02 19:40:04 -0800 | [diff] [blame] | 529 | if (state == Service::kStatePortal && connection_->is_default() && |
| 530 | manager_->GetPortalCheckInterval() != 0) { |
| 531 | CHECK(portal_detector_.get()); |
| 532 | if (!portal_detector_->StartAfterDelay( |
| 533 | manager_->GetPortalCheckURL(), |
| 534 | manager_->GetPortalCheckInterval())) { |
| 535 | LOG(ERROR) << "Device " << FriendlyName() |
| 536 | << ": Portal detection failed to restart: likely bad URL: " |
| 537 | << manager_->GetPortalCheckURL(); |
| 538 | SetServiceState(Service::kStateOnline); |
| 539 | portal_detector_.reset(); |
| 540 | return; |
| 541 | } |
| 542 | VLOG(2) << "Device " << FriendlyName() << ": portal detection retrying."; |
| 543 | } else { |
| 544 | VLOG(2) << "Device " << FriendlyName() << ": portal will not retry."; |
| 545 | portal_detector_.reset(); |
| 546 | } |
| 547 | |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 548 | SetServiceState(state); |
| 549 | } |
| 550 | |
| 551 | void Device::PortalDetectorCallback(const PortalDetector::Result &result) { |
| 552 | if (!result.final) { |
| 553 | VLOG(2) << "Device " << FriendlyName() |
| 554 | << ": received non-final status: " |
| 555 | << PortalDetector::StatusToString(result.status); |
| 556 | return; |
| 557 | } |
| 558 | |
| 559 | VLOG(2) << "Device " << FriendlyName() |
| 560 | << ": received final status: " |
| 561 | << PortalDetector::StatusToString(result.status); |
| 562 | |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 563 | portal_attempts_to_online_ += result.num_attempts; |
| 564 | |
| 565 | metrics()->SendEnumToUMA( |
| 566 | metrics()->GetFullMetricName(Metrics::kMetricPortalResult, technology()), |
| 567 | Metrics::PortalDetectionResultToEnum(result), |
| 568 | Metrics::kPortalResultMax); |
| 569 | |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 570 | if (result.status == PortalDetector::kStatusSuccess) { |
| 571 | SetServiceConnectedState(Service::kStateOnline); |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 572 | |
| 573 | metrics()->SendToUMA( |
| 574 | metrics()->GetFullMetricName( |
| 575 | Metrics::kMetricPortalAttemptsToOnline, technology()), |
| 576 | portal_attempts_to_online_, |
| 577 | Metrics::kMetricPortalAttemptsToOnlineMin, |
| 578 | Metrics::kMetricPortalAttemptsToOnlineMax, |
| 579 | Metrics::kMetricPortalAttemptsToOnlineNumBuckets); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 580 | } else { |
| 581 | SetServiceConnectedState(Service::kStatePortal); |
Thieu Le | 85e050b | 2012-03-13 15:04:38 -0700 | [diff] [blame] | 582 | |
| 583 | metrics()->SendToUMA( |
| 584 | metrics()->GetFullMetricName( |
| 585 | Metrics::kMetricPortalAttempts, technology()), |
| 586 | result.num_attempts, |
| 587 | Metrics::kMetricPortalAttemptsMin, |
| 588 | Metrics::kMetricPortalAttemptsMax, |
| 589 | Metrics::kMetricPortalAttemptsNumBuckets); |
Paul Stewart | 20088d8 | 2012-02-16 06:58:55 -0800 | [diff] [blame] | 590 | } |
| 591 | } |
| 592 | |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 593 | vector<string> Device::AvailableIPConfigs(Error */*error*/) { |
Chris Masone | 4e85161 | 2011-07-01 10:46:53 -0700 | [diff] [blame] | 594 | string id = (ipconfig_.get() ? ipconfig_->GetRpcIdentifier() : string()); |
| 595 | return vector<string>(1, id); |
| 596 | } |
| 597 | |
| 598 | string Device::GetRpcConnectionIdentifier() { |
| 599 | return adaptor_->GetRpcConnectionIdentifier(); |
| 600 | } |
| 601 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 602 | // callback |
| 603 | void Device::OnEnabledStateChanged(const ResultCallback &callback, |
| 604 | const Error &error) { |
| 605 | VLOG(2) << __func__ << "(" << enabled_pending_ << ")"; |
| 606 | if (error.IsSuccess()) { |
| 607 | enabled_ = enabled_pending_; |
| 608 | manager_->UpdateEnabledTechnologies(); |
| 609 | adaptor_->EmitBoolChanged(flimflam::kPoweredProperty, enabled_); |
| 610 | adaptor_->UpdateEnabled(); |
| 611 | } |
| 612 | if (!callback.is_null()) |
| 613 | callback.Run(error); |
| 614 | } |
| 615 | |
| 616 | void Device::SetEnabled(bool enable) { |
| 617 | VLOG(2) << __func__ << "(" << enable << ")"; |
| 618 | SetEnabledInternal(enable, false, NULL, ResultCallback()); |
| 619 | } |
| 620 | |
| 621 | void Device::SetEnabledPersistent(bool enable, |
| 622 | Error *error, |
| 623 | const ResultCallback &callback) { |
| 624 | SetEnabledInternal(enable, true, error, callback); |
| 625 | } |
| 626 | |
| 627 | void Device::SetEnabledInternal(bool enable, |
| 628 | bool persist, |
| 629 | Error *error, |
| 630 | const ResultCallback &callback) { |
| 631 | VLOG(2) << "Device " << link_name_ << " " |
| 632 | << (enable ? "starting" : "stopping"); |
| 633 | if (enable == enabled_) { |
| 634 | if (error) |
| 635 | error->Reset(); |
| 636 | return; |
| 637 | } |
| 638 | |
| 639 | if (enabled_pending_ == enable) { |
| 640 | if (error) |
| 641 | error->Populate(Error::kInProgress, |
| 642 | "Enable operation already in progress"); |
| 643 | return; |
| 644 | } |
| 645 | |
| 646 | if (persist) { |
| 647 | enabled_persistent_ = enable; |
| 648 | manager_->SaveActiveProfile(); |
| 649 | } |
| 650 | |
| 651 | enabled_pending_ = enable; |
| 652 | EnabledStateChangedCallback enabled_callback = |
| 653 | Bind(&Device::OnEnabledStateChanged, |
| 654 | weak_ptr_factory_.GetWeakPtr(), callback); |
| 655 | if (enable) { |
| 656 | running_ = true; |
| 657 | routing_table_->FlushRoutes(interface_index()); |
| 658 | Start(error, enabled_callback); |
| 659 | } else { |
| 660 | running_ = false; |
| 661 | DestroyIPConfig(); // breaks a reference cycle |
| 662 | SelectService(NULL); // breaks a reference cycle |
| 663 | rtnl_handler_->SetInterfaceFlags(interface_index(), 0, IFF_UP); |
| 664 | VLOG(3) << "Device " << link_name_ << " ipconfig_ " |
| 665 | << (ipconfig_ ? "is set." : "is not set."); |
| 666 | VLOG(3) << "Device " << link_name_ << " connection_ " |
| 667 | << (connection_ ? "is set." : "is not set."); |
| 668 | VLOG(3) << "Device " << link_name_ << " selected_service_ " |
| 669 | << (selected_service_ ? "is set." : "is not set."); |
| 670 | Stop(error, enabled_callback); |
| 671 | } |
| 672 | } |
| 673 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 674 | } // namespace shill |