[Companion] Cosmetics in EEPROM import

This commit is contained in:
Bertrand Songis
2016-11-20 22:49:26 +01:00
parent f5e1b01cd3
commit d42fc6c471
7 changed files with 142 additions and 128 deletions
+9 -9
View File
@@ -30,7 +30,7 @@ class DataField {
virtual const char *getName() { return name; }
virtual ~DataField() { }
virtual void ExportBits(QBitArray & output) = 0;
virtual void ImportBits(QBitArray & input) = 0;
virtual void ImportBits(const QBitArray & input) = 0;
virtual unsigned int size() = 0;
QBitArray bytesToBits(QByteArray bytes)
@@ -62,7 +62,7 @@ class DataField {
return 0;
}
int Import(QByteArray & input)
int Import(const QByteArray & input)
{
QBitArray bits = bytesToBits(input);
ImportBits(bits);
@@ -147,7 +147,7 @@ class BaseUnsignedField: public DataField {
}
}
virtual void ImportBits(QBitArray & input)
virtual void ImportBits(const QBitArray & input)
{
field = 0;
for (int i=0; i<N; i++) {
@@ -208,7 +208,7 @@ class BoolField: public DataField {
}
}
virtual void ImportBits(QBitArray & input)
virtual void ImportBits(const QBitArray & input)
{
field = input[0] ? true : false;
eepromImportDebug() << QString("\timported %1<%2>: 0x%3(%4)").arg(name).arg(N).arg(field, 0, 16).arg(field);
@@ -266,7 +266,7 @@ class SignedField: public DataField {
}
}
virtual void ImportBits(QBitArray & input)
virtual void ImportBits(const QBitArray & input)
{
unsigned int value = 0;
for (int i=0; i<N; i++) {
@@ -331,7 +331,7 @@ class CharField: public DataField {
}
}
virtual void ImportBits(QBitArray & input)
virtual void ImportBits(const QBitArray & input)
{
unsigned int b = 0;
for (int i=0; i<N; i++) {
@@ -381,7 +381,7 @@ class ZCharField: public DataField {
}
}
virtual void ImportBits(QBitArray & input)
virtual void ImportBits(const QBitArray & input)
{
unsigned int b = 0;
for (int i=0; i<N; i++) {
@@ -442,7 +442,7 @@ class StructField: public DataField {
}
}
virtual void ImportBits(QBitArray & input)
virtual void ImportBits(const QBitArray & input)
{
eepromImportDebug() << QString("\timporting %1[%2]:").arg(name).arg(fields.size());
int offset = 0;
@@ -497,7 +497,7 @@ class TransformedField: public DataField {
field.ExportBits(output);
}
virtual void ImportBits(QBitArray & input)
virtual void ImportBits(const QBitArray & input)
{
eepromImportDebug() << QString("\timporting TransformedField %1:").arg(field.getName());
field.ImportBits(input);
+2
View File
@@ -1408,6 +1408,8 @@ class EEPROMInterface
virtual unsigned long load(RadioData &radioData, const uint8_t *eeprom, int size) = 0;
virtual unsigned long loadBackup(RadioData &radioData, uint8_t *eeprom, int esize, int index) = 0;
virtual bool loadModelFromByteArray(ModelData & model, const QByteArray & data) { return false; }
virtual unsigned long loadxml(RadioData &radioData, QDomDocument &doc) = 0;
@@ -2604,7 +2604,7 @@ class FrskyScreenField: public DataField {
}
}
virtual void ImportBits(QBitArray & input)
virtual void ImportBits(const QBitArray & input)
{
eepromImportDebug() << QString("importing %1: type: %2").arg(name).arg(screen.type);
@@ -159,7 +159,7 @@ bool OpenTxEepromInterface::loadModel(ModelData &model, uint8_t *data, int index
}
template <class T>
bool OpenTxEepromInterface::loadModelVariant(unsigned int index, ModelData &model, uint8_t *data, unsigned int version, unsigned int variant)
bool OpenTxEepromInterface::loadModelVariant(unsigned int index, ModelData & model, uint8_t *data, unsigned int version, unsigned int variant)
{
T open9xModel(model, board, version, variant);
@@ -198,6 +198,14 @@ bool OpenTxEepromInterface::loadModelVariant(unsigned int index, ModelData &mode
return true;
}
bool OpenTxEepromInterface::loadModelFromByteArray(ModelData & model, const QByteArray & data)
{
OpenTxModelData modelData(model, board, 218, 0);
modelData.Import(data);
modelData.Dump();
return true;
}
bool OpenTxEepromInterface::loadModel(uint8_t version, ModelData &model, uint8_t *data, int index, unsigned int variant, unsigned int stickMode)
{
if (version == 201) {
@@ -1068,7 +1076,7 @@ bool OpenTxEepromInterface::checkVariant(unsigned int version, unsigned int vari
return true;
}
unsigned long OpenTxEepromInterface::loadBackup(RadioData &radioData, uint8_t *eeprom, int esize, int index)
unsigned long OpenTxEepromInterface::loadBackup(RadioData & radioData, uint8_t * eeprom, int esize, int index)
{
std::bitset<NUM_ERRORS> errors;
@@ -1114,7 +1122,8 @@ unsigned long OpenTxEepromInterface::loadBackup(RadioData &radioData, uint8_t *e
if (version_error == OLD_VERSION) {
errors.set(version_error);
errors.set(HAS_WARNINGS);
} else if (version_error == NOT_OPENTX) {
}
else if (version_error == NOT_OPENTX) {
std::cout << " not open9x\n";
errors.set(version_error);
return errors.to_ulong();
@@ -52,7 +52,9 @@ class OpenTxEepromInterface : public EEPROMInterface
EepromLoadErrors checkVersion(unsigned int version);
bool checkVariant(unsigned int version, unsigned int variant);
bool loadModelFromByteArray(ModelData & model, const QByteArray & data);
template <class T>
bool loadModel(ModelData &model, uint8_t *data, int index, unsigned int stickMode=0);
+114 -113
View File
@@ -234,138 +234,139 @@ void MdiChild::newFile()
updateTitle();
}
bool MdiChild::loadFile(const QString &fileName, bool resetCurrentFile)
bool MdiChild::loadFile(const QString & fileName, bool resetCurrentFile)
{
QFile file(fileName);
QFile file(fileName);
if (!file.exists()) {
QMessageBox::critical(this, tr("Error"), tr("Unable to find file %1!").arg(fileName));
return false;
}
if (!file.exists()) {
QMessageBox::critical(this, tr("Error"), tr("Unable to find file %1!").arg(fileName));
return false;
}
int fileType = getFileType(fileName);
int fileType = getFileType(fileName);
#if 0
if (fileType==FILE_TYPE_XML) {
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { //reading HEX TEXT file
QMessageBox::critical(this, tr("Error"),
tr("Error opening file %1:\n%2.")
.arg(fileName)
.arg(file.errorString()));
return false;
}
QTextStream inputStream(&file);
XmlInterface(inputStream).load(radioData);
if (fileType==FILE_TYPE_XML) {
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { //reading HEX TEXT file
QMessageBox::critical(this, tr("Error"),
tr("Error opening file %1:\n%2.")
.arg(fileName)
.arg(file.errorString()));
return false;
}
else
QTextStream inputStream(&file);
XmlInterface(inputStream).load(radioData);
}
else
#endif
if (fileType==FILE_TYPE_HEX || fileType==FILE_TYPE_EEPE) { //read HEX file
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { //reading HEX TEXT file
QMessageBox::critical(this, tr("Error"),
tr("Error opening file %1:\n%2.")
.arg(fileName)
.arg(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)) {
ui->modelsList->refreshList();
if(resetCurrentFile) setCurrentFile(fileName);
return true;
}
}
file.reset();
QTextStream inputStream(&file);
if (fileType==FILE_TYPE_EEPE) { // read EEPE file header
QString hline = inputStream.readLine();
if (hline!=EEPE_EEPROM_FILE_HEADER) {
file.close();
return false;
}
}
QByteArray eeprom(EESIZE_MAX, 0);
int eeprom_size = HexInterface(inputStream).load((uint8_t *)eeprom.data(), EESIZE_MAX);
if (!eeprom_size) {
if (fileType==FILE_TYPE_HEX || fileType==FILE_TYPE_EEPE) { //read HEX file
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { //reading HEX TEXT file
QMessageBox::critical(this, tr("Error"),
tr("Invalid EEPROM File %1")
.arg(fileName));
tr("Error opening file %1:\n%2.")
.arg(fileName)
.arg(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)) {
ui->modelsList->refreshList();
if(resetCurrentFile) setCurrentFile(fileName);
return true;
}
}
file.reset();
QTextStream inputStream(&file);
if (fileType==FILE_TYPE_EEPE) { // read EEPE file header
QString hline = inputStream.readLine();
if (hline!=EEPE_EEPROM_FILE_HEADER) {
file.close();
return false;
}
}
QByteArray eeprom(EESIZE_MAX, 0);
int eeprom_size = HexInterface(inputStream).load((uint8_t *)eeprom.data(), EESIZE_MAX);
if (!eeprom_size) {
QMessageBox::critical(this, tr("Error"),
tr("Invalid EEPROM File %1")
.arg(fileName));
file.close();
return false;
}
std::bitset<NUM_ERRORS> errors((unsigned long long)LoadEeprom(radioData, (uint8_t *)eeprom.data(), eeprom_size));
if (!errors.test(ALL_OK)) {
ShowEepromErrors(this, tr("Error"), tr("Invalid EEPROM File %1").arg(fileName), errors.to_ulong());
file.close();
std::bitset<NUM_ERRORS> errors((unsigned long long)LoadEeprom(radioData, (uint8_t *)eeprom.data(), eeprom_size));
if (!errors.test(ALL_OK)) {
ShowEepromErrors(this, tr("Error"), tr("Invalid EEPROM File %1").arg(fileName), errors.to_ulong());
return false;
}
if (errors.test(HAS_WARNINGS)) {
ShowEepromWarnings(this, tr("Warning"), errors.to_ulong());
}
ui->modelsList->refreshList();
if (resetCurrentFile) setCurrentFile(fileName);
return true;
}
else if (fileType==FILE_TYPE_BIN) { //read binary
int eeprom_size = file.size();
if (!file.open(QFile::ReadOnly)) { //reading binary file - TODO HEX support
QMessageBox::critical(this, tr("Error"),
tr("Error opening file %1:\n%2.")
.arg(fileName)
.arg(file.errorString()));
return false;
}
uint8_t * eeprom = (uint8_t *)malloc(eeprom_size);
memset(eeprom, 0, eeprom_size);
long result = file.read((char*)eeprom, eeprom_size);
file.close();
if (result != eeprom_size) {
QMessageBox::critical(this, tr("Error"),
tr("Error reading file %1:\n%2.")
.arg(fileName)
.arg(file.errorString()));
free(eeprom);
return false;
}
std::bitset<NUM_ERRORS> errorsEeprom((unsigned long long)LoadEeprom(radioData, eeprom, eeprom_size));
if (!errorsEeprom.test(ALL_OK)) {
std::bitset<NUM_ERRORS> errorsBackup((unsigned long long)LoadBackup(radioData, eeprom, eeprom_size, 0));
if (!errorsBackup.test(ALL_OK)) {
ShowEepromErrors(this, tr("Error"), tr("Invalid binary EEPROM File %1").arg(fileName), (errorsEeprom | errorsBackup).to_ulong());
free(eeprom);
return false;
}
if (errors.test(HAS_WARNINGS)) {
ShowEepromWarnings(this, tr("Warning"), errors.to_ulong());
if (errorsBackup.test(HAS_WARNINGS)) {
ShowEepromWarnings(this, tr("Warning"), errorsBackup.to_ulong());
}
ui->modelsList->refreshList();
if(resetCurrentFile) setCurrentFile(fileName);
return true;
}
else if (fileType==FILE_TYPE_BIN) { //read binary
int eeprom_size = file.size();
if (!file.open(QFile::ReadOnly)) { //reading binary file - TODO HEX support
QMessageBox::critical(this, tr("Error"),
tr("Error opening file %1:\n%2.")
.arg(fileName)
.arg(file.errorString()));
return false;
}
uint8_t *eeprom = (uint8_t *)malloc(eeprom_size);
memset(eeprom, 0, eeprom_size);
long result = file.read((char*)eeprom, eeprom_size);
file.close();
if (result != eeprom_size) {
QMessageBox::critical(this, tr("Error"),
tr("Error reading file %1:\n%2.")
.arg(fileName)
.arg(file.errorString()));
free(eeprom);
return false;
}
std::bitset<NUM_ERRORS> errorsEeprom((unsigned long long)LoadEeprom(radioData, eeprom, eeprom_size));
if (!errorsEeprom.test(ALL_OK)) {
std::bitset<NUM_ERRORS> errorsBackup((unsigned long long)LoadBackup(radioData, eeprom, eeprom_size, 0));
if (!errorsBackup.test(ALL_OK)) {
ShowEepromErrors(this, tr("Error"), tr("Invalid binary EEPROM File %1").arg(fileName), (errorsEeprom | errorsBackup).to_ulong());
free(eeprom);
return false;
}
if (errorsBackup.test(HAS_WARNINGS)) {
ShowEepromWarnings(this, tr("Warning"), errorsBackup.to_ulong());
}
} else if (errorsEeprom.test(HAS_WARNINGS)) {
ShowEepromWarnings(this, tr("Warning"), errorsEeprom.to_ulong());
}
ui->modelsList->refreshList();
if(resetCurrentFile) setCurrentFile(fileName);
free(eeprom);
return true;
}
else if (fileType == FILE_TYPE_OTX) { //read zip archive
return loadOtxFile(fileName, resetCurrentFile);
else if (errorsEeprom.test(HAS_WARNINGS)) {
ShowEepromWarnings(this, tr("Warning"), errorsEeprom.to_ulong());
}
return false;
ui->modelsList->refreshList();
if (resetCurrentFile) setCurrentFile(fileName);
free(eeprom);
return true;
}
else if (fileType == FILE_TYPE_OTX) { //read zip archive
return loadOtxFile(fileName, resetCurrentFile);
}
return false;
}
bool MdiChild::loadOtxFile(const QString &fileName, bool resetCurrentFile)
@@ -394,9 +395,9 @@ bool MdiChild::loadOtxFile(const QString &fileName, bool resetCurrentFile)
}
for (QList<ModelFile>::iterator i = storage.models.begin(); i != storage.models.end(); ++i) {
GetEepromInterface()->loadModelFromByteArray(radioData.models[0], i->data);
}
// for test immediately save to another file
storage.write(fileName + "test.otx");
return true;
+1 -1
View File
@@ -7,7 +7,7 @@ unsigned long LoadEeprom(RadioData & radioData, const uint8_t * eeprom, const in
{
std::bitset<NUM_ERRORS> errors;
foreach(EEPROMInterface *eepromInterface, eepromInterfaces) {
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();