[Companion] Work in progress - QTreeView in MdiChild (#4197)

[Companion] QTreeView in MdiChild, Horus and X7 support added, fixes for #4224
This commit is contained in:
Bertrand Songis
2017-01-15 17:45:57 +01:00
committed by GitHub
parent d6b84371a9
commit a65aa3d6c7
60 changed files with 3539 additions and 3153 deletions
+7 -3
View File
@@ -1,14 +1,18 @@
include_directories(${CMAKE_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR})
set(storage_NAMES
storage_eeprom
storage_sdcard
hexinterface
# xmlinterface
mountlist
rlefile
appdata
firmwareinterface
storage
bineeprom
eepe
hexeeprom
categorized
sdcard
otx
)
set(storage_SRCS
+103
View File
@@ -0,0 +1,103 @@
/*
* Copyright (C) OpenTX
*
* Based on code named
* 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 "bineeprom.h"
#include "eeprominterface.h"
#include <QFile>
#include <bitset>
// TODO should be RleFormat, RawFormat etc.
bool BinEepromFormat::load(RadioData & radioData)
{
QFile file(filename);
int size = file.size();
if (!file.open(QFile::ReadOnly)) {
qDebug() << "Unable to open" << filename << file.errorString();
return false;
}
QByteArray eeprom(size, 0);
int result = file.read((char *)eeprom.data(), size);
if (result != size) {
setError(QObject::tr("Error reading %1: %2").arg(filename).arg(file.errorString()));
return false;
}
return extract(radioData, eeprom);
}
bool BinEepromFormat::write(const RadioData & radioData)
{
bool result;
EEPROMInterface * eepromInterface = GetEepromInterface();
uint8_t * eeprom = (uint8_t *)malloc(getEEpromSize(eepromInterface->getBoard()));
int eeprom_size = eepromInterface->save(eeprom, radioData, 0, GetCurrentFirmware()->getVariantNumber());
if (eeprom_size) {
result = writeToFile(eeprom, eeprom_size);
}
else {
setError(QObject::tr("Cannot save EEPROM"));
result = false;
}
free(eeprom);
return result;
}
bool BinEepromFormat::writeToFile(const uint8_t * eeprom, uint32_t size)
{
QFile file(filename);
if (!file.open(QIODevice::WriteOnly)) {
setError(QObject::tr("Cannot open file %1:\n%2.").arg(filename).arg(file.errorString()));
return false;
}
QTextStream outputStream(&file);
long len = file.write((char *)eeprom, size);
if (len != size) {
setError(QObject::tr("Error writing file %1:\n%2.").arg(filename).arg(file.errorString()));
return false;
}
return true;
}
bool BinEepromFormat::extract(RadioData & radioData, const QByteArray & eeprom)
{
std::bitset<NUM_ERRORS> errors;
foreach(EEPROMInterface * eepromInterface, eepromInterfaces) {
std::bitset<NUM_ERRORS> result((unsigned long long)eepromInterface->load(radioData, (uint8_t *)eeprom.data(), eeprom.size()));
if (result.test(ALL_OK)) {
if (errors.test(HAS_WARNINGS)) {
// TODO ShowEepromWarnings(this, QObject::tr("Warning"), errors.to_ulong());
}
return true;
}
else {
errors |= result;
}
}
setError(QObject::tr("Invalid EEPROM File %1: %2").arg(filename).arg(errors.to_ulong()));
return false;
}
@@ -18,16 +18,25 @@
* GNU General Public License for more details.
*/
#ifndef _STORAGE_EEPROM_H_
#define _STORAGE_EEPROM_H_
#ifndef _BINEEPROM_H_
#define _BINEEPROM_H_
#include <QDomDocument>
#include <QString>
#include "eeprominterface.h"
#include "storage.h"
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);
bool convertEEprom(const QString &sourceEEprom, const QString &destinationEEprom, const QString &firmware);
class BinEepromFormat : public StorageFormat
{
public:
BinEepromFormat(const QString & filename):
StorageFormat(filename)
{
}
virtual bool load(RadioData & radioData);
virtual bool write(const RadioData & radioData);
protected:
bool extract(RadioData & radioData, const QByteArray & eeprom);
virtual bool writeToFile(const uint8_t * eeprom, uint32_t size);
};
#endif // _STORAGE_EEPROM_H_
#endif // _BINEEPROM_H_
+114
View File
@@ -0,0 +1,114 @@
/*
* Copyright (C) OpenTX
*
* Based on code named
* 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 "categorized.h"
#include "firmwares/opentx/opentxinterface.h"
bool CategorizedStorageFormat::load(RadioData & radioData)
{
QByteArray radioSettingsBuffer;
if (!loadFile(radioSettingsBuffer, "RADIO/radio.bin")) {
setError(QObject::tr("Can't extract RADIO/radio.bin"));
return false;
}
if (!loadRadioSettingsFromByteArray(radioData.generalSettings, radioSettingsBuffer)) {
return false;
}
QByteArray modelsListBuffer;
if (!loadFile(modelsListBuffer, "RADIO/models.txt")) {
setError(QObject::tr("Can't extract RADIO/models.txt"));
return false;
}
QList<QByteArray> lines = modelsListBuffer.split('\n');
int modelIndex = 0;
int categoryIndex = -1;
foreach (const QByteArray & line, lines) {
if (!line.isEmpty()) {
if (line.startsWith('[') && line.endsWith(']')) {
QString name = line.mid(1, line.size() - 2);
CategoryData category(name.toStdString().c_str());
radioData.categories.push_back(category);
categoryIndex++;
}
else {
qDebug() << "Loading" << line;
QByteArray modelBuffer;
if (!loadFile(modelBuffer, QString("MODELS/%1").arg(QString(line)))) {
setError(QObject::tr("Can't extract %1").arg(QString(line)));
return false;
}
if (!loadModelFromByteArray(radioData.models[modelIndex], modelBuffer)) {
return false;
}
strncpy(radioData.models[modelIndex].filename, line.data(), sizeof(radioData.models[modelIndex].filename));
if (strcmp(radioData.generalSettings.currModelFilename, line.data()) == 0) {
radioData.generalSettings.currModelIndex = modelIndex;
}
radioData.models[modelIndex].category = categoryIndex;
radioData.models[modelIndex].used = true;
modelIndex++;
}
}
}
return true;
}
bool CategorizedStorageFormat::write(const RadioData & radioData)
{
// models.txt
QByteArray modelsList;
int currentCategoryIndex = -1;
// radio.bin
QByteArray radioSettingsData;
writeRadioSettingsToByteArray(radioData.generalSettings, radioSettingsData);
if (!writeFile(radioSettingsData, "RADIO/radio.bin")) {
return false;
}
// all models
for (unsigned int i=0; i<radioData.models.size(); i++) {
const ModelData & model = radioData.models[i];
if (!model.isEmpty()) {
QString modelFilename = QString("MODELS/%1").arg(model.filename);
QByteArray modelData;
writeModelToByteArray(model, modelData);
if (!writeFile(modelData, modelFilename)) {
return false;
}
int categoryIndex = model.category;
if (currentCategoryIndex != categoryIndex) {
modelsList.append(QString().sprintf("[%s]\n", radioData.categories[model.category].name));
currentCategoryIndex = categoryIndex;
}
modelsList.append(QString(model.filename) + "\n");
}
}
if (!writeFile(modelsList, "RADIO/models.txt")) {
return false;
}
return true;
}
+42
View File
@@ -0,0 +1,42 @@
/*
* Copyright (C) OpenTX
*
* Based on code named
* 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.
*/
#ifndef _CATEGORIZED_H_
#define _CATEGORIZED_H_
#include "storage.h"
class CategorizedStorageFormat : public StorageFormat
{
public:
CategorizedStorageFormat(const QString & filename):
StorageFormat(filename)
{
}
virtual bool load(RadioData & radioData);
virtual bool write(const RadioData & radioData);
protected:
virtual bool loadFile(QByteArray & fileData, const QString & fileName) = 0;
virtual bool writeFile(const QByteArray & fileData, const QString & fileName) = 0;
};
#endif // _CATEGORIZED_H_
+53
View File
@@ -0,0 +1,53 @@
/*
* Copyright (C) OpenTX
*
* Based on code named
* 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 "eepe.h"
#include <QFile>
#include "eeprominterface.h"
#include "hexinterface.h"
bool EepeFormat::load(RadioData & radioData)
{
QFile file(filename);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
qDebug() << "Unable to open" << filename << file.errorString();
return false;
}
QTextStream inputStream(&file);
QString hline = inputStream.readLine();
if (hline != EEPE_EEPROM_FILE_HEADER) {
qDebug() << "No EEPE header";
return false;
}
qDebug() << "EEPE header found";
QByteArray eeprom(EESIZE_MAX, 0);
int eeprom_size = HexInterface(inputStream).load((uint8_t *)eeprom.data(), EESIZE_MAX);
if (!eeprom_size) {
setError(QObject::tr("Invalid EEPROM File %1").arg(filename));
return false;
}
eeprom.resize(eeprom_size);
return extract(radioData, eeprom);
}
+37
View File
@@ -0,0 +1,37 @@
/*
* Copyright (C) OpenTX
*
* Based on code named
* 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.
*/
#ifndef _EEPE_H_
#define _EEPE_H_
#include "hexeeprom.h"
class EepeFormat : public HexEepromFormat
{
public:
EepeFormat(const QString & filename):
HexEepromFormat(filename)
{
}
virtual bool load(RadioData & radioData);
};
#endif // _EEPE_H_
+48
View File
@@ -0,0 +1,48 @@
/*
* Copyright (C) OpenTX
*
* Based on code named
* 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.
*/
// TODO not enabled for now
bool EepeXmlFormat::load(RadioData & radioData)
{
QFile file(filename);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
qDebug() << "Unable to open" << filename << file.errorString();
return false;
}
QDomDocument doc(ER9X_EEPROM_FILE_TYPE);
bool xmlOK = doc.setContent(&file);
if (xmlOK) {
std::bitset<NUM_ERRORS> errors((unsigned long long) LoadEepromXml(radioData, doc));
if (errors.test(ALL_OK)) {
return true;
}
else {
qDebug() << "XML parsing error";
}
}
else {
qDebug() << "No XML content";
}
return false;
}
+4 -22
View File
@@ -18,11 +18,12 @@
* GNU General Public License for more details.
*/
#include <QtGui>
#include "hexinterface.h"
#include "splash.h"
#include "firmwareinterface.h"
#include "helpers.h"
#include "storage.h"
#include <QtGui>
#define FW_MARK "FW"
#define VERS_MARK "VERS"
@@ -30,25 +31,6 @@
#define TIME_MARK "TIME"
#define EEPR_MARK "EEPR"
int getFileType(const QString &fullFileName)
{
QString suffix = QFileInfo(fullFileName).suffix().toUpper();
if (suffix == "HEX")
return FILE_TYPE_HEX;
else if (suffix == "BIN")
return FILE_TYPE_BIN;
else if (suffix == "EEPM")
return FILE_TYPE_EEPM;
else if (suffix == "EEPE")
return FILE_TYPE_EEPE;
else if (suffix == "XML")
return FILE_TYPE_XML;
else if (suffix == "OTX")
return FILE_TYPE_OTX;
else
return 0;
}
FirmwareInterface::FirmwareInterface(const QString &filename):
flash(FSIZE_MAX, 0),
flashSize(0),
@@ -355,9 +337,9 @@ unsigned int FirmwareInterface::save(QString fileName)
memcpy(binflash, flash.constData(), flashSize);
QFile file(fileName);
int fileType = getFileType(fileName);
int fileType = getStorageType(fileName);
if (fileType == FILE_TYPE_HEX) {
if (fileType == STORAGE_TYPE_HEX) {
if (!file.open(QIODevice::ReadWrite | QIODevice::Truncate | QIODevice::Text)) { //reading HEX TEXT file
free(binflash);
return -1;
@@ -36,15 +36,6 @@
#define ERSKY9X_SPE "SPE"
#define ERSKY9X_OFFSET (7)
#define FILE_TYPE_BIN 1
#define FILE_TYPE_HEX 2
#define FILE_TYPE_EEPE 3
#define FILE_TYPE_EEPM 4
#define FILE_TYPE_XML 5
#define FILE_TYPE_OTX 6
int getFileType(const QString &fullFileName);
class FirmwareInterface
{
public:
+63
View File
@@ -0,0 +1,63 @@
/*
* Copyright (C) OpenTX
*
* Based on code named
* 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 "hexeeprom.h"
#include "eeprominterface.h"
#include "hexinterface.h"
#include <QFile>
bool HexEepromFormat::load(RadioData & radioData)
{
QFile file(filename);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
qDebug() << "Unable to open" << filename << file.errorString();
return false;
}
QTextStream inputStream(&file);
// TODO remove EESIZE_MAX
QByteArray eeprom(EESIZE_MAX, 0);
int eeprom_size = HexInterface(inputStream).load((uint8_t *)eeprom.data(), EESIZE_MAX);
if (!eeprom_size) {
setError(QObject::tr("Invalid EEPROM File %1").arg(filename));
return false;
}
eeprom.resize(eeprom_size);
return extract(radioData, eeprom);
}
bool HexEepromFormat::writeToFile(const uint8_t * eeprom, uint32_t size)
{
QFile file(filename);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
setError(QObject::tr("Cannot open file %1:\n%2.").arg(filename).arg(file.errorString()));
return false;
}
QTextStream outputStream(&file);
if (!HexInterface(outputStream).save(eeprom, size)) {
setError(QObject::tr("Error writing file %1:\n%2.").arg(filename).arg(file.errorString()));
return false;
}
return true;
}
+40
View File
@@ -0,0 +1,40 @@
/*
* Copyright (C) OpenTX
*
* Based on code named
* 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.
*/
#ifndef _HEXEEPROM_H_
#define _HEXEEPROM_H_
#include "bineeprom.h"
class HexEepromFormat : public BinEepromFormat
{
public:
HexEepromFormat(const QString & filename):
BinEepromFormat(filename)
{
}
virtual bool load(RadioData & radioData);
protected:
virtual bool writeToFile(const uint8_t * eeprom, uint32_t size);
};
#endif // _HEXEEPROM_H_
+2 -2
View File
@@ -84,7 +84,7 @@ int HexInterface::load(uint8_t *data, int maxsize)
}
bool HexInterface::save(uint8_t *data, const int size)
bool HexInterface::save(const uint8_t * data, const int size)
{
int addr = 0;
int nextbank = 1;
@@ -103,7 +103,7 @@ bool HexInterface::save(uint8_t *data, const int size)
return true;
}
QString HexInterface::iHEXLine(quint8 * data, quint32 addr, quint8 len)
QString HexInterface::iHEXLine(const quint8 * data, quint32 addr, quint8 len)
{
unsigned int bankaddr;
bankaddr=addr&0xffff;
+3 -3
View File
@@ -28,13 +28,13 @@ class HexInterface {
public:
HexInterface(QTextStream &stream);
int load(uint8_t *output, int maxsize);
bool save(uint8_t *data, const int size);
int load(uint8_t * output, int maxsize);
bool save(const uint8_t * data, const int size);
protected:
int getValueFromLine(const QString &line, int pos, int len=2);
QString iHEXLine(quint8 * data, quint32 addr, quint8 len);
QString iHEXLine(const quint8 * data, quint32 addr, quint8 len);
QString iHEXExtRec(quint8 bank);
QTextStream & stream;
+116
View File
@@ -0,0 +1,116 @@
/*
* Copyright (C) OpenTX
*
* Based on code named
* 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 "miniz.c"
#include <QFile>
#define MZ_ALLOCATION_SIZE (32*1024)
bool OtxFormat::load(RadioData & radioData)
{
QFile file(filename);
if (!file.open(QFile::ReadOnly)) {
setError(QObject::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() << QObject::tr("Error opening OTX archive %1").arg(filename);
return false;
}
bool result = CategorizedStorageFormat::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(QObject::tr("Error initializing OTX archive writer"));
return false;
}
bool result = CategorizedStorageFormat::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(QObject::tr("Error writing file %1:\n%2.").arg(filename).arg(file.errorString()));
result = false;
}
}
else {
setError(QObject::tr("Error creating OTX file %1:\n%2.").arg(filename).arg(file.errorString()));
result = false;
}
}
else {
setError(QObject::tr("Error creating OTX 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, filename.toStdString().c_str(), &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(QObject::tr("Error adding %1 to OTX archive").arg(filename));
return false;
}
return true;
}
+48
View File
@@ -0,0 +1,48 @@
/*
* Copyright (C) OpenTX
*
* Based on code named
* 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.
*/
#ifndef _OTX_H_
#define _OTX_H_
#include "categorized.h"
#define MINIZ_HEADER_FILE_ONLY
#include "miniz.c"
#undef MINIZ_HEADER_FILE_ONLY
class OtxFormat : public CategorizedStorageFormat
{
public:
OtxFormat(const QString & filename):
CategorizedStorageFormat(filename)
{
}
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);
mz_zip_archive zip_archive;
};
#endif // _OTX_H_
-15
View File
@@ -20,21 +20,6 @@
// TODO should be rle
/*
* Author - Bertrand Songis <bsongis@gmail.com>
*
* Based on th9x -> http://code.google.com/p/th9x/
*
* 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.
*
*/
#ifndef _RLEFILE_H_
#define _RLEFILE_H_
+65
View File
@@ -0,0 +1,65 @@
/*
* Copyright (C) OpenTX
*
* Based on code named
* 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 "sdcard.h"
#include <QFile>
#include <QDir>
bool SdcardFormat::write(const RadioData & radioData)
{
QDir dir(filename);
dir.mkdir("RADIO");
dir.mkdir("MODELS");
return CategorizedStorageFormat::write(radioData);
}
bool SdcardFormat::loadFile(QByteArray & filedata, const QString & filename)
{
QString path = this->filename + "/" + filename;
QFile file(path);
if (!file.open(QFile::ReadOnly)) {
setError(QObject::tr("Error opening file %1:\n%2.").arg(path).arg(file.errorString()));
return false;
}
filedata = file.readAll();
qDebug() << "File" << path << "read, size:" << filedata.size();
return true;
}
bool SdcardFormat::writeFile(const QByteArray & data, const QString & filename)
{
QString path = this->filename + "/" + filename;
QFile file(path);
if (!file.open(QFile::WriteOnly)) {
setError(QObject::tr("Error opening file %1:\n%2.").arg(path).arg(file.errorString()));
qDebug() << "File" << path << "write error";
return false;
}
file.write(data.data(), data.size());
file.close();
qDebug() << "File" << path << "written, size:" << data.size();
return true;
}
bool SdcardStorageFactory::probe(const QString & path)
{
return QDir(path).exists();
}
+52
View File
@@ -0,0 +1,52 @@
/*
* Copyright (C) OpenTX
*
* Based on code named
* 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.
*/
#ifndef _SDCARD_H2_
#define _SDCARD_H2_
#include "categorized.h"
class SdcardFormat : public CategorizedStorageFormat
{
public:
SdcardFormat(const QString & filename):
CategorizedStorageFormat(filename)
{
}
virtual bool write(const RadioData & radioData);
protected:
virtual bool loadFile(QByteArray & fileData, const QString & fileName);
virtual bool writeFile(const QByteArray & fileData, const QString & fileName);
};
class SdcardStorageFactory : public DefaultStorageFactory<SdcardFormat>
{
public:
SdcardStorageFactory():
DefaultStorageFactory<SdcardFormat>("sdcard")
{
}
virtual bool probe(const QString & name);
};
#endif // _SDCARD_H2_
+170
View File
@@ -0,0 +1,170 @@
/*
* Copyright (C) OpenTX
*
* Based on code named
* 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 "bineeprom.h"
#include "eepe.h"
#include "otx.h"
#include "sdcard.h"
#include "firmwareinterface.h"
#include "eeprominterface.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
return STORAGE_TYPE_UNKNOWN;
}
void registerStorageFactory(StorageFactory * factory);
QList<StorageFactory *> registeredStorageFactories;
void registerStorageFactory(StorageFactory * factory)
{
qDebug() << "register storage" << factory->name();
registeredStorageFactories.push_back(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 SdcardStorageFactory());
}
bool Storage::load(RadioData & radioData)
{
QFile file(filename);
if (!file.exists()) {
setError(QObject::tr("Unable to find file %1!").arg(filename));
return false;
}
foreach(StorageFactory * factory, registeredStorageFactories) {
StorageFormat * format = factory->instance(filename);
if (format->load(radioData)) {
setWarning(format->warning());
return true;
}
else {
setError(format->error());
}
}
return false;
}
bool Storage::write(const RadioData & radioData)
{
foreach(StorageFactory * factory, registeredStorageFactories) {
if (factory->probe(filename)) {
return factory->instance(filename)->write(radioData);
}
}
return false;
}
bool convertEEprom(const QString & sourceEEprom, const QString & destinationEEprom, const QString & firmwareFilename)
{
Firmware * currentFirmware = GetCurrentFirmware();
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(EESIZE_MAX, 0);
int size = currentFirmware->saveEEPROM((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
+143
View File
@@ -0,0 +1,143 @@
/*
* Copyright (C) OpenTX
*
* Based on code named
* 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.
*/
#ifndef _STORAGE_H_
#define _STORAGE_H_
#include "radiodata.h"
#include <QString>
#include <QDebug>
enum StorageType
{
STORAGE_TYPE_UNKNOWN,
STORAGE_TYPE_BIN,
STORAGE_TYPE_HEX,
STORAGE_TYPE_EEPE,
STORAGE_TYPE_EEPM,
STORAGE_TYPE_XML,
STORAGE_TYPE_SDCARD,
STORAGE_TYPE_OTX
};
StorageType getStorageType(const QString & filename);
class StorageFormat
{
public:
StorageFormat(const QString & filename, uint8_t version=0):
filename(filename),
version(version)
{
}
virtual bool load(RadioData & radioData) = 0;
virtual bool write(const RadioData & radioData) = 0;
QString error() {
return _error;
}
QString warning() {
return _warning;
}
protected:
void setError(const QString & error)
{
qDebug() << "error:" << error;
_error = error;
}
void setWarning(const QString & warning)
{
if (!warning.isEmpty())
qDebug() << "warning:" << warning;
_warning = warning;
}
QString filename;
uint8_t version;
QString _error;
QString _warning;
};
class StorageFactory
{
public:
StorageFactory()
{
}
virtual QString name() = 0;
virtual bool probe(const QString & filename) = 0;
virtual StorageFormat * instance(const QString & filename) = 0;
};
template <class T>
class DefaultStorageFactory : public StorageFactory
{
public:
DefaultStorageFactory(const QString & name):
StorageFactory(),
_name(name)
{
}
virtual QString name()
{
return _name;
}
virtual bool probe(const QString & filename)
{
return filename.toLower().endsWith("." + _name);
}
virtual StorageFormat * instance(const QString & filename)
{
return new T(filename);
}
QString _name;
};
class Storage : public StorageFormat
{
public:
Storage(const QString & filename):
StorageFormat(filename)
{
}
virtual bool load(RadioData & radioData);
virtual bool write(const RadioData & radioData);
};
void registerStorageFactories();
#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_
-124
View File
@@ -1,124 +0,0 @@
/*
* Copyright (C) OpenTX
*
* Based on code named
* 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 <QFile>
#include "firmwareinterface.h"
#include "storage_eeprom.h"
unsigned long LoadEeprom(RadioData & radioData, const uint8_t * eeprom, const int size)
{
std::bitset<NUM_ERRORS> errors;
foreach(EEPROMInterface * eepromInterface, eepromInterfaces) {
std::bitset<NUM_ERRORS> result((unsigned long long)eepromInterface->load(radioData, eeprom, size));
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 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();
}
bool convertEEprom(const QString & sourceEEprom, const QString & destinationEEprom, const QString & firmwareFilename)
{
Firmware * currentFirmware = GetCurrentFirmware();
FirmwareInterface firmware(firmwareFilename);
if (!firmware.isValid())
return false;
uint8_t version = firmware.getEEpromVersion();
unsigned int variant = firmware.getEEpromVariant();
QFile sourceFile(sourceEEprom);
int eeprom_size = sourceFile.size();
if (!eeprom_size)
return false;
if (!sourceFile.open(QIODevice::ReadOnly))
return false;
QByteArray eeprom(eeprom_size, 0);
long result = sourceFile.read(eeprom.data(), eeprom_size);
sourceFile.close();
QSharedPointer<RadioData> radioData = QSharedPointer<RadioData>(new RadioData());
std::bitset<NUM_ERRORS> errors((unsigned long long)LoadEeprom(*radioData, (uint8_t *)eeprom.data(), eeprom_size));
if (!errors.test(ALL_OK) || !currentFirmware->saveEEPROM((uint8_t *)eeprom.data(), *radioData, version, variant)) {
return false;
}
QFile destinationFile(destinationEEprom);
if (!destinationFile.open(QIODevice::WriteOnly))
return false;
result = destinationFile.write(eeprom.constData(), eeprom_size);
destinationFile.close();
if (result != eeprom_size)
return false;
return true;
}
-300
View File
@@ -1,300 +0,0 @@
/*
* Copyright (C) OpenTX
*
* Based on code named
* 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 <QFileInfo>
#include <QObject>
#include <QDebug>
#include <QRegularExpression>
#include "storage_sdcard.h"
// defines for miniz
#include "miniz.c"
int readFromArchive(QByteArray & data, mz_zip_archive * zip_archive, unsigned int index)
{
size_t uncompSize;
void * uncompData = mz_zip_reader_extract_to_heap(zip_archive, index, &uncompSize, 0);
if (!uncompData) {
return -1;
}
qDebug() << "Extracted file with index" << index << ",size" << uncompSize;
data.append((const char *)uncompData, uncompSize);
mz_free(uncompData);
return 0;
}
int StorageSdcard::read(const QString & path)
{
return QFileInfo(path).isDir() ? readSdcard(path) : readOtx(path);
}
int StorageSdcard::write(const QString & path)
{
return QFileInfo(path).isDir() ? writeSdcard(path) : writeOtx(path);
}
int StorageSdcard::readOtx(const QString & path)
{
QFile file(path);
if (!file.open(QFile::ReadOnly)) {
lastErrorMessage = QObject::tr("Error opening file %1:\n%2.").arg(path).arg(file.errorString());
return -1;
}
QByteArray archiveContents = file.readAll();
file.close();
qDebug() << "File" << path << "read, size:" << archiveContents.size();
// open zip file
mz_zip_archive zip_archive;
memset(&zip_archive, 0, sizeof(zip_archive));
if (!mz_zip_reader_init_mem(&zip_archive, archiveContents.data(), archiveContents.size(), 0)) {
lastErrorMessage = QObject::tr("Error opening OTX archive %1.").arg(path);
qDebug() << lastErrorMessage;
return -1;
}
// go trough all files in an archive
QRegularExpression regexModel("MODELS/\\w+.bin", QRegularExpression::CaseInsensitiveOption);
for (unsigned int i = 0; i < mz_zip_reader_get_num_files(&zip_archive); i++) {
mz_zip_archive_file_stat file_stat;
if (!mz_zip_reader_file_stat(&zip_archive, i, &file_stat)) {
lastErrorMessage = QObject::tr("mz_zip_reader_file_stat() failed!");
qDebug() << lastErrorMessage;
mz_zip_reader_end(&zip_archive);
return -1;
}
// printf("Filename: \"%s\", Comment: \"%s\", Uncompressed size: %u, Compressed size: %u, Is Dir: %u\n", file_stat.m_filename, file_stat.m_comment, (uint)file_stat.m_uncomp_size, (uint)file_stat.m_comp_size, mz_zip_reader_is_file_a_directory(&zip_archive, i));
if (mz_zip_reader_is_file_a_directory(&zip_archive, i)) continue;
QString filename(file_stat.m_filename);
if (regexModel.match(filename).hasMatch()) {
qDebug() << "found model:" << filename;
models.append(ModelFile());
ModelFile & newModel = models.last();
newModel.filename = QFileInfo(filename).fileName();
if (readFromArchive(newModel.data, &zip_archive, i) < 0) {
lastErrorMessage = QObject::tr("Can't extract file %1").arg(filename);
qDebug() << lastErrorMessage;
mz_zip_reader_end(&zip_archive);
return -1;
}
}
else if (QString::compare(filename, "RADIO/radio.bin", Qt::CaseInsensitive) == 0) {
if (readFromArchive(radio, &zip_archive, i) < 0) {
lastErrorMessage = QObject::tr("Can't extract file %1").arg(filename);
qDebug() << lastErrorMessage;
mz_zip_reader_end(&zip_archive);
return -1;
}
}
else if (QString::compare(filename, "RADIO/models.txt", Qt::CaseInsensitive) == 0) {
if (readFromArchive(modelList, &zip_archive, i) < 0) {
lastErrorMessage = QObject::tr("Can't extract file %1").arg(filename);
qDebug() << lastErrorMessage;
mz_zip_reader_end(&zip_archive);
return -1;
}
}
else {
qDebug() << "Unknown file " << filename;
}
}
mz_zip_reader_end(&zip_archive);
return 0;
}
void StorageSdcard::readFile(QByteArray & data, const QString & path)
{
QFile file(path);
if (!file.open(QFile::ReadOnly)) {
lastErrorMessage = QObject::tr("Error opening file %1:\n%2.").arg(path).arg(file.errorString());
throw StorageSdcardReadFileError();
}
data = file.readAll();
file.close();
qDebug() << "File" << path << "read, size:" << data.size();
}
int StorageSdcard::readSdcard(const QString & path)
{
try {
readFile(radio, path + "/RADIO/radio.bin");
readFile(modelList, path + "RADIO/models.txt");
QDir dir(path + "MODELS/");
QStringList filters;
filters << "*.bin";
foreach(QString filename, dir.entryList(filters, QDir::Files)) {
qDebug() << "found" << filename;
models.append(ModelFile());
ModelFile & newModel = models.last();
newModel.filename = QFileInfo(filename).fileName();
readFile(newModel.data, path + "MODELS/" + filename);
}
}
catch (StorageSdcardReadFileError) {
return -1;
}
return 0;
}
void StorageSdcard::writeFile(const QByteArray & data, const QString & path)
{
QFile file(path);
if (!file.open(QFile::WriteOnly)) {
lastErrorMessage = QObject::tr("Error opening file %1:\n%2.").arg(path).arg(file.errorString());
qDebug() << "File" << path << "write error";
throw StorageSdcardWriteFileError();
}
file.write(data.data(), data.size());
file.close();
qDebug() << "File" << path << "written, size:" << data.size();
}
int StorageSdcard::writeSdcard(const QString & path)
{
try {
QDir dir(path);
dir.mkdir("RADIO");
writeFile(radio, path + "/RADIO/radio.bin");
writeFile(modelList, path + "/RADIO/models.txt");
dir.mkdir("MODELS");
for (QList<ModelFile>::const_iterator i = models.begin(); i != models.end(); ++i) {
qDebug() << "writing" << i->filename;
writeFile(i->data, path + "/MODELS/" + i->filename);
}
}
catch (StorageSdcardWriteFileError) {
return -1;
}
return 0;
}
#define MZ_ALLOCATION_SIZE (32*1024)
int StorageSdcard::writeOtx(const QString & path)
{
qDebug() << "Saving to archive" << path;
mz_zip_archive zip_archive;
memset(&zip_archive, 0, sizeof(zip_archive));
if (!mz_zip_writer_init_heap(&zip_archive, 0, MZ_ALLOCATION_SIZE)) {
lastErrorMessage = QObject::tr("Error initializing OTX archive writer");
qDebug() << lastErrorMessage;
return -1;
}
// add radio.bin
if (!mz_zip_writer_add_mem(&zip_archive, "RADIO/radio.bin", radio.data(), radio.size(), MZ_DEFAULT_LEVEL)) {
lastErrorMessage = QObject::tr("Error adding %1 to OTX archive").arg("RADIO/radio.bin");
qDebug() << lastErrorMessage;
mz_zip_writer_end(&zip_archive);
return -1;
}
// add models.txt
if (!mz_zip_writer_add_mem(&zip_archive, "RADIO/models.txt", modelList.data(), modelList.size(), MZ_DEFAULT_LEVEL)) {
lastErrorMessage = QObject::tr("Error adding %1 to OTX archive").arg("RADIO/models.txt");
qDebug() << lastErrorMessage;
mz_zip_writer_end(&zip_archive);
return -1;
}
// add all models
for (QList<ModelFile>::iterator i = models.begin(); i != models.end(); ++i) {
QString filename = "MODELS/" + i->filename;
qDebug() << "\tadding model:" << filename << "size" << i->data.size();
if (!mz_zip_writer_add_mem(&zip_archive, filename.toLatin1(), i->data.data(), i->data.size(), MZ_DEFAULT_LEVEL)) {
lastErrorMessage = QObject::tr("Error adding %1 to OTX archive").arg(filename);
qDebug() << lastErrorMessage;
mz_zip_writer_end(&zip_archive);
return -1;
}
}
// finalize archive and get contents
char * archiveContents;
size_t archiveSize;
if (!mz_zip_writer_finalize_heap_archive(&zip_archive, (void **)&archiveContents, &archiveSize)) {
lastErrorMessage = QObject::tr("Error creating OTX archive");
qDebug() << lastErrorMessage;
mz_zip_writer_end(&zip_archive);
return -1;
}
qDebug() << "Archive size" << archiveSize;
// write contents to file
QFile file(path);
if (!file.open(QIODevice::WriteOnly)) {
lastErrorMessage = QObject::tr("Error creating OTX file %1:\n%2.").arg(path).arg(file.errorString());
qDebug() << lastErrorMessage;
mz_zip_writer_end(&zip_archive);
return -1;
}
qint64 result = file.write(archiveContents, archiveSize);
if(result != (qint64)archiveSize) {
lastErrorMessage = QObject::tr("Error writing file %1:\n%2.").arg(path).arg(file.errorString());
mz_zip_writer_end(&zip_archive);
return -1;
}
file.close();
mz_zip_writer_end(&zip_archive);
return 0;
}
const QByteArray & StorageSdcard::getModelData(const QString & filename) const
{
for (QList<ModelFile>::const_iterator i = models.begin(); i != models.end(); ++i) {
if (filename == i->filename) return i->data;
}
throw StorageSdcardModelNotFound();
}
QList<ModelFile>::iterator StorageSdcard::getModelIterator(const QString & filename)
{
for (QList<ModelFile>::iterator i = models.begin(); i != models.end(); ++i) {
if (filename == i->filename) return i;
}
return models.end();
}
QList<ModelFile>::const_iterator StorageSdcard::getModelIterator(const QString & filename) const
{
for (QList<ModelFile>::const_iterator i = models.begin(); i != models.end(); ++i) {
if (filename == i->filename) return i;
}
return models.end();
}
QList<QString> StorageSdcard::getModelsFileNames() const
{
QList<QString> result;
for (QList<ModelFile>::const_iterator i = models.begin(); i != models.end(); ++i) {
result.append(i->filename);
}
return result;
}
-99
View File
@@ -1,99 +0,0 @@
/*
* Copyright (C) OpenTX
*
* Based on code named
* 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.
*/
#ifndef _STORAGE_SDCARD_H_
#define _STORAGE_SDCARD_H_
#include <QList>
#include <QByteArray>
#include <QString>
#include <QDir>
/*
This class implements a storage model used for radios that
store all settings on the SD card (currently only Hours)
Disk storage format:
* uses extension OTX
* its a normal ZIP archive containing the same directory
structure as the SD card in the radio. Only the relevant
directories and files are included.
*/
class StorageSdcardModelNotFound {};
class StorageSdcardReadFileError {};
class StorageSdcardWriteFileError {};
// representation of modelXX.bin file
struct ModelFile {
QString filename; // file name (without path)
QByteArray data; // file contents
};
class StorageSdcard {
public:
/*
Reads models and radio settings from:
* if a file is specified in path, from that file (OTX archive format)
* if a path is specified, from that path (where it expects to find files like on the SD card)
*/
int read(const QString & path);
/*
|Writes models and radio settings to:
* if a file is specified in path, to that file (OTX archive format)
* if a path is specified, to that path (layout created is like on the SD card)
*/
int write(const QString & path);
/*
Returns a model data for specified model
*/
const QByteArray & getModelData(const QString & filename) const;
/*
Returns a model iterator for specified model
*/
QList<ModelFile>::iterator getModelIterator(const QString & filename);
QList<ModelFile>::const_iterator getModelIterator(const QString & filename) const;
/*
Returns a list of all model bin files (their filenames)
*/
QList<QString> getModelsFileNames() const;
QString lastErrorMessage;
QByteArray radio; // radio settings (radio.bin)
QByteArray modelList; // model names and categories (models.txt)
QList<ModelFile> models; // collection of model data (modelXX.bin)
private:
int readOtx(const QString & path);
int readSdcard(const QString & path);
int writeOtx(const QString & path);
int writeSdcard(const QString & path);
void readFile(QByteArray & data, const QString & path);
void writeFile(const QByteArray & data, const QString & path);
};
#endif // _STORAGE_SDCARD_H_
+1 -1
View File
@@ -195,7 +195,7 @@ bool XmlInterface::save(RadioData &radioData)
// the models
models xml_models;
models::model_sequence & model_sequence (xml_models.model());
for (int i=0; i<CPN_MAX_MODELS; i++) {
for (int i=0; i<radioData.models.size(); i++) {
ModelData & m = radioData.models[i];
if (m.used) {
model xm(m.name);
+2 -1
View File
@@ -23,6 +23,7 @@
#include "eeprominterface.h"
#include <QTextStream>
#include <QDomDocument>
class XmlInterface
{
@@ -32,7 +33,7 @@ class XmlInterface
bool load(RadioData &);
virtual bool loadxml(RadioData &radioData, QDomDocument &doc);
virtual bool loadxml(RadioData & radioData, QDomDocument & doc);
bool save(RadioData &radioData);