Also play model name on model changes (#5416)
[opentx.git] / companion / src / storage / storage.cpp
blobcd9b5bac6a295b07f0aef0c37d13cc9fb4846ab4
1 /*
2 * Copyright (C) OpenTX
4 * Based on code named
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 "eepe.h"
23 #include "otx.h"
24 #include "sdcard.h"
25 #include "firmwareinterface.h"
26 #include "eeprominterface.h"
27 #include <QFileInfo>
29 StorageType getStorageType(const QString & filename)
31 QString suffix = QFileInfo(filename).suffix().toUpper();
32 if (suffix == "HEX")
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;
44 else
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)
70 delete factory;
73 bool Storage::load(RadioData & radioData)
75 QFile file(filename);
76 if (!file.exists()) {
77 setError(QObject::tr("Unable to find file %1!").arg(filename));
78 return false;
81 bool ret = false;
82 foreach(StorageFactory * factory, registeredStorageFactories) {
83 StorageFormat * format = factory->instance(filename);
84 if (format->load(radioData)) {
85 board = format->getBoard();
86 setWarning(format->warning());
87 ret = true;
88 break;
90 else {
91 setError(format->error());
93 delete format;
96 return ret;
99 bool Storage::write(const RadioData & radioData)
101 bool ret = false;
102 foreach(StorageFactory * factory, registeredStorageFactories) {
103 if (factory->probe(filename)) {
104 StorageFormat * format = factory->instance(filename);
105 ret = format->write(radioData);
106 delete format;
107 break;
110 return ret;
113 bool convertEEprom(const QString & sourceEEprom, const QString & destinationEEprom, const QString & firmwareFilename)
115 FirmwareInterface firmware(firmwareFilename);
116 if (!firmware.isValid())
117 return false;
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))
125 return false;
127 QByteArray eeprom(Boards::getEEpromSize(Board::BOARD_UNKNOWN), 0);
128 int size = getCurrentEEpromInterface()->save((uint8_t *)eeprom.data(), *radioData, version, variant);
129 if (size == 0) {
130 return false;
133 QFile destinationFile(destinationEEprom);
134 if (!destinationFile.open(QIODevice::WriteOnly)) {
135 return false;
138 int result = destinationFile.write(eeprom.constData(), size);
139 destinationFile.close();
140 return (result == size);
143 #if 0
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();
153 else {
154 errors |= result;
158 if (errors.none()) {
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();
174 else {
175 errors |= result;
179 if (errors.none()) {
180 errors.set(UNKNOWN_ERROR);
182 return errors.to_ulong();
184 #endif