From 6858f9957c9fbf66a40fa7ff1abfb0076f50c3e4 Mon Sep 17 00:00:00 2001 From: Mitch Bradley Date: Fri, 11 Jun 2021 12:44:30 -1000 Subject: [PATCH] Improved config file handling and messaging Instead of using stdio calls like fopen(), the config file reading code now uses the same Arduino FS class methods that the rest of the code uses. This was done simply for consistency. One might argue that it would be better for everything to use stdio; the problem is that there are some parts of WebUI that depend on Arduino file class semantics. I also commented out some config debug messages that are no longer helpful. --- Grbl_Esp32/src/Grbl.cpp | 6 +++- Grbl_Esp32/src/MachineConfig.cpp | 41 +++++++++------------------ Grbl_Esp32/src/WebUI/WifiServices.cpp | 4 --- 3 files changed, 19 insertions(+), 32 deletions(-) diff --git a/Grbl_Esp32/src/Grbl.cpp b/Grbl_Esp32/src/Grbl.cpp index c84a0316..7eac50c3 100644 --- a/Grbl_Esp32/src/Grbl.cpp +++ b/Grbl_Esp32/src/Grbl.cpp @@ -22,6 +22,7 @@ #include "MachineConfig.h" #include +#include void grbl_init() { try { @@ -43,6 +44,10 @@ void grbl_init() { report_machine_type(CLIENT_SERIAL); #endif + if (!SPIFFS.begin(true)) { + log_error("Cannot mount the local filesystem"); + } + // Load Grbl settings from non-volatile storage debug_serial("Initializing settings..."); settings_init(); @@ -64,7 +69,6 @@ void grbl_init() { config->_axes->read_settings(); config->_axes->init(); - info_serial("Initializing system..."); config->_control->init(); init_output_pins(); // Configure pinout pins and pin-change interrupt (Renamed due to conflict with esp32 files) memset(sys_position, 0, sizeof(sys_position)); // Clear machine position. diff --git a/Grbl_Esp32/src/MachineConfig.cpp b/Grbl_Esp32/src/MachineConfig.cpp index e2150693..d5bc47ef 100644 --- a/Grbl_Esp32/src/MachineConfig.cpp +++ b/Grbl_Esp32/src/MachineConfig.cpp @@ -476,36 +476,29 @@ void MachineConfig::afterParse() { } size_t MachineConfig::readFile(const char* filename, char*& buffer) { - if (!SPIFFS.begin(true)) { - log_info("Cannot mount the local filesystem"); - return 0; + String path = filename; + if ((path.length() > 0) && (path[0] != '/')) { + path = "/" + path; } - - FILE* file = fopen(filename, "rb"); + File file = SPIFFS.open(path, FILE_READ); if (!file) { - log_info("Cannot open the config file " << filename); + log_info("Cannot open the config file " << path); return 0; } - - // Let's just read the entire file in one chunk for now. If we get - // in trouble with this, we can cut it in pieces and read it per chunk. - fseek(file, 0, SEEK_END); - auto filesize = ftell(file); - log_debug("Configuration file has " << int(filesize) << " bytes"); - - fseek(file, 0, SEEK_SET); + auto filesize = file.size(); + // log_debug("Configuration file has " << int(filesize) << " bytes"); buffer = new char[filesize + 1]; long pos = 0; while (pos < filesize) { - auto read = fread(buffer + pos, 1, filesize - pos, file); + auto read = file.read((uint8_t*)(buffer + pos), filesize - pos); if (read == 0) { break; } pos += read; } - fclose(file); + file.close(); buffer[filesize] = 0; log_debug("Read config file:\r\n" << buffer); @@ -522,9 +515,7 @@ size_t MachineConfig::readFile(const char* filename, char*& buffer) { char defaultConfig[] = "name: Default\nboard: None\n"; bool MachineConfig::load(const char* filename) { - // Regardless of what we do next, we _always_ want a MachineConfig instance. - - log_info("Heap size before load config is " << uint32_t(xPortGetFreeHeapSize())); + // log_info("Heap size before load config is " << uint32_t(xPortGetFreeHeapSize())); // If the system crashes we skip the config file and use the default // builtin config. This helps prevent reset loops on bad config files. @@ -548,7 +539,7 @@ bool MachineConfig::load(const char* filename) { // Process file: bool successful = false; try { - log_info("Heap size before parsing is " << uint32_t(xPortGetFreeHeapSize())); + // log_info("Heap size before parsing is " << uint32_t(xPortGetFreeHeapSize())); Configuration::Parser parser(input->begin(), input->end()); Configuration::ParserHandler handler(parser); @@ -568,7 +559,7 @@ bool MachineConfig::load(const char* filename) { log_info("Parsed configuration. Running after-parse tasks"); - log_info("Heap size before after-parse is " << uint32_t(xPortGetFreeHeapSize())); + // log_info("Heap size before after-parse is " << uint32_t(xPortGetFreeHeapSize())); try { Configuration::AfterParse afterParse; @@ -578,7 +569,7 @@ bool MachineConfig::load(const char* filename) { log_info("Validating configuration"); - log_info("Heap size before validation is " << uint32_t(xPortGetFreeHeapSize())); + // log_info("Heap size before validation is " << uint32_t(xPortGetFreeHeapSize())); try { Configuration::Validator validator; @@ -588,14 +579,10 @@ bool MachineConfig::load(const char* filename) { log_info("Validated configuration"); - log_info("Heap size after configuation load is " << uint32_t(xPortGetFreeHeapSize())); + // log_info("Heap size after configuation load is " << uint32_t(xPortGetFreeHeapSize())); successful = true; - // TODO FIXME: If we get here, we want to make the settings live by saving them as - // '/spiffs/config.yaml.new', storing it, and then deleting the .yaml and renaming it. - // That way, we can always check if the yaml is there, and if it's not, load the yaml.new. - } catch (const Configuration::ParseException& ex) { sys.state = State::ConfigAlarm; auto startNear = ex.Near(); diff --git a/Grbl_Esp32/src/WebUI/WifiServices.cpp b/Grbl_Esp32/src/WebUI/WifiServices.cpp index 434f3b71..86ac5cb7 100644 --- a/Grbl_Esp32/src/WebUI/WifiServices.cpp +++ b/Grbl_Esp32/src/WebUI/WifiServices.cpp @@ -59,8 +59,6 @@ namespace WebUI { String& h = config->_comms->_hostname; - //Start SPIFFS - SPIFFS.begin(true); # ifdef ENABLE_OTA ArduinoOTA .onStart([]() { @@ -137,8 +135,6 @@ namespace WebUI { # ifdef ENABLE_OTA ArduinoOTA.end(); # endif - //Stop SPIFFS - SPIFFS.end(); # ifdef ENABLE_MDNS //Stop mDNS MDNS.end();