[companion] Adjust GVAR not possible in global functions (fix #5425)
[opentx.git] / companion / src / storage / storage.h
blob8c4f76a3cb2d29413cd0338ca79b7532e1e0876a
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 #ifndef _STORAGE_H_
22 #define _STORAGE_H_
24 #include "radiodata.h"
25 #include <QString>
26 #include <QDebug>
28 enum StorageType
30 STORAGE_TYPE_UNKNOWN,
31 STORAGE_TYPE_BIN,
32 STORAGE_TYPE_HEX,
33 STORAGE_TYPE_EEPE,
34 STORAGE_TYPE_EEPM,
35 STORAGE_TYPE_XML,
36 STORAGE_TYPE_SDCARD,
37 STORAGE_TYPE_OTX
40 StorageType getStorageType(const QString & filename);
42 class StorageFormat
44 public:
45 StorageFormat(const QString & filename, uint8_t version=0):
46 filename(filename),
47 version(version),
48 board(Board::BOARD_UNKNOWN)
51 virtual ~StorageFormat() {}
52 virtual bool load(RadioData & radioData) = 0;
53 virtual bool write(const RadioData & radioData) = 0;
55 QString error() {
56 return _error;
59 QString warning() {
60 return _warning;
63 virtual QString name() = 0;
65 virtual Board::Type getBoard()
67 return board;
70 static uint32_t getFourCC(Board::Type board)
72 switch (board) {
73 case Board::BOARD_X12S:
74 case Board::BOARD_X10:
75 return 0x3478746F;
76 case Board::BOARD_TARANIS_X7:
77 return 0x3678746F;
78 case Board::BOARD_TARANIS_X9E:
79 return 0x3578746F;
80 case Board::BOARD_TARANIS_X9D:
81 case Board::BOARD_TARANIS_X9DP:
82 return 0x3378746F;
83 case Board::BOARD_SKY9X:
84 case Board::BOARD_AR9X:
85 case Board::BOARD_9XRPRO:
86 return 0x3278746F;
87 case Board::BOARD_MEGA2560:
88 case Board::BOARD_GRUVIN9X:
89 return 0x3178746F;
90 default:
91 return 0;
95 uint32_t getFourCC()
97 return getFourCC(board);
100 virtual bool isBoardCompatible(Board::Type board)
102 return getFourCC() == getFourCC(board);
105 protected:
106 void setError(const QString & error)
108 qDebug() << qPrintable(QString("[%1] error: %2").arg(name()).arg(error));
109 _error = error;
112 void setWarning(const QString & warning)
114 qDebug() << qPrintable(QString("[%1] warning: %2").arg(name()).arg(warning));
115 _warning = warning;
118 QString filename;
119 uint8_t version;
120 QString _error;
121 QString _warning;
122 Board::Type board;
125 class StorageFactory
127 public:
128 StorageFactory()
131 virtual ~StorageFactory() {}
132 virtual QString name() = 0;
133 virtual bool probe(const QString & filename) = 0;
134 virtual StorageFormat * instance(const QString & filename) = 0;
137 template <class T>
138 class DefaultStorageFactory : public StorageFactory
140 public:
141 DefaultStorageFactory(const QString & name):
142 StorageFactory(),
143 _name(name)
147 virtual QString name()
149 return _name;
152 virtual bool probe(const QString & filename)
154 return filename.toLower().endsWith("." + _name);
157 virtual StorageFormat * instance(const QString & filename)
159 return new T(filename);
162 QString _name;
165 class Storage : public StorageFormat
167 public:
168 Storage(const QString & filename):
169 StorageFormat(filename)
173 virtual QString name() { return "storage"; }
175 void setError(const QString & error)
177 _error = error;
180 void setWarning(const QString & warning)
182 _warning = warning;
185 virtual bool load(RadioData & radioData);
186 virtual bool write(const RadioData & radioData);
189 void registerStorageFactories();
190 void unregisterStorageFactories();
192 #if 0
193 unsigned long LoadBackup(RadioData &radioData, uint8_t *eeprom, int esize, int index);
194 unsigned long LoadEeprom(RadioData &radioData, const uint8_t *eeprom, int size);
195 unsigned long LoadEepromXml(RadioData &radioData, QDomDocument &doc);
196 #endif
198 bool convertEEprom(const QString & sourceEEprom, const QString & destinationEEprom, const QString & firmware);
200 #endif // _STORAGE_H_