diff --git a/platforms/common/uORB/SubscriptionInterval.cpp b/platforms/common/uORB/SubscriptionInterval.cpp index 7d518f923ab..d7cba7b6b45 100644 --- a/platforms/common/uORB/SubscriptionInterval.cpp +++ b/platforms/common/uORB/SubscriptionInterval.cpp @@ -45,13 +45,9 @@ namespace uORB template bool SubscriptionIntervalBase::updated() { - hrt_abstime last_update = _last_update.load(); - - if (advertised() && (hrt_elapsed_time(&last_update) >= _interval_us.load())) { - return _subscription.updated(); - } - - return false; + // New data implies advertised(); checking it first keeps the clock read off + // the common no-update path. + return _subscription.updated() && interval_elapsed(); } template diff --git a/platforms/common/uORB/SubscriptionInterval.hpp b/platforms/common/uORB/SubscriptionInterval.hpp index 811352e3d7d..e2efb99cdca 100644 --- a/platforms/common/uORB/SubscriptionInterval.hpp +++ b/platforms/common/uORB/SubscriptionInterval.hpp @@ -39,6 +39,7 @@ #pragma once #include +#include #include #include @@ -172,6 +173,21 @@ public: void set_last_update(hrt_abstime t) { _last_update.store(t); } protected: + // True with no interval set, or once the interval has elapsed since the last + // update. Reads _last_update (64-bit, not lock-free on 32-bit targets) only + // when there is an interval. + bool interval_elapsed() const + { + const uint32_t interval_us = _interval_us.load(); + + if (interval_us == 0) { + return true; + } + + const hrt_abstime last_update = _last_update.load(); + return hrt_elapsed_time(&last_update) >= interval_us; + } + Subscription _subscription; subscription_interval_detail::storage_t _last_update{0}; // last subscription update in microseconds subscription_interval_detail::storage_t _interval_us{0}; // maximum update interval in us diff --git a/platforms/common/uORB/test/CMakeLists.txt b/platforms/common/uORB/test/CMakeLists.txt index 8aed8d0cbd7..cedc67829c8 100644 --- a/platforms/common/uORB/test/CMakeLists.txt +++ b/platforms/common/uORB/test/CMakeLists.txt @@ -34,3 +34,4 @@ px4_add_functional_gtest(SRC uORBMessageFieldsTest.cpp LINKLIBS uORB) px4_add_functional_gtest(SRC uORBSubscriptionTest.cpp LINKLIBS uORB) +px4_add_functional_gtest(SRC uORBSubscriptionIntervalTest.cpp LINKLIBS uORB) diff --git a/platforms/common/uORB/test/uORBSubscriptionIntervalTest.cpp b/platforms/common/uORB/test/uORBSubscriptionIntervalTest.cpp new file mode 100644 index 00000000000..0ea625c9c03 --- /dev/null +++ b/platforms/common/uORB/test/uORBSubscriptionIntervalTest.cpp @@ -0,0 +1,123 @@ +/**************************************************************************** + * + * Copyright (c) 2026 PX4 Development Team. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name PX4 nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/** + * Interval gating of SubscriptionInterval::updated() / update(). + */ + +#include + +#include +#include +#include +#include + +namespace uORB +{ +namespace test +{ + +class uORBSubscriptionIntervalTest : public ::testing::Test +{ +protected: + static void SetUpTestSuite() + { + uORB::Manager::initialize(); + } + + static void TearDownTestSuite() + { + uORB::Manager::terminate(); + } + + void publish(int32_t val) + { + orb_test_s message{}; + message.timestamp = hrt_absolute_time(); + message.val = val; + ASSERT_TRUE(_pub.publish(message)); + } + + uORB::Publication _pub{ORB_ID(orb_test)}; +}; + +TEST_F(uORBSubscriptionIntervalTest, noIntervalReportsEveryPublication) +{ + uORB::SubscriptionInterval sub{ORB_ID(orb_test)}; + orb_test_s msg{}; + + EXPECT_FALSE(sub.updated()); + + publish(1); + EXPECT_TRUE(sub.updated()); + EXPECT_TRUE(sub.update(&msg)); + EXPECT_EQ(msg.val, 1); + EXPECT_FALSE(sub.updated()); + EXPECT_FALSE(sub.update(&msg)); + + publish(2); + EXPECT_TRUE(sub.update(&msg)); + EXPECT_EQ(msg.val, 2); +} + +TEST_F(uORBSubscriptionIntervalTest, intervalGatesUpdates) +{ + static constexpr uint32_t interval_us = 200000; + + uORB::SubscriptionInterval sub{ORB_ID(orb_test), interval_us}; + orb_test_s msg{}; + + // The first copy only aligns the schedule; the gate is armed from the second copy on. + publish(1); + EXPECT_TRUE(sub.update(&msg)); + publish(2); + EXPECT_TRUE(sub.update(&msg)); + EXPECT_EQ(msg.val, 2); + + publish(3); + EXPECT_FALSE(sub.updated()); + EXPECT_FALSE(sub.update(&msg)); + EXPECT_EQ(msg.val, 2); + + px4_usleep(interval_us + interval_us / 2); + EXPECT_TRUE(sub.updated()); + EXPECT_TRUE(sub.update(&msg)); + EXPECT_EQ(msg.val, 3); + + // No new data: elapsed interval alone must not report an update. + px4_usleep(interval_us + interval_us / 2); + EXPECT_FALSE(sub.updated()); +} + +} // namespace test +} // namespace uORB