Skip to content

Commit

Permalink
Convert LinkManager to Singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
HTRamsey committed Nov 28, 2024
1 parent ca7748e commit 6bbb5e7
Show file tree
Hide file tree
Showing 25 changed files with 86 additions and 101 deletions.
2 changes: 1 addition & 1 deletion src/AutoPilotPlugins/PX4/AirframeComponentController.cc
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ void AirframeComponentController::_rebootAfterStackUnwind(void)
QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
}
QGuiApplication::restoreOverrideCursor();
qgcApp()->toolbox()->linkManager()->disconnectAll();
LinkManager::instance()->disconnectAll();
}

AirframeType::AirframeType(const QString& name, const QString& imageResource, QObject* parent) :
Expand Down
2 changes: 1 addition & 1 deletion src/Comms/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ target_link_libraries(Comms
Qt6::Test
MockLink
PositionManager
QGC
Settings
Vehicle
PUBLIC
Qt6::Core
Qt6::Network
AirLink
MAVLink
QGC
QmlControls
Utilities
)
Expand Down
4 changes: 2 additions & 2 deletions src/Comms/LinkInterface.cc
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ bool LinkInterface::_allocateMavlinkChannel()
return true;
}

_mavlinkChannel = qgcApp()->toolbox()->linkManager()->allocateMavlinkChannel();
_mavlinkChannel = LinkManager::instance()->allocateMavlinkChannel();

if (!mavlinkChannelIsSet()) {
qCWarning(LinkInterfaceLog) << Q_FUNC_INFO << "failed";
Expand All @@ -105,7 +105,7 @@ void LinkInterface::_freeMavlinkChannel()
return;
}

qgcApp()->toolbox()->linkManager()->freeMavlinkChannel(_mavlinkChannel);
LinkManager::instance()->freeMavlinkChannel(_mavlinkChannel);
_mavlinkChannel = LinkManager::invalidMavlinkChannel();
}

Expand Down
45 changes: 27 additions & 18 deletions src/Comms/LinkManager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,38 @@
#include <qmdnsengine/service.h>
#endif

#include <QtCore/qapplicationstatic.h>
#include <QtCore/QTimer>
#include <QtQml/qqml.h>

QGC_LOGGING_CATEGORY(LinkManagerLog, "qgc.comms.linkmanager")
QGC_LOGGING_CATEGORY(LinkManagerVerboseLog, "qgc.comms.linkmanager:verbose")

LinkManager::LinkManager(QGCApplication *app, QGCToolbox *toolbox)
: QGCTool(app, toolbox)
Q_APPLICATION_STATIC(LinkManager, _linkManagerInstance);

LinkManager::LinkManager(QObject *parent)
: QObject(parent)
, _portListTimer(new QTimer(this))
, _qmlConfigurations(new QmlObjectListModel(this))
#ifndef NO_SERIAL_LINK
, _nmeaSocket(new UdpIODevice(this))
#endif
{
// qCDebug(LinkManagerLog) << Q_FUNC_INFO << this;
}

LinkManager::~LinkManager()
{
// qCDebug(LinkManagerLog) << Q_FUNC_INFO << this;
}

LinkManager *LinkManager::instance()
{
return _linkManagerInstance();
}

void LinkManager::registerQmlTypes()
{
(void) qmlRegisterUncreatableType<LinkManager>("QGroundControl", 1, 0, "LinkManager", "Reference only");
(void) qmlRegisterUncreatableType<LinkConfiguration>("QGroundControl", 1, 0, "LinkConfiguration", "Reference only");
(void) qmlRegisterUncreatableType<LinkInterface>("QGroundControl", 1, 0, "LinkInterface", "Reference only");
Expand All @@ -78,16 +94,9 @@ LinkManager::LinkManager(QGCApplication *app, QGCToolbox *toolbox)
#endif
}

LinkManager::~LinkManager()
{
// qCDebug(LinkManagerLog) << Q_FUNC_INFO << this;
}

void LinkManager::setToolbox(QGCToolbox *toolbox)
void LinkManager::init()
{
QGCTool::setToolbox(toolbox);

_autoConnectSettings = toolbox->settingsManager()->autoConnectSettings();
_autoConnectSettings = qgcApp()->toolbox()->settingsManager()->autoConnectSettings();

if (!qgcApp()->runningUnitTests()) {
(void) connect(_portListTimer, &QTimer::timeout, this, &LinkManager::_updateAutoConnectLinks);
Expand Down Expand Up @@ -160,7 +169,7 @@ bool LinkManager::createConnectedLink(SharedLinkConfigurationPtr &config)
(void) _rgLinks.append(link);
config->setLink(link);

(void) connect(link.get(), &LinkInterface::communicationError, _app, &QGCApplication::criticalMessageBoxOnMainThread);
(void) connect(link.get(), &LinkInterface::communicationError, qgcApp(), &QGCApplication::showAppMessage);
(void) connect(link.get(), &LinkInterface::bytesReceived, MAVLinkProtocol::instance(), &MAVLinkProtocol::receiveBytes);
(void) connect(link.get(), &LinkInterface::bytesSent, MAVLinkProtocol::instance(), &MAVLinkProtocol::logSentBytes);
(void) connect(link.get(), &LinkInterface::disconnected, this, &LinkManager::_linkDisconnected);
Expand Down Expand Up @@ -218,7 +227,7 @@ void LinkManager::_linkDisconnected()
return;
}

(void) disconnect(link, &LinkInterface::communicationError, _app, &QGCApplication::criticalMessageBoxOnMainThread);
(void) disconnect(link, &LinkInterface::communicationError, qgcApp(), &QGCApplication::showAppMessage);
(void) disconnect(link, &LinkInterface::bytesReceived, MAVLinkProtocol::instance(), &MAVLinkProtocol::receiveBytes);
(void) disconnect(link, &LinkInterface::bytesSent, MAVLinkProtocol::instance(), &MAVLinkProtocol::logSentBytes);
(void) disconnect(link, &LinkInterface::disconnected, this, &LinkManager::_linkDisconnected);
Expand All @@ -227,7 +236,7 @@ void LinkManager::_linkDisconnected()

for (auto it = _rgLinks.begin(); it != _rgLinks.end(); ++it) {
if (it->get() == link) {
qCDebug(LinkManagerLog) << "LinkManager::_linkDisconnected" << it->get()->linkConfiguration()->name() << it->use_count();
qCDebug(LinkManagerLog) << Q_FUNC_INFO << it->get()->linkConfiguration()->name() << it->use_count();
SharedLinkConfigurationPtr config = it->get()->linkConfiguration();
config->setLink(nullptr);
(void) _rgLinks.erase(it);
Expand Down Expand Up @@ -391,7 +400,7 @@ void LinkManager::_addUDPAutoConnectLink()

void LinkManager::_addMAVLinkForwardingLink()
{
if (!_toolbox->settingsManager()->appSettings()->forwardMavlink()->rawValue().toBool()) {
if (!qgcApp()->toolbox()->settingsManager()->appSettings()->forwardMavlink()->rawValue().toBool()) {
return;
}

Expand All @@ -403,7 +412,7 @@ void LinkManager::_addMAVLinkForwardingLink()
}
}

const QString hostName = _toolbox->settingsManager()->appSettings()->forwardMavlinkHostName()->rawValue().toString();
const QString hostName = qgcApp()->toolbox()->settingsManager()->appSettings()->forwardMavlinkHostName()->rawValue().toString();
_createDynamicForwardLink(_mavlinkForwardingLinkName, hostName);
}

Expand Down Expand Up @@ -502,7 +511,7 @@ void LinkManager::shutdown()
disconnectAll();

// Wait for all the vehicles to go away to ensure an orderly shutdown and deletion of all objects
while (_toolbox->multiVehicleManager()->vehicles()->count()) {
while (qgcApp()->toolbox()->multiVehicleManager()->vehicles()->count()) {
QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
}
}
Expand Down Expand Up @@ -608,7 +617,7 @@ void LinkManager::removeConfiguration(LinkConfiguration *config)

void LinkManager::createMavlinkForwardingSupportLink()
{
const QString hostName = _toolbox->settingsManager()->appSettings()->forwardMavlinkAPMSupportHostName()->rawValue().toString();
const QString hostName = qgcApp()->toolbox()->settingsManager()->appSettings()->forwardMavlinkAPMSupportHostName()->rawValue().toString();
_createDynamicForwardLink(_mavlinkForwardingSupportLinkName, hostName);
_mavlinkSupportForwardingEnabled = true;
emit mavlinkSupportForwardingEnabledChanged();
Expand Down
31 changes: 16 additions & 15 deletions src/Comms/LinkManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

#include <limits>

#include "QGCToolbox.h"
#include "LinkConfiguration.h"
#include "LinkInterface.h"
#ifndef NO_SERIAL_LINK
Expand All @@ -28,7 +27,6 @@ Q_DECLARE_LOGGING_CATEGORY(LinkManagerVerboseLog)
class AutoConnectSettings;
class LogReplayLink;
class MAVLinkProtocol;
class QGCApplication;
class QmlObjectListModel;
class QTimer;
class SerialLink;
Expand All @@ -39,7 +37,7 @@ class UdpIODevice;
/// The Link Manager organizes the physical Links. It can manage arbitrary
/// links and takes care of connecting them as well assigning the correct
/// protocol instance to transport the link data into the application.
class LinkManager : public QGCTool
class LinkManager : public QObject
{
Q_OBJECT
Q_MOC_INCLUDE("QmlObjectListModel.h")
Expand All @@ -49,11 +47,13 @@ class LinkManager : public QGCTool
Q_PROPERTY(bool mavlinkSupportForwardingEnabled READ mavlinkSupportForwardingEnabled NOTIFY mavlinkSupportForwardingEnabledChanged)

public:
LinkManager(QGCApplication *app, QGCToolbox *toolbox);
LinkManager(QObject *parent = nullptr);
~LinkManager();

// Override from QGCTool
void setToolbox(QGCToolbox *toolbox) final;
static LinkManager *instance();
static void registerQmlTypes();

void init();

/// Create/Edit Link Configuration
Q_INVOKABLE LinkConfiguration *createConfiguration(int type, const QString &name);
Expand Down Expand Up @@ -141,23 +141,23 @@ private slots:
void _addZeroConfAutoConnectLink();
#endif

QTimer *_portListTimer = nullptr;
QmlObjectListModel *_qmlConfigurations = nullptr;
AutoConnectSettings *_autoConnectSettings = nullptr;

bool _configUpdateSuspended = false; ///< true: stop updating configuration list
bool _configurationsLoaded = false; ///< true: Link configurations have been loaded
bool _connectionsSuspended = false; ///< true: all new connections should not be allowed
bool _mavlinkSupportForwardingEnabled = false;
uint32_t _mavlinkChannelsUsedBitMask = 1;
QString _connectionsSuspendedReason; ///< User visible reason for suspension

AutoConnectSettings *_autoConnectSettings = nullptr;
QTimer *_portListTimer = nullptr;
QmlObjectListModel *_qmlConfigurations = nullptr;

QList<SharedLinkInterfacePtr> _rgLinks;
QList<SharedLinkConfigurationPtr> _rgLinkConfigs;

static constexpr const char* _defaultUDPLinkName = "UDP Link (AutoConnect)";
static constexpr const char* _mavlinkForwardingLinkName = "MAVLink Forwarding Link";
static constexpr const char* _mavlinkForwardingSupportLinkName = "MAVLink Support Forwarding Link";
static constexpr const char *_defaultUDPLinkName = "UDP Link (AutoConnect)";
static constexpr const char *_mavlinkForwardingLinkName = "MAVLink Forwarding Link";
static constexpr const char *_mavlinkForwardingSupportLinkName = "MAVLink Support Forwarding Link";

static constexpr int _autoconnectUpdateTimerMSecs = 1000;
#ifdef Q_OS_WIN
Expand All @@ -168,11 +168,12 @@ private slots:
#endif

#ifndef NO_SERIAL_LINK
public:
private:
Q_PROPERTY(QStringList serialBaudRates READ serialBaudRates CONSTANT)
Q_PROPERTY(QStringList serialPortStrings READ serialPortStrings NOTIFY commPortStringsChanged)
Q_PROPERTY(QStringList serialPorts READ serialPorts NOTIFY commPortsChanged)

public:
static QStringList serialBaudRates();
QStringList serialPortStrings();
QStringList serialPorts();
Expand All @@ -188,6 +189,7 @@ private slots:
void _addSerialAutoConnectLink();
bool _portAlreadyConnected(const QString &portName) const;

UdpIODevice *_nmeaSocket = nullptr;
QMap<QString, int> _autoconnectPortWaitList; ///< key: QGCSerialPortInfo::systemLocation, value: wait count
QList<SerialLink*> _activeLinkCheckList; ///< List of links we are waiting for a vehicle to show up on
QStringList _commPortList;
Expand All @@ -196,6 +198,5 @@ private slots:
QString _nmeaDeviceName;
uint32_t _nmeaBaud = 0;
QSerialPort *_nmeaPort = nullptr;
UdpIODevice *_nmeaSocket = nullptr;
#endif // NO_SERIAL_LINK
};
4 changes: 2 additions & 2 deletions src/Comms/LogReplayLink.cc
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ void LogReplayLink::_readNextLogEntry(void)

void LogReplayLink::_play(void)
{
qgcApp()->toolbox()->linkManager()->setConnectionsSuspended(tr("Connect not allowed during Flight Data replay."));
LinkManager::instance()->setConnectionsSuspended(tr("Connect not allowed during Flight Data replay."));
#ifndef __mobile__
MAVLinkProtocol::instance()->suspendLogForReplay(true);
#endif
Expand All @@ -377,7 +377,7 @@ void LogReplayLink::_play(void)

void LogReplayLink::_pause(void)
{
qgcApp()->toolbox()->linkManager()->setConnectionsAllowed();
LinkManager::instance()->setConnectionsAllowed();
#ifndef __mobile__
MAVLinkProtocol::instance()->suspendLogForReplay(false);
#endif
Expand Down
11 changes: 6 additions & 5 deletions src/Comms/MAVLinkProtocol.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "QGCToolbox.h"
#include "SettingsManager.h"

#include <QtCore/qapplicationstatic.h>
#include <QtCore/QDir>
#include <QtCore/QFileInfo>
#include <QtCore/QMetaType>
Expand Down Expand Up @@ -54,7 +55,7 @@ MAVLinkProtocol *MAVLinkProtocol::instance()

void MAVLinkProtocol::setVersion(unsigned version)
{
const QList<SharedLinkInterfacePtr> sharedLinks = qgcApp()->toolbox()->linkManager()->links();
const QList<SharedLinkInterfacePtr> sharedLinks = LinkManager::instance()->links();
for (const SharedLinkInterfacePtr &interface : sharedLinks) {
mavlink_set_proto_version(interface.get()->mavlinkChannel(), version / 100);
}
Expand Down Expand Up @@ -127,7 +128,7 @@ void MAVLinkProtocol::logSentBytes(const LinkInterface *link, const QByteArray &

void MAVLinkProtocol::receiveBytes(LinkInterface *link, const QByteArray &data)
{
const SharedLinkInterfacePtr linkPtr = qgcApp()->toolbox()->linkManager()->sharedLinkInterfacePointerForLink(link);
const SharedLinkInterfacePtr linkPtr = LinkManager::instance()->sharedLinkInterfacePointerForLink(link);
if (!linkPtr) {
qCDebug(MAVLinkProtocolLog) << "receiveBytes: link gone!" << data.size() << "bytes arrived too late";
return;
Expand Down Expand Up @@ -213,7 +214,7 @@ void MAVLinkProtocol::_forward(const mavlink_message_t &message)
return;
}

SharedLinkInterfacePtr forwardingLink = qgcApp()->toolbox()->linkManager()->mavlinkForwardingLink();
SharedLinkInterfacePtr forwardingLink = LinkManager::instance()->mavlinkForwardingLink();
if (!forwardingLink) {
return;
}
Expand All @@ -229,11 +230,11 @@ void MAVLinkProtocol::_forwardSupport(const mavlink_message_t &message)
return;
}

if (!qgcApp()->toolbox()->linkManager()->mavlinkSupportForwardingEnabled()) {
if (!LinkManager::instance()->mavlinkSupportForwardingEnabled()) {
return;
}

SharedLinkInterfacePtr forwardingSupportLink = qgcApp()->toolbox()->linkManager()->mavlinkForwardingSupportLink();
SharedLinkInterfacePtr forwardingSupportLink = LinkManager::instance()->mavlinkForwardingSupportLink();
if (!forwardingSupportLink) {
return;
}
Expand Down
12 changes: 4 additions & 8 deletions src/Comms/MockLink/MockLink.cc
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,7 @@ bool MockLink::_allocateMavlinkChannel()
return false;
}

auto mgr = qgcApp()->toolbox()->linkManager();
_mavlinkAuxChannel = mgr->allocateMavlinkChannel();
_mavlinkAuxChannel = LinkManager::instance()->allocateMavlinkChannel();
if (!mavlinkAuxChannelIsSet()) {
qCWarning(MockLinkLog) << "_allocateMavlinkChannel failed";
LinkInterface::_freeMavlinkChannel();
Expand All @@ -167,8 +166,7 @@ void MockLink::_freeMavlinkChannel()
return;
}

auto mgr = qgcApp()->toolbox()->linkManager();
mgr->freeMavlinkChannel(_mavlinkAuxChannel);
LinkManager::instance()->freeMavlinkChannel(_mavlinkAuxChannel);
LinkInterface::_freeMavlinkChannel();
}

Expand Down Expand Up @@ -1501,12 +1499,10 @@ void MockConfiguration::loadSettings(QSettings& settings, const QString& root)

MockLink* MockLink::_startMockLink(MockConfiguration* mockConfig)
{
LinkManager* linkMgr = qgcApp()->toolbox()->linkManager();

mockConfig->setDynamic(true);
SharedLinkConfigurationPtr config = linkMgr->addConfiguration(mockConfig);
SharedLinkConfigurationPtr config = LinkManager::instance()->addConfiguration(mockConfig);

if (linkMgr->createConnectedLink(config)) {
if (LinkManager::instance()->createConnectedLink(config)) {
return qobject_cast<MockLink*>(config->link());
} else {
return nullptr;
Expand Down
4 changes: 2 additions & 2 deletions src/Comms/SerialLink.cc
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ void SerialLink::disconnect(void)
}

#ifdef Q_OS_ANDROID
qgcApp()->toolbox()->linkManager()->suspendConfigurationUpdates(false);
LinkManager::instance()->suspendConfigurationUpdates(false);
#endif
}

Expand All @@ -96,7 +96,7 @@ bool SerialLink::_connect(void)
}

#ifdef Q_OS_ANDROID
qgcApp()->toolbox()->linkManager()->suspendConfigurationUpdates(true);
LinkManager::instance()->suspendConfigurationUpdates(true);
#endif

QSerialPort::SerialPortError error;
Expand Down
2 changes: 1 addition & 1 deletion src/FirmwarePlugin/APM/APMFirmwarePlugin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1174,7 +1174,7 @@ uint8_t APMFirmwarePlugin::_reencodeMavlinkChannel()
_channelMutex.lock();
static uint8_t channel{LinkManager::invalidMavlinkChannel()};
if (LinkManager::invalidMavlinkChannel() == channel) {
channel = qgcApp()->toolbox()->linkManager()->allocateMavlinkChannel();
channel = LinkManager::instance()->allocateMavlinkChannel();
}
_channelMutex.unlock();
return channel;
Expand Down
Loading

0 comments on commit 6bbb5e7

Please sign in to comment.