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 #include <cctype>
20 #include <cinttypes>
21 #include <cstdio>
22 #include <cstring>
23
24 namespace chre {
25
parseSsidToStr(char * buffer,size_t bufferLen,const uint8_t * ssid,uint8_t ssidLen)26 bool parseSsidToStr(char *buffer, size_t bufferLen,
27 const uint8_t *ssid, uint8_t ssidLen) {
28 // Ensure that there is enough space in the buffer to copy the SSID and
29 // null-terminate it.
30 bool success = (bufferLen >= (ssidLen + 1));
31
32 if (success) {
33 // Verify that the ssid is entirely printable characters and ASCII spaces.
34 for (uint8_t i = 0; i < ssidLen; i++) {
35 if (!isgraph(ssid[i]) && ssid[i] != ' ') {
36 success = false;
37 break;
38 }
39 }
40 }
41
42 if (success) {
43 // Copy the SSID to the buffer and null-terminate.
44 memcpy(buffer, ssid, ssidLen + 1);
45 buffer[ssidLen] = '\0';
46 }
47
48 return success;
49 }
50
parseBssidToStr(const uint8_t bssid[CHRE_WIFI_BSSID_LEN],char * buffer,size_t bufferLen)51 bool parseBssidToStr(const uint8_t bssid[CHRE_WIFI_BSSID_LEN],
52 char *buffer, size_t bufferLen) {
53 const char *kFormat = "%02" PRIx8 ":%02" PRIx8 ":%02" PRIx8
54 ":%02" PRIx8 ":%02" PRIx8 ":%02" PRIx8;
55
56 bool success = false;
57 if (bufferLen >= kBssidStrLen) {
58 success = true;
59 snprintf(buffer, bufferLen, kFormat, bssid[0], bssid[1], bssid[2],
60 bssid[3], bssid[4], bssid[5]);
61 }
62
63 return success;
64 }
65
parseChreWifiBand(uint8_t band)66 const char *parseChreWifiBand(uint8_t band) {
67 switch (band) {
68 case CHRE_WIFI_BAND_2_4_GHZ:
69 return "2.4GHz";
70 case CHRE_WIFI_BAND_MASK_5_GHZ:
71 return "5GHz";
72 default:
73 return "<invalid>";
74 }
75 }
76
77 } // namespace chre
78