• 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

30.43
/src/mavsdk/core/mavlink_request_message_handler.cpp
1
#include "mavlink_request_message_handler.h"
2
#include "mavsdk_impl.h"
3
#include "server_component_impl.h"
4
#include "mavlink_command_receiver.h"
5
#include "log.h"
6

7
namespace mavsdk {
8

9
MavlinkRequestMessageHandler::MavlinkRequestMessageHandler(
20✔
10
    MavsdkImpl& mavsdk_impl,
11
    ServerComponentImpl& server_component_impl,
12
    MavlinkCommandReceiver& mavlink_command_receiver) :
20✔
13
    _mavsdk_impl(mavsdk_impl),
14
    _server_component_impl(server_component_impl),
15
    _mavlink_command_receiver(mavlink_command_receiver)
20✔
16
{
17
    _mavlink_command_receiver.register_mavlink_command_handler(
20✔
18
        MAV_CMD_REQUEST_MESSAGE,
19
        [this](const MavlinkCommandReceiver::CommandLong& command) {
×
20
            return handle_command_long(command);
×
21
        },
22
        this);
23
}
20✔
24

25
MavlinkRequestMessageHandler::~MavlinkRequestMessageHandler()
20✔
26
{
27
    _mavsdk_impl.mavlink_message_handler.unregister_all(this);
20✔
28
}
20✔
29

30
bool MavlinkRequestMessageHandler::register_handler(
20✔
31
    uint32_t message_id, const Callback& callback, const void* cookie)
32
{
33
    std::lock_guard<std::mutex> lock(_table_mutex);
40✔
34

35
    if (std::find_if(_table.begin(), _table.end(), [message_id](const Entry& entry) {
40✔
36
            return entry.message_id == message_id;
×
37
        }) != _table.end()) {
40✔
38
        LogErr() << "message id " << message_id << " already registered, registration ignored";
×
39
        return false;
×
40
    }
41

42
    _table.emplace_back(Entry{message_id, callback, cookie});
20✔
43
    return true;
20✔
44
}
45

46
void MavlinkRequestMessageHandler::unregister_handler(uint32_t message_id, const void* cookie)
×
47
{
48
    std::lock_guard<std::mutex> lock(_table_mutex);
×
49

50
    _table.erase(
×
51
        std::remove_if(
×
52
            _table.begin(),
53
            _table.end(),
54
            [&message_id, &cookie](const Entry& entry) {
×
55
                return entry.message_id == message_id && entry.cookie == cookie;
×
56
            }),
×
57
        _table.end());
×
58
}
×
59

60
void MavlinkRequestMessageHandler::unregister_all_handlers(const void* cookie)
20✔
61
{
62
    std::lock_guard<std::mutex> lock(_table_mutex);
40✔
63

64
    _table.erase(
20✔
65
        std::remove_if(
20✔
66
            _table.begin(),
67
            _table.end(),
68
            [&cookie](const Entry& entry) { return entry.cookie == cookie; }),
40✔
69
        _table.end());
80✔
70
}
20✔
71

72
std::optional<mavlink_command_ack_t> MavlinkRequestMessageHandler::handle_command_long(
×
73
    const MavlinkCommandReceiver::CommandLong& command)
74
{
75
    std::lock_guard<std::mutex> lock(_table_mutex);
×
76

77
    for (auto& entry : _table) {
×
78
        if (entry.message_id == static_cast<uint32_t>(std::round(command.params.param1))) {
×
79
            if (entry.callback != nullptr) {
×
80
                const auto result = entry.callback(
×
81
                    command.origin_system_id,
×
82
                    command.origin_component_id,
×
83
                    {command.params.param2,
×
84
                     command.params.param3,
×
85
                     command.params.param4,
×
86
                     command.params.param5,
×
87
                     command.params.param6});
×
88

89
                if (result.has_value()) {
×
90
                    return _server_component_impl.make_command_ack_message(command, result.value());
×
91
                }
92
            }
93
            return {};
×
94
        }
95
    }
96

97
    // We could respond with MAV_RESULT_UNSUPPORTED here, however, it's not clear if maybe someone
98
    // else might be answering the command.
99

100
    return {};
×
101
}
102

103
std::optional<mavlink_command_ack_t>
104
MavlinkRequestMessageHandler::handle_command_int(const MavlinkCommandReceiver::CommandInt& command)
×
105
{
106
    std::lock_guard<std::mutex> lock(_table_mutex);
×
107

108
    for (auto& entry : _table) {
×
109
        if (entry.message_id == static_cast<uint32_t>(std::round(command.params.param1))) {
×
110
            if (entry.callback != nullptr) {
×
111
                const auto result = entry.callback(
×
112
                    command.origin_system_id,
×
113
                    command.origin_component_id,
×
114
                    {command.params.param2,
×
115
                     command.params.param3,
×
116
                     command.params.param4,
×
117
                     static_cast<float>(command.params.x),
×
118
                     static_cast<float>(command.params.y)});
×
119

120
                if (result.has_value()) {
×
121
                    return _server_component_impl.make_command_ack_message(command, result.value());
×
122
                }
123
            }
124
            return {};
×
125
        }
126
    }
127

128
    return {};
×
129
}
130

131
} // 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

© 2025 Coveralls, Inc