[core] use Tlv::AppendTlv helper to simplify TLV appends (#12252)

This commit updates various modules to use the `Tlv::AppendTlv` helper
method when appending a TLV with its value to a `Message`.
This commit is contained in:
Abtin Keshavarzian
2025-12-31 14:49:01 -08:00
committed by GitHub
parent da1f792770
commit ca738afc8f
5 changed files with 11 additions and 49 deletions
+1 -5
View File
@@ -606,17 +606,13 @@ Error Commissioner::SendMgmtCommissionerGetRequest(const uint8_t *aTlvs, uint8_t
Error error = kErrorNone;
OwnedPtr<Coap::Message> message;
Tmf::MessageInfo messageInfo(GetInstance());
Tlv tlv;
message.Reset(Get<Tmf::Agent>().NewPriorityConfirmablePostMessage(kUriCommissionerGet));
VerifyOrExit(message != nullptr, error = kErrorNoBufs);
if (aLength > 0)
{
tlv.SetType(Tlv::kGet);
tlv.SetLength(aLength);
SuccessOrExit(error = message->Append(tlv));
SuccessOrExit(error = message->AppendBytes(aTlvs, aLength));
SuccessOrExit(error = Tlv::AppendTlv(*message, Tlv::kGet, aTlvs, aLength));
}
messageInfo.SetSockAddrToRlocPeerAddrToLeaderAloc();
+1 -6
View File
@@ -99,12 +99,7 @@ Error Initiator::AppendLinkMetricsQueryTlv(Message &aMessage, const QueryInfo &a
if (aInfo.mTypeIdCount != 0)
{
QueryOptionsSubTlv queryOptionsTlv;
queryOptionsTlv.Init();
queryOptionsTlv.SetLength(aInfo.mTypeIdCount);
SuccessOrExit(error = aMessage.Append(queryOptionsTlv));
SuccessOrExit(error = aMessage.AppendBytes(aInfo.mTypeIds, aInfo.mTypeIdCount));
SuccessOrExit(error = Tlv::AppendTlv(aMessage, QueryOptionsSubTlv::kType, aInfo.mTypeIds, aInfo.mTypeIdCount));
}
error = Tlv::EndTlv(aMessage, tlvBookmark);
+5 -26
View File
@@ -79,6 +79,11 @@ typedef UintTlvInfo<SubTlv::kQueryId, uint8_t> QueryIdSubTlv;
*/
typedef UintTlvInfo<SubTlv::kStatus, uint8_t> StatusSubTlv;
/**
* Defines Query Option Sub-TLV constants and types.
*/
typedef TlvInfo<SubTlv::kQueryOptions> QueryOptionsSubTlv;
/**
* Implements Link Metrics Report Sub-TLV generation and parsing.
*/
@@ -160,32 +165,6 @@ private:
} mMetricsValue;
} OT_TOOL_PACKED_END;
/**
* Implements Link Metrics Query Options Sub-TLV generation and parsing.
*/
OT_TOOL_PACKED_BEGIN
class QueryOptionsSubTlv : public Tlv, public TlvInfo<SubTlv::kQueryOptions>
{
public:
/**
* Initializes the TLV.
*/
void Init(void)
{
SetType(SubTlv::kQueryOptions);
SetLength(0);
}
/**
* 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(uint8_t); }
} OT_TOOL_PACKED_END;
/**
* Defines Link Metrics Forward Probing Registration Sub-TLV.
*/
+2 -6
View File
@@ -3041,15 +3041,11 @@ Error Mle::SendLinkMetricsManagementRequest(const Ip6::Address &aDestination, co
{
Error error = kErrorNone;
TxMessage *message = NewMleMessage(kCommandLinkMetricsManagementRequest);
Tlv tlv;
VerifyOrExit(message != nullptr, error = kErrorNoBufs);
tlv.SetType(Tlv::kLinkMetricsManagement);
tlv.SetLength(static_cast<uint8_t>(aSubTlv.GetSize()));
SuccessOrExit(error = message->Append(tlv));
SuccessOrExit(error = aSubTlv.AppendTo(*message));
SuccessOrExit(error = Tlv::AppendTlv(*message, Tlv::kLinkMetricsManagement, &aSubTlv,
static_cast<uint16_t>(aSubTlv.GetSize())));
error = message->SendTo(aDestination);
+2 -6
View File
@@ -192,12 +192,8 @@ Error Notifier::SendServerDataNotification(uint16_t aOldRloc16, const NetworkDat
if (aNetworkData != nullptr)
{
ThreadTlv tlv;
tlv.SetType(ThreadTlv::kThreadNetworkData);
tlv.SetLength(aNetworkData->GetLength());
SuccessOrExit(error = message->Append(tlv));
SuccessOrExit(error = message->AppendBytes(aNetworkData->GetBytes(), aNetworkData->GetLength()));
SuccessOrExit(error = Tlv::AppendTlv(*message, ThreadTlv::kThreadNetworkData, aNetworkData->GetBytes(),
aNetworkData->GetLength()));
#if OPENTHREAD_FTD && OPENTHREAD_CONFIG_BORDER_ROUTER_SIGNAL_NETWORK_DATA_FULL
Get<Leader>().CheckForNetDataGettingFull(*aNetworkData, aOldRloc16);