diff --git a/src/core/backbone_router/bbr_manager.cpp b/src/core/backbone_router/bbr_manager.cpp index 25996be1b..791286c6a 100644 --- a/src/core/backbone_router/bbr_manager.cpp +++ b/src/core/backbone_router/bbr_manager.cpp @@ -632,10 +632,8 @@ Error Manager::SendBackboneAnswer(const Ip6::Address &aDstAddr, Coap::Message *message = nullptr; Ip6::MessageInfo messageInfo; - VerifyOrExit((message = mBackboneTmfAgent.NewPriorityMessage()) != nullptr, error = kErrorNoBufs); - - SuccessOrExit(error = message->InitAsPost(aDstAddr, kUriBackboneAnswer)); - SuccessOrExit(error = message->AppendPayloadMarker()); + message = mBackboneTmfAgent.AllocateAndInitPriorityPostMessageTo(kUriBackboneAnswer, aDstAddr); + VerifyOrExit(message != nullptr, error = kErrorNoBufs); SuccessOrExit(error = Tlv::Append(*message, aDua)); diff --git a/src/core/coap/coap.cpp b/src/core/coap/coap.cpp index 95b220754..a751568dc 100644 --- a/src/core/coap/coap.cpp +++ b/src/core/coap/coap.cpp @@ -183,6 +183,16 @@ Message *CoapBase::AllocateAndInitNonConfirmablePostMessage(Uri aUri) return InitMessage(NewMessage(), kTypeNonConfirmable, aUri); } +Message *CoapBase::AllocateAndInitPostMessageTo(Uri aUri, const Ip6::Address &aDestination) +{ + return InitMessage(NewMessage(), aDestination.IsMulticast() ? kTypeNonConfirmable : kTypeConfirmable, aUri); +} + +Message *CoapBase::AllocateAndInitPriorityPostMessageTo(Uri aUri, const Ip6::Address &aDestination) +{ + return InitMessage(NewPriorityMessage(), aDestination.IsMulticast() ? kTypeNonConfirmable : kTypeConfirmable, aUri); +} + Message *CoapBase::AllocateAndInitPriorityResponseFor(const Message &aRequest) { return InitResponse(NewPriorityMessage(), aRequest); diff --git a/src/core/coap/coap.hpp b/src/core/coap/coap.hpp index 2b52b4a59..b59a65794 100644 --- a/src/core/coap/coap.hpp +++ b/src/core/coap/coap.hpp @@ -397,8 +397,7 @@ public: Message *AllocateAndInitConfirmablePostMessage(Uri aUri); /** - * Allocates and initializes a new CoAP Non-confirmable Post message with Network Control priority - * level. + * Allocates and initializes a new CoAP Non-confirmable Post message with Network Control priority level. * * The CoAP header is initialized as `kTypeNonConfirmable` and `kCodePost` with a given URI and a randomly * generated token (of default length). This method also sets the payload marker (calling `AppendPayloadMarker()`). @@ -425,6 +424,42 @@ public: */ Message *AllocateAndInitNonConfirmablePostMessage(Uri aUri); + /** + * Allocates and initializes a new CoAP Post message with normal priority level. + * + * If the provided @p aDestination address is multicast, the message will be Non-Confirmable. Otherwise, it will be + * Confirmable. + * + * The CoAP header is initialized as either `kTypeNonConfirmable` or `kTypeConfirmable` and `kCodePost` with the + * given URI and a randomly generated token (of default length). This method also sets the payload marker (calling + * `AppendPayloadMarker()`). Even if the message has no payload, calling `AppendPayloadMarker()` is harmless, since + * `SendMessage()` will check and remove the payload marker when there is no payload. + * + * @param[in] aUri The URI. + * @param[in] aDestination The destination IPv6 address. + * + * @returns A pointer to the message or `nullptr` if failed to allocate message. + */ + Message *AllocateAndInitPostMessageTo(Uri aUri, const Ip6::Address &aDestination); + + /** + * Allocates and initializes a new CoAP Post message with Network Control priority level. + * + * If the provided @p aDestination address is multicast, the message will be Non-Confirmable. Otherwise, it will be + * Confirmable. + * + * The CoAP header is initialized as either `kTypeNonConfirmable` or `kTypeConfirmable` and `kCodePost` with the + * given URI and a randomly generated token (of default length). This method also sets the payload marker (calling + * `AppendPayloadMarker()`). Even if the message has no payload, calling `AppendPayloadMarker()` is harmless, since + * `SendMessage()` will check and remove the payload marker when there is no payload. + * + * @param[in] aUri The URI. + * @param[in] aDestination The destination IPv6 address. + * + * @returns A pointer to the message or `nullptr` if failed to allocate message. + */ + Message *AllocateAndInitPriorityPostMessageTo(Uri aUri, const Ip6::Address &aDestination); + /** * Allocates and initializes a new CoAP response message with Network Control priority level for a * given request message. diff --git a/src/core/coap/coap_message.cpp b/src/core/coap/coap_message.cpp index e53269875..7dfea78ae 100644 --- a/src/core/coap/coap_message.cpp +++ b/src/core/coap/coap_message.cpp @@ -131,11 +131,6 @@ exit: return error; } -Error Message::InitAsPost(const Ip6::Address &aDestination, Uri aUri) -{ - return Init(aDestination.IsMulticast() ? kTypeNonConfirmable : kTypeConfirmable, kCodePost, aUri); -} - Error Message::InitAsResponse(Type aType, Code aCode, const Message &aRequest) { Error error; diff --git a/src/core/coap/coap_message.hpp b/src/core/coap/coap_message.hpp index db7d1bd48..48b9f201f 100644 --- a/src/core/coap/coap_message.hpp +++ b/src/core/coap/coap_message.hpp @@ -485,23 +485,6 @@ public: */ Error Init(Type aType, Code aCode, Uri aUri); - /** - * Initializes a CoAP POST message, appends a URI Path, and adds a random token. - * - * This method erases any previously written content in the message. - * - * The CoAP Type is determined from the destination IPv6 address: `kTypeNonConfirmable` for multicast and - * `kTypeConfirmable` otherwise. The Message ID is set to zero. A random token of default length - * (`Token::kDefaultLength`) is generated and added. - * - * @param[in] aDestination The message destination IPv6 address, used to determine the CoAP Type. - * @param[in] aUri The URI string. - * - * @retval kErrorNone Successfully initialized the message and appended the URI-path option. - * @retval kErrorNoBufs Could not grow the message to append the option. - */ - Error InitAsPost(const Ip6::Address &aDestination, Uri aUri); - /** * Initializes a CoAP message as a response to a request message. * diff --git a/src/core/meshcop/commissioner.cpp b/src/core/meshcop/commissioner.cpp index 2b30d5be7..f0722a280 100644 --- a/src/core/meshcop/commissioner.cpp +++ b/src/core/meshcop/commissioner.cpp @@ -1013,10 +1013,9 @@ Error Commissioner::SendAnnounceBeginRequest(uint32_t aChannelMask, Coap::Message *message = nullptr; VerifyOrExit(IsActive(), error = kErrorInvalidState); - VerifyOrExit((message = Get().NewPriorityMessage()) != nullptr, error = kErrorNoBufs); - SuccessOrExit(error = message->InitAsPost(aAddress, kUriAnnounceBegin)); - SuccessOrExit(error = message->AppendPayloadMarker()); + message = Get().AllocateAndInitPriorityPostMessageTo(kUriAnnounceBegin, aAddress); + VerifyOrExit(message != nullptr, error = kErrorNoBufs); SuccessOrExit(error = Tlv::Append(*message, GetSessionId())); @@ -1046,10 +1045,9 @@ Error Commissioner::SendEnergyScanQuery(uint32_t aChan Coap::Message *message = nullptr; VerifyOrExit(IsActive(), error = kErrorInvalidState); - VerifyOrExit((message = Get().NewPriorityMessage()) != nullptr, error = kErrorNoBufs); - SuccessOrExit(error = message->InitAsPost(aAddress, kUriEnergyScan)); - SuccessOrExit(error = message->AppendPayloadMarker()); + message = Get().AllocateAndInitPriorityPostMessageTo(kUriEnergyScan, aAddress); + VerifyOrExit(message != nullptr, error = kErrorNoBufs); SuccessOrExit(error = Tlv::Append(*message, GetSessionId())); @@ -1103,10 +1101,9 @@ Error Commissioner::SendPanIdQuery(uint16_t aPanId, Coap::Message *message = nullptr; VerifyOrExit(IsActive(), error = kErrorInvalidState); - VerifyOrExit((message = Get().NewPriorityMessage()) != nullptr, error = kErrorNoBufs); - SuccessOrExit(error = message->InitAsPost(aAddress, kUriPanIdQuery)); - SuccessOrExit(error = message->AppendPayloadMarker()); + message = Get().AllocateAndInitPriorityPostMessageTo(kUriPanIdQuery, aAddress); + VerifyOrExit(message != nullptr, error = kErrorNoBufs); SuccessOrExit(error = Tlv::Append(*message, GetSessionId())); diff --git a/src/core/thread/address_resolver.cpp b/src/core/thread/address_resolver.cpp index 27eb54fbd..a11480f89 100644 --- a/src/core/thread/address_resolver.cpp +++ b/src/core/thread/address_resolver.cpp @@ -720,10 +720,8 @@ void AddressResolver::SendAddressError(const Ip6::Address &aTarget, Error error; Coap::Message *message; - VerifyOrExit((message = Get().NewMessage()) != nullptr, error = kErrorNoBufs); - - SuccessOrExit(error = message->InitAsPost(aDestination, kUriAddressError)); - SuccessOrExit(error = message->AppendPayloadMarker()); + message = Get().AllocateAndInitPostMessageTo(kUriAddressError, aDestination); + VerifyOrExit(message != nullptr, error = kErrorNoBufs); SuccessOrExit(error = Tlv::Append(*message, aTarget)); SuccessOrExit(error = Tlv::Append(*message, aMeshLocalIid));