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"
32 struct InterfaceInfo {
33 InterfaceInfo() = default;
34 InterfaceInfo(uint32_t index_,
35 const std::string name_,
36 const std::vector<uint8_t> mac_address_)
39 mac_address(mac_address_) {}
40 // Index of this interface.
42 // Name of this interface.
44 // MAC address of this interface.
45 std::vector<uint8_t> mac_address;
50 BandInfo(std::vector<uint32_t>& band_2g_,
51 std::vector<uint32_t>& band_5g_,
52 std::vector<uint32_t>& band_dfs_)
55 band_dfs(band_dfs_) {}
56 // Frequencies for 2.4 GHz band.
57 std::vector<uint32_t> band_2g;
58 // Frequencies for 5 GHz band without DFS.
59 std::vector<uint32_t> band_5g;
60 // Frequencies for DFS.
61 std::vector<uint32_t> band_dfs;
64 struct ScanCapabilities {
65 ScanCapabilities() = default;
66 ScanCapabilities(uint8_t max_num_scan_ssids_,
67 uint8_t max_num_sched_scan_ssids_,
68 uint8_t max_match_sets_,
69 uint32_t max_num_scan_plans_,
70 uint32_t max_scan_plan_interval_,
71 uint32_t max_scan_plan_iterations_)
72 : max_num_scan_ssids(max_num_scan_ssids_),
73 max_num_sched_scan_ssids(max_num_sched_scan_ssids_),
74 max_match_sets(max_match_sets_),
75 max_num_scan_plans(max_num_scan_plans_),
76 max_scan_plan_interval(max_scan_plan_interval_),
77 max_scan_plan_iterations(max_scan_plan_iterations_) {}
78 // Number of SSIDs you can scan with a single scan request.
79 uint8_t max_num_scan_ssids;
80 // Number of SSIDs you can scan with a single scheduled scan request.
81 uint8_t max_num_sched_scan_ssids;
82 // Maximum number of sets that can be used with NL80211_ATTR_SCHED_SCAN_MATCH.
83 uint8_t max_match_sets;
84 // Maximum number of scan plans that can be specified.
85 uint32_t max_num_scan_plans;
86 // Maximum interval in seconds for a particular scan plan that can be
88 uint32_t max_scan_plan_interval;
89 // Maximum number of iterations for a particular scan plan that can be
91 uint32_t max_scan_plan_iterations;
94 struct WiphyFeatures {
96 : supports_random_mac_oneshot_scan(false),
97 supports_random_mac_sched_scan(false) {}
98 WiphyFeatures(uint32_t feature_flags)
99 : supports_random_mac_oneshot_scan(
100 feature_flags & NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR),
101 supports_random_mac_sched_scan(
102 feature_flags & NL80211_FEATURE_SCHED_SCAN_RANDOM_MAC_ADDR) {}
103 // This device/driver supports using a random MAC address during scan
104 // (while not associated).
105 bool supports_random_mac_oneshot_scan;
106 // This device/driver supports using a random MAC address for every
107 // scan iteration during scheduled scan (while not associated).
108 bool supports_random_mac_sched_scan;
109 // There are other flags included in NL80211_ATTR_FEATURE_FLAGS.
110 // We will add them once we find them useful.
114 StationInfo() = default;
115 StationInfo(uint32_t station_tx_packets_,
116 uint32_t station_tx_failed_,
117 uint32_t station_tx_bitrate_,
118 int8_t current_rssi_)
119 : station_tx_packets(station_tx_packets_),
120 station_tx_failed(station_tx_failed_),
121 station_tx_bitrate(station_tx_bitrate_),
122 current_rssi(current_rssi_) {}
123 // Number of successfully transmitted packets.
124 int32_t station_tx_packets;
125 // Number of tramsmission failures.
126 int32_t station_tx_failed;
127 // Transimission bit rate in 100kbit/s.
128 uint32_t station_tx_bitrate;
129 // Current signal strength.
131 // There are many other counters/parameters included in station info.
132 // We will add them once we find them useful.
135 class MlmeEventHandler;
136 class NetlinkManager;
139 // Provides NL80211 helper functions.
142 // Currently we only support setting the interface to STATION mode.
143 // This is used for cleaning up interface after KILLING hostapd.
148 explicit NetlinkUtils(NetlinkManager* netlink_manager);
149 virtual ~NetlinkUtils();
151 // Get the wiphy index from kernel.
152 // |*out_wiphy_index| returns the wiphy index from kernel.
153 // Returns true on success.
154 virtual bool GetWiphyIndex(uint32_t* out_wiphy_index);
156 // Get wifi interfaces info from kernel.
157 // |wiphy_index| is the wiphy index we get using GetWiphyIndex().
158 // |interface_info| returns a vector of InterfaceInfo structs with
159 // information about all existing interfaces.
160 // Returns true on success.
161 virtual bool GetInterfaces(uint32_t wiphy_index,
162 std::vector<InterfaceInfo>* interface_info);
164 // Set the mode of interface.
165 // |interface_index| is the interface index.
166 // |mode| is one of the values in |enum InterfaceMode|.
167 // Returns true on success.
168 virtual bool SetInterfaceMode(uint32_t interface_index,
171 // Get wiphy capability information from kernel.
172 // Returns true on success.
173 virtual bool GetWiphyInfo(uint32_t wiphy_index,
174 BandInfo* out_band_info,
175 ScanCapabilities* out_scan_capabilities,
176 WiphyFeatures* out_wiphy_features);
178 // Get station info from kernel.
179 // |*out_station_info]| is the struct of available station information.
180 // Returns true on success.
181 virtual bool GetStationInfo(uint32_t interface_index,
182 const std::vector<uint8_t>& mac_address,
183 StationInfo* out_station_info);
185 // Sign up to be notified when there is MLME event.
186 // Only one handler can be registered per interface index.
187 // New handler will replace the registered handler if they are for the
188 // same interface index.
189 // NetlinkUtils is not going to take ownership of this pointer, and that it
190 // is the caller's responsibility to make sure that the object exists for the
191 // duration of the subscription.
192 virtual void SubscribeMlmeEvent(uint32_t interface_index,
193 MlmeEventHandler* handler);
195 // Cancel the sign-up of receiving MLME event notification
196 // from interface with index |interface_index|.
197 virtual void UnsubscribeMlmeEvent(uint32_t interface_index);
199 // Sign up to be notified when there is an regulatory domain change.
200 // Only one handler can be registered per wiphy index.
201 // New handler will replace the registered handler if they are for the
203 virtual void SubscribeRegDomainChange(uint32_t wiphy_index,
204 OnRegDomainChangedHandler handler);
206 // Cancel the sign-up of receiving regulatory domain change notification
207 // from wiphy with index |wiphy_index|.
208 virtual void UnsubscribeRegDomainChange(uint32_t wiphy_index);
210 // Sign up to be notified when there is an station event.
211 // Only one handler can be registered per interface index.
212 // New handler will replace the registered handler if they are for the
213 // same interface index.
214 virtual void SubscribeStationEvent(uint32_t interface_index,
215 OnStationEventHandler handler);
217 // Cancel the sign-up of receiving station events.
218 virtual void UnsubscribeStationEvent(uint32_t interface_index);
221 bool ParseBandInfo(const NL80211Packet* const packet,
222 BandInfo* out_band_info);
223 bool ParseScanCapabilities(const NL80211Packet* const packet,
224 ScanCapabilities* out_scan_capabilities);
225 NetlinkManager* netlink_manager_;
227 DISALLOW_COPY_AND_ASSIGN(NetlinkUtils);
230 } // namespace wificond
231 } // namespace android
233 #endif // WIFICOND_NET_NETLINK_UTILS_H_