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 "chre/apps/wifi_offload/scan_params.h"
18#include "chre/apps/wifi_offload/channel_histogram.h"
19#include "chre/apps/wifi_offload/vector_serialization.h"
20
21namespace wifi_offload {
22
23ScanParams::ScanParams() : disconnected_mode_scan_interval_ms_(0) {}
24
25bool ScanParams::operator==(const ScanParams &other) const {
26  if (this == &other) {
27    return true;
28  }
29  return ssids_to_scan_ == other.ssids_to_scan_ &&
30         frequencies_to_scan_mhz_ == other.frequencies_to_scan_mhz_ &&
31         disconnected_mode_scan_interval_ms_ ==
32             other.disconnected_mode_scan_interval_ms_;
33}
34
35flatbuffers::Offset<ScanParams::FbsType> ScanParams::Serialize(
36    flatbuffers::FlatBufferBuilder *builder) const {
37  auto ssid_vec = SerializeVector(ssids_to_scan_, builder);
38  auto freq_vec = builder->CreateVector(frequencies_to_scan_mhz_);
39  return fbs::CreateScanParams(*builder, ssid_vec, freq_vec,
40                               disconnected_mode_scan_interval_ms_);
41}
42
43bool ScanParams::Deserialize(const ScanParams::FbsType &fbs_params) {
44  const auto &ssid_vec = fbs_params.ssids_to_scan();
45  if (ssid_vec == nullptr ||
46      !DeserializeVector<Ssid>(*ssid_vec, &ssids_to_scan_)) {
47    LOGE("Failed to deserialize ScanParams. Null or incomplete members.");
48    return false;
49  }
50
51  const auto &freq_vec = fbs_params.frequencies_to_scan_mhz();
52  if (freq_vec == nullptr) {
53    LOGE("Failed to deserialize ScanParams. Null or incomplete members.");
54    return false;
55  }
56  frequencies_to_scan_mhz_.clear();
57  frequencies_to_scan_mhz_.reserve(freq_vec->size());
58  for (const auto &freq : *freq_vec) {
59    if (!ChannelHistogram::IsSupportedFrequency(freq)) {
60      LOGE("Failed to deserialize ScanParams. Invalid frequency to scan.");
61      return false;
62    }
63    frequencies_to_scan_mhz_.push_back(freq);
64  }
65  disconnected_mode_scan_interval_ms_ =
66      fbs_params.disconnected_mode_scan_interval_ms();
67
68  return true;
69}
70
71void ScanParams::Log() const {
72  LOGI("ScanParams:");
73  LOGI("  disconnected mode scan interval (ms): %" PRIu32,
74       disconnected_mode_scan_interval_ms_);
75  LOGI("  number of ssids to scan: %zu", ssids_to_scan_.size());
76  for (const auto &ssid : ssids_to_scan_) {
77    ssid.Log();
78  }
79  LOGI("  number of frequencies to scan: %zu", frequencies_to_scan_mhz_.size());
80  for (const auto &freq : frequencies_to_scan_mhz_) {
81    LOGI("  frequency: %" PRIu32, freq);
82  }
83}
84
85}  // namespace wifi_offload
86