diff --git a/examples/drivers/windows/otLwf/eventprocessing.c b/examples/drivers/windows/otLwf/eventprocessing.c index 06dd4fbca..9bbc743b2 100644 --- a/examples/drivers/windows/otLwf/eventprocessing.c +++ b/examples/drivers/windows/otLwf/eventprocessing.c @@ -904,7 +904,7 @@ otLwfEventWorkerThread( otError error = OT_ERROR_NONE; // Create a new message - otMessage *message = otIp6NewMessage(pFilter->otCtx, TRUE); + otMessage *message = otIp6NewMessage(pFilter->otCtx, NULL); if (message) { // Write to the message diff --git a/include/openthread/coap.h b/include/openthread/coap.h index 5697b826a..a9f76dec8 100644 --- a/include/openthread/coap.h +++ b/include/openthread/coap.h @@ -485,13 +485,17 @@ const otCoapOption *otCoapHeaderGetNextOption(otCoapHeader *aHeader); /** * This function creates a new message with a CoAP header. * - * @param[in] aInstance A pointer to an OpenThread instance. - * @param[in] aHeader A pointer to a CoAP header that is used to create the message. + * @note If @p aSettings is 'NULL', the link layer security is enabled and the message priority is set to + * OT_MESSAGE_PRIORITY_NORMAL by default. * - * @returns A pointer to the message or NULL if failed to allocate message. + * @param[in] aInstance A pointer to an OpenThread instance. + * @param[in] aHeader A pointer to a CoAP header that is used to create the message. + * @param[in] aSettings A pointer to the message settings or NULL to set default settings. + * + * @returns A pointer to the message buffer or NULL if no message buffers are available or parameters are invalid. * */ -otMessage *otCoapNewMessage(otInstance *aInstance, const otCoapHeader *aHeader); +otMessage *otCoapNewMessage(otInstance *aInstance, const otCoapHeader *aHeader, const otMessageSettings *aSettings); /** * This function sends a CoAP request. diff --git a/include/openthread/ip6.h b/include/openthread/ip6.h index 4799ec8c6..7a0e7b6c4 100644 --- a/include/openthread/ip6.h +++ b/include/openthread/ip6.h @@ -370,14 +370,17 @@ otError otIp6CreateSemanticallyOpaqueIid(otInstance *aInstance, otNetifAddress * /** * Allocate a new message buffer for sending an IPv6 message. * - * @param[in] aInstance A pointer to an OpenThread instance. - * @param[in] aLinkSecurityEnabled TRUE if the message should be secured at Layer 2 + * @note If @p aSettings is 'NULL', the link layer security is enabled and the message priority is set to + * OT_MESSAGE_PRIORITY_NORMAL by default. * - * @returns A pointer to the message buffer or NULL if no message buffers are available. + * @param[in] aInstance A pointer to an OpenThread instance. + * @param[in] aSettings A pointer to the message settings or NULL to set default settings. + * + * @returns A pointer to the message buffer or NULL if no message buffers are available or parameters are invalid. * * @sa otFreeMessage */ -otMessage *otIp6NewMessage(otInstance *aInstance, bool aLinkSecurityEnabled); +otMessage *otIp6NewMessage(otInstance *aInstance, const otMessageSettings *aSettings); /** * This function pointer is called when an IPv6 datagram is received. diff --git a/include/openthread/message.h b/include/openthread/message.h index 62a9b3d4a..33731261c 100644 --- a/include/openthread/message.h +++ b/include/openthread/message.h @@ -88,6 +88,27 @@ typedef struct otBufferInfo uint16_t mApplicationCoapBuffers; ///< The number of buffers in the application CoAP send queue. } otBufferInfo; +/** + * This enumeration defines the Openthread message priority levels. + * + */ +typedef enum otMessagePriority +{ + OT_MESSAGE_PRIORITY_LOW = 0, ///< Low priority level. + OT_MESSAGE_PRIORITY_NORMAL = 1, ///< Normal priority level. + OT_MESSAGE_PRIORITY_HIGH = 2, ///< High priority level. +} otMessagePriority; + +/** + * This structure represents a message settings. + * + */ +typedef struct otMessageSettings +{ + bool mLinkSecurityEnabled; ///< TRUE if the message should be secured at Layer 2. + otMessagePriority mPriority; ///< The message priority level. +} otMessageSettings; + /** * Free an allocated message buffer. * diff --git a/include/openthread/udp.h b/include/openthread/udp.h index c0bca7736..5a865cf8e 100644 --- a/include/openthread/udp.h +++ b/include/openthread/udp.h @@ -96,15 +96,18 @@ typedef struct otUdpSocket /** * Allocate a new message buffer for sending a UDP message. * - * @param[in] aInstance A pointer to an OpenThread instance. - * @param[in] aLinkSecurityEnabled TRUE if the message should be secured at Layer 2. + * @note If @p aSettings is 'NULL', the link layer security is enabled and the message priority is set to + * OT_MESSAGE_PRIORITY_NORMAL by default. * - * @returns A pointer to the message buffer or NULL if no message buffers are available. + * @param[in] aInstance A pointer to an OpenThread instance. + * @param[in] aSettings A pointer to the message settings or NULL to set default settings. + * + * @returns A pointer to the message buffer or NULL if no message buffers are available or parameters are invalid. * * @sa otFreeMessage * */ -otMessage *otUdpNewMessage(otInstance *aInstance, bool aLinkSecurityEnabled); +otMessage *otUdpNewMessage(otInstance *aInstance, const otMessageSettings *aSettings); /** * Open a UDP/IPv6 socket. diff --git a/src/cli/cli.cpp b/src/cli/cli.cpp index 5759ed7f9..a88bb3b1d 100644 --- a/src/cli/cli.cpp +++ b/src/cli/cli.cpp @@ -1849,7 +1849,7 @@ void Interpreter::HandlePingTimer() otMessage * message; const otMessageInfo *messageInfo = static_cast(&mMessageInfo); - VerifyOrExit((message = otIp6NewMessage(mInstance, true)) != NULL, error = OT_ERROR_NO_BUFS); + VerifyOrExit((message = otIp6NewMessage(mInstance, NULL)) != NULL, error = OT_ERROR_NO_BUFS); SuccessOrExit(error = otMessageAppend(message, ×tamp, sizeof(timestamp))); SuccessOrExit(error = otMessageSetLength(message, mLength)); SuccessOrExit(error = otIcmp6SendEchoRequest(mInstance, message, messageInfo, 1)); diff --git a/src/cli/cli_coap.cpp b/src/cli/cli_coap.cpp index f326a641c..0c4bc9d13 100644 --- a/src/cli/cli_coap.cpp +++ b/src/cli/cli_coap.cpp @@ -183,7 +183,7 @@ void Coap::HandleServerResponse(otCoapHeader *aHeader, otMessage *aMessage, cons otCoapHeaderSetPayloadMarker(&responseHeader); } - responseMessage = otCoapNewMessage(mInterpreter.mInstance, &responseHeader); + responseMessage = otCoapNewMessage(mInterpreter.mInstance, &responseHeader, NULL); VerifyOrExit(responseMessage != NULL, error = OT_ERROR_NO_BUFS); if (otCoapHeaderGetCode(aHeader) == OT_COAP_CODE_GET) @@ -292,7 +292,7 @@ otError Coap::ProcessRequest(int argc, char *argv[]) } } - message = otCoapNewMessage(mInterpreter.mInstance, &header); + message = otCoapNewMessage(mInterpreter.mInstance, &header, NULL); VerifyOrExit(message != NULL, error = OT_ERROR_NO_BUFS); // Embed content into message if given diff --git a/src/cli/cli_coap_secure.cpp b/src/cli/cli_coap_secure.cpp index 8bceb44f3..7ca3ad6f4 100644 --- a/src/cli/cli_coap_secure.cpp +++ b/src/cli/cli_coap_secure.cpp @@ -416,7 +416,7 @@ void CoapSecure::HandleServerResponse(otCoapHeader *aHeader, otMessage *aMessage otCoapHeaderSetPayloadMarker(&responseHeader); } - responseMessage = otCoapNewMessage(mInterpreter.mInstance, &responseHeader); + responseMessage = otCoapNewMessage(mInterpreter.mInstance, &responseHeader, NULL); VerifyOrExit(responseMessage != NULL, error = OT_ERROR_NO_BUFS); if (otCoapHeaderGetCode(aHeader) == OT_COAP_CODE_GET) @@ -530,7 +530,7 @@ otError CoapSecure::ProcessRequest(int argc, char *argv[]) } } - message = otCoapNewMessage(mInterpreter.mInstance, &header); + message = otCoapNewMessage(mInterpreter.mInstance, &header, NULL); VerifyOrExit(message != NULL, error = OT_ERROR_NO_BUFS); // add payload @@ -618,7 +618,7 @@ void CoapSecure::DefaultHandle(otCoapHeader *aHeader, otMessage *aMessage, const otCoapHeaderSetMessageId(&responseHeader, otCoapHeaderGetMessageId(aHeader)); otCoapHeaderSetToken(&responseHeader, otCoapHeaderGetToken(aHeader), otCoapHeaderGetTokenLength(aHeader)); - responseMessage = otCoapNewMessage(mInterpreter.mInstance, &responseHeader); + responseMessage = otCoapNewMessage(mInterpreter.mInstance, &responseHeader, NULL); VerifyOrExit(responseMessage != NULL, error = OT_ERROR_NO_BUFS); SuccessOrExit(error = otCoapSecureSendResponse(mInterpreter.mInstance, responseMessage, aMessageInfo)); } diff --git a/src/cli/cli_udp.cpp b/src/cli/cli_udp.cpp index 57c1a675a..323a7232b 100644 --- a/src/cli/cli_udp.cpp +++ b/src/cli/cli_udp.cpp @@ -102,7 +102,7 @@ int Udp::Output(const char *aBuf, uint16_t aBufLength) otError error = OT_ERROR_NONE; otMessage *message; - VerifyOrExit((message = otUdpNewMessage(mInstance, true)) != NULL, error = OT_ERROR_NO_BUFS); + VerifyOrExit((message = otUdpNewMessage(mInstance, NULL)) != NULL, error = OT_ERROR_NO_BUFS); SuccessOrExit(error = otMessageSetLength(message, aBufLength)); otMessageWrite(message, 0, aBuf, aBufLength); SuccessOrExit(error = otUdpSend(&mSocket, message, &mPeer)); diff --git a/src/cli/cli_udp_example.cpp b/src/cli/cli_udp_example.cpp index cd32c272a..10668d6ab 100644 --- a/src/cli/cli_udp_example.cpp +++ b/src/cli/cli_udp_example.cpp @@ -158,7 +158,7 @@ otError UdpExample::ProcessSend(int argc, char *argv[]) messageInfo.mInterfaceId = OT_NETIF_INTERFACE_ID_THREAD; } - message = otUdpNewMessage(mInterpreter.mInstance, true); + message = otUdpNewMessage(mInterpreter.mInstance, NULL); VerifyOrExit(message != NULL, error = OT_ERROR_NO_BUFS); error = otMessageAppend(message, argv[curArg], static_cast(strlen(argv[curArg]))); diff --git a/src/core/api/coap_api.cpp b/src/core/api/coap_api.cpp index 1573181c9..28d97d350 100644 --- a/src/core/api/coap_api.cpp +++ b/src/core/api/coap_api.cpp @@ -148,13 +148,20 @@ const otCoapOption *otCoapHeaderGetNextOption(otCoapHeader *aHeader) return static_cast(static_cast(aHeader)->GetNextOption()); } -otMessage *otCoapNewMessage(otInstance *aInstance, const otCoapHeader *aHeader) +otMessage *otCoapNewMessage(otInstance *aInstance, const otCoapHeader *aHeader, const otMessageSettings *aSettings) { Message * message; Instance &instance = *static_cast(aInstance); VerifyOrExit(aHeader != NULL, message = NULL); - message = instance.GetApplicationCoap().NewMessage(*(static_cast(aHeader))); + + if (aSettings != NULL) + { + VerifyOrExit(aSettings->mPriority <= OT_MESSAGE_PRIORITY_HIGH, message = NULL); + } + + message = instance.GetApplicationCoap().NewMessage(*(static_cast(aHeader)), aSettings); + exit: return message; } diff --git a/src/core/api/ip6_api.cpp b/src/core/api/ip6_api.cpp index 0277560d1..8459f6ad3 100644 --- a/src/core/api/ip6_api.cpp +++ b/src/core/api/ip6_api.cpp @@ -194,16 +194,19 @@ otError otIp6Send(otInstance *aInstance, otMessage *aMessage) return error; } -otMessage *otIp6NewMessage(otInstance *aInstance, bool aLinkSecurityEnabled) +otMessage *otIp6NewMessage(otInstance *aInstance, const otMessageSettings *aSettings) { Instance &instance = *static_cast(aInstance); - Message * message = instance.GetMessagePool().New(Message::kTypeIp6, 0); + Message * message; - if (message) + if (aSettings != NULL) { - message->SetLinkSecurityEnabled(aLinkSecurityEnabled); + VerifyOrExit(aSettings->mPriority <= OT_MESSAGE_PRIORITY_HIGH, message = NULL); } + message = instance.GetMessagePool().New(Message::kTypeIp6, 0, aSettings); + +exit: return message; } diff --git a/src/core/api/udp_api.cpp b/src/core/api/udp_api.cpp index 710ba0bd6..1a1ef9bd7 100644 --- a/src/core/api/udp_api.cpp +++ b/src/core/api/udp_api.cpp @@ -40,16 +40,19 @@ using namespace ot; -otMessage *otUdpNewMessage(otInstance *aInstance, bool aLinkSecurityEnabled) +otMessage *otUdpNewMessage(otInstance *aInstance, const otMessageSettings *aSettings) { Instance &instance = *static_cast(aInstance); - Message * message = instance.GetIp6().GetUdp().NewMessage(0); + Message * message; - if (message) + if (aSettings != NULL) { - message->SetLinkSecurityEnabled(aLinkSecurityEnabled); + VerifyOrExit(aSettings->mPriority <= OT_MESSAGE_PRIORITY_HIGH, message = NULL); } + message = instance.GetMessagePool().New(Message::kTypeIp6, 0, aSettings); + +exit: return message; } diff --git a/src/core/coap/coap.cpp b/src/core/coap/coap.cpp index 080584428..ad1931234 100644 --- a/src/core/coap/coap.cpp +++ b/src/core/coap/coap.cpp @@ -142,14 +142,14 @@ void CoapBase::SetDefaultHandler(otCoapRequestHandler aHandler, void *aContext) mDefaultHandlerContext = aContext; } -Message *CoapBase::NewMessage(const Header &aHeader, uint8_t aPriority) +Message *CoapBase::NewMessage(const Header &aHeader, const otMessageSettings *aSettings) { Message *message = NULL; // Ensure that header has minimum required length. VerifyOrExit(aHeader.GetLength() >= Header::kMinHeaderLength); - VerifyOrExit((message = mSocket.NewMessage(aHeader.GetLength(), aPriority)) != NULL); + VerifyOrExit((message = mSocket.NewMessage(aHeader.GetLength(), aSettings)) != NULL); message->Prepend(aHeader.GetBytes(), aHeader.GetLength()); message->SetOffset(0); diff --git a/src/core/coap/coap.hpp b/src/core/coap/coap.hpp index c37624b75..1d40b0dd0 100644 --- a/src/core/coap/coap.hpp +++ b/src/core/coap/coap.hpp @@ -514,13 +514,16 @@ public: /** * This method creates a new message with a CoAP header. * - * @param[in] aHeader A reference to a CoAP header that is used to create the message. - * @param[in] aPriority The message priority level. + * @note If @p aSettings is 'NULL', the link layer security is enabled and the message priority is set to + * OT_MESSAGE_PRIORITY_NORMAL by default. + * + * @param[in] aHeader A reference to a CoAP header that is used to create the message. + * @param[in] aSettings A pointer to the message settings or NULL to set default settings. * * @returns A pointer to the message or NULL if failed to allocate message. * */ - Message *NewMessage(const Header &aHeader, uint8_t aPriority = kDefaultCoapMessagePriority); + Message *NewMessage(const Header &aHeader, const otMessageSettings *aSettings = NULL); /** * This method sends a CoAP message. @@ -717,11 +720,6 @@ protected: Ip6::UdpSocket mSocket; private: - enum - { - kDefaultCoapMessagePriority = Message::kPriorityNormal, - }; - static void HandleUdpReceive(void *aContext, otMessage *aMessage, const otMessageInfo *aMessageInfo); Message *CopyAndEnqueueMessage(const Message &aMessage, uint16_t aCopyLength, const CoapMetadata &aCoapMetadata); diff --git a/src/core/common/message.cpp b/src/core/common/message.cpp index 1f056f519..ae623bc40 100644 --- a/src/core/common/message.cpp +++ b/src/core/common/message.cpp @@ -91,6 +91,32 @@ exit: return message; } +Message *MessagePool::New(uint8_t aType, uint16_t aReserved, const otMessageSettings *aSettings) +{ + Message *message; + bool linkSecurityEnabled; + uint8_t priority; + + if (aSettings == NULL) + { + linkSecurityEnabled = true; + priority = OT_MESSAGE_PRIORITY_NORMAL; + } + else + { + linkSecurityEnabled = aSettings->mLinkSecurityEnabled; + priority = aSettings->mPriority; + } + + message = New(aType, aReserved, priority); + if (message) + { + message->SetLinkSecurityEnabled(linkSecurityEnabled); + } + + return message; +} + void MessagePool::Free(Message *aMessage) { assert(aMessage->Next(MessageInfo::kListAll) == NULL && aMessage->Prev(MessageInfo::kListAll) == NULL); diff --git a/src/core/common/message.hpp b/src/core/common/message.hpp index 2cada8df7..816c09a4e 100644 --- a/src/core/common/message.hpp +++ b/src/core/common/message.hpp @@ -229,10 +229,10 @@ public: enum { - kPriorityLow = 0, ///< Low priority level. - kPriorityNormal = 1, ///< Normal priority level. - kPriorityHigh = 2, ///< High priority level. - kPriorityNet = 3, ///< Network Control priority level. + kPriorityLow = OT_MESSAGE_PRIORITY_LOW, ///< Low priority level. + kPriorityNormal = OT_MESSAGE_PRIORITY_NORMAL, ///< Normal priority level. + kPriorityHigh = OT_MESSAGE_PRIORITY_HIGH, ///< High priority level. + kPriorityNet = OT_MESSAGE_PRIORITY_HIGH + 1, ///< Network Control priority level. kNumPriorities = 4, ///< Number of priority levels. }; @@ -1250,6 +1250,21 @@ public: */ Message *New(uint8_t aType, uint16_t aReserveHeader, uint8_t aPriority = kDefaultMessagePriority); + /** + * This method is used to obtain a new message with specified settings. + * + * @note If @p aSettings is 'NULL', the link layer security is enabled and the message priority is set to + * OT_MESSAGE_PRIORITY_NORMAL by default. + * + * @param[in] aType The message type. + * @param[in] aReserveHeader The number of header bytes to reserve. + * @param[in] aSettings A pointer to the message settings or NULL to set default settings. + * + * @returns A pointer to the message or NULL if no message buffers are available. + * + */ + Message *New(uint8_t aType, uint16_t aReserveHeader, const otMessageSettings *aSettings); + /** * This method is used to free a message and return all message buffers to the buffer pool. * diff --git a/src/core/meshcop/joiner_router.cpp b/src/core/meshcop/joiner_router.cpp index 20d6b328f..cd99bf12e 100644 --- a/src/core/meshcop/joiner_router.cpp +++ b/src/core/meshcop/joiner_router.cpp @@ -227,7 +227,8 @@ void JoinerRouter::HandleRelayTransmit(Coap::Header &aHeader, Message &aMessage, JoinerRouterKekTlv kek; uint16_t offset; uint16_t length; - Message * message = NULL; + Message * message = NULL; + otMessageSettings settings = {false, static_cast(kMeshCoPMessagePriority)}; Ip6::MessageInfo messageInfo; VerifyOrExit(aHeader.GetType() == OT_COAP_TYPE_NON_CONFIRMABLE && aHeader.GetCode() == OT_COAP_CODE_POST, @@ -243,8 +244,7 @@ void JoinerRouter::HandleRelayTransmit(Coap::Header &aHeader, Message &aMessage, SuccessOrExit(error = Tlv::GetValueOffset(aMessage, Tlv::kJoinerDtlsEncapsulation, offset, length)); - VerifyOrExit((message = mSocket.NewMessage(0, kMeshCoPMessagePriority)) != NULL, error = OT_ERROR_NO_BUFS); - message->SetLinkSecurityEnabled(false); + VerifyOrExit((message = mSocket.NewMessage(0, &settings)) != NULL, error = OT_ERROR_NO_BUFS); while (length) { diff --git a/src/core/meshcop/meshcop.hpp b/src/core/meshcop/meshcop.hpp index 8dd08b199..f73b561d3 100644 --- a/src/core/meshcop/meshcop.hpp +++ b/src/core/meshcop/meshcop.hpp @@ -57,7 +57,8 @@ enum */ inline Message *NewMeshCoPMessage(Coap::CoapBase &aCoap, const Coap::Header &aHeader) { - return aCoap.NewMessage(aHeader, kMeshCoPMessagePriority); + otMessageSettings settings = {true, static_cast(kMeshCoPMessagePriority)}; + return aCoap.NewMessage(aHeader, &settings); } /** diff --git a/src/core/net/ip6.cpp b/src/core/net/ip6.cpp index 55bee5f1a..1b5c27ac4 100644 --- a/src/core/net/ip6.cpp +++ b/src/core/net/ip6.cpp @@ -67,10 +67,10 @@ Ip6::Ip6(Instance &aInstance) { } -Message *Ip6::NewMessage(uint16_t aReserved, uint8_t aPriority) +Message *Ip6::NewMessage(uint16_t aReserved, const otMessageSettings *aSettings) { return GetInstance().GetMessagePool().New( - Message::kTypeIp6, sizeof(Header) + sizeof(HopByHopHeader) + sizeof(OptionMpl) + aReserved, aPriority); + Message::kTypeIp6, sizeof(Header) + sizeof(HopByHopHeader) + sizeof(OptionMpl) + aReserved, aSettings); } uint16_t Ip6::UpdateChecksum(uint16_t aChecksum, const Address &aAddress) diff --git a/src/core/net/ip6.hpp b/src/core/net/ip6.hpp index 4e1b1e175..b5fb27161 100644 --- a/src/core/net/ip6.hpp +++ b/src/core/net/ip6.hpp @@ -110,13 +110,16 @@ public: /** * This method allocates a new message buffer from the buffer pool. * + * @note If @p aSettings is 'NULL', the link layer security is enabled and the message priority is set to + * OT_MESSAGE_PRIORITY_NORMAL by default. + * * @param[in] aReserved The number of header bytes to reserve following the IPv6 header. - * @param[in] aPriority The priority level of the message. + * @param[in] aSettings A pointer to the message settings or NULL to set default settings. * * @returns A pointer to the message or NULL if insufficient message buffers are available. * */ - Message *NewMessage(uint16_t aReserved, uint8_t aPriority = kDefaultIp6MessagePriority); + Message *NewMessage(uint16_t aReserved, const otMessageSettings *aSettings = NULL); /** * This constructor initializes the object. diff --git a/src/core/net/udp6.cpp b/src/core/net/udp6.cpp index 5bc8757dd..bd8e9efd1 100644 --- a/src/core/net/udp6.cpp +++ b/src/core/net/udp6.cpp @@ -69,9 +69,9 @@ Udp &UdpSocket::GetUdp(void) return GetInstance().GetIp6().GetUdp(); } -Message *UdpSocket::NewMessage(uint16_t aReserved, uint8_t aPriority) +Message *UdpSocket::NewMessage(uint16_t aReserved, const otMessageSettings *aSettings) { - return GetUdp().NewMessage(aReserved, aPriority); + return GetUdp().NewMessage(aReserved, aSettings); } otError UdpSocket::Open(otUdpReceive aHandler, void *aContext) @@ -299,9 +299,9 @@ uint16_t Udp::GetEphemeralPort(void) return rval; } -Message *Udp::NewMessage(uint16_t aReserved, uint8_t aPriority) +Message *Udp::NewMessage(uint16_t aReserved, const otMessageSettings *aSettings) { - return GetIp6().NewMessage(sizeof(UdpHeader) + aReserved, aPriority); + return GetIp6().NewMessage(sizeof(UdpHeader) + aReserved, aSettings); } otError Udp::SendDatagram(Message &aMessage, MessageInfo &aMessageInfo, IpProto aIpProto) diff --git a/src/core/net/udp6.hpp b/src/core/net/udp6.hpp index ec33f43f8..5475dc884 100644 --- a/src/core/net/udp6.hpp +++ b/src/core/net/udp6.hpp @@ -109,13 +109,16 @@ public: /** * This method returns a new UDP message with sufficient header space reserved. * + * @note If @p aSettings is 'NULL', the link layer security is enabled and the message priority is set to + * OT_MESSAGE_PRIORITY_NORMAL by default. + * * @param[in] aReserved The number of header bytes to reserve after the UDP header. - * @param[in] aPriority A reference to the priority of the message. + * @param[in] aSettings A pointer to the message settings or NULL to set default settings. * * @returns A pointer to the message or NULL if no buffers are available. * */ - Message *NewMessage(uint16_t aReserved, uint8_t aPriority = kDefaultSocketMessagePriority); + Message *NewMessage(uint16_t aReserved, const otMessageSettings *aSettings = NULL); /** * This method opens the UDP socket. @@ -186,11 +189,6 @@ public: SockAddr &GetPeerName(void) { return *static_cast(&mPeerName); } private: - enum - { - kDefaultSocketMessagePriority = Message::kPriorityNormal, - }; - UdpSocket *GetNext(void) { return static_cast(mNext); } void SetNext(UdpSocket *socket) { mNext = static_cast(socket); } @@ -276,7 +274,7 @@ public: * @returns A pointer to the message or NULL if no buffers are available. * */ - Message *NewMessage(uint16_t aReserved, uint8_t aPriority = kDefaultUdpMessagePriority); + Message *NewMessage(uint16_t aReserved, const otMessageSettings *aSettings = NULL); /** * This method sends an IPv6 datagram. @@ -344,11 +342,6 @@ public: #endif // OPENTHREAD_ENABLE_UDP_PROXY private: - enum - { - kDefaultUdpMessagePriority = Message::kPriorityNormal, - }; - enum { kDynamicPortMin = 49152, ///< Service Name and Transport Protocol Port Number Registry diff --git a/src/core/thread/mle.cpp b/src/core/thread/mle.cpp index 030649f3d..daff00574 100644 --- a/src/core/thread/mle.cpp +++ b/src/core/thread/mle.cpp @@ -1079,13 +1079,13 @@ void Mle::GenerateNonce(const Mac::ExtAddress &aMacAddr, Message *Mle::NewMleMessage(void) { - Message *message; + Message * message; + otMessageSettings settings = {false, static_cast(kMleMessagePriority)}; - message = mSocket.NewMessage(0, kMleMessagePriority); + message = mSocket.NewMessage(0, &settings); VerifyOrExit(message != NULL); message->SetSubType(Message::kSubTypeMleGeneral); - message->SetLinkSecurityEnabled(false); exit: return message; diff --git a/src/ncp/ncp_base_mtd.cpp b/src/ncp/ncp_base_mtd.cpp index 54ebaa534..cb3234167 100644 --- a/src/ncp/ncp_base_mtd.cpp +++ b/src/ncp/ncp_base_mtd.cpp @@ -1420,7 +1420,7 @@ template <> otError NcpBase::HandlePropertySet(void) otError error = OT_ERROR_NONE; // STREAM_NET requires layer 2 security. - message = otIp6NewMessage(mInstance, true); + message = otIp6NewMessage(mInstance, NULL); VerifyOrExit(message != NULL, error = OT_ERROR_NO_BUFS); SuccessOrExit(error = mDecoder.ReadDataWithLen(framePtr, frameLen)); @@ -2255,15 +2255,16 @@ template <> otError NcpBase::HandlePropertySet otError NcpBase::HandlePropertySet(void) { - const uint8_t *framePtr = NULL; - uint16_t frameLen = 0; - const uint8_t *metaPtr = NULL; - uint16_t metaLen = 0; - otMessage * message = NULL; - otError error = OT_ERROR_NONE; + const uint8_t * framePtr = NULL; + uint16_t frameLen = 0; + const uint8_t * metaPtr = NULL; + uint16_t metaLen = 0; + otMessage * message = NULL; + otError error = OT_ERROR_NONE; + otMessageSettings msgSettings = {false, OT_MESSAGE_PRIORITY_NORMAL}; // STREAM_NET_INSECURE packets are not secured at layer 2. - message = otIp6NewMessage(mInstance, false); + message = otIp6NewMessage(mInstance, &msgSettings); VerifyOrExit(message != NULL, error = OT_ERROR_NO_BUFS); SuccessOrExit(mDecoder.ReadDataWithLen(framePtr, frameLen)); @@ -2850,9 +2851,10 @@ template <> otError NcpBase::HandlePropertySetmNext) @@ -413,7 +415,7 @@ void platformUdpProcess(otInstance *aInstance, const fd_set *aReadFdSet) continue; } - message = otUdpNewMessage(aInstance, false); + message = otUdpNewMessage(aInstance, &msgSettings); if (message == NULL) { diff --git a/tests/fuzz/ip6_send.cpp b/tests/fuzz/ip6_send.cpp index be67e091a..675ec7ca0 100644 --- a/tests/fuzz/ip6_send.cpp +++ b/tests/fuzz/ip6_send.cpp @@ -43,10 +43,10 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { const otPanId panId = 0xdead; - otInstance *instance = NULL; - otMessage * message = NULL; - otError error = OT_ERROR_NONE; - bool isSecure; + otInstance * instance = NULL; + otMessage * message = NULL; + otError error = OT_ERROR_NONE; + otMessageSettings settings; VerifyOrExit(size > 0); @@ -58,9 +58,10 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) otThreadSetEnabled(instance, true); otThreadBecomeLeader(instance); - isSecure = (data[0] & 0x1) != 0; + settings.mLinkSecurityEnabled = (data[0] & 0x1) != 0; + settings.mPriority = OT_MESSAGE_PRIORITY_NORMAL; - message = otIp6NewMessage(instance, isSecure); + message = otIp6NewMessage(instance, &settings); VerifyOrExit(message != NULL, error = OT_ERROR_NO_BUFS); error = otMessageAppend(message, data + 1, static_cast(size - 1)); diff --git a/tests/unit/test_message_queue.cpp b/tests/unit/test_message_queue.cpp index b4c187558..4a99a5bae 100644 --- a/tests/unit/test_message_queue.cpp +++ b/tests/unit/test_message_queue.cpp @@ -236,7 +236,7 @@ void TestMessageQueueOtApis(void) for (int i = 0; i < kNumTestMessages; i++) { - msg[i] = otIp6NewMessage(sInstance, true); + msg[i] = otIp6NewMessage(sInstance, NULL); VerifyOrQuit(msg[i] != NULL, "otIp6NewMessage() failed.\n"); }