1 // 2 // Copyright (C) 2014 The Android Open Source Project 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 // 16 17 #ifndef APMANAGER_DEVICE_INFO_H_ 18 #define APMANAGER_DEVICE_INFO_H_ 19 20 #include <map> 21 #include <string> 22 23 #include <base/callback.h> 24 #include <base/files/file_path.h> 25 #include <base/memory/ref_counted.h> 26 #include <base/memory/weak_ptr.h> 27 #include <gtest/gtest_prod.h> // for FRIEND_TEST 28 29 #include "apmanager/device.h" 30 31 namespace shill { 32 33 class NetlinkManager; 34 class Nl80211Message; 35 class RTNLHandler; 36 class RTNLMessage; 37 class RTNLListener; 38 39 } // namespace shill 40 41 namespace apmanager { 42 43 class ControlInterface; 44 class Manager; 45 46 // DeviceInfo will enumerate WiFi devices (PHYs) during startup and on-demand 47 // (when new interface is detected but the corresponding device is not 48 // enumerated). And use RTNL to monitor creation/deletion of WiFi interfaces. 49 class DeviceInfo : public base::SupportsWeakPtr<DeviceInfo> { 50 public: 51 explicit DeviceInfo(Manager* manager); 52 virtual ~DeviceInfo(); 53 54 // Start and stop device detection monitoring. 55 void Start(); 56 void Stop(); 57 58 private: 59 friend class DeviceInfoTest; 60 61 static const char kDeviceInfoRoot[]; 62 static const char kInterfaceUevent[]; 63 static const char kInterfaceUeventWifiSignature[]; 64 65 // Use nl80211 to enumerate available WiFi PHYs. 66 void EnumerateDevices(); 67 void OnWiFiPhyInfoReceived(const shill::Nl80211Message& msg); 68 69 // Handler for RTNL link event. 70 void LinkMsgHandler(const shill::RTNLMessage& msg); 71 void AddLinkMsgHandler(const std::string& iface_name, int iface_index); 72 void DelLinkMsgHandler(const std::string& iface_name, int iface_index); 73 74 // Return true if the specify |iface_name| is a wifi interface, false 75 // otherwise. 76 bool IsWifiInterface(const std::string& iface_name); 77 78 // Return the contents of the device info file |path_name| for interface 79 // |iface_name| in output parameter |contents_out|. Return true if file 80 // read succeed, fales otherwise. 81 bool GetDeviceInfoContents(const std::string& iface_name, 82 const std::string& path_name, 83 std::string* contents_out); 84 85 // Use nl80211 to get WiFi interface information for interface on 86 // |iface_index|. 87 void GetWiFiInterfaceInfo(int iface_index); 88 void OnWiFiInterfaceInfoReceived(const shill::Nl80211Message& msg); 89 90 // Use nl80211 to get PHY info for interface on |iface_index|. 91 void GetWiFiInterfacePhyInfo(uint32_t iface_index); 92 void OnWiFiInterfacePhyInfoReceived( 93 uint32_t iface_index, const shill::Nl80211Message& msg); 94 95 scoped_refptr<Device> GetDevice(const std::string& phy_name); 96 void RegisterDevice(scoped_refptr<Device> device); 97 98 // Maps interface index to interface info 99 std::map<uint32_t, Device::WiFiInterface> interface_infos_; 100 // Maps device name to device object. Each device object represents a PHY. 101 std::map<std::string, scoped_refptr<Device>> devices_; 102 103 // RTNL link event callback and listener. 104 base::Callback<void(const shill::RTNLMessage&)> link_callback_; 105 std::unique_ptr<shill::RTNLListener> link_listener_; 106 107 base::FilePath device_info_root_; 108 Manager* manager_; 109 110 // Cache copy of singleton pointers. 111 shill::NetlinkManager* netlink_manager_; 112 shill::RTNLHandler* rtnl_handler_; 113 114 int device_identifier_; 115 116 DISALLOW_COPY_AND_ASSIGN(DeviceInfo); 117 }; 118 119 } // namespace apmanager 120 121 #endif // APMANAGER_DEVICE_INFO_H_ 122