UDP API cleanup.

This commit is contained in:
Jonathan Hui
2017-01-10 18:25:08 -08:00
parent 53cc398251
commit 10e5b9a38f
11 changed files with 165 additions and 126 deletions
-1
View File
@@ -72,7 +72,6 @@ include_HEADERS = \
openthread-message.h \
openthread-tasklet.h \
openthread-types.h \
openthread-udp.h \
$(NULL)
install-headers: install-includeHEADERS
-18
View File
@@ -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
/**
+1
View File
@@ -30,6 +30,7 @@ include $(abs_top_nlbuild_autotools_dir)/automake/pre.am
openthread_headers = \
coap.h \
udp.h \
$(NULL)
openthreaddir = $(includedir)/openthread
@@ -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 <openthread-message.h>
#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);
/**
* @}
+2 -1
View File
@@ -43,7 +43,8 @@
#include <stdarg.h>
#include <openthread-ip6.h>
#include <openthread-udp.h>
#include "openthread/udp.h"
#include <cli/cli_server.hpp>
#include <common/code_utils.hpp>
+4 -4
View File
@@ -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:
+1
View File
@@ -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 \
+103
View File
@@ -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<Ip6::UdpSocket *>(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<Ip6::UdpSocket *>(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<Ip6::UdpSocket *>(aSocket);
return socket->Bind(*static_cast<const Ip6::SockAddr *>(aSockName));
}
ThreadError otUdpSend(otUdpSocket *aSocket, otMessage aMessage, const otMessageInfo *aMessageInfo)
{
Ip6::UdpSocket *socket = static_cast<Ip6::UdpSocket *>(aSocket);
return socket->SendTo(*static_cast<Message *>(aMessage),
*static_cast<const Ip6::MessageInfo *>(aMessageInfo));
}
#ifdef __cplusplus
} // extern "C"
#endif
+2 -1
View File
@@ -37,7 +37,8 @@
#include <stddef.h>
#include <openthread-ip6.h>
#include <openthread-udp.h>
#include "openthread/udp.h"
#include <common/encoding.hpp>
#include <common/message.hpp>
#include <net/icmp6.hpp>
+2
View File
@@ -35,6 +35,8 @@
#define UDP6_HPP_
#include <openthread.h>
#include "openthread/udp.h"
#include <net/ip6_headers.hpp>
namespace Thread {
-57
View File
@@ -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<Ip6::UdpSocket *>(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<Ip6::UdpSocket *>(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<Ip6::UdpSocket *>(aSocket);
return socket->Bind(*static_cast<const Ip6::SockAddr *>(aSockName));
}
ThreadError otSendUdp(otUdpSocket *aSocket, otMessage aMessage, const otMessageInfo *aMessageInfo)
{
Ip6::UdpSocket *socket = static_cast<Ip6::UdpSocket *>(aSocket);
return socket->SendTo(*static_cast<Message *>(aMessage),
*static_cast<const Ip6::MessageInfo *>(aMessageInfo));
}
bool otIcmp6IsEchoEnabled(otInstance *aInstance)
{
return aInstance->mIp6.mIcmp.IsEchoEnabled();