hci_mem_pool.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /**
  2. * Copyright (c) 2013 - 2019, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. /** @file
  41. *
  42. * @defgroup hci_mem_pool Memory pool
  43. * @{
  44. * @ingroup app_common
  45. *
  46. * @brief Memory pool implementation
  47. *
  48. * Memory pool implementation, based on circular buffer data structure, which supports asynchronous
  49. * processing of RX data. The current default implementation supports 1 TX buffer and 4 RX buffers.
  50. * The memory managed by the pool is allocated from static storage instead of heap. The internal
  51. * design of the circular buffer implementing the RX memory layout is illustrated in the picture
  52. * below.
  53. *
  54. * @image html memory_pool.svg "Circular buffer design"
  55. *
  56. * The expected call order for the RX APIs is as follows:
  57. * - hci_mem_pool_rx_produce
  58. * - hci_mem_pool_rx_data_size_set
  59. * - hci_mem_pool_rx_extract
  60. * - hci_mem_pool_rx_consume
  61. *
  62. * @warning If the above mentioned expected call order is violated the end result can be undefined.
  63. *
  64. * \par Component specific configuration options
  65. *
  66. * The following compile time configuration options are available to suit various implementations:
  67. * - TX_BUF_SIZE TX buffer size in bytes.
  68. * - RX_BUF_SIZE RX buffer size in bytes.
  69. * - RX_BUF_QUEUE_SIZE RX buffer element size.
  70. */
  71. #ifndef HCI_MEM_POOL_H__
  72. #define HCI_MEM_POOL_H__
  73. #include <stdint.h>
  74. #include "nrf_error.h"
  75. #ifdef __cplusplus
  76. extern "C" {
  77. #endif
  78. /**@brief Function for opening the module.
  79. *
  80. * @retval NRF_SUCCESS Operation success.
  81. */
  82. uint32_t hci_mem_pool_open(void);
  83. /**@brief Function for closing the module.
  84. *
  85. * @retval NRF_SUCCESS Operation success.
  86. */
  87. uint32_t hci_mem_pool_close(void);
  88. /**@brief Function for allocating requested amount of TX memory.
  89. *
  90. * @param[out] pp_buffer Pointer to the allocated memory.
  91. *
  92. * @retval NRF_SUCCESS Operation success. Memory was allocated.
  93. * @retval NRF_ERROR_NO_MEM Operation failure. No memory available for allocation.
  94. * @retval NRF_ERROR_NULL Operation failure. NULL pointer supplied.
  95. */
  96. uint32_t hci_mem_pool_tx_alloc(void ** pp_buffer);
  97. /**@brief Function for freeing previously allocated TX memory.
  98. *
  99. * @note Memory management follows the FIFO principle meaning that free() order must match the
  100. * alloc(...) order, which is the reason for omitting exact memory block identifier as an
  101. * input parameter.
  102. *
  103. * @retval NRF_SUCCESS Operation success. Memory was freed.
  104. */
  105. uint32_t hci_mem_pool_tx_free(void);
  106. /**@brief Function for producing a free RX memory block for usage.
  107. *
  108. * @note Upon produce request amount being 0, NRF_SUCCESS is returned.
  109. *
  110. * @param[in] length Amount, in bytes, of free memory to be produced.
  111. * @param[out] pp_buffer Pointer to the allocated memory.
  112. *
  113. * @retval NRF_SUCCESS Operation success. Free RX memory block produced.
  114. * @retval NRF_ERROR_NO_MEM Operation failure. No suitable memory available for allocation.
  115. * @retval NRF_ERROR_DATA_SIZE Operation failure. Request size exceeds limit.
  116. * @retval NRF_ERROR_NULL Operation failure. NULL pointer supplied.
  117. */
  118. uint32_t hci_mem_pool_rx_produce(uint32_t length, void ** pp_buffer);
  119. /**@brief Function for setting the length of the last produced RX memory block.
  120. *
  121. * @warning If call to this API is omitted the end result is that the following call to
  122. * mem_pool_rx_extract will return incorrect data in the p_length output parameter.
  123. *
  124. * @param[in] length Amount, in bytes, of actual memory used.
  125. *
  126. * @retval NRF_SUCCESS Operation success. Length was set.
  127. */
  128. uint32_t hci_mem_pool_rx_data_size_set(uint32_t length);
  129. /**@brief Function for extracting a packet, which has been filled with read data, for further
  130. * processing.
  131. *
  132. * @param[out] pp_buffer Pointer to the packet data.
  133. * @param[out] p_length Length of packet data in bytes.
  134. *
  135. * @retval NRF_SUCCESS Operation success.
  136. * @retval NRF_ERROR_NO_MEM Operation failure. No packet available to extract.
  137. * @retval NRF_ERROR_NULL Operation failure. NULL pointer supplied.
  138. */
  139. uint32_t hci_mem_pool_rx_extract(uint8_t ** pp_buffer, uint32_t * p_length);
  140. /**@brief Function for freeing previously extracted packet, which has been filled with read data.
  141. *
  142. * @param[in] p_buffer Pointer to consumed buffer.
  143. *
  144. * @retval NRF_SUCCESS Operation success.
  145. * @retval NRF_ERROR_NO_MEM Operation failure. No packet available to free.
  146. * @retval NRF_ERROR_INVALID_ADDR Operation failure. Not a valid pointer.
  147. */
  148. uint32_t hci_mem_pool_rx_consume(uint8_t * p_buffer);
  149. #ifdef __cplusplus
  150. }
  151. #endif
  152. #endif // HCI_MEM_POOL_H__
  153. /** @} */