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 "wificond/scanning/scan_result.h"
18 
19 #include <algorithm>
20 
21 #include <android-base/logging.h>
22 
23 #include "wificond/logging_utils.h"
24 #include "wificond/parcelable_utils.h"
25 
26 using android::status_t;
27 using android::OK;
28 using std::string;
29 
30 namespace android {
31 namespace net {
32 namespace wifi {
33 namespace nl80211 {
34 
NativeScanResult(std::vector<uint8_t> & ssid_,std::array<uint8_t,ETH_ALEN> & bssid_,std::vector<uint8_t> & info_element_,uint32_t frequency_,int32_t signal_mbm_,uint64_t tsf_,uint16_t capability_,bool associated_,std::vector<RadioChainInfo> & radio_chain_infos_)35 NativeScanResult::NativeScanResult(std::vector<uint8_t>& ssid_,
36                                    std::array<uint8_t, ETH_ALEN>& bssid_,
37                                    std::vector<uint8_t>& info_element_,
38                                    uint32_t frequency_,
39                                    int32_t signal_mbm_,
40                                    uint64_t tsf_,
41                                    uint16_t capability_,
42                                    bool associated_,
43                                    std::vector<RadioChainInfo>& radio_chain_infos_)
44     : ssid(ssid_),
45       bssid(bssid_),
46       info_element(info_element_),
47       frequency(frequency_),
48       signal_mbm(signal_mbm_),
49       tsf(tsf_),
50       capability(capability_),
51       associated(associated_),
52       radio_chain_infos(radio_chain_infos_) {
53 }
54 
writeToParcel(::android::Parcel * parcel) const55 status_t NativeScanResult::writeToParcel(::android::Parcel* parcel) const {
56   RETURN_IF_FAILED(parcel->writeByteVector(ssid));
57   RETURN_IF_FAILED(parcel->writeByteVector(
58       std::vector<uint8_t>(bssid.begin(), bssid.end())));
59   RETURN_IF_FAILED(parcel->writeByteVector(info_element));
60   RETURN_IF_FAILED(parcel->writeUint32(frequency));
61   RETURN_IF_FAILED(parcel->writeInt32(signal_mbm));
62   RETURN_IF_FAILED(parcel->writeUint64(tsf));
63   // There is no writeUint16() available.
64   // Use writeUint32() instead.
65   RETURN_IF_FAILED(parcel->writeUint32(capability));
66   RETURN_IF_FAILED(parcel->writeInt32(associated ? 1 : 0));
67   RETURN_IF_FAILED(parcel->writeInt32(radio_chain_infos.size()));
68   for (const auto& radio_chain_info : radio_chain_infos) {
69     // For Java readTypedList():
70     // A leading number 1 means this object is not null.
71     RETURN_IF_FAILED(parcel->writeInt32(1));
72     RETURN_IF_FAILED(radio_chain_info.writeToParcel(parcel));
73   }
74   return ::android::OK;
75 }
76 
readFromParcel(const::android::Parcel * parcel)77 status_t NativeScanResult::readFromParcel(const ::android::Parcel* parcel) {
78   RETURN_IF_FAILED(parcel->readByteVector(&ssid));
79   {
80     std::vector<uint8_t> bssid_vec;
81     RETURN_IF_FAILED(parcel->readByteVector(&bssid_vec));
82     if (bssid_vec.size() != ETH_ALEN) {
83       LOG(ERROR) << "bssid length expected " << ETH_ALEN << " bytes, but got "
84                  << bssid_vec.size() << " bytes";
85       return ::android::BAD_VALUE;
86     }
87     std::copy_n(bssid_vec.begin(), ETH_ALEN, bssid.begin());
88   }
89   RETURN_IF_FAILED(parcel->readByteVector(&info_element));
90   RETURN_IF_FAILED(parcel->readUint32(&frequency));
91   RETURN_IF_FAILED(parcel->readInt32(&signal_mbm));
92   RETURN_IF_FAILED(parcel->readUint64(&tsf));
93   // There is no readUint16() available.
94   // Use readUint32() instead.
95   capability = static_cast<uint16_t>(parcel->readUint32());
96   associated = (parcel->readInt32() != 0);
97   int32_t num_radio_chain_infos = 0;
98   RETURN_IF_FAILED(parcel->readInt32(&num_radio_chain_infos));
99   for (int i = 0; i < num_radio_chain_infos; i++) {
100     RadioChainInfo radio_chain_info;
101     // From Java writeTypedList():
102     // A leading number 1 means this object is not null.
103     // We never expect a 0 or other values here.
104     int32_t leading_number = 0;
105     RETURN_IF_FAILED(parcel->readInt32(&leading_number));
106     if (leading_number != 1) {
107       LOG(ERROR) << "Unexpected leading number before an object: "
108                  << leading_number;
109       return ::android::BAD_VALUE;
110     }
111     RETURN_IF_FAILED(radio_chain_info.readFromParcel(parcel));
112     radio_chain_infos.push_back(radio_chain_info);
113   }
114   return ::android::OK;
115 }
116 
DebugLog()117 void NativeScanResult::DebugLog() {
118   LOG(INFO) << "Scan result:";
119   // |ssid| might be an encoded array but we just print it as ASCII here.
120   string ssid_str(ssid.data(), ssid.data() + ssid.size());
121   LOG(INFO) << "SSID: " << ssid_str;
122 
123   LOG(INFO) << "BSSID: "
124             << ::android::wificond::LoggingUtils::GetMacString(bssid);
125   LOG(INFO) << "FREQUENCY: " << frequency;
126   LOG(INFO) << "SIGNAL: " << signal_mbm/100 << "dBm";
127   LOG(INFO) << "TSF: " << tsf;
128   LOG(INFO) << "CAPABILITY: " << capability;
129   LOG(INFO) << "ASSOCIATED: " << associated;
130   for (const auto& radio_chain_info : radio_chain_infos) {
131     LOG(INFO) << "RADIO CHAIN ID: " << radio_chain_info.chain_id;
132     LOG(INFO) << "RADIO CHAIN LEVEL: " << radio_chain_info.level;
133   }
134 
135 }
136 
137 }  // namespace nl80211
138 }  // namespace wifi
139 }  // namespace net
140 }  // namespace android
141