blob: 1b5bd9a72df9229541416814257a5590bbf8cd37 [file] [log] [blame]
Peter Qiu326b6cf2015-09-02 11:11:42 -07001//
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//
Peter Qiufb39ba42014-11-21 09:09:59 -080016
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
31namespace shill {
32
33class NetlinkManager;
34class Nl80211Message;
35class RTNLHandler;
36class RTNLMessage;
37class RTNLListener;
Peter Qiufb39ba42014-11-21 09:09:59 -080038
39} // namespace shill
40
41namespace apmanager {
42
43class Manager;
44
Peter Qiu1ff67a72014-11-22 07:06:10 -080045// DeviceInfo will enumerate WiFi devices (PHYs) during startup and on-demand
46// (when new interface is detected but the corresponding device is not
47// enumerated). And use RTNL to monitor creation/deletion of WiFi interfaces.
Peter Qiufb39ba42014-11-21 09:09:59 -080048class DeviceInfo : public base::SupportsWeakPtr<DeviceInfo> {
49 public:
50 explicit DeviceInfo(Manager* manager);
51 virtual ~DeviceInfo();
52
53 // Start and stop device detection monitoring.
54 void Start();
55 void Stop();
56
57 private:
58 friend class DeviceInfoTest;
59
60 static const char kDeviceInfoRoot[];
61 static const char kInterfaceUevent[];
62 static const char kInterfaceUeventWifiSignature[];
63
64 // Use nl80211 to enumerate available WiFi PHYs.
65 void EnumerateDevices();
66 void OnWiFiPhyInfoReceived(const shill::Nl80211Message& msg);
67
68 // Handler for RTNL link event.
69 void LinkMsgHandler(const shill::RTNLMessage& msg);
70 void AddLinkMsgHandler(const std::string& iface_name, int iface_index);
71 void DelLinkMsgHandler(const std::string& iface_name, int iface_index);
72
73 // Return true if the specify |iface_name| is a wifi interface, false
74 // otherwise.
75 bool IsWifiInterface(const std::string& iface_name);
76
77 // Return the contents of the device info file |path_name| for interface
78 // |iface_name| in output parameter |contents_out|. Return true if file
79 // read succeed, fales otherwise.
80 bool GetDeviceInfoContents(const std::string& iface_name,
81 const std::string& path_name,
82 std::string* contents_out);
83
84 // Use nl80211 to get WiFi interface information for interface on
85 // |iface_index|.
86 void GetWiFiInterfaceInfo(int iface_index);
87 void OnWiFiInterfaceInfoReceived(const shill::Nl80211Message& msg);
88
89 // Use nl80211 to get PHY info for interface on |iface_index|.
90 void GetWiFiInterfacePhyInfo(uint32_t iface_index);
91 void OnWiFiInterfacePhyInfoReceived(
92 uint32_t iface_index, const shill::Nl80211Message& msg);
93
94 scoped_refptr<Device> GetDevice(const std::string& phy_name);
95 void RegisterDevice(scoped_refptr<Device> device);
96
97 // Maps interface index to interface info
98 std::map<uint32_t, Device::WiFiInterface> interface_infos_;
99 // Maps device name to device object. Each device object represents a PHY.
100 std::map<std::string, scoped_refptr<Device>> devices_;
101
102 // RTNL link event callback and listener.
103 base::Callback<void(const shill::RTNLMessage&)> link_callback_;
104 std::unique_ptr<shill::RTNLListener> link_listener_;
105
106 base::FilePath device_info_root_;
107 Manager *manager_;
108
109 // Cache copy of singleton pointers.
110 shill::NetlinkManager* netlink_manager_;
111 shill::RTNLHandler* rtnl_handler_;
112
Peter Qiufb39ba42014-11-21 09:09:59 -0800113 DISALLOW_COPY_AND_ASSIGN(DeviceInfo);
114};
115
116} // namespace apmanager
117
118#endif // APMANAGER_DEVICE_INFO_H_