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

mavlink / MAVSDK / 11588874603

30 Oct 2024 07:37AM UTC coverage: 38.621% (+0.7%) from 37.918%
11588874603

Pull #2394

github

web-flow
Merge 991248b3a into cebb708a4
Pull Request #2394: Consolidate CI

12034 of 31159 relevant lines covered (38.62%)

243.25 hits per line

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

58.9
/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
#include <algorithm>
7

8
namespace mavsdk {
9

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

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

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

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

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

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

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

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

65
    _table.erase(
292✔
66
        std::remove_if(
146✔
67
            _table.begin(),
68
            _table.end(),
69
            [&cookie](const Entry& entry) { return entry.cookie == cookie; }),
74✔
70
        _table.end());
146✔
71
}
73✔
72

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

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

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

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

101
    return {};
×
102
}
34✔
103

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

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

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

129
    return {};
×
130
}
×
131

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