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 <algorithm>
18 
19 #include "chre/apps/wifi_offload/chre_scan_params_safe.h"
20 #include "chre/apps/wifi_offload/utility.h"
21 
22 namespace wifi_offload {
23 
ChreScanParamsSafe(const ScanParams & scan_params)24 ChreScanParamsSafe::ChreScanParamsSafe(const ScanParams &scan_params) {
25   // point chre_scan_params_ pointers to allocated buffers
26   chre_scan_params_.frequencyList = chre_scan_params_frequencies_;
27   chre_scan_params_.ssidList = chre_scan_params_ssids_;
28 
29   UpdateFromScanParams(scan_params);
30 }
31 
GetChreWifiScanParams()32 const chreWifiScanParams *ChreScanParamsSafe::GetChreWifiScanParams() {
33   return &chre_scan_params_;
34 }
35 
Log() const36 void ChreScanParamsSafe::Log() const {
37   LOGI("chreWifiScanParams:");
38   LOGI("  scan type: %" PRIu8, chre_scan_params_.scanType);
39   LOGI("  max scan age (ms): %" PRIu32, chre_scan_params_.maxScanAgeMs);
40   LOGI("  frequency list length: %" PRIu16, chre_scan_params_.frequencyListLen);
41   for (size_t i = 0; i < chre_scan_params_.frequencyListLen; i++) {
42     LOGI("  frequency: %" PRIu32, chre_scan_params_.frequencyList[i]);
43   }
44   LOGI("  ssid list length: %" PRIu8, chre_scan_params_.ssidListLen);
45   for (size_t i = 0; i < chre_scan_params_.ssidListLen; i++) {
46     utility::LogSsid(chre_scan_params_.ssidList[i].ssid,
47                      chre_scan_params_.ssidList[i].ssidLen);
48   }
49 }
50 
UpdateFromScanParams(const ScanParams & scan_params)51 void ChreScanParamsSafe::UpdateFromScanParams(const ScanParams &scan_params) {
52   chre_scan_params_.scanType = CHRE_WIFI_SCAN_TYPE_ACTIVE_PLUS_PASSIVE_DFS;
53   chre_scan_params_.maxScanAgeMs = 0;
54 
55   chre_scan_params_.frequencyListLen = std::min(
56       static_cast<uint16_t>(scan_params.frequencies_to_scan_mhz_.size()),
57       static_cast<uint16_t>(CHRE_WIFI_FREQUENCY_LIST_MAX_LEN));
58   if (chre_scan_params_.frequencyListLen <
59       scan_params.frequencies_to_scan_mhz_.size()) {
60     LOGW("Too many ScanParams frequencies %zu truncated to max %" PRIu16,
61          scan_params.frequencies_to_scan_mhz_.size(),
62          chre_scan_params_.frequencyListLen);
63   }
64 
65   std::copy(scan_params.frequencies_to_scan_mhz_.begin(),
66             scan_params.frequencies_to_scan_mhz_.begin() +
67                 chre_scan_params_.frequencyListLen,
68             chre_scan_params_frequencies_);
69 
70   chre_scan_params_.ssidListLen =
71       std::min(static_cast<uint8_t>(scan_params.ssids_to_scan_.size()),
72                static_cast<uint8_t>(CHRE_WIFI_SSID_LIST_MAX_LEN));
73   if (chre_scan_params_.ssidListLen < scan_params.ssids_to_scan_.size()) {
74     LOGW("Too many ScanParams ssids %zu truncated to max %" PRIu8,
75          scan_params.ssids_to_scan_.size(), chre_scan_params_.ssidListLen);
76   }
77 
78   for (size_t i = 0; i < chre_scan_params_.ssidListLen; i++) {
79     scan_params.ssids_to_scan_[i].ToChreWifiSsidListItem(
80         &chre_scan_params_ssids_[i]);
81   }
82 }
83 
84 }  // namespace wifi_offload
85