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

mavlink / MAVSDK / 11767930807

10 Nov 2024 07:33PM UTC coverage: 38.608% (+0.7%) from 37.921%
11767930807

push

github

web-flow
Merge pull request #2394 from mavlink/pr-consolidate-ci

Consolidate CI

12030 of 31159 relevant lines covered (38.61%)

243.33 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) {
×
41
        mavlink_message_t message;
×
42
        mavlink_msg_request_event_encode(
×
43
            _system_impl.get_own_system_id(), _system_impl.get_own_component_id(), &message, &msg);
×
44
        _system_impl.send_message(message);
×
45
    };
×
46

47
    _parser.setProfile(profile);
×
48

49
    _parser.formatters().url = [](const std::string& /*content*/, const std::string& link) {
×
50
        return link;
×
51
    };
×
52

53
    events::ReceiveProtocol::Callbacks callbacks{
×
54
        error_cb,
55
        send_request_cb,
56
        std::bind(&EventHandler::got_event, this, std::placeholders::_1),
×
57
        timeout_cb};
×
58
    _protocol = std::make_unique<events::ReceiveProtocol>(
×
59
        callbacks,
60
        _system_impl.get_own_system_id(),
×
61
        _system_impl.get_own_component_id(),
×
62
        system_id,
63
        component_id);
×
64

65
    _system_impl.register_mavlink_message_handler_with_compid(
×
66
        MAVLINK_MSG_ID_EVENT,
67
        _compid,
×
68
        std::bind(&EventHandler::handle_mavlink_message, this, std::placeholders::_1),
×
69
        &_message_handler_cookies[0]);
×
70
    _system_impl.register_mavlink_message_handler_with_compid(
×
71
        MAVLINK_MSG_ID_CURRENT_EVENT_SEQUENCE,
72
        _compid,
×
73
        std::bind(&EventHandler::handle_mavlink_message, this, std::placeholders::_1),
×
74
        &_message_handler_cookies[1]);
×
75
    _system_impl.register_mavlink_message_handler_with_compid(
×
76
        MAVLINK_MSG_ID_RESPONSE_EVENT_ERROR,
77
        _compid,
×
78
        std::bind(&EventHandler::handle_mavlink_message, this, std::placeholders::_1),
×
79
        &_message_handler_cookies[2]);
×
80
}
×
81
EventHandler::~EventHandler()
×
82
{
83
    if (_timer_cookie != 0) {
×
84
        _system_impl.unregister_timeout_handler(_timer_cookie);
×
85
    }
86
    for (const auto& cookie : _message_handler_cookies) {
×
87
        _system_impl.unregister_all_mavlink_message_handlers(cookie);
×
88
    }
89
}
×
90
void EventHandler::set_metadata(const std::string& metadata_json)
×
91
{
92
    if (_parser.loadDefinitions(metadata_json)) {
×
93
        if (_parser.hasDefinitions()) {
×
94
            // do we have queued events?
95
            for (const auto& event : _pending_events) {
×
96
                got_event(event);
×
97
            }
98
            _pending_events.clear();
×
99
        }
100
    } else {
101
        LogErr() << "Failed to load events JSON metadata file";
×
102
    }
103
}
×
104

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

134
    std::unique_ptr<events::parser::ParsedEvent> parsed_event =
×
135
        _parser.parse(events::EventType(event));
×
136
    if (parsed_event == nullptr) {
×
137
        LogWarn() << "Got Event without known metadata: ID:" << event.id << "comp id:" << _compid;
×
138
        return;
×
139
    }
140

141
    //    LogDebug() << "Got Event: ID: " << parsed_event->id() << " namespace: " <<
142
    //    parsed_event->eventNamespace().c_str() <<
143
    //        " name: " << parsed_event->name().c_str() << " msg: " <<
144
    //        parsed_event->message().c_str();
145

146
    if (_health_and_arming_checks.handleEvent(*parsed_event)) {
×
147
        _health_and_arming_checks_valid = true;
×
148
        _health_and_arming_checks_updated_cb();
×
149
    }
150
    _handle_event_cb(std::move(parsed_event));
×
151
}
×
152
} // 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