blob: cb82509dc56c74300d79f15a8d9658723b28f9d8 [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"
Chris Masoneb925cc82011-06-22 15:39:57 -070019#include "shill/property_store.h"
Chris Masone2b105542011-06-22 10:58:09 -070020#include "shill/refptr_types.h"
Paul Stewart03dba0b2011-08-22 16:32:45 -070021#include "shill/service.h"
Paul Stewartfdd16072011-09-16 12:41:35 -070022#include "shill/technology.h"
Paul Stewart75897df2011-04-27 09:05:53 -070023
24namespace shill {
25
Chris Masone9be4a9d2011-05-16 15:44:09 -070026class ControlInterface;
Darin Petkov77cb6812011-08-15 16:19:41 -070027class DHCPProvider;
Chris Masone9be4a9d2011-05-16 15:44:09 -070028class DeviceAdaptorInterface;
Paul Stewartf1ce5d22011-05-19 13:10:20 -070029class Endpoint;
Chris Masone8fe2c7e2011-06-09 15:51:19 -070030class Error;
31class EventDispatcher;
Paul Stewartf1ce5d22011-05-19 13:10:20 -070032class Manager;
Thieu Le3426c8f2012-01-11 17:35:11 -080033class Metrics;
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -070034class RTNLHandler;
Darin Petkove5bc2cb2011-12-07 14:47:32 +010035class ReturnerInterface;
Chris Masone9be4a9d2011-05-16 15:44:09 -070036
Paul Stewart75897df2011-04-27 09:05:53 -070037// Device superclass. Individual network interfaces types will inherit from
38// this class.
Chris Masone27c4aa52011-07-02 13:10:14 -070039class Device : public base::RefCounted<Device> {
Paul Stewart75897df2011-04-27 09:05:53 -070040 public:
Chris Masone9be4a9d2011-05-16 15:44:09 -070041 // A constructor for the Device object
42 Device(ControlInterface *control_interface,
Paul Stewartb50f0b92011-05-16 16:31:42 -070043 EventDispatcher *dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -080044 Metrics *metrics,
Paul Stewartf1ce5d22011-05-19 13:10:20 -070045 Manager *manager,
Darin Petkovafa6fc42011-06-21 16:21:08 -070046 const std::string &link_name,
Chris Masone626719f2011-08-18 16:58:48 -070047 const std::string &address,
Gaurav Shah435de2c2011-11-17 19:01:07 -080048 int interface_index,
49 Technology::Identifier technology);
Chris Masone9be4a9d2011-05-16 15:44:09 -070050 virtual ~Device();
51
52 virtual void Start();
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -070053
54 // Clear running state, especially any fields that hold a reference back
55 // to us. After a call to Stop(), the Device may be restarted (with a call
56 // to Start()), or destroyed (if its refcount falls to zero).
Chris Masone9be4a9d2011-05-16 15:44:09 -070057 virtual void Stop();
58
Gaurav Shah435de2c2011-11-17 19:01:07 -080059
60 // TODO(gauravsh): We do not really need this since technology() can be used
61 // to get a device's technology for direct comparison.
Darin Petkovafa6fc42011-06-21 16:21:08 -070062 // Base method always returns false.
Paul Stewartfdd16072011-09-16 12:41:35 -070063 virtual bool TechnologyIs(const Technology::Identifier type) const;
Darin Petkovafa6fc42011-06-21 16:21:08 -070064
Paul Stewartf1ce5d22011-05-19 13:10:20 -070065 virtual void LinkEvent(unsigned flags, unsigned change);
Paul Stewart75897df2011-04-27 09:05:53 -070066
Chris Masonec1e50412011-06-07 13:04:53 -070067 virtual void ConfigIP() {}
68
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);
Darin Petkov9ae310f2011-08-30 15:41:13 -070071 virtual void RegisterOnNetwork(const std::string &network_id, Error *error);
Darin Petkovc64fe5e2012-01-11 12:46:13 +010072 virtual void RequirePIN(
73 const std::string &pin, bool require, ReturnerInterface *returner);
Darin Petkove5bc2cb2011-12-07 14:47:32 +010074 virtual void EnterPIN(const std::string &pin, ReturnerInterface *returner);
Darin Petkove42e1012011-08-31 12:35:04 -070075 virtual void UnblockPIN(const std::string &unblock_code,
76 const std::string &pin,
Darin Petkovc64fe5e2012-01-11 12:46:13 +010077 ReturnerInterface *returner);
Darin Petkove42e1012011-08-31 12:35:04 -070078 virtual void ChangePIN(const std::string &old_pin,
79 const std::string &new_pin,
Darin Petkovc64fe5e2012-01-11 12:46:13 +010080 ReturnerInterface *returner);
Paul Stewart2bf1d352011-12-06 15:02:55 -080081 virtual void DisableIPv6();
82 virtual void EnableIPv6();
83 virtual void EnableIPv6Privacy();
Darin Petkov9ae310f2011-08-30 15:41:13 -070084
Paul Stewartc8f4bef2011-12-13 09:45:51 -080085 // Request the removal of reverse-path filtering for this interface.
86 // This will allow packets destined for this interface to be accepted,
87 // even if this is not the default route for such a packet to arrive.
88 virtual void DisableReversePathFilter();
89
90 // Request reverse-path filtering for this interface.
91 virtual void EnableReversePathFilter();
92
Gaurav Shah435de2c2011-11-17 19:01:07 -080093 // Returns true if the selected service on the device (if any) is connected.
94 // Returns false if there is no selected service, or if the selected service
95 // is not connected.
96 bool IsConnected() const;
97
Chris Masone27c4aa52011-07-02 13:10:14 -070098 std::string GetRpcIdentifier();
Chris Masone5dec5f42011-07-22 14:07:55 -070099 std::string GetStorageIdentifier();
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700100
Chris Masone9d779932011-08-25 16:33:41 -0700101 const std::string &address() const { return hardware_address_; }
Darin Petkove0a312e2011-07-20 13:45:28 -0700102 const std::string &link_name() const { return link_name_; }
103 int interface_index() const { return interface_index_; }
Paul Stewarte6132022011-08-16 09:11:02 -0700104 const ConnectionRefPtr &connection() const { return connection_; }
Paul Stewarta41e38d2011-11-11 07:47:29 -0800105 bool powered() const { return powered_; }
Gaurav Shah435de2c2011-11-17 19:01:07 -0800106 virtual Technology::Identifier technology() const { return technology_; }
Darin Petkove0a312e2011-07-20 13:45:28 -0700107
Chris Masone19e30402011-07-19 15:48:47 -0700108 const std::string &FriendlyName() const;
Chris Masone27c4aa52011-07-02 13:10:14 -0700109
Darin Petkovafa6fc42011-06-21 16:21:08 -0700110 // Returns a string that is guaranteed to uniquely identify this Device
111 // instance.
112 const std::string &UniqueName() const;
Chris Masonea82b7112011-05-25 15:16:29 -0700113
mukesh agrawalde29fa82011-09-16 16:16:36 -0700114 PropertyStore *mutable_store() { return &store_; }
115 const PropertyStore &store() const { return store_; }
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700116 RTNLHandler *rtnl_handler() { return rtnl_handler_; }
Chris Masone19e30402011-07-19 15:48:47 -0700117
Darin Petkovb05315f2011-11-07 10:14:25 +0100118 EventDispatcher *dispatcher() const { return dispatcher_; }
119
Paul Stewarta41e38d2011-11-11 07:47:29 -0800120 // Load configuration for the device from |storage|. This may include
121 // instantiating non-visible services for which configuration has been
122 // stored.
123 virtual bool Load(StoreInterface *storage);
124
125 // Save configuration for the device to |storage|.
Chris Masone877ff982011-09-21 16:18:24 -0700126 virtual bool Save(StoreInterface *storage);
Chris Masone5dec5f42011-07-22 14:07:55 -0700127
Darin Petkov77cb6812011-08-15 16:19:41 -0700128 void set_dhcp_provider(DHCPProvider *provider) { dhcp_provider_ = provider; }
129
Darin Petkov3cfbf212011-11-21 16:02:09 +0100130 DeviceAdaptorInterface *adaptor() const { return adaptor_.get(); }
131
Paul Stewartd5843772011-05-11 15:40:42 -0700132 protected:
Paul Stewart2bf1d352011-12-06 15:02:55 -0800133 FRIEND_TEST(DeviceTest, AcquireIPConfig);
Darin Petkovafa6fc42011-06-21 16:21:08 -0700134 FRIEND_TEST(DeviceTest, DestroyIPConfig);
135 FRIEND_TEST(DeviceTest, DestroyIPConfigNULL);
Chris Masone95207da2011-06-29 16:50:49 -0700136 FRIEND_TEST(DeviceTest, GetProperties);
Paul Stewartbe5f5b32011-12-07 17:11:11 -0800137 FRIEND_TEST(DeviceTest, IPConfigUpdatedFailure);
138 FRIEND_TEST(DeviceTest, IPConfigUpdatedSuccess);
Chris Masone5dec5f42011-07-22 14:07:55 -0700139 FRIEND_TEST(DeviceTest, Save);
Paul Stewart03dba0b2011-08-22 16:32:45 -0700140 FRIEND_TEST(DeviceTest, SelectedService);
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700141 FRIEND_TEST(DeviceTest, Stop);
Paul Stewarta41e38d2011-11-11 07:47:29 -0800142 FRIEND_TEST(ManagerTest, DeviceRegistrationAndStart);
Gaurav Shah435de2c2011-11-17 19:01:07 -0800143 FRIEND_TEST(ManagerTest, ConnectedTechnologies);
144 FRIEND_TEST(ManagerTest, DefaultTechnology);
mukesh agrawal32399322011-09-01 10:53:43 -0700145 FRIEND_TEST(WiFiMainTest, Connect);
Darin Petkovafa6fc42011-06-21 16:21:08 -0700146
147 // If there's an IP configuration in |ipconfig_|, releases the IP address and
148 // destroys the configuration instance.
149 void DestroyIPConfig();
150
151 // Creates a new DHCP IP configuration instance, stores it in |ipconfig_| and
152 // requests a new IP configuration. Registers a callback to
153 // IPConfigUpdatedCallback on IP configuration changes. Returns true if the IP
154 // request was successfully sent.
Paul Stewart2bf1d352011-12-06 15:02:55 -0800155 bool AcquireIPConfig();
Darin Petkovafa6fc42011-06-21 16:21:08 -0700156
Paul Stewarte6132022011-08-16 09:11:02 -0700157 // Maintain connection state (Routes, IP Addresses and DNS) in the OS.
158 void CreateConnection();
159
160 // Remove connection state
161 void DestroyConnection();
162
Paul Stewart03dba0b2011-08-22 16:32:45 -0700163 // Selects a service to be "current" -- i.e. link-state or configuration
164 // events that happen to the device are attributed to this service.
165 void SelectService(const ServiceRefPtr &service);
166
167 // Set the state of the selected service
168 void SetServiceState(Service::ConnectState state);
169
170 // Set the failure of the selected service (implicitly sets the state to
171 // "failure")
172 void SetServiceFailure(Service::ConnectFailure failure_state);
173
Chris Masone27c4aa52011-07-02 13:10:14 -0700174 void HelpRegisterDerivedStrings(const std::string &name,
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800175 Strings(Device::*get)(Error *),
mukesh agrawalffa3d042011-10-06 15:26:10 -0700176 void(Device::*set)(const Strings&, Error *));
Chris Masoneb925cc82011-06-22 15:39:57 -0700177
Paul Stewartac4ac002011-08-26 12:04:26 -0700178 // Property getters reserved for subclasses
179 ControlInterface *control_interface() const { return control_interface_; }
Thieu Le3426c8f2012-01-11 17:35:11 -0800180 Metrics *metrics() const { return metrics_; }
Paul Stewartac4ac002011-08-26 12:04:26 -0700181 Manager *manager() const { return manager_; }
Paul Stewarta41e38d2011-11-11 07:47:29 -0800182 bool running() const { return running_; }
Paul Stewartd5843772011-05-11 15:40:42 -0700183
Paul Stewart75897df2011-04-27 09:05:53 -0700184 private:
Chris Masone413a3192011-05-09 17:10:05 -0700185 friend class DeviceAdaptorInterface;
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700186 friend class DeviceTest;
187 friend class CellularTest;
188 friend class WiFiMainTest;
Darin Petkovafa6fc42011-06-21 16:21:08 -0700189
Paul Stewart2bf1d352011-12-06 15:02:55 -0800190 static const char kIPFlagTemplate[];
191 static const char kIPFlagVersion4[];
192 static const char kIPFlagVersion6[];
193 static const char kIPFlagDisableIPv6[];
194 static const char kIPFlagUseTempAddr[];
195 static const char kIPFlagUseTempAddrUsedAndDefault[];
Paul Stewartc8f4bef2011-12-13 09:45:51 -0800196 static const char kIPFlagReversePathFilter[];
197 static const char kIPFlagReversePathFilterEnabled[];
198 static const char kIPFlagReversePathFilterLooseMode[];
Chris Masone5dec5f42011-07-22 14:07:55 -0700199 static const char kStoragePowered[];
200 static const char kStorageIPConfigs[];
201
Darin Petkovafa6fc42011-06-21 16:21:08 -0700202 // Callback invoked on every IP configuration update.
Chris Masone2b105542011-06-22 10:58:09 -0700203 void IPConfigUpdatedCallback(const IPConfigRefPtr &ipconfig, bool success);
Darin Petkovafa6fc42011-06-21 16:21:08 -0700204
Chris Masone5dec5f42011-07-22 14:07:55 -0700205 // Right now, Devices reference IPConfigs directly when persisted to disk
206 // It's not clear that this makes sense long-term, but that's how it is now.
207 // This call generates a string in the right format for this persisting.
Chris Masone34af2182011-08-22 11:59:36 -0700208 // |suffix| is injected into the storage identifier used for the configs.
209 std::string SerializeIPConfigs(const std::string &suffix);
Chris Masone5dec5f42011-07-22 14:07:55 -0700210
Paul Stewart2bf1d352011-12-06 15:02:55 -0800211 // Set an IP configuration flag on the device. |ip_version| should be
212 // "ipv6" or "ipv4". |flag| should be the name of the flag to be set
213 // and |value| is what this flag should be set to.
214 bool SetIPFlag(IPAddress::Family family, const std::string &flag,
215 const std::string &value);
216
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800217 std::vector<std::string> AvailableIPConfigs(Error *error);
Chris Masone4e851612011-07-01 10:46:53 -0700218 std::string GetRpcConnectionIdentifier();
219
Paul Stewartac4ac002011-08-26 12:04:26 -0700220 // Properties
Paul Stewarta41e38d2011-11-11 07:47:29 -0800221 bool powered_; // indicates whether the device is configured to operate
Paul Stewartac4ac002011-08-26 12:04:26 -0700222 bool reconnect_;
223 const std::string hardware_address_;
224
225 PropertyStore store_;
226
Paul Stewartac4ac002011-08-26 12:04:26 -0700227 const int interface_index_;
Paul Stewarta41e38d2011-11-11 07:47:29 -0800228 bool running_; // indicates whether the device is actually in operation
Paul Stewartac4ac002011-08-26 12:04:26 -0700229 const std::string link_name_;
230 const std::string unique_id_;
231 ControlInterface *control_interface_;
232 EventDispatcher *dispatcher_;
Thieu Le3426c8f2012-01-11 17:35:11 -0800233 Metrics *metrics_;
Paul Stewartac4ac002011-08-26 12:04:26 -0700234 Manager *manager_;
235 IPConfigRefPtr ipconfig_;
236 ConnectionRefPtr connection_;
Darin Petkovafa6fc42011-06-21 16:21:08 -0700237 scoped_ptr<DeviceAdaptorInterface> adaptor_;
Gaurav Shah435de2c2011-11-17 19:01:07 -0800238 Technology::Identifier technology_;
Darin Petkovafa6fc42011-06-21 16:21:08 -0700239
Paul Stewart03dba0b2011-08-22 16:32:45 -0700240 // Maintain a reference to the connected / connecting service
241 ServiceRefPtr selected_service_;
242
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700243 // Cache singleton pointers for performance and test purposes.
Darin Petkov77cb6812011-08-15 16:19:41 -0700244 DHCPProvider *dhcp_provider_;
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700245 RTNLHandler *rtnl_handler_;
Darin Petkov77cb6812011-08-15 16:19:41 -0700246
Chris Masone9be4a9d2011-05-16 15:44:09 -0700247 DISALLOW_COPY_AND_ASSIGN(Device);
Paul Stewart75897df2011-04-27 09:05:53 -0700248};
249
Paul Stewart75897df2011-04-27 09:05:53 -0700250} // namespace shill
251
252#endif // SHILL_DEVICE_