1
0
Fork 0
mirror of https://github.com/betaflight/betaflight.git synced 2025-07-12 19:10:32 +03:00

Added virtual blackbox for SITL (#14325)

* Added virtual black box for SITL

* Update src/main/blackbox/blackbox_virtual.c

The platform.h is added into blackbox_virtual,c

Co-authored-by: Mark Haslinghuis <mark@numloq.nl>

* Virtual blackbox code style improvement

* Resolved issue of log files counter

* Added logs file name length checking in virtual blackbox

* Added open directory function result checking

Co-authored-by: Mark Haslinghuis <mark@numloq.nl>

* Added computing of const log file neme length

* Improved virtual blackbox file name generation

* Edited errors description in case of using virtual blackbox for non SITL build

* Removed extra #ifdef

Co-authored-by: Mark Haslinghuis <mark@numloq.nl>

* Apply USE_BLACKBOX, USE_BLACKBOX_VIRTUAL define for SITL build

* Code style improvement

Co-authored-by: Jan Post <Rm2k-Freak@web.de>

* Code style improvement

Co-authored-by: Jan Post <Rm2k-Freak@web.de>

* Code style improvement

Co-authored-by: Jan Post <Rm2k-Freak@web.de>

* Code style improvement

Co-authored-by: Jan Post <Rm2k-Freak@web.de>

* Code style improvement

Co-authored-by: Jan Post <Rm2k-Freak@web.de>

* Code style improvement

Co-authored-by: Jan Post <Rm2k-Freak@web.de>

* Added USE_BLACKBOX_VIRTUAL checking for #error message

---------

Co-authored-by: Mark Haslinghuis <mark@numloq.nl>
Co-authored-by: Jan Post <Rm2k-Freak@web.de>
This commit is contained in:
Vladimir Demidov 2025-04-04 22:57:44 +03:00 committed by GitHub
parent 088620ce35
commit df2e99b953
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 240 additions and 5 deletions

View file

@ -1050,6 +1050,9 @@ void blackboxValidateConfig(void)
#endif
#ifdef USE_SDCARD
case BLACKBOX_DEVICE_SDCARD:
#endif
#ifdef USE_BLACKBOX_VIRTUAL
case BLACKBOX_DEVICE_VIRTUAL:
#endif
case BLACKBOX_DEVICE_SERIAL:
// Device supported, leave the setting alone

View file

@ -29,7 +29,8 @@ typedef enum BlackboxDevice {
BLACKBOX_DEVICE_NONE = 0,
BLACKBOX_DEVICE_FLASH = 1,
BLACKBOX_DEVICE_SDCARD = 2,
BLACKBOX_DEVICE_SERIAL = 3
BLACKBOX_DEVICE_SERIAL = 3,
BLACKBOX_DEVICE_VIRTUAL = 4,
} BlackboxDevice_e;
typedef enum BlackboxMode {

View file

@ -59,6 +59,8 @@
#include "drivers/sdcard.h"
#endif
#include "blackbox_virtual.h"
#define BLACKBOX_SERIAL_PORT_MODE MODE_TX
// How many bytes can we transmit per loop iteration when writing headers?
@ -124,6 +126,11 @@ void blackboxWrite(uint8_t value)
case BLACKBOX_DEVICE_SDCARD:
afatfs_fputc(blackboxSDCard.logFile, value);
break;
#endif
#ifdef USE_BLACKBOX_VIRTUAL
case BLACKBOX_DEVICE_VIRTUAL:
blackboxVirtualPutChar(value);
break;
#endif
case BLACKBOX_DEVICE_SERIAL:
default:
@ -185,6 +192,13 @@ int blackboxWriteString(const char *s)
break;
#endif // USE_SDCARD
#ifdef USE_BLACKBOX_VIRTUAL
case BLACKBOX_DEVICE_VIRTUAL:
length = strlen(s);
blackboxVirtualWrite((const uint8_t*) s, length);
break;
#endif
case BLACKBOX_DEVICE_SERIAL:
default:
pos = (uint8_t*) s;
@ -217,6 +231,11 @@ void blackboxDeviceFlush(void)
flashfsFlushAsync(false);
break;
#endif // USE_FLASHFS
#ifdef USE_BLACKBOX_VIRTUAL
case BLACKBOX_DEVICE_VIRTUAL:
blackboxVirtualFlush();
break;
#endif
default:
;
@ -249,6 +268,10 @@ bool blackboxDeviceFlushForce(void)
// been physically written to the SD card yet.
return afatfs_flush();
#endif // USE_SDCARD
#ifdef USE_BLACKBOX_VIRTUAL
case BLACKBOX_DEVICE_VIRTUAL:
return blackboxVirtualFlush();
#endif
default:
return false;
@ -269,6 +292,11 @@ bool blackboxDeviceFlushForceComplete(void)
return false;
}
#endif // USE_SDCARD
#ifdef USE_BLACKBOX_VIRTUAL
case BLACKBOX_DEVICE_VIRTUAL:
blackboxVirtualFlush();
return true;
#endif
default:
return blackboxDeviceFlushForce();
@ -360,6 +388,11 @@ bool blackboxDeviceOpen(void)
return true;
break;
#endif // USE_SDCARD
#ifdef USE_BLACKBOX_VIRTUAL
case BLACKBOX_DEVICE_VIRTUAL:
return blackboxVirtualOpen();
#endif
default:
return false;
}
@ -428,6 +461,11 @@ void blackboxDeviceClose(void)
// Some flash device, e.g., NAND devices, require explicit close to flush internally buffered data.
flashfsClose();
break;
#endif
#ifdef USE_BLACKBOX_VIRTUAL
case BLACKBOX_DEVICE_VIRTUAL:
blackboxVirtualClose();
break;
#endif
default:
;
@ -564,6 +602,10 @@ bool blackboxDeviceBeginLog(void)
case BLACKBOX_DEVICE_SDCARD:
return blackboxSDCardBeginLog();
#endif // USE_SDCARD
#ifdef USE_BLACKBOX_VIRTUAL
case BLACKBOX_DEVICE_VIRTUAL:
return blackboxVirtualBeginLog();
#endif
default:
return true;
}
@ -598,6 +640,13 @@ bool blackboxDeviceEndLog(bool retainLog)
}
return false;
#endif // USE_SDCARD
#ifdef USE_BLACKBOX_VIRTUAL
case BLACKBOX_DEVICE_VIRTUAL:
blackboxVirtualEndLog();
return true;
#endif
default:
return true;
}
@ -640,6 +689,11 @@ bool isBlackboxDeviceWorking(void)
return flashfsIsReady();
#endif
#ifdef USE_BLACKBOX_VIRTUAL
case BLACKBOX_DEVICE_VIRTUAL:
return true;
#endif
default:
return false;
}
@ -653,6 +707,12 @@ int32_t blackboxGetLogNumber(void)
return blackboxSDCard.largestLogFileNumber;
#endif
#ifdef USE_BLACKBOX_VIRTUAL
case BLACKBOX_DEVICE_VIRTUAL:
return blackboxVirtualLogFileNumber();
break;
#endif
default:
return -1;
}
@ -679,6 +739,11 @@ void blackboxReplenishHeaderBudget(void)
case BLACKBOX_DEVICE_SDCARD:
freeSpace = afatfs_getFreeBufferSpace();
break;
#endif
#ifdef USE_BLACKBOX_VIRTUAL
case BLACKBOX_DEVICE_VIRTUAL:
blackboxHeaderBudget = 1024*8;
return;
#endif
default:
freeSpace = 0;
@ -745,6 +810,12 @@ blackboxBufferReserveStatus_e blackboxDeviceReserveBufferSpace(int32_t bytes)
return BLACKBOX_RESERVE_TEMPORARY_FAILURE;
#endif // USE_SDCARD
#ifdef USE_BLACKBOX_VIRTUAL
case BLACKBOX_DEVICE_VIRTUAL:
return BLACKBOX_RESERVE_TEMPORARY_FAILURE;
#endif
default:
return BLACKBOX_RESERVE_PERMANENT_FAILURE;
}
@ -752,7 +823,6 @@ blackboxBufferReserveStatus_e blackboxDeviceReserveBufferSpace(int32_t bytes)
int8_t blackboxGetLogFileNo(void)
{
#ifdef USE_BLACKBOX
#ifdef USE_SDCARD
// return current file number or -1
if (blackboxSDCard.state == BLACKBOX_SDCARD_READY_TO_LOG) {
@ -764,6 +834,5 @@ int8_t blackboxGetLogFileNo(void)
// will be implemented later for flash based storage
return -1;
#endif
#endif
}
#endif // BLACKBOX

View file

@ -0,0 +1,117 @@
/*
* This file is part of Betaflight.
*
* Betaflight is free software. You can redistribute this software
* and/or modify this software under the terms of the GNU General
* Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later
* version.
*
* Betaflight is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this software.
*
* If not, see <http://www.gnu.org/licenses/>.
*/
#include "platform.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <dirent.h>
#include <common/maths.h>
#define LOGFILE_PREFIX "LOG"
#define LOGFILE_SUFFIX "BFL"
static FILE *blackboxVirtualFile = NULL;
static int32_t largestLogFileNumber = 0;
bool blackboxVirtualOpen(void)
{
const size_t log_name_length = strlen(LOGFILE_PREFIX) + 5 + strlen(LOGFILE_SUFFIX) + 1; //file name template: LOG00001.BFL
DIR *dir = opendir(".");
if (!dir) {
return false; // Failed to open directory
}
struct dirent *entry;
while ((entry = readdir(dir)) != NULL) {
if (strlen(entry->d_name) == log_name_length
&& strncmp(entry->d_name, LOGFILE_PREFIX, strlen(LOGFILE_PREFIX)) == 0
&& strncmp(entry->d_name + 9, LOGFILE_SUFFIX, strlen(LOGFILE_SUFFIX)) == 0) {
char logSequenceNumberString[6];
memcpy(logSequenceNumberString, entry->d_name + 3, 5);
logSequenceNumberString[5] = '\0';
largestLogFileNumber = MAX((int32_t)atoi(logSequenceNumberString), largestLogFileNumber);
}
}
closedir(dir);
return true;
}
void blackboxVirtualPutChar(uint8_t value)
{
if (blackboxVirtualFile != NULL) {
fputc(value, blackboxVirtualFile);
}
}
void blackboxVirtualWrite(const uint8_t *buffer, uint32_t len)
{
if (blackboxVirtualFile != NULL) {
fwrite(buffer, len, 1, blackboxVirtualFile);
}
}
bool blackboxVirtualFlush(void)
{
if (blackboxVirtualFile != NULL) {
fflush(blackboxVirtualFile);
return true;
} else {
return false;
}
}
bool blackboxVirtualBeginLog(void)
{
if (blackboxVirtualFile != NULL) {
return false;
}
const size_t name_buffer_length = strlen(LOGFILE_PREFIX) + 5 + strlen(LOGFILE_SUFFIX) + 2; //file name template: LOG00001.BFL
char filename[name_buffer_length];
sprintf(filename, "%s%05i.%s", LOGFILE_PREFIX, largestLogFileNumber + 1, LOGFILE_SUFFIX);
blackboxVirtualFile = fopen(filename, "w");
if (blackboxVirtualFile != NULL) {
largestLogFileNumber++;
}
return blackboxVirtualFile != NULL;
}
bool blackboxVirtualEndLog(void)
{
if (blackboxVirtualFile != NULL) {
fclose(blackboxVirtualFile);
blackboxVirtualFile = NULL;
}
return true;
}
void blackboxVirtualClose(void)
{
blackboxVirtualEndLog();
}
uint32_t blackboxVirtualLogFileNumber(void)
{
return largestLogFileNumber;
}

View file

@ -0,0 +1,35 @@
/*
* This file is part of Betaflight.
*
* Betaflight is free software. You can redistribute this software
* and/or modify this software under the terms of the GNU General
* Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later
* version.
*
* Betaflight is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this software.
*
* If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#if defined(USE_BLACKBOX_VIRTUAL) && !defined(SIMULATOR_BUILD)
#error "USE_BLACKBOX_VIRTUAL valid for SITL build only"
#endif
bool blackboxVirtualOpen(void);
void blackboxVirtualPutChar(uint8_t value);
void blackboxVirtualWrite(const uint8_t *buffer, uint32_t len);
bool blackboxVirtualFlush(void);
bool blackboxVirtualBeginLog(void);
bool blackboxVirtualEndLog(void);
void blackboxVirtualClose(void);
uint32_t blackboxVirtualLogFileNumber(void);

View file

@ -268,7 +268,13 @@ static const char * const lookupTableGimbalMode[] = {
#ifdef USE_BLACKBOX
static const char * const lookupTableBlackboxDevice[] = {
"NONE", "SPIFLASH", "SDCARD", "SERIAL"
"NONE",
"SPIFLASH",
"SDCARD",
"SERIAL",
#ifdef USE_BLACKBOX_VIRTUAL
"VIRTUAL",
#endif
};
static const char * const lookupTableBlackboxMode[] = {

View file

@ -105,6 +105,9 @@
#define USE_PWM_OUTPUT
#endif
#define USE_BLACKBOX
#define USE_BLACKBOX_VIRTUAL
#undef USE_STACK_CHECK // I think SITL don't need this
#undef USE_DASHBOARD
#undef USE_TELEMETRY_LTM

View file

@ -7,7 +7,8 @@ TARGET_SRC = \
drivers/barometer/barometer_virtual.c \
drivers/compass/compass_virtual.c \
drivers/serial_tcp.c \
io/gps_virtual.c
io/gps_virtual.c \
blackbox/blackbox_virtual.c
SIZE_OPTIMISED_SRC += \
drivers/serial_tcp.c