QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
SendMavlinkMessageState.cc
Go to the documentation of this file.
2#include "MAVLinkProtocol.h"
4#include "Vehicle.h"
7
9 : QGCState(QStringLiteral("SendMavlinkMessageState"), parent)
10 , _encoder(std::move(encoder))
11 , _retryCount(retryCount)
12{
13 connect(this, &QState::entered, this, &SendMavlinkMessageState::_sendMessage);
14 // Note: _runCount is NOT reset on exit - it tracks retries across re-entries.
15 // The state machine is expected to be one-shot (delete after finished).
16}
17
18void SendMavlinkMessageState::_sendMessage()
19{
20 if (++_runCount > _retryCount + 1 /* +1 for initial attempt */) {
21 qCDebug(QGCStateMachineLog) << "Exceeded maximum retries" << stateName();
22 emit error();
23 return;
24 }
25
26 if (!_encoder) {
27 qCDebug(QGCStateMachineLog) << "No MAVLink message encoder configured" << stateName();
28 emit error();
29 return;
30 }
31
33 if (!sharedLink) {
34 qCWarning(QGCStateMachineLog) << "No active link available to send MAVLink message" << stateName();
35 emit error();
36 return;
37 }
38
39 mavlink_message_t message{};
40
41 const uint8_t systemId = MAVLinkProtocol::instance()->getSystemId();
42 const uint8_t componentId [[maybe_unused]] = MAVLinkProtocol::getComponentId();
43 const uint8_t channel = sharedLink->mavlinkChannel();
44
45 _encoder(systemId, channel, &message);
46 if (!vehicle()->sendMessageOnLinkThreadSafe(sharedLink.get(), message)) {
47 qCWarning(QGCStateMachineLog) << "Failed to send MAVLink message" << stateName();
48 emit error();
49 return;
50 }
51
52 emit advance();
53}
std::shared_ptr< LinkInterface > SharedLinkInterfacePtr
struct __mavlink_message mavlink_message_t
static int getComponentId()
Get the component id of this application.
static MAVLinkProtocol * instance()
int getSystemId() const
Get the system id of this application.
QString stateName() const
Vehicle * vehicle() const
SendMavlinkMessageState(QState *parent, MessageEncoder encoder, int retryCount)
std::function< void(uint8_t systemId, uint8_t channel, mavlink_message_t *message)> MessageEncoder
WeakLinkInterfacePtr primaryLink() const
VehicleLinkManager * vehicleLinkManager()
Definition Vehicle.h:580