From 6c73b5ba0a73ed4275facac6b46fff6a21b80477 Mon Sep 17 00:00:00 2001 From: Raphael Coeffic Date: Tue, 3 Aug 2021 11:00:33 +0200 Subject: [PATCH] WiP TODO: support conversions from BIN to YAML. --- radio/src/CMakeLists.txt | 4 +- radio/src/storage/conversions/conversions.cpp | 7 ++- radio/src/storage/sdcard_common.h | 7 ++- radio/src/storage/sdcard_raw.cpp | 46 +++++++++++-------- radio/src/storage/sdcard_raw.h | 2 +- 5 files changed, 42 insertions(+), 24 deletions(-) diff --git a/radio/src/CMakeLists.txt b/radio/src/CMakeLists.txt index 60d890678..532e28b32 100644 --- a/radio/src/CMakeLists.txt +++ b/radio/src/CMakeLists.txt @@ -160,7 +160,9 @@ if(${STORAGE} STREQUAL SDCARD) set(SRC ${SRC} storage/sdcard_raw.cpp) add_definitions(-DSDCARD_RAW) elseif(${STORAGE_FORMAT} STREQUAL YAML) - set(SRC ${SRC} storage/sdcard_yaml.cpp) + # TODO: how do we check if the conversions + # should be done from SD card or EEPROM? + set(SRC ${SRC} storage/sdcard_yaml.cpp storage/sdcard_raw.cpp) add_definitions(-DSDCARD_YAML) include(storage/yaml/CMakeLists.txt) #add_subdirectory(storage/yaml) diff --git a/radio/src/storage/conversions/conversions.cpp b/radio/src/storage/conversions/conversions.cpp index cdd2ebef4..b638f9898 100644 --- a/radio/src/storage/conversions/conversions.cpp +++ b/radio/src/storage/conversions/conversions.cpp @@ -22,7 +22,7 @@ #include "opentx.h" #include "conversions.h" -#if defined(COLORLCD) && !defined(SDCARD_YAML) +#if defined(COLORLCD) #include "storage/modelslist.h" #include "storage/sdcard_common.h" @@ -99,7 +99,10 @@ void convertRadioData(int version) char path[256]; getModelPath(path, filename); - error = writeFile(path, (uint8_t *)&g_model, sizeof(g_model)); + + //TODO: if YAML is used, output to YAML directly + error = writeFileBin(path, (uint8_t *)&g_model, sizeof(g_model)); + //TODO: what should be done with this error? } diff --git a/radio/src/storage/sdcard_common.h b/radio/src/storage/sdcard_common.h index 0c2eef0e4..04673ef2d 100644 --- a/radio/src/storage/sdcard_common.h +++ b/radio/src/storage/sdcard_common.h @@ -35,8 +35,11 @@ #endif // opens radio.bin or model file -const char * openFile(const char * fullpath, FIL * file, uint16_t * size, uint8_t * version); -const char * writeFile(const char * fullpath, const uint8_t * data, uint16_t size); +const char* openFileBin(const char* fullpath, FIL* file, uint16_t* size, + uint8_t* version); + +const char* writeFileBin(const char* fullpath, const uint8_t* data, + uint16_t size); void getModelPath(char * path, const char * filename); diff --git a/radio/src/storage/sdcard_raw.cpp b/radio/src/storage/sdcard_raw.cpp index 517346138..ff5a2c8e8 100644 --- a/radio/src/storage/sdcard_raw.cpp +++ b/radio/src/storage/sdcard_raw.cpp @@ -23,9 +23,9 @@ #include "modelslist.h" #include "conversions/conversions.h" -const char * writeFile(const char * filename, const uint8_t * data, uint16_t size) +const char * writeFileBin(const char * filename, const uint8_t * data, uint16_t size) { - TRACE("writeFile(%s)", filename); + TRACE("writeFileBin(%s)", filename); FIL file; unsigned char buf[8]; @@ -57,22 +57,32 @@ const char * writeFile(const char * filename, const uint8_t * data, uint16_t siz return NULL; } -const char * writeModel() +// TODO: move to "common" +const char * writeModelBin() { char path[256]; getModelPath(path, g_eeGeneral.currModelFilename); sdCheckAndCreateDirectory(MODELS_PATH); - return writeFile(path, (uint8_t *)&g_model, sizeof(g_model)); + return writeFileBin(path, (uint8_t *)&g_model, sizeof(g_model)); } -const char * openFile(const char * fullpath, FIL * file, uint16_t * size, uint8_t * version) +// TODO: move partly to common +const char * openFile(const char * fullpath, FIL * file) { FRESULT result = f_open(file, fullpath, FA_OPEN_EXISTING | FA_READ); if (result != FR_OK) { return SDCARD_ERROR(result); } + return nullptr; +} + +const char* openFileBin(const char * fullpath, FIL * file, uint16_t * size, uint8_t * version) +{ + const char* err = openFile(fullpath, file); + if (err) return err; + if (f_size(file) < 8) { f_close(file); return STR_INCOMPATIBLE; @@ -81,16 +91,15 @@ const char * openFile(const char * fullpath, FIL * file, uint16_t * size, uint8_ UINT read; char buf[8]; - result = f_read(file, (uint8_t *)buf, sizeof(buf), &read); + FRESULT result = f_read(file, (uint8_t *)buf, sizeof(buf), &read); if ((result != FR_OK) || (read != sizeof(buf))) { f_close(file); return SDCARD_ERROR(result); } - //TODO: move this code into some checkCompatibleFormat() - *version = (uint8_t)buf[4]; - if (*(uint32_t*)&buf[0] != OTX_FOURCC || *version < FIRST_CONV_EEPROM_VER || *version > EEPROM_VER || buf[5] != 'M') { + if (*(uint32_t *)&buf[0] != OTX_FOURCC || *version < FIRST_CONV_EEPROM_VER || + *version > EEPROM_VER || buf[5] != 'M') { f_close(file); return STR_INCOMPATIBLE; } @@ -99,7 +108,8 @@ const char * openFile(const char * fullpath, FIL * file, uint16_t * size, uint8_ return nullptr; } -const char * loadFile(const char * fullpath, uint8_t * data, uint16_t maxsize, uint8_t * version) +static const char *loadFile(const char *fullpath, uint8_t *data, + uint16_t maxsize, uint8_t *version) { FIL file; UINT read; @@ -107,7 +117,7 @@ const char * loadFile(const char * fullpath, uint8_t * data, uint16_t maxsize, u TRACE("loadFile(%s)", fullpath); - const char * err = openFile(fullpath, &file, &size, version); + const char * err = openFileBin(fullpath, &file, &size, version); if (err) return err; @@ -122,7 +132,7 @@ const char * loadFile(const char * fullpath, uint8_t * data, uint16_t maxsize, u return nullptr; } -const char * readModel(const char * filename, uint8_t * buffer, uint32_t size, uint8_t * version) +const char * readModelBin(const char * filename, uint8_t * buffer, uint32_t size, uint8_t * version) { char path[256]; getModelPath(path, filename); @@ -130,7 +140,7 @@ const char * readModel(const char * filename, uint8_t * buffer, uint32_t size, u } -const char * loadRadioSettings(const char * path) +const char * loadRadioSettingsBin(const char * path) { uint8_t version; const char * error = loadFile(path, (uint8_t *)&g_eeGeneral, sizeof(g_eeGeneral), &version); @@ -150,17 +160,17 @@ const char * loadRadioSettings(const char * path) return nullptr; } -const char * loadRadioSettings() +const char * loadRadioSettingsBin() { - return loadRadioSettings(RADIO_SETTINGS_PATH); + return loadRadioSettingsBin(RADIO_SETTINGS_PATH); } -const char * writeGeneralSettings() +const char * writeGeneralSettingsBin() { - return writeFile(RADIO_SETTINGS_PATH, (uint8_t *)&g_eeGeneral, sizeof(g_eeGeneral)); + return writeFileBin(RADIO_SETTINGS_PATH, (uint8_t *)&g_eeGeneral, sizeof(g_eeGeneral)); } -void storageCreateModelsList() +void storageCreateModelsListBin() { FIL file; diff --git a/radio/src/storage/sdcard_raw.h b/radio/src/storage/sdcard_raw.h index 1a7c02831..7ac88873b 100644 --- a/radio/src/storage/sdcard_raw.h +++ b/radio/src/storage/sdcard_raw.h @@ -24,6 +24,6 @@ #include "FatFs/ff.h" -const char * openFile(const char * fullpath, FIL* file, uint16_t* size); +//const char * openFileBin(const char * fullpath, FIL* file, uint16_t* size); #endif