Files
edgetx-gx12/companion/src/storage/storage.h
T
Max PapernoandBertrand Songis 4aa0c1bbe4 [simulation] Fix some potential crashes & 26 memory leaks, improve LCD redraws. (#4634)
* [simulation] Hardening: Fix some potential crashes & 26 memory leaks, improve LCD performance.
  * [simpgmspace] Init trims; Make sure `REa` is really defined (to match board files).
  * [simpgmspace][opentxsimulator] Verify current running state before start/stop; Move rotary enc. init.
  * [simpgmspace][LcdWidget] Improve performance by moving LCD content change check to lcdRefresh() & limiting LcdWidget refresh time to 60 fps max.
  * [simueeprom] Ensure thread could be started, set default running state to false.
  * [simufatfs] Fix paths report trace.
  * [eepromimportexport] Fix memory leaks resulting from import debugging scheme being used.
  * [customdebug] Introduce new scheme for custom debug output in compliance with Qt recommendations (see docs for QLoggingCategory).
  * [opentxeeprom] Fix extra conversion table cache elements being created and also not properly deleted (and hence leaking).
  * [opentxinterface] Unregister EEpromInterfaces in unregisterOpenTxFirmwares();
  * [storage] Unregister storage factories on exit (fixes leak); create virtual StorageFormat/StorageFactory destructors (prevents warnings).
  * [helpers] GVarGroup now emits own signal, no need to pass ModelPanel pointer (removes dependency on modeledit.h)
  * [DebugOutput] Clear simulator trace hook before exiting (prevent possible issues); Fix leak and possible bad QString allocations when reading from buffer; Fix leak with combo box event filter.
  * [TelemetrySimulator] Fix leak by deleting LogPlaybackController object on exit; Only set up data fields once; convert timers to static.
  * [build] Consolidate all Companion/Simulator shared items in `common` library to reduce build time/etc (node & edge still remain awkward).

* [simulatorwidget] Delete removed spacer object (previously-forgotten "26th" leak).

* [DebugOutput] Fix stray trailing characters issue with new text buffer allocation (from previous commit); Increase maximum buffer sizes to better accommodate slower systems.

* Cosmetics.
2017-03-21 07:43:40 +01:00

177 lines
3.8 KiB
C++

/*
* Copyright (C) OpenTX
*
* Based on code named
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#ifndef _STORAGE_H_
#define _STORAGE_H_
#include "radiodata.h"
#include <QString>
#include <QDebug>
enum StorageType
{
STORAGE_TYPE_UNKNOWN,
STORAGE_TYPE_BIN,
STORAGE_TYPE_HEX,
STORAGE_TYPE_EEPE,
STORAGE_TYPE_EEPM,
STORAGE_TYPE_XML,
STORAGE_TYPE_SDCARD,
STORAGE_TYPE_OTX
};
StorageType getStorageType(const QString & filename);
class StorageFormat
{
public:
StorageFormat(const QString & filename, uint8_t version=0):
filename(filename),
version(version),
board(Board::BOARD_UNKNOWN)
{
}
virtual ~StorageFormat() {}
virtual bool load(RadioData & radioData) = 0;
virtual bool write(const RadioData & radioData) = 0;
QString error() {
return _error;
}
QString warning() {
return _warning;
}
virtual QString name() = 0;
virtual Board::Type getBoard()
{
return board;
}
virtual bool isBoardCompatible(Board::Type board)
{
if (this->board == board)
return true;
if (IS_TARANIS_X9D(this->board) && IS_TARANIS_X9D(board))
return true;
return false;
}
protected:
void setError(const QString & error)
{
qDebug() << qPrintable(QString("[%1] error: %2").arg(name()).arg(error));
_error = error;
}
void setWarning(const QString & warning)
{
qDebug() << qPrintable(QString("[%1] warning: %2").arg(name()).arg(warning));
_warning = warning;
}
QString filename;
uint8_t version;
QString _error;
QString _warning;
Board::Type board;
};
class StorageFactory
{
public:
StorageFactory()
{
}
virtual ~StorageFactory() {}
virtual QString name() = 0;
virtual bool probe(const QString & filename) = 0;
virtual StorageFormat * instance(const QString & filename) = 0;
};
template <class T>
class DefaultStorageFactory : public StorageFactory
{
public:
DefaultStorageFactory(const QString & name):
StorageFactory(),
_name(name)
{
}
virtual QString name()
{
return _name;
}
virtual bool probe(const QString & filename)
{
return filename.toLower().endsWith("." + _name);
}
virtual StorageFormat * instance(const QString & filename)
{
return new T(filename);
}
QString _name;
};
class Storage : public StorageFormat
{
public:
Storage(const QString & filename):
StorageFormat(filename)
{
}
virtual QString name() { return "storage"; }
void setError(const QString & error)
{
_error = error;
}
void setWarning(const QString & warning)
{
_warning = warning;
}
virtual bool load(RadioData & radioData);
virtual bool write(const RadioData & radioData);
};
void registerStorageFactories();
void unregisterStorageFactories();
#if 0
unsigned long LoadBackup(RadioData &radioData, uint8_t *eeprom, int esize, int index);
unsigned long LoadEeprom(RadioData &radioData, const uint8_t *eeprom, int size);
unsigned long LoadEepromXml(RadioData &radioData, QDomDocument &doc);
#endif
bool convertEEprom(const QString & sourceEEprom, const QString & destinationEEprom, const QString & firmware);
#endif // _STORAGE_H_