diff --git a/src/core/thread/mle.cpp b/src/core/thread/mle.cpp index 1c0e93392..f82042ea7 100644 --- a/src/core/thread/mle.cpp +++ b/src/core/thread/mle.cpp @@ -2208,7 +2208,6 @@ void Mle::SendAnnounce(uint8_t aChannel, AnnounceMode aMode) void Mle::SendAnnounce(uint8_t aChannel, const Ip6::Address &aDestination, AnnounceMode aMode) { Error error = kErrorNone; - ChannelTlv channelTlv; MeshCoP::Timestamp activeTimestamp; TxMessage *message = nullptr; @@ -2217,10 +2216,7 @@ void Mle::SendAnnounce(uint8_t aChannel, const Ip6::Address &aDestination, Annou message->SetLinkSecurityEnabled(true); message->SetChannel(aChannel); - channelTlv.Init(); - channelTlv.SetChannelPage(0); - channelTlv.SetChannel(Get().GetPanChannel()); - SuccessOrExit(error = channelTlv.AppendTo(*message)); + SuccessOrExit(error = Tlv::Append(*message, ChannelTlvValue(Get().GetPanChannel()))); switch (aMode) { @@ -3718,7 +3714,7 @@ exit: void Mle::HandleAnnounce(RxInfo &aRxInfo) { Error error = kErrorNone; - ChannelTlv channelTlv; + ChannelTlvValue channelTlvValue; MeshCoP::Timestamp timestamp; const MeshCoP::Timestamp *localTimestamp; uint8_t channel; @@ -3729,10 +3725,8 @@ void Mle::HandleAnnounce(RxInfo &aRxInfo) Log(kMessageReceive, kTypeAnnounce, aRxInfo.mMessageInfo.GetPeerAddr()); - SuccessOrExit(error = Tlv::FindTlv(aRxInfo.mMessage, channelTlv)); - VerifyOrExit(channelTlv.IsValid(), error = kErrorParse); - - channel = static_cast(channelTlv.GetChannel()); + SuccessOrExit(error = Tlv::Find(aRxInfo.mMessage, channelTlvValue)); + channel = static_cast(channelTlvValue.GetChannel()); SuccessOrExit(error = Tlv::Find(aRxInfo.mMessage, timestamp)); SuccessOrExit(error = Tlv::Find(aRxInfo.mMessage, panId)); @@ -4807,16 +4801,10 @@ exit: #if OPENTHREAD_CONFIG_MAC_CSL_RECEIVER_ENABLE Error Mle::TxMessage::AppendCslChannelTlv(void) { - CslChannelTlv cslChannel; - // CSL channel value of zero indicates that the CSL channel is not // specified. We can use this value in the TLV as well. - cslChannel.Init(); - cslChannel.SetChannelPage(0); - cslChannel.SetChannel(Get().GetCslChannel()); - - return Append(cslChannel); + return Tlv::Append(*this, ChannelTlvValue(Get().GetCslChannel())); } Error Mle::TxMessage::AppendCslTimeoutTlv(void) diff --git a/src/core/thread/mle_router.cpp b/src/core/thread/mle_router.cpp index e6785c33a..b97a7ec33 100644 --- a/src/core/thread/mle_router.cpp +++ b/src/core/thread/mle_router.cpp @@ -2314,8 +2314,8 @@ void MleRouter::HandleChildUpdateRequest(RxInfo &aRxInfo) #if OPENTHREAD_CONFIG_MAC_CSL_TRANSMITTER_ENABLE if (child->IsCslSynchronized()) { - CslChannelTlv cslChannel; - uint32_t cslTimeout; + ChannelTlvValue cslChannelTlvValue; + uint32_t cslTimeout; switch (Tlv::Find(aRxInfo.mMessage, cslTimeout)) { @@ -2330,13 +2330,11 @@ void MleRouter::HandleChildUpdateRequest(RxInfo &aRxInfo) ExitNow(error = kErrorNone); } - if (Tlv::FindTlv(aRxInfo.mMessage, cslChannel) == kErrorNone) + if (Tlv::Find(aRxInfo.mMessage, cslChannelTlvValue) == kErrorNone) { - VerifyOrExit(cslChannel.IsValid(), error = kErrorParse); - // Special value of zero is used to indicate that // CSL channel is not specified. - child->SetCslChannel(static_cast(cslChannel.GetChannel())); + child->SetCslChannel(static_cast(cslChannelTlvValue.GetChannel())); } } #endif // OPENTHREAD_CONFIG_MAC_CSL_TRANSMITTER_ENABLE diff --git a/src/core/thread/mle_tlvs.hpp b/src/core/thread/mle_tlvs.hpp index 467622717..b30d95fde 100644 --- a/src/core/thread/mle_tlvs.hpp +++ b/src/core/thread/mle_tlvs.hpp @@ -1028,31 +1028,44 @@ private: }; /** - * Implements Channel TLV generation and parsing. + * Implements Channel TLV value format. + * + * This is used by both Channel TLV and CSL Channel TLV. * */ OT_TOOL_PACKED_BEGIN -class ChannelTlv : public Tlv, public TlvInfo +class ChannelTlvValue { public: /** - * Initializes the TLV. + * Default constructor. * */ - void Init(void) + ChannelTlvValue(void) = default; + + /** + * Initializes the `ChannelTlvValue` with a given channel page and channel values. + * + * @param[in] aChannelPage The channel page. + * @param[in] aChannel The channel. + * + */ + ChannelTlvValue(uint8_t aChannelPage, uint16_t aChannel) + : mChannelPage(aChannelPage) + , mChannel(HostSwap16(aChannel)) { - SetType(kChannel); - SetLength(sizeof(*this) - sizeof(Tlv)); } /** - * Indicates whether or not the TLV appears to be well-formed. + * Initializes the `ChannelTlvValue` with zero channel page and a given channel value. * - * @retval TRUE If the TLV appears to be well-formed. - * @retval FALSE If the TLV does not appear to be well-formed. + * @param[in] aChannel The channel. * */ - bool IsValid(void) const { return GetLength() >= sizeof(*this) - sizeof(Tlv); } + ChannelTlvValue(uint16_t aChannel) + : ChannelTlvValue(0, aChannel) + { + } /** * Returns the Channel Page value. @@ -1091,6 +1104,18 @@ private: uint16_t mChannel; } OT_TOOL_PACKED_END; +/** + * Defines Channel TLV constants and types. + * + */ +typedef SimpleTlvInfo ChannelTlv; + +/** + * Defines CSL Channel TLV constants and types. + * + */ +typedef SimpleTlvInfo CslChannelTlv; + #if OPENTHREAD_CONFIG_TIME_SYNC_ENABLE /** * Defines Time Request TLV constants and types. @@ -1164,73 +1189,6 @@ private: #endif // OPENTHREAD_CONFIG_TIME_SYNC_ENABLE -#if OPENTHREAD_CONFIG_MAC_CSL_RECEIVER_ENABLE || (OPENTHREAD_FTD && OPENTHREAD_CONFIG_MAC_CSL_TRANSMITTER_ENABLE) -/** - * Implements CSL Channel TLV generation and parsing. - * - */ -OT_TOOL_PACKED_BEGIN -class CslChannelTlv : public Tlv, public TlvInfo -{ -public: - /** - * Initializes the TLV. - * - */ - void Init(void) - { - SetType(kCslChannel); - SetLength(sizeof(*this) - sizeof(Tlv)); - } - - /** - * 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); } - - /** - * Returns the Channel Page value. - * - * @returns The Channel Page value. - * - */ - uint8_t GetChannelPage(void) const { return mChannelPage; } - - /** - * Sets the Channel Page value. - * - * @param[in] aChannelPage The Channel Page value. - * - */ - void SetChannelPage(uint8_t aChannelPage) { mChannelPage = aChannelPage; } - - /** - * Returns the Channel value. - * - * @returns The Channel value. - * - */ - uint16_t GetChannel(void) const { return HostSwap16(mChannel); } - - /** - * Sets the Channel value. - * - * @param[in] aChannel The Channel value. - * - */ - void SetChannel(uint16_t aChannel) { mChannel = HostSwap16(aChannel); } - -private: - uint8_t mChannelPage; - uint16_t mChannel; -} OT_TOOL_PACKED_END; - -#endif // OPENTHREAD_CONFIG_MAC_CSL_RECEIVER_ENABLE || (OPENTHREAD_FTD && OPENTHREAD_CONFIG_MAC_CSL_TRANSMITTER_ENABLE) - #if OPENTHREAD_CONFIG_MAC_CSL_RECEIVER_ENABLE || OPENTHREAD_CONFIG_MAC_CSL_TRANSMITTER_ENABLE /** * Implements CSL Clock Accuracy TLV generation and parsing.