blob: c1731525f0d358e15b5ae8fa64659cf2131d8b87 [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
mukesh agrawal784566d2012-08-08 18:32:58 -0700196 // Suspend event handler. Called by Manager before the system
197 // suspends. For this callback to be useful, the device must specify
198 // a suspend delay. Otherwise, there is no guarantee that the device
199 // will have time to complete its suspend actions, before the system
200 // is suspended.
201 //
202 // TODO(quiche): Add support for suspend delays. crosbug.com/33412
203 virtual void OnBeforeSuspend();
204
205 // Resume event handler. Called by Manager as the system resumes.
206 // The base class implementation takes care of renewing a DHCP lease
207 // (if necessary). Derived classes may implement any technology
208 // specific requirements by overriding, but should include a call to
209 // the base class implementation.
210 virtual void OnAfterResume();
211
Albert Chaulk0e1cdea2013-02-27 15:32:55 -0800212 // Destroy the lease, if any, with this |name|.
213 // Called by the service during Unload() as part of the cleanup sequence.
214 virtual void DestroyIPConfigLease(const std::string &name);
215
Paul Stewartd5843772011-05-11 15:40:42 -0700216 protected:
Darin Petkov2b8e44e2012-06-25 15:13:26 +0200217 friend class base::RefCounted<Device>;
Arman Ugurayf84a4242013-04-09 20:01:07 -0700218 friend class DeviceHealthCheckerTest;
Ben Chan19f83972012-10-03 23:25:56 -0700219 FRIEND_TEST(CellularServiceTest, IsAutoConnectable);
Arman Ugurayd42d8ec2013-04-08 19:28:21 -0700220 FRIEND_TEST(CellularTest, EnableTrafficMonitor);
Eric Shienbrood7fce52c2012-04-13 19:11:02 -0400221 FRIEND_TEST(CellularTest, ModemStateChangeDisable);
Arman Ugurayed8e6102012-11-29 14:47:20 -0800222 FRIEND_TEST(CellularTest, UseNoArpGateway);
Darin Petkov13e6d552012-05-09 14:22:23 +0200223 FRIEND_TEST(ConnectionTest, OnRouteQueryResponse);
Paul Stewart41a071e2013-04-26 07:56:04 -0700224 FRIEND_TEST(DeviceHealthCheckerTest, CreateConnectionCreatesHealthChecker);
225 FRIEND_TEST(DeviceHealthCheckerTest, InitializeHealthCheckIps);
Arman Ugurayf84a4242013-04-09 20:01:07 -0700226 FRIEND_TEST(DeviceHealthCheckerTest, RequestConnectionHealthCheck);
Paul Stewart2bf1d352011-12-06 15:02:55 -0800227 FRIEND_TEST(DeviceTest, AcquireIPConfig);
Darin Petkovafa6fc42011-06-21 16:21:08 -0700228 FRIEND_TEST(DeviceTest, DestroyIPConfig);
229 FRIEND_TEST(DeviceTest, DestroyIPConfigNULL);
Chris Masone95207da2011-06-29 16:50:49 -0700230 FRIEND_TEST(DeviceTest, GetProperties);
Paul Stewart6ff27f52012-07-11 06:51:41 -0700231 FRIEND_TEST(DeviceTest, Load);
Chris Masone5dec5f42011-07-22 14:07:55 -0700232 FRIEND_TEST(DeviceTest, Save);
Paul Stewart03dba0b2011-08-22 16:32:45 -0700233 FRIEND_TEST(DeviceTest, SelectedService);
Darin Petkove7c6ad32012-06-29 10:22:09 +0200234 FRIEND_TEST(DeviceTest, SetEnabledPersistent);
Paul Stewartc681fa02012-03-02 19:40:04 -0800235 FRIEND_TEST(DeviceTest, SetServiceConnectedState);
Paul Stewart8c116a92012-05-02 18:30:03 -0700236 FRIEND_TEST(DeviceTest, Start);
Ben Chanb061f892013-02-27 17:46:55 -0800237 FRIEND_TEST(DeviceTest, StartTrafficMonitor);
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700238 FRIEND_TEST(DeviceTest, Stop);
Ben Chanb061f892013-02-27 17:46:55 -0800239 FRIEND_TEST(DeviceTest, StopTrafficMonitor);
Paul Stewarta41e38d2011-11-11 07:47:29 -0800240 FRIEND_TEST(ManagerTest, DeviceRegistrationAndStart);
Gaurav Shah435de2c2011-11-17 19:01:07 -0800241 FRIEND_TEST(ManagerTest, ConnectedTechnologies);
242 FRIEND_TEST(ManagerTest, DefaultTechnology);
Jason Glasgowdf7c5532012-05-14 14:41:45 -0400243 FRIEND_TEST(ManagerTest, EnableTechnology);
244 FRIEND_TEST(ManagerTest, DisableTechnology);
mukesh agrawal32399322011-09-01 10:53:43 -0700245 FRIEND_TEST(WiFiMainTest, Connect);
Arman Ugurayed8e6102012-11-29 14:47:20 -0800246 FRIEND_TEST(WiFiMainTest, UseArpGateway);
Darin Petkov3a4100c2012-06-14 11:36:59 +0200247 FRIEND_TEST(WiMaxTest, ConnectTimeout);
Arman Ugurayed8e6102012-11-29 14:47:20 -0800248 FRIEND_TEST(WiMaxTest, UseNoArpGateway);
Darin Petkovafa6fc42011-06-21 16:21:08 -0700249
Darin Petkov2b8e44e2012-06-25 15:13:26 +0200250 virtual ~Device();
251
Eric Shienbrood9a245532012-03-07 14:20:39 -0500252 // Each device must implement this method to do the work needed to
253 // enable the device to operate for establishing network connections.
254 // The |error| argument, if not NULL,
255 // will refer to an Error that starts out with the value
256 // Error::kOperationInitiated. This reflects the assumption that
257 // enable (and disable) operations will usually be non-blocking,
258 // and their completion will be indicated by means of an asynchronous
259 // reply sometime later. There are two circumstances in which a
260 // device's Start() method may overwrite |error|:
261 //
262 // 1. If an early failure is detected, such that the non-blocking
263 // part of the operation never takes place, then |error| should
264 // be set to the appropriate value corresponding to the type
265 // of failure. This is the "immediate failure" case.
266 // 2. If the device is enabled without performing any non-blocking
267 // steps, then |error| should be Reset, i.e., its value set
268 // to Error::kSuccess. This is the "immediate success" case.
269 //
270 // In these two cases, because completion is immediate, |callback|
271 // is not used. If neither of these two conditions holds, then |error|
272 // should not be modified, and |callback| should be passed to the
273 // method that will initiate the non-blocking operation.
274 virtual void Start(Error *error,
275 const EnabledStateChangedCallback &callback) = 0;
276
277 // Each device must implement this method to do the work needed to
278 // disable the device, i.e., clear any running state, and make the
279 // device no longer capable of establishing network connections.
280 // The discussion for Start() regarding the use of |error| and
281 // |callback| apply to Stop() as well.
282 virtual void Stop(Error *error,
283 const EnabledStateChangedCallback &callback) = 0;
284
285 // The EnabledStateChangedCallback that gets passed to the device's
286 // Start() and Stop() methods is bound to this method. |callback|
287 // is the callback that was passed to SetEnabled().
288 void OnEnabledStateChanged(const ResultCallback &callback,
289 const Error &error);
290
Darin Petkov2b8e44e2012-06-25 15:13:26 +0200291 // Drops the currently selected service along with its IP configuration and
292 // connection, if any.
293 void DropConnection();
294
Darin Petkovafa6fc42011-06-21 16:21:08 -0700295 // If there's an IP configuration in |ipconfig_|, releases the IP address and
296 // destroys the configuration instance.
297 void DestroyIPConfig();
298
299 // Creates a new DHCP IP configuration instance, stores it in |ipconfig_| and
Paul Stewartd408fdf2012-05-07 17:15:57 -0700300 // requests a new IP configuration. Saves the DHCP lease to the generic
301 // lease filename based on the interface name. Registers a callback to
Darin Petkovafa6fc42011-06-21 16:21:08 -0700302 // IPConfigUpdatedCallback on IP configuration changes. Returns true if the IP
303 // request was successfully sent.
Paul Stewart2bf1d352011-12-06 15:02:55 -0800304 bool AcquireIPConfig();
Darin Petkovafa6fc42011-06-21 16:21:08 -0700305
Paul Stewartd408fdf2012-05-07 17:15:57 -0700306 // Creates a new DHCP IP configuration instance, stores it in |ipconfig_| and
307 // requests a new IP configuration. Saves the DHCP lease to a filename
308 // based on the passed-in |lease_name|. Registers a callback to
309 // IPConfigUpdatedCallback on IP configuration changes. Returns true if the IP
310 // request was successfully sent.
311 bool AcquireIPConfigWithLeaseName(const std::string &lease_name);
312
Darin Petkov79d74c92012-03-07 17:20:32 +0100313 // Callback invoked on every IP configuration update.
314 void OnIPConfigUpdated(const IPConfigRefPtr &ipconfig, bool success);
315
Paul Stewarte6132022011-08-16 09:11:02 -0700316 // Maintain connection state (Routes, IP Addresses and DNS) in the OS.
317 void CreateConnection();
318
319 // Remove connection state
320 void DestroyConnection();
321
Paul Stewart03dba0b2011-08-22 16:32:45 -0700322 // Selects a service to be "current" -- i.e. link-state or configuration
323 // events that happen to the device are attributed to this service.
324 void SelectService(const ServiceRefPtr &service);
325
326 // Set the state of the selected service
327 void SetServiceState(Service::ConnectState state);
328
329 // Set the failure of the selected service (implicitly sets the state to
330 // "failure")
331 void SetServiceFailure(Service::ConnectFailure failure_state);
332
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400333 // Records the failure mode and time of the selected service, and
334 // sets the Service state of the selected service to "Idle".
335 // Avoids showing a failure mole in the UI.
336 void SetServiceFailureSilent(Service::ConnectFailure failure_state);
337
Paul Stewart41a071e2013-04-26 07:56:04 -0700338 // Initializes the health checker's internal collection of remote IPs to try.
339 void InitializeHealthCheckIps();
Arman Ugurayf84a4242013-04-09 20:01:07 -0700340
341 // Checks the network connectivity status by creating a TCP connection, and
342 // optionally sending a small amout of data.
343 void RequestConnectionHealthCheck();
344
345 // Responds to the result from connection health checker in a device specific
346 // manner.
347 virtual void OnConnectionHealthCheckerResult(
348 ConnectionHealthChecker::Result result);
349
Paul Stewart20088d82012-02-16 06:58:55 -0800350 // Called by the Portal Detector whenever a trial completes. Device
351 // subclasses that choose unique mappings from portal results to connected
352 // states can override this method in order to do so.
353 virtual void PortalDetectorCallback(const PortalDetector::Result &result);
354
355 // Initiate portal detection, if enabled for this device type.
356 bool StartPortalDetection();
357
358 // Stop portal detection if it is running.
359 void StopPortalDetection();
360
Paul Stewart036dba02012-08-07 12:34:41 -0700361 // Initiate link monitoring, if enabled for this device type.
362 bool StartLinkMonitor();
363
364 // Stop link monitoring if it is running.
365 void StopLinkMonitor();
366
367 // Respond to a LinkMonitor failure in a Device-specific manner.
368 virtual void OnLinkMonitorFailure();
369
Ben Chanb061f892013-02-27 17:46:55 -0800370 // Initiates traffic monitoring if it's enabled via
371 // set_traffic_monitor_enabled() and returns true if the monitoring
372 // is started.
373 bool StartTrafficMonitor();
374
375 // Stops traffic monitoring if it is running.
376 void StopTrafficMonitor();
377
Thieu Le03026662013-04-04 10:45:11 -0700378 // Responds to a TrafficMonitor no-network-routing failure in a
Ben Chanb061f892013-02-27 17:46:55 -0800379 // Device-specific manner.
Thieu Le03026662013-04-04 10:45:11 -0700380 virtual void OnNoNetworkRouting();
Ben Chanb061f892013-02-27 17:46:55 -0800381
Paul Stewart20088d82012-02-16 06:58:55 -0800382 // Set the state of the selected service, with checks to make sure
383 // the service is already in a connected state before doing so.
384 void SetServiceConnectedState(Service::ConnectState state);
385
Arman Ugurayed8e6102012-11-29 14:47:20 -0800386 // Specifies whether an ARP gateway should be used for the
387 // device technology.
388 virtual bool ShouldUseArpGateway() const;
389
Darin Petkov9893d9c2012-05-17 15:27:31 -0700390 const ServiceRefPtr &selected_service() const { return selected_service_; }
391
Jason Glasgowb5790052012-01-27 01:03:52 -0500392 void HelpRegisterDerivedString(
393 const std::string &name,
394 std::string(Device::*get)(Error *),
395 void(Device::*set)(const std::string&, Error *));
Eric Shienbrood9a245532012-03-07 14:20:39 -0500396
397 void HelpRegisterDerivedStrings(
398 const std::string &name,
399 Strings(Device::*get)(Error *error),
400 void(Device::*set)(const Strings &value, Error *error));
Chris Masoneb925cc82011-06-22 15:39:57 -0700401
Jason Glasgow08afdff2012-04-03 10:22:26 -0400402 void HelpRegisterConstDerivedRpcIdentifiers(
403 const std::string &name,
404 RpcIdentifiers(Device::*get)(Error *));
405
Paul Stewart6ff27f52012-07-11 06:51:41 -0700406 void HelpRegisterConstDerivedUint64(
407 const std::string &name,
408 uint64(Device::*get)(Error *));
409
Paul Stewartac4ac002011-08-26 12:04:26 -0700410 // Property getters reserved for subclasses
411 ControlInterface *control_interface() const { return control_interface_; }
Thieu Le3426c8f2012-01-11 17:35:11 -0800412 Metrics *metrics() const { return metrics_; }
Paul Stewartac4ac002011-08-26 12:04:26 -0700413 Manager *manager() const { return manager_; }
Paul Stewart036dba02012-08-07 12:34:41 -0700414 const LinkMonitor *link_monitor() const { return link_monitor_.get(); }
415 void set_link_monitor(LinkMonitor *link_monitor);
Ben Chanb061f892013-02-27 17:46:55 -0800416 const TrafficMonitor *traffic_monitor() const {
417 return traffic_monitor_.get();
418 }
419
420 // Ownership of |traffic_monitor| is taken.
421 void set_traffic_monitor(TrafficMonitor *traffic_monitor);
422 bool traffic_monitor_enabled() const { return traffic_monitor_enabled_; }
423 void set_traffic_monitor_enabled(bool traffic_monitor_enabled) {
424 traffic_monitor_enabled_ = traffic_monitor_enabled;
425 }
Paul Stewartd5843772011-05-11 15:40:42 -0700426
Paul Stewart41a071e2013-04-26 07:56:04 -0700427 // Ownership of |health_checker_| is taken.
428 void set_health_checker(ConnectionHealthChecker *health_checker);
429
Paul Stewart75897df2011-04-27 09:05:53 -0700430 private:
Darin Petkov3189a472012-10-05 09:55:33 +0200431 friend class CellularCapabilityTest;
432 friend class CellularTest;
Chris Masone413a3192011-05-09 17:10:05 -0700433 friend class DeviceAdaptorInterface;
Paul Stewart6ff27f52012-07-11 06:51:41 -0700434 friend class DeviceByteCountTest;
Paul Stewartc681fa02012-03-02 19:40:04 -0800435 friend class DevicePortalDetectionTest;
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700436 friend class DeviceTest;
Paul Stewartced3ad72013-04-03 13:39:25 -0700437 friend class EthernetTest;
Darin Petkov3189a472012-10-05 09:55:33 +0200438 friend class OpenVPNDriverTest;
Paul Stewarte369ece2012-05-22 09:11:03 -0700439 friend class WiFiObjectTest;
Darin Petkovafa6fc42011-06-21 16:21:08 -0700440
Paul Stewart2bf1d352011-12-06 15:02:55 -0800441 static const char kIPFlagTemplate[];
442 static const char kIPFlagVersion4[];
443 static const char kIPFlagVersion6[];
444 static const char kIPFlagDisableIPv6[];
445 static const char kIPFlagUseTempAddr[];
446 static const char kIPFlagUseTempAddrUsedAndDefault[];
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800447 static const char kIPFlagReversePathFilter[];
448 static const char kIPFlagReversePathFilterEnabled[];
449 static const char kIPFlagReversePathFilterLooseMode[];
Prathmesh Prabhud8468b42013-04-17 15:13:14 -0700450 static const char kStoragePowered[];
Paul Stewart41a071e2013-04-26 07:56:04 -0700451 static const char kStorageIPConfigs[];
Paul Stewart6ff27f52012-07-11 06:51:41 -0700452 static const char kStorageReceiveByteCount[];
453 static const char kStorageTransmitByteCount[];
Chris Masone5dec5f42011-07-22 14:07:55 -0700454
Paul Stewart1062d9d2012-04-27 10:42:27 -0700455 // Configure static IP address parameters if the service provides them.
456 void ConfigureStaticIPTask();
457
Eric Shienbrood9a245532012-03-07 14:20:39 -0500458 void SetEnabledInternal(bool enable, bool persist,
459 Error *error, const ResultCallback &callback);
460
Chris Masone5dec5f42011-07-22 14:07:55 -0700461 // Right now, Devices reference IPConfigs directly when persisted to disk
462 // It's not clear that this makes sense long-term, but that's how it is now.
463 // This call generates a string in the right format for this persisting.
Chris Masone34af2182011-08-22 11:59:36 -0700464 // |suffix| is injected into the storage identifier used for the configs.
465 std::string SerializeIPConfigs(const std::string &suffix);
Chris Masone5dec5f42011-07-22 14:07:55 -0700466
Paul Stewart2bf1d352011-12-06 15:02:55 -0800467 // Set an IP configuration flag on the device. |ip_version| should be
468 // "ipv6" or "ipv4". |flag| should be the name of the flag to be set
469 // and |value| is what this flag should be set to.
470 bool SetIPFlag(IPAddress::Family family, const std::string &flag,
471 const std::string &value);
472
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800473 std::vector<std::string> AvailableIPConfigs(Error *error);
Chris Masone4e851612011-07-01 10:46:53 -0700474 std::string GetRpcConnectionIdentifier();
475
Paul Stewart036dba02012-08-07 12:34:41 -0700476 // Get the LinkMonitor's average response time.
477 uint64 GetLinkMonitorResponseTime(Error *error);
478
Ben Chanb061f892013-02-27 17:46:55 -0800479 // Get receive and transmit byte counters. These methods simply wrap
480 // GetReceiveByteCount and GetTransmitByteCount in order to be used by
481 // HelpRegisterConstDerivedUint64.
482 uint64 GetReceiveByteCountProperty(Error *error);
483 uint64 GetTransmitByteCountProperty(Error *error);
Paul Stewart6ff27f52012-07-11 06:51:41 -0700484
Eric Shienbrood9a245532012-03-07 14:20:39 -0500485 // |enabled_persistent_| is the value of the Powered property, as
486 // read from the profile. If it is not found in the profile, it
487 // defaults to true. |enabled_| reflects the real-time state of
488 // the device, i.e., enabled or disabled. |enabled_pending_| reflects
489 // the target state of the device while an enable or disable operation
490 // is occurring.
491 //
492 // Some typical sequences for these state variables are shown below.
493 //
494 // Shill starts up, profile has been read:
495 // |enabled_persistent_|=true |enabled_|=false |enabled_pending_|=false
496 //
497 // Shill acts on the value of |enabled_persistent_|, calls SetEnabled(true):
498 // |enabled_persistent_|=true |enabled_|=false |enabled_pending_|=true
499 //
500 // SetEnabled completes successfully, device is enabled:
501 // |enabled_persistent_|=true |enabled_|=true |enabled_pending_|=true
502 //
503 // User presses "Disable" button, SetEnabled(false) is called:
504 // |enabled_persistent_|=false |enabled_|=true |enabled_pending_|=false
505 //
506 // SetEnabled completes successfully, device is disabled:
507 // |enabled_persistent_|=false |enabled_|=false |enabled_pending_|=false
508 bool enabled_;
509 bool enabled_persistent_;
510 bool enabled_pending_;
511
512 // Other properties
Paul Stewartac4ac002011-08-26 12:04:26 -0700513 bool reconnect_;
514 const std::string hardware_address_;
515
516 PropertyStore store_;
517
Paul Stewartac4ac002011-08-26 12:04:26 -0700518 const int interface_index_;
Paul Stewarta41e38d2011-11-11 07:47:29 -0800519 bool running_; // indicates whether the device is actually in operation
Paul Stewartac4ac002011-08-26 12:04:26 -0700520 const std::string link_name_;
521 const std::string unique_id_;
522 ControlInterface *control_interface_;
523 EventDispatcher *dispatcher_;
Thieu Le3426c8f2012-01-11 17:35:11 -0800524 Metrics *metrics_;
Paul Stewartac4ac002011-08-26 12:04:26 -0700525 Manager *manager_;
526 IPConfigRefPtr ipconfig_;
527 ConnectionRefPtr connection_;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500528 base::WeakPtrFactory<Device> weak_ptr_factory_;
Darin Petkovafa6fc42011-06-21 16:21:08 -0700529 scoped_ptr<DeviceAdaptorInterface> adaptor_;
Paul Stewart20088d82012-02-16 06:58:55 -0800530 scoped_ptr<PortalDetector> portal_detector_;
Paul Stewart036dba02012-08-07 12:34:41 -0700531 scoped_ptr<LinkMonitor> link_monitor_;
Ben Chanb061f892013-02-27 17:46:55 -0800532 scoped_ptr<TrafficMonitor> traffic_monitor_;
Arman Ugurayf84a4242013-04-09 20:01:07 -0700533 scoped_ptr<ConnectionHealthChecker> health_checker_;
Ben Chanb061f892013-02-27 17:46:55 -0800534 bool traffic_monitor_enabled_;
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500535 base::Callback<void(const PortalDetector::Result &)>
536 portal_detector_callback_;
Gaurav Shah435de2c2011-11-17 19:01:07 -0800537 Technology::Identifier technology_;
Thieu Le85e050b2012-03-13 15:04:38 -0700538 // The number of portal detection attempts from Connected to Online state.
539 // This includes all failure/timeout attempts and the final successful
540 // attempt.
541 int portal_attempts_to_online_;
Darin Petkovafa6fc42011-06-21 16:21:08 -0700542
Paul Stewart6ff27f52012-07-11 06:51:41 -0700543 // Keep track of the offset between the interface-reported byte counters
544 // and our persisted value.
545 uint64 receive_byte_offset_;
546 uint64 transmit_byte_offset_;
547
Paul Stewart03dba0b2011-08-22 16:32:45 -0700548 // Maintain a reference to the connected / connecting service
549 ServiceRefPtr selected_service_;
550
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700551 // Cache singleton pointers for performance and test purposes.
Darin Petkov77cb6812011-08-15 16:19:41 -0700552 DHCPProvider *dhcp_provider_;
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700553 RTNLHandler *rtnl_handler_;
Darin Petkov77cb6812011-08-15 16:19:41 -0700554
Paul Stewart41a071e2013-04-26 07:56:04 -0700555 // TODO(armansito): List of static IPs for connection health check.
556 // These should be removed once shill has a DNS result caching mechanism.
557 std::vector<std::string> health_check_ip_pool_;
558
Chris Masone9be4a9d2011-05-16 15:44:09 -0700559 DISALLOW_COPY_AND_ASSIGN(Device);
Paul Stewart75897df2011-04-27 09:05:53 -0700560};
561
Paul Stewart75897df2011-04-27 09:05:53 -0700562} // namespace shill
563
Darin Petkov2b8e44e2012-06-25 15:13:26 +0200564#endif // SHILL_DEVICE_H_