From 120303366699e06180ff3ee21048c70f7a6429dc Mon Sep 17 00:00:00 2001 From: Holden Date: Fri, 13 Dec 2024 14:32:35 -0500 Subject: [PATCH] Comms: Update AIRLink Threading --- cmake/CustomOptions.cmake | 2 +- src/Comms/AirLink/AirLinkLink.cc | 199 ++++++++++++++-------------- src/Comms/AirLink/AirLinkLink.h | 31 ++--- src/Comms/AirLink/AirLinkManager.cc | 2 +- 4 files changed, 114 insertions(+), 120 deletions(-) diff --git a/cmake/CustomOptions.cmake b/cmake/CustomOptions.cmake index e1a6c5fe46c..0f6aa7150e5 100644 --- a/cmake/CustomOptions.cmake +++ b/cmake/CustomOptions.cmake @@ -12,7 +12,7 @@ option(QGC_STABLE_BUILD "Stable Build" OFF) option(QGC_ENABLE_BLUETOOTH "Enable Bluetooth Links" ON) # Qt6Bluetooth_FOUND option(QGC_ZEROCONF_ENABLED "Enable ZeroConf Compatibility" OFF) -option(QGC_AIRLINK_DISABLED "Disable AIRLink" ON) +option(QGC_AIRLINK_DISABLED "Disable AIRLink" OFF) option(QGC_NO_SERIAL_LINK "Disable Serial Links" OFF) # NOT IOS AND Qt6SerialPort_FOUND option(QGC_UTM_ADAPTER "Enable UTM Adapter" OFF) diff --git a/src/Comms/AirLink/AirLinkLink.cc b/src/Comms/AirLink/AirLinkLink.cc index b55899bc05c..4c2152cf317 100644 --- a/src/Comms/AirLink/AirLinkLink.cc +++ b/src/Comms/AirLink/AirLinkLink.cc @@ -19,9 +19,96 @@ QGC_LOGGING_CATEGORY(AirLinkLinkLog, "qgc.AirLink.AirLinklink"); +/*===========================================================================*/ + +AirLinkConfiguration::AirLinkConfiguration(const QString &name, QObject *parent) + : UDPConfiguration(name) +{ + // qCDebug(AirLinkLinkLog) << Q_FUNC_INFO << this; +} + +AirLinkConfiguration::AirLinkConfiguration(const AirLinkConfiguration *copy, QObject *parent) + : UDPConfiguration(copy) +{ + // qCDebug(AirLinkLinkLog) << Q_FUNC_INFO << this; + + AirLinkConfiguration::copyFrom(copy); +} + +AirLinkConfiguration::~AirLinkConfiguration() +{ + // qCDebug(AirLinkLinkLog) << Q_FUNC_INFO << this; +} + +void AirLinkConfiguration::copyFrom(const LinkConfiguration *source) +{ + Q_ASSERT(source); + + const UDPConfiguration *const udpSource = qobject_cast(source); + Q_ASSERT(udpSource); + UDPConfiguration::copyFrom(udpSource); + + const AirLinkConfiguration *const airLinkSource = qobject_cast(source); + Q_ASSERT(airLinkSource); + + setUsername(airLinkSource->username()); + setPassword(airLinkSource->password()); + setModemName(airLinkSource->modemName()); +} + +void AirLinkConfiguration::loadSettings(QSettings &settings, const QString &root) +{ + AppSettings *const appSettings = SettingsManager::instance()->appSettings(); + + settings.beginGroup(root); + + setUsername(settings.value(_usernameSettingsKey, appSettings->loginAirLink()->rawValueString()).toString()); + setPassword(settings.value(_passwordSettingsKey, appSettings->passAirLink()->rawValueString()).toString()); + setModemName(settings.value(_modemNameSettingsKey).toString()); + + settings.endGroup(); +} + +void AirLinkConfiguration::saveSettings(QSettings &settings, const QString &root) +{ + settings.beginGroup(root); + + settings.setValue(_usernameSettingsKey, _username); + settings.setValue(_passwordSettingsKey, _password); + settings.setValue(_modemNameSettingsKey, _modemName); + + settings.endGroup(); +} + +void AirLinkConfiguration::setUsername(const QString &username) +{ + if (username != _username) { + _username = username; + emit usernameChanged(); + } +} + +void AirLinkConfiguration::setPassword(const QString &password) +{ + if (password != _password) { + _password = password; + emit passwordChanged(); + } +} + +void AirLinkConfiguration::setModemName(const QString &modemName) +{ + if (modemName != _modemName) { + _modemName = modemName; + emit modemNameChanged(); + } +} + +/*===========================================================================*/ + AirLinkLink::AirLinkLink(SharedLinkConfigurationPtr &config, QObject *parent) : UDPLink(config) - , _AirLinkConfig(qobject_cast(config.get())) + , _airLinkConfig(qobject_cast(config.get())) { // qCDebug(AirLinkLinkLog) << Q_FUNC_INFO << this; @@ -51,12 +138,6 @@ AirLinkLink::~AirLinkLink() // qCDebug(AirLinkLinkLog) << Q_FUNC_INFO << this; } -void AirLinkLink::disconnect() -{ - _setConnectFlag(false); - (void) UDPLink::disconnect(); -} - bool AirLinkLink::_connect() { std::shared_ptr conn = std::make_shared(); @@ -65,7 +146,7 @@ bool AirLinkLink::_connect() return; } - mavlink_airlink_auth_response_t responseMsg; + mavlink_airlink_auth_response_t responseMsg{}; mavlink_msg_airlink_auth_response_decode(&message, &responseMsg); const int answer = responseMsg.resp_type; if (answer != AIRLINK_AUTH_RESPONSE_TYPE::AIRLINK_AUTH_OK) { @@ -85,6 +166,12 @@ bool AirLinkLink::_connect() return true; } +void AirLinkLink::disconnect() +{ + _setConnectFlag(false); + (void) UDPLink::disconnect(); +} + void AirLinkLink::_configureUdpSettings() { quint16 availablePort = 14550; @@ -103,12 +190,12 @@ void AirLinkLink::_sendLoginMsgToAirLink() { mavlink_airlink_auth_t auth{}; - const QString login = _AirLinkConfig->modemName(); ///< Connect not to account but to specific modem - const QString pass = _AirLinkConfig->password(); + const QString login = _airLinkConfig->modemName(); ///< Connect not to account but to specific modem + const QString pass = _airLinkConfig->password(); (void) strcpy(auth.login, login.toUtf8().constData()); (void) strcpy(auth.password, pass.toUtf8().constData()); - mavlink_message_t mavmsg; + mavlink_message_t mavmsg{}; (void) mavlink_msg_airlink_auth_pack(0, 0, &mavmsg, auth.login, auth.password); uint8_t buffer[MAVLINK_MAX_PACKET_LEN]; @@ -132,93 +219,3 @@ void AirLinkLink::_setConnectFlag(bool connect) QMutexLocker locker(&_mutex); _needToConnect = connect; } - -/*===========================================================================*/ - -AirLinkConfiguration::AirLinkConfiguration(const QString &name, QObject *parent) - : UDPConfiguration(name) -{ - // qCDebug(AirLinkLinkLog) << Q_FUNC_INFO << this; -} - -AirLinkConfiguration::AirLinkConfiguration(const AirLinkConfiguration *copy, QObject *parent) - : UDPConfiguration(copy) -{ - // qCDebug(AirLinkLinkLog) << Q_FUNC_INFO << this; - - Q_CHECK_PTR(copy); - - copyFrom(copy); -} - -AirLinkConfiguration::~AirLinkConfiguration() -{ - // qCDebug(AirLinkLinkLog) << Q_FUNC_INFO << this; -} - -void AirLinkConfiguration::setUsername(const QString &username) -{ - if (username != _username) { - _username = username; - emit usernameChanged(); - } -} - -void AirLinkConfiguration::setPassword(const QString &password) -{ - if (password != _password) { - _password = password; - emit passwordChanged(); - } -} - -void AirLinkConfiguration::setModemName(const QString &modemName) -{ - if (modemName != _modemName) { - _modemName = modemName; - emit modemNameChanged(); - } -} - -void AirLinkConfiguration::loadSettings(QSettings &settings, const QString &root) -{ - AppSettings *const appSettings = SettingsManager::instance()->appSettings(); - - settings.beginGroup(root); - - setUsername(settings.value(_usernameSettingsKey, appSettings->loginAirLink()->rawValueString()).toString()); - setPassword(settings.value(_passwordSettingsKey, appSettings->passAirLink()->rawValueString()).toString()); - setModemName(settings.value(_modemNameSettingsKey).toString()); - - settings.endGroup(); -} - -void AirLinkConfiguration::saveSettings(QSettings &settings, const QString &root) -{ - settings.beginGroup(root); - - settings.setValue(_usernameSettingsKey, _username); - settings.setValue(_passwordSettingsKey, _password); - settings.setValue(_modemNameSettingsKey, _modemName); - - settings.endGroup(); -} - -void AirLinkConfiguration::copyFrom(const LinkConfiguration *source) -{ - Q_CHECK_PTR(source); - - const AirLinkConfiguration *const AirLinkSource = qobject_cast(source); - if (AirLinkSource) { - setUsername(AirLinkSource->username()); - setPassword(AirLinkSource->password()); - setModemName(AirLinkSource->modemName()); - } else { - qCWarning(AirLinkLinkLog) << "Internal error: cannot read AirLinkConfiguration from given source"; - } - - const UDPConfiguration *const udpSource = qobject_cast(source); - if (udpSource) { - UDPConfiguration::copyFrom(udpSource); - } -} diff --git a/src/Comms/AirLink/AirLinkLink.h b/src/Comms/AirLink/AirLinkLink.h index f5cac7a759e..142238cdbb2 100644 --- a/src/Comms/AirLink/AirLinkLink.h +++ b/src/Comms/AirLink/AirLinkLink.h @@ -24,26 +24,25 @@ class AirLinkConfiguration : public UDPConfiguration Q_PROPERTY(QString password READ password WRITE setPassword NOTIFY passwordChanged) Q_PROPERTY(QString modemName READ modemName WRITE setModemName NOTIFY modemNameChanged) public: - AirLinkConfiguration(const QString &name, QObject *parent = nullptr); - AirLinkConfiguration(const AirLinkConfiguration *copy, QObject *parent = nullptr); + explicit AirLinkConfiguration(const QString &name, QObject *parent = nullptr); + explicit AirLinkConfiguration(const AirLinkConfiguration *copy, QObject *parent = nullptr); ~AirLinkConfiguration(); - QString username() const { return _username; } - QString password() const { return _password; } - QString modemName() const { return _modemName; } - - void setUsername(const QString &username); - void setPassword(const QString &password); - void setModemName(const QString &modemName); - LinkType type() const final { return LinkConfiguration::AirLink; } void copyFrom(const LinkConfiguration *source) final; - void loadSettings(QSettings &settings, const QString &root) final; void saveSettings(QSettings &settings, const QString &root) final; - QString settingsURL() final { return "AirLinkSettings.qml"; } + QString settingsURL() final { return QStringLiteral("AirLinkSettings.qml"); } QString settingsTitle() final { return tr("AirLink Link Settings"); } + QString username() const { return _username; } + void setUsername(const QString &username); + + QString password() const { return _password; } + void setPassword(const QString &password); + + QString modemName() const { return _modemName; } + void setModemName(const QString &modemName); signals: void usernameChanged(); @@ -67,21 +66,19 @@ class AirLinkLink : public UDPLink Q_OBJECT public: - AirLinkLink(SharedLinkConfigurationPtr &config, QObject *parent = nullptr); + explicit AirLinkLink(SharedLinkConfigurationPtr &config, QObject *parent = nullptr); ~AirLinkLink(); void disconnect() final; -private slots: - bool _connect() final; - private: + bool _connect() final; void _configureUdpSettings(); void _sendLoginMsgToAirLink(); bool _stillConnecting(); void _setConnectFlag(bool connect); - const AirLinkConfiguration *_AirLinkConfig = nullptr; + const AirLinkConfiguration *_airLinkConfig = nullptr; QMutex _mutex; bool _needToConnect = false; diff --git a/src/Comms/AirLink/AirLinkManager.cc b/src/Comms/AirLink/AirLinkManager.cc index b6237c0d4eb..a7ca216b37a 100644 --- a/src/Comms/AirLink/AirLinkManager.cc +++ b/src/Comms/AirLink/AirLinkManager.cc @@ -52,7 +52,7 @@ void AirLinkManager::updateCredentials(const QString &login, const QString &pass void AirLinkManager::_connectToAirLinkServer(const QString &login, const QString &pass) { - const QUrl url("https://air-link.space/api/gs/getModems"); + const QUrl url(QStringLiteral("https://air-link.space/api/gs/getModems")); QNetworkRequest request(url); request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");