diff options
Diffstat (limited to 'thirdparty/nRF5_SDK_15.0.0_a53641a/components/iot/socket/libraries/fifo')
-rw-r--r-- | thirdparty/nRF5_SDK_15.0.0_a53641a/components/iot/socket/libraries/fifo/nrf_fifo.c | 176 | ||||
-rw-r--r-- | thirdparty/nRF5_SDK_15.0.0_a53641a/components/iot/socket/libraries/fifo/nrf_fifo.h | 170 |
2 files changed, 346 insertions, 0 deletions
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 <stdint.h> +#include <stdbool.h> + +#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__ |