2 * Copyright (C) 2016 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef WIFICOND_NET_NETLINK_UTILS_H_
18 #define WIFICOND_NET_NETLINK_UTILS_H_
23 #include <linux/nl80211.h>
25 #include <android-base/macros.h>
27 #include "wificond/net/netlink_manager.h"
34 BandInfo(std::vector<uint32_t>& band_2g_,
35 std::vector<uint32_t>& band_5g_,
36 std::vector<uint32_t>& band_dfs_)
39 band_dfs(band_dfs_) {}
40 // Frequencies for 2.4 GHz band.
41 std::vector<uint32_t> band_2g;
42 // Frequencies for 5 GHz band without DFS.
43 std::vector<uint32_t> band_5g;
44 // Frequencies for DFS.
45 std::vector<uint32_t> band_dfs;
48 struct ScanCapabilities {
49 ScanCapabilities() = default;
50 ScanCapabilities(uint8_t max_num_scan_ssids_,
51 uint8_t max_num_sched_scan_ssids_,
52 uint8_t max_match_sets_)
53 : max_num_scan_ssids(max_num_scan_ssids_),
54 max_num_sched_scan_ssids(max_num_sched_scan_ssids_),
55 max_match_sets(max_match_sets_) {}
56 // Number of SSIDs you can scan with a single scan request.
57 uint8_t max_num_scan_ssids;
58 // Number of SSIDs you can scan with a single scheduled scan request.
59 uint8_t max_num_sched_scan_ssids;
60 // Maximum number of sets that can be used with NL80211_ATTR_SCHED_SCAN_MATCH.
61 uint8_t max_match_sets;
64 struct WiphyFeatures {
66 : supports_random_mac_oneshot_scan(false),
67 supports_random_mac_sched_scan(false) {}
68 WiphyFeatures(uint32_t feature_flags)
69 : supports_random_mac_oneshot_scan(
70 feature_flags & NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR),
71 supports_random_mac_sched_scan(
72 feature_flags & NL80211_FEATURE_SCHED_SCAN_RANDOM_MAC_ADDR) {}
73 // This device/driver supports using a random MAC address during scan
74 // (while not associated).
75 bool supports_random_mac_oneshot_scan;
76 // This device/driver supports using a random MAC address for every
77 // scan iteration during scheduled scan (while not associated).
78 bool supports_random_mac_sched_scan;
79 // There are other flags included in NL80211_ATTR_FEATURE_FLAGS.
80 // We will add them once we find them useful.
84 StationInfo() = default;
85 StationInfo(uint32_t station_tx_packets_,
86 uint32_t station_tx_failed_,
87 uint32_t station_tx_bitrate_,
89 : station_tx_packets(station_tx_packets_),
90 station_tx_failed(station_tx_failed_),
91 station_tx_bitrate(station_tx_bitrate_),
92 current_rssi(current_rssi_) {}
93 // Number of successfully transmitted packets.
94 int32_t station_tx_packets;
95 // Number of tramsmission failures.
96 int32_t station_tx_failed;
97 // Transimission bit rate in 100kbit/s.
98 uint32_t station_tx_bitrate;
99 // Current signal strength.
101 // There are many other counters/parameters included in station info.
102 // We will add them once we find them useful.
105 class MlmeEventHandler;
106 class NetlinkManager;
109 // Provides NL80211 helper functions.
112 // Currently we only support setting the interface to STATION mode.
113 // This is used for cleaning up interface after KILLING hostapd.
118 explicit NetlinkUtils(NetlinkManager* netlink_manager);
119 virtual ~NetlinkUtils();
121 // Get the wiphy index from kernel.
122 // |*out_wiphy_index| returns the wiphy index from kernel.
123 // Returns true on success.
124 virtual bool GetWiphyIndex(uint32_t* out_wiphy_index);
126 // Get wifi interface info from kernel.
127 // |wiphy_index| is the wiphy index we get using GetWiphyIndex().
128 // Returns true on success.
129 virtual bool GetInterfaceInfo(uint32_t wiphy_index,
132 std::vector<uint8_t>* mac_addr);
134 // Set the mode of interface.
135 // |interface_index| is the interface index.
136 // |mode| is one of the values in |enum InterfaceMode|.
137 // Returns true on success.
138 virtual bool SetInterfaceMode(uint32_t interface_index,
141 // Get wiphy capability information from kernel.
142 // Returns true on success.
143 virtual bool GetWiphyInfo(uint32_t wiphy_index,
144 BandInfo* out_band_info,
145 ScanCapabilities* out_scan_capabilities,
146 WiphyFeatures* out_wiphy_features);
148 // Get station info from kernel.
149 // |*out_station_info]| is the struct of available station information.
150 // Returns true on success.
151 virtual bool GetStationInfo(uint32_t interface_index,
152 const std::vector<uint8_t>& mac_address,
153 StationInfo* out_station_info);
155 // Sign up to be notified when there is MLME event.
156 // Only one handler can be registered per interface index.
157 // New handler will replace the registered handler if they are for the
158 // same interface index.
159 // NetlinkUtils is not going to take ownership of this pointer, and that it
160 // is the caller's responsibility to make sure that the object exists for the
161 // duration of the subscription.
162 virtual void SubscribeMlmeEvent(uint32_t interface_index,
163 MlmeEventHandler* handler);
165 // Cancel the sign-up of receiving MLME event notification
166 // from interface with index |interface_index|.
167 virtual void UnsubscribeMlmeEvent(uint32_t interface_index);
169 // Sign up to be notified when there is an regulatory domain change.
170 // Only one handler can be registered per wiphy index.
171 // New handler will replace the registered handler if they are for the
173 virtual void SubscribeRegDomainChange(uint32_t wiphy_index,
174 OnRegDomainChangedHandler handler);
176 // Cancel the sign-up of receiving regulatory domain change notification
177 // from wiphy with index |wiphy_index|.
178 virtual void UnsubscribeRegDomainChange(uint32_t wiphy_index);
181 bool ParseBandInfo(const NL80211Packet* const packet,
182 BandInfo* out_band_info);
183 bool ParseScanCapabilities(const NL80211Packet* const packet,
184 ScanCapabilities* out_scan_capabilities);
185 NetlinkManager* netlink_manager_;
187 DISALLOW_COPY_AND_ASSIGN(NetlinkUtils);
190 } // namespace wificond
191 } // namespace android
193 #endif // WIFICOND_NET_NETLINK_UTILS_H_