diff --git a/configure.ac b/configure.ac index d9e0fce42..39ab03bee 100644 --- a/configure.ac +++ b/configure.ac @@ -1083,6 +1083,7 @@ include/cli/Makefile include/commissioning/Makefile include/dhcp6/Makefile include/ncp/Makefile +include/openthread/Makefile include/platform/Makefile src/Makefile src/cli/Makefile diff --git a/include/Makefile.am b/include/Makefile.am index 17d04ff95..adb379619 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -35,6 +35,7 @@ DIST_SUBDIRS = \ commissioning \ dhcp6 \ ncp \ + openthread \ platform \ $(NULL) @@ -45,6 +46,7 @@ SUBDIRS = \ commissioning \ dhcp6 \ ncp \ + openthread \ platform \ $(NULL) @@ -55,13 +57,13 @@ PRETTY_SUBDIRS = \ commissioning \ dhcp6 \ ncp \ + openthread \ platform \ $(NULL) include_HEADERS = \ openthread/link_raw.h \ openthread.h \ - openthread-coap.h \ openthread-crypto.h \ openthread-diag.h \ openthread-icmp6.h \ diff --git a/include/openthread/Makefile.am b/include/openthread/Makefile.am new file mode 100644 index 000000000..da8f4b7e8 --- /dev/null +++ b/include/openthread/Makefile.am @@ -0,0 +1,43 @@ +# +# 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. +# + +include $(abs_top_nlbuild_autotools_dir)/automake/pre.am + +openthread_headers = \ + coap.h \ + $(NULL) + +openthreaddir = $(includedir)/openthread +dist_openthread_HEADERS = $(openthread_headers) + +include_HEADERS = \ + $(NULL) + +install-headers: install-includeHEADERS + +include $(abs_top_nlbuild_autotools_dir)/automake/post.am diff --git a/include/openthread-coap.h b/include/openthread/coap.h similarity index 91% rename from include/openthread-coap.h rename to include/openthread/coap.h index c8d7ca9ad..a3282b4c6 100644 --- a/include/openthread-coap.h +++ b/include/openthread/coap.h @@ -35,9 +35,15 @@ #ifndef OPENTHREAD_COAP_H_ #define OPENTHREAD_COAP_H_ +#ifdef OPENTHREAD_CONFIG_FILE +#include OPENTHREAD_CONFIG_FILE +#else +#include +#endif + #include -#include +#include "openthread-types.h" #ifdef __cplusplus extern "C" { @@ -121,23 +127,23 @@ typedef struct otCoapHeader { struct { - uint8_t mVersionTypeToken; - uint8_t mCode; - uint16_t mMessageId; - } mFields; - uint8_t mBytes[OT_COAP_HEADER_MAX_LENGTH]; - } mHeader; - uint8_t mHeaderLength; - uint16_t mOptionLast; - uint16_t mNextOptionOffset; - otCoapOption mOption; + uint8_t mVersionTypeToken; ///< The CoAP Version, Type, and Token Length + uint8_t mCode; ///< The CoAP Code + uint16_t mMessageId; ///< The CoAP Message ID + } mFields; ///< Structure representing a CoAP base header + uint8_t mBytes[OT_COAP_HEADER_MAX_LENGTH]; ///< The raw byte encoding for the CoAP header + } mHeader; ///< The CoAP header encoding + uint8_t mHeaderLength; ///< The CoAP header length (bytes) + uint16_t mOptionLast; ///< The last CoAP Option Number value + uint16_t mNextOptionOffset; ///< The byte offset for the next CoAP Option + otCoapOption mOption; ///< A structure representing the current CoAP Option. } otCoapHeader; /** * This function pointer is called when a CoAP response is received or on the request timeout. * * @param[in] aContext A pointer to application-specific context. - * @param[in[ aHeader A pointer to the received CoAP header. NULL if no response was received. + * @param[in] aHeader A pointer to the received CoAP header. NULL if no response was received. * @param[in] aMessage A pointer to the message buffer containing the response. NULL if no response was received. * @param[in] aMessageInfo A pointer to the message info for @p aMessage. NULL if no response was received. * @param[in] aResult A result of the CoAP transaction. @@ -168,10 +174,10 @@ typedef void (*otCoapRequestHandler)(void *aContext, otCoapHeader *aHeader, otMe */ typedef struct otCoapResource { - const char *mUriPath; - otCoapRequestHandler mHandler; - void *mContext; - struct otCoapResource *mNext; + const char *mUriPath; ///< The URI Path string + otCoapRequestHandler mHandler; ///< The callback for handling a received request + void *mContext; ///< Application-specific context + struct otCoapResource *mNext; ///< The next CoAP resource in the list } otCoapResource; #if OPENTHREAD_ENABLE_APPLICATION_COAP diff --git a/src/core/Makefile.am b/src/core/Makefile.am index 6c4bd8d88..8f4ef79c2 100644 --- a/src/core/Makefile.am +++ b/src/core/Makefile.am @@ -85,6 +85,12 @@ SOURCES_COMMON = \ utils/slaac_address.cpp \ $(NULL) +if OPENTHREAD_ENABLE_APPLICATION_COAP +SOURCES_COMMON += \ + api/coap_api.cpp \ + $(NULL) +endif # OPENTHREAD_ENABLE_APPLICATION_COAP + if OPENTHREAD_ENABLE_MAC_WHITELIST SOURCES_COMMON += \ mac/mac_blacklist.cpp \ diff --git a/src/core/api/coap_api.cpp b/src/core/api/coap_api.cpp new file mode 100644 index 000000000..005e465e1 --- /dev/null +++ b/src/core/api/coap_api.cpp @@ -0,0 +1,174 @@ +/* + * 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 CoAP API. + */ + +#include "openthread/coap.h" + +#include "openthread-instance.h" +#include "coap/coap_header.hpp" + +using namespace Thread; + +extern "C" { + +void otCoapHeaderInit(otCoapHeader *aHeader, otCoapType aType, otCoapCode aCode) +{ + Coap::Header *header = static_cast(aHeader); + header->Init(aType, aCode); +} + +void otCoapHeaderSetToken(otCoapHeader *aHeader, const uint8_t *aToken, uint8_t aTokenLength) +{ + static_cast(aHeader)->SetToken(aToken, aTokenLength); +} + +void otCoapHeaderGenerateToken(otCoapHeader *aHeader, uint8_t aTokenLength) +{ + static_cast(aHeader)->SetToken(aTokenLength); +} + +ThreadError otCoapHeaderAppendOption(otCoapHeader *aHeader, const otCoapOption *aOption) +{ + return static_cast(aHeader)->AppendOption(*static_cast(aOption)); +} + +ThreadError otCoapHeaderAppendObserveOption(otCoapHeader *aHeader, uint32_t aObserve) +{ + return static_cast(aHeader)->AppendObserveOption(aObserve); +} + +ThreadError otCoapHeaderAppendUriPathOptions(otCoapHeader *aHeader, const char *aUriPath) +{ + return static_cast(aHeader)->AppendUriPathOptions(aUriPath); +} + +ThreadError otCoapHeaderAppendMaxAgeOption(otCoapHeader *aHeader, uint32_t aMaxAge) +{ + return static_cast(aHeader)->AppendMaxAgeOption(aMaxAge); +} + +ThreadError otCoapHeaderAppendUriQueryOption(otCoapHeader *aHeader, const char *aUriQuery) +{ + return static_cast(aHeader)->AppendUriQueryOption(aUriQuery); +} + +void otCoapHeaderSetPayloadMarker(otCoapHeader *aHeader) +{ + static_cast(aHeader)->SetPayloadMarker(); +} + +void otCoapHeaderSetMessageId(otCoapHeader *aHeader, uint16_t aMessageId) +{ + return static_cast(aHeader)->SetMessageId(aMessageId); +} + +otCoapType otCoapHeaderGetType(const otCoapHeader *aHeader) +{ + return static_cast(aHeader)->GetType(); +} + +otCoapCode otCoapHeaderGetCode(const otCoapHeader *aHeader) +{ + return static_cast(aHeader)->GetCode(); +} + +uint16_t otCoapHeaderGetMessageId(const otCoapHeader *aHeader) +{ + return static_cast(aHeader)->GetMessageId(); +} + +uint8_t otCoapHeaderGetTokenLength(const otCoapHeader *aHeader) +{ + return static_cast(aHeader)->GetTokenLength(); +} + +const uint8_t *otCoapHeaderGetToken(const otCoapHeader *aHeader) +{ + return static_cast(aHeader)->GetToken(); +} + +const otCoapOption *otCoapHeaderGetCurrentOption(const otCoapHeader *aHeader) +{ + return static_cast(static_cast(aHeader)->GetCurrentOption()); +} + +const otCoapOption *otCoapHeaderGetNextOption(otCoapHeader *aHeader) +{ + return static_cast(static_cast(aHeader)->GetNextOption()); +} + +otMessage otCoapNewMessage(otInstance *aInstance, const otCoapHeader *aHeader) +{ + return aInstance->mThreadNetif.GetCoapClient().NewMessage(*(static_cast(aHeader))); +} + +ThreadError otCoapSendRequest(otInstance *aInstance, otMessage aMessage, const otMessageInfo *aMessageInfo, + otCoapResponseHandler aHandler, void *aContext) +{ + return aInstance->mThreadNetif.GetCoapClient().SendMessage( + *static_cast(aMessage), + *static_cast(aMessageInfo), + aHandler, aContext); +} + +ThreadError otCoapServerStart(otInstance *aInstance) +{ + return aInstance->mApplicationCoapServer.Start(); +} + +ThreadError otCoapServerStop(otInstance *aInstance) +{ + return aInstance->mApplicationCoapServer.Stop(); +} + +ThreadError otCoapServerSetPort(otInstance *aInstance, uint16_t aPort) +{ + return aInstance->mApplicationCoapServer.SetPort(aPort); +} + +ThreadError otCoapServerAddResource(otInstance *aInstance, otCoapResource *aResource) +{ + return aInstance->mApplicationCoapServer.AddResource(*static_cast(aResource)); +} + +void otCoapServerRemoveResource(otInstance *aInstance, otCoapResource *aResource) +{ + aInstance->mApplicationCoapServer.RemoveResource(*static_cast(aResource)); +} + +ThreadError otCoapSendResponse(otInstance *aInstance, otMessage aMessage, const otMessageInfo *aMessageInfo) +{ + return aInstance->mApplicationCoapServer.SendMessage( + *static_cast(aMessage), *static_cast(aMessageInfo)); +} + +} // extern "C" diff --git a/src/core/coap/coap_client.hpp b/src/core/coap/coap_client.hpp index 34cf0495f..eb4d408e9 100644 --- a/src/core/coap/coap_client.hpp +++ b/src/core/coap/coap_client.hpp @@ -30,7 +30,8 @@ #define COAP_CLIENT_HPP_ #include -#include +#include "openthread/coap.h" + #include #include #include diff --git a/src/core/coap/coap_header.hpp b/src/core/coap/coap_header.hpp index 756cf1b0d..5017650b8 100644 --- a/src/core/coap/coap_header.hpp +++ b/src/core/coap/coap_header.hpp @@ -37,7 +37,8 @@ #include #include -#include +#include "openthread/coap.h" + #include #include diff --git a/src/core/coap/coap_server.hpp b/src/core/coap/coap_server.hpp index a2d3d5e25..7034e010f 100644 --- a/src/core/coap/coap_server.hpp +++ b/src/core/coap/coap_server.hpp @@ -34,7 +34,8 @@ #ifndef COAP_SERVER_HPP_ #define COAP_SERVER_HPP_ -#include +#include "openthread/coap.h" + #include #include #include diff --git a/src/core/openthread.cpp b/src/core/openthread.cpp index 0f3ef89fa..cd159462b 100644 --- a/src/core/openthread.cpp +++ b/src/core/openthread.cpp @@ -40,6 +40,7 @@ #endif #include + #if OPENTHREAD_ENABLE_JAM_DETECTION #include #endif @@ -63,9 +64,6 @@ #include #include #include -#include -#include -#include #include using namespace Thread; @@ -1965,139 +1963,6 @@ ThreadError otJoinerStop(otInstance *aInstance) } #endif // OPENTHREAD_ENABLE_JOINER -#if OPENTHREAD_ENABLE_APPLICATION_COAP -void otCoapHeaderInit(otCoapHeader *aHeader, otCoapType aType, otCoapCode aCode) -{ - Coap::Header *header = static_cast(aHeader); - header->Init(aType, aCode); -} - -void otCoapHeaderSetToken(otCoapHeader *aHeader, const uint8_t *aToken, uint8_t aTokenLength) -{ - static_cast(aHeader)->SetToken(aToken, aTokenLength); -} - -void otCoapHeaderGenerateToken(otCoapHeader *aHeader, uint8_t aTokenLength) -{ - static_cast(aHeader)->SetToken(aTokenLength); -} - -ThreadError otCoapHeaderAppendOption(otCoapHeader *aHeader, const otCoapOption *aOption) -{ - return static_cast(aHeader)->AppendOption(*static_cast(aOption)); -} - -ThreadError otCoapHeaderAppendObserveOption(otCoapHeader *aHeader, uint32_t aObserve) -{ - return static_cast(aHeader)->AppendObserveOption(aObserve); -} - -ThreadError otCoapHeaderAppendUriPathOptions(otCoapHeader *aHeader, const char *aUriPath) -{ - return static_cast(aHeader)->AppendUriPathOptions(aUriPath); -} - -ThreadError otCoapHeaderAppendMaxAgeOption(otCoapHeader *aHeader, uint32_t aMaxAge) -{ - return static_cast(aHeader)->AppendMaxAgeOption(aMaxAge); -} - -ThreadError otCoapHeaderAppendUriQueryOption(otCoapHeader *aHeader, const char *aUriQuery) -{ - return static_cast(aHeader)->AppendUriQueryOption(aUriQuery); -} - -void otCoapHeaderSetPayloadMarker(otCoapHeader *aHeader) -{ - static_cast(aHeader)->SetPayloadMarker(); -} - -void otCoapHeaderSetMessageId(otCoapHeader *aHeader, uint16_t aMessageId) -{ - return static_cast(aHeader)->SetMessageId(aMessageId); -} - -otCoapType otCoapHeaderGetType(const otCoapHeader *aHeader) -{ - return static_cast(aHeader)->GetType(); -} - -otCoapCode otCoapHeaderGetCode(const otCoapHeader *aHeader) -{ - return static_cast(aHeader)->GetCode(); -} - -uint16_t otCoapHeaderGetMessageId(const otCoapHeader *aHeader) -{ - return static_cast(aHeader)->GetMessageId(); -} - -uint8_t otCoapHeaderGetTokenLength(const otCoapHeader *aHeader) -{ - return static_cast(aHeader)->GetTokenLength(); -} - -const uint8_t *otCoapHeaderGetToken(const otCoapHeader *aHeader) -{ - return static_cast(aHeader)->GetToken(); -} - -const otCoapOption *otCoapHeaderGetCurrentOption(const otCoapHeader *aHeader) -{ - return static_cast(static_cast(aHeader)->GetCurrentOption()); -} - -const otCoapOption *otCoapHeaderGetNextOption(otCoapHeader *aHeader) -{ - return static_cast(static_cast(aHeader)->GetNextOption()); -} - -otMessage otCoapNewMessage(otInstance *aInstance, const otCoapHeader *aHeader) -{ - return aInstance->mThreadNetif.GetCoapClient().NewMessage(*(static_cast(aHeader))); -} - -ThreadError otCoapSendRequest(otInstance *aInstance, otMessage aMessage, const otMessageInfo *aMessageInfo, - otCoapResponseHandler aHandler, void *aContext) -{ - return aInstance->mThreadNetif.GetCoapClient().SendMessage( - *static_cast(aMessage), - *static_cast(aMessageInfo), - aHandler, aContext); -} - -ThreadError otCoapServerStart(otInstance *aInstance) -{ - return aInstance->mApplicationCoapServer.Start(); -} - -ThreadError otCoapServerStop(otInstance *aInstance) -{ - return aInstance->mApplicationCoapServer.Stop(); -} - -ThreadError otCoapServerSetPort(otInstance *aInstance, uint16_t aPort) -{ - return aInstance->mApplicationCoapServer.SetPort(aPort); -} - -ThreadError otCoapServerAddResource(otInstance *aInstance, otCoapResource *aResource) -{ - return aInstance->mApplicationCoapServer.AddResource(*static_cast(aResource)); -} - -void otCoapServerRemoveResource(otInstance *aInstance, otCoapResource *aResource) -{ - aInstance->mApplicationCoapServer.RemoveResource(*static_cast(aResource)); -} - -ThreadError otCoapSendResponse(otInstance *aInstance, otMessage aMessage, const otMessageInfo *aMessageInfo) -{ - return aInstance->mApplicationCoapServer.SendMessage( - *static_cast(aMessage), *static_cast(aMessageInfo)); -} -#endif // OPENTHREAD_ENABLE_APPLICATION_COAP - #ifdef __cplusplus } // extern "C" #endif