-
-
Notifications
You must be signed in to change notification settings - Fork 637
Expand file tree
/
Copy pathmavlink_message_handler.cpp
More file actions
210 lines (178 loc) · 6.54 KB
/
Copy pathmavlink_message_handler.cpp
File metadata and controls
210 lines (178 loc) · 6.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include <algorithm>
#include <mutex>
#include "mavlink_message_handler.h"
#include "log.h"
namespace mavsdk {
MavlinkMessageHandler::MavlinkMessageHandler()
{
if (const char* env_p = std::getenv("MAVSDK_MESSAGE_HANDLER_DEBUGGING")) {
if (std::string(env_p) == "1") {
LogDebug() << "Mavlink message handler debugging is on.";
_debugging = true;
}
}
}
void MavlinkMessageHandler::register_one(
uint16_t msg_id, const Callback& callback, const void* cookie)
{
register_one_impl(msg_id, {}, callback, cookie);
}
void MavlinkMessageHandler::register_one_with_component_id(
uint16_t msg_id, uint8_t component_id, const Callback& callback, const void* cookie)
{
register_one_impl(msg_id, {component_id}, callback, cookie);
}
void MavlinkMessageHandler::register_one_impl(
uint16_t msg_id,
std::optional<uint8_t> maybe_component_id,
const Callback& callback,
const void* cookie)
{
Entry entry = {msg_id, maybe_component_id, callback, cookie};
std::unique_lock<std::mutex> lock(_mutex, std::try_to_lock);
if (lock.owns_lock()) {
_table.push_back(entry);
} else {
std::lock_guard<std::mutex> register_later_lock(_register_later_mutex);
_register_later_table.push_back(entry);
}
}
void MavlinkMessageHandler::unregister_one(uint16_t msg_id, const void* cookie)
{
unregister_impl({msg_id}, cookie);
}
void MavlinkMessageHandler::unregister_all(const void* cookie)
{
unregister_impl({}, cookie);
}
void MavlinkMessageHandler::unregister_all_blocking(const void* cookie)
{
// Blocking version for use in destructors - waits for any in-flight callbacks to complete.
// WARNING: Do NOT call this from within a message handler callback - it will deadlock.
// Also purge any deferred registrations for this cookie from _register_later_table.
// If register_one() was called while process_message() held _mutex, the entries land in
// _register_later_table instead of _table. Without this step, check_register_later()
// would later promote those stale entries into _table and fire callbacks on a
// destroyed object (heap-use-after-free).
{
std::lock_guard<std::mutex> register_later_lock(_register_later_mutex);
_register_later_table.erase(
std::remove_if(
_register_later_table.begin(),
_register_later_table.end(),
[&](const auto& entry) { return entry.cookie == cookie; }),
_register_later_table.end());
}
std::lock_guard<std::mutex> lock(_mutex);
_table.erase(
std::remove_if(
_table.begin(), _table.end(), [&](auto& entry) { return entry.cookie == cookie; }),
_table.end());
}
void MavlinkMessageHandler::unregister_impl(
std::optional<uint16_t> maybe_msg_id, const void* cookie)
{
std::unique_lock<std::mutex> lock(_mutex, std::try_to_lock);
if (lock.owns_lock()) {
_table.erase(
std::remove_if(
_table.begin(),
_table.end(),
[&](auto& entry) {
if (maybe_msg_id) {
return (entry.msg_id == maybe_msg_id.value() && entry.cookie == cookie);
} else {
return (entry.cookie == cookie);
}
}),
_table.end());
} else {
std::lock_guard<std::mutex> unregister_later_lock(_unregister_later_mutex);
_unregister_later_table.push_back(UnregisterEntry{maybe_msg_id, cookie});
}
}
void MavlinkMessageHandler::check_register_later()
{
std::lock_guard<std::mutex> _register_later_lock(_register_later_mutex);
// We could probably just grab the lock here, but it's safer not to
// acquire both locks to avoid deadlocks.
std::unique_lock<std::mutex> lock(_mutex, std::try_to_lock);
if (!lock.owns_lock()) {
// Try again later.
return;
}
for (const auto& entry : _register_later_table) {
_table.push_back(entry);
}
_register_later_table.clear();
}
void MavlinkMessageHandler::check_unregister_later()
{
std::lock_guard<std::mutex> _unregister_later_lock(_unregister_later_mutex);
// We could probably just grab the lock here, but it's safer not to
// acquire both locks to avoid deadlocks.
std::unique_lock<std::mutex> lock(_mutex, std::try_to_lock);
if (!lock.owns_lock()) {
// Try again later.
return;
}
for (const auto& unregister_entry : _unregister_later_table) {
_table.erase(
std::remove_if(
_table.begin(),
_table.end(),
[&](auto& entry) {
if (unregister_entry.maybe_msg_id) {
return (
entry.msg_id == unregister_entry.maybe_msg_id.value() &&
entry.cookie == unregister_entry.cookie);
} else {
return (entry.cookie == unregister_entry.cookie);
}
}),
_table.end());
}
_unregister_later_table.clear();
}
void MavlinkMessageHandler::process_message(const mavlink_message_t& message)
{
check_register_later();
check_unregister_later();
std::lock_guard<std::mutex> lock(_mutex);
bool forwarded = false;
if (_debugging) {
LogDebug() << "Table entries: ";
}
for (auto& entry : _table) {
if (_debugging) {
LogDebug() << "Msg id: " << entry.msg_id << ", component id: "
<< (entry.component_id.has_value() ?
std::to_string(entry.component_id.value()) :
"none");
}
if (entry.msg_id == message.msgid &&
(!entry.component_id.has_value() || entry.component_id.value() == message.compid)) {
if (_debugging) {
LogDebug() << "Using msg " << int(message.msgid) << " to " << size_t(entry.cookie);
}
forwarded = true;
entry.callback(message);
}
}
if (_debugging && !forwarded) {
LogDebug() << "Ignoring msg " << int(message.msgid);
}
}
void MavlinkMessageHandler::update_component_id(
uint16_t msg_id, uint8_t component_id, const void* cookie)
{
check_register_later();
check_unregister_later();
std::lock_guard<std::mutex> lock(_mutex);
for (auto& entry : _table) {
if (entry.msg_id == msg_id && entry.cookie == cookie) {
entry.component_id = component_id;
}
}
}
} // namespace mavsdk