Skip to content

Commit

Permalink
Merge pull request #741 from Dronecode/fix-passthrough-unsub
Browse files Browse the repository at this point in the history
Fix MAVLink Passthrough unsubscription
  • Loading branch information
julianoes committed May 8, 2019
2 parents b706135 + 9e0bec3 commit 03d2698
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
15 changes: 15 additions & 0 deletions core/system_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,21 @@ void SystemImpl::register_mavlink_message_handler(uint16_t msg_id,
_iterator_invalidated = true;
}

void SystemImpl::unregister_mavlink_message_handler(uint16_t msg_id, const void *cookie)
{
std::lock_guard<std::mutex> lock(_mavlink_handler_table_mutex);

for (auto it = _mavlink_handler_table.begin(); it != _mavlink_handler_table.end();
/* no ++it */) {
if (it->msg_id == msg_id && it->cookie == cookie) {
it = _mavlink_handler_table.erase(it);
_iterator_invalidated = true;
} else {
++it;
}
}
}

void SystemImpl::unregister_all_mavlink_message_handlers(const void *cookie)
{
std::lock_guard<std::mutex> lock(_mavlink_handler_table_mutex);
Expand Down
1 change: 1 addition & 0 deletions core/system_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class SystemImpl {
mavlink_message_handler_t callback,
const void *cookie);

void unregister_mavlink_message_handler(uint16_t msg_id, const void *cookie);
void unregister_all_mavlink_message_handlers(const void *cookie);

void register_timeout_handler(std::function<void()> callback, double duration_s, void **cookie);
Expand Down
16 changes: 13 additions & 3 deletions integration_tests/mavlink_passthrough.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,25 @@ TEST_F(SitlTest, MavlinkPassthrough)
std::promise<void> prom;
std::future<void> fut = prom.get_future();
unsigned counter = 0;
bool stopped = false;

mavlink_passthrough->subscribe_message_async(
MAVLINK_MSG_ID_HIGHRES_IMU, [&prom, &counter](const mavlink_message_t &message) {
MAVLINK_MSG_ID_HIGHRES_IMU,
[&prom, &counter, &stopped, mavlink_passthrough](const mavlink_message_t &message) {
mavlink_highres_imu_t highres_imu;
mavlink_msg_highres_imu_decode(&message, &highres_imu);

LogInfo() << "HIGHRES_IMU.temperature: " << highres_imu.temperature << " degrees C";
LogInfo() << "HIGHRES_IMU.temperature [1] (" << counter << ")"
<< highres_imu.temperature << " degrees C";
if (++counter > 100) {
prom.set_value();
EXPECT_FALSE(stopped);
if (!stopped) {
stopped = true;
// Unsubscribe again
mavlink_passthrough->subscribe_message_async(MAVLINK_MSG_ID_HIGHRES_IMU,
nullptr);
prom.set_value();
}
};
});

Expand Down
12 changes: 11 additions & 1 deletion plugins/mavlink_passthrough/mavlink_passthrough_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,17 @@ MavlinkPassthrough::Result MavlinkPassthroughImpl::send_message(mavlink_message_
void MavlinkPassthroughImpl::subscribe_message_async(
uint16_t message_id, std::function<void(const mavlink_message_t &)> callback)
{
_parent->register_mavlink_message_handler(message_id, callback, this);
if (callback == nullptr) {
_parent->unregister_mavlink_message_handler(message_id, this);
} else {
auto temp_callback = callback;
_parent->register_mavlink_message_handler(
message_id,
[this, temp_callback](const mavlink_message_t &message) {
_parent->call_user_callback([temp_callback, message]() { temp_callback(message); });
},
this);
}
}

uint8_t MavlinkPassthroughImpl::get_our_sysid() const
Expand Down

0 comments on commit 03d2698

Please sign in to comment.