QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
LinkConfiguration.h
Go to the documentation of this file.
1#pragma once
2
3#include <QtCore/QDeadlineTimer>
4#include <QtCore/QElapsedTimer>
5#include <QtCore/QSettings>
6#include <QtCore/QString>
7#include <QtQmlIntegration/QtQmlIntegration>
8
9class LinkInterface;
10
13class LinkConfiguration : public QObject
14{
15 Q_OBJECT
16 QML_ELEMENT
17 QML_UNCREATABLE("")
18 Q_MOC_INCLUDE("LinkInterface.h")
19
20 Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
21 Q_PROPERTY(LinkInterface *link READ link NOTIFY linkChanged)
22 Q_PROPERTY(bool linkActive READ linkActive NOTIFY linkActiveChanged)
23 Q_PROPERTY(LinkType linkType READ type CONSTANT)
24 Q_PROPERTY(bool dynamic READ isDynamic WRITE setDynamic NOTIFY dynamicChanged)
25 Q_PROPERTY(bool autoConnect READ isAutoConnect WRITE setAutoConnect NOTIFY autoConnectChanged)
26 Q_PROPERTY(QString settingsURL READ settingsURL CONSTANT)
27 Q_PROPERTY(QString settingsTitle READ settingsTitle CONSTANT)
28 Q_PROPERTY(bool highLatency READ isHighLatency WRITE setHighLatency NOTIFY highLatencyChanged)
29
30public:
31 LinkConfiguration(const QString &name, QObject *parent = nullptr);
32 LinkConfiguration(const LinkConfiguration *copy, QObject *parent = nullptr);
33 virtual ~LinkConfiguration();
34
35 QString name() const { return _name; }
36 void setName(const QString &name);
37
38 LinkInterface *link() const { return _link.lock().get(); }
39 void setLink(const std::shared_ptr<LinkInterface> link);
40
43 bool linkActive() const { return (link() != nullptr) || (_autoConnect && _autoConnectStarted && !_suppressAutoReconnect); }
44
47 bool isDynamic() const { return _dynamic; }
48
50 void setDynamic(bool dynamic = true);
51
54 bool isForwarding() const { return _forwarding; }
55
57 void setForwarding(bool forwarding = true) { _forwarding = forwarding; };
58
59 bool isAutoConnect() const { return _autoConnect; }
60
62 virtual void setAutoConnect(bool autoc = true);
63
64 bool suppressAutoReconnect() const { return _suppressAutoReconnect; }
65 void setSuppressAutoReconnect(bool suppress) {
66 if (_suppressAutoReconnect != suppress) { _suppressAutoReconnect = suppress; emit linkActiveChanged(); }
67 }
68
69 bool autoConnectStarted() const { return _autoConnectStarted; }
70 void setAutoConnectStarted(bool started) {
71 if (_autoConnectStarted != started) { _autoConnectStarted = started; emit linkActiveChanged(); }
72 }
73
74 bool reconnectReady() const { return _nextReconnect.hasExpired(); }
76 const int exp = qMin(_reconnectAttempts, 16);
77 _reconnectAttempts = qMin(_reconnectAttempts + 1, 17);
78 _nextReconnect = QDeadlineTimer(qMin(_reconnectBaseMs << exp, _reconnectMaxMs));
79 }
80 void resetReconnectBackoff() { _reconnectAttempts = 0; _nextReconnect = QDeadlineTimer(); }
81 void noteConnected() { _connectedTimer.start(); }
84 if (_connectedTimer.isValid() && (_connectedTimer.elapsed() >= _reconnectStableMs)) {
86 }
87 _connectedTimer.invalidate();
88 }
89
92 bool isHighLatency() const { return _highLatency; }
93
95 void setHighLatency(bool hl = false);
96
100 virtual void copyFrom(const LinkConfiguration *source);
101
104 enum LinkType {
105#ifndef QGC_NO_SERIAL_LINK
107#endif
111#ifdef QT_DEBUG
112 TypeMock,
113#endif
115 TypeLast // Last type value (type >= TypeLast == invalid)
116 };
117 Q_ENUM(LinkType)
118
119
121 virtual LinkType type() const = 0;
122
126 virtual void loadSettings(QSettings &settings, const QString &root) = 0;
127
131 virtual void saveSettings(QSettings &settings, const QString &root) const = 0;
132
134 virtual QString settingsURL() const = 0;
135
137 virtual QString settingsTitle() const = 0;
138
141 static LinkConfiguration *createSettings(int type, const QString &name);
142
146
149 static QString settingsRoot() { return QStringLiteral("LinkConfigurations"); }
150
151signals:
152 void nameChanged(const QString &name);
158
159protected:
160 std::weak_ptr<LinkInterface> _link;
161
162private:
163 QString _name;
164 bool _dynamic = false;
165 bool _forwarding = false;
166 bool _autoConnect = false;
167 bool _highLatency = false;
168 bool _suppressAutoReconnect = false;
169 bool _autoConnectStarted = false;
170 int _reconnectAttempts = 0;
171 QDeadlineTimer _nextReconnect;
172 QElapsedTimer _connectedTimer;
173
174 static constexpr int _reconnectBaseMs = 1000;
175 static constexpr int _reconnectMaxMs = 5000;
176 static constexpr int _reconnectStableMs = 2000;
177};
178
179typedef std::shared_ptr<LinkConfiguration> SharedLinkConfigurationPtr;
180typedef std::weak_ptr<LinkConfiguration> WeakLinkConfigurationPtr;
std::shared_ptr< LinkConfiguration > SharedLinkConfigurationPtr
std::weak_ptr< LinkConfiguration > WeakLinkConfigurationPtr
Interface holding link specific settings.
std::weak_ptr< LinkInterface > _link
Link currently using this configuration (if any)
@ TypeSerial
Serial Link.
@ TypeBluetooth
Bluetooth Link.
virtual void loadSettings(QSettings &settings, const QString &root)=0
virtual LinkType type() const =0
static LinkConfiguration * duplicateSettings(const LinkConfiguration *source)
virtual void copyFrom(const LinkConfiguration *source)
bool isAutoConnect() const
void setDynamic(bool dynamic=true)
Set if this is this a dynamic configuration. (decided at runtime)
virtual QString settingsURL() const =0
Settings URL, Pure virtual method providing the URL for the (QML) settings dialog.
virtual void saveSettings(QSettings &settings, const QString &root) const =0
void setAutoConnectStarted(bool started)
bool suppressAutoReconnect() const
void setHighLatency(bool hl=false)
Set if this is this an High Latency configuration.
static LinkConfiguration * createSettings(int type, const QString &name)
void setForwarding(bool forwarding=true)
Set if this is this a forwarding link configuration. (decided at runtime)
static QString settingsRoot()
void noteDisconnected()
Reset backoff only if the link stayed up long enough to count as working.
LinkInterface * link() const
void autoConnectChanged()
void highLatencyChanged()
void nameChanged(const QString &name)
void setSuppressAutoReconnect(bool suppress)
bool autoConnectStarted() const
void setName(const QString &name)
QString name() const
virtual QString settingsTitle() const =0
Settings Title, Pure virtual method providing the Title for the (QML) settings dialog.
void setLink(const std::shared_ptr< LinkInterface > link)
bool linkActive() const
bool isForwarding() const
bool isHighLatency() const
void linkActiveChanged()
virtual void setAutoConnect(bool autoc=true)
Set if this is this an Auto Connect configuration.
bool reconnectReady() const
The link interface defines the interface for all links used to communicate with the ground station ap...