From 659103271a7b3c7298c861cb7871701d6cee8604 Mon Sep 17 00:00:00 2001 From: philmoz Date: Thu, 20 Feb 2025 11:10:53 +1100 Subject: [PATCH] Limit the number of consecutive retries when writing SD card data. --- radio/src/storage/sdcard_common.cpp | 70 ++++++++++++++++++----------- 1 file changed, 45 insertions(+), 25 deletions(-) diff --git a/radio/src/storage/sdcard_common.cpp b/radio/src/storage/sdcard_common.cpp index 99b344c037e..41c90cb236c 100644 --- a/radio/src/storage/sdcard_common.cpp +++ b/radio/src/storage/sdcard_common.cpp @@ -80,41 +80,61 @@ void storageCheck(bool immediately) // Don't write anything to SD card if in EM if (UNEXPECTED_SHUTDOWN()) return; - if (storageDirtyMsk & EE_GENERAL) { - TRACE("eeprom write general"); - const char * error = writeGeneralSettings(); - if (error) { - TRACE("writeGeneralSettings error=%s", error); - } - else { - storageDirtyMsk &= ~EE_GENERAL; + static constexpr uint8_t retryLimit = 10; + + static uint8_t retryRadioCount = 0; + // TODO: provide some mechanism to alert user that SD card has serious error + if (retryRadioCount < retryLimit) { + if (storageDirtyMsk & EE_GENERAL) { + TRACE("SD card write radio settings"); + const char * error = writeGeneralSettings(); + if (error) { + TRACE("writeGeneralSettings error=%s", error); + retryRadioCount += 1; + } + else { + storageDirtyMsk &= ~EE_GENERAL; + retryRadioCount = 0; + } } } #if defined(STORAGE_MODELSLIST) - if (storageDirtyMsk & EE_LABELS) { - TRACE("SD card write labels"); - const char * error = modelslist.save(); - if (error) { - TRACE("writeLabels error=%s", error); - } - else { - storageDirtyMsk &= ~EE_LABELS; + static uint8_t retryLabelsCount = 0; + // TODO: provide some mechanism to alert user that SD card has serious error + if (retryLabelsCount < retryLimit) { + if (storageDirtyMsk & EE_LABELS) { + TRACE("SD card write labels"); + const char * error = modelslist.save(); + if (error) { + TRACE("writeLabels error=%s", error); + retryLabelsCount += 1; + } + else { + storageDirtyMsk &= ~EE_LABELS; + retryLabelsCount = 0; + } } } #endif - if (storageDirtyMsk & EE_MODEL) { - TRACE("eeprom write model"); - const char * error = writeModel(); + static uint8_t retryModelCount = 0; + // TODO: provide some mechanism to alert user that SD card has serious error + if (retryModelCount < retryLimit) { + if (storageDirtyMsk & EE_MODEL) { + TRACE("SD card write model settings"); + const char * error = writeModel(); #if defined(STORAGE_MODELSLIST) - modelslist.updateCurrentModelCell(); + modelslist.updateCurrentModelCell(); #endif - if (error) { - TRACE("writeModel error=%s", error); - } - else { - storageDirtyMsk &= ~EE_MODEL; + if (error) { + TRACE("writeModel error=%s", error); + retryModelCount += 1; + } + else { + storageDirtyMsk &= ~EE_MODEL; + retryModelCount = 0; + } } } }