From 10e5b9a38f2c77ff16ed9e20d224fdf0643bfb74 Mon Sep 17 00:00:00 2001 From: Jonathan Hui Date: Tue, 10 Jan 2017 18:25:08 -0800 Subject: [PATCH] UDP API cleanup. --- include/Makefile.am | 1 - include/openthread-types.h | 18 --- include/openthread/Makefile.am | 1 + .../{openthread-udp.h => openthread/udp.h} | 94 ++++++++-------- src/cli/cli.hpp | 3 +- src/cli/cli_udp.cpp | 8 +- src/core/Makefile.am | 1 + src/core/api/udp_api.cpp | 103 ++++++++++++++++++ src/core/net/ip6.hpp | 3 +- src/core/net/udp6.hpp | 2 + src/core/openthread.cpp | 57 ---------- 11 files changed, 165 insertions(+), 126 deletions(-) rename include/{openthread-udp.h => openthread/udp.h} (68%) create mode 100644 src/core/api/udp_api.cpp diff --git a/include/Makefile.am b/include/Makefile.am index adb379619..f8631fdd8 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -72,7 +72,6 @@ include_HEADERS = \ openthread-message.h \ openthread-tasklet.h \ openthread-types.h \ - openthread-udp.h \ $(NULL) install-headers: install-includeHEADERS diff --git a/include/openthread-types.h b/include/openthread-types.h index d87aeb3b1..00a4133f5 100644 --- a/include/openthread-types.h +++ b/include/openthread-types.h @@ -1102,24 +1102,6 @@ typedef struct otSockAddr int8_t mScopeId; ///< An IPv6 scope identifier. } otSockAddr; -/** - * This callback allows OpenThread to inform the application of a received UDP message. - */ -typedef void (*otUdpReceive)(void *aContext, otMessage aMessage, const otMessageInfo *aMessageInfo); - -/** - * This structure represents a UDP socket. - */ -typedef struct otUdpSocket -{ - otSockAddr mSockName; ///< The local IPv6 socket address. - otSockAddr mPeerName; ///< The peer IPv6 socket address. - otUdpReceive mHandler; ///< A function pointer to the application callback. - void *mContext; ///< A pointer to application-specific context. - void *mTransport; ///< A pointer to the transport object (internal use only). - struct otUdpSocket *mNext; ///< A pointer to the next UDP socket (internal use only). -} otUdpSocket; - #ifdef OTDLL /** diff --git a/include/openthread/Makefile.am b/include/openthread/Makefile.am index da8f4b7e8..f404dff13 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 \ + udp.h \ $(NULL) openthreaddir = $(includedir)/openthread diff --git a/include/openthread-udp.h b/include/openthread/udp.h similarity index 68% rename from include/openthread-udp.h rename to include/openthread/udp.h index 7281a49d5..4948cd21f 100644 --- a/include/openthread-udp.h +++ b/include/openthread/udp.h @@ -29,38 +29,19 @@ /** * @file * @brief - * This file defines the top-level ip6 functions for the OpenThread library. + * This file defines the OpenThread UDP API. */ #ifndef OPENTHREAD_UDP_H_ #define OPENTHREAD_UDP_H_ -#include +#include "openthread-types.h" +#include "openthread-message.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 udp UDP * @@ -71,6 +52,26 @@ extern "C" { * */ +/** + * This callback allows OpenThread to inform the application of a received UDP message. + * + */ +typedef void (*otUdpReceive)(void *aContext, otMessage aMessage, const otMessageInfo *aMessageInfo); + +/** + * This structure represents a UDP socket. + * + */ +typedef struct otUdpSocket +{ + otSockAddr mSockName; ///< The local IPv6 socket address. + otSockAddr mPeerName; ///< The peer IPv6 socket address. + otUdpReceive mHandler; ///< A function pointer to the application callback. + void *mContext; ///< A pointer to application-specific context. + void *mTransport; ///< A pointer to the transport object (internal use only). + struct otUdpSocket *mNext; ///< A pointer to the next UDP socket (internal use only). +} otUdpSocket; + /** * Allocate a new message buffer for sending a UDP message. * @@ -80,8 +81,9 @@ extern "C" { * @returns A pointer to the message buffer or NULL if no message buffers are available. * * @sa otFreeMessage + * */ -otMessage otNewUdpMessage(otInstance *aInstance, bool aLinkSecurityEnabled); +otMessage otUdpNewMessage(otInstance *aInstance, bool aLinkSecurityEnabled); /** * Open a UDP/IPv6 socket. @@ -94,12 +96,13 @@ otMessage otNewUdpMessage(otInstance *aInstance, bool aLinkSecurityEnabled); * @retval kThreadErrorNone Successfully opened the socket. * @retval kThreadErrorInvalidArgs Given socket structure was already opened. * - * @sa otNewUdpMessage - * @sa otCloseUdpSocket - * @sa otBindUdpSocket - * @sa otSendUdp + * @sa otUdpNewMessage + * @sa otUdpClose + * @sa otUdpBind + * @sa otUdpSend + * */ -ThreadError otOpenUdpSocket(otInstance *aInstance, otUdpSocket *aSocket, otUdpReceive aCallback, void *aContext); +ThreadError otUdpOpen(otInstance *aInstance, otUdpSocket *aSocket, otUdpReceive aCallback, void *aContext); /** * Close a UDP/IPv6 socket. @@ -108,12 +111,13 @@ ThreadError otOpenUdpSocket(otInstance *aInstance, otUdpSocket *aSocket, otUdpRe * * @retval kThreadErrorNone Successfully closed the socket. * - * @sa otNewUdpMessage - * @sa otOpenUdpSocket - * @sa otBindUdpSocket - * @sa otSendUdp + * @sa otUdpNewMessage + * @sa otUdpOpen + * @sa otUdpBind + * @sa otUdpSend + * */ -ThreadError otCloseUdpSocket(otUdpSocket *aSocket); +ThreadError otUdpClose(otUdpSocket *aSocket); /** * Bind a UDP/IPv6 socket. @@ -123,12 +127,13 @@ ThreadError otCloseUdpSocket(otUdpSocket *aSocket); * * @retval kThreadErrorNone Bind operation was successful. * - * @sa otNewUdpMessage - * @sa otOpenUdpSocket - * @sa otCloseUdpSocket - * @sa otSendUdp + * @sa otUdpNewMessage + * @sa otUdpOpen + * @sa otUdpClose + * @sa otUdpSend + * */ -ThreadError otBindUdpSocket(otUdpSocket *aSocket, otSockAddr *aSockName); +ThreadError otUdpBind(otUdpSocket *aSocket, otSockAddr *aSockName); /** * Send a UDP/IPv6 message. @@ -137,13 +142,14 @@ ThreadError otBindUdpSocket(otUdpSocket *aSocket, otSockAddr *aSockName); * @param[in] aMessage A pointer to a message buffer. * @param[in] aMessageInfo A pointer to a message info structure. * - * @sa otNewUdpMessage - * @sa otOpenUdpSocket - * @sa otCloseUdpSocket - * @sa otBindUdpSocket - * @sa otSendUdp + * @sa otUdpNewMessage + * @sa otUdpOpen + * @sa otUdpClose + * @sa otUdpBind + * @sa otUdpSend + * */ -ThreadError otSendUdp(otUdpSocket *aSocket, otMessage aMessage, const otMessageInfo *aMessageInfo); +ThreadError otUdpSend(otUdpSocket *aSocket, otMessage aMessage, const otMessageInfo *aMessageInfo); /** * @} diff --git a/src/cli/cli.hpp b/src/cli/cli.hpp index 4f91a873b..898fe009c 100644 --- a/src/cli/cli.hpp +++ b/src/cli/cli.hpp @@ -43,7 +43,8 @@ #include #include -#include +#include "openthread/udp.h" + #include #include diff --git a/src/cli/cli_udp.cpp b/src/cli/cli_udp.cpp index 0ad9a0615..8466f3621 100644 --- a/src/cli/cli_udp.cpp +++ b/src/cli/cli_udp.cpp @@ -58,8 +58,8 @@ ThreadError Udp::Start(void) memset(&sockaddr, 0, sizeof(otSockAddr)); sockaddr.mPort = 7335; - SuccessOrExit(error = otOpenUdpSocket(mInstance, &mSocket, &Udp::HandleUdpReceive, this)); - SuccessOrExit(error = otBindUdpSocket(&mSocket, &sockaddr)); + SuccessOrExit(error = otUdpOpen(mInstance, &mSocket, &Udp::HandleUdpReceive, this)); + SuccessOrExit(error = otUdpBind(&mSocket, &sockaddr)); exit: return error; @@ -101,10 +101,10 @@ int Udp::Output(const char *aBuf, uint16_t aBufLength) ThreadError error = kThreadError_None; otMessage message; - VerifyOrExit((message = otNewUdpMessage(mInstance, true)) != NULL, error = kThreadError_NoBufs); + VerifyOrExit((message = otUdpNewMessage(mInstance, true)) != NULL, error = kThreadError_NoBufs); SuccessOrExit(error = otSetMessageLength(message, aBufLength)); otWriteMessage(message, 0, aBuf, aBufLength); - SuccessOrExit(error = otSendUdp(&mSocket, message, &mPeer)); + SuccessOrExit(error = otUdpSend(&mSocket, message, &mPeer)); exit: diff --git a/src/core/Makefile.am b/src/core/Makefile.am index 8f4ef79c2..829f8d89f 100644 --- a/src/core/Makefile.am +++ b/src/core/Makefile.am @@ -40,6 +40,7 @@ CPPFLAGS_COMMON = \ SOURCES_COMMON = \ openthread.cpp \ + api/udp_api.cpp \ coap/coap_base.cpp \ coap/coap_client.cpp \ coap/coap_header.cpp \ diff --git a/src/core/api/udp_api.cpp b/src/core/api/udp_api.cpp new file mode 100644 index 000000000..ade0c7046 --- /dev/null +++ b/src/core/api/udp_api.cpp @@ -0,0 +1,103 @@ +/* + * 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 UDP API. + */ + +#include "openthread/udp.h" + +#include "openthread-instance.h" + +using namespace Thread; + +#ifdef __cplusplus +extern "C" { +#endif + +otMessage otUdpNewMessage(otInstance *aInstance, bool aLinkSecurityEnabled) +{ + Message *message = aInstance->mIp6.mUdp.NewMessage(0); + + if (message) + { + message->SetLinkSecurityEnabled(aLinkSecurityEnabled); + } + + return message; +} + +ThreadError otUdpOpen(otInstance *aInstance, otUdpSocket *aSocket, otUdpReceive aCallback, void *aCallbackContext) +{ + ThreadError error = kThreadError_InvalidArgs; + Ip6::UdpSocket *socket = static_cast(aSocket); + + if (socket->mTransport == NULL) + { + socket->mTransport = &aInstance->mIp6.mUdp; + error = socket->Open(aCallback, aCallbackContext); + } + + return error; +} + +ThreadError otUdpClose(otUdpSocket *aSocket) +{ + ThreadError error = kThreadError_InvalidState; + Ip6::UdpSocket *socket = static_cast(aSocket); + + if (socket->mTransport != NULL) + { + error = socket->Close(); + + if (error == kThreadError_None) + { + socket->mTransport = NULL; + } + } + + return error; +} + +ThreadError otUdpBind(otUdpSocket *aSocket, otSockAddr *aSockName) +{ + Ip6::UdpSocket *socket = static_cast(aSocket); + return socket->Bind(*static_cast(aSockName)); +} + +ThreadError otUdpSend(otUdpSocket *aSocket, otMessage aMessage, const otMessageInfo *aMessageInfo) +{ + Ip6::UdpSocket *socket = static_cast(aSocket); + return socket->SendTo(*static_cast(aMessage), + *static_cast(aMessageInfo)); +} + +#ifdef __cplusplus +} // extern "C" +#endif diff --git a/src/core/net/ip6.hpp b/src/core/net/ip6.hpp index 318a644a7..22d926a30 100644 --- a/src/core/net/ip6.hpp +++ b/src/core/net/ip6.hpp @@ -37,7 +37,8 @@ #include #include -#include +#include "openthread/udp.h" + #include #include #include diff --git a/src/core/net/udp6.hpp b/src/core/net/udp6.hpp index e698b1dc8..7c5fd05d0 100644 --- a/src/core/net/udp6.hpp +++ b/src/core/net/udp6.hpp @@ -35,6 +35,8 @@ #define UDP6_HPP_ #include +#include "openthread/udp.h" + #include namespace Thread { diff --git a/src/core/openthread.cpp b/src/core/openthread.cpp index cd159462b..bcbd87f74 100644 --- a/src/core/openthread.cpp +++ b/src/core/openthread.cpp @@ -1575,18 +1575,6 @@ ThreadError otSendIp6Datagram(otInstance *aInstance, otMessage aMessage) return error; } -otMessage otNewUdpMessage(otInstance *aInstance, bool aLinkSecurityEnabled) -{ - Message *message = aInstance->mIp6.mUdp.NewMessage(0); - - if (message) - { - message->SetLinkSecurityEnabled(aLinkSecurityEnabled); - } - - return message; -} - otMessage otNewIp6Message(otInstance *aInstance, bool aLinkSecurityEnabled) { Message *message = aInstance->mIp6.mMessagePool.New(Message::kTypeIp6, 0); @@ -1705,51 +1693,6 @@ exit: return next; } -ThreadError otOpenUdpSocket(otInstance *aInstance, otUdpSocket *aSocket, otUdpReceive aCallback, void *aCallbackContext) -{ - ThreadError error = kThreadError_InvalidArgs; - Ip6::UdpSocket *socket = static_cast(aSocket); - - if (socket->mTransport == NULL) - { - socket->mTransport = &aInstance->mIp6.mUdp; - error = socket->Open(aCallback, aCallbackContext); - } - - return error; -} - -ThreadError otCloseUdpSocket(otUdpSocket *aSocket) -{ - ThreadError error = kThreadError_InvalidState; - Ip6::UdpSocket *socket = static_cast(aSocket); - - if (socket->mTransport != NULL) - { - error = socket->Close(); - - if (error == kThreadError_None) - { - socket->mTransport = NULL; - } - } - - return error; -} - -ThreadError otBindUdpSocket(otUdpSocket *aSocket, otSockAddr *aSockName) -{ - Ip6::UdpSocket *socket = static_cast(aSocket); - return socket->Bind(*static_cast(aSockName)); -} - -ThreadError otSendUdp(otUdpSocket *aSocket, otMessage aMessage, const otMessageInfo *aMessageInfo) -{ - Ip6::UdpSocket *socket = static_cast(aSocket); - return socket->SendTo(*static_cast(aMessage), - *static_cast(aMessageInfo)); -} - bool otIcmp6IsEchoEnabled(otInstance *aInstance) { return aInstance->mIp6.mIcmp.IsEchoEnabled();