QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
LinkInterface.cc
Go to the documentation of this file.
1#include "LinkInterface.h"
2#include "LinkManager.h"
3#include "QGCApplication.h"
5#include "MAVLinkSigning.h"
6#include "SettingsManager.h"
7#include "MavlinkSettings.h"
8
9#include <QtQml/QQmlEngine>
10
11QGC_LOGGING_CATEGORY(LinkInterfaceLog, "Comms.LinkInterface")
12
14 : QObject(parent)
15 , _config(config)
16{
17 QQmlEngine::setObjectOwnership(this, QQmlEngine::CppOwnership);
18}
19
21{
22 if (_vehicleReferenceCount != 0) {
23 qCWarning(LinkInterfaceLog) << "still have vehicle references:" << _vehicleReferenceCount;
24 }
25
26 _config.reset();
27}
28
30{
31 if (!mavlinkChannelIsSet()) {
32 qCWarning(LinkInterfaceLog) << "mavlinkChannelIsSet() == false";
33 }
34
35 return _mavlinkChannel;
36}
37
39{
40 return (LinkManager::invalidMavlinkChannel() != _mavlinkChannel);
41}
42
44{
45 if (!isSecureConnection()) {
46 auto mavlinkSettings = SettingsManager::instance()->mavlinkSettings();
47 const QByteArray signingKeyBytes = mavlinkSettings->mavlink2SigningKey()->rawValue().toByteArray();
49 if (signingKeyBytes.isEmpty()) {
50 qCDebug(LinkInterfaceLog) << "Signing disabled on channel" << _mavlinkChannel;
51 } else {
52 qCDebug(LinkInterfaceLog) << "Signing enabled on channel" << _mavlinkChannel;
53 }
54 } else {
55 qCWarning(LinkInterfaceLog) << "Failed To enable Signing on channel" << _mavlinkChannel;
56 // FIXME: What should we do here?
57 return false;
58 }
59 }
60
61 return true;
62}
63
65{
66 Q_ASSERT(!mavlinkChannelIsSet());
67
68 if (mavlinkChannelIsSet()) {
69 qCWarning(LinkInterfaceLog) << "already have" << _mavlinkChannel;
70 return true;
71 }
72
73 _mavlinkChannel = LinkManager::instance()->allocateMavlinkChannel();
74
75 if (!mavlinkChannelIsSet()) {
76 qCWarning(LinkInterfaceLog) << "failed";
77 return false;
78 }
79
80 qCDebug(LinkInterfaceLog) << "_allocateMavlinkChannel" << _mavlinkChannel;
81
82 mavlink_set_proto_version(_mavlinkChannel, MAVLINK_VERSION); // We only support v2 protcol
84
85 return true;
86}
87
89{
90 qCDebug(LinkInterfaceLog) << _mavlinkChannel;
91
92 if (!mavlinkChannelIsSet()) {
93 return;
94 }
95
96 LinkManager::instance()->freeMavlinkChannel(_mavlinkChannel);
97 _mavlinkChannel = LinkManager::invalidMavlinkChannel();
98}
99
100void LinkInterface::writeBytesThreadSafe(const char *bytes, int length)
101{
102 const QByteArray data(bytes, length);
103 (void) QMetaObject::invokeMethod(this, "_writeBytes", Qt::AutoConnection, data);
104}
105
107{
108 if (_vehicleReferenceCount != 0) {
109 _vehicleReferenceCount--;
111 } else {
112 qCWarning(LinkInterfaceLog) << "called with no vehicle references";
113 }
114}
115
117{
118 if (_vehicleReferenceCount == 0) {
119 // Since there are no vehicles on the link we can disconnect it right now
120 disconnect();
121 } else {
122 // If there are still vehicles on this link we allow communication lost to trigger and don't automatically disconect until all the vehicles go away
123 }
124}
125
127{
128 if (_signingSignatureFailure != failure) {
129 _signingSignatureFailure = failure;
130 if (_signingSignatureFailure) {
131 emit communicationError(tr("Signing Failure"), tr("Signing signature mismatch"));
132 }
133 }
134}
135
137{
138 if (!_mavlinkV1TrafficReported) {
139 _mavlinkV1TrafficReported = true;
140
142 const QString linkName = linkConfig ? linkConfig->name() : QStringLiteral("unknown");
143 qCWarning(LinkInterfaceLog) << "MAVLink v1 traffic detected on link" << linkName;
144 const QString message = tr("MAVLink v1 traffic detected on link '%1'. "
145 "%2 only supports MAVLink v2. "
146 "Please ensure your vehicle is configured to use MAVLink v2.")
147 .arg(linkName).arg(qgcApp()->applicationName());
148 qgcApp()->showAppMessage(message);
149 }
150}
std::shared_ptr< LinkConfiguration > SharedLinkConfigurationPtr
mavlink_channel_t
Definition MAVLinkLib.h:7
#define qgcApp()
#define QGC_LOGGING_CATEGORY(name, categoryStr)
The link interface defines the interface for all links used to communicate with the ground station ap...
virtual ~LinkInterface()
uint8_t mavlinkChannel() const
virtual void _freeMavlinkChannel()
void reportMavlinkV1Traffic()
void _connectionRemoved()
bool mavlinkChannelIsSet() const
virtual bool _allocateMavlinkChannel()
virtual void disconnect()=0
bool initMavlinkSigning()
void removeVehicleReference()
virtual bool isSecureConnection() const
Returns true if the connection is secure (e.g. USB, wired ethernet)
void communicationError(const QString &title, const QString &error)
void setSigningSignatureFailure(bool failure)
void writeBytesThreadSafe(const char *bytes, int length)
SharedLinkConfigurationPtr linkConfiguration()
SharedLinkConfigurationPtr _config
Fact *mavlink2SigningKey READ mavlink2SigningKey CONSTANT Fact * mavlink2SigningKey()
bool insecureConnectionAccceptUnsignedCallback(const mavlink_status_t *status, uint32_t message_id)
bool initSigning(mavlink_channel_t channel, QByteArrayView key, mavlink_accept_unsigned_t callback)