QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
LinkManager.h
Go to the documentation of this file.
1#pragma once
2
3#include <functional>
4#include <limits>
5#include <memory>
6
7#include <QtCore/QDeadlineTimer>
8#include <QtCore/QList>
9#include <QtCore/QMutex>
10#include <QtCore/QMutexLocker>
11#include <QtCore/QStringList>
12#include <QtQmlIntegration/QtQmlIntegration>
13
14#include "LinkConfiguration.h"
15#include "LinkInterface.h"
16
18class LogReplayLink;
19class MAVLinkProtocol;
21class QTimer;
24
30class LinkManager : public QObject
31{
32 Q_OBJECT
33 QML_ELEMENT
34 QML_UNCREATABLE("")
35 Q_MOC_INCLUDE("QmlObjectListModel.h")
36 Q_MOC_INCLUDE("LogReplayLink.h")
38 Q_PROPERTY(QmlObjectListModel *linkConfigurations READ _qmlLinkConfigurations CONSTANT)
39 Q_PROPERTY(QStringList linkTypeStrings READ linkTypeStrings CONSTANT)
41
42 friend class LinkManagerTest;
43
44public:
45 explicit LinkManager(QObject *parent = nullptr);
46 ~LinkManager();
47
48 static LinkManager *instance();
49
50 void init();
51
53 Q_INVOKABLE LinkConfiguration *createConfiguration(int type, const QString &name);
55 Q_INVOKABLE void cancelConfigurationEditing(LinkConfiguration *config) const { delete config; }
56 Q_INVOKABLE void endConfigurationEditing(LinkConfiguration *config, LinkConfiguration *editedConfig);
57 Q_INVOKABLE void endCreateConfiguration(LinkConfiguration *config);
58 Q_INVOKABLE void removeConfiguration(LinkConfiguration *config);
60 Q_INVOKABLE void createConnectedLink(const LinkConfiguration *config);
61 Q_INVOKABLE void disconnectLink(LinkInterface *link);
63 Q_INVOKABLE void disconnectLinkConfiguration(LinkConfiguration *config);
64 Q_INVOKABLE void createMavlinkForwardingSupportLink();
66 Q_INVOKABLE void shutdown();
67 Q_INVOKABLE LogReplayLink *startLogReplay(const QString &logFile);
68
69 QList<SharedLinkInterfacePtr> links();
70 QStringList linkTypeStrings() const;
71 bool mavlinkSupportForwardingEnabled() const { return _mavlinkSupportForwardingEnabled; }
72
75
78 void setConnectionsSuspended(const QString &reason) { _connectionsSuspended = true; _connectionsSuspendedReason = reason; }
79
81 void setConnectionsAllowed() { _connectionsSuspended = false; }
82
83 bool connectionsSuspended() const { return _connectionsSuspended; }
84
87
90
93
94 void disconnectAll();
95
98 uint8_t allocateMavlinkChannel();
99 void freeMavlinkChannel(uint8_t channel);
100
104
105 bool containsLink(const LinkInterface *link);
106
108 bool containsConfiguration(const QString& name) const;
109
111
112 static bool isBluetoothAvailable();
113
114 static bool isLinkUSBDirect(const LinkInterface *link);
115
116 static constexpr uint8_t invalidMavlinkChannel() { return std::numeric_limits<uint8_t>::max(); }
117
118signals:
121
122private slots:
123 void _linkConnected();
124 void _linkDisconnected();
125 void _communicationError(const QString &title, const QString &error);
126
127private:
128 QmlObjectListModel *_qmlLinkConfigurations();
130 bool _connectionsSuspendedMsg() const;
131 void _updateAutoConnectLinks();
132 void _removeConfiguration(const LinkConfiguration *config);
133 void _addUDPAutoConnectLink();
134 void _addMAVLinkForwardingLink();
135 void _reconnectAutoConnectLinks();
137 void _retryDynamicUdpLink(const QString& name, const std::function<void(UDPConfiguration&)>& configure);
138 SharedLinkConfigurationPtr _findDynamicUdpConfiguration(const QString& name) const;
139
140 QTimer *_portListTimer = nullptr;
141 QmlObjectListModel *_qmlConfigurations = nullptr;
142 AutoConnectSettings *_autoConnectSettings = nullptr;
143
144 bool _configUpdateSuspended = false;
145 bool _configurationsLoaded = false;
146 bool _connectionsSuspended = false;
147 bool _mavlinkSupportForwardingEnabled = false;
148 uint32_t _mavlinkChannelsUsedBitMask = 1;
149 QString _connectionsSuspendedReason;
150
151 QMutex _linksMutex;
152 QList<SharedLinkInterfacePtr> _rgLinks;
153 QList<SharedLinkConfigurationPtr> _rgLinkConfigs;
154
155 static constexpr const char *_defaultUDPLinkName = "UDP Link (AutoConnect)";
156 static constexpr const char *_mavlinkForwardingLinkName = "MAVLink Forwarding Link";
157 static constexpr const char *_mavlinkForwardingSupportLinkName = "MAVLink Support Forwarding Link";
158
159 static constexpr int _autoconnectUpdateTimerMSecs = 1000;
160#ifndef QGC_NO_SERIAL_LINK
161private:
162 Q_PROPERTY(QStringList serialBaudRates READ serialBaudRates CONSTANT)
163 Q_PROPERTY(QStringList serialPortStrings READ serialPortStrings NOTIFY commPortStringsChanged)
164 Q_PROPERTY(QStringList serialPorts READ serialPorts NOTIFY commPortsChanged)
165
166public:
167 static QStringList serialBaudRates();
168 QStringList serialPortStrings();
169 QStringList serialPorts();
172 Q_INVOKABLE bool serialPortHasFlowControl(const QString& portName) const;
173
174signals:
177
178private:
179 void _updateSerialPorts();
181 bool _serialPortInUseMsg(const LinkConfiguration& config);
185 bool _usePersistentPortName(LinkConfiguration* config);
189 bool _awaitUsbPermission(const SharedLinkConfigurationPtr& config);
190 void _connectLinkAwaitingUsbPermission();
191 std::weak_ptr<LinkConfiguration> _configAwaitingUsbPermission;
192 QDeadlineTimer _usbPermissionDeadline;
193 std::unique_ptr<SerialAutoConnect> _serialAutoConnect;
194 QStringList _commPortList;
195 QStringList _commPortDisplayList;
196#endif // QGC_NO_SERIAL_LINK
197};
std::shared_ptr< LinkConfiguration > SharedLinkConfigurationPtr
std::shared_ptr< LinkInterface > SharedLinkInterfacePtr
Auto connect settings.
Interface holding link specific settings.
The link interface defines the interface for all links used to communicate with the ground station ap...
void commPortStringsChanged()
Q_INVOKABLE void createConnectedLink(const LinkConfiguration *config)
This should only be used by Qml code.
Q_INVOKABLE bool serialPortHasFlowControl(const QString &portName) const
SharedLinkInterfacePtr sharedLinkInterfacePointerForLink(const LinkInterface *link)
QStringList serialPorts()
bool containsConfiguration(const QString &name) const
Q_INVOKABLE void endConfigurationEditing(LinkConfiguration *config, LinkConfiguration *editedConfig)
Q_INVOKABLE void cancelConfigurationEditing(LinkConfiguration *config) const
Definition LinkManager.h:55
SharedLinkConfigurationPtr addConfiguration(LinkConfiguration *config)
QStringList linkTypeStrings() const
void isBluetoothAvailableChanged()
void loadLinkConfigurationList()
bool connectionsSuspended() const
Definition LinkManager.h:83
static bool isLinkUSBDirect(const LinkInterface *link)
uint8_t allocateMavlinkChannel()
Q_INVOKABLE void createMavlinkForwardingSupportLink()
Q_INVOKABLE LinkConfiguration * createConfiguration(int type, const QString &name)
Create/Edit Link Configuration.
void startAutoConnectedLinks()
Q_INVOKABLE void endCreateConfiguration(LinkConfiguration *config)
void setConnectionsAllowed()
Sets the flag to allow new connections to be made.
Definition LinkManager.h:81
Q_INVOKABLE void shutdown()
Called to signal app shutdown. Disconnects all links while turning off auto-connect.
LinkManager(QObject *parent=nullptr)
static LinkManager * instance()
void freeMavlinkChannel(uint8_t channel)
Q_INVOKABLE void removeConfiguration(LinkConfiguration *config)
friend class LinkManagerTest
Definition LinkManager.h:42
QList< SharedLinkInterfacePtr > links()
static constexpr uint8_t invalidMavlinkChannel()
void setConnectionsSuspended(const QString &reason)
Definition LinkManager.h:78
void disconnectAll()
Q_INVOKABLE LogReplayLink * startLogReplay(const QString &logFile)
void commPortsChanged()
QStringList serialPortStrings()
Q_INVOKABLE void disconnectLink(LinkInterface *link)
static QStringList serialBaudRates()
SharedLinkInterfacePtr mavlinkForwardingSupportLink()
Returns pointer to the mavlink support forwarding link, or nullptr if it does not exist.
Q_INVOKABLE void disconnectLinkConfiguration(LinkConfiguration *config)
Stop a link and suppress auto-reconnect, working whether or not a live link currently exists.
Q_INVOKABLE LinkConfiguration * startConfigurationEditing(LinkConfiguration *config)
bool containsLink(const LinkInterface *link)
SharedLinkInterfacePtr mavlinkForwardingLink()
Returns pointer to the mavlink forwarding link, or nullptr if it does not exist.
static bool isBluetoothAvailable()
void saveLinkConfigurationList()
void mavlinkSupportForwardingEnabledChanged()
bool mavlinkSupportForwardingEnabled() const
Definition LinkManager.h:71
MAVLink micro air vehicle protocol reference implementation.
Application-thread MAVLink serial discovery. LinkManager owns creation and live links.