hci_slip.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /**
  2. * Copyright (c) 2013 - 2020, 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_slip SLIP module
  43. * @{
  44. * @ingroup app_common
  45. *
  46. * @brief SLIP layer for supporting packet framing in HCI transport.
  47. *
  48. * @details This module implements SLIP packet framing as described in the Bluetooth Core
  49. * Specification 4.0, Volume 4, Part D, Chapter 3 SLIP Layer.
  50. *
  51. * SLIP framing ensures that all packets sent on the UART are framed as:
  52. * <0xC0> SLIP packet 1 <0xC0> <0xC0> SLIP packet 2 <0xC0>.
  53. *
  54. * The SLIP layer uses events to notify the upper layer when data transmission is complete
  55. * and when a SLIP packet is received.
  56. */
  57. #ifndef HCI_SLIP_H__
  58. #define HCI_SLIP_H__
  59. #include <stdint.h>
  60. #ifdef __cplusplus
  61. extern "C" {
  62. #endif
  63. /**@brief Event types from the SLIP Layer. */
  64. typedef enum
  65. {
  66. HCI_SLIP_RX_RDY, /**< An event indicating that an RX packet is ready to be read. */
  67. HCI_SLIP_TX_DONE, /**< An event indicating write completion of the TX packet provided in the function call \ref hci_slip_write . */
  68. HCI_SLIP_RX_OVERFLOW, /**< An event indicating that RX data has been discarded due to lack of free RX memory. */
  69. HCI_SLIP_ERROR, /**< An event indicating that an unrecoverable error has occurred. */
  70. HCI_SLIP_EVT_TYPE_MAX /**< Enumeration upper bound. */
  71. } hci_slip_evt_type_t;
  72. /**@brief Structure containing an event from the SLIP layer.
  73. */
  74. typedef struct
  75. {
  76. hci_slip_evt_type_t evt_type; /**< Type of event. */
  77. 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. */
  78. 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. */
  79. } hci_slip_evt_t;
  80. /**@brief Function for the SLIP layer event callback.
  81. */
  82. typedef void (*hci_slip_event_handler_t)(hci_slip_evt_t event);
  83. /**@brief Function for registering the event handler provided as parameter and this event handler
  84. * will be used by SLIP layer to send events described in \ref hci_slip_evt_type_t.
  85. *
  86. * @note Multiple registration requests will overwrite any existing registration.
  87. *
  88. * @param[in] event_handler This function is called by the SLIP layer upon an event.
  89. *
  90. * @retval NRF_SUCCESS Operation success.
  91. */
  92. uint32_t hci_slip_evt_handler_register(hci_slip_event_handler_t event_handler);
  93. /**@brief Function for opening the SLIP layer. This function must be called before
  94. * \ref hci_slip_write and before any data can be received.
  95. *
  96. * @note Can be called multiple times.
  97. *
  98. * @retval NRF_SUCCESS Operation success.
  99. *
  100. * The SLIP layer module will propagate errors from underlying sub-modules.
  101. * This implementation is using UART module as a physical transmission layer, and hci_slip_open
  102. * executes \ref app_uart_init . For an extended error list, please refer to \ref app_uart_init .
  103. */
  104. uint32_t hci_slip_open(void);
  105. /**@brief Function for closing the SLIP layer. After this function is called no data can be
  106. * transmitted or received in this layer.
  107. *
  108. * @note This function can be called multiple times and also for an unopened channel.
  109. *
  110. * @retval NRF_SUCCESS Operation success.
  111. */
  112. uint32_t hci_slip_close(void);
  113. /**@brief Function for writing a packet with SLIP encoding. Packet transmission is confirmed when
  114. * the HCI_SLIP_TX_DONE event is received by the function caller.
  115. *
  116. * @param[in] p_buffer Pointer to the packet to transmit.
  117. * @param[in] length Packet length, in bytes.
  118. *
  119. * @retval NRF_SUCCESS Operation success. Packet was encoded and added to the
  120. * transmission queue and an event will be sent upon transmission
  121. * completion.
  122. * @retval NRF_ERROR_NO_MEM Operation failure. Transmission queue is full and packet was not
  123. * added to the transmission queue. Application shall wait for
  124. * the \ref HCI_SLIP_TX_DONE event. After HCI_SLIP_TX_DONE this
  125. * function can be executed for transmission of next packet.
  126. * @retval NRF_ERROR_INVALID_ADDR If a NULL pointer is provided.
  127. * @retval NRF_ERROR_INVALID_STATE Operation failure. Module is not open.
  128. */
  129. uint32_t hci_slip_write(const uint8_t * p_buffer, uint32_t length);
  130. /**@brief Function for registering a receive buffer. The receive buffer will be used for storage of
  131. * received and SLIP decoded data.
  132. * No data can be received by the SLIP layer until a receive buffer has been registered.
  133. *
  134. * @note The lifetime of the buffer must be valid during complete reception of data. A static
  135. * buffer is recommended.
  136. *
  137. * @warning Multiple registration requests will overwrite any existing registration.
  138. *
  139. * @param[in] p_buffer Pointer to receive buffer. The received and SLIP decoded packet
  140. * will be placed in this buffer.
  141. * @param[in] length Buffer length, in bytes.
  142. *
  143. * @retval NRF_SUCCESS Operation success.
  144. */
  145. uint32_t hci_slip_rx_buffer_register(uint8_t * p_buffer, uint32_t length);
  146. #ifdef __cplusplus
  147. }
  148. #endif
  149. #endif // HCI_SLIP_H__
  150. /** @} */