1 /*
2 * Copyright (C) 2017 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 "offload_hal_test_utils.h"
17
18 namespace android {
19 namespace hardware {
20 namespace wifi {
21 namespace offload {
22 namespace V1_0 {
23 namespace implementation {
24 namespace offload_hal_test_utils {
25
26 using namespace offload_hal_test_constants;
27
createChreScanResultsForTest(std::vector<wifi_offload::ScanResult> * chreScanResults)28 void createChreScanResultsForTest(std::vector<wifi_offload::ScanResult>* chreScanResults) {
29 wifi_offload::ScanResult chreScanResult;
30 chreScanResult.security_modes_ = kNetworkFlags;
31 chreScanResult.capability_ = kCapability;
32 chreScanResult.frequency_scanned_mhz_ = kFrequency1;
33 chreScanResult.rssi_dbm_ = kRssi;
34 chreScanResult.tsf_ = kTsf;
35 chreScanResult.ssid_.SetData(&kSsid1[0], kSsid1_size);
36 memcpy(&chreScanResult.bssid_[0], &kBssid[0], kBssidSize);
37 chreScanResults->push_back(chreScanResult);
38 }
39
createChreScanStatsForTest(wifi_offload::ScanStats * chreScanStats)40 void createChreScanStatsForTest(wifi_offload::ScanStats* chreScanStats) {
41 chreScanStats->num_scans_requested_by_nanoapp_ = kDefaultNumScansRequestedByWifi;
42 chreScanStats->num_scans_serviced_by_hardware_ = kDefaultNumScansServicedByWifi;
43 chreScanStats->num_scans_serviced_by_cache_ =
44 (kDefaultNumScansRequestedByWifi - kDefaultNumScansServicedByWifi);
45 for (size_t j = 0; j < kNumChannelsScanned[0] + kNumChannelsScanned[1]; j++) {
46 chreScanStats->channel_histogram_.IncrementScanCountForChannelForTest(
47 kTestChannels[j], static_cast<uint32_t>(kNumTimesChannelScanned[j]));
48 }
49 chreScanStats->last_subscription_duration_ms_ = kSubscriptionDurationMs;
50 wifi_offload::ScanRecord scanRecord1;
51 scanRecord1.time_spent_scanning_ms_ = kScanDurationMs[0];
52 scanRecord1.num_channels_scanned_ = kNumChannelsScanned[0];
53 scanRecord1.num_entries_aggregated_ = 1;
54 chreScanStats->scan_records_.push_back(scanRecord1);
55 wifi_offload::ScanRecord scanRecord2;
56 scanRecord2.time_spent_scanning_ms_ = kScanDurationMs[1];
57 scanRecord2.num_channels_scanned_ = kNumChannelsScanned[1];
58 scanRecord2.num_entries_aggregated_ = 1;
59 chreScanStats->scan_records_.push_back(scanRecord2);
60
61 for (size_t i = 0; i < kRecordLength; i++) {
62 wifi_offload::RpcLogRecord logRecord;
63 logRecord.record_type_ = kChreRecordTypeList[i];
64 logRecord.timestamp_chre_ms_ = kSystemStartTime + i * kIncrementBetweenEvents;
65 chreScanStats->rpc_log_records_.push_back(logRecord);
66 }
67 }
68
createHidlScanFilterForTest(ScanFilter * scanFilter)69 void createHidlScanFilterForTest(ScanFilter* scanFilter) {
70 std::vector<NetworkInfo> nw_info_list;
71 NetworkInfo nwInfo1;
72 std::vector<uint8_t> ssid1(kSsid1, kSsid1 + kSsid1_size);
73 nwInfo1.ssid = ssid1;
74 nwInfo1.flags = kNetworkFlags;
75 NetworkInfo nwInfo2;
76 std::vector<uint8_t> ssid2(kSsid2, kSsid2 + kSsid2_size);
77 nwInfo2.ssid = ssid2;
78 nwInfo2.flags = kNetworkFlags;
79 nw_info_list.push_back(nwInfo1);
80 nw_info_list.push_back(nwInfo2);
81 scanFilter->preferredNetworkInfoList = nw_info_list;
82 scanFilter->rssiThreshold = kRssiThreshold;
83 }
84
createHidlScanParamForTest(ScanParam * scanParam)85 void createHidlScanParamForTest(ScanParam* scanParam) {
86 scanParam->disconnectedModeScanIntervalMs = kDisconnectedModeScanIntervalMs;
87 std::vector<uint32_t> frequencyList{kFrequency1, kFrequency2};
88 scanParam->frequencyList = frequencyList;
89 std::vector<hidl_vec<uint8_t>> ssidList;
90 std::vector<uint8_t> ssid1(kSsid1, kSsid1 + kSsid1_size);
91 std::vector<uint8_t> ssid2(kSsid2, kSsid2 + kSsid2_size);
92 ssidList.push_back(ssid1);
93 ssidList.push_back(ssid2);
94 scanParam->ssidList = ssidList;
95 }
96
createChreScanConfigForTest(wifi_offload::ScanConfig * scanConfig)97 void createChreScanConfigForTest(wifi_offload::ScanConfig* scanConfig) {
98 scanConfig->scan_params_.disconnected_mode_scan_interval_ms_ = kDisconnectedModeScanIntervalMs;
99 wifi_offload::Ssid chreSsid1;
100 chreSsid1.SetData(&kSsid1[0], kSsid1_size);
101 wifi_offload::Ssid chreSsid2;
102 chreSsid2.SetData(&kSsid2[0], kSsid2_size);
103 scanConfig->scan_params_.ssids_to_scan_.push_back(chreSsid1);
104 scanConfig->scan_params_.ssids_to_scan_.push_back(chreSsid2);
105 scanConfig->scan_params_.frequencies_to_scan_mhz_.push_back(kFrequency1);
106 scanConfig->scan_params_.frequencies_to_scan_mhz_.push_back(kFrequency2);
107 scanConfig->scan_filter_.min_rssi_threshold_dbm_ = kRssiThreshold;
108 wifi_offload::PreferredNetwork chreNwInfo1;
109 chreNwInfo1.security_modes_ = kNetworkFlags;
110 chreNwInfo1.ssid_.SetData(&kSsid1[0], kSsid1_size);
111 scanConfig->scan_filter_.networks_to_match_.push_back(std::move(chreNwInfo1));
112 wifi_offload::PreferredNetwork chreNwInfo2;
113 chreNwInfo2.security_modes_ = kNetworkFlags;
114 chreNwInfo2.ssid_.SetData(&kSsid2[0], kSsid2_size);
115 scanConfig->scan_filter_.networks_to_match_.push_back(std::move(chreNwInfo2));
116 }
117
validateScanResult(const std::vector<ScanResult> & hidlScanResultsTest,const std::vector<wifi_offload::ScanResult> chreScanResults)118 bool validateScanResult(const std::vector<ScanResult>& hidlScanResultsTest,
119 const std::vector<wifi_offload::ScanResult> chreScanResults) {
120 if (hidlScanResultsTest.size() != chreScanResults.size())
121 return false;
122 uint32_t i = 0;
123 for (const auto& hidlScanResult : hidlScanResultsTest) {
124 if (hidlScanResult.tsf != chreScanResults[i].tsf_)
125 return false;
126 if (hidlScanResult.rssi != chreScanResults[i].rssi_dbm_)
127 return false;
128 if (hidlScanResult.capability != chreScanResults[i].capability_)
129 return false;
130 if (hidlScanResult.frequency != chreScanResults[i].frequency_scanned_mhz_)
131 return false;
132 for (int j = 0; j < kBssidSize; j++) {
133 if (hidlScanResult.bssid[j] != chreScanResults[i].bssid_[j])
134 return false;
135 }
136 chreWifiSsidListItem chreWifiSsid;
137 chreScanResults[i].ssid_.ToChreWifiSsidListItem(&chreWifiSsid);
138 for (size_t k = 0; k < chreWifiSsid.ssidLen; k++) {
139 if (hidlScanResult.networkInfo.ssid[k] != chreWifiSsid.ssid[k])
140 return false;
141 }
142 if (hidlScanResult.networkInfo.flags != kNetworkFlags) {
143 return false;
144 }
145 i++;
146 }
147 return true;
148 }
149
validateScanStats(const ScanStats & hidlScanStats,const wifi_offload::ScanStats & chreScanStats)150 bool validateScanStats(const ScanStats& hidlScanStats,
151 const wifi_offload::ScanStats& chreScanStats) {
152 if (hidlScanStats.subscriptionDurationMs != chreScanStats.last_subscription_duration_ms_)
153 return false;
154 if (hidlScanStats.numScansRequestedByWifi != chreScanStats.num_scans_requested_by_nanoapp_)
155 return false;
156 if (hidlScanStats.numScansServicedByWifi != chreScanStats.num_scans_serviced_by_hardware_)
157 return false;
158 if (hidlScanStats.numScansServicedbyCache != chreScanStats.num_scans_serviced_by_cache_)
159 return false;
160 for (uint32_t i = 0; i < kNumChannelsInHistogram; i++) {
161 if (hidlScanStats.histogramChannelsScanned[i] !=
162 chreScanStats.channel_histogram_.GetChannelScanCount(i)) {
163 return false;
164 }
165 }
166 if (hidlScanStats.scanRecord.size() != chreScanStats.scan_records_.size())
167 return false;
168 uint32_t i = 0;
169 for (const auto& scanRecord : hidlScanStats.scanRecord) {
170 if (scanRecord.durationMs != chreScanStats.scan_records_[i].time_spent_scanning_ms_)
171 return false;
172 if (scanRecord.numChannelsScanned != chreScanStats.scan_records_[i].num_channels_scanned_)
173 return false;
174 if (scanRecord.numEntriesAggregated !=
175 chreScanStats.scan_records_[i].num_entries_aggregated_)
176 return false;
177 i++;
178 }
179 if (hidlScanStats.logRecord.size() != chreScanStats.rpc_log_records_.size())
180 return false;
181 i = 0;
182
183 for (const auto& logRecord : hidlScanStats.logRecord) {
184 if (logRecord.recordName != kHidlRecordNameList[i]) {
185 return false;
186 }
187 if (logRecord.logTimeMs != chreScanStats.rpc_log_records_[i].timestamp_chre_ms_)
188 return false;
189 i++;
190 }
191 return true;
192 }
193
194 } // namespace offload_hal_test_utils
195 } // namespace implementation
196 } // namespace V1_0
197 } // namespace offload
198 } // namespace wifi
199 } // namespace hardware
200 } // namespace android
201