summaryrefslogtreecommitdiff
path: root/scanning/single_scan_settings.cpp
blob: 47809fbda52182983a699afbeedc59e658ed3033 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "android/net/wifi/nl80211/IWifiScannerImpl.h"
#include "wificond/scanning/single_scan_settings.h"

#include <android-base/logging.h>

#include "wificond/parcelable_utils.h"

using android::net::wifi::nl80211::IWifiScannerImpl;
using android::status_t;

namespace android {
namespace net {
namespace wifi {
namespace nl80211 {
bool SingleScanSettings::isValidScanType() const {
  return (scan_type_ == IWifiScannerImpl::SCAN_TYPE_LOW_SPAN ||
          scan_type_ == IWifiScannerImpl::SCAN_TYPE_LOW_POWER ||
          scan_type_ == IWifiScannerImpl::SCAN_TYPE_HIGH_ACCURACY);
}

status_t SingleScanSettings::writeToParcel(::android::Parcel* parcel) const {
  if (!isValidScanType()) {
    LOG(ERROR) << "Unexpected scan type: " << scan_type_;
    return ::android::BAD_VALUE;
  }
  RETURN_IF_FAILED(parcel->writeInt32(scan_type_));
  RETURN_IF_FAILED(parcel->writeBool(enable_6ghz_rnr_));
  RETURN_IF_FAILED(parcel->writeInt32(channel_settings_.size()));
  for (const auto& channel : channel_settings_) {
    // For Java readTypedList():
    // A leading number 1 means this object is not null.
    RETURN_IF_FAILED(parcel->writeInt32(1));
    RETURN_IF_FAILED(channel.writeToParcel(parcel));
  }
  RETURN_IF_FAILED(parcel->writeInt32(hidden_networks_.size()));
  for (const auto& network : hidden_networks_) {
    // For Java readTypedList():
    // A leading number 1 means this object is not null.
    RETURN_IF_FAILED(parcel->writeInt32(1));
    RETURN_IF_FAILED(network.writeToParcel(parcel));
  }
  return ::android::OK;
}

status_t SingleScanSettings::readFromParcel(const ::android::Parcel* parcel) {
  RETURN_IF_FAILED(parcel->readInt32(&scan_type_));
  if (!isValidScanType()) {
    LOG(ERROR) << "Unexpected scan type: " << scan_type_;
    return ::android::BAD_VALUE;
  }
  RETURN_IF_FAILED(parcel->readBool(&enable_6ghz_rnr_));
  int32_t num_channels = 0;
  RETURN_IF_FAILED(parcel->readInt32(&num_channels));
  // Convention used by Java side writeTypedList():
  // -1 means a null list.
  // 0 means an empty list.
  // Both are mapped to an empty vector in C++ code.
  for (int i = 0; i < num_channels; i++) {
    ChannelSettings channel;
    // From Java writeTypedList():
    // A leading number 1 means this object is not null.
    // We never expect a 0 or other values here.
    int32_t leading_number = 0;
    RETURN_IF_FAILED(parcel->readInt32(&leading_number));
    if (leading_number != 1) {
      LOG(ERROR) << "Unexpected leading number before an object: "
                 << leading_number;
      return ::android::BAD_VALUE;
    }
    RETURN_IF_FAILED(channel.readFromParcel(parcel));
    channel_settings_.push_back(channel);
  }
  int32_t num_hidden_networks = 0;
  RETURN_IF_FAILED(parcel->readInt32(&num_hidden_networks));
  // Convention used by Java side writeTypedList():
  // -1 means a null list.
  // 0 means an empty list.
  // Both are mapped to an empty vector in C++ code.
  for (int i = 0; i < num_hidden_networks; i++) {
    HiddenNetwork network;
    // From Java writeTypedList():
    // A leading number 1 means this object is not null.
    // We never expect a 0 or other values here.
    int32_t leading_number = 0;
    RETURN_IF_FAILED(parcel->readInt32(&leading_number));
    if (leading_number != 1) {
      LOG(ERROR) << "Unexpected leading number before an object: "
                 << leading_number;
      return ::android::BAD_VALUE;
    }
    RETURN_IF_FAILED(network.readFromParcel(parcel));
    hidden_networks_.push_back(network);
  }
  return ::android::OK;
}

}  // namespace nl80211
}  // namespace wifi
}  // namespace net
}  // namespace android