Add blackbox device FILE for SITL build
[inav.git] / src / main / blackbox / blackbox_io.c
blobdfa79b16edad4894ec0e3596385f9568519b2dca
1 /*
2 * This file is part of Cleanflight.
4 * Cleanflight is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * Cleanflight is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
18 #include <stdlib.h>
19 #include <stdarg.h>
20 #include <string.h>
22 #if defined(SITL_BUILD)
23 #include <stdio.h>
24 #include <time.h>
25 #endif
27 #include "platform.h"
29 #ifdef USE_BLACKBOX
31 #include "blackbox.h"
32 #include "blackbox_io.h"
34 #include "common/axis.h"
35 #include "common/encoding.h"
36 #include "common/maths.h"
37 #include "common/printf.h"
38 #include "common/typeconversion.h"
40 #include "config/parameter_group.h"
41 #include "config/parameter_group_ids.h"
43 #ifdef USE_SDCARD
44 #include "drivers/sdcard/sdcard.h"
45 #endif
47 #include "io/asyncfatfs/asyncfatfs.h"
48 #include "io/flashfs.h"
49 #include "io/serial.h"
51 #include "msp/msp_serial.h"
53 #include "sensors/gyro.h"
54 #include "fc/config.h"
56 #define BLACKBOX_SERIAL_PORT_MODE MODE_TX
58 // How many bytes can we transmit per loop iteration when writing headers?
59 static uint8_t blackboxMaxHeaderBytesPerIteration;
61 // How many bytes can we write *this* iteration without overflowing transmit buffers or overstressing the OpenLog?
62 int32_t blackboxHeaderBudget;
64 STATIC_UNIT_TESTED serialPort_t *blackboxPort = NULL;
65 #ifndef UNIT_TEST
66 static portSharing_e blackboxPortSharing;
67 #endif // UNIT_TEST
69 #ifdef USE_SDCARD
71 static struct {
72 afatfsFilePtr_t logFile;
73 afatfsFilePtr_t logDirectory;
74 afatfsFinder_t logDirectoryFinder;
75 uint32_t largestLogFileNumber;
77 enum {
78 BLACKBOX_SDCARD_INITIAL,
79 BLACKBOX_SDCARD_WAITING,
80 BLACKBOX_SDCARD_ENUMERATE_FILES,
81 BLACKBOX_SDCARD_CHANGE_INTO_LOG_DIRECTORY,
82 BLACKBOX_SDCARD_READY_TO_CREATE_LOG,
83 BLACKBOX_SDCARD_READY_TO_LOG
84 } state;
85 } blackboxSDCard;
87 #endif
89 #if defined(SITL_BUILD)
90 static struct {
91 FILE *file_handler;
92 } blackboxFile;
93 #endif
95 #ifndef UNIT_TEST
96 void blackboxOpen(void)
98 serialPort_t *sharedBlackboxAndMspPort = findSharedSerialPort(FUNCTION_BLACKBOX, FUNCTION_MSP);
99 if (sharedBlackboxAndMspPort) {
100 mspSerialReleasePortIfAllocated(sharedBlackboxAndMspPort);
103 #endif // UNIT_TEST
105 void blackboxWrite(uint8_t value)
107 switch (blackboxConfig()->device) {
108 #ifdef USE_FLASHFS
109 case BLACKBOX_DEVICE_FLASH:
110 flashfsWriteByte(value); // Write byte asynchronously
111 break;
112 #endif
113 #ifdef USE_SDCARD
114 case BLACKBOX_DEVICE_SDCARD:
115 afatfs_fputc(blackboxSDCard.logFile, value);
116 break;
117 #endif
118 #if defined(SITL_BUILD)
119 case BLACKBOX_DEVICE_FILE:
120 fputc(value, blackboxFile.file_handler);
121 break;
122 #endif
123 case BLACKBOX_DEVICE_SERIAL:
124 default:
125 serialWrite(blackboxPort, value);
126 break;
130 // Print the null-terminated string 's' to the blackbox device and return the number of bytes written
131 int blackboxPrint(const char *s)
133 int length;
134 const uint8_t *pos;
136 switch (blackboxConfig()->device) {
138 #ifdef USE_FLASHFS
139 case BLACKBOX_DEVICE_FLASH:
140 length = strlen(s);
141 flashfsWrite((const uint8_t*) s, length, false); // Write asynchronously
142 break;
143 #endif
145 #ifdef USE_SDCARD
146 case BLACKBOX_DEVICE_SDCARD:
147 length = strlen(s);
148 afatfs_fwrite(blackboxSDCard.logFile, (const uint8_t*) s, length); // Ignore failures due to buffers filling up
149 break;
150 #endif
152 #if defined(SITL_BUILD)
153 case BLACKBOX_DEVICE_FILE:
154 length = strlen(s);
155 fputs(s, blackboxFile.file_handler);
156 break;
157 #endif
159 case BLACKBOX_DEVICE_SERIAL:
160 default:
161 pos = (uint8_t*) s;
162 while (*pos) {
163 serialWrite(blackboxPort, *pos);
164 pos++;
167 length = pos - (uint8_t*) s;
168 break;
171 return length;
175 * If there is data waiting to be written to the blackbox device, attempt to write (a portion of) that now.
177 * Intended to be called regularly for the blackbox device to perform housekeeping.
179 void blackboxDeviceFlush(void)
181 switch (blackboxConfig()->device) {
182 #ifdef USE_FLASHFS
184 * This is our only output device which requires us to call flush() in order for it to write anything. The other
185 * devices will progressively write in the background without Blackbox calling anything.
187 case BLACKBOX_DEVICE_FLASH:
188 flashfsFlushAsync();
189 break;
190 #endif
192 default:
198 * If there is data waiting to be written to the blackbox device, attempt to write (a portion of) that now.
200 * Returns true if all data has been written to the device.
202 bool blackboxDeviceFlushForce(void)
204 switch (blackboxConfig()->device) {
205 case BLACKBOX_DEVICE_SERIAL:
206 // Nothing to speed up flushing on serial, as serial is continuously being drained out of its buffer
207 return isSerialTransmitBufferEmpty(blackboxPort);
209 #ifdef USE_FLASHFS
210 case BLACKBOX_DEVICE_FLASH:
211 return flashfsFlushAsync();
212 #endif
214 #ifdef USE_SDCARD
215 case BLACKBOX_DEVICE_SDCARD:
216 /* SD card will flush itself without us calling it, but we need to call flush manually in order to check
217 * if it's done yet or not!
219 return afatfs_flush();
220 #endif
222 #if defined(SITL_BUILD)
223 case BLACKBOX_DEVICE_FILE:
224 fflush(blackboxFile.file_handler);
225 return true;
226 #endif
228 default:
229 return false;
234 * Attempt to open the logging device. Returns true if successful.
236 #ifndef UNIT_TEST
237 bool blackboxDeviceOpen(void)
239 switch (blackboxConfig()->device) {
240 case BLACKBOX_DEVICE_SERIAL:
242 serialPortConfig_t *portConfig = findSerialPortConfig(FUNCTION_BLACKBOX);
243 baudRate_e baudRateIndex;
244 portOptions_t portOptions = SERIAL_PARITY_NO | SERIAL_NOT_INVERTED;
246 if (!portConfig) {
247 return false;
250 blackboxPortSharing = determinePortSharing(portConfig, FUNCTION_BLACKBOX);
251 baudRateIndex = portConfig->peripheral_baudrateIndex;
253 if (baudRates[baudRateIndex] == 230400) {
255 * OpenLog's 230400 baud rate is very inaccurate, so it requires a larger inter-character gap in
256 * order to maintain synchronization.
258 portOptions |= SERIAL_STOPBITS_2;
259 } else {
260 portOptions |= SERIAL_STOPBITS_1;
263 blackboxPort = openSerialPort(portConfig->identifier, FUNCTION_BLACKBOX, NULL, NULL, baudRates[baudRateIndex],
264 BLACKBOX_SERIAL_PORT_MODE, portOptions);
267 * The slowest MicroSD cards have a write latency approaching 150ms. The OpenLog's buffer is about 900
268 * bytes. In order for its buffer to be able to absorb this latency we must write slower than 6000 B/s.
270 * So:
271 * Bytes per loop iteration = floor((looptime_ns / 1000000.0) * 6000)
272 * = floor((looptime_ns * 6000) / 1000000.0)
273 * = floor((looptime_ns * 3) / 500.0)
274 * = (looptime_ns * 3) / 500
276 blackboxMaxHeaderBytesPerIteration = constrain((getLooptime() * 3) / 500, 1, BLACKBOX_TARGET_HEADER_BUDGET_PER_ITERATION);
278 return blackboxPort != NULL;
280 break;
281 #ifdef USE_FLASHFS
282 case BLACKBOX_DEVICE_FLASH:
283 if (flashfsGetSize() == 0 || isBlackboxDeviceFull()) {
284 return false;
287 blackboxMaxHeaderBytesPerIteration = BLACKBOX_TARGET_HEADER_BUDGET_PER_ITERATION;
289 return true;
290 break;
291 #endif
292 #ifdef USE_SDCARD
293 case BLACKBOX_DEVICE_SDCARD:
294 if (afatfs_getFilesystemState() == AFATFS_FILESYSTEM_STATE_FATAL || afatfs_getFilesystemState() == AFATFS_FILESYSTEM_STATE_UNKNOWN || afatfs_isFull()) {
295 return false;
298 blackboxMaxHeaderBytesPerIteration = BLACKBOX_TARGET_HEADER_BUDGET_PER_ITERATION;
300 return true;
301 break;
302 #endif
303 #if defined(SITL_BUILD)
304 case BLACKBOX_DEVICE_FILE:
306 const time_t now = time(NULL);
307 const struct tm *t = localtime(&now);
308 char filename[32];
309 strftime(filename, sizeof(filename), "%Y_%m_%d_%H%M%S.TXT", t);
311 blackboxFile.file_handler = fopen(filename, "wb");
312 if (blackboxFile.file_handler == NULL) {
313 fprintf(stderr, "[BlackBox] Failed to create log file\n");
314 return false;
316 fprintf(stderr, "[BlackBox] Created %s\n", filename);
319 blackboxMaxHeaderBytesPerIteration = BLACKBOX_TARGET_HEADER_BUDGET_PER_ITERATION;
320 return true;
321 break;
322 #endif
323 default:
324 return false;
327 #endif // UNIT_TEST
330 * Close the Blackbox logging device immediately without attempting to flush any remaining data.
332 #ifndef UNIT_TEST
333 void blackboxDeviceClose(void)
335 switch (blackboxConfig()->device) {
336 case BLACKBOX_DEVICE_SERIAL:
337 // Since the serial port could be shared with other processes, we have to give it back here
338 closeSerialPort(blackboxPort);
339 blackboxPort = NULL;
342 * Normally this would be handled by mw.c, but since we take an unknown amount
343 * of time to shut down asynchronously, we're the only ones that know when to call it.
345 if (blackboxPortSharing == PORTSHARING_SHARED) {
346 mspSerialAllocatePorts();
348 break;
349 #ifdef USE_FLASHFS
350 case BLACKBOX_DEVICE_FLASH:
351 // Some flash device, e.g., NAND devices, require explicit close to flush internally buffered data.
352 flashfsClose();
353 break;
354 #endif
355 #if defined(SITL_BUILD)
356 case BLACKBOX_DEVICE_FILE:
357 fclose(blackboxFile.file_handler);
358 break;
359 #endif
360 default:
364 #endif // UNIT_TEST
366 #ifdef USE_SDCARD
368 static void blackboxLogDirCreated(afatfsFilePtr_t directory)
370 if (directory) {
371 blackboxSDCard.logDirectory = directory;
373 afatfs_findFirst(blackboxSDCard.logDirectory, &blackboxSDCard.logDirectoryFinder);
375 blackboxSDCard.state = BLACKBOX_SDCARD_ENUMERATE_FILES;
376 } else {
377 // Retry
378 blackboxSDCard.state = BLACKBOX_SDCARD_INITIAL;
382 static void blackboxLogFileCreated(afatfsFilePtr_t file)
384 if (file) {
385 blackboxSDCard.logFile = file;
387 blackboxSDCard.largestLogFileNumber++;
389 blackboxSDCard.state = BLACKBOX_SDCARD_READY_TO_LOG;
390 } else {
391 // Retry
392 blackboxSDCard.state = BLACKBOX_SDCARD_READY_TO_CREATE_LOG;
396 static void blackboxCreateLogFile(void)
398 uint32_t remainder = blackboxSDCard.largestLogFileNumber + 1;
400 char filename[13];
402 filename[0] = 'L';
403 filename[1] = 'O';
404 filename[2] = 'G';
406 for (int i = 7; i >= 3; i--) {
407 filename[i] = (remainder % 10) + '0';
408 remainder /= 10;
411 filename[8] = '.';
412 filename[9] = 'T';
413 filename[10] = 'X';
414 filename[11] = 'T';
415 filename[12] = 0;
417 blackboxSDCard.state = BLACKBOX_SDCARD_WAITING;
419 afatfs_fopen(filename, "as", blackboxLogFileCreated);
423 * Begin a new log on the SDCard.
425 * Keep calling until the function returns true (open is complete).
427 static bool blackboxSDCardBeginLog(void)
429 fatDirectoryEntry_t *directoryEntry;
431 doMore:
432 switch (blackboxSDCard.state) {
433 case BLACKBOX_SDCARD_INITIAL:
434 if (afatfs_getFilesystemState() == AFATFS_FILESYSTEM_STATE_READY) {
435 blackboxSDCard.state = BLACKBOX_SDCARD_WAITING;
437 afatfs_mkdir("logs", blackboxLogDirCreated);
439 break;
441 case BLACKBOX_SDCARD_WAITING:
442 // Waiting for directory entry to be created
443 break;
445 case BLACKBOX_SDCARD_ENUMERATE_FILES:
446 while (afatfs_findNext(blackboxSDCard.logDirectory, &blackboxSDCard.logDirectoryFinder, &directoryEntry) == AFATFS_OPERATION_SUCCESS) {
447 if (directoryEntry && !fat_isDirectoryEntryTerminator(directoryEntry)) {
448 // If this is a log file, parse the log number from the filename
449 if (
450 directoryEntry->filename[0] == 'L' && directoryEntry->filename[1] == 'O' && directoryEntry->filename[2] == 'G'
451 && directoryEntry->filename[8] == 'T' && directoryEntry->filename[9] == 'X' && directoryEntry->filename[10] == 'T'
453 char logSequenceNumberString[6];
455 memcpy(logSequenceNumberString, directoryEntry->filename + 3, 5);
456 logSequenceNumberString[5] = '\0';
458 blackboxSDCard.largestLogFileNumber = MAX((uint32_t) fastA2I(logSequenceNumberString), blackboxSDCard.largestLogFileNumber);
460 } else {
461 // We're done checking all the files on the card, now we can create a new log file
462 afatfs_findLast(blackboxSDCard.logDirectory);
464 blackboxSDCard.state = BLACKBOX_SDCARD_CHANGE_INTO_LOG_DIRECTORY;
465 goto doMore;
468 break;
470 case BLACKBOX_SDCARD_CHANGE_INTO_LOG_DIRECTORY:
471 // Change into the log directory:
472 if (afatfs_chdir(blackboxSDCard.logDirectory)) {
473 // We no longer need our open handle on the log directory
474 afatfs_fclose(blackboxSDCard.logDirectory, NULL);
475 blackboxSDCard.logDirectory = NULL;
477 blackboxSDCard.state = BLACKBOX_SDCARD_READY_TO_CREATE_LOG;
478 goto doMore;
480 break;
482 case BLACKBOX_SDCARD_READY_TO_CREATE_LOG:
483 blackboxCreateLogFile();
484 break;
486 case BLACKBOX_SDCARD_READY_TO_LOG:
487 return true; // Log has been created!
490 // Not finished init yet
491 return false;
494 #endif
497 * Begin a new log (for devices which support separations between the logs of multiple flights).
499 * Keep calling until the function returns true (open is complete).
501 bool blackboxDeviceBeginLog(void)
503 switch (blackboxConfig()->device) {
504 #ifdef USE_SDCARD
505 case BLACKBOX_DEVICE_SDCARD:
506 return blackboxSDCardBeginLog();
507 #endif
508 default:
509 return true;
515 * Terminate the current log (for devices which support separations between the logs of multiple flights).
517 * retainLog - Pass true if the log should be kept, or false if the log should be discarded (if supported).
519 * Keep calling until this returns true
521 bool blackboxDeviceEndLog(bool retainLog)
523 #ifndef USE_SDCARD
524 (void) retainLog;
525 #endif
527 switch (blackboxConfig()->device) {
528 #ifdef USE_SDCARD
529 case BLACKBOX_DEVICE_SDCARD:
530 // Keep retrying until the close operation queues
531 if (
532 (retainLog && afatfs_fclose(blackboxSDCard.logFile, NULL))
533 || (!retainLog && afatfs_funlink(blackboxSDCard.logFile, NULL))
535 // Don't bother waiting the for the close to complete, it's queued now and will complete eventually
536 blackboxSDCard.logFile = NULL;
537 blackboxSDCard.state = BLACKBOX_SDCARD_READY_TO_CREATE_LOG;
538 return true;
540 return false;
541 #endif
542 default:
543 return true;
547 bool isBlackboxDeviceFull(void)
549 switch (blackboxConfig()->device) {
550 case BLACKBOX_DEVICE_SERIAL:
551 return false;
553 #ifdef USE_FLASHFS
554 case BLACKBOX_DEVICE_FLASH:
555 return flashfsIsEOF();
556 #endif
558 #ifdef USE_SDCARD
559 case BLACKBOX_DEVICE_SDCARD:
560 return afatfs_isFull();
561 #endif
563 #if defined (SITL_BUILD)
564 case BLACKBOX_DEVICE_FILE:
565 return false;
566 #endif
568 default:
569 return false;
573 bool isBlackboxDeviceWorking(void)
575 switch (blackboxConfig()->device) {
576 case BLACKBOX_DEVICE_SERIAL:
577 return blackboxPort != NULL;
578 #ifdef USE_SDCARD
579 case BLACKBOX_DEVICE_SDCARD:
580 return sdcard_isInserted() && sdcard_isFunctional() && (afatfs_getFilesystemState() == AFATFS_FILESYSTEM_STATE_READY);
581 #endif
582 #ifdef USE_FLASHFS
583 case BLACKBOX_DEVICE_FLASH:
584 return flashfsIsReady();
585 #endif
586 default:
587 return false;
591 int32_t blackboxGetLogNumber(void)
593 switch (blackboxConfig()->device) {
594 #ifdef USE_SDCARD
595 case BLACKBOX_DEVICE_SDCARD:
596 return blackboxSDCard.largestLogFileNumber;
597 #endif
598 default:
599 return -1;
604 * Call once every loop iteration in order to maintain the global blackboxHeaderBudget with the number of bytes we can
605 * transmit this iteration.
607 void blackboxReplenishHeaderBudget(void)
609 int32_t freeSpace;
611 switch (blackboxConfig()->device) {
612 case BLACKBOX_DEVICE_SERIAL:
613 freeSpace = serialTxBytesFree(blackboxPort);
614 break;
615 #ifdef USE_FLASHFS
616 case BLACKBOX_DEVICE_FLASH:
617 freeSpace = flashfsGetWriteBufferFreeSpace();
618 break;
619 #endif
620 #ifdef USE_SDCARD
621 case BLACKBOX_DEVICE_SDCARD:
622 freeSpace = afatfs_getFreeBufferSpace();
623 break;
624 #endif
625 #if defined(SITL_BUILD)
626 case BLACKBOX_DEVICE_FILE:
627 freeSpace = BLACKBOX_MAX_ACCUMULATED_HEADER_BUDGET;
628 break;
629 #endif
630 default:
631 freeSpace = 0;
634 blackboxHeaderBudget = MIN(MIN(freeSpace, blackboxHeaderBudget + blackboxMaxHeaderBytesPerIteration), BLACKBOX_MAX_ACCUMULATED_HEADER_BUDGET);
638 * You must call this function before attempting to write Blackbox header bytes to ensure that the write will not
639 * cause buffers to overflow. The number of bytes you can write is capped by the blackboxHeaderBudget. Calling this
640 * reservation function doesn't decrease blackboxHeaderBudget, so you must manually decrement that variable by the
641 * number of bytes you actually wrote.
643 * When the Blackbox device is FlashFS, a successful return code guarantees that no data will be lost if you write that
644 * many bytes to the device (i.e. FlashFS's buffers won't overflow).
646 * When the device is a serial port, a successful return code guarantees that Cleanflight's serial Tx buffer will not
647 * overflow, and the outgoing bandwidth is likely to be small enough to give the OpenLog time to absorb MicroSD card
648 * latency. However the OpenLog could still end up silently dropping data.
650 * Returns:
651 * BLACKBOX_RESERVE_SUCCESS - Upon success
652 * BLACKBOX_RESERVE_TEMPORARY_FAILURE - The buffer is currently too full to service the request, try again later
653 * BLACKBOX_RESERVE_PERMANENT_FAILURE - The buffer is too small to ever service this request
655 blackboxBufferReserveStatus_e blackboxDeviceReserveBufferSpace(int32_t bytes)
657 if (bytes <= blackboxHeaderBudget) {
658 return BLACKBOX_RESERVE_SUCCESS;
661 // Handle failure:
662 switch (blackboxConfig()->device) {
663 case BLACKBOX_DEVICE_SERIAL:
665 * One byte of the tx buffer isn't available for user data (due to its circular list implementation),
666 * hence the -1. Note that the USB VCP implementation doesn't use a buffer and has txBufferSize set to zero.
668 if (blackboxPort->txBufferSize && bytes > (int32_t) blackboxPort->txBufferSize - 1) {
669 return BLACKBOX_RESERVE_PERMANENT_FAILURE;
672 return BLACKBOX_RESERVE_TEMPORARY_FAILURE;
674 #ifdef USE_FLASHFS
675 case BLACKBOX_DEVICE_FLASH:
676 if (bytes > (int32_t) flashfsGetWriteBufferSize()) {
677 return BLACKBOX_RESERVE_PERMANENT_FAILURE;
680 if (bytes > (int32_t) flashfsGetWriteBufferFreeSpace()) {
682 * The write doesn't currently fit in the buffer, so try to make room for it. Our flushing here means
683 * that the Blackbox header writing code doesn't have to guess about the best time to ask flashfs to
684 * flush, and doesn't stall waiting for a flush that would otherwise not automatically be called.
686 flashfsFlushAsync();
689 return BLACKBOX_RESERVE_TEMPORARY_FAILURE;
690 #endif
692 #ifdef USE_SDCARD
693 case BLACKBOX_DEVICE_SDCARD:
694 // Assume that all writes will fit in the SDCard's buffers
695 return BLACKBOX_RESERVE_TEMPORARY_FAILURE;
696 #endif
698 #if defined(SITL_BUILD)
699 case BLACKBOX_DEVICE_FILE:
700 // Assume that all writes will fit in the file's buffers
701 return BLACKBOX_RESERVE_TEMPORARY_FAILURE;
702 #endif
704 default:
705 return BLACKBOX_RESERVE_PERMANENT_FAILURE;
709 #endif