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"
25 #include "firmwareinterface.h"
26 #include "eeprominterface.h"
29 StorageType
getStorageType(const QString
& filename
)
31 QString suffix
= QFileInfo(filename
).suffix().toUpper();
33 return STORAGE_TYPE_HEX
;
34 else if (suffix
== "BIN")
35 return STORAGE_TYPE_BIN
;
36 else if (suffix
== "EEPM")
37 return STORAGE_TYPE_EEPM
;
38 else if (suffix
== "EEPE")
39 return STORAGE_TYPE_EEPE
;
40 else if (suffix
== "XML")
41 return STORAGE_TYPE_XML
;
42 else if (suffix
== "OTX")
43 return STORAGE_TYPE_OTX
;
45 return STORAGE_TYPE_UNKNOWN
;
48 void registerStorageFactory(StorageFactory
* factory
);
50 QList
<StorageFactory
*> registeredStorageFactories
;
52 void registerStorageFactory(StorageFactory
* factory
)
54 qDebug() << "register storage" << factory
->name();
55 registeredStorageFactories
.push_back(factory
);
58 void registerStorageFactories()
60 registerStorageFactory(new DefaultStorageFactory
<BinEepromFormat
>("bin"));
61 registerStorageFactory(new DefaultStorageFactory
<EepeFormat
>("eepe"));
62 registerStorageFactory(new DefaultStorageFactory
<HexEepromFormat
>("hex"));
63 registerStorageFactory(new DefaultStorageFactory
<OtxFormat
>("otx"));
64 registerStorageFactory(new SdcardStorageFactory());
67 void unregisterStorageFactories()
69 foreach (StorageFactory
* factory
, registeredStorageFactories
)
73 bool Storage::load(RadioData
& radioData
)
77 setError(QObject::tr("Unable to find file %1!").arg(filename
));
82 foreach(StorageFactory
* factory
, registeredStorageFactories
) {
83 StorageFormat
* format
= factory
->instance(filename
);
84 if (format
->load(radioData
)) {
85 board
= format
->getBoard();
86 setWarning(format
->warning());
91 setError(format
->error());
99 bool Storage::write(const RadioData
& radioData
)
102 foreach(StorageFactory
* factory
, registeredStorageFactories
) {
103 if (factory
->probe(filename
)) {
104 StorageFormat
* format
= factory
->instance(filename
);
105 ret
= format
->write(radioData
);
113 bool convertEEprom(const QString
& sourceEEprom
, const QString
& destinationEEprom
, const QString
& firmwareFilename
)
115 FirmwareInterface
firmware(firmwareFilename
);
116 if (!firmware
.isValid())
119 uint8_t version
= firmware
.getEEpromVersion();
120 unsigned int variant
= firmware
.getEEpromVariant();
122 QSharedPointer
<RadioData
> radioData
= QSharedPointer
<RadioData
>(new RadioData());
123 Storage
storage(sourceEEprom
);
124 if (!storage
.load(*radioData
))
127 QByteArray
eeprom(Boards::getEEpromSize(Board::BOARD_UNKNOWN
), 0);
128 int size
= getCurrentEEpromInterface()->save((uint8_t *)eeprom
.data(), *radioData
, version
, variant
);
133 QFile
destinationFile(destinationEEprom
);
134 if (!destinationFile
.open(QIODevice::WriteOnly
)) {
138 int result
= destinationFile
.write(eeprom
.constData(), size
);
139 destinationFile
.close();
140 return (result
== size
);
144 unsigned long LoadBackup(RadioData
& radioData
, uint8_t * eeprom
, int size
, int index
)
146 std::bitset
<NUM_ERRORS
> errors
;
148 foreach(EEPROMInterface
*eepromInterface
, eepromInterfaces
) {
149 std::bitset
<NUM_ERRORS
> result((unsigned long long)eepromInterface
->loadBackup(radioData
, eeprom
, size
, index
));
150 if (result
.test(ALL_OK
)) {
151 return result
.to_ulong();
159 errors
.set(UNKNOWN_ERROR
);
161 return errors
.to_ulong();
165 unsigned long LoadEepromXml(RadioData
& radioData
, QDomDocument
& doc
)
167 std::bitset
<NUM_ERRORS
> errors
;
169 foreach(EEPROMInterface
*eepromInterface
, eepromInterfaces
) {
170 std::bitset
<NUM_ERRORS
> result((unsigned long long)eepromInterface
->loadxml(radioData
, doc
));
171 if (result
.test(ALL_OK
)) {
172 return result
.to_ulong();
180 errors
.set(UNKNOWN_ERROR
);
182 return errors
.to_ulong();