mirror of
https://github.com/espressif/openthread.git
synced 2026-06-06 05:24:51 +00:00
[message] introduce MessageAllocator to unify allocation (#12702)
This commit introduces the `MessageAllocator` template class using the CRTP pattern to provide a unified implementation of the `NewMessage()` methods. It standardizes the reserved header sizes for different message types within `ReservedHeaderSize`. This removes boilerplate code and redundant `NewMessage()` method implementations across the `Ip6`, `Icmp`, `Udp`, `Udp::Socket`, and `CoapBase` classes.
This commit is contained in:
committed by
GitHub
parent
422a649919
commit
44f5cddc2e
@@ -462,6 +462,7 @@ openthread_core_files = [
|
||||
"common/logging.hpp",
|
||||
"common/message.cpp",
|
||||
"common/message.hpp",
|
||||
"common/message_allocator.hpp",
|
||||
"common/msg_backed_array.hpp",
|
||||
"common/non_copyable.hpp",
|
||||
"common/notifier.cpp",
|
||||
|
||||
@@ -132,7 +132,7 @@ exit:
|
||||
|
||||
otMessage *otIp6NewMessage(otInstance *aInstance, const otMessageSettings *aSettings)
|
||||
{
|
||||
return AsCoreType(aInstance).Get<Ip6::Ip6>().NewMessage(0, Message::Settings::From(aSettings));
|
||||
return AsCoreType(aInstance).Get<Ip6::Ip6>().NewMessage(Message::Settings::From(aSettings));
|
||||
}
|
||||
|
||||
otMessage *otIp6NewMessageFromBuffer(otInstance *aInstance,
|
||||
|
||||
@@ -39,7 +39,7 @@ using namespace ot;
|
||||
|
||||
otMessage *otUdpNewMessage(otInstance *aInstance, const otMessageSettings *aSettings)
|
||||
{
|
||||
return AsCoreType(aInstance).Get<Ip6::Udp>().NewMessage(0, Message::Settings::From(aSettings));
|
||||
return AsCoreType(aInstance).Get<Ip6::Udp>().NewMessage(Message::Settings::From(aSettings));
|
||||
}
|
||||
|
||||
otError otUdpOpen(otInstance *aInstance, otUdpSocket *aSocket, otUdpReceive aCallback, void *aContext)
|
||||
|
||||
+5
-22
@@ -145,27 +145,9 @@ void CoapBase::RemoveResource(Resource &aResource)
|
||||
aResource.SetNext(nullptr);
|
||||
}
|
||||
|
||||
Message *CoapBase::NewMessage(const Message::Settings &aSettings)
|
||||
{
|
||||
Message *message = nullptr;
|
||||
|
||||
VerifyOrExit((message = AsCoapMessagePtr(Get<Ip6::Udp>().NewMessage(0, aSettings))) != nullptr);
|
||||
message->SetOffset(0);
|
||||
|
||||
exit:
|
||||
return message;
|
||||
}
|
||||
|
||||
Message *CoapBase::NewMessage(void) { return NewMessage(Message::Settings::GetDefault()); }
|
||||
|
||||
Message *CoapBase::NewPriorityMessage(void)
|
||||
{
|
||||
return NewMessage(Message::Settings(kWithLinkSecurity, Message::kPriorityNet));
|
||||
}
|
||||
|
||||
Message *CoapBase::AllocateAndInitPriorityConfirmablePostMessage(Uri aUri)
|
||||
{
|
||||
return InitMessage(NewPriorityMessage(), kTypeConfirmable, aUri);
|
||||
return InitMessage(NewNetPriorityMessage(), kTypeConfirmable, aUri);
|
||||
}
|
||||
|
||||
Message *CoapBase::AllocateAndInitConfirmablePostMessage(Uri aUri)
|
||||
@@ -175,7 +157,7 @@ Message *CoapBase::AllocateAndInitConfirmablePostMessage(Uri aUri)
|
||||
|
||||
Message *CoapBase::AllocateAndInitPriorityNonConfirmablePostMessage(Uri aUri)
|
||||
{
|
||||
return InitMessage(NewPriorityMessage(), kTypeNonConfirmable, aUri);
|
||||
return InitMessage(NewNetPriorityMessage(), kTypeNonConfirmable, aUri);
|
||||
}
|
||||
|
||||
Message *CoapBase::AllocateAndInitNonConfirmablePostMessage(Uri aUri)
|
||||
@@ -190,12 +172,13 @@ Message *CoapBase::AllocateAndInitPostMessageTo(Uri aUri, const Ip6::Address &aD
|
||||
|
||||
Message *CoapBase::AllocateAndInitPriorityPostMessageTo(Uri aUri, const Ip6::Address &aDestination)
|
||||
{
|
||||
return InitMessage(NewPriorityMessage(), aDestination.IsMulticast() ? kTypeNonConfirmable : kTypeConfirmable, aUri);
|
||||
return InitMessage(NewNetPriorityMessage(), aDestination.IsMulticast() ? kTypeNonConfirmable : kTypeConfirmable,
|
||||
aUri);
|
||||
}
|
||||
|
||||
Message *CoapBase::AllocateAndInitPriorityResponseFor(const Message &aRequest)
|
||||
{
|
||||
return InitResponse(NewPriorityMessage(), aRequest);
|
||||
return InitResponse(NewNetPriorityMessage(), aRequest);
|
||||
}
|
||||
|
||||
Message *CoapBase::AllocateAndInitResponseFor(const Message &aRequest) { return InitResponse(NewMessage(), aRequest); }
|
||||
|
||||
+5
-24
@@ -40,6 +40,7 @@
|
||||
#include "common/linked_list.hpp"
|
||||
#include "common/locator.hpp"
|
||||
#include "common/message.hpp"
|
||||
#include "common/message_allocator.hpp"
|
||||
#include "common/non_copyable.hpp"
|
||||
#include "common/owned_ptr.hpp"
|
||||
#include "common/timer.hpp"
|
||||
@@ -288,7 +289,10 @@ protected:
|
||||
/**
|
||||
* Implements the CoAP client and server.
|
||||
*/
|
||||
class CoapBase : public InstanceLocator, private NonCopyable
|
||||
class CoapBase
|
||||
: public InstanceLocator,
|
||||
public MessageAllocator<CoapBase, ReservedHeaderSize::kCoapMessage, Message::kTypeIp6, ot::Coap::Message>,
|
||||
private NonCopyable
|
||||
{
|
||||
public:
|
||||
/**
|
||||
@@ -345,29 +349,6 @@ public:
|
||||
*/
|
||||
void SetResponseFallback(ResponseFallback aHandler, void *aContext) { mResponseFallback.Set(aHandler, aContext); }
|
||||
|
||||
/**
|
||||
* Allocates a new message with a CoAP header.
|
||||
*
|
||||
* @param[in] aSettings The message settings.
|
||||
*
|
||||
* @returns A pointer to the message or `nullptr` if failed to allocate message.
|
||||
*/
|
||||
Message *NewMessage(const Message::Settings &aSettings);
|
||||
|
||||
/**
|
||||
* Allocates a new message with a CoAP header with default settings.
|
||||
*
|
||||
* @returns A pointer to the message or `nullptr` if failed to allocate message.
|
||||
*/
|
||||
Message *NewMessage(void);
|
||||
|
||||
/**
|
||||
* Allocates a new message with a CoAP header that has Network Control priority level.
|
||||
*
|
||||
* @returns A pointer to the message or `nullptr` if failed to allocate message.
|
||||
*/
|
||||
Message *NewPriorityMessage(void);
|
||||
|
||||
/**
|
||||
* Allocates and initializes a new CoAP Confirmable Post message with Network Control priority level.
|
||||
*
|
||||
|
||||
@@ -0,0 +1,142 @@
|
||||
/*
|
||||
* Copyright (c) 2026, The OpenThread Authors.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the copyright holder nor the
|
||||
* names of its contributors may be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file
|
||||
* This file includes definitions for the message allocator.
|
||||
*/
|
||||
|
||||
#ifndef OT_CORE_COMMON_MESSAGE_ALLOCATOR_HPP_
|
||||
#define OT_CORE_COMMON_MESSAGE_ALLOCATOR_HPP_
|
||||
|
||||
#include "openthread-core-config.h"
|
||||
|
||||
#include "common/message.hpp"
|
||||
#include "net/ip6_headers.hpp"
|
||||
|
||||
namespace ot {
|
||||
|
||||
/**
|
||||
* @addtogroup core-message
|
||||
*
|
||||
* @brief
|
||||
* This module includes definitions for the message allocator base class.
|
||||
*
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* Defines constants for the reserved header sizes for different message types.
|
||||
*/
|
||||
struct ReservedHeaderSize
|
||||
{
|
||||
/**
|
||||
* The reserved header size for an IPv6 message.
|
||||
*/
|
||||
static constexpr uint16_t kIp6Message = sizeof(Ip6::Header) + sizeof(Ip6::HopByHopHeader) + sizeof(Ip6::MplOption);
|
||||
|
||||
/**
|
||||
* The reserved header size for a UDP/IPv6 message
|
||||
*/
|
||||
static constexpr uint16_t kUdpMessage = kIp6Message + sizeof(Ip6::UdpHeader);
|
||||
|
||||
/**
|
||||
* The reserved header size for an ICMPv6 message.
|
||||
*/
|
||||
static constexpr uint16_t kIcmp6Message = kIp6Message + sizeof(Ip6::Icmp6Header);
|
||||
|
||||
/**
|
||||
* The reserved header size for a CoAP message.
|
||||
*/
|
||||
static constexpr uint16_t kCoapMessage = kUdpMessage;
|
||||
};
|
||||
|
||||
/**
|
||||
* Defines a `MessageAllocator` object which provides `NewMessage` methods with a fixed reserved header size.
|
||||
*
|
||||
* Users of this class should follow CRTP-style inheritance, i.e., the `Type` class itself should publicly inherit
|
||||
* from `MessageAllocator<Type, kReservedHeader>`.
|
||||
*
|
||||
* @tparam Type The type of the class that inherits from this one (CRTP).
|
||||
* @tparam kReservedHeader The number of header bytes to reserve.
|
||||
* @tparam kType The `Message::Type` value to use for the allocated message.
|
||||
* @tparam MessageType The allocated message's type. MUST be sub-class of `ot::Message` (e.g. `Coap::Message`).
|
||||
*/
|
||||
template <typename Type,
|
||||
uint16_t kReservedHeader,
|
||||
Message::Type kType = Message::kTypeIp6,
|
||||
typename MessageType = ot::Message>
|
||||
class MessageAllocator
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* Allocates a new message with default settings (link security enabled and `kPriorityNormal`) and the
|
||||
* `kReservedHeader` reserved header size.
|
||||
*
|
||||
* @returns A pointer to the message or `nullptr` if no buffers are available.
|
||||
*/
|
||||
MessageType *NewMessage(void)
|
||||
{
|
||||
return AsMessageType(static_cast<Type *>(this)->template Get<MessagePool>().Allocate(kType, kReservedHeader));
|
||||
}
|
||||
|
||||
/**
|
||||
* Allocates a new message with given settings and the `kReservedHeader` reserved header size.
|
||||
*
|
||||
* @param[in] aSettings The message settings.
|
||||
*
|
||||
* @returns A pointer to the message or `nullptr` if no buffers are available.
|
||||
*/
|
||||
MessageType *NewMessage(const Message::Settings &aSettings)
|
||||
{
|
||||
return AsMessageType(
|
||||
static_cast<Type *>(this)->template Get<MessagePool>().Allocate(kType, kReservedHeader, aSettings));
|
||||
}
|
||||
|
||||
/**
|
||||
* Allocates a new message with link security enabled and `kPriorityNet` and the `kReservedHeader` reserved header
|
||||
* size.
|
||||
*
|
||||
* @returns A pointer to the message or `nullptr` if no buffers are available.
|
||||
*/
|
||||
MessageType *NewNetPriorityMessage(void) { return NewMessage(Message::Settings(Message::kPriorityNet)); }
|
||||
|
||||
protected:
|
||||
MessageAllocator(void) = default;
|
||||
|
||||
private:
|
||||
static MessageType *AsMessageType(Message *aMessage) { return static_cast<MessageType *>(aMessage); }
|
||||
};
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
} // namespace ot
|
||||
|
||||
#endif // OT_CORE_COMMON_MESSAGE_ALLOCATOR_HPP_
|
||||
@@ -769,7 +769,7 @@ void Manager::CoapDtlsSession::HandleLeaderResponseToFwdTmf(const ForwardContext
|
||||
|
||||
SuccessOrExit(error = aResult);
|
||||
|
||||
forwardMessage.Reset(NewPriorityMessage());
|
||||
forwardMessage.Reset(NewNetPriorityMessage());
|
||||
VerifyOrExit(forwardMessage != nullptr, error = kErrorNoBufs);
|
||||
|
||||
if (aResponse->GetCode() == Coap::kCodeChanged)
|
||||
@@ -926,7 +926,7 @@ void Manager::CoapDtlsSession::SendErrorMessage(Error aError, const Coap::Token
|
||||
OwnedPtr<Coap::Message> message;
|
||||
Coap::Message::Code code;
|
||||
|
||||
message.Reset(NewPriorityMessage());
|
||||
message.Reset(NewNetPriorityMessage());
|
||||
VerifyOrExit(message != nullptr, error = kErrorNoBufs);
|
||||
|
||||
code = (aError == kErrorParse) ? Coap::kCodeBadRequest : Coap::kCodeInternalError;
|
||||
|
||||
@@ -168,7 +168,7 @@ template <> void JoinerRouter::HandleTmf<kUriRelayTx>(Coap::Msg &aMsg)
|
||||
|
||||
SuccessOrExit(error = Tlv::FindTlvValueOffsetRange(aMsg.mMessage, Tlv::kJoinerDtlsEncapsulation, offsetRange));
|
||||
|
||||
VerifyOrExit((message = mSocket.NewMessage(0, settings)) != nullptr, error = kErrorNoBufs);
|
||||
VerifyOrExit((message = mSocket.NewMessage(settings)) != nullptr, error = kErrorNoBufs);
|
||||
|
||||
SuccessOrExit(error = message->AppendBytesFromMessage(aMsg.mMessage, offsetRange));
|
||||
|
||||
|
||||
@@ -47,8 +47,6 @@ Icmp::Icmp(Instance &aInstance)
|
||||
{
|
||||
}
|
||||
|
||||
Message *Icmp::NewMessage(void) { return Get<Ip6>().NewMessage(sizeof(Header)); }
|
||||
|
||||
Error Icmp::RegisterHandler(Handler &aHandler) { return mHandlers.Add(aHandler); }
|
||||
|
||||
Error Icmp::UnregisterHandler(Handler &aHandler) { return mHandlers.Remove(aHandler); }
|
||||
@@ -103,7 +101,7 @@ Error Icmp::SendError(Header::Type aType, Header::Code aCode, const MessageInfo
|
||||
|
||||
messageInfoLocal = aMessageInfo;
|
||||
|
||||
VerifyOrExit((message = Get<Ip6>().NewMessage(0, settings)) != nullptr, error = kErrorNoBufs);
|
||||
VerifyOrExit((message = Get<Ip6>().NewMessage(settings)) != nullptr, error = kErrorNoBufs);
|
||||
|
||||
// Prepare the ICMPv6 error message. We only include the IPv6 header
|
||||
// of the original message causing the error.
|
||||
@@ -189,7 +187,7 @@ Error Icmp::HandleEchoRequest(Message &aRequestMessage, const MessageInfo &aMess
|
||||
icmp6Header.Clear();
|
||||
icmp6Header.SetType(Header::kTypeEchoReply);
|
||||
|
||||
if ((replyMessage = Get<Ip6>().NewMessage(0)) == nullptr)
|
||||
if ((replyMessage = Get<Ip6>().NewMessage()) == nullptr)
|
||||
{
|
||||
LogDebg("Failed to allocate a new message");
|
||||
ExitNow();
|
||||
|
||||
@@ -43,6 +43,7 @@
|
||||
#include "common/encoding.hpp"
|
||||
#include "common/linked_list.hpp"
|
||||
#include "common/locator.hpp"
|
||||
#include "common/message_allocator.hpp"
|
||||
#include "common/non_copyable.hpp"
|
||||
#include "net/ip6_headers.hpp"
|
||||
|
||||
@@ -63,7 +64,9 @@ class Headers;
|
||||
/**
|
||||
* Implements ICMPv6.
|
||||
*/
|
||||
class Icmp : public InstanceLocator, private NonCopyable
|
||||
class Icmp : public InstanceLocator,
|
||||
public MessageAllocator<Icmp, ReservedHeaderSize::kIcmp6Message>,
|
||||
private NonCopyable
|
||||
{
|
||||
public:
|
||||
typedef Icmp6Header Header; ///< ICMPv6 header
|
||||
@@ -103,13 +106,6 @@ public:
|
||||
*/
|
||||
explicit Icmp(Instance &aInstance);
|
||||
|
||||
/**
|
||||
* Returns a new ICMP message with sufficient header space reserved.
|
||||
*
|
||||
* @returns A pointer to the message or `nullptr` if no buffers are available.
|
||||
*/
|
||||
Message *NewMessage(void);
|
||||
|
||||
/**
|
||||
* Registers ICMPv6 handler.
|
||||
*
|
||||
|
||||
@@ -66,16 +66,6 @@ Ip6::Ip6(Instance &aInstance)
|
||||
#endif
|
||||
}
|
||||
|
||||
Message *Ip6::NewMessage(void) { return NewMessage(0); }
|
||||
|
||||
Message *Ip6::NewMessage(uint16_t aReserved) { return NewMessage(aReserved, Message::Settings::GetDefault()); }
|
||||
|
||||
Message *Ip6::NewMessage(uint16_t aReserved, const Message::Settings &aSettings)
|
||||
{
|
||||
return Get<MessagePool>().Allocate(
|
||||
Message::kTypeIp6, sizeof(Header) + sizeof(HopByHopHeader) + sizeof(MplOption) + aReserved, aSettings);
|
||||
}
|
||||
|
||||
Message *Ip6::NewMessageFromData(const uint8_t *aData, uint16_t aDataLength, const Message::Settings &aSettings)
|
||||
{
|
||||
Message *message = nullptr;
|
||||
|
||||
+2
-29
@@ -48,6 +48,7 @@
|
||||
#include "common/locator.hpp"
|
||||
#include "common/log.hpp"
|
||||
#include "common/message.hpp"
|
||||
#include "common/message_allocator.hpp"
|
||||
#include "common/non_copyable.hpp"
|
||||
#include "common/owned_ptr.hpp"
|
||||
#include "common/time_ticker.hpp"
|
||||
@@ -102,7 +103,7 @@ namespace Ip6 {
|
||||
/**
|
||||
* Implements the core IPv6 message processing.
|
||||
*/
|
||||
class Ip6 : public InstanceLocator, private NonCopyable
|
||||
class Ip6 : public InstanceLocator, public MessageAllocator<Ip6, ReservedHeaderSize::kIp6Message>, private NonCopyable
|
||||
{
|
||||
friend class ot::Instance;
|
||||
friend class ot::TimeTicker;
|
||||
@@ -118,34 +119,6 @@ public:
|
||||
*/
|
||||
explicit Ip6(Instance &aInstance);
|
||||
|
||||
/**
|
||||
* Allocates a new message buffer from the buffer pool with default settings (link security
|
||||
* enabled and `kPriorityMedium`).
|
||||
*
|
||||
* @returns A pointer to the message or `nullptr` if insufficient message buffers are available.
|
||||
*/
|
||||
Message *NewMessage(void);
|
||||
|
||||
/**
|
||||
* Allocates a new message buffer from the buffer pool with default settings (link security
|
||||
* enabled and `kPriorityMedium`).
|
||||
*
|
||||
* @param[in] aReserved The number of header bytes to reserve following the IPv6 header.
|
||||
*
|
||||
* @returns A pointer to the message or `nullptr` if insufficient message buffers are available.
|
||||
*/
|
||||
Message *NewMessage(uint16_t aReserved);
|
||||
|
||||
/**
|
||||
* Allocates a new message buffer from the buffer pool.
|
||||
*
|
||||
* @param[in] aReserved The number of header bytes to reserve following the IPv6 header.
|
||||
* @param[in] aSettings The message settings.
|
||||
*
|
||||
* @returns A pointer to the message or `nullptr` if insufficient message buffers are available.
|
||||
*/
|
||||
Message *NewMessage(uint16_t aReserved, const Message::Settings &aSettings);
|
||||
|
||||
/**
|
||||
* Allocates a new message buffer from the buffer pool and writes the IPv6 datagram to the message.
|
||||
*
|
||||
|
||||
@@ -76,7 +76,7 @@ Translator::Translator(Instance &aInstance)
|
||||
|
||||
Message *Translator::NewIp4Message(const Message::Settings &aSettings)
|
||||
{
|
||||
Message *message = Get<Ip6::Ip6>().NewMessage(sizeof(Ip6::Header) - sizeof(Ip4::Header), aSettings);
|
||||
Message *message = Get<Ip6::Ip6>().NewMessage(aSettings);
|
||||
|
||||
if (message != nullptr)
|
||||
{
|
||||
|
||||
@@ -946,7 +946,7 @@ extern "C" {
|
||||
otMessage *tcplp_sys_new_message(otInstance *aInstance)
|
||||
{
|
||||
Instance &instance = AsCoreType(aInstance);
|
||||
Message *message = instance.Get<ot::Ip6::Ip6>().NewMessage(0);
|
||||
Message *message = instance.Get<ot::Ip6::Ip6>().NewMessage();
|
||||
|
||||
if (message)
|
||||
{
|
||||
|
||||
@@ -87,15 +87,6 @@ Udp::Socket::Socket(Instance &aInstance, ReceiveHandler aHandler, void *aContext
|
||||
mContext = aContext;
|
||||
}
|
||||
|
||||
Message *Udp::Socket::NewMessage(void) { return NewMessage(0); }
|
||||
|
||||
Message *Udp::Socket::NewMessage(uint16_t aReserved) { return NewMessage(aReserved, Message::Settings::GetDefault()); }
|
||||
|
||||
Message *Udp::Socket::NewMessage(uint16_t aReserved, const Message::Settings &aSettings)
|
||||
{
|
||||
return Get<Udp>().NewMessage(aReserved, aSettings);
|
||||
}
|
||||
|
||||
Error Udp::Socket::Open(NetifIdentifier aNetifId) { return Get<Udp>().Open(*this, aNetifId, mHandler, mContext); }
|
||||
|
||||
bool Udp::Socket::IsOpen(void) const { return Get<Udp>().IsOpen(*this); }
|
||||
@@ -405,15 +396,6 @@ uint16_t Udp::GetEphemeralPort(void)
|
||||
return mEphemeralPort;
|
||||
}
|
||||
|
||||
Message *Udp::NewMessage(void) { return NewMessage(0); }
|
||||
|
||||
Message *Udp::NewMessage(uint16_t aReserved) { return NewMessage(aReserved, Message::Settings::GetDefault()); }
|
||||
|
||||
Message *Udp::NewMessage(uint16_t aReserved, const Message::Settings &aSettings)
|
||||
{
|
||||
return Get<Ip6>().NewMessage(sizeof(Header) + aReserved, aSettings);
|
||||
}
|
||||
|
||||
Error Udp::SendDatagram(Message &aMessage, MessageInfo &aMessageInfo)
|
||||
{
|
||||
Error error = kErrorNone;
|
||||
|
||||
+5
-54
@@ -44,6 +44,7 @@
|
||||
#include "common/clearable.hpp"
|
||||
#include "common/linked_list.hpp"
|
||||
#include "common/locator.hpp"
|
||||
#include "common/message_allocator.hpp"
|
||||
#include "common/non_copyable.hpp"
|
||||
#include "net/ip6_headers.hpp"
|
||||
|
||||
@@ -79,7 +80,7 @@ enum NetifIdentifier : uint8_t
|
||||
/**
|
||||
* Implements core UDP message handling.
|
||||
*/
|
||||
class Udp : public InstanceLocator, private NonCopyable
|
||||
class Udp : public InstanceLocator, public MessageAllocator<Udp, ReservedHeaderSize::kUdpMessage>, private NonCopyable
|
||||
{
|
||||
public:
|
||||
typedef UdpHeader Header; ///< UDP header.
|
||||
@@ -175,7 +176,9 @@ public:
|
||||
/**
|
||||
* Implements a UDP/IPv6 socket.
|
||||
*/
|
||||
class Socket : public InstanceLocator, public SocketHandle
|
||||
class Socket : public InstanceLocator,
|
||||
public SocketHandle,
|
||||
public MessageAllocator<Socket, ReservedHeaderSize::kUdpMessage>
|
||||
{
|
||||
friend class Udp;
|
||||
|
||||
@@ -189,32 +192,6 @@ public:
|
||||
*/
|
||||
Socket(Instance &aInstance, ReceiveHandler aHandler, void *aContext);
|
||||
|
||||
/**
|
||||
* Returns a new UDP message with default settings (link security enabled and `kPriorityNormal`)
|
||||
*
|
||||
* @returns A pointer to the message or `nullptr` if no buffers are available.
|
||||
*/
|
||||
Message *NewMessage(void);
|
||||
|
||||
/**
|
||||
* Returns a new UDP message with default settings (link security enabled and `kPriorityNormal`)
|
||||
*
|
||||
* @param[in] aReserved The number of header bytes to reserve after the UDP header.
|
||||
*
|
||||
* @returns A pointer to the message or `nullptr` if no buffers are available.
|
||||
*/
|
||||
Message *NewMessage(uint16_t aReserved);
|
||||
|
||||
/**
|
||||
* Returns a new UDP message with sufficient header space reserved.
|
||||
*
|
||||
* @param[in] aReserved The number of header bytes to reserve after the UDP header.
|
||||
* @param[in] aSettings The message settings (default is used if not provided).
|
||||
*
|
||||
* @returns A pointer to the message or `nullptr` if no buffers are available.
|
||||
*/
|
||||
Message *NewMessage(uint16_t aReserved, const Message::Settings &aSettings);
|
||||
|
||||
/**
|
||||
* Opens the UDP socket.
|
||||
*
|
||||
@@ -492,32 +469,6 @@ public:
|
||||
*/
|
||||
uint16_t GetEphemeralPort(void);
|
||||
|
||||
/**
|
||||
* Returns a new UDP message with default settings (link security enabled and `kPriorityNormal`)
|
||||
*
|
||||
* @returns A pointer to the message or `nullptr` if no buffers are available.
|
||||
*/
|
||||
Message *NewMessage(void);
|
||||
|
||||
/**
|
||||
* Returns a new UDP message with default settings (link security enabled and `kPriorityNormal`)
|
||||
*
|
||||
* @param[in] aReserved The number of header bytes to reserve after the UDP header.
|
||||
*
|
||||
* @returns A pointer to the message or `nullptr` if no buffers are available.
|
||||
*/
|
||||
Message *NewMessage(uint16_t aReserved);
|
||||
|
||||
/**
|
||||
* Returns a new UDP message with sufficient header space reserved.
|
||||
*
|
||||
* @param[in] aReserved The number of header bytes to reserve after the UDP header.
|
||||
* @param[in] aSettings The message settings.
|
||||
*
|
||||
* @returns A pointer to the message or `nullptr` if no buffers are available.
|
||||
*/
|
||||
Message *NewMessage(uint16_t aReserved, const Message::Settings &aSettings);
|
||||
|
||||
/**
|
||||
* Sends an IPv6 datagram.
|
||||
*
|
||||
|
||||
@@ -2623,7 +2623,7 @@ void Mle::InformPreviousParent(void)
|
||||
Message *message = nullptr;
|
||||
Ip6::MessageInfo messageInfo;
|
||||
|
||||
VerifyOrExit((message = Get<Ip6::Ip6>().NewMessage(0)) != nullptr, error = kErrorNoBufs);
|
||||
VerifyOrExit((message = Get<Ip6::Ip6>().NewMessage()) != nullptr, error = kErrorNoBufs);
|
||||
SuccessOrExit(error = message->SetLength(0));
|
||||
|
||||
messageInfo.SetSockAddr(GetMeshLocalEid());
|
||||
@@ -3455,7 +3455,7 @@ Mle::TxMessage *Mle::NewMleMessage(Command aCommand)
|
||||
Message::Settings settings(kNoLinkSecurity, Message::kPriorityNet);
|
||||
uint8_t securitySuite;
|
||||
|
||||
message = static_cast<TxMessage *>(mSocket.NewMessage(0, settings));
|
||||
message = static_cast<TxMessage *>(mSocket.NewMessage(settings));
|
||||
VerifyOrExit(message != nullptr, error = kErrorNoBufs);
|
||||
|
||||
securitySuite = k154Security;
|
||||
|
||||
@@ -184,7 +184,7 @@ void TestUdpMessageChecksum(void)
|
||||
|
||||
for (uint16_t size = kMinSize; size <= kMaxSize; size++)
|
||||
{
|
||||
Message *message = instance->Get<Ip6::Ip6>().NewMessage(sizeof(Ip6::Udp::Header));
|
||||
Message *message = instance->Get<Ip6::Ip6>().NewMessage();
|
||||
Ip6::Udp::Header udpHeader;
|
||||
Ip6::MessageInfo messageInfo;
|
||||
|
||||
@@ -252,7 +252,7 @@ void TestIcmp6MessageChecksum(void)
|
||||
|
||||
for (uint16_t size = kMinSize; size <= kMaxSize; size++)
|
||||
{
|
||||
Message *message = instance->Get<Ip6::Ip6>().NewMessage(sizeof(Ip6::Icmp::Header));
|
||||
Message *message = instance->Get<Ip6::Ip6>().NewMessage();
|
||||
Ip6::Icmp::Header icmp6Header;
|
||||
Ip6::MessageInfo messageInfo;
|
||||
|
||||
@@ -327,7 +327,7 @@ void TestTcp4MessageChecksum(void)
|
||||
|
||||
for (uint16_t size = kMinSize; size <= kMaxSize; size++)
|
||||
{
|
||||
Message *message = instance->Get<Ip6::Ip6>().NewMessage(sizeof(Ip4::Tcp::Header));
|
||||
Message *message = instance->Get<Ip6::Ip6>().NewMessage();
|
||||
Ip4::Tcp::Header tcpHeader;
|
||||
|
||||
VerifyOrQuit(message != nullptr, "Ip6::NewMesssage() failed");
|
||||
@@ -382,7 +382,7 @@ void TestUdp4MessageChecksum(void)
|
||||
|
||||
for (uint16_t size = kMinSize; size <= kMaxSize; size++)
|
||||
{
|
||||
Message *message = instance->Get<Ip6::Ip6>().NewMessage(sizeof(Ip4::Udp::Header));
|
||||
Message *message = instance->Get<Ip6::Ip6>().NewMessage();
|
||||
Ip4::Udp::Header udpHeader;
|
||||
|
||||
VerifyOrQuit(message != nullptr, "Ip6::NewMesssage() failed");
|
||||
@@ -427,7 +427,7 @@ void TestIcmp4MessageChecksum(void)
|
||||
"\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f\x30\x31\x32\x33\x34\x35\x36\x37";
|
||||
uint16_t kChecksumForExampleMessage = 0x5594;
|
||||
Instance *instance = static_cast<Instance *>(testInitInstance());
|
||||
Message *message = instance->Get<Ip6::Ip6>().NewMessage(sizeof(kExampleIcmpMessage));
|
||||
Message *message = instance->Get<Ip6::Ip6>().NewMessage();
|
||||
|
||||
Ip4::Address source;
|
||||
Ip4::Address dest;
|
||||
|
||||
@@ -117,7 +117,7 @@ void Verify6To4(const char *aTestName,
|
||||
uint16_t aIp4Length,
|
||||
Error aError)
|
||||
{
|
||||
Message *message = sInstance->Get<Ip6::Ip6>().NewMessage(0);
|
||||
Message *message = sInstance->Get<Ip6::Ip6>().NewMessage();
|
||||
Error error;
|
||||
|
||||
Log("- - - - - - - - - - - - - - - - - - - - - - - - - ");
|
||||
@@ -161,7 +161,7 @@ void Verify4To6(const char *aTestName,
|
||||
uint16_t aIp6Length,
|
||||
Error aError)
|
||||
{
|
||||
Message *message = sInstance->Get<Ip6::Ip6>().NewMessage(0);
|
||||
Message *message = sInstance->Get<Ip6::Ip6>().NewMessage();
|
||||
Error error;
|
||||
|
||||
Log("- - - - - - - - - - - - - - - - - - - - - - - - - ");
|
||||
|
||||
Reference in New Issue
Block a user