app_mw_ble_gattc.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  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 <stdint.h>
  41. #include "ble_gattc.h"
  42. #include "ble_gattc_app.h"
  43. #include "ble_serialization.h"
  44. #include "ser_sd_transport.h"
  45. #include "app_error.h"
  46. static void tx_buf_alloc(uint8_t * * p_data, uint16_t * p_len)
  47. {
  48. uint32_t err_code;
  49. do
  50. {
  51. err_code = ser_sd_transport_tx_alloc(p_data, p_len);
  52. }
  53. while (err_code != NRF_SUCCESS);
  54. *p_data[0] = SER_PKT_TYPE_CMD;
  55. *p_len -= 1;
  56. }
  57. /**@brief Command response callback function for @ref sd_ble_gattc_primary_services_discover BLE command.
  58. *
  59. * Callback for decoding the command response return code.
  60. *
  61. * @param[in] p_buffer Pointer to begin of command response buffer.
  62. * @param[in] length Length of data in bytes.
  63. *
  64. * @return Decoded command response return code.
  65. */
  66. static uint32_t gattc_primary_services_discover_rsp_dec(const uint8_t * p_buffer, uint16_t length)
  67. {
  68. uint32_t result_code;
  69. const uint32_t err_code = ble_gattc_primary_services_discover_rsp_dec(p_buffer,
  70. length,
  71. &result_code);
  72. APP_ERROR_CHECK(err_code);
  73. return result_code;
  74. }
  75. #ifndef _sd_ble_gattc_primary_services_discover
  76. #define _sd_ble_gattc_primary_services_discover sd_ble_gattc_primary_services_discover
  77. #endif
  78. uint32_t _sd_ble_gattc_primary_services_discover(uint16_t conn_handle,
  79. uint16_t start_handle,
  80. ble_uuid_t const * const p_srvc_uuid)
  81. {
  82. uint8_t * p_buffer;
  83. uint32_t buffer_length = 0;
  84. tx_buf_alloc(&p_buffer, (uint16_t *)&buffer_length);
  85. const uint32_t err_code = ble_gattc_primary_services_discover_req_enc(conn_handle,
  86. start_handle,
  87. p_srvc_uuid,
  88. &(p_buffer[1]),
  89. &buffer_length);
  90. APP_ERROR_CHECK(err_code);
  91. //@note: Increment buffer length as internally managed packet type field must be included.
  92. return ser_sd_transport_cmd_write(p_buffer,
  93. (++buffer_length),
  94. gattc_primary_services_discover_rsp_dec);
  95. }
  96. /**@brief Command response callback function for @ref sd_ble_gattc_relationships_discover BLE command.
  97. *
  98. * Callback for decoding the command response return code.
  99. *
  100. * @param[in] p_buffer Pointer to begin of command response buffer.
  101. * @param[in] length Length of data in bytes.
  102. *
  103. * @return Decoded command response return code.
  104. */
  105. static uint32_t gattc_relationships_discover_rsp_dec(const uint8_t * p_buffer, uint16_t length)
  106. {
  107. uint32_t result_code;
  108. const uint32_t err_code = ble_gattc_relationships_discover_rsp_dec(p_buffer,
  109. length,
  110. &result_code);
  111. APP_ERROR_CHECK(err_code);
  112. return result_code;
  113. }
  114. #ifndef _sd_ble_gattc_relationships_discover
  115. #define _sd_ble_gattc_relationships_discover sd_ble_gattc_relationships_discover
  116. #endif
  117. uint32_t _sd_ble_gattc_relationships_discover(uint16_t conn_handle,
  118. ble_gattc_handle_range_t const * const p_handle_range)
  119. {
  120. uint8_t * p_buffer;
  121. uint32_t buffer_length = 0;
  122. tx_buf_alloc(&p_buffer, (uint16_t *)&buffer_length);
  123. const uint32_t err_code = ble_gattc_relationships_discover_req_enc(conn_handle,
  124. p_handle_range,
  125. &(p_buffer[1]),
  126. &buffer_length);
  127. APP_ERROR_CHECK(err_code);
  128. //@note: Increment buffer length as internally managed packet type field must be included.
  129. return ser_sd_transport_cmd_write(p_buffer,
  130. (++buffer_length),
  131. gattc_relationships_discover_rsp_dec);
  132. }
  133. /**@brief Command response callback function for @ref sd_ble_gattc_characteristics_discover BLE command.
  134. *
  135. * Callback for decoding the command response return code.
  136. *
  137. * @param[in] p_buffer Pointer to begin of command response buffer.
  138. * @param[in] length Length of data in bytes.
  139. *
  140. * @return Decoded command response return code.
  141. */
  142. static uint32_t gattc_characteristics_discover_rsp_dec(const uint8_t * p_buffer, uint16_t length)
  143. {
  144. uint32_t result_code;
  145. const uint32_t err_code = ble_gattc_characteristics_discover_rsp_dec(p_buffer,
  146. length,
  147. &result_code);
  148. APP_ERROR_CHECK(err_code);
  149. return result_code;
  150. }
  151. #ifndef _sd_ble_gattc_characteristics_discover
  152. #define _sd_ble_gattc_characteristics_discover sd_ble_gattc_characteristics_discover
  153. #endif
  154. uint32_t _sd_ble_gattc_characteristics_discover(
  155. uint16_t conn_handle,
  156. ble_gattc_handle_range_t const * const
  157. p_handle_range)
  158. {
  159. uint8_t * p_buffer;
  160. uint32_t buffer_length = 0;
  161. tx_buf_alloc(&p_buffer, (uint16_t *)&buffer_length);
  162. const uint32_t err_code = ble_gattc_characteristics_discover_req_enc(conn_handle,
  163. p_handle_range,
  164. &(p_buffer[1]),
  165. &buffer_length);
  166. APP_ERROR_CHECK(err_code);
  167. //@note: Increment buffer length as internally managed packet type field must be included.
  168. return ser_sd_transport_cmd_write(p_buffer,
  169. (++buffer_length),
  170. gattc_characteristics_discover_rsp_dec);
  171. }
  172. /**@brief Command response callback function for @ref sd_ble_gattc_descriptors_discover BLE command.
  173. *
  174. * Callback for decoding the command response return code.
  175. *
  176. * @param[in] p_buffer Pointer to begin of command response buffer.
  177. * @param[in] length Length of data in bytes.
  178. *
  179. * @return Decoded command response return code.
  180. */
  181. static uint32_t gattc_descriptors_discover_rsp_dec(const uint8_t * p_buffer, uint16_t length)
  182. {
  183. uint32_t result_code;
  184. const uint32_t err_code = ble_gattc_descriptors_discover_rsp_dec(p_buffer, length, &result_code);
  185. APP_ERROR_CHECK(err_code);
  186. return result_code;
  187. }
  188. #ifndef _sd_ble_gattc_descriptors_discover
  189. #define _sd_ble_gattc_descriptors_discover sd_ble_gattc_descriptors_discover
  190. #endif
  191. uint32_t _sd_ble_gattc_descriptors_discover(uint16_t conn_handle,
  192. ble_gattc_handle_range_t const * const p_handle_range)
  193. {
  194. uint8_t * p_buffer;
  195. uint32_t buffer_length = 0;
  196. tx_buf_alloc(&p_buffer, (uint16_t *)&buffer_length);
  197. const uint32_t err_code = ble_gattc_descriptors_discover_req_enc(conn_handle,
  198. p_handle_range,
  199. &(p_buffer[1]),
  200. &buffer_length);
  201. APP_ERROR_CHECK(err_code);
  202. //@note: Increment buffer length as internally managed packet type field must be included.
  203. return ser_sd_transport_cmd_write(p_buffer,
  204. (++buffer_length),
  205. gattc_descriptors_discover_rsp_dec);
  206. }
  207. /**@brief Command response callback function for @ref sd_ble_gattc_char_value_by_uuid_read BLE command.
  208. *
  209. * Callback for decoding the command response return code.
  210. *
  211. * @param[in] p_buffer Pointer to begin of command response buffer.
  212. * @param[in] length Length of data in bytes.
  213. *
  214. * @return Decoded command response return code.
  215. */
  216. static uint32_t gattc_char_value_by_uuid_read_rsp_dec(const uint8_t * p_buffer, uint16_t length)
  217. {
  218. uint32_t result_code;
  219. const uint32_t err_code = ble_gattc_char_value_by_uuid_read_rsp_dec(p_buffer,
  220. length,
  221. &result_code);
  222. APP_ERROR_CHECK(err_code);
  223. return result_code;
  224. }
  225. #ifndef _sd_ble_gattc_char_value_by_uuid_read
  226. #define _sd_ble_gattc_char_value_by_uuid_read sd_ble_gattc_char_value_by_uuid_read
  227. #endif
  228. uint32_t _sd_ble_gattc_char_value_by_uuid_read(uint16_t conn_handle,
  229. ble_uuid_t const * const p_uuid,
  230. ble_gattc_handle_range_t const * const p_handle_range)
  231. {
  232. uint8_t * p_buffer;
  233. uint32_t buffer_length = 0;
  234. tx_buf_alloc(&p_buffer, (uint16_t *)&buffer_length);
  235. const uint32_t err_code = ble_gattc_char_value_by_uuid_read_req_enc(conn_handle,
  236. p_uuid,
  237. p_handle_range,
  238. &(p_buffer[1]),
  239. &buffer_length);
  240. APP_ERROR_CHECK(err_code);
  241. //@note: Increment buffer length as internally managed packet type field must be included.
  242. return ser_sd_transport_cmd_write(p_buffer,
  243. (++buffer_length),
  244. gattc_char_value_by_uuid_read_rsp_dec);
  245. }
  246. /**@brief Command response callback function for @ref sd_ble_gattc_read BLE command.
  247. *
  248. * Callback for decoding the command response return code.
  249. *
  250. * @param[in] p_buffer Pointer to begin of command response buffer.
  251. * @param[in] length Length of data in bytes.
  252. *
  253. * @return Decoded command response return code.
  254. */
  255. static uint32_t gattc_read_rsp_dec(const uint8_t * p_buffer, uint16_t length)
  256. {
  257. uint32_t result_code;
  258. const uint32_t err_code = ble_gattc_read_rsp_dec(p_buffer, length, &result_code);
  259. APP_ERROR_CHECK(err_code);
  260. return result_code;
  261. }
  262. #ifndef _sd_ble_gattc_read
  263. #define _sd_ble_gattc_read sd_ble_gattc_read
  264. #endif
  265. uint32_t _sd_ble_gattc_read(uint16_t conn_handle,
  266. uint16_t handle,
  267. uint16_t offset)
  268. {
  269. uint8_t * p_buffer;
  270. uint32_t buffer_length = 0;
  271. tx_buf_alloc(&p_buffer, (uint16_t *)&buffer_length);
  272. const uint32_t err_code = ble_gattc_read_req_enc(conn_handle,
  273. handle,
  274. offset,
  275. &(p_buffer[1]),
  276. &buffer_length);
  277. APP_ERROR_CHECK(err_code);
  278. //@note: Increment buffer length as internally managed packet type field must be included.
  279. return ser_sd_transport_cmd_write(p_buffer,
  280. (++buffer_length),
  281. gattc_read_rsp_dec);
  282. }
  283. /**@brief Command response callback function for @ref sd_ble_gattc_char_values_read BLE command.
  284. *
  285. * Callback for decoding the command response return code.
  286. *
  287. * @param[in] p_buffer Pointer to begin of command response buffer.
  288. * @param[in] length Length of data in bytes.
  289. *
  290. * @return Decoded command response return code.
  291. */
  292. static uint32_t gattc_char_values_read_rsp_dec(const uint8_t * p_buffer, uint16_t length)
  293. {
  294. uint32_t result_code;
  295. const uint32_t err_code = ble_gattc_char_values_read_rsp_dec(p_buffer, length, &result_code);
  296. APP_ERROR_CHECK(err_code);
  297. return result_code;
  298. }
  299. #ifndef _sd_ble_gattc_char_values_read
  300. #define _sd_ble_gattc_char_values_read sd_ble_gattc_char_values_read
  301. #endif
  302. uint32_t _sd_ble_gattc_char_values_read(uint16_t conn_handle,
  303. uint16_t const * const p_handles,
  304. uint16_t handle_count)
  305. {
  306. uint8_t * p_buffer;
  307. uint32_t buffer_length = 0;
  308. tx_buf_alloc(&p_buffer, (uint16_t *)&buffer_length);
  309. const uint32_t err_code = ble_gattc_char_values_read_req_enc(conn_handle,
  310. p_handles,
  311. handle_count,
  312. &(p_buffer[1]),
  313. &buffer_length);
  314. APP_ERROR_CHECK(err_code);
  315. //@note: Increment buffer length as internally managed packet type field must be included.
  316. return ser_sd_transport_cmd_write(p_buffer,
  317. (++buffer_length),
  318. gattc_char_values_read_rsp_dec);
  319. }
  320. /**@brief Command response callback function for @ref sd_ble_gattc_write BLE command.
  321. *
  322. * Callback for decoding the command response return code.
  323. *
  324. * @param[in] p_buffer Pointer to begin of command response buffer.
  325. * @param[in] length Length of data in bytes.
  326. *
  327. * @return Decoded command response return code.
  328. */
  329. static uint32_t gattc_write_rsp_dec(const uint8_t * p_buffer, uint16_t length)
  330. {
  331. uint32_t result_code;
  332. const uint32_t err_code = ble_gattc_write_rsp_dec(p_buffer, length, &result_code);
  333. APP_ERROR_CHECK(err_code);
  334. return result_code;
  335. }
  336. #ifndef _sd_ble_gattc_write
  337. #define _sd_ble_gattc_write sd_ble_gattc_write
  338. #endif
  339. uint32_t _sd_ble_gattc_write(uint16_t conn_handle,
  340. ble_gattc_write_params_t const * const p_write_params)
  341. {
  342. uint8_t * p_buffer;
  343. uint32_t buffer_length = 0;
  344. tx_buf_alloc(&p_buffer, (uint16_t *)&buffer_length);
  345. const uint32_t err_code = ble_gattc_write_req_enc(conn_handle,
  346. p_write_params,
  347. &(p_buffer[1]),
  348. &buffer_length);
  349. APP_ERROR_CHECK(err_code);
  350. //@note: Increment buffer length as internally managed packet type field must be included.
  351. return ser_sd_transport_cmd_write(p_buffer,
  352. (++buffer_length),
  353. gattc_write_rsp_dec);
  354. }
  355. /**@brief Command response callback function for @ref sd_ble_gattc_hv_confirm BLE command.
  356. *
  357. * Callback for decoding the command response return code.
  358. *
  359. * @param[in] p_buffer Pointer to begin of command response buffer.
  360. * @param[in] length Length of data in bytes.
  361. *
  362. * @return Decoded command response return code.
  363. */
  364. static uint32_t gattc_hv_confirm_rsp_dec(const uint8_t * p_buffer, uint16_t length)
  365. {
  366. uint32_t result_code;
  367. const uint32_t err_code = ble_gattc_hv_confirm_rsp_dec(p_buffer, length, &result_code);
  368. APP_ERROR_CHECK(err_code);
  369. return result_code;
  370. }
  371. #ifndef _sd_ble_gattc_hv_confirm
  372. #define _sd_ble_gattc_hv_confirm sd_ble_gattc_hv_confirm
  373. #endif
  374. uint32_t _sd_ble_gattc_hv_confirm(uint16_t conn_handle,
  375. uint16_t handle)
  376. {
  377. uint8_t * p_buffer;
  378. uint32_t buffer_length = 0;
  379. tx_buf_alloc(&p_buffer, (uint16_t *)&buffer_length);
  380. const uint32_t err_code = ble_gattc_hv_confirm_req_enc(conn_handle,
  381. handle,
  382. &(p_buffer[1]),
  383. &buffer_length);
  384. APP_ERROR_CHECK(err_code);
  385. //@note: Increment buffer length as internally managed packet type field must be included.
  386. return ser_sd_transport_cmd_write(p_buffer,
  387. (++buffer_length),
  388. gattc_hv_confirm_rsp_dec);
  389. }
  390. /**@brief Command response callback function for @ref sd_ble_gattc_info_discover BLE command.
  391. *
  392. * Callback for decoding the command response return code.
  393. *
  394. * @param[in] p_buffer Pointer to begin of command response buffer.
  395. * @param[in] length Length of data in bytes.
  396. *
  397. * @return Decoded command response return code.
  398. */
  399. static uint32_t gattc_attr_info_discover_rsp_dec(const uint8_t * p_buffer, uint16_t length)
  400. {
  401. uint32_t result_code;
  402. const uint32_t err_code = ble_gattc_attr_info_discover_rsp_dec(p_buffer, length, &result_code);
  403. APP_ERROR_CHECK(err_code);
  404. return result_code;
  405. }
  406. #ifndef _sd_ble_gattc_attr_info_discover
  407. #define _sd_ble_gattc_attr_info_discover sd_ble_gattc_attr_info_discover
  408. #endif
  409. uint32_t _sd_ble_gattc_attr_info_discover(uint16_t conn_handle,
  410. ble_gattc_handle_range_t const * const p_handle_range)
  411. {
  412. uint8_t * p_buffer;
  413. uint32_t buffer_length = 0;
  414. tx_buf_alloc(&p_buffer, (uint16_t *)&buffer_length);
  415. const uint32_t err_code = ble_gattc_attr_info_discover_req_enc(conn_handle,
  416. p_handle_range,
  417. &(p_buffer[1]),
  418. &buffer_length);
  419. APP_ERROR_CHECK(err_code);
  420. //@note: Increment buffer length as internally managed packet type field must be included.
  421. return ser_sd_transport_cmd_write(p_buffer,
  422. (++buffer_length),
  423. gattc_attr_info_discover_rsp_dec);
  424. }
  425. /**@brief Command response callback function for @ref sd_ble_gattc_write BLE command.
  426. *
  427. * Callback for decoding the command response return code.
  428. *
  429. * @param[in] p_buffer Pointer to begin of command response buffer.
  430. * @param[in] length Length of data in bytes.
  431. *
  432. * @return Decoded command response return code.
  433. */
  434. static uint32_t gattc_exchange_mtu_request_rsp_dec(const uint8_t * p_buffer, uint16_t length)
  435. {
  436. uint32_t result_code;
  437. const uint32_t err_code = ble_gattc_exchange_mtu_request_rsp_dec(p_buffer, length, &result_code);
  438. APP_ERROR_CHECK(err_code);
  439. return result_code;
  440. }
  441. #ifndef _sd_ble_gattc_exchange_mtu_request
  442. #define _sd_ble_gattc_exchange_mtu_request sd_ble_gattc_exchange_mtu_request
  443. #endif
  444. uint32_t _sd_ble_gattc_exchange_mtu_request(uint16_t conn_handle,
  445. uint16_t client_rx_mtu)
  446. {
  447. uint8_t * p_buffer;
  448. uint32_t buffer_length = 0;
  449. tx_buf_alloc(&p_buffer, (uint16_t *)&buffer_length);
  450. const uint32_t err_code = ble_gattc_exchange_mtu_request_req_enc(conn_handle,
  451. client_rx_mtu,
  452. &(p_buffer[1]),
  453. &buffer_length);
  454. APP_ERROR_CHECK(err_code);
  455. //@note: Increment buffer length as internally managed packet type field must be included.
  456. return ser_sd_transport_cmd_write(p_buffer,
  457. (++buffer_length),
  458. gattc_exchange_mtu_request_rsp_dec);
  459. }