[coap] simplify Coap::Message implementation (#12313)

This change simplifies the `Coap::Message` implementation and removes
the fragile `HelpData` struct which was used to cache header
information within reserved portion of message.

The `Coap::Msg` class is updated to hold the parsed CoAP header
information (type, code, message ID, token). It now inherits from a
new `HeaderInfo` class which contains the parsed fields. This change
helps to simplify many of the call sites which previously had to parse
the header information themselves.

The key changes are:
- The `HelpData` struct is removed from `Coap::Message`.
- `Coap::Msg` is updated to track parsed header info in `HeaderInfo`.
- `otCoapMessageInit()` and `otCoapMessageInitResponse()` now return an
  `otError`.
- Methods are renamed to harmonize their names.
- A new unit test `test_coap_message.cpp` is added to verify the
  `Coap::Message` implementation.
This commit is contained in:
Abtin Keshavarzian
2026-01-23 10:52:42 -08:00
committed by GitHub
parent 3b676fb6f3
commit 5cae26e22b
35 changed files with 1294 additions and 636 deletions
+9 -9
View File
@@ -3333,7 +3333,7 @@ void Mle::HandleAddressSolicitResponse(Coap::Message *aMessage, const Ip6::Messa
VerifyOrExit(aResult == kErrorNone && aMessage != nullptr && aMessageInfo != nullptr);
VerifyOrExit(aMessage->GetCode() == Coap::kCodeChanged);
VerifyOrExit(aMessage->ReadCode() == Coap::kCodeChanged);
Log(kMessageReceive, kTypeAddressReply, aMessageInfo->GetPeerAddr());
@@ -3459,18 +3459,18 @@ exit:
return willBecomeRouter;
}
Error Mle::AddrSolicitInfo::ParseFrom(const Coap::Message &aMessage)
Error Mle::AddrSolicitInfo::ParseFrom(const Coap::Msg &aMsg)
{
// Parses a `kUriAddressSolicit` request message.
Error error;
VerifyOrExit(aMessage.IsConfirmablePostRequest(), error = kErrorParse);
VerifyOrExit(aMsg.IsConfirmablePostRequest(), error = kErrorParse);
SuccessOrExit(error = Tlv::Find<ThreadExtMacAddressTlv>(aMessage, mExtAddress));
SuccessOrExit(error = Tlv::Find<ThreadStatusTlv>(aMessage, mReason));
SuccessOrExit(error = Tlv::Find<ThreadExtMacAddressTlv>(aMsg.mMessage, mExtAddress));
SuccessOrExit(error = Tlv::Find<ThreadStatusTlv>(aMsg.mMessage, mReason));
switch (Tlv::Find<ThreadRloc16Tlv>(aMessage, mRequestedRloc16))
switch (Tlv::Find<ThreadRloc16Tlv>(aMsg.mMessage, mRequestedRloc16))
{
case kErrorNone:
break;
@@ -3482,7 +3482,7 @@ Error Mle::AddrSolicitInfo::ParseFrom(const Coap::Message &aMessage)
}
#if OPENTHREAD_CONFIG_TIME_SYNC_ENABLE
switch (Tlv::Find<XtalAccuracyTlv>(aMessage, mXtalAccuracy))
switch (Tlv::Find<XtalAccuracyTlv>(aMsg.mMessage, mXtalAccuracy))
{
case kErrorNone:
break;
@@ -3576,7 +3576,7 @@ template <> void Mle::HandleTmf<kUriAddressSolicit>(Coap::Msg &aMsg)
Log(kMessageReceive, kTypeAddressSolicit, aMsg.mMessageInfo.GetPeerAddr());
SuccessOrExit(info.ParseFrom(aMsg.mMessage));
SuccessOrExit(info.ParseFrom(aMsg));
ProcessAddressSolicit(info);
@@ -3637,7 +3637,7 @@ template <> void Mle::HandleTmf<kUriAddressRelease>(Coap::Msg &aMsg)
VerifyOrExit(mRole == kRoleLeader);
VerifyOrExit(aMsg.mMessage.IsConfirmablePostRequest());
VerifyOrExit(aMsg.IsConfirmablePostRequest());
Log(kMessageReceive, kTypeAddressRelease, aMsg.mMessageInfo.GetPeerAddr());