app_mw_ble_l2cap.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /**
  2. * Copyright (c) 2014 - 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. #include "ble_gap.h"
  41. #include <stdint.h>
  42. #include "ble_serialization.h"
  43. #include "ser_sd_transport.h"
  44. #include "ble_l2cap_app.h"
  45. #include "app_error.h"
  46. #if defined(NRF_SD_BLE_API_VERSION) && ((NRF_SD_BLE_API_VERSION < 4) || (NRF_SD_BLE_API_VERSION >=5))
  47. static void tx_buf_alloc(uint8_t * * p_data, uint16_t * p_len)
  48. {
  49. uint32_t err_code;
  50. do
  51. {
  52. err_code = ser_sd_transport_tx_alloc(p_data, p_len);
  53. }
  54. while (err_code != NRF_SUCCESS);
  55. *p_data[0] = SER_PKT_TYPE_CMD;
  56. *p_len -= 1;
  57. }
  58. #endif
  59. #if defined(NRF_SD_BLE_API_VERSION) && NRF_SD_BLE_API_VERSION < 4
  60. /**@brief Command response callback function for @ref ble_l2cap_cid_register_req_enc BLE command.
  61. *
  62. * Callback for decoding the output parameters and the command response return code.
  63. *
  64. * @param[in] p_buffer Pointer to begin of command response buffer.
  65. * @param[in] length Length of data in bytes.
  66. *
  67. * @return Decoded command response return code.
  68. */
  69. static uint32_t l2cap_cid_register_rsp_dec(const uint8_t * p_buffer, uint16_t length)
  70. {
  71. uint32_t result_code;
  72. const uint32_t err_code =
  73. ble_l2cap_cid_register_rsp_dec(p_buffer,
  74. length,
  75. &result_code);
  76. APP_ERROR_CHECK(err_code);
  77. return result_code;
  78. }
  79. #ifndef _sd_ble_l2cap_cid_register
  80. #define _sd_ble_l2cap_cid_register sd_ble_l2cap_cid_register
  81. #endif
  82. uint32_t _sd_ble_l2cap_cid_register(uint16_t cid)
  83. {
  84. uint8_t * p_buffer;
  85. uint32_t buffer_length = 0;
  86. tx_buf_alloc(&p_buffer, (uint16_t *)&buffer_length);
  87. const uint32_t err_code = ble_l2cap_cid_register_req_enc(cid,
  88. &(p_buffer[1]),
  89. &buffer_length);
  90. //@note: Should never fail.
  91. APP_ERROR_CHECK(err_code);
  92. //@note: Increment buffer length as internally managed packet type field must be included.
  93. return ser_sd_transport_cmd_write(p_buffer,
  94. (++buffer_length),
  95. l2cap_cid_register_rsp_dec);
  96. }
  97. /**@brief Command response callback function for @ref ble_l2cap_cid_unregister_req_enc BLE command.
  98. *
  99. * Callback for decoding the output parameters and the command response return code.
  100. *
  101. * @param[in] p_buffer Pointer to begin of command response buffer.
  102. * @param[in] length Length of data in bytes.
  103. *
  104. * @return Decoded command response return code.
  105. */
  106. static uint32_t l2cap_cid_unregister_rsp_dec(const uint8_t * p_buffer, uint16_t length)
  107. {
  108. uint32_t result_code;
  109. const uint32_t err_code =
  110. ble_l2cap_cid_unregister_rsp_dec(p_buffer,
  111. length,
  112. &result_code);
  113. APP_ERROR_CHECK(err_code);
  114. return result_code;
  115. }
  116. #ifndef _sd_ble_l2cap_cid_unregister
  117. #define _sd_ble_l2cap_cid_unregister sd_ble_l2cap_cid_unregister
  118. #endif
  119. uint32_t _sd_ble_l2cap_cid_unregister(uint16_t cid)
  120. {
  121. uint8_t * p_buffer;
  122. uint32_t buffer_length = 0;
  123. tx_buf_alloc(&p_buffer, (uint16_t *)&buffer_length);
  124. const uint32_t err_code = ble_l2cap_cid_unregister_req_enc(cid,
  125. &(p_buffer[1]),
  126. &buffer_length);
  127. //@note: Should never fail.
  128. APP_ERROR_CHECK(err_code);
  129. //@note: Increment buffer length as internally managed packet type field must be included.
  130. return ser_sd_transport_cmd_write(p_buffer,
  131. (++buffer_length),
  132. l2cap_cid_unregister_rsp_dec);
  133. }
  134. /**@brief Command response callback function for @ref ble_l2cap_tx_req_enc BLE command.
  135. *
  136. * Callback for decoding the output parameters and the command response return code.
  137. *
  138. * @param[in] p_buffer Pointer to begin of command response buffer.
  139. * @param[in] length Length of data in bytes.
  140. *
  141. * @return Decoded command response return code.
  142. */
  143. static uint32_t l2cap_tx_rsp_dec(const uint8_t * p_buffer, uint16_t length)
  144. {
  145. uint32_t result_code;
  146. const uint32_t err_code =
  147. ble_l2cap_tx_rsp_dec(p_buffer,
  148. length,
  149. &result_code);
  150. APP_ERROR_CHECK(err_code);
  151. return result_code;
  152. }
  153. #ifndef _sd_ble_l2cap_tx
  154. #define _sd_ble_l2cap_tx sd_ble_l2cap_tx
  155. #endif
  156. uint32_t _sd_ble_l2cap_tx(uint16_t conn_handle,
  157. ble_l2cap_header_t const * const p_header,
  158. uint8_t const * const p_data)
  159. {
  160. uint8_t * p_buffer;
  161. uint32_t buffer_length = 0;
  162. tx_buf_alloc(&p_buffer, (uint16_t *)&buffer_length);
  163. const uint32_t err_code = ble_l2cap_tx_req_enc(conn_handle, p_header, p_data,
  164. &(p_buffer[1]),
  165. &buffer_length);
  166. //@note: Should never fail.
  167. APP_ERROR_CHECK(err_code);
  168. //@note: Increment buffer length as internally managed packet type field must be included.
  169. return ser_sd_transport_cmd_write(p_buffer,
  170. (++buffer_length),
  171. l2cap_tx_rsp_dec);
  172. }
  173. #endif
  174. #if NRF_SD_BLE_API_VERSION >= 5
  175. static void * mp_out_params[1];
  176. /**@brief Command response callback function for @ref ble_l2cap_ch_setup_req_enc BLE command.
  177. *
  178. * Callback for decoding the output parameters and the command response return code.
  179. *
  180. * @param[in] p_buffer Pointer to begin of command response buffer.
  181. * @param[in] length Length of data in bytes.
  182. *
  183. * @return Decoded command response return code.
  184. */
  185. static uint32_t l2cap_ch_setup_rsp_dec(const uint8_t * p_buffer, uint16_t length)
  186. {
  187. uint32_t result_code;
  188. const uint32_t err_code =
  189. ble_l2cap_ch_setup_rsp_dec(p_buffer,
  190. length,
  191. (uint16_t *)mp_out_params[0],
  192. &result_code);
  193. APP_ERROR_CHECK(err_code);
  194. return result_code;
  195. }
  196. #ifndef _sd_ble_l2cap_ch_setup
  197. #define _sd_ble_l2cap_ch_setup sd_ble_l2cap_ch_setup
  198. #endif
  199. uint32_t _sd_ble_l2cap_ch_setup(uint16_t conn_handle,
  200. uint16_t * p_local_cid,
  201. ble_l2cap_ch_setup_params_t const *p_params)
  202. {
  203. uint8_t * p_buffer;
  204. uint32_t buffer_length = 0;
  205. tx_buf_alloc(&p_buffer, (uint16_t *)&buffer_length);
  206. mp_out_params[0] = p_local_cid;
  207. const uint32_t err_code = ble_l2cap_ch_setup_req_enc(conn_handle, p_local_cid, p_params,
  208. &(p_buffer[1]),
  209. &buffer_length);
  210. //@note: Should never fail.
  211. APP_ERROR_CHECK(err_code);
  212. //@note: Increment buffer length as internally managed packet type field must be included.
  213. return ser_sd_transport_cmd_write(p_buffer,
  214. (++buffer_length),
  215. l2cap_ch_setup_rsp_dec);
  216. }
  217. /**@brief Command response callback function for @ref ble_l2cap_ch_release_req_enc BLE command.
  218. *
  219. * Callback for decoding the output parameters and the command response return code.
  220. *
  221. * @param[in] p_buffer Pointer to begin of command response buffer.
  222. * @param[in] length Length of data in bytes.
  223. *
  224. * @return Decoded command response return code.
  225. */
  226. static uint32_t l2cap_ch_release_rsp_dec(const uint8_t * p_buffer, uint16_t length)
  227. {
  228. uint32_t result_code;
  229. const uint32_t err_code =
  230. ble_l2cap_ch_release_rsp_dec(p_buffer,
  231. length,
  232. &result_code);
  233. APP_ERROR_CHECK(err_code);
  234. return result_code;
  235. }
  236. #ifndef _sd_ble_l2cap_ch_release
  237. #define _sd_ble_l2cap_ch_release sd_ble_l2cap_ch_release
  238. #endif
  239. uint32_t _sd_ble_l2cap_ch_release(uint16_t conn_handle,
  240. uint16_t local_cid)
  241. {
  242. uint8_t * p_buffer;
  243. uint32_t buffer_length = 0;
  244. tx_buf_alloc(&p_buffer, (uint16_t *)&buffer_length);
  245. const uint32_t err_code = ble_l2cap_ch_release_req_enc(conn_handle, local_cid,
  246. &(p_buffer[1]),
  247. &buffer_length);
  248. //@note: Should never fail.
  249. APP_ERROR_CHECK(err_code);
  250. //@note: Increment buffer length as internally managed packet type field must be included.
  251. return ser_sd_transport_cmd_write(p_buffer,
  252. (++buffer_length),
  253. l2cap_ch_release_rsp_dec);
  254. }
  255. /**@brief Command response callback function for @ref ble_l2cap_ch_rx_req_enc BLE command.
  256. *
  257. * Callback for decoding the output parameters and the command response return code.
  258. *
  259. * @param[in] p_buffer Pointer to begin of command response buffer.
  260. * @param[in] length Length of data in bytes.
  261. *
  262. * @return Decoded command response return code.
  263. */
  264. static uint32_t l2cap_ch_rx_rsp_dec(const uint8_t * p_buffer, uint16_t length)
  265. {
  266. uint32_t result_code;
  267. const uint32_t err_code =
  268. ble_l2cap_ch_rx_rsp_dec(p_buffer,
  269. length,
  270. &result_code);
  271. APP_ERROR_CHECK(err_code);
  272. return result_code;
  273. }
  274. #ifndef _sd_ble_l2cap_ch_rx
  275. #define _sd_ble_l2cap_ch_rx sd_ble_l2cap_ch_rx
  276. #endif
  277. uint32_t _sd_ble_l2cap_ch_rx(uint16_t conn_handle, uint16_t local_cid, ble_data_t const *p_sdu_buf)
  278. {
  279. uint8_t * p_buffer;
  280. uint32_t buffer_length = 0;
  281. tx_buf_alloc(&p_buffer, (uint16_t *)&buffer_length);
  282. const uint32_t err_code = ble_l2cap_ch_rx_req_enc(conn_handle, local_cid, p_sdu_buf,
  283. &(p_buffer[1]),
  284. &buffer_length);
  285. //@note: Should never fail.
  286. APP_ERROR_CHECK(err_code);
  287. //@note: Increment buffer length as internally managed packet type field must be included.
  288. return ser_sd_transport_cmd_write(p_buffer,
  289. (++buffer_length),
  290. l2cap_ch_rx_rsp_dec);
  291. }
  292. /**@brief Command response callback function for @ref ble_l2cap_ch_tx_req_enc BLE command.
  293. *
  294. * Callback for decoding the output parameters and the command response return code.
  295. *
  296. * @param[in] p_buffer Pointer to begin of command response buffer.
  297. * @param[in] length Length of data in bytes.
  298. *
  299. * @return Decoded command response return code.
  300. */
  301. static uint32_t l2cap_ch_tx_rsp_dec(const uint8_t * p_buffer, uint16_t length)
  302. {
  303. uint32_t result_code;
  304. const uint32_t err_code =
  305. ble_l2cap_ch_tx_rsp_dec(p_buffer,
  306. length,
  307. &result_code);
  308. APP_ERROR_CHECK(err_code);
  309. return result_code;
  310. }
  311. #ifndef _sd_ble_l2cap_ch_tx
  312. #define _sd_ble_l2cap_ch_tx sd_ble_l2cap_ch_tx
  313. #endif
  314. uint32_t _sd_ble_l2cap_ch_tx(uint16_t conn_handle, uint16_t local_cid, ble_data_t const *p_sdu_buf)
  315. {
  316. uint8_t * p_buffer;
  317. uint32_t buffer_length = 0;
  318. tx_buf_alloc(&p_buffer, (uint16_t *)&buffer_length);
  319. const uint32_t err_code = ble_l2cap_ch_tx_req_enc(conn_handle, local_cid, p_sdu_buf,
  320. &(p_buffer[1]),
  321. &buffer_length);
  322. //@note: Should never fail.
  323. APP_ERROR_CHECK(err_code);
  324. //@note: Increment buffer length as internally managed packet type field must be included.
  325. return ser_sd_transport_cmd_write(p_buffer,
  326. (++buffer_length),
  327. l2cap_ch_tx_rsp_dec);
  328. }
  329. /**@brief Command response callback function for @ref ble_l2cap_ch_flow_control_req_enc BLE command.
  330. *
  331. * Callback for decoding the output parameters and the command response return code.
  332. *
  333. * @param[in] p_buffer Pointer to begin of command response buffer.
  334. * @param[in] length Length of data in bytes.
  335. *
  336. * @return Decoded command response return code.
  337. */
  338. static uint32_t l2cap_ch_flow_control_rsp_dec(const uint8_t * p_buffer, uint16_t length)
  339. {
  340. uint32_t result_code;
  341. const uint32_t err_code =
  342. ble_l2cap_ch_flow_control_rsp_dec(p_buffer,
  343. length,
  344. (uint16_t *)mp_out_params[0],
  345. &result_code);
  346. APP_ERROR_CHECK(err_code);
  347. return result_code;
  348. }
  349. #ifndef _sd_ble_l2cap_ch_flow_control
  350. #define _sd_ble_l2cap_ch_flow_control sd_ble_l2cap_ch_flow_control
  351. #endif
  352. uint32_t _sd_ble_l2cap_ch_flow_control(uint16_t conn_handle,
  353. uint16_t local_cid,
  354. uint16_t credits,
  355. uint16_t *p_credits)
  356. {
  357. uint8_t * p_buffer;
  358. uint32_t buffer_length = 0;
  359. tx_buf_alloc(&p_buffer, (uint16_t *)&buffer_length);
  360. mp_out_params[0] = p_credits;
  361. const uint32_t err_code = ble_l2cap_ch_flow_control_req_enc(conn_handle, local_cid, credits, p_credits,
  362. &(p_buffer[1]),
  363. &buffer_length);
  364. //@note: Should never fail.
  365. APP_ERROR_CHECK(err_code);
  366. //@note: Increment buffer length as internally managed packet type field must be included.
  367. return ser_sd_transport_cmd_write(p_buffer,
  368. (++buffer_length),
  369. l2cap_ch_flow_control_rsp_dec);
  370. }
  371. #endif