ble_gatts_conn.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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_gatts_conn.h"
  41. #include <string.h>
  42. #include "ble_serialization.h"
  43. #include "ble_struct_serialization.h"
  44. #include "ble_gatts_struct_serialization.h"
  45. #include "app_util.h"
  46. #include "cond_field_serialization.h"
  47. uint32_t ble_gatts_attr_get_req_dec(uint8_t const * const p_buf,
  48. uint32_t packet_len,
  49. uint16_t * p_handle,
  50. ble_uuid_t * * pp_uuid,
  51. ble_gatts_attr_md_t * * pp_md)
  52. {
  53. SER_REQ_DEC_BEGIN(SD_BLE_GATTS_ATTR_GET);
  54. SER_PULL_uint16(p_handle);
  55. SER_PULL_COND(pp_uuid, NULL);
  56. SER_PULL_COND(pp_md, NULL);
  57. SER_REQ_DEC_END;
  58. }
  59. uint32_t ble_gatts_attr_get_rsp_enc(uint32_t return_code,
  60. uint8_t * const p_buf,
  61. uint32_t * const p_buf_len,
  62. ble_uuid_t * p_uuid,
  63. ble_gatts_attr_md_t * p_md)
  64. {
  65. SER_RSP_ENC_BEGIN(SD_BLE_GATTS_ATTR_GET);
  66. SER_PUSH_COND(p_uuid, ble_uuid_t_enc);
  67. SER_PUSH_COND(p_md, ble_gatts_attr_md_t_enc);
  68. SER_RSP_ENC_END;
  69. }
  70. uint32_t ble_gatts_characteristic_add_req_dec(uint8_t const * const p_buf,
  71. uint32_t packet_len,
  72. uint16_t * p_service_handle,
  73. ble_gatts_char_md_t * * const pp_char_md,
  74. ble_gatts_attr_t * * const pp_attr_char_value,
  75. ble_gatts_char_handles_t * * const pp_handles)
  76. {
  77. SER_REQ_DEC_BEGIN(SD_BLE_GATTS_CHARACTERISTIC_ADD);
  78. SER_PULL_uint16(p_service_handle);
  79. SER_PULL_COND(pp_char_md, ble_gatts_char_md_t_dec);
  80. SER_PULL_COND(pp_attr_char_value, ble_gatts_attr_t_dec);
  81. SER_PULL_COND(pp_handles, NULL);
  82. SER_REQ_DEC_END;
  83. }
  84. uint32_t ble_gatts_characteristic_add_rsp_enc(uint32_t return_code,
  85. uint8_t * const p_buf,
  86. uint32_t * const p_buf_len,
  87. ble_gatts_char_handles_t const * const p_handles)
  88. {
  89. SER_RSP_ENC_BEGIN(SD_BLE_GATTS_CHARACTERISTIC_ADD);
  90. SER_PUSH_COND(p_handles, ble_gatts_char_handles_t_enc);
  91. SER_RSP_ENC_END;
  92. }
  93. uint32_t ble_gatts_descriptor_add_req_dec(uint8_t const * const p_buf,
  94. uint32_t packet_len,
  95. uint16_t * const p_char_handle,
  96. ble_gatts_attr_t * * const pp_attr,
  97. uint16_t * * const pp_handle)
  98. {
  99. SER_REQ_DEC_BEGIN(SD_BLE_GATTS_DESCRIPTOR_ADD);
  100. SER_PULL_uint16(p_char_handle);
  101. SER_PULL_COND(pp_attr, ble_gatts_attr_t_dec);
  102. SER_PULL_COND(pp_handle, NULL);
  103. SER_REQ_DEC_END;
  104. }
  105. uint32_t ble_gatts_descriptor_add_rsp_enc(uint32_t return_code,
  106. uint8_t * const p_buf,
  107. uint32_t * const p_buf_len,
  108. uint16_t * p_handle)
  109. {
  110. SER_RSP_ENC_BEGIN(SD_BLE_GATTS_DESCRIPTOR_ADD);
  111. SER_PUSH_COND(p_handle,uint16_t_enc);
  112. SER_RSP_ENC_END;
  113. }
  114. uint32_t ble_gatts_hvx_req_dec(uint8_t const * const p_buf,
  115. uint32_t packet_len,
  116. uint16_t * const p_conn_handle,
  117. ble_gatts_hvx_params_t * * const pp_hvx_params)
  118. {
  119. SER_REQ_DEC_BEGIN(SD_BLE_GATTS_HVX);
  120. SER_PULL_uint16(p_conn_handle);
  121. SER_PULL_COND(pp_hvx_params, ble_gatts_hvx_params_t_dec);
  122. SER_REQ_DEC_END;
  123. }
  124. uint32_t ble_gatts_hvx_rsp_enc(uint32_t return_code,
  125. uint8_t * const p_buf,
  126. uint32_t * const p_buf_len,
  127. uint16_t const * const p_bytes_written)
  128. {
  129. SER_RSP_ENC_BEGIN(SD_BLE_GATTS_HVX);
  130. SER_PUSH_COND(p_bytes_written, uint16_t_enc);
  131. SER_RSP_ENC_END;
  132. }
  133. uint32_t ble_gatts_include_add_req_dec(uint8_t const * const p_buf,
  134. uint16_t packet_len,
  135. uint16_t * const p_service_handle,
  136. uint16_t * const p_inc_srvc_handle,
  137. uint16_t * * const pp_include_handle)
  138. {
  139. SER_REQ_DEC_BEGIN(SD_BLE_GATTS_INCLUDE_ADD);
  140. SER_PULL_uint16(p_service_handle);
  141. SER_PULL_uint16(p_inc_srvc_handle);
  142. SER_PULL_COND(pp_include_handle, NULL);
  143. SER_REQ_DEC_END;
  144. }
  145. uint32_t ble_gatts_include_add_rsp_enc(uint32_t return_code,
  146. uint8_t * const p_buf,
  147. uint32_t * const p_buf_len,
  148. uint16_t const * const p_include_handle)
  149. {
  150. SER_RSP_ENC_BEGIN(SD_BLE_GATTS_INCLUDE_ADD);
  151. SER_PUSH_COND(p_include_handle, uint16_t_enc);
  152. SER_RSP_ENC_END;
  153. }
  154. uint32_t ble_gatts_initial_user_handle_get_req_dec(uint8_t const * const p_buf,
  155. uint32_t packet_len,
  156. uint16_t * * pp_handle)
  157. {
  158. SER_REQ_DEC_BEGIN(SD_BLE_GATTS_INITIAL_USER_HANDLE_GET);
  159. SER_PULL_COND(pp_handle, NULL);
  160. SER_REQ_DEC_END;
  161. }
  162. uint32_t ble_gatts_initial_user_handle_get_rsp_enc(uint32_t return_code,
  163. uint8_t * const p_buf,
  164. uint32_t * const p_buf_len,
  165. uint16_t * p_handle)
  166. {
  167. SER_RSP_ENC_BEGIN(SD_BLE_GATTS_INITIAL_USER_HANDLE_GET);
  168. SER_PUSH_COND(p_handle, uint16_t_enc);
  169. SER_RSP_ENC_END;
  170. }
  171. uint32_t ble_gatts_rw_authorize_reply_req_dec(uint8_t const * const p_buf,
  172. uint32_t packet_len,
  173. uint16_t * p_conn_handle,
  174. ble_gatts_rw_authorize_reply_params_t * * const pp_reply_params)
  175. {
  176. SER_REQ_DEC_BEGIN(SD_BLE_GATTS_RW_AUTHORIZE_REPLY);
  177. SER_PULL_uint16(p_conn_handle);
  178. SER_PULL_COND(pp_reply_params, ble_gatts_rw_authorize_reply_params_t_dec);
  179. SER_REQ_DEC_END;
  180. }
  181. uint32_t ble_gatts_rw_authorize_reply_rsp_enc(uint32_t return_code,
  182. uint8_t * const p_buf,
  183. uint32_t * const p_buf_len)
  184. {
  185. SER_RSP_ENC_RESULT_ONLY(SD_BLE_GATTS_RW_AUTHORIZE_REPLY);
  186. }
  187. uint32_t ble_gatts_service_add_req_dec(uint8_t const * const p_buf,
  188. uint32_t packet_len,
  189. uint8_t * const p_type,
  190. ble_uuid_t * * const pp_uuid,
  191. uint16_t * * const pp_handle)
  192. {
  193. SER_REQ_DEC_BEGIN(SD_BLE_GATTS_SERVICE_ADD);
  194. SER_PULL_uint8(p_type);
  195. SER_PULL_COND(pp_uuid, ble_uuid_t_dec);
  196. SER_PULL_COND(pp_handle, NULL);
  197. SER_REQ_DEC_END;
  198. }
  199. uint32_t ble_gatts_service_add_rsp_enc(uint32_t return_code,
  200. uint8_t * const p_buf,
  201. uint32_t * const p_buf_len,
  202. uint16_t const * const p_handle)
  203. {
  204. SER_RSP_ENC_BEGIN(SD_BLE_GATTS_SERVICE_ADD);
  205. SER_PUSH_COND(p_handle, uint16_t_enc);
  206. //SER_PUSH_uint16(p_handle);
  207. SER_RSP_ENC_END;
  208. }
  209. uint32_t ble_gatts_service_changed_req_dec(uint8_t const * const p_buf,
  210. uint32_t packet_len,
  211. uint16_t * p_conn_handle,
  212. uint16_t * p_start_handle,
  213. uint16_t * p_end_handle)
  214. {
  215. SER_REQ_DEC_BEGIN(SD_BLE_GATTS_SERVICE_CHANGED);
  216. SER_PULL_uint16(p_conn_handle);
  217. SER_PULL_uint16(p_start_handle);
  218. SER_PULL_uint16(p_end_handle);
  219. SER_REQ_DEC_END;
  220. }
  221. uint32_t ble_gatts_service_changed_rsp_enc(uint32_t return_code,
  222. uint8_t * const p_buf,
  223. uint32_t * const p_buf_len)
  224. {
  225. SER_RSP_ENC_RESULT_ONLY(SD_BLE_GATTS_SERVICE_CHANGED);
  226. }
  227. uint32_t ble_gatts_sys_attr_get_req_dec(uint8_t const * const p_buf,
  228. uint32_t packet_len,
  229. uint16_t * const p_conn_handle,
  230. uint8_t * * const pp_sys_attr_data,
  231. uint16_t * * const pp_sys_attr_data_len,
  232. uint32_t * const p_flags)
  233. {
  234. SER_REQ_DEC_BEGIN(SD_BLE_GATTS_SYS_ATTR_GET);
  235. SER_PULL_uint16(p_conn_handle);
  236. SER_PULL_COND(pp_sys_attr_data_len, uint16_t_dec);
  237. SER_PULL_COND(pp_sys_attr_data, NULL);
  238. SER_PULL_uint32(p_flags);
  239. SER_REQ_DEC_END;
  240. }
  241. uint32_t ble_gatts_sys_attr_get_rsp_enc(uint32_t return_code,
  242. uint8_t * const p_buf,
  243. uint32_t * const p_buf_len,
  244. uint8_t const * const p_sys_attr_data,
  245. uint16_t const * const p_sys_attr_data_len)
  246. {
  247. SER_RSP_ENC_BEGIN(SD_BLE_GATTS_SYS_ATTR_GET);
  248. SER_PUSH_COND(p_sys_attr_data_len, uint16_t_enc);
  249. if (p_sys_attr_data_len)
  250. {
  251. SER_PUSH_buf(p_sys_attr_data, *p_sys_attr_data_len);
  252. }
  253. SER_RSP_ENC_END;
  254. }
  255. uint32_t ble_gatts_sys_attr_set_req_dec(uint8_t const * const p_buf,
  256. uint32_t packet_len,
  257. uint16_t * const p_conn_handle,
  258. uint8_t * * const pp_sys_attr_data,
  259. uint16_t * const p_sys_attr_data_len,
  260. uint32_t * const p_flags)
  261. {
  262. SER_REQ_DEC_BEGIN(SD_BLE_GATTS_SYS_ATTR_SET);
  263. SER_PULL_uint16(p_conn_handle);
  264. SER_PULL_len16data(pp_sys_attr_data, p_sys_attr_data_len);
  265. SER_PULL_uint32(p_flags);
  266. SER_REQ_DEC_END;
  267. }
  268. uint32_t ble_gatts_sys_attr_set_rsp_enc(uint32_t return_code,
  269. uint8_t * const p_buf,
  270. uint32_t * const p_buf_len)
  271. {
  272. SER_RSP_ENC_RESULT_ONLY(SD_BLE_GATTS_SYS_ATTR_SET);
  273. }
  274. uint32_t ble_gatts_value_get_req_dec(uint8_t const * const p_buf,
  275. uint16_t packet_len,
  276. uint16_t * const p_conn_handle,
  277. uint16_t * const p_handle,
  278. ble_gatts_value_t * * const pp_value)
  279. {
  280. SER_REQ_DEC_BEGIN(SD_BLE_GATTS_VALUE_GET);
  281. SER_PULL_uint16(p_conn_handle);
  282. SER_PULL_uint16(p_handle);
  283. //Special case: skip the data.
  284. SER_PULL_COND(pp_value, NULL);
  285. if (*pp_value)
  286. {
  287. SER_PULL_uint16(&(*pp_value)->offset);
  288. SER_PULL_uint16(&(*pp_value)->len);
  289. SER_PULL_COND(&(*pp_value)->p_value, NULL);
  290. }
  291. SER_REQ_DEC_END;
  292. }
  293. uint32_t ble_gatts_value_get_rsp_enc(uint32_t return_code,
  294. uint8_t * const p_buf,
  295. uint32_t * const p_buf_len,
  296. ble_gatts_value_t * const p_value)
  297. {
  298. SER_RSP_ENC_BEGIN(SD_BLE_GATTS_VALUE_GET);
  299. SER_PUSH_COND(p_value, ble_gatts_value_t_enc);
  300. SER_RSP_ENC_END;
  301. }
  302. uint32_t ble_gatts_value_set_req_dec(uint8_t const * const p_buf,
  303. uint16_t packet_len,
  304. uint16_t * p_conn_handle,
  305. uint16_t * p_handle,
  306. ble_gatts_value_t * * const pp_value)
  307. {
  308. SER_REQ_DEC_BEGIN(SD_BLE_GATTS_VALUE_SET);
  309. SER_PULL_uint16(p_conn_handle);
  310. SER_PULL_uint16(p_handle);
  311. SER_PULL_COND(pp_value, ble_gatts_value_t_dec);
  312. SER_REQ_DEC_END;
  313. }
  314. uint32_t ble_gatts_value_set_rsp_enc(uint32_t return_code,
  315. uint8_t * const p_buf,
  316. uint32_t * const p_buf_len,
  317. ble_gatts_value_t * p_value)
  318. {
  319. SER_RSP_ENC_BEGIN(SD_BLE_GATTS_VALUE_SET);
  320. SER_PUSH_COND(p_value, ble_gatts_value_t_enc);
  321. SER_RSP_ENC_END;
  322. }
  323. uint32_t ble_gatts_exchange_mtu_reply_req_dec(uint8_t const * const p_buf,
  324. uint16_t packet_len,
  325. uint16_t * const p_conn_handle,
  326. uint16_t * const p_server_rx_mtu)
  327. {
  328. SER_REQ_DEC_BEGIN(SD_BLE_GATTS_EXCHANGE_MTU_REPLY);
  329. SER_PULL_uint16(p_conn_handle);
  330. SER_PULL_uint16(p_server_rx_mtu);
  331. SER_REQ_DEC_END;
  332. }
  333. uint32_t ble_gatts_exchange_mtu_reply_rsp_enc(uint32_t return_code,
  334. uint8_t * const p_buf,
  335. uint32_t * const p_buf_len)
  336. {
  337. SER_RSP_ENC_RESULT_ONLY(SD_BLE_GATTS_EXCHANGE_MTU_REPLY);
  338. }