chore(cpn): remove binary file format models and settings support (#5653)

This commit is contained in:
Neil Horne
2024-11-22 10:27:19 +10:00
committed by GitHub
parent d94bc3321c
commit ebb5d39a14
37 changed files with 101 additions and 9548 deletions
-3
View File
@@ -4,17 +4,14 @@ project(storage)
set(${PROJECT_NAME}_NAMES
hexinterface
mountlist
rlefile
appdata
firmwareinterface
storage
bineeprom
eepe
hexeeprom
labeled
sdcard
etx
otx
yaml
crc
minizinterface
+16 -165
View File
@@ -25,159 +25,20 @@
#include <regex>
bool LabelsStorageFormat::load(RadioData & radioData)
{
StorageType st = getStorageType(filename);
if (st == STORAGE_TYPE_UNKNOWN) {
st = probeFormat();
}
if (st == STORAGE_TYPE_ETX || st == STORAGE_TYPE_YML
|| st == STORAGE_TYPE_UNKNOWN) {
return loadYaml(radioData);
} else {
return loadBin(radioData);
}
}
bool LabelsStorageFormat::write(const RadioData & radioData)
{
StorageType st = getStorageType(filename);
if (st == STORAGE_TYPE_ETX || st == STORAGE_TYPE_YML ||
st == STORAGE_TYPE_UNKNOWN) {
return writeYaml(radioData);
}
return false;
}
StorageType LabelsStorageFormat::probeFormat()
{
if (QFile(filename + "/RADIO/radio.yml").exists()) // converted
return getStorageType("radio.yml");
else if (QFile(filename + "/RADIO/radio.bin").exists()) // unconverted
return getStorageType("radio.bin");
else
return getStorageType(filename);
}
bool LabelsStorageFormat::loadBin(RadioData & radioData)
bool LabelsStorageFormat::load(RadioData & radioData)
{
QByteArray radioSettingsBuffer;
if (!loadFile(radioSettingsBuffer, "RADIO/radio.bin")) {
setError(tr("Cannot extract RADIO/radio.bin"));
return false;
}
StorageType st = getStorageType(filename);
if (st == STORAGE_TYPE_UNKNOWN)
st = probeFormat();
OpenTxEepromInterface * loadInterface = loadRadioSettingsFromByteArray(radioData.generalSettings, radioSettingsBuffer);
if (!loadInterface) {
return false;
}
board = loadInterface->getBoard();
radioData.generalSettings.convertLegacyConfiguration(board);
QByteArray modelsListBuffer;
if (!loadFile(modelsListBuffer, "RADIO/models.txt")) {
setError(tr("Cannot extract RADIO/models.txt"));
return false;
}
QList<QByteArray> lines = modelsListBuffer.split('\n');
int modelIndex = 0;
foreach (const QByteArray & lineArray, lines) {
QString line = QString(lineArray).trimmed();
if (line.isEmpty()) continue;
// qDebug() << "parsing line" << line;
if (line.startsWith('[') && line.endsWith(']')) {
continue;
}
// determine if we have a model number
QStringList parts = line.split(QRegExp("\\s+"), Qt::SkipEmptyParts);
if (parts.size() == 2) {
// parse model number
int modelNumber = parts[0].toInt();
if (modelNumber > 0 && modelNumber > modelIndex && modelNumber < getCurrentFirmware()->getCapability(Models)) {
modelIndex = modelNumber;
qDebug() << "advanced model number to" << modelIndex;
}
else {
if (modelNumber != modelIndex) qDebug() << "Invalid model number" << parts[0];
}
parts.removeFirst();
}
if (parts.size() == 1) {
// parse model file name and load
QString fileName = parts[0];
qDebug() << "Loading model from file" << fileName << "into slot" << modelIndex;
QByteArray modelBuffer;
if (!loadFile(modelBuffer, QString("MODELS/%1").arg(fileName))) {
setError(tr("Cannot extract %1").arg(fileName));
return false;
}
if ((int)radioData.models.size() <= modelIndex) {
radioData.models.resize(modelIndex + 1);
}
if (!loadModelFromByteArray(radioData.models[modelIndex], modelBuffer)) {
setError(tr("Error loading models"));
return false;
}
strncpy(radioData.models[modelIndex].filename, qPrintable(fileName), sizeof(radioData.models[modelIndex].filename));
if (IS_FAMILY_HORUS_OR_T16(board) && !strcmp(radioData.generalSettings.currModelFilename, qPrintable(fileName))) {
radioData.generalSettings.currModelIndex = modelIndex;
qDebug() << "currModelIndex =" << modelIndex;
}
radioData.models[modelIndex].used = true;
modelIndex++;
continue;
}
// invalid line
// TODO add to parsing report
qDebug() << "Invalid line" <<line;
continue;
}
// Add a Favorites label
if(getCurrentFirmware()->getCapability(HasModelLabels)) {
RadioData::LabelData ld = { tr("Favorites"), false };
radioData.labels.append(ld);
}
return true;
}
bool LabelsStorageFormat::writeBin(const RadioData & radioData)
{
QByteArray radioSettingsData; // radio.bin
size_t numModels = radioData.models.size();
size_t numCategories = radioData.labels.size();
std::vector<std::vector<QString>> sortedModels(numCategories);
writeRadioSettingsToByteArray(radioData.generalSettings, radioSettingsData);
if (!writeFile(radioSettingsData, "RADIO/radio.bin")) {
return false;
}
for (size_t m=0; m<numModels; m++) {
const ModelData & model = radioData.models[m];
if (model.isEmpty()) continue;
QString modelFilename = QString("MODELS/%1").arg(model.filename);
QByteArray modelData;
writeModelToByteArray(model, modelData);
if (!writeFile(modelData, modelFilename)) {
return false;
}
}
return true;
}
bool LabelsStorageFormat::loadYaml(RadioData & radioData)
{
if (getStorageType(filename) == STORAGE_TYPE_UNKNOWN && probeFormat() == STORAGE_TYPE_ETX) {
if (!QFile(filename + "/" + "RADIO/radio.yml").exists())
qDebug() << tr("Cannot find %1/RADIO/radio.yml").arg(filename);
@@ -238,11 +99,11 @@ bool LabelsStorageFormat::loadYaml(RadioData & radioData)
// Scan for all models
std::list<std::string> filelist;
if (!getFileList(filelist)) {
if (!getFileList(filelist))
return false;
}
const std::regex yml_regex("MODELS/(model([0-9]+)\\.yml)");
for(const auto& f : filelist) {
std::smatch match;
if (std::regex_match(f, match, yml_regex)) {
@@ -302,10 +163,8 @@ bool LabelsStorageFormat::loadYaml(RadioData & radioData)
model.modelIndex = modelIdx;
strncpy(model.filename, mc.filename.c_str(), sizeof(model.filename)-1);
if (hasLabels && !strncmp(radioData.generalSettings.currModelFilename,
model.filename, sizeof(model.filename))) {
if (hasLabels && !strncmp(radioData.generalSettings.currModelFilename, model.filename, sizeof(model.filename)))
radioData.generalSettings.currModelIndex = modelIdx;
}
model.used = true;
modelIdx++;
@@ -325,16 +184,14 @@ bool LabelsStorageFormat::loadYaml(RadioData & radioData)
return true;
}
bool LabelsStorageFormat::writeYaml(const RadioData & radioData)
bool LabelsStorageFormat::write(const RadioData & radioData)
{
QByteArray radioSettingsBuffer;
if (!writeRadioSettingsToYaml(radioData.generalSettings, radioSettingsBuffer)) {
if (!writeRadioSettingsToYaml(radioData.generalSettings, radioSettingsBuffer))
return false;
}
if (!writeFile(radioSettingsBuffer, "RADIO/radio.yml")) {
if (!writeFile(radioSettingsBuffer, "RADIO/radio.yml"))
return false;
}
bool hasLabels = getCurrentFirmware()->getCapability(HasModelLabels);
@@ -347,7 +204,7 @@ bool LabelsStorageFormat::writeYaml(const RadioData & radioData)
// Delete all old modelxx.yml from radio MODELS folder before writing new modelxx.yml files
const std::regex yml_regex("MODELS/(model([0-9s]+)\\.yml)");
for(const auto& f : filelist) {
for (const auto& f : filelist) {
std::smatch match;
if (std::regex_match(f, match, yml_regex)) {
if (match.size() == 3) {
@@ -368,28 +225,22 @@ bool LabelsStorageFormat::writeYaml(const RadioData & radioData)
QString modelFilename;
if (hasLabels) {
std::string ymlFilename = patchFilenameToYaml(model.filename);
modelFilename =
QString("MODELS/%1").arg(QString::fromStdString(ymlFilename));
modelFiles.push_back(
{ymlFilename, std::string(model.name)});
modelFilename = QString("MODELS/%1").arg(QString::fromStdString(ymlFilename));
modelFiles.push_back({ymlFilename, std::string(model.name)});
} else {
modelFilename = QString("MODELS/model%1.yml")
.arg(model.modelIndex, 2, 10, QLatin1Char('0'));
modelFilename = QString("MODELS/model%1.yml").arg(model.modelIndex, 2, 10, QLatin1Char('0'));
}
QByteArray modelData;
writeModelToYaml(model, modelData);
if (!writeFile(modelData, modelFilename)) {
if (!writeFile(modelData, modelFilename))
return false;
}
}
if (hasLabels) {
QByteArray labelsListBuffer;
if (!writeLabelsListToYaml(radioData, labelsListBuffer)
|| !writeFile(labelsListBuffer, "MODELS/labels.yml")) {
if (!writeLabelsListToYaml(radioData, labelsListBuffer) || !writeFile(labelsListBuffer, "MODELS/labels.yml"))
return false;
}
}
return true;
-5
View File
@@ -47,10 +47,5 @@ class LabelsStorageFormat : public StorageFormat
virtual bool getFileList(std::list<std::string>& filelist) = 0;
virtual bool deleteFile(const QString & fileName) = 0;
virtual bool loadBin(RadioData & radioData);
virtual bool writeBin(const RadioData & radioData);
virtual bool loadYaml(RadioData & radioData);
virtual bool writeYaml(const RadioData & radioData);
StorageType probeFormat();
};
-116
View File
@@ -1,116 +0,0 @@
/*
* 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 "otx.h"
#include <QFile>
#define MZ_ALLOCATION_SIZE (32*1024)
bool OtxFormat::load(RadioData & radioData)
{
QFile file(filename);
if (!file.open(QFile::ReadOnly)) {
setError(tr("Error opening file %1:\n%2.").arg(filename).arg(file.errorString()));
return false;
}
QByteArray archiveContents = file.readAll();
qDebug() << "File" << filename << "read, size:" << archiveContents.size();
// open zip file
memset(&zip_archive, 0, sizeof(zip_archive));
if (!mz_zip_reader_init_mem(&zip_archive, archiveContents.data(), archiveContents.size(), 0)) {
qDebug() << tr("Error opening OpenTX archive %1").arg(filename);
return false;
}
bool result = LabelsStorageFormat::load(radioData);
mz_zip_reader_end(&zip_archive);
return result;
}
bool OtxFormat::write(const RadioData & radioData)
{
qDebug() << "Saving to archive" << filename;
memset(&zip_archive, 0, sizeof(zip_archive));
if (!mz_zip_writer_init_heap(&zip_archive, 0, MZ_ALLOCATION_SIZE)) {
setError(tr("Error initializing OpenTX archive writer"));
return false;
}
bool result = LabelsStorageFormat::write(radioData);
if (result) {
// finalize archive and get contents
char * archiveContents;
size_t archiveSize;
if (mz_zip_writer_finalize_heap_archive(&zip_archive, (void **)&archiveContents, &archiveSize)) {
qDebug() << "Archive size" << archiveSize;
// write contents to file
QFile file(filename);
if (file.open(QIODevice::WriteOnly)) {
qint64 len = file.write(archiveContents, archiveSize);
if (len != (qint64)archiveSize) {
setError(tr("Error writing file %1:\n%2.").arg(filename).arg(file.errorString()));
result = false;
}
}
else {
setError(tr("Error creating OpenTX file %1:\n%2.").arg(filename).arg(file.errorString()));
result = false;
}
}
else {
setError(tr("Error creating OpenTX archive"));
result = false;
}
}
mz_zip_writer_end(&zip_archive);
return result;
}
bool OtxFormat::loadFile(QByteArray & filedata, const QString & filename)
{
size_t size;
void * data = mz_zip_reader_extract_file_to_heap(&zip_archive, qPrintable(filename), &size, 0);
if (!data) {
return false;
}
qDebug() << QString("Extracted file %1, size=%2").arg(filename).arg(size);
filedata.clear();
filedata.append((char *)data, size);
mz_free(data);
return true;
}
bool OtxFormat::writeFile(const QByteArray & filedata, const QString & filename)
{
if (!mz_zip_writer_add_mem(&zip_archive, filename.toStdString().c_str(), filedata.data(), filedata.size(), MZ_DEFAULT_LEVEL)) {
setError(tr("Error adding %1 to OpenTX archive").arg(filename));
return false;
}
return true;
}
-49
View File
@@ -1,49 +0,0 @@
/*
* 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
#include "labeled.h"
#include <QtCore>
class OtxFormat : public LabelsStorageFormat
{
Q_DECLARE_TR_FUNCTIONS(OtxFormat)
public:
OtxFormat(const QString & filename):
LabelsStorageFormat(filename)
{
}
virtual QString name() { return "otx"; }
virtual bool load(RadioData & radioData);
virtual bool write(const RadioData & radioData);
protected:
virtual bool loadFile(QByteArray & fileData, const QString & fileName);
virtual bool writeFile(const QByteArray & fileData, const QString & fileName);
virtual bool getFileList(std::list<std::string>& filelist) { return false; }
virtual bool deleteFile(const QString & fileName) { return false; }
mz_zip_archive zip_archive;
};
+1 -4
View File
@@ -19,8 +19,7 @@
* GNU General Public License for more details.
*/
#ifndef _SDCARD_H2_
#define _SDCARD_H2_
#pragma once
#include "labeled.h"
@@ -56,5 +55,3 @@ class SdcardStorageFactory : public DefaultStorageFactory<SdcardFormat>
virtual bool probe(const QString & name);
};
#endif // _SDCARD_H2_
+7 -99
View File
@@ -19,32 +19,17 @@
* GNU General Public License for more details.
*/
#include "bineeprom.h"
#include "eepe.h"
#include "otx.h"
#include "etx.h"
#include "sdcard.h"
#include "yaml.h"
#include "firmwareinterface.h"
#include "eeprominterface.h"
#include "sdcard.h"
#include <QFileInfo>
StorageType getStorageType(const QString & filename)
{
QString suffix = QFileInfo(filename).suffix().toUpper();
if (suffix == "HEX")
return STORAGE_TYPE_HEX;
else if (suffix == "BIN")
return STORAGE_TYPE_BIN;
else if (suffix == "EEPM")
return STORAGE_TYPE_EEPM;
else if (suffix == "EEPE")
return STORAGE_TYPE_EEPE;
else if (suffix == "XML")
return STORAGE_TYPE_XML;
else if (suffix == "OTX")
return STORAGE_TYPE_OTX;
else if (suffix == "ETX")
if (suffix == "ETX")
return STORAGE_TYPE_ETX;
else if (suffix == "YML")
return STORAGE_TYPE_YML;
@@ -64,10 +49,6 @@ void registerStorageFactory(StorageFactory * factory)
void registerStorageFactories()
{
registerStorageFactory(new DefaultStorageFactory<BinEepromFormat>("bin"));
registerStorageFactory(new DefaultStorageFactory<EepeFormat>("eepe"));
registerStorageFactory(new DefaultStorageFactory<HexEepromFormat>("hex"));
registerStorageFactory(new DefaultStorageFactory<OtxFormat>("otx"));
registerStorageFactory(new DefaultStorageFactory<EtxFormat>("etx"));
registerStorageFactory(new DefaultStorageFactory<YamlFormat>("yml"));
registerStorageFactory(new SdcardStorageFactory());
@@ -88,7 +69,7 @@ bool Storage::load(RadioData & radioData)
}
bool ret = false;
foreach(StorageFactory * factory, registeredStorageFactories) {
foreach (StorageFactory * factory, registeredStorageFactories) {
if (factory->probe(filename)) {
StorageFormat * format = factory->instance(filename);
if (format->load(radioData)) {
@@ -111,7 +92,7 @@ bool Storage::load(RadioData & radioData)
bool Storage::write(const RadioData & radioData)
{
bool ret = false;
foreach(StorageFactory * factory, registeredStorageFactories) {
foreach (StorageFactory * factory, registeredStorageFactories) {
if (factory->probe(filename)) {
StorageFormat * format = factory->instance(filename);
ret = format->write(radioData);
@@ -125,7 +106,7 @@ bool Storage::write(const RadioData & radioData)
bool Storage::writeModel(const RadioData & radioData, const int modelIndex)
{
bool ret = false;
foreach(StorageFactory * factory, registeredStorageFactories) {
foreach (StorageFactory * factory, registeredStorageFactories) {
if (factory->probe(filename)) {
StorageFormat * format = factory->instance(filename);
ret = format->writeModel(radioData, modelIndex);
@@ -135,76 +116,3 @@ bool Storage::writeModel(const RadioData & radioData, const int modelIndex)
}
return ret;
}
bool convertEEprom(const QString & sourceEEprom, const QString & destinationEEprom, const QString & firmwareFilename)
{
FirmwareInterface firmware(firmwareFilename);
if (!firmware.isValid())
return false;
uint8_t version = firmware.getEEpromVersion();
unsigned int variant = firmware.getEEpromVariant();
QSharedPointer<RadioData> radioData = QSharedPointer<RadioData>(new RadioData());
Storage storage(sourceEEprom);
if (!storage.load(*radioData))
return false;
QByteArray eeprom(Boards::getEEpromSize(Board::BOARD_UNKNOWN), 0);
int size = getCurrentEEpromInterface()->save((uint8_t *)eeprom.data(), *radioData, version, variant);
if (size == 0) {
return false;
}
QFile destinationFile(destinationEEprom);
if (!destinationFile.open(QIODevice::WriteOnly)) {
return false;
}
int result = destinationFile.write(eeprom.constData(), size);
destinationFile.close();
return (result == size);
}
#if 0
unsigned long LoadBackup(RadioData & radioData, uint8_t * eeprom, int size, int index)
{
std::bitset<NUM_ERRORS> errors;
foreach(EEPROMInterface *eepromInterface, eepromInterfaces) {
std::bitset<NUM_ERRORS> result((unsigned long long)eepromInterface->loadBackup(radioData, eeprom, size, index));
if (result.test(ALL_OK)) {
return result.to_ulong();
}
else {
errors |= result;
}
}
if (errors.none()) {
errors.set(UNKNOWN_ERROR);
}
return errors.to_ulong();
}
unsigned long LoadEepromXml(RadioData & radioData, QDomDocument & doc)
{
std::bitset<NUM_ERRORS> errors;
foreach(EEPROMInterface *eepromInterface, eepromInterfaces) {
std::bitset<NUM_ERRORS> result((unsigned long long)eepromInterface->loadxml(radioData, doc));
if (result.test(ALL_OK)) {
return result.to_ulong();
}
else {
errors |= result;
}
}
if (errors.none()) {
errors.set(UNKNOWN_ERROR);
}
return errors.to_ulong();
}
#endif
+2 -18
View File
@@ -19,8 +19,7 @@
* GNU General Public License for more details.
*/
#ifndef _STORAGE_H_
#define _STORAGE_H_
#pragma once
#include "radiodata.h"
@@ -31,13 +30,8 @@
enum StorageType
{
STORAGE_TYPE_UNKNOWN,
STORAGE_TYPE_BIN,
STORAGE_TYPE_HEX,
STORAGE_TYPE_EEPE,
STORAGE_TYPE_EEPM,
STORAGE_TYPE_XML,
STORAGE_TYPE_HEX, // needed for FirmwareInterface
STORAGE_TYPE_SDCARD,
STORAGE_TYPE_OTX,
STORAGE_TYPE_ETX,
STORAGE_TYPE_YML
};
@@ -164,13 +158,3 @@ class Storage : public StorageFormat
void registerStorageFactories();
void unregisterStorageFactories();
#if 0
unsigned long LoadBackup(RadioData &radioData, uint8_t *eeprom, int esize, int index);
unsigned long LoadEeprom(RadioData &radioData, const uint8_t *eeprom, int size);
unsigned long LoadEepromXml(RadioData &radioData, QDomDocument &doc);
#endif
bool convertEEprom(const QString & sourceEEprom, const QString & destinationEEprom, const QString & firmware);
#endif // _STORAGE_H_