ble_hrs_c.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /**
  2. * Copyright (c) 2012 - 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_hrs_c Heart Rate Service Client
  43. * @{
  44. * @ingroup ble_sdk_srv
  45. * @brief Heart Rate Service Client module.
  46. *
  47. * @details This module contains the APIs and types exposed by the Heart Rate Service Client
  48. * module. The application can use these APIs and types to perform the discovery of
  49. * Heart Rate Service at the peer and to interact with it.
  50. *
  51. * @warning Currently, this module only supports the Heart Rate Measurement characteristic. This
  52. * means that it is able to enable notification of the characteristic at the peer and
  53. * is able to receive Heart Rate Measurement notifications from the peer. It does not
  54. * support the Body Sensor Location and the Heart Rate Control Point characteristics.
  55. * When a Heart Rate Measurement is received, this module decodes only the
  56. * Heart Rate Measurement value field (both 8-bit and 16-bit) and provides it to
  57. * the application.
  58. *
  59. * @note The application must register this module as the BLE event observer by using the
  60. * NRF_SDH_BLE_OBSERVER macro. Example:
  61. * @code
  62. * ble_hrs_c_t instance;
  63. * NRF_SDH_BLE_OBSERVER(anything, BLE_HRS_C_BLE_OBSERVER_PRIO,
  64. * ble_hrs_c_on_ble_evt, &instance);
  65. * @endcode
  66. */
  67. #ifndef BLE_HRS_C_H__
  68. #define BLE_HRS_C_H__
  69. #include <stdint.h>
  70. #include "ble.h"
  71. #include "ble_db_discovery.h"
  72. #include "ble_srv_common.h"
  73. #include "sdk_config.h"
  74. #include "nrf_ble_gq.h"
  75. #include "nrf_sdh_ble.h"
  76. #ifdef __cplusplus
  77. extern "C" {
  78. #endif
  79. /**@brief Macro for defining a ble_hrs_c instance.
  80. *
  81. * @param _name Name of the instance.
  82. * @hideinitializer
  83. */
  84. #define BLE_HRS_C_DEF(_name) \
  85. static ble_hrs_c_t _name; \
  86. NRF_SDH_BLE_OBSERVER(_name ## _obs, \
  87. BLE_HRS_C_BLE_OBSERVER_PRIO, \
  88. ble_hrs_c_on_ble_evt, &_name)
  89. /** @brief Macro for defining multiple ble_hrs_c instances.
  90. *
  91. * @param _name Name of the array of instances.
  92. * @param _cnt Number of instances to define.
  93. * @hideinitializer
  94. */
  95. #define BLE_HRS_C_ARRAY_DEF(_name, _cnt) \
  96. static ble_hrs_c_t _name[_cnt]; \
  97. NRF_SDH_BLE_OBSERVERS(_name ## _obs, \
  98. BLE_HRS_C_BLE_OBSERVER_PRIO, \
  99. ble_hrs_c_on_ble_evt, &_name, _cnt)
  100. /** @brief Maximum number of RR intervals to be decoded for each HRM notifications (any extra RR intervals are ignored).
  101. *
  102. * This define should be defined in the sdk_config.h file to override the default.
  103. */
  104. #ifndef BLE_HRS_C_RR_INTERVALS_MAX_CNT
  105. #define BLE_HRS_C_RR_INTERVALS_MAX_CNT 20
  106. #endif
  107. /**
  108. * @defgroup hrs_c_enums Enumerations
  109. * @{
  110. */
  111. /**@brief HRS Client event type. */
  112. typedef enum
  113. {
  114. BLE_HRS_C_EVT_DISCOVERY_COMPLETE = 1, /**< Event indicating that the Heart Rate Service was discovered at the peer. */
  115. BLE_HRS_C_EVT_HRM_NOTIFICATION /**< Event indicating that a notification of the Heart Rate Measurement characteristic was received from the peer. */
  116. } ble_hrs_c_evt_type_t;
  117. /** @} */
  118. /**
  119. * @defgroup hrs_c_structs Structures
  120. * @{
  121. */
  122. /**@brief Structure containing the Heart Rate Measurement received from the peer. */
  123. typedef struct
  124. {
  125. uint16_t hr_value; /**< Heart Rate Value. */
  126. uint8_t rr_intervals_cnt; /**< Number of RR intervals. */
  127. uint16_t rr_intervals[BLE_HRS_C_RR_INTERVALS_MAX_CNT]; /**< RR intervals. */
  128. } ble_hrm_t;
  129. /**@brief Structure containing the handles related to the Heart Rate Service found on the peer. */
  130. typedef struct
  131. {
  132. uint16_t hrm_cccd_handle; /**< Handle of the CCCD of the Heart Rate Measurement characteristic. */
  133. uint16_t hrm_handle; /**< Handle of the Heart Rate Measurement characteristic, as provided by the SoftDevice. */
  134. } hrs_db_t;
  135. /**@brief Heart Rate Event structure. */
  136. typedef struct
  137. {
  138. ble_hrs_c_evt_type_t evt_type; /**< Type of the event. */
  139. uint16_t conn_handle; /**< Connection handle on which the Heart Rate service was discovered on the peer device..*/
  140. union
  141. {
  142. hrs_db_t peer_db; /**< Handles related to the Heart Rate, found on the peer device. This is filled if the evt_type is @ref BLE_HRS_C_EVT_DISCOVERY_COMPLETE.*/
  143. ble_hrm_t hrm; /**< Heart Rate Measurement received. This is filled if the evt_type is @ref BLE_HRS_C_EVT_HRM_NOTIFICATION. */
  144. } params;
  145. } ble_hrs_c_evt_t;
  146. /** @} */
  147. /**
  148. * @defgroup hrs_c_types Types
  149. * @{
  150. */
  151. // Forward declaration of the ble_bas_t type.
  152. typedef struct ble_hrs_c_s ble_hrs_c_t;
  153. /**@brief Event handler type.
  154. *
  155. * @details This is the type of the event handler that is to be provided by the application
  156. * of this module to receive events.
  157. */
  158. typedef void (* ble_hrs_c_evt_handler_t) (ble_hrs_c_t * p_ble_hrs_c, ble_hrs_c_evt_t * p_evt);
  159. /** @} */
  160. /**
  161. * @addtogroup hrs_c_structs
  162. * @{
  163. */
  164. /**@brief Heart Rate Client structure.
  165. */
  166. struct ble_hrs_c_s
  167. {
  168. uint16_t conn_handle; /**< Connection handle, as provided by the SoftDevice. */
  169. hrs_db_t peer_hrs_db; /**< Handles related to HRS on the peer. */
  170. ble_hrs_c_evt_handler_t evt_handler; /**< Application event handler to be called when there is an event related to the Heart Rate Service. */
  171. ble_srv_error_handler_t error_handler; /**< Function to be called in case of an error. */
  172. nrf_ble_gq_t * p_gatt_queue; /**< Pointer to the BLE GATT Queue instance. */
  173. };
  174. /**@brief Heart Rate Client initialization structure.
  175. */
  176. typedef struct
  177. {
  178. ble_hrs_c_evt_handler_t evt_handler; /**< Event handler to be called by the Heart Rate Client module when there is an event related to the Heart Rate Service. */
  179. ble_srv_error_handler_t error_handler; /**< Function to be called in case of an error. */
  180. nrf_ble_gq_t * p_gatt_queue; /**< Pointer to the BLE GATT Queue instance. */
  181. } ble_hrs_c_init_t;
  182. /** @} */
  183. /**
  184. * @defgroup hrs_c_functions Functions
  185. * @{
  186. */
  187. /**@brief Function for initializing the Heart Rate Client module.
  188. *
  189. * @details This function registers with the Database Discovery module for the Heart Rate Service.
  190. * The module looks for the presence of a Heart Rate Service instance at the peer
  191. * when a discovery is started.
  192. *
  193. * @param[in] p_ble_hrs_c Pointer to the Heart Rate Client structure.
  194. * @param[in] p_ble_hrs_c_init Pointer to the Heart Rate initialization structure that contains
  195. * the initialization information.
  196. *
  197. * @retval NRF_SUCCESS On successful initialization.
  198. * @retval err_code Otherwise, this function propagates the error code returned by the Database Discovery module API
  199. * @ref ble_db_discovery_evt_register.
  200. */
  201. uint32_t ble_hrs_c_init(ble_hrs_c_t * p_ble_hrs_c, ble_hrs_c_init_t * p_ble_hrs_c_init);
  202. /**@brief Function for handling BLE events from the SoftDevice.
  203. *
  204. * @details This function handles the BLE events received from the SoftDevice. If a BLE
  205. * event is relevant to the Heart Rate Client module, the function uses the event's data to update
  206. * interval variables and, if necessary, send events to the application.
  207. *
  208. * @param[in] p_ble_evt Pointer to the BLE event.
  209. * @param[in] p_context Pointer to the Heart Rate Client structure.
  210. */
  211. void ble_hrs_c_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context);
  212. /**@brief Function for requesting the peer to start sending notification of Heart Rate
  213. * Measurement.
  214. *
  215. * @details This function enables notification of the Heart Rate Measurement at the peer
  216. * by writing to the CCCD of the Heart Rate Measurement characteristic.
  217. *
  218. * @param p_ble_hrs_c Pointer to the Heart Rate Client structure.
  219. *
  220. * @retval NRF_SUCCESS If the SoftDevice is requested to write to the CCCD of the peer.
  221. * @retval err_code Otherwise, this function propagates the error code returned
  222. * by the SoftDevice API @ref sd_ble_gattc_write.
  223. */
  224. uint32_t ble_hrs_c_hrm_notif_enable(ble_hrs_c_t * p_ble_hrs_c);
  225. /**@brief Function for handling events from the Database Discovery module.
  226. *
  227. * @details Call this function when you get a callback event from the Database Discovery module.
  228. * This function handles an event from the Database Discovery module and determines
  229. * whether it relates to the discovery of Heart Rate Service at the peer. If it does, the function
  230. * calls the application's event handler to indicate that the Heart Rate Service was
  231. * discovered at the peer. The function also populates the event with service-related
  232. * information before providing it to the application.
  233. *
  234. * @param[in] p_ble_hrs_c Pointer to the Heart Rate Client structure instance for associating the link.
  235. * @param[in] p_evt Pointer to the event received from the Database Discovery module.
  236. *
  237. */
  238. void ble_hrs_on_db_disc_evt(ble_hrs_c_t * p_ble_hrs_c, const ble_db_discovery_evt_t * p_evt);
  239. /**@brief Function for assigning handles to an instance of hrs_c.
  240. *
  241. * @details Call this function when a link has been established with a peer to
  242. * associate the link to this instance of the module. This association makes it
  243. * possible to handle several links and associate each link to a particular
  244. * instance of this module. The connection handle and attribute handles are
  245. * provided from the discovery event @ref BLE_HRS_C_EVT_DISCOVERY_COMPLETE.
  246. *
  247. * @param[in] p_ble_hrs_c Pointer to the Heart Rate Client structure instance for associating the link.
  248. * @param[in] conn_handle Connection handle to associate with the given Heart Rate Client Instance.
  249. * @param[in] p_peer_hrs_handles Attribute handles for the HRS server you want this HRS_C client to
  250. * interact with.
  251. */
  252. uint32_t ble_hrs_c_handles_assign(ble_hrs_c_t * p_ble_hrs_c,
  253. uint16_t conn_handle,
  254. const hrs_db_t * p_peer_hrs_handles);
  255. /** @} */ // End tag for Function group.
  256. #ifdef __cplusplus
  257. }
  258. #endif
  259. #endif // BLE_HRS_C_H__
  260. /** @} */ // End tag for the file.