blob: 8036cd1a986ae6a4e7ecd0cdb5c87db140b1a5e6 [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;
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -070035class RTNLHandler;
Darin Petkove5bc2cb2011-12-07 14:47:32 +010036class ReturnerInterface;
Chris Masone9be4a9d2011-05-16 15:44:09 -070037
Paul Stewart75897df2011-04-27 09:05:53 -070038// Device superclass. Individual network interfaces types will inherit from
39// this class.
Chris Masone27c4aa52011-07-02 13:10:14 -070040class Device : public base::RefCounted<Device> {
Paul Stewart75897df2011-04-27 09:05:53 -070041 public:
Chris Masone9be4a9d2011-05-16 15:44:09 -070042 // A constructor for the Device object
43 Device(ControlInterface *control_interface,
Paul Stewartb50f0b92011-05-16 16:31:42 -070044 EventDispatcher *dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -080045 Metrics *metrics,
Paul Stewartf1ce5d22011-05-19 13:10:20 -070046 Manager *manager,
Darin Petkovafa6fc42011-06-21 16:21:08 -070047 const std::string &link_name,
Chris Masone626719f2011-08-18 16:58:48 -070048 const std::string &address,
Gaurav Shah435de2c2011-11-17 19:01:07 -080049 int interface_index,
50 Technology::Identifier technology);
Chris Masone9be4a9d2011-05-16 15:44:09 -070051 virtual ~Device();
52
53 virtual void Start();
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -070054
55 // Clear running state, especially any fields that hold a reference back
56 // to us. After a call to Stop(), the Device may be restarted (with a call
57 // to Start()), or destroyed (if its refcount falls to zero).
Chris Masone9be4a9d2011-05-16 15:44:09 -070058 virtual void Stop();
59
Gaurav Shah435de2c2011-11-17 19:01:07 -080060
61 // TODO(gauravsh): We do not really need this since technology() can be used
62 // to get a device's technology for direct comparison.
Darin Petkovafa6fc42011-06-21 16:21:08 -070063 // Base method always returns false.
Paul Stewartfdd16072011-09-16 12:41:35 -070064 virtual bool TechnologyIs(const Technology::Identifier type) const;
Darin Petkovafa6fc42011-06-21 16:21:08 -070065
Paul Stewartf1ce5d22011-05-19 13:10:20 -070066 virtual void LinkEvent(unsigned flags, unsigned change);
Paul Stewart75897df2011-04-27 09:05:53 -070067
Chris Masonec1e50412011-06-07 13:04:53 -070068 virtual void ConfigIP() {}
69
Darin Petkov9ae310f2011-08-30 15:41:13 -070070 // The default implementation sets |error| to kNotSupported.
Darin Petkovc0865312011-09-16 15:31:20 -070071 virtual void Scan(Error *error);
Eric Shienbrood5de44ab2011-12-05 10:46:27 -050072 virtual void RegisterOnNetwork(const std::string &network_id,
73 ReturnerInterface *returner);
Darin Petkovc64fe5e2012-01-11 12:46:13 +010074 virtual void RequirePIN(
75 const std::string &pin, bool require, ReturnerInterface *returner);
Darin Petkove5bc2cb2011-12-07 14:47:32 +010076 virtual void EnterPIN(const std::string &pin, ReturnerInterface *returner);
Darin Petkove42e1012011-08-31 12:35:04 -070077 virtual void UnblockPIN(const std::string &unblock_code,
78 const std::string &pin,
Darin Petkovc64fe5e2012-01-11 12:46:13 +010079 ReturnerInterface *returner);
Darin Petkove42e1012011-08-31 12:35:04 -070080 virtual void ChangePIN(const std::string &old_pin,
81 const std::string &new_pin,
Darin Petkovc64fe5e2012-01-11 12:46:13 +010082 ReturnerInterface *returner);
Paul Stewart2bf1d352011-12-06 15:02:55 -080083 virtual void DisableIPv6();
84 virtual void EnableIPv6();
85 virtual void EnableIPv6Privacy();
Darin Petkov9ae310f2011-08-30 15:41:13 -070086
Paul Stewartc8f4bef2011-12-13 09:45:51 -080087 // Request the removal of reverse-path filtering for this interface.
88 // This will allow packets destined for this interface to be accepted,
89 // even if this is not the default route for such a packet to arrive.
90 virtual void DisableReversePathFilter();
91
92 // Request reverse-path filtering for this interface.
93 virtual void EnableReversePathFilter();
94
Gaurav Shah435de2c2011-11-17 19:01:07 -080095 // Returns true if the selected service on the device (if any) is connected.
96 // Returns false if there is no selected service, or if the selected service
97 // is not connected.
98 bool IsConnected() const;
99
Chris Masone27c4aa52011-07-02 13:10:14 -0700100 std::string GetRpcIdentifier();
Chris Masone5dec5f42011-07-22 14:07:55 -0700101 std::string GetStorageIdentifier();
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700102
Chris Masone9d779932011-08-25 16:33:41 -0700103 const std::string &address() const { return hardware_address_; }
Darin Petkove0a312e2011-07-20 13:45:28 -0700104 const std::string &link_name() const { return link_name_; }
105 int interface_index() const { return interface_index_; }
Paul Stewarte6132022011-08-16 09:11:02 -0700106 const ConnectionRefPtr &connection() const { return connection_; }
Paul Stewarta41e38d2011-11-11 07:47:29 -0800107 bool powered() const { return powered_; }
Gaurav Shah435de2c2011-11-17 19:01:07 -0800108 virtual Technology::Identifier technology() const { return technology_; }
Jason Glasgowb5790052012-01-27 01:03:52 -0500109 std::string GetTechnologyString(Error *error);
Darin Petkove0a312e2011-07-20 13:45:28 -0700110
Chris Masone19e30402011-07-19 15:48:47 -0700111 const std::string &FriendlyName() const;
Chris Masone27c4aa52011-07-02 13:10:14 -0700112
Darin Petkovafa6fc42011-06-21 16:21:08 -0700113 // Returns a string that is guaranteed to uniquely identify this Device
114 // instance.
115 const std::string &UniqueName() const;
Chris Masonea82b7112011-05-25 15:16:29 -0700116
mukesh agrawalde29fa82011-09-16 16:16:36 -0700117 PropertyStore *mutable_store() { return &store_; }
118 const PropertyStore &store() const { return store_; }
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700119 RTNLHandler *rtnl_handler() { return rtnl_handler_; }
Chris Masone19e30402011-07-19 15:48:47 -0700120
Darin Petkovb05315f2011-11-07 10:14:25 +0100121 EventDispatcher *dispatcher() const { return dispatcher_; }
122
Paul Stewarta41e38d2011-11-11 07:47:29 -0800123 // Load configuration for the device from |storage|. This may include
124 // instantiating non-visible services for which configuration has been
125 // stored.
126 virtual bool Load(StoreInterface *storage);
127
128 // Save configuration for the device to |storage|.
Chris Masone877ff982011-09-21 16:18:24 -0700129 virtual bool Save(StoreInterface *storage);
Chris Masone5dec5f42011-07-22 14:07:55 -0700130
Darin Petkov77cb6812011-08-15 16:19:41 -0700131 void set_dhcp_provider(DHCPProvider *provider) { dhcp_provider_ = provider; }
132
Darin Petkov3cfbf212011-11-21 16:02:09 +0100133 DeviceAdaptorInterface *adaptor() const { return adaptor_.get(); }
134
Paul Stewartd5843772011-05-11 15:40:42 -0700135 protected:
Paul Stewart2bf1d352011-12-06 15:02:55 -0800136 FRIEND_TEST(DeviceTest, AcquireIPConfig);
Darin Petkovafa6fc42011-06-21 16:21:08 -0700137 FRIEND_TEST(DeviceTest, DestroyIPConfig);
138 FRIEND_TEST(DeviceTest, DestroyIPConfigNULL);
Chris Masone95207da2011-06-29 16:50:49 -0700139 FRIEND_TEST(DeviceTest, GetProperties);
Chris Masone5dec5f42011-07-22 14:07:55 -0700140 FRIEND_TEST(DeviceTest, Save);
Paul Stewart03dba0b2011-08-22 16:32:45 -0700141 FRIEND_TEST(DeviceTest, SelectedService);
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700142 FRIEND_TEST(DeviceTest, Stop);
Paul Stewarta41e38d2011-11-11 07:47:29 -0800143 FRIEND_TEST(ManagerTest, DeviceRegistrationAndStart);
Gaurav Shah435de2c2011-11-17 19:01:07 -0800144 FRIEND_TEST(ManagerTest, ConnectedTechnologies);
145 FRIEND_TEST(ManagerTest, DefaultTechnology);
mukesh agrawal32399322011-09-01 10:53:43 -0700146 FRIEND_TEST(WiFiMainTest, Connect);
Darin Petkovafa6fc42011-06-21 16:21:08 -0700147
148 // If there's an IP configuration in |ipconfig_|, releases the IP address and
149 // destroys the configuration instance.
150 void DestroyIPConfig();
151
152 // Creates a new DHCP IP configuration instance, stores it in |ipconfig_| and
153 // requests a new IP configuration. Registers a callback to
154 // IPConfigUpdatedCallback on IP configuration changes. Returns true if the IP
155 // request was successfully sent.
Paul Stewart2bf1d352011-12-06 15:02:55 -0800156 bool AcquireIPConfig();
Darin Petkovafa6fc42011-06-21 16:21:08 -0700157
Paul Stewarte6132022011-08-16 09:11:02 -0700158 // Maintain connection state (Routes, IP Addresses and DNS) in the OS.
159 void CreateConnection();
160
161 // Remove connection state
162 void DestroyConnection();
163
Paul Stewart03dba0b2011-08-22 16:32:45 -0700164 // Selects a service to be "current" -- i.e. link-state or configuration
165 // events that happen to the device are attributed to this service.
166 void SelectService(const ServiceRefPtr &service);
167
168 // Set the state of the selected service
169 void SetServiceState(Service::ConnectState state);
170
171 // Set the failure of the selected service (implicitly sets the state to
172 // "failure")
173 void SetServiceFailure(Service::ConnectFailure failure_state);
174
Paul Stewart20088d82012-02-16 06:58:55 -0800175 // Called by the Portal Detector whenever a trial completes. Device
176 // subclasses that choose unique mappings from portal results to connected
177 // states can override this method in order to do so.
178 virtual void PortalDetectorCallback(const PortalDetector::Result &result);
179
180 // Initiate portal detection, if enabled for this device type.
181 bool StartPortalDetection();
182
183 // Stop portal detection if it is running.
184 void StopPortalDetection();
185
186 // Set the state of the selected service, with checks to make sure
187 // the service is already in a connected state before doing so.
188 void SetServiceConnectedState(Service::ConnectState state);
189
Jason Glasgowb5790052012-01-27 01:03:52 -0500190 void HelpRegisterDerivedString(
191 const std::string &name,
192 std::string(Device::*get)(Error *),
193 void(Device::*set)(const std::string&, Error *));
Chris Masone27c4aa52011-07-02 13:10:14 -0700194 void HelpRegisterDerivedStrings(const std::string &name,
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800195 Strings(Device::*get)(Error *),
mukesh agrawalffa3d042011-10-06 15:26:10 -0700196 void(Device::*set)(const Strings&, Error *));
Chris Masoneb925cc82011-06-22 15:39:57 -0700197
Paul Stewartac4ac002011-08-26 12:04:26 -0700198 // Property getters reserved for subclasses
199 ControlInterface *control_interface() const { return control_interface_; }
Thieu Le3426c8f2012-01-11 17:35:11 -0800200 Metrics *metrics() const { return metrics_; }
Paul Stewartac4ac002011-08-26 12:04:26 -0700201 Manager *manager() const { return manager_; }
Paul Stewarta41e38d2011-11-11 07:47:29 -0800202 bool running() const { return running_; }
Paul Stewartd5843772011-05-11 15:40:42 -0700203
Paul Stewart75897df2011-04-27 09:05:53 -0700204 private:
Chris Masone413a3192011-05-09 17:10:05 -0700205 friend class DeviceAdaptorInterface;
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700206 friend class DeviceTest;
207 friend class CellularTest;
208 friend class WiFiMainTest;
Darin Petkovafa6fc42011-06-21 16:21:08 -0700209
Paul Stewart2bf1d352011-12-06 15:02:55 -0800210 static const char kIPFlagTemplate[];
211 static const char kIPFlagVersion4[];
212 static const char kIPFlagVersion6[];
213 static const char kIPFlagDisableIPv6[];
214 static const char kIPFlagUseTempAddr[];
215 static const char kIPFlagUseTempAddrUsedAndDefault[];
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800216 static const char kIPFlagReversePathFilter[];
217 static const char kIPFlagReversePathFilterEnabled[];
218 static const char kIPFlagReversePathFilterLooseMode[];
Chris Masone5dec5f42011-07-22 14:07:55 -0700219 static const char kStoragePowered[];
220 static const char kStorageIPConfigs[];
221
Darin Petkovafa6fc42011-06-21 16:21:08 -0700222 // Callback invoked on every IP configuration update.
Chris Masone2b105542011-06-22 10:58:09 -0700223 void IPConfigUpdatedCallback(const IPConfigRefPtr &ipconfig, bool success);
Darin Petkovafa6fc42011-06-21 16:21:08 -0700224
Chris Masone5dec5f42011-07-22 14:07:55 -0700225 // Right now, Devices reference IPConfigs directly when persisted to disk
226 // It's not clear that this makes sense long-term, but that's how it is now.
227 // This call generates a string in the right format for this persisting.
Chris Masone34af2182011-08-22 11:59:36 -0700228 // |suffix| is injected into the storage identifier used for the configs.
229 std::string SerializeIPConfigs(const std::string &suffix);
Chris Masone5dec5f42011-07-22 14:07:55 -0700230
Paul Stewart2bf1d352011-12-06 15:02:55 -0800231 // Set an IP configuration flag on the device. |ip_version| should be
232 // "ipv6" or "ipv4". |flag| should be the name of the flag to be set
233 // and |value| is what this flag should be set to.
234 bool SetIPFlag(IPAddress::Family family, const std::string &flag,
235 const std::string &value);
236
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800237 std::vector<std::string> AvailableIPConfigs(Error *error);
Chris Masone4e851612011-07-01 10:46:53 -0700238 std::string GetRpcConnectionIdentifier();
239
Paul Stewartac4ac002011-08-26 12:04:26 -0700240 // Properties
Paul Stewarta41e38d2011-11-11 07:47:29 -0800241 bool powered_; // indicates whether the device is configured to operate
Paul Stewartac4ac002011-08-26 12:04:26 -0700242 bool reconnect_;
243 const std::string hardware_address_;
244
245 PropertyStore store_;
246
Paul Stewartac4ac002011-08-26 12:04:26 -0700247 const int interface_index_;
Paul Stewarta41e38d2011-11-11 07:47:29 -0800248 bool running_; // indicates whether the device is actually in operation
Paul Stewartac4ac002011-08-26 12:04:26 -0700249 const std::string link_name_;
250 const std::string unique_id_;
251 ControlInterface *control_interface_;
252 EventDispatcher *dispatcher_;
Thieu Le3426c8f2012-01-11 17:35:11 -0800253 Metrics *metrics_;
Paul Stewartac4ac002011-08-26 12:04:26 -0700254 Manager *manager_;
255 IPConfigRefPtr ipconfig_;
256 ConnectionRefPtr connection_;
Darin Petkovafa6fc42011-06-21 16:21:08 -0700257 scoped_ptr<DeviceAdaptorInterface> adaptor_;
Paul Stewart20088d82012-02-16 06:58:55 -0800258 scoped_ptr<PortalDetector> portal_detector_;
259 scoped_ptr<Callback1<const PortalDetector::Result &>::Type>
260 portal_detector_callback_;
Gaurav Shah435de2c2011-11-17 19:01:07 -0800261 Technology::Identifier technology_;
Darin Petkovafa6fc42011-06-21 16:21:08 -0700262
Paul Stewart03dba0b2011-08-22 16:32:45 -0700263 // Maintain a reference to the connected / connecting service
264 ServiceRefPtr selected_service_;
265
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700266 // Cache singleton pointers for performance and test purposes.
Darin Petkov77cb6812011-08-15 16:19:41 -0700267 DHCPProvider *dhcp_provider_;
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700268 RTNLHandler *rtnl_handler_;
Darin Petkov77cb6812011-08-15 16:19:41 -0700269
Chris Masone9be4a9d2011-05-16 15:44:09 -0700270 DISALLOW_COPY_AND_ASSIGN(Device);
Paul Stewart75897df2011-04-27 09:05:53 -0700271};
272
Paul Stewart75897df2011-04-27 09:05:53 -0700273} // namespace shill
274
275#endif // SHILL_DEVICE_