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

mavlink / MAVSDK / 20738098150

06 Jan 2026 01:54AM UTC coverage: 48.071% (+0.02%) from 48.056%
20738098150

push

github

web-flow
Merge pull request #2748 from mavlink/pr-log-annoyance

Clean up debug messages on discovery a bit

8 of 11 new or added lines in 2 files covered. (72.73%)

4 existing lines in 3 files now uncovered.

17743 of 36910 relevant lines covered (48.07%)

464.68 hits per line

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

76.7
/src/mavsdk/core/mavlink_message_handler.cpp
1
#include <algorithm>
2
#include <mutex>
3
#include "mavlink_message_handler.h"
4
#include "log.h"
5

6
namespace mavsdk {
7

8
MavlinkMessageHandler::MavlinkMessageHandler()
410✔
9
{
10
    if (const char* env_p = std::getenv("MAVSDK_MESSAGE_HANDLER_DEBUGGING")) {
410✔
11
        if (std::string(env_p) == "1") {
×
12
            LogDebug() << "Mavlink message handler debugging is on.";
×
13
            _debugging = true;
×
14
        }
15
    }
16
}
410✔
17

18
void MavlinkMessageHandler::register_one(
2,814✔
19
    uint16_t msg_id, const Callback& callback, const void* cookie)
20
{
21
    register_one_impl(msg_id, {}, callback, cookie);
2,814✔
22
}
2,817✔
23

24
void MavlinkMessageHandler::register_one_with_component_id(
×
25
    uint16_t msg_id, uint8_t component_id, const Callback& callback, const void* cookie)
26
{
27
    register_one_impl(msg_id, {component_id}, callback, cookie);
×
28
}
×
29

30
void MavlinkMessageHandler::register_one_impl(
2,814✔
31
    uint16_t msg_id,
32
    std::optional<uint8_t> maybe_component_id,
33
    const Callback& callback,
34
    const void* cookie)
35
{
36
    Entry entry = {msg_id, maybe_component_id, callback, cookie};
2,814✔
37

38
    std::unique_lock<std::mutex> lock(_mutex, std::try_to_lock);
2,816✔
39
    if (lock.owns_lock()) {
2,818✔
40
        _table.push_back(entry);
2,742✔
41
    } else {
42
        std::lock_guard<std::mutex> register_later_lock(_register_later_mutex);
74✔
43
        _register_later_table.push_back(entry);
74✔
44
    }
73✔
45
}
2,818✔
46

47
void MavlinkMessageHandler::unregister_one(uint16_t msg_id, const void* cookie)
28✔
48
{
49
    unregister_impl({msg_id}, cookie);
28✔
50
}
28✔
51

52
void MavlinkMessageHandler::unregister_all(const void* cookie)
1,271✔
53
{
54
    unregister_impl({}, cookie);
1,271✔
55
}
1,271✔
56

57
void MavlinkMessageHandler::unregister_all_blocking(const void* cookie)
160✔
58
{
59
    // Blocking version for use in destructors - waits for any in-flight callbacks to complete.
60
    // WARNING: Do NOT call this from within a message handler callback - it will deadlock.
61
    std::lock_guard<std::mutex> lock(_mutex);
160✔
62
    _table.erase(
320✔
63
        std::remove_if(
160✔
64
            _table.begin(), _table.end(), [&](auto& entry) { return entry.cookie == cookie; }),
1,339✔
65
        _table.end());
160✔
66
}
160✔
67

68
void MavlinkMessageHandler::unregister_impl(
1,299✔
69
    std::optional<uint16_t> maybe_msg_id, const void* cookie)
70
{
71
    std::unique_lock<std::mutex> lock(_mutex, std::try_to_lock);
1,299✔
72
    if (lock.owns_lock()) {
1,299✔
73
        _table.erase(
2,590✔
74
            std::remove_if(
1,295✔
75
                _table.begin(),
76
                _table.end(),
77
                [&](auto& entry) {
6,103✔
78
                    if (maybe_msg_id) {
6,103✔
79
                        return (entry.msg_id == maybe_msg_id.value() && entry.cookie == cookie);
466✔
80
                    } else {
81
                        return (entry.cookie == cookie);
5,637✔
82
                    }
83
                }),
84
            _table.end());
2,590✔
85
    } else {
86
        std::lock_guard<std::mutex> unregister_later_lock(_unregister_later_mutex);
4✔
87
        _unregister_later_table.push_back(UnregisterEntry{maybe_msg_id, cookie});
4✔
88
    }
4✔
89
}
1,299✔
90

91
void MavlinkMessageHandler::check_register_later()
5,027✔
92
{
93
    std::lock_guard<std::mutex> _register_later_lock(_register_later_mutex);
5,027✔
94

95
    // We could probably just grab the lock here, but it's safer not to
96
    // acquire both locks to avoid deadlocks.
97
    std::unique_lock<std::mutex> lock(_mutex, std::try_to_lock);
5,042✔
98
    if (!lock.owns_lock()) {
5,040✔
99
        // Try again later.
UNCOV
100
        return;
×
101
    }
102

103
    for (const auto& entry : _register_later_table) {
5,109✔
104
        _table.push_back(entry);
74✔
105
    }
106

107
    _register_later_table.clear();
5,034✔
108
}
5,033✔
109

110
void MavlinkMessageHandler::check_unregister_later()
5,033✔
111
{
112
    std::lock_guard<std::mutex> _unregister_later_lock(_unregister_later_mutex);
5,033✔
113

114
    // We could probably just grab the lock here, but it's safer not to
115
    // acquire both locks to avoid deadlocks.
116
    std::unique_lock<std::mutex> lock(_mutex, std::try_to_lock);
5,040✔
117
    if (!lock.owns_lock()) {
5,042✔
118
        // Try again later.
UNCOV
119
        return;
×
120
    }
121

122
    for (const auto& unregister_entry : _unregister_later_table) {
5,046✔
123
        _table.erase(
8✔
124
            std::remove_if(
4✔
125
                _table.begin(),
126
                _table.end(),
127
                [&](auto& entry) {
32✔
128
                    if (unregister_entry.maybe_msg_id) {
32✔
129
                        return (
130
                            entry.msg_id == unregister_entry.maybe_msg_id.value() &&
44✔
131
                            entry.cookie == unregister_entry.cookie);
44✔
132
                    } else {
133
                        return (entry.cookie == unregister_entry.cookie);
×
134
                    }
135
                }),
136
            _table.end());
8✔
137
    }
138

139
    _unregister_later_table.clear();
5,035✔
140
}
5,032✔
141

142
void MavlinkMessageHandler::process_message(const mavlink_message_t& message)
5,029✔
143
{
144
    check_register_later();
5,029✔
145
    check_unregister_later();
5,042✔
146

147
    std::lock_guard<std::mutex> lock(_mutex);
5,041✔
148

149
    bool forwarded = false;
5,041✔
150

151
    if (_debugging) {
5,041✔
152
        LogDebug() << "Table entries: ";
×
153
    }
154

155
    for (auto& entry : _table) {
47,629✔
156
        if (_debugging) {
42,589✔
157
            LogDebug() << "Msg id: " << entry.msg_id << ", component id: "
×
158
                       << (entry.component_id.has_value() ?
×
159
                               std::to_string(entry.component_id.value()) :
×
160
                               "none");
×
161
        }
162

163
        if (entry.msg_id == message.msgid &&
46,558✔
164
            (!entry.component_id.has_value() || entry.component_id.value() == message.compid)) {
3,969✔
165
            if (_debugging) {
3,966✔
166
                LogDebug() << "Using msg " << int(message.msgid) << " to " << size_t(entry.cookie);
×
167
            }
168

169
            forwarded = true;
3,966✔
170
            entry.callback(message);
3,966✔
171
        }
172
    }
173

174
    if (_debugging && !forwarded) {
5,034✔
175
        LogDebug() << "Ignoring msg " << int(message.msgid);
×
176
    }
177
}
5,034✔
178

179
void MavlinkMessageHandler::update_component_id(
×
180
    uint16_t msg_id, uint8_t component_id, const void* cookie)
181
{
182
    check_register_later();
×
183
    check_unregister_later();
×
184

185
    std::lock_guard<std::mutex> lock(_mutex);
×
186

187
    for (auto& entry : _table) {
×
188
        if (entry.msg_id == msg_id && entry.cookie == cookie) {
×
189
            entry.component_id = component_id;
×
190
        }
191
    }
192
}
×
193

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