Skip to content

Commit 01b6968

Browse files
committed
WIP: Accept array of networks, conditionally pick last if array
1 parent 628e089 commit 01b6968

File tree

2 files changed

+185
-128
lines changed

2 files changed

+185
-128
lines changed
Lines changed: 130 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -1,113 +1,131 @@
1-
/*!
2-
* @file Wippersnapper_LittleFS.cpp
3-
*
4-
* Interfaces with LittleFS filesystem for ESP32, ESP8266 platforms.
5-
*
6-
* Adafruit invests time and resources providing this open source code,
7-
* please support Adafruit and open-source hardware by purchasing
8-
* products from Adafruit!
9-
*
10-
* Copyright (c) Brent Rubell 2021-2022 for Adafruit Industries.
11-
*
12-
* BSD license, all text here must be included in any redistribution.
13-
*
14-
*/
15-
#if defined(ARDUINO_FEATHER_ESP32) || \
16-
defined(ARDUINO_ESP8266_ADAFRUIT_HUZZAH) || \
17-
defined(ARDUINO_ADAFRUIT_ITSYBITSY_ESP32) || \
18-
defined(ARDUINO_ADAFRUIT_FEATHER_ESP32_V2) || \
19-
defined(ARDUINO_ADAFRUIT_QTPY_ESP32_PICO) || \
20-
defined(ARDUINO_ADAFRUIT_QTPY_ESP32C3)
21-
#include "WipperSnapper_LittleFS.h"
22-
23-
/**************************************************************************/
24-
/*!
25-
@brief Attempts to set up and initialize a pre-existing LittleFS
26-
filesystem.
27-
*/
28-
/**************************************************************************/
29-
WipperSnapper_LittleFS::WipperSnapper_LittleFS() {
30-
// Attempt to initialize filesystem
31-
if (!LittleFS.begin()) {
32-
setStatusLEDColor(RED);
33-
fsHalt("ERROR: Failure initializing LittleFS!");
34-
}
35-
}
36-
37-
/**************************************************************************/
38-
/*!
39-
@brief Destructor for LittleFS
40-
*/
41-
/**************************************************************************/
42-
WipperSnapper_LittleFS::~WipperSnapper_LittleFS() { LittleFS.end(); }
43-
44-
/**************************************************************************/
45-
/*!
46-
@brief Locates, opens and parses the WipperSnapper secrets file
47-
on the LittleFS filesystem.
48-
*/
49-
/**************************************************************************/
50-
void WipperSnapper_LittleFS::parseSecrets() {
51-
// Check if `secrets.json` file exists on FS
52-
if (!LittleFS.exists("/secrets.json")) {
53-
fsHalt("ERROR: No secrets.json found on filesystem - did you upload "
54-
"credentials?");
55-
}
56-
57-
// Attempt to open secrets.json file for reading
58-
File secretsFile = LittleFS.open("/secrets.json", "r");
59-
if (!secretsFile) {
60-
fsHalt("ERROR: Could not open secrets.json file for reading!");
61-
}
62-
63-
// Attempt to deserialize the file's JSON document
64-
JsonDocument doc;
65-
DeserializationError error = deserializeJson(doc, secretsFile);
66-
if (error) {
67-
fsHalt(String("ERROR: deserializeJson() failed with code ") +
68-
error.c_str());
69-
}
70-
71-
// Extract a config struct from the JSON document
72-
WS._config = doc.as<secretsConfig>();
73-
74-
// Validate the config struct is not filled with default values
75-
if (strcmp(WS._config.aio_user, "YOUR_IO_USERNAME_HERE") == 0 ||
76-
strcmp(WS._config.aio_key, "YOUR_IO_KEY_HERE") == 0) {
77-
fsHalt(
78-
"ERROR: Invalid IO credentials in secrets.json! TO FIX: Please change "
79-
"io_username and io_key to match your Adafruit IO credentials!\n");
80-
}
81-
82-
if (strcmp(WS._config.network.ssid, "YOUR_WIFI_SSID_HERE") == 0 ||
83-
strcmp(WS._config.network.pass, "YOUR_WIFI_PASS_HERE") == 0) {
84-
fsHalt("ERROR: Invalid network credentials in secrets.json! TO FIX: Please "
85-
"change network_ssid and network_password to match your Adafruit IO "
86-
"credentials!\n");
87-
}
88-
89-
// Close the file
90-
secretsFile.close();
91-
92-
// Stop LittleFS, we no longer need it
93-
LittleFS.end();
94-
}
95-
96-
/**************************************************************************/
97-
/*!
98-
@brief Halts execution and blinks the status LEDs yellow.
99-
@param msg
100-
Error message to print to serial console.
101-
*/
102-
/**************************************************************************/
103-
void WipperSnapper_LittleFS::fsHalt(String msg) {
104-
statusLEDSolid(WS_LED_STATUS_FS_WRITE);
105-
while (1) {
106-
WS_DEBUG_PRINTLN("Fatal Error: Halted execution!");
107-
WS_DEBUG_PRINTLN(msg.c_str());
108-
delay(1000);
109-
yield();
110-
}
111-
}
112-
1+
/*!
2+
* @file Wippersnapper_LittleFS.cpp
3+
*
4+
* Interfaces with LittleFS filesystem for ESP32, ESP8266 platforms.
5+
*
6+
* Adafruit invests time and resources providing this open source code,
7+
* please support Adafruit and open-source hardware by purchasing
8+
* products from Adafruit!
9+
*
10+
* Copyright (c) Brent Rubell 2021-2022 for Adafruit Industries.
11+
*
12+
* BSD license, all text here must be included in any redistribution.
13+
*
14+
*/
15+
#if defined(ARDUINO_FEATHER_ESP32) || \
16+
defined(ARDUINO_ESP8266_ADAFRUIT_HUZZAH) || \
17+
defined(ARDUINO_ADAFRUIT_ITSYBITSY_ESP32) || \
18+
defined(ARDUINO_ADAFRUIT_FEATHER_ESP32_V2) || \
19+
defined(ARDUINO_ADAFRUIT_QTPY_ESP32_PICO) || \
20+
defined(ARDUINO_ADAFRUIT_QTPY_ESP32C3)
21+
#include "WipperSnapper_LittleFS.h"
22+
23+
/**************************************************************************/
24+
/*!
25+
@brief Attempts to set up and initialize a pre-existing LittleFS
26+
filesystem.
27+
*/
28+
/**************************************************************************/
29+
WipperSnapper_LittleFS::WipperSnapper_LittleFS() {
30+
// Attempt to initialize filesystem
31+
if (!LittleFS.begin()) {
32+
setStatusLEDColor(RED);
33+
fsHalt("ERROR: Failure initializing LittleFS!");
34+
}
35+
}
36+
37+
/**************************************************************************/
38+
/*!
39+
@brief Destructor for LittleFS
40+
*/
41+
/**************************************************************************/
42+
WipperSnapper_LittleFS::~WipperSnapper_LittleFS() { LittleFS.end(); }
43+
44+
/**************************************************************************/
45+
/*!
46+
@brief Locates, opens and parses the WipperSnapper secrets file
47+
on the LittleFS filesystem.
48+
*/
49+
/**************************************************************************/
50+
void WipperSnapper_LittleFS::parseSecrets() {
51+
// Check if `secrets.json` file exists on FS
52+
if (!LittleFS.exists("/secrets.json")) {
53+
fsHalt("ERROR: No secrets.json found on filesystem - did you upload "
54+
"credentials?");
55+
}
56+
57+
// Attempt to open secrets.json file for reading
58+
File secretsFile = LittleFS.open("/secrets.json", "r");
59+
if (!secretsFile) {
60+
fsHalt("ERROR: Could not open secrets.json file for reading!");
61+
}
62+
63+
// Attempt to deserialize the file's JSON document
64+
JsonDocument doc;
65+
DeserializationError error = deserializeJson(doc, secretsFile);
66+
if (error) {
67+
fsHalt(String("ERROR: deserializeJson() failed with code ") +
68+
error.c_str());
69+
}
70+
71+
if (doc.containsKey("network_type_wifi")) {
72+
if (doc["network_type_wifi"].is<JsonObjectConst>()) {
73+
WS_DEBUG_PRINTLN("Found single wifi network in secrets.json");
74+
// Parse network credentials from secrets
75+
convertFromJson(doc["network_type_wifi"], WS._config.network);
76+
} else if (doc["network_type_wifi"].is<JsonArrayConst>()) {
77+
WS_DEBUG_PRINTLN("Found multiple wifi networks in secrets.json");
78+
// Parse network credentials from secrets
79+
int8_t networkCount = doc["network_type_wifi"].size();
80+
convertFromJson(doc["network_type_wifi"][networkCount - 1],
81+
WS._config.network);
82+
} else {
83+
fsHalt("ERROR: Unrecognised value type for network_type_wifi in "
84+
"secrets.json!");
85+
}
86+
} else {
87+
fsHalt("ERROR: Could not find network_type_wifi in secrets.json!");
88+
}
89+
// Extract a config struct from the JSON document
90+
WS._config = doc.as<secretsConfig>();
91+
92+
// Validate the config struct is not filled with default values
93+
if (strcmp(WS._config.aio_user, "YOUR_IO_USERNAME_HERE") == 0 ||
94+
strcmp(WS._config.aio_key, "YOUR_IO_KEY_HERE") == 0) {
95+
fsHalt(
96+
"ERROR: Invalid IO credentials in secrets.json! TO FIX: Please change "
97+
"io_username and io_key to match your Adafruit IO credentials!\n");
98+
}
99+
100+
if (strcmp(WS._config.network.ssid, "YOUR_WIFI_SSID_HERE") == 0 ||
101+
strcmp(WS._config.network.pass, "YOUR_WIFI_PASS_HERE") == 0) {
102+
fsHalt("ERROR: Invalid network credentials in secrets.json! TO FIX: Please "
103+
"change network_ssid and network_password to match your Adafruit IO "
104+
"credentials!\n");
105+
}
106+
107+
// Close the file
108+
secretsFile.close();
109+
110+
// Stop LittleFS, we no longer need it
111+
LittleFS.end();
112+
}
113+
114+
/**************************************************************************/
115+
/*!
116+
@brief Halts execution and blinks the status LEDs yellow.
117+
@param msg
118+
Error message to print to serial console.
119+
*/
120+
/**************************************************************************/
121+
void WipperSnapper_LittleFS::fsHalt(String msg) {
122+
statusLEDSolid(WS_LED_STATUS_FS_WRITE);
123+
while (1) {
124+
WS_DEBUG_PRINTLN("Fatal Error: Halted execution!");
125+
WS_DEBUG_PRINTLN(msg.c_str());
126+
delay(1000);
127+
yield();
128+
}
129+
}
130+
113131
#endif

src/provisioning/tinyusb/Wippersnapper_FS.cpp

Lines changed: 55 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
*
1414
*/
1515
#if defined(ARDUINO_MAGTAG29_ESP32S2) || defined(ARDUINO_METRO_ESP32S2) || \
16-
defined(ARDUINO_FUNHOUSE_ESP32S2) || defined(ADAFRUIT_PYPORTAL_M4_TITANO) || \
16+
defined(ARDUINO_FUNHOUSE_ESP32S2) || \
17+
defined(ADAFRUIT_PYPORTAL_M4_TITANO) || \
1718
defined(ADAFRUIT_METRO_M4_AIRLIFT_LITE) || defined(ADAFRUIT_PYPORTAL) || \
1819
defined(ARDUINO_ADAFRUIT_FEATHER_ESP32S2) || \
1920
defined(ARDUINO_ADAFRUIT_QTPY_ESP32S2) || \
@@ -295,7 +296,7 @@ bool Wippersnapper_FS::createBootFile() {
295296
void Wippersnapper_FS::createSecretsFile() {
296297
// Open file for writing
297298
File32 secretsFile = wipperFatFs.open("/secrets.json", FILE_WRITE);
298-
299+
299300
// Create a default secretsConfig structure
300301
secretsConfig secretsConfig;
301302
strcpy(secretsConfig.aio_user, "YOUR_IO_USERNAME_HERE");
@@ -317,7 +318,8 @@ void Wippersnapper_FS::createSecretsFile() {
317318
delay(2500);
318319

319320
// Signal to user that action must be taken (edit secrets.json)
320-
writeToBootOut("ERROR: Please edit the secrets.json file. Then, reset your board.\n");
321+
writeToBootOut(
322+
"ERROR: Please edit the secrets.json file. Then, reset your board.\n");
321323
#ifdef USE_DISPLAY
322324
WS._ui_helper->show_scr_error(
323325
"INVALID SETTINGS FILE",
@@ -344,33 +346,66 @@ void Wippersnapper_FS::parseSecrets() {
344346
JsonDocument doc;
345347
DeserializationError error = deserializeJson(doc, secretsFile);
346348
if (error) {
347-
fsHalt(String("ERROR: Unable to parse secrets.json file - deserializeJson() failed with code") + error.c_str());
349+
fsHalt(String("ERROR: Unable to parse secrets.json file - "
350+
"deserializeJson() failed with code") +
351+
error.c_str());
352+
}
353+
354+
if (doc.containsKey("network_type_wifi")) {
355+
if (doc["network_type_wifi"].is<JsonObjectConst>()) {
356+
WS_DEBUG_PRINTLN("Found single wifi network in secrets.json");
357+
// Parse network credentials from secrets
358+
convertFromJson(doc["network_type_wifi"], WS._config.network);
359+
} else if (doc["network_type_wifi"].is<JsonArrayConst>()) {
360+
WS_DEBUG_PRINTLN("Found multiple wifi networks in secrets.json");
361+
// Parse network credentials from secrets
362+
int8_t networkCount = doc["network_type_wifi"].size();
363+
convertFromJson(doc["network_type_wifi"][networkCount - 1],
364+
WS._config.network);
365+
} else {
366+
fsHalt("ERROR: Unrecognised value type for network_type_wifi in "
367+
"secrets.json!");
368+
}
369+
} else {
370+
fsHalt("ERROR: Could not find network_type_wifi in secrets.json!");
348371
}
349372

350373
// Extract a config struct from the JSON document
351-
WS._config = doc.as<secretsConfig>();
374+
WS._config = doc.as<secretsConfig>();
352375

353376
// Validate the config struct is not filled with default values
354-
if (strcmp(WS._config.aio_user, "YOUR_IO_USERNAME_HERE") == 0 || strcmp(WS._config.aio_key, "YOUR_IO_KEY_HERE") == 0) {
355-
writeToBootOut("ERROR: Invalid IO credentials in secrets.json! TO FIX: Please change io_username and io_key to match your Adafruit IO credentials!\n");
377+
if (strcmp(WS._config.aio_user, "YOUR_IO_USERNAME_HERE") == 0 ||
378+
strcmp(WS._config.aio_key, "YOUR_IO_KEY_HERE") == 0) {
379+
writeToBootOut(
380+
"ERROR: Invalid IO credentials in secrets.json! TO FIX: Please change "
381+
"io_username and io_key to match your Adafruit IO credentials!\n");
356382
#ifdef USE_DISPLAY
357383
WS._ui_helper->show_scr_error(
358384
"INVALID IO CREDS",
359-
"The \"io_username/io_key\" fields within secrets.json are invalid, please "
385+
"The \"io_username/io_key\" fields within secrets.json are invalid, "
386+
"please "
360387
"change it to match your Adafruit IO credentials. Then, press RESET.");
361388
#endif
362-
fsHalt("ERROR: Invalid IO credentials in secrets.json! TO FIX: Please change io_username and io_key to match your Adafruit IO credentials!");
389+
fsHalt(
390+
"ERROR: Invalid IO credentials in secrets.json! TO FIX: Please change "
391+
"io_username and io_key to match your Adafruit IO credentials!");
363392
}
364393

365-
if (strcmp(WS._config.network.ssid, "YOUR_WIFI_SSID_HERE") == 0 || strcmp(WS._config.network.pass, "YOUR_WIFI_PASS_HERE") == 0) {
366-
writeToBootOut("ERROR: Invalid network credentials in secrets.json! TO FIX: Please change network_ssid and network_password to match your Adafruit IO credentials!\n");
394+
if (strcmp(WS._config.network.ssid, "YOUR_WIFI_SSID_HERE") == 0 ||
395+
strcmp(WS._config.network.pass, "YOUR_WIFI_PASS_HERE") == 0) {
396+
writeToBootOut("ERROR: Invalid network credentials in secrets.json! TO "
397+
"FIX: Please change network_ssid and network_password to "
398+
"match your Adafruit IO credentials!\n");
367399
#ifdef USE_DISPLAY
368400
WS._ui_helper->show_scr_error(
369401
"INVALID NETWORK",
370-
"The \"network_ssid and network_password\" fields within secrets.json are invalid, please "
402+
"The \"network_ssid and network_password\" fields within secrets.json "
403+
"are invalid, please "
371404
"change it to match your WiFi credentials. Then, press RESET.");
372405
#endif
373-
fsHalt("ERROR: Invalid network credentials in secrets.json! TO FIX: Please change network_ssid and network_password to match your Adafruit IO credentials!");
406+
fsHalt("ERROR: Invalid network credentials in secrets.json! TO FIX: Please "
407+
"change network_ssid and network_password to match your Adafruit IO "
408+
"credentials!");
374409
}
375410

376411
// Close secrets.json file
@@ -437,7 +472,8 @@ void Wippersnapper_FS::createDisplayConfig() {
437472
// Create and fill JSON document from displayConfig
438473
JsonDocument doc;
439474
if (!doc.set(displayConfig)) {
440-
fsHalt("ERROR: Unable to set displayConfig, no space in arduinoJSON document!");
475+
fsHalt("ERROR: Unable to set displayConfig, no space in arduinoJSON "
476+
"document!");
441477
}
442478
// Write the file out to the filesystem
443479
serializeJsonPretty(doc, displayFile);
@@ -451,7 +487,8 @@ void Wippersnapper_FS::parseDisplayConfig(displayConfig &dispCfg) {
451487
if (!wipperFatFs.exists("/display_config.json")) {
452488
WS_DEBUG_PRINTLN("Could not find display_config.json, generating...");
453489
#ifdef ARDUINO_FUNHOUSE_ESP32S2
454-
createDisplayConfig(); // generate a default display_config.json for FunHouse
490+
createDisplayConfig(); // generate a default display_config.json for
491+
// FunHouse
455492
#endif
456493
}
457494

@@ -465,7 +502,9 @@ void Wippersnapper_FS::parseDisplayConfig(displayConfig &dispCfg) {
465502
JsonDocument doc;
466503
DeserializationError error = deserializeJson(doc, file);
467504
if (error) {
468-
fsHalt(String("FATAL ERROR: Unable to parse display_config.json - deserializeJson() failed with code") + error.c_str());
505+
fsHalt(String("FATAL ERROR: Unable to parse display_config.json - "
506+
"deserializeJson() failed with code") +
507+
error.c_str());
469508
}
470509
// Close the file, we're done with it
471510
file.close();

0 commit comments

Comments
 (0)