Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 1 | // 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 Masone | 46eaaf5 | 2011-05-24 13:08:30 -0700 | [diff] [blame] | 8 | #include <string> |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 9 | #include <vector> |
| 10 | |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 11 | #include <base/basictypes.h> |
Chris Masone | 487b8bf | 2011-05-13 16:27:57 -0700 | [diff] [blame] | 12 | #include <base/memory/ref_counted.h> |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 13 | #include <base/memory/scoped_ptr.h> |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 14 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
Paul Stewart | d584377 | 2011-05-11 15:40:42 -0700 | [diff] [blame] | 15 | |
Paul Stewart | 26b327e | 2011-10-19 11:38:09 -0700 | [diff] [blame] | 16 | #include "shill/event_dispatcher.h" |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 17 | #include "shill/ipconfig.h" |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 18 | #include "shill/property_store.h" |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 19 | #include "shill/refptr_types.h" |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 20 | #include "shill/service.h" |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 21 | #include "shill/technology.h" |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 22 | |
| 23 | namespace shill { |
| 24 | |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 25 | class ControlInterface; |
Darin Petkov | 77cb681 | 2011-08-15 16:19:41 -0700 | [diff] [blame] | 26 | class DHCPProvider; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 27 | class DeviceAdaptorInterface; |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 28 | class Endpoint; |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 29 | class Error; |
| 30 | class EventDispatcher; |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 31 | class Manager; |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 32 | class RTNLHandler; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 33 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 34 | // Device superclass. Individual network interfaces types will inherit from |
| 35 | // this class. |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 36 | class Device : public base::RefCounted<Device> { |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 37 | public: |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 38 | // A constructor for the Device object |
| 39 | Device(ControlInterface *control_interface, |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 40 | EventDispatcher *dispatcher, |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 41 | Manager *manager, |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 42 | const std::string &link_name, |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 43 | const std::string &address, |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 44 | int interface_index); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 45 | virtual ~Device(); |
| 46 | |
| 47 | virtual void Start(); |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 48 | |
| 49 | // Clear running state, especially any fields that hold a reference back |
| 50 | // to us. After a call to Stop(), the Device may be restarted (with a call |
| 51 | // to Start()), or destroyed (if its refcount falls to zero). |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 52 | virtual void Stop(); |
| 53 | |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 54 | // Base method always returns false. |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 55 | virtual bool TechnologyIs(const Technology::Identifier type) const; |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 56 | |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 57 | virtual void LinkEvent(unsigned flags, unsigned change); |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 58 | |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 59 | virtual void ConfigIP() {} |
| 60 | |
Darin Petkov | 9ae310f | 2011-08-30 15:41:13 -0700 | [diff] [blame] | 61 | // The default implementation sets |error| to kNotSupported. |
Darin Petkov | c086531 | 2011-09-16 15:31:20 -0700 | [diff] [blame] | 62 | virtual void Scan(Error *error); |
Darin Petkov | 9ae310f | 2011-08-30 15:41:13 -0700 | [diff] [blame] | 63 | virtual void RegisterOnNetwork(const std::string &network_id, Error *error); |
Darin Petkov | e42e101 | 2011-08-31 12:35:04 -0700 | [diff] [blame] | 64 | virtual void RequirePIN(const std::string &pin, bool require, Error *error); |
| 65 | virtual void EnterPIN(const std::string &pin, Error *error); |
| 66 | virtual void UnblockPIN(const std::string &unblock_code, |
| 67 | const std::string &pin, |
| 68 | Error *error); |
| 69 | virtual void ChangePIN(const std::string &old_pin, |
| 70 | const std::string &new_pin, |
| 71 | Error *error); |
Darin Petkov | 9ae310f | 2011-08-30 15:41:13 -0700 | [diff] [blame] | 72 | |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 73 | std::string GetRpcIdentifier(); |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 74 | std::string GetStorageIdentifier(); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 75 | |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 76 | const std::string &address() const { return hardware_address_; } |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 77 | const std::string &link_name() const { return link_name_; } |
| 78 | int interface_index() const { return interface_index_; } |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 79 | const ConnectionRefPtr &connection() const { return connection_; } |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 80 | bool powered() const { return powered_; } |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 81 | |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 82 | const std::string &FriendlyName() const; |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 83 | |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 84 | // Returns a string that is guaranteed to uniquely identify this Device |
| 85 | // instance. |
| 86 | const std::string &UniqueName() const; |
Chris Masone | a82b711 | 2011-05-25 15:16:29 -0700 | [diff] [blame] | 87 | |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 88 | PropertyStore *mutable_store() { return &store_; } |
| 89 | const PropertyStore &store() const { return store_; } |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 90 | RTNLHandler *rtnl_handler() { return rtnl_handler_; } |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 91 | |
Darin Petkov | b05315f | 2011-11-07 10:14:25 +0100 | [diff] [blame] | 92 | EventDispatcher *dispatcher() const { return dispatcher_; } |
| 93 | |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 94 | // Load configuration for the device from |storage|. This may include |
| 95 | // instantiating non-visible services for which configuration has been |
| 96 | // stored. |
| 97 | virtual bool Load(StoreInterface *storage); |
| 98 | |
| 99 | // Save configuration for the device to |storage|. |
Chris Masone | 877ff98 | 2011-09-21 16:18:24 -0700 | [diff] [blame] | 100 | virtual bool Save(StoreInterface *storage); |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 101 | |
Darin Petkov | 77cb681 | 2011-08-15 16:19:41 -0700 | [diff] [blame] | 102 | void set_dhcp_provider(DHCPProvider *provider) { dhcp_provider_ = provider; } |
| 103 | |
Darin Petkov | 3cfbf21 | 2011-11-21 16:02:09 +0100 | [diff] [blame] | 104 | DeviceAdaptorInterface *adaptor() const { return adaptor_.get(); } |
| 105 | |
Paul Stewart | d584377 | 2011-05-11 15:40:42 -0700 | [diff] [blame] | 106 | protected: |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 107 | FRIEND_TEST(DeviceTest, AcquireDHCPConfig); |
| 108 | FRIEND_TEST(DeviceTest, DestroyIPConfig); |
| 109 | FRIEND_TEST(DeviceTest, DestroyIPConfigNULL); |
Chris Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame] | 110 | FRIEND_TEST(DeviceTest, GetProperties); |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 111 | FRIEND_TEST(DeviceTest, Save); |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 112 | FRIEND_TEST(DeviceTest, SelectedService); |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 113 | FRIEND_TEST(DeviceTest, Stop); |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 114 | FRIEND_TEST(ManagerTest, DeviceRegistrationAndStart); |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 115 | FRIEND_TEST(WiFiMainTest, Connect); |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 116 | |
| 117 | // If there's an IP configuration in |ipconfig_|, releases the IP address and |
| 118 | // destroys the configuration instance. |
| 119 | void DestroyIPConfig(); |
| 120 | |
| 121 | // Creates a new DHCP IP configuration instance, stores it in |ipconfig_| and |
| 122 | // requests a new IP configuration. Registers a callback to |
| 123 | // IPConfigUpdatedCallback on IP configuration changes. Returns true if the IP |
| 124 | // request was successfully sent. |
| 125 | bool AcquireDHCPConfig(); |
| 126 | |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 127 | // Maintain connection state (Routes, IP Addresses and DNS) in the OS. |
| 128 | void CreateConnection(); |
| 129 | |
| 130 | // Remove connection state |
| 131 | void DestroyConnection(); |
| 132 | |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 133 | // Selects a service to be "current" -- i.e. link-state or configuration |
| 134 | // events that happen to the device are attributed to this service. |
| 135 | void SelectService(const ServiceRefPtr &service); |
| 136 | |
| 137 | // Set the state of the selected service |
| 138 | void SetServiceState(Service::ConnectState state); |
| 139 | |
| 140 | // Set the failure of the selected service (implicitly sets the state to |
| 141 | // "failure") |
| 142 | void SetServiceFailure(Service::ConnectFailure failure_state); |
| 143 | |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 144 | void HelpRegisterDerivedStrings(const std::string &name, |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 145 | Strings(Device::*get)(Error *), |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 146 | void(Device::*set)(const Strings&, Error *)); |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 147 | |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 148 | // Property getters reserved for subclasses |
| 149 | ControlInterface *control_interface() const { return control_interface_; } |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 150 | Manager *manager() const { return manager_; } |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 151 | bool running() const { return running_; } |
Paul Stewart | d584377 | 2011-05-11 15:40:42 -0700 | [diff] [blame] | 152 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 153 | private: |
Chris Masone | 413a319 | 2011-05-09 17:10:05 -0700 | [diff] [blame] | 154 | friend class DeviceAdaptorInterface; |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 155 | friend class DeviceTest; |
| 156 | friend class CellularTest; |
| 157 | friend class WiFiMainTest; |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 158 | |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 159 | static const char kStoragePowered[]; |
| 160 | static const char kStorageIPConfigs[]; |
| 161 | |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 162 | // Callback invoked on every IP configuration update. |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 163 | void IPConfigUpdatedCallback(const IPConfigRefPtr &ipconfig, bool success); |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 164 | |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 165 | // Right now, Devices reference IPConfigs directly when persisted to disk |
| 166 | // It's not clear that this makes sense long-term, but that's how it is now. |
| 167 | // This call generates a string in the right format for this persisting. |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 168 | // |suffix| is injected into the storage identifier used for the configs. |
| 169 | std::string SerializeIPConfigs(const std::string &suffix); |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 170 | |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 171 | std::vector<std::string> AvailableIPConfigs(Error *error); |
Chris Masone | 4e85161 | 2011-07-01 10:46:53 -0700 | [diff] [blame] | 172 | std::string GetRpcConnectionIdentifier(); |
| 173 | |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 174 | // Properties |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 175 | bool powered_; // indicates whether the device is configured to operate |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 176 | bool reconnect_; |
| 177 | const std::string hardware_address_; |
| 178 | |
| 179 | PropertyStore store_; |
| 180 | |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 181 | const int interface_index_; |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 182 | bool running_; // indicates whether the device is actually in operation |
Paul Stewart | ac4ac00 | 2011-08-26 12:04:26 -0700 | [diff] [blame] | 183 | const std::string link_name_; |
| 184 | const std::string unique_id_; |
| 185 | ControlInterface *control_interface_; |
| 186 | EventDispatcher *dispatcher_; |
| 187 | Manager *manager_; |
| 188 | IPConfigRefPtr ipconfig_; |
| 189 | ConnectionRefPtr connection_; |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 190 | scoped_ptr<DeviceAdaptorInterface> adaptor_; |
| 191 | |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 192 | // Maintain a reference to the connected / connecting service |
| 193 | ServiceRefPtr selected_service_; |
| 194 | |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 195 | // Cache singleton pointers for performance and test purposes. |
Darin Petkov | 77cb681 | 2011-08-15 16:19:41 -0700 | [diff] [blame] | 196 | DHCPProvider *dhcp_provider_; |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 197 | RTNLHandler *rtnl_handler_; |
Darin Petkov | 77cb681 | 2011-08-15 16:19:41 -0700 | [diff] [blame] | 198 | |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 199 | DISALLOW_COPY_AND_ASSIGN(Device); |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 200 | }; |
| 201 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 202 | } // namespace shill |
| 203 | |
| 204 | #endif // SHILL_DEVICE_ |