hci_transport.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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_transport HCI Transport
  43. * @{
  44. * @ingroup app_common
  45. *
  46. * @brief HCI transport module implementation.
  47. *
  48. * This module implements certain specific features from the three-wire UART transport layer,
  49. * defined by the Bluetooth specification version 4.0 [Vol 4] part D.
  50. *
  51. * \par Features supported
  52. * - Transmission and reception of Vendor Specific HCI packet type application packets.
  53. * - Transmission and reception of reliable packets: defined by chapter 6 of the specification.
  54. *
  55. * \par Features not supported
  56. * - Link establishment procedure: defined by chapter 8 of the specification.
  57. * - Low power: defined by chapter 9 of the specification.
  58. *
  59. * \par Implementation specific behaviour
  60. * - As Link establishment procedure is not supported following static link configuration parameters
  61. * are used:
  62. * + TX window size is 1.
  63. * + 16 bit CCITT-CRC must be used.
  64. * + Out of frame software flow control not supported.
  65. * + Parameters specific for resending reliable packets are compile time configurable (clarifed
  66. * later in this document).
  67. * + Acknowledgement packet transmissions are not timeout driven , meaning they are delivered for
  68. * transmission within same context which the corresponding application packet was received.
  69. *
  70. * \par Implementation specific limitations
  71. * Current implementation has the following limitations which will have impact to system wide
  72. * behaviour:
  73. * - Delayed acknowledgement scheduling not implemented:
  74. * There exists a possibility that acknowledgement TX packet and application TX packet will collide
  75. * in the TX pipeline having the end result that acknowledgement packet will be excluded from the TX
  76. * pipeline which will trigger the retransmission algorithm within the peer protocol entity.
  77. * - Delayed retransmission scheduling not implemented:
  78. * There exists a possibility that retransmitted application TX packet and acknowledgement TX packet
  79. * will collide in the TX pipeline having the end result that retransmitted application TX packet
  80. * will be excluded from the TX pipeline.
  81. * - Processing of the acknowledgement number from RX application packets:
  82. * Acknowledgement number is not processed from the RX application packets having the end result
  83. * that unnecessary application packet retransmissions can occur.
  84. *
  85. * The application TX packet processing flow is illustrated by the statemachine below.
  86. *
  87. * @image html hci_transport_tx_sm.svg "TX - application packet statemachine"
  88. *
  89. * \par Component specific configuration options
  90. *
  91. * The following compile time configuration options are available, and used to configure the
  92. * application TX packet retransmission interval, in order to suite various application specific
  93. * implementations:
  94. * - MAC_PACKET_SIZE_IN_BITS Maximum size of a single application packet in bits.
  95. * - USED_BAUD_RATE Used uart baudrate.
  96. *
  97. * The following compile time configuration option is available to configure module specific
  98. * behaviour:
  99. * - MAX_RETRY_COUNT Max retransmission retry count for applicaton packets.
  100. */
  101. #ifndef HCI_TRANSPORT_H__
  102. #define HCI_TRANSPORT_H__
  103. #include <stdint.h>
  104. #include "nrf_error.h"
  105. #ifdef __cplusplus
  106. extern "C" {
  107. #endif
  108. /**@brief Generic event callback function events. */
  109. typedef enum
  110. {
  111. HCI_TRANSPORT_RX_RDY, /**< An event indicating that RX packet is ready for read. */
  112. HCI_TRANSPORT_EVT_TYPE_MAX /**< Enumeration upper bound. */
  113. } hci_transport_evt_type_t;
  114. /**@brief Struct containing events from the Transport layer.
  115. */
  116. typedef struct
  117. {
  118. hci_transport_evt_type_t evt_type; /**< Type of event. */
  119. } hci_transport_evt_t;
  120. /**@brief Transport layer generic event callback function type.
  121. *
  122. * @param[in] event Transport layer event.
  123. */
  124. typedef void (*hci_transport_event_handler_t)(hci_transport_evt_t event);
  125. /**@brief TX done event callback function result codes. */
  126. typedef enum
  127. {
  128. HCI_TRANSPORT_TX_DONE_SUCCESS, /**< Transmission success, peer transport entity has acknowledged the transmission. */
  129. HCI_TRANSPORT_TX_DONE_FAILURE /**< Transmission failure. */
  130. } hci_transport_tx_done_result_t;
  131. /**@brief Transport layer TX done event callback function type.
  132. *
  133. * @param[in] result TX done event result code.
  134. */
  135. typedef void (*hci_transport_tx_done_handler_t)(hci_transport_tx_done_result_t result);
  136. /**@brief Function for registering a generic event handler.
  137. *
  138. * @note Multiple registration requests will overwrite any possible existing registration.
  139. *
  140. * @param[in] event_handler The function to be called by the transport layer upon an event.
  141. *
  142. * @retval NRF_SUCCESS Operation success.
  143. * @retval NRF_ERROR_NULL Operation failure. NULL pointer supplied.
  144. */
  145. uint32_t hci_transport_evt_handler_reg(hci_transport_event_handler_t event_handler);
  146. /**@brief Function for registering a handler for TX done event.
  147. *
  148. * @note Multiple registration requests will overwrite any possible existing registration.
  149. *
  150. * @param[in] event_handler The function to be called by the transport layer upon TX done
  151. * event.
  152. *
  153. * @retval NRF_SUCCESS Operation success.
  154. * @retval NRF_ERROR_NULL Operation failure. NULL pointer supplied.
  155. */
  156. uint32_t hci_transport_tx_done_register(hci_transport_tx_done_handler_t event_handler);
  157. /**@brief Function for opening the transport channel and initializing the transport layer.
  158. *
  159. * @warning Must not be called for a channel which has been allready opened.
  160. *
  161. * @retval NRF_SUCCESS Operation success.
  162. * @retval NRF_ERROR_INTERNAL Operation failure. Internal error ocurred.
  163. */
  164. uint32_t hci_transport_open(void);
  165. /**@brief Function for closing the transport channel.
  166. *
  167. * @note Can be called multiple times and also for not opened channel.
  168. *
  169. * @retval NRF_SUCCESS Operation success.
  170. */
  171. uint32_t hci_transport_close(void);
  172. /**@brief Function for allocating tx packet memory.
  173. *
  174. * @param[out] pp_memory Pointer to the packet data.
  175. *
  176. * @retval NRF_SUCCESS Operation success. Memory was allocated.
  177. * @retval NRF_ERROR_NO_MEM Operation failure. No memory available.
  178. * @retval NRF_ERROR_NULL Operation failure. NULL pointer supplied.
  179. */
  180. uint32_t hci_transport_tx_alloc(uint8_t ** pp_memory);
  181. /**@brief Function for freeing tx packet memory.
  182. *
  183. * @note Memory management works in FIFO principle meaning that free order must match the alloc
  184. * order.
  185. *
  186. * @retval NRF_SUCCESS Operation success. Memory was freed.
  187. */
  188. uint32_t hci_transport_tx_free(void);
  189. /**@brief Function for writing a packet.
  190. *
  191. * @note Completion of this method does not guarantee that actual peripheral transmission would
  192. * have completed.
  193. *
  194. * @note In case of 0 byte packet length write request, message will consist of only transport
  195. * module specific headers.
  196. *
  197. * @retval NRF_SUCCESS Operation success. Packet was added to the transmission queue
  198. * and an event will be send upon transmission completion.
  199. * @retval NRF_ERROR_NO_MEM Operation failure. Transmission queue is full and packet was not
  200. * added to the transmission queue. User should wait for
  201. * a appropriate event prior issuing this operation again.
  202. * @retval NRF_ERROR_DATA_SIZE Operation failure. Packet size exceeds limit.
  203. * @retval NRF_ERROR_NULL Operation failure. NULL pointer supplied.
  204. * @retval NRF_ERROR_INVALID_STATE Operation failure. Channel is not open.
  205. */
  206. uint32_t hci_transport_pkt_write(const uint8_t * p_buffer, uint16_t length);
  207. /**@brief Function for extracting received packet.
  208. *
  209. * @note Extracted memory can't be reused by the underlying transport layer untill freed by call to
  210. * hci_transport_rx_pkt_consume().
  211. *
  212. * @param[out] pp_buffer Pointer to the packet data.
  213. * @param[out] p_length Length of packet data in bytes.
  214. *
  215. * @retval NRF_SUCCESS Operation success. Packet was extracted.
  216. * @retval NRF_ERROR_NO_MEM Operation failure. No packet available to extract.
  217. * @retval NRF_ERROR_NULL Operation failure. NULL pointer supplied.
  218. */
  219. uint32_t hci_transport_rx_pkt_extract(uint8_t ** pp_buffer, uint16_t * p_length);
  220. /**@brief Function for consuming extracted packet described by p_buffer.
  221. *
  222. * RX memory pointed to by p_buffer is freed and can be reused by the underlying transport layer.
  223. *
  224. * @param[in] p_buffer Pointer to the buffer that has been consumed.
  225. *
  226. * @retval NRF_SUCCESS Operation success.
  227. * @retval NRF_ERROR_NO_MEM Operation failure. No packet available to consume.
  228. * @retval NRF_ERROR_INVALID_ADDR Operation failure. Not a valid pointer.
  229. */
  230. uint32_t hci_transport_rx_pkt_consume(uint8_t * p_buffer);
  231. #ifdef __cplusplus
  232. }
  233. #endif
  234. #endif // HCI_TRANSPORT_H__
  235. /** @} */