blob: 27fee7421d5ed67d245c62462580fec42e27cc76 [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 Stewart75897df2011-04-27 09:05:53 -070019#include "shill/shill_event.h"
20
21namespace shill {
22
Chris Masone9be4a9d2011-05-16 15:44:09 -070023class ControlInterface;
Darin Petkov77cb6812011-08-15 16:19:41 -070024class DHCPProvider;
Chris Masone9be4a9d2011-05-16 15:44:09 -070025class DeviceAdaptorInterface;
Chris Masonec1e50412011-06-07 13:04:53 -070026class DeviceInfo;
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,
Paul Stewartb50f0b92011-05-16 16:31:42 -070050 int interface_index);
Chris Masone9be4a9d2011-05-16 15:44:09 -070051 virtual ~Device();
52
53 virtual void Start();
54 virtual void Stop();
55
Darin Petkovafa6fc42011-06-21 16:21:08 -070056 // Base method always returns false.
Darin Petkov6f9eaa32011-08-09 15:26:44 -070057 virtual bool TechnologyIs(const Technology type) const;
Darin Petkovafa6fc42011-06-21 16:21:08 -070058
Paul Stewartf1ce5d22011-05-19 13:10:20 -070059 virtual void LinkEvent(unsigned flags, unsigned change);
60 virtual void Scan();
Paul Stewart75897df2011-04-27 09:05:53 -070061
Chris Masonec1e50412011-06-07 13:04:53 -070062 virtual void ConfigIP() {}
63
Chris Masone27c4aa52011-07-02 13:10:14 -070064 std::string GetRpcIdentifier();
Chris Masone5dec5f42011-07-22 14:07:55 -070065 std::string GetStorageIdentifier();
Chris Masone8fe2c7e2011-06-09 15:51:19 -070066
Darin Petkove0a312e2011-07-20 13:45:28 -070067 const std::string &link_name() const { return link_name_; }
68 int interface_index() const { return interface_index_; }
Paul Stewarte6132022011-08-16 09:11:02 -070069 const ConnectionRefPtr &connection() const { return connection_; }
Darin Petkove0a312e2011-07-20 13:45:28 -070070
Chris Masone19e30402011-07-19 15:48:47 -070071 const std::string &FriendlyName() const;
Chris Masone27c4aa52011-07-02 13:10:14 -070072
Darin Petkovafa6fc42011-06-21 16:21:08 -070073 // Returns a string that is guaranteed to uniquely identify this Device
74 // instance.
75 const std::string &UniqueName() const;
Chris Masonea82b7112011-05-25 15:16:29 -070076
Chris Masone19e30402011-07-19 15:48:47 -070077 PropertyStore *store() { return &store_; }
78
Chris Masone5dec5f42011-07-22 14:07:55 -070079 bool Load(StoreInterface *storage);
80 bool Save(StoreInterface *storage);
81
Darin Petkov77cb6812011-08-15 16:19:41 -070082 void set_dhcp_provider(DHCPProvider *provider) { dhcp_provider_ = provider; }
83
Paul Stewartd5843772011-05-11 15:40:42 -070084 protected:
Darin Petkovafa6fc42011-06-21 16:21:08 -070085 FRIEND_TEST(DeviceTest, AcquireDHCPConfig);
86 FRIEND_TEST(DeviceTest, DestroyIPConfig);
87 FRIEND_TEST(DeviceTest, DestroyIPConfigNULL);
Chris Masone95207da2011-06-29 16:50:49 -070088 FRIEND_TEST(DeviceTest, GetProperties);
Chris Masone5dec5f42011-07-22 14:07:55 -070089 FRIEND_TEST(DeviceTest, Save);
Darin Petkovafa6fc42011-06-21 16:21:08 -070090
91 // If there's an IP configuration in |ipconfig_|, releases the IP address and
92 // destroys the configuration instance.
93 void DestroyIPConfig();
94
95 // Creates a new DHCP IP configuration instance, stores it in |ipconfig_| and
96 // requests a new IP configuration. Registers a callback to
97 // IPConfigUpdatedCallback on IP configuration changes. Returns true if the IP
98 // request was successfully sent.
99 bool AcquireDHCPConfig();
100
Paul Stewarte6132022011-08-16 09:11:02 -0700101 // Maintain connection state (Routes, IP Addresses and DNS) in the OS.
102 void CreateConnection();
103
104 // Remove connection state
105 void DestroyConnection();
106
Chris Masone27c4aa52011-07-02 13:10:14 -0700107 void HelpRegisterDerivedStrings(const std::string &name,
Chris Masone889666b2011-07-03 12:58:50 -0700108 Strings(Device::*get)(void),
109 bool(Device::*set)(const Strings&));
Chris Masoneb925cc82011-06-22 15:39:57 -0700110
111 // Properties
112 std::string hardware_address_;
Chris Masoneb925cc82011-06-22 15:39:57 -0700113 bool powered_; // TODO(pstew): Is this what |running_| is for?
114 bool reconnect_;
Chris Masoneb925cc82011-06-22 15:39:57 -0700115
Chris Masone27c4aa52011-07-02 13:10:14 -0700116 PropertyStore store_;
117
Chris Masonec1e50412011-06-07 13:04:53 -0700118 std::vector<ServiceRefPtr> services_;
Darin Petkove0a312e2011-07-20 13:45:28 -0700119 const int interface_index_;
Paul Stewartb50f0b92011-05-16 16:31:42 -0700120 bool running_;
Chris Masone7df0c672011-07-15 10:24:54 -0700121 const std::string link_name_;
Chris Masone19e30402011-07-19 15:48:47 -0700122 const std::string unique_id_;
Darin Petkovd9661952011-08-03 16:25:42 -0700123 ControlInterface *control_interface_;
124 EventDispatcher *dispatcher_;
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700125 Manager *manager_;
Darin Petkovafa6fc42011-06-21 16:21:08 -0700126 IPConfigRefPtr ipconfig_;
Paul Stewarte6132022011-08-16 09:11:02 -0700127 ConnectionRefPtr connection_;
Paul Stewartd5843772011-05-11 15:40:42 -0700128
Paul Stewart75897df2011-04-27 09:05:53 -0700129 private:
Chris Masone413a3192011-05-09 17:10:05 -0700130 friend class DeviceAdaptorInterface;
Darin Petkovafa6fc42011-06-21 16:21:08 -0700131
Chris Masone5dec5f42011-07-22 14:07:55 -0700132 static const char kStoragePowered[];
133 static const char kStorageIPConfigs[];
134
Darin Petkovafa6fc42011-06-21 16:21:08 -0700135 // Callback invoked on every IP configuration update.
Chris Masone2b105542011-06-22 10:58:09 -0700136 void IPConfigUpdatedCallback(const IPConfigRefPtr &ipconfig, bool success);
Darin Petkovafa6fc42011-06-21 16:21:08 -0700137
Chris Masone5dec5f42011-07-22 14:07:55 -0700138 // Right now, Devices reference IPConfigs directly when persisted to disk
139 // It's not clear that this makes sense long-term, but that's how it is now.
140 // This call generates a string in the right format for this persisting.
141 std::string SerializeIPConfigsForStorage();
142
Chris Masone4e851612011-07-01 10:46:53 -0700143 std::vector<std::string> AvailableIPConfigs();
144 std::string GetRpcConnectionIdentifier();
145
Darin Petkovafa6fc42011-06-21 16:21:08 -0700146 scoped_ptr<DeviceAdaptorInterface> adaptor_;
147
Darin Petkov77cb6812011-08-15 16:19:41 -0700148 // Cache singleton pointer for performance and test purposes.
149 DHCPProvider *dhcp_provider_;
150
Chris Masone9be4a9d2011-05-16 15:44:09 -0700151 DISALLOW_COPY_AND_ASSIGN(Device);
Paul Stewart75897df2011-04-27 09:05:53 -0700152};
153
Paul Stewart75897df2011-04-27 09:05:53 -0700154} // namespace shill
155
156#endif // SHILL_DEVICE_