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 #include "wificond/scanning/scanner_impl.h"
22 #include <android-base/logging.h>
24 #include "wificond/client_interface_impl.h"
25 #include "wificond/scanning/scan_utils.h"
27 using android::binder::Status;
28 using android::net::wifi::IPnoScanEvent;
29 using android::net::wifi::IScanEvent;
31 using com::android::server::wifi::wificond::NativeScanResult;
32 using com::android::server::wifi::wificond::PnoSettings;
33 using com::android::server::wifi::wificond::SingleScanSettings;
38 using namespace std::placeholders;
43 ScannerImpl::ScannerImpl(uint32_t wiphy_index,
44 uint32_t interface_index,
45 const ScanCapabilities& scan_capabilities,
46 const WiphyFeatures& wiphy_features,
47 ClientInterfaceImpl* client_interface,
48 NetlinkUtils* netlink_utils,
49 ScanUtils* scan_utils)
52 pno_scan_started_(false),
53 wiphy_index_(wiphy_index),
54 interface_index_(interface_index),
55 scan_capabilities_(scan_capabilities),
56 wiphy_features_(wiphy_features),
57 client_interface_(client_interface),
58 netlink_utils_(netlink_utils),
59 scan_utils_(scan_utils),
60 scan_event_handler_(nullptr) {
61 // Subscribe one-shot scan result notification from kernel.
62 LOG(INFO) << "subscribe scan result for interface with index: "
63 << (int)interface_index_;
64 scan_utils_->SubscribeScanResultNotification(
66 std::bind(&ScannerImpl::OnScanResultsReady,
69 // Subscribe scheduled scan result notification from kernel.
70 scan_utils_->SubscribeSchedScanResultNotification(
72 std::bind(&ScannerImpl::OnSchedScanResultsReady,
77 ScannerImpl::~ScannerImpl() {
80 void ScannerImpl::Invalidate() {
81 LOG(INFO) << "Unsubscribe scan result for interface with index: "
82 << (int)interface_index_;
83 scan_utils_->UnsubscribeScanResultNotification(interface_index_);
84 scan_utils_->UnsubscribeSchedScanResultNotification(interface_index_);
87 bool ScannerImpl::CheckIsValid() {
89 LOG(DEBUG) << "Calling on a invalid scanner object."
90 << "Underlying client interface object was destroyed.";
95 Status ScannerImpl::getAvailable2gChannels(
96 std::unique_ptr<vector<int32_t>>* out_frequencies) {
97 if (!CheckIsValid()) {
101 if (!netlink_utils_->GetWiphyInfo(wiphy_index_,
105 LOG(ERROR) << "Failed to get wiphy info from kernel";
106 out_frequencies->reset(nullptr);
110 out_frequencies->reset(new vector<int32_t>(band_info.band_2g.begin(),
111 band_info.band_2g.end()));
115 Status ScannerImpl::getAvailable5gNonDFSChannels(
116 std::unique_ptr<vector<int32_t>>* out_frequencies) {
117 if (!CheckIsValid()) {
121 if (!netlink_utils_->GetWiphyInfo(wiphy_index_,
125 LOG(ERROR) << "Failed to get wiphy info from kernel";
126 out_frequencies->reset(nullptr);
130 out_frequencies->reset(new vector<int32_t>(band_info.band_5g.begin(),
131 band_info.band_5g.end()));
135 Status ScannerImpl::getAvailableDFSChannels(
136 std::unique_ptr<vector<int32_t>>* out_frequencies) {
137 if (!CheckIsValid()) {
141 if (!netlink_utils_->GetWiphyInfo(wiphy_index_,
145 LOG(ERROR) << "Failed to get wiphy info from kernel";
146 out_frequencies->reset(nullptr);
150 out_frequencies->reset(new vector<int32_t>(band_info.band_dfs.begin(),
151 band_info.band_dfs.end()));
155 Status ScannerImpl::getScanResults(vector<NativeScanResult>* out_scan_results) {
156 if (!CheckIsValid()) {
159 if (!scan_utils_->GetScanResult(interface_index_, out_scan_results)) {
160 LOG(ERROR) << "Failed to get scan results via NL80211";
165 Status ScannerImpl::scan(const SingleScanSettings& scan_settings,
167 if (!CheckIsValid()) {
172 LOG(WARNING) << "Scan already started";
174 // Only request MAC address randomization when station is not associated.
175 bool request_random_mac = wiphy_features_.supports_random_mac_oneshot_scan &&
176 !client_interface_->IsAssociated();
178 // Initialize it with an empty ssid for a wild card scan.
179 vector<vector<uint8_t>> ssids = {{}};
180 for (auto& network : scan_settings.hidden_networks_) {
181 if (ssids.size() + 1 > scan_capabilities_.max_num_scan_ssids) {
182 LOG(WARNING) << "Skip scan ssid for single scan: "
183 << string(network.ssid_.begin(), network.ssid_.end());
186 ssids.push_back(network.ssid_);
189 vector<uint32_t> freqs;
190 for (auto& channel : scan_settings.channel_settings_) {
191 freqs.push_back(channel.frequency_);
194 if (!scan_utils_->Scan(interface_index_, request_random_mac, ssids, freqs)) {
195 *out_success = false;
198 scan_started_ = true;
203 Status ScannerImpl::startPnoScan(const PnoSettings& pno_settings,
205 if (!CheckIsValid()) {
208 if (pno_scan_started_) {
209 LOG(WARNING) << "Pno scan already started";
211 // An empty ssid for a wild card scan.
212 vector<vector<uint8_t>> scan_ssids = {{}};
213 vector<vector<uint8_t>> match_ssids;
214 // Empty frequency list: scan all frequencies.
215 vector<uint32_t> freqs;
217 for (auto& network : pno_settings.pno_networks_) {
218 // Add hidden network ssid.
219 if (network.is_hidden_) {
220 if (scan_ssids.size() + 1 > scan_capabilities_.max_num_sched_scan_ssids) {
221 LOG(WARNING) << "Skip scan ssid for pno scan: "
222 << string(network.ssid_.begin(), network.ssid_.end());
225 scan_ssids.push_back(network.ssid_);
228 if (match_ssids.size() + 1 > scan_capabilities_.max_match_sets) {
229 LOG(WARNING) << "Skip match ssid for pno scan: "
230 << string(network.ssid_.begin(), network.ssid_.end());
233 match_ssids.push_back(network.ssid_);
236 // Only request MAC address randomization when station is not associated.
237 bool request_random_mac = wiphy_features_.supports_random_mac_sched_scan &&
238 !client_interface_->IsAssociated();
240 if (!scan_utils_->StartScheduledScan(interface_index_,
241 pno_settings.interval_ms_,
242 // TODO: honor both rssi thresholds.
243 pno_settings.min_2g_rssi_,
248 *out_success = false;
249 LOG(ERROR) << "Failed to start scheduled scan";
252 pno_scan_started_ = true;
257 Status ScannerImpl::stopPnoScan(bool* out_success) {
258 if (!CheckIsValid()) {
262 if (!pno_scan_started_) {
263 LOG(WARNING) << "No pno scan started";
265 if (!scan_utils_->StopScheduledScan(interface_index_)) {
266 *out_success = false;
269 pno_scan_started_ = false;
274 Status ScannerImpl::subscribeScanEvents(const sp<IScanEvent>& handler) {
275 if (!CheckIsValid()) {
279 if (scan_event_handler_ != nullptr) {
280 LOG(ERROR) << "Found existing scan events subscriber."
281 << " This subscription request will unsubscribe it";
283 scan_event_handler_ = handler;
287 Status ScannerImpl::unsubscribeScanEvents() {
288 scan_event_handler_ = nullptr;
293 Status ScannerImpl::subscribePnoScanEvents(const sp<IPnoScanEvent>& handler) {
294 if (!CheckIsValid()) {
298 if (pno_scan_event_handler_ != nullptr) {
299 LOG(ERROR) << "Found existing pno scan events subscriber."
300 << " This subscription request will unsubscribe it";
302 pno_scan_event_handler_ = handler;
307 Status ScannerImpl::unsubscribePnoScanEvents() {
308 pno_scan_event_handler_ = nullptr;
312 void ScannerImpl::OnScanResultsReady(
313 uint32_t interface_index,
315 vector<vector<uint8_t>>& ssids,
316 vector<uint32_t>& frequencies) {
317 LOG(INFO) << "Received scan result notification from kernel.";
318 scan_started_ = false;
319 if (scan_event_handler_ != nullptr) {
320 // TODO: Pass other parameters back once we find framework needs them.
322 LOG(WARNING) << "Scan aborted";
323 scan_event_handler_->OnScanFailed();
325 scan_event_handler_->OnScanResultReady();
328 LOG(WARNING) << "No scan event handler found.";
332 void ScannerImpl::OnSchedScanResultsReady(uint32_t interface_index,
334 if (pno_scan_event_handler_ != nullptr) {
336 // If |pno_scan_started_| is false.
337 // This stop notification might result from our own request.
338 // See the document for NL80211_CMD_SCHED_SCAN_STOPPED in nl80211.h.
339 if (pno_scan_started_) {
340 pno_scan_event_handler_->OnPnoScanFailed();
342 pno_scan_started_ = false;
344 pno_scan_event_handler_->OnPnoNetworkFound();
349 } // namespace wificond
350 } // namespace android