From 1455b612f91b6384808bdad7b13fa879ba2c3f6b Mon Sep 17 00:00:00 2001 From: Jonathan Hui Date: Wed, 11 Jan 2017 13:58:33 -0800 Subject: [PATCH] Message API cleanup. --- etc/visual-studio/libopenthread.vcxproj | 1 + .../libopenthread.vcxproj.filters | 3 + etc/visual-studio/libopenthread_k.vcxproj | 1 + .../libopenthread_k.vcxproj.filters | 3 + examples/drivers/windows/otApi/otApi.cpp | 2 +- examples/drivers/windows/otLwf/datapath.c | 6 +- .../drivers/windows/otLwf/eventprocessing.c | 4 +- include/Makefile.am | 1 - include/openthread-icmp6.h | 2 +- include/openthread-ip6.h | 2 +- include/openthread-types.h | 33 +--- include/openthread.h | 10 +- include/openthread/Makefile.am | 1 + include/openthread/coap.h | 1 + .../message.h} | 179 ++++++++--------- include/openthread/udp.h | 2 +- src/cli/cli.cpp | 8 +- src/cli/cli_udp.cpp | 10 +- src/core/Makefile.am | 1 + src/core/api/message_api.cpp | 180 ++++++++++++++++++ src/core/common/message.hpp | 2 + src/core/openthread.cpp | 134 ------------- src/ncp/ncp_base.cpp | 21 +- src/ncp/ncp_base.hpp | 2 +- src/ncp/ncp_buffer.cpp | 14 +- src/ncp/ncp_buffer.hpp | 2 +- tests/unit/test_message_queue.cpp | 2 +- 27 files changed, 323 insertions(+), 304 deletions(-) rename include/{openthread-message.h => openthread/message.h} (74%) create mode 100644 src/core/api/message_api.cpp diff --git a/etc/visual-studio/libopenthread.vcxproj b/etc/visual-studio/libopenthread.vcxproj index fb25856c4..576a94784 100644 --- a/etc/visual-studio/libopenthread.vcxproj +++ b/etc/visual-studio/libopenthread.vcxproj @@ -56,6 +56,7 @@ + diff --git a/etc/visual-studio/libopenthread.vcxproj.filters b/etc/visual-studio/libopenthread.vcxproj.filters index 7830899bc..e33b0aa8f 100644 --- a/etc/visual-studio/libopenthread.vcxproj.filters +++ b/etc/visual-studio/libopenthread.vcxproj.filters @@ -63,6 +63,9 @@ Source Files\api + + Source Files\api + Source Files\api diff --git a/etc/visual-studio/libopenthread_k.vcxproj b/etc/visual-studio/libopenthread_k.vcxproj index 13fe8e6b9..819ae8c2f 100644 --- a/etc/visual-studio/libopenthread_k.vcxproj +++ b/etc/visual-studio/libopenthread_k.vcxproj @@ -64,6 +64,7 @@ + diff --git a/etc/visual-studio/libopenthread_k.vcxproj.filters b/etc/visual-studio/libopenthread_k.vcxproj.filters index 563d77688..d932b3f7f 100644 --- a/etc/visual-studio/libopenthread_k.vcxproj.filters +++ b/etc/visual-studio/libopenthread_k.vcxproj.filters @@ -63,6 +63,9 @@ Source Files\api + + Source Files\api + Source Files\api diff --git a/examples/drivers/windows/otApi/otApi.cpp b/examples/drivers/windows/otApi/otApi.cpp index 393d7ec90..f9e796669 100644 --- a/examples/drivers/windows/otApi/otApi.cpp +++ b/examples/drivers/windows/otApi/otApi.cpp @@ -3164,7 +3164,7 @@ otGetMacCounters( OTAPI void OTCALL -otGetMessageBufferInfo( +otMessageGetBufferInfo( _In_ otInstance *, _Out_ otBufferInfo *aBufferInfo ) diff --git a/examples/drivers/windows/otLwf/datapath.c b/examples/drivers/windows/otLwf/datapath.c index e9c118e25..63146a8ee 100644 --- a/examples/drivers/windows/otLwf/datapath.c +++ b/examples/drivers/windows/otLwf/datapath.c @@ -425,7 +425,7 @@ otLwfReceiveIp6DatagramCallback( ) { PMS_FILTER pFilter = (PMS_FILTER)aContext; - uint16_t messageLength = otGetMessageLength(aMessage); + uint16_t messageLength = otMessageGetLength(aMessage); PNET_BUFFER_LIST NetBufferList = NULL; PNET_BUFFER NetBuffer = NULL; NDIS_STATUS Status = NDIS_STATUS_SUCCESS; @@ -487,7 +487,7 @@ otLwfReceiveIp6DatagramCallback( } // Read the bytes to the buffer - BytesRead = otReadMessage(aMessage, 0, DataBuffer, messageLength); + BytesRead = otMessageRead(aMessage, 0, DataBuffer, messageLength); NT_ASSERT(BytesRead == (int)messageLength); if (BytesRead != (int)messageLength) { @@ -563,7 +563,7 @@ otLwfReceiveIp6DatagramCallback( error: - otFreeMessage(aMessage); + otMessageFree(aMessage); } // Called in response to receiving a Spinel Ip6 packet command diff --git a/examples/drivers/windows/otLwf/eventprocessing.c b/examples/drivers/windows/otLwf/eventprocessing.c index b46ff4ef1..519b35a2c 100644 --- a/examples/drivers/windows/otLwf/eventprocessing.c +++ b/examples/drivers/windows/otLwf/eventprocessing.c @@ -908,11 +908,11 @@ otLwfEventWorkerThread( if (message) { // Write to the message - error = otAppendMessage(message, MessageBuffer, (uint16_t)NET_BUFFER_DATA_LENGTH(CurrNb)); + error = otMessageAppend(message, MessageBuffer, (uint16_t)NET_BUFFER_DATA_LENGTH(CurrNb)); if (error != kThreadError_None) { LogError(DRIVER_DATA_PATH, "otAppendMessage failed with %!otError!", error); - otFreeMessage(message); + otMessageFree(message); } else { diff --git a/include/Makefile.am b/include/Makefile.am index 4350aa4e1..03873a8b7 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -69,7 +69,6 @@ include_HEADERS = \ openthread-icmp6.h \ openthread-ip6.h \ openthread-jam-detection.h \ - openthread-message.h \ openthread-types.h \ $(NULL) diff --git a/include/openthread-icmp6.h b/include/openthread-icmp6.h index 64b69c338..9e1fe569b 100644 --- a/include/openthread-icmp6.h +++ b/include/openthread-icmp6.h @@ -36,7 +36,7 @@ #define OPENTHREAD_ICMP6_H_ #include -#include +#include #ifdef __cplusplus extern "C" { diff --git a/include/openthread-ip6.h b/include/openthread-ip6.h index 0d9030286..6a720d8d6 100644 --- a/include/openthread-ip6.h +++ b/include/openthread-ip6.h @@ -35,7 +35,7 @@ #ifndef OPENTHREAD_IP6_H_ #define OPENTHREAD_IP6_H_ -#include +#include "openthread/message.h" #include #ifdef __cplusplus diff --git a/include/openthread-types.h b/include/openthread-types.h index 00a4133f5..5680b4b0f 100644 --- a/include/openthread-types.h +++ b/include/openthread-types.h @@ -344,6 +344,11 @@ typedef struct otMessageInfo const void *mLinkInfo; ///< A pointer to link-specific information. } otMessageInfo; +/** + * This type points to an OpenThread message buffer. + */ +typedef void *otMessage; + /** * @addtogroup commands Commands * @@ -974,34 +979,6 @@ typedef struct uint16_t mSecretKeyLength; ///< Secret key length in bytes. Should be at least 16 bytes == 128 bits. } otSemanticallyOpaqueIidGeneratorData; -/** - * @addtogroup messages Message Buffers - * - * @brief - * This module includes functions that manipulate OpenThread message buffers - * - * @{ - * - */ - -/** - * This type points to an OpenThread message buffer. - */ -typedef void *otMessage; - -/** - * This structure represents an OpenThread message queue. - */ -typedef struct -{ - void *mData; ///< Opaque data used by the implementation. -} otMessageQueue; - -/** - * @} - * - */ - /** * @addtogroup icmp6 ICMPv6 * diff --git a/include/openthread.h b/include/openthread.h index aebb6810a..e392dff36 100644 --- a/include/openthread.h +++ b/include/openthread.h @@ -37,6 +37,7 @@ #include +#include "openthread/message.h" #include "openthread/tasklet.h" #ifdef __cplusplus @@ -2004,15 +2005,6 @@ OTAPI ThreadError OTCALL otSendDiagnosticReset(otInstance *aInstance, const otIp */ OTAPI const otMacCounters *OTCALL otGetMacCounters(otInstance *aInstance); -/** - * Get the Message Buffer information. - * - * @param[in] aInstance A pointer to the OpenThread instance. - * @param[out] aBufferInfo A pointer where the message buffer information is written. - * - */ -OTAPI void OTCALL otGetMessageBufferInfo(otInstance *aInstance, otBufferInfo *aBufferInfo); - /** * @} * diff --git a/include/openthread/Makefile.am b/include/openthread/Makefile.am index 6d9d29ef1..53ac70f64 100644 --- a/include/openthread/Makefile.am +++ b/include/openthread/Makefile.am @@ -30,6 +30,7 @@ include $(abs_top_nlbuild_autotools_dir)/automake/pre.am openthread_headers = \ coap.h \ + message.h \ tasklet.h \ udp.h \ $(NULL) diff --git a/include/openthread/coap.h b/include/openthread/coap.h index a3282b4c6..7e787b98e 100644 --- a/include/openthread/coap.h +++ b/include/openthread/coap.h @@ -44,6 +44,7 @@ #include #include "openthread-types.h" +#include "openthread/message.h" #ifdef __cplusplus extern "C" { diff --git a/include/openthread-message.h b/include/openthread/message.h similarity index 74% rename from include/openthread-message.h rename to include/openthread/message.h index 345e5184c..c04710bc0 100644 --- a/include/openthread-message.h +++ b/include/openthread/message.h @@ -35,32 +35,12 @@ #ifndef OPENTHREAD_MESSAGE_H_ #define OPENTHREAD_MESSAGE_H_ -#include +#include "openthread-types.h" #ifdef __cplusplus extern "C" { #endif -/** - * @defgroup api API - * @brief - * This module includes the application programming interface to the OpenThread stack. - * - * @{ - * - * @defgroup execution Execution - * @defgroup commands Commands - * @defgroup config Configuration - * @defgroup diags Diagnostics - * @defgroup messages Message Buffers - * @defgroup ip6 IPv6 - * @defgroup udp UDP - * @defgroup coap CoAP - * - * @} - * - */ - /** * @addtogroup messages Message Buffers * @@ -78,16 +58,15 @@ extern "C" { * * @retval kThreadErrorNone Successfully freed the message buffer. * - * @sa otNewUdpMessage - * @sa otAppendMessage - * @sa otGetMessageLength - * @sa otSetMessageLength - * @sa otGetMessageOffset - * @sa otSetMessageOffset - * @sa otReadMessage - * @sa otWriteMessage + * @sa otMessageAppend + * @sa otMessageGetLength + * @sa otMessageSetLength + * @sa otMessageGetOffset + * @sa otMessageSetOffset + * @sa otMessageRead + * @sa otMessageWrite */ -ThreadError otFreeMessage(otMessage aMessage); +ThreadError otMessageFree(otMessage aMessage); /** * Get the message length in bytes. @@ -96,17 +75,16 @@ ThreadError otFreeMessage(otMessage aMessage); * * @returns The message length in bytes. * - * @sa otNewUdpMessage - * @sa otFreeMessage - * @sa otAppendMessage - * @sa otSetMessageLength - * @sa otGetMessageOffset - * @sa otSetMessageOffset - * @sa otReadMessage - * @sa otWriteMessage - * @sa otSetMessageLength + * @sa otMessageFree + * @sa otMessageAppend + * @sa otMessageSetLength + * @sa otMessageGetOffset + * @sa otMessageSetOffset + * @sa otMessageRead + * @sa otMessageWrite + * @sa otMessageSetLength */ -uint16_t otGetMessageLength(otMessage aMessage); +uint16_t otMessageGetLength(otMessage aMessage); /** * Set the message length in bytes. @@ -117,16 +95,15 @@ uint16_t otGetMessageLength(otMessage aMessage); * @retval kThreadErrorNone Successfully set the message length. * @retval kThreadErrorNoBufs No available buffers to grow the message. * - * @sa otNewUdpMessage - * @sa otFreeMessage - * @sa otAppendMessage - * @sa otGetMessageLength - * @sa otGetMessageOffset - * @sa otSetMessageOffset - * @sa otReadMessage - * @sa otWriteMessage + * @sa otMessageFree + * @sa otMessageAppend + * @sa otMessageGetLength + * @sa otMessageGetOffset + * @sa otMessageSetOffset + * @sa otMessageRead + * @sa otMessageWrite */ -ThreadError otSetMessageLength(otMessage aMessage, uint16_t aLength); +ThreadError otMessageSetLength(otMessage aMessage, uint16_t aLength); /** * Get the message offset in bytes. @@ -135,16 +112,15 @@ ThreadError otSetMessageLength(otMessage aMessage, uint16_t aLength); * * @returns The message offset value. * - * @sa otNewUdpMessage - * @sa otFreeMessage - * @sa otAppendMessage - * @sa otGetMessageLength - * @sa otSetMessageLength - * @sa otSetMessageOffset - * @sa otReadMessage - * @sa otWriteMessage + * @sa otMessageFree + * @sa otMessageAppend + * @sa otMessageGetLength + * @sa otMessageSetLength + * @sa otMessageSetOffset + * @sa otMessageRead + * @sa otMessageWrite */ -uint16_t otGetMessageOffset(otMessage aMessage); +uint16_t otMessageGetOffset(otMessage aMessage); /** * Set the message offset in bytes. @@ -155,16 +131,15 @@ uint16_t otGetMessageOffset(otMessage aMessage); * @retval kThreadErrorNone Successfully set the message offset. * @retval kThreadErrorInvalidArg The offset is beyond the message length. * - * @sa otNewUdpMessage - * @sa otFreeMessage - * @sa otAppendMessage - * @sa otGetMessageLength - * @sa otSetMessageLength - * @sa otGetMessageOffset - * @sa otReadMessage - * @sa otWriteMessage + * @sa otMessageFree + * @sa otMessageAppend + * @sa otMessageGetLength + * @sa otMessageSetLength + * @sa otMessageGetOffset + * @sa otMessageRead + * @sa otMessageWrite */ -ThreadError otSetMessageOffset(otMessage aMessage, uint16_t aOffset); +ThreadError otMessageSetOffset(otMessage aMessage, uint16_t aOffset); /** * This function indicates whether or not link security is enabled for the message. @@ -175,7 +150,7 @@ ThreadError otSetMessageOffset(otMessage aMessage, uint16_t aOffset); * @retval FALSE If link security is not enabled. * */ -bool otIsMessageLinkSecurityEnabled(otMessage aMessage); +bool otMessageIsLinkSecurityEnabled(otMessage aMessage); /** * This function sets/forces the message to be forwarded using direct transmission. @@ -198,16 +173,15 @@ void otMessageSetDirectTransmission(otMessage aMessage, bool aEnabled); * @retval kThreadErrorNone Successfully appended to the message * @retval kThreadErrorNoBufs No available buffers to grow the message. * - * @sa otNewUdpMessage - * @sa otFreeMessage - * @sa otGetMessageLength - * @sa otSetMessageLength - * @sa otGetMessageOffset - * @sa otSetMessageOffset - * @sa otReadMessage - * @sa otWriteMessage + * @sa otMessageFree + * @sa otMessageGetLength + * @sa otMessageSetLength + * @sa otMessageGetOffset + * @sa otMessageSetOffset + * @sa otMessageRead + * @sa otMessageWrite */ -ThreadError otAppendMessage(otMessage aMessage, const void *aBuf, uint16_t aLength); +ThreadError otMessageAppend(otMessage aMessage, const void *aBuf, uint16_t aLength); /** * Read bytes from a message. @@ -219,16 +193,15 @@ ThreadError otAppendMessage(otMessage aMessage, const void *aBuf, uint16_t aLeng * * @returns The number of bytes read. * - * @sa otNewUdpMessage - * @sa otFreeMessage - * @sa otAppendMessage - * @sa otGetMessageLength - * @sa otSetMessageLength - * @sa otGetMessageOffset - * @sa otSetMessageOffset - * @sa otWriteMessage + * @sa otMessageFree + * @sa otMessageAppend + * @sa otMessageGetLength + * @sa otMessageSetLength + * @sa otMessageGetOffset + * @sa otMessageSetOffset + * @sa otMessageWrite */ -int otReadMessage(otMessage aMessage, uint16_t aOffset, void *aBuf, uint16_t aLength); +int otMessageRead(otMessage aMessage, uint16_t aOffset, void *aBuf, uint16_t aLength); /** * Write bytes to a message. @@ -240,16 +213,23 @@ int otReadMessage(otMessage aMessage, uint16_t aOffset, void *aBuf, uint16_t aLe * * @returns The number of bytes written. * - * @sa otNewUdpMessage - * @sa otFreeMessage - * @sa otAppendMessage - * @sa otGetMessageLength - * @sa otSetMessageLength - * @sa otGetMessageOffset - * @sa otSetMessageOffset - * @sa otReadMessage + * @sa otMessageFree + * @sa otMessageAppend + * @sa otMessageGetLength + * @sa otMessageSetLength + * @sa otMessageGetOffset + * @sa otMessageSetOffset + * @sa otMessageRead */ -int otWriteMessage(otMessage aMessage, uint16_t aOffset, const void *aBuf, uint16_t aLength); +int otMessageWrite(otMessage aMessage, uint16_t aOffset, const void *aBuf, uint16_t aLength); + +/** + * This structure represents an OpenThread message queue. + */ +typedef struct +{ + void *mData; ///< Opaque data used by the implementation. +} otMessageQueue; /** * @@ -310,6 +290,15 @@ otMessage otMessageQueueGetHead(otMessageQueue *aQueue); */ otMessage otMessageQueueGetNext(otMessageQueue *aQueue, const otMessage aMessage); +/** + * Get the Message Buffer information. + * + * @param[in] aInstance A pointer to the OpenThread instance. + * @param[out] aBufferInfo A pointer where the message buffer information is written. + * + */ +OTAPI void OTCALL otMessageGetBufferInfo(otInstance *aInstance, otBufferInfo *aBufferInfo); + /** * @} * diff --git a/include/openthread/udp.h b/include/openthread/udp.h index 4948cd21f..fea12e193 100644 --- a/include/openthread/udp.h +++ b/include/openthread/udp.h @@ -36,7 +36,7 @@ #define OPENTHREAD_UDP_H_ #include "openthread-types.h" -#include "openthread-message.h" +#include "openthread/message.h" #ifdef __cplusplus extern "C" { diff --git a/src/cli/cli.cpp b/src/cli/cli.cpp index 9b7931a15..c360a6230 100644 --- a/src/cli/cli.cpp +++ b/src/cli/cli.cpp @@ -412,7 +412,7 @@ void Interpreter::ProcessBufferInfo(int argc, char *argv[]) (void)argc; (void)argv; - otGetMessageBufferInfo(mInstance, &bufferInfo); + otMessageGetBufferInfo(mInstance, &bufferInfo); sServer->OutputFormat("total: %d\r\n", bufferInfo.mTotalBuffers); sServer->OutputFormat("free: %d\r\n", bufferInfo.mFreeBuffers); @@ -1443,8 +1443,8 @@ void Interpreter::HandlePingTimer() const otMessageInfo *messageInfo = static_cast(&sMessageInfo); VerifyOrExit((message = otNewIp6Message(mInstance, true)) != NULL, error = kThreadError_NoBufs); - SuccessOrExit(error = otAppendMessage(message, ×tamp, sizeof(timestamp))); - SuccessOrExit(error = otSetMessageLength(message, sLength)); + SuccessOrExit(error = otMessageAppend(message, ×tamp, sizeof(timestamp))); + SuccessOrExit(error = otMessageSetLength(message, sLength)); SuccessOrExit(error = otIcmp6SendEchoRequest(mInstance, message, messageInfo, 1)); sCount--; @@ -1453,7 +1453,7 @@ exit: if (error != kThreadError_None && message != NULL) { - otFreeMessage(message); + otMessageFree(message); } if (sCount) diff --git a/src/cli/cli_udp.cpp b/src/cli/cli_udp.cpp index 8466f3621..001fb4d8c 100644 --- a/src/cli/cli_udp.cpp +++ b/src/cli/cli_udp.cpp @@ -72,11 +72,11 @@ void Udp::HandleUdpReceive(void *aContext, otMessage aMessage, const otMessageIn void Udp::HandleUdpReceive(otMessage aMessage, const otMessageInfo *aMessageInfo) { - uint16_t payloadLength = otGetMessageLength(aMessage) - otGetMessageOffset(aMessage); + uint16_t payloadLength = otMessageGetLength(aMessage) - otMessageGetOffset(aMessage); char buf[512]; VerifyOrExit(payloadLength <= sizeof(buf), ;); - otReadMessage(aMessage, otGetMessageOffset(aMessage), buf, payloadLength); + otMessageRead(aMessage, otMessageGetOffset(aMessage), buf, payloadLength); if (buf[payloadLength - 1] == '\n') { @@ -102,15 +102,15 @@ int Udp::Output(const char *aBuf, uint16_t aBufLength) otMessage message; VerifyOrExit((message = otUdpNewMessage(mInstance, true)) != NULL, error = kThreadError_NoBufs); - SuccessOrExit(error = otSetMessageLength(message, aBufLength)); - otWriteMessage(message, 0, aBuf, aBufLength); + SuccessOrExit(error = otMessageSetLength(message, aBufLength)); + otMessageWrite(message, 0, aBuf, aBufLength); SuccessOrExit(error = otUdpSend(&mSocket, message, &mPeer)); exit: if (error != kThreadError_None && message != NULL) { - otFreeMessage(message); + otMessageFree(message); aBufLength = 0; } diff --git a/src/core/Makefile.am b/src/core/Makefile.am index 02fcd4840..9b2bc39e9 100644 --- a/src/core/Makefile.am +++ b/src/core/Makefile.am @@ -40,6 +40,7 @@ CPPFLAGS_COMMON = \ SOURCES_COMMON = \ openthread.cpp \ + api/message_api.cpp \ api/tasklet_api.cpp \ api/udp_api.cpp \ coap/coap_base.cpp \ diff --git a/src/core/api/message_api.cpp b/src/core/api/message_api.cpp new file mode 100644 index 000000000..c0da88a05 --- /dev/null +++ b/src/core/api/message_api.cpp @@ -0,0 +1,180 @@ +/* + * Copyright (c) 2016, 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 implements the OpenThread Message API. + */ + +#include "openthread/message.h" + +#include "openthread-instance.h" + +using namespace Thread; + +#ifdef __cplusplus +extern "C" { +#endif + +ThreadError otMessageFree(otMessage aMessage) +{ + return static_cast(aMessage)->Free(); +} + +uint16_t otMessageGetLength(otMessage aMessage) +{ + Message *message = static_cast(aMessage); + return message->GetLength(); +} + +ThreadError otMessageSetLength(otMessage aMessage, uint16_t aLength) +{ + Message *message = static_cast(aMessage); + return message->SetLength(aLength); +} + +uint16_t otMessageGetOffset(otMessage aMessage) +{ + Message *message = static_cast(aMessage); + return message->GetOffset(); +} + +ThreadError otMessageSetOffset(otMessage aMessage, uint16_t aOffset) +{ + Message *message = static_cast(aMessage); + return message->SetOffset(aOffset); +} + +bool otMessageIsLinkSecurityEnabled(otMessage aMessage) +{ + Message *message = static_cast(aMessage); + return message->IsLinkSecurityEnabled(); +} + +void otMessageSetDirectTransmission(otMessage aMessage, bool aEnabled) +{ + Message *message = static_cast(aMessage); + + if (aEnabled) + { + message->SetDirectTransmission(); + } + else + { + message->ClearDirectTransmission(); + } +} + +ThreadError otMessageAppend(otMessage aMessage, const void *aBuf, uint16_t aLength) +{ + Message *message = static_cast(aMessage); + return message->Append(aBuf, aLength); +} + +int otMessageRead(otMessage aMessage, uint16_t aOffset, void *aBuf, uint16_t aLength) +{ + Message *message = static_cast(aMessage); + return message->Read(aOffset, aLength, aBuf); +} + +int otMessageWrite(otMessage aMessage, uint16_t aOffset, const void *aBuf, uint16_t aLength) +{ + Message *message = static_cast(aMessage); + return message->Write(aOffset, aLength, aBuf); +} + +void otMessageQueueInit(otMessageQueue *aQueue) +{ + aQueue->mData = NULL; +} + +ThreadError otMessageQueueEnqueue(otMessageQueue *aQueue, otMessage aMessage) +{ + Message *message = static_cast(aMessage); + MessageQueue *queue = static_cast(aQueue); + return queue->Enqueue(*message); +} + +ThreadError otMessageQueueDequeue(otMessageQueue *aQueue, otMessage aMessage) +{ + Message *message = static_cast(aMessage); + MessageQueue *queue = static_cast(aQueue); + return queue->Dequeue(*message); +} + +otMessage otMessageQueueGetHead(otMessageQueue *aQueue) +{ + MessageQueue *queue = static_cast(aQueue); + return queue->GetHead(); +} + +otMessage otMessageQueueGetNext(otMessageQueue *aQueue, otMessage aMessage) +{ + Message *next; + Message *message = static_cast(aMessage); + MessageQueue *queue = static_cast(aQueue); + + VerifyOrExit(message != NULL, next = NULL); + VerifyOrExit(message->GetMessageQueue() == queue, next = NULL); + next = message->GetNext(); + +exit: + return next; +} + +void otMessageGetBufferInfo(otInstance *aInstance, otBufferInfo *aBufferInfo) +{ + aBufferInfo->mTotalBuffers = OPENTHREAD_CONFIG_NUM_MESSAGE_BUFFERS; + + aBufferInfo->mFreeBuffers = aInstance->mThreadNetif.GetIp6().mMessagePool.GetFreeBufferCount(); + + aInstance->mThreadNetif.GetMeshForwarder().GetSendQueue().GetInfo(aBufferInfo->m6loSendMessages, + aBufferInfo->m6loSendBuffers); + + aInstance->mThreadNetif.GetMeshForwarder().GetReassemblyQueue().GetInfo(aBufferInfo->m6loReassemblyMessages, + aBufferInfo->m6loReassemblyBuffers); + + aInstance->mThreadNetif.GetMeshForwarder().GetResolvingQueue().GetInfo(aBufferInfo->mArpMessages, + aBufferInfo->mArpBuffers); + + aInstance->mThreadNetif.GetIp6().GetSendQueue().GetInfo(aBufferInfo->mIp6Messages, + aBufferInfo->mIp6Buffers); + + aInstance->mThreadNetif.GetIp6().mMpl.GetBufferedMessageSet().GetInfo(aBufferInfo->mMplMessages, + aBufferInfo->mMplBuffers); + + aInstance->mThreadNetif.GetMle().GetMessageQueue().GetInfo(aBufferInfo->mMleMessages, + aBufferInfo->mMleBuffers); + + aInstance->mThreadNetif.GetCoapClient().GetRequestMessages().GetInfo(aBufferInfo->mCoapClientMessages, + aBufferInfo->mCoapClientBuffers); +} + +#ifdef __cplusplus +} // extern "C" +#endif diff --git a/src/core/common/message.hpp b/src/core/common/message.hpp index 2970e6906..371f289b3 100644 --- a/src/core/common/message.hpp +++ b/src/core/common/message.hpp @@ -43,6 +43,8 @@ #include #include +#include "openthread/message.h" + #include #include #include diff --git a/src/core/openthread.cpp b/src/core/openthread.cpp index aab4efe77..89814394e 100644 --- a/src/core/openthread.cpp +++ b/src/core/openthread.cpp @@ -955,34 +955,6 @@ const otMacCounters *otGetMacCounters(otInstance *aInstance) return &aInstance->mThreadNetif.GetMac().GetCounters(); } -void otGetMessageBufferInfo(otInstance *aInstance, otBufferInfo *aBufferInfo) -{ - aBufferInfo->mTotalBuffers = OPENTHREAD_CONFIG_NUM_MESSAGE_BUFFERS; - - aBufferInfo->mFreeBuffers = aInstance->mThreadNetif.GetIp6().mMessagePool.GetFreeBufferCount(); - - aInstance->mThreadNetif.GetMeshForwarder().GetSendQueue().GetInfo(aBufferInfo->m6loSendMessages, - aBufferInfo->m6loSendBuffers); - - aInstance->mThreadNetif.GetMeshForwarder().GetReassemblyQueue().GetInfo(aBufferInfo->m6loReassemblyMessages, - aBufferInfo->m6loReassemblyBuffers); - - aInstance->mThreadNetif.GetMeshForwarder().GetResolvingQueue().GetInfo(aBufferInfo->mArpMessages, - aBufferInfo->mArpBuffers); - - aInstance->mThreadNetif.GetIp6().GetSendQueue().GetInfo(aBufferInfo->mIp6Messages, - aBufferInfo->mIp6Buffers); - - aInstance->mThreadNetif.GetIp6().mMpl.GetBufferedMessageSet().GetInfo(aBufferInfo->mMplMessages, - aBufferInfo->mMplBuffers); - - aInstance->mThreadNetif.GetMle().GetMessageQueue().GetInfo(aBufferInfo->mMleMessages, - aBufferInfo->mMleBuffers); - - aInstance->mThreadNetif.GetCoapClient().GetRequestMessages().GetInfo(aBufferInfo->mCoapClientMessages, - aBufferInfo->mCoapClientBuffers); -} - #if OPENTHREAD_ENABLE_JAM_DETECTION ThreadError otSetJamDetectionRssiThreshold(otInstance *aInstance, int8_t aRssiThreshold) { @@ -1574,112 +1546,6 @@ otMessage otNewIp6Message(otInstance *aInstance, bool aLinkSecurityEnabled) return message; } -ThreadError otFreeMessage(otMessage aMessage) -{ - return static_cast(aMessage)->Free(); -} - -uint16_t otGetMessageLength(otMessage aMessage) -{ - Message *message = static_cast(aMessage); - return message->GetLength(); -} - -ThreadError otSetMessageLength(otMessage aMessage, uint16_t aLength) -{ - Message *message = static_cast(aMessage); - return message->SetLength(aLength); -} - -uint16_t otGetMessageOffset(otMessage aMessage) -{ - Message *message = static_cast(aMessage); - return message->GetOffset(); -} - -ThreadError otSetMessageOffset(otMessage aMessage, uint16_t aOffset) -{ - Message *message = static_cast(aMessage); - return message->SetOffset(aOffset); -} - -bool otIsMessageLinkSecurityEnabled(otMessage aMessage) -{ - Message *message = static_cast(aMessage); - return message->IsLinkSecurityEnabled(); -} - -void otMessageSetDirectTransmission(otMessage aMessage, bool aEnabled) -{ - Message *message = static_cast(aMessage); - - if (aEnabled) - { - message->SetDirectTransmission(); - } - else - { - message->ClearDirectTransmission(); - } -} - -ThreadError otAppendMessage(otMessage aMessage, const void *aBuf, uint16_t aLength) -{ - Message *message = static_cast(aMessage); - return message->Append(aBuf, aLength); -} - -int otReadMessage(otMessage aMessage, uint16_t aOffset, void *aBuf, uint16_t aLength) -{ - Message *message = static_cast(aMessage); - return message->Read(aOffset, aLength, aBuf); -} - -int otWriteMessage(otMessage aMessage, uint16_t aOffset, const void *aBuf, uint16_t aLength) -{ - Message *message = static_cast(aMessage); - return message->Write(aOffset, aLength, aBuf); -} - -void otMessageQueueInit(otMessageQueue *aQueue) -{ - aQueue->mData = NULL; -} - -ThreadError otMessageQueueEnqueue(otMessageQueue *aQueue, otMessage aMessage) -{ - Message *message = static_cast(aMessage); - MessageQueue *queue = static_cast(aQueue); - return queue->Enqueue(*message); -} - -ThreadError otMessageQueueDequeue(otMessageQueue *aQueue, otMessage aMessage) -{ - Message *message = static_cast(aMessage); - MessageQueue *queue = static_cast(aQueue); - return queue->Dequeue(*message); -} - -otMessage otMessageQueueGetHead(otMessageQueue *aQueue) -{ - MessageQueue *queue = static_cast(aQueue); - return queue->GetHead(); -} - -otMessage otMessageQueueGetNext(otMessageQueue *aQueue, otMessage aMessage) -{ - Message *next; - Message *message = static_cast(aMessage); - MessageQueue *queue = static_cast(aQueue); - - VerifyOrExit(message != NULL, next = NULL); - VerifyOrExit(message->GetMessageQueue() == queue, next = NULL); - next = message->GetNext(); - -exit: - return next; -} - bool otIcmp6IsEchoEnabled(otInstance *aInstance) { return aInstance->mIp6.mIcmp.IsEchoEnabled(); diff --git a/src/ncp/ncp_base.cpp b/src/ncp/ncp_base.cpp index 49fd54f5e..46bd9276c 100644 --- a/src/ncp/ncp_base.cpp +++ b/src/ncp/ncp_base.cpp @@ -37,6 +37,9 @@ #endif #include + +#include "openthread/message.h" + #include #include #include @@ -581,8 +584,8 @@ void NcpBase::HandleDatagramFromStack(otMessage aMessage, void *aContext) void NcpBase::HandleDatagramFromStack(otMessage aMessage) { ThreadError errorCode = kThreadError_None; - bool isSecure = otIsMessageLinkSecurityEnabled(aMessage); - uint16_t length = otGetMessageLength(aMessage); + bool isSecure = otMessageIsLinkSecurityEnabled(aMessage); + uint16_t length = otMessageGetLength(aMessage); SuccessOrExit(errorCode = OutboundFrameBegin()); @@ -612,7 +615,7 @@ exit: if (aMessage != NULL) { - otFreeMessage(aMessage); + otMessageFree(aMessage); } if (errorCode != kThreadError_None) @@ -1454,7 +1457,7 @@ exit: if (aMessage != NULL) { - otFreeMessage(aMessage); + otMessageFree(aMessage); } return errorCode; @@ -3082,7 +3085,7 @@ ThreadError NcpBase::GetPropertyHandler_MSG_BUFFER_COUNTERS(uint8_t header, spin ThreadError errorCode = kThreadError_None; otBufferInfo bufferInfo; - otGetMessageBufferInfo(mInstance, &bufferInfo); + otMessageGetBufferInfo(mInstance, &bufferInfo); SuccessOrExit(errorCode = OutboundFrameBegin()); SuccessOrExit(errorCode = OutboundFrameFeedPacked("Cii", header, SPINEL_CMD_PROP_VALUE_IS, key)); @@ -4357,7 +4360,7 @@ ThreadError NcpBase::SetPropertyHandler_STREAM_NET_INSECURE(uint8_t header, spin (void)meta_len; (void)parsedLength; - errorCode = otAppendMessage(message, frame_ptr, static_cast(frame_len)); + errorCode = otMessageAppend(message, frame_ptr, static_cast(frame_len)); } if (errorCode == kThreadError_None) @@ -4369,7 +4372,7 @@ ThreadError NcpBase::SetPropertyHandler_STREAM_NET_INSECURE(uint8_t header, spin } else if (message) { - otFreeMessage(message); + otMessageFree(message); } if (errorCode == kThreadError_None) @@ -4430,7 +4433,7 @@ ThreadError NcpBase::SetPropertyHandler_STREAM_NET(uint8_t header, spinel_prop_k (void)meta_len; (void)parsedLength; - errorCode = otAppendMessage(message, frame_ptr, static_cast(frame_len)); + errorCode = otMessageAppend(message, frame_ptr, static_cast(frame_len)); } if (errorCode == kThreadError_None) @@ -4439,7 +4442,7 @@ ThreadError NcpBase::SetPropertyHandler_STREAM_NET(uint8_t header, spinel_prop_k } else if (message) { - otFreeMessage(message); + otMessageFree(message); } if (errorCode == kThreadError_None) diff --git a/src/ncp/ncp_base.hpp b/src/ncp/ncp_base.hpp index 7e918c9cb..968426072 100644 --- a/src/ncp/ncp_base.hpp +++ b/src/ncp/ncp_base.hpp @@ -40,7 +40,7 @@ #endif #include -#include +#include "openthread/message.h" #include #include #include diff --git a/src/ncp/ncp_buffer.cpp b/src/ncp/ncp_buffer.cpp index 32d4ac90d..a0e06f600 100644 --- a/src/ncp/ncp_buffer.cpp +++ b/src/ncp/ncp_buffer.cpp @@ -81,13 +81,13 @@ void NcpFrameBuffer::Clear(void) while ((message = otMessageQueueGetHead(&mWriteFrameMessageQueue)) != NULL) { otMessageQueueDequeue(&mWriteFrameMessageQueue, message); - otFreeMessage(message); + otMessageFree(message); } while ((message = otMessageQueueGetHead(&mMessageQueue)) != NULL) { otMessageQueueDequeue(&mMessageQueue, message); - otFreeMessage(message); + otMessageFree(message); } if (!wasEmpty) @@ -252,7 +252,7 @@ void NcpFrameBuffer::InFrameDiscard(void) while ((message = otMessageQueueGetHead(&mWriteFrameMessageQueue)) != NULL) { otMessageQueueDequeue(&mWriteFrameMessageQueue, message); - otFreeMessage(message); + otMessageFree(message); } } @@ -433,10 +433,10 @@ ThreadError NcpFrameBuffer::OutFrameFillMessageBuffer(void) VerifyOrExit(mReadMessage != NULL, error = kThreadError_NotFound); - VerifyOrExit(mReadMessageOffset < otGetMessageLength(mReadMessage), error = kThreadError_NotFound); + VerifyOrExit(mReadMessageOffset < otMessageGetLength(mReadMessage), error = kThreadError_NotFound); // Read portion of current message from the offset into message buffer. - readLength = otReadMessage(mReadMessage, mReadMessageOffset, mMessageBuffer, sizeof(mMessageBuffer)); + readLength = otMessageRead(mReadMessage, mReadMessageOffset, mMessageBuffer, sizeof(mMessageBuffer)); VerifyOrExit(readLength > 0, error = kThreadError_NotFound); @@ -577,7 +577,7 @@ ThreadError NcpFrameBuffer::OutFrameRemove(void) if ((message = otMessageQueueGetHead(&mMessageQueue)) != NULL) { otMessageQueueDequeue(&mMessageQueue, message); - otFreeMessage(message); + otMessageFree(message); } } @@ -644,7 +644,7 @@ uint16_t NcpFrameBuffer::OutFrameGetLength(void) if (message != NULL) { - frameLength += otGetMessageLength(message); + frameLength += otMessageGetLength(message); } } diff --git a/src/ncp/ncp_buffer.hpp b/src/ncp/ncp_buffer.hpp index 3d8cda5b4..1957fdf73 100644 --- a/src/ncp/ncp_buffer.hpp +++ b/src/ncp/ncp_buffer.hpp @@ -34,7 +34,7 @@ #define NCP_FRAME_BUFFER_HPP_ #include -#include +#include "openthread/message.h" namespace Thread { diff --git a/tests/unit/test_message_queue.cpp b/tests/unit/test_message_queue.cpp index 490a339ae..e02d361fb 100644 --- a/tests/unit/test_message_queue.cpp +++ b/tests/unit/test_message_queue.cpp @@ -29,7 +29,7 @@ #include "test_util.h" #include -#include +#include "openthread/message.h" #include #include