1 /*
2  * Copyright (C) 2017 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 "chre/util/nanoapp/wifi.h"
18 
19 #ifdef CHRE_IS_NANOAPP_BUILD
20 #include "chre/util/nanoapp/log.h"
21 #else
22 #include "chre/platform/log.h"
23 #endif  // CHRE_IS_NANOAPP_BUILD
24 
25 #include <cinttypes>
26 #include <cstdio>
27 #include <cstring>
28 
29 namespace chre {
30 
31 namespace {
32 /** Returns true if `ch` is `[A-Za-z0-9]` or punctuation. */
isgraph(int ch)33 inline bool isgraph(int ch) {
34   return (ch >= '!' && ch <= '~');
35 }
36 }  // namespace
37 
logChreWifiResult(const chreWifiScanResult & result,bool logSsidOnly)38 void logChreWifiResult(const chreWifiScanResult &result, bool logSsidOnly) {
39   const char *ssidStr = "<non-printable>";
40   char ssidBuffer[kMaxSsidStrLen];
41   if (result.ssidLen == 0) {
42     ssidStr = "<empty>";
43   } else if (parseSsidToStr(ssidBuffer, sizeof(ssidBuffer), result.ssid,
44                             result.ssidLen)) {
45     ssidStr = ssidBuffer;
46   }
47 
48   LOGI("Found network with SSID: %s", ssidStr);
49   if (!logSsidOnly) {
50     const char *bssidStr = "<non-printable>";
51     char bssidBuffer[kBssidStrLen];
52     if (parseBssidToStr(result.bssid, bssidBuffer, sizeof(bssidBuffer))) {
53       bssidStr = bssidBuffer;
54     }
55 
56     LOGI("  age (ms): %" PRIu32, result.ageMs);
57     LOGI("  capability info: %" PRIx16, result.capabilityInfo);
58     LOGI("  bssid: %s", bssidStr);
59     LOGI("  flags: %" PRIx8, result.flags);
60     LOGI("  rssi: %" PRId8 "dBm", result.rssi);
61     LOGI("  band: %s (%" PRIu8 ")", parseChreWifiBand(result.band),
62          result.band);
63     LOGI("  primary channel: %" PRIu32, result.primaryChannel);
64     LOGI("  center frequency primary: %" PRIu32, result.centerFreqPrimary);
65     LOGI("  center frequency secondary: %" PRIu32, result.centerFreqSecondary);
66     LOGI("  channel width: %" PRIu8, result.channelWidth);
67     LOGI("  security mode: %" PRIx8, result.securityMode);
68   }
69 }
70 
parseSsidToStr(char * buffer,size_t bufferLen,const uint8_t * ssid,uint8_t ssidLen)71 bool parseSsidToStr(char *buffer, size_t bufferLen, const uint8_t *ssid,
72                     uint8_t ssidLen) {
73   // Ensure that there is enough space in the buffer to copy the SSID and
74   // null-terminate it.
75   bool success = (bufferLen >= static_cast<size_t>(ssidLen + 1));
76 
77   if (success) {
78     // Verify that the ssid is entirely printable characters and ASCII spaces.
79     for (uint8_t i = 0; i < ssidLen; i++) {
80       if (!isgraph(ssid[i]) && ssid[i] != ' ') {
81         success = false;
82         break;
83       }
84     }
85   }
86 
87   if (success) {
88     // Copy the SSID to the buffer and null-terminate.
89     memcpy(buffer, ssid, ssidLen + 1);
90     buffer[ssidLen] = '\0';
91   }
92 
93   return success;
94 }
95 
parseBssidToStr(const uint8_t bssid[CHRE_WIFI_BSSID_LEN],char * buffer,size_t bufferLen)96 bool parseBssidToStr(const uint8_t bssid[CHRE_WIFI_BSSID_LEN], char *buffer,
97                      size_t bufferLen) {
98   const char *kFormat = "%02" PRIx8 ":%02" PRIx8 ":%02" PRIx8 ":%02" PRIx8
99                         ":%02" PRIx8 ":%02" PRIx8;
100 
101   bool success = false;
102   if (bufferLen >= kBssidStrLen) {
103     success = true;
104     snprintf(buffer, bufferLen, kFormat, bssid[0], bssid[1], bssid[2], bssid[3],
105              bssid[4], bssid[5]);
106   }
107 
108   return success;
109 }
110 
parseChreWifiBand(uint8_t band)111 const char *parseChreWifiBand(uint8_t band) {
112   switch (band) {
113     case CHRE_WIFI_BAND_2_4_GHZ:
114       return "2.4GHz";
115     case CHRE_WIFI_BAND_MASK_5_GHZ:
116       return "5GHz";
117     default:
118       return "<invalid>";
119   }
120 }
121 
122 }  // namespace chre
123