ble_gls.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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_gls Glucose Service
  43. * @{
  44. * @ingroup ble_sdk_srv
  45. * @brief Glucose Service module.
  46. *
  47. * @details This module implements the Glucose Service.
  48. *
  49. * @note The application must register this module as BLE event observer using the
  50. * NRF_SDH_BLE_OBSERVER macro. Example:
  51. * @code
  52. * ble_gls_t instance;
  53. * NRF_SDH_BLE_OBSERVER(anything, BLE_GLS_BLE_OBSERVER_PRIO,
  54. * ble_gls_on_ble_evt, &instance);
  55. * @endcode
  56. *
  57. * @note Attention!
  58. * To maintain compliance with Nordic Semiconductor ASA Bluetooth profile
  59. * qualification listings, this section of source code must not be modified.
  60. */
  61. #ifndef BLE_GLS_H__
  62. #define BLE_GLS_H__
  63. #include <stdint.h>
  64. #include <stdbool.h>
  65. #include "ble.h"
  66. #include "ble_srv_common.h"
  67. #include "ble_date_time.h"
  68. #include "nrf_ble_gq.h"
  69. #include "nrf_sdh_ble.h"
  70. #ifdef __cplusplus
  71. extern "C" {
  72. #endif
  73. /**@brief Macro for defining a ble_gls instance.
  74. *
  75. * @param _name Name of the instance.
  76. * @hideinitializer
  77. */
  78. #define BLE_GLS_DEF(_name) \
  79. static ble_gls_t _name; \
  80. NRF_SDH_BLE_OBSERVER(_name ## _obs, \
  81. BLE_GLS_BLE_OBSERVER_PRIO, \
  82. ble_gls_on_ble_evt, &_name)
  83. /**@brief Glucose feature */
  84. #define BLE_GLS_FEATURE_LOW_BATT 0x0001 /**< Low Battery Detection During Measurement Supported */
  85. #define BLE_GLS_FEATURE_MALFUNC 0x0002 /**< Sensor Malfunction Detection Supported */
  86. #define BLE_GLS_FEATURE_SAMPLE_SIZE 0x0004 /**< Sensor Sample Size Supported */
  87. #define BLE_GLS_FEATURE_INSERT_ERR 0x0008 /**< Sensor Strip Insertion Error Detection Supported */
  88. #define BLE_GLS_FEATURE_TYPE_ERR 0x0010 /**< Sensor Strip Type Error Detection Supported */
  89. #define BLE_GLS_FEATURE_RES_HIGH_LOW 0x0020 /**< Sensor Result High-Low Detection Supported */
  90. #define BLE_GLS_FEATURE_TEMP_HIGH_LOW 0x0040 /**< Sensor Temperature High-Low Detection Supported */
  91. #define BLE_GLS_FEATURE_READ_INT 0x0080 /**< Sensor Read Interrupt Detection Supported */
  92. #define BLE_GLS_FEATURE_GENERAL_FAULT 0x0100 /**< General Device Fault Supported */
  93. #define BLE_GLS_FEATURE_TIME_FAULT 0x0200 /**< Time Fault Supported */
  94. #define BLE_GLS_FEATURE_MULTI_BOND 0x0400 /**< Multiple Bond Supported */
  95. /**@brief Glucose measurement flags */
  96. #define BLE_GLS_MEAS_FLAG_TIME_OFFSET 0x01 /**< Time Offset Present */
  97. #define BLE_GLS_MEAS_FLAG_CONC_TYPE_LOC 0x02 /**< Glucose Concentration, Type, and Sample Location Present */
  98. #define BLE_GLS_MEAS_FLAG_UNITS_KG_L 0x00 /**< Glucose Concentration Units kg/L */
  99. #define BLE_GLS_MEAS_FLAG_UNITS_MOL_L 0x04 /**< Glucose Concentration Units mol/L */
  100. #define BLE_GLS_MEAS_FLAG_SENSOR_STATUS 0x08 /**< Sensor Status Annunciation Present */
  101. #define BLE_GLS_MEAS_FLAG_CONTEXT_INFO 0x10 /**< Context Information Follows */
  102. /**@brief Glucose measurement type */
  103. #define BLE_GLS_MEAS_TYPE_CAP_BLOOD 1 /**< Capillary whole blood */
  104. #define BLE_GLS_MEAS_TYPE_CAP_PLASMA 2 /**< Capillary plasma */
  105. #define BLE_GLS_MEAS_TYPE_VEN_BLOOD 3 /**< Venous whole blood */
  106. #define BLE_GLS_MEAS_TYPE_VEN_PLASMA 4 /**< Venous plasma */
  107. #define BLE_GLS_MEAS_TYPE_ART_BLOOD 5 /**< Arterial whole blood */
  108. #define BLE_GLS_MEAS_TYPE_ART_PLASMA 6 /**< Arterial plasma */
  109. #define BLE_GLS_MEAS_TYPE_UNDET_BLOOD 7 /**< Undetermined whole blood */
  110. #define BLE_GLS_MEAS_TYPE_UNDET_PLASMA 8 /**< Undetermined plasma */
  111. #define BLE_GLS_MEAS_TYPE_FLUID 9 /**< Interstitial fluid (ISF) */
  112. #define BLE_GLS_MEAS_TYPE_CONTROL 10 /**< Control solution */
  113. /**@brief Glucose measurement location */
  114. #define BLE_GLS_MEAS_LOC_FINGER 1 /**< Finger */
  115. #define BLE_GLS_MEAS_LOC_AST 2 /**< Alternate Site Test (AST) */
  116. #define BLE_GLS_MEAS_LOC_EAR 3 /**< Earlobe */
  117. #define BLE_GLS_MEAS_LOC_CONTROL 4 /**< Control solution */
  118. #define BLE_GLS_MEAS_LOC_NOT_AVAIL 15 /**< Sample Location value not available */
  119. /**@brief Glucose sensor status annunciation */
  120. #define BLE_GLS_MEAS_STATUS_BATT_LOW 0x0001 /**< Device battery low at time of measurement */
  121. #define BLE_GLS_MEAS_STATUS_SENSOR_FAULT 0x0002 /**< Sensor malfunction or faulting at time of measurement */
  122. #define BLE_GLS_MEAS_STATUS_SAMPLE_SIZE 0x0004 /**< Sample size for blood or control solution insufficient at time of measurement */
  123. #define BLE_GLS_MEAS_STATUS_STRIP_INSERT 0x0008 /**< Strip insertion error */
  124. #define BLE_GLS_MEAS_STATUS_STRIP_TYPE 0x0010 /**< Strip type incorrect for device */
  125. #define BLE_GLS_MEAS_STATUS_RESULT_HIGH 0x0020 /**< Sensor result higher than the device can process */
  126. #define BLE_GLS_MEAS_STATUS_RESULT_LOW 0x0040 /**< Sensor result lower than the device can process */
  127. #define BLE_GLS_MEAS_STATUS_TEMP_HIGH 0x0080 /**< Sensor temperature too high for valid test/result at time of measurement */
  128. #define BLE_GLS_MEAS_STATUS_TEMP_LOW 0x0100 /**< Sensor temperature too low for valid test/result at time of measurement */
  129. #define BLE_GLS_MEAS_STATUS_STRIP_PULL 0x0200 /**< Sensor read interrupted because strip was pulled too soon at time of measurement */
  130. #define BLE_GLS_MEAS_STATUS_GENERAL_FAULT 0x0400 /**< General device fault has occurred in the sensor */
  131. #define BLE_GLS_MEAS_STATUS_TIME_FAULT 0x0800 /**< Time fault has occurred in the sensor and time may be inaccurate */
  132. /**@brief Glucose measurement context flags */
  133. #define BLE_GLS_CONTEXT_FLAG_CARB 0x01 /**< Carbohydrate id and carbohydrate present */
  134. #define BLE_GLS_CONTEXT_FLAG_MEAL 0x02 /**< Meal present */
  135. #define BLE_GLS_CONTEXT_FLAG_TESTER 0x04 /**< Tester-health present */
  136. #define BLE_GLS_CONTEXT_FLAG_EXERCISE 0x08 /**< Exercise duration and exercise intensity present */
  137. #define BLE_GLS_CONTEXT_FLAG_MED 0x10 /**< Medication ID and medication present */
  138. #define BLE_GLS_CONTEXT_FLAG_MED_KG 0x00 /**< Medication value units, kilograms */
  139. #define BLE_GLS_CONTEXT_FLAG_MED_L 0x20 /**< Medication value units, liters */
  140. #define BLE_GLS_CONTEXT_FLAG_HBA1C 0x40 /**< Hba1c present */
  141. #define BLE_GLS_CONTEXT_FLAG_EXT 0x80 /**< Extended flags present */
  142. /**@brief Glucose measurement context carbohydrate ID */
  143. #define BLE_GLS_CONTEXT_CARB_BREAKFAST 1 /**< Breakfast */
  144. #define BLE_GLS_CONTEXT_CARB_LUNCH 2 /**< Lunch */
  145. #define BLE_GLS_CONTEXT_CARB_DINNER 3 /**< Dinner */
  146. #define BLE_GLS_CONTEXT_CARB_SNACK 4 /**< Snack */
  147. #define BLE_GLS_CONTEXT_CARB_DRINK 5 /**< Drink */
  148. #define BLE_GLS_CONTEXT_CARB_SUPPER 6 /**< Supper */
  149. #define BLE_GLS_CONTEXT_CARB_BRUNCH 7 /**< Brunch */
  150. /**@brief Glucose measurement context meal */
  151. #define BLE_GLS_CONTEXT_MEAL_PREPRANDIAL 1 /**< Preprandial (before meal) */
  152. #define BLE_GLS_CONTEXT_MEAL_POSTPRANDIAL 2 /**< Postprandial (after meal) */
  153. #define BLE_GLS_CONTEXT_MEAL_FASTING 3 /**< Fasting */
  154. #define BLE_GLS_CONTEXT_MEAL_CASUAL 4 /**< Casual (snacks, drinks, etc.) */
  155. #define BLE_GLS_CONTEXT_MEAL_BEDTIME 5 /**< Bedtime */
  156. /**@brief Glucose measurement context tester */
  157. #define BLE_GLS_CONTEXT_TESTER_SELF 1 /**< Self */
  158. #define BLE_GLS_CONTEXT_TESTER_PRO 2 /**< Health care professional */
  159. #define BLE_GLS_CONTEXT_TESTER_LAB 3 /**< Lab test */
  160. #define BLE_GLS_CONTEXT_TESTER_NOT_AVAIL 15 /**< Tester value not available */
  161. /**@brief Glucose measurement context health */
  162. #define BLE_GLS_CONTEXT_HEALTH_MINOR 1 /**< Minor health issues */
  163. #define BLE_GLS_CONTEXT_HEALTH_MAJOR 2 /**< Major health issues */
  164. #define BLE_GLS_CONTEXT_HEALTH_MENSES 3 /**< During menses */
  165. #define BLE_GLS_CONTEXT_HEALTH_STRESS 4 /**< Under stress */
  166. #define BLE_GLS_CONTEXT_HEALTH_NONE 5 /**< No health issues */
  167. #define BLE_GLS_CONTEXT_HEALTH_NOT_AVAIL 15 /**< Health value not available */
  168. /**@brief Glucose measurement context medication ID */
  169. #define BLE_GLS_CONTEXT_MED_RAPID 1 /**< Rapid acting insulin */
  170. #define BLE_GLS_CONTEXT_MED_SHORT 2 /**< Short acting insulin */
  171. #define BLE_GLS_CONTEXT_MED_INTERMED 3 /**< Intermediate acting insulin */
  172. #define BLE_GLS_CONTEXT_MED_LONG 4 /**< Long acting insulin */
  173. #define BLE_GLS_CONTEXT_MED_PREMIX 5 /**< Pre-mixed insulin */
  174. /**@brief SFLOAT format (IEEE-11073 16-bit FLOAT, meaning 4 bits for exponent (base 10) and 12 bits mantissa) */
  175. typedef struct
  176. {
  177. int8_t exponent; /**< Base 10 exponent, should be using only 4 bits */
  178. int16_t mantissa; /**< Mantissa, should be using only 12 bits */
  179. } sfloat_t;
  180. /**@brief Glucose Service event type. */
  181. typedef enum
  182. {
  183. BLE_GLS_EVT_NOTIFICATION_ENABLED, /**< Glucose value notification enabled event. */
  184. BLE_GLS_EVT_NOTIFICATION_DISABLED /**< Glucose value notification disabled event. */
  185. } ble_gls_evt_type_t;
  186. /**@brief Glucose Service event. */
  187. typedef struct
  188. {
  189. ble_gls_evt_type_t evt_type; /**< Type of event. */
  190. } ble_gls_evt_t;
  191. // Forward declaration of the ble_gls_t type.
  192. typedef struct ble_gls_s ble_gls_t;
  193. /**@brief Glucose Service event handler type. */
  194. typedef void (*ble_gls_evt_handler_t)(ble_gls_t * p_gls, ble_gls_evt_t * p_evt);
  195. /**@brief Glucose Measurement structure. This contains glucose measurement value. */
  196. typedef struct
  197. {
  198. uint8_t flags; /**< Flags */
  199. uint16_t sequence_number; /**< Sequence number */
  200. ble_date_time_t base_time; /**< Time stamp */
  201. int16_t time_offset; /**< Time offset */
  202. sfloat_t glucose_concentration; /**< Glucose concentration */
  203. uint8_t type; /**< Type */
  204. uint8_t sample_location; /**< Sample location */
  205. uint16_t sensor_status_annunciation; /**< Sensor status annunciation */
  206. } ble_gls_meas_t;
  207. /**@brief Glucose measurement context structure */
  208. typedef struct
  209. {
  210. uint8_t flags; /**< Flags */
  211. uint8_t extended_flags; /**< Extended Flags */
  212. uint8_t carbohydrate_id; /**< Carbohydrate ID */
  213. sfloat_t carbohydrate; /**< Carbohydrate */
  214. uint8_t meal; /**< Meal */
  215. uint8_t tester_and_health; /**< Tester and health */
  216. uint16_t exercise_duration; /**< Exercise Duration */
  217. uint8_t exercise_intensity; /**< Exercise Intensity */
  218. uint8_t medication_id; /**< Medication ID */
  219. sfloat_t medication; /**< Medication */
  220. uint16_t hba1c; /**< HbA1c */
  221. } ble_gls_meas_context_t;
  222. /**@brief Glucose measurement record */
  223. typedef struct
  224. {
  225. ble_gls_meas_t meas; /**< Glucose measurement */
  226. ble_gls_meas_context_t context; /**< Glucose measurement context */
  227. } ble_gls_rec_t;
  228. /**@brief Glucose Service init structure. This contains all options and data needed for
  229. * initialization of the service. */
  230. typedef struct
  231. {
  232. ble_gls_evt_handler_t evt_handler; /**< Event handler to be called for handling events in the Glucose Service. */
  233. ble_srv_error_handler_t error_handler; /**< Function to be called in case of an error. */
  234. uint16_t feature; /**< Glucose Feature value indicating supported features. */
  235. bool is_context_supported; /**< Determines if optional Glucose Measurement Context is to be supported. */
  236. security_req_t gl_meas_cccd_wr_sec; /**< Security requirement for writing glucose measurement characteristic CCCD. */
  237. security_req_t gl_feature_rd_sec; /**< Security requirement for reading glucose feature characteristic. */
  238. security_req_t racp_cccd_wr_sec; /**< Security requirement for writing RACP Characteristic CCCD. */
  239. security_req_t racp_wr_sec; /**< Security requirement for writing RACP Characteristic. (Service specification mandates authentication) */
  240. nrf_ble_gq_t * p_gatt_queue; /**< Pointer to BLE GATT Queue instance. */
  241. } ble_gls_init_t;
  242. /**@brief Glucose Service structure. This contains various status information for the service. */
  243. struct ble_gls_s
  244. {
  245. ble_gls_evt_handler_t evt_handler; /**< Event handler to be called for handling events in the Glucose Service. */
  246. ble_srv_error_handler_t error_handler; /**< Function to be called in case of an error. */
  247. uint16_t service_handle; /**< Handle of Glucose Service (as provided by the BLE stack). */
  248. ble_gatts_char_handles_t glm_handles; /**< Handles related to the Glucose Measurement characteristic. */
  249. ble_gatts_char_handles_t glm_context_handles; /**< Handles related to the Glucose Measurement Context characteristic. */
  250. ble_gatts_char_handles_t glf_handles; /**< Handles related to the Glucose Feature characteristic. */
  251. ble_gatts_char_handles_t racp_handles; /**< Handles related to the Record Access Control Point characteristic. */
  252. 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). */
  253. nrf_ble_gq_t * p_gatt_queue; /**< Pointer to BLE GATT Queue instance. */
  254. uint16_t feature;
  255. bool is_context_supported;
  256. };
  257. /**@brief Function for initializing the Glucose Service.
  258. *
  259. * @details This call allows the application to initialize the Glucose Service.
  260. *
  261. * @param[out] p_gls Glucose Service structure. This structure will have to be supplied by
  262. * the application. It will be initialized by this function, and will later
  263. * be used to identify this particular service instance.
  264. * @param[in] p_gls_init Information needed to initialize the service.
  265. *
  266. * @return NRF_SUCCESS on successful initialization of service, otherwise an error code.
  267. */
  268. uint32_t ble_gls_init(ble_gls_t * p_gls, ble_gls_init_t const * p_gls_init);
  269. /**@brief Function for handling the Application's BLE Stack events.
  270. *
  271. * @details Handles all events from the BLE stack of interest to the Glucose Service.
  272. *
  273. * @param[in] p_ble_evt Event received from the BLE stack.
  274. * @param[in] p_context Glucose Service structure.
  275. */
  276. void ble_gls_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context);
  277. /**@brief Function for reporting a new glucose measurement to the glucose service module.
  278. *
  279. * @details The application calls this function after having performed a new glucose measurement.
  280. * The new measurement is recorded in the RACP database.
  281. *
  282. * @param[in] p_gls Glucose Service structure.
  283. * @param[in] p_rec Pointer to glucose record (measurement plus context).
  284. *
  285. * @return NRF_SUCCESS on success, otherwise an error code.
  286. */
  287. uint32_t ble_gls_glucose_new_meas(ble_gls_t * p_gls, ble_gls_rec_t * p_rec);
  288. #ifdef __cplusplus
  289. }
  290. #endif
  291. #endif // BLE_GLS_H__
  292. /** @} */