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 com {
28 namespace android {
29 namespace server {
30 namespace wifi {
31 namespace wificond {
32 
33 class NativeScanResult;
34 
35 }  // namespace wificond
36 }  // namespace wifi
37 }  // namespace server
38 }  // namespace android
39 }  // namespace com
40 
41 namespace android {
42 namespace wificond {
43 
44 class NL80211Packet;
45 
46 // Provides scanning helper functions.
47 class ScanUtils {
48  public:
49   explicit ScanUtils(NetlinkManager* netlink_manager);
50   virtual ~ScanUtils();
51 
52   // Send 'get scan results' request to kernel and get the latest scan results.
53   // |interface_index| is the index of interface we want to get scan results
54   // from.
55   // A vector of ScanResult object will be returned by |*out_scan_results|.
56   // Returns true on success.
57   virtual bool GetScanResult(
58       uint32_t interface_index,
59       std::vector<::com::android::server::wifi::wificond::NativeScanResult>* out_scan_results);
60 
61   // Send scan request to kernel for interface with index |interface_index|.
62   // |request_random_mac| is used for asking device/driver to use a random MAC
63   // address during scan.
64   // This flag should only be set if kernel supports this feature as
65   // |supports_random_mac_oneshot_scan| indicates.
66   // |ssids| is a vector of ssids we request to scan, which mostly is used
67   // for hidden networks.
68   // If |ssids| is an empty vector, it will do a passive scan.
69   // If |ssids| contains an empty string, it will a scan for all ssids.
70   // |freqs| is a vector of frequencies we request to scan.
71   // If |freqs| is an empty vector, it will scan all supported frequencies.
72   // Returns true on success.
73   virtual bool Scan(uint32_t interface_index,
74                     bool request_random_mac,
75                     const std::vector<std::vector<uint8_t>>& ssids,
76                     const std::vector<uint32_t>& freqs);
77 
78   // Send scan request to kernel for interface with index |interface_index|.
79   // |inteval_ms| is the expected scan interval in milliseconds.
80   // |rssi_threshold| is the minimum RSSI threshold value as a filter.
81   // |scan_ssids| is a vector of ssids we request to scan, which is mostly
82   // used for hidden networks.
83   // |request_random_mac| is used for asking device/driver to use a random MAC
84   // address during scan.
85   // This flag should only be set if kernel supports this feature as
86   // |supports_random_mac_sched_scan| indicates.
87   // If |scan_ssids| is an empty vector, it will do a passive scan.
88   // If |scan_ssids| contains an empty string, it will a scan for all ssids.
89   // |freqs| is a vector of frequencies we request to scan.
90   // |match_ssids| is the list of ssids that we want to add as filters.
91   // If |freqs| is an empty vector, it will scan all supported frequencies.
92   // Only BSSs match the |match_ssids| and |rssi_threshold| will be returned as
93   // scan results.
94   // Returns true on success.
95   virtual bool StartScheduledScan(
96       uint32_t interface_index,
97       uint32_t interval_ms,
98       int32_t rssi_threshold,
99       bool request_random_mac,
100       const std::vector<std::vector<uint8_t>>& scan_ssids,
101       const std::vector<std::vector<uint8_t>>& match_ssids,
102       const std::vector<uint32_t>& freqs);
103 
104   // Stop existing scheduled scan on interface with index |interface_index|.
105   // Returns true on success.
106   // Returns false on error or when there is no scheduled scan running.
107   virtual bool StopScheduledScan(uint32_t interface_index);
108 
109   // Abort ongoing single scan on interface with index |interface_index|.
110   // Returns true on success.
111   virtual bool AbortScan(uint32_t interface_index);
112 
113   // Sign up to be notified when new scan results are available.
114   // |handler| will be called when the kernel signals to wificond that a scan
115   // has been completed on the given |interface_index|.  See the declaration of
116   // OnScanResultsReadyHandler for documentation on the semantics of this
117   // callback.
118   virtual void SubscribeScanResultNotification(
119       uint32_t interface_index,
120       OnScanResultsReadyHandler handler);
121 
122   // Cancel the sign-up of receiving new scan result notification from
123   // interface with index |interface_index|.
124   virtual void UnsubscribeScanResultNotification(uint32_t interface_index);
125 
126   // Sign up to be notified when new scan results are available.
127   // |handler| will be called when the kernel signals to wificond that a
128   // scheduled scan has been completed on the given |interface_index|.
129   // See the declaration of OnSchedScanResultsReadyHandler for documentation
130   // on the semantics of this callback.
131   virtual void SubscribeSchedScanResultNotification(
132       uint32_t interface_index,
133       OnSchedScanResultsReadyHandler handler);
134 
135   // Cancel the sign-up of receiving new scheduled scan result notification from
136   // interface with index |interface_index|.
137   virtual void UnsubscribeSchedScanResultNotification(uint32_t interface_index);
138 
139  private:
140   bool GetSSIDFromInfoElement(const std::vector<uint8_t>& ie,
141                               std::vector<uint8_t>* ssid);
142   // Converts a NL80211_CMD_NEW_SCAN_RESULTS packet to a ScanResult object.
143   bool ParseScanResult(
144       std::unique_ptr<const NL80211Packet> packet,
145       ::com::android::server::wifi::wificond::NativeScanResult* scan_result);
146 
147   NetlinkManager* netlink_manager_;
148 
149   DISALLOW_COPY_AND_ASSIGN(ScanUtils);
150 };
151 
152 }  // namespace wificond
153 }  // namespace android
154 
155 #endif  // WIFICOND_SCANNING_SCAN_UTILS_H_
156