blob: afa900d5334d5d5a1581ba31c7cbcf2b41cc5975 [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
5#ifndef SHILL_DEVICE_
6#define SHILL_DEVICE_
7
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>
Darin Petkovafa6fc42011-06-21 16:21:08 -070014#include <gtest/gtest_prod.h> // for FRIEND_TEST
Paul Stewartd5843772011-05-11 15:40:42 -070015
Paul Stewart26b327e2011-10-19 11:38:09 -070016#include "shill/event_dispatcher.h"
Paul Stewart2bf1d352011-12-06 15:02:55 -080017#include "shill/ip_address.h"
Darin Petkovafa6fc42011-06-21 16:21:08 -070018#include "shill/ipconfig.h"
Paul Stewart20088d82012-02-16 06:58:55 -080019#include "shill/portal_detector.h"
Chris Masoneb925cc82011-06-22 15:39:57 -070020#include "shill/property_store.h"
Chris Masone2b105542011-06-22 10:58:09 -070021#include "shill/refptr_types.h"
Paul Stewart03dba0b2011-08-22 16:32:45 -070022#include "shill/service.h"
Paul Stewartfdd16072011-09-16 12:41:35 -070023#include "shill/technology.h"
Paul Stewart75897df2011-04-27 09:05:53 -070024
25namespace shill {
26
Chris Masone9be4a9d2011-05-16 15:44:09 -070027class ControlInterface;
Darin Petkov77cb6812011-08-15 16:19:41 -070028class DHCPProvider;
Chris Masone9be4a9d2011-05-16 15:44:09 -070029class DeviceAdaptorInterface;
Paul Stewartf1ce5d22011-05-19 13:10:20 -070030class Endpoint;
Chris Masone8fe2c7e2011-06-09 15:51:19 -070031class Error;
32class EventDispatcher;
Paul Stewartf1ce5d22011-05-19 13:10:20 -070033class Manager;
Thieu Le3426c8f2012-01-11 17:35:11 -080034class Metrics;
Darin Petkove5bc2cb2011-12-07 14:47:32 +010035class ReturnerInterface;
Thieu Lefb46caf2012-03-08 11:57:15 -080036class RoutingTable;
37class RTNLHandler;
Chris Masone9be4a9d2011-05-16 15:44:09 -070038
Paul Stewart75897df2011-04-27 09:05:53 -070039// Device superclass. Individual network interfaces types will inherit from
40// this class.
Chris Masone27c4aa52011-07-02 13:10:14 -070041class Device : public base::RefCounted<Device> {
Paul Stewart75897df2011-04-27 09:05:53 -070042 public:
Chris Masone9be4a9d2011-05-16 15:44:09 -070043 // A constructor for the Device object
44 Device(ControlInterface *control_interface,
Paul Stewartb50f0b92011-05-16 16:31:42 -070045 EventDispatcher *dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -080046 Metrics *metrics,
Paul Stewartf1ce5d22011-05-19 13:10:20 -070047 Manager *manager,
Darin Petkovafa6fc42011-06-21 16:21:08 -070048 const std::string &link_name,
Chris Masone626719f2011-08-18 16:58:48 -070049 const std::string &address,
Gaurav Shah435de2c2011-11-17 19:01:07 -080050 int interface_index,
51 Technology::Identifier technology);
Chris Masone9be4a9d2011-05-16 15:44:09 -070052 virtual ~Device();
53
54 virtual void Start();
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -070055
56 // Clear running state, especially any fields that hold a reference back
57 // to us. After a call to Stop(), the Device may be restarted (with a call
58 // to Start()), or destroyed (if its refcount falls to zero).
Chris Masone9be4a9d2011-05-16 15:44:09 -070059 virtual void Stop();
60
Gaurav Shah435de2c2011-11-17 19:01:07 -080061
62 // TODO(gauravsh): We do not really need this since technology() can be used
63 // to get a device's technology for direct comparison.
Darin Petkovafa6fc42011-06-21 16:21:08 -070064 // Base method always returns false.
Paul Stewartfdd16072011-09-16 12:41:35 -070065 virtual bool TechnologyIs(const Technology::Identifier type) const;
Darin Petkovafa6fc42011-06-21 16:21:08 -070066
Paul Stewartf1ce5d22011-05-19 13:10:20 -070067 virtual void LinkEvent(unsigned flags, unsigned change);
Paul Stewart75897df2011-04-27 09:05:53 -070068
Darin Petkov9ae310f2011-08-30 15:41:13 -070069 // The default implementation sets |error| to kNotSupported.
Darin Petkovc0865312011-09-16 15:31:20 -070070 virtual void Scan(Error *error);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050071 virtual void RegisterOnNetwork(const std::string &network_id,
72 ReturnerInterface *returner);
Darin Petkovc64fe5e2012-01-11 12:46:13 +010073 virtual void RequirePIN(
74 const std::string &pin, bool require, ReturnerInterface *returner);
Darin Petkove5bc2cb2011-12-07 14:47:32 +010075 virtual void EnterPIN(const std::string &pin, ReturnerInterface *returner);
Darin Petkove42e1012011-08-31 12:35:04 -070076 virtual void UnblockPIN(const std::string &unblock_code,
77 const std::string &pin,
Darin Petkovc64fe5e2012-01-11 12:46:13 +010078 ReturnerInterface *returner);
Darin Petkove42e1012011-08-31 12:35:04 -070079 virtual void ChangePIN(const std::string &old_pin,
80 const std::string &new_pin,
Darin Petkovc64fe5e2012-01-11 12:46:13 +010081 ReturnerInterface *returner);
Paul Stewart2bf1d352011-12-06 15:02:55 -080082 virtual void DisableIPv6();
83 virtual void EnableIPv6();
84 virtual void EnableIPv6Privacy();
Darin Petkov9ae310f2011-08-30 15:41:13 -070085
Paul Stewartc8f4bef2011-12-13 09:45:51 -080086 // Request the removal of reverse-path filtering for this interface.
87 // This will allow packets destined for this interface to be accepted,
88 // even if this is not the default route for such a packet to arrive.
89 virtual void DisableReversePathFilter();
90
91 // Request reverse-path filtering for this interface.
92 virtual void EnableReversePathFilter();
93
Gaurav Shah435de2c2011-11-17 19:01:07 -080094 // Returns true if the selected service on the device (if any) is connected.
95 // Returns false if there is no selected service, or if the selected service
96 // is not connected.
97 bool IsConnected() const;
98
Paul Stewartc681fa02012-03-02 19:40:04 -080099 // Requests that portal detection be done, if this device has the default
100 // connection. Returns true if portal detection was started.
101 virtual bool RequestPortalDetection();
102
Chris Masone27c4aa52011-07-02 13:10:14 -0700103 std::string GetRpcIdentifier();
Chris Masone5dec5f42011-07-22 14:07:55 -0700104 std::string GetStorageIdentifier();
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700105
Chris Masone9d779932011-08-25 16:33:41 -0700106 const std::string &address() const { return hardware_address_; }
Darin Petkove0a312e2011-07-20 13:45:28 -0700107 const std::string &link_name() const { return link_name_; }
108 int interface_index() const { return interface_index_; }
Paul Stewarte6132022011-08-16 09:11:02 -0700109 const ConnectionRefPtr &connection() const { return connection_; }
Darin Petkov79d74c92012-03-07 17:20:32 +0100110 const IPConfigRefPtr &ipconfig() const { return ipconfig_; }
Paul Stewarta41e38d2011-11-11 07:47:29 -0800111 bool powered() const { return powered_; }
Gaurav Shah435de2c2011-11-17 19:01:07 -0800112 virtual Technology::Identifier technology() const { return technology_; }
Jason Glasgowb5790052012-01-27 01:03:52 -0500113 std::string GetTechnologyString(Error *error);
Darin Petkove0a312e2011-07-20 13:45:28 -0700114
Chris Masone19e30402011-07-19 15:48:47 -0700115 const std::string &FriendlyName() const;
Chris Masone27c4aa52011-07-02 13:10:14 -0700116
Darin Petkovafa6fc42011-06-21 16:21:08 -0700117 // Returns a string that is guaranteed to uniquely identify this Device
118 // instance.
119 const std::string &UniqueName() const;
Chris Masonea82b7112011-05-25 15:16:29 -0700120
mukesh agrawalde29fa82011-09-16 16:16:36 -0700121 PropertyStore *mutable_store() { return &store_; }
122 const PropertyStore &store() const { return store_; }
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700123 RTNLHandler *rtnl_handler() { return rtnl_handler_; }
Chris Masone19e30402011-07-19 15:48:47 -0700124
Darin Petkovb05315f2011-11-07 10:14:25 +0100125 EventDispatcher *dispatcher() const { return dispatcher_; }
126
Paul Stewarta41e38d2011-11-11 07:47:29 -0800127 // Load configuration for the device from |storage|. This may include
128 // instantiating non-visible services for which configuration has been
129 // stored.
130 virtual bool Load(StoreInterface *storage);
131
132 // Save configuration for the device to |storage|.
Chris Masone877ff982011-09-21 16:18:24 -0700133 virtual bool Save(StoreInterface *storage);
Chris Masone5dec5f42011-07-22 14:07:55 -0700134
Darin Petkov77cb6812011-08-15 16:19:41 -0700135 void set_dhcp_provider(DHCPProvider *provider) { dhcp_provider_ = provider; }
136
Darin Petkov3cfbf212011-11-21 16:02:09 +0100137 DeviceAdaptorInterface *adaptor() const { return adaptor_.get(); }
138
Paul Stewartd5843772011-05-11 15:40:42 -0700139 protected:
Paul Stewart2bf1d352011-12-06 15:02:55 -0800140 FRIEND_TEST(DeviceTest, AcquireIPConfig);
Darin Petkovafa6fc42011-06-21 16:21:08 -0700141 FRIEND_TEST(DeviceTest, DestroyIPConfig);
142 FRIEND_TEST(DeviceTest, DestroyIPConfigNULL);
Chris Masone95207da2011-06-29 16:50:49 -0700143 FRIEND_TEST(DeviceTest, GetProperties);
Chris Masone5dec5f42011-07-22 14:07:55 -0700144 FRIEND_TEST(DeviceTest, Save);
Paul Stewart03dba0b2011-08-22 16:32:45 -0700145 FRIEND_TEST(DeviceTest, SelectedService);
Paul Stewartc681fa02012-03-02 19:40:04 -0800146 FRIEND_TEST(DeviceTest, SetServiceConnectedState);
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700147 FRIEND_TEST(DeviceTest, Stop);
Paul Stewarta41e38d2011-11-11 07:47:29 -0800148 FRIEND_TEST(ManagerTest, DeviceRegistrationAndStart);
Gaurav Shah435de2c2011-11-17 19:01:07 -0800149 FRIEND_TEST(ManagerTest, ConnectedTechnologies);
150 FRIEND_TEST(ManagerTest, DefaultTechnology);
mukesh agrawal32399322011-09-01 10:53:43 -0700151 FRIEND_TEST(WiFiMainTest, Connect);
Darin Petkovafa6fc42011-06-21 16:21:08 -0700152
153 // If there's an IP configuration in |ipconfig_|, releases the IP address and
154 // destroys the configuration instance.
155 void DestroyIPConfig();
156
157 // Creates a new DHCP IP configuration instance, stores it in |ipconfig_| and
158 // requests a new IP configuration. Registers a callback to
159 // IPConfigUpdatedCallback on IP configuration changes. Returns true if the IP
160 // request was successfully sent.
Paul Stewart2bf1d352011-12-06 15:02:55 -0800161 bool AcquireIPConfig();
Darin Petkovafa6fc42011-06-21 16:21:08 -0700162
Darin Petkov79d74c92012-03-07 17:20:32 +0100163 // Callback invoked on every IP configuration update.
164 void OnIPConfigUpdated(const IPConfigRefPtr &ipconfig, bool success);
165
Paul Stewarte6132022011-08-16 09:11:02 -0700166 // Maintain connection state (Routes, IP Addresses and DNS) in the OS.
167 void CreateConnection();
168
169 // Remove connection state
170 void DestroyConnection();
171
Paul Stewart03dba0b2011-08-22 16:32:45 -0700172 // Selects a service to be "current" -- i.e. link-state or configuration
173 // events that happen to the device are attributed to this service.
174 void SelectService(const ServiceRefPtr &service);
175
176 // Set the state of the selected service
177 void SetServiceState(Service::ConnectState state);
178
179 // Set the failure of the selected service (implicitly sets the state to
180 // "failure")
181 void SetServiceFailure(Service::ConnectFailure failure_state);
182
Paul Stewart20088d82012-02-16 06:58:55 -0800183 // Called by the Portal Detector whenever a trial completes. Device
184 // subclasses that choose unique mappings from portal results to connected
185 // states can override this method in order to do so.
186 virtual void PortalDetectorCallback(const PortalDetector::Result &result);
187
188 // Initiate portal detection, if enabled for this device type.
189 bool StartPortalDetection();
190
191 // Stop portal detection if it is running.
192 void StopPortalDetection();
193
194 // Set the state of the selected service, with checks to make sure
195 // the service is already in a connected state before doing so.
196 void SetServiceConnectedState(Service::ConnectState state);
197
Jason Glasgowb5790052012-01-27 01:03:52 -0500198 void HelpRegisterDerivedString(
199 const std::string &name,
200 std::string(Device::*get)(Error *),
201 void(Device::*set)(const std::string&, Error *));
Chris Masone27c4aa52011-07-02 13:10:14 -0700202 void HelpRegisterDerivedStrings(const std::string &name,
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800203 Strings(Device::*get)(Error *),
mukesh agrawalffa3d042011-10-06 15:26:10 -0700204 void(Device::*set)(const Strings&, Error *));
Chris Masoneb925cc82011-06-22 15:39:57 -0700205
Paul Stewartac4ac002011-08-26 12:04:26 -0700206 // Property getters reserved for subclasses
207 ControlInterface *control_interface() const { return control_interface_; }
Thieu Le3426c8f2012-01-11 17:35:11 -0800208 Metrics *metrics() const { return metrics_; }
Paul Stewartac4ac002011-08-26 12:04:26 -0700209 Manager *manager() const { return manager_; }
Paul Stewarta41e38d2011-11-11 07:47:29 -0800210 bool running() const { return running_; }
Paul Stewartd5843772011-05-11 15:40:42 -0700211
Paul Stewart75897df2011-04-27 09:05:53 -0700212 private:
Chris Masone413a3192011-05-09 17:10:05 -0700213 friend class DeviceAdaptorInterface;
Paul Stewartc681fa02012-03-02 19:40:04 -0800214 friend class DevicePortalDetectionTest;
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700215 friend class DeviceTest;
216 friend class CellularTest;
217 friend class WiFiMainTest;
Darin Petkovafa6fc42011-06-21 16:21:08 -0700218
Paul Stewart2bf1d352011-12-06 15:02:55 -0800219 static const char kIPFlagTemplate[];
220 static const char kIPFlagVersion4[];
221 static const char kIPFlagVersion6[];
222 static const char kIPFlagDisableIPv6[];
223 static const char kIPFlagUseTempAddr[];
224 static const char kIPFlagUseTempAddrUsedAndDefault[];
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800225 static const char kIPFlagReversePathFilter[];
226 static const char kIPFlagReversePathFilterEnabled[];
227 static const char kIPFlagReversePathFilterLooseMode[];
Chris Masone5dec5f42011-07-22 14:07:55 -0700228 static const char kStoragePowered[];
229 static const char kStorageIPConfigs[];
230
Chris Masone5dec5f42011-07-22 14:07:55 -0700231 // Right now, Devices reference IPConfigs directly when persisted to disk
232 // It's not clear that this makes sense long-term, but that's how it is now.
233 // This call generates a string in the right format for this persisting.
Chris Masone34af2182011-08-22 11:59:36 -0700234 // |suffix| is injected into the storage identifier used for the configs.
235 std::string SerializeIPConfigs(const std::string &suffix);
Chris Masone5dec5f42011-07-22 14:07:55 -0700236
Paul Stewart2bf1d352011-12-06 15:02:55 -0800237 // Set an IP configuration flag on the device. |ip_version| should be
238 // "ipv6" or "ipv4". |flag| should be the name of the flag to be set
239 // and |value| is what this flag should be set to.
240 bool SetIPFlag(IPAddress::Family family, const std::string &flag,
241 const std::string &value);
242
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800243 std::vector<std::string> AvailableIPConfigs(Error *error);
Chris Masone4e851612011-07-01 10:46:53 -0700244 std::string GetRpcConnectionIdentifier();
245
Paul Stewartac4ac002011-08-26 12:04:26 -0700246 // Properties
Paul Stewarta41e38d2011-11-11 07:47:29 -0800247 bool powered_; // indicates whether the device is configured to operate
Paul Stewartac4ac002011-08-26 12:04:26 -0700248 bool reconnect_;
249 const std::string hardware_address_;
250
251 PropertyStore store_;
252
Paul Stewartac4ac002011-08-26 12:04:26 -0700253 const int interface_index_;
Paul Stewarta41e38d2011-11-11 07:47:29 -0800254 bool running_; // indicates whether the device is actually in operation
Paul Stewartac4ac002011-08-26 12:04:26 -0700255 const std::string link_name_;
256 const std::string unique_id_;
257 ControlInterface *control_interface_;
258 EventDispatcher *dispatcher_;
Thieu Le3426c8f2012-01-11 17:35:11 -0800259 Metrics *metrics_;
Paul Stewartac4ac002011-08-26 12:04:26 -0700260 Manager *manager_;
261 IPConfigRefPtr ipconfig_;
262 ConnectionRefPtr connection_;
Darin Petkovafa6fc42011-06-21 16:21:08 -0700263 scoped_ptr<DeviceAdaptorInterface> adaptor_;
Paul Stewart20088d82012-02-16 06:58:55 -0800264 scoped_ptr<PortalDetector> portal_detector_;
265 scoped_ptr<Callback1<const PortalDetector::Result &>::Type>
266 portal_detector_callback_;
Gaurav Shah435de2c2011-11-17 19:01:07 -0800267 Technology::Identifier technology_;
Darin Petkovafa6fc42011-06-21 16:21:08 -0700268
Paul Stewart03dba0b2011-08-22 16:32:45 -0700269 // Maintain a reference to the connected / connecting service
270 ServiceRefPtr selected_service_;
271
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700272 // Cache singleton pointers for performance and test purposes.
Darin Petkov77cb6812011-08-15 16:19:41 -0700273 DHCPProvider *dhcp_provider_;
Thieu Lefb46caf2012-03-08 11:57:15 -0800274 RoutingTable *routing_table_;
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700275 RTNLHandler *rtnl_handler_;
Darin Petkov77cb6812011-08-15 16:19:41 -0700276
Chris Masone9be4a9d2011-05-16 15:44:09 -0700277 DISALLOW_COPY_AND_ASSIGN(Device);
Paul Stewart75897df2011-04-27 09:05:53 -0700278};
279
Paul Stewart75897df2011-04-27 09:05:53 -0700280} // namespace shill
281
282#endif // SHILL_DEVICE_