WiP storage format conversions

This commit is contained in:
Raphael Coeffic
2021-11-10 11:07:01 +01:00
parent 0503e6fb8e
commit 8d04e373b6
12 changed files with 412 additions and 97 deletions
@@ -6,6 +6,7 @@ if(STORAGE_CONVERSIONS)
${SRC}
storage/conversions/conversions.cpp
storage/conversions/conversions_219_220.cpp
storage/conversions/conversions_220_221.cpp
)
include(storage/conversions/yaml/CMakeLists.txt)
+56 -27
View File
@@ -54,9 +54,9 @@ static void drawProgressScreen(const char* filename, int progress, int total)
#endif
}
void convertRadioData(int version)
void convertBinRadioData(const char * path, int version)
{
TRACE("convertRadioData(%d)", version);
TRACE("convertRadioData(%s,%d)", path, version);
#if defined(COLORLCD)
// the theme has not been loaded before
@@ -71,6 +71,7 @@ void convertRadioData(int version)
AU_NONE);
// Load models list before converting
// TODO: force old .txt format
modelslist.load();
unsigned converted = 0;
@@ -81,13 +82,18 @@ void convertRadioData(int version)
#if STORAGE_CONVERSIONS < 220
if (version == 219) {
convertRadioData_219_to_220(g_eeGeneral);
storageDirty(EE_GENERAL);
// TODO: alloc specific structure for destination
convertRadioData_219_to_220(path);
}
#endif
#if STORAGE_CONVERSIONS < 220
if (version == 220) {
convertRadioData_220_to_221(path);
// g_eeGeneral.version = EEPROM_VER;
// storageDirty(EE_GENERAL);
// storageCheck(true);
}
#endif
g_eeGeneral.version = EEPROM_VER;
storageDirty(EE_GENERAL);
storageCheck(true);
converted++;
#if defined(SIMU)
@@ -107,17 +113,20 @@ void convertRadioData(int version)
error = readModelBin(filename, (uint8_t *)&g_model, sizeof(g_model), &model_version);
if (!error) {
convertModelData(model_version);
// TODO: error handling
convertBinModelData(filename, model_version);
#if defined(SDCARD_YAML)
//TODO: convert filename & output to YAML directly
// patch model filename
const char* ext = strrchr(filename, '.');
if ((ext != nullptr) && !strncmp(ext, MODELS_EXT, 4)) {
memcpy((void*)ext, (void*)YAML_EXT, sizeof(YAML_EXT) - 1);
}
// in case it's the current model, patch it as well
if (!strncmp(filename, g_eeGeneral.currModelFilename, LEN_MODEL_FILENAME)) {
strncpy(g_eeGeneral.currModelFilename, filename, LEN_MODEL_FILENAME+1);
}
// and write to YAML file
error = writeModelYaml(filename);
#else
char path[256];
@@ -146,34 +155,47 @@ void convertRadioData(int version)
modelslist.clear();
modelslist.load();
}
#endif
void convertModelData(int version)
void convertBinModelData(const char* filename, int version)
{
TRACE("convertModelData(%d)", version);
TRACE("convertModelData(%s,%d)", filename);
#if STORAGE_CONVERSIONS < 220
if (version == 219) {
version = 219;
convertModelData_219_to_220(g_model);
convertModelData_219_to_220(filename);
version = 220;
}
#endif
#if STORAGE_CONVERSIONS < 221
if (version == 220) {
convertModelData_220_to_221(filename);
version = 221;
}
#endif
// TODO: error handling
}
#endif
#if defined(EEPROM) || defined(EEPROM_RLC)
#include "storage/eeprom_common.h"
void eeConvertModel(int id, int version)
{
eeLoadModelData(id);
convertModelData(version);
uint8_t currModel = g_eeGeneral.currModel;
g_eeGeneral.currModel = id;
storageDirty(EE_MODEL);
storageCheck(true);
g_eeGeneral.currModel = currModel;
TRACE("eeConvertModel(%d,%d)", id, version);
#if STORAGE_CONVERSIONS < 220
if (version == 219) {
convertModelData_219_to_220(id);
version = 220;
}
#endif
#if STORAGE_CONVERSIONS < 221
if (version == 220) {
convertModelData_220_to_221(id);
version = 221;
}
#endif
// TODO: error handling
}
bool eeConvert()
@@ -209,12 +231,19 @@ bool eeConvert()
#if STORAGE_CONVERSIONS < 220
if (version == 219) {
version = 220;
convertRadioData_219_to_220(g_eeGeneral);
convertRadioData_219_to_220();
}
#endif
#if STORAGE_CONVERSIONS < 221
if (version == 220) {
version = 221;
convertRadioData_220_to_221();
}
#endif
//TODO: reload from YAML
g_eeGeneral.version = EEPROM_VER;
storageDirty(EE_GENERAL);
storageCheck(true);
// storageDirty(EE_GENERAL);
// storageCheck(true);
#if defined(STORAGE_MODELSLIST)
modelslist.clear();
+15 -4
View File
@@ -19,8 +19,9 @@
* GNU General Public License for more details.
*/
void convertRadioData(int version);
void convertModelData(int version);
// Entry points for converting radio & model data
void convertBinRadioData(const char * path, int version);
void convertBinModelData(const char * path, int version);
bool eeConvert();
void eeConvertModel(int id, int version);
@@ -39,5 +40,15 @@ void eeConvertModel(int id, int version);
// Conversions 219 to 220
void convertModelData_219_to_220(ModelData &model);
void convertRadioData_219_to_220(RadioData &settings);
const char* convertModelData_219_to_220(uint8_t id);
const char* convertModelData_219_to_220(const char* filename);
const char* convertRadioData_219_to_220();
const char* convertRadioData_219_to_220(const char* path);
// Conversions 220 to 221
const char* convertModelData_220_to_221(uint8_t id);
const char* convertModelData_220_to_221(const char* filename);
const char* convertRadioData_220_to_221();
const char* convertRadioData_220_to_221(const char* path);
@@ -21,11 +21,16 @@
#include "opentx.h"
#include "datastructs_219.h"
#include "datastructs_220.h"
#if defined(COLORLCD)
#include "theme.h"
#endif
#if defined(EEPROM) || defined(EEPROM_RLC)
#include <storage/eeprom_common.h>
#endif
//
// Summary of all changes between 219 & 220
//
@@ -99,18 +104,21 @@ extern const LayoutFactory * defaultLayout;
extern OpenTxTheme * defaultTheme;
#endif
void convertModelData_219_to_220(ModelData &model)
void convertModelData_219_to_220(void* data)
{
ModelData* oldModelAllocated = (ModelData*)malloc(sizeof(ModelData));
ModelData& oldModel = *oldModelAllocated;
constexpr unsigned md_size = sizeof(bin_storage_220::ModelData);
auto& model = *reinterpret_cast<bin_storage_220::ModelData*>(data);
memcpy(&oldModel, &model, sizeof(ModelData));
ModelData& newModel = (ModelData&)model;
auto oldModelAllocated = reinterpret_cast<bin_storage_220::ModelData*>(malloc(md_size));
bin_storage_220::ModelData& oldModel = *oldModelAllocated;
memcpy(&oldModel, &model, md_size);
bin_storage_220::ModelData& newModel = (bin_storage_220::ModelData&)model;
convertToStr(model.header.name, LEN_MODEL_NAME_219);
for (uint8_t i=0; i<MAX_TIMERS_219; i++) {
// convert to new timers v220
TimerData& timer = newModel.timers[i];
bin_storage_220::TimerData& timer = newModel.timers[i];
convertToStr(timer.name, LEN_TIMER_NAME_219);
TimerData_v219& timer_219 = *(TimerData_v219*)(&oldModel.timers[i]);
@@ -212,29 +220,88 @@ void convertModelData_219_to_220(ModelData &model)
sizeof(newModel.topbarData));
if (defaultLayout) {
strcpy(newModel.screenData[0].LayoutId, defaultLayout->getId());
defaultLayout->initPersistentData(&newModel.screenData[0].layoutData, true);
strcpy(newModel.screenData[0].LayoutId, "Layout2P1");
// TODO: copy the raw structure for the default layout in version 220
//defaultLayout->initPersistentData(&newModel.screenData[0].layoutData, true);
auto persistentData = &newModel.screenData[0].layoutData;
// top bar
persistentData->options[0].type = bin_storage_220::ZOV_Bool;
persistentData->options[0].value.boolValue = true;
// flight mode
persistentData->options[1].type = bin_storage_220::ZOV_Bool;
persistentData->options[1].value.boolValue = true;
// sliders
persistentData->options[2].type = bin_storage_220::ZOV_Bool;
persistentData->options[2].value.boolValue = true;
// trims
persistentData->options[3].type = bin_storage_220::ZOV_Bool;
persistentData->options[3].value.boolValue = true;
// mirrored
persistentData->options[4].type = bin_storage_220::ZOV_Bool;
persistentData->options[4].value.boolValue = false;
}
#endif
free(oldModelAllocated);
}
void convertRadioData_219_to_220(RadioData & settings)
#if !defined(EEPROM_RLC)
const char* convertModelData_219_to_220(const char* path)
{
constexpr unsigned md_size = sizeof(bin_storage_220::ModelData);
auto md_220 = reinterpret_cast<bin_storage_220::ModelData*>(malloc(md_size));
memset(md_220, 0, md_size);
uint8_t version;
const char* error = loadFileBin(path, (uint8_t *)md_220, md_size, &version);
if (!error) {
convertModelData_219_to_220((void*)md_220);
error = writeFileBin(path, (uint8_t *)md_220, md_size, 220);
}
free(md_220);
return error;
}
#else
const char* convertModelData_219_to_220(uint8_t id)
{
constexpr unsigned md_size = sizeof(bin_storage_220::ModelData);
auto md_220 = reinterpret_cast<uint8_t*>(malloc(md_size));
memset(md_220, 0, md_size);
uint16_t read = eeLoadModelData(id, md_220, md_size);
if (read == md_size) {
convertModelData_219_to_220((void*)md_220);
eeWriteModelData(id, (uint8_t *)md_220, md_size, true);
} else {
// ERROR: size mismatch !!!
}
free(md_220);
return nullptr;
}
#endif
void convertRadioData_219_to_220(void* data)
{
TRACE("Radio conversion from v219 to v220");
auto& settings = *reinterpret_cast<bin_storage_220::RadioData*>(data);
settings.version = 220;
settings.variant = EEPROM_VARIANT;
RadioData* oldSettingsAllocated = (RadioData*)malloc(sizeof(RadioData));
RadioData& oldSettings = *oldSettingsAllocated;
memcpy(&oldSettings, &settings, sizeof(RadioData));
#if defined(COLORLCD)
// Clear CustomScreenData + TopBarPersistentData
// as they cannot be converted (missing option types)
strcpy(g_eeGeneral.themeName, defaultTheme->getName());
defaultTheme->init();
strcpy(settings.themeName, defaultTheme->getName());
defaultTheme->init(); // TODO: just dump the data into the settings
#endif
for (int i = 0; i < NUM_SWITCHES_219; ++i) {
@@ -250,6 +317,41 @@ void convertRadioData_219_to_220(RadioData & settings)
#if defined(PCBHORUS) || defined(PCBNV14)
convertToStr(settings.bluetoothName, LEN_BLUETOOTH_NAME_219);
#endif
free(oldSettingsAllocated);
}
#if !defined(EEPROM_RLC)
const char* convertRadioData_219_to_220(const char* path)
{
constexpr unsigned rd_size = sizeof(bin_storage_220::RadioData);
auto rd_220 = reinterpret_cast<bin_storage_220::RadioData*>(malloc(rd_size));
memset(rd_220, 0, rd_size);
uint8_t version;
const char* error = loadFileBin(path, (uint8_t *)rd_220, rd_size, &version);
if (!error) {
convertRadioData_219_to_220((void*)rd_220);
error = writeFileBin(path, (uint8_t *)rd_220, rd_size, 220);
}
free(rd_220);
return error;
}
#else
const char* convertRadioData_219_to_220()
{
constexpr unsigned rd_size = sizeof(bin_storage_220::RadioData);
auto rd_220 = reinterpret_cast<uint8_t*>(malloc(rd_size));
uint16_t read = eeLoadGeneralSettingsData(rd_220, rd_size);
if (read == rd_size) {
convertRadioData_219_to_220((void*)rd_220);
eeWriteGeneralSettingData((uint8_t *)rd_220, rd_size, true);
} else {
// ERROR: size mismatch !!!
}
free(rd_220);
return nullptr;
}
#endif
@@ -0,0 +1,117 @@
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program 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.
*/
#include "opentx.h"
#include "datastructs_220.h"
// see "yaml/yaml_datastructs.h"
namespace yaml_conv_220 {
const YamlNode* get_radiodata_nodes();
const YamlNode* get_modeldata_nodes();
const YamlNode* get_partialmodel_nodes();
};
#if !defined(EEPROM_RLC)
static const char* convertData_220_to_221(const char* path, unsigned size,
const YamlNode* root_node)
{
auto data = reinterpret_cast<uint8_t*>(malloc(size));
memset(data, 0, size);
uint8_t version;
const char* error = loadFileBin(path, data, size, &version);
if (!error) {
// TODO: patch file name into .yml
error = writeFileYaml(path, root_node, data);
}
free(data);
return error;
}
const char* convertModelData_220_to_221(const char* path)
{
constexpr unsigned md_size = sizeof(bin_storage_220::ModelData);
return convertData_220_to_221(path, md_size, yaml_conv_220::get_modeldata_nodes());
}
const char* convertRadioData_220_to_221(const char* path)
{
constexpr unsigned rd_size = sizeof(bin_storage_220::RadioData);
return convertData_220_to_221(path, rd_size, yaml_conv_220::get_radiodata_nodes());
}
#else
#include <storage/eeprom_common.h>
constexpr uint8_t MODELIDX_STRLEN = sizeof(MODEL_FILENAME_PREFIX "00");
static void getModelNumberStr(uint8_t idx, char* model_idx)
{
memcpy(model_idx, MODEL_FILENAME_PREFIX, sizeof(MODEL_FILENAME_PREFIX));
model_idx[sizeof(MODEL_FILENAME_PREFIX)-1] = '0' + idx / 10;
model_idx[sizeof(MODEL_FILENAME_PREFIX)] = '0' + idx % 10;
model_idx[sizeof(MODEL_FILENAME_PREFIX)+1] = '\0';
}
const char* convertModelData_220_to_221(uint8_t id)
{
constexpr unsigned size = sizeof(bin_storage_220::ModelData);
auto data = reinterpret_cast<uint8_t*>(malloc(size));
memset(data, 0, size);
const char* error = nullptr;
uint16_t read = eeLoadModelData(id, data, size);
if (read == size) {
char model_idx[MODELIDX_STRLEN + sizeof(YAML_EXT)];
getModelNumberStr(id, model_idx);
GET_FILENAME(path, MODELS_PATH, model_idx, YAML_EXT);
error = writeFileYaml(path, yaml_conv_220::get_modeldata_nodes(), data);
} else {
// ERROR: size mismatch !!!
}
free(data);
return error;
}
const char* convertRadioData_220_to_221()
{
constexpr unsigned size = sizeof(bin_storage_220::RadioData);
auto data = reinterpret_cast<uint8_t*>(malloc(size));
memset(data, 0, size);
const char* error = nullptr;
uint16_t read = eeLoadGeneralSettingsData(data, size);
if (read == size) {
error = writeFileYaml(RADIO_SETTINGS_YAML_PATH,
yaml_conv_220::get_radiodata_nodes(), data);
} else {
// ERROR: size mismatch !!!
}
free(data);
return error;
}
#endif
@@ -0,0 +1,30 @@
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program 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.
*/
#pragma once
namespace bin_storage_220 {
#include "yaml/dataconstants_220.h"
#include "yaml/datastructs_220.h"
#include "yaml/myeeprom_220.h"
};