QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
BluetoothLink.cc
Go to the documentation of this file.
1#include "BluetoothLink.h"
2#include "BluetoothWorker.h"
4
5#include <QtCore/QCoreApplication>
6#include <QtCore/QPermissions>
7#include <QtCore/QThread>
8
9QGC_LOGGING_CATEGORY(BluetoothLinkLog, "Comms.Bluetooth.BluetoothLink")
10
11/*===========================================================================*/
12
14 : LinkInterface(config, parent)
15 , _bluetoothConfig(qobject_cast<BluetoothConfiguration*>(config.get()))
16 , _workerThread(new QThread(this))
17{
18 if (!_bluetoothConfig) {
19 qCCritical(BluetoothLinkLog) << "Invalid BluetoothConfiguration";
20 return;
21 }
22
23 _worker = BluetoothWorker::create(_bluetoothConfig);
24
25 qCDebug(BluetoothLinkLog) << this;
26
27 const QString threadName = (_bluetoothConfig->mode() == BluetoothConfiguration::BluetoothMode::ModeLowEnergy)
28 ? QStringLiteral("BLE_%1") : QStringLiteral("Bluetooth_%1");
29 _workerThread->setObjectName(threadName.arg(_bluetoothConfig->name()));
30
31 _worker->moveToThread(_workerThread.data());
32
33 (void) connect(_workerThread.data(), &QThread::started, _worker.data(), &BluetoothWorker::setupConnection);
34 (void) connect(_workerThread.data(), &QThread::finished, _worker.data(), &QObject::deleteLater);
35
36 (void) connect(_worker.data(), &BluetoothWorker::connected, this, &BluetoothLink::_onConnected, Qt::QueuedConnection);
37 (void) connect(_worker.data(), &BluetoothWorker::disconnected, this, &BluetoothLink::_onDisconnected, Qt::QueuedConnection);
38 (void) connect(_worker.data(), &BluetoothWorker::errorOccurred, this, &BluetoothLink::_onErrorOccurred, Qt::QueuedConnection);
39 (void) connect(_worker.data(), &BluetoothWorker::dataReceived, this, &BluetoothLink::_onDataReceived, Qt::QueuedConnection);
40 (void) connect(_worker.data(), &BluetoothWorker::dataSent, this, &BluetoothLink::_onDataSent, Qt::QueuedConnection);
41 (void) connect(_worker.data(), &BluetoothWorker::rssiUpdated, this, &BluetoothLink::_onRssiUpdated, Qt::QueuedConnection);
42
43 (void) connect(_bluetoothConfig, &BluetoothConfiguration::errorOccurred, this, &BluetoothLink::_onErrorOccurred);
44
45 _checkPermission();
46}
47
49{
50 if (_workerThread) {
51 if (_workerThread->isRunning() && _connectedCache && _worker) {
52 (void) QMetaObject::invokeMethod(_worker, "disconnectLink", Qt::QueuedConnection);
53 }
54
55 _shutdownWorkerThread(_workerThread, BluetoothLinkLog());
56 }
57
58 qCDebug(BluetoothLinkLog) << this;
59}
60
62{
63 return _connectedCache;
64}
65
66bool BluetoothLink::_connect()
67{
68 if (!_worker) {
69 return false;
70 }
71 if (_bluetoothConfig) {
72 _bluetoothConfig->stopScan();
73 }
74 return QMetaObject::invokeMethod(_worker.data(), "connectLink", Qt::QueuedConnection);
75}
76
78{
79 if (_worker) {
80 (void) QMetaObject::invokeMethod(_worker.data(), "disconnectLink", Qt::QueuedConnection);
81 }
82}
83
84void BluetoothLink::_onConnected()
85{
86 _connectedCache = true;
87 _disconnectedEmitted = false;
88 emit connected();
89}
90
91void BluetoothLink::_onDisconnected()
92{
93 _connectedCache = false;
94 if (!_disconnectedEmitted.exchange(true)) {
95 emit disconnected();
96 }
97}
98
99void BluetoothLink::_onErrorOccurred(const QString &errorString)
100{
101 qCWarning(BluetoothLinkLog) << "Communication error:" << errorString;
102
103 if (!_bluetoothConfig) {
104 emit communicationError(tr("Bluetooth Link Error"), errorString);
105 return;
106 }
107
108 const QString linkType = (_bluetoothConfig->mode() == BluetoothConfiguration::BluetoothMode::ModeLowEnergy)
109 ? tr("Bluetooth Low Energy") : tr("Bluetooth");
110
111 emit communicationError(tr("%1 Link Error").arg(linkType),
112 tr("Link %1: (Device: %2) %3").arg(_bluetoothConfig->name(),
113 _bluetoothConfig->device().name(),
114 errorString));
115}
116
117void BluetoothLink::_onDataReceived(const QByteArray &data)
118{
119 emit bytesReceived(this, data);
120}
121
122void BluetoothLink::_onDataSent(const QByteArray &data)
123{
124 emit bytesSent(this, data);
125}
126
127void BluetoothLink::_onRssiUpdated(qint16 rssi)
128{
129 if (_bluetoothConfig) {
130 _bluetoothConfig->setConnectedRssi(rssi);
131 }
132}
133
134void BluetoothLink::_writeBytes(const QByteArray &bytes)
135{
136 if (_worker) {
137 (void) QMetaObject::invokeMethod(_worker.data(), "writeData", Qt::QueuedConnection, Q_ARG(QByteArray, bytes));
138 }
139}
140
141void BluetoothLink::_checkPermission()
142{
143 QBluetoothPermission permission;
144 permission.setCommunicationModes(QBluetoothPermission::Access);
145
146 const Qt::PermissionStatus permissionStatus = QCoreApplication::instance()->checkPermission(permission);
147 if (permissionStatus == Qt::PermissionStatus::Undetermined) {
148 QCoreApplication::instance()->requestPermission(permission, this, [this](const QPermission &perm) {
149 _handlePermissionStatus(perm.status());
150 });
151 } else {
152 _handlePermissionStatus(permissionStatus);
153 }
154}
155
156void BluetoothLink::_handlePermissionStatus(Qt::PermissionStatus permissionStatus)
157{
158 if (permissionStatus != Qt::PermissionStatus::Granted) {
159 qCWarning(BluetoothLinkLog) << "Bluetooth Permission Denied";
160 _onErrorOccurred(tr("Bluetooth Permission Denied"));
161 _onDisconnected();
162 return;
163 }
164
165 qCDebug(BluetoothLinkLog) << "Bluetooth Permission Granted";
166
167 if (_workerThread && !_workerThread->isRunning()) {
168 _workerThread->start();
169 if (!_workerThread->isRunning()) {
170 qCCritical(BluetoothLinkLog) << "Failed to start worker thread";
171 _onErrorOccurred(tr("Failed to start Bluetooth worker thread"));
172 _worker->deleteLater();
173 _worker = nullptr;
174 }
175 }
176}
std::shared_ptr< LinkConfiguration > SharedLinkConfigurationPtr
#define QGC_LOGGING_CATEGORY(name, categoryStr)
void errorOccurred(const QString &errorString)
void dataReceived(const QByteArray &data)
void dataSent(const QByteArray &data)
void errorOccurred(const QString &errorString)
void rssiUpdated(qint16 rssi)
static BluetoothWorker * create(const BluetoothConfiguration *config, QObject *parent=nullptr)
void bytesReceived(LinkInterface *link, const QByteArray &data)
void disconnected()
void _shutdownWorkerThread(QThread *thread, const QLoggingCategory &category, bool allowTerminate=true)
LinkInterface(SharedLinkConfigurationPtr &config, QObject *parent=nullptr)
Links are only created by LinkManager so constructor is not public.
void communicationError(const QString &title, const QString &error)
void bytesSent(LinkInterface *link, const QByteArray &data)
void connected()