5 * th9x - http://code.google.com/p/th9x
6 * er9x - http://code.google.com/p/er9x
7 * gruvin9x - http://code.google.com/p/gruvin9x
9 * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
21 #include "bineeprom.h"
22 #include "eeprominterface.h"
26 // TODO should be RleFormat, RawFormat etc.
28 bool BinEepromFormat::load(RadioData
& radioData
)
32 int size
= file
.size();
34 if (!file
.open(QFile::ReadOnly
)) {
35 qDebug() << "Unable to open" << filename
<< file
.errorString();
39 QByteArray
eeprom(size
, 0);
40 int result
= file
.read((char *)eeprom
.data(), size
);
42 setError(QObject::tr("Error reading %1: %2").arg(filename
).arg(file
.errorString()));
46 return extract(radioData
, eeprom
);
49 bool BinEepromFormat::write(const RadioData
& radioData
)
52 EEPROMInterface
* eepromInterface
= getCurrentEEpromInterface();
53 int size
= Boards::getEEpromSize(eepromInterface
->getBoard());
57 uint8_t * eeprom
= (uint8_t *)malloc(size
);
58 int eeprom_size
= eepromInterface
->save(eeprom
, radioData
, 0, getCurrentFirmware()->getVariantNumber());
60 result
= writeToFile(eeprom
, eeprom_size
);
63 // TODO here we could call setError(eepromInterface->errors())
64 setError(QObject::tr("Cannot save EEPROM"));
71 bool BinEepromFormat::writeToFile(const uint8_t * eeprom
, uint32_t size
)
74 if (!file
.open(QIODevice::WriteOnly
)) {
75 setError(QObject::tr("Cannot open file %1:\n%2.").arg(filename
).arg(file
.errorString()));
79 QTextStream
outputStream(&file
);
80 qint64 len
= file
.write((char *)eeprom
, size
);
81 if (len
!= qint64(size
)) {
82 setError(QObject::tr("Error writing file %1:\n%2.").arg(filename
).arg(file
.errorString()));
89 bool BinEepromFormat::extract(RadioData
& radioData
, const QByteArray
& eeprom
)
91 std::bitset
<NUM_ERRORS
> errors
;
93 foreach(EEPROMInterface
* eepromInterface
, eepromInterfaces
) {
94 std::bitset
<NUM_ERRORS
> result((unsigned long long)eepromInterface
->load(radioData
, (uint8_t *)eeprom
.data(), eeprom
.size()));
95 if (result
.test(ALL_OK
)) {
96 if (errors
.test(HAS_WARNINGS
)) {
97 // TODO ShowEepromWarnings(this, QObject::tr("Warning"), errors.to_ulong());
99 board
= eepromInterface
->getBoard();
107 setError(QObject::tr("Invalid binary EEPROM file %1").arg(filename
));