rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
mmc_card_util.cpp
Go to the documentation of this file.
1#include "pch.h"
2
3#if EFI_PROD_CODE && EFI_FILE_LOGGING
4
5#include "ff.h"
6#include "mmc_card_util.h"
7#include "backup_ram.h"
8#include "mmc_card.h"
9
10#define LOG_INDEX_FILENAME "index.txt"
11
12using namespace rusefi::stringutil;
13
14// 10 because we want at least 4 character name (is that about TS protocol which we do not use any more?)
15#define MIN_FILE_INDEX 10
16int logFileIndex = MIN_FILE_INDEX;
17
18void incLogFileName(FIL *fd) {
19 // clear the memory
20 memset(fd, 0, sizeof(FIL));
21 // This file has the index for next log file name
22 FRESULT ret = f_open(fd, LOG_INDEX_FILENAME, FA_READ);
23
24 char data[_MAX_FILLER];
25 memset(data, 0, sizeof(data));
26
27 if (ret == FR_OK) {
28 UINT readed = 0;
29 // leave one byte for terminating 0
30 ret = f_read(fd, (void*)data, sizeof(data) - 1, &readed);
31
32 if (ret != FR_OK) {
33 printFatFsError("log index file read", ret);
34 logFileIndex = MIN_FILE_INDEX;
35 } else {
36 efiPrintf("Got content [%s] size %d", data, readed);
37 logFileIndex = maxI(MIN_FILE_INDEX, atoi(data));
38 if (absI(logFileIndex) == ATOI_ERROR_CODE) {
39 logFileIndex = MIN_FILE_INDEX;
40 } else {
41 // next file would use next file name
43 }
44 }
45 f_close(fd);
46 } else if (ret == FR_NO_FILE) {
47 // no index file - this is not an error, just an empty SD
48 logFileIndex = MIN_FILE_INDEX;
49 } else {
50 printFatFsError("log index file open", ret);
51 efiPrintf("%s: not found or error: %d", LOG_INDEX_FILENAME, ret);
52 logFileIndex = MIN_FILE_INDEX;
53 }
54
55 // truncate or create new
56 ret = f_open(fd, LOG_INDEX_FILENAME, FA_CREATE_ALWAYS | FA_WRITE);
57 if (ret == FR_OK) {
58 UINT writen = 0;
59 size_t len = itoa10(data, logFileIndex) - data;
60 ret = f_write(fd, (void*)data, len, &writen);
61 if ((ret != FR_OK) || (len != writen)) {
62 printFatFsError("log index write", ret);
63 }
64 f_close(fd);
65 } else {
66 printFatFsError("log index file write", ret);
67 }
68
69 efiPrintf("New log file index %d", logFileIndex);
70}
71
72#endif // EFI_PROD_CODE && EFI_FILE_LOGGING
Non-volatile backup-RAM registers support.
char * itoa10(char *p, int num)
Definition efilib.cpp:107
void printFatFsError(const char *str, FRESULT f_error)
Definition mmc_card.cpp:273
void incLogFileName(FIL *fd)
int logFileIndex
static NO_CACHE FIL fd