Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert FirmwarePluginManager to Singleton #12079

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 32 additions & 20 deletions src/FirmwarePlugin/FirmwarePluginManager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,54 +14,67 @@
#include "FirmwarePluginManager.h"
#include "FirmwarePlugin.h"
#include "FirmwarePluginFactory.h"
#include "QGCLoggingCategory.h"

FirmwarePluginManager::FirmwarePluginManager(QGCApplication* app, QGCToolbox* toolbox)
: QGCTool(app, toolbox)
, _genericFirmwarePlugin(nullptr)
{
#include <QtCore/qapplicationstatic.h>

QGC_LOGGING_CATEGORY(FirmwarePluginManagerLog, "qgc.firmwareplugin.firmwarepluginmanager");

Q_APPLICATION_STATIC(FirmwarePluginManager, _firmwarePluginManagerInstance);

FirmwarePluginManager::FirmwarePluginManager(QObject *parent)
: QObject(parent)
{
// qCDebug(FirmwarePluginManagerLog) << Q_FUNC_INFO << this;
}

FirmwarePluginManager::~FirmwarePluginManager()
{
// qCDebug(FirmwarePluginManagerLog) << Q_FUNC_INFO << this;

delete _genericFirmwarePlugin;
}

QList<QGCMAVLink::FirmwareClass_t> FirmwarePluginManager::supportedFirmwareClasses(void)
FirmwarePluginManager *FirmwarePluginManager::instance()
{
return _firmwarePluginManagerInstance();
}

QList<QGCMAVLink::FirmwareClass_t> FirmwarePluginManager::supportedFirmwareClasses()
{
if (_supportedFirmwareClasses.isEmpty()) {
QList<FirmwarePluginFactory*> factoryList = FirmwarePluginFactoryRegister::instance()->pluginFactories();
for (int i = 0; i < factoryList.count(); i++) {
_supportedFirmwareClasses.append(factoryList[i]->supportedFirmwareClasses());
const QList<FirmwarePluginFactory*> factoryList = FirmwarePluginFactoryRegister::instance()->pluginFactories();
for (const FirmwarePluginFactory *factory: factoryList) {
_supportedFirmwareClasses.append(factory->supportedFirmwareClasses());
}
_supportedFirmwareClasses.append(QGCMAVLink::FirmwareClassGeneric);
}

return _supportedFirmwareClasses;
}

QList<QGCMAVLink::VehicleClass_t> FirmwarePluginManager::supportedVehicleClasses(QGCMAVLink::FirmwareClass_t firmwareClass)
{
QList<QGCMAVLink::VehicleClass_t> vehicleClasses;

FirmwarePluginFactory* factory = _findPluginFactory(firmwareClass);
const FirmwarePluginFactory *const factory = _findPluginFactory(firmwareClass);

if (factory) {
vehicleClasses = factory->supportedVehicleClasses();
} else if (firmwareClass == QGCMAVLink::FirmwareClassGeneric) {
// Generic supports all specific vehicle class
vehicleClasses = QGCMAVLink::allVehicleClasses();
vehicleClasses.removeOne(QGCMAVLink::VehicleClassGeneric);
(void) vehicleClasses.removeOne(QGCMAVLink::VehicleClassGeneric);
} else {
qWarning() << "Request for unknown firmware plugin factory" << firmwareClass;
qCWarning(FirmwarePluginManagerLog) << "Request for unknown firmware plugin factory" << firmwareClass;
}

return vehicleClasses;
}

FirmwarePlugin* FirmwarePluginManager::firmwarePluginForAutopilot(MAV_AUTOPILOT firmwareType, MAV_TYPE vehicleType)
FirmwarePlugin *FirmwarePluginManager::firmwarePluginForAutopilot(MAV_AUTOPILOT firmwareType, MAV_TYPE vehicleType)
{
FirmwarePluginFactory* factory = _findPluginFactory(firmwareType);
FirmwarePlugin* plugin = nullptr;
FirmwarePluginFactory *const factory = _findPluginFactory(firmwareType);
FirmwarePlugin *plugin = nullptr;

if (factory) {
plugin = factory->firmwarePluginForAutopilot(firmwareType, vehicleType);
Expand All @@ -70,21 +83,20 @@ FirmwarePlugin* FirmwarePluginManager::firmwarePluginForAutopilot(MAV_AUTOPILOT
if (!plugin) {
// Default plugin fallback
if (!_genericFirmwarePlugin) {
_genericFirmwarePlugin = new FirmwarePlugin;
_genericFirmwarePlugin = new FirmwarePlugin();
}
plugin = _genericFirmwarePlugin;
}

return plugin;
}

FirmwarePluginFactory* FirmwarePluginManager::_findPluginFactory(QGCMAVLink::FirmwareClass_t firmwareClass)
FirmwarePluginFactory *FirmwarePluginManager::_findPluginFactory(QGCMAVLink::FirmwareClass_t firmwareClass)
{
QList<FirmwarePluginFactory*> factoryList = FirmwarePluginFactoryRegister::instance()->pluginFactories();
const QList<FirmwarePluginFactory*> factoryList = FirmwarePluginFactoryRegister::instance()->pluginFactories();

// Find the plugin which supports this vehicle
for (int i=0; i<factoryList.count(); i++) {
FirmwarePluginFactory* factory = factoryList[i];
for (FirmwarePluginFactory *factory: factoryList) {
if (factory->supportedFirmwareClasses().contains(firmwareClass)) {
return factory;
}
Expand Down
29 changes: 18 additions & 11 deletions src/FirmwarePlugin/FirmwarePluginManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,35 @@

#pragma once

#include <QtCore/QLoggingCategory>
#include <QtCore/QObject>

#include "QGCMAVLink.h"
#include "QGCToolbox.h"


class QGCApplication;
class FirmwarePlugin;
class FirmwarePluginFactory;

/// FirmwarePluginManager is a singleton which is used to return the correct FirmwarePlugin for a MAV_AUTOPILOT type.
Q_DECLARE_LOGGING_CATEGORY(FirmwarePluginManagerLog)

class FirmwarePluginManager : public QGCTool
/// FirmwarePluginManager is a singleton which is used to return the correct FirmwarePlugin for a MAV_AUTOPILOT type.
class FirmwarePluginManager : public QObject
{
Q_OBJECT

public:
FirmwarePluginManager(QGCApplication* app, QGCToolbox* toolbox);
/// Constructs an FirmwarePluginManager object.
/// @param parent The parent QObject.
explicit FirmwarePluginManager(QObject *parent = nullptr);

/// Destructor for the FirmwarePluginManager class.
~FirmwarePluginManager();

/// Gets the singleton instance of FirmwarePluginManager.
/// @return The singleton instance.
static FirmwarePluginManager *instance();

/// Returns list of firmwares which are supported by the system
QList<QGCMAVLink::FirmwareClass_t> supportedFirmwareClasses(void);
QList<QGCMAVLink::FirmwareClass_t> supportedFirmwareClasses();

/// Returns the list of supported vehicle types for the specified firmware
QList<QGCMAVLink::VehicleClass_t> supportedVehicleClasses(QGCMAVLink::FirmwareClass_t firmwareClass);
Expand All @@ -39,11 +46,11 @@ class FirmwarePluginManager : public QGCTool
/// @param firmwareType Type of firmwware to return plugin for.
/// @param vehicleType Vehicle type to return plugin for.
/// @return Singleton FirmwarePlugin instance for the specified MAV_AUTOPILOT.
FirmwarePlugin* firmwarePluginForAutopilot(MAV_AUTOPILOT firmwareType, MAV_TYPE vehicleType);
FirmwarePlugin *firmwarePluginForAutopilot(MAV_AUTOPILOT firmwareType, MAV_TYPE vehicleType);

private:
FirmwarePluginFactory* _findPluginFactory(QGCMAVLink::FirmwareClass_t firmwareClass);
FirmwarePluginFactory *_findPluginFactory(QGCMAVLink::FirmwareClass_t firmwareClass);

FirmwarePlugin* _genericFirmwarePlugin;
QList<QGCMAVLink::FirmwareClass_t> _supportedFirmwareClasses;
FirmwarePlugin *_genericFirmwarePlugin = nullptr;
QList<QGCMAVLink::FirmwareClass_t> _supportedFirmwareClasses;
};
6 changes: 3 additions & 3 deletions src/MissionManager/MissionCommandTree.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ void MissionCommandTree::setToolbox(QGCToolbox* toolbox)
} else {
#endif
// Load all levels of hierarchy
for (const QGCMAVLink::FirmwareClass_t firmwareClass: _toolbox->firmwarePluginManager()->supportedFirmwareClasses()) {
FirmwarePlugin* plugin = _toolbox->firmwarePluginManager()->firmwarePluginForAutopilot(QGCMAVLink::firmwareClassToAutopilot(firmwareClass), MAV_TYPE_QUADROTOR);
for (const QGCMAVLink::FirmwareClass_t firmwareClass: FirmwarePluginManager::instance()->supportedFirmwareClasses()) {
FirmwarePlugin* plugin = FirmwarePluginManager::instance()->firmwarePluginForAutopilot(QGCMAVLink::firmwareClassToAutopilot(firmwareClass), MAV_TYPE_QUADROTOR);

for (const QGCMAVLink::VehicleClass_t vehicleClass: QGCMAVLink::allVehicleClasses()) {
QString overrideFile = plugin->missionCommandOverrides(vehicleClass);
Expand Down Expand Up @@ -209,7 +209,7 @@ QVariantList MissionCommandTree::getCommandsForCategory(Vehicle* vehicle, const

// vehicle can be null in which case _firmwareAndVehicleClassInfo will tell of the firmware/vehicle type for the offline editing vehicle.
// We then use that to get a firmware plugin so we can get the list of supported commands.
FirmwarePlugin* firmwarePlugin = qgcApp()->toolbox()->firmwarePluginManager()->firmwarePluginForAutopilot(QGCMAVLink::firmwareClassToAutopilot(firmwareClass), QGCMAVLink::vehicleClassToMavType(vehicleClass));
FirmwarePlugin* firmwarePlugin = FirmwarePluginManager::instance()->firmwarePluginForAutopilot(QGCMAVLink::firmwareClassToAutopilot(firmwareClass), QGCMAVLink::vehicleClassToMavType(vehicleClass));
QList<MAV_CMD> supportedCommands = firmwarePlugin->supportedMissionCommands(vehicleClass);

QVariantList list;
Expand Down
4 changes: 2 additions & 2 deletions src/MissionManager/PlanMasterController.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ QGC_LOGGING_CATEGORY(PlanMasterControllerLog, "PlanMasterControllerLog")
PlanMasterController::PlanMasterController(QObject* parent)
: QObject (parent)
, _multiVehicleMgr (qgcApp()->toolbox()->multiVehicleManager())
, _controllerVehicle (new Vehicle(Vehicle::MAV_AUTOPILOT_TRACK, Vehicle::MAV_TYPE_TRACK, qgcApp()->toolbox()->firmwarePluginManager(), this))
, _controllerVehicle (new Vehicle(Vehicle::MAV_AUTOPILOT_TRACK, Vehicle::MAV_TYPE_TRACK, this))
, _managerVehicle (_controllerVehicle)
, _missionController (this)
, _geoFenceController (this)
Expand All @@ -47,7 +47,7 @@ PlanMasterController::PlanMasterController(QObject* parent)
PlanMasterController::PlanMasterController(MAV_AUTOPILOT firmwareType, MAV_TYPE vehicleType, QObject* parent)
: QObject (parent)
, _multiVehicleMgr (qgcApp()->toolbox()->multiVehicleManager())
, _controllerVehicle (new Vehicle(firmwareType, vehicleType, qgcApp()->toolbox()->firmwarePluginManager()))
, _controllerVehicle (new Vehicle(firmwareType, vehicleType))
, _managerVehicle (_controllerVehicle)
, _missionController (this)
, _geoFenceController (this)
Expand Down
1 change: 0 additions & 1 deletion src/QGCApplication.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
#include "SyslinkComponentController.h"
#include "AutoPilotPlugin.h"
#include "VehicleComponent.h"
#include "FirmwarePluginManager.h"
#include "MultiVehicleManager.h"
#include "Vehicle.h"
#include "JoystickConfigController.h"
Expand Down
3 changes: 0 additions & 3 deletions src/QGCToolbox.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
****************************************************************************/


#include "FirmwarePluginManager.h"
#include "LinkManager.h"
#include "MAVLinkProtocol.h"
#include "MissionCommandTree.h"
Expand Down Expand Up @@ -38,7 +37,6 @@ QGCToolbox::QGCToolbox(QGCApplication* app)

//-- Scan and load plugins
_scanAndLoadPlugins(app);
_firmwarePluginManager = new FirmwarePluginManager (app, this);
_linkManager = new LinkManager (app, this);
_mavlinkProtocol = new MAVLinkProtocol (app, this);
_missionCommandTree = new MissionCommandTree (app, this);
Expand All @@ -60,7 +58,6 @@ void QGCToolbox::setChildToolboxes(void)
_settingsManager->setToolbox(this);

_corePlugin->setToolbox(this);
_firmwarePluginManager->setToolbox(this);
_linkManager->setToolbox(this);
_mavlinkProtocol->setToolbox(this);
_missionCommandTree->setToolbox(this);
Expand Down
3 changes: 0 additions & 3 deletions src/QGCToolbox.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

#include <QtCore/QObject>

class FirmwarePluginManager;
class LinkManager;
class MAVLinkProtocol;
class MissionCommandTree;
Expand All @@ -37,7 +36,6 @@ class QGCToolbox : public QObject {
public:
QGCToolbox(QGCApplication* app);

FirmwarePluginManager* firmwarePluginManager () { return _firmwarePluginManager; }
LinkManager* linkManager () { return _linkManager; }
MAVLinkProtocol* mavlinkProtocol () { return _mavlinkProtocol; }
MissionCommandTree* missionCommandTree () { return _missionCommandTree; }
Expand All @@ -57,7 +55,6 @@ class QGCToolbox : public QObject {
void setChildToolboxes(void);
void _scanAndLoadPlugins(QGCApplication *app);

FirmwarePluginManager* _firmwarePluginManager = nullptr;
LinkManager* _linkManager = nullptr;
MAVLinkProtocol* _mavlinkProtocol = nullptr;
MissionCommandTree* _missionCommandTree = nullptr;
Expand Down
9 changes: 4 additions & 5 deletions src/QmlControls/QGroundControlQmlGlobal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ void QGroundControlQmlGlobal::setToolbox(QGCToolbox* toolbox)
_videoManager = toolbox->videoManager();
_mavlinkLogManager = toolbox->mavlinkLogManager();
_corePlugin = toolbox->corePlugin();
_firmwarePluginManager = toolbox->firmwarePluginManager();
_settingsManager = toolbox->settingsManager();
#ifndef NO_SERIAL_LINK
_gpsRtkFactGroup = GPSManager::instance()->gpsRtk()->gpsRtkFactGroup();
Expand Down Expand Up @@ -209,26 +208,26 @@ void QGroundControlQmlGlobal::setMavlinkSystemID(int id)

bool QGroundControlQmlGlobal::singleFirmwareSupport(void)
{
return _firmwarePluginManager->supportedFirmwareClasses().count() == 1;
return FirmwarePluginManager::instance()->supportedFirmwareClasses().count() == 1;
}

bool QGroundControlQmlGlobal::singleVehicleSupport(void)
{
if (singleFirmwareSupport()) {
return _firmwarePluginManager->supportedVehicleClasses(_firmwarePluginManager->supportedFirmwareClasses()[0]).count() == 1;
return FirmwarePluginManager::instance()->supportedVehicleClasses(FirmwarePluginManager::instance()->supportedFirmwareClasses()[0]).count() == 1;
}

return false;
}

bool QGroundControlQmlGlobal::px4ProFirmwareSupported()
{
return _firmwarePluginManager->supportedFirmwareClasses().contains(QGCMAVLink::FirmwareClassPX4);
return FirmwarePluginManager::instance()->supportedFirmwareClasses().contains(QGCMAVLink::FirmwareClassPX4);
}

bool QGroundControlQmlGlobal::apmFirmwareSupported()
{
return _firmwarePluginManager->supportedFirmwareClasses().contains(QGCMAVLink::FirmwareClassArduPilot);
return FirmwarePluginManager::instance()->supportedFirmwareClasses().contains(QGCMAVLink::FirmwareClassArduPilot);
}

bool QGroundControlQmlGlobal::linesIntersect(QPointF line1A, QPointF line1B, QPointF line2A, QPointF line2B)
Expand Down
1 change: 0 additions & 1 deletion src/QmlControls/QGroundControlQmlGlobal.h
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,6 @@ class QGroundControlQmlGlobal : public QGCTool
VideoManager* _videoManager = nullptr;
MAVLinkLogManager* _mavlinkLogManager = nullptr;
QGCCorePlugin* _corePlugin = nullptr;
FirmwarePluginManager* _firmwarePluginManager = nullptr;
SettingsManager* _settingsManager = nullptr;
#ifndef NO_SERIAL_LINK
FactGroup* _gpsRtkFactGroup = nullptr;
Expand Down
5 changes: 2 additions & 3 deletions src/Vehicle/Components/CompInfoParam.cc
Original file line number Diff line number Diff line change
Expand Up @@ -144,16 +144,15 @@ FactMetaData* CompInfoParam::factMetaDataForName(const QString& name, FactMetaDa

FirmwarePlugin* CompInfoParam::_anyVehicleTypeFirmwarePlugin(MAV_AUTOPILOT firmwareType)
{
FirmwarePluginManager* const pluginMgr = qgcApp()->toolbox()->firmwarePluginManager();
const QGCMAVLink::FirmwareClass_t firmwareClass = QGCMAVLink::firmwareClass(firmwareType);
const QList<QGCMAVLink::VehicleClass_t> supportedClasses = pluginMgr->supportedVehicleClasses(firmwareClass);
const QList<QGCMAVLink::VehicleClass_t> supportedClasses = FirmwarePluginManager::instance()->supportedVehicleClasses(firmwareClass);
MAV_TYPE anySupportedVehicleType;
if (!supportedClasses.isEmpty()) {
anySupportedVehicleType = QGCMAVLink::vehicleClassToMavType(supportedClasses[0]);
} else {
anySupportedVehicleType = MAV_TYPE_GENERIC;
}
return pluginMgr->firmwarePluginForAutopilot(firmwareType, anySupportedVehicleType);
return FirmwarePluginManager::instance()->firmwarePluginForAutopilot(firmwareType, anySupportedVehicleType);
}

QString CompInfoParam::_parameterMetaDataFile(Vehicle* vehicle, MAV_AUTOPILOT firmwareType, int& majorVersion, int& minorVersion)
Expand Down
6 changes: 2 additions & 4 deletions src/Vehicle/MultiVehicleManager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ MultiVehicleManager::MultiVehicleManager(QGCApplication* app, QGCToolbox* toolbo
, _parameterReadyVehicleAvailable(false)
, _activeVehicle(nullptr)
, _offlineEditingVehicle(nullptr)
, _firmwarePluginManager(nullptr)
, _mavlinkProtocol(nullptr)
, _gcsHeartbeatEnabled(true)
{
Expand All @@ -45,7 +44,6 @@ void MultiVehicleManager::setToolbox(QGCToolbox *toolbox)
{
QGCTool::setToolbox(toolbox);

_firmwarePluginManager = _toolbox->firmwarePluginManager();
_mavlinkProtocol = _toolbox->mavlinkProtocol();

QQmlEngine::setObjectOwnership(this, QQmlEngine::CppOwnership);
Expand All @@ -62,7 +60,7 @@ void MultiVehicleManager::setToolbox(QGCToolbox *toolbox)
_gcsHeartbeatTimer.start();
}

_offlineEditingVehicle = new Vehicle(Vehicle::MAV_AUTOPILOT_TRACK, Vehicle::MAV_TYPE_TRACK, _firmwarePluginManager, this);
_offlineEditingVehicle = new Vehicle(Vehicle::MAV_AUTOPILOT_TRACK, Vehicle::MAV_TYPE_TRACK, this);
}

void MultiVehicleManager::_vehicleHeartbeatInfo(LinkInterface* link, int vehicleId, int componentId, int vehicleFirmwareType, int vehicleType)
Expand Down Expand Up @@ -117,7 +115,7 @@ void MultiVehicleManager::_vehicleHeartbeatInfo(LinkInterface* link, int vehicle
_app->showAppMessage(tr("Warning: A vehicle is using the same system id as %1: %2").arg(QCoreApplication::applicationName()).arg(vehicleId));
}

Vehicle* vehicle = new Vehicle(link, vehicleId, componentId, (MAV_AUTOPILOT)vehicleFirmwareType, (MAV_TYPE)vehicleType, _firmwarePluginManager, this);
Vehicle* vehicle = new Vehicle(link, vehicleId, componentId, (MAV_AUTOPILOT)vehicleFirmwareType, (MAV_TYPE)vehicleType, this);
connect(vehicle, &Vehicle::requestProtocolVersion, this, &MultiVehicleManager::_requestProtocolVersion);
connect(vehicle->vehicleLinkManager(), &VehicleLinkManager::allLinksRemoved, this, &MultiVehicleManager::_deleteVehiclePhase1);
connect(vehicle->parameterManager(), &ParameterManager::parametersReadyChanged, this, &MultiVehicleManager::_vehicleParametersReadyChanged);
Expand Down
2 changes: 0 additions & 2 deletions src/Vehicle/MultiVehicleManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#include "QGCToolbox.h"
#include "QmlObjectListModel.h"

class FirmwarePluginManager;
class QGCApplication;
class MAVLinkProtocol;
class LinkInterface;
Expand Down Expand Up @@ -109,7 +108,6 @@ private slots:

QmlObjectListModel _vehicles;

FirmwarePluginManager* _firmwarePluginManager;
MAVLinkProtocol* _mavlinkProtocol;
QGeoCoordinate _lastKnownLocation;

Expand Down
Loading
Loading