blob: 95dec9f5465c400578c98dee6d4af570ed184003 [file] [log] [blame]
Darin Petkovc64fe5e2012-01-11 12:46:13 +01001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Paul Stewart75897df2011-04-27 09:05:53 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Darin Petkov2b8e44e2012-06-25 15:13:26 +02005#ifndef SHILL_DEVICE_H_
6#define SHILL_DEVICE_H_
Paul Stewart75897df2011-04-27 09:05:53 -07007
Chris Masone46eaaf52011-05-24 13:08:30 -07008#include <string>
Chris Masone9be4a9d2011-05-16 15:44:09 -07009#include <vector>
10
Chris Masone8fe2c7e2011-06-09 15:51:19 -070011#include <base/basictypes.h>
Chris Masone487b8bf2011-05-13 16:27:57 -070012#include <base/memory/ref_counted.h>
Paul Stewartb50f0b92011-05-16 16:31:42 -070013#include <base/memory/scoped_ptr.h>
Eric Shienbrood9a245532012-03-07 14:20:39 -050014#include <base/memory/weak_ptr.h>
Darin Petkovafa6fc42011-06-21 16:21:08 -070015#include <gtest/gtest_prod.h> // for FRIEND_TEST
Paul Stewartd5843772011-05-11 15:40:42 -070016
Eric Shienbrood9a245532012-03-07 14:20:39 -050017#include "shill/adaptor_interfaces.h"
18#include "shill/callbacks.h"
Paul Stewart26b327e2011-10-19 11:38:09 -070019#include "shill/event_dispatcher.h"
Paul Stewart2bf1d352011-12-06 15:02:55 -080020#include "shill/ip_address.h"
Darin Petkovafa6fc42011-06-21 16:21:08 -070021#include "shill/ipconfig.h"
Paul Stewart20088d82012-02-16 06:58:55 -080022#include "shill/portal_detector.h"
Chris Masoneb925cc82011-06-22 15:39:57 -070023#include "shill/property_store.h"
Chris Masone2b105542011-06-22 10:58:09 -070024#include "shill/refptr_types.h"
Paul Stewart03dba0b2011-08-22 16:32:45 -070025#include "shill/service.h"
Paul Stewartfdd16072011-09-16 12:41:35 -070026#include "shill/technology.h"
Paul Stewart75897df2011-04-27 09:05:53 -070027
28namespace shill {
29
Chris Masone9be4a9d2011-05-16 15:44:09 -070030class ControlInterface;
Darin Petkov77cb6812011-08-15 16:19:41 -070031class DHCPProvider;
Chris Masone9be4a9d2011-05-16 15:44:09 -070032class DeviceAdaptorInterface;
Paul Stewartf1ce5d22011-05-19 13:10:20 -070033class Endpoint;
Chris Masone8fe2c7e2011-06-09 15:51:19 -070034class Error;
35class EventDispatcher;
Gaurav Shah6d2c72d2012-10-16 16:30:44 -070036class GeolocationInfo;
Paul Stewart036dba02012-08-07 12:34:41 -070037class LinkMonitor;
Paul Stewartf1ce5d22011-05-19 13:10:20 -070038class Manager;
Thieu Le3426c8f2012-01-11 17:35:11 -080039class Metrics;
Thieu Lefb46caf2012-03-08 11:57:15 -080040class RTNLHandler;
Ben Chanb061f892013-02-27 17:46:55 -080041class TrafficMonitor;
Chris Masone9be4a9d2011-05-16 15:44:09 -070042
Paul Stewart75897df2011-04-27 09:05:53 -070043// Device superclass. Individual network interfaces types will inherit from
44// this class.
Chris Masone27c4aa52011-07-02 13:10:14 -070045class Device : public base::RefCounted<Device> {
Paul Stewart75897df2011-04-27 09:05:53 -070046 public:
Wade Guthrie68d41092013-04-02 12:56:02 -070047 // Progressively scanning for access points (APs) is done with multiple scans,
48 // each containing a group of channels. The scans are performed in order of
49 // decreasing likelihood of connecting on one of the channels in a group
50 // (the number of channels in a group is a matter for system tuning). Fully
51 // scanning for APs does a complete scan of all the channels in a single scan.
52 // Progressive scanning is supported for wifi devices; technologies that
53 // support scan but don't support progressive scan will always perform a full
54 // scan, regardless of the requested scan type.
55 enum ScanType { kProgressiveScan, kFullScan };
56
Chris Masone9be4a9d2011-05-16 15:44:09 -070057 // A constructor for the Device object
58 Device(ControlInterface *control_interface,
Paul Stewartb50f0b92011-05-16 16:31:42 -070059 EventDispatcher *dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -080060 Metrics *metrics,
Paul Stewartf1ce5d22011-05-19 13:10:20 -070061 Manager *manager,
Darin Petkovafa6fc42011-06-21 16:21:08 -070062 const std::string &link_name,
Chris Masone626719f2011-08-18 16:58:48 -070063 const std::string &address,
Gaurav Shah435de2c2011-11-17 19:01:07 -080064 int interface_index,
65 Technology::Identifier technology);
Chris Masone9be4a9d2011-05-16 15:44:09 -070066
mukesh agrawal28185512013-10-18 16:57:09 -070067 // Enable or disable the device. This is a convenience method for
68 // cases where we want to SetEnabledNonPersistent, but don't care
69 // about the results.
Eric Shienbrood9a245532012-03-07 14:20:39 -050070 virtual void SetEnabled(bool enable);
mukesh agrawalb1136662013-10-14 16:39:38 -070071 // Enable or disable the device. Unlike SetEnabledPersistent, it does not
Ben Chan9f3dcf82013-09-25 18:04:58 -070072 // save the setting in the profile.
73 //
mukesh agrawal28185512013-10-18 16:57:09 -070074 // TODO(quiche): Replace both of the next two methods with calls to
75 // SetEnabledChecked.
Ben Chan9f3dcf82013-09-25 18:04:58 -070076 virtual void SetEnabledNonPersistent(bool enable,
77 Error *error,
78 const ResultCallback &callback);
Eric Shienbrood9a245532012-03-07 14:20:39 -050079 // Enable or disable the device, and save the setting in the profile.
80 // The setting is persisted before the enable or disable operation
81 // starts, so that even if it fails, the user's intent is still recorded
82 // for the next time shill restarts.
Jason Glasgowdf7c5532012-05-14 14:41:45 -040083 virtual void SetEnabledPersistent(bool enable,
84 Error *error,
85 const ResultCallback &callback);
mukesh agrawal28185512013-10-18 16:57:09 -070086 // Enable or disable the Device, depending on |enable|.
87 // Save the new setting to the profile, if |persist| is true.
88 // Report synchronous errors using |error|, and asynchronous completion
89 // with |callback|.
90 virtual void SetEnabledChecked(bool enable,
91 bool persist,
92 Error *error,
93 const ResultCallback &callback);
94 // Similar to SetEnabledChecked, but without sanity checking, and
95 // without saving the new value of |enable| to the profile. If you
96 // are sane (i.e. not Cellular), you should use
97 // SetEnabledChecked instead.
98 virtual void SetEnabledUnchecked(bool enable,
99 Error *error,
100 const ResultCallback &callback);
Gaurav Shah435de2c2011-11-17 19:01:07 -0800101
Eric Shienbrood7fce52c2012-04-13 19:11:02 -0400102 // Returns true if the underlying device reports that it is already enabled.
103 // Used when the device is registered with the Manager, so that shill can
104 // sync its state/ with the true state of the device. The default is to
105 // report false.
106 virtual bool IsUnderlyingDeviceEnabled() const;
107
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700108 virtual void LinkEvent(unsigned flags, unsigned change);
Paul Stewart75897df2011-04-27 09:05:53 -0700109
Darin Petkov9ae310f2011-08-30 15:41:13 -0700110 // The default implementation sets |error| to kNotSupported.
Wade Guthrie4823f4f2013-07-25 10:03:03 -0700111 virtual void Scan(ScanType scan_type, Error *error,
112 const std::string &reason);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500113 virtual void RegisterOnNetwork(const std::string &network_id, Error *error,
114 const ResultCallback &callback);
115 virtual void RequirePIN(const std::string &pin, bool require,
116 Error *error, const ResultCallback &callback);
117 virtual void EnterPIN(const std::string &pin,
118 Error *error, const ResultCallback &callback);
Darin Petkove42e1012011-08-31 12:35:04 -0700119 virtual void UnblockPIN(const std::string &unblock_code,
120 const std::string &pin,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500121 Error *error, const ResultCallback &callback);
Darin Petkove42e1012011-08-31 12:35:04 -0700122 virtual void ChangePIN(const std::string &old_pin,
123 const std::string &new_pin,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500124 Error *error, const ResultCallback &callback);
Ben Chanad663e12013-01-08 01:58:47 -0800125 virtual void Reset(Error *error, const ResultCallback &callback);
126
Darin Petkovc37a9c42012-09-06 15:28:22 +0200127 virtual void SetCarrier(const std::string &carrier,
128 Error *error, const ResultCallback &callback);
Ben Chanbcc6e012013-11-04 14:28:37 -0800129
130 // Returns true if IPv6 is allowed and should be enabled when the device
131 // tries to acquire an IP configuration. The default implementation allows
132 // IPv6, which can be overridden by a derived class.
133 virtual bool IsIPv6Allowed() const;
134
Paul Stewart2bf1d352011-12-06 15:02:55 -0800135 virtual void DisableIPv6();
136 virtual void EnableIPv6();
137 virtual void EnableIPv6Privacy();
Darin Petkov9ae310f2011-08-30 15:41:13 -0700138
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800139 // Request the removal of reverse-path filtering for this interface.
140 // This will allow packets destined for this interface to be accepted,
141 // even if this is not the default route for such a packet to arrive.
142 virtual void DisableReversePathFilter();
143
144 // Request reverse-path filtering for this interface.
145 virtual void EnableReversePathFilter();
146
Gaurav Shah435de2c2011-11-17 19:01:07 -0800147 // Returns true if the selected service on the device (if any) is connected.
148 // Returns false if there is no selected service, or if the selected service
149 // is not connected.
150 bool IsConnected() const;
151
Christopher Wiley5519e9e2013-01-08 16:55:56 -0800152 // Called by Device so that subclasses can run hooks on the selected service
153 // getting an IP. Subclasses should call up to the parent first.
154 virtual void OnConnected();
155
Paul Stewart8596f9f2013-03-14 07:58:26 -0700156 // Called by the Connection so that the Device can update the service sorting
157 // after one connection is bound to another.
158 virtual void OnConnectionUpdated();
159
Paul Stewartd215af62012-04-24 23:25:50 -0700160 // Returns true if the selected service on the device (if any) is connected
161 // and matches the passed-in argument |service|. Returns false if there is
162 // no connected service, or if it does not match |service|.
163 virtual bool IsConnectedToService(const ServiceRefPtr &service) const;
164
Paul Stewartfa11e282013-12-02 22:04:25 -0800165 // Returns true if the DHCP parameters provided indicate that we are tethered
166 // to a mobile device.
167 virtual bool IsConnectedViaTether() const;
168
Paul Stewartd215af62012-04-24 23:25:50 -0700169 // Restart the portal detection process on a connected device. This is
170 // useful if the properties on the connected service have changed in a
171 // way that may affect the decision to run portal detection at all.
172 // Returns true if portal detection was started.
173 virtual bool RestartPortalDetection();
174
Ben Chanb061f892013-02-27 17:46:55 -0800175 // Get receive and transmit byte counters.
176 virtual uint64 GetReceiveByteCount();
177 virtual uint64 GetTransmitByteCount();
178
Paul Stewartc6fbad92013-11-13 14:50:52 -0800179 // Perform a TDLS |operation| on the underlying device, with respect
180 // to a given |peer|. The string returned is empty for any operation
181 // other than kTDLSOperationStatus, which returns the state of the
182 // TDLS link with |peer|. This method is only valid for WiFi devices,
183 // but needs to be declared here since it is part of the Device RPC
184 // API.
185 virtual std::string PerformTDLSOperation(const std::string &operation,
186 const std::string &peer,
187 Error *error);
188
Paul Stewart6ff27f52012-07-11 06:51:41 -0700189 // Reset the persisted byte counters associated with the device.
190 void ResetByteCounters();
191
Paul Stewartc681fa02012-03-02 19:40:04 -0800192 // Requests that portal detection be done, if this device has the default
193 // connection. Returns true if portal detection was started.
194 virtual bool RequestPortalDetection();
195
mukesh agrawalf6b32092013-04-10 15:49:55 -0700196 std::string GetRpcIdentifier() const;
Chris Masone5dec5f42011-07-22 14:07:55 -0700197 std::string GetStorageIdentifier();
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700198
Gaurav Shah6d2c72d2012-10-16 16:30:44 -0700199 // Returns a list of Geolocation objects. Each object is multiple
200 // key-value pairs representing one entity that can be used for
201 // Geolocation.
202 virtual std::vector<GeolocationInfo> GetGeolocationObjects() const;
203
Chris Masone9d779932011-08-25 16:33:41 -0700204 const std::string &address() const { return hardware_address_; }
Darin Petkove0a312e2011-07-20 13:45:28 -0700205 const std::string &link_name() const { return link_name_; }
206 int interface_index() const { return interface_index_; }
Paul Stewarte6132022011-08-16 09:11:02 -0700207 const ConnectionRefPtr &connection() const { return connection_; }
Eric Shienbrood9a245532012-03-07 14:20:39 -0500208 bool enabled() const { return enabled_; }
209 bool enabled_persistent() const { return enabled_persistent_; }
Gaurav Shah435de2c2011-11-17 19:01:07 -0800210 virtual Technology::Identifier technology() const { return technology_; }
Jason Glasgowb5790052012-01-27 01:03:52 -0500211 std::string GetTechnologyString(Error *error);
Darin Petkove0a312e2011-07-20 13:45:28 -0700212
Thieu Le03026662013-04-04 10:45:11 -0700213 virtual const IPConfigRefPtr &ipconfig() const { return ipconfig_; }
Darin Petkov31a2eca2012-03-14 11:07:18 +0100214 void set_ipconfig(const IPConfigRefPtr &config) { ipconfig_ = config; }
215
Chris Masone19e30402011-07-19 15:48:47 -0700216 const std::string &FriendlyName() const;
Chris Masone27c4aa52011-07-02 13:10:14 -0700217
Darin Petkovafa6fc42011-06-21 16:21:08 -0700218 // Returns a string that is guaranteed to uniquely identify this Device
219 // instance.
220 const std::string &UniqueName() const;
Chris Masonea82b7112011-05-25 15:16:29 -0700221
mukesh agrawalde29fa82011-09-16 16:16:36 -0700222 PropertyStore *mutable_store() { return &store_; }
223 const PropertyStore &store() const { return store_; }
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700224 RTNLHandler *rtnl_handler() { return rtnl_handler_; }
Ben Chan19f83972012-10-03 23:25:56 -0700225 bool running() const { return running_; }
Chris Masone19e30402011-07-19 15:48:47 -0700226
Darin Petkovb05315f2011-11-07 10:14:25 +0100227 EventDispatcher *dispatcher() const { return dispatcher_; }
228
Paul Stewarta41e38d2011-11-11 07:47:29 -0800229 // Load configuration for the device from |storage|. This may include
230 // instantiating non-visible services for which configuration has been
231 // stored.
232 virtual bool Load(StoreInterface *storage);
233
234 // Save configuration for the device to |storage|.
Chris Masone877ff982011-09-21 16:18:24 -0700235 virtual bool Save(StoreInterface *storage);
Chris Masone5dec5f42011-07-22 14:07:55 -0700236
Darin Petkov77cb6812011-08-15 16:19:41 -0700237 void set_dhcp_provider(DHCPProvider *provider) { dhcp_provider_ = provider; }
238
Darin Petkov3cfbf212011-11-21 16:02:09 +0100239 DeviceAdaptorInterface *adaptor() const { return adaptor_.get(); }
240
mukesh agrawal784566d2012-08-08 18:32:58 -0700241 // Suspend event handler. Called by Manager before the system
mukesh agrawalb1136662013-10-14 16:39:38 -0700242 // suspends. This handler, along with any other suspect handlers,
243 // will have Manager::kTerminationActionsTimeoutMilliseconds to
244 // execute before the system enters the suspend state.
mukesh agrawal784566d2012-08-08 18:32:58 -0700245 //
mukesh agrawalb1136662013-10-14 16:39:38 -0700246 // Code that needs to run on exit, as well as on suspend, should use
247 // Manager::AddTerminationAction, rather than OnBeforeSuspend.
mukesh agrawal784566d2012-08-08 18:32:58 -0700248 virtual void OnBeforeSuspend();
249
250 // Resume event handler. Called by Manager as the system resumes.
251 // The base class implementation takes care of renewing a DHCP lease
252 // (if necessary). Derived classes may implement any technology
253 // specific requirements by overriding, but should include a call to
254 // the base class implementation.
255 virtual void OnAfterResume();
256
Albert Chaulk0e1cdea2013-02-27 15:32:55 -0800257 // Destroy the lease, if any, with this |name|.
258 // Called by the service during Unload() as part of the cleanup sequence.
259 virtual void DestroyIPConfigLease(const std::string &name);
260
Paul Stewartd55f6ae2014-04-24 16:34:21 -0700261 // Called by DeviceInfo when the kernel adds or removes a globally-scoped
262 // IPv6 address from this interface.
Paul Stewartd4f26482014-04-25 19:12:03 -0700263 virtual void OnIPv6AddressChanged();
Paul Stewartd55f6ae2014-04-24 16:34:21 -0700264
Paul Stewartd5843772011-05-11 15:40:42 -0700265 protected:
Darin Petkov2b8e44e2012-06-25 15:13:26 +0200266 friend class base::RefCounted<Device>;
Arman Ugurayf84a4242013-04-09 20:01:07 -0700267 friend class DeviceHealthCheckerTest;
Ben Chan19f83972012-10-03 23:25:56 -0700268 FRIEND_TEST(CellularServiceTest, IsAutoConnectable);
Arman Ugurayd42d8ec2013-04-08 19:28:21 -0700269 FRIEND_TEST(CellularTest, EnableTrafficMonitor);
Eric Shienbrood7fce52c2012-04-13 19:11:02 -0400270 FRIEND_TEST(CellularTest, ModemStateChangeDisable);
Arman Ugurayed8e6102012-11-29 14:47:20 -0800271 FRIEND_TEST(CellularTest, UseNoArpGateway);
Darin Petkov13e6d552012-05-09 14:22:23 +0200272 FRIEND_TEST(ConnectionTest, OnRouteQueryResponse);
Prathmesh Prabhuba99b592013-04-17 15:13:14 -0700273 FRIEND_TEST(DeviceHealthCheckerTest, HealthCheckerPersistsAcrossDeviceReset);
Arman Ugurayf84a4242013-04-09 20:01:07 -0700274 FRIEND_TEST(DeviceHealthCheckerTest, RequestConnectionHealthCheck);
Prathmesh Prabhuba99b592013-04-17 15:13:14 -0700275 FRIEND_TEST(DeviceHealthCheckerTest, SetupHealthChecker);
Paul Stewart2bf1d352011-12-06 15:02:55 -0800276 FRIEND_TEST(DeviceTest, AcquireIPConfig);
Paul Stewartd4f26482014-04-25 19:12:03 -0700277 FRIEND_TEST(DeviceTest, AvailableIPConfigs);
Darin Petkovafa6fc42011-06-21 16:21:08 -0700278 FRIEND_TEST(DeviceTest, DestroyIPConfig);
279 FRIEND_TEST(DeviceTest, DestroyIPConfigNULL);
Ben Chanbcc6e012013-11-04 14:28:37 -0800280 FRIEND_TEST(DeviceTest, EnableIPv6);
Chris Masone95207da2011-06-29 16:50:49 -0700281 FRIEND_TEST(DeviceTest, GetProperties);
Paul Stewartfa11e282013-12-02 22:04:25 -0800282 FRIEND_TEST(DeviceTest, IsConnectedViaTether);
Paul Stewart6ff27f52012-07-11 06:51:41 -0700283 FRIEND_TEST(DeviceTest, Load);
Paul Stewartd4f26482014-04-25 19:12:03 -0700284 FRIEND_TEST(DeviceTest, OnIPv6AddressChanged);
Chris Masone5dec5f42011-07-22 14:07:55 -0700285 FRIEND_TEST(DeviceTest, Save);
Paul Stewart03dba0b2011-08-22 16:32:45 -0700286 FRIEND_TEST(DeviceTest, SelectedService);
Ben Chan9f3dcf82013-09-25 18:04:58 -0700287 FRIEND_TEST(DeviceTest, SetEnabledNonPersistent);
Darin Petkove7c6ad32012-06-29 10:22:09 +0200288 FRIEND_TEST(DeviceTest, SetEnabledPersistent);
Paul Stewartc681fa02012-03-02 19:40:04 -0800289 FRIEND_TEST(DeviceTest, SetServiceConnectedState);
Paul Stewart75a68b92013-10-24 10:50:27 -0700290 FRIEND_TEST(DeviceTest, ShouldUseArpGateway);
291 FRIEND_TEST(DeviceTest, ShouldUseMinimalDHCPConfig);
Paul Stewart8c116a92012-05-02 18:30:03 -0700292 FRIEND_TEST(DeviceTest, Start);
Ben Chanb061f892013-02-27 17:46:55 -0800293 FRIEND_TEST(DeviceTest, StartTrafficMonitor);
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700294 FRIEND_TEST(DeviceTest, Stop);
Ben Chanb061f892013-02-27 17:46:55 -0800295 FRIEND_TEST(DeviceTest, StopTrafficMonitor);
Paul Stewarta41e38d2011-11-11 07:47:29 -0800296 FRIEND_TEST(ManagerTest, DeviceRegistrationAndStart);
Gaurav Shah435de2c2011-11-17 19:01:07 -0800297 FRIEND_TEST(ManagerTest, ConnectedTechnologies);
298 FRIEND_TEST(ManagerTest, DefaultTechnology);
mukesh agrawal46c27cc2013-07-10 16:39:10 -0700299 FRIEND_TEST(ManagerTest, SetEnabledStateForTechnology);
mukesh agrawalf407d592013-07-31 11:37:57 -0700300 FRIEND_TEST(PPPDeviceTest, UpdateIPConfigFromPPP);
mukesh agrawal32399322011-09-01 10:53:43 -0700301 FRIEND_TEST(WiFiMainTest, Connect);
Arman Ugurayed8e6102012-11-29 14:47:20 -0800302 FRIEND_TEST(WiFiMainTest, UseArpGateway);
Darin Petkov3a4100c2012-06-14 11:36:59 +0200303 FRIEND_TEST(WiMaxTest, ConnectTimeout);
Arman Ugurayed8e6102012-11-29 14:47:20 -0800304 FRIEND_TEST(WiMaxTest, UseNoArpGateway);
Darin Petkovafa6fc42011-06-21 16:21:08 -0700305
Darin Petkov2b8e44e2012-06-25 15:13:26 +0200306 virtual ~Device();
307
Eric Shienbrood9a245532012-03-07 14:20:39 -0500308 // Each device must implement this method to do the work needed to
309 // enable the device to operate for establishing network connections.
310 // The |error| argument, if not NULL,
311 // will refer to an Error that starts out with the value
312 // Error::kOperationInitiated. This reflects the assumption that
313 // enable (and disable) operations will usually be non-blocking,
314 // and their completion will be indicated by means of an asynchronous
315 // reply sometime later. There are two circumstances in which a
316 // device's Start() method may overwrite |error|:
317 //
318 // 1. If an early failure is detected, such that the non-blocking
319 // part of the operation never takes place, then |error| should
320 // be set to the appropriate value corresponding to the type
321 // of failure. This is the "immediate failure" case.
322 // 2. If the device is enabled without performing any non-blocking
323 // steps, then |error| should be Reset, i.e., its value set
324 // to Error::kSuccess. This is the "immediate success" case.
325 //
326 // In these two cases, because completion is immediate, |callback|
327 // is not used. If neither of these two conditions holds, then |error|
328 // should not be modified, and |callback| should be passed to the
329 // method that will initiate the non-blocking operation.
330 virtual void Start(Error *error,
331 const EnabledStateChangedCallback &callback) = 0;
332
333 // Each device must implement this method to do the work needed to
334 // disable the device, i.e., clear any running state, and make the
335 // device no longer capable of establishing network connections.
336 // The discussion for Start() regarding the use of |error| and
337 // |callback| apply to Stop() as well.
338 virtual void Stop(Error *error,
339 const EnabledStateChangedCallback &callback) = 0;
340
341 // The EnabledStateChangedCallback that gets passed to the device's
342 // Start() and Stop() methods is bound to this method. |callback|
343 // is the callback that was passed to SetEnabled().
344 void OnEnabledStateChanged(const ResultCallback &callback,
345 const Error &error);
346
Darin Petkov2b8e44e2012-06-25 15:13:26 +0200347 // Drops the currently selected service along with its IP configuration and
348 // connection, if any.
mukesh agrawal5d851b12013-07-11 14:09:41 -0700349 virtual void DropConnection();
Darin Petkov2b8e44e2012-06-25 15:13:26 +0200350
Darin Petkovafa6fc42011-06-21 16:21:08 -0700351 // If there's an IP configuration in |ipconfig_|, releases the IP address and
352 // destroys the configuration instance.
353 void DestroyIPConfig();
354
355 // Creates a new DHCP IP configuration instance, stores it in |ipconfig_| and
Paul Stewartd408fdf2012-05-07 17:15:57 -0700356 // requests a new IP configuration. Saves the DHCP lease to the generic
357 // lease filename based on the interface name. Registers a callback to
Darin Petkovafa6fc42011-06-21 16:21:08 -0700358 // IPConfigUpdatedCallback on IP configuration changes. Returns true if the IP
359 // request was successfully sent.
Paul Stewart2bf1d352011-12-06 15:02:55 -0800360 bool AcquireIPConfig();
Darin Petkovafa6fc42011-06-21 16:21:08 -0700361
Paul Stewartd408fdf2012-05-07 17:15:57 -0700362 // Creates a new DHCP IP configuration instance, stores it in |ipconfig_| and
363 // requests a new IP configuration. Saves the DHCP lease to a filename
364 // based on the passed-in |lease_name|. Registers a callback to
365 // IPConfigUpdatedCallback on IP configuration changes. Returns true if the IP
366 // request was successfully sent.
367 bool AcquireIPConfigWithLeaseName(const std::string &lease_name);
368
Ben Chan539ab022014-02-03 16:34:57 -0800369 // Assigns the IP configuration |properties| to |ipconfig_|.
370 void AssignIPConfig(const IPConfig::Properties &properties);
371
Paul Stewartc5099532013-12-12 07:53:15 -0800372 // Callback invoked on successful IP configuration updates.
373 void OnIPConfigUpdated(const IPConfigRefPtr &ipconfig);
374
375 // Callback invoked on IP configuration failures.
376 void OnIPConfigFailed(const IPConfigRefPtr &ipconfig);
Darin Petkov79d74c92012-03-07 17:20:32 +0100377
Paul Stewart82236532013-12-10 15:33:11 -0800378 // Callback invoked when "Refresh" is invoked on an IPConfig. This usually
379 // signals a change in static IP parameters.
380 void OnIPConfigRefreshed(const IPConfigRefPtr &ipconfig);
381
Paul Stewart1f916e42013-12-23 09:52:54 -0800382 // Callback invoked when an IPConfig restarts due to lease expiry. This
383 // is advisory, since an "Updated" or "Failed" signal is guaranteed to
384 // follow.
385 void OnIPConfigExpired(const IPConfigRefPtr &ipconfig);
386
Paul Stewartf6f96482013-07-12 12:49:15 -0700387 // Called by Device so that subclasses can run hooks on the selected service
388 // failing to get an IP. The default implementation disconnects the selected
389 // service with Service::kFailureDHCP.
390 virtual void OnIPConfigFailure();
391
Paul Stewarte6132022011-08-16 09:11:02 -0700392 // Maintain connection state (Routes, IP Addresses and DNS) in the OS.
393 void CreateConnection();
394
395 // Remove connection state
396 void DestroyConnection();
397
Paul Stewart03dba0b2011-08-22 16:32:45 -0700398 // Selects a service to be "current" -- i.e. link-state or configuration
399 // events that happen to the device are attributed to this service.
400 void SelectService(const ServiceRefPtr &service);
401
mukesh agrawalfc362912013-08-06 18:10:07 -0700402 // Set the state of the |selected_service_|.
mukesh agrawal0381f9a2013-07-11 16:41:52 -0700403 virtual void SetServiceState(Service::ConnectState state);
Paul Stewart03dba0b2011-08-22 16:32:45 -0700404
405 // Set the failure of the selected service (implicitly sets the state to
mukesh agrawalfc362912013-08-06 18:10:07 -0700406 // "failure").
mukesh agrawal0381f9a2013-07-11 16:41:52 -0700407 virtual void SetServiceFailure(Service::ConnectFailure failure_state);
Paul Stewart03dba0b2011-08-22 16:32:45 -0700408
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400409 // Records the failure mode and time of the selected service, and
410 // sets the Service state of the selected service to "Idle".
411 // Avoids showing a failure mole in the UI.
mukesh agrawal0381f9a2013-07-11 16:41:52 -0700412 virtual void SetServiceFailureSilent(Service::ConnectFailure failure_state);
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400413
Paul Stewart20088d82012-02-16 06:58:55 -0800414 // Called by the Portal Detector whenever a trial completes. Device
415 // subclasses that choose unique mappings from portal results to connected
416 // states can override this method in order to do so.
417 virtual void PortalDetectorCallback(const PortalDetector::Result &result);
418
419 // Initiate portal detection, if enabled for this device type.
420 bool StartPortalDetection();
421
422 // Stop portal detection if it is running.
423 void StopPortalDetection();
424
Paul Stewart036dba02012-08-07 12:34:41 -0700425 // Initiate link monitoring, if enabled for this device type.
426 bool StartLinkMonitor();
427
428 // Stop link monitoring if it is running.
429 void StopLinkMonitor();
430
431 // Respond to a LinkMonitor failure in a Device-specific manner.
432 virtual void OnLinkMonitorFailure();
433
Peter Qiu9d581932014-04-14 16:37:37 -0700434 // Respond to a LinkMonitor gateway's MAC address found/change event.
435 virtual void OnLinkMonitorGatewayChange();
Peter Qiub5d124f2014-04-14 12:05:02 -0700436
Paul Stewart20088d82012-02-16 06:58:55 -0800437 // Set the state of the selected service, with checks to make sure
438 // the service is already in a connected state before doing so.
439 void SetServiceConnectedState(Service::ConnectState state);
440
Arman Ugurayed8e6102012-11-29 14:47:20 -0800441 // Specifies whether an ARP gateway should be used for the
442 // device technology.
443 virtual bool ShouldUseArpGateway() const;
444
Paul Stewart75a68b92013-10-24 10:50:27 -0700445 // Specifies whether DHCP replies have not been succeeding or we have
446 // previously noticed that requesting a minimal DHCP response seems
447 // to alleviate issues with receiving a reply.
448 bool ShouldUseMinimalDHCPConfig() const;
449
Darin Petkov9893d9c2012-05-17 15:27:31 -0700450 const ServiceRefPtr &selected_service() const { return selected_service_; }
451
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700452 void HelpRegisterConstDerivedString(
Jason Glasgowb5790052012-01-27 01:03:52 -0500453 const std::string &name,
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700454 std::string(Device::*get)(Error *));
Chris Masoneb925cc82011-06-22 15:39:57 -0700455
Jason Glasgow08afdff2012-04-03 10:22:26 -0400456 void HelpRegisterConstDerivedRpcIdentifiers(
457 const std::string &name,
458 RpcIdentifiers(Device::*get)(Error *));
459
Paul Stewart6ff27f52012-07-11 06:51:41 -0700460 void HelpRegisterConstDerivedUint64(
461 const std::string &name,
462 uint64(Device::*get)(Error *));
463
Paul Stewartac4ac002011-08-26 12:04:26 -0700464 // Property getters reserved for subclasses
465 ControlInterface *control_interface() const { return control_interface_; }
Thieu Le3426c8f2012-01-11 17:35:11 -0800466 Metrics *metrics() const { return metrics_; }
Paul Stewartac4ac002011-08-26 12:04:26 -0700467 Manager *manager() const { return manager_; }
Paul Stewart036dba02012-08-07 12:34:41 -0700468 const LinkMonitor *link_monitor() const { return link_monitor_.get(); }
469 void set_link_monitor(LinkMonitor *link_monitor);
Paul Stewartd5843772011-05-11 15:40:42 -0700470
Paul Stewart75897df2011-04-27 09:05:53 -0700471 private:
Darin Petkov3189a472012-10-05 09:55:33 +0200472 friend class CellularCapabilityTest;
473 friend class CellularTest;
Chris Masone413a3192011-05-09 17:10:05 -0700474 friend class DeviceAdaptorInterface;
Paul Stewart6ff27f52012-07-11 06:51:41 -0700475 friend class DeviceByteCountTest;
Paul Stewartc681fa02012-03-02 19:40:04 -0800476 friend class DevicePortalDetectionTest;
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700477 friend class DeviceTest;
Paul Stewartced3ad72013-04-03 13:39:25 -0700478 friend class EthernetTest;
Darin Petkov3189a472012-10-05 09:55:33 +0200479 friend class OpenVPNDriverTest;
Ben Chanbcc6e012013-11-04 14:28:37 -0800480 friend class TestDevice;
mukesh agrawal4a165582013-08-02 15:58:21 -0700481 friend class VirtualDeviceTest;
Paul Stewarte369ece2012-05-22 09:11:03 -0700482 friend class WiFiObjectTest;
Darin Petkovafa6fc42011-06-21 16:21:08 -0700483
Paul Stewart2bf1d352011-12-06 15:02:55 -0800484 static const char kIPFlagTemplate[];
485 static const char kIPFlagVersion4[];
486 static const char kIPFlagVersion6[];
487 static const char kIPFlagDisableIPv6[];
488 static const char kIPFlagUseTempAddr[];
489 static const char kIPFlagUseTempAddrUsedAndDefault[];
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800490 static const char kIPFlagReversePathFilter[];
491 static const char kIPFlagReversePathFilterEnabled[];
492 static const char kIPFlagReversePathFilterLooseMode[];
Prathmesh Prabhuba99b592013-04-17 15:13:14 -0700493 static const char kStoragePowered[];
Paul Stewart6ff27f52012-07-11 06:51:41 -0700494 static const char kStorageReceiveByteCount[];
495 static const char kStorageTransmitByteCount[];
Chris Masone5dec5f42011-07-22 14:07:55 -0700496
Paul Stewart1062d9d2012-04-27 10:42:27 -0700497 // Configure static IP address parameters if the service provides them.
498 void ConfigureStaticIPTask();
499
Chris Masone5dec5f42011-07-22 14:07:55 -0700500 // Right now, Devices reference IPConfigs directly when persisted to disk
501 // It's not clear that this makes sense long-term, but that's how it is now.
502 // This call generates a string in the right format for this persisting.
Chris Masone34af2182011-08-22 11:59:36 -0700503 // |suffix| is injected into the storage identifier used for the configs.
504 std::string SerializeIPConfigs(const std::string &suffix);
Chris Masone5dec5f42011-07-22 14:07:55 -0700505
Ben Chanbcc6e012013-11-04 14:28:37 -0800506 // Set an IP configuration flag on the device. |family| should be "ipv6" or
507 // "ipv4". |flag| should be the name of the flag to be set and |value| is
508 // what this flag should be set to. Overridden by unit tests to pretend
509 // writing to procfs.
510 virtual bool SetIPFlag(IPAddress::Family family,
511 const std::string &flag,
512 const std::string &value);
Paul Stewart2bf1d352011-12-06 15:02:55 -0800513
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800514 std::vector<std::string> AvailableIPConfigs(Error *error);
Chris Masone4e851612011-07-01 10:46:53 -0700515 std::string GetRpcConnectionIdentifier();
516
Paul Stewart036dba02012-08-07 12:34:41 -0700517 // Get the LinkMonitor's average response time.
518 uint64 GetLinkMonitorResponseTime(Error *error);
519
Ben Chanb061f892013-02-27 17:46:55 -0800520 // Get receive and transmit byte counters. These methods simply wrap
521 // GetReceiveByteCount and GetTransmitByteCount in order to be used by
522 // HelpRegisterConstDerivedUint64.
523 uint64 GetReceiveByteCountProperty(Error *error);
524 uint64 GetTransmitByteCountProperty(Error *error);
Paul Stewart6ff27f52012-07-11 06:51:41 -0700525
Paul Stewartd4f26482014-04-25 19:12:03 -0700526 // Emit a property change signal for the "IPConfigs" property of this device.
527 void UpdateIPConfigsProperty();
528
Eric Shienbrood9a245532012-03-07 14:20:39 -0500529 // |enabled_persistent_| is the value of the Powered property, as
530 // read from the profile. If it is not found in the profile, it
531 // defaults to true. |enabled_| reflects the real-time state of
532 // the device, i.e., enabled or disabled. |enabled_pending_| reflects
533 // the target state of the device while an enable or disable operation
534 // is occurring.
535 //
536 // Some typical sequences for these state variables are shown below.
537 //
538 // Shill starts up, profile has been read:
539 // |enabled_persistent_|=true |enabled_|=false |enabled_pending_|=false
540 //
541 // Shill acts on the value of |enabled_persistent_|, calls SetEnabled(true):
542 // |enabled_persistent_|=true |enabled_|=false |enabled_pending_|=true
543 //
544 // SetEnabled completes successfully, device is enabled:
545 // |enabled_persistent_|=true |enabled_|=true |enabled_pending_|=true
546 //
547 // User presses "Disable" button, SetEnabled(false) is called:
548 // |enabled_persistent_|=false |enabled_|=true |enabled_pending_|=false
549 //
550 // SetEnabled completes successfully, device is disabled:
551 // |enabled_persistent_|=false |enabled_|=false |enabled_pending_|=false
552 bool enabled_;
553 bool enabled_persistent_;
554 bool enabled_pending_;
555
556 // Other properties
Paul Stewartac4ac002011-08-26 12:04:26 -0700557 bool reconnect_;
558 const std::string hardware_address_;
559
560 PropertyStore store_;
561
Paul Stewartac4ac002011-08-26 12:04:26 -0700562 const int interface_index_;
Paul Stewarta41e38d2011-11-11 07:47:29 -0800563 bool running_; // indicates whether the device is actually in operation
Paul Stewartac4ac002011-08-26 12:04:26 -0700564 const std::string link_name_;
565 const std::string unique_id_;
566 ControlInterface *control_interface_;
567 EventDispatcher *dispatcher_;
Thieu Le3426c8f2012-01-11 17:35:11 -0800568 Metrics *metrics_;
Paul Stewartac4ac002011-08-26 12:04:26 -0700569 Manager *manager_;
570 IPConfigRefPtr ipconfig_;
Paul Stewartd4f26482014-04-25 19:12:03 -0700571 IPConfigRefPtr ip6config_;
Paul Stewartac4ac002011-08-26 12:04:26 -0700572 ConnectionRefPtr connection_;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500573 base::WeakPtrFactory<Device> weak_ptr_factory_;
Darin Petkovafa6fc42011-06-21 16:21:08 -0700574 scoped_ptr<DeviceAdaptorInterface> adaptor_;
Paul Stewart20088d82012-02-16 06:58:55 -0800575 scoped_ptr<PortalDetector> portal_detector_;
Paul Stewart036dba02012-08-07 12:34:41 -0700576 scoped_ptr<LinkMonitor> link_monitor_;
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500577 base::Callback<void(const PortalDetector::Result &)>
578 portal_detector_callback_;
Gaurav Shah435de2c2011-11-17 19:01:07 -0800579 Technology::Identifier technology_;
Thieu Le85e050b2012-03-13 15:04:38 -0700580 // The number of portal detection attempts from Connected to Online state.
581 // This includes all failure/timeout attempts and the final successful
582 // attempt.
583 int portal_attempts_to_online_;
Darin Petkovafa6fc42011-06-21 16:21:08 -0700584
Paul Stewart6ff27f52012-07-11 06:51:41 -0700585 // Keep track of the offset between the interface-reported byte counters
586 // and our persisted value.
587 uint64 receive_byte_offset_;
588 uint64 transmit_byte_offset_;
589
Paul Stewart03dba0b2011-08-22 16:32:45 -0700590 // Maintain a reference to the connected / connecting service
591 ServiceRefPtr selected_service_;
592
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700593 // Cache singleton pointers for performance and test purposes.
Darin Petkov77cb6812011-08-15 16:19:41 -0700594 DHCPProvider *dhcp_provider_;
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700595 RTNLHandler *rtnl_handler_;
Darin Petkov77cb6812011-08-15 16:19:41 -0700596
Chris Masone9be4a9d2011-05-16 15:44:09 -0700597 DISALLOW_COPY_AND_ASSIGN(Device);
Paul Stewart75897df2011-04-27 09:05:53 -0700598};
599
Paul Stewart75897df2011-04-27 09:05:53 -0700600} // namespace shill
601
Darin Petkov2b8e44e2012-06-25 15:13:26 +0200602#endif // SHILL_DEVICE_H_