blob: 7574e4ee9b1446d05ed018f96360601c4f06773d [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
Darin Petkovafa6fc42011-06-21 16:21:08 -070016#include "shill/ipconfig.h"
Chris Masoneb925cc82011-06-22 15:39:57 -070017#include "shill/property_store.h"
Chris Masone2b105542011-06-22 10:58:09 -070018#include "shill/refptr_types.h"
Paul Stewart03dba0b2011-08-22 16:32:45 -070019#include "shill/service.h"
Paul Stewart75897df2011-04-27 09:05:53 -070020#include "shill/shill_event.h"
21
22namespace shill {
23
Chris Masone9be4a9d2011-05-16 15:44:09 -070024class ControlInterface;
Darin Petkov77cb6812011-08-15 16:19:41 -070025class DHCPProvider;
Chris Masone9be4a9d2011-05-16 15:44:09 -070026class DeviceAdaptorInterface;
Paul Stewartf1ce5d22011-05-19 13:10:20 -070027class Endpoint;
Chris Masone8fe2c7e2011-06-09 15:51:19 -070028class Error;
29class EventDispatcher;
Paul Stewartf1ce5d22011-05-19 13:10:20 -070030class Manager;
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -070031class RTNLHandler;
Chris Masone9be4a9d2011-05-16 15:44:09 -070032
Paul Stewart75897df2011-04-27 09:05:53 -070033// Device superclass. Individual network interfaces types will inherit from
34// this class.
Chris Masone27c4aa52011-07-02 13:10:14 -070035class Device : public base::RefCounted<Device> {
Paul Stewart75897df2011-04-27 09:05:53 -070036 public:
Chris Masone9be4a9d2011-05-16 15:44:09 -070037 enum Technology {
38 kEthernet,
39 kWifi,
40 kCellular,
mukesh agrawal8f317b62011-07-15 11:53:23 -070041 kBlacklisted,
Paul Stewartb50f0b92011-05-16 16:31:42 -070042 kUnknown,
Chris Masone9be4a9d2011-05-16 15:44:09 -070043 kNumTechnologies
44 };
Chris Masone0e1d1042011-05-09 18:07:03 -070045
Chris Masone9be4a9d2011-05-16 15:44:09 -070046 // A constructor for the Device object
47 Device(ControlInterface *control_interface,
Paul Stewartb50f0b92011-05-16 16:31:42 -070048 EventDispatcher *dispatcher,
Paul Stewartf1ce5d22011-05-19 13:10:20 -070049 Manager *manager,
Darin Petkovafa6fc42011-06-21 16:21:08 -070050 const std::string &link_name,
Chris Masone626719f2011-08-18 16:58:48 -070051 const std::string &address,
Paul Stewartb50f0b92011-05-16 16:31:42 -070052 int interface_index);
Chris Masone9be4a9d2011-05-16 15:44:09 -070053 virtual ~Device();
54
55 virtual void Start();
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -070056
57 // Clear running state, especially any fields that hold a reference back
58 // to us. After a call to Stop(), the Device may be restarted (with a call
59 // to Start()), or destroyed (if its refcount falls to zero).
Chris Masone9be4a9d2011-05-16 15:44:09 -070060 virtual void Stop();
61
Darin Petkovafa6fc42011-06-21 16:21:08 -070062 // Base method always returns false.
Darin Petkov6f9eaa32011-08-09 15:26:44 -070063 virtual bool TechnologyIs(const Technology 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 Petkove42e1012011-08-31 12:35:04 -070072 virtual void RequirePIN(const std::string &pin, bool require, Error *error);
73 virtual void EnterPIN(const std::string &pin, Error *error);
74 virtual void UnblockPIN(const std::string &unblock_code,
75 const std::string &pin,
76 Error *error);
77 virtual void ChangePIN(const std::string &old_pin,
78 const std::string &new_pin,
79 Error *error);
Darin Petkov9ae310f2011-08-30 15:41:13 -070080
Chris Masone27c4aa52011-07-02 13:10:14 -070081 std::string GetRpcIdentifier();
Chris Masone5dec5f42011-07-22 14:07:55 -070082 std::string GetStorageIdentifier();
Chris Masone8fe2c7e2011-06-09 15:51:19 -070083
Chris Masone9d779932011-08-25 16:33:41 -070084 const std::string &address() const { return hardware_address_; }
Darin Petkove0a312e2011-07-20 13:45:28 -070085 const std::string &link_name() const { return link_name_; }
86 int interface_index() const { return interface_index_; }
Paul Stewarte6132022011-08-16 09:11:02 -070087 const ConnectionRefPtr &connection() const { return connection_; }
Darin Petkove0a312e2011-07-20 13:45:28 -070088
Chris Masone19e30402011-07-19 15:48:47 -070089 const std::string &FriendlyName() const;
Chris Masone27c4aa52011-07-02 13:10:14 -070090
Darin Petkovafa6fc42011-06-21 16:21:08 -070091 // Returns a string that is guaranteed to uniquely identify this Device
92 // instance.
93 const std::string &UniqueName() const;
Chris Masonea82b7112011-05-25 15:16:29 -070094
mukesh agrawalde29fa82011-09-16 16:16:36 -070095 PropertyStore *mutable_store() { return &store_; }
96 const PropertyStore &store() const { return store_; }
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -070097 RTNLHandler *rtnl_handler() { return rtnl_handler_; }
Chris Masone19e30402011-07-19 15:48:47 -070098
Chris Masone5dec5f42011-07-22 14:07:55 -070099 bool Load(StoreInterface *storage);
Chris Masone877ff982011-09-21 16:18:24 -0700100 virtual bool Save(StoreInterface *storage);
Chris Masone5dec5f42011-07-22 14:07:55 -0700101
Darin Petkov77cb6812011-08-15 16:19:41 -0700102 void set_dhcp_provider(DHCPProvider *provider) { dhcp_provider_ = provider; }
103
Paul Stewartd5843772011-05-11 15:40:42 -0700104 protected:
Darin Petkovafa6fc42011-06-21 16:21:08 -0700105 FRIEND_TEST(DeviceTest, AcquireDHCPConfig);
106 FRIEND_TEST(DeviceTest, DestroyIPConfig);
107 FRIEND_TEST(DeviceTest, DestroyIPConfigNULL);
Chris Masone95207da2011-06-29 16:50:49 -0700108 FRIEND_TEST(DeviceTest, GetProperties);
Chris Masone5dec5f42011-07-22 14:07:55 -0700109 FRIEND_TEST(DeviceTest, Save);
Paul Stewart03dba0b2011-08-22 16:32:45 -0700110 FRIEND_TEST(DeviceTest, SelectedService);
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700111 FRIEND_TEST(DeviceTest, Stop);
mukesh agrawal32399322011-09-01 10:53:43 -0700112 FRIEND_TEST(WiFiMainTest, Connect);
Darin Petkovafa6fc42011-06-21 16:21:08 -0700113
114 // If there's an IP configuration in |ipconfig_|, releases the IP address and
115 // destroys the configuration instance.
116 void DestroyIPConfig();
117
118 // Creates a new DHCP IP configuration instance, stores it in |ipconfig_| and
119 // requests a new IP configuration. Registers a callback to
120 // IPConfigUpdatedCallback on IP configuration changes. Returns true if the IP
121 // request was successfully sent.
122 bool AcquireDHCPConfig();
123
Paul Stewarte6132022011-08-16 09:11:02 -0700124 // Maintain connection state (Routes, IP Addresses and DNS) in the OS.
125 void CreateConnection();
126
127 // Remove connection state
128 void DestroyConnection();
129
Paul Stewart03dba0b2011-08-22 16:32:45 -0700130 // Selects a service to be "current" -- i.e. link-state or configuration
131 // events that happen to the device are attributed to this service.
132 void SelectService(const ServiceRefPtr &service);
133
134 // Set the state of the selected service
135 void SetServiceState(Service::ConnectState state);
136
137 // Set the failure of the selected service (implicitly sets the state to
138 // "failure")
139 void SetServiceFailure(Service::ConnectFailure failure_state);
140
Chris Masone27c4aa52011-07-02 13:10:14 -0700141 void HelpRegisterDerivedStrings(const std::string &name,
Chris Masone889666b2011-07-03 12:58:50 -0700142 Strings(Device::*get)(void),
143 bool(Device::*set)(const Strings&));
Chris Masoneb925cc82011-06-22 15:39:57 -0700144
Paul Stewartac4ac002011-08-26 12:04:26 -0700145 // Property getters reserved for subclasses
146 ControlInterface *control_interface() const { return control_interface_; }
147 EventDispatcher *dispatcher() const { return dispatcher_; }
148 Manager *manager() const { return manager_; }
149 std::vector<ServiceRefPtr> *services() { return &services_; }
Paul Stewartd5843772011-05-11 15:40:42 -0700150
Paul Stewart75897df2011-04-27 09:05:53 -0700151 private:
Chris Masone413a3192011-05-09 17:10:05 -0700152 friend class DeviceAdaptorInterface;
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700153 friend class DeviceTest;
154 friend class CellularTest;
155 friend class WiFiMainTest;
Darin Petkovafa6fc42011-06-21 16:21:08 -0700156
Chris Masone5dec5f42011-07-22 14:07:55 -0700157 static const char kStoragePowered[];
158 static const char kStorageIPConfigs[];
159
Darin Petkovafa6fc42011-06-21 16:21:08 -0700160 // Callback invoked on every IP configuration update.
Chris Masone2b105542011-06-22 10:58:09 -0700161 void IPConfigUpdatedCallback(const IPConfigRefPtr &ipconfig, bool success);
Darin Petkovafa6fc42011-06-21 16:21:08 -0700162
Chris Masone5dec5f42011-07-22 14:07:55 -0700163 // Right now, Devices reference IPConfigs directly when persisted to disk
164 // It's not clear that this makes sense long-term, but that's how it is now.
165 // This call generates a string in the right format for this persisting.
Chris Masone34af2182011-08-22 11:59:36 -0700166 // |suffix| is injected into the storage identifier used for the configs.
167 std::string SerializeIPConfigs(const std::string &suffix);
Chris Masone5dec5f42011-07-22 14:07:55 -0700168
Chris Masone4e851612011-07-01 10:46:53 -0700169 std::vector<std::string> AvailableIPConfigs();
170 std::string GetRpcConnectionIdentifier();
171
Paul Stewartac4ac002011-08-26 12:04:26 -0700172 // Properties
173 bool powered_; // TODO(pstew): Is this what |running_| is for?
174 bool reconnect_;
175 const std::string hardware_address_;
176
177 PropertyStore store_;
178
179 std::vector<ServiceRefPtr> services_;
180 const int interface_index_;
181 bool running_;
182 const std::string link_name_;
183 const std::string unique_id_;
184 ControlInterface *control_interface_;
185 EventDispatcher *dispatcher_;
186 Manager *manager_;
187 IPConfigRefPtr ipconfig_;
188 ConnectionRefPtr connection_;
Darin Petkovafa6fc42011-06-21 16:21:08 -0700189 scoped_ptr<DeviceAdaptorInterface> adaptor_;
190
Paul Stewart03dba0b2011-08-22 16:32:45 -0700191 // Maintain a reference to the connected / connecting service
192 ServiceRefPtr selected_service_;
193
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700194 // Cache singleton pointers for performance and test purposes.
Darin Petkov77cb6812011-08-15 16:19:41 -0700195 DHCPProvider *dhcp_provider_;
mukesh agrawal5c4dd0b2011-09-14 13:53:14 -0700196 RTNLHandler *rtnl_handler_;
Darin Petkov77cb6812011-08-15 16:19:41 -0700197
Chris Masone9be4a9d2011-05-16 15:44:09 -0700198 DISALLOW_COPY_AND_ASSIGN(Device);
Paul Stewart75897df2011-04-27 09:05:53 -0700199};
200
Paul Stewart75897df2011-04-27 09:05:53 -0700201} // namespace shill
202
203#endif // SHILL_DEVICE_