Peter Qiu | fb39ba4 | 2014-11-21 09:09:59 -0800 | [diff] [blame] | 1 | // Copyright 2014 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 APMANAGER_DEVICE_INFO_H_ |
| 6 | #define APMANAGER_DEVICE_INFO_H_ |
| 7 | |
| 8 | #include <map> |
| 9 | #include <string> |
| 10 | |
| 11 | #include <base/callback.h> |
| 12 | #include <base/files/file_path.h> |
| 13 | #include <base/memory/ref_counted.h> |
| 14 | #include <base/memory/weak_ptr.h> |
| 15 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
| 16 | |
| 17 | #include "apmanager/device.h" |
| 18 | |
| 19 | namespace shill { |
| 20 | |
| 21 | class NetlinkManager; |
| 22 | class Nl80211Message; |
| 23 | class RTNLHandler; |
| 24 | class RTNLMessage; |
| 25 | class RTNLListener; |
| 26 | class Sockets; |
| 27 | |
| 28 | } // namespace shill |
| 29 | |
| 30 | namespace apmanager { |
| 31 | |
| 32 | class Manager; |
| 33 | |
Peter Qiu | 1ff67a7 | 2014-11-22 07:06:10 -0800 | [diff] [blame^] | 34 | // DeviceInfo will enumerate WiFi devices (PHYs) during startup and on-demand |
| 35 | // (when new interface is detected but the corresponding device is not |
| 36 | // enumerated). And use RTNL to monitor creation/deletion of WiFi interfaces. |
Peter Qiu | fb39ba4 | 2014-11-21 09:09:59 -0800 | [diff] [blame] | 37 | class DeviceInfo : public base::SupportsWeakPtr<DeviceInfo> { |
| 38 | public: |
| 39 | explicit DeviceInfo(Manager* manager); |
| 40 | virtual ~DeviceInfo(); |
| 41 | |
| 42 | // Start and stop device detection monitoring. |
| 43 | void Start(); |
| 44 | void Stop(); |
| 45 | |
| 46 | private: |
| 47 | friend class DeviceInfoTest; |
| 48 | |
| 49 | static const char kDeviceInfoRoot[]; |
| 50 | static const char kInterfaceUevent[]; |
| 51 | static const char kInterfaceUeventWifiSignature[]; |
| 52 | |
| 53 | // Use nl80211 to enumerate available WiFi PHYs. |
| 54 | void EnumerateDevices(); |
| 55 | void OnWiFiPhyInfoReceived(const shill::Nl80211Message& msg); |
| 56 | |
| 57 | // Handler for RTNL link event. |
| 58 | void LinkMsgHandler(const shill::RTNLMessage& msg); |
| 59 | void AddLinkMsgHandler(const std::string& iface_name, int iface_index); |
| 60 | void DelLinkMsgHandler(const std::string& iface_name, int iface_index); |
| 61 | |
| 62 | // Return true if the specify |iface_name| is a wifi interface, false |
| 63 | // otherwise. |
| 64 | bool IsWifiInterface(const std::string& iface_name); |
| 65 | |
| 66 | // Return the contents of the device info file |path_name| for interface |
| 67 | // |iface_name| in output parameter |contents_out|. Return true if file |
| 68 | // read succeed, fales otherwise. |
| 69 | bool GetDeviceInfoContents(const std::string& iface_name, |
| 70 | const std::string& path_name, |
| 71 | std::string* contents_out); |
| 72 | |
| 73 | // Use nl80211 to get WiFi interface information for interface on |
| 74 | // |iface_index|. |
| 75 | void GetWiFiInterfaceInfo(int iface_index); |
| 76 | void OnWiFiInterfaceInfoReceived(const shill::Nl80211Message& msg); |
| 77 | |
| 78 | // Use nl80211 to get PHY info for interface on |iface_index|. |
| 79 | void GetWiFiInterfacePhyInfo(uint32_t iface_index); |
| 80 | void OnWiFiInterfacePhyInfoReceived( |
| 81 | uint32_t iface_index, const shill::Nl80211Message& msg); |
| 82 | |
| 83 | scoped_refptr<Device> GetDevice(const std::string& phy_name); |
| 84 | void RegisterDevice(scoped_refptr<Device> device); |
| 85 | |
| 86 | // Maps interface index to interface info |
| 87 | std::map<uint32_t, Device::WiFiInterface> interface_infos_; |
| 88 | // Maps device name to device object. Each device object represents a PHY. |
| 89 | std::map<std::string, scoped_refptr<Device>> devices_; |
| 90 | |
| 91 | // RTNL link event callback and listener. |
| 92 | base::Callback<void(const shill::RTNLMessage&)> link_callback_; |
| 93 | std::unique_ptr<shill::RTNLListener> link_listener_; |
| 94 | |
| 95 | base::FilePath device_info_root_; |
| 96 | Manager *manager_; |
| 97 | |
| 98 | // Cache copy of singleton pointers. |
| 99 | shill::NetlinkManager* netlink_manager_; |
| 100 | shill::RTNLHandler* rtnl_handler_; |
| 101 | |
| 102 | std::unique_ptr<shill::Sockets> sockets_; |
| 103 | |
| 104 | DISALLOW_COPY_AND_ASSIGN(DeviceInfo); |
| 105 | }; |
| 106 | |
| 107 | } // namespace apmanager |
| 108 | |
| 109 | #endif // APMANAGER_DEVICE_INFO_H_ |