ble_ots_l2cap.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. /**
  2. * Copyright (c) 2017 - 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_ots_l2cap.h"
  41. #include <string.h>
  42. #include "ble_l2cap.h"
  43. #include "ble_ots.h"
  44. #include "ble_ots_oacp.h"
  45. #define NRF_LOG_MODULE_NAME ble_ots_l2cap
  46. #include "nrf_log.h"
  47. NRF_LOG_MODULE_REGISTER();
  48. #define SDU_SIZE 1024
  49. static uint8_t data[SDU_SIZE];
  50. static ble_data_t m_sdu_buf =
  51. {
  52. .p_data = data,
  53. .len = SDU_SIZE
  54. };
  55. bool ble_ots_l2cap_is_channel_available(ble_ots_l2cap_t * p_ots_l2cap)
  56. {
  57. return p_ots_l2cap->state == CONNECTED;
  58. }
  59. uint32_t ble_ots_l2cap_abort_transmission(ble_ots_l2cap_t * p_ots_l2cap)
  60. {
  61. if (p_ots_l2cap == NULL)
  62. {
  63. return NRF_ERROR_NULL;
  64. }
  65. return NRF_ERROR_INVALID_PARAM;
  66. }
  67. uint32_t ble_ots_l2cap_init(ble_ots_l2cap_t * p_ots_l2cap, ble_ots_l2cap_init_t * p_ots_l2cap_init)
  68. {
  69. if (p_ots_l2cap == NULL || p_ots_l2cap_init == NULL)
  70. {
  71. return NRF_ERROR_NULL;
  72. }
  73. if (p_ots_l2cap_init->p_transfer_buffer == NULL || p_ots_l2cap_init->buffer_len == 0)
  74. {
  75. return NRF_ERROR_INVALID_PARAM;
  76. }
  77. if (p_ots_l2cap_init->p_ots_oacp == NULL)
  78. {
  79. return NRF_ERROR_INVALID_PARAM;
  80. }
  81. if (p_ots_l2cap_init->evt_handler == NULL)
  82. {
  83. return NRF_ERROR_INVALID_PARAM;
  84. }
  85. memset(p_ots_l2cap, 0, sizeof(ble_ots_l2cap_t));
  86. p_ots_l2cap->local_cid = BLE_OTS_INVALID_CID;
  87. p_ots_l2cap->p_ots_oacp = p_ots_l2cap_init->p_ots_oacp;
  88. p_ots_l2cap->evt_handler = p_ots_l2cap_init->evt_handler;
  89. p_ots_l2cap->rx_params.sdu_buf.len = m_sdu_buf.len;
  90. p_ots_l2cap->rx_params.sdu_buf.p_data = m_sdu_buf.p_data;
  91. p_ots_l2cap->state = NOT_CONNECTED;
  92. return NRF_SUCCESS;
  93. }
  94. /**@brief This function receives the next MTU of the object.
  95. *
  96. * @param[in] p_ots_l2cap Object Transfer Service structure.
  97. */
  98. static void receive_resume(ble_ots_l2cap_t * p_ots_l2cap)
  99. {
  100. ret_code_t err_code;
  101. ble_data_t sdu_buf;
  102. sdu_buf.p_data = &p_ots_l2cap->rx_params.sdu_buf.p_data[p_ots_l2cap->received_bytes];
  103. sdu_buf.len = p_ots_l2cap->rx_params.sdu_buf.len - p_ots_l2cap->received_bytes;
  104. err_code = sd_ble_l2cap_ch_rx(p_ots_l2cap->p_ots_oacp->p_ots->conn_handle,
  105. p_ots_l2cap->local_cid,
  106. &sdu_buf);
  107. if (err_code == NRF_ERROR_RESOURCES)
  108. {
  109. return; // Too many SDUs queued for transmission, the transmission will be tried again on the next BLE_L2CAP_EVT_CH_RX event.
  110. }
  111. if (err_code != NRF_SUCCESS && p_ots_l2cap->p_ots_oacp->p_ots->error_handler != NULL)
  112. {
  113. p_ots_l2cap->p_ots_oacp->p_ots->error_handler(err_code);
  114. }
  115. }
  116. /**@brief This function sends the next MTU of the object.
  117. *
  118. * @param[in] p_ots_l2cap Object Transfer Service structure.
  119. */
  120. static void send_resume(ble_ots_l2cap_t * p_ots_l2cap)
  121. {
  122. ret_code_t err_code;
  123. uint16_t tx_size;
  124. tx_size = MIN(p_ots_l2cap->transfer_len - p_ots_l2cap->transmitted_bytes,
  125. p_ots_l2cap->tx_params.tx_mtu);
  126. ble_data_t obj;
  127. obj.p_data = &p_ots_l2cap->tx_transfer_buffer.p_data[p_ots_l2cap->transmitted_bytes];
  128. obj.len = tx_size;
  129. err_code = sd_ble_l2cap_ch_tx(p_ots_l2cap->p_ots_oacp->p_ots->conn_handle,
  130. p_ots_l2cap->local_cid,
  131. &obj);
  132. if (err_code == NRF_ERROR_RESOURCES)
  133. {
  134. return; // Too many SDUs queued for transmission, the transmission will be tried again on the next BLE_L2CAP_EVT_CH_TX event.
  135. }
  136. if (err_code != NRF_SUCCESS && p_ots_l2cap->p_ots_oacp->p_ots->error_handler != NULL)
  137. {
  138. p_ots_l2cap->p_ots_oacp->p_ots->error_handler(err_code);
  139. }
  140. }
  141. uint32_t ble_ots_l2cap_obj_send(ble_ots_l2cap_t * p_ots_l2cap, uint8_t * p_data, uint16_t data_len)
  142. {
  143. if (p_ots_l2cap == NULL)
  144. {
  145. return NRF_ERROR_NULL;
  146. }
  147. if (p_data == NULL)
  148. {
  149. return NRF_ERROR_NULL;
  150. }
  151. if (data_len == 0)
  152. {
  153. return NRF_ERROR_INVALID_LENGTH;
  154. }
  155. if (p_ots_l2cap->local_cid == BLE_L2CAP_CID_INVALID)
  156. {
  157. return NRF_ERROR_INVALID_STATE;
  158. }
  159. if (p_ots_l2cap->state != CONNECTED)
  160. {
  161. return NRF_ERROR_INVALID_STATE;
  162. }
  163. p_ots_l2cap->tx_transfer_buffer.p_data = p_data;
  164. p_ots_l2cap->tx_transfer_buffer.len = data_len;
  165. p_ots_l2cap->transmitted_bytes = 0;
  166. p_ots_l2cap->transfer_len = data_len;
  167. p_ots_l2cap->state = SENDING;
  168. send_resume(p_ots_l2cap);
  169. return NRF_SUCCESS;
  170. }
  171. uint32_t ble_ots_l2cap_start_recv(ble_ots_l2cap_t * p_ots_l2cap, uint16_t len)
  172. {
  173. uint32_t err_code;
  174. if (p_ots_l2cap == NULL)
  175. {
  176. return NRF_ERROR_NULL;
  177. }
  178. if (p_ots_l2cap->state != CONNECTED)
  179. {
  180. return NRF_ERROR_INVALID_STATE;
  181. }
  182. if (p_ots_l2cap->local_cid == BLE_L2CAP_CID_INVALID)
  183. {
  184. return NRF_ERROR_INVALID_STATE;
  185. }
  186. p_ots_l2cap->received_bytes = 0;
  187. p_ots_l2cap->transfer_len = len;
  188. err_code = sd_ble_l2cap_ch_rx(p_ots_l2cap->p_ots_oacp->p_ots->conn_handle,
  189. p_ots_l2cap->local_cid,
  190. &p_ots_l2cap->rx_params.sdu_buf);
  191. if (err_code == NRF_SUCCESS)
  192. {
  193. p_ots_l2cap->state = RECEIVING;
  194. }
  195. return err_code;
  196. }
  197. /**@brief This function is called on a channel setup request. It responds with the connection parameters.
  198. * @param[in] p_ots_l2cap Object transfer service l2cap module structure.
  199. * @param[in] p_ble_evt Event received from the BLE stack.
  200. */
  201. static inline void on_l2cap_ch_setup_request(ble_ots_l2cap_t * p_ots_l2cap, ble_evt_t const * p_ble_evt)
  202. {
  203. uint32_t err_code;
  204. ble_l2cap_ch_setup_params_t ch_setup_params = {{0}};
  205. if (p_ble_evt->evt.l2cap_evt.params.ch_setup_request.le_psm != BLE_OTS_PSM)
  206. {
  207. ch_setup_params.status = BLE_L2CAP_CH_STATUS_CODE_LE_PSM_NOT_SUPPORTED;
  208. NRF_LOG_WARNING("l2cap setup request for PSM 0x%x, rejecting since OTS PSM is 0x%x",
  209. p_ble_evt->evt.l2cap_evt.params.ch_setup_request.le_psm,
  210. BLE_L2CAP_CH_STATUS_CODE_LE_PSM_NOT_SUPPORTED);
  211. }
  212. else
  213. {
  214. ch_setup_params.status = BLE_L2CAP_CH_STATUS_CODE_SUCCESS;
  215. ch_setup_params.rx_params.rx_mps = p_ots_l2cap->conn_mps;
  216. if (ch_setup_params.rx_params.rx_mps < BLE_L2CAP_MPS_MIN)
  217. {
  218. ch_setup_params.rx_params.rx_mps = BLE_L2CAP_MPS_MIN;
  219. }
  220. ch_setup_params.rx_params.rx_mtu = p_ots_l2cap->conn_mtu;
  221. if (ch_setup_params.rx_params.rx_mtu < BLE_L2CAP_MTU_MIN)
  222. {
  223. ch_setup_params.rx_params.rx_mtu = BLE_L2CAP_MTU_MIN;
  224. }
  225. ch_setup_params.rx_params.sdu_buf.p_data = NULL;
  226. }
  227. p_ots_l2cap->local_cid = p_ble_evt->evt.l2cap_evt.local_cid;
  228. err_code = sd_ble_l2cap_ch_setup(p_ots_l2cap->p_ots_oacp->p_ots->conn_handle,
  229. &p_ots_l2cap->local_cid,
  230. &ch_setup_params);
  231. if (err_code != NRF_SUCCESS)
  232. {
  233. if (p_ots_l2cap->p_ots_oacp->p_ots->error_handler != NULL)
  234. {
  235. p_ots_l2cap->p_ots_oacp->p_ots->error_handler(err_code);
  236. }
  237. }
  238. NRF_LOG_INFO("Accepted L2CAP Channel");
  239. }
  240. /**@brief This function is called on a channel setup request. The parameters are stored. Events are forwarded.
  241. *
  242. * @param[in] p_ots_l2cap Object transfer service l2cap module structure.
  243. * @param[in] p_ble_evt Event received from the BLE stack.
  244. */
  245. static inline void on_l2cap_ch_setup(ble_ots_l2cap_t * p_ots_l2cap, ble_evt_t const * p_ble_evt)
  246. {
  247. p_ots_l2cap->local_cid = p_ble_evt->evt.l2cap_evt.local_cid;
  248. p_ots_l2cap->tx_params.credits = p_ble_evt->evt.l2cap_evt.params.ch_setup.tx_params.credits;
  249. p_ots_l2cap->tx_params.tx_mps = p_ble_evt->evt.l2cap_evt.params.ch_setup.tx_params.tx_mps;
  250. p_ots_l2cap->tx_params.tx_mtu = p_ble_evt->evt.l2cap_evt.params.ch_setup.tx_params.tx_mtu;
  251. ble_ots_l2cap_evt_t evt;
  252. evt.type = BLE_OTS_L2CAP_EVT_CH_CONNECTED;
  253. p_ots_l2cap->state = CONNECTED;
  254. p_ots_l2cap->evt_handler(p_ots_l2cap, &evt);
  255. }
  256. /**@brief Function for handling the BLE_L2CAP_EVT_CH_RELEASED event.
  257. *
  258. * @param[in] p_ots_l2cap Object transfer service l2cap module structure.
  259. * @param[in] p_ble_evt Pointer to the event received from BLE stack.
  260. */
  261. static inline void on_l2cap_ch_released(ble_ots_l2cap_t * p_ots_l2cap, ble_evt_t const * p_ble_evt)
  262. {
  263. if(p_ots_l2cap->local_cid != p_ble_evt->evt.l2cap_evt.local_cid)
  264. {
  265. return;
  266. }
  267. ble_ots_l2cap_evt_t evt;
  268. evt.type = BLE_OTS_L2CAP_EVT_CH_DISCONNECTED;
  269. p_ots_l2cap->evt_handler(p_ots_l2cap, &evt);
  270. p_ots_l2cap->state = NOT_CONNECTED;
  271. p_ots_l2cap->local_cid = BLE_OTS_INVALID_CID;
  272. }
  273. /**@brief Function for handling the BLE_L2CAP_EVT_CH_TX event.
  274. *
  275. * @param[in] p_ots_l2cap Object transfer service l2cap module structure.
  276. * @param[in] p_ble_evt Pointer to the event received from BLE stack.
  277. */
  278. static void on_l2cap_ch_tx(ble_ots_l2cap_t * p_ots_l2cap, ble_evt_t const * p_ble_evt)
  279. {
  280. if(p_ots_l2cap->local_cid != p_ble_evt->evt.l2cap_evt.local_cid)
  281. {
  282. return;
  283. }
  284. NRF_LOG_DEBUG("Bytes sent in the previous PDU: %i",
  285. p_ble_evt->evt.l2cap_evt.params.tx.sdu_buf.len);
  286. NRF_LOG_HEXDUMP_DEBUG(p_ble_evt->evt.l2cap_evt.params.tx.sdu_buf.p_data,
  287. p_ble_evt->evt.l2cap_evt.params.tx.sdu_buf.len);
  288. p_ots_l2cap->transmitted_bytes += p_ble_evt->evt.l2cap_evt.params.tx.sdu_buf.len;
  289. uint16_t remaining_tx_bytes = p_ots_l2cap->tx_transfer_buffer.len - p_ots_l2cap->transmitted_bytes;
  290. NRF_LOG_DEBUG("Total bytes transmitted: %i ",
  291. (p_ots_l2cap->transmitted_bytes));
  292. NRF_LOG_DEBUG("Total bytes remaining: %i",
  293. (remaining_tx_bytes));
  294. if (remaining_tx_bytes == 0)
  295. {
  296. ble_ots_l2cap_evt_t evt;
  297. evt.type = BLE_OTS_L2CAP_EVT_SEND_COMPLETE;
  298. evt.param.len = p_ots_l2cap->transfer_len;
  299. evt.param.p_data = p_ots_l2cap->tx_transfer_buffer.p_data;
  300. evt.param.len = p_ots_l2cap->tx_transfer_buffer.len;
  301. p_ots_l2cap->evt_handler(p_ots_l2cap, &evt);
  302. p_ots_l2cap->state = CONNECTED;
  303. p_ots_l2cap->transfer_len = 0;
  304. }
  305. else
  306. {
  307. send_resume(p_ots_l2cap);
  308. }
  309. }
  310. static void on_l2cap_ch_rx(ble_ots_l2cap_t * p_ots_l2cap, ble_evt_t const * p_ble_evt)
  311. {
  312. if(p_ots_l2cap->local_cid != p_ble_evt->evt.l2cap_evt.local_cid)
  313. {
  314. return;
  315. }
  316. NRF_LOG_DEBUG("Bytes received: %i", p_ble_evt->evt.l2cap_evt.params.rx.sdu_len);
  317. NRF_LOG_HEXDUMP_DEBUG(p_ble_evt->evt.l2cap_evt.params.rx.sdu_buf.p_data,
  318. p_ble_evt->evt.l2cap_evt.params.rx.sdu_len);
  319. ble_ots_l2cap_evt_t evt;
  320. memcpy(&p_ots_l2cap->p_ots_oacp->p_ots->p_current_object->data[p_ots_l2cap->received_bytes],
  321. p_ble_evt->evt.l2cap_evt.params.rx.sdu_buf.p_data,
  322. p_ble_evt->evt.l2cap_evt.params.rx.sdu_len);
  323. p_ots_l2cap->received_bytes += p_ble_evt->evt.l2cap_evt.params.rx.sdu_len;
  324. uint16_t remaining_bytes = (p_ots_l2cap->transfer_len - p_ots_l2cap->received_bytes);
  325. NRF_LOG_DEBUG("Remaining bytes to receive: %i", remaining_bytes);
  326. if(remaining_bytes == 0)
  327. {
  328. evt.type = BLE_OTS_L2CAP_EVT_RECV_COMPLETE;
  329. evt.param.len = p_ots_l2cap->rx_params.sdu_buf.len;
  330. evt.param.p_data = p_ots_l2cap->rx_params.sdu_buf.p_data;
  331. p_ots_l2cap->evt_handler(p_ots_l2cap, &evt);
  332. p_ots_l2cap->state = CONNECTED;
  333. p_ots_l2cap->transfer_len = 0;
  334. }
  335. else
  336. {
  337. receive_resume(p_ots_l2cap);
  338. }
  339. }
  340. void ble_ots_l2cap_on_ble_evt(ble_ots_l2cap_t * p_ots_l2cap, ble_evt_t const * p_ble_evt)
  341. {
  342. if ((p_ots_l2cap == NULL) || (p_ble_evt == NULL))
  343. {
  344. return;
  345. }
  346. switch (p_ble_evt->header.evt_id)
  347. {
  348. case BLE_L2CAP_EVT_CH_SETUP_REQUEST:
  349. on_l2cap_ch_setup_request(p_ots_l2cap, p_ble_evt);
  350. break;
  351. case BLE_L2CAP_EVT_CH_SETUP_REFUSED:
  352. // This should not happen because the client should be the initiator.
  353. if (p_ots_l2cap->p_ots_oacp->p_ots->error_handler != NULL)
  354. {
  355. p_ots_l2cap->p_ots_oacp->p_ots->error_handler(NRF_ERROR_INVALID_STATE);
  356. }
  357. break;
  358. case BLE_L2CAP_EVT_CH_SETUP:
  359. on_l2cap_ch_setup(p_ots_l2cap, p_ble_evt);
  360. break;
  361. case BLE_L2CAP_EVT_CH_RELEASED:
  362. on_l2cap_ch_released(p_ots_l2cap, p_ble_evt);
  363. break;
  364. case BLE_L2CAP_EVT_CH_SDU_BUF_RELEASED:
  365. break;
  366. case BLE_L2CAP_EVT_CH_CREDIT:
  367. break;
  368. case BLE_L2CAP_EVT_CH_RX:
  369. on_l2cap_ch_rx(p_ots_l2cap, p_ble_evt);
  370. break;
  371. case BLE_L2CAP_EVT_CH_TX:
  372. on_l2cap_ch_tx(p_ots_l2cap, p_ble_evt);
  373. break;
  374. default:
  375. // No implementation needed.
  376. break;
  377. }
  378. }