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 #include "wificond/scanning/offload/offload_scan_utils.h"
17
18 #include <android-base/logging.h>
19 #include <utils/Timers.h>
20
21 #include "wificond/scanning/offload/scan_stats.h"
22 #include "wificond/scanning/scan_result.h"
23
24 using ::com::android::server::wifi::wificond::NativeScanResult;
25 using ::com::android::server::wifi::wificond::NativeScanStats;
26 using android::hardware::wifi::offload::V1_0::ScanResult;
27 using android::hardware::wifi::offload::V1_0::ScanParam;
28 using android::hardware::wifi::offload::V1_0::ScanFilter;
29 using android::hardware::wifi::offload::V1_0::ScanStats;
30 using android::hardware::wifi::offload::V1_0::NetworkInfo;
31 using android::hardware::hidl_vec;
32 using std::vector;
33
34 namespace android {
35 namespace wificond {
36
convertToNativeScanResults(const vector<ScanResult> & scan_result,vector<NativeScanResult> * native_scan_result)37 bool OffloadScanUtils::convertToNativeScanResults(
38 const vector<ScanResult>& scan_result,
39 vector<NativeScanResult>* native_scan_result) {
40 if (native_scan_result == nullptr) return false;
41 for (size_t i = 0; i < scan_result.size(); i++) {
42 NativeScanResult single_scan_result;
43 single_scan_result.ssid.assign(scan_result[i].networkInfo.ssid.begin(),
44 scan_result[i].networkInfo.ssid.end());
45 for (size_t j = 0; j < scan_result[i].bssid.elementCount(); j++) {
46 single_scan_result.bssid.at(j) = scan_result[i].bssid[j];
47 }
48 single_scan_result.frequency = scan_result[i].frequency;
49 single_scan_result.signal_mbm = scan_result[i].rssi;
50 single_scan_result.tsf = systemTime(SYSTEM_TIME_MONOTONIC) / 1000;
51 single_scan_result.capability = scan_result[i].capability;
52 single_scan_result.associated = false;
53 native_scan_result->push_back(std::move(single_scan_result));
54 }
55 return true;
56 }
57
createScanParam(const vector<vector<uint8_t>> & ssid_list,const vector<uint32_t> & frequency_list,uint32_t scan_interval_ms)58 ScanParam OffloadScanUtils::createScanParam(
59 const vector<vector<uint8_t>>& ssid_list,
60 const vector<uint32_t>& frequency_list, uint32_t scan_interval_ms) {
61 ScanParam scan_param;
62 scan_param.disconnectedModeScanIntervalMs = scan_interval_ms;
63 scan_param.frequencyList = frequency_list;
64 vector<hidl_vec<uint8_t>> ssid_list_tmp;
65 for (const auto& ssid : ssid_list) {
66 ssid_list_tmp.push_back(ssid);
67 }
68 scan_param.ssidList = ssid_list_tmp;
69 return scan_param;
70 }
71
createScanFilter(const vector<vector<uint8_t>> & ssids,const vector<uint8_t> & flags,int8_t rssi_threshold)72 ScanFilter OffloadScanUtils::createScanFilter(
73 const vector<vector<uint8_t>>& ssids, const vector<uint8_t>& flags,
74 int8_t rssi_threshold) {
75 ScanFilter scan_filter;
76 vector<NetworkInfo> nw_info_list;
77 size_t i = 0;
78 scan_filter.rssiThreshold = rssi_threshold;
79 // Note that the number of ssids should match the number of security flags
80 for (const auto& ssid : ssids) {
81 NetworkInfo nw_info;
82 nw_info.ssid = ssid;
83 if (i < flags.size()) {
84 nw_info.flags = flags[i++];
85 } else {
86 continue;
87 }
88 nw_info_list.push_back(nw_info);
89 }
90 scan_filter.preferredNetworkInfoList = nw_info_list;
91 return scan_filter;
92 }
93
convertToNativeScanStats(const ScanStats & scanStats)94 NativeScanStats OffloadScanUtils::convertToNativeScanStats(
95 const ScanStats& scanStats) {
96 uint32_t num_channels_scanned = 0;
97 uint32_t scan_duration_ms = 0;
98 vector<uint8_t> histogram_channels;
99
100 for (size_t i = 0; i < scanStats.scanRecord.size(); i++) {
101 scan_duration_ms += scanStats.scanRecord[i].durationMs;
102 num_channels_scanned += scanStats.scanRecord[i].numChannelsScanned;
103 }
104 for (size_t i = 0; i < scanStats.histogramChannelsScanned.size(); i++) {
105 histogram_channels.push_back(scanStats.histogramChannelsScanned[i]);
106 }
107
108 NativeScanStats native_scan_stats(
109 scanStats.numScansRequestedByWifi, scanStats.numScansServicedByWifi,
110 scanStats.subscriptionDurationMs, scan_duration_ms, num_channels_scanned,
111 histogram_channels);
112 return native_scan_stats;
113 }
114
115 } // namespace wificond
116 } // namespace android
117