blob: b876b48a7aca6f3e0a77151c0aa2e7f8162ab70a [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;
Chris Masone9be4a9d2011-05-16 15:44:09 -070031
Paul Stewart75897df2011-04-27 09:05:53 -070032// Device superclass. Individual network interfaces types will inherit from
33// this class.
Chris Masone27c4aa52011-07-02 13:10:14 -070034class Device : public base::RefCounted<Device> {
Paul Stewart75897df2011-04-27 09:05:53 -070035 public:
Chris Masone9be4a9d2011-05-16 15:44:09 -070036 enum Technology {
37 kEthernet,
38 kWifi,
39 kCellular,
mukesh agrawal8f317b62011-07-15 11:53:23 -070040 kBlacklisted,
Paul Stewartb50f0b92011-05-16 16:31:42 -070041 kUnknown,
Chris Masone9be4a9d2011-05-16 15:44:09 -070042 kNumTechnologies
43 };
Chris Masone0e1d1042011-05-09 18:07:03 -070044
Chris Masone9be4a9d2011-05-16 15:44:09 -070045 // A constructor for the Device object
46 Device(ControlInterface *control_interface,
Paul Stewartb50f0b92011-05-16 16:31:42 -070047 EventDispatcher *dispatcher,
Paul Stewartf1ce5d22011-05-19 13:10:20 -070048 Manager *manager,
Darin Petkovafa6fc42011-06-21 16:21:08 -070049 const std::string &link_name,
Chris Masone626719f2011-08-18 16:58:48 -070050 const std::string &address,
Paul Stewartb50f0b92011-05-16 16:31:42 -070051 int interface_index);
Chris Masone9be4a9d2011-05-16 15:44:09 -070052 virtual ~Device();
53
54 virtual void Start();
55 virtual void Stop();
56
Darin Petkovafa6fc42011-06-21 16:21:08 -070057 // Base method always returns false.
Darin Petkov6f9eaa32011-08-09 15:26:44 -070058 virtual bool TechnologyIs(const Technology type) const;
Darin Petkovafa6fc42011-06-21 16:21:08 -070059
Paul Stewartf1ce5d22011-05-19 13:10:20 -070060 virtual void LinkEvent(unsigned flags, unsigned change);
61 virtual void Scan();
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.
66 virtual void RegisterOnNetwork(const std::string &network_id, Error *error);
67
Chris Masone27c4aa52011-07-02 13:10:14 -070068 std::string GetRpcIdentifier();
Chris Masone5dec5f42011-07-22 14:07:55 -070069 std::string GetStorageIdentifier();
Chris Masone8fe2c7e2011-06-09 15:51:19 -070070
Darin Petkove0a312e2011-07-20 13:45:28 -070071 const std::string &link_name() const { return link_name_; }
72 int interface_index() const { return interface_index_; }
Paul Stewarte6132022011-08-16 09:11:02 -070073 const ConnectionRefPtr &connection() const { return connection_; }
Darin Petkove0a312e2011-07-20 13:45:28 -070074
Chris Masone19e30402011-07-19 15:48:47 -070075 const std::string &FriendlyName() const;
Chris Masone27c4aa52011-07-02 13:10:14 -070076
Darin Petkovafa6fc42011-06-21 16:21:08 -070077 // Returns a string that is guaranteed to uniquely identify this Device
78 // instance.
79 const std::string &UniqueName() const;
Chris Masonea82b7112011-05-25 15:16:29 -070080
Chris Masone19e30402011-07-19 15:48:47 -070081 PropertyStore *store() { return &store_; }
82
Chris Masone5dec5f42011-07-22 14:07:55 -070083 bool Load(StoreInterface *storage);
84 bool Save(StoreInterface *storage);
85
Darin Petkov77cb6812011-08-15 16:19:41 -070086 void set_dhcp_provider(DHCPProvider *provider) { dhcp_provider_ = provider; }
87
Paul Stewartd5843772011-05-11 15:40:42 -070088 protected:
Darin Petkovafa6fc42011-06-21 16:21:08 -070089 FRIEND_TEST(DeviceTest, AcquireDHCPConfig);
90 FRIEND_TEST(DeviceTest, DestroyIPConfig);
91 FRIEND_TEST(DeviceTest, DestroyIPConfigNULL);
Chris Masone95207da2011-06-29 16:50:49 -070092 FRIEND_TEST(DeviceTest, GetProperties);
Chris Masone5dec5f42011-07-22 14:07:55 -070093 FRIEND_TEST(DeviceTest, Save);
Paul Stewart03dba0b2011-08-22 16:32:45 -070094 FRIEND_TEST(DeviceTest, SelectedService);
Darin Petkovafa6fc42011-06-21 16:21:08 -070095
96 // If there's an IP configuration in |ipconfig_|, releases the IP address and
97 // destroys the configuration instance.
98 void DestroyIPConfig();
99
100 // Creates a new DHCP IP configuration instance, stores it in |ipconfig_| and
101 // requests a new IP configuration. Registers a callback to
102 // IPConfigUpdatedCallback on IP configuration changes. Returns true if the IP
103 // request was successfully sent.
104 bool AcquireDHCPConfig();
105
Paul Stewarte6132022011-08-16 09:11:02 -0700106 // Maintain connection state (Routes, IP Addresses and DNS) in the OS.
107 void CreateConnection();
108
109 // Remove connection state
110 void DestroyConnection();
111
Paul Stewart03dba0b2011-08-22 16:32:45 -0700112 // Selects a service to be "current" -- i.e. link-state or configuration
113 // events that happen to the device are attributed to this service.
114 void SelectService(const ServiceRefPtr &service);
115
116 // Set the state of the selected service
117 void SetServiceState(Service::ConnectState state);
118
119 // Set the failure of the selected service (implicitly sets the state to
120 // "failure")
121 void SetServiceFailure(Service::ConnectFailure failure_state);
122
Chris Masone27c4aa52011-07-02 13:10:14 -0700123 void HelpRegisterDerivedStrings(const std::string &name,
Chris Masone889666b2011-07-03 12:58:50 -0700124 Strings(Device::*get)(void),
125 bool(Device::*set)(const Strings&));
Chris Masoneb925cc82011-06-22 15:39:57 -0700126
Paul Stewartac4ac002011-08-26 12:04:26 -0700127 // Property getters reserved for subclasses
128 ControlInterface *control_interface() const { return control_interface_; }
129 EventDispatcher *dispatcher() const { return dispatcher_; }
130 Manager *manager() const { return manager_; }
131 std::vector<ServiceRefPtr> *services() { return &services_; }
Paul Stewartd5843772011-05-11 15:40:42 -0700132
Paul Stewart75897df2011-04-27 09:05:53 -0700133 private:
Chris Masone413a3192011-05-09 17:10:05 -0700134 friend class DeviceAdaptorInterface;
Darin Petkovafa6fc42011-06-21 16:21:08 -0700135
Chris Masone5dec5f42011-07-22 14:07:55 -0700136 static const char kStoragePowered[];
137 static const char kStorageIPConfigs[];
138
Darin Petkovafa6fc42011-06-21 16:21:08 -0700139 // Callback invoked on every IP configuration update.
Chris Masone2b105542011-06-22 10:58:09 -0700140 void IPConfigUpdatedCallback(const IPConfigRefPtr &ipconfig, bool success);
Darin Petkovafa6fc42011-06-21 16:21:08 -0700141
Chris Masone5dec5f42011-07-22 14:07:55 -0700142 // Right now, Devices reference IPConfigs directly when persisted to disk
143 // It's not clear that this makes sense long-term, but that's how it is now.
144 // This call generates a string in the right format for this persisting.
Chris Masone34af2182011-08-22 11:59:36 -0700145 // |suffix| is injected into the storage identifier used for the configs.
146 std::string SerializeIPConfigs(const std::string &suffix);
Chris Masone5dec5f42011-07-22 14:07:55 -0700147
Chris Masone4e851612011-07-01 10:46:53 -0700148 std::vector<std::string> AvailableIPConfigs();
149 std::string GetRpcConnectionIdentifier();
150
Paul Stewartac4ac002011-08-26 12:04:26 -0700151 // Properties
152 bool powered_; // TODO(pstew): Is this what |running_| is for?
153 bool reconnect_;
154 const std::string hardware_address_;
155
156 PropertyStore store_;
157
158 std::vector<ServiceRefPtr> services_;
159 const int interface_index_;
160 bool running_;
161 const std::string link_name_;
162 const std::string unique_id_;
163 ControlInterface *control_interface_;
164 EventDispatcher *dispatcher_;
165 Manager *manager_;
166 IPConfigRefPtr ipconfig_;
167 ConnectionRefPtr connection_;
Darin Petkovafa6fc42011-06-21 16:21:08 -0700168 scoped_ptr<DeviceAdaptorInterface> adaptor_;
169
Paul Stewart03dba0b2011-08-22 16:32:45 -0700170 // Maintain a reference to the connected / connecting service
171 ServiceRefPtr selected_service_;
172
Darin Petkov77cb6812011-08-15 16:19:41 -0700173 // Cache singleton pointer for performance and test purposes.
174 DHCPProvider *dhcp_provider_;
175
Chris Masone9be4a9d2011-05-16 15:44:09 -0700176 DISALLOW_COPY_AND_ASSIGN(Device);
Paul Stewart75897df2011-04-27 09:05:53 -0700177};
178
Paul Stewart75897df2011-04-27 09:05:53 -0700179} // namespace shill
180
181#endif // SHILL_DEVICE_