summaryrefslogtreecommitdiff
path: root/sensor/patchedBLE/src/GeneralUtils.cpp
blob: 019c81bd8f794566a0908b734679c5c440bd7ee3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
/*
 * GeneralUtils.cpp
 *
 *  Created on: May 20, 2017
 *      Author: kolban
 */
 
#include "GeneralUtils.h"
#include <esp_system.h>
#include <string.h>
#include <stdio.h>
#include <string>
#include <sstream>
#include <iomanip>
#include "FreeRTOS.h"
#include <esp_err.h>
#include <nvs.h>
#include <esp_wifi.h>
#include <esp_heap_caps.h>
#include <esp_system.h>
 
#if defined(ARDUINO_ARCH_ESP32) && defined(CONFIG_ARDUHAL_ESP_LOG)
#include "esp32-hal-log.h"
#define LOG_TAG ""
#else
#include "esp_log.h"
static const char* LOG_TAG = "GeneralUtils";
#endif
 
 
static const char kBase64Alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";
 
static int base64EncodedLength(size_t length) {
return (length + 2 - ((length + 2) % 3)) / 3 * 4;
} // base64EncodedLength 
 
 
static int base64EncodedLength(const std::string& in) {
return base64EncodedLength(in.length());
} // base64EncodedLength 
 
 
static void a3_to_a4(unsigned char* a4, unsigned char* a3) {
a4[0] = (a3[0] & 0xfc) >> 2;
a4[1] = ((a3[0] & 0x03) << 4) + ((a3[1] & 0xf0) >> 4);
a4[2] = ((a3[1] & 0x0f) << 2) + ((a3[2] & 0xc0) >> 6);
a4[3] = (a3[2] & 0x3f);
} // a3_to_a4 
 
 
static void a4_to_a3(unsigned char* a3, unsigned char* a4) {
a3[0] = (a4[0] << 2) + ((a4[1] & 0x30) >> 4);
a3[1] = ((a4[1] & 0xf) << 4) + ((a4[2] & 0x3c) >> 2);
a3[2] = ((a4[2] & 0x3) << 6) + a4[3];
} // a4_to_a3 
 
 
/**
 * @brief Encode a string into base 64.
 * @param [in] in
 * @param [out] out
 */
bool GeneralUtils::base64Encode(const std::string& in, std::string* out) {
int i = 0, j = 0;
size_t enc_len = 0;
unsigned char a3[3];
unsigned char a4[4];
 
out->resize(base64EncodedLength(in));
 
int input_len = in.size();
std::string::const_iterator input = in.begin();
 
while (input_len--) {
a3[i++] = *(input++);
if (i == 3{
a3_to_a4(a4, a3);
 
for (i = 0; i < 4; i++) {
(*out)[enc_len++] = kBase64Alphabet[a4[i]];
}
 
i = 0;
}
}
 
if (i) {
for (j = i; j < 3; j++) {
a3[j] = '\0';
}
 
a3_to_a4(a4, a3);
 
for (j = 0; j < i + 1; j++) {
(*out)[enc_len++] = kBase64Alphabet[a4[j]];
}
 
while ((i++ < 3)) {
(*out)[enc_len++] = '=';
}
}
 
return (enc_len == out->size());
} // base64Encode 
 
 
/**
 * @brief Dump general info to the log.
 * Data includes:
 * * Amount of free RAM
 */
void GeneralUtils::dumpInfo() {
size_t freeHeap = heap_caps_get_free_size(MALLOC_CAP_8BIT);
esp_chip_info_t chipInfo;
esp_chip_info(&chipInfo);
ESP_LOGV(LOG_TAG, "--- dumpInfo ---");
ESP_LOGV(LOG_TAG, "Free heap: %d", freeHeap);
ESP_LOGV(LOG_TAG, "Chip Info: Model: %d, cores: %d, revision: %d", chipInfo.model, chipInfo.cores, chipInfo.revision);
ESP_LOGV(LOG_TAG, "ESP-IDF version: %s", esp_get_idf_version());
ESP_LOGV(LOG_TAG, "---");
} // dumpInfo 
 
 
/**
 * @brief Does the string end with a specific character?
 * @param [in] str The string to examine.
 * @param [in] c The character to look form.
 * @return True if the string ends with the given character.
 */
bool GeneralUtils::endsWith(std::string str, char c) {
if (str.empty()) {
return false;
}
if (str.at(str.length() - 1) == c) {
return true;
}
return false;
} // endsWidth 
 
 
static int DecodedLength(const std::string& in) {
int numEq = 0;
int n = (int) in.size();
 
for (std::string::const_reverse_iterator it = in.rbegin(); *it == '='; ++it) {
++numEq;
}
return ((6 * n) / 8) - numEq;
} // DecodedLength 
 
 
static unsigned char b64_lookup(unsigned char c) {
if(c >='A' && c <='Z'return c - 'A';
if(c >='a' && c <='z'return c - 71;
if(c >='0' && c <='9'return c + 4;
if(c == '+'return 62;
if(c == '/'return 63;
return 255;
}// b64_lookup 
 
 
/**
 * @brief Decode a chunk of data that is base64 encoded.
 * @param [in] in The string to be decoded.
 * @param [out] out The resulting data.
 */
bool GeneralUtils::base64Decode(const std::string& in, std::string* out) {
int i = 0, j = 0;
size_t dec_len = 0;
unsigned char a3[3];
unsigned char a4[4];
 
int input_len = in.size();
std::string::const_iterator input = in.begin();
 
out->resize(DecodedLength(in));
 
while (input_len--) {
if (*input == '='{
break;
}
 
a4[i++] = *(input++);
if (i == 4{
for (i = 0; i <4; i++) {
a4[i] = b64_lookup(a4[i]);
}
 
a4_to_a3(a3,a4);
 
for (i = 0; i < 3; i++) {
(*out)[dec_len++] = a3[i];
}
 
i = 0;
}
}
 
if (i) {
for (j = i; j < 4; j++) {
a4[j] = '\0';
}
 
for (j = 0; j < 4; j++) {
a4[j] = b64_lookup(a4[j]);
}
 
a4_to_a3(a3,a4);
 
for (j = 0; j < i - 1; j++) {
(*out)[dec_len++] = a3[j];
}
}
 
return (dec_len == out->size());
 } // base64Decode 
 
/*
void GeneralUtils::hexDump(uint8_t* pData, uint32_t length) {
uint32_t index=0;
std::stringstream ascii;
std::stringstream hex;
char asciiBuf[80];
char hexBuf[80];
hex.str("");
ascii.str("");
while(index < length) {
hex << std::setfill('0') << std::setw(2) << std::hex << (int)pData[index] << ' ';
if (std::isprint(pData[index])) {
ascii << pData[index];
} else {
ascii << '.';
}
index++;
if (index % 16 == 0) {
strcpy(hexBuf, hex.str().c_str());
strcpy(asciiBuf, ascii.str().c_str());
ESP_LOGV(tag, "%s %s", hexBuf, asciiBuf);
hex.str("");
ascii.str("");
}
}
if (index %16 != 0) {
while(index % 16 != 0) {
hex << "   ";
index++;
}
strcpy(hexBuf, hex.str().c_str());
strcpy(asciiBuf, ascii.str().c_str());
ESP_LOGV(tag, "%s %s", hexBuf, asciiBuf);
//ESP_LOGV(tag, "%s %s", hex.str().c_str(), ascii.str().c_str());
}
FreeRTOS::sleep(1000);
}
*/
 
/*
void GeneralUtils::hexDump(uint8_t* pData, uint32_t length) {
uint32_t index=0;
static std::stringstream ascii;
static std::stringstream hex;
hex.str("");
ascii.str("");
while(index < length) {
hex << std::setfill('0') << std::setw(2) << std::hex << (int)pData[index] << ' ';
if (std::isprint(pData[index])) {
ascii << pData[index];
} else {
ascii << '.';
}
index++;
if (index % 16 == 0) {
ESP_LOGV(tag, "%s %s", hex.str().c_str(), ascii.str().c_str());
hex.str("");
ascii.str("");
}
}
if (index %16 != 0) {
while(index % 16 != 0) {
hex << "   ";
index++;
}
ESP_LOGV(tag, "%s %s", hex.str().c_str(), ascii.str().c_str());
}
FreeRTOS::sleep(1000);
}
*/
 
 
/**
 * @brief Dump a representation of binary data to the console.
 *
 * @param [in] pData Pointer to the start of data to be logged.
 * @param [in] length Length of the data (in bytes) to be logged.
 * @return N/A.
 */
void GeneralUtils::hexDump(const uint8_t* pData, uint32_t length) {
char ascii[80];
char hex[80];
char tempBuf[80];
uint32_t lineNumber = 0;
 
ESP_LOGV(LOG_TAG, "     00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f");
ESP_LOGV(LOG_TAG, "     -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --");
strcpy(ascii, "");
strcpy(hex, "");
uint32_t index = 0;
while (index < length) {
sprintf(tempBuf, "%.2x ", pData[index]);
strcat(hex, tempBuf);
if (isprint(pData[index])) {
sprintf(tempBuf, "%c", pData[index]);
} else {
sprintf(tempBuf, ".");
}
strcat(ascii, tempBuf);
index++;
if (index % 16 == 0{
ESP_LOGV(LOG_TAG, "%.4x %s %s", lineNumber * 16, hex, ascii);
strcpy(ascii, "");
strcpy(hex, "");
lineNumber++;
}
}
if (index %16 != 0{
while (index % 16 != 0{
strcat(hex, "   ");
index++;
}
ESP_LOGV(LOG_TAG, "%.4x %s %s", lineNumber * 16, hex, ascii);
}
} // hexDump 
 
 
/**
 * @brief Convert an IP address to string.
 * @param ip The 4 byte IP address.
 * @return A string representation of the IP address.
 */
std::string GeneralUtils::ipToString(uint8_t *ip) {
std::stringstream s;
s << (int) ip[0] << '.' << (int) ip[1] << '.' << (int) ip[2] << '.' << (int) ip[3];
return s.str();
} // ipToString 
 
 
/**
 * @brief Split a string into parts based on a delimiter.
 * @param [in] source The source string to split.
 * @param [in] delimiter The delimiter characters.
 * @return A vector of strings that are the split of the input.
 */
std::vector<std::string> GeneralUtils::split(std::string source, char delimiter) {
// See also: https://stackoverflow.com/questions/5167625/splitting-a-c-stdstring-using-tokens-e-g 
std::vector<std::string> strings;
std::istringstream iss(source);
std::string s;
while (std::getline(iss, s, delimiter)) {
strings.push_back(trim(s));
}
return strings;
} // split 
 
 
/**
 * @brief Convert an ESP error code to a string.
 * @param [in] errCode The errCode to be converted.
 * @return A string representation of the error code.
 */
const char* GeneralUtils::errorToString(esp_err_t errCode) {
switch (errCode) {
#if CONFIG_LOG_DEFAULT_LEVEL > 4
case ESP_OK:
return "ESP_OK";
case ESP_FAIL:
return "ESP_FAIL";
case ESP_ERR_NO_MEM:
return "ESP_ERR_NO_MEM";
case ESP_ERR_INVALID_ARG:
return "ESP_ERR_INVALID_ARG";
case ESP_ERR_INVALID_SIZE:
return "ESP_ERR_INVALID_SIZE";
case ESP_ERR_INVALID_STATE:
return "ESP_ERR_INVALID_STATE";
case ESP_ERR_NOT_FOUND:
return "ESP_ERR_NOT_FOUND";
case ESP_ERR_NOT_SUPPORTED:
return "ESP_ERR_NOT_SUPPORTED";
case ESP_ERR_TIMEOUT:
return "ESP_ERR_TIMEOUT";
case ESP_ERR_NVS_NOT_INITIALIZED:
return "ESP_ERR_NVS_NOT_INITIALIZED";
case ESP_ERR_NVS_NOT_FOUND:
return "ESP_ERR_NVS_NOT_FOUND";
case ESP_ERR_NVS_TYPE_MISMATCH:
return "ESP_ERR_NVS_TYPE_MISMATCH";
case ESP_ERR_NVS_READ_ONLY:
return "ESP_ERR_NVS_READ_ONLY";
case ESP_ERR_NVS_NOT_ENOUGH_SPACE:
return "ESP_ERR_NVS_NOT_ENOUGH_SPACE";
case ESP_ERR_NVS_INVALID_NAME:
return "ESP_ERR_NVS_INVALID_NAME";
case ESP_ERR_NVS_INVALID_HANDLE:
return "ESP_ERR_NVS_INVALID_HANDLE";
case ESP_ERR_NVS_REMOVE_FAILED:
return "ESP_ERR_NVS_REMOVE_FAILED";
case ESP_ERR_NVS_KEY_TOO_LONG:
return "ESP_ERR_NVS_KEY_TOO_LONG";
case ESP_ERR_NVS_PAGE_FULL:
return "ESP_ERR_NVS_PAGE_FULL";
case ESP_ERR_NVS_INVALID_STATE:
return "ESP_ERR_NVS_INVALID_STATE";
case ESP_ERR_NVS_INVALID_LENGTH:
return "ESP_ERR_NVS_INVALID_LENGTH";
case ESP_ERR_WIFI_NOT_INIT:
return "ESP_ERR_WIFI_NOT_INIT";
//case ESP_ERR_WIFI_NOT_START: 
// return "ESP_ERR_WIFI_NOT_START"; 
case ESP_ERR_WIFI_IF:
return "ESP_ERR_WIFI_IF";
case ESP_ERR_WIFI_MODE:
return "ESP_ERR_WIFI_MODE";
case ESP_ERR_WIFI_STATE:
return "ESP_ERR_WIFI_STATE";
case ESP_ERR_WIFI_CONN:
return "ESP_ERR_WIFI_CONN";
case ESP_ERR_WIFI_NVS:
return "ESP_ERR_WIFI_NVS";
case ESP_ERR_WIFI_MAC:
return "ESP_ERR_WIFI_MAC";
case ESP_ERR_WIFI_SSID:
return "ESP_ERR_WIFI_SSID";
case ESP_ERR_WIFI_PASSWORD:
return "ESP_ERR_WIFI_PASSWORD";
case ESP_ERR_WIFI_TIMEOUT:
return "ESP_ERR_WIFI_TIMEOUT";
case ESP_ERR_WIFI_WAKE_FAIL:
return "ESP_ERR_WIFI_WAKE_FAIL";
#endif
default:
return "Unknown ESP_ERR error";
}
} // errorToString 
 
/**
 * @brief Convert a wifi_err_reason_t code to a string.
 * @param [in] errCode The errCode to be converted.
 * @return A string representation of the error code.
 *
 * @note: wifi_err_reason_t values as of April 2018 are: (1-24, 200-204) and are defined in ~/esp-idf/components/esp32/include/esp_wifi_types.h.
 */
const char* GeneralUtils::wifiErrorToString(uint8_t errCode) {
if (errCode == ESP_OK) return "ESP_OK (received SYSTEM_EVENT_STA_GOT_IP event)";
if (errCode == UINT8_MAX) return "Not Connected (default value)";
 
switch ((wifi_err_reason_t) errCode) {
#if CONFIG_LOG_DEFAULT_LEVEL > 4
case WIFI_REASON_UNSPECIFIED:
return "WIFI_REASON_UNSPECIFIED";
case WIFI_REASON_AUTH_EXPIRE:
return "WIFI_REASON_AUTH_EXPIRE";
case WIFI_REASON_AUTH_LEAVE:
return "WIFI_REASON_AUTH_LEAVE";
case WIFI_REASON_ASSOC_EXPIRE:
return "WIFI_REASON_ASSOC_EXPIRE";
case WIFI_REASON_ASSOC_TOOMANY:
return "WIFI_REASON_ASSOC_TOOMANY";
case WIFI_REASON_NOT_AUTHED:
return "WIFI_REASON_NOT_AUTHED";
case WIFI_REASON_NOT_ASSOCED:
return "WIFI_REASON_NOT_ASSOCED";
case WIFI_REASON_ASSOC_LEAVE:
return "WIFI_REASON_ASSOC_LEAVE";
case WIFI_REASON_ASSOC_NOT_AUTHED:
return "WIFI_REASON_ASSOC_NOT_AUTHED";
case WIFI_REASON_DISASSOC_PWRCAP_BAD:
return "WIFI_REASON_DISASSOC_PWRCAP_BAD";
case WIFI_REASON_DISASSOC_SUPCHAN_BAD:
return "WIFI_REASON_DISASSOC_SUPCHAN_BAD";
case WIFI_REASON_IE_INVALID:
return "WIFI_REASON_IE_INVALID";
case WIFI_REASON_MIC_FAILURE:
return "WIFI_REASON_MIC_FAILURE";
case WIFI_REASON_4WAY_HANDSHAKE_TIMEOUT:
return "WIFI_REASON_4WAY_HANDSHAKE_TIMEOUT";
case WIFI_REASON_GROUP_KEY_UPDATE_TIMEOUT:
return "WIFI_REASON_GROUP_KEY_UPDATE_TIMEOUT";
case WIFI_REASON_IE_IN_4WAY_DIFFERS:
return "WIFI_REASON_IE_IN_4WAY_DIFFERS";
case WIFI_REASON_GROUP_CIPHER_INVALID:
return "WIFI_REASON_GROUP_CIPHER_INVALID";
case WIFI_REASON_PAIRWISE_CIPHER_INVALID:
return "WIFI_REASON_PAIRWISE_CIPHER_INVALID";
case WIFI_REASON_AKMP_INVALID:
return "WIFI_REASON_AKMP_INVALID";
case WIFI_REASON_UNSUPP_RSN_IE_VERSION:
return "WIFI_REASON_UNSUPP_RSN_IE_VERSION";
case WIFI_REASON_INVALID_RSN_IE_CAP:
return "WIFI_REASON_INVALID_RSN_IE_CAP";
case WIFI_REASON_802_1X_AUTH_FAILED:
return "WIFI_REASON_802_1X_AUTH_FAILED";
case WIFI_REASON_CIPHER_SUITE_REJECTED:
return "WIFI_REASON_CIPHER_SUITE_REJECTED";
case WIFI_REASON_BEACON_TIMEOUT:
return "WIFI_REASON_BEACON_TIMEOUT";
case WIFI_REASON_NO_AP_FOUND:
return "WIFI_REASON_NO_AP_FOUND";
case WIFI_REASON_AUTH_FAIL:
return "WIFI_REASON_AUTH_FAIL";
case WIFI_REASON_ASSOC_FAIL:
return "WIFI_REASON_ASSOC_FAIL";
case WIFI_REASON_HANDSHAKE_TIMEOUT:
return "WIFI_REASON_HANDSHAKE_TIMEOUT";
#endif
default:
return "Unknown ESP_ERR error";
}
} // wifiErrorToString 
 
 
/**
 * @brief Convert a string to lower case.
 * @param [in] value The string to convert to lower case.
 * @return A lower case representation of the string.
 */
std::string GeneralUtils::toLower(std::string& value) {
// Question: Could this be improved with a signature of: 
// std::string& GeneralUtils::toLower(std::string& value) 
std::transform(value.begin(), value.end(), value.begin(), ::tolower);
return value;
} // toLower 
 
 
/**
 * @brief Remove white space from a string.
 */
std::string GeneralUtils::trim(const std::string& str) {
size_t first = str.find_first_not_of(' ');
if (std::string::npos == first) return str;
size_t last = str.find_last_not_of(' ');
return str.substr(first, (last - first + 1));
} // trim