ble_l2cap.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. /*
  2. * Copyright (c) 2011 - 2018, 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, except as embedded into a Nordic
  12. * Semiconductor ASA integrated circuit in a product or a software update for
  13. * such product, must reproduce the above copyright notice, this list of
  14. * conditions and the following disclaimer in the documentation and/or other
  15. * materials provided with the distribution.
  16. *
  17. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  18. * contributors may be used to endorse or promote products derived from this
  19. * software without specific prior written permission.
  20. *
  21. * 4. This software, with or without modification, must only be used with a
  22. * Nordic Semiconductor ASA integrated circuit.
  23. *
  24. * 5. Any software provided in binary form under this license must not be reverse
  25. * engineered, decompiled, modified and/or disassembled.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  28. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  29. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  30. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  31. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  32. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  33. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  34. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  35. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  36. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  37. */
  38. /**
  39. @addtogroup BLE_L2CAP Logical Link Control and Adaptation Protocol (L2CAP)
  40. @{
  41. @brief Definitions and prototypes for the L2CAP interface.
  42. */
  43. #ifndef BLE_L2CAP_H__
  44. #define BLE_L2CAP_H__
  45. #include <stdint.h>
  46. #include "nrf_svc.h"
  47. #include "nrf_error.h"
  48. #include "ble_ranges.h"
  49. #include "ble_types.h"
  50. #include "ble_err.h"
  51. #ifdef __cplusplus
  52. extern "C" {
  53. #endif
  54. /**@addtogroup BLE_L2CAP_TERMINOLOGY Terminology
  55. * @{
  56. * @details
  57. *
  58. * L2CAP SDU
  59. * - A data unit that the application can send/receive to/from a peer.
  60. *
  61. * L2CAP PDU
  62. * - A data unit that is exchanged between local and remote L2CAP entities.
  63. * It consists of L2CAP protocol control information and payload fields.
  64. * The payload field can contain an L2CAP SDU or a part of an L2CAP SDU.
  65. *
  66. * L2CAP MTU
  67. * - The maximum length of an L2CAP SDU.
  68. *
  69. * L2CAP MPS
  70. * - The maximum length of an L2CAP PDU payload field.
  71. *
  72. * Credits
  73. * - A value indicating the number of L2CAP PDUs that the receiver of the credit can send to the peer.
  74. * @} */
  75. /**@addtogroup BLE_L2CAP_ENUMERATIONS Enumerations
  76. * @{ */
  77. /**@brief L2CAP API SVC numbers. */
  78. enum BLE_L2CAP_SVCS
  79. {
  80. SD_BLE_L2CAP_CH_SETUP = BLE_L2CAP_SVC_BASE + 0, /**< Set up an L2CAP channel. */
  81. SD_BLE_L2CAP_CH_RELEASE = BLE_L2CAP_SVC_BASE + 1, /**< Release an L2CAP channel. */
  82. SD_BLE_L2CAP_CH_RX = BLE_L2CAP_SVC_BASE + 2, /**< Receive an SDU on an L2CAP channel. */
  83. SD_BLE_L2CAP_CH_TX = BLE_L2CAP_SVC_BASE + 3, /**< Transmit an SDU on an L2CAP channel. */
  84. SD_BLE_L2CAP_CH_FLOW_CONTROL = BLE_L2CAP_SVC_BASE + 4, /**< Advanced SDU reception flow control. */
  85. };
  86. /**@brief L2CAP Event IDs. */
  87. enum BLE_L2CAP_EVTS
  88. {
  89. BLE_L2CAP_EVT_CH_SETUP_REQUEST = BLE_L2CAP_EVT_BASE + 0, /**< L2CAP Channel Setup Request event.
  90. \n See @ref ble_l2cap_evt_ch_setup_request_t. */
  91. BLE_L2CAP_EVT_CH_SETUP_REFUSED = BLE_L2CAP_EVT_BASE + 1, /**< L2CAP Channel Setup Refused event.
  92. \n See @ref ble_l2cap_evt_ch_setup_refused_t. */
  93. BLE_L2CAP_EVT_CH_SETUP = BLE_L2CAP_EVT_BASE + 2, /**< L2CAP Channel Setup Completed event.
  94. \n See @ref ble_l2cap_evt_ch_setup_t. */
  95. BLE_L2CAP_EVT_CH_RELEASED = BLE_L2CAP_EVT_BASE + 3, /**< L2CAP Channel Released event.
  96. \n No additional event structure applies. */
  97. BLE_L2CAP_EVT_CH_SDU_BUF_RELEASED = BLE_L2CAP_EVT_BASE + 4, /**< L2CAP Channel SDU data buffer released event.
  98. \n See @ref ble_l2cap_evt_ch_sdu_buf_released_t. */
  99. BLE_L2CAP_EVT_CH_CREDIT = BLE_L2CAP_EVT_BASE + 5, /**< L2CAP Channel Credit received.
  100. \n See @ref ble_l2cap_evt_ch_credit_t. */
  101. BLE_L2CAP_EVT_CH_RX = BLE_L2CAP_EVT_BASE + 6, /**< L2CAP Channel SDU received.
  102. \n See @ref ble_l2cap_evt_ch_rx_t. */
  103. BLE_L2CAP_EVT_CH_TX = BLE_L2CAP_EVT_BASE + 7, /**< L2CAP Channel SDU transmitted.
  104. \n See @ref ble_l2cap_evt_ch_tx_t. */
  105. };
  106. /** @} */
  107. /**@addtogroup BLE_L2CAP_DEFINES Defines
  108. * @{ */
  109. /**@brief Maximum number of L2CAP channels per connection. */
  110. #define BLE_L2CAP_CH_COUNT_MAX (64)
  111. /**@brief Minimum L2CAP MTU, in bytes. */
  112. #define BLE_L2CAP_MTU_MIN (23)
  113. /**@brief Minimum L2CAP MPS, in bytes. */
  114. #define BLE_L2CAP_MPS_MIN (23)
  115. /**@brief Invalid CID. */
  116. #define BLE_L2CAP_CID_INVALID (0x0000)
  117. /**@brief Default number of credits for @ref sd_ble_l2cap_ch_flow_control. */
  118. #define BLE_L2CAP_CREDITS_DEFAULT (1)
  119. /**@defgroup BLE_L2CAP_CH_SETUP_REFUSED_SRCS L2CAP channel setup refused sources
  120. * @{ */
  121. #define BLE_L2CAP_CH_SETUP_REFUSED_SRC_LOCAL (0x01) /**< Local. */
  122. #define BLE_L2CAP_CH_SETUP_REFUSED_SRC_REMOTE (0x02) /**< Remote. */
  123. /** @} */
  124. /** @defgroup BLE_L2CAP_CH_STATUS_CODES L2CAP channel status codes
  125. * @{ */
  126. #define BLE_L2CAP_CH_STATUS_CODE_SUCCESS (0x0000) /**< Success. */
  127. #define BLE_L2CAP_CH_STATUS_CODE_LE_PSM_NOT_SUPPORTED (0x0002) /**< LE_PSM not supported. */
  128. #define BLE_L2CAP_CH_STATUS_CODE_NO_RESOURCES (0x0004) /**< No resources available. */
  129. #define BLE_L2CAP_CH_STATUS_CODE_INSUFF_AUTHENTICATION (0x0005) /**< Insufficient authentication. */
  130. #define BLE_L2CAP_CH_STATUS_CODE_INSUFF_AUTHORIZATION (0x0006) /**< Insufficient authorization. */
  131. #define BLE_L2CAP_CH_STATUS_CODE_INSUFF_ENC_KEY_SIZE (0x0007) /**< Insufficient encryption key size. */
  132. #define BLE_L2CAP_CH_STATUS_CODE_INSUFF_ENC (0x0008) /**< Insufficient encryption. */
  133. #define BLE_L2CAP_CH_STATUS_CODE_INVALID_SCID (0x0009) /**< Invalid Source CID. */
  134. #define BLE_L2CAP_CH_STATUS_CODE_SCID_ALLOCATED (0x000A) /**< Source CID already allocated. */
  135. #define BLE_L2CAP_CH_STATUS_CODE_UNACCEPTABLE_PARAMS (0x000B) /**< Unacceptable parameters. */
  136. #define BLE_L2CAP_CH_STATUS_CODE_NOT_UNDERSTOOD (0x8000) /**< Command Reject received instead of LE Credit Based Connection Response. */
  137. #define BLE_L2CAP_CH_STATUS_CODE_TIMEOUT (0xC000) /**< Operation timed out. */
  138. /** @} */
  139. /** @} */
  140. /**@addtogroup BLE_L2CAP_STRUCTURES Structures
  141. * @{ */
  142. /**
  143. * @brief BLE L2CAP connection configuration parameters, set with @ref sd_ble_cfg_set.
  144. *
  145. * @note These parameters are set per connection, so all L2CAP channels created on this connection
  146. * will have the same parameters.
  147. *
  148. * @retval ::NRF_ERROR_INVALID_PARAM One or more of the following is true:
  149. * - rx_mps is smaller than @ref BLE_L2CAP_MPS_MIN.
  150. * - tx_mps is smaller than @ref BLE_L2CAP_MPS_MIN.
  151. * - ch_count is greater than @ref BLE_L2CAP_CH_COUNT_MAX.
  152. * @retval ::NRF_ERROR_NO_MEM rx_mps or tx_mps is set too high.
  153. */
  154. typedef struct
  155. {
  156. uint16_t rx_mps; /**< The maximum L2CAP PDU payload size, in bytes, that L2CAP shall
  157. be able to receive on L2CAP channels on connections with this
  158. configuration. The minimum value is @ref BLE_L2CAP_MPS_MIN. */
  159. uint16_t tx_mps; /**< The maximum L2CAP PDU payload size, in bytes, that L2CAP shall
  160. be able to transmit on L2CAP channels on connections with this
  161. configuration. The minimum value is @ref BLE_L2CAP_MPS_MIN. */
  162. uint8_t rx_queue_size; /**< Number of SDU data buffers that can be queued for reception per
  163. L2CAP channel. The minimum value is one. */
  164. uint8_t tx_queue_size; /**< Number of SDU data buffers that can be queued for transmission
  165. per L2CAP channel. The minimum value is one. */
  166. uint8_t ch_count; /**< Number of L2CAP channels the application can create per connection
  167. with this configuration. The default value is zero, the maximum
  168. value is @ref BLE_L2CAP_CH_COUNT_MAX.
  169. @note if this parameter is set to zero, all other parameters in
  170. @ref ble_l2cap_conn_cfg_t are ignored. */
  171. } ble_l2cap_conn_cfg_t;
  172. /**@brief L2CAP channel RX parameters. */
  173. typedef struct
  174. {
  175. uint16_t rx_mtu; /**< The maximum L2CAP SDU size, in bytes, that L2CAP shall be able to
  176. receive on this L2CAP channel.
  177. - Must be equal to or greater than @ref BLE_L2CAP_MTU_MIN. */
  178. uint16_t rx_mps; /**< The maximum L2CAP PDU payload size, in bytes, that L2CAP shall be
  179. able to receive on this L2CAP channel.
  180. - Must be equal to or greater than @ref BLE_L2CAP_MPS_MIN.
  181. - Must be equal to or less than @ref ble_l2cap_conn_cfg_t::rx_mps. */
  182. ble_data_t sdu_buf; /**< SDU data buffer for reception.
  183. - If @ref ble_data_t::p_data is non-NULL, initial credits are
  184. issued to the peer.
  185. - If @ref ble_data_t::p_data is NULL, no initial credits are
  186. issued to the peer. */
  187. } ble_l2cap_ch_rx_params_t;
  188. /**@brief L2CAP channel setup parameters. */
  189. typedef struct
  190. {
  191. ble_l2cap_ch_rx_params_t rx_params; /**< L2CAP channel RX parameters. */
  192. uint16_t le_psm; /**< LE Protocol/Service Multiplexer. Used when requesting
  193. setup of an L2CAP channel, ignored otherwise. */
  194. uint16_t status; /**< Status code, see @ref BLE_L2CAP_CH_STATUS_CODES.
  195. Used when replying to a setup request of an L2CAP
  196. channel, ignored otherwise. */
  197. } ble_l2cap_ch_setup_params_t;
  198. /**@brief L2CAP channel TX parameters. */
  199. typedef struct
  200. {
  201. uint16_t tx_mtu; /**< The maximum L2CAP SDU size, in bytes, that L2CAP is able to
  202. transmit on this L2CAP channel. */
  203. uint16_t peer_mps; /**< The maximum L2CAP PDU payload size, in bytes, that the peer is
  204. able to receive on this L2CAP channel. */
  205. uint16_t tx_mps; /**< The maximum L2CAP PDU payload size, in bytes, that L2CAP is able
  206. to transmit on this L2CAP channel. This is effective tx_mps,
  207. selected by the SoftDevice as
  208. MIN( @ref ble_l2cap_ch_tx_params_t::peer_mps, @ref ble_l2cap_conn_cfg_t::tx_mps ) */
  209. uint16_t credits; /**< Initial credits given by the peer. */
  210. } ble_l2cap_ch_tx_params_t;
  211. /**@brief L2CAP Channel Setup Request event. */
  212. typedef struct
  213. {
  214. ble_l2cap_ch_tx_params_t tx_params; /**< L2CAP channel TX parameters. */
  215. uint16_t le_psm; /**< LE Protocol/Service Multiplexer. */
  216. } ble_l2cap_evt_ch_setup_request_t;
  217. /**@brief L2CAP Channel Setup Refused event. */
  218. typedef struct
  219. {
  220. uint8_t source; /**< Source, see @ref BLE_L2CAP_CH_SETUP_REFUSED_SRCS */
  221. uint16_t status; /**< Status code, see @ref BLE_L2CAP_CH_STATUS_CODES */
  222. } ble_l2cap_evt_ch_setup_refused_t;
  223. /**@brief L2CAP Channel Setup Completed event. */
  224. typedef struct
  225. {
  226. ble_l2cap_ch_tx_params_t tx_params; /**< L2CAP channel TX parameters. */
  227. } ble_l2cap_evt_ch_setup_t;
  228. /**@brief L2CAP Channel SDU Data Buffer Released event. */
  229. typedef struct
  230. {
  231. ble_data_t sdu_buf; /**< Returned reception or transmission SDU data buffer. The SoftDevice
  232. returns SDU data buffers supplied by the application, which have
  233. not yet been returned previously via a @ref BLE_L2CAP_EVT_CH_RX or
  234. @ref BLE_L2CAP_EVT_CH_TX event. */
  235. } ble_l2cap_evt_ch_sdu_buf_released_t;
  236. /**@brief L2CAP Channel Credit received event. */
  237. typedef struct
  238. {
  239. uint16_t credits; /**< Additional credits given by the peer. */
  240. } ble_l2cap_evt_ch_credit_t;
  241. /**@brief L2CAP Channel received SDU event. */
  242. typedef struct
  243. {
  244. uint16_t sdu_len; /**< Total SDU length, in bytes. */
  245. ble_data_t sdu_buf; /**< SDU data buffer.
  246. @note If there is not enough space in the buffer
  247. (sdu_buf.len < sdu_len) then the rest of the SDU will be
  248. silently discarded by the SoftDevice. */
  249. } ble_l2cap_evt_ch_rx_t;
  250. /**@brief L2CAP Channel transmitted SDU event. */
  251. typedef struct
  252. {
  253. ble_data_t sdu_buf; /**< SDU data buffer. */
  254. } ble_l2cap_evt_ch_tx_t;
  255. /**@brief L2CAP event structure. */
  256. typedef struct
  257. {
  258. uint16_t conn_handle; /**< Connection Handle on which the event occured. */
  259. uint16_t local_cid; /**< Local Channel ID of the L2CAP channel, or
  260. @ref BLE_L2CAP_CID_INVALID if not present. */
  261. union
  262. {
  263. ble_l2cap_evt_ch_setup_request_t ch_setup_request; /**< L2CAP Channel Setup Request Event Parameters. */
  264. ble_l2cap_evt_ch_setup_refused_t ch_setup_refused; /**< L2CAP Channel Setup Refused Event Parameters. */
  265. ble_l2cap_evt_ch_setup_t ch_setup; /**< L2CAP Channel Setup Completed Event Parameters. */
  266. ble_l2cap_evt_ch_sdu_buf_released_t ch_sdu_buf_released;/**< L2CAP Channel SDU Data Buffer Released Event Parameters. */
  267. ble_l2cap_evt_ch_credit_t credit; /**< L2CAP Channel Credit Received Event Parameters. */
  268. ble_l2cap_evt_ch_rx_t rx; /**< L2CAP Channel SDU Received Event Parameters. */
  269. ble_l2cap_evt_ch_tx_t tx; /**< L2CAP Channel SDU Transmitted Event Parameters. */
  270. } params; /**< Event Parameters. */
  271. } ble_l2cap_evt_t;
  272. /** @} */
  273. /**@addtogroup BLE_L2CAP_FUNCTIONS Functions
  274. * @{ */
  275. /**@brief Set up an L2CAP channel.
  276. *
  277. * @details This function is used to:
  278. * - Request setup of an L2CAP channel: sends an LE Credit Based Connection Request packet to a peer.
  279. * - Reply to a setup request of an L2CAP channel (if called in response to a
  280. * @ref BLE_L2CAP_EVT_CH_SETUP_REQUEST event): sends an LE Credit Based Connection
  281. * Response packet to a peer.
  282. *
  283. * @note A call to this function will require the application to keep the SDU data buffer alive
  284. * until the SDU data buffer is returned in @ref BLE_L2CAP_EVT_CH_RX or
  285. * @ref BLE_L2CAP_EVT_CH_SDU_BUF_RELEASED event.
  286. *
  287. * @events
  288. * @event{@ref BLE_L2CAP_EVT_CH_SETUP, Setup successful.}
  289. * @event{@ref BLE_L2CAP_EVT_CH_SETUP_REFUSED, Setup failed.}
  290. * @endevents
  291. *
  292. * @mscs
  293. * @mmsc{@ref BLE_L2CAP_CH_SETUP_MSC}
  294. * @endmscs
  295. *
  296. * @param[in] conn_handle Connection Handle.
  297. * @param[in,out] p_local_cid Pointer to a uint16_t containing Local Channel ID of the L2CAP channel:
  298. * - As input: @ref BLE_L2CAP_CID_INVALID when requesting setup of an L2CAP
  299. * channel or local_cid provided in the @ref BLE_L2CAP_EVT_CH_SETUP_REQUEST
  300. * event when replying to a setup request of an L2CAP channel.
  301. * - As output: local_cid for this channel.
  302. * @param[in] p_params L2CAP channel parameters.
  303. *
  304. * @retval ::NRF_SUCCESS Successfully queued request or response for transmission.
  305. * @retval ::NRF_ERROR_BUSY The stack is busy, process pending events and retry.
  306. * @retval ::NRF_ERROR_INVALID_ADDR Invalid pointer supplied.
  307. * @retval ::BLE_ERROR_INVALID_CONN_HANDLE Invalid Connection Handle.
  308. * @retval ::NRF_ERROR_INVALID_PARAM Invalid parameter(s) supplied.
  309. * @retval ::NRF_ERROR_INVALID_LENGTH Supplied higher rx_mps than has been configured on this link.
  310. * @retval ::NRF_ERROR_INVALID_STATE Invalid State to perform operation (L2CAP channel already set up).
  311. * @retval ::NRF_ERROR_NOT_FOUND CID not found.
  312. * @retval ::NRF_ERROR_RESOURCES The limit has been reached for available L2CAP channels,
  313. * see @ref ble_l2cap_conn_cfg_t::ch_count.
  314. */
  315. SVCALL(SD_BLE_L2CAP_CH_SETUP, uint32_t, sd_ble_l2cap_ch_setup(uint16_t conn_handle, uint16_t *p_local_cid, ble_l2cap_ch_setup_params_t const *p_params));
  316. /**@brief Release an L2CAP channel.
  317. *
  318. * @details This sends a Disconnection Request packet to a peer.
  319. *
  320. * @events
  321. * @event{@ref BLE_L2CAP_EVT_CH_RELEASED, Release complete.}
  322. * @endevents
  323. *
  324. * @mscs
  325. * @mmsc{@ref BLE_L2CAP_CH_RELEASE_MSC}
  326. * @endmscs
  327. *
  328. * @param[in] conn_handle Connection Handle.
  329. * @param[in] local_cid Local Channel ID of the L2CAP channel.
  330. *
  331. * @retval ::NRF_SUCCESS Successfully queued request for transmission.
  332. * @retval ::BLE_ERROR_INVALID_CONN_HANDLE Invalid Connection Handle.
  333. * @retval ::NRF_ERROR_INVALID_STATE Invalid State to perform operation (Setup or release is
  334. * in progress for the L2CAP channel).
  335. * @retval ::NRF_ERROR_NOT_FOUND CID not found.
  336. */
  337. SVCALL(SD_BLE_L2CAP_CH_RELEASE, uint32_t, sd_ble_l2cap_ch_release(uint16_t conn_handle, uint16_t local_cid));
  338. /**@brief Receive an SDU on an L2CAP channel.
  339. *
  340. * @details This may issue additional credits to the peer using an LE Flow Control Credit packet.
  341. *
  342. * @note A call to this function will require the application to keep the memory pointed by
  343. * @ref ble_data_t::p_data alive until the SDU data buffer is returned in @ref BLE_L2CAP_EVT_CH_RX
  344. * or @ref BLE_L2CAP_EVT_CH_SDU_BUF_RELEASED event.
  345. *
  346. * @note The SoftDevice can queue up to @ref ble_l2cap_conn_cfg_t::rx_queue_size SDU data buffers
  347. * for reception per L2CAP channel.
  348. *
  349. * @events
  350. * @event{@ref BLE_L2CAP_EVT_CH_RX, The SDU is received.}
  351. * @endevents
  352. *
  353. * @mscs
  354. * @mmsc{@ref BLE_L2CAP_CH_RX_MSC}
  355. * @endmscs
  356. *
  357. * @param[in] conn_handle Connection Handle.
  358. * @param[in] local_cid Local Channel ID of the L2CAP channel.
  359. * @param[in] p_sdu_buf Pointer to the SDU data buffer.
  360. *
  361. * @retval ::NRF_SUCCESS Buffer accepted.
  362. * @retval ::NRF_ERROR_INVALID_ADDR Invalid pointer supplied.
  363. * @retval ::BLE_ERROR_INVALID_CONN_HANDLE Invalid Connection Handle.
  364. * @retval ::NRF_ERROR_INVALID_STATE Invalid State to perform operation (Setup or release is
  365. * in progress for an L2CAP channel).
  366. * @retval ::NRF_ERROR_NOT_FOUND CID not found.
  367. * @retval ::NRF_ERROR_RESOURCES Too many SDU data buffers supplied. Wait for a
  368. * @ref BLE_L2CAP_EVT_CH_RX event and retry.
  369. */
  370. SVCALL(SD_BLE_L2CAP_CH_RX, uint32_t, sd_ble_l2cap_ch_rx(uint16_t conn_handle, uint16_t local_cid, ble_data_t const *p_sdu_buf));
  371. /**@brief Transmit an SDU on an L2CAP channel.
  372. *
  373. * @note A call to this function will require the application to keep the memory pointed by
  374. * @ref ble_data_t::p_data alive until the SDU data buffer is returned in @ref BLE_L2CAP_EVT_CH_TX
  375. * or @ref BLE_L2CAP_EVT_CH_SDU_BUF_RELEASED event.
  376. *
  377. * @note The SoftDevice can queue up to @ref ble_l2cap_conn_cfg_t::tx_queue_size SDUs for
  378. * transmission per L2CAP channel.
  379. *
  380. * @note The application can keep track of the available credits for transmission by following
  381. * the procedure below:
  382. * - Store initial credits given by the peer in a variable.
  383. * (Initial credits are provided in a @ref BLE_L2CAP_EVT_CH_SETUP event.)
  384. * - Decrement the variable, which stores the currently available credits, by
  385. * ceiling((@ref ble_data_t::len + 2) / tx_mps) when a call to this function returns
  386. * @ref NRF_SUCCESS. (tx_mps is provided in a @ref BLE_L2CAP_EVT_CH_SETUP event.)
  387. * - Increment the variable, which stores the currently available credits, by additional
  388. * credits given by the peer in a @ref BLE_L2CAP_EVT_CH_CREDIT event.
  389. *
  390. * @events
  391. * @event{@ref BLE_L2CAP_EVT_CH_TX, The SDU is transmitted.}
  392. * @endevents
  393. *
  394. * @mscs
  395. * @mmsc{@ref BLE_L2CAP_CH_TX_MSC}
  396. * @endmscs
  397. *
  398. * @param[in] conn_handle Connection Handle.
  399. * @param[in] local_cid Local Channel ID of the L2CAP channel.
  400. * @param[in] p_sdu_buf Pointer to the SDU data buffer.
  401. *
  402. * @retval ::NRF_SUCCESS Successfully queued L2CAP SDU for transmission.
  403. * @retval ::NRF_ERROR_INVALID_ADDR Invalid pointer supplied.
  404. * @retval ::BLE_ERROR_INVALID_CONN_HANDLE Invalid Connection Handle.
  405. * @retval ::NRF_ERROR_INVALID_STATE Invalid State to perform operation (Setup or release is
  406. * in progress for the L2CAP channel).
  407. * @retval ::NRF_ERROR_NOT_FOUND CID not found.
  408. * @retval ::NRF_ERROR_DATA_SIZE Invalid SDU length supplied, must not be more than
  409. * @ref ble_l2cap_ch_tx_params_t::tx_mtu provided in
  410. * @ref BLE_L2CAP_EVT_CH_SETUP event.
  411. * @retval ::NRF_ERROR_RESOURCES Too many SDUs queued for transmission. Wait for a
  412. * @ref BLE_L2CAP_EVT_CH_TX event and retry.
  413. */
  414. SVCALL(SD_BLE_L2CAP_CH_TX, uint32_t, sd_ble_l2cap_ch_tx(uint16_t conn_handle, uint16_t local_cid, ble_data_t const *p_sdu_buf));
  415. /**@brief Advanced SDU reception flow control.
  416. *
  417. * @details Adjust the way the SoftDevice issues credits to the peer.
  418. * This may issue additional credits to the peer using an LE Flow Control Credit packet.
  419. *
  420. * @mscs
  421. * @mmsc{@ref BLE_L2CAP_CH_FLOW_CONTROL_MSC}
  422. * @endmscs
  423. *
  424. * @param[in] conn_handle Connection Handle.
  425. * @param[in] local_cid Local Channel ID of the L2CAP channel or @ref BLE_L2CAP_CID_INVALID to set
  426. * the value that will be used for newly created channels.
  427. * @param[in] credits Number of credits that the SoftDevice will make sure the peer has every
  428. * time it starts using a new reception buffer.
  429. * - @ref BLE_L2CAP_CREDITS_DEFAULT is the default value the SoftDevice will
  430. * use if this function is not called.
  431. * - If set to zero, the SoftDevice will stop issuing credits for new reception
  432. * buffers the application provides or has provided. SDU reception that is
  433. * currently ongoing will be allowed to complete.
  434. * @param[out] p_credits NULL or pointer to a uint16_t. If a valid pointer is provided, it will be
  435. * written by the SoftDevice with the number of credits that is or will be
  436. * available to the peer. If the value written by the SoftDevice is 0 when
  437. * credits parameter was set to 0, the peer will not be able to send more
  438. * data until more credits are provided by calling this function again with
  439. * credits > 0. This parameter is ignored when local_cid is set to
  440. * @ref BLE_L2CAP_CID_INVALID.
  441. *
  442. * @note Application should take care when setting number of credits higher than default value. In
  443. * this case the application must make sure that the SoftDevice always has reception buffers
  444. * available (see @ref sd_ble_l2cap_ch_rx) for that channel. If the SoftDevice does not have
  445. * such buffers available, packets may be NACKed on the Link Layer and all Bluetooth traffic
  446. * on the connection handle may be stalled until the SoftDevice again has an available
  447. * reception buffer. This applies even if the application has used this call to set the
  448. * credits back to default, or zero.
  449. *
  450. * @retval ::NRF_SUCCESS Flow control parameters accepted.
  451. * @retval ::NRF_ERROR_INVALID_ADDR Invalid pointer supplied.
  452. * @retval ::BLE_ERROR_INVALID_CONN_HANDLE Invalid Connection Handle.
  453. * @retval ::NRF_ERROR_INVALID_STATE Invalid State to perform operation (Setup or release is
  454. * in progress for an L2CAP channel).
  455. * @retval ::NRF_ERROR_NOT_FOUND CID not found.
  456. */
  457. SVCALL(SD_BLE_L2CAP_CH_FLOW_CONTROL, uint32_t, sd_ble_l2cap_ch_flow_control(uint16_t conn_handle, uint16_t local_cid, uint16_t credits, uint16_t *p_credits));
  458. /** @} */
  459. #ifdef __cplusplus
  460. }
  461. #endif
  462. #endif // BLE_L2CAP_H__
  463. /**
  464. @}
  465. */