diff --git a/tasmota/include/tasmota.h b/tasmota/include/tasmota.h index 60ce13c13..b37be129a 100644 --- a/tasmota/include/tasmota.h +++ b/tasmota/include/tasmota.h @@ -428,7 +428,7 @@ enum LightTypes { LT_BASIC, LT_PWM1, LT_PWM2, LT_PWM3, LT_PWM4, LT enum XsnsFunctions { FUNC_SETTINGS_OVERRIDE, FUNC_SETUP_RING1, FUNC_SETUP_RING2, FUNC_PRE_INIT, FUNC_INIT, FUNC_LOOP, FUNC_SLEEP_LOOP, FUNC_EVERY_50_MSECOND, FUNC_EVERY_100_MSECOND, FUNC_EVERY_200_MSECOND, FUNC_EVERY_250_MSECOND, FUNC_EVERY_SECOND, - FUNC_RESET_SETTINGS, FUNC_SAVE_SETTINGS, FUNC_SAVE_AT_MIDNIGHT, FUNC_SAVE_BEFORE_RESTART, FUNC_INTERRUPT_STOP, FUNC_INTERRUPT_START, + FUNC_RESET_SETTINGS, FUNC_RESTORE_SETTINGS, FUNC_SAVE_SETTINGS, FUNC_SAVE_AT_MIDNIGHT, FUNC_SAVE_BEFORE_RESTART, FUNC_INTERRUPT_STOP, FUNC_INTERRUPT_START, FUNC_AFTER_TELEPERIOD, FUNC_JSON_APPEND, FUNC_WEB_SENSOR, FUNC_WEB_COL_SENSOR, FUNC_MQTT_SUBSCRIBE, FUNC_MQTT_INIT, FUNC_SET_POWER, FUNC_SHOW_SENSOR, FUNC_ANY_KEY, FUNC_LED_LINK, diff --git a/tasmota/tasmota_support/settings.ino b/tasmota/tasmota_support/settings.ino index 981012033..c4e56179c 100644 --- a/tasmota/tasmota_support/settings.ino +++ b/tasmota/tasmota_support/settings.ino @@ -544,14 +544,22 @@ bool SettingsConfigRestore(void) { if (settings_size > sizeof(TSettings)) { uint8_t *filebuf_ptr = settings_buffer + sizeof(TSettings); while ((filebuf_ptr - settings_buffer) < settings_size) { - filebuf_ptr += 14; - uint32_t fsize = *filebuf_ptr; - filebuf_ptr++; - fsize += *filebuf_ptr << 8; - filebuf_ptr++; - AddLog(LOG_LEVEL_DEBUG, PSTR("CFG: Restore file %s (%d)"), (char*)filebuf_ptr -16, fsize); - TfsSaveFile((const char*)filebuf_ptr -16, (uint8_t*)filebuf_ptr, fsize); - filebuf_ptr += ((fsize / 16) * 16) + 16; + uint32_t driver = atoi((const char*)filebuf_ptr +8); // /.drvset012 = 12 + uint32_t fsize = filebuf_ptr[15] << 8 | filebuf_ptr[14]; // Tar header settings size + filebuf_ptr += 16; // Start of file settings + uint32_t buffer_crc32 = filebuf_ptr[3] << 24 | filebuf_ptr[2] << 16 | filebuf_ptr[1] << 8 | filebuf_ptr[0]; + bool valid_buffer = (GetCfgCrc32(filebuf_ptr +4, fsize -4) == buffer_crc32); + if (valid_buffer) { + XdrvMailbox.data = (char*)filebuf_ptr; + XdrvMailbox.index = fsize; + if (XdrvCallDriver(driver, FUNC_RESTORE_SETTINGS)) { + AddLog(LOG_LEVEL_DEBUG, PSTR("CFG: Restore driver %d"), driver); + } else { + AddLog(LOG_LEVEL_DEBUG, PSTR("CFG: Restore file %s (%d)"), (char*)filebuf_ptr -16, fsize); + TfsSaveFile((const char*)filebuf_ptr -16, (uint8_t*)filebuf_ptr, fsize); + } + } + filebuf_ptr += ((fsize / 16) * 16) + 16; // Next tar header or eof } } #endif // USE_UFILESYS diff --git a/tasmota/tasmota_xdrv_driver/xdrv_03_esp32_energy.ino b/tasmota/tasmota_xdrv_driver/xdrv_03_esp32_energy.ino index 7662bc5af..b539c9350 100644 --- a/tasmota/tasmota_xdrv_driver/xdrv_03_esp32_energy.ino +++ b/tasmota/tasmota_xdrv_driver/xdrv_03_esp32_energy.ino @@ -372,6 +372,14 @@ void EnergySettingsSave(void) { #endif // USE_UFILESYS } +bool EnergySettingsRestore(void) { +#ifdef USE_UFILESYS + uint32_t max_size = (XdrvMailbox.index > sizeof(tEnergySettings)) ? sizeof(tEnergySettings) : XdrvMailbox.index; + memcpy((uint8_t*)&Energy->Settings, (uint8_t*)XdrvMailbox.data, max_size); // Restore version and auto upgrade after restart + return true; +#endif // USE_UFILESYS +} + /********************************************************************************************/ const uint16_t GUISZ = 600; // Max number of characters in WebEnergyFmt string @@ -1764,6 +1772,9 @@ bool Xdrv03(uint32_t function) case FUNC_RESET_SETTINGS: EnergySettingsLoad(1); break; + case FUNC_RESTORE_SETTINGS: + result = EnergySettingsRestore(); + break; case FUNC_SAVE_SETTINGS: EnergySettingsSave(); EnergyRtcSettingsSave(); diff --git a/tasmota/tasmota_xdrv_driver/xdrv_122_file_settings_demo.ino b/tasmota/tasmota_xdrv_driver/xdrv_122_file_settings_demo.ino index fb6c2eff8..e064c6941 100644 --- a/tasmota/tasmota_xdrv_driver/xdrv_122_file_settings_demo.ino +++ b/tasmota/tasmota_xdrv_driver/xdrv_122_file_settings_demo.ino @@ -34,7 +34,7 @@ #define DRV_DEMO_MAX_DRV_TEXT 16 -const uint32_t DRV_DEMO_VERSION = 0x01010101; // Latest driver version (See settings deltas below) +const uint32_t DRV_DEMO_VERSION = 0x0101; // Latest driver version (See settings deltas below) // Demo command line commands const char kDrvDemoCommands[] PROGMEM = "Drv|" // Prefix @@ -46,7 +46,8 @@ void (* const DrvDemoCommand[])(void) PROGMEM = { // Global structure containing driver saved variables struct { uint32_t crc32; // To detect file changes - uint32_t version; // To detect driver function changes + uint16_t version; // To detect driver function changes + uint16_t spare; char drv_text[DRV_DEMO_MAX_DRV_TEXT -1][10]; } DrvDemoSettings; @@ -155,6 +156,14 @@ void DrvDemoSettingsSave(void) { #endif // USE_UFILESYS } +bool DrvDemoSettingsRestore(void) { +#ifdef USE_UFILESYS + uint32_t max_size = (XdrvMailbox.index > sizeof(DrvDemoSettings)) ? sizeof(DrvDemoSettings) : XdrvMailbox.index; + memcpy((uint8_t*)&DrvDemoSettings, (uint8_t*)XdrvMailbox.data, max_size); // Restore version and auto upgrade after restart + return true; +#endif // USE_UFILESYS +} + /*********************************************************************************************\ * Interface \*********************************************************************************************/ @@ -166,6 +175,9 @@ bool Xdrv122(uint32_t function) { case FUNC_RESET_SETTINGS: DrvDemoSettingsLoad(1); break; + case FUNC_RESTORE_SETTINGS: + result = DrvDemoSettingsRestore(); + break; case FUNC_SAVE_SETTINGS: DrvDemoSettingsSave(); break; diff --git a/tasmota/tasmota_xdrv_driver/xdrv_27_esp32_shutter.ino b/tasmota/tasmota_xdrv_driver/xdrv_27_esp32_shutter.ino index ca10aa92a..5e489e29a 100644 --- a/tasmota/tasmota_xdrv_driver/xdrv_27_esp32_shutter.ino +++ b/tasmota/tasmota_xdrv_driver/xdrv_27_esp32_shutter.ino @@ -53,7 +53,7 @@ const char HTTP_MSG_SLIDER_SHUTTER[] PROGMEM = "