[coap] remove Clone() method from Coap::Message (#12616)

This commit removes the redundant `Clone()` methods from `Coap::Message`.
These methods previously served to retain the `HeaderOffset` across the
cloned message, which was internally tracked by modifying `MeshDest`.
Since `ot::Message::Clone()` natively preserves the `MeshDest` property,
the specialized `Coap::Message::Clone()` wrappers are unnecessary.
Callers now directly use `ot::Message::Clone()` and type-cast the result
using `AsCoapMessagePtr()`.
This commit is contained in:
Abtin Keshavarzian
2026-03-05 14:53:41 -08:00
committed by GitHub
parent e4d97681c5
commit 08f7b370b3
3 changed files with 7 additions and 61 deletions
+7 -8
View File
@@ -1024,11 +1024,10 @@ Error CoapBase::CacheLastBlockResponse(Message *aResponse)
FreeLastBlockResponse();
if ((mLastResponse = aResponse->Clone()) == nullptr)
{
error = kErrorNoBufs;
}
mLastResponse = AsCoapMessagePtr(aResponse->Clone());
VerifyOrExit(mLastResponse != nullptr, error = kErrorNoBufs);
exit:
return error;
}
@@ -1529,7 +1528,7 @@ Error CoapBase::PendingRequests::AddClone(const Message &aMessage, uint16_t aCop
{
Error error = kErrorNone;
aRequest.mMessage = aMessage.Clone(aCopyLength);
aRequest.mMessage = AsCoapMessagePtr(aMessage.Clone(aCopyLength));
VerifyOrExit(aRequest.HasMessage(), error = kErrorNoBufs);
SuccessOrExit(error = aRequest.AppendMetadataToMessage());
@@ -1669,7 +1668,7 @@ void CoapBase::PendingRequests::RetransmitRequest(const Request &aRequest)
Message *clone;
Ip6::MessageInfo messageInfo;
clone = aRequest.mMessage->Clone(aRequest.mMessage->GetLength() - sizeof(Request::Metadata));
clone = AsCoapMessagePtr(aRequest.mMessage->Clone(aRequest.mMessage->GetLength() - sizeof(Request::Metadata)));
VerifyOrExit(clone != nullptr, error = kErrorNoBufs);
aRequest.mMetadata.CopyInfoTo(messageInfo);
@@ -1784,7 +1783,7 @@ Error CoapBase::ResponseCache::SendCachedResponse(const Msg &aRxMsg, CoapBase &a
VerifyOrExit(match != nullptr, error = kErrorNotFound);
response = match->Clone(match->GetLength() - sizeof(ResponseMetadata));
response = AsCoapMessagePtr(match->Clone(match->GetLength() - sizeof(ResponseMetadata)));
VerifyOrExit(response != nullptr, error = kErrorNoBufs);
error = aCoapBase.Send(*response, aRxMsg.mMessageInfo);
@@ -1830,7 +1829,7 @@ void CoapBase::ResponseCache::Add(const Msg &aTxMsg, uint32_t aExchangeLifetime)
MaintainCacheSize();
responseClone = aTxMsg.mMessage.Clone();
responseClone = AsCoapMessagePtr(aTxMsg.mMessage.Clone());
VerifyOrExit(responseClone != nullptr);
metadata.mExpireTime = TimerMilli::GetNow() + aExchangeLifetime;
-16
View File
@@ -690,22 +690,6 @@ exit:
return hasSame;
}
Message *Message::Clone(void) const { return Clone(GetLength()); }
Message *Message::Clone(uint16_t aLength) const { return Clone(aLength, GetReserved()); }
Message *Message::Clone(uint16_t aLength, uint16_t aReserveHeader) const
{
Message *message = static_cast<Message *>(ot::Message::Clone(aLength, aReserveHeader));
VerifyOrExit(message != nullptr);
message->SetHeaderOffset(GetHeaderOffset());
exit:
return message;
}
#if OPENTHREAD_CONFIG_COAP_API_ENABLE
const char *Message::CodeToString(void) const
{
-37
View File
@@ -826,43 +826,6 @@ public:
*/
Error AppendPayloadMarker(void);
/**
* Creates a copy of the message.
*
* It allocates the new message from the same message pool as the original one and copies the entire payload. The
* `Type`, `SubType`, `LinkSecurity`, `Offset`, and `Priority` fields on the cloned message are also
* copied from the original one.
*
* @returns A pointer to the message or `nullptr` if insufficient message buffers are available.
*/
Message *Clone(void) const;
/**
* Creates a copy of this CoAP message.
*
* It allocates the new message from the same message pool as the original one and copies @p aLength octets
* of the payload. The `Type`, `SubType`, `LinkSecurity`, `Offset`, and `Priority` fields on the
* cloned message are also copied from the original one.
*
* @param[in] aLength Number of payload bytes to copy.
*
* @returns A pointer to the message or `nullptr` if insufficient message buffers are available.
*/
Message *Clone(uint16_t aLength) const;
/**
* Creates a copy of the message using a given configuration.
*
* It allocates the new message from the same message pool as the original one. The `Type`, `SubType`,
* `LinkSecurity`, `Offset`, and `Priority` fields on the cloned message are copied from the original one.
*
* @param[in] aLength Number of message bytes to copy.
* @param[in] aReserveHeader Number of header bytes to reserve in the new cloned message.
*
* @returns A pointer to the message or `nullptr` if insufficient message buffers are available.
*/
Message *Clone(uint16_t aLength, uint16_t aReserveHeader) const;
/**
* Returns a pointer to the next message after this as a `Coap::Message`.
*