• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/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::IWifiScannerImpl;
25 using android::status_t;
26 
27 namespace com {
28 namespace android {
29 namespace server {
30 namespace wifi {
31 namespace wificond {
isValidScanType() const32 bool SingleScanSettings::isValidScanType() const {
33   return (scan_type_ == IWifiScannerImpl::SCAN_TYPE_LOW_SPAN ||
34           scan_type_ == IWifiScannerImpl::SCAN_TYPE_LOW_POWER ||
35           scan_type_ == IWifiScannerImpl::SCAN_TYPE_HIGH_ACCURACY);
36 }
37 
writeToParcel(::android::Parcel * parcel) const38 status_t SingleScanSettings::writeToParcel(::android::Parcel* parcel) const {
39   if (!isValidScanType()) {
40     LOG(ERROR) << "Unexpected scan type: " << scan_type_;
41     return ::android::BAD_VALUE;
42   }
43   RETURN_IF_FAILED(parcel->writeInt32(scan_type_));
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)61 status_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   int32_t num_channels = 0;
68   RETURN_IF_FAILED(parcel->readInt32(&num_channels));
69   // Convention used by Java side writeTypedList():
70   // -1 means a null list.
71   // 0 means an empty list.
72   // Both are mapped to an empty vector in C++ code.
73   for (int i = 0; i < num_channels; i++) {
74     ChannelSettings channel;
75     // From Java writeTypedList():
76     // A leading number 1 means this object is not null.
77     // We never expect a 0 or other values here.
78     int32_t leading_number = 0;
79     RETURN_IF_FAILED(parcel->readInt32(&leading_number));
80     if (leading_number != 1) {
81       LOG(ERROR) << "Unexpected leading number before an object: "
82                  << leading_number;
83       return ::android::BAD_VALUE;
84     }
85     RETURN_IF_FAILED(channel.readFromParcel(parcel));
86     channel_settings_.push_back(channel);
87   }
88   int32_t num_hidden_networks = 0;
89   RETURN_IF_FAILED(parcel->readInt32(&num_hidden_networks));
90   // Convention used by Java side writeTypedList():
91   // -1 means a null list.
92   // 0 means an empty list.
93   // Both are mapped to an empty vector in C++ code.
94   for (int i = 0; i < num_hidden_networks; i++) {
95     HiddenNetwork network;
96     // From Java writeTypedList():
97     // A leading number 1 means this object is not null.
98     // We never expect a 0 or other values here.
99     int32_t leading_number = 0;
100     RETURN_IF_FAILED(parcel->readInt32(&leading_number));
101     if (leading_number != 1) {
102       LOG(ERROR) << "Unexpected leading number before an object: "
103                  << leading_number;
104       return ::android::BAD_VALUE;
105     }
106     RETURN_IF_FAILED(network.readFromParcel(parcel));
107     hidden_networks_.push_back(network);
108   }
109   return ::android::OK;
110 }
111 
112 }  // namespace wificond
113 }  // namespace wifi
114 }  // namespace server
115 }  // namespace android
116 }  // namespace com
117