From e5365622963d243c460135c84dd4d6b70cb15581 Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Thu, 16 Apr 2026 08:27:08 -0700 Subject: [PATCH] [mle] simplify `CslClockAccuracyTlv` (#12905) This commit updates `CslClockAccuracyTlv` to use the `SimpleTlvInfo` and a separate `CslClockAccuracyTlvValue` class. This change simplifies how the TLV is appended to and read from messages by leveraging the `Tlv::Append` and `Tlv::Find` helper methods (avoiding the use of `FindTlv()`). --- src/core/thread/mle.cpp | 19 ++++-------- src/core/thread/mle_tlvs.cpp | 19 ++++++++++++ src/core/thread/mle_tlvs.hpp | 57 ++++++++++++------------------------ 3 files changed, 44 insertions(+), 51 deletions(-) diff --git a/src/core/thread/mle.cpp b/src/core/thread/mle.cpp index d21127af0..f6a6b85ee 100644 --- a/src/core/thread/mle.cpp +++ b/src/core/thread/mle.cpp @@ -3852,13 +3852,8 @@ Error Mle::TxMessage::AppendCslTimeoutTlv(void) #if OPENTHREAD_CONFIG_MAC_CSL_TRANSMITTER_ENABLE Error Mle::TxMessage::AppendCslClockAccuracyTlv(void) { - CslClockAccuracyTlv cslClockAccuracyTlv; - - cslClockAccuracyTlv.Init(); - cslClockAccuracyTlv.SetCslClockAccuracy(Get().GetCslAccuracy()); - cslClockAccuracyTlv.SetCslUncertainty(Get().GetCslUncertainty()); - - return Append(cslClockAccuracyTlv); + return Tlv::Append( + *this, CslClockAccuracyTlvValue(Get().GetCslAccuracy(), Get().GetCslUncertainty())); } #endif @@ -4237,13 +4232,11 @@ exit: #if OPENTHREAD_CONFIG_MAC_CSL_RECEIVER_ENABLE Error Mle::RxMessage::ReadCslClockAccuracyTlv(Mac::CslAccuracy &aCslAccuracy) const { - Error error; - CslClockAccuracyTlv clockAccuracyTlv; + Error error; + CslClockAccuracyTlvValue tlvValue; - SuccessOrExit(error = Tlv::FindTlv(*this, clockAccuracyTlv)); - VerifyOrExit(clockAccuracyTlv.IsValid(), error = kErrorParse); - aCslAccuracy.SetClockAccuracy(clockAccuracyTlv.GetCslClockAccuracy()); - aCslAccuracy.SetUncertainty(clockAccuracyTlv.GetCslUncertainty()); + SuccessOrExit(error = Tlv::Find(*this, tlvValue)); + tlvValue.Get(aCslAccuracy); exit: return error; diff --git a/src/core/thread/mle_tlvs.cpp b/src/core/thread/mle_tlvs.cpp index 61350c3fb..a7ca6244c 100644 --- a/src/core/thread/mle_tlvs.cpp +++ b/src/core/thread/mle_tlvs.cpp @@ -196,5 +196,24 @@ void LeaderDataTlvValue::Get(LeaderData &aLeaderData) const aLeaderData.SetLeaderRouterId(mLeaderRouterId); } +//--------------------------------------------------------------------------------------------------------------------- +// CslClockAccuracyTlvValue + +#if OPENTHREAD_CONFIG_MAC_CSL_RECEIVER_ENABLE || OPENTHREAD_CONFIG_MAC_CSL_TRANSMITTER_ENABLE + +CslClockAccuracyTlvValue::CslClockAccuracyTlvValue(uint8_t aClockAccuracy, uint8_t aUncertainty) + : mClockAccuracy(aClockAccuracy) + , mUncertainty(aUncertainty) +{ +} + +void CslClockAccuracyTlvValue::Get(Mac::CslAccuracy &aAccuracy) const +{ + aAccuracy.SetClockAccuracy(mClockAccuracy); + aAccuracy.SetUncertainty(mUncertainty); +} + +#endif + } // namespace Mle } // namespace ot diff --git a/src/core/thread/mle_tlvs.hpp b/src/core/thread/mle_tlvs.hpp index a88dd90f8..d39d3daa5 100644 --- a/src/core/thread/mle_tlvs.hpp +++ b/src/core/thread/mle_tlvs.hpp @@ -926,63 +926,44 @@ private: #endif // OPENTHREAD_CONFIG_TIME_SYNC_ENABLE #if OPENTHREAD_CONFIG_MAC_CSL_RECEIVER_ENABLE || OPENTHREAD_CONFIG_MAC_CSL_TRANSMITTER_ENABLE + /** - * Implements CSL Clock Accuracy TLV generation and parsing. + * Represents CSL Clock Accuracy TLV value. */ OT_TOOL_PACKED_BEGIN -class CslClockAccuracyTlv : public Tlv, public TlvInfo +class CslClockAccuracyTlvValue { public: /** - * Initializes the TLV. + * Default constructor. */ - void Init(void) - { - SetType(kCslClockAccuracy); - SetLength(sizeof(*this) - sizeof(Tlv)); - } + CslClockAccuracyTlvValue(void) = default; /** - * Indicates whether or not the TLV appears to be well-formed. + * Initializes the TLV value with given clock accuracy and uncertainty. * - * @retval TRUE If the TLV appears to be well-formed. - * @retval FALSE If the TLV does not appear to be well-formed. + * @param[in] aClockAccuracy The clock accuracy in ppm. + * @param[in] aUncertainty The clock uncertainty in units of 10 us. */ - bool IsValid(void) const { return GetLength() >= sizeof(*this) - sizeof(Tlv); } + CslClockAccuracyTlvValue(uint8_t aClockAccuracy, uint8_t aUncertainty); /** - * Returns the CSL Clock Accuracy value. + * Gets the CSL clock accuracy and uncertainty values. * - * @returns The CSL Clock Accuracy value. + * @param[out] aAccuracy A reference to a `Mac::CslAccuracy` to return the values. */ - uint8_t GetCslClockAccuracy(void) const { return mCslClockAccuracy; } - - /** - * Sets the CSL Clock Accuracy value. - * - * @param[in] aCslClockAccuracy The CSL Clock Accuracy value. - */ - void SetCslClockAccuracy(uint8_t aCslClockAccuracy) { mCslClockAccuracy = aCslClockAccuracy; } - - /** - * Returns the Clock Uncertainty value. - * - * @returns The Clock Uncertainty value. - */ - uint8_t GetCslUncertainty(void) const { return mCslUncertainty; } - - /** - * Sets the CSL Uncertainty value. - * - * @param[in] aCslUncertainty The CSL Uncertainty value. - */ - void SetCslUncertainty(uint8_t aCslUncertainty) { mCslUncertainty = aCslUncertainty; } + void Get(Mac::CslAccuracy &aAccuracy) const; private: - uint8_t mCslClockAccuracy; - uint8_t mCslUncertainty; + uint8_t mClockAccuracy; + uint8_t mUncertainty; } OT_TOOL_PACKED_END; +/** + * Defines CSL Clock Accuracy TLV constants and types. + */ +typedef SimpleTlvInfo CslClockAccuracyTlv; + #endif // OPENTHREAD_CONFIG_MAC_CSL_RECEIVER_ENABLE || OPENTHREAD_CONFIG_MAC_CSL_TRANSMITTER_ENABLE /** * @}