conn_mw_ble_l2cap.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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_l2cap_conn.h"
  41. #include "conn_mw_ble_l2cap.h"
  42. #include "ble_serialization.h"
  43. #if defined(NRF_SD_BLE_API_VERSION) && NRF_SD_BLE_API_VERSION < 4
  44. uint32_t conn_mw_ble_l2cap_cid_register(uint8_t const * const p_rx_buf,
  45. uint32_t rx_buf_len,
  46. uint8_t * const p_tx_buf,
  47. uint32_t * const p_tx_buf_len)
  48. {
  49. SER_ASSERT_NOT_NULL(p_rx_buf);
  50. SER_ASSERT_NOT_NULL(p_tx_buf);
  51. SER_ASSERT_NOT_NULL(p_tx_buf_len);
  52. uint16_t cid;
  53. uint32_t err_code = NRF_SUCCESS;
  54. uint32_t sd_err_code;
  55. err_code = ble_l2cap_cid_register_req_dec(p_rx_buf, rx_buf_len, &cid);
  56. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  57. sd_err_code = sd_ble_l2cap_cid_register(cid);
  58. err_code = ble_l2cap_cid_register_rsp_enc(sd_err_code, p_tx_buf, p_tx_buf_len);
  59. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  60. return err_code;
  61. }
  62. uint32_t conn_mw_ble_l2cap_cid_unregister(uint8_t const * const p_rx_buf,
  63. uint32_t rx_buf_len,
  64. uint8_t * const p_tx_buf,
  65. uint32_t * const p_tx_buf_len)
  66. {
  67. SER_ASSERT_NOT_NULL(p_rx_buf);
  68. SER_ASSERT_NOT_NULL(p_tx_buf);
  69. SER_ASSERT_NOT_NULL(p_tx_buf_len);
  70. uint16_t cid;
  71. uint32_t err_code = NRF_SUCCESS;
  72. uint32_t sd_err_code;
  73. err_code = ble_l2cap_cid_unregister_req_dec(p_rx_buf, rx_buf_len, &cid);
  74. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  75. sd_err_code = sd_ble_l2cap_cid_unregister(cid);
  76. err_code = ble_l2cap_cid_unregister_rsp_enc(sd_err_code, p_tx_buf, p_tx_buf_len);
  77. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  78. return err_code;
  79. }
  80. uint32_t conn_mw_ble_l2cap_tx(uint8_t const * const p_rx_buf,
  81. uint32_t rx_buf_len,
  82. uint8_t * const p_tx_buf,
  83. uint32_t * const p_tx_buf_len)
  84. {
  85. SER_ASSERT_NOT_NULL(p_rx_buf);
  86. SER_ASSERT_NOT_NULL(p_tx_buf);
  87. SER_ASSERT_NOT_NULL(p_tx_buf_len);
  88. uint16_t conn_handle;
  89. ble_l2cap_header_t l2cap_header;
  90. ble_l2cap_header_t * p_l2cap_header = &l2cap_header;
  91. uint32_t err_code = NRF_SUCCESS;
  92. uint32_t sd_err_code;
  93. uint8_t const * p_data = NULL;
  94. err_code = ble_l2cap_tx_req_dec(p_rx_buf, rx_buf_len, &conn_handle, &p_l2cap_header, &p_data);
  95. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  96. sd_err_code = sd_ble_l2cap_tx(conn_handle, p_l2cap_header, p_data);
  97. err_code = ble_l2cap_tx_rsp_enc(sd_err_code, p_tx_buf, p_tx_buf_len);
  98. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  99. return err_code;
  100. }
  101. #endif
  102. #if NRF_SD_BLE_API_VERSION >= 5
  103. uint32_t conn_mw_l2cap_ch_setup(uint8_t const * const p_rx_buf,
  104. uint32_t rx_buf_len,
  105. uint8_t * const p_tx_buf,
  106. uint32_t * const p_tx_buf_len)
  107. {
  108. SER_ASSERT_NOT_NULL(p_rx_buf);
  109. SER_ASSERT_NOT_NULL(p_tx_buf);
  110. SER_ASSERT_NOT_NULL(p_tx_buf_len);
  111. uint16_t conn_handle;
  112. uint16_t local_cid;
  113. uint16_t * p_local_cid = &local_cid;
  114. ble_l2cap_ch_setup_params_t params;
  115. ble_l2cap_ch_setup_params_t * p_params = &params;
  116. uint32_t sd_err_code;
  117. uint32_t err_code = NRF_SUCCESS;
  118. err_code = ble_l2cap_ch_setup_req_dec(p_rx_buf, rx_buf_len, &conn_handle, &p_local_cid, &p_params);
  119. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  120. sd_err_code = sd_ble_l2cap_ch_setup(conn_handle, p_local_cid, p_params);
  121. err_code = ble_l2cap_ch_setup_rsp_enc(sd_err_code, p_tx_buf, p_tx_buf_len, p_local_cid);
  122. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  123. return err_code;
  124. }
  125. uint32_t conn_mw_l2cap_ch_release(uint8_t const * const p_rx_buf,
  126. uint32_t rx_buf_len,
  127. uint8_t * const p_tx_buf,
  128. uint32_t * const p_tx_buf_len)
  129. {
  130. SER_ASSERT_NOT_NULL(p_rx_buf);
  131. SER_ASSERT_NOT_NULL(p_tx_buf);
  132. SER_ASSERT_NOT_NULL(p_tx_buf_len);
  133. uint16_t conn_handle;
  134. uint16_t local_cid;
  135. uint32_t sd_err_code;
  136. uint32_t err_code = NRF_SUCCESS;
  137. err_code = ble_l2cap_ch_release_req_dec(p_rx_buf, rx_buf_len, &conn_handle, &local_cid);
  138. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  139. sd_err_code = sd_ble_l2cap_ch_release(conn_handle, local_cid);
  140. err_code = ble_l2cap_ch_release_rsp_enc(sd_err_code, p_tx_buf, p_tx_buf_len);
  141. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  142. return err_code;
  143. }
  144. uint32_t conn_mw_l2cap_ch_rx(uint8_t const * const p_rx_buf,
  145. uint32_t rx_buf_len,
  146. uint8_t * const p_tx_buf,
  147. uint32_t * const p_tx_buf_len)
  148. {
  149. SER_ASSERT_NOT_NULL(p_rx_buf);
  150. SER_ASSERT_NOT_NULL(p_tx_buf);
  151. SER_ASSERT_NOT_NULL(p_tx_buf_len);
  152. uint16_t conn_handle;
  153. uint16_t local_cid;
  154. ble_data_t ble_data;
  155. ble_data_t * p_ble_data = &ble_data;
  156. uint32_t sd_err_code;
  157. uint32_t err_code = NRF_SUCCESS;
  158. err_code = ble_l2cap_ch_rx_req_dec(p_rx_buf, rx_buf_len, &conn_handle, &local_cid, &p_ble_data);
  159. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  160. sd_err_code = sd_ble_l2cap_ch_rx(conn_handle, local_cid, p_ble_data);
  161. err_code = ble_l2cap_ch_rx_rsp_enc(sd_err_code, p_tx_buf, p_tx_buf_len);
  162. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  163. return err_code;
  164. }
  165. uint32_t conn_mw_l2cap_ch_tx(uint8_t const * const p_rx_buf,
  166. uint32_t rx_buf_len,
  167. uint8_t * const p_tx_buf,
  168. uint32_t * const p_tx_buf_len)
  169. {
  170. SER_ASSERT_NOT_NULL(p_rx_buf);
  171. SER_ASSERT_NOT_NULL(p_tx_buf);
  172. SER_ASSERT_NOT_NULL(p_tx_buf_len);
  173. uint16_t conn_handle;
  174. uint16_t local_cid;
  175. ble_data_t ble_data;
  176. ble_data_t * p_ble_data = &ble_data;
  177. uint32_t sd_err_code;
  178. uint32_t err_code = NRF_SUCCESS;
  179. err_code = ble_l2cap_ch_tx_req_dec(p_rx_buf, rx_buf_len, &conn_handle, &local_cid, &p_ble_data);
  180. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  181. sd_err_code = sd_ble_l2cap_ch_tx(conn_handle, local_cid, p_ble_data);
  182. err_code = ble_l2cap_ch_tx_rsp_enc(sd_err_code, p_tx_buf, p_tx_buf_len);
  183. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  184. return err_code;
  185. }
  186. uint32_t conn_mw_l2cap_ch_flow_control(uint8_t const * const p_rx_buf,
  187. uint32_t rx_buf_len,
  188. uint8_t * const p_tx_buf,
  189. uint32_t * const p_tx_buf_len)
  190. {
  191. SER_ASSERT_NOT_NULL(p_rx_buf);
  192. SER_ASSERT_NOT_NULL(p_tx_buf);
  193. SER_ASSERT_NOT_NULL(p_tx_buf_len);
  194. uint16_t conn_handle;
  195. uint16_t local_cid;
  196. uint16_t credits;
  197. uint16_t out_credits;
  198. uint16_t * p_out_credits = &out_credits;
  199. uint32_t sd_err_code;
  200. uint32_t err_code = NRF_SUCCESS;
  201. err_code = ble_l2cap_ch_flow_control_req_dec(p_rx_buf, rx_buf_len,
  202. &conn_handle, &local_cid, &credits, &p_out_credits);
  203. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  204. sd_err_code = sd_ble_l2cap_ch_flow_control(conn_handle, local_cid, credits, p_out_credits);
  205. err_code = ble_l2cap_ch_flow_control_rsp_enc(sd_err_code, p_tx_buf, p_tx_buf_len, p_out_credits);
  206. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  207. return err_code;
  208. }
  209. #endif