conn_mw_ble_gattc.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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_gattc_conn.h"
  41. #include "conn_mw_ble_gattc.h"
  42. #include "ble_serialization.h"
  43. #if defined(BLE_GATT_MTU_SIZE_DEFAULT) && !defined(GATT_MTU_SIZE_DEFAULT)
  44. #define GATT_MTU_SIZE_DEFAULT BLE_GATT_MTU_SIZE_DEFAULT
  45. #endif
  46. #if defined(BLE_GATT_ATT_MTU_DEFAULT) && !defined(GATT_MTU_SIZE_DEFAULT)
  47. #define GATT_MTU_SIZE_DEFAULT BLE_GATT_ATT_MTU_DEFAULT
  48. #endif
  49. #define BLE_GATTC_WRITE_P_VALUE_LEN_MAX (247 - 3)
  50. /** See Bluetooth 4.0 spec: 3.4.4.7. */
  51. #define BLE_GATTC_HANDLE_COUNT_LEN_MAX ((GATT_MTU_SIZE_DEFAULT - 1) / 2)
  52. uint32_t conn_mw_ble_gattc_primary_services_discover(uint8_t const * const p_rx_buf,
  53. uint32_t rx_buf_len,
  54. uint8_t * const p_tx_buf,
  55. uint32_t * const p_tx_buf_len)
  56. {
  57. SER_ASSERT_NOT_NULL(p_rx_buf);
  58. SER_ASSERT_NOT_NULL(p_tx_buf);
  59. SER_ASSERT_NOT_NULL(p_tx_buf_len);
  60. uint16_t conn_handle;
  61. uint16_t start_handle;
  62. ble_uuid_t srvc_uuid;
  63. ble_uuid_t * p_srvc_uuid = &srvc_uuid;
  64. uint32_t err_code = NRF_SUCCESS;
  65. uint32_t sd_err_code;
  66. err_code = ble_gattc_primary_services_discover_req_dec(p_rx_buf,
  67. rx_buf_len,
  68. &conn_handle,
  69. &start_handle,
  70. &p_srvc_uuid);
  71. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  72. sd_err_code = sd_ble_gattc_primary_services_discover(conn_handle, start_handle, p_srvc_uuid);
  73. err_code = ble_gattc_primary_services_discover_rsp_enc(sd_err_code, p_tx_buf, p_tx_buf_len);
  74. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  75. return err_code;
  76. }
  77. uint32_t conn_mw_ble_gattc_relationships_discover(uint8_t const * const p_rx_buf,
  78. uint32_t rx_buf_len,
  79. uint8_t * const p_tx_buf,
  80. uint32_t * const p_tx_buf_len)
  81. {
  82. SER_ASSERT_NOT_NULL(p_rx_buf);
  83. SER_ASSERT_NOT_NULL(p_tx_buf);
  84. SER_ASSERT_NOT_NULL(p_tx_buf_len);
  85. uint16_t conn_handle;
  86. ble_gattc_handle_range_t handle_range;
  87. ble_gattc_handle_range_t * p_handle_range = &handle_range;
  88. uint32_t err_code = NRF_SUCCESS;
  89. uint32_t sd_err_code;
  90. err_code = ble_gattc_relationships_discover_req_dec(p_rx_buf, rx_buf_len,
  91. &conn_handle, &p_handle_range);
  92. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  93. sd_err_code = sd_ble_gattc_relationships_discover(conn_handle, p_handle_range);
  94. err_code = ble_gattc_relationships_discover_rsp_enc(sd_err_code, p_tx_buf, p_tx_buf_len);
  95. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  96. return err_code;
  97. }
  98. uint32_t conn_mw_ble_gattc_characteristics_discover(uint8_t const * const p_rx_buf,
  99. uint32_t rx_buf_len,
  100. uint8_t * const p_tx_buf,
  101. uint32_t * const p_tx_buf_len)
  102. {
  103. SER_ASSERT_NOT_NULL(p_rx_buf);
  104. SER_ASSERT_NOT_NULL(p_tx_buf);
  105. SER_ASSERT_NOT_NULL(p_tx_buf_len);
  106. uint16_t conn_handle;
  107. ble_gattc_handle_range_t handle_range;
  108. ble_gattc_handle_range_t * p_handle_range = &handle_range;
  109. uint32_t err_code = NRF_SUCCESS;
  110. uint32_t sd_err_code;
  111. err_code = ble_gattc_characteristics_discover_req_dec(p_rx_buf, rx_buf_len,
  112. &conn_handle, &p_handle_range);
  113. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  114. sd_err_code = sd_ble_gattc_characteristics_discover(conn_handle, p_handle_range);
  115. err_code = ble_gattc_characteristics_discover_rsp_enc(sd_err_code, p_tx_buf, p_tx_buf_len);
  116. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  117. return err_code;
  118. }
  119. uint32_t conn_mw_ble_gattc_descriptors_discover(uint8_t const * const p_rx_buf,
  120. uint32_t rx_buf_len,
  121. uint8_t * const p_tx_buf,
  122. uint32_t * const p_tx_buf_len)
  123. {
  124. SER_ASSERT_NOT_NULL(p_rx_buf);
  125. SER_ASSERT_NOT_NULL(p_tx_buf);
  126. SER_ASSERT_NOT_NULL(p_tx_buf_len);
  127. uint16_t conn_handle;
  128. ble_gattc_handle_range_t handle_range;
  129. ble_gattc_handle_range_t * p_handle_range = &handle_range;
  130. uint32_t err_code = NRF_SUCCESS;
  131. uint32_t sd_err_code;
  132. err_code = ble_gattc_descriptors_discover_req_dec(p_rx_buf, rx_buf_len,
  133. &conn_handle, &p_handle_range);
  134. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  135. sd_err_code = sd_ble_gattc_descriptors_discover(conn_handle, p_handle_range);
  136. err_code = ble_gattc_descriptors_discover_rsp_enc(sd_err_code, p_tx_buf, p_tx_buf_len);
  137. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  138. return err_code;
  139. }
  140. uint32_t conn_mw_ble_gattc_char_value_by_uuid_read(uint8_t const * const p_rx_buf,
  141. uint32_t rx_buf_len,
  142. uint8_t * const p_tx_buf,
  143. uint32_t * const p_tx_buf_len)
  144. {
  145. SER_ASSERT_NOT_NULL(p_rx_buf);
  146. SER_ASSERT_NOT_NULL(p_tx_buf);
  147. SER_ASSERT_NOT_NULL(p_tx_buf_len);
  148. uint16_t conn_handle;
  149. ble_uuid_t uuid = {0};
  150. ble_uuid_t * p_uuid = &uuid;
  151. ble_gattc_handle_range_t handle_range;
  152. ble_gattc_handle_range_t * p_handle_range = &handle_range;
  153. uint32_t err_code = NRF_SUCCESS;
  154. uint32_t sd_err_code;
  155. err_code = ble_gattc_char_value_by_uuid_read_req_dec(p_rx_buf, rx_buf_len,
  156. &conn_handle, &p_uuid, &p_handle_range);
  157. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  158. sd_err_code = sd_ble_gattc_char_value_by_uuid_read(conn_handle, p_uuid, p_handle_range);
  159. err_code = ble_gattc_char_value_by_uuid_read_rsp_enc(sd_err_code, p_tx_buf, p_tx_buf_len);
  160. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  161. return err_code;
  162. }
  163. uint32_t conn_mw_ble_gattc_read(uint8_t const * const p_rx_buf,
  164. uint32_t rx_buf_len,
  165. uint8_t * const p_tx_buf,
  166. uint32_t * const p_tx_buf_len)
  167. {
  168. SER_ASSERT_NOT_NULL(p_rx_buf);
  169. SER_ASSERT_NOT_NULL(p_tx_buf);
  170. SER_ASSERT_NOT_NULL(p_tx_buf_len);
  171. uint16_t conn_handle;
  172. uint16_t * p_conn_handle = &conn_handle;
  173. uint16_t handle;
  174. uint16_t * p_handle = &handle;
  175. uint16_t offset;
  176. uint16_t * p_offset = &offset;
  177. uint32_t err_code = NRF_SUCCESS;
  178. uint32_t sd_err_code;
  179. err_code = ble_gattc_read_req_dec(p_rx_buf, rx_buf_len, p_conn_handle, p_handle, p_offset);
  180. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  181. sd_err_code = sd_ble_gattc_read(conn_handle, handle, offset);
  182. err_code = ble_gattc_read_rsp_enc(sd_err_code, p_tx_buf, p_tx_buf_len);
  183. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  184. return err_code;
  185. }
  186. uint32_t conn_mw_ble_gattc_char_values_read(uint8_t const * const p_rx_buf,
  187. uint32_t rx_buf_len,
  188. uint8_t * const p_tx_buf,
  189. uint32_t * const p_tx_buf_len)
  190. {
  191. SER_ASSERT_NOT_NULL(p_rx_buf);
  192. SER_ASSERT_NOT_NULL(p_tx_buf);
  193. SER_ASSERT_NOT_NULL(p_tx_buf_len);
  194. uint16_t conn_handle;
  195. uint16_t * p_conn_handle = &conn_handle;
  196. uint16_t handles[BLE_GATTC_HANDLE_COUNT_LEN_MAX];
  197. uint16_t * p_handles = handles;
  198. uint16_t handle_count = BLE_GATTC_HANDLE_COUNT_LEN_MAX;
  199. uint16_t * p_handle_count = &handle_count;
  200. uint32_t err_code = NRF_SUCCESS;
  201. uint32_t sd_err_code;
  202. err_code = ble_gattc_char_values_read_req_dec(p_rx_buf,
  203. rx_buf_len,
  204. p_conn_handle,
  205. &p_handles,
  206. p_handle_count);
  207. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  208. sd_err_code = sd_ble_gattc_char_values_read(conn_handle, p_handles, handle_count);
  209. err_code = ble_gattc_char_values_read_rsp_enc(sd_err_code, p_tx_buf, p_tx_buf_len);
  210. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  211. return err_code;
  212. }
  213. uint32_t conn_mw_ble_gattc_write(uint8_t const * const p_rx_buf,
  214. uint32_t rx_buf_len,
  215. uint8_t * const p_tx_buf,
  216. uint32_t * const p_tx_buf_len)
  217. {
  218. SER_ASSERT_NOT_NULL(p_rx_buf);
  219. SER_ASSERT_NOT_NULL(p_tx_buf);
  220. SER_ASSERT_NOT_NULL(p_tx_buf_len);
  221. uint16_t conn_handle;
  222. uint16_t * p_conn_handle = &conn_handle;
  223. uint8_t value[BLE_GATTC_WRITE_P_VALUE_LEN_MAX];
  224. ble_gattc_write_params_t write_params = {0};
  225. ble_gattc_write_params_t * p_write_params = &write_params;
  226. p_write_params->len = BLE_GATTC_WRITE_P_VALUE_LEN_MAX;
  227. p_write_params->p_value = value;
  228. uint32_t err_code = NRF_SUCCESS;
  229. uint32_t sd_err_code;
  230. err_code = ble_gattc_write_req_dec(p_rx_buf, rx_buf_len, p_conn_handle, &p_write_params);
  231. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  232. sd_err_code = sd_ble_gattc_write(conn_handle, p_write_params);
  233. err_code = ble_gattc_write_rsp_enc(sd_err_code, p_tx_buf, p_tx_buf_len);
  234. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  235. return err_code;
  236. }
  237. uint32_t conn_mw_ble_gattc_hv_confirm(uint8_t const * const p_rx_buf,
  238. uint32_t rx_buf_len,
  239. uint8_t * const p_tx_buf,
  240. uint32_t * const p_tx_buf_len)
  241. {
  242. SER_ASSERT_NOT_NULL(p_rx_buf);
  243. SER_ASSERT_NOT_NULL(p_tx_buf);
  244. SER_ASSERT_NOT_NULL(p_tx_buf_len);
  245. uint32_t err_code = NRF_SUCCESS;
  246. uint32_t sd_err_code;
  247. uint16_t conn_handle;
  248. uint16_t * p_conn_handle = &conn_handle;
  249. uint16_t handle;
  250. uint16_t * p_handle = &handle;
  251. err_code = ble_gattc_hv_confirm_req_dec(p_rx_buf, rx_buf_len, p_conn_handle, p_handle);
  252. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  253. sd_err_code = sd_ble_gattc_hv_confirm(conn_handle, handle);
  254. err_code = ble_gattc_hv_confirm_rsp_enc(sd_err_code, p_tx_buf, p_tx_buf_len);
  255. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  256. return err_code;
  257. }
  258. uint32_t conn_mw_ble_gattc_attr_info_discover(uint8_t const * const p_rx_buf,
  259. uint32_t rx_buf_len,
  260. uint8_t * const p_tx_buf,
  261. uint32_t * const p_tx_buf_len)
  262. {
  263. SER_ASSERT_NOT_NULL(p_rx_buf);
  264. SER_ASSERT_NOT_NULL(p_tx_buf);
  265. SER_ASSERT_NOT_NULL(p_tx_buf_len);
  266. uint16_t conn_handle;
  267. uint16_t * p_conn_handle = &conn_handle;
  268. ble_gattc_handle_range_t range = {0};
  269. ble_gattc_handle_range_t * p_range = ⦥
  270. uint32_t err_code = NRF_SUCCESS;
  271. uint32_t sd_err_code;
  272. err_code = ble_gattc_attr_info_discover_req_dec(p_rx_buf, rx_buf_len, p_conn_handle, &p_range);
  273. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  274. sd_err_code = sd_ble_gattc_attr_info_discover(conn_handle, p_range);
  275. err_code = ble_gattc_attr_info_discover_rsp_enc(sd_err_code, p_tx_buf, p_tx_buf_len);
  276. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  277. return err_code;
  278. }
  279. uint32_t conn_mw_ble_gattc_exchange_mtu_request(uint8_t const * const p_rx_buf,
  280. uint32_t rx_buf_len,
  281. uint8_t * const p_tx_buf,
  282. uint32_t * const p_tx_buf_len)
  283. {
  284. SER_ASSERT_NOT_NULL(p_rx_buf);
  285. SER_ASSERT_NOT_NULL(p_tx_buf);
  286. SER_ASSERT_NOT_NULL(p_tx_buf_len);
  287. uint16_t conn_handle;
  288. uint16_t client_rx_mtu;
  289. uint32_t err_code = NRF_SUCCESS;
  290. uint32_t sd_err_code;
  291. err_code = ble_gattc_exchange_mtu_request_req_dec(p_rx_buf, rx_buf_len, &conn_handle, &client_rx_mtu);
  292. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  293. sd_err_code = sd_ble_gattc_exchange_mtu_request(conn_handle, client_rx_mtu);
  294. err_code = ble_gattc_exchange_mtu_request_rsp_enc(sd_err_code, p_tx_buf, p_tx_buf_len);
  295. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  296. return err_code;
  297. }