• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  ****************************************************************************************
3  *
4  * @file gls.h
5  *
6  * @brief Glucose Service API.
7  *
8  ****************************************************************************************
9  * @attention
10   #####Copyright (c) 2019 GOODIX
11   All rights reserved.
12 
13     Redistribution and use in source and binary forms, with or without
14     modification, are permitted provided that the following conditions are met:
15   * Redistributions of source code must retain the above copyright
16     notice, this list of conditions and the following disclaimer.
17   * Redistributions in binary form must reproduce the above copyright
18     notice, this list of conditions and the following disclaimer in the
19     documentation and/or other materials provided with the distribution.
20   * Neither the name of GOODIX nor the names of its contributors may be used
21     to endorse or promote products derived from this software without
22     specific prior written permission.
23 
24   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27   ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE
28   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34   POSSIBILITY OF SUCH DAMAGE.
35  *****************************************************************************************
36  */
37 
38 /**
39  * @addtogroup BLE_SRV BLE Services
40  * @{
41  * @brief Definitions and prototypes for the BLE Service interface.
42  */
43 
44 /**
45  * @defgroup BLE_SDK_GLS Glucose Service (GLS)
46  * @{
47  * @brief Glucose Service module.
48  *
49  * @details The Glucose Service exposes glucose and other data related to a personal glucose
50  *          sensor for consumer healthcare applications and is not designed for clinical use.
51  *          This module implements the Glucose Service with Glucose Measurement, Glucose Measurement
52  *          Context, Glucose Feature and Record Access Control Point characteristics.
53  *
54  *          After \ref gls_init_t variable is intialized, the application must call \ref gls_service_init()
55  *          to add Glucose Service and Glucose Measurement, Glucose Measurement Context, Glucose
56  *          Feature and Record Access Control Point characteristics to the BLE Stack database
57  *          according to \ref gls_init_t.char_mask.
58  */
59 
60 #ifndef __GLS_H__
61 #define __GLS_H__
62 
63 /*
64  * INCLUDE FILES
65  ****************************************************************************************
66  */
67 #include <stdint.h>
68 #include <stdbool.h>
69 #include "gr55xx_sys.h"
70 #include "custom_config.h"
71 #include "ble_prf_utils.h"
72 
73 /**
74  * @defgroup GLS_MACRO Defines
75  * @{
76  */
77 #define GLS_CONNECTION_MAX          (10 < CFG_MAX_CONNECTIONS ?\
78                                     10 : CFG_MAX_CONNECTIONS)   /**< Maximum number of Glucose Profile connections. */
79 #define GLS_MEAS_VAL_LEN_MAX                20   /**< Maximum length of GLS measurement value. */
80 #define GLS_MEAS_CTX_LEN_MAX                20   /**< Maximum length of GLS measurement context value. */
81 #define GLS_REC_ACCESS_CTRL_LEN_MIN         2    /**< Minimum length of Record Access Control Point packet. */
82 #define GLS_REC_ACCESS_CTRL_LEN_MAX         21   /**< Maximum length of Record Access Control Point packet. */
83 
84 
85 #define GLS_NTF_OF_NULL              0x00       /**< Mask for no notify. */
86 #define GLS_NTF_OF_MEAS              0x01       /**< Mask for measurement notify. */
87 #define GLS_NTF_OF_MEAS_CTX          0x10       /**< Mask for measurement context notify. */
88 
89 #define GLS_ERROR_PROC_IN_PROCESS    0x80       /**< Error code: A previously triggered SC Control Point operation \
90                                                      is still in progress. */
91 #define GLS_ERROR_CCCD_INVALID       0x81       /**< Error code: The Client Characteristic Configuration descriptor \
92                                                      is not configured. */
93 
94 /**
95  * @defgroup GLS_CHAR_MASK Characteristics Mask
96  * @{
97  * @brief Bit masks for the initialization of \ref gls_init_t.char_mask.
98  */
99 #define GLS_CHAR_MANDATORY      0x0f8f /**< Bit mask for mandatory characteristic in GLS. */
100 #define GLS_CHAR_MEAS_CTX_SUP   0x0070 /**< Bit mask for Glucose Measurement Context characteristic that is optional. */
101 #define GLS_CHAR_FULL           0x0fff     /**< Bit mask of the full characteristic. */
102 /** @} */
103 
104 /**
105  * @defgroup GLS_FEAT Glucose Feature
106  * @{
107  * @brief Glucose Service feature.
108  */
109 #define GLS_FEAT_LOW_BATT             (0x01 << 0)     /**< Low Battery Detection During Measurement Supported */
110 #define GLS_FEAT_MALFUNC              (0x01 << 1)     /**< Sensor Malfunction Detection Supported */
111 #define GLS_FEAT_SAMPLE_SIZE          (0x01 << 2)     /**< Sensor Sample Size Supported */
112 #define GLS_FEAT_INSERT_ERR           (0x01 << 3)     /**< Sensor Strip Insertion Error Detection Supported */
113 #define GLS_FEAT_TYPE_ERR             (0x01 << 4)     /**< Sensor Strip Type Error Detection Supported */
114 #define GLS_FEAT_RES_HIGH_LOW         (0x01 << 5)     /**< Sensor Result High-Low Detection Supported */
115 #define GLS_FEAT_TEMP_HIGH_LOW        (0x01 << 6)     /**< Sensor Temperature High-Low Detection Supported */
116 #define GLS_FEAT_READ_INT             (0x01 << 7)     /**< Sensor Read Interrupt Detection Supported */
117 #define GLS_FEAT_GENERAL_FAULT        (0x01 << 8)     /**< General Device Fault Supported */
118 #define GLS_FEAT_TIME_FAULT           (0x01 << 9)     /**< Time Fault Supported */
119 #define GLS_FEAT_MULTI_BOND           (0x01 << 10)    /**< Multiple Bond Supported */
120 #define GLS_FEAT_FULL                 (0x07ff)         /**< All feature Supported. */
121 /** @} */
122 
123 
124 /**
125  * @defgroup GLS_MEAS_FLAG Measurement Flag
126  * @{
127  * @brief Glucose Measurement Flags.
128  */
129 #define GLS_MEAS_FLAG_TIME_OFFSET       0x01    /**< Time Offset Present */
130 #define GLS_MEAS_FLAG_CONC_TYPE_LOC     0x02    /**< Glucose Concentration, Type, and Sample Location Present */
131 #define GLS_MEAS_FLAG_UNITS_KG_L        0x00    /**< Glucose Concentration Units kg/L */
132 #define GLS_MEAS_FLAG_UNITS_MOL_L       0x04    /**< Glucose Concentration Units mol/L */
133 #define GLS_MEAS_FLAG_SENSOR_STATUS     0x08    /**< Sensor Status Annunciation Present */
134 #define GLS_MEAS_FLAG_CONTEXT_INFO      0x10    /**< Context Information Follows */
135 /** @} */
136 
137 /**
138  * @defgroup GLS_MEAS_CTX_FLAG Measurement Context Flag
139  * @{
140  * @brief Glucose measurement context flags
141  */
142 #define GLS_MEAS_CTX_FLAG_CARB          (0x01 << 0)    /**< Carbohydrate id and carbohydrate present */
143 #define GLS_MEAS_CTX_FLAG_MEAL          (0x01 << 1)    /**< Meal present */
144 #define GLS_MEAS_CTX_FLAG_TESTER        (0x01 << 2)    /**< Tester-health present */
145 #define GLS_MEAS_CTX_FLAG_EXERCISE      (0x01 << 3)    /**< Exercise duration and exercise intensity present */
146 #define GLS_MEAS_CTX_FLAG_MED           (0x01 << 4)    /**< Medication ID and medication present */
147 #define GLS_MEAS_CTX_FLAG_MED_KG        (0x01 << 5)    /**< Medication value units, kilograms */
148 #define GLS_MEAS_CTX_FLAG_MED_L         (0x01 << 6)    /**< Medication value units, liters */
149 #define GLS_MEAS_CTX_FLAG_HBA1C         (0x01 << 7)    /**< Hba1c present */
150 #define GLS_MEAS_CTX_FLAG_EXT           (0x01 << 8)    /**< Extended flags present */
151 /** @} */
152 
153 /**
154  * @defgroup GLS_MEAS_STATUS Measurement Status
155  * @{
156  * @brief Glucose measurement status annunciation.
157  */
158 #define GLS_MEAS_STATUS_BATT_LOW       (0x01 << 0)      /**< Device battery low at time of measurement */
159 #define GLS_MEAS_STATUS_SENSOR_FAULT   (0x01 << 1)      /**< Sensor malfunction or faulting at time of measurement */
160 #define GLS_MEAS_STATUS_SAMPLE_SIZE    (0x01 << 2)      /**< Sample size for blood or control solution insufficient \
161                                                              at time of measurement */
162 #define GLS_MEAS_STATUS_STRIP_INSERT   (0x01 << 3)      /**< Strip insertion error */
163 #define GLS_MEAS_STATUS_STRIP_TYPE     (0x01 << 4)      /**< Strip type incorrect for device */
164 #define GLS_MEAS_STATUS_RESULT_HIGH    (0x01 << 5)      /**< Sensor result higher than the device can process */
165 #define GLS_MEAS_STATUS_RESULT_LOW     (0x01 << 6)      /**< Sensor result lower than the device can process */
166 #define GLS_MEAS_STATUS_TEMP_HIGH      (0x01 << 7)      /**< Sensor temperature too high for valid test/result \
167                                                              at time of measurement */
168 #define GLS_MEAS_STATUS_TEMP_LOW       (0x01 << 8)      /**< Sensor temperature too low for valid test/result \
169                                                              at time of measurement */
170 #define GLS_MEAS_STATUS_STRIP_PULL     (0x01 << 9)      /**< Sensor read interrupted because strip was pulled too soon \
171                                                              at time of measurement */
172 #define GLS_MEAS_STATUS_GENERAL_FAULT  (0x01 << 10)     /**< General device fault has occurred in the sensor */
173 #define GLS_MEAS_STATUS_TIME_FAULT     (0x01 << 11)     /**< Time fault has occurred in the sensor and \
174                                                              time may be inaccurate */
175 /** @} */
176 /** @} */
177 
178 /**
179  * @defgroup GLS_ENUM Enumerations
180  * @{
181  */
182 /**@brief Glucose measurement type */
183 typedef enum {
184     GLS_MEAS_TYPE_CAP_BLOOD = 0x01,     /**< Capillary whole blood */
185     GLS_MEAS_TYPE_CAP_PLASMA,           /**< Capillary plasma */
186     GLS_MEAS_TYPE_VEN_BLOOD,            /**< Venous whole blood */
187     GLS_MEAS_TYPE_VEN_PLASMA,           /**< Venous plasma */
188     GLS_MEAS_TYPE_ART_BLOOD,            /**< Arterial whole blood */
189     GLS_MEAS_TYPE_ART_PLASMA,           /**< Arterial plasma */
190     GLS_MEAS_TYPE_UNDET_BLOOD,          /**< Undetermined whole blood */
191     GLS_MEAS_TYPE_UNDET_PLASMA,         /**< Undetermined plasma */
192     GLS_MEAS_TYPE_FLUID,                /**< Interstitial fluid (ISF) */
193     GLS_MEAS_TYPE_CONTROL,              /**< Control solution */
194 } gls_meas_type_t;
195 
196 /**@brief Glucose measurement location */
197 typedef enum {
198     GLS_MEAS_LOC_FINGER = 0x01,     /**< Finger */
199     GLS_MEAS_LOC_AST,               /**< Alternate Site Test (AST) */
200     GLS_MEAS_LOC_EAR,               /**< Earlobe */
201     GLS_MEAS_LOC_CONTROL,           /**< Control solution */
202     GLS_MEAS_LOC_NOT_AVAIL = 0x0f,  /**< Sample Location value not available */
203 } gls_meas_loc_t;
204 
205 /**@brief Glucose measurement context carbohydrate ID */
206 typedef enum {
207     GLS_MEAS_CTX_CARB_BREAKFAST = 0x01,      /**< Breakfast */
208     GLS_MEAS_CTX_CARB_LUNCH,                 /**< Lunch */
209     GLS_MEAS_CTX_CARB_DINNER,                /**< Dinner */
210     GLS_MEAS_CTX_CARB_SNACK,                 /**< Snack */
211     GLS_MEAS_CTX_CARB_DRINK,                 /**< Drink */
212     GLS_MEAS_CTX_CARB_SUPPER,                /**< Supper */
213     GLS_MEAS_CTX_CARB_BRUNCH,                /**< Brunch */
214 } gls_meas_ctx_carb_id_t;
215 
216 /**@brief Glucose measurement context meal */
217 typedef enum {
218     GLS_MEAS_CTX_MEAL_PREPRANDIAL  = 0x01,   /**< Preprandial (before meal) */
219     GLS_MEAS_CTX_MEAL_POSTPRANDIAL,          /**< Postprandial (after meal) */
220     GLS_MEAS_CTX_MEAL_FASTING,               /**< Fasting */
221     GLS_MEAS_CTX_MEAL_CASUAL,                /**< Casual (snacks, drinks, etc.) */
222     GLS_MEAS_CTX_MEAL_BEDTIME,               /**< Bedtime */
223 } gls_meas_ctx_meal_t;
224 
225 /**@brief Glucose measurement context tester */
226 typedef enum {
227     GLS_MEAS_CTX_TESTER_SELF       = 0x01,       /**< Self */
228     GLS_MEAS_CTX_TESTER_PRO,                     /**< Health care professional */
229     GLS_MEAS_CTX_TESTER_LAB,                     /**< Lab test */
230     GLS_MEAS_CTX_TESTER_NOT_AVAIL  = 0x0f,       /**< Tester value not available */
231 } gls_meas_ctx_tester_t;
232 
233 /**@brief Glucose measurement context health */
234 typedef enum {
235     GLS_MEAS_CTX_HEALTH_MINOR      = 0x01,       /**< Minor health issues */
236     GLS_MEAS_CTX_HEALTH_MAJOR,                   /**< Major health issues */
237     GLS_MEAS_CTX_HEALTH_MENSES,                  /**< During menses */
238     GLS_MEAS_CTX_HEALTH_STRESS,                  /**< Under stress */
239     GLS_MEAS_CTX_HEALTH_NONE,                    /**< No health issues */
240     GLS_MEAS_CTX_HEALTH_NOT_AVAIL  = 0x0f,       /**< Health value not available */
241 } gls_meas_ctx_health_t;
242 
243 /**@brief Glucose measurement context medication ID */
244 typedef enum {
245     GLS_MEAS_CTX_MED_RAPID    = 0x01,    /**< Rapid acting insulin */
246     GLS_MEAS_CTX_MED_SHORT,              /**< Short acting insulin */
247     GLS_MEAS_CTX_MED_INTERMED,           /**< Intermediate acting insulin */
248     GLS_MEAS_CTX_MED_LONG,               /**< Long acting insulin */
249     GLS_MEAS_CTX_MED_PREMIX,             /**< Pre-mixed insulin */
250 } gls_meas_ctx_medic_id_t;
251 
252 /**@brief Glucose Service event type. */
253 typedef enum {
254     GLS_EVT_INVALID = 0x00,                 /**< Invalid event. */
255     GLS_EVT_MEAS_NOTIFICATION_ENABLED,      /**< Glucose Measurement notification enabled event. */
256     GLS_EVT_MEAS_NOTIFICATION_DISABLED,     /**< Glucose Measurement notification disabled event. */
257     GLS_EVT_CTX_NOTIFICATION_ENABLED,       /**< Glucose Measurement Context notification enabled event. */
258     GLS_EVT_CTX_NOTIFICATION_DISABLED,      /**< Glucose Measurement Context notification disabled event. */
259     GLS_EVT_CTRL_INDICATION_ENABLED,        /**< Record Access Control Point indication enabled event. */
260     GLS_EVT_CTRL_INDICATION_DISABLED,       /**< Record Access Control Point indication disabled event. */
261     GLS_EVT_CTRL_WRITE,                     /**< Record Access Control Point write. */
262 } gls_evt_type_t;
263 /** @} */
264 
265 /**
266  * @defgroup GLS_STRUCT Structures
267  * @{
268  */
269 /**@brief SFLOAT format (IEEE-11073 16-bit FLOAT, defined as a 16-bit value with 12-bit mantissa and 4-bit exponent). */
270 typedef struct {
271     int8_t  exponent;            /**< Base 10 exponent, only 4 bits. */
272     int16_t mantissa;            /**< Mantissa, only 12 bits. */
273 } ieee_float16_t;
274 
275 /**@brief Glucose Service event. */
276 typedef struct {
277     gls_evt_type_t  evt_type;    /**< The GLS event type. */
278     uint8_t         conn_idx;    /**< The index of the connection. */
279     const uint8_t  *p_data;      /**< Pointer to event data. */
280     uint16_t        length;      /**< Length of event data. */
281 } gls_evt_t;
282 /** @} */
283 
284 /**
285  * @defgroup GLS_TYPEDEF Typedefs
286  * @{
287  */
288 /**@brief Glucose Service event handler type.*/
289 typedef void (*gls_evt_handler_t)(gls_evt_t *p_evt);
290 /** @} */
291 
292 /**
293  * @defgroup GLS_STRUCT Structures
294  * @{
295  */
296 /**@brief Glucose Measurement structure. This contains glucose measurement value. */
297 typedef struct {
298     uint8_t         flags;                       /**< Flags. */
299     uint16_t        sequence_number;             /**< Sequence number. */
300     prf_date_time_t base_time;                   /**< Time stamp. */
301     int16_t         time_offset;                 /**< Time offset. */
302     ieee_float16_t  glucose_concentration;       /**< Glucose concentration. */
303     uint8_t         type;                        /**< Type. */
304     uint8_t         sample_location;             /**< Sample location. */
305     uint16_t        sensor_status_annunciation;  /**< Sensor status annunciation. */
306 } gls_meas_val_t;
307 
308 /**@brief Glucose measurement context structure */
309 typedef struct {
310     uint8_t         flags;                  /**< Flags. */
311     uint8_t         extended_flags;         /**< Extended Flags. */
312     uint8_t         carbohydrate_id;        /**< Carbohydrate ID. */
313     ieee_float16_t  carbohydrate;           /**< Carbohydrate. */
314     uint8_t         meal;                   /**< Meal. */
315     uint8_t         tester_and_health;      /**< Tester and health. */
316     uint16_t        exercise_duration;      /**< Exercise Duration. */
317     uint8_t         exercise_intensity;     /**< Exercise Intensity. */
318     uint8_t         medication_id;          /**< Medication ID. */
319     ieee_float16_t  medication;             /**< Medication. */
320     uint16_t        hba1c;                  /**< HbA1c. */
321 } gls_meas_ctx_t;
322 
323 /**@brief Glucose measurement record */
324 typedef struct {
325     gls_meas_val_t  meas_val;      /**< Glucose measurement value. */
326     gls_meas_ctx_t  meas_ctx;      /**< Glucose measurement context. */
327 } gls_rec_t;
328 
329 /**@brief Glucose Service init stucture. This contains all option and data needed for initialization of the service. */
330 typedef struct {
331     gls_evt_handler_t  evt_handler;            /**< Glucose Service event handler. */
332     uint16_t
333     char_mask;              /**< Initial mask of supported characteristics, and configured with \ref GLS_CHAR_MASK. */
334     uint16_t           feature;                /**< Initial value for features. */
335 } gls_init_t;
336 /** @} */
337 
338 /**
339  * @defgroup GLS_FUNCTION Functions
340  * @{
341  */
342 /**
343  *****************************************************************************************
344  * @brief Initialize a Glucose Service instance and add in the DB.
345  *
346  * @param[in] p_gls_init: Pointer to gls_init_t Service initialization variable
347  *
348  * @return Result of service initialization.
349  *****************************************************************************************
350  */
351 sdk_err_t gls_service_init(gls_init_t *p_gls_init);
352 
353 /**
354  *****************************************************************************************
355  * @brief Record a new glucose measurement.
356  *
357  * @param[in] p_rec: Pointer to glucose record (measurement plus context).
358  *
359  * @return If record successfully or not.
360  *****************************************************************************************
361  */
362 bool gls_new_meas_record(gls_rec_t *p_rec);
363 
364 /**
365  *****************************************************************************************
366  * @brief Send RACP responce if indication has been enabled.
367  *
368  * @param[in] conn_idx: Connnection index.
369  * @param[in] p_data:   Pointer to data.
370  * @param[in] length:   Length of data.
371  *
372  * @return Result of indicate value
373  *****************************************************************************************
374  */
375 sdk_err_t gls_racp_rsp_send(uint8_t conn_idx, uint8_t *p_data, uint16_t length);
376 /** @} */
377 
378 #endif
379 /** @} */
380 /** @} */
381 
382