From a21acebdb447ec29a2a720df133ae4012ca3e846 Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Wed, 13 Jan 2021 13:51:49 +0100 Subject: [PATCH] Add support for Last Known Good Settings --- tasmota/settings.ino | 16 ++++++++++++---- tasmota/support_tasmota.ino | 9 +++++++++ tasmota/tasmota_globals.h | 5 +++-- 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/tasmota/settings.ino b/tasmota/settings.ino index d61f66743..b2d438df6 100644 --- a/tasmota/settings.ino +++ b/tasmota/settings.ino @@ -540,7 +540,7 @@ void SettingsSave(uint8_t rotate) #ifdef ESP8266 #ifdef USE_UFILESYS TfsSaveFile(TASM_FILE_SETTINGS, (const uint8_t*)&Settings, sizeof(Settings)); -#endif +#endif // USE_UFILESYS if (ESP.flashEraseSector(settings_location)) { ESP.flashWrite(settings_location * SPI_FLASH_SEC_SIZE, (uint32*)&Settings, sizeof(Settings)); } @@ -578,7 +578,7 @@ void SettingsLoad(void) { flash_location = 1; slot = 0; } -#endif +#endif // USE_UFILESYS while (slot <= max_slots) { // Read all config pages in search of valid and latest if (slot > 0) { flash_location = (1 == slot) ? FLASH_EEPROM_START : (2 == slot) ? SETTINGS_LOCATION : flash_location -1; @@ -602,7 +602,7 @@ void SettingsLoad(void) { TfsLoadFile(TASM_FILE_SETTINGS, (uint8_t*)&Settings, sizeof(Settings)); AddLog_P(LOG_LEVEL_NONE, PSTR(D_LOG_CONFIG "Loaded from File, " D_COUNT " %lu"), Settings.save_flag); } else -#endif +#endif // USE_UFILESYS { ESP.flashRead(settings_location * SPI_FLASH_SEC_SIZE, (uint32*)&Settings, sizeof(Settings)); AddLog_P(LOG_LEVEL_NONE, PSTR(D_LOG_CONFIG D_LOADED_FROM_FLASH_AT " %X, " D_COUNT " %lu"), settings_location, Settings.save_flag); @@ -617,7 +617,15 @@ void SettingsLoad(void) { #ifndef FIRMWARE_MINIMAL if ((0 == settings_location) || (Settings.cfg_holder != (uint16_t)CFG_HOLDER)) { // Init defaults if cfg_holder differs from user settings in my_user_config.h - SettingsDefault(); +#ifdef USE_UFILESYS + if (TfsLoadFile(TASM_FILE_SETTINGS_LKG, (uint8_t*)&Settings, sizeof(Settings)) && (Settings.cfg_crc32 == GetSettingsCrc32())) { + settings_location = 1; + AddLog_P(LOG_LEVEL_NONE, PSTR(D_LOG_CONFIG "Loaded from LKG File, " D_COUNT " %lu"), Settings.save_flag); + } else +#endif // USE_UFILESYS + { + SettingsDefault(); + } } settings_crc32 = GetSettingsCrc32(); #endif // FIRMWARE_MINIMAL diff --git a/tasmota/support_tasmota.ino b/tasmota/support_tasmota.ino index a6e0b2104..3ac17a490 100644 --- a/tasmota/support_tasmota.ino +++ b/tasmota/support_tasmota.ino @@ -886,6 +886,15 @@ void PerformEverySecond(void) ESP_getSketchSize(); // Init sketchsize as it can take up to 2 seconds } #endif + +#ifdef USE_UFILESYS + static bool settings_lkg = false; // Settings saved as Last Known Good + // Copy Settings as Last Known Good if no changes have been saved since 30 minutes + if (!settings_lkg && (UtcTime() > START_VALID_TIME) && (Settings.cfg_timestamp < UtcTime() - (30 * 60))) { + TfsSaveFile(TASM_FILE_SETTINGS_LKG, (const uint8_t*)&Settings, sizeof(Settings)); + settings_lkg = true; + } +#endif } /*-------------------------------------------------------------------------------------------*\ diff --git a/tasmota/tasmota_globals.h b/tasmota/tasmota_globals.h index 330a1a87e..4524410dd 100644 --- a/tasmota/tasmota_globals.h +++ b/tasmota/tasmota_globals.h @@ -225,8 +225,9 @@ const uint16_t LOG_BUFFER_SIZE = 4000; // Max number of characters in lo #error "Arduino ESP8266 Core versions before 2.7.1 are not supported" #endif -#define TASM_FILE_SETTINGS "/.settings" -#define TASM_FILE_ZIGBEE "/zb" +#define TASM_FILE_SETTINGS "/.settings" // Settings binary blob +#define TASM_FILE_SETTINGS_LKG "/.settings.lkg" // Last Known Good Settings binary blob +#define TASM_FILE_ZIGBEE "/zb" // Zigbee settings blob as used by CC2530 on ESP32 #ifndef MQTT_MAX_PACKET_SIZE #define MQTT_MAX_PACKET_SIZE 1200 // Bytes