blob: bfba743a2bf06d93bb977380eb74a14e01642ca4 [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 Guthrie4823f4f2013-07-25 10:03:03 -070087 virtual void Scan(ScanType scan_type, Error *error,
88 const std::string &reason);
Eric Shienbrood9a245532012-03-07 14:20:39 -050089 virtual void RegisterOnNetwork(const std::string &network_id, Error *error,
90 const ResultCallback &callback);
91 virtual void RequirePIN(const std::string &pin, bool require,
92 Error *error, const ResultCallback &callback);
93 virtual void EnterPIN(const std::string &pin,
94 Error *error, const ResultCallback &callback);
Darin Petkove42e1012011-08-31 12:35:04 -070095 virtual void UnblockPIN(const std::string &unblock_code,
96 const std::string &pin,
Eric Shienbrood9a245532012-03-07 14:20:39 -050097 Error *error, const ResultCallback &callback);
Darin Petkove42e1012011-08-31 12:35:04 -070098 virtual void ChangePIN(const std::string &old_pin,
99 const std::string &new_pin,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500100 Error *error, const ResultCallback &callback);
Ben Chanad663e12013-01-08 01:58:47 -0800101 virtual void Reset(Error *error, const ResultCallback &callback);
102
Darin Petkovc37a9c42012-09-06 15:28:22 +0200103 virtual void SetCarrier(const std::string &carrier,
104 Error *error, const ResultCallback &callback);
Paul Stewart2bf1d352011-12-06 15:02:55 -0800105 virtual void DisableIPv6();
106 virtual void EnableIPv6();
107 virtual void EnableIPv6Privacy();
Darin Petkov9ae310f2011-08-30 15:41:13 -0700108
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800109 // Request the removal of reverse-path filtering for this interface.
110 // This will allow packets destined for this interface to be accepted,
111 // even if this is not the default route for such a packet to arrive.
112 virtual void DisableReversePathFilter();
113
114 // Request reverse-path filtering for this interface.
115 virtual void EnableReversePathFilter();
116
Gaurav Shah435de2c2011-11-17 19:01:07 -0800117 // Returns true if the selected service on the device (if any) is connected.
118 // Returns false if there is no selected service, or if the selected service
119 // is not connected.
120 bool IsConnected() const;
121
Christopher Wiley5519e9e2013-01-08 16:55:56 -0800122 // Called by Device so that subclasses can run hooks on the selected service
123 // getting an IP. Subclasses should call up to the parent first.
124 virtual void OnConnected();
125
Paul Stewart8596f9f2013-03-14 07:58:26 -0700126 // Called by the Connection so that the Device can update the service sorting
127 // after one connection is bound to another.
128 virtual void OnConnectionUpdated();
129
Paul Stewartd215af62012-04-24 23:25:50 -0700130 // Returns true if the selected service on the device (if any) is connected
131 // and matches the passed-in argument |service|. Returns false if there is
132 // no connected service, or if it does not match |service|.
133 virtual bool IsConnectedToService(const ServiceRefPtr &service) const;
134
135 // Restart the portal detection process on a connected device. This is
136 // useful if the properties on the connected service have changed in a
137 // way that may affect the decision to run portal detection at all.
138 // Returns true if portal detection was started.
139 virtual bool RestartPortalDetection();
140
Ben Chanb061f892013-02-27 17:46:55 -0800141 // Get receive and transmit byte counters.
142 virtual uint64 GetReceiveByteCount();
143 virtual uint64 GetTransmitByteCount();
144
Paul Stewart6ff27f52012-07-11 06:51:41 -0700145 // Reset the persisted byte counters associated with the device.
146 void ResetByteCounters();
147
Paul Stewartc681fa02012-03-02 19:40:04 -0800148 // Requests that portal detection be done, if this device has the default
149 // connection. Returns true if portal detection was started.
150 virtual bool RequestPortalDetection();
151
mukesh agrawalf6b32092013-04-10 15:49:55 -0700152 std::string GetRpcIdentifier() const;
Chris Masone5dec5f42011-07-22 14:07:55 -0700153 std::string GetStorageIdentifier();
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700154
Gaurav Shah6d2c72d2012-10-16 16:30:44 -0700155 // Returns a list of Geolocation objects. Each object is multiple
156 // key-value pairs representing one entity that can be used for
157 // Geolocation.
158 virtual std::vector<GeolocationInfo> GetGeolocationObjects() const;
159
Chris Masone9d779932011-08-25 16:33:41 -0700160 const std::string &address() const { return hardware_address_; }
Darin Petkove0a312e2011-07-20 13:45:28 -0700161 const std::string &link_name() const { return link_name_; }
162 int interface_index() const { return interface_index_; }
Paul Stewarte6132022011-08-16 09:11:02 -0700163 const ConnectionRefPtr &connection() const { return connection_; }
Eric Shienbrood9a245532012-03-07 14:20:39 -0500164 bool enabled() const { return enabled_; }
165 bool enabled_persistent() const { return enabled_persistent_; }
Gaurav Shah435de2c2011-11-17 19:01:07 -0800166 virtual Technology::Identifier technology() const { return technology_; }
Jason Glasgowb5790052012-01-27 01:03:52 -0500167 std::string GetTechnologyString(Error *error);
Darin Petkove0a312e2011-07-20 13:45:28 -0700168
Thieu Le03026662013-04-04 10:45:11 -0700169 virtual const IPConfigRefPtr &ipconfig() const { return ipconfig_; }
Darin Petkov31a2eca2012-03-14 11:07:18 +0100170 void set_ipconfig(const IPConfigRefPtr &config) { ipconfig_ = config; }
171
Chris Masone19e30402011-07-19 15:48:47 -0700172 const std::string &FriendlyName() const;
Chris Masone27c4aa52011-07-02 13:10:14 -0700173
Darin Petkovafa6fc42011-06-21 16:21:08 -0700174 // Returns a string that is guaranteed to uniquely identify this Device
175 // instance.
176 const std::string &UniqueName() const;
Chris Masonea82b7112011-05-25 15:16:29 -0700177
mukesh agrawalde29fa82011-09-16 16:16:36 -0700178 PropertyStore *mutable_store() { return &store_; }
179 const PropertyStore &store() const { return store_; }
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700180 RTNLHandler *rtnl_handler() { return rtnl_handler_; }
Ben Chan19f83972012-10-03 23:25:56 -0700181 bool running() const { return running_; }
Chris Masone19e30402011-07-19 15:48:47 -0700182
Darin Petkovb05315f2011-11-07 10:14:25 +0100183 EventDispatcher *dispatcher() const { return dispatcher_; }
184
Paul Stewarta41e38d2011-11-11 07:47:29 -0800185 // Load configuration for the device from |storage|. This may include
186 // instantiating non-visible services for which configuration has been
187 // stored.
188 virtual bool Load(StoreInterface *storage);
189
190 // Save configuration for the device to |storage|.
Chris Masone877ff982011-09-21 16:18:24 -0700191 virtual bool Save(StoreInterface *storage);
Chris Masone5dec5f42011-07-22 14:07:55 -0700192
Darin Petkov77cb6812011-08-15 16:19:41 -0700193 void set_dhcp_provider(DHCPProvider *provider) { dhcp_provider_ = provider; }
194
Darin Petkov3cfbf212011-11-21 16:02:09 +0100195 DeviceAdaptorInterface *adaptor() const { return adaptor_.get(); }
196
Prathmesh Prabhuba99b592013-04-17 15:13:14 -0700197 void set_health_checker(ConnectionHealthChecker *health_checker) {
198 health_checker_.reset(health_checker);
199 }
200
mukesh agrawal784566d2012-08-08 18:32:58 -0700201 // Suspend event handler. Called by Manager before the system
202 // suspends. For this callback to be useful, the device must specify
203 // a suspend delay. Otherwise, there is no guarantee that the device
204 // will have time to complete its suspend actions, before the system
205 // is suspended.
206 //
Paul Stewartee6b3d72013-07-12 16:07:51 -0700207 // TODO(quiche): Add support for suspend delays. crbug.com/215582
mukesh agrawal784566d2012-08-08 18:32:58 -0700208 virtual void OnBeforeSuspend();
209
210 // Resume event handler. Called by Manager as the system resumes.
211 // The base class implementation takes care of renewing a DHCP lease
212 // (if necessary). Derived classes may implement any technology
213 // specific requirements by overriding, but should include a call to
214 // the base class implementation.
215 virtual void OnAfterResume();
216
Albert Chaulk0e1cdea2013-02-27 15:32:55 -0800217 // Destroy the lease, if any, with this |name|.
218 // Called by the service during Unload() as part of the cleanup sequence.
219 virtual void DestroyIPConfigLease(const std::string &name);
220
Paul Stewartd5843772011-05-11 15:40:42 -0700221 protected:
Darin Petkov2b8e44e2012-06-25 15:13:26 +0200222 friend class base::RefCounted<Device>;
Arman Ugurayf84a4242013-04-09 20:01:07 -0700223 friend class DeviceHealthCheckerTest;
Ben Chan19f83972012-10-03 23:25:56 -0700224 FRIEND_TEST(CellularServiceTest, IsAutoConnectable);
Arman Ugurayd42d8ec2013-04-08 19:28:21 -0700225 FRIEND_TEST(CellularTest, EnableTrafficMonitor);
Eric Shienbrood7fce52c2012-04-13 19:11:02 -0400226 FRIEND_TEST(CellularTest, ModemStateChangeDisable);
Arman Ugurayed8e6102012-11-29 14:47:20 -0800227 FRIEND_TEST(CellularTest, UseNoArpGateway);
Darin Petkov13e6d552012-05-09 14:22:23 +0200228 FRIEND_TEST(ConnectionTest, OnRouteQueryResponse);
Prathmesh Prabhuba99b592013-04-17 15:13:14 -0700229 FRIEND_TEST(DeviceHealthCheckerTest, HealthCheckerPersistsAcrossDeviceReset);
Arman Ugurayf84a4242013-04-09 20:01:07 -0700230 FRIEND_TEST(DeviceHealthCheckerTest, RequestConnectionHealthCheck);
Prathmesh Prabhuba99b592013-04-17 15:13:14 -0700231 FRIEND_TEST(DeviceHealthCheckerTest, SetupHealthChecker);
Paul Stewart2bf1d352011-12-06 15:02:55 -0800232 FRIEND_TEST(DeviceTest, AcquireIPConfig);
Darin Petkovafa6fc42011-06-21 16:21:08 -0700233 FRIEND_TEST(DeviceTest, DestroyIPConfig);
234 FRIEND_TEST(DeviceTest, DestroyIPConfigNULL);
Chris Masone95207da2011-06-29 16:50:49 -0700235 FRIEND_TEST(DeviceTest, GetProperties);
Paul Stewart6ff27f52012-07-11 06:51:41 -0700236 FRIEND_TEST(DeviceTest, Load);
Chris Masone5dec5f42011-07-22 14:07:55 -0700237 FRIEND_TEST(DeviceTest, Save);
Paul Stewart03dba0b2011-08-22 16:32:45 -0700238 FRIEND_TEST(DeviceTest, SelectedService);
Darin Petkove7c6ad32012-06-29 10:22:09 +0200239 FRIEND_TEST(DeviceTest, SetEnabledPersistent);
Paul Stewartc681fa02012-03-02 19:40:04 -0800240 FRIEND_TEST(DeviceTest, SetServiceConnectedState);
Paul Stewart8c116a92012-05-02 18:30:03 -0700241 FRIEND_TEST(DeviceTest, Start);
Ben Chanb061f892013-02-27 17:46:55 -0800242 FRIEND_TEST(DeviceTest, StartTrafficMonitor);
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700243 FRIEND_TEST(DeviceTest, Stop);
Ben Chanb061f892013-02-27 17:46:55 -0800244 FRIEND_TEST(DeviceTest, StopTrafficMonitor);
Paul Stewarta41e38d2011-11-11 07:47:29 -0800245 FRIEND_TEST(ManagerTest, DeviceRegistrationAndStart);
Gaurav Shah435de2c2011-11-17 19:01:07 -0800246 FRIEND_TEST(ManagerTest, ConnectedTechnologies);
247 FRIEND_TEST(ManagerTest, DefaultTechnology);
mukesh agrawal46c27cc2013-07-10 16:39:10 -0700248 FRIEND_TEST(ManagerTest, SetEnabledStateForTechnology);
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.
mukesh agrawal5d851b12013-07-11 14:09:41 -0700297 virtual void DropConnection();
Darin Petkov2b8e44e2012-06-25 15:13:26 +0200298
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 Stewartf6f96482013-07-12 12:49:15 -0700320 // Called by Device so that subclasses can run hooks on the selected service
321 // failing to get an IP. The default implementation disconnects the selected
322 // service with Service::kFailureDHCP.
323 virtual void OnIPConfigFailure();
324
Paul Stewarte6132022011-08-16 09:11:02 -0700325 // Maintain connection state (Routes, IP Addresses and DNS) in the OS.
326 void CreateConnection();
327
328 // Remove connection state
329 void DestroyConnection();
330
Paul Stewart03dba0b2011-08-22 16:32:45 -0700331 // Selects a service to be "current" -- i.e. link-state or configuration
332 // events that happen to the device are attributed to this service.
333 void SelectService(const ServiceRefPtr &service);
334
335 // Set the state of the selected service
mukesh agrawal0381f9a2013-07-11 16:41:52 -0700336 virtual void SetServiceState(Service::ConnectState state);
Paul Stewart03dba0b2011-08-22 16:32:45 -0700337
338 // Set the failure of the selected service (implicitly sets the state to
339 // "failure")
mukesh agrawal0381f9a2013-07-11 16:41:52 -0700340 virtual void SetServiceFailure(Service::ConnectFailure failure_state);
Paul Stewart03dba0b2011-08-22 16:32:45 -0700341
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400342 // Records the failure mode and time of the selected service, and
343 // sets the Service state of the selected service to "Idle".
344 // Avoids showing a failure mole in the UI.
mukesh agrawal0381f9a2013-07-11 16:41:52 -0700345 virtual void SetServiceFailureSilent(Service::ConnectFailure failure_state);
Eric Shienbroodcc95c5d2012-03-30 15:25:49 -0400346
Prathmesh Prabhuba99b592013-04-17 15:13:14 -0700347 virtual void SetupConnectionHealthChecker();
Arman Ugurayf84a4242013-04-09 20:01:07 -0700348
349 // Checks the network connectivity status by creating a TCP connection, and
350 // optionally sending a small amout of data.
351 void RequestConnectionHealthCheck();
352
353 // Responds to the result from connection health checker in a device specific
354 // manner.
355 virtual void OnConnectionHealthCheckerResult(
356 ConnectionHealthChecker::Result result);
357
Paul Stewart20088d82012-02-16 06:58:55 -0800358 // Called by the Portal Detector whenever a trial completes. Device
359 // subclasses that choose unique mappings from portal results to connected
360 // states can override this method in order to do so.
361 virtual void PortalDetectorCallback(const PortalDetector::Result &result);
362
363 // Initiate portal detection, if enabled for this device type.
364 bool StartPortalDetection();
365
366 // Stop portal detection if it is running.
367 void StopPortalDetection();
368
Paul Stewart036dba02012-08-07 12:34:41 -0700369 // Initiate link monitoring, if enabled for this device type.
370 bool StartLinkMonitor();
371
372 // Stop link monitoring if it is running.
373 void StopLinkMonitor();
374
375 // Respond to a LinkMonitor failure in a Device-specific manner.
376 virtual void OnLinkMonitorFailure();
377
Ben Chanb061f892013-02-27 17:46:55 -0800378 // Initiates traffic monitoring if it's enabled via
379 // set_traffic_monitor_enabled() and returns true if the monitoring
380 // is started.
381 bool StartTrafficMonitor();
382
383 // Stops traffic monitoring if it is running.
384 void StopTrafficMonitor();
385
Thieu Le03026662013-04-04 10:45:11 -0700386 // Responds to a TrafficMonitor no-network-routing failure in a
Ben Chanb061f892013-02-27 17:46:55 -0800387 // Device-specific manner.
Thieu Le03026662013-04-04 10:45:11 -0700388 virtual void OnNoNetworkRouting();
Ben Chanb061f892013-02-27 17:46:55 -0800389
Paul Stewart20088d82012-02-16 06:58:55 -0800390 // Set the state of the selected service, with checks to make sure
391 // the service is already in a connected state before doing so.
392 void SetServiceConnectedState(Service::ConnectState state);
393
Arman Ugurayed8e6102012-11-29 14:47:20 -0800394 // Specifies whether an ARP gateway should be used for the
395 // device technology.
396 virtual bool ShouldUseArpGateway() const;
397
Darin Petkov9893d9c2012-05-17 15:27:31 -0700398 const ServiceRefPtr &selected_service() const { return selected_service_; }
399
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700400 void HelpRegisterConstDerivedString(
Jason Glasgowb5790052012-01-27 01:03:52 -0500401 const std::string &name,
mukesh agrawalbebf1b82013-04-23 15:06:33 -0700402 std::string(Device::*get)(Error *));
Chris Masoneb925cc82011-06-22 15:39:57 -0700403
Jason Glasgow08afdff2012-04-03 10:22:26 -0400404 void HelpRegisterConstDerivedRpcIdentifiers(
405 const std::string &name,
406 RpcIdentifiers(Device::*get)(Error *));
407
Paul Stewart6ff27f52012-07-11 06:51:41 -0700408 void HelpRegisterConstDerivedUint64(
409 const std::string &name,
410 uint64(Device::*get)(Error *));
411
Paul Stewartac4ac002011-08-26 12:04:26 -0700412 // Property getters reserved for subclasses
413 ControlInterface *control_interface() const { return control_interface_; }
Thieu Le3426c8f2012-01-11 17:35:11 -0800414 Metrics *metrics() const { return metrics_; }
Paul Stewartac4ac002011-08-26 12:04:26 -0700415 Manager *manager() const { return manager_; }
Paul Stewart036dba02012-08-07 12:34:41 -0700416 const LinkMonitor *link_monitor() const { return link_monitor_.get(); }
417 void set_link_monitor(LinkMonitor *link_monitor);
Ben Chanb061f892013-02-27 17:46:55 -0800418 const TrafficMonitor *traffic_monitor() const {
419 return traffic_monitor_.get();
420 }
421
422 // Ownership of |traffic_monitor| is taken.
423 void set_traffic_monitor(TrafficMonitor *traffic_monitor);
424 bool traffic_monitor_enabled() const { return traffic_monitor_enabled_; }
425 void set_traffic_monitor_enabled(bool traffic_monitor_enabled) {
426 traffic_monitor_enabled_ = traffic_monitor_enabled;
427 }
Paul Stewartd5843772011-05-11 15:40:42 -0700428
Paul Stewart75897df2011-04-27 09:05:53 -0700429 private:
Darin Petkov3189a472012-10-05 09:55:33 +0200430 friend class CellularCapabilityTest;
431 friend class CellularTest;
Chris Masone413a3192011-05-09 17:10:05 -0700432 friend class DeviceAdaptorInterface;
Paul Stewart6ff27f52012-07-11 06:51:41 -0700433 friend class DeviceByteCountTest;
Paul Stewartc681fa02012-03-02 19:40:04 -0800434 friend class DevicePortalDetectionTest;
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700435 friend class DeviceTest;
Paul Stewartced3ad72013-04-03 13:39:25 -0700436 friend class EthernetTest;
Darin Petkov3189a472012-10-05 09:55:33 +0200437 friend class OpenVPNDriverTest;
Paul Stewarte369ece2012-05-22 09:11:03 -0700438 friend class WiFiObjectTest;
Darin Petkovafa6fc42011-06-21 16:21:08 -0700439
Paul Stewart2bf1d352011-12-06 15:02:55 -0800440 static const char kIPFlagTemplate[];
441 static const char kIPFlagVersion4[];
442 static const char kIPFlagVersion6[];
443 static const char kIPFlagDisableIPv6[];
444 static const char kIPFlagUseTempAddr[];
445 static const char kIPFlagUseTempAddrUsedAndDefault[];
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800446 static const char kIPFlagReversePathFilter[];
447 static const char kIPFlagReversePathFilterEnabled[];
448 static const char kIPFlagReversePathFilterLooseMode[];
Paul Stewart41a071e2013-04-26 07:56:04 -0700449 static const char kStorageIPConfigs[];
Prathmesh Prabhuba99b592013-04-17 15:13:14 -0700450 static const char kStoragePowered[];
Paul Stewart6ff27f52012-07-11 06:51:41 -0700451 static const char kStorageReceiveByteCount[];
452 static const char kStorageTransmitByteCount[];
Chris Masone5dec5f42011-07-22 14:07:55 -0700453
Paul Stewart1062d9d2012-04-27 10:42:27 -0700454 // Configure static IP address parameters if the service provides them.
455 void ConfigureStaticIPTask();
456
Eric Shienbrood9a245532012-03-07 14:20:39 -0500457 void SetEnabledInternal(bool enable, bool persist,
458 Error *error, const ResultCallback &callback);
459
Chris Masone5dec5f42011-07-22 14:07:55 -0700460 // Right now, Devices reference IPConfigs directly when persisted to disk
461 // It's not clear that this makes sense long-term, but that's how it is now.
462 // This call generates a string in the right format for this persisting.
Chris Masone34af2182011-08-22 11:59:36 -0700463 // |suffix| is injected into the storage identifier used for the configs.
464 std::string SerializeIPConfigs(const std::string &suffix);
Chris Masone5dec5f42011-07-22 14:07:55 -0700465
Paul Stewart2bf1d352011-12-06 15:02:55 -0800466 // Set an IP configuration flag on the device. |ip_version| should be
467 // "ipv6" or "ipv4". |flag| should be the name of the flag to be set
468 // and |value| is what this flag should be set to.
469 bool SetIPFlag(IPAddress::Family family, const std::string &flag,
470 const std::string &value);
471
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800472 std::vector<std::string> AvailableIPConfigs(Error *error);
Chris Masone4e851612011-07-01 10:46:53 -0700473 std::string GetRpcConnectionIdentifier();
474
Paul Stewart036dba02012-08-07 12:34:41 -0700475 // Get the LinkMonitor's average response time.
476 uint64 GetLinkMonitorResponseTime(Error *error);
477
Ben Chanb061f892013-02-27 17:46:55 -0800478 // Get receive and transmit byte counters. These methods simply wrap
479 // GetReceiveByteCount and GetTransmitByteCount in order to be used by
480 // HelpRegisterConstDerivedUint64.
481 uint64 GetReceiveByteCountProperty(Error *error);
482 uint64 GetTransmitByteCountProperty(Error *error);
Paul Stewart6ff27f52012-07-11 06:51:41 -0700483
Eric Shienbrood9a245532012-03-07 14:20:39 -0500484 // |enabled_persistent_| is the value of the Powered property, as
485 // read from the profile. If it is not found in the profile, it
486 // defaults to true. |enabled_| reflects the real-time state of
487 // the device, i.e., enabled or disabled. |enabled_pending_| reflects
488 // the target state of the device while an enable or disable operation
489 // is occurring.
490 //
491 // Some typical sequences for these state variables are shown below.
492 //
493 // Shill starts up, profile has been read:
494 // |enabled_persistent_|=true |enabled_|=false |enabled_pending_|=false
495 //
496 // Shill acts on the value of |enabled_persistent_|, calls SetEnabled(true):
497 // |enabled_persistent_|=true |enabled_|=false |enabled_pending_|=true
498 //
499 // SetEnabled completes successfully, device is enabled:
500 // |enabled_persistent_|=true |enabled_|=true |enabled_pending_|=true
501 //
502 // User presses "Disable" button, SetEnabled(false) is called:
503 // |enabled_persistent_|=false |enabled_|=true |enabled_pending_|=false
504 //
505 // SetEnabled completes successfully, device is disabled:
506 // |enabled_persistent_|=false |enabled_|=false |enabled_pending_|=false
507 bool enabled_;
508 bool enabled_persistent_;
509 bool enabled_pending_;
510
511 // Other properties
Paul Stewartac4ac002011-08-26 12:04:26 -0700512 bool reconnect_;
513 const std::string hardware_address_;
514
515 PropertyStore store_;
516
Paul Stewartac4ac002011-08-26 12:04:26 -0700517 const int interface_index_;
Paul Stewarta41e38d2011-11-11 07:47:29 -0800518 bool running_; // indicates whether the device is actually in operation
Paul Stewartac4ac002011-08-26 12:04:26 -0700519 const std::string link_name_;
520 const std::string unique_id_;
521 ControlInterface *control_interface_;
522 EventDispatcher *dispatcher_;
Thieu Le3426c8f2012-01-11 17:35:11 -0800523 Metrics *metrics_;
Paul Stewartac4ac002011-08-26 12:04:26 -0700524 Manager *manager_;
525 IPConfigRefPtr ipconfig_;
526 ConnectionRefPtr connection_;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500527 base::WeakPtrFactory<Device> weak_ptr_factory_;
Darin Petkovafa6fc42011-06-21 16:21:08 -0700528 scoped_ptr<DeviceAdaptorInterface> adaptor_;
Paul Stewart20088d82012-02-16 06:58:55 -0800529 scoped_ptr<PortalDetector> portal_detector_;
Paul Stewart036dba02012-08-07 12:34:41 -0700530 scoped_ptr<LinkMonitor> link_monitor_;
Ben Chanb061f892013-02-27 17:46:55 -0800531 scoped_ptr<TrafficMonitor> traffic_monitor_;
Arman Ugurayf84a4242013-04-09 20:01:07 -0700532 scoped_ptr<ConnectionHealthChecker> health_checker_;
Ben Chanb061f892013-02-27 17:46:55 -0800533 bool traffic_monitor_enabled_;
Eric Shienbrood3e20a232012-02-16 11:35:56 -0500534 base::Callback<void(const PortalDetector::Result &)>
535 portal_detector_callback_;
Gaurav Shah435de2c2011-11-17 19:01:07 -0800536 Technology::Identifier technology_;
Thieu Le85e050b2012-03-13 15:04:38 -0700537 // The number of portal detection attempts from Connected to Online state.
538 // This includes all failure/timeout attempts and the final successful
539 // attempt.
540 int portal_attempts_to_online_;
Darin Petkovafa6fc42011-06-21 16:21:08 -0700541
Paul Stewart6ff27f52012-07-11 06:51:41 -0700542 // Keep track of the offset between the interface-reported byte counters
543 // and our persisted value.
544 uint64 receive_byte_offset_;
545 uint64 transmit_byte_offset_;
546
Paul Stewart03dba0b2011-08-22 16:32:45 -0700547 // Maintain a reference to the connected / connecting service
548 ServiceRefPtr selected_service_;
549
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700550 // Cache singleton pointers for performance and test purposes.
Darin Petkov77cb6812011-08-15 16:19:41 -0700551 DHCPProvider *dhcp_provider_;
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700552 RTNLHandler *rtnl_handler_;
Darin Petkov77cb6812011-08-15 16:19:41 -0700553
Chris Masone9be4a9d2011-05-16 15:44:09 -0700554 DISALLOW_COPY_AND_ASSIGN(Device);
Paul Stewart75897df2011-04-27 09:05:53 -0700555};
556
Paul Stewart75897df2011-04-27 09:05:53 -0700557} // namespace shill
558
Darin Petkov2b8e44e2012-06-25 15:13:26 +0200559#endif // SHILL_DEVICE_H_