ble_hrs.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 Heart Rate Service
  43. * @{
  44. * @ingroup ble_sdk_srv
  45. * @brief Heart Rate Service module.
  46. *
  47. * @details This module implements the Heart Rate Service with the Heart Rate Measurement,
  48. * Body Sensor Location and Heart Rate Control Point characteristics.
  49. * During initialization it adds the Heart Rate Service and Heart Rate Measurement
  50. * characteristic to the BLE stack database. Optionally it also adds the
  51. * Body Sensor Location and Heart Rate Control Point characteristics.
  52. *
  53. * If enabled, notification of the Heart Rate Measurement characteristic is performed
  54. * when the application calls ble_hrs_heart_rate_measurement_send().
  55. *
  56. * The Heart Rate Service also provides a set of functions for manipulating the
  57. * various fields in the Heart Rate Measurement characteristic, as well as setting
  58. * the Body Sensor Location characteristic value.
  59. *
  60. * If an event handler is supplied by the application, the Heart Rate Service will
  61. * generate Heart Rate Service events to the application.
  62. *
  63. * @note The application must register this module as BLE event observer using the
  64. * NRF_SDH_BLE_OBSERVER macro. Example:
  65. * @code
  66. * ble_hrs_t instance;
  67. * NRF_SDH_BLE_OBSERVER(anything, BLE_HRS_BLE_OBSERVER_PRIO,
  68. * ble_hrs_on_ble_evt, &instance);
  69. * @endcode
  70. *
  71. * @note Attention!
  72. * To maintain compliance with Nordic Semiconductor ASA Bluetooth profile
  73. * qualification listings, this section of source code must not be modified.
  74. */
  75. #ifndef BLE_HRS_H__
  76. #define BLE_HRS_H__
  77. #include <stdint.h>
  78. #include <stdbool.h>
  79. #include "ble.h"
  80. #include "ble_srv_common.h"
  81. #include "nrf_sdh_ble.h"
  82. #include "nrf_ble_gatt.h"
  83. #ifdef __cplusplus
  84. extern "C" {
  85. #endif
  86. /**@brief Macro for defining a ble_hrs instance.
  87. *
  88. * @param _name Name of the instance.
  89. * @hideinitializer
  90. */
  91. #define BLE_HRS_DEF(_name) \
  92. static ble_hrs_t _name; \
  93. NRF_SDH_BLE_OBSERVER(_name ## _obs, \
  94. BLE_HRS_BLE_OBSERVER_PRIO, \
  95. ble_hrs_on_ble_evt, &_name)
  96. // Body Sensor Location values
  97. #define BLE_HRS_BODY_SENSOR_LOCATION_OTHER 0
  98. #define BLE_HRS_BODY_SENSOR_LOCATION_CHEST 1
  99. #define BLE_HRS_BODY_SENSOR_LOCATION_WRIST 2
  100. #define BLE_HRS_BODY_SENSOR_LOCATION_FINGER 3
  101. #define BLE_HRS_BODY_SENSOR_LOCATION_HAND 4
  102. #define BLE_HRS_BODY_SENSOR_LOCATION_EAR_LOBE 5
  103. #define BLE_HRS_BODY_SENSOR_LOCATION_FOOT 6
  104. #define BLE_HRS_MAX_BUFFERED_RR_INTERVALS 20 /**< Size of RR Interval buffer inside service. */
  105. /**@brief Heart Rate Service event type. */
  106. typedef enum
  107. {
  108. BLE_HRS_EVT_NOTIFICATION_ENABLED, /**< Heart Rate value notification enabled event. */
  109. BLE_HRS_EVT_NOTIFICATION_DISABLED /**< Heart Rate value notification disabled event. */
  110. } ble_hrs_evt_type_t;
  111. /**@brief Heart Rate Service event. */
  112. typedef struct
  113. {
  114. ble_hrs_evt_type_t evt_type; /**< Type of event. */
  115. } ble_hrs_evt_t;
  116. // Forward declaration of the ble_hrs_t type.
  117. typedef struct ble_hrs_s ble_hrs_t;
  118. /**@brief Heart Rate Service event handler type. */
  119. typedef void (*ble_hrs_evt_handler_t) (ble_hrs_t * p_hrs, ble_hrs_evt_t * p_evt);
  120. /**@brief Heart Rate Service init structure. This contains all options and data needed for
  121. * initialization of the service. */
  122. typedef struct
  123. {
  124. ble_hrs_evt_handler_t evt_handler; /**< Event handler to be called for handling events in the Heart Rate Service. */
  125. bool is_sensor_contact_supported; /**< Determines if sensor contact detection is to be supported. */
  126. uint8_t * p_body_sensor_location; /**< If not NULL, initial value of the Body Sensor Location characteristic. */
  127. security_req_t hrm_cccd_wr_sec; /**< Security requirement for writing the HRM characteristic CCCD. */
  128. security_req_t bsl_rd_sec; /**< Security requirement for reading the BSL characteristic value. */
  129. } ble_hrs_init_t;
  130. /**@brief Heart Rate Service structure. This contains various status information for the service. */
  131. struct ble_hrs_s
  132. {
  133. ble_hrs_evt_handler_t evt_handler; /**< Event handler to be called for handling events in the Heart Rate Service. */
  134. bool is_expended_energy_supported; /**< TRUE if Expended Energy measurement is supported. */
  135. bool is_sensor_contact_supported; /**< TRUE if sensor contact detection is supported. */
  136. uint16_t service_handle; /**< Handle of Heart Rate Service (as provided by the BLE stack). */
  137. ble_gatts_char_handles_t hrm_handles; /**< Handles related to the Heart Rate Measurement characteristic. */
  138. ble_gatts_char_handles_t bsl_handles; /**< Handles related to the Body Sensor Location characteristic. */
  139. ble_gatts_char_handles_t hrcp_handles; /**< Handles related to the Heart Rate Control Point characteristic. */
  140. uint16_t conn_handle; /**< Handle of the current connection (as provided by the BLE stack, is BLE_CONN_HANDLE_INVALID if not in a connection). */
  141. bool is_sensor_contact_detected; /**< TRUE if sensor contact has been detected. */
  142. uint16_t rr_interval[BLE_HRS_MAX_BUFFERED_RR_INTERVALS]; /**< Set of RR Interval measurements since the last Heart Rate Measurement transmission. */
  143. uint16_t rr_interval_count; /**< Number of RR Interval measurements since the last Heart Rate Measurement transmission. */
  144. uint8_t max_hrm_len; /**< Current maximum HR measurement length, adjusted according to the current ATT MTU. */
  145. };
  146. /**@brief Function for initializing the Heart Rate Service.
  147. *
  148. * @param[out] p_hrs Heart Rate Service structure. This structure will have to be supplied by
  149. * the application. It will be initialized by this function, and will later
  150. * be used to identify this particular service instance.
  151. * @param[in] p_hrs_init Information needed to initialize the service.
  152. *
  153. * @return NRF_SUCCESS on successful initialization of service, otherwise an error code.
  154. */
  155. uint32_t ble_hrs_init(ble_hrs_t * p_hrs, ble_hrs_init_t const * p_hrs_init);
  156. /**@brief Function for handling the GATT module's events.
  157. *
  158. * @details Handles all events from the GATT module of interest to the Heart Rate Service.
  159. *
  160. * @param[in] p_hrs Heart Rate Service structure.
  161. * @param[in] p_gatt_evt Event received from the GATT module.
  162. */
  163. void ble_hrs_on_gatt_evt(ble_hrs_t * p_hrs, nrf_ble_gatt_evt_t const * p_gatt_evt);
  164. /**@brief Function for handling the Application's BLE Stack events.
  165. *
  166. * @details Handles all events from the BLE stack of interest to the Heart Rate Service.
  167. *
  168. * @param[in] p_ble_evt Event received from the BLE stack.
  169. * @param[in] p_context Heart Rate Service structure.
  170. */
  171. void ble_hrs_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context);
  172. /**@brief Function for sending heart rate measurement if notification has been enabled.
  173. *
  174. * @details The application calls this function after having performed a heart rate measurement.
  175. * If notification has been enabled, the heart rate measurement data is encoded and sent to
  176. * the client.
  177. *
  178. * @param[in] p_hrs Heart Rate Service structure.
  179. * @param[in] heart_rate New heart rate measurement.
  180. *
  181. * @return NRF_SUCCESS on success, otherwise an error code.
  182. */
  183. uint32_t ble_hrs_heart_rate_measurement_send(ble_hrs_t * p_hrs, uint16_t heart_rate);
  184. /**@brief Function for adding a RR Interval measurement to the RR Interval buffer.
  185. *
  186. * @details All buffered RR Interval measurements will be included in the next heart rate
  187. * measurement message, up to the maximum number of measurements that will fit into the
  188. * message. If the buffer is full, the oldest measurement in the buffer will be deleted.
  189. *
  190. * @param[in] p_hrs Heart Rate Service structure.
  191. * @param[in] rr_interval New RR Interval measurement (will be buffered until the next
  192. * transmission of Heart Rate Measurement).
  193. */
  194. void ble_hrs_rr_interval_add(ble_hrs_t * p_hrs, uint16_t rr_interval);
  195. /**@brief Function for checking if RR Interval buffer is full.
  196. *
  197. * @param[in] p_hrs Heart Rate Service structure.
  198. *
  199. * @return true if RR Interval buffer is full, false otherwise.
  200. */
  201. bool ble_hrs_rr_interval_buffer_is_full(ble_hrs_t * p_hrs);
  202. /**@brief Function for setting the state of the Sensor Contact Supported bit.
  203. *
  204. * @param[in] p_hrs Heart Rate Service structure.
  205. * @param[in] is_sensor_contact_supported New state of the Sensor Contact Supported bit.
  206. *
  207. * @return NRF_SUCCESS on success, otherwise an error code.
  208. */
  209. uint32_t ble_hrs_sensor_contact_supported_set(ble_hrs_t * p_hrs, bool is_sensor_contact_supported);
  210. /**@brief Function for setting the state of the Sensor Contact Detected bit.
  211. *
  212. * @param[in] p_hrs Heart Rate Service structure.
  213. * @param[in] is_sensor_contact_detected TRUE if sensor contact is detected, FALSE otherwise.
  214. */
  215. void ble_hrs_sensor_contact_detected_update(ble_hrs_t * p_hrs, bool is_sensor_contact_detected);
  216. /**@brief Function for setting the Body Sensor Location.
  217. *
  218. * @details Sets a new value of the Body Sensor Location characteristic. The new value will be sent
  219. * to the client the next time the client reads the Body Sensor Location characteristic.
  220. *
  221. * @param[in] p_hrs Heart Rate Service structure.
  222. * @param[in] body_sensor_location New Body Sensor Location.
  223. *
  224. * @return NRF_SUCCESS on success, otherwise an error code.
  225. */
  226. uint32_t ble_hrs_body_sensor_location_set(ble_hrs_t * p_hrs, uint8_t body_sensor_location);
  227. #ifdef __cplusplus
  228. }
  229. #endif
  230. #endif // BLE_HRS_H__
  231. /** @} */