{Companion] Unload simulator libraries after each use -- reset all statically initialized firmware variables. (#4655)
* [simulatormainwindow] Move sub-window destructors to main destructor. * [simulation] Move SimulatorInterface creation/ownership to SimulatorMainWindow. * [simulation] Add option to unload simulator libraries after each use (eg. from Companion) so as to properly reset all statically initialized firmware variables; Introduce new SimulatorLoader class.
This commit is contained in:
committed by
Bertrand Songis
parent
dc89d2d630
commit
e48b1fd07d
@@ -19,38 +19,29 @@
|
||||
*/
|
||||
|
||||
#include "simulatorinterface.h"
|
||||
#include <QDebug>
|
||||
#include <QDir>
|
||||
#include <QLibrary>
|
||||
#include <QLibraryInfo>
|
||||
#include <QMap>
|
||||
#include <QMessageBox>
|
||||
#include "customdebug.h"
|
||||
#include "version.h"
|
||||
|
||||
#include <QDebug>
|
||||
#include <QLibraryInfo>
|
||||
|
||||
#if defined _MSC_VER || !defined __GNUC__
|
||||
#include <windows.h>
|
||||
#endif
|
||||
|
||||
QMap<QString, SimulatorFactory *> registered_simulators;
|
||||
#ifndef SIMULATOR_INTERFACE_LOADER_METHOD
|
||||
#define SIMULATOR_INTERFACE_LOADER_DYNAMIC 1 // How to load simulator libraries: 1=dynamic load and unload; 0=load once (old way)
|
||||
#endif
|
||||
|
||||
void registerSimulator(const QString & filename)
|
||||
QMap<QString, QLibrary *> SimulatorLoader::registeredSimulators;
|
||||
|
||||
QStringList SimulatorLoader::getAvailableSimulators()
|
||||
{
|
||||
QLibrary lib(filename);
|
||||
typedef SimulatorFactory * (*RegisterSimulator)();
|
||||
qDebug() << "trying to register simulator in " << filename;
|
||||
RegisterSimulator registerFunc = (RegisterSimulator)lib.resolve("registerSimu");
|
||||
if (registerFunc) {
|
||||
SimulatorFactory * factory = registerFunc();
|
||||
registered_simulators[factory->name()] = factory;
|
||||
qDebug() << "Registered" << factory->name() << "simulator";
|
||||
}
|
||||
else {
|
||||
qWarning() << "Library error" << filename << lib.errorString();
|
||||
}
|
||||
return registeredSimulators.keys();
|
||||
}
|
||||
|
||||
int registerSimulators(const QDir & dir)
|
||||
int SimulatorLoader::registerSimulators(const QDir & dir)
|
||||
{
|
||||
int noSimulatorsFound = 0;
|
||||
QStringList filters;
|
||||
#if defined(__APPLE__)
|
||||
filters << "*-simulator.dylib";
|
||||
@@ -59,17 +50,41 @@ int registerSimulators(const QDir & dir)
|
||||
#else
|
||||
filters << "*-simulator.so";
|
||||
#endif
|
||||
registeredSimulators.clear();
|
||||
|
||||
qCDebug(simulatorInterfaceLoader) << "Searching for simulators in" << dir.path() << "matching pattern" << filters;
|
||||
|
||||
qDebug() << "Searching for simulators in" << dir.path();
|
||||
foreach(QString filename, dir.entryList(filters, QDir::Files)) {
|
||||
QString libraryFilename = dir.path() + "/" + filename;
|
||||
registerSimulator(libraryFilename);
|
||||
noSimulatorsFound++;
|
||||
QLibrary * lib = new QLibrary( dir.path() + "/" + filename);
|
||||
|
||||
qCDebug(simulatorInterfaceLoader) << "Trying to register simulator in " << filename;
|
||||
|
||||
SimulatorFactory * factory;
|
||||
RegisterSimulator registerFunc = (RegisterSimulator)lib->resolve("registerSimu");
|
||||
|
||||
if (registerFunc && (factory = registerFunc())) {
|
||||
if (getAvailableSimulators().contains(factory->name()))
|
||||
continue;
|
||||
|
||||
lib->setProperty("instances_used", 0);
|
||||
registeredSimulators.insert(factory->name(), lib);
|
||||
delete factory;
|
||||
#if SIMULATOR_INTERFACE_LOADER_DYNAMIC
|
||||
lib->unload();
|
||||
#endif
|
||||
qCDebug(simulatorInterfaceLoader) << "Registered" << registeredSimulators.lastKey() << "simulator in " << lib->fileName() << "and unloaded:" << !lib->isLoaded();
|
||||
}
|
||||
else {
|
||||
qWarning() << "Library error" << lib->fileName() << lib->errorString();
|
||||
delete lib;
|
||||
}
|
||||
|
||||
}
|
||||
return noSimulatorsFound;
|
||||
qCDebug(simulatorInterfaceLoader) << "Found libraries:" << (registeredSimulators.size() ? registeredSimulators.keys() : QStringList() << "none");
|
||||
return registeredSimulators.size();
|
||||
}
|
||||
|
||||
void registerSimulators()
|
||||
void SimulatorLoader::registerSimulators()
|
||||
{
|
||||
QDir dir(".");
|
||||
if (registerSimulators(dir)) {
|
||||
@@ -90,31 +105,93 @@ void registerSimulators()
|
||||
registerSimulators(dir);
|
||||
}
|
||||
|
||||
SimulatorFactory * getSimulatorFactory(const QString & name)
|
||||
void SimulatorLoader::unregisterSimulators()
|
||||
{
|
||||
foreach(QLibrary * lib, registeredSimulators)
|
||||
delete lib;
|
||||
}
|
||||
|
||||
QString SimulatorLoader::findSimulatorByFirmwareName(const QString & name)
|
||||
{
|
||||
int pos;
|
||||
QString ret;
|
||||
QString simuName = name;
|
||||
|
||||
while(1) {
|
||||
qDebug() << "searching" << simuName << "simulator";
|
||||
foreach (QString name, registered_simulators.keys()) {
|
||||
if (name.contains(simuName)) {
|
||||
simuName = name;
|
||||
qDebug() << "found" << simuName;
|
||||
return registered_simulators[simuName];
|
||||
}
|
||||
qCDebug(simulatorInterfaceLoader) << "searching" << simuName << "simulator";
|
||||
if (registeredSimulators.contains(simuName)) {
|
||||
ret = simuName;
|
||||
break;
|
||||
}
|
||||
int pos = simuName.lastIndexOf('-');
|
||||
if (pos <= 0)
|
||||
if ((pos = simuName.lastIndexOf('-')) <= 0)
|
||||
break;
|
||||
simuName = simuName.mid(0, pos);
|
||||
if (simuName.count('-') == 0)
|
||||
break;
|
||||
}
|
||||
return NULL;
|
||||
return ret;
|
||||
}
|
||||
|
||||
void unregisterSimulators()
|
||||
SimulatorInterface * SimulatorLoader::loadSimulator(const QString & name)
|
||||
{
|
||||
foreach(SimulatorFactory *factory, registered_simulators) {
|
||||
SimulatorInterface * si = NULL;
|
||||
QString libname = findSimulatorByFirmwareName(name);
|
||||
|
||||
if (libname.isEmpty()) {
|
||||
qWarning() << "Simulator" << name << "not found.";
|
||||
return si;
|
||||
}
|
||||
|
||||
QLibrary * lib = registeredSimulators.value(libname, NULL);
|
||||
if (!lib) {
|
||||
qWarning() << "Simulator library is NULL";
|
||||
return si;
|
||||
}
|
||||
|
||||
qCDebug(simulatorInterfaceLoader) << "Trying to load simulator in " << lib->fileName();
|
||||
|
||||
SimulatorFactory * factory;
|
||||
RegisterSimulator registerFunc = (RegisterSimulator)lib->resolve("registerSimu");
|
||||
if (registerFunc && (factory = registerFunc()) && (si = factory->create())) {
|
||||
quint8 instance = lib->property("instances_used").toUInt();
|
||||
lib->setProperty("instances_used", ++instance);
|
||||
qCDebug(simulatorInterfaceLoader) << "Loaded" << factory->name() << "simulator instance" << instance;
|
||||
delete factory;
|
||||
}
|
||||
else {
|
||||
qWarning() << "Library error" << lib->fileName() << lib->errorString();
|
||||
}
|
||||
return si;
|
||||
}
|
||||
|
||||
bool SimulatorLoader::unloadSimulator(const QString & name)
|
||||
{
|
||||
bool ret = false;
|
||||
#if SIMULATOR_INTERFACE_LOADER_DYNAMIC
|
||||
QString simuName = findSimulatorByFirmwareName(name);
|
||||
if (simuName.isEmpty())
|
||||
return ret;
|
||||
|
||||
QLibrary * lib = registeredSimulators.value(simuName, NULL);
|
||||
|
||||
if (lib && lib->isLoaded()) {
|
||||
quint8 instance = lib->property("instances_used").toUInt();
|
||||
lib->setProperty("instances_used", --instance);
|
||||
if (!instance) {
|
||||
ret = lib->unload();
|
||||
qCDebug(simulatorInterfaceLoader) << "Unloading" << simuName << "(" << lib->fileName() << ")" << "result:" << ret;
|
||||
}
|
||||
else {
|
||||
ret = true;
|
||||
qCDebug(simulatorInterfaceLoader) << "Simulator" << simuName << "instances remaining:" << instance;
|
||||
}
|
||||
}
|
||||
else {
|
||||
qCDebug(simulatorInterfaceLoader) << "Simulator library for " << simuName << "already unloaded.";
|
||||
}
|
||||
#else
|
||||
qCDebug(simulatorInterfaceLoader) << "Keeping simulator library" << simuName << "loaded.";
|
||||
#endif
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -23,11 +23,16 @@
|
||||
|
||||
#include "boards.h"
|
||||
#include "constants.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <inttypes.h>
|
||||
|
||||
#include <QObject>
|
||||
#include <QString>
|
||||
#include <QByteArray>
|
||||
#include <QDir>
|
||||
#include <QLibrary>
|
||||
#include <QMap>
|
||||
#include <algorithm>
|
||||
|
||||
struct TxInputs
|
||||
{
|
||||
@@ -122,9 +127,21 @@ class SimulatorFactory {
|
||||
virtual SimulatorInterface *create() = 0;
|
||||
};
|
||||
|
||||
void registerSimulators();
|
||||
void unregisterSimulators();
|
||||
SimulatorFactory *getSimulatorFactory(const QString &name);
|
||||
extern QMap<QString, SimulatorFactory *> registered_simulators;
|
||||
class SimulatorLoader
|
||||
{
|
||||
public:
|
||||
static void registerSimulators();
|
||||
static void unregisterSimulators();
|
||||
static QStringList getAvailableSimulators();
|
||||
static QString findSimulatorByFirmwareName(const QString & name);
|
||||
static SimulatorInterface * loadSimulator(const QString & name);
|
||||
static bool unloadSimulator(const QString & name);
|
||||
|
||||
protected:
|
||||
typedef SimulatorFactory * (*RegisterSimulator)();
|
||||
|
||||
static int registerSimulators(const QDir & dir);
|
||||
static QMap<QString, QLibrary *> registeredSimulators;
|
||||
};
|
||||
|
||||
#endif // _SIMULATORINTERFACE_H_
|
||||
|
||||
@@ -40,9 +40,8 @@ extern AppData g; // ensure what "g" means
|
||||
|
||||
const quint16 SimulatorMainWindow::m_savedUiStateVersion = 2;
|
||||
|
||||
SimulatorMainWindow::SimulatorMainWindow(QWidget *parent, SimulatorInterface * simulator, quint8 flags, Qt::WindowFlags wflags) :
|
||||
SimulatorMainWindow::SimulatorMainWindow(QWidget *parent, const QString & firmwareId, quint8 flags, Qt::WindowFlags wflags) :
|
||||
QMainWindow(parent, wflags),
|
||||
m_simulator(simulator),
|
||||
ui(new Ui::SimulatorMainWindow),
|
||||
m_simulatorWidget(NULL),
|
||||
m_consoleWidget(NULL),
|
||||
@@ -52,12 +51,24 @@ SimulatorMainWindow::SimulatorMainWindow(QWidget *parent, SimulatorInterface * s
|
||||
m_telemetryDockWidget(NULL),
|
||||
m_trainerDockWidget(NULL),
|
||||
m_outputsDockWidget(NULL),
|
||||
m_simulatorId(firmwareId),
|
||||
m_exitStatusCode(0),
|
||||
m_radioProfileId(g.sessionId()),
|
||||
m_radioSizeConstraint(Qt::Horizontal | Qt::Vertical),
|
||||
m_firstShow(true),
|
||||
m_showRadioDocked(true),
|
||||
m_showMenubar(true)
|
||||
{
|
||||
if (m_simulatorId.isEmpty()) {
|
||||
m_simulatorId = SimulatorLoader::findSimulatorByFirmwareName(getCurrentFirmware()->getId());
|
||||
}
|
||||
m_simulator = SimulatorLoader::loadSimulator(m_simulatorId);
|
||||
if (!m_simulator) {
|
||||
m_exitStatusMsg = tr("ERROR: Failed to create simulator interface, possibly missing or bad library.");
|
||||
m_exitStatusCode = -1;
|
||||
return;
|
||||
}
|
||||
|
||||
ui->setupUi(this);
|
||||
|
||||
setCorner(Qt::TopLeftCorner, Qt::LeftDockWidgetArea);
|
||||
@@ -128,25 +139,30 @@ SimulatorMainWindow::SimulatorMainWindow(QWidget *parent, SimulatorInterface * s
|
||||
|
||||
SimulatorMainWindow::~SimulatorMainWindow()
|
||||
{
|
||||
delete ui;
|
||||
}
|
||||
|
||||
void SimulatorMainWindow::closeEvent(QCloseEvent *)
|
||||
{
|
||||
saveUiState();
|
||||
|
||||
if (m_telemetryDockWidget)
|
||||
delete m_telemetryDockWidget;
|
||||
if (m_trainerDockWidget)
|
||||
delete m_trainerDockWidget;
|
||||
if (m_outputsDockWidget)
|
||||
delete m_outputsDockWidget;
|
||||
if (m_consoleDockWidget)
|
||||
delete m_consoleDockWidget;
|
||||
if (m_simulatorDockWidget)
|
||||
delete m_simulatorDockWidget;
|
||||
else if (m_simulatorWidget)
|
||||
delete m_simulatorWidget;
|
||||
if (m_consoleDockWidget)
|
||||
delete m_consoleDockWidget;
|
||||
|
||||
delete ui;
|
||||
|
||||
if (m_simulator)
|
||||
delete m_simulator;
|
||||
|
||||
SimulatorLoader::unloadSimulator(m_simulatorId);
|
||||
}
|
||||
|
||||
void SimulatorMainWindow::closeEvent(QCloseEvent *)
|
||||
{
|
||||
saveUiState();
|
||||
}
|
||||
|
||||
void SimulatorMainWindow::show()
|
||||
@@ -173,7 +189,8 @@ void SimulatorMainWindow::changeEvent(QEvent *e)
|
||||
}
|
||||
}
|
||||
|
||||
QMenu * SimulatorMainWindow::createPopupMenu(){
|
||||
QMenu * SimulatorMainWindow::createPopupMenu()
|
||||
{
|
||||
QMenu * menu = QMainWindow::createPopupMenu();
|
||||
menu->clear();
|
||||
menu->addActions(ui->menuView->actions());
|
||||
@@ -214,6 +231,13 @@ void SimulatorMainWindow::restoreUiState()
|
||||
restoreState(windowState, m_savedUiStateVersion);
|
||||
}
|
||||
|
||||
int SimulatorMainWindow::getExitStatus(QString * msg)
|
||||
{
|
||||
if (msg)
|
||||
*msg = m_exitStatusMsg;
|
||||
return m_exitStatusCode;
|
||||
}
|
||||
|
||||
bool SimulatorMainWindow::setRadioData(RadioData * radioData)
|
||||
{
|
||||
return m_simulatorWidget->setRadioData(radioData);
|
||||
|
||||
@@ -25,7 +25,6 @@
|
||||
|
||||
#include <QDockWidget>
|
||||
#include <QMainWindow>
|
||||
#include <QPointer>
|
||||
|
||||
class DebugOutput;
|
||||
class RadioData;
|
||||
@@ -48,13 +47,14 @@ class SimulatorMainWindow : public QMainWindow
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit SimulatorMainWindow(QWidget * parent, SimulatorInterface * simulator, quint8 flags=0, Qt::WindowFlags wflags = Qt::WindowFlags());
|
||||
explicit SimulatorMainWindow(QWidget * parent, const QString & firmwareId = "", quint8 flags=0, Qt::WindowFlags wflags = Qt::WindowFlags());
|
||||
~SimulatorMainWindow();
|
||||
|
||||
int getExitStatus(QString * msg = Q_NULLPTR);
|
||||
bool setRadioData(RadioData * radioData);
|
||||
bool useTempDataPath(bool deleteOnClose = true);
|
||||
bool setOptions(SimulatorOptions & options, bool withSave = true);
|
||||
QMenu * createPopupMenu();
|
||||
virtual QMenu * createPopupMenu();
|
||||
|
||||
public slots:
|
||||
virtual void show();
|
||||
@@ -95,6 +95,9 @@ class SimulatorMainWindow : public QMainWindow
|
||||
QDockWidget * m_outputsDockWidget;
|
||||
|
||||
QVector<keymapHelp_t> m_keymapHelp;
|
||||
QString m_simulatorId;
|
||||
QString m_exitStatusMsg;
|
||||
int m_exitStatusCode;
|
||||
int m_radioProfileId;
|
||||
int m_radioSizeConstraint;
|
||||
bool m_firstShow;
|
||||
|
||||
@@ -23,13 +23,14 @@
|
||||
|
||||
#include "appdata.h"
|
||||
#include "constants.h"
|
||||
#include "eeprominterface.h"
|
||||
#include "simulatorinterface.h"
|
||||
#include "helpers.h"
|
||||
|
||||
#include <QFileDialog>
|
||||
|
||||
using namespace Simulator;
|
||||
|
||||
extern AppData g;
|
||||
extern QMap<QString, SimulatorFactory *> registered_simulators;
|
||||
|
||||
SimulatorStartupDialog::SimulatorStartupDialog(SimulatorOptions * options, int * profId, QWidget *parent) :
|
||||
QDialog(parent),
|
||||
@@ -45,7 +46,7 @@ SimulatorStartupDialog::SimulatorStartupDialog(SimulatorOptions * options, int *
|
||||
ui->radioProfile->addItem(pi.value(), pi.key());
|
||||
}
|
||||
|
||||
ui->radioType->addItems(registered_simulators.keys());
|
||||
ui->radioType->addItems(SimulatorLoader::getAvailableSimulators());
|
||||
|
||||
ui->optGrp_dataSource->setId(ui->optFile, SimulatorOptions::START_WITH_FILE);
|
||||
ui->optGrp_dataSource->setId(ui->optFolder, SimulatorOptions::START_WITH_FOLDER);
|
||||
@@ -151,7 +152,7 @@ void SimulatorStartupDialog::updateContainerTypes(void)
|
||||
|
||||
void SimulatorStartupDialog::loadRadioProfile(int id)
|
||||
{
|
||||
QString tmpstr;
|
||||
QString tmpstr, tmpstr2;
|
||||
int i;
|
||||
|
||||
if (id < 0 || !g.getActiveProfiles().contains(id))
|
||||
@@ -166,8 +167,8 @@ void SimulatorStartupDialog::loadRadioProfile(int id)
|
||||
tmpstr = m_options->firmwareId;
|
||||
if (tmpstr.isEmpty() && !g.profile[id].fwType().isEmpty())
|
||||
tmpstr = g.profile[id].fwType();
|
||||
else if (getSimulatorFactory(tmpstr))
|
||||
tmpstr = getSimulatorFactory(tmpstr)->name();
|
||||
else if (!(tmpstr2 = SimulatorLoader::findSimulatorByFirmwareName(m_options->firmwareId)).isEmpty())
|
||||
tmpstr = tmpstr2;
|
||||
i = ui->radioType->findText(findRadioId(tmpstr), Qt::MatchContains);
|
||||
if (i > -1)
|
||||
ui->radioType->setCurrentIndex(i);
|
||||
|
||||
@@ -28,8 +28,6 @@ namespace Ui {
|
||||
class SimulatorStartupDialog;
|
||||
}
|
||||
|
||||
using namespace Simulator;
|
||||
|
||||
class SimulatorStartupDialog : public QDialog
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
Reference in New Issue
Block a user