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
+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;