From 3061ecca3d0fdfb87dabbf5f63c9e06c2a30f53a Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Thu, 23 Aug 2018 17:08:59 +0200 Subject: o Initial import. --- .../iot/socket/libraries/addr_util/inet_pton.c | 60 +++++++ .../iot/socket/libraries/fifo/nrf_fifo.c | 176 ++++++++++++++++++++ .../iot/socket/libraries/fifo/nrf_fifo.h | 170 +++++++++++++++++++ .../components/iot/socket/libraries/mbuf/mbuf.c | 122 ++++++++++++++ .../components/iot/socket/libraries/mbuf/mbuf.h | 146 ++++++++++++++++ .../iot/socket/libraries/portdb/portdb.c | 185 +++++++++++++++++++++ .../iot/socket/libraries/portdb/portdb.h | 123 ++++++++++++++ 7 files changed, 982 insertions(+) create mode 100644 thirdparty/nRF5_SDK_15.0.0_a53641a/components/iot/socket/libraries/addr_util/inet_pton.c create mode 100644 thirdparty/nRF5_SDK_15.0.0_a53641a/components/iot/socket/libraries/fifo/nrf_fifo.c create mode 100644 thirdparty/nRF5_SDK_15.0.0_a53641a/components/iot/socket/libraries/fifo/nrf_fifo.h create mode 100644 thirdparty/nRF5_SDK_15.0.0_a53641a/components/iot/socket/libraries/mbuf/mbuf.c create mode 100644 thirdparty/nRF5_SDK_15.0.0_a53641a/components/iot/socket/libraries/mbuf/mbuf.h create mode 100644 thirdparty/nRF5_SDK_15.0.0_a53641a/components/iot/socket/libraries/portdb/portdb.c create mode 100644 thirdparty/nRF5_SDK_15.0.0_a53641a/components/iot/socket/libraries/portdb/portdb.h (limited to 'thirdparty/nRF5_SDK_15.0.0_a53641a/components/iot/socket/libraries') diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/iot/socket/libraries/addr_util/inet_pton.c b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/iot/socket/libraries/addr_util/inet_pton.c new file mode 100644 index 0000000..2fd1f4c --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/iot/socket/libraries/addr_util/inet_pton.c @@ -0,0 +1,60 @@ +/** + * Copyright (c) 2015 - 2018, Nordic Semiconductor ASA + * + * 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, except as embedded into a Nordic + * Semiconductor ASA integrated circuit in a product or a software update for + * such product, 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 Nordic Semiconductor ASA nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 4. This software, with or without modification, must only be used with a + * Nordic Semiconductor ASA integrated circuit. + * + * 5. Any software provided in binary form under this license must not be reverse + * engineered, decompiled, modified and/or disassembled. + * + * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA 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 "socket_api.h" +#include "ipv6_parse.h" + +int inet_pton(socket_family_t af, const char * p_src, void * p_dst) +{ + int ret_val = 1; + if (af != AF_INET6) + { + ret_val = -1; + } + else + { + in6_addr_t * p_addr = (in6_addr_t *)p_dst; + uint32_t err_code = ipv6_parse_addr(p_addr->s6_addr, p_src, strlen(p_src)); + if (err_code != NRF_SUCCESS) + { + ret_val = 0; + } + } + return ret_val; +} diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/iot/socket/libraries/fifo/nrf_fifo.c b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/iot/socket/libraries/fifo/nrf_fifo.c new file mode 100644 index 0000000..1458b01 --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/iot/socket/libraries/fifo/nrf_fifo.c @@ -0,0 +1,176 @@ +/** + * Copyright (c) 2015 - 2018, Nordic Semiconductor ASA + * + * 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, except as embedded into a Nordic + * Semiconductor ASA integrated circuit in a product or a software update for + * such product, 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 Nordic Semiconductor ASA nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 4. This software, with or without modification, must only be used with a + * Nordic Semiconductor ASA integrated circuit. + * + * 5. Any software provided in binary form under this license must not be reverse + * engineered, decompiled, modified and/or disassembled. + * + * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA 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 "iot_defines.h" +#include "iot_errors.h" +#include "nrf_fifo.h" +#include "mem_manager.h" +#include "nrf.h" + +static __INLINE uint32_t fifo_inc(nrf_fifo_t * p_fifo, uint32_t pos) +{ + return (pos + 1) % p_fifo->nmemb; +} + +static __INLINE bool fifo_full(nrf_fifo_t * p_fifo) +{ + return fifo_inc(p_fifo, p_fifo->write_pos) == p_fifo->read_pos; +} + +static __INLINE bool fifo_empty(nrf_fifo_t * p_fifo) +{ + return p_fifo->read_pos == p_fifo->write_pos; +} + +static __INLINE void fifo_enq(nrf_fifo_t * p_fifo, void * p_ctx) +{ + p_fifo->pp_elements[p_fifo->write_pos] = p_ctx; + __DSB(); + p_fifo->write_pos = fifo_inc(p_fifo, p_fifo->write_pos); +} + + +static __INLINE void fifo_deq(nrf_fifo_t * p_fifo, void ** pp_ctx) +{ + *pp_ctx = p_fifo->pp_elements[p_fifo->read_pos]; + __DSB(); + p_fifo->read_pos = fifo_inc(p_fifo, p_fifo->read_pos); +} + +uint32_t nrf_fifo_init(nrf_fifo_t * p_fifo, uint32_t nmemb, fifo_wait_fn wait_fn, fifo_flush_fn flush_fn) +{ + uint32_t err_code = NRF_SUCCESS; + uint32_t nmemb_actual = nmemb + 1; // Required to allow detection of empty and full state + p_fifo->pp_elements = nrf_malloc(nmemb_actual * sizeof(void *)); + + if (p_fifo->pp_elements == NULL) + { + err_code = NRF_ERROR_NO_MEM; + } + else + { + p_fifo->nmemb = nmemb_actual; + p_fifo->wait = wait_fn; + p_fifo->flush = flush_fn; + p_fifo->read_pos = 0; + p_fifo->write_pos = 0; + } + return err_code; +} + +void nrf_fifo_deinit(nrf_fifo_t * p_fifo) +{ + if (p_fifo->flush != NULL) + { + void * p_data; + uint32_t err_code = nrf_fifo_deq(p_fifo, &p_data, false); + while (err_code == NRF_SUCCESS) + { + p_fifo->flush(p_data); + err_code = nrf_fifo_deq(p_fifo, &p_data, false); + } + } + nrf_free(p_fifo->pp_elements); + p_fifo->nmemb = 0; + p_fifo->read_pos = 0; + p_fifo->write_pos = 0; + p_fifo->wait = NULL; + p_fifo->flush = NULL; +} + +uint32_t nrf_fifo_enq(nrf_fifo_t * p_fifo, void * p_ctx, bool wait) +{ + uint32_t err_code = NRF_SUCCESS; + + if (fifo_full(p_fifo) == true) + { + if (wait == false || p_fifo->wait == NULL) + { + err_code = SOCKET_WOULD_BLOCK; + } + else + { + while (fifo_full(p_fifo) == true && err_code == NRF_SUCCESS) + { + err_code = p_fifo->wait(); + } + } + } + else + { + fifo_enq(p_fifo, p_ctx); + } + return err_code; +} + +uint32_t nrf_fifo_deq(nrf_fifo_t * p_fifo, void ** pp_ctx, bool wait) +{ + uint32_t err_code = NRF_SUCCESS; + + if (fifo_empty(p_fifo) == true) + { + if (wait == false || p_fifo->wait == NULL) + { + err_code = SOCKET_WOULD_BLOCK; + } + else + { + while (fifo_empty(p_fifo) == true && err_code == NRF_SUCCESS) + { + err_code = p_fifo->wait(); + } + } + } + + if (err_code == NRF_SUCCESS) + { + fifo_deq(p_fifo, pp_ctx); + } + return err_code; +} + +bool nrf_fifo_empty(nrf_fifo_t * p_fifo) +{ + return fifo_empty(p_fifo); +} + +bool nrf_fifo_full(nrf_fifo_t * p_fifo) +{ + return fifo_full(p_fifo); +} diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/iot/socket/libraries/fifo/nrf_fifo.h b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/iot/socket/libraries/fifo/nrf_fifo.h new file mode 100644 index 0000000..c6fb378 --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/iot/socket/libraries/fifo/nrf_fifo.h @@ -0,0 +1,170 @@ +/** + * Copyright (c) 2015 - 2018, Nordic Semiconductor ASA + * + * 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, except as embedded into a Nordic + * Semiconductor ASA integrated circuit in a product or a software update for + * such product, 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 Nordic Semiconductor ASA nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 4. This software, with or without modification, must only be used with a + * Nordic Semiconductor ASA integrated circuit. + * + * 5. Any software provided in binary form under this license must not be reverse + * engineered, decompiled, modified and/or disassembled. + * + * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA 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. + * + */ +#ifndef NRF_FIFO_H__ +#define NRF_FIFO_H__ + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @file nrf_fifo.h + * + * @defgroup iot_socket_fifo FIFO + * @ingroup iot_sdk_socket + * @{ + * @brief A wait-free bounded FIFO of pointers for single producer/single consumer use. + * + * This FIFO is safe to use in single producer/single consumer patterns. In addition, the following + * restrictions apply for init/deinit: + * + * a) nrf_fifo_enq() and nrf_fifo_deq() may only be called after nrf_fifo_init() is called. + * + * b) All calls to nrf_fifo_enq() and nrf_fifo_deq() must be finished and no new calls must be made before nrf_fifo_deinit() is called. + * + * These restrictions must be handled by the user of the module, for instance by using a mutex. + */ + +/** + * @brief Wait function for blocking enqueue/dequeue. + * + * Should return NRF_SUCCESS as long as there are no errors while waiting. + */ +typedef uint32_t (*fifo_wait_fn)(void); + +/** + * @brief Flush function called on deinit. + * + * On deinit, this function will be called with each remaining element in the FIFO as argument. This + * can be used to ensure that memory is deallocated properly. + * + * @param[in] p_data Pointer to data that is flushed from FIFO. + */ +typedef void (*fifo_flush_fn)(void * p_data); + +/** + * @brief FIFO data structure. + */ +typedef struct { + void ** pp_elements; /**< The array of elements in the FIFO. */ + uint32_t nmemb; /**< The number of elements in this FIFO. */ + fifo_wait_fn wait; /**< The wait function used if blocking. */ + fifo_flush_fn flush; /**< The flush function used on deinit. */ + volatile uint32_t read_pos; /**< Read pointer to next element to read. */ + volatile uint32_t write_pos; /**< Write pointer to next element to write. */ +} nrf_fifo_t; + +/** + * @brief Function for initializing the FIFO. + * + * @param[out] p_fifo The FIFO to initialize. + * @param[in] nmemb The maximum number of elements in the FIFO. + * @param[in] wait_fn The wait function to use for blocking enqueue/dequeue. If NULL, the enq/deq + * functions will never block. + * @param[in] flush_fn The flush function to call on deinit. If NULL, the flush function will not + * be called. + * + * @retval NRF_SUCCESS if fifo was initialized successfully. + */ +uint32_t nrf_fifo_init(nrf_fifo_t * p_fifo, uint32_t nmemb, fifo_wait_fn wait_fn, fifo_flush_fn flush_fn); + +/** + * @brief Function for deinitializing the FIFO. + * + * Frees all memory allocated by this FIFO. All elements are removed. If a flush function was + * specified in nrf_fifo_init(), the function will be called for each remaining element in the + * FIFO. + * + * @param[in, out] p_fifo The FIFO to deinitialize. + */ +void nrf_fifo_deinit(nrf_fifo_t * p_fifo); + +/** + * @brief Function for enqueuing an element on the FIFO. + * + * @param[in, out] p_fifo The FIFO to enqueue elements on. + * @param[in] p_ctx The pointer to enqueue. + * @param[in] wait If true, this function will block until the FIFO has available space. Any + * errors returned by this function will be propagated to the caller. + * + * @retval NRF_SUCCESS if the element was queued. + * @retval NRF_ERROR_NO_MEM if wait was set to false and no space was available. + */ +uint32_t nrf_fifo_enq(nrf_fifo_t * p_fifo, void * p_ctx, bool wait); + +/** + * @brief Function for dequeuing an element from the FIFO. + * + * @param[in, out] p_fifo The FIFO to dequeue elements from. + * @param[out] pp_ctx Pointer to where the dequeued element should be stored. + * @param[in] wait If true, this function will block until the FIFO has elements for dequeuing. + * Any errors returned by this function will be propagated to the caller. + * + * @retval NRF_SUCCESS if the element was queued. + * @retval NRF_ERROR_NO_MEM if wait was set to false and no space was available. + */ +uint32_t nrf_fifo_deq(nrf_fifo_t * p_fifo, void ** pp_ctx, bool wait); + +/** + * @brief Function for checking if the FIFO is empty. + * + * @param[in] p_fifo The FIFO to check. + * @return true if empty, false if not. + */ +bool nrf_fifo_empty(nrf_fifo_t * p_fifo); + +/** + * @brief Function for checking if the FIFO is full. + * + * @param[in] p_fifo The FIFO to check. + * @return true if full, false if not. + */ +bool nrf_fifo_full(nrf_fifo_t * p_fifo); + +/**@} */ + +#ifdef __cplusplus +} +#endif + +#endif // NRF_FIFO_H__ diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/iot/socket/libraries/mbuf/mbuf.c b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/iot/socket/libraries/mbuf/mbuf.c new file mode 100644 index 0000000..d4b0c79 --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/iot/socket/libraries/mbuf/mbuf.c @@ -0,0 +1,122 @@ +/** + * Copyright (c) 2015 - 2018, Nordic Semiconductor ASA + * + * 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, except as embedded into a Nordic + * Semiconductor ASA integrated circuit in a product or a software update for + * such product, 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 Nordic Semiconductor ASA nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 4. This software, with or without modification, must only be used with a + * Nordic Semiconductor ASA integrated circuit. + * + * 5. Any software provided in binary form under this license must not be reverse + * engineered, decompiled, modified and/or disassembled. + * + * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA 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 "iot_defines.h" +#include "iot_errors.h" +#include "sdk_config.h" +#include "mem_manager.h" +#include "mbuf.h" +#include "nrf_fifo.h" +#include "socket_common.h" + +uint32_t mbuf_init(mbuf_t * p_mbuf, + mbuf_read_fn read_fn, + mbuf_buf_len_fn buf_len_fn, + mbuf_free_fn free_fn, + uint32_t nmemb) +{ + p_mbuf->p_current = NULL; + p_mbuf->read_pos = 0; + p_mbuf->read = read_fn; + p_mbuf->buf_len = buf_len_fn; + p_mbuf->free = free_fn; + return nrf_fifo_init(&p_mbuf->fifo, nmemb, socket_wait, free_fn); +} + +void mbuf_deinit(mbuf_t * p_mbuf) +{ + if (p_mbuf->p_current != NULL) + { + p_mbuf->free(p_mbuf->p_current); + p_mbuf->p_current = NULL; + } + + p_mbuf->read_pos = 0; + p_mbuf->read = NULL; + p_mbuf->buf_len = NULL; + p_mbuf->free = NULL; + nrf_fifo_deinit(&p_mbuf->fifo); +} + +static bool mbuf_empty_current(mbuf_t * p_mbuf) +{ + return (p_mbuf->buf_len(p_mbuf->p_current) == p_mbuf->read_pos); +} + +bool mbuf_empty(mbuf_t * p_mbuf) +{ + return ((p_mbuf->p_current == NULL || mbuf_empty_current(p_mbuf)) && + nrf_fifo_empty(&p_mbuf->fifo)); +} + +uint32_t mbuf_write(mbuf_t * p_mbuf, void * p_ctx) +{ + return nrf_fifo_enq(&p_mbuf->fifo, p_ctx, false); +} + +static void mbuf_load(mbuf_t * p_mbuf) +{ + if (p_mbuf->p_current == NULL) + { + (void)nrf_fifo_deq(&p_mbuf->fifo, &p_mbuf->p_current, false); + } +} + +uint32_t mbuf_read(mbuf_t * p_mbuf, void * p_buf, uint32_t buf_size) +{ + uint32_t nbytes = 0; + while (nbytes < buf_size && mbuf_empty(p_mbuf) == false) + { + mbuf_load(p_mbuf); + void * p_current = p_mbuf->p_current; + const uint32_t copy_len = p_mbuf->read(p_current, + p_mbuf->read_pos, + ((uint8_t *)p_buf) + nbytes, + buf_size - nbytes); + p_mbuf->read_pos += copy_len; + nbytes += copy_len; + if (mbuf_empty_current(p_mbuf) == true) + { + p_mbuf->free(p_mbuf->p_current); + p_mbuf->p_current = NULL; + p_mbuf->read_pos = 0; + } + } + return nbytes; +} diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/iot/socket/libraries/mbuf/mbuf.h b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/iot/socket/libraries/mbuf/mbuf.h new file mode 100644 index 0000000..884c650 --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/iot/socket/libraries/mbuf/mbuf.h @@ -0,0 +1,146 @@ +/** + * Copyright (c) 2015 - 2018, Nordic Semiconductor ASA + * + * 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, except as embedded into a Nordic + * Semiconductor ASA integrated circuit in a product or a software update for + * such product, 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 Nordic Semiconductor ASA nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 4. This software, with or without modification, must only be used with a + * Nordic Semiconductor ASA integrated circuit. + * + * 5. Any software provided in binary form under this license must not be reverse + * engineered, decompiled, modified and/or disassembled. + * + * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA 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. + * + */ +#ifndef MBUF_H__ +#define MBUF_H__ + +/** + * @file mbuf.h + * + * @defgroup iot_socket_mbuf Memory management for socket + * @ingroup iot_sdk_socket + * @{ + * @brief Memory management for socket. + */ + +#include +#include "nrf_fifo.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/**@brief Function for reading data from a buffer. */ +typedef uint32_t (*mbuf_read_fn)(void * p_ctx, + uint32_t read_offset, + uint8_t * p_dest, + uint32_t dest_len); + +/**@brief Function for checking buffer length. */ +typedef uint32_t (*mbuf_buf_len_fn)(void * p_ctx); + +/**@brief Function for freeing a buffer. */ +typedef void (*mbuf_free_fn)(void * p_ctx); + +/**@brief Memory management structure. */ +typedef struct +{ + nrf_fifo_t fifo; /**< FIFO for storing data buffers. */ + mbuf_read_fn read; /**< Function for reading data from a buffer. */ + mbuf_buf_len_fn buf_len; /**< Function for checking buffer length. */ + mbuf_free_fn free; /**< Function for freeing a buffer. */ + uint32_t read_pos; /**< Read position in the currently processed buffer. */ + void * p_current; /**< Pointer to the currently processed buffer. */ +} mbuf_t; + +/** + * @brief Function for initializing the memory buffer manager. + * + * This function allocates resources for the mbuf FIFO and initializes the mbuf. + * + * @param[in, out] p_mbuf Pointer to the mbuf structure to initialize. + * @param[in] read_fn Function for reading data from a buffer. + * @param[in] buf_len_fn Function for checking buffer length. + * @param[in] free_fn Function for freeing a buffer. + * @param[in] nmemb Maximum number of data buffers. + * + * @return NRF_SUCCESS on success, otherwise error code is returned. + */ +uint32_t mbuf_init(mbuf_t * p_mbuf, + mbuf_read_fn read_fn, + mbuf_buf_len_fn buf_len_fn, + mbuf_free_fn free_fn, + uint32_t nmemb); + +/** + * @brief Function for deinitializing the memory buffer manager. + * + * This function releases any resources allocated for mbuf instance pointed by p_mbuf. + * + * @param[in, out] p_mbuf Pointer to the mbuf structure to deinitialize. + */ +void mbuf_deinit(mbuf_t * p_mbuf); + +/** + * @brief Function for putting a data buffer in the mbuf. + * + * @param[in, out] p_mbuf Pointer to the mbuf structure that shall store the buffer. + * @param[in] p_ctx Pointer to the data buffer to store. + * + * @return NRF_SUCCESS on success, otherwise error code is returned. + */ +uint32_t mbuf_write(mbuf_t * p_mbuf, void * p_ctx); + +/** + * @brief Function for reading data from the mbuf. + * + * @param[in, out] p_mbuf Pointer to the mbuf structure to read data from. + * @param[in] p_buf Pointer to the buffer where data shall be read from. + * @param[out] buf_size Size of the buffer pointed by p_buf. + * + * @return NRF_SUCCESS on success, otherwise error code is returned. + */ +uint32_t mbuf_read(mbuf_t * p_mbuf, void * p_buf, uint32_t buf_size); + +/** + * @brief Function for checking if the mbuf is empty. + * + * @param[in] p_mbuf Pointer to the mbuf structure that shall be checked. + * + * @return True if mbuf is empty, false otherwise. + */ +bool mbuf_empty(mbuf_t * p_mbuf); + +#ifdef __cplusplus +} +#endif + +/**@} */ + +#endif // MBUF_H__ diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/iot/socket/libraries/portdb/portdb.c b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/iot/socket/libraries/portdb/portdb.c new file mode 100644 index 0000000..7bfbcc4 --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/iot/socket/libraries/portdb/portdb.c @@ -0,0 +1,185 @@ +/** + * Copyright (c) 2016 - 2018, Nordic Semiconductor ASA + * + * 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, except as embedded into a Nordic + * Semiconductor ASA integrated circuit in a product or a software update for + * such product, 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 Nordic Semiconductor ASA nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 4. This software, with or without modification, must only be used with a + * Nordic Semiconductor ASA integrated circuit. + * + * 5. Any software provided in binary form under this license must not be reverse + * engineered, decompiled, modified and/or disassembled. + * + * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA 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 "portdb.h" +#include +#include "iot_common.h" +#include "iot_errors.h" +#include "mem_manager.h" +#include "nrf_error.h" + +#define IANA_EPHEMRAL_BEGIN 49152u /**< Minimum port number of the ephemeral port range. */ +#define IANA_EPHEMRAL_END 65535u /**< Maximum port number of the ephemeral port range. */ + +static uint16_t * m_portdb; /**< A pointer to the port database. */ +static uint32_t m_portdb_len; /**< Length of the port database. */ + + + +static __INLINE uint32_t db_size_get(void) +{ + return m_portdb_len * sizeof(uint16_t); +} + + +static __INLINE void db_reset(void) +{ + memset(&m_portdb[0], 0, db_size_get()); +} + + +uint32_t portdb_init(uint32_t max_ports) +{ + uint32_t err_code = NRF_SUCCESS; + m_portdb_len = max_ports; + m_portdb = nrf_malloc(db_size_get()); + if (m_portdb == NULL) + { + err_code = NRF_ERROR_NO_MEM; + } + else + { + db_reset(); + } + return err_code; +} + + +void portdb_deinit(void) +{ + nrf_free(m_portdb); + m_portdb = NULL; + m_portdb_len = 0; +} + + +void portdb_reset(void) +{ + db_reset(); +} + + +static inline uint32_t check_port_in_use(uint16_t port) +{ + uint32_t err_code = NRF_SUCCESS; + + for (uint32_t i = 0; i < m_portdb_len; i++) + { + if (m_portdb[i] == port) + { + err_code = SOCKET_PORT_IN_USE; + break; + } + } + return err_code; +} + +static inline uint32_t find_free_index(uint32_t * p_idx) +{ + uint32_t err_code = SOCKET_NO_AVAILABLE_PORTS; + + for (uint32_t i = 0; i < m_portdb_len; i++) + { + if (m_portdb[i] == 0) + { + *p_idx = i; + err_code = NRF_SUCCESS; + break; + } + } + return err_code; +} + +static uint32_t portdb_find_available_index(uint32_t * p_idx, uint16_t port) +{ + uint32_t err_code = SOCKET_NO_AVAILABLE_PORTS; + + err_code = check_port_in_use(port); + + if (err_code == NRF_SUCCESS) + { + err_code = find_free_index(p_idx); + } + return err_code; +} + +uint32_t portdb_register(uint16_t port) +{ + uint32_t idx = 0; + uint32_t err_code = portdb_find_available_index(&idx, port); + + if (err_code == NRF_SUCCESS) + { + m_portdb[idx] = port; + } + return err_code; +} + +uint32_t portdb_alloc(uint16_t * p_port) +{ + uint32_t err_code = SOCKET_NO_AVAILABLE_PORTS; + for (uint32_t i = IANA_EPHEMRAL_BEGIN; i <= IANA_EPHEMRAL_END; i++) + { + uint16_t port = (uint16_t)i; + err_code = portdb_register(port); + + if (err_code == NRF_SUCCESS) + { + *p_port = port; + break; + } + else if (err_code == SOCKET_NO_AVAILABLE_PORTS) + { + break; + } + } + return err_code; +} + +void portdb_free(uint16_t port) +{ + for (uint32_t i = 0; i < m_portdb_len; i++) + { + if (m_portdb[i] == port) + { + m_portdb[i] = 0; + break; + } + } +} diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/iot/socket/libraries/portdb/portdb.h b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/iot/socket/libraries/portdb/portdb.h new file mode 100644 index 0000000..94f0d38 --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/iot/socket/libraries/portdb/portdb.h @@ -0,0 +1,123 @@ +/** + * Copyright (c) 2016 - 2018, Nordic Semiconductor ASA + * + * 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, except as embedded into a Nordic + * Semiconductor ASA integrated circuit in a product or a software update for + * such product, 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 Nordic Semiconductor ASA nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 4. This software, with or without modification, must only be used with a + * Nordic Semiconductor ASA integrated circuit. + * + * 5. Any software provided in binary form under this license must not be reverse + * engineered, decompiled, modified and/or disassembled. + * + * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS + * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA 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. + * + */ +#ifndef PORTDB_H__ +#define PORTDB_H__ + +/** + * @file portdb.h + * + * @defgroup iot_socket_portdb Port Database + * @ingroup iot_sdk_socket + * @{ + * @brief Port database for sockets. + * + * The port database provides a functionality for registering, allocating, and freeing ports. + */ + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Function for initializing the port database. + * + * This must be called before allocating and freeing ports. + * + * @param max_len The max length of the portdb. + * + * @retval NRF_SUCCESS If successfully initialized. + */ +uint32_t portdb_init(uint32_t max_len); + +/** + * @brief Function for deinitializing the portdb. + * + * This will free all memory allocated by this portdb. + */ +void portdb_deinit(void); + +/** + * @brief Function to reset all ports without freeing any memories. + */ +void portdb_reset(void); + +/** + * @brief Function for allocating a port. + * + * Looks for an available port in the database and allocates it to the caller. + * + * @param[out] p_port Pointer to a variable where the allocated port number should be stored. + * + * @retval NRF_SUCCESS If a free port was located and successfully allocated. + * @retval SOCKET_NO_AVAILABLE_PORTS If no available ports were found. + */ +uint32_t portdb_alloc(uint16_t * p_port); + +/** + * @brief Function for registering a port. + * + * Marks a given port in the database as being in use. + * + * @param[in] port The port to mark as in use. + * + * @retval NRF_SUCCESS If port was successfully marked as in use. + * @retval SOCKET_NO_AVAILABLE_PORTS If there was no slot in which to register the port. + * @retval SOCKET_PORT_IN_USE If the port has already been registered or allocated. + */ +uint32_t portdb_register(uint16_t port); + +/** + * @brief Function for freeing a port. + * + * Mark a given port as free and make it available for others to register or allocate. + * + * @param[in] port The port to mark as free. + */ +void portdb_free(uint16_t port); + +#ifdef __cplusplus +} +#endif + +/**@} */ + +#endif // PORTDB_H__ -- cgit v1.2.3