ble_l2cap_struct_serialization.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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_struct_serialization.h"
  41. #include "ble_struct_serialization.h"
  42. #include "ble_serialization.h"
  43. #include "app_util.h"
  44. #include "cond_field_serialization.h"
  45. #include <string.h>
  46. #if defined(NRF_SD_BLE_API_VERSION) && NRF_SD_BLE_API_VERSION < 4
  47. uint32_t ble_l2cap_header_t_enc(void const * const p_void_struct,
  48. uint8_t * const p_buf,
  49. uint32_t buf_len,
  50. uint32_t * const p_index)
  51. {
  52. SER_STRUCT_ENC_BEGIN(ble_l2cap_header_t);
  53. SER_PUSH_uint16(&p_struct->len);
  54. SER_PUSH_uint16(&p_struct->cid);
  55. SER_STRUCT_ENC_END;
  56. }
  57. uint32_t ble_l2cap_header_t_dec(uint8_t const * const p_buf,
  58. uint32_t buf_len,
  59. uint32_t * const p_index,
  60. void * const p_void_struct)
  61. {
  62. SER_STRUCT_DEC_BEGIN(ble_l2cap_header_t);
  63. SER_PULL_uint16(&p_struct->len);
  64. SER_PULL_uint16(&p_struct->cid);
  65. SER_STRUCT_DEC_END;
  66. }
  67. uint32_t ble_l2cap_evt_rx_t_enc(void const * const p_void_struct,
  68. uint8_t * const p_buf,
  69. uint32_t buf_len,
  70. uint32_t * const p_index)
  71. {
  72. SER_STRUCT_ENC_BEGIN(ble_l2cap_evt_rx_t);
  73. SER_PUSH_FIELD(&p_struct->header, ble_l2cap_header_t_enc);
  74. SER_PUSH_uint8array(p_struct->data, p_struct->header.len);
  75. SER_STRUCT_ENC_END;
  76. }
  77. uint32_t ble_l2cap_evt_rx_t_dec(uint8_t const * const p_buf,
  78. uint32_t buf_len,
  79. uint32_t * const p_index,
  80. uint32_t * const p_ext_len,
  81. void * const p_void_struct)
  82. {
  83. SER_STRUCT_DEC_BEGIN(ble_l2cap_evt_rx_t);
  84. SER_PULL_FIELD(&p_struct->header, ble_l2cap_header_t_dec);
  85. uint32_t data_len = (SUB1(p_struct->header.len));
  86. SER_ASSERT_LENGTH_LEQ(data_len, *p_ext_len);
  87. SER_PULL_uint8array(p_struct->data, p_struct->header.len);
  88. *p_ext_len = data_len;
  89. SER_STRUCT_DEC_END;
  90. }
  91. #endif
  92. #if NRF_SD_BLE_API_VERSION >= 5
  93. uint32_t ble_l2cap_conn_cfg_t_enc(void const * const p_void_struct,
  94. uint8_t * const p_buf,
  95. uint32_t buf_len,
  96. uint32_t * const p_index)
  97. {
  98. SER_STRUCT_ENC_BEGIN(ble_l2cap_conn_cfg_t);
  99. SER_PUSH_uint16(&p_struct->rx_mps);
  100. SER_PUSH_uint16(&p_struct->tx_mps);
  101. SER_PUSH_uint8(&p_struct->rx_queue_size);
  102. SER_PUSH_uint8(&p_struct->tx_queue_size);
  103. SER_PUSH_uint8(&p_struct->ch_count);
  104. SER_STRUCT_ENC_END;
  105. }
  106. uint32_t ble_l2cap_conn_cfg_t_dec(uint8_t const * const p_buf,
  107. uint32_t buf_len,
  108. uint32_t * const p_index,
  109. void * const p_void_struct)
  110. {
  111. SER_STRUCT_DEC_BEGIN(ble_l2cap_conn_cfg_t);
  112. SER_PULL_uint16(&p_struct->rx_mps);
  113. SER_PULL_uint16(&p_struct->tx_mps);
  114. SER_PULL_uint8(&p_struct->rx_queue_size);
  115. SER_PULL_uint8(&p_struct->tx_queue_size);
  116. SER_PULL_uint8(&p_struct->ch_count);
  117. SER_STRUCT_DEC_END;
  118. }
  119. uint32_t ble_l2cap_ch_rx_params_t_enc(void const * const p_void_struct,
  120. uint8_t * const p_buf,
  121. uint32_t buf_len,
  122. uint32_t * const p_index)
  123. {
  124. SER_STRUCT_ENC_BEGIN(ble_l2cap_ch_rx_params_t);
  125. SER_PUSH_uint16(&p_struct->rx_mtu);
  126. SER_PUSH_uint16(&p_struct->rx_mps);
  127. SER_PUSH_uint16(&p_struct->sdu_buf.len);
  128. SER_PUSH_uint32(&p_struct->sdu_buf.p_data);
  129. SER_STRUCT_ENC_END;
  130. }
  131. uint32_t ble_l2cap_ch_rx_params_t_dec(uint8_t const * const p_buf,
  132. uint32_t buf_len,
  133. uint32_t * const p_index,
  134. void * const p_void_struct)
  135. {
  136. SER_STRUCT_DEC_BEGIN(ble_l2cap_ch_rx_params_t);
  137. SER_PULL_uint16(&p_struct->rx_mtu);
  138. SER_PULL_uint16(&p_struct->rx_mps);
  139. SER_PULL_uint16(&p_struct->sdu_buf.len);
  140. SER_PULL_uint32(&p_struct->sdu_buf.p_data);
  141. SER_STRUCT_DEC_END;
  142. }
  143. uint32_t ble_l2cap_ch_setup_params_t_enc(void const * const p_void_struct,
  144. uint8_t * const p_buf,
  145. uint32_t buf_len,
  146. uint32_t * const p_index)
  147. {
  148. SER_STRUCT_ENC_BEGIN(ble_l2cap_ch_setup_params_t);
  149. SER_PUSH_FIELD(&p_struct->rx_params, ble_l2cap_ch_rx_params_t_enc);
  150. SER_PUSH_uint16(&p_struct->le_psm);
  151. SER_PUSH_uint16(&p_struct->status);
  152. SER_STRUCT_ENC_END;
  153. }
  154. uint32_t ble_l2cap_ch_setup_params_t_dec(uint8_t const * const p_buf,
  155. uint32_t buf_len,
  156. uint32_t * const p_index,
  157. void * const p_void_struct)
  158. {
  159. SER_STRUCT_DEC_BEGIN(ble_l2cap_ch_setup_params_t);
  160. SER_PULL_FIELD(&p_struct->rx_params, ble_l2cap_ch_rx_params_t_dec);
  161. SER_PULL_uint16(&p_struct->le_psm);
  162. SER_PULL_uint16(&p_struct->status);
  163. SER_STRUCT_DEC_END;
  164. }
  165. uint32_t ble_l2cap_ch_tx_params_t_enc(void const * const p_void_struct,
  166. uint8_t * const p_buf,
  167. uint32_t buf_len,
  168. uint32_t * const p_index)
  169. {
  170. SER_STRUCT_ENC_BEGIN(ble_l2cap_ch_tx_params_t);
  171. SER_PUSH_uint16(&p_struct->tx_mtu);
  172. SER_PUSH_uint16(&p_struct->peer_mps);
  173. SER_PUSH_uint16(&p_struct->tx_mps);
  174. SER_PUSH_uint16(&p_struct->credits);
  175. SER_STRUCT_ENC_END;
  176. }
  177. uint32_t ble_l2cap_ch_tx_params_t_dec(uint8_t const * const p_buf,
  178. uint32_t buf_len,
  179. uint32_t * const p_index,
  180. void * const p_void_struct)
  181. {
  182. SER_STRUCT_DEC_BEGIN(ble_l2cap_ch_tx_params_t);
  183. SER_PULL_uint16(&p_struct->tx_mtu);
  184. SER_PULL_uint16(&p_struct->peer_mps);
  185. SER_PULL_uint16(&p_struct->tx_mps);
  186. SER_PULL_uint16(&p_struct->credits);
  187. SER_STRUCT_DEC_END;
  188. }
  189. #endif //NRF_SD_BLE_API_VERSION >= 5