ble_l2cap_app.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 <string.h>
  41. #include "ble_l2cap_app.h"
  42. #include "ble_serialization.h"
  43. #include "ble_struct_serialization.h"
  44. #include "ble_l2cap_struct_serialization.h"
  45. #include "ble_gap.h"
  46. #include "app_util.h"
  47. #include "cond_field_serialization.h"
  48. #if defined(NRF_SD_BLE_API_VERSION) && NRF_SD_BLE_API_VERSION < 4
  49. uint32_t ble_l2cap_cid_register_req_enc(uint16_t cid,
  50. uint8_t * const p_buf,
  51. uint32_t * const p_buf_len)
  52. {
  53. uint32_t index = 0;
  54. uint32_t err_code = NRF_SUCCESS;
  55. SER_ASSERT_NOT_NULL(p_buf);
  56. SER_ASSERT_NOT_NULL(p_buf_len);
  57. SER_ASSERT_LENGTH_LEQ(index + 3, *p_buf_len);
  58. p_buf[index++] = SD_BLE_L2CAP_CID_REGISTER;
  59. err_code = uint16_t_enc(&cid, p_buf, *p_buf_len, &index);
  60. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  61. *p_buf_len = index;
  62. return err_code;
  63. }
  64. uint32_t ble_l2cap_cid_register_rsp_dec(uint8_t const * const p_buf,
  65. uint32_t packet_len,
  66. uint32_t * const p_result_code)
  67. {
  68. return ser_ble_cmd_rsp_dec(p_buf, packet_len, SD_BLE_L2CAP_CID_REGISTER, p_result_code);
  69. }
  70. uint32_t ble_l2cap_cid_unregister_req_enc(uint16_t cid,
  71. uint8_t * const p_buf,
  72. uint32_t * const p_buf_len)
  73. {
  74. uint32_t index = 0;
  75. uint32_t err_code = NRF_SUCCESS;
  76. SER_ASSERT_NOT_NULL(p_buf);
  77. SER_ASSERT_NOT_NULL(p_buf_len);
  78. SER_ASSERT_LENGTH_LEQ(index + 3, *p_buf_len);
  79. p_buf[index++] = SD_BLE_L2CAP_CID_UNREGISTER;
  80. err_code = uint16_t_enc(&cid, p_buf, *p_buf_len, &index);
  81. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  82. *p_buf_len = index;
  83. return err_code;
  84. }
  85. uint32_t ble_l2cap_cid_unregister_rsp_dec(uint8_t const * const p_buf,
  86. uint32_t packet_len,
  87. uint32_t * const p_result_code)
  88. {
  89. return ser_ble_cmd_rsp_dec(p_buf, packet_len, SD_BLE_L2CAP_CID_UNREGISTER, p_result_code);
  90. }
  91. uint32_t ble_l2cap_tx_req_enc(uint16_t conn_handle,
  92. ble_l2cap_header_t const * const p_l2cap_header,
  93. uint8_t const * const p_data,
  94. uint8_t * const p_buf,
  95. uint32_t * const p_buf_len)
  96. {
  97. uint32_t index = 0;
  98. uint32_t err_code = NRF_SUCCESS;
  99. SER_ASSERT_NOT_NULL(p_buf);
  100. SER_ASSERT_NOT_NULL(p_buf_len);
  101. SER_ASSERT_LENGTH_LEQ(1, *p_buf_len);
  102. p_buf[index++] = SD_BLE_L2CAP_TX;
  103. err_code = uint16_t_enc(&conn_handle, p_buf, *p_buf_len, &index);
  104. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  105. err_code = cond_field_enc(p_l2cap_header, p_buf, *p_buf_len, &index, ble_l2cap_header_t_enc);
  106. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  107. if (p_l2cap_header != NULL)
  108. {
  109. err_code = buf_enc(p_data, p_l2cap_header->len, p_buf, *p_buf_len, &index);
  110. }
  111. else
  112. {
  113. err_code = buf_enc(NULL, 0, p_buf, *p_buf_len, &index);
  114. }
  115. *p_buf_len = index;
  116. return err_code;
  117. }
  118. uint32_t ble_l2cap_tx_rsp_dec(uint8_t const * const p_buf,
  119. uint32_t packet_len,
  120. uint32_t * const p_result_code)
  121. {
  122. return ser_ble_cmd_rsp_dec(p_buf, packet_len, SD_BLE_L2CAP_TX, p_result_code);
  123. }
  124. #endif
  125. #if NRF_SD_BLE_API_VERSION >= 5
  126. uint32_t ble_l2cap_ch_setup_req_enc(uint16_t conn_handle,
  127. uint16_t * p_local_cid,
  128. ble_l2cap_ch_setup_params_t const *p_params,
  129. uint8_t * const p_buf,
  130. uint32_t * const p_buf_len)
  131. {
  132. SER_REQ_ENC_BEGIN(SD_BLE_L2CAP_CH_SETUP);
  133. SER_PUSH_uint16(&conn_handle);
  134. SER_PUSH_COND(p_local_cid, uint16_t_enc);
  135. SER_PUSH_COND(p_params, ble_l2cap_ch_setup_params_t_enc);
  136. SER_REQ_ENC_END;
  137. }
  138. uint32_t ble_l2cap_ch_setup_rsp_dec(uint8_t const * const p_buf,
  139. uint32_t packet_len,
  140. uint16_t * p_local_cid,
  141. uint32_t * const p_result_code)
  142. {
  143. SER_RSP_DEC_BEGIN(SD_BLE_L2CAP_CH_SETUP);
  144. SER_PULL_COND((void **)&p_local_cid, uint16_t_dec);
  145. SER_RSP_DEC_END;
  146. }
  147. uint32_t ble_l2cap_ch_release_req_enc(uint16_t conn_handle,
  148. uint16_t local_cid,
  149. uint8_t * const p_buf,
  150. uint32_t * const p_buf_len)
  151. {
  152. SER_REQ_ENC_BEGIN(SD_BLE_L2CAP_CH_RELEASE);
  153. SER_PUSH_uint16(&conn_handle);
  154. SER_PUSH_uint16(&local_cid);
  155. SER_REQ_ENC_END;
  156. }
  157. uint32_t ble_l2cap_ch_release_rsp_dec(uint8_t const * const p_buf,
  158. uint32_t packet_len,
  159. uint32_t * const p_result_code)
  160. {
  161. SER_RSP_DEC_RESULT_ONLY(SD_BLE_L2CAP_CH_RELEASE);
  162. }
  163. uint32_t ble_l2cap_ch_rx_req_enc(uint16_t conn_handle,
  164. uint16_t local_cid,
  165. ble_data_t const *p_sdu_buf,
  166. uint8_t * const p_buf,
  167. uint32_t * const p_buf_len)
  168. {
  169. SER_REQ_ENC_BEGIN(SD_BLE_L2CAP_CH_RX);
  170. SER_PUSH_uint16(&conn_handle);
  171. SER_PUSH_uint16(&local_cid);
  172. SER_PUSH_COND(p_sdu_buf, NULL);
  173. if (p_sdu_buf)
  174. {
  175. SER_PUSH_uint16(&p_sdu_buf->len);
  176. SER_PUSH_uint32(&p_sdu_buf->p_data);
  177. }
  178. SER_REQ_ENC_END;
  179. }
  180. uint32_t ble_l2cap_ch_rx_rsp_dec(uint8_t const * const p_buf,
  181. uint32_t packet_len,
  182. uint32_t * const p_result_code)
  183. {
  184. SER_RSP_DEC_RESULT_ONLY(SD_BLE_L2CAP_CH_RX);
  185. }
  186. uint32_t ble_l2cap_ch_tx_req_enc(uint16_t conn_handle,
  187. uint16_t local_cid,
  188. ble_data_t const *p_sdu_buf,
  189. uint8_t * const p_buf,
  190. uint32_t * const p_buf_len)
  191. {
  192. SER_REQ_ENC_BEGIN(SD_BLE_L2CAP_CH_TX);
  193. SER_PUSH_uint16(&conn_handle);
  194. SER_PUSH_uint16(&local_cid);
  195. SER_PUSH_COND(p_sdu_buf, NULL);
  196. if (p_sdu_buf)
  197. {
  198. SER_PUSH_uint32(&p_sdu_buf->p_data);
  199. SER_PUSH_uint16(&p_sdu_buf->len);
  200. SER_PUSH_buf(p_sdu_buf->p_data, p_sdu_buf->len);
  201. }
  202. SER_REQ_ENC_END;
  203. }
  204. uint32_t ble_l2cap_ch_tx_rsp_dec(uint8_t const * const p_buf,
  205. uint32_t packet_len,
  206. uint32_t * const p_result_code)
  207. {
  208. SER_RSP_DEC_RESULT_ONLY(SD_BLE_L2CAP_CH_TX);
  209. }
  210. uint32_t ble_l2cap_ch_flow_control_req_enc(uint16_t conn_handle,
  211. uint16_t local_cid,
  212. uint16_t credits,
  213. uint16_t *p_credits,
  214. uint8_t * const p_buf,
  215. uint32_t * const p_buf_len)
  216. {
  217. SER_REQ_ENC_BEGIN(SD_BLE_L2CAP_CH_FLOW_CONTROL);
  218. SER_PUSH_uint16(&conn_handle);
  219. SER_PUSH_uint16(&local_cid);
  220. SER_PUSH_uint16(&credits);
  221. SER_PUSH_COND(p_credits, NULL);
  222. SER_REQ_ENC_END;
  223. }
  224. uint32_t ble_l2cap_ch_flow_control_rsp_dec(uint8_t const * const p_buf,
  225. uint32_t packet_len,
  226. uint16_t * p_credits,
  227. uint32_t * const p_result_code)
  228. {
  229. SER_RSP_DEC_BEGIN(SD_BLE_L2CAP_CH_FLOW_CONTROL);
  230. SER_PULL_COND((void **)&p_credits, uint16_t_dec);
  231. SER_RSP_DEC_END;
  232. }
  233. #endif //NRF_SD_BLE_API_VERSION >= 5