blob: 79a2f6e21630db549d3790d3f88fce528a9bf31e [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"
Arman Ugurayf84a4242013-04-09 20:01:07 -070019#include "shill/connection_health_checker.h"
Paul Stewart26b327e2011-10-19 11:38:09 -070020#include "shill/event_dispatcher.h"
Paul Stewart2bf1d352011-12-06 15:02:55 -080021#include "shill/ip_address.h"
Darin Petkovafa6fc42011-06-21 16:21:08 -070022#include "shill/ipconfig.h"
Paul Stewart20088d82012-02-16 06:58:55 -080023#include "shill/portal_detector.h"
Chris Masoneb925cc82011-06-22 15:39:57 -070024#include "shill/property_store.h"
Chris Masone2b105542011-06-22 10:58:09 -070025#include "shill/refptr_types.h"
Paul Stewart03dba0b2011-08-22 16:32:45 -070026#include "shill/service.h"
Paul Stewartfdd16072011-09-16 12:41:35 -070027#include "shill/technology.h"
Paul Stewart75897df2011-04-27 09:05:53 -070028
29namespace shill {
30
Chris Masone9be4a9d2011-05-16 15:44:09 -070031class ControlInterface;
Darin Petkov77cb6812011-08-15 16:19:41 -070032class DHCPProvider;
Chris Masone9be4a9d2011-05-16 15:44:09 -070033class DeviceAdaptorInterface;
Paul Stewartf1ce5d22011-05-19 13:10:20 -070034class Endpoint;
Chris Masone8fe2c7e2011-06-09 15:51:19 -070035class Error;
36class EventDispatcher;
Gaurav Shah6d2c72d2012-10-16 16:30:44 -070037class GeolocationInfo;
Paul Stewart036dba02012-08-07 12:34:41 -070038class LinkMonitor;
Paul Stewartf1ce5d22011-05-19 13:10:20 -070039class Manager;
Thieu Le3426c8f2012-01-11 17:35:11 -080040class Metrics;
Thieu Lefb46caf2012-03-08 11:57:15 -080041class RTNLHandler;
Ben Chanb061f892013-02-27 17:46:55 -080042class TrafficMonitor;
Chris Masone9be4a9d2011-05-16 15:44:09 -070043
Paul Stewart75897df2011-04-27 09:05:53 -070044// Device superclass. Individual network interfaces types will inherit from
45// this class.
Chris Masone27c4aa52011-07-02 13:10:14 -070046class Device : public base::RefCounted<Device> {
Paul Stewart75897df2011-04-27 09:05:53 -070047 public:
Wade Guthrie68d41092013-04-02 12:56:02 -070048 // Progressively scanning for access points (APs) is done with multiple scans,
49 // each containing a group of channels. The scans are performed in order of
50 // decreasing likelihood of connecting on one of the channels in a group
51 // (the number of channels in a group is a matter for system tuning). Fully
52 // scanning for APs does a complete scan of all the channels in a single scan.
53 // Progressive scanning is supported for wifi devices; technologies that
54 // support scan but don't support progressive scan will always perform a full
55 // scan, regardless of the requested scan type.
56 enum ScanType { kProgressiveScan, kFullScan };
57
Chris Masone9be4a9d2011-05-16 15:44:09 -070058 // A constructor for the Device object
59 Device(ControlInterface *control_interface,
Paul Stewartb50f0b92011-05-16 16:31:42 -070060 EventDispatcher *dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -080061 Metrics *metrics,
Paul Stewartf1ce5d22011-05-19 13:10:20 -070062 Manager *manager,
Darin Petkovafa6fc42011-06-21 16:21:08 -070063 const std::string &link_name,
Chris Masone626719f2011-08-18 16:58:48 -070064 const std::string &address,
Gaurav Shah435de2c2011-11-17 19:01:07 -080065 int interface_index,
66 Technology::Identifier technology);
Chris Masone9be4a9d2011-05-16 15:44:09 -070067
Eric Shienbrood9a245532012-03-07 14:20:39 -050068 // Enable or disable the device.
69 virtual void SetEnabled(bool enable);
70 // Enable or disable the device, and save the setting in the profile.
71 // The setting is persisted before the enable or disable operation
72 // starts, so that even if it fails, the user's intent is still recorded
73 // for the next time shill restarts.
Jason Glasgowdf7c5532012-05-14 14:41:45 -040074 virtual void SetEnabledPersistent(bool enable,
75 Error *error,
76 const ResultCallback &callback);
Gaurav Shah435de2c2011-11-17 19:01:07 -080077
Eric Shienbrood7fce52c2012-04-13 19:11:02 -040078 // Returns true if the underlying device reports that it is already enabled.
79 // Used when the device is registered with the Manager, so that shill can
80 // sync its state/ with the true state of the device. The default is to
81 // report false.
82 virtual bool IsUnderlyingDeviceEnabled() const;
83
Paul Stewartf1ce5d22011-05-19 13:10:20 -070084 virtual void LinkEvent(unsigned flags, unsigned change);
Paul Stewart75897df2011-04-27 09:05:53 -070085
Darin Petkov9ae310f2011-08-30 15:41:13 -070086 // The default implementation sets |error| to kNotSupported.
Wade Guthrie68d41092013-04-02 12:56:02 -070087 virtual void Scan(ScanType scan_type, Error *error);
Eric Shienbrood9a245532012-03-07 14:20:39 -050088 virtual void RegisterOnNetwork(const std::string &network_id, Error *error,
89 const ResultCallback &callback);
90 virtual void RequirePIN(const std::string &pin, bool require,
91 Error *error, const ResultCallback &callback);
92 virtual void EnterPIN(const std::string &pin,
93 Error *error, const ResultCallback &callback);
Darin Petkove42e1012011-08-31 12:35:04 -070094 virtual void UnblockPIN(const std::string &unblock_code,
95 const std::string &pin,
Eric Shienbrood9a245532012-03-07 14:20:39 -050096 Error *error, const ResultCallback &callback);
Darin Petkove42e1012011-08-31 12:35:04 -070097 virtual void ChangePIN(const std::string &old_pin,
98 const std::string &new_pin,
Eric Shienbrood9a245532012-03-07 14:20:39 -050099 Error *error, const ResultCallback &callback);
Ben Chanad663e12013-01-08 01:58:47 -0800100 virtual void Reset(Error *error, const ResultCallback &callback);
101
Darin Petkovc37a9c42012-09-06 15:28:22 +0200102 virtual void SetCarrier(const std::string &carrier,
103 Error *error, const ResultCallback &callback);
Paul Stewart2bf1d352011-12-06 15:02:55 -0800104 virtual void DisableIPv6();
105 virtual void EnableIPv6();
106 virtual void EnableIPv6Privacy();
Darin Petkov9ae310f2011-08-30 15:41:13 -0700107
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800108 // Request the removal of reverse-path filtering for this interface.
109 // This will allow packets destined for this interface to be accepted,
110 // even if this is not the default route for such a packet to arrive.
111 virtual void DisableReversePathFilter();
112
113 // Request reverse-path filtering for this interface.
114 virtual void EnableReversePathFilter();
115
Gaurav Shah435de2c2011-11-17 19:01:07 -0800116 // Returns true if the selected service on the device (if any) is connected.
117 // Returns false if there is no selected service, or if the selected service
118 // is not connected.
119 bool IsConnected() const;
120
Christopher Wiley5519e9e2013-01-08 16:55:56 -0800121 // Called by Device so that subclasses can run hooks on the selected service
122 // getting an IP. Subclasses should call up to the parent first.
123 virtual void OnConnected();
124
Paul Stewart8596f9f2013-03-14 07:58:26 -0700125 // Called by the Connection so that the Device can update the service sorting
126 // after one connection is bound to another.
127 virtual void OnConnectionUpdated();
128
Paul Stewartd215af62012-04-24 23:25:50 -0700129 // Returns true if the selected service on the device (if any) is connected
130 // and matches the passed-in argument |service|. Returns false if there is
131 // no connected service, or if it does not match |service|.
132 virtual bool IsConnectedToService(const ServiceRefPtr &service) const;
133
134 // Restart the portal detection process on a connected device. This is
135 // useful if the properties on the connected service have changed in a
136 // way that may affect the decision to run portal detection at all.
137 // Returns true if portal detection was started.
138 virtual bool RestartPortalDetection();
139
Ben Chanb061f892013-02-27 17:46:55 -0800140 // Get receive and transmit byte counters.
141 virtual uint64 GetReceiveByteCount();
142 virtual uint64 GetTransmitByteCount();
143
Paul Stewart6ff27f52012-07-11 06:51:41 -0700144 // Reset the persisted byte counters associated with the device.
145 void ResetByteCounters();
146
Paul Stewartc681fa02012-03-02 19:40:04 -0800147 // Requests that portal detection be done, if this device has the default
148 // connection. Returns true if portal detection was started.
149 virtual bool RequestPortalDetection();
150
mukesh agrawalf6b32092013-04-10 15:49:55 -0700151 std::string GetRpcIdentifier() const;
Chris Masone5dec5f42011-07-22 14:07:55 -0700152 std::string GetStorageIdentifier();
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700153
Gaurav Shah6d2c72d2012-10-16 16:30:44 -0700154 // Returns a list of Geolocation objects. Each object is multiple
155 // key-value pairs representing one entity that can be used for
156 // Geolocation.
157 virtual std::vector<GeolocationInfo> GetGeolocationObjects() const;
158
Chris Masone9d779932011-08-25 16:33:41 -0700159 const std::string &address() const { return hardware_address_; }
Darin Petkove0a312e2011-07-20 13:45:28 -0700160 const std::string &link_name() const { return link_name_; }
161 int interface_index() const { return interface_index_; }
Paul Stewarte6132022011-08-16 09:11:02 -0700162 const ConnectionRefPtr &connection() const { return connection_; }
Eric Shienbrood9a245532012-03-07 14:20:39 -0500163 bool enabled() const { return enabled_; }
164 bool enabled_persistent() const { return enabled_persistent_; }
Gaurav Shah435de2c2011-11-17 19:01:07 -0800165 virtual Technology::Identifier technology() const { return technology_; }
Jason Glasgowb5790052012-01-27 01:03:52 -0500166 std::string GetTechnologyString(Error *error);
Darin Petkove0a312e2011-07-20 13:45:28 -0700167
Thieu Le03026662013-04-04 10:45:11 -0700168 virtual const IPConfigRefPtr &ipconfig() const { return ipconfig_; }
Darin Petkov31a2eca2012-03-14 11:07:18 +0100169 void set_ipconfig(const IPConfigRefPtr &config) { ipconfig_ = config; }
170
Chris Masone19e30402011-07-19 15:48:47 -0700171 const std::string &FriendlyName() const;
Chris Masone27c4aa52011-07-02 13:10:14 -0700172
Darin Petkovafa6fc42011-06-21 16:21:08 -0700173 // Returns a string that is guaranteed to uniquely identify this Device
174 // instance.
175 const std::string &UniqueName() const;
Chris Masonea82b7112011-05-25 15:16:29 -0700176
mukesh agrawalde29fa82011-09-16 16:16:36 -0700177 PropertyStore *mutable_store() { return &store_; }
178 const PropertyStore &store() const { return store_; }
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700179 RTNLHandler *rtnl_handler() { return rtnl_handler_; }
Ben Chan19f83972012-10-03 23:25:56 -0700180 bool running() const { return running_; }
Chris Masone19e30402011-07-19 15:48:47 -0700181
Darin Petkovb05315f2011-11-07 10:14:25 +0100182 EventDispatcher *dispatcher() const { return dispatcher_; }
183
Paul Stewarta41e38d2011-11-11 07:47:29 -0800184 // Load configuration for the device from |storage|. This may include
185 // instantiating non-visible services for which configuration has been
186 // stored.
187 virtual bool Load(StoreInterface *storage);
188
189 // Save configuration for the device to |storage|.
Chris Masone877ff982011-09-21 16:18:24 -0700190 virtual bool Save(StoreInterface *storage);
Chris Masone5dec5f42011-07-22 14:07:55 -0700191
Darin Petkov77cb6812011-08-15 16:19:41 -0700192 void set_dhcp_provider(DHCPProvider *provider) { dhcp_provider_ = provider; }
193
Darin Petkov3cfbf212011-11-21 16:02:09 +0100194 DeviceAdaptorInterface *adaptor() const { return adaptor_.get(); }
195
Prathmesh Prabhuba99b592013-04-17 15:13:14 -0700196 void set_health_checker(ConnectionHealthChecker *health_checker) {
197 health_checker_.reset(health_checker);
198 }
199
mukesh agrawal784566d2012-08-08 18:32:58 -0700200 // Suspend event handler. Called by Manager before the system
201 // suspends. For this callback to be useful, the device must specify
202 // a suspend delay. Otherwise, there is no guarantee that the device
203 // will have time to complete its suspend actions, before the system
204 // is suspended.
205 //
206 // TODO(quiche): Add support for suspend delays. crosbug.com/33412
207 virtual void OnBeforeSuspend();
208
209 // Resume event handler. Called by Manager as the system resumes.
210 // The base class implementation takes care of renewing a DHCP lease
211 // (if necessary). Derived classes may implement any technology
212 // specific requirements by overriding, but should include a call to
213 // the base class implementation.
214 virtual void OnAfterResume();
215
Albert Chaulk0e1cdea2013-02-27 15:32:55 -0800216 // Destroy the lease, if any, with this |name|.
217 // Called by the service during Unload() as part of the cleanup sequence.
218 virtual void DestroyIPConfigLease(const std::string &name);
219
Paul Stewartd5843772011-05-11 15:40:42 -0700220 protected:
Darin Petkov2b8e44e2012-06-25 15:13:26 +0200221 friend class base::RefCounted<Device>;
Arman Ugurayf84a4242013-04-09 20:01:07 -0700222 friend class DeviceHealthCheckerTest;
Ben Chan19f83972012-10-03 23:25:56 -0700223 FRIEND_TEST(CellularServiceTest, IsAutoConnectable);
Arman Ugurayd42d8ec2013-04-08 19:28:21 -0700224 FRIEND_TEST(CellularTest, EnableTrafficMonitor);
Eric Shienbrood7fce52c2012-04-13 19:11:02 -0400225 FRIEND_TEST(CellularTest, ModemStateChangeDisable);
Arman Ugurayed8e6102012-11-29 14:47:20 -0800226 FRIEND_TEST(CellularTest, UseNoArpGateway);
Darin Petkov13e6d552012-05-09 14:22:23 +0200227 FRIEND_TEST(ConnectionTest, OnRouteQueryResponse);
Prathmesh Prabhuba99b592013-04-17 15:13:14 -0700228 FRIEND_TEST(DeviceHealthCheckerTest, HealthCheckerPersistsAcrossDeviceReset);
Arman Ugurayf84a4242013-04-09 20:01:07 -0700229 FRIEND_TEST(DeviceHealthCheckerTest, RequestConnectionHealthCheck);
Prathmesh Prabhuba99b592013-04-17 15:13:14 -0700230 FRIEND_TEST(DeviceHealthCheckerTest, SetupHealthChecker);
Paul Stewart2bf1d352011-12-06 15:02:55 -0800231 FRIEND_TEST(DeviceTest, AcquireIPConfig);
Darin Petkovafa6fc42011-06-21 16:21:08 -0700232 FRIEND_TEST(DeviceTest, DestroyIPConfig);
233 FRIEND_TEST(DeviceTest, DestroyIPConfigNULL);
Chris Masone95207da2011-06-29 16:50:49 -0700234 FRIEND_TEST(DeviceTest, GetProperties);
Paul Stewart6ff27f52012-07-11 06:51:41 -0700235 FRIEND_TEST(DeviceTest, Load);
Chris Masone5dec5f42011-07-22 14:07:55 -0700236 FRIEND_TEST(DeviceTest, Save);
Paul Stewart03dba0b2011-08-22 16:32:45 -0700237 FRIEND_TEST(DeviceTest, SelectedService);
Darin Petkove7c6ad32012-06-29 10:22:09 +0200238 FRIEND_TEST(DeviceTest, SetEnabledPersistent);
Paul Stewartc681fa02012-03-02 19:40:04 -0800239 FRIEND_TEST(DeviceTest, SetServiceConnectedState);
Paul Stewart8c116a92012-05-02 18:30:03 -0700240 FRIEND_TEST(DeviceTest, Start);
Ben Chanb061f892013-02-27 17:46:55 -0800241 FRIEND_TEST(DeviceTest, StartTrafficMonitor);
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700242 FRIEND_TEST(DeviceTest, Stop);
Ben Chanb061f892013-02-27 17:46:55 -0800243 FRIEND_TEST(DeviceTest, StopTrafficMonitor);
Paul Stewarta41e38d2011-11-11 07:47:29 -0800244 FRIEND_TEST(ManagerTest, DeviceRegistrationAndStart);
Gaurav Shah435de2c2011-11-17 19:01:07 -0800245 FRIEND_TEST(ManagerTest, ConnectedTechnologies);
246 FRIEND_TEST(ManagerTest, DefaultTechnology);
Jason Glasgowdf7c5532012-05-14 14:41:45 -0400247 FRIEND_TEST(ManagerTest, EnableTechnology);
248 FRIEND_TEST(ManagerTest, DisableTechnology);
mukesh agrawal32399322011-09-01 10:53:43 -0700249 FRIEND_TEST(WiFiMainTest, Connect);
Arman Ugurayed8e6102012-11-29 14:47:20 -0800250 FRIEND_TEST(WiFiMainTest, UseArpGateway);
Darin Petkov3a4100c2012-06-14 11:36:59 +0200251 FRIEND_TEST(WiMaxTest, ConnectTimeout);
Arman Ugurayed8e6102012-11-29 14:47:20 -0800252 FRIEND_TEST(WiMaxTest, UseNoArpGateway);
Darin Petkovafa6fc42011-06-21 16:21:08 -0700253
Darin Petkov2b8e44e2012-06-25 15:13:26 +0200254 virtual ~Device();
255
Eric Shienbrood9a245532012-03-07 14:20:39 -0500256 // Each device must implement this method to do the work needed to
257 // enable the device to operate for establishing network connections.
258 // The |error| argument, if not NULL,
259 // will refer to an Error that starts out with the value
260 // Error::kOperationInitiated. This reflects the assumption that
261 // enable (and disable) operations will usually be non-blocking,
262 // and their completion will be indicated by means of an asynchronous
263 // reply sometime later. There are two circumstances in which a
264 // device's Start() method may overwrite |error|:
265 //
266 // 1. If an early failure is detected, such that the non-blocking
267 // part of the operation never takes place, then |error| should
268 // be set to the appropriate value corresponding to the type
269 // of failure. This is the "immediate failure" case.
270 // 2. If the device is enabled without performing any non-blocking
271 // steps, then |error| should be Reset, i.e., its value set
272 // to Error::kSuccess. This is the "immediate success" case.
273 //
274 // In these two cases, because completion is immediate, |callback|
275 // is not used. If neither of these two conditions holds, then |error|
276 // should not be modified, and |callback| should be passed to the
277 // method that will initiate the non-blocking operation.
278 virtual void Start(Error *error,
279 const EnabledStateChangedCallback &callback) = 0;
280
281 // Each device must implement this method to do the work needed to
282 // disable the device, i.e., clear any running state, and make the
283 // device no longer capable of establishing network connections.
284 // The discussion for Start() regarding the use of |error| and
285 // |callback| apply to Stop() as well.
286 virtual void Stop(Error *error,
287 const EnabledStateChangedCallback &callback) = 0;
288
289 // The EnabledStateChangedCallback that gets passed to the device's
290 // Start() and Stop() methods is bound to this method. |callback|
291 // is the callback that was passed to SetEnabled().
292 void OnEnabledStateChanged(const ResultCallback &callback,
293 const Error &error);
294
Darin Petkov2b8e44e2012-06-25 15:13:26 +0200295 // Drops the currently selected service along with its IP configuration and
296 // connection, if any.
297 void DropConnection();
298
Darin Petkovafa6fc42011-06-21 16:21:08 -0700299 // If there's an IP configuration in |ipconfig_|, releases the IP address and
300 // destroys the configuration instance.
301 void DestroyIPConfig();
302
303 // Creates a new DHCP IP configuration instance, stores it in |ipconfig_| and
Paul Stewartd408fdf2012-05-07 17:15:57 -0700304 // requests a new IP configuration. Saves the DHCP lease to the generic
305 // lease filename based on the interface name. Registers a callback to
Darin Petkovafa6fc42011-06-21 16:21:08 -0700306 // IPConfigUpdatedCallback on IP configuration changes. Returns true if the IP
307 // request was successfully sent.
Paul Stewart2bf1d352011-12-06 15:02:55 -0800308 bool AcquireIPConfig();
Darin Petkovafa6fc42011-06-21 16:21:08 -0700309
Paul Stewartd408fdf2012-05-07 17:15:57 -0700310 // Creates a new DHCP IP configuration instance, stores it in |ipconfig_| and
311 // requests a new IP configuration. Saves the DHCP lease to a filename
312 // based on the passed-in |lease_name|. Registers a callback to
313 // IPConfigUpdatedCallback on IP configuration changes. Returns true if the IP
314 // request was successfully sent.
315 bool AcquireIPConfigWithLeaseName(const std::string &lease_name);
316
Darin Petkov79d74c92012-03-07 17:20:32 +0100317 // Callback invoked on every IP configuration update.
318 void OnIPConfigUpdated(const IPConfigRefPtr &ipconfig, bool success);
319
Paul Stewarte6132022011-08-16 09:11:02 -0700320 // Maintain connection state (Routes, IP Addresses and DNS) in the OS.
321 void CreateConnection();
322
323 // Remove connection state
324 void DestroyConnection();
325
Paul Stewart03dba0b2011-08-22 16:32:45 -0700326 // Selects a service to be "current" -- i.e. link-state or configuration
327 // events that happen to the device are attributed to this service.
328 void SelectService(const ServiceRefPtr &service);
329
330 // Set the state of the selected service
331 void SetServiceState(Service::ConnectState state);
332
333 // Set the failure of the selected service (implicitly sets the state to
334 // "failure")
335 void SetServiceFailure(Service::ConnectFailure failure_state);
336
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400337 // Records the failure mode and time of the selected service, and
338 // sets the Service state of the selected service to "Idle".
339 // Avoids showing a failure mole in the UI.
340 void SetServiceFailureSilent(Service::ConnectFailure failure_state);
341
Prathmesh Prabhuba99b592013-04-17 15:13:14 -0700342 virtual void SetupConnectionHealthChecker();
Arman Ugurayf84a4242013-04-09 20:01:07 -0700343
344 // Checks the network connectivity status by creating a TCP connection, and
345 // optionally sending a small amout of data.
346 void RequestConnectionHealthCheck();
347
348 // Responds to the result from connection health checker in a device specific
349 // manner.
350 virtual void OnConnectionHealthCheckerResult(
351 ConnectionHealthChecker::Result result);
352
Paul Stewart20088d82012-02-16 06:58:55 -0800353 // Called by the Portal Detector whenever a trial completes. Device
354 // subclasses that choose unique mappings from portal results to connected
355 // states can override this method in order to do so.
356 virtual void PortalDetectorCallback(const PortalDetector::Result &result);
357
358 // Initiate portal detection, if enabled for this device type.
359 bool StartPortalDetection();
360
361 // Stop portal detection if it is running.
362 void StopPortalDetection();
363
Paul Stewart036dba02012-08-07 12:34:41 -0700364 // Initiate link monitoring, if enabled for this device type.
365 bool StartLinkMonitor();
366
367 // Stop link monitoring if it is running.
368 void StopLinkMonitor();
369
370 // Respond to a LinkMonitor failure in a Device-specific manner.
371 virtual void OnLinkMonitorFailure();
372
Ben Chanb061f892013-02-27 17:46:55 -0800373 // Initiates traffic monitoring if it's enabled via
374 // set_traffic_monitor_enabled() and returns true if the monitoring
375 // is started.
376 bool StartTrafficMonitor();
377
378 // Stops traffic monitoring if it is running.
379 void StopTrafficMonitor();
380
Thieu Le03026662013-04-04 10:45:11 -0700381 // Responds to a TrafficMonitor no-network-routing failure in a
Ben Chanb061f892013-02-27 17:46:55 -0800382 // Device-specific manner.
Thieu Le03026662013-04-04 10:45:11 -0700383 virtual void OnNoNetworkRouting();
Ben Chanb061f892013-02-27 17:46:55 -0800384
Paul Stewart20088d82012-02-16 06:58:55 -0800385 // Set the state of the selected service, with checks to make sure
386 // the service is already in a connected state before doing so.
387 void SetServiceConnectedState(Service::ConnectState state);
388
Arman Ugurayed8e6102012-11-29 14:47:20 -0800389 // Specifies whether an ARP gateway should be used for the
390 // device technology.
391 virtual bool ShouldUseArpGateway() const;
392
Darin Petkov9893d9c2012-05-17 15:27:31 -0700393 const ServiceRefPtr &selected_service() const { return selected_service_; }
394
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700395 void HelpRegisterConstDerivedString(
Jason Glasgowb5790052012-01-27 01:03:52 -0500396 const std::string &name,
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700397 std::string(Device::*get)(Error *));
Chris Masoneb925cc82011-06-22 15:39:57 -0700398
Jason Glasgow08afdff2012-04-03 10:22:26 -0400399 void HelpRegisterConstDerivedRpcIdentifiers(
400 const std::string &name,
401 RpcIdentifiers(Device::*get)(Error *));
402
Paul Stewart6ff27f52012-07-11 06:51:41 -0700403 void HelpRegisterConstDerivedUint64(
404 const std::string &name,
405 uint64(Device::*get)(Error *));
406
Paul Stewartac4ac002011-08-26 12:04:26 -0700407 // Property getters reserved for subclasses
408 ControlInterface *control_interface() const { return control_interface_; }
Thieu Le3426c8f2012-01-11 17:35:11 -0800409 Metrics *metrics() const { return metrics_; }
Paul Stewartac4ac002011-08-26 12:04:26 -0700410 Manager *manager() const { return manager_; }
Paul Stewart036dba02012-08-07 12:34:41 -0700411 const LinkMonitor *link_monitor() const { return link_monitor_.get(); }
412 void set_link_monitor(LinkMonitor *link_monitor);
Ben Chanb061f892013-02-27 17:46:55 -0800413 const TrafficMonitor *traffic_monitor() const {
414 return traffic_monitor_.get();
415 }
416
417 // Ownership of |traffic_monitor| is taken.
418 void set_traffic_monitor(TrafficMonitor *traffic_monitor);
419 bool traffic_monitor_enabled() const { return traffic_monitor_enabled_; }
420 void set_traffic_monitor_enabled(bool traffic_monitor_enabled) {
421 traffic_monitor_enabled_ = traffic_monitor_enabled;
422 }
Paul Stewartd5843772011-05-11 15:40:42 -0700423
Paul Stewart75897df2011-04-27 09:05:53 -0700424 private:
Darin Petkov3189a472012-10-05 09:55:33 +0200425 friend class CellularCapabilityTest;
426 friend class CellularTest;
Chris Masone413a3192011-05-09 17:10:05 -0700427 friend class DeviceAdaptorInterface;
Paul Stewart6ff27f52012-07-11 06:51:41 -0700428 friend class DeviceByteCountTest;
Paul Stewartc681fa02012-03-02 19:40:04 -0800429 friend class DevicePortalDetectionTest;
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700430 friend class DeviceTest;
Paul Stewartced3ad72013-04-03 13:39:25 -0700431 friend class EthernetTest;
Darin Petkov3189a472012-10-05 09:55:33 +0200432 friend class OpenVPNDriverTest;
Paul Stewarte369ece2012-05-22 09:11:03 -0700433 friend class WiFiObjectTest;
Darin Petkovafa6fc42011-06-21 16:21:08 -0700434
Paul Stewart2bf1d352011-12-06 15:02:55 -0800435 static const char kIPFlagTemplate[];
436 static const char kIPFlagVersion4[];
437 static const char kIPFlagVersion6[];
438 static const char kIPFlagDisableIPv6[];
439 static const char kIPFlagUseTempAddr[];
440 static const char kIPFlagUseTempAddrUsedAndDefault[];
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800441 static const char kIPFlagReversePathFilter[];
442 static const char kIPFlagReversePathFilterEnabled[];
443 static const char kIPFlagReversePathFilterLooseMode[];
Paul Stewart41a071e2013-04-26 07:56:04 -0700444 static const char kStorageIPConfigs[];
Prathmesh Prabhuba99b592013-04-17 15:13:14 -0700445 static const char kStoragePowered[];
Paul Stewart6ff27f52012-07-11 06:51:41 -0700446 static const char kStorageReceiveByteCount[];
447 static const char kStorageTransmitByteCount[];
Chris Masone5dec5f42011-07-22 14:07:55 -0700448
Paul Stewart1062d9d2012-04-27 10:42:27 -0700449 // Configure static IP address parameters if the service provides them.
450 void ConfigureStaticIPTask();
451
Eric Shienbrood9a245532012-03-07 14:20:39 -0500452 void SetEnabledInternal(bool enable, bool persist,
453 Error *error, const ResultCallback &callback);
454
Chris Masone5dec5f42011-07-22 14:07:55 -0700455 // Right now, Devices reference IPConfigs directly when persisted to disk
456 // It's not clear that this makes sense long-term, but that's how it is now.
457 // This call generates a string in the right format for this persisting.
Chris Masone34af2182011-08-22 11:59:36 -0700458 // |suffix| is injected into the storage identifier used for the configs.
459 std::string SerializeIPConfigs(const std::string &suffix);
Chris Masone5dec5f42011-07-22 14:07:55 -0700460
Paul Stewart2bf1d352011-12-06 15:02:55 -0800461 // Set an IP configuration flag on the device. |ip_version| should be
462 // "ipv6" or "ipv4". |flag| should be the name of the flag to be set
463 // and |value| is what this flag should be set to.
464 bool SetIPFlag(IPAddress::Family family, const std::string &flag,
465 const std::string &value);
466
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800467 std::vector<std::string> AvailableIPConfigs(Error *error);
Chris Masone4e851612011-07-01 10:46:53 -0700468 std::string GetRpcConnectionIdentifier();
469
Paul Stewart036dba02012-08-07 12:34:41 -0700470 // Get the LinkMonitor's average response time.
471 uint64 GetLinkMonitorResponseTime(Error *error);
472
Ben Chanb061f892013-02-27 17:46:55 -0800473 // Get receive and transmit byte counters. These methods simply wrap
474 // GetReceiveByteCount and GetTransmitByteCount in order to be used by
475 // HelpRegisterConstDerivedUint64.
476 uint64 GetReceiveByteCountProperty(Error *error);
477 uint64 GetTransmitByteCountProperty(Error *error);
Paul Stewart6ff27f52012-07-11 06:51:41 -0700478
Eric Shienbrood9a245532012-03-07 14:20:39 -0500479 // |enabled_persistent_| is the value of the Powered property, as
480 // read from the profile. If it is not found in the profile, it
481 // defaults to true. |enabled_| reflects the real-time state of
482 // the device, i.e., enabled or disabled. |enabled_pending_| reflects
483 // the target state of the device while an enable or disable operation
484 // is occurring.
485 //
486 // Some typical sequences for these state variables are shown below.
487 //
488 // Shill starts up, profile has been read:
489 // |enabled_persistent_|=true |enabled_|=false |enabled_pending_|=false
490 //
491 // Shill acts on the value of |enabled_persistent_|, calls SetEnabled(true):
492 // |enabled_persistent_|=true |enabled_|=false |enabled_pending_|=true
493 //
494 // SetEnabled completes successfully, device is enabled:
495 // |enabled_persistent_|=true |enabled_|=true |enabled_pending_|=true
496 //
497 // User presses "Disable" button, SetEnabled(false) is called:
498 // |enabled_persistent_|=false |enabled_|=true |enabled_pending_|=false
499 //
500 // SetEnabled completes successfully, device is disabled:
501 // |enabled_persistent_|=false |enabled_|=false |enabled_pending_|=false
502 bool enabled_;
503 bool enabled_persistent_;
504 bool enabled_pending_;
505
506 // Other properties
Paul Stewartac4ac002011-08-26 12:04:26 -0700507 bool reconnect_;
508 const std::string hardware_address_;
509
510 PropertyStore store_;
511
Paul Stewartac4ac002011-08-26 12:04:26 -0700512 const int interface_index_;
Paul Stewarta41e38d2011-11-11 07:47:29 -0800513 bool running_; // indicates whether the device is actually in operation
Paul Stewartac4ac002011-08-26 12:04:26 -0700514 const std::string link_name_;
515 const std::string unique_id_;
516 ControlInterface *control_interface_;
517 EventDispatcher *dispatcher_;
Thieu Le3426c8f2012-01-11 17:35:11 -0800518 Metrics *metrics_;
Paul Stewartac4ac002011-08-26 12:04:26 -0700519 Manager *manager_;
520 IPConfigRefPtr ipconfig_;
521 ConnectionRefPtr connection_;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500522 base::WeakPtrFactory<Device> weak_ptr_factory_;
Darin Petkovafa6fc42011-06-21 16:21:08 -0700523 scoped_ptr<DeviceAdaptorInterface> adaptor_;
Paul Stewart20088d82012-02-16 06:58:55 -0800524 scoped_ptr<PortalDetector> portal_detector_;
Paul Stewart036dba02012-08-07 12:34:41 -0700525 scoped_ptr<LinkMonitor> link_monitor_;
Ben Chanb061f892013-02-27 17:46:55 -0800526 scoped_ptr<TrafficMonitor> traffic_monitor_;
Arman Ugurayf84a4242013-04-09 20:01:07 -0700527 scoped_ptr<ConnectionHealthChecker> health_checker_;
Ben Chanb061f892013-02-27 17:46:55 -0800528 bool traffic_monitor_enabled_;
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500529 base::Callback<void(const PortalDetector::Result &)>
530 portal_detector_callback_;
Gaurav Shah435de2c2011-11-17 19:01:07 -0800531 Technology::Identifier technology_;
Thieu Le85e050b2012-03-13 15:04:38 -0700532 // The number of portal detection attempts from Connected to Online state.
533 // This includes all failure/timeout attempts and the final successful
534 // attempt.
535 int portal_attempts_to_online_;
Darin Petkovafa6fc42011-06-21 16:21:08 -0700536
Paul Stewart6ff27f52012-07-11 06:51:41 -0700537 // Keep track of the offset between the interface-reported byte counters
538 // and our persisted value.
539 uint64 receive_byte_offset_;
540 uint64 transmit_byte_offset_;
541
Paul Stewart03dba0b2011-08-22 16:32:45 -0700542 // Maintain a reference to the connected / connecting service
543 ServiceRefPtr selected_service_;
544
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700545 // Cache singleton pointers for performance and test purposes.
Darin Petkov77cb6812011-08-15 16:19:41 -0700546 DHCPProvider *dhcp_provider_;
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700547 RTNLHandler *rtnl_handler_;
Darin Petkov77cb6812011-08-15 16:19:41 -0700548
Chris Masone9be4a9d2011-05-16 15:44:09 -0700549 DISALLOW_COPY_AND_ASSIGN(Device);
Paul Stewart75897df2011-04-27 09:05:53 -0700550};
551
Paul Stewart75897df2011-04-27 09:05:53 -0700552} // namespace shill
553
Darin Petkov2b8e44e2012-06-25 15:13:26 +0200554#endif // SHILL_DEVICE_H_