[mle] simplify TimeParameterTlv (#12923)

This commit simplifies the `TimeParameterTlv` implementation by
defining a `TimeParameterTlvValue` and using the `SimpleTlvInfo`
template to define `TimeParameterTlv`.
This commit is contained in:
Abtin Keshavarzian
2026-04-20 16:50:01 -05:00
committed by GitHub
parent e88998abc3
commit 1d6aa9c6d7
2 changed files with 24 additions and 40 deletions
+17 -21
View File
@@ -3788,13 +3788,12 @@ Error Mle::TxMessage::AppendTimeRequestTlv(void)
Error Mle::TxMessage::AppendTimeParameterTlv(void)
{
TimeParameterTlv tlv;
TimeParameterTlvValue tlvValue;
tlv.Init();
tlv.SetTimeSyncPeriod(Get<TimeSync>().GetTimeSyncPeriod());
tlv.SetXtalThreshold(Get<TimeSync>().GetXtalThreshold());
tlvValue.SetTimeSyncPeriod(Get<TimeSync>().GetTimeSyncPeriod());
tlvValue.SetXtalThreshold(Get<TimeSync>().GetXtalThreshold());
return tlv.AppendTo(*this);
return Tlv::Append<TimeParameterTlv>(*this, tlvValue);
}
Error Mle::TxMessage::AppendXtalAccuracyTlv(void)
@@ -5265,9 +5264,6 @@ void Mle::Attacher::HandleParentResponse(RxInfo &aRxInfo)
uint32_t mleFrameCounter;
Mac::ExtAddress extAddress;
Mac::CslAccuracy cslAccuracy;
#if OPENTHREAD_CONFIG_TIME_SYNC_ENABLE
TimeParameterTlv timeParameterTlv;
#endif
SuccessOrExit(error = Tlv::Find<SourceAddressTlv>(aRxInfo.mMessage, sourceAddress));
@@ -5394,23 +5390,23 @@ void Mle::Attacher::HandleParentResponse(RxInfo &aRxInfo)
SuccessOrExit(error = aRxInfo.mMessage.ReadFrameCounterTlvs(linkFrameCounter, mleFrameCounter));
#if OPENTHREAD_CONFIG_TIME_SYNC_ENABLE
if (Tlv::FindTlv(aRxInfo.mMessage, timeParameterTlv) == kErrorNone)
{
VerifyOrExit(timeParameterTlv.IsValid());
Get<TimeSync>().SetTimeSyncPeriod(timeParameterTlv.GetTimeSyncPeriod());
Get<TimeSync>().SetXtalThreshold(timeParameterTlv.GetXtalThreshold());
}
TimeParameterTlvValue tlvValue;
if (Tlv::Find<TimeParameterTlv>(aRxInfo.mMessage, tlvValue) == kErrorNone)
{
Get<TimeSync>().SetTimeSyncPeriod(tlvValue.GetTimeSyncPeriod());
Get<TimeSync>().SetXtalThreshold(tlvValue.GetXtalThreshold());
}
#if OPENTHREAD_CONFIG_TIME_SYNC_REQUIRED
else
{
// If the time sync feature is required, don't choose the
// parent which doesn't support it.
ExitNow();
}
else
{
// If the time sync feature is required, don't choose the
// parent which doesn't support it.
ExitNow();
}
#endif
}
#endif // OPENTHREAD_CONFIG_TIME_SYNC_ENABLE
SuccessOrExit(error = aRxInfo.mMessage.ReadChallengeTlv(mParentCandidate.mRxChallenge));
+7 -19
View File
@@ -867,29 +867,12 @@ typedef SimpleTlvInfo<Tlv::kCslChannel, ChannelTlvValue> CslChannelTlv;
typedef TlvInfo<Tlv::kTimeRequest> TimeRequestTlv;
/**
* Implements Time Parameter TLV generation and parsing.
* Represents a Time Parameter TLV value.
*/
OT_TOOL_PACKED_BEGIN
class TimeParameterTlv : public Tlv, public TlvInfo<Tlv::kTimeParameter>
class TimeParameterTlvValue
{
public:
/**
* Initializes the TLV.
*/
void Init(void)
{
SetType(kTimeParameter);
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 time sync period.
*
@@ -923,6 +906,11 @@ private:
uint16_t mXtalThreshold;
} OT_TOOL_PACKED_END;
/**
* Defines Time Parameter TLV constants and types.
*/
typedef SimpleTlvInfo<Tlv::kTimeParameter, TimeParameterTlvValue> TimeParameterTlv;
#endif // OPENTHREAD_CONFIG_TIME_SYNC_ENABLE
#if OPENTHREAD_CONFIG_MAC_CSL_RECEIVER_ENABLE || OPENTHREAD_CONFIG_MAC_CSL_TRANSMITTER_ENABLE