ble_gattc.h 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  1. /*
  2. * Copyright (c) 2011 - 2020, Nordic Semiconductor ASA
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form, except as embedded into a Nordic
  12. * Semiconductor ASA integrated circuit in a product or a software update for
  13. * such product, must reproduce the above copyright notice, this list of
  14. * conditions and the following disclaimer in the documentation and/or other
  15. * materials provided with the distribution.
  16. *
  17. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  18. * contributors may be used to endorse or promote products derived from this
  19. * software without specific prior written permission.
  20. *
  21. * 4. This software, with or without modification, must only be used with a
  22. * Nordic Semiconductor ASA integrated circuit.
  23. *
  24. * 5. Any software provided in binary form under this license must not be reverse
  25. * engineered, decompiled, modified and/or disassembled.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  28. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  29. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  30. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  31. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  32. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  33. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  34. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  35. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  36. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  37. */
  38. /**
  39. @addtogroup BLE_GATTC Generic Attribute Profile (GATT) Client
  40. @{
  41. @brief Definitions and prototypes for the GATT Client interface.
  42. */
  43. #ifndef BLE_GATTC_H__
  44. #define BLE_GATTC_H__
  45. #include <stdint.h>
  46. #include "nrf.h"
  47. #include "nrf_svc.h"
  48. #include "nrf_error.h"
  49. #include "ble_ranges.h"
  50. #include "ble_types.h"
  51. #include "ble_err.h"
  52. #include "ble_gatt.h"
  53. #ifdef __cplusplus
  54. extern "C" {
  55. #endif
  56. /** @addtogroup BLE_GATTC_ENUMERATIONS Enumerations
  57. * @{ */
  58. /**@brief GATTC API SVC numbers. */
  59. enum BLE_GATTC_SVCS
  60. {
  61. SD_BLE_GATTC_PRIMARY_SERVICES_DISCOVER = BLE_GATTC_SVC_BASE, /**< Primary Service Discovery. */
  62. SD_BLE_GATTC_RELATIONSHIPS_DISCOVER, /**< Relationship Discovery. */
  63. SD_BLE_GATTC_CHARACTERISTICS_DISCOVER, /**< Characteristic Discovery. */
  64. SD_BLE_GATTC_DESCRIPTORS_DISCOVER, /**< Characteristic Descriptor Discovery. */
  65. SD_BLE_GATTC_ATTR_INFO_DISCOVER, /**< Attribute Information Discovery. */
  66. SD_BLE_GATTC_CHAR_VALUE_BY_UUID_READ, /**< Read Characteristic Value by UUID. */
  67. SD_BLE_GATTC_READ, /**< Generic read. */
  68. SD_BLE_GATTC_CHAR_VALUES_READ, /**< Read multiple Characteristic Values. */
  69. SD_BLE_GATTC_WRITE, /**< Generic write. */
  70. SD_BLE_GATTC_HV_CONFIRM, /**< Handle Value Confirmation. */
  71. SD_BLE_GATTC_EXCHANGE_MTU_REQUEST, /**< Exchange MTU Request. */
  72. };
  73. /**
  74. * @brief GATT Client Event IDs.
  75. */
  76. enum BLE_GATTC_EVTS
  77. {
  78. BLE_GATTC_EVT_PRIM_SRVC_DISC_RSP = BLE_GATTC_EVT_BASE, /**< Primary Service Discovery Response event. \n See @ref ble_gattc_evt_prim_srvc_disc_rsp_t. */
  79. BLE_GATTC_EVT_REL_DISC_RSP, /**< Relationship Discovery Response event. \n See @ref ble_gattc_evt_rel_disc_rsp_t. */
  80. BLE_GATTC_EVT_CHAR_DISC_RSP, /**< Characteristic Discovery Response event. \n See @ref ble_gattc_evt_char_disc_rsp_t. */
  81. BLE_GATTC_EVT_DESC_DISC_RSP, /**< Descriptor Discovery Response event. \n See @ref ble_gattc_evt_desc_disc_rsp_t. */
  82. BLE_GATTC_EVT_ATTR_INFO_DISC_RSP, /**< Attribute Information Response event. \n See @ref ble_gattc_evt_attr_info_disc_rsp_t. */
  83. BLE_GATTC_EVT_CHAR_VAL_BY_UUID_READ_RSP, /**< Read By UUID Response event. \n See @ref ble_gattc_evt_char_val_by_uuid_read_rsp_t. */
  84. BLE_GATTC_EVT_READ_RSP, /**< Read Response event. \n See @ref ble_gattc_evt_read_rsp_t. */
  85. BLE_GATTC_EVT_CHAR_VALS_READ_RSP, /**< Read multiple Response event. \n See @ref ble_gattc_evt_char_vals_read_rsp_t. */
  86. BLE_GATTC_EVT_WRITE_RSP, /**< Write Response event. \n See @ref ble_gattc_evt_write_rsp_t. */
  87. BLE_GATTC_EVT_HVX, /**< Handle Value Notification or Indication event. \n Confirm indication with @ref sd_ble_gattc_hv_confirm. \n See @ref ble_gattc_evt_hvx_t. */
  88. BLE_GATTC_EVT_EXCHANGE_MTU_RSP, /**< Exchange MTU Response event. \n See @ref ble_gattc_evt_exchange_mtu_rsp_t. */
  89. BLE_GATTC_EVT_TIMEOUT, /**< Timeout event. \n See @ref ble_gattc_evt_timeout_t. */
  90. BLE_GATTC_EVT_WRITE_CMD_TX_COMPLETE /**< Write without Response transmission complete. \n See @ref ble_gattc_evt_write_cmd_tx_complete_t. */
  91. };
  92. /**@brief GATTC Option IDs.
  93. * IDs that uniquely identify a GATTC option.
  94. */
  95. enum BLE_GATTC_OPTS
  96. {
  97. BLE_GATTC_OPT_UUID_DISC = BLE_GATTC_OPT_BASE, /**< UUID discovery. @ref ble_gattc_opt_uuid_disc_t */
  98. };
  99. /** @} */
  100. /** @addtogroup BLE_GATTC_DEFINES Defines
  101. * @{ */
  102. /** @defgroup BLE_ERRORS_GATTC SVC return values specific to GATTC
  103. * @{ */
  104. #define BLE_ERROR_GATTC_PROC_NOT_PERMITTED (NRF_GATTC_ERR_BASE + 0x000) /**< Procedure not Permitted. */
  105. /** @} */
  106. /** @defgroup BLE_GATTC_ATTR_INFO_FORMAT Attribute Information Formats
  107. * @{ */
  108. #define BLE_GATTC_ATTR_INFO_FORMAT_16BIT 1 /**< 16-bit Attribute Information Format. */
  109. #define BLE_GATTC_ATTR_INFO_FORMAT_128BIT 2 /**< 128-bit Attribute Information Format. */
  110. /** @} */
  111. /** @defgroup BLE_GATTC_DEFAULTS GATT Client defaults
  112. * @{ */
  113. #define BLE_GATTC_WRITE_CMD_TX_QUEUE_SIZE_DEFAULT 1 /**< Default number of Write without Response that can be queued for transmission. */
  114. /** @} */
  115. /** @} */
  116. /** @addtogroup BLE_GATTC_STRUCTURES Structures
  117. * @{ */
  118. /**
  119. * @brief BLE GATTC connection configuration parameters, set with @ref sd_ble_cfg_set.
  120. */
  121. typedef struct
  122. {
  123. uint8_t write_cmd_tx_queue_size; /**< The guaranteed minimum number of Write without Response that can be queued for transmission.
  124. The default value is @ref BLE_GATTC_WRITE_CMD_TX_QUEUE_SIZE_DEFAULT */
  125. } ble_gattc_conn_cfg_t;
  126. /**@brief Operation Handle Range. */
  127. typedef struct
  128. {
  129. uint16_t start_handle; /**< Start Handle. */
  130. uint16_t end_handle; /**< End Handle. */
  131. } ble_gattc_handle_range_t;
  132. /**@brief GATT service. */
  133. typedef struct
  134. {
  135. ble_uuid_t uuid; /**< Service UUID. */
  136. ble_gattc_handle_range_t handle_range; /**< Service Handle Range. */
  137. } ble_gattc_service_t;
  138. /**@brief GATT include. */
  139. typedef struct
  140. {
  141. uint16_t handle; /**< Include Handle. */
  142. ble_gattc_service_t included_srvc; /**< Handle of the included service. */
  143. } ble_gattc_include_t;
  144. /**@brief GATT characteristic. */
  145. typedef struct
  146. {
  147. ble_uuid_t uuid; /**< Characteristic UUID. */
  148. ble_gatt_char_props_t char_props; /**< Characteristic Properties. */
  149. uint8_t char_ext_props : 1; /**< Extended properties present. */
  150. uint16_t handle_decl; /**< Handle of the Characteristic Declaration. */
  151. uint16_t handle_value; /**< Handle of the Characteristic Value. */
  152. } ble_gattc_char_t;
  153. /**@brief GATT descriptor. */
  154. typedef struct
  155. {
  156. uint16_t handle; /**< Descriptor Handle. */
  157. ble_uuid_t uuid; /**< Descriptor UUID. */
  158. } ble_gattc_desc_t;
  159. /**@brief Write Parameters. */
  160. typedef struct
  161. {
  162. uint8_t write_op; /**< Write Operation to be performed, see @ref BLE_GATT_WRITE_OPS. */
  163. uint8_t flags; /**< Flags, see @ref BLE_GATT_EXEC_WRITE_FLAGS. */
  164. uint16_t handle; /**< Handle to the attribute to be written. */
  165. uint16_t offset; /**< Offset in bytes. @note For WRITE_CMD and WRITE_REQ, offset must be 0. */
  166. uint16_t len; /**< Length of data in bytes. */
  167. uint8_t const *p_value; /**< Pointer to the value data. */
  168. } ble_gattc_write_params_t;
  169. /**@brief Attribute Information for 16-bit Attribute UUID. */
  170. typedef struct
  171. {
  172. uint16_t handle; /**< Attribute handle. */
  173. ble_uuid_t uuid; /**< 16-bit Attribute UUID. */
  174. } ble_gattc_attr_info16_t;
  175. /**@brief Attribute Information for 128-bit Attribute UUID. */
  176. typedef struct
  177. {
  178. uint16_t handle; /**< Attribute handle. */
  179. ble_uuid128_t uuid; /**< 128-bit Attribute UUID. */
  180. } ble_gattc_attr_info128_t;
  181. /**@brief Event structure for @ref BLE_GATTC_EVT_PRIM_SRVC_DISC_RSP. */
  182. typedef struct
  183. {
  184. uint16_t count; /**< Service count. */
  185. ble_gattc_service_t services[1]; /**< Service data. @note This is a variable length array. The size of 1 indicated is only a placeholder for compilation.
  186. See @ref sd_ble_evt_get for more information on how to use event structures with variable length array members. */
  187. } ble_gattc_evt_prim_srvc_disc_rsp_t;
  188. /**@brief Event structure for @ref BLE_GATTC_EVT_REL_DISC_RSP. */
  189. typedef struct
  190. {
  191. uint16_t count; /**< Include count. */
  192. ble_gattc_include_t includes[1]; /**< Include data. @note This is a variable length array. The size of 1 indicated is only a placeholder for compilation.
  193. See @ref sd_ble_evt_get for more information on how to use event structures with variable length array members. */
  194. } ble_gattc_evt_rel_disc_rsp_t;
  195. /**@brief Event structure for @ref BLE_GATTC_EVT_CHAR_DISC_RSP. */
  196. typedef struct
  197. {
  198. uint16_t count; /**< Characteristic count. */
  199. ble_gattc_char_t chars[1]; /**< Characteristic data. @note This is a variable length array. The size of 1 indicated is only a placeholder for compilation.
  200. See @ref sd_ble_evt_get for more information on how to use event structures with variable length array members. */
  201. } ble_gattc_evt_char_disc_rsp_t;
  202. /**@brief Event structure for @ref BLE_GATTC_EVT_DESC_DISC_RSP. */
  203. typedef struct
  204. {
  205. uint16_t count; /**< Descriptor count. */
  206. ble_gattc_desc_t descs[1]; /**< Descriptor data. @note This is a variable length array. The size of 1 indicated is only a placeholder for compilation.
  207. See @ref sd_ble_evt_get for more information on how to use event structures with variable length array members. */
  208. } ble_gattc_evt_desc_disc_rsp_t;
  209. /**@brief Event structure for @ref BLE_GATTC_EVT_ATTR_INFO_DISC_RSP. */
  210. typedef struct
  211. {
  212. uint16_t count; /**< Attribute count. */
  213. uint8_t format; /**< Attribute information format, see @ref BLE_GATTC_ATTR_INFO_FORMAT. */
  214. union {
  215. ble_gattc_attr_info16_t attr_info16[1]; /**< Attribute information for 16-bit Attribute UUID.
  216. @note This is a variable length array. The size of 1 indicated is only a placeholder for compilation.
  217. See @ref sd_ble_evt_get for more information on how to use event structures with variable length array members. */
  218. ble_gattc_attr_info128_t attr_info128[1]; /**< Attribute information for 128-bit Attribute UUID.
  219. @note This is a variable length array. The size of 1 indicated is only a placeholder for compilation.
  220. See @ref sd_ble_evt_get for more information on how to use event structures with variable length array members. */
  221. } info; /**< Attribute information union. */
  222. } ble_gattc_evt_attr_info_disc_rsp_t;
  223. /**@brief GATT read by UUID handle value pair. */
  224. typedef struct
  225. {
  226. uint16_t handle; /**< Attribute Handle. */
  227. uint8_t *p_value; /**< Pointer to the Attribute Value, length is available in @ref ble_gattc_evt_char_val_by_uuid_read_rsp_t::value_len. */
  228. } ble_gattc_handle_value_t;
  229. /**@brief Event structure for @ref BLE_GATTC_EVT_CHAR_VAL_BY_UUID_READ_RSP. */
  230. typedef struct
  231. {
  232. uint16_t count; /**< Handle-Value Pair Count. */
  233. uint16_t value_len; /**< Length of the value in Handle-Value(s) list. */
  234. uint8_t handle_value[1]; /**< Handle-Value(s) list. To iterate through the list use @ref sd_ble_gattc_evt_char_val_by_uuid_read_rsp_iter.
  235. @note This is a variable length array. The size of 1 indicated is only a placeholder for compilation.
  236. See @ref sd_ble_evt_get for more information on how to use event structures with variable length array members. */
  237. } ble_gattc_evt_char_val_by_uuid_read_rsp_t;
  238. /**@brief Event structure for @ref BLE_GATTC_EVT_READ_RSP. */
  239. typedef struct
  240. {
  241. uint16_t handle; /**< Attribute Handle. */
  242. uint16_t offset; /**< Offset of the attribute data. */
  243. uint16_t len; /**< Attribute data length. */
  244. uint8_t data[1]; /**< Attribute data. @note This is a variable length array. The size of 1 indicated is only a placeholder for compilation.
  245. See @ref sd_ble_evt_get for more information on how to use event structures with variable length array members. */
  246. } ble_gattc_evt_read_rsp_t;
  247. /**@brief Event structure for @ref BLE_GATTC_EVT_CHAR_VALS_READ_RSP. */
  248. typedef struct
  249. {
  250. uint16_t len; /**< Concatenated Attribute values length. */
  251. uint8_t values[1]; /**< Attribute values. @note This is a variable length array. The size of 1 indicated is only a placeholder for compilation.
  252. See @ref sd_ble_evt_get for more information on how to use event structures with variable length array members. */
  253. } ble_gattc_evt_char_vals_read_rsp_t;
  254. /**@brief Event structure for @ref BLE_GATTC_EVT_WRITE_RSP. */
  255. typedef struct
  256. {
  257. uint16_t handle; /**< Attribute Handle. */
  258. uint8_t write_op; /**< Type of write operation, see @ref BLE_GATT_WRITE_OPS. */
  259. uint16_t offset; /**< Data offset. */
  260. uint16_t len; /**< Data length. */
  261. uint8_t data[1]; /**< Data. @note This is a variable length array. The size of 1 indicated is only a placeholder for compilation.
  262. See @ref sd_ble_evt_get for more information on how to use event structures with variable length array members. */
  263. } ble_gattc_evt_write_rsp_t;
  264. /**@brief Event structure for @ref BLE_GATTC_EVT_HVX. */
  265. typedef struct
  266. {
  267. uint16_t handle; /**< Handle to which the HVx operation applies. */
  268. uint8_t type; /**< Indication or Notification, see @ref BLE_GATT_HVX_TYPES. */
  269. uint16_t len; /**< Attribute data length. */
  270. uint8_t data[1]; /**< Attribute data. @note This is a variable length array. The size of 1 indicated is only a placeholder for compilation.
  271. See @ref sd_ble_evt_get for more information on how to use event structures with variable length array members. */
  272. } ble_gattc_evt_hvx_t;
  273. /**@brief Event structure for @ref BLE_GATTC_EVT_EXCHANGE_MTU_RSP. */
  274. typedef struct
  275. {
  276. uint16_t server_rx_mtu; /**< Server RX MTU size. */
  277. } ble_gattc_evt_exchange_mtu_rsp_t;
  278. /**@brief Event structure for @ref BLE_GATTC_EVT_TIMEOUT. */
  279. typedef struct
  280. {
  281. uint8_t src; /**< Timeout source, see @ref BLE_GATT_TIMEOUT_SOURCES. */
  282. } ble_gattc_evt_timeout_t;
  283. /**@brief Event structure for @ref BLE_GATTC_EVT_WRITE_CMD_TX_COMPLETE. */
  284. typedef struct
  285. {
  286. uint8_t count; /**< Number of write without response transmissions completed. */
  287. } ble_gattc_evt_write_cmd_tx_complete_t;
  288. /**@brief GATTC event structure. */
  289. typedef struct
  290. {
  291. uint16_t conn_handle; /**< Connection Handle on which event occurred. */
  292. uint16_t gatt_status; /**< GATT status code for the operation, see @ref BLE_GATT_STATUS_CODES. */
  293. uint16_t error_handle; /**< In case of error: The handle causing the error. In all other cases @ref BLE_GATT_HANDLE_INVALID. */
  294. union
  295. {
  296. ble_gattc_evt_prim_srvc_disc_rsp_t prim_srvc_disc_rsp; /**< Primary Service Discovery Response Event Parameters. */
  297. ble_gattc_evt_rel_disc_rsp_t rel_disc_rsp; /**< Relationship Discovery Response Event Parameters. */
  298. ble_gattc_evt_char_disc_rsp_t char_disc_rsp; /**< Characteristic Discovery Response Event Parameters. */
  299. ble_gattc_evt_desc_disc_rsp_t desc_disc_rsp; /**< Descriptor Discovery Response Event Parameters. */
  300. ble_gattc_evt_char_val_by_uuid_read_rsp_t char_val_by_uuid_read_rsp; /**< Characteristic Value Read by UUID Response Event Parameters. */
  301. ble_gattc_evt_read_rsp_t read_rsp; /**< Read Response Event Parameters. */
  302. ble_gattc_evt_char_vals_read_rsp_t char_vals_read_rsp; /**< Characteristic Values Read Response Event Parameters. */
  303. ble_gattc_evt_write_rsp_t write_rsp; /**< Write Response Event Parameters. */
  304. ble_gattc_evt_hvx_t hvx; /**< Handle Value Notification/Indication Event Parameters. */
  305. ble_gattc_evt_exchange_mtu_rsp_t exchange_mtu_rsp; /**< Exchange MTU Response Event Parameters. */
  306. ble_gattc_evt_timeout_t timeout; /**< Timeout Event Parameters. */
  307. ble_gattc_evt_attr_info_disc_rsp_t attr_info_disc_rsp; /**< Attribute Information Discovery Event Parameters. */
  308. ble_gattc_evt_write_cmd_tx_complete_t write_cmd_tx_complete; /**< Write without Response transmission complete Event Parameters. */
  309. } params; /**< Event Parameters. @note Only valid if @ref gatt_status == @ref BLE_GATT_STATUS_SUCCESS. */
  310. } ble_gattc_evt_t;
  311. /**@brief UUID discovery option.
  312. *
  313. * @details Used with @ref sd_ble_opt_set to enable and disable automatic insertion of discovered 128-bit UUIDs to the
  314. * Vendor Specific UUID table. Disabled by default.
  315. * - When disabled, if a procedure initiated by
  316. * @ref sd_ble_gattc_primary_services_discover,
  317. * @ref sd_ble_gattc_relationships_discover,
  318. * @ref sd_ble_gattc_characteristics_discover,
  319. * @ref sd_ble_gattc_descriptors_discover
  320. * finds a 128-bit UUID which was not added by @ref sd_ble_uuid_vs_add, @ref ble_uuid_t::type will be set
  321. * to @ref BLE_UUID_TYPE_UNKNOWN in the corresponding event.
  322. * - When enabled, all found 128-bit UUIDs will be automatically added. The application can use
  323. * @ref sd_ble_uuid_encode to retrieve the 128-bit UUID from @ref ble_uuid_t received in the corresponding
  324. * event. If the total number of Vendor Specific UUIDs exceeds the table capacity, @ref ble_uuid_t::type will
  325. * be set to @ref BLE_UUID_TYPE_UNKNOWN in the corresponding event.
  326. * See also @ref ble_common_cfg_vs_uuid_t, @ref sd_ble_uuid_vs_remove.
  327. *
  328. * @note @ref sd_ble_opt_get is not supported for this option.
  329. *
  330. * @retval ::NRF_SUCCESS Set successfully.
  331. *
  332. */
  333. typedef struct
  334. {
  335. uint8_t auto_add_vs_enable : 1; /**< Set to 1 to enable (or 0 to disable) automatic insertion of discovered 128-bit UUIDs. */
  336. } ble_gattc_opt_uuid_disc_t;
  337. /**@brief Option structure for GATTC options. */
  338. typedef union
  339. {
  340. ble_gattc_opt_uuid_disc_t uuid_disc; /**< Parameters for the UUID discovery option. */
  341. } ble_gattc_opt_t;
  342. /** @} */
  343. /** @addtogroup BLE_GATTC_FUNCTIONS Functions
  344. * @{ */
  345. /**@brief Initiate or continue a GATT Primary Service Discovery procedure.
  346. *
  347. * @details This function initiates or resumes a Primary Service discovery procedure, starting from the supplied handle.
  348. * If the last service has not been reached, this function must be called again with an updated start handle value to continue the search.
  349. * See also @ref ble_gattc_opt_uuid_disc_t.
  350. *
  351. * @events
  352. * @event{@ref BLE_GATTC_EVT_PRIM_SRVC_DISC_RSP}
  353. * @endevents
  354. *
  355. * @mscs
  356. * @mmsc{@ref BLE_GATTC_PRIM_SRVC_DISC_MSC}
  357. * @endmscs
  358. *
  359. * @param[in] conn_handle The connection handle identifying the connection to perform this procedure on.
  360. * @param[in] start_handle Handle to start searching from.
  361. * @param[in] p_srvc_uuid Pointer to the service UUID to be found. If it is NULL, all primary services will be returned.
  362. *
  363. * @retval ::NRF_SUCCESS Successfully started or resumed the Primary Service Discovery procedure.
  364. * @retval ::BLE_ERROR_INVALID_CONN_HANDLE Invalid Connection Handle.
  365. * @retval ::NRF_ERROR_INVALID_STATE Invalid Connection State.
  366. * @retval ::NRF_ERROR_INVALID_PARAM Invalid parameter(s) supplied.
  367. * @retval ::NRF_ERROR_BUSY Client procedure already in progress.
  368. * @retval ::NRF_ERROR_TIMEOUT There has been a GATT procedure timeout. No new GATT procedure can be performed without reestablishing the connection.
  369. */
  370. SVCALL(SD_BLE_GATTC_PRIMARY_SERVICES_DISCOVER, uint32_t, sd_ble_gattc_primary_services_discover(uint16_t conn_handle, uint16_t start_handle, ble_uuid_t const *p_srvc_uuid));
  371. /**@brief Initiate or continue a GATT Relationship Discovery procedure.
  372. *
  373. * @details This function initiates or resumes the Find Included Services sub-procedure. If the last included service has not been reached,
  374. * this must be called again with an updated handle range to continue the search.
  375. * See also @ref ble_gattc_opt_uuid_disc_t.
  376. *
  377. * @events
  378. * @event{@ref BLE_GATTC_EVT_REL_DISC_RSP}
  379. * @endevents
  380. *
  381. * @mscs
  382. * @mmsc{@ref BLE_GATTC_REL_DISC_MSC}
  383. * @endmscs
  384. *
  385. * @param[in] conn_handle The connection handle identifying the connection to perform this procedure on.
  386. * @param[in] p_handle_range A pointer to the range of handles of the Service to perform this procedure on.
  387. *
  388. * @retval ::NRF_SUCCESS Successfully started or resumed the Relationship Discovery procedure.
  389. * @retval ::BLE_ERROR_INVALID_CONN_HANDLE Invalid Connection Handle.
  390. * @retval ::NRF_ERROR_INVALID_STATE Invalid Connection State.
  391. * @retval ::NRF_ERROR_INVALID_ADDR Invalid pointer supplied.
  392. * @retval ::NRF_ERROR_INVALID_PARAM Invalid parameter(s) supplied.
  393. * @retval ::NRF_ERROR_BUSY Client procedure already in progress.
  394. * @retval ::NRF_ERROR_TIMEOUT There has been a GATT procedure timeout. No new GATT procedure can be performed without reestablishing the connection.
  395. */
  396. SVCALL(SD_BLE_GATTC_RELATIONSHIPS_DISCOVER, uint32_t, sd_ble_gattc_relationships_discover(uint16_t conn_handle, ble_gattc_handle_range_t const *p_handle_range));
  397. /**@brief Initiate or continue a GATT Characteristic Discovery procedure.
  398. *
  399. * @details This function initiates or resumes a Characteristic discovery procedure. If the last Characteristic has not been reached,
  400. * this must be called again with an updated handle range to continue the discovery.
  401. * See also @ref ble_gattc_opt_uuid_disc_t.
  402. *
  403. * @events
  404. * @event{@ref BLE_GATTC_EVT_CHAR_DISC_RSP}
  405. * @endevents
  406. *
  407. * @mscs
  408. * @mmsc{@ref BLE_GATTC_CHAR_DISC_MSC}
  409. * @endmscs
  410. *
  411. * @param[in] conn_handle The connection handle identifying the connection to perform this procedure on.
  412. * @param[in] p_handle_range A pointer to the range of handles of the Service to perform this procedure on.
  413. *
  414. * @retval ::NRF_SUCCESS Successfully started or resumed the Characteristic Discovery procedure.
  415. * @retval ::BLE_ERROR_INVALID_CONN_HANDLE Invalid Connection Handle.
  416. * @retval ::NRF_ERROR_INVALID_STATE Invalid Connection State.
  417. * @retval ::NRF_ERROR_INVALID_ADDR Invalid pointer supplied.
  418. * @retval ::NRF_ERROR_BUSY Client procedure already in progress.
  419. * @retval ::NRF_ERROR_TIMEOUT There has been a GATT procedure timeout. No new GATT procedure can be performed without reestablishing the connection.
  420. */
  421. SVCALL(SD_BLE_GATTC_CHARACTERISTICS_DISCOVER, uint32_t, sd_ble_gattc_characteristics_discover(uint16_t conn_handle, ble_gattc_handle_range_t const *p_handle_range));
  422. /**@brief Initiate or continue a GATT Characteristic Descriptor Discovery procedure.
  423. *
  424. * @details This function initiates or resumes a Characteristic Descriptor discovery procedure. If the last Descriptor has not been reached,
  425. * this must be called again with an updated handle range to continue the discovery.
  426. * See also @ref ble_gattc_opt_uuid_disc_t.
  427. *
  428. * @events
  429. * @event{@ref BLE_GATTC_EVT_DESC_DISC_RSP}
  430. * @endevents
  431. *
  432. * @mscs
  433. * @mmsc{@ref BLE_GATTC_DESC_DISC_MSC}
  434. * @endmscs
  435. *
  436. * @param[in] conn_handle The connection handle identifying the connection to perform this procedure on.
  437. * @param[in] p_handle_range A pointer to the range of handles of the Characteristic to perform this procedure on.
  438. *
  439. * @retval ::NRF_SUCCESS Successfully started or resumed the Descriptor Discovery procedure.
  440. * @retval ::BLE_ERROR_INVALID_CONN_HANDLE Invalid Connection Handle.
  441. * @retval ::NRF_ERROR_INVALID_STATE Invalid Connection State.
  442. * @retval ::NRF_ERROR_INVALID_ADDR Invalid pointer supplied.
  443. * @retval ::NRF_ERROR_BUSY Client procedure already in progress.
  444. * @retval ::NRF_ERROR_TIMEOUT There has been a GATT procedure timeout. No new GATT procedure can be performed without reestablishing the connection.
  445. */
  446. SVCALL(SD_BLE_GATTC_DESCRIPTORS_DISCOVER, uint32_t, sd_ble_gattc_descriptors_discover(uint16_t conn_handle, ble_gattc_handle_range_t const *p_handle_range));
  447. /**@brief Initiate or continue a GATT Read using Characteristic UUID procedure.
  448. *
  449. * @details This function initiates or resumes a Read using Characteristic UUID procedure. If the last Characteristic has not been reached,
  450. * this must be called again with an updated handle range to continue the discovery.
  451. *
  452. * @events
  453. * @event{@ref BLE_GATTC_EVT_CHAR_VAL_BY_UUID_READ_RSP}
  454. * @endevents
  455. *
  456. * @mscs
  457. * @mmsc{@ref BLE_GATTC_READ_UUID_MSC}
  458. * @endmscs
  459. *
  460. * @param[in] conn_handle The connection handle identifying the connection to perform this procedure on.
  461. * @param[in] p_uuid Pointer to a Characteristic value UUID to read.
  462. * @param[in] p_handle_range A pointer to the range of handles to perform this procedure on.
  463. *
  464. * @retval ::NRF_SUCCESS Successfully started or resumed the Read using Characteristic UUID procedure.
  465. * @retval ::BLE_ERROR_INVALID_CONN_HANDLE Invalid Connection Handle.
  466. * @retval ::NRF_ERROR_INVALID_STATE Invalid Connection State.
  467. * @retval ::NRF_ERROR_INVALID_ADDR Invalid pointer supplied.
  468. * @retval ::NRF_ERROR_BUSY Client procedure already in progress.
  469. * @retval ::NRF_ERROR_TIMEOUT There has been a GATT procedure timeout. No new GATT procedure can be performed without reestablishing the connection.
  470. */
  471. SVCALL(SD_BLE_GATTC_CHAR_VALUE_BY_UUID_READ, uint32_t, sd_ble_gattc_char_value_by_uuid_read(uint16_t conn_handle, ble_uuid_t const *p_uuid, ble_gattc_handle_range_t const *p_handle_range));
  472. /**@brief Initiate or continue a GATT Read (Long) Characteristic or Descriptor procedure.
  473. *
  474. * @details This function initiates or resumes a GATT Read (Long) Characteristic or Descriptor procedure. If the Characteristic or Descriptor
  475. * to be read is longer than ATT_MTU - 1, this function must be called multiple times with appropriate offset to read the
  476. * complete value.
  477. *
  478. * @events
  479. * @event{@ref BLE_GATTC_EVT_READ_RSP}
  480. * @endevents
  481. *
  482. * @mscs
  483. * @mmsc{@ref BLE_GATTC_VALUE_READ_MSC}
  484. * @endmscs
  485. *
  486. * @param[in] conn_handle The connection handle identifying the connection to perform this procedure on.
  487. * @param[in] handle The handle of the attribute to be read.
  488. * @param[in] offset Offset into the attribute value to be read.
  489. *
  490. * @retval ::NRF_SUCCESS Successfully started or resumed the Read (Long) procedure.
  491. * @retval ::BLE_ERROR_INVALID_CONN_HANDLE Invalid Connection Handle.
  492. * @retval ::NRF_ERROR_INVALID_STATE Invalid Connection State.
  493. * @retval ::NRF_ERROR_BUSY Client procedure already in progress.
  494. * @retval ::NRF_ERROR_TIMEOUT There has been a GATT procedure timeout. No new GATT procedure can be performed without reestablishing the connection.
  495. */
  496. SVCALL(SD_BLE_GATTC_READ, uint32_t, sd_ble_gattc_read(uint16_t conn_handle, uint16_t handle, uint16_t offset));
  497. /**@brief Initiate a GATT Read Multiple Characteristic Values procedure.
  498. *
  499. * @details This function initiates a GATT Read Multiple Characteristic Values procedure.
  500. *
  501. * @events
  502. * @event{@ref BLE_GATTC_EVT_CHAR_VALS_READ_RSP}
  503. * @endevents
  504. *
  505. * @mscs
  506. * @mmsc{@ref BLE_GATTC_READ_MULT_MSC}
  507. * @endmscs
  508. *
  509. * @param[in] conn_handle The connection handle identifying the connection to perform this procedure on.
  510. * @param[in] p_handles A pointer to the handle(s) of the attribute(s) to be read.
  511. * @param[in] handle_count The number of handles in p_handles.
  512. *
  513. * @retval ::NRF_SUCCESS Successfully started the Read Multiple Characteristic Values procedure.
  514. * @retval ::BLE_ERROR_INVALID_CONN_HANDLE Invalid Connection Handle.
  515. * @retval ::NRF_ERROR_INVALID_STATE Invalid Connection State.
  516. * @retval ::NRF_ERROR_INVALID_ADDR Invalid pointer supplied.
  517. * @retval ::NRF_ERROR_BUSY Client procedure already in progress.
  518. * @retval ::NRF_ERROR_TIMEOUT There has been a GATT procedure timeout. No new GATT procedure can be performed without reestablishing the connection.
  519. */
  520. SVCALL(SD_BLE_GATTC_CHAR_VALUES_READ, uint32_t, sd_ble_gattc_char_values_read(uint16_t conn_handle, uint16_t const *p_handles, uint16_t handle_count));
  521. /**@brief Perform a Write (Characteristic Value or Descriptor, with or without response, signed or not, long or reliable) procedure.
  522. *
  523. * @details This function can perform all write procedures described in GATT.
  524. *
  525. * @note Only one write with response procedure can be ongoing per connection at a time.
  526. * If the application tries to write with response while another write with response procedure is ongoing,
  527. * the function call will return @ref NRF_ERROR_BUSY.
  528. * A @ref BLE_GATTC_EVT_WRITE_RSP event will be issued as soon as the write response arrives from the peer.
  529. *
  530. * @note The number of Write without Response that can be queued is configured by @ref ble_gattc_conn_cfg_t::write_cmd_tx_queue_size
  531. * When the queue is full, the function call will return @ref NRF_ERROR_RESOURCES.
  532. * A @ref BLE_GATTC_EVT_WRITE_CMD_TX_COMPLETE event will be issued as soon as the transmission of the write without response is complete.
  533. *
  534. * @note The application can keep track of the available queue element count for writes without responses by following the procedure below:
  535. * - Store initial queue element count in a variable.
  536. * - Decrement the variable, which stores the currently available queue element count, by one when a call to this function returns @ref NRF_SUCCESS.
  537. * - Increment the variable, which stores the current available queue element count, by the count variable in @ref BLE_GATTC_EVT_WRITE_CMD_TX_COMPLETE event.
  538. *
  539. * @events
  540. * @event{@ref BLE_GATTC_EVT_WRITE_CMD_TX_COMPLETE, Write without response transmission complete.}
  541. * @event{@ref BLE_GATTC_EVT_WRITE_RSP, Write response received from the peer.}
  542. * @endevents
  543. *
  544. * @mscs
  545. * @mmsc{@ref BLE_GATTC_VALUE_WRITE_WITHOUT_RESP_MSC}
  546. * @mmsc{@ref BLE_GATTC_VALUE_WRITE_MSC}
  547. * @mmsc{@ref BLE_GATTC_VALUE_LONG_WRITE_MSC}
  548. * @mmsc{@ref BLE_GATTC_VALUE_RELIABLE_WRITE_MSC}
  549. * @endmscs
  550. *
  551. * @param[in] conn_handle The connection handle identifying the connection to perform this procedure on.
  552. * @param[in] p_write_params A pointer to a write parameters structure.
  553. *
  554. * @retval ::NRF_SUCCESS Successfully started the Write procedure.
  555. * @retval ::BLE_ERROR_INVALID_CONN_HANDLE Invalid Connection Handle.
  556. * @retval ::NRF_ERROR_INVALID_STATE Invalid Connection State.
  557. * @retval ::NRF_ERROR_INVALID_ADDR Invalid pointer supplied.
  558. * @retval ::NRF_ERROR_INVALID_PARAM Invalid parameter(s) supplied.
  559. * @retval ::NRF_ERROR_DATA_SIZE Invalid data size(s) supplied.
  560. * @retval ::NRF_ERROR_BUSY For write with response, procedure already in progress. Wait for a @ref BLE_GATTC_EVT_WRITE_RSP event and retry.
  561. * @retval ::NRF_ERROR_RESOURCES Too many writes without responses queued.
  562. * Wait for a @ref BLE_GATTC_EVT_WRITE_CMD_TX_COMPLETE event and retry.
  563. * @retval ::NRF_ERROR_TIMEOUT There has been a GATT procedure timeout. No new GATT procedure can be performed without reestablishing the connection.
  564. */
  565. SVCALL(SD_BLE_GATTC_WRITE, uint32_t, sd_ble_gattc_write(uint16_t conn_handle, ble_gattc_write_params_t const *p_write_params));
  566. /**@brief Send a Handle Value Confirmation to the GATT Server.
  567. *
  568. * @mscs
  569. * @mmsc{@ref BLE_GATTC_HVI_MSC}
  570. * @endmscs
  571. *
  572. * @param[in] conn_handle The connection handle identifying the connection to perform this procedure on.
  573. * @param[in] handle The handle of the attribute in the indication.
  574. *
  575. * @retval ::NRF_SUCCESS Successfully queued the Handle Value Confirmation for transmission.
  576. * @retval ::BLE_ERROR_INVALID_CONN_HANDLE Invalid Connection Handle.
  577. * @retval ::NRF_ERROR_INVALID_STATE Invalid Connection State or no Indication pending to be confirmed.
  578. * @retval ::BLE_ERROR_INVALID_ATTR_HANDLE Invalid attribute handle.
  579. * @retval ::NRF_ERROR_TIMEOUT There has been a GATT procedure timeout. No new GATT procedure can be performed without reestablishing the connection.
  580. */
  581. SVCALL(SD_BLE_GATTC_HV_CONFIRM, uint32_t, sd_ble_gattc_hv_confirm(uint16_t conn_handle, uint16_t handle));
  582. /**@brief Discovers information about a range of attributes on a GATT server.
  583. *
  584. * @events
  585. * @event{@ref BLE_GATTC_EVT_ATTR_INFO_DISC_RSP, Generated when information about a range of attributes has been received.}
  586. * @endevents
  587. *
  588. * @param[in] conn_handle The connection handle identifying the connection to perform this procedure on.
  589. * @param[in] p_handle_range The range of handles to request information about.
  590. *
  591. * @retval ::NRF_SUCCESS Successfully started an attribute information discovery procedure.
  592. * @retval ::BLE_ERROR_INVALID_CONN_HANDLE Invalid connection handle.
  593. * @retval ::NRF_ERROR_INVALID_STATE Invalid connection state
  594. * @retval ::NRF_ERROR_INVALID_ADDR Invalid pointer supplied.
  595. * @retval ::NRF_ERROR_BUSY Client procedure already in progress.
  596. * @retval ::NRF_ERROR_TIMEOUT There has been a GATT procedure timeout. No new GATT procedure can be performed without reestablishing the connection.
  597. */
  598. SVCALL(SD_BLE_GATTC_ATTR_INFO_DISCOVER, uint32_t, sd_ble_gattc_attr_info_discover(uint16_t conn_handle, ble_gattc_handle_range_t const * p_handle_range));
  599. /**@brief Start an ATT_MTU exchange by sending an Exchange MTU Request to the server.
  600. *
  601. * @details The SoftDevice sets ATT_MTU to the minimum of:
  602. * - The Client RX MTU value, and
  603. * - The Server RX MTU value from @ref BLE_GATTC_EVT_EXCHANGE_MTU_RSP.
  604. *
  605. * However, the SoftDevice never sets ATT_MTU lower than @ref BLE_GATT_ATT_MTU_DEFAULT.
  606. *
  607. * @events
  608. * @event{@ref BLE_GATTC_EVT_EXCHANGE_MTU_RSP}
  609. * @endevents
  610. *
  611. * @mscs
  612. * @mmsc{@ref BLE_GATTC_MTU_EXCHANGE}
  613. * @endmscs
  614. *
  615. * @param[in] conn_handle The connection handle identifying the connection to perform this procedure on.
  616. * @param[in] client_rx_mtu Client RX MTU size.
  617. * - The minimum value is @ref BLE_GATT_ATT_MTU_DEFAULT.
  618. * - The maximum value is @ref ble_gatt_conn_cfg_t::att_mtu in the connection configuration
  619. used for this connection.
  620. * - The value must be equal to Server RX MTU size given in @ref sd_ble_gatts_exchange_mtu_reply
  621. * if an ATT_MTU exchange has already been performed in the other direction.
  622. *
  623. * @retval ::NRF_SUCCESS Successfully sent request to the server.
  624. * @retval ::BLE_ERROR_INVALID_CONN_HANDLE Invalid connection handle.
  625. * @retval ::NRF_ERROR_INVALID_STATE Invalid connection state or an ATT_MTU exchange was already requested once.
  626. * @retval ::NRF_ERROR_INVALID_PARAM Invalid Client RX MTU size supplied.
  627. * @retval ::NRF_ERROR_BUSY Client procedure already in progress.
  628. * @retval ::NRF_ERROR_TIMEOUT There has been a GATT procedure timeout. No new GATT procedure can be performed without reestablishing the connection.
  629. */
  630. SVCALL(SD_BLE_GATTC_EXCHANGE_MTU_REQUEST, uint32_t, sd_ble_gattc_exchange_mtu_request(uint16_t conn_handle, uint16_t client_rx_mtu));
  631. /**@brief Iterate through Handle-Value(s) list in @ref BLE_GATTC_EVT_CHAR_VAL_BY_UUID_READ_RSP event.
  632. *
  633. * @param[in] p_gattc_evt Pointer to event buffer containing @ref BLE_GATTC_EVT_CHAR_VAL_BY_UUID_READ_RSP event.
  634. * @note If the buffer contains different event, behavior is undefined.
  635. * @param[in,out] p_iter Iterator, points to @ref ble_gattc_handle_value_t structure that will be filled in with
  636. * the next Handle-Value pair in each iteration. If the function returns other than
  637. * @ref NRF_SUCCESS, it will not be changed.
  638. * - To start iteration, initialize the structure to zero.
  639. * - To continue, pass the value from previous iteration.
  640. *
  641. * \code
  642. * ble_gattc_handle_value_t iter;
  643. * memset(&iter, 0, sizeof(ble_gattc_handle_value_t));
  644. * while (sd_ble_gattc_evt_char_val_by_uuid_read_rsp_iter(&ble_evt.evt.gattc_evt, &iter) == NRF_SUCCESS)
  645. * {
  646. * app_handle = iter.handle;
  647. * memcpy(app_value, iter.p_value, ble_evt.evt.gattc_evt.params.char_val_by_uuid_read_rsp.value_len);
  648. * }
  649. * \endcode
  650. *
  651. * @retval ::NRF_SUCCESS Successfully retrieved the next Handle-Value pair.
  652. * @retval ::NRF_ERROR_NOT_FOUND No more Handle-Value pairs available in the list.
  653. */
  654. __STATIC_INLINE uint32_t sd_ble_gattc_evt_char_val_by_uuid_read_rsp_iter(ble_gattc_evt_t *p_gattc_evt, ble_gattc_handle_value_t *p_iter);
  655. /** @} */
  656. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  657. __STATIC_INLINE uint32_t sd_ble_gattc_evt_char_val_by_uuid_read_rsp_iter(ble_gattc_evt_t *p_gattc_evt, ble_gattc_handle_value_t *p_iter)
  658. {
  659. uint32_t value_len = p_gattc_evt->params.char_val_by_uuid_read_rsp.value_len;
  660. uint8_t *p_first = p_gattc_evt->params.char_val_by_uuid_read_rsp.handle_value;
  661. uint8_t *p_next = p_iter->p_value ? p_iter->p_value + value_len : p_first;
  662. if ((p_next - p_first) / (sizeof(uint16_t) + value_len) < p_gattc_evt->params.char_val_by_uuid_read_rsp.count)
  663. {
  664. p_iter->handle = (uint16_t)p_next[1] << 8 | p_next[0];
  665. p_iter->p_value = p_next + sizeof(uint16_t);
  666. return NRF_SUCCESS;
  667. }
  668. else
  669. {
  670. return NRF_ERROR_NOT_FOUND;
  671. }
  672. }
  673. #endif /* SUPPRESS_INLINE_IMPLEMENTATION */
  674. #ifdef __cplusplus
  675. }
  676. #endif
  677. #endif /* BLE_GATTC_H__ */
  678. /**
  679. @}
  680. */