blob: 299b0c6188c8ef604f739fc612f4e91e84076284 [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"
Darin Petkovafa6fc42011-06-21 16:21:08 -070017#include "shill/ipconfig.h"
Chris Masoneb925cc82011-06-22 15:39:57 -070018#include "shill/property_store.h"
Chris Masone2b105542011-06-22 10:58:09 -070019#include "shill/refptr_types.h"
Paul Stewart03dba0b2011-08-22 16:32:45 -070020#include "shill/service.h"
Paul Stewartfdd16072011-09-16 12:41:35 -070021#include "shill/technology.h"
Paul Stewart75897df2011-04-27 09:05:53 -070022
23namespace shill {
24
Chris Masone9be4a9d2011-05-16 15:44:09 -070025class ControlInterface;
Darin Petkov77cb6812011-08-15 16:19:41 -070026class DHCPProvider;
Chris Masone9be4a9d2011-05-16 15:44:09 -070027class DeviceAdaptorInterface;
Paul Stewartf1ce5d22011-05-19 13:10:20 -070028class Endpoint;
Chris Masone8fe2c7e2011-06-09 15:51:19 -070029class Error;
30class EventDispatcher;
Paul Stewartf1ce5d22011-05-19 13:10:20 -070031class Manager;
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -070032class RTNLHandler;
Chris Masone9be4a9d2011-05-16 15:44:09 -070033
Paul Stewart75897df2011-04-27 09:05:53 -070034// Device superclass. Individual network interfaces types will inherit from
35// this class.
Chris Masone27c4aa52011-07-02 13:10:14 -070036class Device : public base::RefCounted<Device> {
Paul Stewart75897df2011-04-27 09:05:53 -070037 public:
Chris Masone9be4a9d2011-05-16 15:44:09 -070038 // A constructor for the Device object
39 Device(ControlInterface *control_interface,
Paul Stewartb50f0b92011-05-16 16:31:42 -070040 EventDispatcher *dispatcher,
Paul Stewartf1ce5d22011-05-19 13:10:20 -070041 Manager *manager,
Darin Petkovafa6fc42011-06-21 16:21:08 -070042 const std::string &link_name,
Chris Masone626719f2011-08-18 16:58:48 -070043 const std::string &address,
Gaurav Shah435de2c2011-11-17 19:01:07 -080044 int interface_index,
45 Technology::Identifier technology);
Chris Masone9be4a9d2011-05-16 15:44:09 -070046 virtual ~Device();
47
48 virtual void Start();
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -070049
50 // Clear running state, especially any fields that hold a reference back
51 // to us. After a call to Stop(), the Device may be restarted (with a call
52 // to Start()), or destroyed (if its refcount falls to zero).
Chris Masone9be4a9d2011-05-16 15:44:09 -070053 virtual void Stop();
54
Gaurav Shah435de2c2011-11-17 19:01:07 -080055
56 // TODO(gauravsh): We do not really need this since technology() can be used
57 // to get a device's technology for direct comparison.
Darin Petkovafa6fc42011-06-21 16:21:08 -070058 // Base method always returns false.
Paul Stewartfdd16072011-09-16 12:41:35 -070059 virtual bool TechnologyIs(const Technology::Identifier type) const;
Darin Petkovafa6fc42011-06-21 16:21:08 -070060
Paul Stewartf1ce5d22011-05-19 13:10:20 -070061 virtual void LinkEvent(unsigned flags, unsigned change);
Paul Stewart75897df2011-04-27 09:05:53 -070062
Chris Masonec1e50412011-06-07 13:04:53 -070063 virtual void ConfigIP() {}
64
Darin Petkov9ae310f2011-08-30 15:41:13 -070065 // The default implementation sets |error| to kNotSupported.
Darin Petkovc0865312011-09-16 15:31:20 -070066 virtual void Scan(Error *error);
Darin Petkov9ae310f2011-08-30 15:41:13 -070067 virtual void RegisterOnNetwork(const std::string &network_id, Error *error);
Darin Petkove42e1012011-08-31 12:35:04 -070068 virtual void RequirePIN(const std::string &pin, bool require, Error *error);
69 virtual void EnterPIN(const std::string &pin, Error *error);
70 virtual void UnblockPIN(const std::string &unblock_code,
71 const std::string &pin,
72 Error *error);
73 virtual void ChangePIN(const std::string &old_pin,
74 const std::string &new_pin,
75 Error *error);
Darin Petkov9ae310f2011-08-30 15:41:13 -070076
Gaurav Shah435de2c2011-11-17 19:01:07 -080077 // Returns true if the selected service on the device (if any) is connected.
78 // Returns false if there is no selected service, or if the selected service
79 // is not connected.
80 bool IsConnected() const;
81
Chris Masone27c4aa52011-07-02 13:10:14 -070082 std::string GetRpcIdentifier();
Chris Masone5dec5f42011-07-22 14:07:55 -070083 std::string GetStorageIdentifier();
Chris Masone8fe2c7e2011-06-09 15:51:19 -070084
Chris Masone9d779932011-08-25 16:33:41 -070085 const std::string &address() const { return hardware_address_; }
Darin Petkove0a312e2011-07-20 13:45:28 -070086 const std::string &link_name() const { return link_name_; }
87 int interface_index() const { return interface_index_; }
Paul Stewarte6132022011-08-16 09:11:02 -070088 const ConnectionRefPtr &connection() const { return connection_; }
Paul Stewarta41e38d2011-11-11 07:47:29 -080089 bool powered() const { return powered_; }
Gaurav Shah435de2c2011-11-17 19:01:07 -080090 virtual Technology::Identifier technology() const { return technology_; }
Darin Petkove0a312e2011-07-20 13:45:28 -070091
Chris Masone19e30402011-07-19 15:48:47 -070092 const std::string &FriendlyName() const;
Chris Masone27c4aa52011-07-02 13:10:14 -070093
Darin Petkovafa6fc42011-06-21 16:21:08 -070094 // Returns a string that is guaranteed to uniquely identify this Device
95 // instance.
96 const std::string &UniqueName() const;
Chris Masonea82b7112011-05-25 15:16:29 -070097
mukesh agrawalde29fa82011-09-16 16:16:36 -070098 PropertyStore *mutable_store() { return &store_; }
99 const PropertyStore &store() const { return store_; }
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700100 RTNLHandler *rtnl_handler() { return rtnl_handler_; }
Chris Masone19e30402011-07-19 15:48:47 -0700101
Darin Petkovb05315f2011-11-07 10:14:25 +0100102 EventDispatcher *dispatcher() const { return dispatcher_; }
103
Paul Stewarta41e38d2011-11-11 07:47:29 -0800104 // Load configuration for the device from |storage|. This may include
105 // instantiating non-visible services for which configuration has been
106 // stored.
107 virtual bool Load(StoreInterface *storage);
108
109 // Save configuration for the device to |storage|.
Chris Masone877ff982011-09-21 16:18:24 -0700110 virtual bool Save(StoreInterface *storage);
Chris Masone5dec5f42011-07-22 14:07:55 -0700111
Darin Petkov77cb6812011-08-15 16:19:41 -0700112 void set_dhcp_provider(DHCPProvider *provider) { dhcp_provider_ = provider; }
113
Darin Petkov3cfbf212011-11-21 16:02:09 +0100114 DeviceAdaptorInterface *adaptor() const { return adaptor_.get(); }
115
Paul Stewartd5843772011-05-11 15:40:42 -0700116 protected:
Darin Petkovafa6fc42011-06-21 16:21:08 -0700117 FRIEND_TEST(DeviceTest, AcquireDHCPConfig);
118 FRIEND_TEST(DeviceTest, DestroyIPConfig);
119 FRIEND_TEST(DeviceTest, DestroyIPConfigNULL);
Chris Masone95207da2011-06-29 16:50:49 -0700120 FRIEND_TEST(DeviceTest, GetProperties);
Chris Masone5dec5f42011-07-22 14:07:55 -0700121 FRIEND_TEST(DeviceTest, Save);
Paul Stewart03dba0b2011-08-22 16:32:45 -0700122 FRIEND_TEST(DeviceTest, SelectedService);
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700123 FRIEND_TEST(DeviceTest, Stop);
Paul Stewarta41e38d2011-11-11 07:47:29 -0800124 FRIEND_TEST(ManagerTest, DeviceRegistrationAndStart);
Gaurav Shah435de2c2011-11-17 19:01:07 -0800125 FRIEND_TEST(ManagerTest, ConnectedTechnologies);
126 FRIEND_TEST(ManagerTest, DefaultTechnology);
mukesh agrawal32399322011-09-01 10:53:43 -0700127 FRIEND_TEST(WiFiMainTest, Connect);
Darin Petkovafa6fc42011-06-21 16:21:08 -0700128
129 // If there's an IP configuration in |ipconfig_|, releases the IP address and
130 // destroys the configuration instance.
131 void DestroyIPConfig();
132
133 // Creates a new DHCP IP configuration instance, stores it in |ipconfig_| and
134 // requests a new IP configuration. Registers a callback to
135 // IPConfigUpdatedCallback on IP configuration changes. Returns true if the IP
136 // request was successfully sent.
137 bool AcquireDHCPConfig();
138
Paul Stewarte6132022011-08-16 09:11:02 -0700139 // Maintain connection state (Routes, IP Addresses and DNS) in the OS.
140 void CreateConnection();
141
142 // Remove connection state
143 void DestroyConnection();
144
Paul Stewart03dba0b2011-08-22 16:32:45 -0700145 // Selects a service to be "current" -- i.e. link-state or configuration
146 // events that happen to the device are attributed to this service.
147 void SelectService(const ServiceRefPtr &service);
148
149 // Set the state of the selected service
150 void SetServiceState(Service::ConnectState state);
151
152 // Set the failure of the selected service (implicitly sets the state to
153 // "failure")
154 void SetServiceFailure(Service::ConnectFailure failure_state);
155
Chris Masone27c4aa52011-07-02 13:10:14 -0700156 void HelpRegisterDerivedStrings(const std::string &name,
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800157 Strings(Device::*get)(Error *),
mukesh agrawalffa3d042011-10-06 15:26:10 -0700158 void(Device::*set)(const Strings&, Error *));
Chris Masoneb925cc82011-06-22 15:39:57 -0700159
Paul Stewartac4ac002011-08-26 12:04:26 -0700160 // Property getters reserved for subclasses
161 ControlInterface *control_interface() const { return control_interface_; }
Paul Stewartac4ac002011-08-26 12:04:26 -0700162 Manager *manager() const { return manager_; }
Paul Stewarta41e38d2011-11-11 07:47:29 -0800163 bool running() const { return running_; }
Paul Stewartd5843772011-05-11 15:40:42 -0700164
Paul Stewart75897df2011-04-27 09:05:53 -0700165 private:
Chris Masone413a3192011-05-09 17:10:05 -0700166 friend class DeviceAdaptorInterface;
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700167 friend class DeviceTest;
168 friend class CellularTest;
169 friend class WiFiMainTest;
Darin Petkovafa6fc42011-06-21 16:21:08 -0700170
Chris Masone5dec5f42011-07-22 14:07:55 -0700171 static const char kStoragePowered[];
172 static const char kStorageIPConfigs[];
173
Darin Petkovafa6fc42011-06-21 16:21:08 -0700174 // Callback invoked on every IP configuration update.
Chris Masone2b105542011-06-22 10:58:09 -0700175 void IPConfigUpdatedCallback(const IPConfigRefPtr &ipconfig, bool success);
Darin Petkovafa6fc42011-06-21 16:21:08 -0700176
Chris Masone5dec5f42011-07-22 14:07:55 -0700177 // Right now, Devices reference IPConfigs directly when persisted to disk
178 // It's not clear that this makes sense long-term, but that's how it is now.
179 // This call generates a string in the right format for this persisting.
Chris Masone34af2182011-08-22 11:59:36 -0700180 // |suffix| is injected into the storage identifier used for the configs.
181 std::string SerializeIPConfigs(const std::string &suffix);
Chris Masone5dec5f42011-07-22 14:07:55 -0700182
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800183 std::vector<std::string> AvailableIPConfigs(Error *error);
Chris Masone4e851612011-07-01 10:46:53 -0700184 std::string GetRpcConnectionIdentifier();
185
Paul Stewartac4ac002011-08-26 12:04:26 -0700186 // Properties
Paul Stewarta41e38d2011-11-11 07:47:29 -0800187 bool powered_; // indicates whether the device is configured to operate
Paul Stewartac4ac002011-08-26 12:04:26 -0700188 bool reconnect_;
189 const std::string hardware_address_;
190
191 PropertyStore store_;
192
Paul Stewartac4ac002011-08-26 12:04:26 -0700193 const int interface_index_;
Paul Stewarta41e38d2011-11-11 07:47:29 -0800194 bool running_; // indicates whether the device is actually in operation
Paul Stewartac4ac002011-08-26 12:04:26 -0700195 const std::string link_name_;
196 const std::string unique_id_;
197 ControlInterface *control_interface_;
198 EventDispatcher *dispatcher_;
199 Manager *manager_;
200 IPConfigRefPtr ipconfig_;
201 ConnectionRefPtr connection_;
Darin Petkovafa6fc42011-06-21 16:21:08 -0700202 scoped_ptr<DeviceAdaptorInterface> adaptor_;
Gaurav Shah435de2c2011-11-17 19:01:07 -0800203 Technology::Identifier technology_;
Darin Petkovafa6fc42011-06-21 16:21:08 -0700204
Paul Stewart03dba0b2011-08-22 16:32:45 -0700205 // Maintain a reference to the connected / connecting service
206 ServiceRefPtr selected_service_;
207
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700208 // Cache singleton pointers for performance and test purposes.
Darin Petkov77cb6812011-08-15 16:19:41 -0700209 DHCPProvider *dhcp_provider_;
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700210 RTNLHandler *rtnl_handler_;
Darin Petkov77cb6812011-08-15 16:19:41 -0700211
Chris Masone9be4a9d2011-05-16 15:44:09 -0700212 DISALLOW_COPY_AND_ASSIGN(Device);
Paul Stewart75897df2011-04-27 09:05:53 -0700213};
214
Paul Stewart75897df2011-04-27 09:05:53 -0700215} // namespace shill
216
217#endif // SHILL_DEVICE_