app_mw_ble_gatts.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  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.h"
  41. #include <stdint.h>
  42. #include "ble_serialization.h"
  43. #include "ser_sd_transport.h"
  44. #include "ble_gatts_app.h"
  45. #include "app_error.h"
  46. //Pointer for sd calls output params
  47. static void * mp_out_params[3];
  48. static void tx_buf_alloc(uint8_t * * p_data, uint16_t * p_len)
  49. {
  50. uint32_t err_code;
  51. do
  52. {
  53. err_code = ser_sd_transport_tx_alloc(p_data, p_len);
  54. }
  55. while (err_code != NRF_SUCCESS);
  56. *p_data[0] = SER_PKT_TYPE_CMD;
  57. *p_len -= 1;
  58. }
  59. /**@brief Command response callback function for @ref sd_ble_gatts_sys_attr_set BLE command.
  60. *
  61. * Callback for decoding the command response return code.
  62. *
  63. * @param[in] p_buffer Pointer to begin of command response buffer.
  64. * @param[in] length Length of data in bytes.
  65. *
  66. * @return Decoded command response return code.
  67. */
  68. static uint32_t gatts_sys_attr_set_rsp_dec(const uint8_t * p_buffer, uint16_t length)
  69. {
  70. uint32_t result_code;
  71. const uint32_t err_code = ble_gatts_sys_attr_set_rsp_dec(p_buffer, length, &result_code);
  72. APP_ERROR_CHECK(err_code);
  73. return result_code;
  74. }
  75. #ifndef _sd_ble_gatts_sys_attr_set
  76. #define _sd_ble_gatts_sys_attr_set sd_ble_gatts_sys_attr_set
  77. #endif
  78. uint32_t _sd_ble_gatts_sys_attr_set(uint16_t conn_handle,
  79. uint8_t const * const p_sys_attr_data,
  80. uint16_t len,
  81. uint32_t flags)
  82. {
  83. uint8_t * p_buffer;
  84. uint32_t buffer_length = 0;
  85. tx_buf_alloc(&p_buffer, (uint16_t *)&buffer_length);
  86. const uint32_t err_code = ble_gatts_sys_attr_set_req_enc(conn_handle,
  87. p_sys_attr_data,
  88. len,
  89. flags,
  90. &(p_buffer[1]),
  91. &buffer_length);
  92. APP_ERROR_CHECK(err_code);
  93. //@note: Increment buffer length as internally managed packet type field must be included.
  94. return ser_sd_transport_cmd_write(p_buffer,
  95. (++buffer_length),
  96. gatts_sys_attr_set_rsp_dec);
  97. }
  98. /**@brief Command response callback function for @ref sd_ble_gatts_hvx BLE command.
  99. *
  100. * Callback for decoding the command response return code.
  101. *
  102. * @param[in] p_buffer Pointer to begin of command response buffer.
  103. * @param[in] length Length of data in bytes.
  104. *
  105. * @return Decoded command response return code.
  106. */
  107. static uint32_t gatts_hvx_rsp_dec(const uint8_t * p_buffer, uint16_t length)
  108. {
  109. uint32_t result_code;
  110. const uint32_t err_code = ble_gatts_hvx_rsp_dec(p_buffer, length, &result_code,
  111. (uint16_t * *)&mp_out_params[0]);
  112. APP_ERROR_CHECK(err_code);
  113. return result_code;
  114. }
  115. #ifndef _sd_ble_gatts_hvx
  116. #define _sd_ble_gatts_hvx sd_ble_gatts_hvx
  117. #endif
  118. uint32_t _sd_ble_gatts_hvx(uint16_t conn_handle, ble_gatts_hvx_params_t const * const p_hvx_params)
  119. {
  120. uint8_t * p_buffer;
  121. uint32_t buffer_length = 0;
  122. tx_buf_alloc(&p_buffer, (uint16_t *)&buffer_length);
  123. mp_out_params[0] = (p_hvx_params) ? p_hvx_params->p_len : NULL;
  124. const uint32_t err_code = ble_gatts_hvx_req_enc(conn_handle,
  125. p_hvx_params,
  126. &(p_buffer[1]),
  127. &buffer_length);
  128. APP_ERROR_CHECK(err_code);
  129. //@note: Increment buffer length as internally managed packet type field must be included.
  130. return ser_sd_transport_cmd_write(p_buffer,
  131. (++buffer_length),
  132. gatts_hvx_rsp_dec);
  133. }
  134. /**@brief Command response callback function for @ref sd_ble_gatts_service_add BLE command.
  135. *
  136. * Callback for decoding the output parameters and the command response return code.
  137. *
  138. * @param[in] p_buffer Pointer to begin of command response buffer.
  139. * @param[in] length Length of data in bytes.
  140. *
  141. * @return Decoded command response return code.
  142. */
  143. static uint32_t gatts_service_add_rsp_dec(const uint8_t * p_buffer, uint16_t length)
  144. {
  145. uint32_t result_code;
  146. const uint32_t err_code =
  147. ble_gatts_service_add_rsp_dec(p_buffer,
  148. length,
  149. (uint16_t *)mp_out_params[0],
  150. &result_code);
  151. APP_ERROR_CHECK(err_code);
  152. return result_code;
  153. }
  154. #ifndef _sd_ble_gatts_service_add
  155. #define _sd_ble_gatts_service_add sd_ble_gatts_service_add
  156. #endif
  157. uint32_t _sd_ble_gatts_service_add(uint8_t type,
  158. ble_uuid_t const * const p_uuid,
  159. uint16_t * const p_handle)
  160. {
  161. uint8_t * p_buffer;
  162. uint32_t buffer_length = 0;
  163. tx_buf_alloc(&p_buffer, (uint16_t *)&buffer_length);
  164. mp_out_params[0] = p_handle;
  165. const uint32_t err_code = ble_gatts_service_add_req_enc(type,
  166. p_uuid,
  167. p_handle,
  168. &(p_buffer[1]),
  169. &buffer_length);
  170. APP_ERROR_CHECK(err_code);
  171. //@note: Increment buffer length as internally managed packet type field must be included.
  172. return ser_sd_transport_cmd_write(p_buffer,
  173. (++buffer_length),
  174. gatts_service_add_rsp_dec);
  175. }
  176. /**@brief Command response callback function for @ref sd_ble_gatts_service_add BLE command.
  177. *
  178. * Callback for decoding the output parameters and the command response return code.
  179. *
  180. * @param[in] p_buffer Pointer to begin of command response buffer.
  181. * @param[in] length Length of data in bytes.
  182. *
  183. * @return Decoded command response return code.
  184. */
  185. static uint32_t gatts_service_changed_rsp_dec(const uint8_t * p_buffer, uint16_t length)
  186. {
  187. uint32_t result_code = NRF_SUCCESS;
  188. const uint32_t err_code = ble_gatts_service_changed_rsp_dec(p_buffer,
  189. length,
  190. &result_code);
  191. APP_ERROR_CHECK(err_code);
  192. return result_code;
  193. }
  194. #ifndef _sd_ble_gatts_service_changed
  195. #define _sd_ble_gatts_service_changed sd_ble_gatts_service_changed
  196. #endif
  197. uint32_t _sd_ble_gatts_service_changed(uint16_t conn_handle,
  198. uint16_t start_handle,
  199. uint16_t end_handle)
  200. {
  201. uint8_t * p_buffer;
  202. uint32_t buffer_length = 0;
  203. tx_buf_alloc(&p_buffer, (uint16_t *)&buffer_length);
  204. const uint32_t err_code = ble_gatts_service_changed_req_enc(conn_handle,
  205. start_handle,
  206. end_handle,
  207. &(p_buffer[1]),
  208. &buffer_length);
  209. APP_ERROR_CHECK(err_code);
  210. //@note: Increment buffer length as internally managed packet type field must be included.
  211. return ser_sd_transport_cmd_write(p_buffer,
  212. (++buffer_length),
  213. gatts_service_changed_rsp_dec);
  214. }
  215. /**@brief Command response callback function for @ref sd_ble_gatts_include_add BLE command.
  216. *
  217. * Callback for decoding the output parameters and the command response return code.
  218. *
  219. * @param[in] p_buffer Pointer to begin of command response buffer.
  220. * @param[in] length Length of data in bytes.
  221. *
  222. * @return Decoded command response return code.
  223. */
  224. static uint32_t gatts_include_add_rsp_dec(const uint8_t * p_buffer, uint16_t length)
  225. {
  226. uint32_t result_code = NRF_SUCCESS;
  227. const uint32_t err_code =
  228. ble_gatts_include_add_rsp_dec(p_buffer,
  229. length,
  230. (uint16_t *) mp_out_params[0],
  231. &result_code);
  232. APP_ERROR_CHECK(err_code);
  233. return result_code;
  234. }
  235. #ifndef _sd_ble_gatts_include_add
  236. #define _sd_ble_gatts_include_add sd_ble_gatts_include_add
  237. #endif
  238. uint32_t _sd_ble_gatts_include_add(uint16_t service_handle,
  239. uint16_t inc_serv_handle,
  240. uint16_t * const p_include_handle)
  241. {
  242. uint8_t * p_buffer;
  243. uint32_t buffer_length = 0;
  244. tx_buf_alloc(&p_buffer, (uint16_t *)&buffer_length);
  245. mp_out_params[0] = p_include_handle;
  246. const uint32_t err_code = ble_gatts_include_add_req_enc(service_handle,
  247. inc_serv_handle,
  248. p_include_handle,
  249. &(p_buffer[1]),
  250. &buffer_length);
  251. APP_ERROR_CHECK(err_code);
  252. //@note: Increment buffer length as internally managed packet type field must be included.
  253. return ser_sd_transport_cmd_write(p_buffer,
  254. (++buffer_length),
  255. gatts_include_add_rsp_dec);
  256. }
  257. /**@brief Command response callback function for @ref sd_ble_gatts_characteristic_add BLE command.
  258. *
  259. * Callback for decoding the output parameters and the command response return code.
  260. *
  261. * @param[in] p_buffer Pointer to begin of command response buffer.
  262. * @param[in] length Length of data in bytes.
  263. *
  264. * @return Decoded command response return code.
  265. */
  266. static uint32_t gatts_characteristic_add_rsp_dec(const uint8_t * p_buffer, uint16_t length)
  267. {
  268. uint32_t result_code;
  269. const uint32_t err_code = ble_gatts_characteristic_add_rsp_dec(
  270. p_buffer,
  271. length,
  272. (uint16_t * *)&mp_out_params[0],
  273. &result_code);
  274. APP_ERROR_CHECK(err_code);
  275. return result_code;
  276. }
  277. #ifndef _sd_ble_gatts_characteristic_add
  278. #define _sd_ble_gatts_characteristic_add sd_ble_gatts_characteristic_add
  279. #endif
  280. uint32_t _sd_ble_gatts_characteristic_add(uint16_t service_handle,
  281. ble_gatts_char_md_t const * const p_char_md,
  282. ble_gatts_attr_t const * const p_attr_char_value,
  283. ble_gatts_char_handles_t * const p_handles)
  284. {
  285. uint8_t * p_buffer;
  286. uint32_t buffer_length = 0;
  287. tx_buf_alloc(&p_buffer, (uint16_t *)&buffer_length);
  288. mp_out_params[0] = p_handles;
  289. const uint32_t err_code = ble_gatts_characteristic_add_req_enc(service_handle,
  290. p_char_md,
  291. p_attr_char_value,
  292. p_handles,
  293. &(p_buffer[1]),
  294. &buffer_length);
  295. APP_ERROR_CHECK(err_code);
  296. //@note: Increment buffer length as internally managed packet type field must be included.
  297. return ser_sd_transport_cmd_write(p_buffer,
  298. (++buffer_length),
  299. gatts_characteristic_add_rsp_dec);
  300. }
  301. /**@brief Command response callback function for @ref sd_ble_gatts_descriptor_add BLE command.
  302. *
  303. * Callback for decoding the output parameters and the command response return code.
  304. *
  305. * @param[in] p_buffer Pointer to begin of command response buffer.
  306. * @param[in] length Length of data in bytes.
  307. *
  308. * @return Decoded command response return code.
  309. */
  310. static uint32_t gatts_descriptor_add_rsp_dec(const uint8_t * p_buffer, uint16_t length)
  311. {
  312. uint32_t result_code = NRF_SUCCESS;
  313. const uint32_t err_code =
  314. ble_gatts_descriptor_add_rsp_dec(p_buffer,
  315. length,
  316. (uint16_t *) mp_out_params[0],
  317. &result_code);
  318. APP_ERROR_CHECK(err_code);
  319. return result_code;
  320. }
  321. #ifndef _sd_ble_gatts_descriptor_add
  322. #define _sd_ble_gatts_descriptor_add sd_ble_gatts_descriptor_add
  323. #endif
  324. uint32_t _sd_ble_gatts_descriptor_add(uint16_t char_handle,
  325. ble_gatts_attr_t const * const p_attr,
  326. uint16_t * const p_handle)
  327. {
  328. uint8_t * p_buffer;
  329. uint32_t buffer_length = 0;
  330. tx_buf_alloc(&p_buffer, (uint16_t *)&buffer_length);
  331. mp_out_params[0] = p_handle;
  332. const uint32_t err_code = ble_gatts_descriptor_add_req_enc(char_handle,
  333. p_attr,
  334. p_handle,
  335. &(p_buffer[1]),
  336. &buffer_length);
  337. APP_ERROR_CHECK(err_code);
  338. //@note: Increment buffer length as internally managed packet type field must be included.
  339. return ser_sd_transport_cmd_write(p_buffer,
  340. (++buffer_length),
  341. gatts_descriptor_add_rsp_dec);
  342. }
  343. /**@brief Command response callback function for @ref sd_ble_gatts_rw_authorize_reply BLE command.
  344. *
  345. * Callback for decoding the output parameters and the command response return code.
  346. *
  347. * @param[in] p_buffer Pointer to begin of command response buffer.
  348. * @param[in] length Length of data in bytes.
  349. *
  350. * @return Decoded command response return code.
  351. */
  352. static uint32_t gatts_rw_authorize_reply_rsp_dec(const uint8_t * p_buffer, uint16_t length)
  353. {
  354. uint32_t result_code = NRF_SUCCESS;
  355. const uint32_t err_code = ble_gatts_rw_authorize_reply_rsp_dec(p_buffer,
  356. length,
  357. &result_code);
  358. APP_ERROR_CHECK(err_code);
  359. return result_code;
  360. }
  361. #ifndef _sd_ble_gatts_rw_authorize_reply
  362. #define _sd_ble_gatts_rw_authorize_reply sd_ble_gatts_rw_authorize_reply
  363. #endif
  364. uint32_t _sd_ble_gatts_rw_authorize_reply(
  365. uint16_t conn_handle,
  366. ble_gatts_rw_authorize_reply_params_t const * const
  367. p_rw_authorize_reply_params)
  368. {
  369. uint8_t * p_buffer;
  370. uint32_t buffer_length = 0;
  371. tx_buf_alloc(&p_buffer, (uint16_t *)&buffer_length);
  372. const uint32_t err_code = ble_gatts_rw_authorize_reply_req_enc(conn_handle,
  373. p_rw_authorize_reply_params,
  374. &(p_buffer[1]),
  375. &buffer_length);
  376. APP_ERROR_CHECK(err_code);
  377. //@note: Increment buffer length as internally managed packet type field must be included.
  378. return ser_sd_transport_cmd_write(p_buffer,
  379. (++buffer_length),
  380. gatts_rw_authorize_reply_rsp_dec);
  381. }
  382. /**@brief Command response callback function for @ref sd_ble_gatts_value_get BLE command.
  383. *
  384. * Callback for decoding the output parameters and the command response return code.
  385. *
  386. * @param[in] p_buffer Pointer to begin of command response buffer.
  387. * @param[in] length Length of data in bytes.
  388. *
  389. * @return Decoded command response return code.
  390. */
  391. static uint32_t gatts_value_get_rsp_dec(const uint8_t * p_buffer, uint16_t length)
  392. {
  393. uint32_t result_code;
  394. const uint32_t err_code = ble_gatts_value_get_rsp_dec(p_buffer,
  395. length,
  396. (ble_gatts_value_t *)mp_out_params[0],
  397. &result_code);
  398. APP_ERROR_CHECK(err_code);
  399. return result_code;
  400. }
  401. #ifndef _sd_ble_gatts_value_get
  402. #define _sd_ble_gatts_value_get sd_ble_gatts_value_get
  403. #endif
  404. uint32_t _sd_ble_gatts_value_get(uint16_t conn_handle,
  405. uint16_t handle,
  406. ble_gatts_value_t * p_value)
  407. {
  408. uint8_t * p_buffer;
  409. uint32_t buffer_length = 0;
  410. tx_buf_alloc(&p_buffer, (uint16_t *)&buffer_length);
  411. mp_out_params[0] = p_value;
  412. const uint32_t err_code = ble_gatts_value_get_req_enc(conn_handle,
  413. handle,
  414. p_value,
  415. &(p_buffer[1]),
  416. &buffer_length);
  417. APP_ERROR_CHECK(err_code);
  418. //@note: Increment buffer length as internally managed packet type field must be included.
  419. return ser_sd_transport_cmd_write(p_buffer,
  420. (++buffer_length),
  421. gatts_value_get_rsp_dec);
  422. }
  423. /**@brief Command response callback function for @ref sd_ble_gatts_value_set BLE command.
  424. *
  425. * Callback for decoding the output parameters and the command response return code.
  426. *
  427. * @param[in] p_buffer Pointer to begin of command response buffer.
  428. * @param[in] length Length of data in bytes.
  429. *
  430. * @return Decoded command response return code.
  431. */
  432. static uint32_t gatts_value_set_rsp_dec(const uint8_t * p_buffer, uint16_t length)
  433. {
  434. uint32_t result_code;
  435. const uint32_t err_code = ble_gatts_value_set_rsp_dec(
  436. p_buffer,
  437. length,
  438. (ble_gatts_value_t *)mp_out_params[0],
  439. &result_code);
  440. APP_ERROR_CHECK(err_code);
  441. return result_code;
  442. }
  443. #ifndef _sd_ble_gatts_value_set
  444. #define _sd_ble_gatts_value_set sd_ble_gatts_value_set
  445. #endif
  446. uint32_t _sd_ble_gatts_value_set(uint16_t conn_handle,
  447. uint16_t handle,
  448. ble_gatts_value_t * p_value)
  449. {
  450. uint8_t * p_buffer;
  451. uint32_t buffer_length = 0;
  452. tx_buf_alloc(&p_buffer, (uint16_t *)&buffer_length);
  453. mp_out_params[0] = p_value;
  454. const uint32_t err_code = ble_gatts_value_set_req_enc(conn_handle,
  455. handle,
  456. p_value,
  457. &(p_buffer[1]),
  458. &buffer_length);
  459. //@note: Should never fail.
  460. APP_ERROR_CHECK(err_code);
  461. //@note: Increment buffer length as internally managed packet type field must be included.
  462. return ser_sd_transport_cmd_write(p_buffer,
  463. (++buffer_length),
  464. gatts_value_set_rsp_dec);
  465. }
  466. /**@brief Command response callback function for @ref sd_ble_gatts_sys_attr_get BLE command.
  467. *
  468. * Callback for decoding the output parameters and the command response return code.
  469. *
  470. * @param[in] p_buffer Pointer to begin of command response buffer.
  471. * @param[in] length Length of data in bytes.
  472. *
  473. * @return Decoded command response return code.
  474. */
  475. static uint32_t gatts_sys_attr_get_rsp_dec(const uint8_t * p_buffer, uint16_t length)
  476. {
  477. uint32_t result_code;
  478. const uint32_t err_code = ble_gatts_sys_attr_get_rsp_dec(
  479. p_buffer,
  480. length,
  481. (uint8_t * *) &mp_out_params[0],
  482. (uint16_t * *) &mp_out_params[1],
  483. &result_code);
  484. APP_ERROR_CHECK(err_code);
  485. return result_code;
  486. }
  487. #ifndef _sd_ble_gatts_sys_attr_get
  488. #define _sd_ble_gatts_sys_attr_get sd_ble_gatts_sys_attr_get
  489. #endif
  490. uint32_t _sd_ble_gatts_sys_attr_get(uint16_t conn_handle,
  491. uint8_t * const p_sys_attr_data,
  492. uint16_t * const p_len,
  493. uint32_t flags)
  494. {
  495. uint8_t * p_buffer;
  496. uint32_t buffer_length = 0;
  497. tx_buf_alloc(&p_buffer, (uint16_t *)&buffer_length);
  498. mp_out_params[0] = p_sys_attr_data;
  499. mp_out_params[1] = p_len;
  500. const uint32_t err_code = ble_gatts_sys_attr_get_req_enc(conn_handle,
  501. p_sys_attr_data,
  502. p_len,
  503. flags,
  504. &(p_buffer[1]),
  505. &buffer_length);
  506. //@note: Should never fail.
  507. APP_ERROR_CHECK(err_code);
  508. //@note: Increment buffer length as internally managed packet type field must be included.
  509. return ser_sd_transport_cmd_write(p_buffer,
  510. (++buffer_length),
  511. gatts_sys_attr_get_rsp_dec);
  512. }
  513. /**@brief Command response callback function for @ref sd_ble_gatts_attr_get BLE command.
  514. *
  515. * Callback for decoding the output parameters and the command response return code.
  516. *
  517. * @param[in] p_buffer Pointer to begin of command response buffer.
  518. * @param[in] length Length of data in bytes.
  519. *
  520. * @return Decoded command response return code.
  521. */
  522. static uint32_t gatts_attr_get_rsp_dec(const uint8_t * p_buffer, uint16_t length)
  523. {
  524. uint32_t result_code;
  525. const uint32_t err_code = ble_gatts_attr_get_rsp_dec(
  526. p_buffer,
  527. length,
  528. (ble_uuid_t **)&mp_out_params[0],
  529. (ble_gatts_attr_md_t **)&mp_out_params[1],
  530. &result_code);
  531. APP_ERROR_CHECK(err_code);
  532. return result_code;
  533. }
  534. #ifndef _sd_ble_gatts_attr_get
  535. #define _sd_ble_gatts_attr_get sd_ble_gatts_attr_get
  536. #endif
  537. uint32_t _sd_ble_gatts_attr_get(uint16_t handle,
  538. ble_uuid_t * p_uuid,
  539. ble_gatts_attr_md_t * p_md)
  540. {
  541. uint8_t * p_buffer;
  542. uint32_t buffer_length = 0;
  543. tx_buf_alloc(&p_buffer, (uint16_t *)&buffer_length);
  544. mp_out_params[0] = p_uuid;
  545. mp_out_params[1] = p_md;
  546. const uint32_t err_code = ble_gatts_attr_get_req_enc(handle,
  547. p_uuid,
  548. p_md,
  549. &(p_buffer[1]),
  550. &buffer_length);
  551. //@note: Should never fail.
  552. APP_ERROR_CHECK(err_code);
  553. //@note: Increment buffer length as internally managed packet type field must be included.
  554. return ser_sd_transport_cmd_write(p_buffer,
  555. (++buffer_length),
  556. gatts_attr_get_rsp_dec);
  557. }
  558. /**@brief Command response callback function for @ref sd_ble_gatts_initial_user_handle_get BLE command.
  559. *
  560. * Callback for decoding the output parameters and the command response return code.
  561. *
  562. * @param[in] p_buffer Pointer to begin of command response buffer.
  563. * @param[in] length Length of data in bytes.
  564. *
  565. * @return Decoded command response return code.
  566. */
  567. static uint32_t gatts_initial_user_handle_get_rsp_dec(const uint8_t * p_buffer, uint16_t length)
  568. {
  569. uint32_t result_code;
  570. const uint32_t err_code = ble_gatts_initial_user_handle_get_rsp_dec(
  571. p_buffer,
  572. length,
  573. (uint16_t **)&mp_out_params[0],
  574. &result_code);
  575. APP_ERROR_CHECK(err_code);
  576. return result_code;
  577. }
  578. #ifndef _sd_ble_gatts_initial_user_handle_get
  579. #define _sd_ble_gatts_initial_user_handle_get sd_ble_gatts_initial_user_handle_get
  580. #endif
  581. uint32_t _sd_ble_gatts_initial_user_handle_get(uint16_t * p_handle)
  582. {
  583. uint8_t * p_buffer;
  584. uint32_t buffer_length = 0;
  585. tx_buf_alloc(&p_buffer, (uint16_t *)&buffer_length);
  586. mp_out_params[0] = p_handle;
  587. const uint32_t err_code = ble_gatts_initial_user_handle_get_req_enc(p_handle,
  588. &(p_buffer[1]),
  589. &buffer_length);
  590. //@note: Should never fail.
  591. APP_ERROR_CHECK(err_code);
  592. //@note: Increment buffer length as internally managed packet type field must be included.
  593. return ser_sd_transport_cmd_write(p_buffer,
  594. (++buffer_length),
  595. gatts_initial_user_handle_get_rsp_dec);
  596. }
  597. /**@brief Command response callback function for @ref sd_ble_gatts_exchange_mtu_reply BLE command.
  598. *
  599. * Callback for decoding the output parameters and the command response return code.
  600. *
  601. * @param[in] p_buffer Pointer to begin of command response buffer.
  602. * @param[in] length Length of data in bytes.
  603. *
  604. * @return Decoded command response return code.
  605. */
  606. static uint32_t gatts_exchange_mtu_reply_rsp_dec(const uint8_t * p_buffer, uint16_t length)
  607. {
  608. uint32_t result_code;
  609. const uint32_t err_code = ble_gatts_exchange_mtu_reply_rsp_dec(
  610. p_buffer,
  611. length,
  612. &result_code);
  613. APP_ERROR_CHECK(err_code);
  614. return result_code;
  615. }
  616. #ifndef _sd_ble_gatts_exchange_mtu_reply
  617. #define _sd_ble_gatts_exchange_mtu_reply sd_ble_gatts_exchange_mtu_reply
  618. #endif
  619. uint32_t _sd_ble_gatts_exchange_mtu_reply(uint16_t conn_handle, uint16_t server_rx_mtu)
  620. {
  621. uint8_t * p_buffer;
  622. uint32_t buffer_length = 0;
  623. tx_buf_alloc(&p_buffer, (uint16_t *)&buffer_length);
  624. const uint32_t err_code = ble_gatts_exchange_mtu_reply_req_enc(conn_handle,
  625. server_rx_mtu,
  626. &(p_buffer[1]),
  627. &buffer_length);
  628. APP_ERROR_CHECK(err_code);
  629. //@note: Increment buffer length as internally managed packet type field must be included.
  630. return ser_sd_transport_cmd_write(p_buffer,
  631. (++buffer_length),
  632. gatts_exchange_mtu_reply_rsp_dec);
  633. }