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