• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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 WIFICOND_SCANNING_SCAN_UTILS_H_
18 #define WIFICOND_SCANNING_SCAN_UTILS_H_
19 
20 #include <memory>
21 #include <vector>
22 
23 #include <android-base/macros.h>
24 
25 #include "wificond/net/netlink_manager.h"
26 
27 namespace android {
28 namespace net {
29 namespace wifi {
30 namespace nl80211 {
31 
32 class NativeScanResult;
33 class RadioChainInfo;
34 
35 }  // namespace nl80211
36 }  // namespace wifi
37 }  // namespace net
38 }  // namespace android
39 
40 namespace android {
41 namespace wificond {
42 
43 class NL80211NestedAttr;
44 class NL80211Packet;
45 
46 struct SchedScanIntervalSetting {
47   struct ScanPlan {
48     uint32_t interval_ms;
49     uint32_t n_iterations;
50   };
51   std::vector<ScanPlan> plans;
52   // After |plans| has been exhausted, scan at every
53   // |final_interval_ms|.
54   uint32_t final_interval_ms{0};
55 };
56 
57 struct SchedScanReqFlags {
58   bool request_random_mac;
59   bool request_low_power;
60   bool request_sched_scan_relative_rssi;
61 };
62 
63 // Provides scanning helper functions.
64 class ScanUtils {
65  public:
66   explicit ScanUtils(NetlinkManager* netlink_manager);
67   virtual ~ScanUtils();
68 
69   // Send 'get scan results' request to kernel and get the latest scan results.
70   // |interface_index| is the index of interface we want to get scan results
71   // from.
72   // A vector of ScanResult object will be returned by |*out_scan_results|.
73   // Returns true on success.
74   virtual bool GetScanResult(
75       uint32_t interface_index,
76       std::vector<android::net::wifi::nl80211::NativeScanResult>* out_scan_results);
77 
78   // Send scan request to kernel for interface with index |interface_index|.
79   // - |request_random_mac| If true, request device/driver to use a random MAC
80   // address during scan. Requires |supports_random_mac_sched_scan|
81   // address during scan.
82   // - |scan_type| Type of scan to perform. One of,
83   // |SCAN_TYPE_LOW_SPAN| (prioritize to reduce latency over other scan
84   // performance attributes),
85   // |SCAN_TYPE_LOW_POWER| (prioritize to reduce power consumption over other
86   // scan performance attributes),
87   // |SCAN_TYPE_HIGH_ACCURACY| (prioritize to increase accuracy over other scan
88   // performance atrributes) OR
89   // |SCAN_TYPE_DEFAULT| (no prioritization).
90   // - |enable_6ghz_rnr| Whether to scan for collocated 6Ghz APs reported by by 2.4/5Ghz APs.
91   // - |ssids| is a vector of ssids we request to scan, which mostly is used
92   // for hidden networks.
93   // If |ssids| is an empty vector, it will do a passive scan.
94   // If |ssids| contains an empty string, it will a scan for all ssids.
95   // - |freqs| is a vector of frequencies we request to scan.
96   // If |freqs| is an empty vector, it will scan all supported frequencies.
97   // - |error_code| contains the errno kernel replied when this returns false.
98   // Returns true on success.
99   virtual bool Scan(uint32_t interface_index,
100                     bool request_random_mac,
101                     int scan_type,
102                     bool enable_6ghz_rnr,
103                     const std::vector<std::vector<uint8_t>>& ssids,
104                     const std::vector<uint32_t>& freqs,
105                     int* error_code);
106 
107   // Send scan request to kernel for interface with index |interface_index|.
108   // - |inteval_ms| is the expected scan interval in milliseconds.
109   // - |rssi_threshold_2g| is the minimum RSSI threshold value as a filter for
110   // 2GHz band.
111   // - |rssi_threshold_5g| is the minimum RSSI threshold value as a filter for
112   // 5GHz band.
113   // - |scan_ssids| is a vector of ssids we request to scan, which is mostly
114   // used for hidden networks.
115   // - |request_random_mac| If true, request device/driver to use a random MAC
116   // address during scan. Requires |supports_random_mac_sched_scan|
117   // - |request_low_power|: If true, prioritize power consumption over
118   // other scan performance attributes.
119   // Requires |supports_low_power_oneshot_scan|.
120   // - |request_sched_scan_relative_rssi| is sched_scan flag for better BSS's from connected BSS.
121   // If |request_sched_scan_relative_rssi| is true, it will fill scan rssi adjust to
122   // get BSS's with better RSSI from connected BSS.
123   // - |scan_ssids| is the list of ssids to actively scan for.
124   // If |scan_ssids| is an empty vector, it will do a passive scan.
125   // If |scan_ssids| contains an empty string, it will a scan for all ssids.
126   // - |match_ssids| is the list of ssids that we want to add as filters.
127   // - |freqs| is a vector of frequencies we request to scan.
128   // If |freqs| is an empty vector, it will scan all supported frequencies.
129   // - |error_code| contains the errno kernel replied when this returns false.
130   // Only BSSs match the |match_ssids| and |rssi_threshold| will be returned as
131   // scan results.
132   // Returns true on success.
133   virtual bool StartScheduledScan(
134       uint32_t interface_index,
135       const SchedScanIntervalSetting& interval_setting,
136       int32_t rssi_threshold_2g,
137       int32_t rssi_threshold_5g,
138       int32_t rssi_threshold_6g,
139       const SchedScanReqFlags& req_flags,
140       const std::vector<std::vector<uint8_t>>& scan_ssids,
141       const std::vector<std::vector<uint8_t>>& match_ssids,
142       const std::vector<uint32_t>& freqs,
143       int* error_code);
144 
145   // Stop existing scheduled scan on interface with index |interface_index|.
146   // Returns true on success.
147   // Returns false on error or when there is no scheduled scan running.
148   virtual bool StopScheduledScan(uint32_t interface_index);
149 
150   // Abort ongoing single scan on interface with index |interface_index|.
151   // Returns true on success.
152   virtual bool AbortScan(uint32_t interface_index);
153 
154   // Visible for testing.
155   // Get a timestamp for the scan result |bss| represents.
156   // This timestamp records the time passed since boot when last time the
157   // AP was seen.
158   virtual bool GetBssTimestampForTesting(
159       const NL80211NestedAttr& bss,
160        uint64_t* last_seen_since_boot_microseconds);
161 
162   // Sign up to be notified when new scan results are available.
163   // |handler| will be called when the kernel signals to wificond that a scan
164   // has been completed on the given |interface_index|.  See the declaration of
165   // OnScanResultsReadyHandler for documentation on the semantics of this
166   // callback.
167   virtual void SubscribeScanResultNotification(
168       uint32_t interface_index,
169       OnScanResultsReadyHandler handler);
170 
171   // Cancel the sign-up of receiving new scan result notification from
172   // interface with index |interface_index|.
173   virtual void UnsubscribeScanResultNotification(uint32_t interface_index);
174 
175   // Sign up to be notified when new scan results are available.
176   // |handler| will be called when the kernel signals to wificond that a
177   // scheduled scan has been completed on the given |interface_index|.
178   // See the declaration of OnSchedScanResultsReadyHandler for documentation
179   // on the semantics of this callback.
180   virtual void SubscribeSchedScanResultNotification(
181       uint32_t interface_index,
182       OnSchedScanResultsReadyHandler handler);
183 
184   // Cancel the sign-up of receiving new scheduled scan result notification from
185   // interface with index |interface_index|.
186   virtual void UnsubscribeSchedScanResultNotification(uint32_t interface_index);
187 
188  private:
189   bool GetBssTimestamp(const NL80211NestedAttr& bss,
190                        uint64_t* last_seen_since_boot_microseconds);
191   bool ParseRadioChainInfos(
192       const NL80211NestedAttr& bss,
193       std::vector<android::net::wifi::nl80211::RadioChainInfo>
194         *radio_chain_infos);
195   bool GetSSIDFromInfoElement(const std::vector<uint8_t>& ie,
196                               std::vector<uint8_t>* ssid);
197   // Converts a NL80211_CMD_NEW_SCAN_RESULTS packet to a ScanResult object.
198   bool ParseScanResult(
199       std::unique_ptr<const NL80211Packet> packet,
200       android::net::wifi::nl80211::NativeScanResult* scan_result);
201 
202   NetlinkManager* netlink_manager_;
203 
204   DISALLOW_COPY_AND_ASSIGN(ScanUtils);
205 };
206 
207 }  // namespace wificond
208 }  // namespace android
209 
210 #endif  // WIFICOND_SCANNING_SCAN_UTILS_H_
211