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