QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
RequestMessageState.cc
Go to the documentation of this file.
3#include "Vehicle.h"
5
7 uint32_t messageId,
8 MessageHandler messageHandler,
9 int compId,
10 int timeoutMsecs,
11 float param1,
12 float param2,
13 float param3,
14 float param4,
15 float param5)
16 : WaitStateBase(QStringLiteral("RequestMessageState:%1").arg(messageId), parent, timeoutMsecs)
17 , _messageId(messageId)
18 , _messageHandler(std::move(messageHandler))
19 , _compId(compId)
20 , _param1(param1)
21 , _param2(param2)
22 , _param3(param3)
23 , _param4(param4)
24 , _param5(param5)
25{
26}
27
29{
31
33
34 if (!sharedLink) {
35 qCDebug(QGCStateMachineLog) << "Skipping request due to no primary link" << stateName();
37 waitFailed();
38 return;
39 }
40
41 if (sharedLink->linkConfiguration()->isHighLatency() || sharedLink->isLogReplay()) {
42 qCDebug(QGCStateMachineLog) << "Skipping request due to link type" << stateName();
43 // Not an error - just skip on high latency/replay links
45 return;
46 }
47
48 qCDebug(QGCStateMachineLog) << "Requesting message id" << _messageId << stateName();
49 vehicle()->requestMessage(_resultHandler, this, _compId, _messageId, _param1, _param2, _param3, _param4, _param5);
50}
51
53{
54 qCDebug(QGCStateMachineLog) << "Timeout waiting for message" << _messageId << stateName();
57}
58
59void RequestMessageState::_resultHandler(void* resultHandlerData,
60 MAV_RESULT commandResult,
62 const mavlink_message_t& message)
63{
64 auto* state = static_cast<RequestMessageState*>(resultHandlerData);
65 state->_failureCode = failureCode;
66 state->_commandResult = commandResult;
67
69 qCDebug(QGCStateMachineLog) << "Message received successfully" << state->stateName();
70
71 if (state->_messageHandler) {
72 state->_messageHandler(state->vehicle(), message);
73 }
74
75 emit state->messageReceived(message);
76 state->waitComplete();
77 } else {
78 switch (failureCode) {
80 qCDebug(QGCStateMachineLog) << "Command error" << commandResult << state->stateName();
81 break;
83 qCDebug(QGCStateMachineLog) << "Command not acked" << state->stateName();
84 break;
86 qCDebug(QGCStateMachineLog) << "Message not received" << state->stateName();
87 break;
89 qCDebug(QGCStateMachineLog) << "Duplicate command" << state->stateName();
90 break;
91 default:
92 qCDebug(QGCStateMachineLog) << "Unknown failure" << failureCode << state->stateName();
93 break;
94 }
95 state->waitFailed();
96 }
97}
std::shared_ptr< LinkInterface > SharedLinkInterfacePtr
struct __mavlink_message mavlink_message_t
QString stateName() const
Vehicle * vehicle() const
Requests a MAVLink message from the vehicle using MAV_CMD_REQUEST_MESSAGE.
void onWaitEntered() override
Called when the state is entered - subclasses should call base implementation.
MAV_RESULT commandResult() const
VehicleTypes::RequestMessageResultHandlerFailureCode_t failureCode() const
std::function< void(Vehicle *vehicle, const mavlink_message_t &message)> MessageHandler
void onWaitTimeout() override
Called when the timeout expires - default emits timeout(), subclasses can override.
RequestMessageState(QState *parent, uint32_t messageId, MessageHandler messageHandler=MessageHandler(), int compId=MAV_COMP_ID_AUTOPILOT1, int timeoutMsecs=5000, float param1=0.0f, float param2=0.0f, float param3=0.0f, float param4=0.0f, float param5=0.0f)
WeakLinkInterfacePtr primaryLink() const
VehicleLinkManager * vehicleLinkManager()
Definition Vehicle.h:575
void requestMessage(RequestMessageResultHandler resultHandler, void *resultHandlerData, int compId, int messageId, float param1=0.0f, float param2=0.0f, float param3=0.0f, float param4=0.0f, float param5=0.0f)
Definition Vehicle.cc:2252
Base class for states that wait for something with optional timeout.
virtual void onWaitTimeout()
Called when the timeout expires - default emits timeout(), subclasses can override.
virtual void onWaitEntered()
Called when the state is entered - subclasses should call base implementation.
RequestMessageResultHandlerFailureCode_t
@ RequestMessageFailureMessageNotReceived
@ RequestMessageFailureCommandNotAcked
@ RequestMessageFailureDuplicate
Exact duplicate request already active or queued for this component/message id.
@ RequestMessageFailureCommandError
@ RequestMessageNoFailure