Nordic stack and drivers for the mbed BLE API

Dependents:   BLE_ANCS_SDAPI BLE_temperature BLE_HeartRate writable_gatt ... more

Committer:
Vincent Coubard
Date:
Wed Sep 14 14:39:43 2016 +0100
Revision:
638:c90ae1400bf2
Sync with bdab10dc0f90748b6989c8b577771bb403ca6bd8 from ARMmbed/mbed-os.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Vincent Coubard 638:c90ae1400bf2 1 /*
Vincent Coubard 638:c90ae1400bf2 2 * Copyright (c) Nordic Semiconductor ASA
Vincent Coubard 638:c90ae1400bf2 3 * All rights reserved.
Vincent Coubard 638:c90ae1400bf2 4 *
Vincent Coubard 638:c90ae1400bf2 5 * Redistribution and use in source and binary forms, with or without modification,
Vincent Coubard 638:c90ae1400bf2 6 * are permitted provided that the following conditions are met:
Vincent Coubard 638:c90ae1400bf2 7 *
Vincent Coubard 638:c90ae1400bf2 8 * 1. Redistributions of source code must retain the above copyright notice, this
Vincent Coubard 638:c90ae1400bf2 9 * list of conditions and the following disclaimer.
Vincent Coubard 638:c90ae1400bf2 10 *
Vincent Coubard 638:c90ae1400bf2 11 * 2. Redistributions in binary form must reproduce the above copyright notice, this
Vincent Coubard 638:c90ae1400bf2 12 * list of conditions and the following disclaimer in the documentation and/or
Vincent Coubard 638:c90ae1400bf2 13 * other materials provided with the distribution.
Vincent Coubard 638:c90ae1400bf2 14 *
Vincent Coubard 638:c90ae1400bf2 15 * 3. Neither the name of Nordic Semiconductor ASA nor the names of other
Vincent Coubard 638:c90ae1400bf2 16 * contributors to this software may be used to endorse or promote products
Vincent Coubard 638:c90ae1400bf2 17 * derived from this software without specific prior written permission.
Vincent Coubard 638:c90ae1400bf2 18 *
Vincent Coubard 638:c90ae1400bf2 19 *
Vincent Coubard 638:c90ae1400bf2 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
Vincent Coubard 638:c90ae1400bf2 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
Vincent Coubard 638:c90ae1400bf2 22 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
Vincent Coubard 638:c90ae1400bf2 23 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
Vincent Coubard 638:c90ae1400bf2 24 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
Vincent Coubard 638:c90ae1400bf2 25 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
Vincent Coubard 638:c90ae1400bf2 26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
Vincent Coubard 638:c90ae1400bf2 27 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Vincent Coubard 638:c90ae1400bf2 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
Vincent Coubard 638:c90ae1400bf2 29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Vincent Coubard 638:c90ae1400bf2 30 *
Vincent Coubard 638:c90ae1400bf2 31 */
Vincent Coubard 638:c90ae1400bf2 32
Vincent Coubard 638:c90ae1400bf2 33 #include "hci_mem_pool.h"
Vincent Coubard 638:c90ae1400bf2 34 #include "hci_mem_pool_internal.h"
Vincent Coubard 638:c90ae1400bf2 35 #include <stdbool.h>
Vincent Coubard 638:c90ae1400bf2 36 #include <stdio.h>
Vincent Coubard 638:c90ae1400bf2 37
Vincent Coubard 638:c90ae1400bf2 38 /**@brief RX buffer element instance structure.
Vincent Coubard 638:c90ae1400bf2 39 */
Vincent Coubard 638:c90ae1400bf2 40 typedef struct
Vincent Coubard 638:c90ae1400bf2 41 {
Vincent Coubard 638:c90ae1400bf2 42 uint8_t rx_buffer[RX_BUF_SIZE]; /**< RX buffer memory array. */
Vincent Coubard 638:c90ae1400bf2 43 uint32_t length; /**< Length of the RX buffer memory array. */
Vincent Coubard 638:c90ae1400bf2 44 } rx_buffer_elem_t;
Vincent Coubard 638:c90ae1400bf2 45
Vincent Coubard 638:c90ae1400bf2 46 /**@brief RX buffer queue element instance structure.
Vincent Coubard 638:c90ae1400bf2 47 */
Vincent Coubard 638:c90ae1400bf2 48 typedef struct
Vincent Coubard 638:c90ae1400bf2 49 {
Vincent Coubard 638:c90ae1400bf2 50 rx_buffer_elem_t * p_buffer; /**< Pointer to RX buffer element. */
Vincent Coubard 638:c90ae1400bf2 51 uint32_t free_window_count; /**< Free space element count. */
Vincent Coubard 638:c90ae1400bf2 52 uint32_t free_available_count; /**< Free area element count. */
Vincent Coubard 638:c90ae1400bf2 53 uint32_t read_available_count; /**< Read area element count. */
Vincent Coubard 638:c90ae1400bf2 54 uint32_t write_index; /**< Write position index. */
Vincent Coubard 638:c90ae1400bf2 55 uint32_t read_index; /**< Read position index. */
Vincent Coubard 638:c90ae1400bf2 56 uint32_t free_index; /**< Free position index. */
Vincent Coubard 638:c90ae1400bf2 57 } rx_buffer_queue_t;
Vincent Coubard 638:c90ae1400bf2 58
Vincent Coubard 638:c90ae1400bf2 59 static bool m_is_tx_allocated; /**< Boolean value to determine if the TX buffer is allocated. */
Vincent Coubard 638:c90ae1400bf2 60 static rx_buffer_elem_t m_rx_buffer_elem_queue[RX_BUF_QUEUE_SIZE]; /**< RX buffer element instances. */
Vincent Coubard 638:c90ae1400bf2 61 static rx_buffer_queue_t m_rx_buffer_queue; /**< RX buffer queue element instance. */
Vincent Coubard 638:c90ae1400bf2 62
Vincent Coubard 638:c90ae1400bf2 63
Vincent Coubard 638:c90ae1400bf2 64 uint32_t hci_mem_pool_open(void)
Vincent Coubard 638:c90ae1400bf2 65 {
Vincent Coubard 638:c90ae1400bf2 66 m_is_tx_allocated = false;
Vincent Coubard 638:c90ae1400bf2 67 m_rx_buffer_queue.p_buffer = m_rx_buffer_elem_queue;
Vincent Coubard 638:c90ae1400bf2 68 m_rx_buffer_queue.free_window_count = RX_BUF_QUEUE_SIZE;
Vincent Coubard 638:c90ae1400bf2 69 m_rx_buffer_queue.free_available_count = 0;
Vincent Coubard 638:c90ae1400bf2 70 m_rx_buffer_queue.read_available_count = 0;
Vincent Coubard 638:c90ae1400bf2 71 m_rx_buffer_queue.write_index = 0;
Vincent Coubard 638:c90ae1400bf2 72 m_rx_buffer_queue.read_index = 0;
Vincent Coubard 638:c90ae1400bf2 73 m_rx_buffer_queue.free_index = 0;
Vincent Coubard 638:c90ae1400bf2 74
Vincent Coubard 638:c90ae1400bf2 75 return NRF_SUCCESS;
Vincent Coubard 638:c90ae1400bf2 76 }
Vincent Coubard 638:c90ae1400bf2 77
Vincent Coubard 638:c90ae1400bf2 78
Vincent Coubard 638:c90ae1400bf2 79 uint32_t hci_mem_pool_close(void)
Vincent Coubard 638:c90ae1400bf2 80 {
Vincent Coubard 638:c90ae1400bf2 81 return NRF_SUCCESS;
Vincent Coubard 638:c90ae1400bf2 82 }
Vincent Coubard 638:c90ae1400bf2 83
Vincent Coubard 638:c90ae1400bf2 84
Vincent Coubard 638:c90ae1400bf2 85 uint32_t hci_mem_pool_tx_alloc(void ** pp_buffer)
Vincent Coubard 638:c90ae1400bf2 86 {
Vincent Coubard 638:c90ae1400bf2 87 static uint8_t tx_buffer[TX_BUF_SIZE];
Vincent Coubard 638:c90ae1400bf2 88
Vincent Coubard 638:c90ae1400bf2 89 uint32_t err_code;
Vincent Coubard 638:c90ae1400bf2 90
Vincent Coubard 638:c90ae1400bf2 91 if (pp_buffer == NULL)
Vincent Coubard 638:c90ae1400bf2 92 {
Vincent Coubard 638:c90ae1400bf2 93 return NRF_ERROR_NULL;
Vincent Coubard 638:c90ae1400bf2 94 }
Vincent Coubard 638:c90ae1400bf2 95
Vincent Coubard 638:c90ae1400bf2 96 if (!m_is_tx_allocated)
Vincent Coubard 638:c90ae1400bf2 97 {
Vincent Coubard 638:c90ae1400bf2 98 m_is_tx_allocated = true;
Vincent Coubard 638:c90ae1400bf2 99 *pp_buffer = tx_buffer;
Vincent Coubard 638:c90ae1400bf2 100 err_code = NRF_SUCCESS;
Vincent Coubard 638:c90ae1400bf2 101 }
Vincent Coubard 638:c90ae1400bf2 102 else
Vincent Coubard 638:c90ae1400bf2 103 {
Vincent Coubard 638:c90ae1400bf2 104 err_code = NRF_ERROR_NO_MEM;
Vincent Coubard 638:c90ae1400bf2 105 }
Vincent Coubard 638:c90ae1400bf2 106
Vincent Coubard 638:c90ae1400bf2 107 return err_code;
Vincent Coubard 638:c90ae1400bf2 108 }
Vincent Coubard 638:c90ae1400bf2 109
Vincent Coubard 638:c90ae1400bf2 110
Vincent Coubard 638:c90ae1400bf2 111 uint32_t hci_mem_pool_tx_free(void)
Vincent Coubard 638:c90ae1400bf2 112 {
Vincent Coubard 638:c90ae1400bf2 113 m_is_tx_allocated = false;
Vincent Coubard 638:c90ae1400bf2 114
Vincent Coubard 638:c90ae1400bf2 115 return NRF_SUCCESS;
Vincent Coubard 638:c90ae1400bf2 116 }
Vincent Coubard 638:c90ae1400bf2 117
Vincent Coubard 638:c90ae1400bf2 118
Vincent Coubard 638:c90ae1400bf2 119 uint32_t hci_mem_pool_rx_produce(uint32_t length, void ** pp_buffer)
Vincent Coubard 638:c90ae1400bf2 120 {
Vincent Coubard 638:c90ae1400bf2 121 uint32_t err_code;
Vincent Coubard 638:c90ae1400bf2 122
Vincent Coubard 638:c90ae1400bf2 123 if (pp_buffer == NULL)
Vincent Coubard 638:c90ae1400bf2 124 {
Vincent Coubard 638:c90ae1400bf2 125 return NRF_ERROR_NULL;
Vincent Coubard 638:c90ae1400bf2 126 }
Vincent Coubard 638:c90ae1400bf2 127 *pp_buffer = NULL;
Vincent Coubard 638:c90ae1400bf2 128
Vincent Coubard 638:c90ae1400bf2 129 if (m_rx_buffer_queue.free_window_count != 0)
Vincent Coubard 638:c90ae1400bf2 130 {
Vincent Coubard 638:c90ae1400bf2 131 if (length <= RX_BUF_SIZE)
Vincent Coubard 638:c90ae1400bf2 132 {
Vincent Coubard 638:c90ae1400bf2 133 --(m_rx_buffer_queue.free_window_count);
Vincent Coubard 638:c90ae1400bf2 134 ++(m_rx_buffer_queue.read_available_count);
Vincent Coubard 638:c90ae1400bf2 135
Vincent Coubard 638:c90ae1400bf2 136 *pp_buffer =
Vincent Coubard 638:c90ae1400bf2 137 m_rx_buffer_queue.p_buffer[m_rx_buffer_queue.write_index].rx_buffer;
Vincent Coubard 638:c90ae1400bf2 138
Vincent Coubard 638:c90ae1400bf2 139 m_rx_buffer_queue.free_index |= (1u << m_rx_buffer_queue.write_index);
Vincent Coubard 638:c90ae1400bf2 140
Vincent Coubard 638:c90ae1400bf2 141 // @note: Adjust the write_index making use of the fact that the buffer size is of
Vincent Coubard 638:c90ae1400bf2 142 // power of two and two's complement arithmetic. For details refer example to book
Vincent Coubard 638:c90ae1400bf2 143 // "Making embedded systems: Elicia White".
Vincent Coubard 638:c90ae1400bf2 144 m_rx_buffer_queue.write_index =
Vincent Coubard 638:c90ae1400bf2 145 (m_rx_buffer_queue.write_index + 1u) & (RX_BUF_QUEUE_SIZE - 1u);
Vincent Coubard 638:c90ae1400bf2 146
Vincent Coubard 638:c90ae1400bf2 147 err_code = NRF_SUCCESS;
Vincent Coubard 638:c90ae1400bf2 148 }
Vincent Coubard 638:c90ae1400bf2 149 else
Vincent Coubard 638:c90ae1400bf2 150 {
Vincent Coubard 638:c90ae1400bf2 151 err_code = NRF_ERROR_DATA_SIZE;
Vincent Coubard 638:c90ae1400bf2 152 }
Vincent Coubard 638:c90ae1400bf2 153 }
Vincent Coubard 638:c90ae1400bf2 154 else
Vincent Coubard 638:c90ae1400bf2 155 {
Vincent Coubard 638:c90ae1400bf2 156 err_code = NRF_ERROR_NO_MEM;
Vincent Coubard 638:c90ae1400bf2 157 }
Vincent Coubard 638:c90ae1400bf2 158
Vincent Coubard 638:c90ae1400bf2 159 return err_code;
Vincent Coubard 638:c90ae1400bf2 160 }
Vincent Coubard 638:c90ae1400bf2 161
Vincent Coubard 638:c90ae1400bf2 162
Vincent Coubard 638:c90ae1400bf2 163 uint32_t hci_mem_pool_rx_consume(uint8_t * p_buffer)
Vincent Coubard 638:c90ae1400bf2 164 {
Vincent Coubard 638:c90ae1400bf2 165 uint32_t err_code;
Vincent Coubard 638:c90ae1400bf2 166 uint32_t consume_index;
Vincent Coubard 638:c90ae1400bf2 167 uint32_t start_index;
Vincent Coubard 638:c90ae1400bf2 168
Vincent Coubard 638:c90ae1400bf2 169 if (m_rx_buffer_queue.free_available_count != 0)
Vincent Coubard 638:c90ae1400bf2 170 {
Vincent Coubard 638:c90ae1400bf2 171 // Find the buffer that has been freed -
Vincent Coubard 638:c90ae1400bf2 172 // Start at read_index minus free_available_count and then increment until read index.
Vincent Coubard 638:c90ae1400bf2 173 err_code = NRF_ERROR_INVALID_ADDR;
Vincent Coubard 638:c90ae1400bf2 174 consume_index = (m_rx_buffer_queue.read_index - m_rx_buffer_queue.free_available_count) &
Vincent Coubard 638:c90ae1400bf2 175 (RX_BUF_QUEUE_SIZE - 1u);
Vincent Coubard 638:c90ae1400bf2 176 start_index = consume_index;
Vincent Coubard 638:c90ae1400bf2 177
Vincent Coubard 638:c90ae1400bf2 178 do
Vincent Coubard 638:c90ae1400bf2 179 {
Vincent Coubard 638:c90ae1400bf2 180 if (m_rx_buffer_queue.p_buffer[consume_index].rx_buffer == p_buffer)
Vincent Coubard 638:c90ae1400bf2 181 {
Vincent Coubard 638:c90ae1400bf2 182 m_rx_buffer_queue.free_index ^= (1u << consume_index);
Vincent Coubard 638:c90ae1400bf2 183 err_code = NRF_SUCCESS;
Vincent Coubard 638:c90ae1400bf2 184 break;
Vincent Coubard 638:c90ae1400bf2 185 }
Vincent Coubard 638:c90ae1400bf2 186 else
Vincent Coubard 638:c90ae1400bf2 187 {
Vincent Coubard 638:c90ae1400bf2 188 consume_index = (consume_index + 1u) & (RX_BUF_QUEUE_SIZE - 1u);
Vincent Coubard 638:c90ae1400bf2 189 }
Vincent Coubard 638:c90ae1400bf2 190 }
Vincent Coubard 638:c90ae1400bf2 191 while (consume_index != m_rx_buffer_queue.read_index);
Vincent Coubard 638:c90ae1400bf2 192
Vincent Coubard 638:c90ae1400bf2 193 while (!(m_rx_buffer_queue.free_index & (1 << start_index)) &&
Vincent Coubard 638:c90ae1400bf2 194 (m_rx_buffer_queue.free_available_count != 0))
Vincent Coubard 638:c90ae1400bf2 195 {
Vincent Coubard 638:c90ae1400bf2 196 --(m_rx_buffer_queue.free_available_count);
Vincent Coubard 638:c90ae1400bf2 197 ++(m_rx_buffer_queue.free_window_count);
Vincent Coubard 638:c90ae1400bf2 198 start_index = (consume_index + 1u) & (RX_BUF_QUEUE_SIZE - 1u);
Vincent Coubard 638:c90ae1400bf2 199 }
Vincent Coubard 638:c90ae1400bf2 200 }
Vincent Coubard 638:c90ae1400bf2 201 else
Vincent Coubard 638:c90ae1400bf2 202 {
Vincent Coubard 638:c90ae1400bf2 203 err_code = NRF_ERROR_NO_MEM;
Vincent Coubard 638:c90ae1400bf2 204 }
Vincent Coubard 638:c90ae1400bf2 205
Vincent Coubard 638:c90ae1400bf2 206 return err_code;
Vincent Coubard 638:c90ae1400bf2 207 }
Vincent Coubard 638:c90ae1400bf2 208
Vincent Coubard 638:c90ae1400bf2 209
Vincent Coubard 638:c90ae1400bf2 210 uint32_t hci_mem_pool_rx_data_size_set(uint32_t length)
Vincent Coubard 638:c90ae1400bf2 211 {
Vincent Coubard 638:c90ae1400bf2 212 // @note: Adjust the write_index making use of the fact that the buffer size is of power
Vincent Coubard 638:c90ae1400bf2 213 // of two and two's complement arithmetic. For details refer example to book
Vincent Coubard 638:c90ae1400bf2 214 // "Making embedded systems: Elicia White".
Vincent Coubard 638:c90ae1400bf2 215 const uint32_t index = (m_rx_buffer_queue.write_index - 1u) & (RX_BUF_QUEUE_SIZE - 1u);
Vincent Coubard 638:c90ae1400bf2 216 m_rx_buffer_queue.p_buffer[index].length = length;
Vincent Coubard 638:c90ae1400bf2 217
Vincent Coubard 638:c90ae1400bf2 218 return NRF_SUCCESS;
Vincent Coubard 638:c90ae1400bf2 219 }
Vincent Coubard 638:c90ae1400bf2 220
Vincent Coubard 638:c90ae1400bf2 221
Vincent Coubard 638:c90ae1400bf2 222 uint32_t hci_mem_pool_rx_extract(uint8_t ** pp_buffer, uint32_t * p_length)
Vincent Coubard 638:c90ae1400bf2 223 {
Vincent Coubard 638:c90ae1400bf2 224 uint32_t err_code;
Vincent Coubard 638:c90ae1400bf2 225
Vincent Coubard 638:c90ae1400bf2 226 if ((pp_buffer == NULL) || (p_length == NULL))
Vincent Coubard 638:c90ae1400bf2 227 {
Vincent Coubard 638:c90ae1400bf2 228 return NRF_ERROR_NULL;
Vincent Coubard 638:c90ae1400bf2 229 }
Vincent Coubard 638:c90ae1400bf2 230
Vincent Coubard 638:c90ae1400bf2 231 if (m_rx_buffer_queue.read_available_count != 0)
Vincent Coubard 638:c90ae1400bf2 232 {
Vincent Coubard 638:c90ae1400bf2 233 --(m_rx_buffer_queue.read_available_count);
Vincent Coubard 638:c90ae1400bf2 234 ++(m_rx_buffer_queue.free_available_count);
Vincent Coubard 638:c90ae1400bf2 235
Vincent Coubard 638:c90ae1400bf2 236 *pp_buffer =
Vincent Coubard 638:c90ae1400bf2 237 m_rx_buffer_queue.p_buffer[m_rx_buffer_queue.read_index].rx_buffer;
Vincent Coubard 638:c90ae1400bf2 238 *p_length =
Vincent Coubard 638:c90ae1400bf2 239 m_rx_buffer_queue.p_buffer[m_rx_buffer_queue.read_index].length;
Vincent Coubard 638:c90ae1400bf2 240
Vincent Coubard 638:c90ae1400bf2 241 // @note: Adjust the write_index making use of the fact that the buffer size is of power
Vincent Coubard 638:c90ae1400bf2 242 // of two and two's complement arithmetic. For details refer example to book
Vincent Coubard 638:c90ae1400bf2 243 // "Making embedded systems: Elicia White".
Vincent Coubard 638:c90ae1400bf2 244 m_rx_buffer_queue.read_index =
Vincent Coubard 638:c90ae1400bf2 245 (m_rx_buffer_queue.read_index + 1u) & (RX_BUF_QUEUE_SIZE - 1u);
Vincent Coubard 638:c90ae1400bf2 246
Vincent Coubard 638:c90ae1400bf2 247 err_code = NRF_SUCCESS;
Vincent Coubard 638:c90ae1400bf2 248 }
Vincent Coubard 638:c90ae1400bf2 249 else
Vincent Coubard 638:c90ae1400bf2 250 {
Vincent Coubard 638:c90ae1400bf2 251 err_code = NRF_ERROR_NO_MEM;
Vincent Coubard 638:c90ae1400bf2 252 }
Vincent Coubard 638:c90ae1400bf2 253
Vincent Coubard 638:c90ae1400bf2 254 return err_code;
Vincent Coubard 638:c90ae1400bf2 255 }