From 49f81af5275f36f209f2299447e3a167bca11cc9 Mon Sep 17 00:00:00 2001 From: Bart Slinger Date: Fri, 12 Jul 2024 21:35:45 +0800 Subject: [PATCH 1/3] Add blackbox device FILE for SITL build --- src/main/blackbox/blackbox.c | 3 ++ src/main/blackbox/blackbox_io.c | 70 +++++++++++++++++++++++++++++++++ src/main/blackbox/blackbox_io.h | 3 ++ src/main/fc/settings.yaml | 2 +- 4 files changed, 77 insertions(+), 1 deletion(-) diff --git a/src/main/blackbox/blackbox.c b/src/main/blackbox/blackbox.c index 419b7b4add4..324b41a1095 100644 --- a/src/main/blackbox/blackbox.c +++ b/src/main/blackbox/blackbox.c @@ -1442,6 +1442,9 @@ static void blackboxValidateConfig(void) #endif #ifdef USE_SDCARD case BLACKBOX_DEVICE_SDCARD: +#endif +#if defined(SITL_BUILD) + case BLACKBOX_DEVICE_FILE: #endif case BLACKBOX_DEVICE_SERIAL: // Device supported, leave the setting alone diff --git a/src/main/blackbox/blackbox_io.c b/src/main/blackbox/blackbox_io.c index 3e6adb719b7..dfa79b16eda 100644 --- a/src/main/blackbox/blackbox_io.c +++ b/src/main/blackbox/blackbox_io.c @@ -19,6 +19,11 @@ #include #include +#if defined(SITL_BUILD) +#include +#include +#endif + #include "platform.h" #ifdef USE_BLACKBOX @@ -81,6 +86,12 @@ static struct { #endif +#if defined(SITL_BUILD) +static struct { + FILE *file_handler; +} blackboxFile; +#endif + #ifndef UNIT_TEST void blackboxOpen(void) { @@ -103,6 +114,11 @@ void blackboxWrite(uint8_t value) case BLACKBOX_DEVICE_SDCARD: afatfs_fputc(blackboxSDCard.logFile, value); break; +#endif +#if defined(SITL_BUILD) + case BLACKBOX_DEVICE_FILE: + fputc(value, blackboxFile.file_handler); + break; #endif case BLACKBOX_DEVICE_SERIAL: default: @@ -133,6 +149,13 @@ int blackboxPrint(const char *s) break; #endif +#if defined(SITL_BUILD) + case BLACKBOX_DEVICE_FILE: + length = strlen(s); + fputs(s, blackboxFile.file_handler); + break; +#endif + case BLACKBOX_DEVICE_SERIAL: default: pos = (uint8_t*) s; @@ -196,6 +219,12 @@ bool blackboxDeviceFlushForce(void) return afatfs_flush(); #endif +#if defined(SITL_BUILD) + case BLACKBOX_DEVICE_FILE: + fflush(blackboxFile.file_handler); + return true; +#endif + default: return false; } @@ -271,6 +300,26 @@ bool blackboxDeviceOpen(void) return true; break; #endif +#if defined(SITL_BUILD) + case BLACKBOX_DEVICE_FILE: + { + const time_t now = time(NULL); + const struct tm *t = localtime(&now); + char filename[32]; + strftime(filename, sizeof(filename), "%Y_%m_%d_%H%M%S.TXT", t); + + blackboxFile.file_handler = fopen(filename, "wb"); + if (blackboxFile.file_handler == NULL) { + fprintf(stderr, "[BlackBox] Failed to create log file\n"); + return false; + } + fprintf(stderr, "[BlackBox] Created %s\n", filename); + } + + blackboxMaxHeaderBytesPerIteration = BLACKBOX_TARGET_HEADER_BUDGET_PER_ITERATION; + return true; + break; +#endif default: return false; } @@ -302,6 +351,11 @@ void blackboxDeviceClose(void) // Some flash device, e.g., NAND devices, require explicit close to flush internally buffered data. flashfsClose(); break; +#endif +#if defined(SITL_BUILD) + case BLACKBOX_DEVICE_FILE: + fclose(blackboxFile.file_handler); + break; #endif default: ; @@ -506,6 +560,11 @@ bool isBlackboxDeviceFull(void) return afatfs_isFull(); #endif +#if defined (SITL_BUILD) + case BLACKBOX_DEVICE_FILE: + return false; +#endif + default: return false; } @@ -562,6 +621,11 @@ void blackboxReplenishHeaderBudget(void) case BLACKBOX_DEVICE_SDCARD: freeSpace = afatfs_getFreeBufferSpace(); break; +#endif +#if defined(SITL_BUILD) + case BLACKBOX_DEVICE_FILE: + freeSpace = BLACKBOX_MAX_ACCUMULATED_HEADER_BUDGET; + break; #endif default: freeSpace = 0; @@ -631,6 +695,12 @@ blackboxBufferReserveStatus_e blackboxDeviceReserveBufferSpace(int32_t bytes) return BLACKBOX_RESERVE_TEMPORARY_FAILURE; #endif +#if defined(SITL_BUILD) + case BLACKBOX_DEVICE_FILE: + // Assume that all writes will fit in the file's buffers + return BLACKBOX_RESERVE_TEMPORARY_FAILURE; +#endif + default: return BLACKBOX_RESERVE_PERMANENT_FAILURE; } diff --git a/src/main/blackbox/blackbox_io.h b/src/main/blackbox/blackbox_io.h index d143b72098d..76b03b5cd6a 100644 --- a/src/main/blackbox/blackbox_io.h +++ b/src/main/blackbox/blackbox_io.h @@ -31,6 +31,9 @@ typedef enum BlackboxDevice { #ifdef USE_SDCARD BLACKBOX_DEVICE_SDCARD = 2, #endif +#if defined(SITL_BUILD) + BLACKBOX_DEVICE_FILE = 3, +#endif BLACKBOX_DEVICE_END } BlackboxDevice; diff --git a/src/main/fc/settings.yaml b/src/main/fc/settings.yaml index 52d6d63f66e..341fe95bddd 100644 --- a/src/main/fc/settings.yaml +++ b/src/main/fc/settings.yaml @@ -25,7 +25,7 @@ tables: - name: serial_rx values: ["SPEK1024", "SPEK2048", "SBUS", "SUMD", "IBUS", "JETIEXBUS", "CRSF", "FPORT", "SBUS_FAST", "FPORT2", "SRXL2", "GHST", "MAVLINK", "FBUS"] - name: blackbox_device - values: ["SERIAL", "SPIFLASH", "SDCARD"] + values: ["SERIAL", "SPIFLASH", "SDCARD", "FILE"] - name: motor_pwm_protocol values: ["STANDARD", "ONESHOT125", "MULTISHOT", "BRUSHED", "DSHOT150", "DSHOT300", "DSHOT600"] - name: servo_protocol From ed4aac11222a3da2ee6c92a36d9faf34d8495ed3 Mon Sep 17 00:00:00 2001 From: Bart Slinger Date: Fri, 12 Jul 2024 22:29:36 +0800 Subject: [PATCH 2/3] Set blackbox file handler to NULL after closing file --- src/main/blackbox/blackbox_io.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/blackbox/blackbox_io.c b/src/main/blackbox/blackbox_io.c index dfa79b16eda..36a9750be84 100644 --- a/src/main/blackbox/blackbox_io.c +++ b/src/main/blackbox/blackbox_io.c @@ -355,6 +355,7 @@ void blackboxDeviceClose(void) #if defined(SITL_BUILD) case BLACKBOX_DEVICE_FILE: fclose(blackboxFile.file_handler); + blackboxFile.file_handler = NULL; break; #endif default: From 934fb4c777f89f752793135f8ffb8c7b3c11f93b Mon Sep 17 00:00:00 2001 From: Bart Slinger Date: Sun, 14 Jul 2024 10:55:36 +0800 Subject: [PATCH 3/3] Blackbox device type 'file' (SITL) considered working when file handler is available --- src/main/blackbox/blackbox_io.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/blackbox/blackbox_io.c b/src/main/blackbox/blackbox_io.c index 36a9750be84..b344b0dfff6 100644 --- a/src/main/blackbox/blackbox_io.c +++ b/src/main/blackbox/blackbox_io.c @@ -583,6 +583,10 @@ bool isBlackboxDeviceWorking(void) #ifdef USE_FLASHFS case BLACKBOX_DEVICE_FLASH: return flashfsIsReady(); +#endif +#if defined(SITL_BUILD) + case BLACKBOX_DEVICE_FILE: + return blackboxFile.file_handler != NULL; #endif default: return false;