diff --git a/tasmota/tasmota_xdrv_driver/xdrv_50_filesystem.ino b/tasmota/tasmota_xdrv_driver/xdrv_50_filesystem.ino index db60d2e74..fc7ca39b8 100644 --- a/tasmota/tasmota_xdrv_driver/xdrv_50_filesystem.ino +++ b/tasmota/tasmota_xdrv_driver/xdrv_50_filesystem.ino @@ -306,6 +306,16 @@ bool TfsFileExists(const char *fname){ return yes; } +size_t TfsFileSize(const char *fname){ + if (!ffs_type) { return 0; } + + File file = ffsp->open(fname, "r"); + if (!file) { return 0; } + size_t flen = file.size(); + file.close(); + return flen; +} + bool TfsSaveFile(const char *fname, const uint8_t *buf, uint32_t len) { if (!ffs_type) { return false; } #ifdef USE_WEBCAM @@ -362,7 +372,6 @@ bool TfsInitFile(const char *fname, uint32_t len, uint8_t init_value) { bool TfsLoadFile(const char *fname, uint8_t *buf, uint32_t len) { if (!ffs_type) { return false; } - if (!TfsFileExists(fname)) { return false; } File file = ffsp->open(fname, "r"); if (!file) { @@ -371,15 +380,19 @@ bool TfsLoadFile(const char *fname, uint8_t *buf, uint32_t len) { } size_t flen = file.size(); - if (len > flen){ - len = flen; - } - + if (len > flen) { len = flen; } // Adjust requested length to smaller file length file.read(buf, len); file.close(); return true; } +String TfsLoadString(const char *fname) { + // Use a reasonable amount of stack space considering 4k/8k available on ESP8266/ESP32 and manageable string length + char buf[2048] = { 0 }; // Prepare empty string of max 2047 characters on stack + TfsLoadFile(fname, (uint8_t*)buf, 2047); // Leave last position as end of string ('\0') + return String(buf); // Received string or empty on error +} + bool TfsDeleteFile(const char *fname) { if (!ffs_type) { return false; } @@ -825,7 +838,7 @@ void UfsListDir(char *path, uint8_t depth) { editpath[0]=0; #endif // GUI_TRASH_FILE ext_snprintf_P(npath, sizeof(npath), UFS_FORM_SDC_HREF, ppe, epe); - WSContentSend_P(UFS_FORM_SDC_DIRb, hiddable ? UFS_FORM_SDC_DIR_HIDDABLE : UFS_FORM_SDC_DIR_NORMAL, npath, epe, + WSContentSend_P(UFS_FORM_SDC_DIRb, hiddable ? UFS_FORM_SDC_DIR_HIDDABLE : UFS_FORM_SDC_DIR_NORMAL, npath, epe, HtmlEscape(name).c_str(), tstr.c_str(), entry.size(), delpath, editpath); } entry.close(); diff --git a/tasmota/tasmota_xnrg_energy/xnrg_29_modbus.ino b/tasmota/tasmota_xnrg_energy/xnrg_29_modbus.ino index a95f65f8a..a98f94fe2 100644 --- a/tasmota/tasmota_xnrg_energy/xnrg_29_modbus.ino +++ b/tasmota/tasmota_xnrg_energy/xnrg_29_modbus.ino @@ -75,7 +75,6 @@ //#define ENERGY_MODBUS_DEBUG_SHOW #define ENERGY_MODBUS_FILE "/modbus.json" // Modbus parameter file name used by filesystem -#define ENERGY_MODBUS_MAX_FSIZE 1024 // Modbus parameter file max size const uint16_t nrg_mbs_reg_not_used = 0xFFFF; // Odd number 65535 is unused register @@ -482,15 +481,9 @@ bool EnergyModbusReadRegisters(void) { String modbus = ""; #ifdef USE_UFILESYS - char *modbus_file = (char*)calloc(ENERGY_MODBUS_MAX_FSIZE, 1); - if (modbus_file) { - if (TfsLoadFile(ENERGY_MODBUS_FILE, (uint8_t*)modbus_file, ENERGY_MODBUS_MAX_FSIZE -1)) { - if (strlen(modbus_file) < ENERGY_MODBUS_MAX_FSIZE) { - modbus = modbus_file; - AddLog(LOG_LEVEL_DEBUG, PSTR("NRG: Loaded from File")); - } - } - free(modbus_file); + modbus = TfsLoadString(ENERGY_MODBUS_FILE); + if (modbus.length()) { + AddLog(LOG_LEVEL_DEBUG, PSTR("NRG: Loaded from File")); } #endif // USE_UFILESYS @@ -508,13 +501,12 @@ bool EnergyModbusReadRegisters(void) { } #endif // USE_SCRIPT - if (!modbus.length()) { return false; } // File not found + if (modbus.length() < 7) { return false; } // File not found or Invalid JSON + // AddLog(LOG_LEVEL_DEBUG, PSTR("NRG: File '%s'"), modbus.c_str()); const char* json = modbus.c_str(); uint32_t len = strlen(json) +1; - if (len < 7) { return false; } // Invalid JSON - char json_buffer[len]; memcpy(json_buffer, json, len); // Keep original safe JsonParser parser(json_buffer);