diff --git a/companion/src/eepromimportexport.h b/companion/src/eepromimportexport.h index e647f18f0..08f90a72e 100644 --- a/companion/src/eepromimportexport.h +++ b/companion/src/eepromimportexport.h @@ -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: 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 -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 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(); diff --git a/companion/src/firmwares/opentx/opentxinterface.h b/companion/src/firmwares/opentx/opentxinterface.h index e4ed72b42..521fe7e5b 100644 --- a/companion/src/firmwares/opentx/opentxinterface.h +++ b/companion/src/firmwares/opentx/opentxinterface.h @@ -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 bool loadModel(ModelData &model, uint8_t *data, int index, unsigned int stickMode=0); diff --git a/companion/src/mdichild.cpp b/companion/src/mdichild.cpp index 95bddb0bf..9fb84e2e7 100644 --- a/companion/src/mdichild.cpp +++ b/companion/src/mdichild.cpp @@ -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 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 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 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 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 errorsEeprom((unsigned long long)LoadEeprom(radioData, eeprom, eeprom_size)); + if (!errorsEeprom.test(ALL_OK)) { + std::bitset 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 errorsEeprom((unsigned long long)LoadEeprom(radioData, eeprom, eeprom_size)); - if (!errorsEeprom.test(ALL_OK)) { - std::bitset 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::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; diff --git a/companion/src/storage/storage_eeprom.cpp b/companion/src/storage/storage_eeprom.cpp index e265b4347..460e46e0e 100644 --- a/companion/src/storage/storage_eeprom.cpp +++ b/companion/src/storage/storage_eeprom.cpp @@ -7,7 +7,7 @@ unsigned long LoadEeprom(RadioData & radioData, const uint8_t * eeprom, const in { std::bitset errors; - foreach(EEPROMInterface *eepromInterface, eepromInterfaces) { + foreach(EEPROMInterface * eepromInterface, eepromInterfaces) { std::bitset result((unsigned long long)eepromInterface->load(radioData, eeprom, size)); if (result.test(ALL_OK)) { return result.to_ulong();