ble_hts.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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_hts Health Thermometer Service
  43. * @{
  44. * @ingroup ble_sdk_srv
  45. * @brief Health Thermometer Service module.
  46. *
  47. * @details This module implements the Health Thermometer Service.
  48. *
  49. * If an event handler is supplied by the application, the Health Thermometer
  50. * Service will generate Health Thermometer 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_hts_t instance;
  56. * NRF_SDH_BLE_OBSERVER(anything, BLE_HTS_BLE_OBSERVER_PRIO,
  57. * ble_hts_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_HTS_H__
  65. #define BLE_HTS_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_hts instance.
  77. *
  78. * @param _name Name of the instance.
  79. * @hideinitializer
  80. */
  81. #define BLE_HTS_DEF(_name) \
  82. static ble_hts_t _name; \
  83. NRF_SDH_BLE_OBSERVER(_name ## _obs, \
  84. BLE_HTS_BLE_OBSERVER_PRIO, \
  85. ble_hts_on_ble_evt, &_name)
  86. // Temperature Type measurement locations
  87. #define BLE_HTS_TEMP_TYPE_ARMPIT 1
  88. #define BLE_HTS_TEMP_TYPE_BODY 2
  89. #define BLE_HTS_TEMP_TYPE_EAR 3
  90. #define BLE_HTS_TEMP_TYPE_FINGER 4
  91. #define BLE_HTS_TEMP_TYPE_GI_TRACT 5
  92. #define BLE_HTS_TEMP_TYPE_MOUTH 6
  93. #define BLE_HTS_TEMP_TYPE_RECTUM 7
  94. #define BLE_HTS_TEMP_TYPE_TOE 8
  95. #define BLE_HTS_TEMP_TYPE_EAR_DRUM 9
  96. /**@brief Health Thermometer Service event type. */
  97. typedef enum
  98. {
  99. BLE_HTS_EVT_INDICATION_ENABLED, /**< Health Thermometer value indication enabled event. */
  100. BLE_HTS_EVT_INDICATION_DISABLED, /**< Health Thermometer value indication disabled event. */
  101. BLE_HTS_EVT_INDICATION_CONFIRMED /**< Confirmation of a temperature measurement indication has been received. */
  102. } ble_hts_evt_type_t;
  103. /**@brief Health Thermometer Service event. */
  104. typedef struct
  105. {
  106. ble_hts_evt_type_t evt_type; /**< Type of event. */
  107. } ble_hts_evt_t;
  108. // Forward declaration of the ble_hts_t type.
  109. typedef struct ble_hts_s ble_hts_t;
  110. /**@brief Health Thermometer Service event handler type. */
  111. typedef void (*ble_hts_evt_handler_t) (ble_hts_t * p_hts, ble_hts_evt_t * p_evt);
  112. /**@brief FLOAT format (IEEE-11073 32-bit FLOAT, defined as a 32-bit value with a 24-bit mantissa
  113. * and an 8-bit exponent. */
  114. typedef struct
  115. {
  116. int8_t exponent; /**< Base 10 exponent */
  117. int32_t mantissa; /**< Mantissa, should be using only 24 bits */
  118. } ieee_float32_t;
  119. /**@brief Health Thermometer Service init structure. This contains all options and data
  120. * needed for initialization of the service. */
  121. typedef struct
  122. {
  123. ble_hts_evt_handler_t evt_handler; /**< Event handler to be called for handling events in the Health Thermometer Service. */
  124. ble_srv_error_handler_t error_handler; /**< Function to be called in case of an error. */
  125. security_req_t ht_meas_cccd_wr_sec; /**< Security requirement for writing health thermometer measurement characteristic CCCD. */
  126. security_req_t ht_type_rd_sec; /**< Security requirement for reading health thermometer type characteristic. */
  127. uint8_t temp_type_as_characteristic; /**< Set non-zero if temp type given as characteristic */
  128. uint8_t temp_type; /**< Temperature type if temperature characteristic is used */
  129. nrf_ble_gq_t * p_gatt_queue; /**< Pointer to BLE GATT Queue instance. */
  130. } ble_hts_init_t;
  131. /**@brief Health Thermometer Service structure. This contains various status information for
  132. * the service. */
  133. struct ble_hts_s
  134. {
  135. ble_hts_evt_handler_t evt_handler; /**< Event handler to be called for handling events in the Health Thermometer Service. */
  136. ble_srv_error_handler_t error_handler; /**< Function to be called in case of an error. */
  137. uint16_t service_handle; /**< Handle of Health Thermometer Service (as provided by the BLE stack). */
  138. ble_gatts_char_handles_t meas_handles; /**< Handles related to the Health Thermometer Measurement characteristic. */
  139. ble_gatts_char_handles_t temp_type_handles; /**< Handles related to the Health Thermometer Temperature Type 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. uint8_t temp_type; /**< Temperature type indicates where the measurement was taken. */
  142. nrf_ble_gq_t * p_gatt_queue; /**< Pointer to BLE GATT Queue instance. */
  143. };
  144. /**@brief Health Thermometer Service measurement structure. This contains a Health Thermometer
  145. * measurement. */
  146. typedef struct ble_hts_meas_s
  147. {
  148. bool temp_in_fahr_units; /**< True if Temperature is in Fahrenheit units, Celcius otherwise. */
  149. bool time_stamp_present; /**< True if Time Stamp is present. */
  150. bool temp_type_present; /**< True if Temperature Type is present. */
  151. ieee_float32_t temp_in_celcius; /**< Temperature Measurement Value (Celcius). */
  152. ieee_float32_t temp_in_fahr; /**< Temperature Measurement Value (Fahrenheit). */
  153. ble_date_time_t time_stamp; /**< Time Stamp. */
  154. uint8_t temp_type; /**< Temperature Type. */
  155. } ble_hts_meas_t;
  156. /**@brief Function for initializing the Health Thermometer Service.
  157. *
  158. * @param[out] p_hts Health Thermometer Service structure. This structure will have to
  159. * be supplied by the application. It will be initialized by this function,
  160. * and will later be used to identify this particular service instance.
  161. * @param[in] p_hts_init Information needed to initialize the service.
  162. *
  163. * @return NRF_SUCCESS on successful initialization of service, otherwise an error code.
  164. */
  165. uint32_t ble_hts_init(ble_hts_t * p_hts, const ble_hts_init_t * p_hts_init);
  166. /**@brief Function for handling the Application's BLE Stack events.
  167. *
  168. * @details Handles all events from the BLE stack of interest to the Health Thermometer Service.
  169. *
  170. * @param[in] p_ble_evt Event received from the BLE stack.
  171. * @param[in] p_context Health Thermometer Service structure.
  172. */
  173. void ble_hts_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context);
  174. /**@brief Function for sending health thermometer measurement if indication has been enabled.
  175. *
  176. * @details The application calls this function after having performed a Health Thermometer
  177. * measurement. If indication has been enabled, the measurement data is encoded and
  178. * sent to the client.
  179. *
  180. * @param[in] p_hts Health Thermometer Service structure.
  181. * @param[in] p_hts_meas Pointer to new health thermometer measurement.
  182. *
  183. * @return NRF_SUCCESS on success, otherwise an error code.
  184. */
  185. uint32_t ble_hts_measurement_send(ble_hts_t * p_hts, ble_hts_meas_t * p_hts_meas);
  186. /**@brief Function for checking if indication of Temperature Measurement is currently enabled.
  187. *
  188. * @param[in] p_hts Health Thermometer Service structure.
  189. * @param[out] p_indication_enabled TRUE if indication is enabled, FALSE otherwise.
  190. *
  191. * @return NRF_SUCCESS on success, otherwise an error code.
  192. */
  193. uint32_t ble_hts_is_indication_enabled(ble_hts_t * p_hts, bool * p_indication_enabled);
  194. #ifdef __cplusplus
  195. }
  196. #endif
  197. #endif // BLE_HTS_H__
  198. /** @} */