1 /*
2  * Copyright (C) 2020 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 "wifi_scan_result.h"
18 
19 #include <chre.h>
20 
21 #include "chre/util/nanoapp/log.h"
22 
23 #include <stdio.h>
24 #include <cstring>
25 
26 #define LOG_TAG "ChreCrossValidatorWifi"
27 
WifiScanResult(pb_istream_t * apWifiScanResultStream)28 WifiScanResult::WifiScanResult(pb_istream_t *apWifiScanResultStream) {
29   memset(mSsid, 0, CHRE_WIFI_SSID_MAX_LEN);
30   chre_cross_validation_wifi_WifiScanResult wifiScanResultProto =
31       chre_cross_validation_wifi_WifiScanResult_init_default;
32   wifiScanResultProto.ssid = {.funcs = {.decode = decodeString}, .arg = mSsid};
33   wifiScanResultProto.bssid = {.funcs = {.decode = decodeString},
34                                .arg = mBssid};
35   if (!pb_decode(apWifiScanResultStream,
36                  chre_cross_validation_wifi_WifiScanResult_fields,
37                  &wifiScanResultProto)) {
38     LOGE("AP wifi scan result proto message decode failed");
39   }
40   mTotalNumResults = wifiScanResultProto.totalNumResults;
41   mResultIndex = wifiScanResultProto.resultIndex;
42 }
43 
WifiScanResult(const chreWifiScanResult & chreScanResult)44 WifiScanResult::WifiScanResult(const chreWifiScanResult &chreScanResult) {
45   memset(mSsid, 0, CHRE_WIFI_SSID_MAX_LEN);
46   memcpy(mSsid, chreScanResult.ssid, chreScanResult.ssidLen);
47   memcpy(mBssid, chreScanResult.bssid, CHRE_WIFI_BSSID_LEN);
48 }
49 
areEqual(const WifiScanResult & result1,const WifiScanResult & result2)50 bool WifiScanResult::areEqual(const WifiScanResult &result1,
51                               const WifiScanResult &result2) {
52   // TODO(b/184653034): Compare all fields that are shared between AP and CHRE
53   // scan result.
54   return strcmp(result1.mSsid, result2.mSsid) == 0 &&
55          bssidsAreEqual(result1, result2);
56 }
57 
bssidsAreEqual(const WifiScanResult & result1,const WifiScanResult & result2)58 bool WifiScanResult::bssidsAreEqual(const WifiScanResult &result1,
59                                     const WifiScanResult &result2) {
60   return memcmp(result1.mBssid, result2.mBssid, CHRE_WIFI_BSSID_LEN) == 0;
61 }
62 
decodeString(pb_istream_t * stream,const pb_field_t *,void ** arg)63 bool WifiScanResult::decodeString(pb_istream_t *stream,
64                                   const pb_field_t * /*field*/, void **arg) {
65   pb_byte_t *strPtr = reinterpret_cast<pb_byte_t *>(*arg);
66   return pb_read(stream, strPtr, stream->bytes_left);
67 }
68