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

mavlink / MAVSDK / 21701918810

05 Feb 2026 06:53AM UTC coverage: 49.049% (+0.09%) from 48.959%
21701918810

Pull #2762

github

web-flow
Merge 0c0966514 into b84eae3a5
Pull Request #2762: core: fix MAVLink message sequence

51 of 58 new or added lines in 6 files covered. (87.93%)

9 existing lines in 5 files now uncovered.

18369 of 37450 relevant lines covered (49.05%)

668.53 hits per line

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

0.0
/src/mavsdk/plugins/events/event_handler.cpp
1

2
#include "event_handler.h"
3

4
#include <utility>
5

6
namespace mavsdk {
7

8
EventHandler::EventHandler(
×
9
    const std::string& profile,
10
    handle_event_f handle_event_cb,
11
    health_and_arming_checks_updated_f health_and_arming_checks_updated_cb,
12
    SystemImpl& system_impl,
13
    uint8_t system_id,
14
    uint8_t component_id) :
×
15
    _handle_event_cb(std::move(handle_event_cb)),
×
16
    _health_and_arming_checks_updated_cb(std::move(health_and_arming_checks_updated_cb)),
×
17
    _compid(component_id),
×
18
    _system_impl(system_impl)
×
19
{
20
    auto error_cb = [component_id, this](int num_events_lost) {
×
21
        _health_and_arming_checks.reset();
×
22
        LogWarn() << "Events got lost:" << num_events_lost << "comp_id:" << component_id;
×
23
    };
×
24

25
    const auto timeout_cb = [this](int timeout_ms) {
×
26
        if (_timer_cookie != 0) {
×
27
            _system_impl.unregister_timeout_handler(_timer_cookie);
×
28
            _timer_cookie = 0;
×
29
        }
30
        if (timeout_ms > 0) {
×
31
            _timer_cookie = _system_impl.register_timeout_handler(
×
32
                [this]() {
×
33
                    const std::lock_guard lg{_protocol_mutex};
×
34
                    _protocol->timerEvent();
×
35
                },
×
36
                timeout_ms / 1000.0);
37
        }
38
    };
×
39

40
    const auto send_request_cb = [this](const mavlink_request_event_t& msg) {
×
NEW
41
        _system_impl.queue_message([&](MavlinkAddress mavlink_address, uint8_t channel) {
×
42
            mavlink_message_t message;
NEW
43
            mavlink_msg_request_event_encode_chan(
×
NEW
44
                mavlink_address.system_id,
×
NEW
45
                mavlink_address.component_id,
×
46
                channel,
47
                &message,
NEW
48
                &msg);
×
NEW
49
            return message;
×
50
        });
UNCOV
51
    };
×
52

53
    _parser.setProfile(profile);
×
54

55
    _parser.formatters().url = [](const std::string& /*content*/, const std::string& link) {
×
56
        return link;
×
57
    };
×
58

59
    events::ReceiveProtocol::Callbacks callbacks{
60
        error_cb,
61
        send_request_cb,
62
        std::bind(&EventHandler::got_event, this, std::placeholders::_1),
×
63
        timeout_cb};
×
64
    _protocol = std::make_unique<events::ReceiveProtocol>(
×
65
        callbacks,
66
        _system_impl.get_own_system_id(),
×
67
        _system_impl.get_own_component_id(),
×
68
        system_id,
69
        component_id);
×
70

71
    _system_impl.register_mavlink_message_handler_with_compid(
×
72
        MAVLINK_MSG_ID_EVENT,
73
        _compid,
×
74
        std::bind(&EventHandler::handle_mavlink_message, this, std::placeholders::_1),
×
75
        &_message_handler_cookies[0]);
×
76
    _system_impl.register_mavlink_message_handler_with_compid(
×
77
        MAVLINK_MSG_ID_CURRENT_EVENT_SEQUENCE,
78
        _compid,
×
79
        std::bind(&EventHandler::handle_mavlink_message, this, std::placeholders::_1),
×
80
        &_message_handler_cookies[1]);
×
81
    _system_impl.register_mavlink_message_handler_with_compid(
×
82
        MAVLINK_MSG_ID_RESPONSE_EVENT_ERROR,
83
        _compid,
×
84
        std::bind(&EventHandler::handle_mavlink_message, this, std::placeholders::_1),
×
85
        &_message_handler_cookies[2]);
×
86
}
×
87
EventHandler::~EventHandler()
×
88
{
89
    if (_timer_cookie != 0) {
×
90
        _system_impl.unregister_timeout_handler(_timer_cookie);
×
91
    }
92
    // Use blocking version to ensure any in-flight callbacks complete before destruction.
93
    for (size_t i = 0; i < _message_handler_cookies.size(); ++i) {
×
94
        _system_impl.unregister_all_mavlink_message_handlers_blocking(&_message_handler_cookies[i]);
×
95
    }
96
}
×
97
void EventHandler::set_metadata(const std::string& metadata_json)
×
98
{
99
    if (_parser.loadDefinitions(metadata_json)) {
×
100
        if (_parser.hasDefinitions()) {
×
101
            // do we have queued events?
102
            for (const auto& event : _pending_events) {
×
103
                got_event(event);
×
104
            }
105
            _pending_events.clear();
×
106
        }
107
    } else {
108
        LogErr() << "Failed to load events JSON metadata file";
×
109
    }
110
}
×
111

112
std::optional<int> EventHandler::get_mode_group(uint32_t custom_mode) const
×
113
{
114
    events::parser::Parser::NavigationModeGroups groups = _parser.navigationModeGroups(_compid);
×
115
    for (auto group_iter : groups.groups) {
×
116
        if (group_iter.second.find(custom_mode) != group_iter.second.end()) {
×
117
            return group_iter.first;
×
118
        }
119
    }
×
120
    return std::nullopt;
×
121
}
×
122
void EventHandler::handle_mavlink_message(const mavlink_message_t& message)
×
123
{
124
    const std::lock_guard lg{_protocol_mutex};
×
125
    _protocol->processMessage(message);
×
126
}
×
127
void EventHandler::got_event(const mavlink_event_t& event)
×
128
{
129
    if (!_parser.hasDefinitions()) {
×
130
        if (_pending_events.size() > 50) { // Limit size (not expected to hit that limit)
×
131
            _pending_events.clear();
×
132
        }
133
        if (_pending_events.empty()) { // Print only for the first to avoid spamming
×
134
            LogDebug() << "No metadata, queuing event, ID: " << event.id
×
135
                       << ", num pending: " << _pending_events.size();
×
136
        }
137
        _pending_events.push_back(event);
×
138
        return;
×
139
    }
140

141
    std::unique_ptr<events::parser::ParsedEvent> parsed_event =
142
        _parser.parse(events::EventType(event));
×
143
    if (parsed_event == nullptr) {
×
144
        LogWarn() << "Got Event without known metadata: ID:" << event.id << "comp id:" << _compid;
×
145
        return;
×
146
    }
147

148
    //    LogDebug() << "Got Event: ID: " << parsed_event->id() << " namespace: " <<
149
    //    parsed_event->eventNamespace().c_str() <<
150
    //        " name: " << parsed_event->name().c_str() << " msg: " <<
151
    //        parsed_event->message().c_str();
152

153
    if (_health_and_arming_checks.handleEvent(*parsed_event)) {
×
154
        _health_and_arming_checks_valid = true;
×
155
        _health_and_arming_checks_updated_cb();
×
156
    }
157
    _handle_event_cb(std::move(parsed_event));
×
158
}
×
159
} // 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