1 /*
2  * Copyright (C) 2016 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 "android/net/wifi/nl80211/IWifiScannerImpl.h"
18 #include "wificond/scanning/single_scan_settings.h"
19 
20 #include <android-base/logging.h>
21 
22 #include "wificond/parcelable_utils.h"
23 
24 using android::net::wifi::nl80211::IWifiScannerImpl;
25 using android::status_t;
26 
27 namespace android {
28 namespace net {
29 namespace wifi {
30 namespace nl80211 {
isValidScanType() const31 bool SingleScanSettings::isValidScanType() const {
32   return (scan_type_ == IWifiScannerImpl::SCAN_TYPE_LOW_SPAN ||
33           scan_type_ == IWifiScannerImpl::SCAN_TYPE_LOW_POWER ||
34           scan_type_ == IWifiScannerImpl::SCAN_TYPE_HIGH_ACCURACY);
35 }
36 
writeToParcel(::android::Parcel * parcel) const37 status_t SingleScanSettings::writeToParcel(::android::Parcel* parcel) const {
38   if (!isValidScanType()) {
39     LOG(ERROR) << "Unexpected scan type: " << scan_type_;
40     return ::android::BAD_VALUE;
41   }
42   RETURN_IF_FAILED(parcel->writeInt32(scan_type_));
43   RETURN_IF_FAILED(parcel->writeBool(enable_6ghz_rnr_));
44   RETURN_IF_FAILED(parcel->writeInt32(channel_settings_.size()));
45   for (const auto& channel : channel_settings_) {
46     // For Java readTypedList():
47     // A leading number 1 means this object is not null.
48     RETURN_IF_FAILED(parcel->writeInt32(1));
49     RETURN_IF_FAILED(channel.writeToParcel(parcel));
50   }
51   RETURN_IF_FAILED(parcel->writeInt32(hidden_networks_.size()));
52   for (const auto& network : hidden_networks_) {
53     // For Java readTypedList():
54     // A leading number 1 means this object is not null.
55     RETURN_IF_FAILED(parcel->writeInt32(1));
56     RETURN_IF_FAILED(network.writeToParcel(parcel));
57   }
58   RETURN_IF_FAILED(parcel->writeByteVector(vendor_ies_));
59   return ::android::OK;
60 }
61 
readFromParcel(const::android::Parcel * parcel)62 status_t SingleScanSettings::readFromParcel(const ::android::Parcel* parcel) {
63   RETURN_IF_FAILED(parcel->readInt32(&scan_type_));
64   if (!isValidScanType()) {
65     LOG(ERROR) << "Unexpected scan type: " << scan_type_;
66     return ::android::BAD_VALUE;
67   }
68   RETURN_IF_FAILED(parcel->readBool(&enable_6ghz_rnr_));
69   int32_t num_channels = 0;
70   RETURN_IF_FAILED(parcel->readInt32(&num_channels));
71   // Convention used by Java side writeTypedList():
72   // -1 means a null list.
73   // 0 means an empty list.
74   // Both are mapped to an empty vector in C++ code.
75   for (int i = 0; i < num_channels; i++) {
76     ChannelSettings channel;
77     // From Java writeTypedList():
78     // A leading number 1 means this object is not null.
79     // We never expect a 0 or other values here.
80     int32_t leading_number = 0;
81     RETURN_IF_FAILED(parcel->readInt32(&leading_number));
82     if (leading_number != 1) {
83       LOG(ERROR) << "Unexpected leading number before an object: "
84                  << leading_number;
85       return ::android::BAD_VALUE;
86     }
87     RETURN_IF_FAILED(channel.readFromParcel(parcel));
88     channel_settings_.push_back(channel);
89   }
90   int32_t num_hidden_networks = 0;
91   RETURN_IF_FAILED(parcel->readInt32(&num_hidden_networks));
92   // Convention used by Java side writeTypedList():
93   // -1 means a null list.
94   // 0 means an empty list.
95   // Both are mapped to an empty vector in C++ code.
96   for (int i = 0; i < num_hidden_networks; i++) {
97     HiddenNetwork network;
98     // From Java writeTypedList():
99     // A leading number 1 means this object is not null.
100     // We never expect a 0 or other values here.
101     int32_t leading_number = 0;
102     RETURN_IF_FAILED(parcel->readInt32(&leading_number));
103     if (leading_number != 1) {
104       LOG(ERROR) << "Unexpected leading number before an object: "
105                  << leading_number;
106       return ::android::BAD_VALUE;
107     }
108     RETURN_IF_FAILED(network.readFromParcel(parcel));
109     hidden_networks_.push_back(network);
110   }
111   RETURN_IF_FAILED(parcel->readByteVector(&vendor_ies_));
112   return ::android::OK;
113 }
114 
115 }  // namespace nl80211
116 }  // namespace wifi
117 }  // namespace net
118 }  // namespace android
119