blob: c019a8f135091cd7802b1e0141e3208433a150c3 [file] [log] [blame]
Paul Stewart75897df2011-04-27 09:05:53 -07001// Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
2// 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;
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -070033class RTNLHandler;
Chris Masone9be4a9d2011-05-16 15:44:09 -070034
Paul Stewart75897df2011-04-27 09:05:53 -070035// Device superclass. Individual network interfaces types will inherit from
36// this class.
Chris Masone27c4aa52011-07-02 13:10:14 -070037class Device : public base::RefCounted<Device> {
Paul Stewart75897df2011-04-27 09:05:53 -070038 public:
Chris Masone9be4a9d2011-05-16 15:44:09 -070039 // A constructor for the Device object
40 Device(ControlInterface *control_interface,
Paul Stewartb50f0b92011-05-16 16:31:42 -070041 EventDispatcher *dispatcher,
Paul Stewartf1ce5d22011-05-19 13:10:20 -070042 Manager *manager,
Darin Petkovafa6fc42011-06-21 16:21:08 -070043 const std::string &link_name,
Chris Masone626719f2011-08-18 16:58:48 -070044 const std::string &address,
Gaurav Shah435de2c2011-11-17 19:01:07 -080045 int interface_index,
46 Technology::Identifier technology);
Chris Masone9be4a9d2011-05-16 15:44:09 -070047 virtual ~Device();
48
49 virtual void Start();
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -070050
51 // Clear running state, especially any fields that hold a reference back
52 // to us. After a call to Stop(), the Device may be restarted (with a call
53 // to Start()), or destroyed (if its refcount falls to zero).
Chris Masone9be4a9d2011-05-16 15:44:09 -070054 virtual void Stop();
55
Gaurav Shah435de2c2011-11-17 19:01:07 -080056
57 // TODO(gauravsh): We do not really need this since technology() can be used
58 // to get a device's technology for direct comparison.
Darin Petkovafa6fc42011-06-21 16:21:08 -070059 // Base method always returns false.
Paul Stewartfdd16072011-09-16 12:41:35 -070060 virtual bool TechnologyIs(const Technology::Identifier type) const;
Darin Petkovafa6fc42011-06-21 16:21:08 -070061
Paul Stewartf1ce5d22011-05-19 13:10:20 -070062 virtual void LinkEvent(unsigned flags, unsigned change);
Paul Stewart75897df2011-04-27 09:05:53 -070063
Chris Masonec1e50412011-06-07 13:04:53 -070064 virtual void ConfigIP() {}
65
Darin Petkov9ae310f2011-08-30 15:41:13 -070066 // The default implementation sets |error| to kNotSupported.
Darin Petkovc0865312011-09-16 15:31:20 -070067 virtual void Scan(Error *error);
Darin Petkov9ae310f2011-08-30 15:41:13 -070068 virtual void RegisterOnNetwork(const std::string &network_id, Error *error);
Darin Petkove42e1012011-08-31 12:35:04 -070069 virtual void RequirePIN(const std::string &pin, bool require, Error *error);
70 virtual void EnterPIN(const std::string &pin, Error *error);
71 virtual void UnblockPIN(const std::string &unblock_code,
72 const std::string &pin,
73 Error *error);
74 virtual void ChangePIN(const std::string &old_pin,
75 const std::string &new_pin,
76 Error *error);
Paul Stewart2bf1d352011-12-06 15:02:55 -080077 virtual void DisableIPv6();
78 virtual void EnableIPv6();
79 virtual void EnableIPv6Privacy();
Darin Petkov9ae310f2011-08-30 15:41:13 -070080
Gaurav Shah435de2c2011-11-17 19:01:07 -080081 // Returns true if the selected service on the device (if any) is connected.
82 // Returns false if there is no selected service, or if the selected service
83 // is not connected.
84 bool IsConnected() const;
85
Chris Masone27c4aa52011-07-02 13:10:14 -070086 std::string GetRpcIdentifier();
Chris Masone5dec5f42011-07-22 14:07:55 -070087 std::string GetStorageIdentifier();
Chris Masone8fe2c7e2011-06-09 15:51:19 -070088
Chris Masone9d779932011-08-25 16:33:41 -070089 const std::string &address() const { return hardware_address_; }
Darin Petkove0a312e2011-07-20 13:45:28 -070090 const std::string &link_name() const { return link_name_; }
91 int interface_index() const { return interface_index_; }
Paul Stewarte6132022011-08-16 09:11:02 -070092 const ConnectionRefPtr &connection() const { return connection_; }
Paul Stewarta41e38d2011-11-11 07:47:29 -080093 bool powered() const { return powered_; }
Gaurav Shah435de2c2011-11-17 19:01:07 -080094 virtual Technology::Identifier technology() const { return technology_; }
Darin Petkove0a312e2011-07-20 13:45:28 -070095
Chris Masone19e30402011-07-19 15:48:47 -070096 const std::string &FriendlyName() const;
Chris Masone27c4aa52011-07-02 13:10:14 -070097
Darin Petkovafa6fc42011-06-21 16:21:08 -070098 // Returns a string that is guaranteed to uniquely identify this Device
99 // instance.
100 const std::string &UniqueName() const;
Chris Masonea82b7112011-05-25 15:16:29 -0700101
mukesh agrawalde29fa82011-09-16 16:16:36 -0700102 PropertyStore *mutable_store() { return &store_; }
103 const PropertyStore &store() const { return store_; }
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700104 RTNLHandler *rtnl_handler() { return rtnl_handler_; }
Chris Masone19e30402011-07-19 15:48:47 -0700105
Darin Petkovb05315f2011-11-07 10:14:25 +0100106 EventDispatcher *dispatcher() const { return dispatcher_; }
107
Paul Stewarta41e38d2011-11-11 07:47:29 -0800108 // Load configuration for the device from |storage|. This may include
109 // instantiating non-visible services for which configuration has been
110 // stored.
111 virtual bool Load(StoreInterface *storage);
112
113 // Save configuration for the device to |storage|.
Chris Masone877ff982011-09-21 16:18:24 -0700114 virtual bool Save(StoreInterface *storage);
Chris Masone5dec5f42011-07-22 14:07:55 -0700115
Darin Petkov77cb6812011-08-15 16:19:41 -0700116 void set_dhcp_provider(DHCPProvider *provider) { dhcp_provider_ = provider; }
117
Darin Petkov3cfbf212011-11-21 16:02:09 +0100118 DeviceAdaptorInterface *adaptor() const { return adaptor_.get(); }
119
Paul Stewartd5843772011-05-11 15:40:42 -0700120 protected:
Paul Stewart2bf1d352011-12-06 15:02:55 -0800121 FRIEND_TEST(DeviceTest, AcquireIPConfig);
Darin Petkovafa6fc42011-06-21 16:21:08 -0700122 FRIEND_TEST(DeviceTest, DestroyIPConfig);
123 FRIEND_TEST(DeviceTest, DestroyIPConfigNULL);
Chris Masone95207da2011-06-29 16:50:49 -0700124 FRIEND_TEST(DeviceTest, GetProperties);
Chris Masone5dec5f42011-07-22 14:07:55 -0700125 FRIEND_TEST(DeviceTest, Save);
Paul Stewart03dba0b2011-08-22 16:32:45 -0700126 FRIEND_TEST(DeviceTest, SelectedService);
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700127 FRIEND_TEST(DeviceTest, Stop);
Paul Stewarta41e38d2011-11-11 07:47:29 -0800128 FRIEND_TEST(ManagerTest, DeviceRegistrationAndStart);
Gaurav Shah435de2c2011-11-17 19:01:07 -0800129 FRIEND_TEST(ManagerTest, ConnectedTechnologies);
130 FRIEND_TEST(ManagerTest, DefaultTechnology);
mukesh agrawal32399322011-09-01 10:53:43 -0700131 FRIEND_TEST(WiFiMainTest, Connect);
Darin Petkovafa6fc42011-06-21 16:21:08 -0700132
133 // If there's an IP configuration in |ipconfig_|, releases the IP address and
134 // destroys the configuration instance.
135 void DestroyIPConfig();
136
137 // Creates a new DHCP IP configuration instance, stores it in |ipconfig_| and
138 // requests a new IP configuration. Registers a callback to
139 // IPConfigUpdatedCallback on IP configuration changes. Returns true if the IP
140 // request was successfully sent.
Paul Stewart2bf1d352011-12-06 15:02:55 -0800141 bool AcquireIPConfig();
Darin Petkovafa6fc42011-06-21 16:21:08 -0700142
Paul Stewarte6132022011-08-16 09:11:02 -0700143 // Maintain connection state (Routes, IP Addresses and DNS) in the OS.
144 void CreateConnection();
145
146 // Remove connection state
147 void DestroyConnection();
148
Paul Stewart03dba0b2011-08-22 16:32:45 -0700149 // Selects a service to be "current" -- i.e. link-state or configuration
150 // events that happen to the device are attributed to this service.
151 void SelectService(const ServiceRefPtr &service);
152
153 // Set the state of the selected service
154 void SetServiceState(Service::ConnectState state);
155
156 // Set the failure of the selected service (implicitly sets the state to
157 // "failure")
158 void SetServiceFailure(Service::ConnectFailure failure_state);
159
Chris Masone27c4aa52011-07-02 13:10:14 -0700160 void HelpRegisterDerivedStrings(const std::string &name,
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800161 Strings(Device::*get)(Error *),
mukesh agrawalffa3d042011-10-06 15:26:10 -0700162 void(Device::*set)(const Strings&, Error *));
Chris Masoneb925cc82011-06-22 15:39:57 -0700163
Paul Stewartac4ac002011-08-26 12:04:26 -0700164 // Property getters reserved for subclasses
165 ControlInterface *control_interface() const { return control_interface_; }
Paul Stewartac4ac002011-08-26 12:04:26 -0700166 Manager *manager() const { return manager_; }
Paul Stewarta41e38d2011-11-11 07:47:29 -0800167 bool running() const { return running_; }
Paul Stewartd5843772011-05-11 15:40:42 -0700168
Paul Stewart75897df2011-04-27 09:05:53 -0700169 private:
Chris Masone413a3192011-05-09 17:10:05 -0700170 friend class DeviceAdaptorInterface;
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700171 friend class DeviceTest;
172 friend class CellularTest;
173 friend class WiFiMainTest;
Darin Petkovafa6fc42011-06-21 16:21:08 -0700174
Paul Stewart2bf1d352011-12-06 15:02:55 -0800175 static const char kIPFlagTemplate[];
176 static const char kIPFlagVersion4[];
177 static const char kIPFlagVersion6[];
178 static const char kIPFlagDisableIPv6[];
179 static const char kIPFlagUseTempAddr[];
180 static const char kIPFlagUseTempAddrUsedAndDefault[];
Chris Masone5dec5f42011-07-22 14:07:55 -0700181 static const char kStoragePowered[];
182 static const char kStorageIPConfigs[];
183
Darin Petkovafa6fc42011-06-21 16:21:08 -0700184 // Callback invoked on every IP configuration update.
Chris Masone2b105542011-06-22 10:58:09 -0700185 void IPConfigUpdatedCallback(const IPConfigRefPtr &ipconfig, bool success);
Darin Petkovafa6fc42011-06-21 16:21:08 -0700186
Chris Masone5dec5f42011-07-22 14:07:55 -0700187 // Right now, Devices reference IPConfigs directly when persisted to disk
188 // It's not clear that this makes sense long-term, but that's how it is now.
189 // This call generates a string in the right format for this persisting.
Chris Masone34af2182011-08-22 11:59:36 -0700190 // |suffix| is injected into the storage identifier used for the configs.
191 std::string SerializeIPConfigs(const std::string &suffix);
Chris Masone5dec5f42011-07-22 14:07:55 -0700192
Paul Stewart2bf1d352011-12-06 15:02:55 -0800193 // Set an IP configuration flag on the device. |ip_version| should be
194 // "ipv6" or "ipv4". |flag| should be the name of the flag to be set
195 // and |value| is what this flag should be set to.
196 bool SetIPFlag(IPAddress::Family family, const std::string &flag,
197 const std::string &value);
198
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800199 std::vector<std::string> AvailableIPConfigs(Error *error);
Chris Masone4e851612011-07-01 10:46:53 -0700200 std::string GetRpcConnectionIdentifier();
201
Paul Stewartac4ac002011-08-26 12:04:26 -0700202 // Properties
Paul Stewarta41e38d2011-11-11 07:47:29 -0800203 bool powered_; // indicates whether the device is configured to operate
Paul Stewartac4ac002011-08-26 12:04:26 -0700204 bool reconnect_;
205 const std::string hardware_address_;
206
207 PropertyStore store_;
208
Paul Stewartac4ac002011-08-26 12:04:26 -0700209 const int interface_index_;
Paul Stewarta41e38d2011-11-11 07:47:29 -0800210 bool running_; // indicates whether the device is actually in operation
Paul Stewartac4ac002011-08-26 12:04:26 -0700211 const std::string link_name_;
212 const std::string unique_id_;
213 ControlInterface *control_interface_;
214 EventDispatcher *dispatcher_;
215 Manager *manager_;
216 IPConfigRefPtr ipconfig_;
217 ConnectionRefPtr connection_;
Darin Petkovafa6fc42011-06-21 16:21:08 -0700218 scoped_ptr<DeviceAdaptorInterface> adaptor_;
Gaurav Shah435de2c2011-11-17 19:01:07 -0800219 Technology::Identifier technology_;
Darin Petkovafa6fc42011-06-21 16:21:08 -0700220
Paul Stewart03dba0b2011-08-22 16:32:45 -0700221 // Maintain a reference to the connected / connecting service
222 ServiceRefPtr selected_service_;
223
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700224 // Cache singleton pointers for performance and test purposes.
Darin Petkov77cb6812011-08-15 16:19:41 -0700225 DHCPProvider *dhcp_provider_;
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700226 RTNLHandler *rtnl_handler_;
Darin Petkov77cb6812011-08-15 16:19:41 -0700227
Chris Masone9be4a9d2011-05-16 15:44:09 -0700228 DISALLOW_COPY_AND_ASSIGN(Device);
Paul Stewart75897df2011-04-27 09:05:53 -0700229};
230
Paul Stewart75897df2011-04-27 09:05:53 -0700231} // namespace shill
232
233#endif // SHILL_DEVICE_