Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Prevent functional degradation if SD has less than 50MB free space (#3730) #3802

Merged
merged 4 commits into from
Jul 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions radio/src/gui/colorlcd/popups.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
* GNU General Public License for more details.
*/

#include "opentx.h"
#include "popups.h"
#include "libopenui.h"
#include "pwr.h"
Expand All @@ -30,10 +31,12 @@ static void _run_popup_dialog(const char* title, const char* msg,
{
bool running = true;

resetBacklightTimeout();

// reset input devices to avoid
// RELEASED/CLICKED to be called in a loop
lv_indev_reset(nullptr, nullptr);

auto md = new MessageDialog(MainWindow::instance(), title, msg);
md->setCloseHandler([&]() { running = false; });
if (info) {
Expand All @@ -53,6 +56,8 @@ static void _run_popup_dialog(const char* title, const char* msg,
RTOS_WAIT_MS(1);
continue;
}

checkBacklight();
WDG_RESET();
MainWindow::instance()->run();
LvglWrapper::runNested();
Expand Down Expand Up @@ -84,8 +89,12 @@ void show_ui_popup()
}
}

void POPUP_WARNING_ON_UI_TASK(const char * message, const char * info)
void POPUP_WARNING_ON_UI_TASK(const char * message, const char * info, bool waitForClose)
{
// if already in a popup, and we don't want to wait, ignore call
if (!waitForClose && ui_popup_active)
return;

// Wait in case already in popup.
while (ui_popup_active) {
RTOS_WAIT_MS(20);
Expand All @@ -94,8 +103,11 @@ void POPUP_WARNING_ON_UI_TASK(const char * message, const char * info)
ui_popup_msg = message;
ui_popup_info = info;
ui_popup_active = true;

// Wait until closed
while (ui_popup_active) {
RTOS_WAIT_MS(20);
if (waitForClose) {
while (ui_popup_active) {
RTOS_WAIT_MS(20);
}
}
}
2 changes: 1 addition & 1 deletion radio/src/gui/colorlcd/popups.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ typedef std::function<void(const char *, const char *, int, int)> ProgressHandle

void POPUP_INFORMATION(const char * message);
void POPUP_WARNING(const char * message, const char * info = nullptr);
void POPUP_WARNING_ON_UI_TASK(const char * message, const char * info = nullptr);
void POPUP_WARNING_ON_UI_TASK(const char * message, const char * info = nullptr, bool waitForClose = true);

void show_ui_popup();
6 changes: 3 additions & 3 deletions radio/src/gui/common/stdlcd/popups.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,15 +176,15 @@ void runPopupWarning(event_t event)
return;

case WARNING_TYPE_INFO:
lcdDrawText(WARNING_LINE_X, WARNING_LINE_Y+2*FH+2, STR_OK);
lcdDrawText(WARNING_LINE_X, WARNING_LINE_Y+4*FH+2, STR_OK);
break;

case WARNING_TYPE_ASTERISK:
lcdDrawText(WARNING_LINE_X, WARNING_LINE_Y+2*FH+2, BUTTON(TR_EXIT));
lcdDrawText(WARNING_LINE_X, WARNING_LINE_Y+4*FH+2, BUTTON(TR_EXIT));
break;

default:
lcdDrawText(WARNING_LINE_X, WARNING_LINE_Y+2*FH+2, STR_POPUPS_ENTER_EXIT);
lcdDrawText(WARNING_LINE_X, WARNING_LINE_Y+4*FH+2, STR_POPUPS_ENTER_EXIT);
break;
}

Expand Down
6 changes: 4 additions & 2 deletions radio/src/gui/common/stdlcd/popups.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ enum
#if !defined(GUI)
#define DISPLAY_WARNING(...)
inline void POPUP_WAIT(const char * s) { }
inline void POPUP_WARNING(const char *, const char * = nullptr) { }
inline void POPUP_WARNING(const char *, const char * = nullptr, bool waitForClose) { }
inline void POPUP_CONFIRMATION(const char * s, PopupMenuHandler handler) { }
inline void POPUP_INPUT(const char * s, PopupFunc func) { }
inline void SET_WARNING_INFO(const char * info, uint8_t length, uint8_t flags) { }
Expand Down Expand Up @@ -105,8 +105,10 @@ enum
popupFunc = runPopupWarning;
}

inline void POPUP_WARNING(const char * message, const char * info = nullptr)
inline void POPUP_WARNING(const char * message, const char * info = nullptr, bool waitForClose = true)
{
(void)waitForClose;

warningText = message;
warningInfoText = info;
warningInfoLength = info ? strlen(info) : 0;
Expand Down
5 changes: 5 additions & 0 deletions radio/src/gui/screenshot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ const char * writeScreenshot()
UINT written;
char filename[42]; // /SCREENSHOTS/screen-2013-01-01-123540.bmp

if (IS_SDCARD_FULL()) {
POPUP_WARNING(STR_SDCARD_FULL_EXT);
return STR_SDCARD_FULL_EXT;
}

// check and create folder here
strcpy(filename, SCREENSHOTS_PATH);
const char * error = sdCheckAndCreateDirectory(filename);
Expand Down
37 changes: 24 additions & 13 deletions radio/src/logs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,6 @@ const char * logsOpen()
if (!sdMounted())
return STR_NO_SDCARD;

if (sdGetFreeSectors() == 0)
return STR_SDCARD_FULL;

// check and create folder here
strcpy(filename, STR_LOGS_PATH);
const char * error = sdCheckAndCreateDirectory(filename);
Expand Down Expand Up @@ -184,16 +181,14 @@ const char * logsOpen()

void logsClose()
{
if (sdMounted()) {
if (g_oLogFile.obj.fs && sdMounted()) {
if (f_close(&g_oLogFile) != FR_OK) {
// close failed, forget file
g_oLogFile.obj.fs = 0;
}
lastLogTime = 0;
}
#if !defined(SIMU)
loggingTimerStop();
#endif

}

void writeHeader()
Expand Down Expand Up @@ -285,17 +280,31 @@ void logsWrite()
{
#endif

bool sdCardFull = IS_SDCARD_FULL();

// check if file needs to be opened
if (!g_oLogFile.obj.fs) {
const char * result = logsOpen();
const char *result = sdCardFull ? STR_SDCARD_FULL_EXT : logsOpen();

// SD card is full or file open failed
if (result) {
if (result != error_displayed) {
error_displayed = result;
POPUP_WARNING(result);
POPUP_WARNING_ON_UI_TASK(result, nullptr, false);
}
return;
}
}

// check at every write cycle
if (sdCardFull) {
logsClose(); // timer is still running and code above will try to
// open the file again but will fail with error
// which will trigger the warning popup
return;
}


#if defined(RTCLOCK)
{
static struct gtm utm;
Expand Down Expand Up @@ -383,15 +392,17 @@ void logsWrite()

if (result<0 && !error_displayed) {
error_displayed = STR_SDCARD_ERROR;
POPUP_WARNING(STR_SDCARD_ERROR);
POPUP_WARNING_ON_UI_TASK(STR_SDCARD_ERROR, nullptr, false);
logsClose();
}
}
}
else {
error_displayed = nullptr;
if (g_oLogFile.obj.fs) {
logsClose();
}
logsClose();

#if !defined(SIMU)
loggingTimerStop();
#endif
}
}
7 changes: 7 additions & 0 deletions radio/src/opentx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,11 @@ static void checkRTCBattery()
}
}

void checkSDfreeStorage() {
if(IS_SDCARD_FULL())
ALERT(STR_SD_CARD, STR_SDCARD_FULL, AU_ERROR);
}

#if defined(PCBFRSKY) || defined(PCBFLYSKY)
static void checkFailsafe()
{
Expand Down Expand Up @@ -672,6 +677,8 @@ void checkAll()
checkLowEEPROM();
#endif

checkSDfreeStorage();

// we don't check the throttle stick if the radio is not calibrated
if (g_eeGeneral.chkSum == evalChkSum()) {
checkThrottleStick();
Expand Down
4 changes: 4 additions & 0 deletions radio/src/opentx.h
Original file line number Diff line number Diff line change
Expand Up @@ -1056,6 +1056,10 @@ inline bool IS_TXBATT_WARNING()
return g_vbat100mV <= g_eeGeneral.vBatWarn;
}

inline bool IS_SDCARD_FULL() {
return sdGetFreeSectors() < ((50 *1024*1024) / BLOCK_SIZE); // 50MB safety margin
}

enum TelemetryViews {
TELEMETRY_CUSTOM_SCREEN_1,
TELEMETRY_CUSTOM_SCREEN_2,
Expand Down
4 changes: 3 additions & 1 deletion radio/src/sdcard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
#include "libopenui/src/libopenui_file.h"
#endif

#define SDCARD_MIN_FREE_SPACE_MB 50 // Maintain a 50MB free space buffer to prevent crashes

const char * sdCheckAndCreateDirectory(const char * path)
{
DIR archiveFolder;
Expand Down Expand Up @@ -463,7 +465,7 @@ uint32_t sdGetSize()

uint32_t sdGetFreeSectors()
{
return 10;
return ((SDCARD_MIN_FREE_SPACE_MB*1024*1024)/BLOCK_SIZE)+1; // SIMU SD card is always above threshold
}

#endif // #if !defined(SIMU) || defined(SIMU_DISKIO)
1 change: 1 addition & 0 deletions radio/src/translations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,7 @@ const char STR_SDCARD_ERROR[] = TR_SDCARD_ERROR;
#define STR_CLEAR TR_CLEAR
const char STR_NO_SDCARD[] = TR_NO_SDCARD;
const char STR_SDCARD_FULL[] = TR_SDCARD_FULL;
const char STR_SDCARD_FULL_EXT[] = TR_SDCARD_FULL_EXT;
const char STR_INCOMPATIBLE[] = TR_INCOMPATIBLE;
const char STR_LOGS_PATH[] = LOGS_PATH;
const char STR_LOGS_EXT[] = LOGS_EXT;
Expand Down
1 change: 1 addition & 0 deletions radio/src/translations.h
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,7 @@ extern const char STR_DELETE_ERROR[];
extern const char STR_SDCARD_ERROR[];
extern const char STR_NO_SDCARD[];
extern const char STR_SDCARD_FULL[];
extern const char STR_SDCARD_FULL_EXT[];
extern const char STR_INCOMPATIBLE[];
extern const char STR_LOGS_PATH[];
extern const char STR_LOGS_EXT[];
Expand Down
7 changes: 6 additions & 1 deletion radio/src/translations/cn.h
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,12 @@
#define TR_POWER_METER_INT "功率计 (内置)"
#define TR_SPECTRUM_ANALYSER_EXT "频谱仪 (外置)"
#define TR_SPECTRUM_ANALYSER_INT "频谱仪 (内置)"
#define TR_SDCARD_FULL "存储卡已满"
#define TR_SDCARD_FULL "SD卡已满"
#if defined(COLORLCD)
#define TR_SDCARD_FULL_EXT TR_SDCARD_FULL "\n日志和截屏功能将被禁用"
#else
#define TR_SDCARD_FULL_EXT TR_SDCARD_FULL "\036日志和 " LCDW_128_480_LINEBREAK "截屏功能将被禁用"
#endif
#define TR_NEEDS_FILE "需要文件名包含"
#define TR_EXT_MULTI_SPEC "opentx-inv"
#define TR_INT_MULTI_SPEC "stm-opentx-noinv"
Expand Down
7 changes: 6 additions & 1 deletion radio/src/translations/cz.h
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,12 @@
#define TR_POWER_METER_INT "Měřič výkonu (INT)"
#define TR_SPECTRUM_ANALYSER_EXT "Spektální an. (EXT)"
#define TR_SPECTRUM_ANALYSER_INT "Spektální an. (INT)"
#define TR_SDCARD_FULL "Plná SD karta"
#define TR_SDCARD_FULL "Plná karta SD"
#if defined(COLORLCD)
#define TR_SDCARD_FULL_EXT TR_SDCARD_FULL "\nLogování dat a snímky obrazovky vypnuty"
#else
#define TR_SDCARD_FULL_EXT TR_SDCARD_FULL "\036Logy a " LCDW_128_480_LINEBREAK " Snímky obrazovky vypnuty"
#endif
#define TR_NEEDS_FILE "Vyžadován soubor"
#define TR_EXT_MULTI_SPEC "opentx-inv"
#define TR_INT_MULTI_SPEC "stm-opentx-noinv"
Expand Down
5 changes: 5 additions & 0 deletions radio/src/translations/da.h
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,11 @@
#define TR_SPECTRUM_ANALYSER_EXT "Spectrum (EXT)"
#define TR_SPECTRUM_ANALYSER_INT "Spectrum (INT)"
#define TR_SDCARD_FULL "SD kort fuldt"
#if defined(COLORLCD)
#define TR_SDCARD_FULL_EXT TR_SDCARD_FULL "\nLog & skærmklip deaktiveret"
#else
#define TR_SDCARD_FULL_EXT TR_SDCARD_FULL "\036Log & skærmklip" LCDW_128_480_LINEBREAK "deaktiveret"
#endif
#define TR_NEEDS_FILE "MANGLER FIL"
#define TR_EXT_MULTI_SPEC "opentx-inv"
#define TR_INT_MULTI_SPEC "stm-opentx-noinv"
Expand Down
7 changes: 6 additions & 1 deletion radio/src/translations/de.h
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,12 @@
#define TR_POWER_METER_INT "Power Meter (INT)"
#define TR_SPECTRUM_ANALYSER_EXT "Spectrum (EXT)"
#define TR_SPECTRUM_ANALYSER_INT "Spectrum (INT)"
#define TR_SDCARD_FULL "SD-Karte voll"
#define TR_SDCARD_FULL "SD-Karte voll"
#if defined(COLORLCD)
#define TR_SDCARD_FULL_EXT TR_SDCARD_FULL "\nLogs und Screenshots deaktiviert"
#else
#define TR_SDCARD_FULL_EXT TR_SDCARD_FULL "\036Logs und " LCDW_128_480_LINEBREAK "Screenshots deaktiviert"
#endif
#define TR_NEEDS_FILE "Datei benötigt"
#define TR_EXT_MULTI_SPEC "opentx-inv"
#define TR_INT_MULTI_SPEC "stm-opentx-noinv"
Expand Down
5 changes: 5 additions & 0 deletions radio/src/translations/en.h
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,11 @@
#define TR_SPECTRUM_ANALYSER_EXT "Spectrum (EXT)"
#define TR_SPECTRUM_ANALYSER_INT "Spectrum (INT)"
#define TR_SDCARD_FULL "SD card full"
#if defined(COLORLCD)
#define TR_SDCARD_FULL_EXT TR_SDCARD_FULL "\nLogs and Screenshots disabled"
#else
#define TR_SDCARD_FULL_EXT TR_SDCARD_FULL "\036Logs & Screenshots" LCDW_128_480_LINEBREAK "disabled"
#endif
#define TR_NEEDS_FILE "NEEDS FILE"
#define TR_EXT_MULTI_SPEC "opentx-inv"
#define TR_INT_MULTI_SPEC "stm-opentx-noinv"
Expand Down
5 changes: 5 additions & 0 deletions radio/src/translations/es.h
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,11 @@
#define TR_SPECTRUM_ANALYSER_EXT "Espectro (EXT)"
#define TR_SPECTRUM_ANALYSER_INT "Espectro (INT)"
#define TR_SDCARD_FULL "SD Card llena"
#if defined(COLORLCD)
#define TR_SDCARD_FULL_EXT TR_SDCARD_FULL "\nLogs and Screenshots disabled"
#else
#define TR_SDCARD_FULL_EXT TR_SDCARD_FULL "\036Logs and " LCDW_128_480_LINEBREAK "Screenshots disabled"
#endif
#define TR_NEEDS_FILE "NECESITA ARCHIVO"
#define TR_EXT_MULTI_SPEC "opentx-inv"
#define TR_INT_MULTI_SPEC "stm-opentx-noinv"
Expand Down
5 changes: 5 additions & 0 deletions radio/src/translations/fi.h
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,11 @@
#define TR_SPECTRUM_ANALYSER_EXT "Spectrum (EXT)"
#define TR_SPECTRUM_ANALYSER_INT "Spectrum (INT)"
#define TR_SDCARD_FULL "SD Card Full"
#if defined(COLORLCD)
#define TR_SDCARD_FULL_EXT TR_SDCARD_FULL "\nLogs and Screenshots disabled"
#else
#define TR_SDCARD_FULL_EXT TR_SDCARD_FULL "\036Logs and " LCDW_128_480_LINEBREAK "Screenshots disabled"
#endif
#define TR_NEEDS_FILE "NEEDS FILE"
#define TR_EXT_MULTI_SPEC "opentx-inv"
#define TR_INT_MULTI_SPEC "stm-opentx-noinv"
Expand Down
7 changes: 6 additions & 1 deletion radio/src/translations/fr.h
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,12 @@
#define TR_POWER_METER_INT "Puissancemètre (INT)"
#define TR_SPECTRUM_ANALYSER_EXT TR("Spectre (EXT)", "Analyseur Spectre (EXT)")
#define TR_SPECTRUM_ANALYSER_INT TR("Spectre (INT)", "Analyseur Spectre (INT)")
#define TR_SDCARD_FULL "Carte SD pleine"
#define TR_SDCARD_FULL "SD carte pleine"
#if defined(COLORLCD)
#define TR_SDCARD_FULL_EXT TR_SDCARD_FULL "\nJournaux et Impr. écran désactivé"
#else
#define TR_SDCARD_FULL_EXT TR_SDCARD_FULL "\036Journaux et" LCDW_128_480_LINEBREAK "Impr. écran désactivé"
#endif
#define TR_NEEDS_FILE "FICHIER EXIGE"
#define TR_EXT_MULTI_SPEC "opentx-inv"
#define TR_INT_MULTI_SPEC "stm-opentx-noinv"
Expand Down
7 changes: 6 additions & 1 deletion radio/src/translations/he.h
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,12 @@
#define TR_POWER_METER_INT "Power Meter (INT)"
#define TR_SPECTRUM_ANALYSER_EXT "Spectrum (EXT)"
#define TR_SPECTRUM_ANALYSER_INT "Spectrum (INT)"
#define TR_SDCARD_FULL "SD card full"
#define TR_SDCARD_FULL "הדיסק מלא״
#if defined(COLORLCD)
#define TR_SDCARD_FULL_EXT TR_SDCARD_FULL "\לוגים ושמירת צילומי מסך מושבתים"
#else
#define TR_SDCARD_FULL_EXT TR_SDCARD_FULL "\036לוגים" LCDW_128_480_LINEBREAK "ושמירת צילומי מסך מושבתים"
#endif
#define TR_NEEDS_FILE "NEEDS FILE"
#define TR_EXT_MULTI_SPEC "opentx-inv"
#define TR_INT_MULTI_SPEC "stm-opentx-noinv"
Expand Down
Loading