ble_l2cap.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Copyright (c) Nordic Semiconductor ASA
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice, this
  12. * list of conditions and the following disclaimer in the documentation and/or
  13. * other materials provided with the distribution.
  14. *
  15. * 3. Neither the name of Nordic Semiconductor ASA nor the names of other
  16. * contributors to this software may be used to endorse or promote products
  17. * derived from this software without specific prior written permission.
  18. *
  19. * 4. This software must only be used in a processor manufactured by Nordic
  20. * Semiconductor ASA, or in a processor manufactured by a third party that
  21. * is used in combination with a processor manufactured by Nordic Semiconductor.
  22. *
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  25. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  26. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  27. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  28. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  29. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  30. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  31. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  32. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  33. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. */
  36. /**
  37. @addtogroup BLE_L2CAP Logical Link Control and Adaptation Protocol (L2CAP)
  38. @{
  39. @brief Definitions and prototypes for the L2CAP interface.
  40. */
  41. #ifndef BLE_L2CAP_H__
  42. #define BLE_L2CAP_H__
  43. #include "ble_types.h"
  44. #include "ble_ranges.h"
  45. #include "ble_err.h"
  46. #include "nrf_svc.h"
  47. #ifdef __cplusplus
  48. extern "C" {
  49. #endif
  50. /**@addtogroup BLE_L2CAP_ENUMERATIONS Enumerations
  51. * @{ */
  52. /**@brief L2CAP API SVC numbers. */
  53. enum BLE_L2CAP_SVCS
  54. {
  55. SD_BLE_L2CAP_CID_REGISTER = BLE_L2CAP_SVC_BASE, /**< Register a CID. */
  56. SD_BLE_L2CAP_CID_UNREGISTER, /**< Unregister a CID. */
  57. SD_BLE_L2CAP_TX /**< Transmit a packet. */
  58. };
  59. /**@brief L2CAP Event IDs. */
  60. enum BLE_L2CAP_EVTS
  61. {
  62. BLE_L2CAP_EVT_RX = BLE_L2CAP_EVT_BASE /**< L2CAP packet received. */
  63. };
  64. /** @} */
  65. /**@addtogroup BLE_L2CAP_DEFINES Defines
  66. * @{ */
  67. /**@defgroup BLE_ERRORS_L2CAP SVC return values specific to L2CAP
  68. * @{ */
  69. #define BLE_ERROR_L2CAP_CID_IN_USE (NRF_L2CAP_ERR_BASE + 0x000) /**< CID already in use. */
  70. /** @} */
  71. /**@brief Default L2CAP MTU. */
  72. #define BLE_L2CAP_MTU_DEF (23)
  73. /**@brief Invalid Channel Identifier. */
  74. #define BLE_L2CAP_CID_INVALID (0x0000)
  75. /**@brief Dynamic Channel Identifier base. */
  76. #define BLE_L2CAP_CID_DYN_BASE (0x0040)
  77. /**@brief Maximum amount of dynamic CIDs. */
  78. #define BLE_L2CAP_CID_DYN_MAX (8)
  79. /** @} */
  80. /**@addtogroup BLE_L2CAP_STRUCTURES Structures
  81. * @{ */
  82. /**@brief Packet header format for L2CAP transmission. */
  83. typedef struct
  84. {
  85. uint16_t len; /**< Length of valid info in data member. */
  86. uint16_t cid; /**< Channel ID on which packet is transmitted. */
  87. } ble_l2cap_header_t;
  88. /**@brief L2CAP Received packet event report. */
  89. typedef struct
  90. {
  91. ble_l2cap_header_t header; /**< L2CAP packet header. */
  92. uint8_t data[1]; /**< Packet data. @note This is a variable length array. The size of 1 indicated is only a placeholder for compilation.
  93. See @ref sd_ble_evt_get for more information on how to use event structures with variable length array members. */
  94. } ble_l2cap_evt_rx_t;
  95. /**@brief L2CAP event callback event structure. */
  96. typedef struct
  97. {
  98. uint16_t conn_handle; /**< Connection Handle on which event occured. */
  99. union
  100. {
  101. ble_l2cap_evt_rx_t rx; /**< RX Event parameters. */
  102. } params; /**< Event Parameters. */
  103. } ble_l2cap_evt_t;
  104. /** @} */
  105. /**@addtogroup BLE_L2CAP_FUNCTIONS Functions
  106. * @{ */
  107. /**@brief Register a CID with L2CAP.
  108. *
  109. * @details This registers a higher protocol layer with the L2CAP multiplexer, and is requried prior to all operations on the CID.
  110. *
  111. * @mscs
  112. * @mmsc{@ref BLE_L2CAP_API_MSC}
  113. * @endmscs
  114. *
  115. * @param[in] cid L2CAP CID.
  116. *
  117. * @retval ::NRF_SUCCESS Successfully registered a CID with the L2CAP layer.
  118. * @retval ::NRF_ERROR_INVALID_PARAM Invalid parameter(s) supplied, CID must be above @ref BLE_L2CAP_CID_DYN_BASE.
  119. * @retval ::BLE_ERROR_L2CAP_CID_IN_USE L2CAP CID already in use.
  120. * @retval ::NRF_ERROR_NO_MEM Not enough memory to complete operation.
  121. */
  122. SVCALL(SD_BLE_L2CAP_CID_REGISTER, uint32_t, sd_ble_l2cap_cid_register(uint16_t cid));
  123. /**@brief Unregister a CID with L2CAP.
  124. *
  125. * @details This unregisters a previously registerd higher protocol layer with the L2CAP multiplexer.
  126. *
  127. * @mscs
  128. * @mmsc{@ref BLE_L2CAP_API_MSC}
  129. * @endmscs
  130. *
  131. * @param[in] cid L2CAP CID.
  132. *
  133. * @retval ::NRF_SUCCESS Successfully unregistered the CID.
  134. * @retval ::NRF_ERROR_INVALID_PARAM Invalid parameter(s) supplied.
  135. * @retval ::NRF_ERROR_NOT_FOUND CID not previously registered.
  136. */
  137. SVCALL(SD_BLE_L2CAP_CID_UNREGISTER, uint32_t, sd_ble_l2cap_cid_unregister(uint16_t cid));
  138. /**@brief Transmit an L2CAP packet.
  139. *
  140. * @note It is important to note that a call to this function will <b>consume an application packet</b>, and will therefore
  141. * generate a @ref BLE_EVT_TX_COMPLETE event when the packet has been transmitted.
  142. * Please see the documentation of @ref sd_ble_tx_packet_count_get for more details.
  143. *
  144. * @events
  145. * @event{@ref BLE_EVT_TX_COMPLETE}
  146. * @event{@ref BLE_L2CAP_EVT_RX}
  147. * @endevents
  148. *
  149. * @mscs
  150. * @mmsc{@ref BLE_L2CAP_API_MSC}
  151. * @endmscs
  152. *
  153. * @param[in] conn_handle Connection Handle.
  154. * @param[in] p_header Pointer to a packet header containing length and CID.
  155. * @param[in] p_data Pointer to the data to be transmitted.
  156. *
  157. * @retval ::NRF_SUCCESS Successfully queued an L2CAP packet for transmission.
  158. * @retval ::NRF_ERROR_INVALID_ADDR Invalid pointer supplied.
  159. * @retval ::NRF_ERROR_INVALID_PARAM Invalid parameter(s) supplied, CIDs must be registered beforehand with @ref sd_ble_l2cap_cid_register.
  160. * @retval ::NRF_ERROR_NOT_FOUND CID not found.
  161. * @retval ::NRF_ERROR_NO_MEM Not enough memory to complete operation.
  162. * @retval ::BLE_ERROR_NO_TX_PACKETS Not enough application packets available.
  163. * @retval ::NRF_ERROR_DATA_SIZE Invalid data size(s) supplied, see @ref BLE_L2CAP_MTU_DEF.
  164. */
  165. SVCALL(SD_BLE_L2CAP_TX, uint32_t, sd_ble_l2cap_tx(uint16_t conn_handle, ble_l2cap_header_t const *p_header, uint8_t const *p_data));
  166. /** @} */
  167. #ifdef __cplusplus
  168. }
  169. #endif
  170. #endif // BLE_L2CAP_H__
  171. /**
  172. @}
  173. */