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
17 #include "include/utility.h"
18
19 using RpcLog = wifi_offload::RpcLogRecord::RpcLogRecordType;
20
21 namespace wifi_offload_test {
22
init(uint8_t & val,RandomGenerator & rand_gen)23 void init(uint8_t &val, RandomGenerator &rand_gen) {
24 val = rand_gen.get<uint8_t>();
25 }
26
init(uint16_t & val,RandomGenerator & rand_gen)27 void init(uint16_t &val, RandomGenerator &rand_gen) {
28 val = rand_gen.get<uint16_t>();
29 }
30
init(uint32_t & val,RandomGenerator & rand_gen)31 void init(uint32_t &val, RandomGenerator &rand_gen) {
32 val = rand_gen.get<uint32_t>();
33 }
34
init(uint64_t & val,RandomGenerator & rand_gen)35 void init(uint64_t &val, RandomGenerator &rand_gen) {
36 val = rand_gen.get<uint64_t>();
37 }
38
init(uint8_t * arr,size_t len,RandomGenerator & rand_gen)39 void init(uint8_t *arr, size_t len, RandomGenerator &rand_gen) {
40 for (size_t i = 0; i < len; i++) {
41 arr[i] = rand_gen.get<uint8_t>();
42 }
43 }
44
init_security_mode(uint8_t & sec_mode,RandomGenerator & rand_gen)45 void init_security_mode(uint8_t &sec_mode, RandomGenerator &rand_gen) {
46 init(sec_mode, rand_gen);
47 sec_mode &= 0x0f;
48 }
49
init_rssi(int8_t & rssi,RandomGenerator & rand_gen)50 void init_rssi(int8_t &rssi, RandomGenerator &rand_gen) {
51 rssi = rand_gen.get<int8_t>();
52 if (rssi > 0) {
53 rssi = -rssi;
54 }
55 }
56
init_rpc_log_record(RpcLog & log_record,RandomGenerator & rand_gen)57 void init_rpc_log_record(RpcLog &log_record, RandomGenerator &rand_gen) {
58 log_record = static_cast<RpcLog>((rand_gen.get<uint32_t>() % 6u) +
59 static_cast<uint32_t>(RpcLog::CMD_BASE) + 1);
60 }
61
init_frequency(uint32_t & freq,RandomGenerator & rand_gen)62 void init_frequency(uint32_t &freq, RandomGenerator &rand_gen) {
63 freq = kAllFrequencies_Test[rand_gen.get<uint8_t>() % kNumFrequencies_Test];
64 }
65
init_capability(uint16_t & capa,RandomGenerator & rand_gen)66 void init_capability(uint16_t &capa, RandomGenerator &rand_gen) {
67 capa = (rand_gen.get<uint16_t>() % 0xffff) + 1;
68 }
69
init(wifi_offload::Ssid & ssid,RandomGenerator & rand_gen)70 void init(wifi_offload::Ssid &ssid, RandomGenerator &rand_gen) {
71 uint8_t rand_ssid[wifi_offload::Ssid::kMaxSsidLen];
72 size_t len = (rand_gen.get<uint8_t>() % wifi_offload::Ssid::kMaxSsidLen) + 1;
73 init(rand_ssid, len, rand_gen);
74 ssid.SetData(rand_ssid, len);
75 }
76
init(wifi_offload::PreferredNetwork & net_info,RandomGenerator & rand_gen)77 void init(wifi_offload::PreferredNetwork &net_info, RandomGenerator &rand_gen) {
78 init(net_info.ssid_, rand_gen);
79 init_security_mode(net_info.security_modes_, rand_gen);
80 }
81
init(wifi_offload::ScanRecord & record,RandomGenerator & rand_gen)82 void init(wifi_offload::ScanRecord &record, RandomGenerator &rand_gen) {
83 init(record.time_spent_scanning_ms_, rand_gen);
84 init(record.num_channels_scanned_, rand_gen);
85 init(record.num_entries_aggregated_, rand_gen);
86 }
87
init(wifi_offload::RpcLogRecord & record,RandomGenerator & rand_gen)88 void init(wifi_offload::RpcLogRecord &record, RandomGenerator &rand_gen) {
89 init_rpc_log_record(record.record_type_, rand_gen);
90 init(record.timestamp_chre_ms_, rand_gen);
91 }
92
init(wifi_offload::ChannelHistogram & histo,RandomGenerator & rand_gen)93 void init(wifi_offload::ChannelHistogram &histo, RandomGenerator &rand_gen) {
94 for (size_t i = 0; i < kNumFrequencies_Test; i++) {
95 histo.IncrementScanCountForFrequencyForTest(kAllFrequencies_Test[i],
96 rand_gen.get<uint16_t>());
97 }
98 }
99
init(wifi_offload::ScanStats & stats,RandomGenerator & rand_gen)100 void init(wifi_offload::ScanStats &stats, RandomGenerator &rand_gen) {
101 init(stats.num_scans_requested_by_nanoapp_, rand_gen);
102 init(stats.num_scans_serviced_by_hardware_, rand_gen);
103 init(stats.num_scans_serviced_by_cache_, rand_gen);
104 init(stats.updated_at_chre_ms_, rand_gen);
105 init(stats.sent_at_chre_ms_, rand_gen);
106 init(stats.last_subscription_duration_ms_, rand_gen);
107 init(stats.channel_histogram_, rand_gen);
108 init<wifi_offload::ScanRecord>(stats.scan_records_, rand_gen);
109 init<wifi_offload::RpcLogRecord>(stats.rpc_log_records_, rand_gen);
110 }
111
init(wifi_offload::ScanParams & params,RandomGenerator & rand_gen)112 void init(wifi_offload::ScanParams ¶ms, RandomGenerator &rand_gen) {
113 init<wifi_offload::Ssid>(params.ssids_to_scan_, rand_gen);
114 // Need to init frequency vector with valid frequency values
115 // init<uint32_t>(params.frequencies_to_scan_mhz_, rand_gen);
116 params.frequencies_to_scan_mhz_.clear();
117 size_t vec_len = (rand_gen.get<uint8_t>() % 10) + 1;
118 for (size_t i = 0; i < vec_len; i++) {
119 uint32_t new_freq;
120 init_frequency(new_freq, rand_gen);
121 params.frequencies_to_scan_mhz_.push_back(new_freq);
122 }
123 init(params.disconnected_mode_scan_interval_ms_, rand_gen);
124 }
125
init(wifi_offload::ScanFilter & filter,RandomGenerator & rand_gen)126 void init(wifi_offload::ScanFilter &filter, RandomGenerator &rand_gen) {
127 init<wifi_offload::PreferredNetwork>(filter.networks_to_match_, rand_gen);
128 init_rssi(filter.min_rssi_threshold_dbm_, rand_gen);
129 }
130
init(wifi_offload::ScanConfig & config,RandomGenerator & rand_gen)131 void init(wifi_offload::ScanConfig &config, RandomGenerator &rand_gen) {
132 init(config.scan_params_, rand_gen);
133 init(config.scan_filter_, rand_gen);
134 }
135
init(wifi_offload::ScanResult & result,RandomGenerator & rand_gen)136 void init(wifi_offload::ScanResult &result, RandomGenerator &rand_gen) {
137 init(result.ssid_, rand_gen);
138 init_security_mode(result.security_modes_, rand_gen);
139 init(result.bssid_, wifi_offload::ScanResult::kBssidSize, rand_gen);
140 init_capability(result.capability_, rand_gen);
141 init_frequency(result.frequency_scanned_mhz_, rand_gen);
142 init_rssi(result.rssi_dbm_, rand_gen);
143 init(result.tsf_, rand_gen);
144 }
145
init(chreWifiScanResult & result,RandomGenerator & rand_gen)146 void init(chreWifiScanResult &result, RandomGenerator &rand_gen) {
147 init(result.ageMs, rand_gen);
148 init_capability(result.capabilityInfo, rand_gen);
149 result.ssidLen =
150 (rand_gen.get<uint8_t>() % wifi_offload::Ssid::kMaxSsidLen) + 1;
151 init(result.ssid, result.ssidLen, rand_gen);
152 init(result.bssid, CHRE_WIFI_BSSID_LEN, rand_gen);
153 init(result.flags, rand_gen);
154 init_rssi(result.rssi, rand_gen);
155 init(result.band, rand_gen);
156 init_frequency(result.primaryChannel, rand_gen);
157 init(result.centerFreqPrimary, rand_gen);
158 init(result.centerFreqSecondary, rand_gen);
159 result.channelWidth = CHRE_WIFI_CHANNEL_WIDTH_20_MHZ;
160 init_security_mode(result.securityMode, rand_gen);
161 }
162
163 } // namespace wifi_offload_test
164