ble_bps.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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_bps Blood Pressure Service
  43. * @{
  44. * @ingroup ble_sdk_srv
  45. * @brief Blood Pressure Service module.
  46. *
  47. * @details This module implements the Blood Pressure Service.
  48. *
  49. * If an event handler is supplied by the application, the Blood Pressure
  50. * Service will generate Blood Pressure Service events to the application.
  51. *
  52. * @note The application must register this module as BLE event observer using the
  53. * NRF_SDH_BLE_OBSERVER macro. Example:
  54. * @code
  55. * ble_bps_t instance;
  56. * NRF_SDH_BLE_OBSERVER(anything, BLE_BPS_BLE_OBSERVER_PRIO,
  57. * ble_bps_on_ble_evt, &instance);
  58. * @endcode
  59. *
  60. * @note Attention!
  61. * To maintain compliance with Nordic Semiconductor ASA Bluetooth profile
  62. * qualification listings, this section of source code must not be modified.
  63. */
  64. #ifndef BLE_BPS_H__
  65. #define BLE_BPS_H__
  66. #include <stdint.h>
  67. #include <stdbool.h>
  68. #include "ble.h"
  69. #include "ble_srv_common.h"
  70. #include "ble_date_time.h"
  71. #include "nrf_sdh_ble.h"
  72. #include "nrf_ble_gq.h"
  73. #ifdef __cplusplus
  74. extern "C" {
  75. #endif
  76. /**@brief Macro for defining a ble_bps instance.
  77. *
  78. * @param _name Name of the instance.
  79. * @hideinitializer
  80. */
  81. #define BLE_BPS_DEF(_name) \
  82. static ble_bps_t _name; \
  83. NRF_SDH_BLE_OBSERVER(_name ## _obs, \
  84. BLE_BPS_BLE_OBSERVER_PRIO, \
  85. ble_bps_on_ble_evt, &_name)
  86. // Blood Pressure Feature bits
  87. #define BLE_BPS_FEATURE_BODY_MOVEMENT_BIT (0x01 << 0) /**< Body Movement Detection Support bit. */
  88. #define BLE_BPS_FEATURE_CUFF_FIT_BIT (0x01 << 1) /**< Cuff Fit Detection Support bit. */
  89. #define BLE_BPS_FEATURE_IRREGULAR_PULSE_BIT (0x01 << 2) /**< Irregular Pulse Detection Support bit. */
  90. #define BLE_BPS_FEATURE_PULSE_RATE_RANGE_BIT (0x01 << 3) /**< Pulse Rate Range Detection Support bit. */
  91. #define BLE_BPS_FEATURE_MEASUREMENT_POSITION_BIT (0x01 << 4) /**< Measurement Position Detection Support bit. */
  92. #define BLE_BPS_FEATURE_MULTIPLE_BOND_BIT (0x01 << 5) /**< Multiple Bond Support bit. */
  93. /**@brief Blood Pressure Service event type. */
  94. typedef enum
  95. {
  96. BLE_BPS_EVT_INDICATION_ENABLED, /**< Blood Pressure value indication enabled event. */
  97. BLE_BPS_EVT_INDICATION_DISABLED, /**< Blood Pressure value indication disabled event. */
  98. BLE_BPS_EVT_INDICATION_CONFIRMED /**< Confirmation of a blood pressure measurement indication has been received. */
  99. } ble_bps_evt_type_t;
  100. /**@brief Blood Pressure Service event. */
  101. typedef struct
  102. {
  103. ble_bps_evt_type_t evt_type; /**< Type of event. */
  104. } ble_bps_evt_t;
  105. // Forward declaration of the ble_bps_t type.
  106. typedef struct ble_bps_s ble_bps_t;
  107. /**@brief Blood Pressure Service event handler type. */
  108. typedef void (*ble_bps_evt_handler_t) (ble_bps_t * p_bps, ble_bps_evt_t * p_evt);
  109. /**@brief SFLOAT format (IEEE-11073 16-bit FLOAT, defined as a 16-bit vlue with 12-bit mantissa and
  110. * 4-bit exponent. */
  111. typedef struct
  112. {
  113. int8_t exponent; /**< Base 10 exponent, only 4 bits */
  114. int16_t mantissa; /**< Mantissa, only 12 bits */
  115. } ieee_float16_t;
  116. /**@brief Blood Pressure Service init structure. This contains all options and data
  117. * needed for initialization of the service. */
  118. typedef struct
  119. {
  120. ble_bps_evt_handler_t evt_handler; /**< Event handler to be called for handling events in the Blood Pressure Service. */
  121. ble_srv_error_handler_t error_handler; /**< Function to be called in case of an error. */
  122. security_req_t bp_cccd_wr_sec; /**< Security requirement for writing blood pressure measurement characteristic CCCD. */
  123. security_req_t bp_feature_rd_sec; /**< Security requirement for reading the blood pressure feature characteristic. */
  124. uint16_t feature; /**< Initial value for blood pressure feature */
  125. nrf_ble_gq_t * p_gatt_queue; /**< Pointer to BLE GATT Queue instance. */
  126. } ble_bps_init_t;
  127. /**@brief Blood Pressure Service structure. This contains various status information for
  128. * the service. */
  129. struct ble_bps_s
  130. {
  131. ble_bps_evt_handler_t evt_handler; /**< Event handler to be called for handling events in the Blood Pressure Service. */
  132. ble_srv_error_handler_t error_handler; /**< Function to be called in case of an error. */
  133. uint16_t service_handle; /**< Handle of Blood Pressure Service (as provided by the BLE stack). */
  134. ble_gatts_char_handles_t meas_handles; /**< Handles related to the Blood Pressure Measurement characteristic. */
  135. ble_gatts_char_handles_t feature_handles; /**< Handles related to the Blood Pressure Feature characteristic. */
  136. 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). */
  137. uint16_t feature; /**< Value of Blood Pressure feature. */
  138. nrf_ble_gq_t * p_gatt_queue; /**< Pointer to BLE GATT Queue instance. */
  139. };
  140. /**@brief Blood Pressure Service measurement structure. This contains a Blood Pressure
  141. * measurement. */
  142. typedef struct ble_bps_meas_s
  143. {
  144. bool blood_pressure_units_in_kpa; /**< Blood Pressure Units Flag, 0=mmHg, 1=kPa */
  145. bool time_stamp_present; /**< Time Stamp Flag, 0=not present, 1=present. */
  146. bool pulse_rate_present; /**< Pulse Rate Flag, 0=not present, 1=present. */
  147. bool user_id_present; /**< User ID Flag, 0=not present, 1=present. */
  148. bool measurement_status_present; /**< Measurement Status Flag, 0=not present, 1=present. */
  149. ieee_float16_t blood_pressure_systolic; /**< Blood Pressure Measurement Compound Value - Systolic. */
  150. ieee_float16_t blood_pressure_diastolic; /**< Blood Pressure Measurement Compound Value - Diastolic . */
  151. ieee_float16_t mean_arterial_pressure; /**< Blood Pressure Measurement Compound Value - Mean Arterial Pressure. */
  152. ble_date_time_t time_stamp; /**< Time Stamp. */
  153. ieee_float16_t pulse_rate; /**< Pulse Rate. */
  154. uint8_t user_id; /**< User ID. */
  155. uint16_t measurement_status; /**< Measurement Status. */
  156. } ble_bps_meas_t;
  157. /**@brief Function for initializing the Blood Pressure Service.
  158. *
  159. * @param[out] p_bps Blood Pressure Service structure. This structure will have to
  160. * be supplied by the application. It will be initialized by this function,
  161. * and will later be used to identify this particular service instance.
  162. * @param[in] p_bps_init Information needed to initialize the service.
  163. *
  164. * @return NRF_SUCCESS on successful initialization of service, otherwise an error code.
  165. */
  166. uint32_t ble_bps_init(ble_bps_t * p_bps, ble_bps_init_t const * p_bps_init);
  167. /**@brief Function for handling the Application's BLE Stack events.
  168. *
  169. * @details Handles all events from the BLE stack of interest to the Blood Pressure Service.
  170. *
  171. * @param[in] p_ble_evt Event received from the BLE stack.
  172. * @param[in] p_context Blood Pressure Service structure.
  173. */
  174. void ble_bps_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context);
  175. /**@brief Function for sending blood pressure measurement if indication has been enabled.
  176. *
  177. * @details The application calls this function after having performed a Blood Pressure
  178. * measurement. If indication has been enabled, the measurement data is encoded and
  179. * sent to the client.
  180. *
  181. * @param[in] p_bps Blood Pressure Service structure.
  182. * @param[in] p_bps_meas Pointer to new blood pressure measurement.
  183. *
  184. * @return NRF_SUCCESS on success, otherwise an error code.
  185. */
  186. uint32_t ble_bps_measurement_send(ble_bps_t * p_bps, ble_bps_meas_t * p_bps_meas);
  187. /**@brief Function for checking if indication of Blood Pressure Measurement is currently enabled.
  188. *
  189. * @param[in] p_bps Blood Pressure Service structure.
  190. * @param[out] p_indication_enabled TRUE if indication is enabled, FALSE otherwise.
  191. *
  192. * @return NRF_SUCCESS on success, otherwise an error code.
  193. */
  194. uint32_t ble_bps_is_indication_enabled(ble_bps_t * p_bps, bool * p_indication_enabled);
  195. #ifdef __cplusplus
  196. }
  197. #endif
  198. #endif // BLE_BPS_H__
  199. /** @} */