ble_gatts_app.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /**
  2. * Copyright (c) 2013 - 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_app.h"
  41. #include <stdlib.h>
  42. #include <string.h>
  43. #include "ble_serialization.h"
  44. #include "ble_gatts_struct_serialization.h"
  45. #include "ble_struct_serialization.h"
  46. #include "cond_field_serialization.h"
  47. #include "app_util.h"
  48. uint32_t ble_gatts_attr_get_req_enc(uint16_t handle,
  49. ble_uuid_t * p_uuid,
  50. ble_gatts_attr_md_t * p_md,
  51. uint8_t * const p_buf,
  52. uint32_t * p_buf_len)
  53. {
  54. SER_REQ_ENC_BEGIN(SD_BLE_GATTS_ATTR_GET);
  55. SER_PUSH_uint16(&handle);
  56. SER_PUSH_COND(p_uuid, NULL);
  57. SER_PUSH_COND(p_md, NULL);
  58. SER_REQ_ENC_END;
  59. }
  60. uint32_t ble_gatts_attr_get_rsp_dec(uint8_t const * const p_buf,
  61. uint32_t packet_len,
  62. ble_uuid_t ** pp_uuid,
  63. ble_gatts_attr_md_t ** pp_md,
  64. uint32_t * const p_result_code)
  65. {
  66. SER_RSP_DEC_BEGIN(SD_BLE_GATTS_ATTR_GET);
  67. SER_PULL_COND(pp_uuid, ble_uuid_t_dec);
  68. SER_PULL_COND(pp_md, ble_gatts_attr_md_t_dec);
  69. SER_RSP_DEC_END;
  70. }
  71. uint32_t ble_gatts_characteristic_add_req_enc(
  72. uint16_t service_handle,
  73. ble_gatts_char_md_t const * const p_char_md,
  74. ble_gatts_attr_t const * const p_attr_char_value,
  75. ble_gatts_char_handles_t const * const p_handles,
  76. uint8_t * const p_buf,
  77. uint32_t * const p_buf_len)
  78. {
  79. SER_REQ_ENC_BEGIN(SD_BLE_GATTS_CHARACTERISTIC_ADD);
  80. SER_PUSH_uint16(&service_handle);
  81. SER_PUSH_COND(p_char_md, ble_gatts_char_md_t_enc);
  82. SER_PUSH_COND(p_attr_char_value, ble_gatts_attr_t_enc);
  83. SER_PUSH_COND(p_handles, NULL);
  84. SER_REQ_ENC_END;
  85. }
  86. uint32_t ble_gatts_characteristic_add_rsp_dec(uint8_t const * const p_buf,
  87. uint32_t packet_len,
  88. uint16_t * * const pp_handles,
  89. uint32_t * const p_result_code)
  90. {
  91. SER_RSP_DEC_BEGIN(SD_BLE_GATTS_CHARACTERISTIC_ADD);
  92. SER_PULL_COND(pp_handles, ble_gatts_char_handles_t_dec);
  93. SER_RSP_DEC_END;
  94. }
  95. uint32_t ble_gatts_descriptor_add_req_enc(uint16_t char_handle,
  96. ble_gatts_attr_t const * const p_attr,
  97. uint16_t * const p_handle,
  98. uint8_t * const p_buf,
  99. uint32_t * const p_buf_len)
  100. {
  101. SER_REQ_ENC_BEGIN(SD_BLE_GATTS_DESCRIPTOR_ADD);
  102. SER_PUSH_uint16(&char_handle);
  103. SER_PUSH_COND(p_attr, ble_gatts_attr_t_enc);
  104. SER_PUSH_COND(p_handle, NULL);
  105. SER_REQ_ENC_END;
  106. }
  107. uint32_t ble_gatts_descriptor_add_rsp_dec(uint8_t const * const p_buf,
  108. uint32_t packet_len,
  109. uint16_t * const p_handle,
  110. uint32_t * const p_result_code)
  111. {
  112. SER_RSP_DEC_BEGIN(SD_BLE_GATTS_DESCRIPTOR_ADD);
  113. SER_PULL_COND(&p_handle, uint16_t_dec);
  114. SER_RSP_DEC_END;
  115. }
  116. uint32_t ble_gatts_hvx_req_enc(uint16_t conn_handle,
  117. ble_gatts_hvx_params_t const * const p_hvx_params,
  118. uint8_t * const p_buf,
  119. uint32_t * const p_buf_len)
  120. {
  121. SER_REQ_ENC_BEGIN(SD_BLE_GATTS_HVX);
  122. SER_PUSH_uint16(&conn_handle);
  123. SER_PUSH_COND(p_hvx_params, ble_gatts_hvx_params_t_enc);
  124. SER_REQ_ENC_END;
  125. }
  126. uint32_t ble_gatts_hvx_rsp_dec(uint8_t const * const p_buf,
  127. uint32_t packet_len,
  128. uint32_t * const p_result_code,
  129. uint16_t * * const pp_bytes_written)
  130. {
  131. SER_RSP_DEC_BEGIN(SD_BLE_GATTS_HVX);
  132. SER_PULL_COND(pp_bytes_written, uint16_t_dec);
  133. SER_RSP_DEC_END;
  134. }
  135. uint32_t ble_gatts_include_add_req_enc(uint16_t service_handle,
  136. uint16_t inc_srvc_handle,
  137. uint16_t * const p_include_handle,
  138. uint8_t * const p_buf,
  139. uint32_t * const p_buf_len)
  140. {
  141. SER_REQ_ENC_BEGIN(SD_BLE_GATTS_INCLUDE_ADD);
  142. SER_PUSH_uint16(&service_handle);
  143. SER_PUSH_uint16(&inc_srvc_handle);
  144. SER_PUSH_COND(p_include_handle, NULL);
  145. SER_REQ_ENC_END;
  146. }
  147. uint32_t ble_gatts_include_add_rsp_dec(uint8_t const * const p_buf,
  148. uint32_t packet_len,
  149. uint16_t * const p_include_handle,
  150. uint32_t * const p_result_code)
  151. {
  152. SER_RSP_DEC_BEGIN(SD_BLE_GATTS_INCLUDE_ADD);
  153. SER_PULL_COND(&p_include_handle, uint16_t_dec);
  154. SER_RSP_DEC_END;
  155. }
  156. uint32_t ble_gatts_initial_user_handle_get_req_enc(uint16_t * p_handle,
  157. uint8_t * const p_buf,
  158. uint32_t * p_buf_len)
  159. {
  160. SER_REQ_ENC_BEGIN(SD_BLE_GATTS_INITIAL_USER_HANDLE_GET);
  161. SER_PUSH_COND(p_handle, NULL);
  162. SER_REQ_ENC_END;
  163. }
  164. uint32_t ble_gatts_initial_user_handle_get_rsp_dec(uint8_t const * const p_buf,
  165. uint32_t packet_len,
  166. uint16_t ** pp_handle,
  167. uint32_t * const p_result_code)
  168. {
  169. SER_RSP_DEC_BEGIN(SD_BLE_GATTS_INITIAL_USER_HANDLE_GET);
  170. SER_PULL_COND(pp_handle, uint16_t_dec);
  171. SER_RSP_DEC_END;
  172. }
  173. uint32_t ble_gatts_rw_authorize_reply_req_enc(uint16_t conn_handle,
  174. ble_gatts_rw_authorize_reply_params_t const * const p_reply_params,
  175. uint8_t * const p_buf,
  176. uint32_t * const p_buf_len)
  177. {
  178. SER_REQ_ENC_BEGIN(SD_BLE_GATTS_RW_AUTHORIZE_REPLY);
  179. SER_PUSH_uint16(&conn_handle);
  180. SER_PUSH_COND(p_reply_params, ble_gatts_rw_authorize_reply_params_t_enc);
  181. SER_REQ_ENC_END;
  182. }
  183. uint32_t ble_gatts_rw_authorize_reply_rsp_dec(uint8_t const * const p_buf,
  184. uint32_t packet_len,
  185. uint32_t * const p_result_code)
  186. {
  187. SER_RSP_DEC_RESULT_ONLY(SD_BLE_GATTS_RW_AUTHORIZE_REPLY);
  188. }
  189. uint32_t ble_gatts_service_add_req_enc(uint8_t type,
  190. ble_uuid_t const * const p_uuid,
  191. uint16_t const * const p_conn_handle,
  192. uint8_t * const p_buf,
  193. uint32_t * const p_buf_len)
  194. {
  195. SER_REQ_ENC_BEGIN(SD_BLE_GATTS_SERVICE_ADD);
  196. SER_PUSH_uint8(&type);
  197. SER_PUSH_COND(p_uuid, ble_uuid_t_enc);
  198. SER_PUSH_COND(p_conn_handle, NULL);
  199. SER_REQ_ENC_END;
  200. }
  201. uint32_t ble_gatts_service_add_rsp_dec(uint8_t const * const p_buf,
  202. uint32_t packet_len,
  203. uint16_t * const p_conn_handle,
  204. uint32_t * const p_result_code)
  205. {
  206. SER_RSP_DEC_BEGIN(SD_BLE_GATTS_SERVICE_ADD);
  207. SER_PULL_COND(&p_conn_handle, uint16_t_dec);
  208. SER_RSP_DEC_END;
  209. }
  210. uint32_t ble_gatts_service_changed_req_enc(uint16_t conn_handle,
  211. uint16_t start_handle,
  212. uint16_t end_handle,
  213. uint8_t * const p_buf,
  214. uint32_t * const p_buf_len)
  215. {
  216. SER_REQ_ENC_BEGIN(SD_BLE_GATTS_SERVICE_CHANGED);
  217. SER_PUSH_uint16(&conn_handle);
  218. SER_PUSH_uint16(&start_handle);
  219. SER_PUSH_uint16(&end_handle);
  220. SER_REQ_ENC_END;
  221. }
  222. uint32_t ble_gatts_service_changed_rsp_dec(uint8_t const * const p_buf,
  223. uint32_t packet_len,
  224. uint32_t * const p_result_code)
  225. {
  226. SER_RSP_DEC_RESULT_ONLY(SD_BLE_GATTS_SERVICE_CHANGED);
  227. }
  228. uint32_t ble_gatts_sys_attr_get_req_enc(uint16_t conn_handle,
  229. uint8_t const * const p_sys_attr_data,
  230. uint16_t const * const p_sys_attr_data_len,
  231. uint32_t flags,
  232. uint8_t * const p_buf,
  233. uint32_t * p_buf_len)
  234. {
  235. SER_REQ_ENC_BEGIN(SD_BLE_GATTS_SYS_ATTR_GET);
  236. SER_PUSH_uint16(&conn_handle);
  237. SER_PUSH_COND(p_sys_attr_data_len, uint16_t_enc);
  238. SER_PUSH_COND(p_sys_attr_data, NULL);
  239. SER_PUSH_uint32(&flags);
  240. SER_REQ_ENC_END;
  241. }
  242. uint32_t ble_gatts_sys_attr_get_rsp_dec(uint8_t const * const p_buf,
  243. uint32_t packet_len,
  244. uint8_t * * const pp_sys_attr_data,
  245. uint16_t * * const pp_sys_attr_data_len,
  246. uint32_t * const p_result_code)
  247. {
  248. SER_RSP_DEC_BEGIN(SD_BLE_GATTS_SYS_ATTR_GET);
  249. SER_PULL_COND(pp_sys_attr_data_len, uint16_t_dec);
  250. if (*pp_sys_attr_data_len)
  251. {
  252. SER_PULL_buf(pp_sys_attr_data, **pp_sys_attr_data_len, **pp_sys_attr_data_len);
  253. }
  254. SER_RSP_DEC_END;
  255. }
  256. uint32_t ble_gatts_sys_attr_set_req_enc(uint16_t conn_handle,
  257. uint8_t const * const p_sys_attr_data,
  258. uint16_t sys_attr_data_len,
  259. uint32_t flags,
  260. uint8_t * const p_buf,
  261. uint32_t * const p_buf_len)
  262. {
  263. SER_REQ_ENC_BEGIN(SD_BLE_GATTS_SYS_ATTR_SET);
  264. SER_PUSH_uint16(&conn_handle);
  265. SER_PUSH_len16data(p_sys_attr_data, sys_attr_data_len);
  266. SER_PUSH_uint32(&flags);
  267. SER_REQ_ENC_END;
  268. }
  269. uint32_t ble_gatts_sys_attr_set_rsp_dec(uint8_t const * const p_buf,
  270. uint32_t packet_len,
  271. uint32_t * const p_result_code)
  272. {
  273. return ser_ble_cmd_rsp_dec(p_buf, packet_len, SD_BLE_GATTS_SYS_ATTR_SET, p_result_code);
  274. }
  275. uint32_t ble_gatts_value_get_req_enc(uint16_t conn_handle,
  276. uint16_t handle,
  277. ble_gatts_value_t const * const p_value,
  278. uint8_t * const p_buf,
  279. uint32_t * const p_buf_len)
  280. {
  281. SER_REQ_ENC_BEGIN(SD_BLE_GATTS_VALUE_GET);
  282. SER_PUSH_uint16(&conn_handle);
  283. SER_PUSH_uint16(&handle);
  284. //Special case: skip the data.
  285. SER_PUSH_COND(p_value, NULL);
  286. if (p_value)
  287. {
  288. SER_PUSH_uint16(&p_value->offset);
  289. SER_PUSH_uint16(&p_value->len);
  290. SER_PUSH_COND(p_value->p_value, NULL);
  291. }
  292. SER_REQ_ENC_END;
  293. }
  294. uint32_t ble_gatts_value_get_rsp_dec(uint8_t const * const p_buf,
  295. uint32_t packet_len,
  296. ble_gatts_value_t * const p_value,
  297. uint32_t * const p_result_code)
  298. {
  299. SER_RSP_DEC_BEGIN(SD_BLE_GATTS_VALUE_GET);
  300. SER_PULL_COND(&p_value, ble_gatts_value_t_dec);
  301. SER_RSP_DEC_END;
  302. }
  303. uint32_t ble_gatts_value_set_req_enc(uint16_t conn_handle,
  304. uint16_t handle,
  305. ble_gatts_value_t * p_value,
  306. uint8_t * const p_buf,
  307. uint32_t * const p_buf_len)
  308. {
  309. SER_REQ_ENC_BEGIN(SD_BLE_GATTS_VALUE_SET);
  310. SER_PUSH_uint16(&conn_handle);
  311. SER_PUSH_uint16(&handle);
  312. SER_PUSH_COND(p_value, ble_gatts_value_t_enc);
  313. SER_REQ_ENC_END;
  314. }
  315. uint32_t ble_gatts_value_set_rsp_dec(uint8_t const * const p_buf,
  316. uint32_t packet_len,
  317. ble_gatts_value_t * const p_value,
  318. uint32_t * const p_result_code)
  319. {
  320. SER_RSP_DEC_BEGIN(SD_BLE_GATTS_VALUE_SET);
  321. SER_PULL_COND(&p_value, ble_gatts_value_t_dec);
  322. SER_RSP_DEC_END;
  323. }
  324. uint32_t ble_gatts_exchange_mtu_reply_req_enc(uint16_t conn_handle,
  325. uint16_t server_rx_mtu,
  326. uint8_t * const p_buf,
  327. uint32_t * const p_buf_len)
  328. {
  329. SER_REQ_ENC_BEGIN(SD_BLE_GATTS_EXCHANGE_MTU_REPLY);
  330. SER_PUSH_uint16(&conn_handle);
  331. SER_PUSH_uint16(&server_rx_mtu);
  332. SER_REQ_ENC_END;
  333. }
  334. uint32_t ble_gatts_exchange_mtu_reply_rsp_dec(uint8_t const * const p_buf,
  335. uint32_t packet_len,
  336. uint32_t * const p_result_code)
  337. {
  338. SER_RSP_DEC_RESULT_ONLY(SD_BLE_GATTS_EXCHANGE_MTU_REPLY);
  339. }