ble_l2cap_conn.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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 <stdlib.h>
  41. #include <string.h>
  42. #include "ble_l2cap_conn.h"
  43. #include "ble_serialization.h"
  44. #include "ble_struct_serialization.h"
  45. #include "ble_l2cap_struct_serialization.h"
  46. #include "cond_field_serialization.h"
  47. #include "conn_ble_l2cap_sdu_pool.h"
  48. #include "app_util.h"
  49. #if defined(NRF_SD_BLE_API_VERSION) && NRF_SD_BLE_API_VERSION < 4
  50. uint32_t ble_l2cap_cid_register_req_dec(uint8_t const * const p_buf,
  51. uint32_t buf_len,
  52. uint16_t * p_cid)
  53. {
  54. uint32_t index = 0;
  55. uint32_t err_code = NRF_SUCCESS;
  56. SER_ASSERT_NOT_NULL(p_cid);
  57. SER_ASSERT_NOT_NULL(p_buf);
  58. SER_ASSERT_LENGTH_EQ(3, buf_len);
  59. SER_ASSERT(p_buf[index] == SD_BLE_L2CAP_CID_REGISTER, NRF_ERROR_INVALID_PARAM);
  60. index++;
  61. err_code = uint16_t_dec(p_buf, buf_len, &index, p_cid);
  62. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  63. SER_ASSERT_LENGTH_EQ(index, buf_len);
  64. return err_code;
  65. }
  66. uint32_t ble_l2cap_cid_register_rsp_enc(uint32_t return_code,
  67. uint8_t * const p_buf,
  68. uint32_t * const p_buf_len)
  69. {
  70. return ser_ble_cmd_rsp_status_code_enc(SD_BLE_L2CAP_CID_REGISTER, return_code,
  71. p_buf, p_buf_len);
  72. }
  73. uint32_t ble_l2cap_cid_unregister_req_dec(uint8_t const * const p_buf,
  74. uint32_t buf_len,
  75. uint16_t * p_cid)
  76. {
  77. uint32_t index = 0;
  78. uint32_t err_code = NRF_SUCCESS;
  79. SER_ASSERT_NOT_NULL(p_cid);
  80. SER_ASSERT_NOT_NULL(p_buf);
  81. SER_ASSERT_LENGTH_EQ(3, buf_len);
  82. SER_ASSERT(p_buf[index] == SD_BLE_L2CAP_CID_UNREGISTER, NRF_ERROR_INVALID_PARAM);
  83. index++;
  84. err_code = uint16_t_dec(p_buf, buf_len, &index, p_cid);
  85. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  86. SER_ASSERT_LENGTH_EQ(index, buf_len);
  87. return err_code;
  88. }
  89. uint32_t ble_l2cap_cid_unregister_rsp_enc(uint32_t return_code,
  90. uint8_t * const p_buf,
  91. uint32_t * const p_buf_len)
  92. {
  93. return ser_ble_cmd_rsp_status_code_enc(SD_BLE_L2CAP_CID_UNREGISTER, return_code,
  94. p_buf, p_buf_len);
  95. }
  96. uint32_t ble_l2cap_tx_req_dec(uint8_t const * const p_buf,
  97. uint32_t const buf_len,
  98. uint16_t * p_conn_handle,
  99. ble_l2cap_header_t * * const pp_l2cap_header,
  100. uint8_t const * * pp_data)
  101. {
  102. SER_ASSERT_NOT_NULL(p_buf);
  103. SER_ASSERT_NOT_NULL(p_conn_handle);
  104. SER_ASSERT_NOT_NULL(pp_l2cap_header);
  105. SER_ASSERT_NOT_NULL(*pp_l2cap_header);
  106. SER_ASSERT_NOT_NULL(pp_data);
  107. //SER_ASSERT_NOT_NULL(*pp_data);
  108. uint32_t err_code = NRF_SUCCESS;
  109. uint32_t index = SER_CMD_DATA_POS;
  110. err_code = uint16_t_dec(p_buf, buf_len, &index, p_conn_handle);
  111. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  112. err_code = cond_field_dec(p_buf, buf_len, &index, (void * *)pp_l2cap_header,
  113. ble_l2cap_header_t_dec);
  114. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  115. if (*pp_l2cap_header != NULL)
  116. {
  117. *pp_data = p_buf + index + 1;
  118. index += 1 + (*pp_l2cap_header)->len;
  119. }
  120. else
  121. {
  122. *pp_data = NULL;
  123. index++;
  124. }
  125. SER_ASSERT_LENGTH_EQ(index, buf_len);
  126. return err_code;
  127. }
  128. uint32_t ble_l2cap_tx_rsp_enc(uint32_t return_code,
  129. uint8_t * const p_buf,
  130. uint32_t * const p_buf_len)
  131. {
  132. return ser_ble_cmd_rsp_status_code_enc(SD_BLE_L2CAP_TX, return_code,
  133. p_buf, p_buf_len);
  134. }
  135. #endif
  136. #if NRF_SD_BLE_API_VERSION >= 5
  137. uint32_t ble_l2cap_ch_setup_req_dec(uint8_t const * const p_buf,
  138. uint16_t packet_len,
  139. uint16_t * p_conn_handle,
  140. uint16_t * * pp_local_cid,
  141. ble_l2cap_ch_setup_params_t * * const pp_params)
  142. {
  143. SER_REQ_DEC_BEGIN(SD_BLE_L2CAP_CH_SETUP);
  144. SER_PULL_uint16(p_conn_handle);
  145. SER_PULL_COND(pp_local_cid, uint16_t_dec);
  146. SER_PULL_COND(pp_params, ble_l2cap_ch_setup_params_t_dec);
  147. if (*pp_params && (*pp_params)->rx_params.sdu_buf.p_data)
  148. {
  149. SER_ASSERT_LENGTH_LEQ((*pp_params)->rx_params.sdu_buf.len, CONN_BLE_L2CAP_SDU_MAX_BUFFER_SIZE);
  150. uint8_t * p_data = conn_ble_l2cap_sdu_pool_alloc((*pp_params)->rx_params.sdu_buf.len,
  151. (uint32_t)((*pp_params)->rx_params.sdu_buf.p_data));
  152. (*pp_params)->rx_params.sdu_buf.p_data = p_data;
  153. }
  154. SER_REQ_DEC_END;
  155. }
  156. uint32_t ble_l2cap_ch_setup_rsp_enc(uint32_t return_code,
  157. uint8_t * const p_buf,
  158. uint32_t * const p_buf_len,
  159. uint16_t * p_local_cid)
  160. {
  161. SER_RSP_ENC_BEGIN(SD_BLE_L2CAP_CH_SETUP);
  162. SER_PUSH_COND(p_local_cid, uint16_t_enc);
  163. SER_RSP_ENC_END;
  164. }
  165. uint32_t ble_l2cap_ch_release_req_dec(uint8_t const * const p_buf,
  166. uint16_t packet_len,
  167. uint16_t * p_conn_handle,
  168. uint16_t * p_local_cid)
  169. {
  170. SER_REQ_DEC_BEGIN(SD_BLE_L2CAP_CH_RELEASE);
  171. SER_PULL_uint16(p_conn_handle);
  172. SER_PULL_uint16(p_local_cid);
  173. SER_REQ_DEC_END;
  174. }
  175. uint32_t ble_l2cap_ch_release_rsp_enc(uint32_t return_code,
  176. uint8_t * const p_buf,
  177. uint32_t * const p_buf_len)
  178. {
  179. SER_RSP_ENC_RESULT_ONLY(SD_BLE_L2CAP_CH_RELEASE);
  180. }
  181. uint32_t ble_l2cap_ch_rx_req_dec(uint8_t const * const p_buf,
  182. uint16_t packet_len,
  183. uint16_t * p_conn_handle,
  184. uint16_t * p_local_cid,
  185. ble_data_t * * pp_sdu_buf)
  186. {
  187. SER_REQ_DEC_BEGIN(SD_BLE_L2CAP_CH_RX);
  188. SER_PULL_uint16(p_conn_handle);
  189. SER_PULL_uint16(p_local_cid);
  190. uint8_t * p_tmp = (uint8_t *)1;
  191. SER_PULL_COND(&p_tmp, NULL);
  192. if (p_tmp)
  193. {
  194. SER_PULL_uint16((uint16_t*)&(*pp_sdu_buf)->len);
  195. uint32_t addr;
  196. SER_PULL_uint32(&addr);
  197. SER_ASSERT_LENGTH_LEQ((*pp_sdu_buf)->len, CONN_BLE_L2CAP_SDU_MAX_BUFFER_SIZE);
  198. uint8_t * p_data = conn_ble_l2cap_sdu_pool_alloc((*pp_sdu_buf)->len, addr);
  199. (*pp_sdu_buf)->p_data = p_data;
  200. }
  201. else
  202. {
  203. *pp_sdu_buf = NULL;
  204. }
  205. SER_REQ_DEC_END;
  206. }
  207. uint32_t ble_l2cap_ch_rx_rsp_enc(uint32_t return_code,
  208. uint8_t * const p_buf,
  209. uint32_t * const p_buf_len)
  210. {
  211. SER_RSP_ENC_RESULT_ONLY(SD_BLE_L2CAP_CH_RX);
  212. }
  213. uint32_t ble_l2cap_ch_tx_req_dec(uint8_t const * const p_buf,
  214. uint16_t packet_len,
  215. uint16_t * p_conn_handle,
  216. uint16_t * p_local_cid,
  217. ble_data_t * * const pp_sdu_buf)
  218. {
  219. SER_REQ_DEC_BEGIN(SD_BLE_L2CAP_CH_TX);
  220. SER_PULL_uint16(p_conn_handle);
  221. SER_PULL_uint16(p_local_cid);
  222. uint8_t * p_tmp = (uint8_t *)1;
  223. SER_PULL_COND(&p_tmp, NULL);
  224. if (p_tmp)
  225. {
  226. uint32_t id;
  227. uint16_t len;
  228. SER_PULL_uint32(&id);
  229. SER_PULL_uint16(&len);
  230. SER_ASSERT_LENGTH_LEQ(len, CONN_BLE_L2CAP_SDU_MAX_BUFFER_SIZE);
  231. uint8_t * p_data = conn_ble_l2cap_sdu_pool_alloc(len, id);
  232. if (p_data)
  233. {
  234. SER_PULL_buf(&p_data, len, len);
  235. }
  236. (*pp_sdu_buf)->p_data = p_data;
  237. (*pp_sdu_buf)->len = len;
  238. }
  239. else
  240. {
  241. *pp_sdu_buf = NULL;
  242. }
  243. SER_REQ_DEC_END;
  244. }
  245. uint32_t ble_l2cap_ch_tx_rsp_enc(uint32_t return_code,
  246. uint8_t * const p_buf,
  247. uint32_t * const p_buf_len)
  248. {
  249. SER_RSP_ENC_RESULT_ONLY(SD_BLE_L2CAP_CH_TX);
  250. }
  251. uint32_t ble_l2cap_ch_flow_control_req_dec(uint8_t const * const p_buf,
  252. uint16_t packet_len,
  253. uint16_t * p_conn_handle,
  254. uint16_t * p_local_cid,
  255. uint16_t * p_credits,
  256. uint16_t * * pp_credits)
  257. {
  258. SER_REQ_DEC_BEGIN(SD_BLE_L2CAP_CH_FLOW_CONTROL);
  259. SER_PULL_uint16(p_conn_handle);
  260. SER_PULL_uint16(p_local_cid);
  261. SER_PULL_uint16(p_credits);
  262. SER_PULL_COND(pp_credits, NULL);
  263. SER_REQ_DEC_END;
  264. }
  265. uint32_t ble_l2cap_ch_flow_control_rsp_enc(uint32_t return_code,
  266. uint8_t * const p_buf,
  267. uint32_t * const p_buf_len,
  268. uint16_t const * const p_credits)
  269. {
  270. SER_RSP_ENC_BEGIN(SD_BLE_L2CAP_CH_FLOW_CONTROL);
  271. SER_PUSH_COND(p_credits, uint16_t_enc);
  272. SER_RSP_ENC_END;
  273. }
  274. #endif //NRF_SD_BLE_API_VERSION >= 5