Skip to content

Commit

Permalink
Comms: Update AIRLink Threading
Browse files Browse the repository at this point in the history
  • Loading branch information
HTRamsey committed Jan 7, 2025
1 parent eb2e314 commit 1203033
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 120 deletions.
2 changes: 1 addition & 1 deletion cmake/CustomOptions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
199 changes: 98 additions & 101 deletions src/Comms/AirLink/AirLinkLink.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<const UDPConfiguration*>(source);
Q_ASSERT(udpSource);
UDPConfiguration::copyFrom(udpSource);

const AirLinkConfiguration *const airLinkSource = qobject_cast<const AirLinkConfiguration*>(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<const AirLinkConfiguration*>(config.get()))
, _airLinkConfig(qobject_cast<const AirLinkConfiguration*>(config.get()))
{
// qCDebug(AirLinkLinkLog) << Q_FUNC_INFO << this;

Expand Down Expand Up @@ -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<QMetaObject::Connection> conn = std::make_shared<QMetaObject::Connection>();
Expand All @@ -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) {
Expand All @@ -85,6 +166,12 @@ bool AirLinkLink::_connect()
return true;
}

void AirLinkLink::disconnect()
{
_setConnectFlag(false);
(void) UDPLink::disconnect();
}

void AirLinkLink::_configureUdpSettings()
{
quint16 availablePort = 14550;
Expand All @@ -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];
Expand All @@ -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<const AirLinkConfiguration*>(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<const UDPConfiguration*>(source);
if (udpSource) {
UDPConfiguration::copyFrom(udpSource);
}
}
31 changes: 14 additions & 17 deletions src/Comms/AirLink/AirLinkLink.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/Comms/AirLink/AirLinkManager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down

0 comments on commit 1203033

Please sign in to comment.