ble_dis_c.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /**
  2. * Copyright (c) 2017 - 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. /**@file
  41. *
  42. * @defgroup ble_dis_c Device Information Service Client
  43. * @{
  44. * @ingroup ble_sdk_srv
  45. * @brief Device information Service Client module.
  46. *
  47. * @details This module contains the APIs and types exposed by the Device information Service Client
  48. * module. These APIs and types can be used by the application to perform discovery of
  49. * the Device information Service at the peer and interact with it.
  50. *
  51. * @note The application must register this module as BLE event observer using the
  52. * NRF_SDH_BLE_OBSERVER macro. Example:
  53. * @code
  54. * ble_dis_c_t instance;
  55. * NRF_SDH_BLE_OBSERVER(anything, BLE_DIS_C_BLE_OBSERVER_PRIO,
  56. * ble_dis_c_on_ble_evt, &instance);
  57. * @endcode
  58. *
  59. */
  60. #ifndef BLE_DIS_C_H__
  61. #define BLE_DIS_C_H__
  62. #include <stdint.h>
  63. #include <stdbool.h>
  64. #include "ble.h"
  65. #include "ble_gatt.h"
  66. #include "ble_db_discovery.h"
  67. #include "ble_srv_common.h"
  68. #include "ble_dis.h"
  69. #include "nrf_ble_gq.h"
  70. #include "nrf_sdh_ble.h"
  71. #include "sdk_config.h"
  72. #ifdef __cplusplus
  73. extern "C" {
  74. #endif
  75. /**@brief Macro for defining a ble_dis_c instance.
  76. *
  77. * @param _name Name of the instance.
  78. * @hideinitializer
  79. */
  80. #define BLE_DIS_C_DEF(_name) \
  81. static ble_dis_c_t _name; \
  82. NRF_SDH_BLE_OBSERVER(_name ## _obs, \
  83. BLE_DIS_C_BLE_OBSERVER_PRIO, \
  84. ble_dis_c_on_ble_evt, &_name)
  85. /** @brief Macro for defining multiple ble_dis_c instances.
  86. *
  87. * @param _name Name of the array of instances.
  88. * @param _cnt Number of instances to define.
  89. * @hideinitializer
  90. */
  91. #define BLE_DIS_C_ARRAY_DEF(_name, _cnt) \
  92. static ble_dis_c_t _name[_cnt]; \
  93. NRF_SDH_BLE_OBSERVERS(_name ## _obs, \
  94. BLE_DIS_C_BLE_OBSERVER_PRIO, \
  95. ble_dis_c_on_ble_evt, &_name, _cnt)
  96. /**@brief DIS Client event type. */
  97. typedef enum
  98. {
  99. BLE_DIS_C_EVT_DISCOVERY_COMPLETE, /**< Event indicating that the DIS and its characteristics were discovered. See @ref ble_dis_c_evt_disc_complete_t. */
  100. BLE_DIS_C_EVT_DIS_C_READ_RSP, /**< Event indicating that the client has received a read response from a peer. See @ref ble_dis_c_evt_read_rsp_t. */
  101. BLE_DIS_C_EVT_DIS_C_READ_RSP_ERROR, /**< Event indicating that the client's read request has failed. See @ref ble_dis_c_evt_read_rsp_err_t. */
  102. BLE_DIS_C_EVT_DISCONNECTED /**< Event indicating that the DIS server has disconnected. */
  103. } ble_dis_c_evt_type_t;
  104. /**@brief DIS Client characteristic type. */
  105. typedef enum
  106. {
  107. BLE_DIS_C_MANUF_NAME, /**< Manufacturer Name String characteristic. */
  108. BLE_DIS_C_MODEL_NUM, /**< Model Number String characteristic. */
  109. BLE_DIS_C_SERIAL_NUM, /**< Serial Number String characteristic. */
  110. BLE_DIS_C_HW_REV, /**< Hardware Revision String characteristic. */
  111. BLE_DIS_C_FW_REV, /**< Firmware Revision String characteristic. */
  112. BLE_DIS_C_SW_REV, /**< Software Revision String characteristic. */
  113. BLE_DIS_C_SYS_ID, /**< System ID characteristic. */
  114. BLE_DIS_C_CERT_LIST, /**< IEEE 11073-20601 Regulatory Certification Data List characteristic. */
  115. BLE_DIS_C_PNP_ID, /**< PnP ID characteristic. */
  116. BLE_DIS_C_CHAR_TYPES_NUM /**< Number of all possible characteristic types. */
  117. } ble_dis_c_char_type_t;
  118. /**@brief Attribute handle pointing to DIS characteristics on the connected peer device. */
  119. typedef uint16_t ble_dis_c_handle_t;
  120. /**@brief Event structure for @ref BLE_DIS_C_EVT_DISCOVERY_COMPLETE. */
  121. typedef struct
  122. {
  123. ble_dis_c_handle_t handles[BLE_DIS_C_CHAR_TYPES_NUM]; /**< Handles on the connected peer device needed to interact with it. */
  124. } ble_dis_c_evt_disc_complete_t;
  125. /**@brief Response data for string-based DIS characteristics. */
  126. typedef struct
  127. {
  128. uint8_t * p_data; /**< Pointer to response data. */
  129. uint8_t len; /**< Response data length. */
  130. } ble_dis_c_string_t;
  131. /**@brief Event structure for @ref BLE_DIS_C_EVT_DIS_C_READ_RSP. */
  132. typedef struct
  133. {
  134. ble_dis_c_char_type_t char_type; /**< Characteristic type. */
  135. ble_dis_c_handle_t handle; /**< Attribute handle from the response event. */
  136. union
  137. {
  138. ble_dis_c_string_t string; /**< String-based characteristics response data. Filled when char_type is in the following range: @ref BLE_DIS_C_MANUF_NAME - @ref BLE_DIS_C_SW_REV (inclusive). */
  139. ble_dis_sys_id_t sys_id; /**< System ID characteristic response data. Filled when char_type is @ref BLE_DIS_C_SYS_ID. */
  140. ble_dis_reg_cert_data_list_t cert_list; /**< IEEE 11073-20601 Regulatory Certification Data List characteristic response data. Filled when char_type is @ref BLE_DIS_C_CERT_LIST. */
  141. ble_dis_pnp_id_t pnp_id; /**< PnP ID characteristic response data. Filled when char_type is @ref BLE_DIS_C_PNP_ID. */
  142. } content;
  143. } ble_dis_c_evt_read_rsp_t;
  144. /**@brief Event structure for @ref BLE_DIS_C_EVT_DIS_C_READ_RSP_ERROR. */
  145. typedef struct
  146. {
  147. ble_dis_c_char_type_t char_type; /**< Characteristic type. */
  148. ble_dis_c_handle_t err_handle; /**< Attribute handle from the response event. */
  149. uint16_t gatt_status; /**< GATT status code for the read operation, see @ref BLE_GATT_STATUS_CODES. */
  150. } ble_dis_c_evt_read_rsp_err_t;
  151. /**@brief Structure containing the DIS event data received from the peer. */
  152. typedef struct
  153. {
  154. ble_dis_c_evt_type_t evt_type; /**< Type of the event. */
  155. uint16_t conn_handle; /**< Connection handle on which the @ref ble_dis_c_evt_t event occurred.*/
  156. union
  157. {
  158. ble_dis_c_evt_disc_complete_t disc_complete; /**< Discovery Complete Event Parameters. Filled when evt_type is @ref BLE_DIS_C_EVT_DISCOVERY_COMPLETE. */
  159. ble_dis_c_evt_read_rsp_t read_rsp; /**< Read Response Event Parameters. Filled when evt_type is @ref BLE_DIS_C_EVT_DIS_C_READ_RSP. */
  160. ble_dis_c_evt_read_rsp_err_t read_rsp_err; /**< Read Response Error Event Parameters. Filled when evt_type is @ref BLE_DIS_C_EVT_DIS_C_READ_RSP_ERROR. */
  161. } params;
  162. } ble_dis_c_evt_t;
  163. // Forward declaration of the ble_dis_t type.
  164. typedef struct ble_dis_c_s ble_dis_c_t;
  165. /**@brief Event handler type.
  166. *
  167. * @details This is the type of the event handler that should be provided by the application
  168. * of this module to receive events.
  169. */
  170. typedef void (* ble_dis_c_evt_handler_t)(ble_dis_c_t * p_ble_dis_c, ble_dis_c_evt_t const * p_evt);
  171. /**@brief DIS Client structure. */
  172. struct ble_dis_c_s
  173. {
  174. uint16_t conn_handle; /**< Handle of the current connection. Set with @ref ble_dis_c_handles_assign when connected. */
  175. uint16_t char_mask; /**< Mask with enabled DIS characteristics.*/
  176. ble_dis_c_handle_t handles[BLE_DIS_C_CHAR_TYPES_NUM]; /**< Handles on the connected peer device needed to interact with it. */
  177. ble_srv_error_handler_t error_handler; /**< Application error handler to be called in case of an error. */
  178. ble_dis_c_evt_handler_t evt_handler; /**< Application event handler to be called when there is an event related to the DIS. */
  179. nrf_ble_gq_t * p_gatt_queue; /**< Pointer to BLE GATT Queue instance. */
  180. };
  181. /**@brief Structure describing the group of DIS characteristics with which this module can interact. */
  182. typedef struct
  183. {
  184. ble_dis_c_char_type_t * p_char_type; /**< Pointer to array with selected characteristics. */
  185. uint8_t size; /**< Group size. */
  186. } ble_dis_c_char_group_t;
  187. /**@brief DIS Client initialization structure. */
  188. typedef struct
  189. {
  190. ble_dis_c_char_group_t char_group; /**< Group of DIS characteristics that should be enabled for this module instance. */
  191. ble_srv_error_handler_t error_handler; /**< Application error handler to be called in case of an error. */
  192. ble_dis_c_evt_handler_t evt_handler; /**< Application event handler to be called when there is an event related to the Device Information service. */
  193. nrf_ble_gq_t * p_gatt_queue; /**< Pointer to BLE GATT Queue instance. */
  194. } ble_dis_c_init_t;
  195. /**@brief Function for initializing the Device Information service client module.
  196. *
  197. * @details This function registers with the Database Discovery module
  198. * for the DIS. Doing so will make the Database Discovery
  199. * module look for the presence of a DIS instance at the peer when a
  200. * discovery is started.
  201. *
  202. * @param[in] p_ble_dis_c Pointer to the DIS client structure.
  203. * @param[in] p_ble_dis_c_init Pointer to the DIS initialization structure containing the
  204. * initialization information.
  205. *
  206. * @retval NRF_SUCCESS If the module was initialized successfully.
  207. * @retval NRF_ERROR_NULL Any parameter is NULL.
  208. * @return If functions from other modules return errors to this function
  209. * (@ref ble_db_discovery_evt_register), the @ref nrf_error are propagated.
  210. */
  211. ret_code_t ble_dis_c_init(ble_dis_c_t * p_ble_dis_c, ble_dis_c_init_t * p_ble_dis_c_init);
  212. /**@brief Function for handling events from the database discovery module.
  213. *
  214. * @details This function will handle an event from the database discovery module, and determine
  215. * if it relates to the discovery of DIS at the peer. If so, it will
  216. * call the application's event handler indicating that DIS has been
  217. * discovered at the peer. It also populates the event with the service related
  218. * information before providing it to the application.
  219. *
  220. * @param[in] p_ble_dis_c Pointer to the DIS client structure.
  221. * @param[in] p_evt Pointer to the event received from the database discovery module.
  222. */
  223. void ble_dis_c_on_db_disc_evt(ble_dis_c_t * p_ble_dis_c, ble_db_discovery_evt_t * p_evt);
  224. /**@brief Function for handling BLE events from the SoftDevice.
  225. *
  226. * @details This function handles the BLE events received from the SoftDevice. If a BLE
  227. * event is relevant to the DIS module, it is used to update internal variables
  228. * and, if necessary, send events to the application.
  229. *
  230. * @param[in] p_ble_evt Pointer to the BLE event.
  231. * @param[in] p_context Pointer to the DIS client structure.
  232. */
  233. void ble_dis_c_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context);
  234. /**@brief Function for reading different characteristics from DIS.
  235. *
  236. * @details This function can be used to read different characteristics that are available
  237. * inside DIS. The response data will be provided from the response event
  238. * @ref BLE_DIS_C_EVT_DIS_C_READ_RSP. The @ref BLE_DIS_C_EVT_DIS_C_READ_RSP_ERROR
  239. * event can be generated if the read operation is unsuccessful.
  240. *
  241. * @param[in] p_ble_dis_c Pointer to the DIS client structure.
  242. * @param[in] char_type Type of characteristic to read.
  243. *
  244. * @retval NRF_SUCCESS If the operation was successful.
  245. * @retval NRF_ERROR_NULL If a \p p_ble_dis_c was a NULL pointer.
  246. * @retval NRF_ERROR_INVALID_PARAM If a \p char_type is not valid.
  247. * @retval NRF_ERROR_INVALID_STATE If connection handle or attribute handle is invalid.
  248. * @retval NRF_ERROR_NO_MEM If the client request queue is full.
  249. */
  250. ret_code_t ble_dis_c_read(ble_dis_c_t * p_ble_dis_c, ble_dis_c_char_type_t char_type);
  251. /**@brief Function for assigning handles to this instance of dis_c.
  252. *
  253. * @details Call this function when a link has been established with a peer to
  254. * associate this link to this instance of the module. This makes it
  255. * possible to handle several links and associate each link to a particular
  256. * instance of this module. The connection handle and attribute handles will be
  257. * provided from the discovery event @ref BLE_DIS_C_EVT_DISCOVERY_COMPLETE.
  258. *
  259. * @param[in] p_ble_dis_c Pointer to the DIS client structure instance to associate with these
  260. * handles.
  261. * @param[in] conn_handle Connection handle associated with the given DIS Instance.
  262. * @param[in] p_peer_handles Attribute handles on the DIS server that you want this DIS client to
  263. * interact with.
  264. *
  265. * @retval NRF_SUCCESS If the operation was successful.
  266. * @retval NRF_ERROR_NULL If a \p p_ble_dis_c was a NULL pointer.
  267. */
  268. ret_code_t ble_dis_c_handles_assign(ble_dis_c_t * p_ble_dis_c,
  269. uint16_t conn_handle,
  270. ble_dis_c_handle_t const * p_peer_handles);
  271. #ifdef __cplusplus
  272. }
  273. #endif
  274. #endif // BLE_DIS_C_H__
  275. /** @} */