From bda32739a8d55f970f537435c12c4e36bb4f7926 Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Mon, 13 Mar 2023 15:41:21 +0100 Subject: [PATCH] Support for reset settings on filesystem --- CHANGELOG.md | 1 + tasmota/include/tasmota.h | 2 +- tasmota/tasmota_support/support_tasmota.ino | 3 + .../xdrv_03_esp32_energy.ino | 15 ++- .../xdrv_122_file_settings_demo.ino | 108 ++++++++---------- .../xdrv_86_esp32_sonoff_spm.ino | 15 ++- .../xdrv_87_esp32_sonoff_tm1621.ino | 15 ++- 7 files changed, 86 insertions(+), 73 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ba7048cd9..6be73cfb3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ All notable changes to this project will be documented in this file. - Berry `webclient` `collect_headers()` and `set_headers` - Display TM1650 commands like TM1637 (#18109) - Berry add `web_get_arg` event to drivers when `FUNC_WEB_GET_ARG` event is processed +- Support for reset settings on filesystem ### Breaking Changed - Shelly Pro 4PM using standard MCP23xxx driver and needs one time Auto-Configuration diff --git a/tasmota/include/tasmota.h b/tasmota/include/tasmota.h index 7cf0af7d3..78b17d960 100644 --- a/tasmota/include/tasmota.h +++ b/tasmota/include/tasmota.h @@ -389,7 +389,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_SAVE_SETTINGS, FUNC_SAVE_AT_MIDNIGHT, FUNC_SAVE_BEFORE_RESTART, FUNC_INTERRUPT_STOP, FUNC_INTERRUPT_START, + FUNC_RESET_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/support_tasmota.ino b/tasmota/tasmota_support/support_tasmota.ino index e22ad5cba..845f18766 100644 --- a/tasmota/tasmota_support/support_tasmota.ino +++ b/tasmota/tasmota_support/support_tasmota.ino @@ -1513,6 +1513,9 @@ void Every250mSeconds(void) SettingsUpdateText(SET_MQTT_TOPIC, storage_mqtttopic); Settings->mqtt_port = mqtt_port; } + + XdrvCall(FUNC_RESET_SETTINGS); + TasmotaGlobal.restart_flag = 3; // Finish backlog then Restart 1 } else if (213 == TasmotaGlobal.restart_flag) { // Reset 3 diff --git a/tasmota/tasmota_xdrv_driver/xdrv_03_esp32_energy.ino b/tasmota/tasmota_xdrv_driver/xdrv_03_esp32_energy.ino index c99219145..122c1a62b 100644 --- a/tasmota/tasmota_xdrv_driver/xdrv_03_esp32_energy.ino +++ b/tasmota/tasmota_xdrv_driver/xdrv_03_esp32_energy.ino @@ -273,7 +273,7 @@ bool EnergyRtcSettingsValid(void) { const uint32_t XDRV_03_VERSION = 0x0102; // Latest driver version (See settings deltas below) -void EnergySettingsLoad(void) { +void EnergySettingsLoad(bool erase) { // *** Start init default values in case file is not found *** memset(&Energy->Settings, 0x00, sizeof(tEnergySettings)); Energy->Settings.version = XDRV_03_VERSION; @@ -320,7 +320,10 @@ void EnergySettingsLoad(void) { char filename[20]; // Use for drivers: snprintf_P(filename, sizeof(filename), PSTR(TASM_FILE_DRIVER), XDRV_03); - if (TfsLoadFile(filename, (uint8_t*)&Energy->Settings, sizeof(tEnergySettings))) { + if (erase) { + TfsDeleteFile(filename); // Use defaults + } + else if (TfsLoadFile(filename, (uint8_t*)&Energy->Settings, sizeof(tEnergySettings))) { if (Energy->Settings.version != XDRV_03_VERSION) { // Fix version dependent changes // *** Start fix possible setting deltas *** @@ -336,7 +339,8 @@ void EnergySettingsLoad(void) { EnergySettingsSave(); } AddLog(LOG_LEVEL_INFO, PSTR("CFG: Energy loaded from file")); - } else { + } + else { // File system not ready: No flash space reserved for file system AddLog(LOG_LEVEL_INFO, PSTR("CFG: Energy use defaults as file system not ready or file not found")); } @@ -1338,7 +1342,7 @@ void EnergyDrvInit(void) { Energy = (tEnergy*)calloc(sizeof(tEnergy), 1); // Need calloc to reset registers to 0/false if (!Energy) { return; } - EnergySettingsLoad(); + EnergySettingsLoad(0); EnergyRtcSettingsLoad(); // Energy->voltage_common = false; @@ -1746,6 +1750,9 @@ bool Xdrv03(uint32_t function) case FUNC_SERIAL: result = XnrgCall(FUNC_SERIAL); break; + case FUNC_RESET_SETTINGS: + EnergySettingsLoad(1); + 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 117bebc83..7637e8259 100644 --- a/tasmota/tasmota_xdrv_driver/xdrv_122_file_settings_demo.ino +++ b/tasmota/tasmota_xdrv_driver/xdrv_122_file_settings_demo.ino @@ -78,96 +78,81 @@ void CmndDrvText(void) { * Driver Settings load and save \*********************************************************************************************/ -uint32_t DrvDemoSettingsCrc32(void) { - // Use Tasmota CRC calculation function - return GetCfgCrc32((uint8_t*)&DrvDemoSettings +4, sizeof(DrvDemoSettings) -4); // Skip crc32 -} - -void DrvDemoSettingsDefault(void) { - // Init default values in case file is not found +void DrvDemoSettingsLoad(bool erase) { + // Called from FUNC_PRE_INIT (erase = 0) once at restart + // Called from FUNC_RESET_SETTINGS (erase = 1) after command reset 4, 5, or 6 + // *** Start init default values in case file is not found *** AddLog(LOG_LEVEL_INFO, PSTR("DRV: " D_USE_DEFAULTS)); memset(&DrvDemoSettings, 0x00, sizeof(DrvDemoSettings)); DrvDemoSettings.version = DRV_DEMO_VERSION; // Init any other parameter in struct DrvDemoSettings snprintf_P(DrvDemoSettings.drv_text[0], sizeof(DrvDemoSettings.drv_text[0]), PSTR("Azalea")); -} -void DrvDemoSettingsDelta(void) { - // Fix possible setting deltas - - if (DrvDemoSettings.version != DRV_DEMO_VERSION) { // Fix version dependent changes - - if (Settings->version < 0x01010100) { - AddLog(LOG_LEVEL_INFO, PSTR("DRV: Update oldest version restore")); - - } - if (Settings->version < 0x01010101) { - AddLog(LOG_LEVEL_INFO, PSTR("DRV: Update old version restore")); - - } - - // Set current version and save settings - DrvDemoSettings.version = DRV_DEMO_VERSION; - DrvDemoSettingsSave(); - } -} - -void DrvDemoSettingsLoad(void) { - // Called from FUNC_PRE_INIT once at restart - - // Init default values in case file is not found - DrvDemoSettingsDefault(); + // *** End Init default values *** +#ifndef USE_UFILESYS + AddLog(LOG_LEVEL_INFO, PSTR("CFG: Demo use defaults as file system not enabled")); +#else // Try to load file /.drvset122 char filename[20]; // Use for sensors: // snprintf_P(filename, sizeof(filename), PSTR(TASM_FILE_SENSOR), XSNS_122); // Use for drivers: snprintf_P(filename, sizeof(filename), PSTR(TASM_FILE_DRIVER), XDRV_122); - - AddLog(LOG_LEVEL_INFO, PSTR("DRV: About to load settings from file %s"), filename); - -#ifdef USE_UFILESYS - if (TfsLoadFile(filename, (uint8_t*)&DrvDemoSettings, sizeof(DrvDemoSettings))) { - // Fix possible setting deltas - DrvDemoSettingsDelta(); - } else { - // File system not ready: No flash space reserved for file system - AddLog(LOG_LEVEL_INFO, PSTR("DRV: ERROR File system not ready or file not found")); + if (erase) { + TfsDeleteFile(filename); // Use defaults } -#else - AddLog(LOG_LEVEL_INFO, PSTR("DRV: ERROR File system not enabled")); -#endif // USE_UFILESYS + else if (TfsLoadFile(filename, (uint8_t*)&DrvDemoSettings, sizeof(DrvDemoSettings))) { + if (DrvDemoSettings.version != DRV_DEMO_VERSION) { // Fix version dependent changes - DrvDemoSettings.crc32 = DrvDemoSettingsCrc32(); + // *** Start fix possible setting deltas *** + if (Settings->version < 0x01010100) { + AddLog(LOG_LEVEL_INFO, PSTR("CFG: Update oldest version restore")); + + } + if (Settings->version < 0x01010101) { + AddLog(LOG_LEVEL_INFO, PSTR("CFG: Update old version restore")); + + } + + // *** End setting deltas *** + + // Set current version and save settings + DrvDemoSettings.version = DRV_DEMO_VERSION; + DrvDemoSettingsSave(); + } + AddLog(LOG_LEVEL_INFO, PSTR("CFG: Demo loaded from file")); + } + else { + // File system not ready: No flash space reserved for file system + AddLog(LOG_LEVEL_INFO, PSTR("CFG: Demo use defaults as file system not ready or file not found")); + } +#endif // USE_UFILESYS } void DrvDemoSettingsSave(void) { // Called from FUNC_SAVE_SETTINGS every SaveData second and at restart - - if (DrvDemoSettingsCrc32() != DrvDemoSettings.crc32) { +#ifdef USE_UFILESYS + uint32_t crc32 = GetCfgCrc32((uint8_t*)&DrvDemoSettings +4, sizeof(DrvDemoSettings) -4); // Skip crc32 + if (crc32 != DrvDemoSettings.crc32) { // Try to save file /.drvset122 - DrvDemoSettings.crc32 = DrvDemoSettingsCrc32(); + DrvDemoSettings.crc32 = crc32; char filename[20]; // Use for sensors: // snprintf_P(filename, sizeof(filename), PSTR(TASM_FILE_SENSOR), XSNS_122); // Use for drivers: snprintf_P(filename, sizeof(filename), PSTR(TASM_FILE_DRIVER), XDRV_122); - - AddLog(LOG_LEVEL_INFO, PSTR("DRV: About to save settings to file %s"), filename); - -#ifdef USE_UFILESYS - if (!TfsSaveFile(filename, (const uint8_t*)&DrvDemoSettings, sizeof(DrvDemoSettings))) { + if (TfsSaveFile(filename, (const uint8_t*)&DrvDemoSettings, sizeof(DrvDemoSettings))) { + AddLog(LOG_LEVEL_DEBUG, PSTR("CFG: Demo saved to file")); + } else { // File system not ready: No flash space reserved for file system - AddLog(LOG_LEVEL_INFO, PSTR("DRV: ERROR File system not ready or unable to save file")); + AddLog(LOG_LEVEL_DEBUG, PSTR("CFG: ERROR Demo file system not ready or unable to save file")); } -#else - AddLog(LOG_LEVEL_INFO, PSTR("DRV: ERROR File system not enabled")); -#endif // USE_UFILESYS } +#endif // USE_UFILESYS } /*********************************************************************************************\ @@ -178,6 +163,9 @@ bool Xdrv122(uint32_t function) { bool result = false; switch (function) { + case FUNC_RESET_SETTINGS: + DrvDemoSettingsLoad(1); + break; case FUNC_SAVE_SETTINGS: DrvDemoSettingsSave(); break; @@ -185,7 +173,7 @@ bool Xdrv122(uint32_t function) { result = DecodeCommand(kDrvDemoCommands, DrvDemoCommand); break; case FUNC_PRE_INIT: - DrvDemoSettingsLoad(); + DrvDemoSettingsLoad(0); break; case FUNC_SAVE_BEFORE_RESTART: // !!! DO NOT USE AS IT'S FUNCTION IS BETTER HANDLED BY FUNC_SAVE_SETTINGS !!! diff --git a/tasmota/tasmota_xdrv_driver/xdrv_86_esp32_sonoff_spm.ino b/tasmota/tasmota_xdrv_driver/xdrv_86_esp32_sonoff_spm.ino index 3a222f91f..66dc60856 100644 --- a/tasmota/tasmota_xdrv_driver/xdrv_86_esp32_sonoff_spm.ino +++ b/tasmota/tasmota_xdrv_driver/xdrv_86_esp32_sonoff_spm.ino @@ -310,7 +310,7 @@ TSspm *Sspm = nullptr; const uint32_t XDRV_86_VERSION = 0x0104; // Latest driver version (See settings deltas below) -void Xdrv86SettingsLoad(void) { +void Xdrv86SettingsLoad(bool erase) { // *** Start init default values in case file is not found *** memset(&Sspm->Settings, 0x00, sizeof(tSspmSettings)); Sspm->Settings.version = XDRV_86_VERSION; @@ -326,7 +326,10 @@ void Xdrv86SettingsLoad(void) { char filename[20]; // Use for drivers: snprintf_P(filename, sizeof(filename), PSTR(TASM_FILE_DRIVER), XDRV_86); - if (TfsLoadFile(filename, (uint8_t*)&Sspm->Settings, sizeof(tSspmSettings))) { + if (erase) { + TfsDeleteFile(filename); // Use defaults + } + else if (TfsLoadFile(filename, (uint8_t*)&Sspm->Settings, sizeof(tSspmSettings))) { if (Sspm->Settings.version != XDRV_86_VERSION) { // Fix version dependent changes // *** Start fix possible setting deltas *** @@ -341,7 +344,8 @@ void Xdrv86SettingsLoad(void) { Xdrv86SettingsSave(); } AddLog(LOG_LEVEL_INFO, PSTR("CFG: XDRV86 loaded from file")); - } else { + } + else { // File system not ready: No flash space reserved for file system AddLog(LOG_LEVEL_DEBUG, PSTR("CFG: XDRV86 Use defaults as file system not ready or file not found")); } @@ -1907,7 +1911,7 @@ void SSPMInit(void) { return; } - Xdrv86SettingsLoad(); + Xdrv86SettingsLoad(0); pinMode(SSPM_GPIO_ARM_RESET, OUTPUT); digitalWrite(SSPM_GPIO_ARM_RESET, 1); @@ -2645,6 +2649,9 @@ bool Xdrv86(uint32_t function) { case FUNC_EVERY_100_MSECOND: SSPMEvery100ms(); break; + case FUNC_RESET_SETTINGS: + Xdrv86SettingsLoad(1); + break; case FUNC_SAVE_SETTINGS: Xdrv86SettingsSave(); break; diff --git a/tasmota/tasmota_xdrv_driver/xdrv_87_esp32_sonoff_tm1621.ino b/tasmota/tasmota_xdrv_driver/xdrv_87_esp32_sonoff_tm1621.ino index 51fd818ad..090dfaa90 100644 --- a/tasmota/tasmota_xdrv_driver/xdrv_87_esp32_sonoff_tm1621.ino +++ b/tasmota/tasmota_xdrv_driver/xdrv_87_esp32_sonoff_tm1621.ino @@ -99,7 +99,7 @@ tXdrv87Settings Xdrv87Settings; /*********************************************************************************************/ -void Xdrv87SettingsLoad(void) { +void Xdrv87SettingsLoad(bool erase) { // *** Start init default values in case file is not found *** memset(&Xdrv87Settings, 0x00, sizeof(tXdrv87Settings)); Xdrv87Settings.version = XDRV_87_VERSION; @@ -115,7 +115,10 @@ void Xdrv87SettingsLoad(void) { char filename[20]; // Use for drivers: snprintf_P(filename, sizeof(filename), PSTR(TASM_FILE_DRIVER), XDRV_87); - if (TfsLoadFile(filename, (uint8_t*)&Xdrv87Settings, sizeof(tXdrv87Settings))) { + if (erase) { + TfsDeleteFile(filename); // Use defaults + } + else if (TfsLoadFile(filename, (uint8_t*)&Xdrv87Settings, sizeof(tXdrv87Settings))) { if (Xdrv87Settings.version != XDRV_87_VERSION) { // Fix version dependent changes // *** Start fix possible setting deltas *** @@ -130,7 +133,8 @@ void Xdrv87SettingsLoad(void) { Xdrv87SettingsSave(); } AddLog(LOG_LEVEL_INFO, PSTR("CFG: XDRV87 loaded from file")); - } else { + } + else { // File system not ready: No flash space reserved for file system AddLog(LOG_LEVEL_DEBUG, PSTR("CFG: XDRV87 Use defaults as file system not ready or file not found")); } @@ -298,7 +302,7 @@ void TM1621PreInit(void) { pinMode(Tm1621.pin_wr, OUTPUT); digitalWrite(Tm1621.pin_wr, 1); - Xdrv87SettingsLoad(); + Xdrv87SettingsLoad(0); Tm1621.state = 200; @@ -578,6 +582,9 @@ bool Xdrv87(uint32_t function) { case FUNC_EVERY_SECOND: TM1621EverySecond(); break; + case FUNC_RESET_SETTINGS: + Xdrv87SettingsLoad(1); + break; case FUNC_SAVE_SETTINGS: Xdrv87SettingsSave(); break;