• 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   // - |ssids| is a vector of ssids we request to scan, which mostly is used
91   // for hidden networks.
92   // If |ssids| is an empty vector, it will do a passive scan.
93   // If |ssids| contains an empty string, it will a scan for all ssids.
94   // - |freqs| is a vector of frequencies we request to scan.
95   // If |freqs| is an empty vector, it will scan all supported frequencies.
96   // - |error_code| contains the errno kernel replied when this returns false.
97   // Returns true on success.
98   virtual bool Scan(uint32_t interface_index,
99                     bool request_random_mac,
100                     int scan_type,
101                     const std::vector<std::vector<uint8_t>>& ssids,
102                     const std::vector<uint32_t>& freqs,
103                     int* error_code);
104 
105   // Send scan request to kernel for interface with index |interface_index|.
106   // - |inteval_ms| is the expected scan interval in milliseconds.
107   // - |rssi_threshold_2g| is the minimum RSSI threshold value as a filter for
108   // 2GHz band.
109   // - |rssi_threshold_5g| is the minimum RSSI threshold value as a filter for
110   // 5GHz band.
111   // - |scan_ssids| is a vector of ssids we request to scan, which is mostly
112   // used for hidden networks.
113   // - |request_random_mac| If true, request device/driver to use a random MAC
114   // address during scan. Requires |supports_random_mac_sched_scan|
115   // - |request_low_power|: If true, prioritize power consumption over
116   // other scan performance attributes.
117   // Requires |supports_low_power_oneshot_scan|.
118   // - |request_sched_scan_relative_rssi| is sched_scan flag for better BSS's from connected BSS.
119   // If |request_sched_scan_relative_rssi| is true, it will fill scan rssi adjust to
120   // get BSS's with better RSSI from connected BSS.
121   // - |scan_ssids| is the list of ssids to actively scan for.
122   // If |scan_ssids| is an empty vector, it will do a passive scan.
123   // If |scan_ssids| contains an empty string, it will a scan for all ssids.
124   // - |match_ssids| is the list of ssids that we want to add as filters.
125   // - |freqs| is a vector of frequencies we request to scan.
126   // If |freqs| is an empty vector, it will scan all supported frequencies.
127   // - |error_code| contains the errno kernel replied when this returns false.
128   // Only BSSs match the |match_ssids| and |rssi_threshold| will be returned as
129   // scan results.
130   // Returns true on success.
131   virtual bool StartScheduledScan(
132       uint32_t interface_index,
133       const SchedScanIntervalSetting& interval_setting,
134       int32_t rssi_threshold_2g,
135       int32_t rssi_threshold_5g,
136       int32_t rssi_threshold_6g,
137       const SchedScanReqFlags& req_flags,
138       const std::vector<std::vector<uint8_t>>& scan_ssids,
139       const std::vector<std::vector<uint8_t>>& match_ssids,
140       const std::vector<uint32_t>& freqs,
141       int* error_code);
142 
143   // Stop existing scheduled scan on interface with index |interface_index|.
144   // Returns true on success.
145   // Returns false on error or when there is no scheduled scan running.
146   virtual bool StopScheduledScan(uint32_t interface_index);
147 
148   // Abort ongoing single scan on interface with index |interface_index|.
149   // Returns true on success.
150   virtual bool AbortScan(uint32_t interface_index);
151 
152   // Visible for testing.
153   // Get a timestamp for the scan result |bss| represents.
154   // This timestamp records the time passed since boot when last time the
155   // AP was seen.
156   virtual bool GetBssTimestampForTesting(
157       const NL80211NestedAttr& bss,
158        uint64_t* last_seen_since_boot_microseconds);
159 
160   // Sign up to be notified when new scan results are available.
161   // |handler| will be called when the kernel signals to wificond that a scan
162   // has been completed on the given |interface_index|.  See the declaration of
163   // OnScanResultsReadyHandler for documentation on the semantics of this
164   // callback.
165   virtual void SubscribeScanResultNotification(
166       uint32_t interface_index,
167       OnScanResultsReadyHandler handler);
168 
169   // Cancel the sign-up of receiving new scan result notification from
170   // interface with index |interface_index|.
171   virtual void UnsubscribeScanResultNotification(uint32_t interface_index);
172 
173   // Sign up to be notified when new scan results are available.
174   // |handler| will be called when the kernel signals to wificond that a
175   // scheduled scan has been completed on the given |interface_index|.
176   // See the declaration of OnSchedScanResultsReadyHandler for documentation
177   // on the semantics of this callback.
178   virtual void SubscribeSchedScanResultNotification(
179       uint32_t interface_index,
180       OnSchedScanResultsReadyHandler handler);
181 
182   // Cancel the sign-up of receiving new scheduled scan result notification from
183   // interface with index |interface_index|.
184   virtual void UnsubscribeSchedScanResultNotification(uint32_t interface_index);
185 
186  private:
187   bool GetBssTimestamp(const NL80211NestedAttr& bss,
188                        uint64_t* last_seen_since_boot_microseconds);
189   bool ParseRadioChainInfos(
190       const NL80211NestedAttr& bss,
191       std::vector<android::net::wifi::nl80211::RadioChainInfo>
192         *radio_chain_infos);
193   bool GetSSIDFromInfoElement(const std::vector<uint8_t>& ie,
194                               std::vector<uint8_t>* ssid);
195   // Converts a NL80211_CMD_NEW_SCAN_RESULTS packet to a ScanResult object.
196   bool ParseScanResult(
197       std::unique_ptr<const NL80211Packet> packet,
198       android::net::wifi::nl80211::NativeScanResult* scan_result);
199 
200   NetlinkManager* netlink_manager_;
201 
202   DISALLOW_COPY_AND_ASSIGN(ScanUtils);
203 };
204 
205 }  // namespace wificond
206 }  // namespace android
207 
208 #endif  // WIFICOND_SCANNING_SCAN_UTILS_H_
209