From 03f201b40683435bb7da2ce218ac7e4260dab9e2 Mon Sep 17 00:00:00 2001 From: Torsten Stauder Date: Sat, 3 Jul 2021 14:28:26 +0200 Subject: [PATCH] Fix to break bootloops when PLAY_LAST_RFID_AFTER_REBOOT is active --- src/Log.h | 2 +- src/LogMessages_DE.cpp | 2 ++ src/LogMessages_EN.cpp | 2 ++ src/logmessages.h | 2 ++ src/main.cpp | 40 ++++++++++++++++++++++++++++++++++++++-- 5 files changed, 45 insertions(+), 3 deletions(-) diff --git a/src/Log.h b/src/Log.h index c9f39ea..97cd381 100644 --- a/src/Log.h +++ b/src/Log.h @@ -1,5 +1,5 @@ #pragma once -#include "LogMessages.h" +#include "logmessages.h" // Loglevels available (don't change!) #define LOGLEVEL_ERROR 1 // only errors diff --git a/src/LogMessages_DE.cpp b/src/LogMessages_DE.cpp index bcf9193..7635134 100644 --- a/src/LogMessages_DE.cpp +++ b/src/LogMessages_DE.cpp @@ -192,5 +192,7 @@ const char playlistGenModeUncached[] PROGMEM = "Playlist-Generierung: uncached"; const char playlistGenModeCached[] PROGMEM = "Playlist-Generierung: cached"; const char playlistCacheFoundBut0[] PROGMEM = "Playlist-Cache-File gefunden, jedoch 0 Bytes groß"; + const char bootLoopDetected[] PROGMEM = "Bootschleife erkannt! Letzte RFID wird nicht aufgerufen."; + const char noBootLoopDetected[] PROGMEM = "Keine Bootschleife erkannt. Wunderbar :-)"; #endif diff --git a/src/LogMessages_EN.cpp b/src/LogMessages_EN.cpp index 9d3c19e..2a76445 100644 --- a/src/LogMessages_EN.cpp +++ b/src/LogMessages_EN.cpp @@ -192,5 +192,7 @@ const char playlistGenModeUncached[] PROGMEM = "Playlist-generation: uncached"; const char playlistGenModeCached[] PROGMEM = "Playlist-generation: cached"; const char playlistCacheFoundBut0[] PROGMEM = "Playlist-cache-file found but 0 bytes"; + const char bootLoopDetected[] PROGMEM = "Bootloop detected! Last RFID won't be restored."; + const char noBootLoopDetected[] PROGMEM = "No bootloop detected. Great :-)"; #endif diff --git a/src/logmessages.h b/src/logmessages.h index 711e09a..49cffcc 100644 --- a/src/logmessages.h +++ b/src/logmessages.h @@ -188,3 +188,5 @@ extern const char warningRefactoring[]; extern const char playlistGenModeUncached[]; extern const char playlistGenModeCached[]; extern const char playlistCacheFoundBut0[]; +extern const char bootLoopDetected[]; +extern const char noBootLoopDetected[]; diff --git a/src/main.cpp b/src/main.cpp index b6cab6e..32fa464 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -26,6 +26,9 @@ #ifdef PLAY_LAST_RFID_AFTER_REBOOT bool recoverLastRfid = true; + bool recoverBootCount = true; + bool resetBootCount = false; + uint32_t bootCount = 0; #endif //////////// @@ -42,8 +45,40 @@ #endif #ifdef PLAY_LAST_RFID_AFTER_REBOOT + // If a problem occurs, remembering last rfid can lead into a boot loop that's hard to escape of. + // That reason for a mechanism is necessary to prevent this. + // At start of a boot, bootCount is incremented by one and after 30s decremented because + // uptime of 30s is considered as "successful boot". + void recoverBootCountFromNvs(void) { + if (recoverBootCount) { + recoverBootCount = false; + resetBootCount = true; + bootCount = gPrefsSettings.getUInt("bootCount", 999); + + if (bootCount == 999) { // first init + bootCount = 1; + gPrefsSettings.putUInt("bootCount", bootCount); + } else if (bootCount >= 3) { // considered being a bootloop => don't recover last rfid! + bootCount = 1; + gPrefsSettings.putUInt("bootCount", bootCount); + gPrefsSettings.putString("lastRfid", "-1"); // reset last rfid + Log_Println((char *) FPSTR(bootLoopDetected), LOGLEVEL_ERROR); + recoverLastRfid = false; + } else { // normal operation + gPrefsSettings.putUInt("bootCount", ++bootCount); + } + } + + if (resetBootCount && millis() >= 30000) { // reset bootcount + resetBootCount = false; + bootCount = 0; + gPrefsSettings.putUInt("bootCount", bootCount); + Log_Println((char *) FPSTR(noBootLoopDetected), LOGLEVEL_INFO); + } + } + // Get last RFID-tag applied from NVS - void recoverLastRfidPlayed(void) { + void recoverLastRfidPlayedFromNvs(void) { if (recoverLastRfid) { if (System_GetOperationMode() == OPMODE_BLUETOOTH) { // Don't recover if BT-mode is desired recoverLastRfid = false; @@ -220,7 +255,8 @@ void loop() { Rfid_PreferenceLookupHandler(); #ifdef PLAY_LAST_RFID_AFTER_REBOOT - recoverLastRfidPlayed(); + recoverBootCountFromNvs(); + recoverLastRfidPlayedFromNvs(); #endif IrReceiver_Cyclic();