ble_l2cap_evt_conn.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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_evt_conn.h"
  41. #include <string.h>
  42. #include "ble_serialization.h"
  43. #include "ble_struct_serialization.h"
  44. #include "ble_l2cap_struct_serialization.h"
  45. #include "app_util.h"
  46. #include "conn_ble_l2cap_sdu_pool.h"
  47. #if defined(NRF_SD_BLE_API_VERSION) && NRF_SD_BLE_API_VERSION < 4
  48. uint32_t ble_l2cap_evt_rx_enc(ble_evt_t const * const p_event,
  49. uint32_t event_len,
  50. uint8_t * const p_buf,
  51. uint32_t * const p_buf_len)
  52. {
  53. SER_EVT_ENC_BEGIN(BLE_L2CAP_EVT_RX);
  54. SER_PUSH_uint16(&p_event->evt.l2cap_evt.conn_handle);
  55. SER_PUSH_FIELD(&p_event->evt.l2cap_evt.params.rx, ble_l2cap_evt_rx_t_enc);
  56. SER_EVT_ENC_END;
  57. }
  58. #endif
  59. #if defined(NRF_SD_BLE_API_VERSION) && NRF_SD_BLE_API_VERSION >= 5
  60. uint32_t ble_l2cap_evt_ch_setup_request_enc(ble_evt_t const * const p_event,
  61. uint32_t event_len,
  62. uint8_t * const p_buf,
  63. uint32_t * const p_buf_len)
  64. {
  65. SER_EVT_ENC_BEGIN(BLE_L2CAP_EVT_CH_SETUP_REQUEST);
  66. SER_PUSH_uint16(&p_event->evt.l2cap_evt.conn_handle);
  67. SER_PUSH_uint16(&p_event->evt.l2cap_evt.local_cid);
  68. SER_PUSH_uint16(&p_event->evt.l2cap_evt.params.ch_setup_request.le_psm);
  69. SER_PUSH_FIELD(&p_event->evt.l2cap_evt.params.ch_setup_request.tx_params, ble_l2cap_ch_tx_params_t_enc);
  70. SER_EVT_ENC_END;
  71. }
  72. uint32_t ble_l2cap_evt_ch_setup_refused_enc(ble_evt_t const * const p_event,
  73. uint32_t event_len,
  74. uint8_t * const p_buf,
  75. uint32_t * const p_buf_len)
  76. {
  77. SER_EVT_ENC_BEGIN(BLE_L2CAP_EVT_CH_SETUP_REFUSED);
  78. SER_PUSH_uint16(&p_event->evt.l2cap_evt.conn_handle);
  79. SER_PUSH_uint16(&p_event->evt.l2cap_evt.local_cid);
  80. SER_PUSH_uint8(&p_event->evt.l2cap_evt.params.ch_setup_refused.source);
  81. SER_PUSH_uint16(&p_event->evt.l2cap_evt.params.ch_setup_refused.status);
  82. SER_EVT_ENC_END;
  83. }
  84. uint32_t ble_l2cap_evt_ch_setup_enc(ble_evt_t const * const p_event,
  85. uint32_t event_len,
  86. uint8_t * const p_buf,
  87. uint32_t * const p_buf_len)
  88. {
  89. SER_EVT_ENC_BEGIN(BLE_L2CAP_EVT_CH_SETUP);
  90. SER_PUSH_uint16(&p_event->evt.l2cap_evt.conn_handle);
  91. SER_PUSH_uint16(&p_event->evt.l2cap_evt.local_cid);
  92. SER_PUSH_FIELD(&p_event->evt.l2cap_evt.params.ch_setup.tx_params, ble_l2cap_ch_tx_params_t_enc);
  93. SER_EVT_ENC_END;
  94. }
  95. uint32_t ble_l2cap_evt_ch_released_enc(ble_evt_t const * const p_event,
  96. uint32_t event_len,
  97. uint8_t * const p_buf,
  98. uint32_t * const p_buf_len)
  99. {
  100. SER_EVT_ENC_BEGIN(BLE_L2CAP_EVT_CH_RELEASED);
  101. SER_PUSH_uint16(&p_event->evt.l2cap_evt.conn_handle);
  102. SER_PUSH_uint16(&p_event->evt.l2cap_evt.local_cid);
  103. SER_EVT_ENC_END;
  104. }
  105. uint32_t ble_l2cap_evt_ch_sdu_buf_released_enc(ble_evt_t const * const p_event,
  106. uint32_t event_len,
  107. uint8_t * const p_buf,
  108. uint32_t * const p_buf_len)
  109. {
  110. SER_EVT_ENC_BEGIN(BLE_L2CAP_EVT_CH_SDU_BUF_RELEASED);
  111. SER_PUSH_uint16(&p_event->evt.l2cap_evt.conn_handle);
  112. SER_PUSH_uint16(&p_event->evt.l2cap_evt.local_cid);
  113. SER_PUSH_uint16(&p_event->evt.l2cap_evt.params.ch_sdu_buf_released.sdu_buf.len);
  114. uint32_t * p_data = (uint32_t *)p_event->evt.l2cap_evt.params.ch_sdu_buf_released.sdu_buf.p_data;
  115. if (p_data)
  116. {
  117. uint32_t id = *(p_data - 1);
  118. conn_ble_l2cap_sdu_pool_free(p_data);
  119. SER_PUSH_uint32(&id);
  120. }
  121. SER_EVT_ENC_END;
  122. }
  123. uint32_t ble_l2cap_evt_ch_credit_enc(ble_evt_t const * const p_event,
  124. uint32_t event_len,
  125. uint8_t * const p_buf,
  126. uint32_t * const p_buf_len)
  127. {
  128. SER_EVT_ENC_BEGIN(BLE_L2CAP_EVT_CH_CREDIT);
  129. SER_PUSH_uint16(&p_event->evt.l2cap_evt.conn_handle);
  130. SER_PUSH_uint16(&p_event->evt.l2cap_evt.local_cid);
  131. SER_PUSH_uint16(&p_event->evt.l2cap_evt.params.credit.credits);
  132. SER_EVT_ENC_END;
  133. }
  134. uint32_t ble_l2cap_evt_ch_rx_enc(ble_evt_t const * const p_event,
  135. uint32_t event_len,
  136. uint8_t * const p_buf,
  137. uint32_t * const p_buf_len)
  138. {
  139. SER_EVT_ENC_BEGIN(BLE_L2CAP_EVT_CH_RX);
  140. SER_PUSH_uint16(&p_event->evt.l2cap_evt.conn_handle);
  141. SER_PUSH_uint16(&p_event->evt.l2cap_evt.local_cid);
  142. SER_PUSH_uint16(&p_event->evt.l2cap_evt.params.rx.sdu_len);
  143. SER_PUSH_uint16(&p_event->evt.l2cap_evt.params.rx.sdu_buf.len);
  144. uint32_t * p_data = (uint32_t *)p_event->evt.l2cap_evt.params.rx.sdu_buf.p_data;
  145. uint32_t id = *(p_data - 1);
  146. SER_PUSH_uint32(&id);
  147. SER_PUSH_buf(p_event->evt.l2cap_evt.params.rx.sdu_buf.p_data, p_event->evt.l2cap_evt.params.rx.sdu_buf.len);
  148. conn_ble_l2cap_sdu_pool_free(p_data);
  149. SER_EVT_ENC_END;
  150. }
  151. uint32_t ble_l2cap_evt_ch_tx_enc(ble_evt_t const * const p_event,
  152. uint32_t event_len,
  153. uint8_t * const p_buf,
  154. uint32_t * const p_buf_len)
  155. {
  156. SER_EVT_ENC_BEGIN(BLE_L2CAP_EVT_CH_TX);
  157. SER_PUSH_uint16(&p_event->evt.l2cap_evt.conn_handle);
  158. SER_PUSH_uint16(&p_event->evt.l2cap_evt.local_cid);
  159. SER_PUSH_uint16(&p_event->evt.l2cap_evt.params.tx.sdu_buf.len);
  160. uint32_t * p_data = (uint32_t *)p_event->evt.l2cap_evt.params.tx.sdu_buf.p_data;
  161. if (p_data)
  162. {
  163. uint32_t id = *(p_data - 1);
  164. conn_ble_l2cap_sdu_pool_free(p_data);
  165. SER_PUSH_uint32(&id);
  166. }
  167. SER_EVT_ENC_END;
  168. }
  169. #endif