• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mavlink / MAVSDK / 6568658631

19 Oct 2023 01:31AM UTC coverage: 31.23% (+0.02%) from 31.215%
6568658631

push

github

web-flow
Merge pull request #2155 from mavlink/pr-static-fixes

Threading fixes, MAVLink sequence number cleanup

1386 of 1386 new or added lines in 46 files covered. (100.0%)

7906 of 25315 relevant lines covered (31.23%)

23.54 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

62.34
/src/mavsdk/core/mavlink_command_receiver.cpp
1
#include "mavlink_command_receiver.h"
2
#include "mavsdk_impl.h"
3
#include "log.h"
4
#include "server_component_impl.h"
5
#include <cmath>
6
#include <future>
7
#include <memory>
8

9
namespace mavsdk {
10

11
MavlinkCommandReceiver::MavlinkCommandReceiver(ServerComponentImpl& server_component_impl) :
20✔
12
    _server_component_impl(server_component_impl)
20✔
13
{
14
    _server_component_impl.register_mavlink_message_handler(
20✔
15
        MAVLINK_MSG_ID_COMMAND_LONG,
16
        [this](const mavlink_message_t& message) { receive_command_long(message); },
15✔
17
        this);
18

19
    _server_component_impl.register_mavlink_message_handler(
20✔
20
        MAVLINK_MSG_ID_COMMAND_INT,
21
        [this](const mavlink_message_t& message) { receive_command_int(message); },
×
22
        this);
23
}
20✔
24

25
MavlinkCommandReceiver::~MavlinkCommandReceiver()
20✔
26
{
27
    _server_component_impl.unregister_all_mavlink_command_handlers(this);
20✔
28
    _server_component_impl.unregister_all_mavlink_message_handlers(this);
20✔
29
}
20✔
30

31
void MavlinkCommandReceiver::receive_command_int(const mavlink_message_t& message)
×
32
{
33
    MavlinkCommandReceiver::CommandInt cmd(message);
×
34

35
    std::lock_guard<std::mutex> lock(_mavlink_command_handler_table_mutex);
×
36

37
    for (auto& handler : _mavlink_command_int_handler_table) {
×
38
        if (handler.cmd_id == cmd.command) {
×
39
            // The client side can pack a COMMAND_ACK as a response to receiving the command.
40
            auto maybe_command_ack = handler.callback(cmd);
×
41
            if (maybe_command_ack) {
×
42
                _server_component_impl.queue_message(
×
43
                    [&, this](MavlinkAddress mavlink_address, uint8_t channel) {
×
44
                        mavlink_message_t response_message;
45
                        mavlink_msg_command_ack_encode_chan(
×
46
                            mavlink_address.system_id,
×
47
                            mavlink_address.component_id,
×
48
                            channel,
49
                            &response_message,
50
                            &maybe_command_ack.value());
×
51
                        return response_message;
×
52
                    });
53
            }
54
        }
55
    }
56
}
×
57

58
void MavlinkCommandReceiver::receive_command_long(const mavlink_message_t& message)
15✔
59
{
60
    MavlinkCommandReceiver::CommandLong cmd(message);
15✔
61

62
    std::lock_guard<std::mutex> lock(_mavlink_command_handler_table_mutex);
30✔
63

64
    for (auto& handler : _mavlink_command_long_handler_table) {
63✔
65
        if (handler.cmd_id == cmd.command) {
48✔
66
            // The client side can pack a COMMAND_ACK as a response to receiving the command.
67
            auto maybe_command_ack = handler.callback(cmd);
14✔
68
            if (maybe_command_ack) {
14✔
69
                _server_component_impl.queue_message(
14✔
70
                    [&, this](MavlinkAddress mavlink_address, uint8_t channel) {
14✔
71
                        mavlink_message_t response_message;
72
                        mavlink_msg_command_ack_encode_chan(
14✔
73
                            mavlink_address.system_id,
14✔
74
                            mavlink_address.component_id,
14✔
75
                            channel,
76
                            &response_message,
77
                            &maybe_command_ack.value());
14✔
78
                        return response_message;
14✔
79
                    });
80
            }
81
        }
82
    }
83
}
15✔
84

85
void MavlinkCommandReceiver::register_mavlink_command_handler(
20✔
86
    uint16_t cmd_id, const MavlinkCommandIntHandler& callback, const void* cookie)
87
{
88
    std::lock_guard<std::mutex> lock(_mavlink_command_handler_table_mutex);
40✔
89

90
    MAVLinkCommandIntHandlerTableEntry entry = {cmd_id, callback, cookie};
40✔
91
    _mavlink_command_int_handler_table.push_back(entry);
20✔
92
}
20✔
93

94
void MavlinkCommandReceiver::register_mavlink_command_handler(
63✔
95
    uint16_t cmd_id, const MavlinkCommandLongHandler& callback, const void* cookie)
96
{
97
    std::lock_guard<std::mutex> lock(_mavlink_command_handler_table_mutex);
126✔
98

99
    MAVLinkCommandLongHandlerTableEntry entry = {cmd_id, callback, cookie};
126✔
100
    _mavlink_command_long_handler_table.push_back(entry);
63✔
101
}
63✔
102

103
void MavlinkCommandReceiver::unregister_mavlink_command_handler(uint16_t cmd_id, const void* cookie)
×
104
{
105
    std::lock_guard<std::mutex> lock(_mavlink_command_handler_table_mutex);
×
106

107
    // COMMAND_INT
108
    for (auto it = _mavlink_command_int_handler_table.begin();
×
109
         it != _mavlink_command_int_handler_table.end();
×
110
         /* no ++it */) {
111
        if (it->cmd_id == cmd_id && it->cookie == cookie) {
×
112
            it = _mavlink_command_int_handler_table.erase(it);
×
113
        } else {
114
            ++it;
×
115
        }
116
    }
117

118
    // COMMAND_LONG
119
    for (auto it = _mavlink_command_long_handler_table.begin();
×
120
         it != _mavlink_command_long_handler_table.end();
×
121
         /* no ++it */) {
122
        if (it->cmd_id == cmd_id && it->cookie == cookie) {
×
123
            it = _mavlink_command_long_handler_table.erase(it);
×
124
        } else {
125
            ++it;
×
126
        }
127
    }
128
}
×
129

130
void MavlinkCommandReceiver::unregister_all_mavlink_command_handlers(const void* cookie)
42✔
131
{
132
    std::lock_guard<std::mutex> lock(_mavlink_command_handler_table_mutex);
84✔
133

134
    // COMMAND_INT
135
    for (auto it = _mavlink_command_int_handler_table.begin();
64✔
136
         it != _mavlink_command_int_handler_table.end();
64✔
137
         /* no ++it */) {
138
        if (it->cookie == cookie) {
22✔
139
            it = _mavlink_command_int_handler_table.erase(it);
20✔
140
        } else {
141
            ++it;
2✔
142
        }
143
    }
144

145
    // COMMAND_LONG
146
    for (auto it = _mavlink_command_long_handler_table.begin();
130✔
147
         it != _mavlink_command_long_handler_table.end();
130✔
148
         /* no ++it */) {
149
        if (it->cookie == cookie) {
88✔
150
            it = _mavlink_command_long_handler_table.erase(it);
42✔
151
        } else {
152
            ++it;
46✔
153
        }
154
    }
155
}
42✔
156

157
} // namespace mavsdk
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc