diff --git a/src/modules/mavlink/mavlink_orb_subscription.cpp b/src/modules/mavlink/mavlink_orb_subscription.cpp index 66d309a156..87cf8624d3 100644 --- a/src/modules/mavlink/mavlink_orb_subscription.cpp +++ b/src/modules/mavlink/mavlink_orb_subscription.cpp @@ -74,7 +74,7 @@ MavlinkOrbSubscription::get_instance() const } bool -MavlinkOrbSubscription::update(uint64_t *time, void* data) +MavlinkOrbSubscription::update(uint64_t *time, void *data) { // TODO this is NOT atomic operation, we can get data newer than time // if topic was published between orb_stat and orb_copy calls. @@ -106,11 +106,34 @@ MavlinkOrbSubscription::update(uint64_t *time, void* data) } bool -MavlinkOrbSubscription::update(void* data) +MavlinkOrbSubscription::update(void *data) { return !orb_copy(_topic, _fd, data); } +bool +MavlinkOrbSubscription::update_if_changed(void *data) +{ + bool updated; + if (orb_check(_fd, &updated)) { + return false; + } + + if (!updated) { + return false; + } + + if (orb_copy(_topic, _fd, data)) { + if (data != nullptr) { + /* error copying topic data */ + memset(data, 0, _topic->o_size); + } + return false; + } + + return true; +} + bool MavlinkOrbSubscription::is_published() { diff --git a/src/modules/mavlink/mavlink_orb_subscription.h b/src/modules/mavlink/mavlink_orb_subscription.h index 5394e5097b..913f19ec97 100644 --- a/src/modules/mavlink/mavlink_orb_subscription.h +++ b/src/modules/mavlink/mavlink_orb_subscription.h @@ -54,26 +54,37 @@ public: ~MavlinkOrbSubscription(); /** - * Check if subscription updated and get data. + * Check if subscription updated based on timestamp. * - * @return true only if topic was updated and data copied to buffer successfully. - * If topic was not updated since last check it will return false but still copy the data. - * If no data available data buffer will be filled with zeroes. + * @return true only if topic was updated based on a timestamp and + * copied to buffer successfully. + * If topic was not updated since last check it will return false but + * still copy the data. + * If no data available data buffer will be filled with zeros. */ - bool update(uint64_t *time, void* data); + bool update(uint64_t *time, void *data); /** * Copy topic data to given buffer. * * @return true only if topic data copied successfully. */ - bool update(void* data); + bool update(void *data); + + /** + * Check if the subscription has been updated. + * + * @return true if there has been an update which has been + * copied successfully. + */ + bool update_if_changed(void *data); /** * Check if the topic has been published. * * This call will return true if the topic was ever published. * @return true if the topic has been published at least once. + * If no data is available the buffer will be filled with zeros. */ bool is_published(); orb_id_t get_topic() const;