From 494a4868a3173a916619934b7162af85adc474dd Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Wed, 27 May 2026 10:25:03 -0700 Subject: [PATCH] [net-diag] convert MAC and MLE counters TLVs to `SimpleTlvInfo` (#13157) This commit updates `MacCountersTlv` and `MleCountersTlv` to use the `SimpleTlvInfo` template. The original classes are replaced with `MacCountersTlvValue` and `MleCountersTlvValue` which only represent the TLV values. This helps simplify the TLV parsing and appending logic and more importantly allows the TLV value formats to be reused. --- src/core/thread/net_diag.cpp | 26 ++++++++-------- src/core/thread/net_diag_tlvs.cpp | 18 ++++-------- src/core/thread/net_diag_tlvs.hpp | 49 +++++++++++++------------------ 3 files changed, 39 insertions(+), 54 deletions(-) diff --git a/src/core/thread/net_diag.cpp b/src/core/thread/net_diag.cpp index 8406ddc35..a3acabce3 100644 --- a/src/core/thread/net_diag.cpp +++ b/src/core/thread/net_diag.cpp @@ -371,19 +371,19 @@ Error Server::AppendDiagTlv(uint8_t aTlvType, Message &aMessage) case Tlv::kMacCounters: { - MacCountersTlv tlv; + MacCountersTlvValue tlvValue; - tlv.Init(Get().GetCounters()); - error = tlv.AppendTo(aMessage); + tlvValue.InitFrom(Get().GetCounters()); + error = Tlv::Append(aMessage, tlvValue); break; } case Tlv::kMleCounters: { - MleCountersTlv tlv; + MleCountersTlvValue tlvValue; - tlv.Init(Get().GetCounters()); - error = tlv.AppendTo(aMessage); + tlvValue.InitFrom(Get().GetCounters()); + error = Tlv::Append(aMessage, tlvValue); break; } @@ -1195,21 +1195,19 @@ Error Client::GetNextDiagTlv(const Coap::Message &aMessage, Iterator &aIterator, case Tlv::kMacCounters: { - MacCountersTlv macCountersTlv; + MacCountersTlvValue tlvValue; - SuccessOrExit(error = aMessage.Read(offset, macCountersTlv)); - VerifyOrExit(macCountersTlv.IsValid(), error = kErrorParse); - macCountersTlv.Read(aDiagTlv.mData.mMacCounters); + SuccessOrExit(error = tlvInfo.Read(aMessage, tlvValue)); + tlvValue.Read(aDiagTlv.mData.mMacCounters); break; } case Tlv::kMleCounters: { - MleCountersTlv mleCoutersTlv; + MleCountersTlvValue tlvValue; - SuccessOrExit(error = aMessage.Read(offset, mleCoutersTlv)); - VerifyOrExit(mleCoutersTlv.IsValid(), error = kErrorParse); - mleCoutersTlv.Read(aDiagTlv.mData.mMleCounters); + SuccessOrExit(error = tlvInfo.Read(aMessage, tlvValue)); + tlvValue.Read(aDiagTlv.mData.mMleCounters); break; } diff --git a/src/core/thread/net_diag_tlvs.cpp b/src/core/thread/net_diag_tlvs.cpp index 6956cdcf0..fb116f305 100644 --- a/src/core/thread/net_diag_tlvs.cpp +++ b/src/core/thread/net_diag_tlvs.cpp @@ -214,13 +214,10 @@ void AnswerTlvValue::Init(uint16_t aIndex, IsLastFlag aIsLastFlag) } //--------------------------------------------------------------------------------------------------------------------- -// MacCountersTlv +// MacCountersTlvValue -void MacCountersTlv::Init(const Mac::Counters &aMacCounters) +void MacCountersTlvValue::InitFrom(const Mac::Counters &aMacCounters) { - SetType(kMacCounters); - SetLength(sizeof(*this) - sizeof(Tlv)); - mIfInUnknownProtos = BigEndian::HostSwap32(aMacCounters.mRxOther); mIfInErrors = BigEndian::HostSwap32(aMacCounters.mRxErrNoFrame + aMacCounters.mRxErrUnknownNeighbor + aMacCounters.mRxErrInvalidSrcAddr + aMacCounters.mRxErrSec + @@ -235,7 +232,7 @@ void MacCountersTlv::Init(const Mac::Counters &aMacCounters) mIfOutDiscards = BigEndian::HostSwap32(aMacCounters.mTxErrBusyChannel); } -void MacCountersTlv::Read(MacCounters &aDiagMacCounters) const +void MacCountersTlvValue::Read(MacCounters &aDiagMacCounters) const { aDiagMacCounters.mIfInUnknownProtos = BigEndian::HostSwap32(mIfInUnknownProtos); aDiagMacCounters.mIfInErrors = BigEndian::HostSwap32(mIfInErrors); @@ -249,13 +246,10 @@ void MacCountersTlv::Read(MacCounters &aDiagMacCounters) const } //--------------------------------------------------------------------------------------------------------------------- -// MleCountersTlv +// MleCountersTlvValue -void MleCountersTlv::Init(const Mle::Counters &aMleCounters) +void MleCountersTlvValue::InitFrom(const Mle::Counters &aMleCounters) { - SetType(kMleCounters); - SetLength(sizeof(*this) - sizeof(Tlv)); - mDisabledRole = BigEndian::HostSwap16(aMleCounters.mDisabledRole); mDetachedRole = BigEndian::HostSwap16(aMleCounters.mDetachedRole); mChildRole = BigEndian::HostSwap16(aMleCounters.mChildRole); @@ -273,7 +267,7 @@ void MleCountersTlv::Init(const Mle::Counters &aMleCounters) mLeaderTime = BigEndian::HostSwap64(aMleCounters.mLeaderTime); } -void MleCountersTlv::Read(MleCounters &aDiagMleCounters) const +void MleCountersTlvValue::Read(MleCounters &aDiagMleCounters) const { aDiagMleCounters.mDisabledRole = BigEndian::HostSwap16(mDisabledRole); aDiagMleCounters.mDetachedRole = BigEndian::HostSwap16(mDetachedRole); diff --git a/src/core/thread/net_diag_tlvs.hpp b/src/core/thread/net_diag_tlvs.hpp index 1efa1a50b..3ed1b40ec 100644 --- a/src/core/thread/net_diag_tlvs.hpp +++ b/src/core/thread/net_diag_tlvs.hpp @@ -330,31 +330,23 @@ typedef SimpleTlvInfo LeaderDataTlv; typedef otNetworkDiagMacCounters MacCounters; /** - * Implements Mac Counters TLV generation and parsing. + * Implements Mac Counters TLV value generation and parsing. */ OT_TOOL_PACKED_BEGIN -class MacCountersTlv : public Tlv, public TlvInfo +class MacCountersTlvValue { public: /** - * Initializes the TLV. + * Initializes the TLV value. * * @param[in] aMacCounters The MAC counters to initialize the TLV with. */ - void Init(const Mac::Counters &aMacCounters); + void InitFrom(const Mac::Counters &aMacCounters); /** - * Indicates whether or not the TLV appears to be well-formed. + * Reads the counters from the TLV value. * - * @retval TRUE If the TLV appears to be well-formed. - * @retval FALSE If the TLV does not appear to be well-formed. - */ - bool IsValid(void) const { return GetLength() >= sizeof(*this) - sizeof(Tlv); } - - /** - * Reads the counters from TLV. - * - * @param[out] aDiagMacCounters A reference to `NetDiag::MacCounters` to populate. + * @param[out] aDiagMacCounters A reference to `MacCounters` to populate. */ void Read(MacCounters &aDiagMacCounters) const; @@ -370,6 +362,11 @@ private: uint32_t mIfOutDiscards; } OT_TOOL_PACKED_END; +/** + * Defines Mac Counters TLV constants and types. + */ +typedef SimpleTlvInfo MacCountersTlv; + /** * Implements Child Table TLV Entry generation and parsing. */ @@ -870,30 +867,21 @@ typedef SimpleTlvInfo AnswerTlv; typedef otNetworkDiagMleCounters MleCounters; /** - * Implements MLE Counters TLV generation and parsing. + * Implements MLE Counters TLV value generation and parsing. */ OT_TOOL_PACKED_BEGIN -class MleCountersTlv : public Tlv, public TlvInfo +class MleCountersTlvValue { public: /** - * Initializes the TLV. + * Initializes the TLV value. * * @param[in] aMleCounters The MLE counters to initialize the TLV with. */ - void Init(const Mle::Counters &aMleCounters); + void InitFrom(const Mle::Counters &aMleCounters); /** - * Indicates whether or not the TLV appears to be well-formed. - * - * @retval TRUE If the TLV appears to be well-formed. - * @retval FALSE If the TLV does not appear to be well-formed. - */ - bool IsValid(void) const { return GetLength() >= sizeof(*this) - sizeof(Tlv); } - - /** - * - * Reads the counters from TLV. + * Reads the counters from TLV value * * @param[out] aDiagMleCounters A reference to `NetDiag::MleCounters` to populate. */ @@ -917,6 +905,11 @@ private: uint64_t mLeaderTime; // Milliseconds device has been in leader role. } OT_TOOL_PACKED_END; +/** + * Defines MLE Counters TLV constants and types. + */ +typedef SimpleTlvInfo MleCountersTlv; + } // namespace NetDiag DefineCoreType(otNetworkDiagConnectivity, NetDiag::Connectivity);