diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2018-08-23 17:08:59 +0200 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2018-08-23 17:12:21 +0200 |
commit | 3061ecca3d0fdfb87dabbf5f63c9e06c2a30f53a (patch) | |
tree | ab49cc16ed0b853452c5c2ed2d3042416d628986 /thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/hci | |
download | iot-sensors-master.tar.gz iot-sensors-master.tar.bz2 iot-sensors-master.tar.xz iot-sensors-master.zip |
Diffstat (limited to 'thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/hci')
6 files changed, 2118 insertions, 0 deletions
diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/hci/hci_mem_pool.c b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/hci/hci_mem_pool.c new file mode 100644 index 0000000..ec97aa6 --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/hci/hci_mem_pool.c @@ -0,0 +1,264 @@ +/** + * Copyright (c) 2013 - 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 "sdk_common.h" +#if NRF_MODULE_ENABLED(HCI_MEM_POOL) +#include "hci_mem_pool.h" +#include <stdbool.h> +#include <stdio.h> + +/**@brief RX buffer element instance structure. + */ +typedef struct +{ + uint8_t rx_buffer[HCI_RX_BUF_SIZE]; /**< RX buffer memory array. */ + uint32_t length; /**< Length of the RX buffer memory array. */ +} rx_buffer_elem_t; + +/**@brief RX buffer queue element instance structure. + */ +typedef struct +{ + rx_buffer_elem_t * p_buffer; /**< Pointer to RX buffer element. */ + uint32_t free_window_count; /**< Free space element count. */ + uint32_t free_available_count; /**< Free area element count. */ + uint32_t read_available_count; /**< Read area element count. */ + uint32_t write_index; /**< Write position index. */ + uint32_t read_index; /**< Read position index. */ + uint32_t free_index; /**< Free position index. */ +} rx_buffer_queue_t; + +static bool m_is_tx_allocated; /**< Boolean value to determine if the TX buffer is allocated. */ +static rx_buffer_elem_t m_rx_buffer_elem_queue[HCI_RX_BUF_QUEUE_SIZE]; /**< RX buffer element instances. */ +static rx_buffer_queue_t m_rx_buffer_queue; /**< RX buffer queue element instance. */ + + +uint32_t hci_mem_pool_open(void) +{ + m_is_tx_allocated = false; + m_rx_buffer_queue.p_buffer = m_rx_buffer_elem_queue; + m_rx_buffer_queue.free_window_count = HCI_RX_BUF_QUEUE_SIZE; + m_rx_buffer_queue.free_available_count = 0; + m_rx_buffer_queue.read_available_count = 0; + m_rx_buffer_queue.write_index = 0; + m_rx_buffer_queue.read_index = 0; + m_rx_buffer_queue.free_index = 0; + + return NRF_SUCCESS; +} + + +uint32_t hci_mem_pool_close(void) +{ + return NRF_SUCCESS; +} + + +uint32_t hci_mem_pool_tx_alloc(void ** pp_buffer) +{ + static uint8_t tx_buffer[HCI_TX_BUF_SIZE]; + + uint32_t err_code; + + if (pp_buffer == NULL) + { + return NRF_ERROR_NULL; + } + + if (!m_is_tx_allocated) + { + m_is_tx_allocated = true; + *pp_buffer = tx_buffer; + err_code = NRF_SUCCESS; + } + else + { + err_code = NRF_ERROR_NO_MEM; + } + + return err_code; +} + + +uint32_t hci_mem_pool_tx_free(void) +{ + m_is_tx_allocated = false; + + return NRF_SUCCESS; +} + + +uint32_t hci_mem_pool_rx_produce(uint32_t length, void ** pp_buffer) +{ + uint32_t err_code; + + if (pp_buffer == NULL) + { + return NRF_ERROR_NULL; + } + *pp_buffer = NULL; + + if (m_rx_buffer_queue.free_window_count != 0) + { + if (length <= HCI_RX_BUF_SIZE) + { + --(m_rx_buffer_queue.free_window_count); + ++(m_rx_buffer_queue.read_available_count); + + *pp_buffer = + m_rx_buffer_queue.p_buffer[m_rx_buffer_queue.write_index].rx_buffer; + + m_rx_buffer_queue.free_index |= (1u << m_rx_buffer_queue.write_index); + + // @note: Adjust the write_index making use of the fact that the buffer size is of + // power of two and two's complement arithmetic. For details refer example to book + // "Making embedded systems: Elicia White". + m_rx_buffer_queue.write_index = + (m_rx_buffer_queue.write_index + 1u) & (HCI_RX_BUF_QUEUE_SIZE - 1u); + + err_code = NRF_SUCCESS; + } + else + { + err_code = NRF_ERROR_DATA_SIZE; + } + } + else + { + err_code = NRF_ERROR_NO_MEM; + } + + return err_code; +} + + +uint32_t hci_mem_pool_rx_consume(uint8_t * p_buffer) +{ + uint32_t err_code; + uint32_t consume_index; + uint32_t start_index; + + if (m_rx_buffer_queue.free_available_count != 0) + { + // Find the buffer that has been freed - + // Start at read_index minus free_available_count and then increment until read index. + err_code = NRF_ERROR_INVALID_ADDR; + consume_index = (m_rx_buffer_queue.read_index - m_rx_buffer_queue.free_available_count) & + (HCI_RX_BUF_QUEUE_SIZE - 1u); + start_index = consume_index; + + do + { + if (m_rx_buffer_queue.p_buffer[consume_index].rx_buffer == p_buffer) + { + m_rx_buffer_queue.free_index ^= (1u << consume_index); + err_code = NRF_SUCCESS; + break; + } + else + { + consume_index = (consume_index + 1u) & (HCI_RX_BUF_QUEUE_SIZE - 1u); + } + } + while (consume_index != m_rx_buffer_queue.read_index); + + while (!(m_rx_buffer_queue.free_index & (1 << start_index)) && + (m_rx_buffer_queue.free_available_count != 0)) + { + --(m_rx_buffer_queue.free_available_count); + ++(m_rx_buffer_queue.free_window_count); + start_index = (consume_index + 1u) & (HCI_RX_BUF_QUEUE_SIZE - 1u); + } + } + else + { + err_code = NRF_ERROR_NO_MEM; + } + + return err_code; +} + + +uint32_t hci_mem_pool_rx_data_size_set(uint32_t length) +{ + // @note: Adjust the write_index making use of the fact that the buffer size is of power + // of two and two's complement arithmetic. For details refer example to book + // "Making embedded systems: Elicia White". + const uint32_t index = (m_rx_buffer_queue.write_index - 1u) & (HCI_RX_BUF_QUEUE_SIZE - 1u); + m_rx_buffer_queue.p_buffer[index].length = length; + + return NRF_SUCCESS; +} + + +uint32_t hci_mem_pool_rx_extract(uint8_t ** pp_buffer, uint32_t * p_length) +{ + uint32_t err_code; + + if ((pp_buffer == NULL) || (p_length == NULL)) + { + return NRF_ERROR_NULL; + } + + if (m_rx_buffer_queue.read_available_count != 0) + { + --(m_rx_buffer_queue.read_available_count); + ++(m_rx_buffer_queue.free_available_count); + + *pp_buffer = + m_rx_buffer_queue.p_buffer[m_rx_buffer_queue.read_index].rx_buffer; + *p_length = + m_rx_buffer_queue.p_buffer[m_rx_buffer_queue.read_index].length; + + // @note: Adjust the write_index making use of the fact that the buffer size is of power + // of two and two's complement arithmetic. For details refer example to book + // "Making embedded systems: Elicia White". + m_rx_buffer_queue.read_index = + (m_rx_buffer_queue.read_index + 1u) & (HCI_RX_BUF_QUEUE_SIZE - 1u); + + err_code = NRF_SUCCESS; + } + else + { + err_code = NRF_ERROR_NO_MEM; + } + + return err_code; +} +#endif //NRF_MODULE_ENABLED(HCI_MEM_POOL) diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/hci/hci_mem_pool.h b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/hci/hci_mem_pool.h new file mode 100644 index 0000000..9b1bf7e --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/hci/hci_mem_pool.h @@ -0,0 +1,168 @@ +/** + * Copyright (c) 2013 - 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. + * + */ +/** @file + * + * @defgroup hci_mem_pool Memory pool + * @{ + * @ingroup app_common + * + * @brief Memory pool implementation + * + * Memory pool implementation, based on circular buffer data structure, which supports asynchronous + * processing of RX data. The current default implementation supports 1 TX buffer and 4 RX buffers. + * The memory managed by the pool is allocated from static storage instead of heap. The internal + * design of the circular buffer implementing the RX memory layout is illustrated in the picture + * below. + * + * @image html memory_pool.svg "Circular buffer design" + * + * The expected call order for the RX APIs is as follows: + * - hci_mem_pool_rx_produce + * - hci_mem_pool_rx_data_size_set + * - hci_mem_pool_rx_extract + * - hci_mem_pool_rx_consume + * + * @warning If the above mentioned expected call order is violated the end result can be undefined. + * + * \par Component specific configuration options + * + * The following compile time configuration options are available to suit various implementations: + * - TX_BUF_SIZE TX buffer size in bytes. + * - RX_BUF_SIZE RX buffer size in bytes. + * - RX_BUF_QUEUE_SIZE RX buffer element size. + */ + +#ifndef HCI_MEM_POOL_H__ +#define HCI_MEM_POOL_H__ + +#include <stdint.h> +#include "nrf_error.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/**@brief Function for opening the module. + * + * @retval NRF_SUCCESS Operation success. + */ +uint32_t hci_mem_pool_open(void); + +/**@brief Function for closing the module. + * + * @retval NRF_SUCCESS Operation success. + */ +uint32_t hci_mem_pool_close(void); + +/**@brief Function for allocating requested amount of TX memory. + * + * @param[out] pp_buffer Pointer to the allocated memory. + * + * @retval NRF_SUCCESS Operation success. Memory was allocated. + * @retval NRF_ERROR_NO_MEM Operation failure. No memory available for allocation. + * @retval NRF_ERROR_NULL Operation failure. NULL pointer supplied. + */ +uint32_t hci_mem_pool_tx_alloc(void ** pp_buffer); + +/**@brief Function for freeing previously allocated TX memory. + * + * @note Memory management follows the FIFO principle meaning that free() order must match the + * alloc(...) order, which is the reason for omitting exact memory block identifier as an + * input parameter. + * + * @retval NRF_SUCCESS Operation success. Memory was freed. + */ +uint32_t hci_mem_pool_tx_free(void); + +/**@brief Function for producing a free RX memory block for usage. + * + * @note Upon produce request amount being 0, NRF_SUCCESS is returned. + * + * @param[in] length Amount, in bytes, of free memory to be produced. + * @param[out] pp_buffer Pointer to the allocated memory. + * + * @retval NRF_SUCCESS Operation success. Free RX memory block produced. + * @retval NRF_ERROR_NO_MEM Operation failure. No suitable memory available for allocation. + * @retval NRF_ERROR_DATA_SIZE Operation failure. Request size exceeds limit. + * @retval NRF_ERROR_NULL Operation failure. NULL pointer supplied. + */ +uint32_t hci_mem_pool_rx_produce(uint32_t length, void ** pp_buffer); + +/**@brief Function for setting the length of the last produced RX memory block. + * + * @warning If call to this API is omitted the end result is that the following call to + * mem_pool_rx_extract will return incorrect data in the p_length output parameter. + * + * @param[in] length Amount, in bytes, of actual memory used. + * + * @retval NRF_SUCCESS Operation success. Length was set. + */ +uint32_t hci_mem_pool_rx_data_size_set(uint32_t length); + +/**@brief Function for extracting a packet, which has been filled with read data, for further + * processing. + * + * @param[out] pp_buffer Pointer to the packet data. + * @param[out] p_length Length of packet data in bytes. + * + * @retval NRF_SUCCESS Operation success. + * @retval NRF_ERROR_NO_MEM Operation failure. No packet available to extract. + * @retval NRF_ERROR_NULL Operation failure. NULL pointer supplied. + */ +uint32_t hci_mem_pool_rx_extract(uint8_t ** pp_buffer, uint32_t * p_length); + +/**@brief Function for freeing previously extracted packet, which has been filled with read data. + * + * @param[in] p_buffer Pointer to consumed buffer. + * + * @retval NRF_SUCCESS Operation success. + * @retval NRF_ERROR_NO_MEM Operation failure. No packet available to free. + * @retval NRF_ERROR_INVALID_ADDR Operation failure. Not a valid pointer. + */ +uint32_t hci_mem_pool_rx_consume(uint8_t * p_buffer); + + +#ifdef __cplusplus +} +#endif + +#endif // HCI_MEM_POOL_H__ + +/** @} */ diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/hci/hci_slip.c b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/hci/hci_slip.c new file mode 100644 index 0000000..ab70b82 --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/hci/hci_slip.c @@ -0,0 +1,457 @@ +/** + * Copyright (c) 2013 - 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 "sdk_common.h" +#if NRF_MODULE_ENABLED(HCI_SLIP) +#include "hci_slip.h" +#include <stdlib.h> +#include "app_uart.h" +#include "nrf_error.h" + +#define APP_SLIP_END 0xC0 /**< SLIP code for identifying the beginning and end of a packet frame.. */ +#define APP_SLIP_ESC 0xDB /**< SLIP escape code. This code is used to specify that the following character is specially encoded. */ +#define APP_SLIP_ESC_END 0xDC /**< SLIP special code. When this code follows 0xDB, this character is interpreted as payload data 0xC0.. */ +#define APP_SLIP_ESC_ESC 0xDD /**< SLIP special code. When this code follows 0xDB, this character is interpreted as payload data 0xDB. */ + +/** @brief States for the SLIP state machine. */ +typedef enum +{ + SLIP_OFF, /**< SLIP state OFF. */ + SLIP_READY, /**< SLIP state ON. */ + SLIP_TRANSMITTING, /**< SLIP state is transmitting indicating write() has been called but data transmission has not completed. */ +} slip_states_t; + +static slip_states_t m_current_state = SLIP_OFF; /** Current state for the SLIP TX state machine. */ + +static hci_slip_event_handler_t m_slip_event_handler; /** Event callback function for handling of SLIP events, @ref hci_slip_evt_type_t . */ + +static const uint8_t * mp_tx_buffer; /** Pointer to the current TX buffer that is in transmission. */ +static uint32_t m_tx_buffer_length; /** Length of the current TX buffer that is in transmission. */ +static volatile uint32_t m_tx_buffer_index; /** Current index for next byte to transmit in the mp_tx_buffer. */ + +static uint8_t * mp_rx_buffer; /** Pointer to the current RX buffer where the next SLIP decoded packet will be stored. */ +static uint32_t m_rx_buffer_length; /** Length of the current RX buffer. */ +static uint32_t m_rx_received_count; /** Number of SLIP decoded bytes received and stored in mp_rx_buffer. */ + + +/**@brief Function for parsing bytes received on the UART until a SLIP escape byte is received. + * + * @param[in] byte Byte received in UART module. + */ +static void handle_rx_byte_default(uint8_t byte); + +/**@brief Function for parsing bytes received on the UART until SLIP end byte is received. + * + * @param[in] byte Byte received in UART module. + */ +static void handle_rx_byte_wait_start(uint8_t byte); + +/**@brief Function for decoding a received SLIP escape byte. + * It will ensure correct decoding of the byte following the SLIP escape byte. + * + * @param[in] byte Byte received in UART module. + */ +static void handle_rx_byte_esc(uint8_t byte); + +/**@brief Function pointer for parsing and decoding SLIP bytes from the UART module. + * + * @param[in] byte Byte received in UART module. + */ +static void (*handle_rx_byte) (uint8_t byte) = handle_rx_byte_wait_start; + +/**@brief Function pointer for sending a byte through the UART module. + */ +static uint32_t send_tx_byte_default(void); + +/**@brief Function for transferring a SLIP escape byte (0xDB) when special bytes are transferred, + * that is 0xC0 and 0xDB. + */ +static uint32_t send_tx_byte_esc(void); + +/**@brief Function for transferring a byte when it collides with SLIP commands and follows the SLIP + * escape byte, that is 0xC0 => 0xDC and 0xDB => 0xDD. + */ +static uint32_t send_tx_byte_encoded(void); + +/**@brief Function for transferring the SLIP end frame byte, 0xC0. + */ +static uint32_t send_tx_byte_end(void); + +/**@brief Function pointer for sending a byte through the UART module. + */ +uint32_t (*send_tx_byte) (void) = send_tx_byte_default; + + +static uint32_t send_tx_byte_end(void) +{ + uint32_t err_code = app_uart_put(APP_SLIP_END); + + if ((err_code == NRF_SUCCESS) && (m_tx_buffer_index == 0)) + { + // Packet transmission started. + send_tx_byte = send_tx_byte_default; + } + + return err_code; +} + + +static uint32_t send_tx_byte_default(void) +{ + uint32_t err_code = app_uart_put(mp_tx_buffer[m_tx_buffer_index]); + + if (err_code == NRF_SUCCESS) + { + m_tx_buffer_index++; + } + + return err_code; +} + + +static uint32_t send_tx_byte_encoded(void) +{ + uint32_t err_code; + + switch (mp_tx_buffer[m_tx_buffer_index]) + { + case APP_SLIP_END: + err_code = app_uart_put(APP_SLIP_ESC_END); + break; + + case APP_SLIP_ESC: + err_code = app_uart_put(APP_SLIP_ESC_ESC); + break; + + default: + err_code = NRF_ERROR_NO_MEM; + break; + } + + if (err_code == NRF_SUCCESS) + { + m_tx_buffer_index++; + send_tx_byte = send_tx_byte_default; + } + + return err_code; +} + + +static uint32_t send_tx_byte_esc(void) +{ + uint32_t err_code = app_uart_put(APP_SLIP_ESC); + + if (err_code == NRF_SUCCESS) + { + send_tx_byte = send_tx_byte_encoded; + } + + return err_code; +} + + +/** @brief Function for transferring the content of the mp_tx_buffer to the UART. + * It continues to transfer bytes until the UART buffer is full or the complete buffer is + * transferred. + */ +static void transmit_buffer(void) +{ + uint32_t err_code = NRF_SUCCESS; + + while (m_tx_buffer_index < m_tx_buffer_length) + { + if ((mp_tx_buffer[m_tx_buffer_index] == APP_SLIP_END || + mp_tx_buffer[m_tx_buffer_index] == APP_SLIP_ESC) && + send_tx_byte == send_tx_byte_default) + { + send_tx_byte = send_tx_byte_esc; + } + + err_code = send_tx_byte(); + + if (err_code == NRF_ERROR_NO_MEM || err_code == NRF_ERROR_BUSY) + { + // No memory left in UART TX buffer. Abort and wait for APP_UART_TX_EMPTY to continue. + return; + } + } + + send_tx_byte = send_tx_byte_end; + + err_code = send_tx_byte(); + + if (err_code == NRF_SUCCESS) + { + // Packet transmission ended. Notify higher level. + m_current_state = SLIP_READY; + + if (m_slip_event_handler != NULL) + { + hci_slip_evt_t event = {HCI_SLIP_TX_DONE, mp_tx_buffer, m_tx_buffer_index}; + + m_slip_event_handler(event); + } + } +} + + +/** @brief Function for handling the reception of a SLIP end byte. + * If the number of bytes received is greater than zero it will call m_slip_event_handler + * with number of bytes received and invalidate the mp_rx_buffer to protect against data + * corruption. + * No new bytes can be received until a new RX buffer is supplied. + */ +static void handle_slip_end(void) +{ + if (m_rx_received_count > 0) + { + // Full packet received, push it up. + if (m_slip_event_handler != NULL) + { + hci_slip_evt_t event = {HCI_SLIP_RX_RDY, mp_rx_buffer, m_rx_received_count}; + + m_rx_received_count = 0; + mp_rx_buffer = NULL; + + m_slip_event_handler(event); + } + } +} + + +static void handle_rx_byte_esc(uint8_t byte) +{ + switch (byte) + { + case APP_SLIP_END: + handle_slip_end(); + break; + + case APP_SLIP_ESC_END: + mp_rx_buffer[m_rx_received_count++] = APP_SLIP_END; + break; + + case APP_SLIP_ESC_ESC: + mp_rx_buffer[m_rx_received_count++] = APP_SLIP_ESC; + break; + + default: + mp_rx_buffer[m_rx_received_count++] = byte; + break; + } + + handle_rx_byte = handle_rx_byte_default; +} + + +static void handle_rx_byte_default(uint8_t byte) +{ + switch (byte) + { + case APP_SLIP_END: + handle_slip_end(); + break; + + case APP_SLIP_ESC: + handle_rx_byte = handle_rx_byte_esc; + break; + + default: + mp_rx_buffer[m_rx_received_count++] = byte; + break; + } +} + + +static void handle_rx_byte_wait_start(uint8_t byte) +{ + if (byte == APP_SLIP_END) + { + handle_rx_byte = handle_rx_byte_default; + } +} + + +/** @brief Function for checking the current index and length of the RX buffer to determine if the + * buffer is full. If an event handler has been registered, the callback function will + * be executed.. + * + * @retval true If RX buffer has overflowed. + * @retval false otherwise. + * + */ +static bool rx_buffer_overflowed(void) +{ + if (mp_rx_buffer == NULL || m_rx_received_count >= m_rx_buffer_length) + { + if (m_slip_event_handler != NULL) + { + hci_slip_evt_t event = {HCI_SLIP_RX_OVERFLOW, mp_rx_buffer, m_rx_received_count}; + m_slip_event_handler(event); + } + + return true; + } + + return false; +} + + +/** @brief Function for handling the UART module event. It parses events from the UART when + * bytes are received/transmitted. + * + * @param[in] uart_event Event received from app_uart module. + */ +static void slip_uart_eventhandler(app_uart_evt_t * uart_event) +{ + if (uart_event->evt_type == APP_UART_TX_EMPTY && m_current_state == SLIP_TRANSMITTING) + { + transmit_buffer(); + } + + if ((uart_event->evt_type == APP_UART_DATA) && (!rx_buffer_overflowed())) + { + handle_rx_byte(uart_event->data.value); + } +} + + +/** @brief Function for enabling the UART module when the SLIP layer is opened. + */ +static uint32_t slip_uart_open(void) +{ + uint32_t err_code; + + app_uart_comm_params_t comm_params = + { + HCI_UART_RX_PIN, + HCI_UART_TX_PIN, + HCI_UART_RTS_PIN, + HCI_UART_CTS_PIN, + (app_uart_flow_control_t)HCI_UART_FLOW_CONTROL, + false, + HCI_UART_BAUDRATE + }; + + err_code = app_uart_init(&comm_params, + NULL, + slip_uart_eventhandler, + APP_IRQ_PRIORITY_LOWEST); + + if (err_code == NRF_SUCCESS) + { + m_current_state = SLIP_READY; + } + + return err_code; +} + + +uint32_t hci_slip_evt_handler_register(hci_slip_event_handler_t event_handler) +{ + m_slip_event_handler = event_handler; + + return NRF_SUCCESS; +} + + +uint32_t hci_slip_open() +{ + switch (m_current_state) + { + case SLIP_OFF: + return slip_uart_open(); + + default: + // Do nothing. + break; + } + + return NRF_SUCCESS; +} + + +uint32_t hci_slip_close() +{ + m_current_state = SLIP_OFF; + uint32_t err_code = app_uart_close(); + + return err_code; +} + + +uint32_t hci_slip_write(const uint8_t * p_buffer, uint32_t length) +{ + if (p_buffer == NULL) + { + return NRF_ERROR_INVALID_ADDR; + } + + switch (m_current_state) + { + case SLIP_READY: + m_tx_buffer_index = 0; + m_tx_buffer_length = length; + mp_tx_buffer = p_buffer; + m_current_state = SLIP_TRANSMITTING; + send_tx_byte = send_tx_byte_end; + + transmit_buffer(); + return NRF_SUCCESS; + + case SLIP_TRANSMITTING: + return NRF_ERROR_NO_MEM; + + case SLIP_OFF: + default: + return NRF_ERROR_INVALID_STATE; + } +} + + +uint32_t hci_slip_rx_buffer_register(uint8_t * p_buffer, uint32_t length) +{ + mp_rx_buffer = p_buffer; + m_rx_buffer_length = length; + m_rx_received_count = 0; + handle_rx_byte = handle_rx_byte_wait_start; + return NRF_SUCCESS; +} +#endif //NRF_MODULE_ENABLED(HCI_SLIP) diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/hci/hci_slip.h b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/hci/hci_slip.h new file mode 100644 index 0000000..7741319 --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/hci/hci_slip.h @@ -0,0 +1,165 @@ +/** + * Copyright (c) 2013 - 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. + * + */ +/** @file + * + * @defgroup hci_slip SLIP module + * @{ + * @ingroup app_common + * + * @brief SLIP layer for supporting packet framing in HCI transport. + * + * @details This module implements SLIP packet framing as described in the Bluetooth Core + * Specification 4.0, Volume 4, Part D, Chapter 3 SLIP Layer. + * + * SLIP framing ensures that all packets sent on the UART are framed as: + * <0xC0> SLIP packet 1 <0xC0> <0xC0> SLIP packet 2 <0xC0>. + * + * The SLIP layer uses events to notify the upper layer when data transmission is complete + * and when a SLIP packet is received. + */ + +#ifndef HCI_SLIP_H__ +#define HCI_SLIP_H__ + +#include <stdint.h> + +#ifdef __cplusplus +extern "C" { +#endif + +/**@brief Event types from the SLIP Layer. */ +typedef enum +{ + HCI_SLIP_RX_RDY, /**< An event indicating that an RX packet is ready to be read. */ + HCI_SLIP_TX_DONE, /**< An event indicating write completion of the TX packet provided in the function call \ref hci_slip_write . */ + HCI_SLIP_RX_OVERFLOW, /**< An event indicating that RX data has been discarded due to lack of free RX memory. */ + HCI_SLIP_ERROR, /**< An event indicating that an unrecoverable error has occurred. */ + HCI_SLIP_EVT_TYPE_MAX /**< Enumeration upper bound. */ +} hci_slip_evt_type_t; + +/**@brief Structure containing an event from the SLIP layer. + */ +typedef struct +{ + hci_slip_evt_type_t evt_type; /**< Type of event. */ + const uint8_t * packet; /**< This field contains a pointer to the packet for which the event relates, i.e. SLIP_TX_DONE: the packet transmitted, SLIP_RX_RDY: the packet received, SLIP_RX_OVERFLOW: The packet which overflow/or NULL if no receive buffer is available. */ + uint32_t packet_length; /**< Packet length, i.e. SLIP_TX_DONE: Bytes transmitted, SLIP_RX_RDY: Bytes received, SLIP_RX_OVERFLOW: index at which the packet overflowed. */ +} hci_slip_evt_t; + +/**@brief Function for the SLIP layer event callback. + */ +typedef void (*hci_slip_event_handler_t)(hci_slip_evt_t event); + +/**@brief Function for registering the event handler provided as parameter and this event handler + * will be used by SLIP layer to send events described in \ref hci_slip_evt_type_t. + * + * @note Multiple registration requests will overwrite any existing registration. + * + * @param[in] event_handler This function is called by the SLIP layer upon an event. + * + * @retval NRF_SUCCESS Operation success. + */ +uint32_t hci_slip_evt_handler_register(hci_slip_event_handler_t event_handler); + +/**@brief Function for opening the SLIP layer. This function must be called before + * \ref hci_slip_write and before any data can be received. + * + * @note Can be called multiple times. + * + * @retval NRF_SUCCESS Operation success. + * + * The SLIP layer module will propagate errors from underlying sub-modules. + * This implementation is using UART module as a physical transmission layer, and hci_slip_open + * executes \ref app_uart_init . For an extended error list, please refer to \ref app_uart_init . + */ +uint32_t hci_slip_open(void); + +/**@brief Function for closing the SLIP layer. After this function is called no data can be + * transmitted or received in this layer. + * + * @note This function can be called multiple times and also for an unopened channel. + * + * @retval NRF_SUCCESS Operation success. + */ +uint32_t hci_slip_close(void); + +/**@brief Function for writing a packet with SLIP encoding. Packet transmission is confirmed when + * the HCI_SLIP_TX_DONE event is received by the function caller. + * + * @param[in] p_buffer Pointer to the packet to transmit. + * @param[in] length Packet length, in bytes. + * + * @retval NRF_SUCCESS Operation success. Packet was encoded and added to the + * transmission queue and an event will be sent upon transmission + * completion. + * @retval NRF_ERROR_NO_MEM Operation failure. Transmission queue is full and packet was not + * added to the transmission queue. Application shall wait for + * the \ref HCI_SLIP_TX_DONE event. After HCI_SLIP_TX_DONE this + * function can be executed for transmission of next packet. + * @retval NRF_ERROR_INVALID_ADDR If a NULL pointer is provided. + * @retval NRF_ERROR_INVALID_STATE Operation failure. Module is not open. + */ +uint32_t hci_slip_write(const uint8_t * p_buffer, uint32_t length); + +/**@brief Function for registering a receive buffer. The receive buffer will be used for storage of + * received and SLIP decoded data. + * No data can be received by the SLIP layer until a receive buffer has been registered. + * + * @note The lifetime of the buffer must be valid during complete reception of data. A static + * buffer is recommended. + * + * @warning Multiple registration requests will overwrite any existing registration. + * + * @param[in] p_buffer Pointer to receive buffer. The received and SLIP decoded packet + * will be placed in this buffer. + * @param[in] length Buffer length, in bytes. + * + * @retval NRF_SUCCESS Operation success. + */ +uint32_t hci_slip_rx_buffer_register(uint8_t * p_buffer, uint32_t length); + + +#ifdef __cplusplus +} +#endif + +#endif // HCI_SLIP_H__ + +/** @} */ diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/hci/hci_transport.c b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/hci/hci_transport.c new file mode 100644 index 0000000..f4d6c02 --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/hci/hci_transport.c @@ -0,0 +1,808 @@ +/** + * Copyright (c) 2013 - 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 "sdk_common.h" +#if NRF_MODULE_ENABLED(HCI_TRANSPORT) +#include "hci_transport.h" +#include "hci_slip.h" +#include "crc16.h" +#include "hci_mem_pool.h" +#include "app_timer.h" +#include "app_error.h" +#include <stdio.h> + +#define PKT_HDR_SIZE 4u /**< Packet header size in number of bytes. */ +#define PKT_CRC_SIZE 2u /**< Packet CRC size in number of bytes. */ +#define PKT_TYPE_VENDOR_SPECIFIC 14u /**< Packet type vendor specific. */ +#define PKT_TYPE_ACK 0 /**< Packet type acknowledgement. */ +#define DATA_INTEGRITY_MASK (1u << 6u) /**< Mask for data integrity bit in the packet header. */ +#define RELIABLE_PKT_MASK (1u << 7u) /**< Mask for reliable packet bit in the packet header. */ +#define INITIAL_ACK_NUMBER_EXPECTED 1u /**< Initial acknowledge number expected. */ +#define INITIAL_ACK_NUMBER_TX INITIAL_ACK_NUMBER_EXPECTED /**< Initial acknowledge number transmitted. */ +#define INVALID_PKT_TYPE 0xFFFFFFFFu /**< Internal invalid packet type value. */ +#define HCI_UART_REG_VALUE_TO_BAUDRATE(BAUDRATE) ((BAUDRATE)/268) /**< Estimated relation between UART baudrate register value and actual baudrate */ +#define MAX_TRANSMISSION_TIME \ + (ROUNDED_DIV((HCI_MAX_PACKET_SIZE_IN_BITS * 1000u), \ + HCI_UART_REG_VALUE_TO_BAUDRATE(HCI_UART_BAUDRATE))) /**< Max transmission time of a single application packet over UART in units of mseconds. */ +#define RETRANSMISSION_TIMEOUT_IN_MS (3u * MAX_TRANSMISSION_TIME) /**< Retransmission timeout for application packet in units of mseconds. */ +#define RETRANSMISSION_TIMEOUT_IN_TICKS APP_TIMER_TICKS(RETRANSMISSION_TIMEOUT_IN_MS) /**< Retransmission timeout for application packet in units of timer ticks. */ +#define MAX_RETRY_COUNT 5u /**< Max retransmission retry count for application packets. */ +#define ACK_BUF_SIZE 5u /**< Length of module internal RX buffer which is big enough to hold an acknowledgement packet. */ + +/**@brief States of the TX state machine. */ +typedef enum +{ + TX_STATE_IDLE, /**< State for: no application transmission packet processing in progress. */ + TX_STATE_PENDING, /**< State for: TX in progress in slip layer and TX-done event is waited for to signal the end of transmission. */ + TX_STATE_ACTIVE /**< State for: application packet has been delivered to slip for transmission and peer transport entity acknowledgement packet is waited for. */ +} tx_state_t; + +/**@brief TX state machine events. */ +typedef enum +{ + TX_EVENT_STATE_ENTRY, /**< Event for: state entry use case. */ + TX_EVENT_SLIP_TX_DONE, /**< Event for: HCI_SLIP_TX_DONE event use case. */ + TX_EVENT_TIMEOUT, /**< Event for: retransmission timeout use case. */ + TX_EVENT_VALID_RX_ACK /**< Event for: valid acknowledgement received for TX packet use case. */ +} tx_event_t; + +static void tx_sm_state_change(tx_state_t new_state); + +static tx_state_t m_tx_state; /**< Current TX state. */ +static hci_transport_tx_done_handler_t m_transport_tx_done_handle; /**< TX done event callback function. */ +static hci_transport_event_handler_t m_transport_event_handle; /**< Event handler callback function. */ +static uint8_t * mp_slip_used_rx_buffer; /**< Reference to RX buffer used by the slip layer. */ +static uint32_t m_packet_expected_seq_number; /**< Sequence number counter of the packet expected to be received . */ +static uint32_t m_packet_transmit_seq_number; /**< Sequence number counter of the transmitted packet for which acknowledgement packet is waited for. */ +static uint8_t * mp_tx_buffer; /**< Pointer to TX application buffer to be transmitted. */ +static uint32_t m_tx_buffer_length; /**< Length of application TX packet data to be transmitted in bytes. */ +static bool m_is_slip_decode_ready; /**< Boolean to determine has slip decode been completed or not. */ +APP_TIMER_DEF(m_app_timer_id); /**< Application timer id. */ +static uint32_t m_tx_retry_counter; /**< Application packet retransmission counter. */ +static hci_transport_tx_done_result_t m_tx_done_result_code; /**< TX done event callback function result code. */ +static uint8_t m_rx_ack_buffer[ACK_BUF_SIZE];/**< RX buffer big enough to hold an acknowledgement packet and which is taken in use upon receiving HCI_SLIP_RX_OVERFLOW event. */ + + +/**@brief Function for validating a received packet. + * + * @param[in] p_buffer Pointer to the packet data. + * @param[in] length Length of packet data in bytes. + * + * @return true if received packet is valid, false in other case. + */ +static bool is_rx_pkt_valid(const uint8_t * p_buffer, uint32_t length) +{ + // Executed packet filtering algorithm order: + // - verify packet overall length + // - verify data integrity bit set + // - verify reliable packet bit set + // - verify supported packet type + // - verify header checksum + // - verify payload length field + // - verify CRC + if (length <= PKT_HDR_SIZE) + { + return false; + } + + if (!(p_buffer[0] & DATA_INTEGRITY_MASK)) + { + return false; + } + + if (!(p_buffer[0] & RELIABLE_PKT_MASK)) + { + return false; + } + + if ((p_buffer[1] & 0x0Fu) != PKT_TYPE_VENDOR_SPECIFIC) + { + return false; + } + + const uint32_t expected_checksum = + ((p_buffer[0] + p_buffer[1] + p_buffer[2] + p_buffer[3])) & 0xFFu; + if (expected_checksum != 0) + { + return false; + } + + const uint16_t crc_calculated = crc16_compute(p_buffer, (length - PKT_CRC_SIZE), NULL); + const uint16_t crc_received = uint16_decode(&p_buffer[length - PKT_CRC_SIZE]); + if (crc_calculated != crc_received) + { + return false; + } + + return true; +} + + +/**@brief Function for getting the sequence number of the next reliable packet expected. + * + * @return sequence number of the next reliable packet expected. + */ +static __INLINE uint8_t packet_number_expected_get(void) +{ + return (uint8_t) m_packet_expected_seq_number; +} + + +/**@brief Function for calculating a packet header checksum. + * + * @param[in] p_hdr Pointer to the packet header. + * + * @return Calculated checksum. + */ +static uint8_t header_checksum_calculate(const uint8_t * p_hdr) +{ + // @note: no pointer validation check needed as already checked by calling function. + uint32_t checksum; + + checksum = p_hdr[0]; + checksum += p_hdr[1]; + checksum += p_hdr[2]; + checksum &= 0xFFu; + checksum = (~checksum + 1u); + + return (uint8_t)checksum; +} + + +/**@brief Function for writing an acknowledgment packet for transmission. + */ +static void ack_transmit(void) +{ + static uint8_t ack_packet[PKT_HDR_SIZE]; + + // TX ACK packet format: + // - Unreliable Packet type + // - Payload Length set to 0 + // - Sequence Number set to 0 + // - Header checksum calculated + // - Acknowledge Number set correctly + ack_packet[0] = (packet_number_expected_get() << 3u); + ack_packet[1] = 0; + ack_packet[2] = 0; + ack_packet[3] = header_checksum_calculate(ack_packet); + + // @note: no return value check needed for hci_slip_write(...) call as acknowledgement packets + // are considered to be from system design point of view unreliable packets.Use case where + // underlying slip layer does not accept a packet for transmission is managed either by: + // - acknowledged by possible future application packet as acknowledgement number header field + // is included + // - protocol peer entity will retransmit the packet + UNUSED_VARIABLE(hci_slip_write(ack_packet, sizeof(ack_packet))); +} + + +/**@brief Function for validating a received packet. + * + * @param[in] p_buffer Pointer to the packet data. + * + * @return sequence number field of the packet header with unrelated data masked out. + */ +static __INLINE uint8_t packet_seq_nmbr_extract(const uint8_t * p_buffer) +{ + return (p_buffer[0] & 0x07u); +} + + +/**@brief Function for incrementing the sequence number counter for next reliable packet expected. + */ +static __INLINE void packet_number_expected_inc(void) +{ + ++m_packet_expected_seq_number; + m_packet_expected_seq_number &= 0x07u; +} + + +/**@brief Function for decoding a packet type field. + * + * @param[in] p_buffer Pointer to the packet data. + * @param[in] length Length of packet data in bytes. + * + * @return Packet type field or INVALID_PKT_TYPE in case of decode error. + */ +static __INLINE uint32_t packet_type_decode(const uint8_t * p_buffer, uint32_t length) +{ + // @note: no pointer validation check needed as allready checked by calling function. + uint32_t return_value; + + if (length >= PKT_HDR_SIZE) + { + return_value = (p_buffer[1] & 0x0Fu); + } + else + { + return_value = INVALID_PKT_TYPE; + } + + return return_value; +} + + +/**@brief Function for processing a received vendor specific packet. + * + * @param[in] p_buffer Pointer to the packet data. + * @param[in] length Length of packet data in bytes. + */ +static void rx_vendor_specific_pkt_type_handle(const uint8_t * p_buffer, uint32_t length) +{ + // @note: no pointer validation check needed as allready checked by calling function. + uint32_t err_code; + + if (is_rx_pkt_valid(p_buffer, length)) + { + // RX packet is valid: validate sequence number. + const uint8_t rx_seq_number = packet_seq_nmbr_extract(p_buffer); + if (packet_number_expected_get() == rx_seq_number) + { + // Sequence number is valid: transmit acknowledgement. + packet_number_expected_inc(); + ack_transmit(); + + m_is_slip_decode_ready = true; + + err_code = hci_mem_pool_rx_data_size_set(length); + APP_ERROR_CHECK(err_code); + + err_code = hci_mem_pool_rx_produce(HCI_RX_BUF_SIZE, (void **)&mp_slip_used_rx_buffer); + APP_ERROR_CHECK_BOOL((err_code == NRF_SUCCESS) || (err_code == NRF_ERROR_NO_MEM)); + + // If memory pool RX buffer produce succeeded we register that buffer to slip layer + // otherwise we register the internal acknowledgement buffer. + err_code = hci_slip_rx_buffer_register( + (err_code == NRF_SUCCESS) ? mp_slip_used_rx_buffer : m_rx_ack_buffer, + (err_code == NRF_SUCCESS) ? HCI_RX_BUF_SIZE : ACK_BUF_SIZE); + + APP_ERROR_CHECK(err_code); + + if (m_transport_event_handle != NULL) + { + // Send application event of RX packet reception. + const hci_transport_evt_t evt = {HCI_TRANSPORT_RX_RDY}; + m_transport_event_handle(evt); + } + } + else + { + // RX packet discarded: sequence number not valid, set the same buffer to slip layer in + // order to avoid buffer overrun. + err_code = hci_slip_rx_buffer_register(mp_slip_used_rx_buffer, HCI_RX_BUF_SIZE); + APP_ERROR_CHECK(err_code); + + // As packet did not have expected sequence number: send acknowledgement with the + // current expected sequence number. + ack_transmit(); + } + } + else + { + // RX packet discarded: reset the same buffer to slip layer in order to avoid buffer + // overrun. + err_code = hci_slip_rx_buffer_register(mp_slip_used_rx_buffer, HCI_RX_BUF_SIZE); + APP_ERROR_CHECK(err_code); + } +} + + +/**@brief Function for getting the sequence number of a reliable TX packet for which peer protocol + * entity acknowledgment is pending. + * + * @return sequence number of a reliable TX packet for which peer protocol entity acknowledgement + * is pending. + */ +static __INLINE uint8_t packet_number_to_transmit_get(void) +{ + return m_packet_transmit_seq_number; +} + + +/**@brief Function for getting the expected acknowledgement number. + * + * @return expected acknowledgement number. + */ +static __INLINE uint8_t expected_ack_number_get(void) +{ + uint8_t seq_nmbr = packet_number_to_transmit_get(); + ++seq_nmbr; + seq_nmbr &= 0x07u; + + return seq_nmbr; +} + + +/**@brief Function for processing a received acknowledgement packet. + * + * Verifies does the received acknowledgement packet has the expected acknowledgement number and + * that the header checksum is correct. + * + * @param[in] p_buffer Pointer to the packet data. + * + * @return true if valid acknowledgement packet received. + */ +static __INLINE bool rx_ack_pkt_type_handle(const uint8_t * p_buffer) +{ + // @note: no pointer validation check needed as allready checked by calling function. + + // Verify header checksum. + const uint32_t expected_checksum = + ((p_buffer[0] + p_buffer[1] + p_buffer[2] + p_buffer[3])) & 0xFFu; + if (expected_checksum != 0) + { + return false; + } + + const uint8_t ack_number = (p_buffer[0] >> 3u) & 0x07u; + + // Verify expected acknowledgment number. + return (ack_number == expected_ack_number_get()); +} + + +/**@brief Function for incrementing the sequence number counter of the TX packet. + */ +static __INLINE void packet_number_tx_inc(void) +{ + ++m_packet_transmit_seq_number; + m_packet_transmit_seq_number &= 0x07u; +} + + +/**@brief Function for TX state machine event processing in a state centric manner. + * + * @param[in] event Type of event occurred. + */ +static void tx_sm_event_handle(tx_event_t event) +{ + uint32_t err_code; + + switch (m_tx_state) + { + case TX_STATE_IDLE: + if (event == TX_EVENT_STATE_ENTRY) + { + err_code = app_timer_stop(m_app_timer_id); + APP_ERROR_CHECK(err_code); + + // Send TX-done event if registered handler exists. + if (m_transport_tx_done_handle != NULL) + { + m_transport_tx_done_handle(m_tx_done_result_code); + } + } + break; + + case TX_STATE_PENDING: + if (event == TX_EVENT_SLIP_TX_DONE) + { + // @note: this call should always succeed as called from HCI_SLIP_TX_DONE context + // and error cases are managed by dedicated error event from the slip layer. + err_code = hci_slip_write(mp_tx_buffer, + (m_tx_buffer_length + PKT_HDR_SIZE + PKT_CRC_SIZE)); + APP_ERROR_CHECK(err_code); + tx_sm_state_change(TX_STATE_ACTIVE); + } + break; + + case TX_STATE_ACTIVE: + switch (event) + { + case TX_EVENT_VALID_RX_ACK: + // Tx sequence number counter incremented as packet transmission + // acknowledged by peer transport entity. + packet_number_tx_inc(); + tx_sm_state_change(TX_STATE_IDLE); + break; + + case TX_EVENT_STATE_ENTRY: + m_tx_retry_counter = 0; + err_code = app_timer_start(m_app_timer_id, + RETRANSMISSION_TIMEOUT_IN_TICKS, + NULL); + APP_ERROR_CHECK(err_code); + break; + + case TX_EVENT_TIMEOUT: + if (m_tx_retry_counter != MAX_RETRY_COUNT) + { + ++m_tx_retry_counter; + // @note: no return value check done for hci_slip_write(...) call as current + // system design allows use case where retransmission is not accepted by the + // slip layer due to existing acknowledgement packet transmission in the + // slip layer. + UNUSED_VARIABLE(hci_slip_write(mp_tx_buffer, + (m_tx_buffer_length + + PKT_HDR_SIZE + + PKT_CRC_SIZE))); + } + else + { + // Application packet retransmission count reached: + // - set correct TX done event callback function result code + // - execute state change + // @note: m_tx_retry_counter is reset in TX_STATE_ACTIVE state entry. + m_tx_done_result_code = HCI_TRANSPORT_TX_DONE_FAILURE; + tx_sm_state_change(TX_STATE_IDLE); + } + break; + + default: + // No implementation needed. + break; + } + break; + + default: + // No implementation needed. + break; + } +} + + +/**@brief Function for changing the state of the TX state machine. + * + * @param[in] new_state State TX state machine transits to. + */ +static void tx_sm_state_change(tx_state_t new_state) +{ + m_tx_state = new_state; + tx_sm_event_handle(TX_EVENT_STATE_ENTRY); +} + + +/**@brief Function for handling slip events. + * + * @param[in] event The event structure. + */ +void slip_event_handle(hci_slip_evt_t event) +{ + uint32_t return_code; + uint32_t err_code; + + switch (event.evt_type) + { + case HCI_SLIP_TX_DONE: + tx_sm_event_handle(TX_EVENT_SLIP_TX_DONE); + break; + + case HCI_SLIP_RX_RDY: + return_code = packet_type_decode(event.packet, event.packet_length); + + switch (return_code) + { + case PKT_TYPE_VENDOR_SPECIFIC: + rx_vendor_specific_pkt_type_handle(event.packet, event.packet_length); + break; + + case PKT_TYPE_ACK: + if (rx_ack_pkt_type_handle(event.packet)) + { + // Valid expected acknowledgement packet received: set correct TX done event + // callback function result code and execute state change. + m_tx_done_result_code = HCI_TRANSPORT_TX_DONE_SUCCESS; + tx_sm_event_handle(TX_EVENT_VALID_RX_ACK); + } + + /* fall-through */ + default: + // RX packet dropped: reset memory buffer to slip in order to avoid RX buffer + // overflow. + // If existing mem pool produced RX buffer exists reuse that one. If existing + // mem pool produced RX buffer does not exist try to produce new one. If + // producing fails use the internal acknowledgement buffer. + if (mp_slip_used_rx_buffer != NULL) + { + err_code = hci_slip_rx_buffer_register(mp_slip_used_rx_buffer, HCI_RX_BUF_SIZE); + APP_ERROR_CHECK(err_code); + } + else + { + err_code = hci_mem_pool_rx_produce(HCI_RX_BUF_SIZE, + (void **)&mp_slip_used_rx_buffer); + APP_ERROR_CHECK_BOOL((err_code == NRF_SUCCESS) || + (err_code == NRF_ERROR_NO_MEM)); + + err_code = hci_slip_rx_buffer_register( + (err_code == NRF_SUCCESS) ? mp_slip_used_rx_buffer : m_rx_ack_buffer, + (err_code == NRF_SUCCESS) ? HCI_RX_BUF_SIZE : ACK_BUF_SIZE); + APP_ERROR_CHECK(err_code); + } + break; + } + break; + + case HCI_SLIP_RX_OVERFLOW: + err_code = hci_slip_rx_buffer_register(m_rx_ack_buffer, ACK_BUF_SIZE); + APP_ERROR_CHECK(err_code); + break; + + case HCI_SLIP_ERROR: + APP_ERROR_HANDLER(event.evt_type); + break; + + default: + APP_ERROR_HANDLER(event.evt_type); + break; + } +} + + +uint32_t hci_transport_evt_handler_reg(hci_transport_event_handler_t event_handler) +{ + uint32_t err_code; + + m_transport_event_handle = event_handler; + err_code = hci_slip_evt_handler_register(slip_event_handle); + APP_ERROR_CHECK(err_code); + + return (event_handler != NULL) ? NRF_SUCCESS : NRF_ERROR_NULL; +} + + +uint32_t hci_transport_tx_done_register(hci_transport_tx_done_handler_t event_handler) +{ + uint32_t err_code; + + m_transport_tx_done_handle = event_handler; + err_code = hci_slip_evt_handler_register(slip_event_handle); + APP_ERROR_CHECK(err_code); + + return (event_handler != NULL) ? NRF_SUCCESS : NRF_ERROR_NULL; +} + + +/**@brief Function for handling the application packet retransmission timeout. + * + * This function is registered in the @ref app_timer module when a timer is created on + * @ref hci_transport_open. + * + * @note This function must be executed in APP-LO context otherwise retransmission behaviour is + * undefined, see @ref nrf51_system_integration_serialization. + * + * @param[in] p_context The timeout context. + */ +void hci_transport_timeout_handle(void * p_context) +{ + tx_sm_event_handle(TX_EVENT_TIMEOUT); +} + + +uint32_t hci_transport_open(void) +{ + mp_tx_buffer = NULL; + m_tx_buffer_length = 0; + m_tx_retry_counter = 0; + m_is_slip_decode_ready = false; + m_tx_state = TX_STATE_IDLE; + m_packet_expected_seq_number = INITIAL_ACK_NUMBER_EXPECTED; + m_packet_transmit_seq_number = INITIAL_ACK_NUMBER_TX; + m_tx_done_result_code = HCI_TRANSPORT_TX_DONE_FAILURE; + + uint32_t err_code = app_timer_create(&m_app_timer_id, + APP_TIMER_MODE_REPEATED, + hci_transport_timeout_handle); + if (err_code != NRF_SUCCESS) + { + // @note: conduct required interface adjustment. + return NRF_ERROR_INTERNAL; + } + + err_code = hci_mem_pool_open(); + VERIFY_SUCCESS(err_code); + + err_code = hci_slip_open(); + VERIFY_SUCCESS(err_code); + + err_code = hci_mem_pool_rx_produce(HCI_RX_BUF_SIZE, (void **)&mp_slip_used_rx_buffer); + if (err_code != NRF_SUCCESS) + { + // @note: conduct required interface adjustment. + return NRF_ERROR_INTERNAL; + } + + err_code = hci_slip_rx_buffer_register(mp_slip_used_rx_buffer, HCI_RX_BUF_SIZE); + + return err_code; +} + + +uint32_t hci_transport_close(void) +{ + uint32_t err_code; + + m_transport_tx_done_handle = NULL; + m_transport_event_handle = NULL; + + err_code = hci_mem_pool_close(); + APP_ERROR_CHECK(err_code); + err_code = hci_slip_close(); + APP_ERROR_CHECK(err_code); + + // @note: NRF_ERROR_NO_MEM is the only return value which should never be returned. + err_code = app_timer_stop(m_app_timer_id); + APP_ERROR_CHECK_BOOL(err_code != NRF_ERROR_NO_MEM); + + return NRF_SUCCESS; +} + + +uint32_t hci_transport_tx_alloc(uint8_t ** pp_memory) +{ + const uint32_t err_code = hci_mem_pool_tx_alloc((void **)pp_memory); + if (err_code == NRF_SUCCESS) + { + // @note: no need to validate pp_memory against null as validation has already been done + // by hci_mem_pool_tx_alloc(...) and visible to us from the method return code. + //lint -e(413) "Likely use of null pointer" + *pp_memory += PKT_HDR_SIZE; + } + + return err_code; +} + + +uint32_t hci_transport_tx_free(void) +{ + return hci_mem_pool_tx_free(); +} + + +/**@brief Function for constructing 1st byte of the packet header of the packet to be transmitted. + * + * @return 1st byte of the packet header of the packet to be transmitted + */ +static __INLINE uint8_t tx_packet_byte_zero_construct(void) +{ + const uint32_t value = DATA_INTEGRITY_MASK | + RELIABLE_PKT_MASK | + (packet_number_expected_get() << 3u) | + packet_number_to_transmit_get(); + + return (uint8_t) value; +} + + +/**@brief Function for handling the application packet write request in tx-idle state. + */ +static uint32_t pkt_write_handle(void) +{ + uint32_t err_code; + + // Set packet header fields. + + mp_tx_buffer -= PKT_HDR_SIZE; + mp_tx_buffer[0] = tx_packet_byte_zero_construct(); + + const uint16_t type_and_length_fields = ((m_tx_buffer_length << 4u) | PKT_TYPE_VENDOR_SPECIFIC); + // @note: no use case for uint16_encode(...) return value. + UNUSED_VARIABLE(uint16_encode(type_and_length_fields, &(mp_tx_buffer[1]))); + mp_tx_buffer[3] = header_checksum_calculate(mp_tx_buffer); + + // Calculate, append CRC to the packet and write it. + + const uint16_t crc = crc16_compute(mp_tx_buffer, (PKT_HDR_SIZE + m_tx_buffer_length), NULL); + // @note: no use case for uint16_encode(...) return value. + UNUSED_VARIABLE(uint16_encode(crc, &(mp_tx_buffer[PKT_HDR_SIZE + m_tx_buffer_length]))); + err_code = hci_slip_write(mp_tx_buffer, (m_tx_buffer_length + PKT_HDR_SIZE + PKT_CRC_SIZE)); + switch (err_code) + { + case NRF_SUCCESS: + tx_sm_state_change(TX_STATE_ACTIVE); + break; + + case NRF_ERROR_NO_MEM: + tx_sm_state_change(TX_STATE_PENDING); + err_code = NRF_SUCCESS; + break; + + default: + // No implementation needed. + break; + } + + return err_code; +} + + +uint32_t hci_transport_pkt_write(const uint8_t * p_buffer, uint16_t length) +{ + uint32_t err_code; + + if (p_buffer) + { + switch (m_tx_state) + { + case TX_STATE_IDLE: + mp_tx_buffer = (uint8_t *)p_buffer; + m_tx_buffer_length = length; + err_code = pkt_write_handle(); + break; + + default: + err_code = NRF_ERROR_NO_MEM; + break; + } + } + else + { + err_code = NRF_ERROR_NULL; + } + + return err_code; +} + + +uint32_t hci_transport_rx_pkt_extract(uint8_t ** pp_buffer, uint16_t * p_length) +{ + uint32_t err_code; + + if (pp_buffer != NULL && p_length != NULL) + { + uint32_t length = 0; + + if (m_is_slip_decode_ready) + { + m_is_slip_decode_ready = false; + err_code = hci_mem_pool_rx_extract(pp_buffer, &length); + length -= (PKT_HDR_SIZE + PKT_CRC_SIZE); + + *p_length = (uint16_t)length; + *pp_buffer += PKT_HDR_SIZE; + } + else + { + err_code = NRF_ERROR_NO_MEM; + } + } + else + { + err_code = NRF_ERROR_NULL; + } + + return err_code; +} + + +uint32_t hci_transport_rx_pkt_consume(uint8_t * p_buffer) +{ + return (hci_mem_pool_rx_consume(p_buffer - PKT_HDR_SIZE)); +} +#endif //NRF_MODULE_ENABLED(HCI_TRANSPORT) diff --git a/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/hci/hci_transport.h b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/hci/hci_transport.h new file mode 100644 index 0000000..df5df35 --- /dev/null +++ b/thirdparty/nRF5_SDK_15.0.0_a53641a/components/libraries/hci/hci_transport.h @@ -0,0 +1,256 @@ +/** + * Copyright (c) 2013 - 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. + * + */ +/**@file + * + * @defgroup hci_transport HCI Transport + * @{ + * @ingroup app_common + * + * @brief HCI transport module implementation. + * + * This module implements certain specific features from the three-wire UART transport layer, + * defined by the Bluetooth specification version 4.0 [Vol 4] part D. + * + * \par Features supported + * - Transmission and reception of Vendor Specific HCI packet type application packets. + * - Transmission and reception of reliable packets: defined by chapter 6 of the specification. + * + * \par Features not supported + * - Link establishment procedure: defined by chapter 8 of the specification. + * - Low power: defined by chapter 9 of the specification. + * + * \par Implementation specific behaviour + * - As Link establishment procedure is not supported following static link configuration parameters + * are used: + * + TX window size is 1. + * + 16 bit CCITT-CRC must be used. + * + Out of frame software flow control not supported. + * + Parameters specific for resending reliable packets are compile time configurable (clarifed + * later in this document). + * + Acknowledgement packet transmissions are not timeout driven , meaning they are delivered for + * transmission within same context which the corresponding application packet was received. + * + * \par Implementation specific limitations + * Current implementation has the following limitations which will have impact to system wide + * behaviour: + * - Delayed acknowledgement scheduling not implemented: + * There exists a possibility that acknowledgement TX packet and application TX packet will collide + * in the TX pipeline having the end result that acknowledgement packet will be excluded from the TX + * pipeline which will trigger the retransmission algorithm within the peer protocol entity. + * - Delayed retransmission scheduling not implemented: + * There exists a possibility that retransmitted application TX packet and acknowledgement TX packet + * will collide in the TX pipeline having the end result that retransmitted application TX packet + * will be excluded from the TX pipeline. + * - Processing of the acknowledgement number from RX application packets: + * Acknowledgement number is not processed from the RX application packets having the end result + * that unnecessary application packet retransmissions can occur. + * + * The application TX packet processing flow is illustrated by the statemachine below. + * + * @image html hci_transport_tx_sm.svg "TX - application packet statemachine" + * + * \par Component specific configuration options + * + * The following compile time configuration options are available, and used to configure the + * application TX packet retransmission interval, in order to suite various application specific + * implementations: + * - MAC_PACKET_SIZE_IN_BITS Maximum size of a single application packet in bits. + * - USED_BAUD_RATE Used uart baudrate. + * + * The following compile time configuration option is available to configure module specific + * behaviour: + * - MAX_RETRY_COUNT Max retransmission retry count for applicaton packets. + */ + +#ifndef HCI_TRANSPORT_H__ +#define HCI_TRANSPORT_H__ + +#include <stdint.h> +#include "nrf_error.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/**@brief Generic event callback function events. */ +typedef enum +{ + HCI_TRANSPORT_RX_RDY, /**< An event indicating that RX packet is ready for read. */ + HCI_TRANSPORT_EVT_TYPE_MAX /**< Enumeration upper bound. */ +} hci_transport_evt_type_t; + +/**@brief Struct containing events from the Transport layer. + */ +typedef struct +{ + hci_transport_evt_type_t evt_type; /**< Type of event. */ +} hci_transport_evt_t; + +/**@brief Transport layer generic event callback function type. + * + * @param[in] event Transport layer event. + */ +typedef void (*hci_transport_event_handler_t)(hci_transport_evt_t event); + +/**@brief TX done event callback function result codes. */ +typedef enum +{ + HCI_TRANSPORT_TX_DONE_SUCCESS, /**< Transmission success, peer transport entity has acknowledged the transmission. */ + HCI_TRANSPORT_TX_DONE_FAILURE /**< Transmission failure. */ +} hci_transport_tx_done_result_t; + +/**@brief Transport layer TX done event callback function type. + * + * @param[in] result TX done event result code. + */ +typedef void (*hci_transport_tx_done_handler_t)(hci_transport_tx_done_result_t result); + +/**@brief Function for registering a generic event handler. + * + * @note Multiple registration requests will overwrite any possible existing registration. + * + * @param[in] event_handler The function to be called by the transport layer upon an event. + * + * @retval NRF_SUCCESS Operation success. + * @retval NRF_ERROR_NULL Operation failure. NULL pointer supplied. + */ +uint32_t hci_transport_evt_handler_reg(hci_transport_event_handler_t event_handler); + +/**@brief Function for registering a handler for TX done event. + * + * @note Multiple registration requests will overwrite any possible existing registration. + * + * @param[in] event_handler The function to be called by the transport layer upon TX done + * event. + * + * @retval NRF_SUCCESS Operation success. + * @retval NRF_ERROR_NULL Operation failure. NULL pointer supplied. + */ +uint32_t hci_transport_tx_done_register(hci_transport_tx_done_handler_t event_handler); + +/**@brief Function for opening the transport channel and initializing the transport layer. + * + * @warning Must not be called for a channel which has been allready opened. + * + * @retval NRF_SUCCESS Operation success. + * @retval NRF_ERROR_INTERNAL Operation failure. Internal error ocurred. + */ +uint32_t hci_transport_open(void); + +/**@brief Function for closing the transport channel. + * + * @note Can be called multiple times and also for not opened channel. + * + * @retval NRF_SUCCESS Operation success. + */ +uint32_t hci_transport_close(void); + +/**@brief Function for allocating tx packet memory. + * + * @param[out] pp_memory Pointer to the packet data. + * + * @retval NRF_SUCCESS Operation success. Memory was allocated. + * @retval NRF_ERROR_NO_MEM Operation failure. No memory available. + * @retval NRF_ERROR_NULL Operation failure. NULL pointer supplied. + */ +uint32_t hci_transport_tx_alloc(uint8_t ** pp_memory); + +/**@brief Function for freeing tx packet memory. + * + * @note Memory management works in FIFO principle meaning that free order must match the alloc + * order. + * + * @retval NRF_SUCCESS Operation success. Memory was freed. + */ +uint32_t hci_transport_tx_free(void); + +/**@brief Function for writing a packet. + * + * @note Completion of this method does not guarantee that actual peripheral transmission would + * have completed. + * + * @note In case of 0 byte packet length write request, message will consist of only transport + * module specific headers. + * + * @retval NRF_SUCCESS Operation success. Packet was added to the transmission queue + * and an event will be send upon transmission completion. + * @retval NRF_ERROR_NO_MEM Operation failure. Transmission queue is full and packet was not + * added to the transmission queue. User should wait for + * a appropriate event prior issuing this operation again. + * @retval NRF_ERROR_DATA_SIZE Operation failure. Packet size exceeds limit. + * @retval NRF_ERROR_NULL Operation failure. NULL pointer supplied. + * @retval NRF_ERROR_INVALID_STATE Operation failure. Channel is not open. + */ +uint32_t hci_transport_pkt_write(const uint8_t * p_buffer, uint16_t length); + +/**@brief Function for extracting received packet. + * + * @note Extracted memory can't be reused by the underlying transport layer untill freed by call to + * hci_transport_rx_pkt_consume(). + * + * @param[out] pp_buffer Pointer to the packet data. + * @param[out] p_length Length of packet data in bytes. + * + * @retval NRF_SUCCESS Operation success. Packet was extracted. + * @retval NRF_ERROR_NO_MEM Operation failure. No packet available to extract. + * @retval NRF_ERROR_NULL Operation failure. NULL pointer supplied. + */ +uint32_t hci_transport_rx_pkt_extract(uint8_t ** pp_buffer, uint16_t * p_length); + +/**@brief Function for consuming extracted packet described by p_buffer. + * + * RX memory pointed to by p_buffer is freed and can be reused by the underlying transport layer. + * + * @param[in] p_buffer Pointer to the buffer that has been consumed. + * + * @retval NRF_SUCCESS Operation success. + * @retval NRF_ERROR_NO_MEM Operation failure. No packet available to consume. + * @retval NRF_ERROR_INVALID_ADDR Operation failure. Not a valid pointer. + */ +uint32_t hci_transport_rx_pkt_consume(uint8_t * p_buffer); + + +#ifdef __cplusplus +} +#endif + +#endif // HCI_TRANSPORT_H__ + +/** @} */ |