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() const31bool 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) const37status_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 ::android::OK; 59 } 60 readFromParcel(const::android::Parcel * parcel)61status_t SingleScanSettings::readFromParcel(const ::android::Parcel* parcel) { 62 RETURN_IF_FAILED(parcel->readInt32(&scan_type_)); 63 if (!isValidScanType()) { 64 LOG(ERROR) << "Unexpected scan type: " << scan_type_; 65 return ::android::BAD_VALUE; 66 } 67 RETURN_IF_FAILED(parcel->readBool(&enable_6ghz_rnr_)); 68 int32_t num_channels = 0; 69 RETURN_IF_FAILED(parcel->readInt32(&num_channels)); 70 // Convention used by Java side writeTypedList(): 71 // -1 means a null list. 72 // 0 means an empty list. 73 // Both are mapped to an empty vector in C++ code. 74 for (int i = 0; i < num_channels; i++) { 75 ChannelSettings channel; 76 // From Java writeTypedList(): 77 // A leading number 1 means this object is not null. 78 // We never expect a 0 or other values here. 79 int32_t leading_number = 0; 80 RETURN_IF_FAILED(parcel->readInt32(&leading_number)); 81 if (leading_number != 1) { 82 LOG(ERROR) << "Unexpected leading number before an object: " 83 << leading_number; 84 return ::android::BAD_VALUE; 85 } 86 RETURN_IF_FAILED(channel.readFromParcel(parcel)); 87 channel_settings_.push_back(channel); 88 } 89 int32_t num_hidden_networks = 0; 90 RETURN_IF_FAILED(parcel->readInt32(&num_hidden_networks)); 91 // Convention used by Java side writeTypedList(): 92 // -1 means a null list. 93 // 0 means an empty list. 94 // Both are mapped to an empty vector in C++ code. 95 for (int i = 0; i < num_hidden_networks; i++) { 96 HiddenNetwork network; 97 // From Java writeTypedList(): 98 // A leading number 1 means this object is not null. 99 // We never expect a 0 or other values here. 100 int32_t leading_number = 0; 101 RETURN_IF_FAILED(parcel->readInt32(&leading_number)); 102 if (leading_number != 1) { 103 LOG(ERROR) << "Unexpected leading number before an object: " 104 << leading_number; 105 return ::android::BAD_VALUE; 106 } 107 RETURN_IF_FAILED(network.readFromParcel(parcel)); 108 hidden_networks_.push_back(network); 109 } 110 return ::android::OK; 111 } 112 113 } // namespace nl80211 114 } // namespace wifi 115 } // namespace net 116 } // namespace android 117