From 0313eb7b72b332a295966a6d72bc728763c2a690 Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Mon, 3 Aug 2026 21:55:05 -0700 Subject: [PATCH] [radio] add `Radio::ConvertUncertaintyToUsec()` helper (#13458) This commit introduces `Radio::kUncertaintyUnit` constant and `Radio::ConvertUncertaintyToUsec()` helper function to convert CSL uncertainty (in units of 10 microseconds) to microseconds. It updates `SubMac`, `Mle`, and `CslAccuracy` to use the helper, eliminating manual `* 10` calculations and redundant internal constants. --- src/core/mac/mac_types.hpp | 13 ++----------- src/core/mac/sub_mac_csl_receiver.cpp | 3 ++- src/core/radio/radio.hpp | 2 +- src/core/radio/radio_types.hpp | 11 +++++++++++ src/core/thread/mle.cpp | 2 +- 5 files changed, 17 insertions(+), 14 deletions(-) diff --git a/src/core/mac/mac_types.hpp b/src/core/mac/mac_types.hpp index 975651456..3a408ec80 100644 --- a/src/core/mac/mac_types.hpp +++ b/src/core/mac/mac_types.hpp @@ -903,27 +903,18 @@ public: /** * Returns the CSL uncertainty. * - * @returns The uncertainty in units 10 microseconds. + * @returns The uncertainty in units of 10 microseconds (`Radio::kUncertaintyUnit`). */ uint8_t GetUncertainty(void) const { return mUncertainty; } - /** - * Gets the CLS uncertainty in microseconds. - * - * @returns the CLS uncertainty in microseconds. - */ - uint16_t GetUncertaintyInMicrosec(void) const { return static_cast(mUncertainty) * kUsPerUncertUnit; } - /** * Sets the CSL uncertainty. * - * @param[in] aUncertainty The CSL uncertainty in units 10 microseconds. + * @param[in] aUncertainty The CSL uncertainty in units of 10 microseconds. */ void SetUncertainty(uint8_t aUncertainty) { mUncertainty = aUncertainty; } private: - static constexpr uint8_t kUsPerUncertUnit = 10; - uint8_t mClockAccuracy; uint8_t mUncertainty; }; diff --git a/src/core/mac/sub_mac_csl_receiver.cpp b/src/core/mac/sub_mac_csl_receiver.cpp index 29a12b3ef..9a3643806 100644 --- a/src/core/mac/sub_mac_csl_receiver.cpp +++ b/src/core/mac/sub_mac_csl_receiver.cpp @@ -271,7 +271,8 @@ void SubMac::GetCslWindowEdges(uint32_t &aAhead, uint32_t &aAfter) #endif semiWindow = DetermineClockDrift(elapsed); - semiWindow += mCslParentAccuracy.GetUncertaintyInMicrosec() + Get().GetCslUncertainty() * 10; + semiWindow += + Radio::ConvertUncertaintyToUsec(mCslParentAccuracy.GetUncertainty() + Get().GetCslUncertainty()); aAhead = Min(semiPeriod, semiWindow + kMinReceiveOnAhead + kCslReceiveTimeAhead); aAfter = Min(semiPeriod, semiWindow + kMinReceiveOnAfter); diff --git a/src/core/radio/radio.hpp b/src/core/radio/radio.hpp index 154ee2c84..124d79945 100644 --- a/src/core/radio/radio.hpp +++ b/src/core/radio/radio.hpp @@ -649,7 +649,7 @@ public: /** * Get the fixed uncertainty of the Device for scheduling CSL operations in units of 10 microseconds. * - * @returns The CSL Uncertainty in units of 10 us. + * @returns The CSL Uncertainty in units of 10 microseconds (`kUncertaintyUnit`). */ uint8_t GetCslUncertainty(void); #endif // OT_CONFIG_RADIO_TIME_ENABLE diff --git a/src/core/radio/radio_types.hpp b/src/core/radio/radio_types.hpp index 59ea5c1f1..f2edf7782 100644 --- a/src/core/radio/radio_types.hpp +++ b/src/core/radio/radio_types.hpp @@ -56,6 +56,8 @@ namespace Radio { class Radio; +constexpr uint32_t kUncertaintyUnit = 10; ///< Clock uncertainty unit in microseconds. + /** * Represents a 64-bit radio time in microseconds referenced to a continuous monotonic local radio clock. */ @@ -99,6 +101,15 @@ bool IsTimeStrictlyBefore(Time32 aFirstTime, Time32 aSecondTime); */ uint32_t DetermineClockDrift(uint16_t aClockAccuracy, uint32_t aIntervalUs); +/** + * Converts CSL uncertainty value to microseconds. + * + * @param[in] aUncertainty The uncertainty in units of 10 microseconds (`kUncertaintyUnit`). + * + * @returns The uncertainty duration in microseconds. + */ +inline uint32_t ConvertUncertaintyToUsec(uint16_t aUncertainty) { return aUncertainty * kUncertaintyUnit; } + #if OT_CONFIG_RADIO_TIME_ENABLE && OPENTHREAD_CONFIG_PLATFORM_USEC_TIMER_ENABLE /** diff --git a/src/core/thread/mle.cpp b/src/core/thread/mle.cpp index cb8d34679..c5de531fb 100644 --- a/src/core/thread/mle.cpp +++ b/src/core/thread/mle.cpp @@ -3041,7 +3041,7 @@ uint64_t Mle::CalcParentCslMetric(const Mac::CslAccuracy &aCslAccuracy) const uint64_t k = cslTimeoutUs / cslPeriodUs; return k * (k + 1) * cslPeriodUs / usInSecond * aCslAccuracy.GetClockAccuracy() + - aCslAccuracy.GetUncertaintyInMicrosec() * k; + Radio::ConvertUncertaintyToUsec(aCslAccuracy.GetUncertainty()) * k; } #endif