Skip to content

Commit

Permalink
WIP: more fsHalt
Browse files Browse the repository at this point in the history
  • Loading branch information
tyeth committed Jan 26, 2024
1 parent 6fe49af commit 49e8be7
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 38 deletions.
5 changes: 5 additions & 0 deletions src/Wippersnapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2306,9 +2306,14 @@ void Wippersnapper::errorWriteHang(String error) {
WS_DEBUG_PRINTLN(error);
#ifdef USE_TINYUSB
_fileSystem->writeToBootOut(error.c_str());
delay(500);
TinyUSBDevice.attach();
delay(500);
#endif
// Signal and hang forever
while (1) {
WS_DEBUG_PRINTLN("ERROR: Halted execution");
WS_DEBUG_PRINTLN(error.c_str());
WS.feedWDT();
statusLEDBlink(WS_LED_STATUS_ERROR_RUNTIME);
delay(1000);
Expand Down
29 changes: 13 additions & 16 deletions src/provisioning/littlefs/WipperSnapper_LittleFS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
WipperSnapper_LittleFS::WipperSnapper_LittleFS() {
// Attempt to initialize filesystem
if (!LittleFS.begin()) {
WS_DEBUG_PRINTLN("ERROR: Failure initializing LittleFS!");
setStatusLEDColor(RED);
fsHalt("ERROR: Failure initializing LittleFS!");
while (1)
;
}
Expand All @@ -51,25 +51,21 @@ WipperSnapper_LittleFS::~WipperSnapper_LittleFS() { LittleFS.end(); }
void WipperSnapper_LittleFS::parseSecrets() {
// Check if `secrets.json` file exists on FS
if (!LittleFS.exists("/secrets.json")) {
WS_DEBUG_PRINTLN("ERROR: No secrets.json found on filesystem - did you "
"upload credentials?");
fsHalt();
fsHalt("ERROR: No secrets.json found on filesystem - did you upload "
"credentials?");
}

// Attempt to open secrets.json file for reading
File secretsFile = LittleFS.open("/secrets.json", "r");
if (!secretsFile) {
WS_DEBUG_PRINTLN("ERROR: Could not open secrets.json file for reading!");
fsHalt();
fsHalt("ERROR: Could not open secrets.json file for reading!");
}

// Attempt to deserialize the file's JSON document
JsonDocument doc;
DeserializationError error = deserializeJson(doc, secretsFile);
if (error) {
WS_DEBUG_PRINT("ERROR: deserializeJson() failed with code ");
WS_DEBUG_PRINTLN(error.c_str());
fsHalt();
fsHalt(String("ERROR: deserializeJson() failed with code ") + error);
}

// Extract a config struct from the JSON document
Expand All @@ -78,18 +74,16 @@ void WipperSnapper_LittleFS::parseSecrets() {
// Validate the config struct is not filled with default values
if (strcmp(WS._config.aio_user, "YOUR_IO_USERNAME_HERE") == 0 ||
strcmp(WS._config.aio_key, "YOUR_IO_KEY_HERE") == 0) {
WS_DEBUG_PRINTLN(
fsHalt(
"ERROR: Invalid IO credentials in secrets.json! TO FIX: Please change "
"io_username and io_key to match your Adafruit IO credentials!\n");
fsHalt();
}

if (strcmp(WS._config.network.ssid, "YOUR_WIFI_SSID_HERE") == 0 ||
strcmp(WS._config.network.pass, "YOUR_WIFI_PASS_HERE") == 0) {
WS_DEBUG_PRINTLN("ERROR: Invalid network credentials in secrets.json! TO "
"FIX: Please change network_ssid and network_password to "
"match your Adafruit IO credentials!\n");
fsHalt();
fsHalt("ERROR: Invalid network credentials in secrets.json! TO FIX: Please "
"change network_ssid and network_password to match your Adafruit IO "
"credentials!\n");
}

// Close the file
Expand All @@ -99,11 +93,14 @@ void WipperSnapper_LittleFS::parseSecrets() {
LittleFS.end();
}

void WipperSnapper_LittleFS::fsHalt() {
void WipperSnapper_LittleFS::fsHalt(String msg) {
while (1) {
WS_DEBUG_PRINTLN("ERROR: Halted execution!");
WS_DEBUG_PRINTLN(msg.c_str());
statusLEDSolid(WS_LED_STATUS_FS_WRITE);
delay(1000);
yield();
}
}

#endif
2 changes: 1 addition & 1 deletion src/provisioning/littlefs/WipperSnapper_LittleFS.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class WipperSnapper_LittleFS {
WipperSnapper_LittleFS();
~WipperSnapper_LittleFS();
void parseSecrets();
void fsHalt();
void fsHalt(String msg="");
};

extern Wippersnapper WS;
Expand Down
40 changes: 20 additions & 20 deletions src/provisioning/tinyusb/Wippersnapper_FS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ Wippersnapper_FS::Wippersnapper_FS() {

// If a filesystem does not already exist - attempt to initialize a new
// filesystem
if (!initFilesystem() || !initFilesystem(true)) {
WS_DEBUG_PRINTLN("ERROR Initializing Filesystem");
if (!initFilesystem() && !initFilesystem(true)) {
setStatusLEDColor(RED);
fsHalt("ERROR Initializing Filesystem");
while (1)
;
}
Expand All @@ -107,7 +107,7 @@ Wippersnapper_FS::Wippersnapper_FS() {

// If we created a new filesystem, halt until user RESETs device.
if (_freshFS)
fsHalt();
fsHalt("New filesystem created! Please RESET your board.");
}

/************************************************************/
Expand Down Expand Up @@ -325,7 +325,7 @@ void Wippersnapper_FS::createSecretsFile() {
"Please edit it to reflect your Adafruit IO and network credentials. "
"When you're done, press RESET on the board.");
#endif
fsHalt();
fsHalt("ERROR: Please edit the secrets.json file. Then, reset your board.");
}

/**************************************************************************/
Expand All @@ -337,17 +337,14 @@ void Wippersnapper_FS::parseSecrets() {
// Attempt to open the secrets.json file for reading
File32 secretsFile = wipperFatFs.open("/secrets.json");
if (!secretsFile) {
WS_DEBUG_PRINTLN("ERROR: Could not open secrets.json file for reading!");
fsHalt();
fsHalt("ERROR: Could not open secrets.json file for reading!");
}

// Attempt to deserialize the file's JSON document
JsonDocument doc;
DeserializationError error = deserializeJson(doc, secretsFile);
if (error) {
WS_DEBUG_PRINT("ERROR: deserializeJson() failed with code ");
WS_DEBUG_PRINTLN(error.c_str());
fsHalt();
fsHalt(String("ERROR: Unable to parse secrets.json file - deserializeJson() failed with code") + error.c_str());
}

// Extract a config struct from the JSON document
Expand All @@ -362,7 +359,7 @@ void Wippersnapper_FS::parseSecrets() {
"The \"io_username/io_key\" fields within secrets.json are invalid, please "
"change it to match your Adafruit IO credentials. Then, press RESET.");
#endif
fsHalt();
fsHalt("ERROR: Invalid IO credentials in secrets.json! TO FIX: Please change io_username and io_key to match your Adafruit IO credentials!");
}

if (strcmp(WS._config.network.ssid, "YOUR_WIFI_SSID_HERE") == 0 || strcmp(WS._config.network.pass, "YOUR_WIFI_PASS_HERE") == 0) {
Expand All @@ -373,7 +370,7 @@ void Wippersnapper_FS::parseSecrets() {
"The \"network_ssid and network_password\" fields within secrets.json are invalid, please "
"change it to match your WiFi credentials. Then, press RESET.");
#endif
fsHalt();
fsHalt("ERROR: Invalid network credentials in secrets.json! TO FIX: Please change network_ssid and network_password to match your Adafruit IO credentials!");
}

// Close secrets.json file
Expand All @@ -396,16 +393,24 @@ void Wippersnapper_FS::writeToBootOut(PGM_P str) {
bootFile.close();
} else {
WS_DEBUG_PRINTLN("ERROR: Unable to open wipper_boot_out.txt for logging!");
// feels like we should check why, if good use-case ok, otherwise fsHalt
// as indicates fs corruption or disc access issue (maybe latter is okay)
}
}

/**************************************************************************/
/*!
@brief Halts execution and blinks the status LEDs yellow.
@param msg
Error message to print to serial console.
*/
/**************************************************************************/
void Wippersnapper_FS::fsHalt() {
void Wippersnapper_FS::fsHalt(String msg) {
TinyUSBDevice.attach();
delay(1500);
while (1) {
WS_DEBUG_PRINTLN("ERROR: Halted execution!");
WS_DEBUG_PRINTLN(msg.c_str());
// statusLEDSolid(WS_LED_STATUS_FS_WRITE);
delay(1000);
yield();
Expand All @@ -432,8 +437,7 @@ void Wippersnapper_FS::createDisplayConfig() {
// Create and fill JSON document from displayConfig
JsonDocument doc;
if (!doc.set(displayConfig)) {
WS_DEBUG_PRINTLN("ERROR: Unable to set displayConfig, no space in arduinoJSON document!");
fsHalt();
fsHalt("ERROR: Unable to set displayConfig, no space in arduinoJSON document!");
}
// Write the file out to the filesystem
serializeJsonPretty(doc, displayFile);
Expand All @@ -454,18 +458,14 @@ void Wippersnapper_FS::parseDisplayConfig(displayConfig &dispCfg) {
// Attempt to open file for JSON parsing
File32 file = wipperFatFs.open("/display_config.json", FILE_READ);
if (!file) {
WS_DEBUG_PRINTLN(
"FATAL ERROR: Unable to open display_config.json for parsing");
fsHalt();
fsHalt("FATAL ERROR: Unable to open display_config.json for parsing");
}

// Attempt to deserialize the file's json document
JsonDocument doc;
DeserializationError error = deserializeJson(doc, file);
if (error) {
WS_DEBUG_PRINTLN("deserializeJson() of display file failed, rc:")
Serial.println(error.c_str());
fsHalt();
fsHalt(String("FATAL ERROR: Unable to parse display_config.json - deserializeJson() failed with code") + error);
}
// Close the file, we're done with it
file.close();
Expand Down
2 changes: 1 addition & 1 deletion src/provisioning/tinyusb/Wippersnapper_FS.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class Wippersnapper_FS {
void createSecretsFile();
bool createBootFile();
void writeToBootOut(PGM_P str);
void fsHalt();
void fsHalt(String msg = "");

void parseSecrets();

Expand Down

0 comments on commit 49e8be7

Please sign in to comment.