QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
BluetoothWorker.h
Go to the documentation of this file.
1#pragma once
2
4
5#include <QtBluetooth/QBluetoothDeviceInfo>
6#include <QtBluetooth/QBluetoothUuid>
7#include <QtCore/QPointer>
8#include <QtCore/QTimer>
9
10#include <atomic>
11
12class BluetoothWorker : public QObject
13{
14 Q_OBJECT
15
16public:
17 explicit BluetoothWorker(const BluetoothConfiguration *config, QObject *parent = nullptr);
18 ~BluetoothWorker() override;
19
20 bool isConnected() const;
21
22 static BluetoothWorker *create(const BluetoothConfiguration *config, QObject *parent = nullptr);
23
24signals:
25 void connected();
27 void errorOccurred(const QString &errorString);
28 void dataReceived(const QByteArray &data);
29 void dataSent(const QByteArray &data);
30 void rssiUpdated(qint16 rssi);
31
32public slots:
33 void setupConnection();
34 void connectLink();
35 void disconnectLink();
36 void writeData(const QByteArray &data);
37
38protected slots:
39 void _reconnectTimeout();
41
42protected:
43 virtual void onSetupConnection() = 0;
44 virtual void onConnectLink() = 0;
45 virtual void onDisconnectLink() = 0;
46 virtual void onWriteData(const QByteArray &data) = 0;
47 virtual void onServiceDiscoveryTimeout() = 0;
49
50 const QBluetoothDeviceInfo _device;
51 const QBluetoothUuid _serviceUuid;
52 const QBluetoothUuid _readCharacteristicUuid;
53 const QBluetoothUuid _writeCharacteristicUuid;
54
55 QPointer<QTimer> _reconnectTimer;
56 QPointer<QTimer> _serviceDiscoveryTimer;
57
58 std::atomic<bool> _intentionalDisconnect{false};
59 std::atomic<bool> _connected{false};
60 std::atomic<int> _reconnectAttempts{0};
62
63 static constexpr int MAX_RECONNECT_ATTEMPTS = 10;
64 static constexpr int RECONNECT_BASE_INTERVAL_MS = 5000;
65 static constexpr int MAX_RECONNECT_INTERVAL_MS = 60000;
66 static constexpr int SERVICE_DISCOVERY_TIMEOUT_MS = 30000;
67 static constexpr int MAX_CONSECUTIVE_FAILURES = 3;
68};
QString errorString
static constexpr int MAX_RECONNECT_INTERVAL_MS
virtual void onSetupConnection()=0
void dataReceived(const QByteArray &data)
void writeData(const QByteArray &data)
const QBluetoothUuid _readCharacteristicUuid
QPointer< QTimer > _serviceDiscoveryTimer
void dataSent(const QByteArray &data)
const QBluetoothUuid _serviceUuid
void errorOccurred(const QString &errorString)
virtual void onWriteData(const QByteArray &data)=0
const QBluetoothUuid _writeCharacteristicUuid
void rssiUpdated(qint16 rssi)
static constexpr int MAX_CONSECUTIVE_FAILURES
virtual void onResetAfterConsecutiveFailures()=0
virtual void onConnectLink()=0
static constexpr int MAX_RECONNECT_ATTEMPTS
QPointer< QTimer > _reconnectTimer
void _serviceDiscoveryTimeout()
virtual void onServiceDiscoveryTimeout()=0
std::atomic< int > _reconnectAttempts
static BluetoothWorker * create(const BluetoothConfiguration *config, QObject *parent=nullptr)
std::atomic< bool > _connected
bool isConnected() const
const QBluetoothDeviceInfo _device
~BluetoothWorker() override
virtual void onDisconnectLink()=0
std::atomic< bool > _intentionalDisconnect
static constexpr int RECONNECT_BASE_INTERVAL_MS
static constexpr int SERVICE_DISCOVERY_TIMEOUT_MS