summaryrefslogtreecommitdiff
path: root/device_info.h
blob: 83faa2a858939aae0a27e0662c3513f6f4ef8d7d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//
// Copyright (C) 2014 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#ifndef APMANAGER_DEVICE_INFO_H_
#define APMANAGER_DEVICE_INFO_H_

#include <map>
#include <string>

#include <base/callback.h>
#include <base/files/file_path.h>
#include <base/memory/ref_counted.h>
#include <base/memory/weak_ptr.h>
#include <gtest/gtest_prod.h>  // for FRIEND_TEST

#include "apmanager/device.h"

namespace shill {

class NetlinkManager;
class Nl80211Message;
class RTNLHandler;
class RTNLMessage;
class RTNLListener;

}  // namespace shill

namespace apmanager {

class ControlInterface;
class Manager;

// DeviceInfo will enumerate WiFi devices (PHYs) during startup and on-demand
// (when new interface is detected but the corresponding device is not
// enumerated). And use RTNL to monitor creation/deletion of WiFi interfaces.
class DeviceInfo : public base::SupportsWeakPtr<DeviceInfo> {
 public:
  explicit DeviceInfo(Manager* manager);
  virtual ~DeviceInfo();

  // Start and stop device detection monitoring.
  void Start();
  void Stop();

 private:
  friend class DeviceInfoTest;

  static const char kDeviceInfoRoot[];
  static const char kInterfaceUevent[];
  static const char kInterfaceUeventWifiSignature[];

  // Use nl80211 to enumerate available WiFi PHYs.
  void EnumerateDevices();
  void OnWiFiPhyInfoReceived(const shill::Nl80211Message& msg);

  // Handler for RTNL link event.
  void LinkMsgHandler(const shill::RTNLMessage& msg);
  void AddLinkMsgHandler(const std::string& iface_name, int iface_index);
  void DelLinkMsgHandler(const std::string& iface_name, int iface_index);

  // Return true if the specify |iface_name| is a wifi interface, false
  // otherwise.
  bool IsWifiInterface(const std::string& iface_name);

  // Return the contents of the device info file |path_name| for interface
  // |iface_name| in output parameter |contents_out|. Return true if file
  // read succeed, fales otherwise.
  bool GetDeviceInfoContents(const std::string& iface_name,
                             const std::string& path_name,
                             std::string* contents_out);

  // Use nl80211 to get WiFi interface information for interface on
  // |iface_index|.
  void GetWiFiInterfaceInfo(int iface_index);
  void OnWiFiInterfaceInfoReceived(const shill::Nl80211Message& msg);

  // Use nl80211 to get PHY info for interface on |iface_index|.
  void GetWiFiInterfacePhyInfo(uint32_t iface_index);
  void OnWiFiInterfacePhyInfoReceived(
      uint32_t iface_index, const shill::Nl80211Message& msg);

  scoped_refptr<Device> GetDevice(const std::string& phy_name);
  void RegisterDevice(const scoped_refptr<Device>& device);

  // Maps interface index to interface info
  std::map<uint32_t, Device::WiFiInterface> interface_infos_;
  // Maps device name to device object. Each device object represents a PHY.
  std::map<std::string, scoped_refptr<Device>> devices_;

  // RTNL link event callback and listener.
  base::Callback<void(const shill::RTNLMessage&)> link_callback_;
  std::unique_ptr<shill::RTNLListener> link_listener_;

  base::FilePath device_info_root_;
  Manager* manager_;

  // Cache copy of singleton pointers.
  shill::NetlinkManager* netlink_manager_;
  shill::RTNLHandler* rtnl_handler_;

  int device_identifier_;

  DISALLOW_COPY_AND_ASSIGN(DeviceInfo);
};

}  // namespace apmanager

#endif  // APMANAGER_DEVICE_INFO_H_