1 /** 2 ***************************************************************************************** 3 * 4 * @file bcs.h 5 * 6 * @brief Body Composition 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_BCS Body Composition Service (BCS) 46 * @{ 47 * @brief Definitions and prototypes for the BCS interface. 48 * 49 * @details The Body Composition Service (BCS) exposes data related to body composition from a 50 * body composition analyzer (Server) intended for consumer healthcare as well as 51 * sports/fitness applications. This module implements the Body Compositon Service 52 * with the Body Composition Feature and Body Composition Measurement characteristics. 53 * 54 * After \ref bcs_init_t variable is initialized, the application must call \ref bcs_service_init() 55 * to optionally add the Body Compisition Service, Body Composition Meaturement characteristics to 56 * the BLE stack database according to \ref bcs_init_t.char_mask. 57 */ 58 59 #ifndef _BCS_H_ 60 #define _BCS_H_ 61 62 #include <stdint.h> 63 #include <stdbool.h> 64 #include "gr55xx_sys.h" 65 #include "custom_config.h" 66 #include "ble_prf_utils.h" 67 68 /** 69 * @defgroup BCS_MACRO Defines 70 * @{ 71 */ 72 #define BCS_CONNECTION_MAX (10 < CFG_MAX_CONNECTIONS ? \ 73 10 : CFG_MAX_CONNECTIONS) /**< Maximum number of Body Composition Service connections. */ 74 #define BCS_MEAS_VAL_LEN_MAX 20 /**< Maximum length of BC Measurment value. */ 75 #define BCS_FEAT_VAL_LEN_MAX 4 /**< Maximum length of BC Feature value. */ 76 77 #define INDI_PAYLOAD_HEADER_LEN 3 /**< The length of indication payload header. */ 78 79 /** 80 * @defgroup BCS_MEAS_PACKETS_INDEX Measurement packets index 81 * @{ 82 * @brief BCS Measurement Packet Index. 83 * The least value of MTU is 23 octets. The size of Attribute Value is (23-3) 84 * octets which could be less than the size (30 octets) of all fields of Body 85 * Composition Measurement. If the required data exceeds the current size (MTU - 3) 86 * octets, the remaining optional fields shall be sent in the subsequent indication. 87 * So we need no more than 2 packets. 88 */ 89 #define NUM_PACKETS 2 /**< Measurement Packet numbers. */ 90 #define MEAS_PACKET_FIRST 0 /**< The first Measurement Packet. */ 91 #define MEAS_PACKET_SUB 1 /**< The second Measurement Packet. */ 92 /** @} */ 93 94 #define BCS_CACHE_MEAS_NUM_MAX 25 /**< Maximum number of cache muasurements value for each user. */ 95 96 #define BCS_MEAS_UNSUCCESS 0xFFFF /**< Measurement unsuccessful. */ 97 98 /** 99 * @defgroup BCS_CHAR_MASK Characteristics Mask 100 * @{ 101 * @brief Bit masks for the initialization of \ref bcs_init_t.char_mask. 102 */ 103 #define BCS_CHAR_FEAT_MANDATORY 0x3F /**< Bit mask for mandatory characteristic in BCS. */ 104 /** @} */ 105 /** @} */ 106 107 108 /** 109 * @defgroup BCS_ENUM Enumerations 110 * @{ 111 */ 112 /** 113 * @defgroup BCS_MEAS_FLAG_BIT Measurement Flag Bits 114 * @{ 115 */ 116 /**@brief Body Composition Measurement Flags. */ 117 enum bcs_meas_flag_bits { 118 BCS_MEAS_FLAG_UNIT_SI = 0x0000, /**< Flag bit for SI Measurement Units Present. */ 119 BCS_MEAS_FLAG_UNIT_IMPERIAL = 0x0001, /**< Flag bit for Imperial Measurement Units Present. */ 120 BCS_MEAS_FLAG_DATE_TIME_PRESENT = 0x0002, /**< Flag bit for Time Stamp Present. */ 121 BCS_MEAS_FLAG_USER_ID_PRESENT = 0x0004, /**< Flag bit for User ID Present. */ 122 BCS_MEAS_FLAG_BASAL_METABOLISM = 0x0008, /**< Flag bit for Basal Metabolism Present. */ 123 BCS_MEAS_FLAG_MUSCLE_PERCENTAGE = 0x0010, /**< Flag bit for Muscle Percentage Present. */ 124 BCS_MEAS_FLAG_MUSCLE_MASS = 0x0020, /**< Flag bit for Muscle Mass Present. */ 125 BCS_MEAS_FLAG_FAT_FREE_MASS = 0x0040, /**< Flag bit for Fat Free Mass Present. */ 126 BCS_MEAS_FLAG_SOFT_LEAN_MASS = 0x0080, /**< Flag bit for Soft Lean Mass Present. */ 127 BCS_MEAS_FLAG_BODY_WATER_MASS = 0x0100, /**< Flag bit for Body Water Mass Present. */ 128 BCS_MEAS_FLAG_IMPEDANCE = 0x0200, /**< Flag bit for Impedance Present. */ 129 BCS_MEAS_FLAG_WEIGHT = 0x0400, /**< Flag bit for Weight Present. */ 130 BCS_MEAS_FLAG_HEIGHT = 0x0800, /**< Flag bit for Height Present. */ 131 BCS_MEAS_FLAG_MUTI_PACKET = 0x1000, /**< Flag bit for Multiple Packet Measurement Present. */ 132 }; 133 /** @} */ 134 135 /**@brief Body Composition Feature characteristic bit values. */ 136 typedef enum { 137 /* Supported Flags */ 138 BCS_FEAT_TIME_STAMP = 0x00000001, /**< Time Stamp supported */ 139 BCS_FEAT_MULTI_USER = 0x00000002, /**< Multiple Users supported */ 140 BCS_FEAT_BASAL_METABOLISM = 0x00000004, /**< Basal metabolism supported */ 141 BCS_FEAT_MUSCLE_PERCENTAGE = 0x00000008, /**< Muscle percentage supported */ 142 BCS_FEAT_MUSCLE_MASS = 0x00000010, /**< Muscle mass supported */ 143 BCS_FEAT_FAT_FREE_MASS = 0x00000020, /**< Fat free mass supported */ 144 BCS_FEAT_SOFT_LEAN_MASS = 0x00000040, /**< Soft lean mass supported */ 145 BCS_FEAT_BODY_WATER_MASS = 0x00000080, /**< Body water mass supported */ 146 BCS_FEAT_IMPEDANCE = 0x00000100, /**< Impedance supported */ 147 BCS_FEAT_WEIGHT = 0x00000200, /**< Weight supported */ 148 BCS_FEAT_HEIGHT = 0x00000400, /**< Height supported */ 149 150 /* Mass Resolution */ 151 BCS_FEAT_MASS_RES_500G = 0x00000800, /**< Resolution of 0.5kg or 1lb */ 152 BCS_FEAT_MASS_RES_200G = 0x00001000, /**< Resolution of 0.2kg or 0.5lb */ 153 BCS_FEAT_MASS_RES_100G = 0x00001800, /**< Resolution of 0.1kg or 0.2lb */ 154 BCS_FEAT_MASS_RES_50G = 0x00002000, /**< Resolution of 0.05kg or 0.1lb */ 155 BCS_FEAT_MASS_RES_20G = 0x00002800, /**< Resolution of 0.02kg or 0.05lb */ 156 BCS_FEAT_MASS_RES_10G = 0x00003000, /**< Resolution of 0.01kg or 0.02lb */ 157 BCS_FEAT_MASS_RES_5G = 0x00003800, /**< Resolution of 0.005kg or 0.01lb */ 158 159 /* Height Resolution */ 160 BCS_FEAT_HEIGHT_RES_10MM = 0x00008000, /**< Resolution of 0.01m or 1in */ 161 BCS_FEAT_HEIGHT_RES_5MM = 0x00010000, /**< Resolution of 0.005m or 0.5in */ 162 BCS_FEAT_HEIGHT_RES_1MM = 0x00018000, /**< Resolution of 0.001m or 0.1in */ 163 164 BCS_FEAT_FULL_BIT = 0x0001BFFF, 165 } bcs_feature_t; 166 167 /**@brief BCS Weight Measurement resolutions. */ 168 typedef enum { 169 BCS_MASS_RES_500G, /**< Resolution of 0.5kg or 1lb. */ 170 BCS_MASS_RES_200G, /**< Resolution of 0.2kg or 0.5lb. */ 171 BCS_MASS_RES_100G, /**< Resolution of 0.1kg or 0.2lb. */ 172 BCS_MASS_RES_50G, /**< Resolution of 0.05kg or 0.1lb. */ 173 BCS_MASS_RES_20G, /**< Resolution of 0.02kg or 0.05lb. */ 174 BCS_MASS_RES_10G, /**< Resolution of 0.01kg or 0.02lb. */ 175 BCS_MASS_RES_5G, /**< Resolution of 0.005kg or 0.01lb. */ 176 } bcs_mass_res_t; 177 178 /**@brief BCS Height Measurement resolutions. */ 179 typedef enum { 180 BCS_HEIGHT_RES_10MM, /**< Resolution of 0.01m or 1in. */ 181 BCS_HEIGHT_RES_5MM, /**< Resolution of 0.005m or 0.5in. */ 182 BCS_HEIGHT_RES_1MM, /**< Resolution of 0.001m or 0.1in. */ 183 } bcs_height_res_t; 184 185 /**@brief BCS unit types. */ 186 typedef enum { 187 BCS_UNIT_SI, /**< Weight in kilograms and height in meters */ 188 BCS_UNIT_IMPERIAL, /**< Weight in pounds and height in inches */ 189 } bcs_unit_t; 190 191 /**@brief Body Composition Service event type. */ 192 typedef enum { 193 BCS_EVT_INVALID, /**< Indicate that invalid event. */ 194 BCS_EVT_MEAS_INDICATION_ENABLE, /**< Indicate that body composition measurement indication has been enabled. */ 195 BCS_EVT_MEAS_INDICATION_DISABLE, /**< Indicate that body composition measurement indication has been disabled. */ 196 BCS_EVT_MEAS_INDICATION_CPLT, /**< Indicate that BC Measurement has been indicated. */ 197 BCS_EVT_MEAS_READ_CHARACTERISTIC, /**< The peer reads the characteristic. */ 198 } bcs_evt_type_t; 199 /** @} */ 200 201 /** 202 * @defgroup BCS_STRUCT Structures 203 * @{ 204 */ 205 /**@brief Body composition Measurement flag data. */ 206 typedef struct { 207 uint8_t time_stamp_present : 1; /**< Time Stamp flag. */ 208 uint8_t user_id_present : 1; /**< User ID flag. */ 209 uint8_t basal_metabolism_present : 1; /**< Basal Metabolism flag. */ 210 uint8_t muscle_percentage_present : 1; /**< Muscle Percentage flag. */ 211 uint8_t muscle_mass_present : 1; /**< Muscle Mass flag. */ 212 uint8_t fat_free_mass_present : 1; /**< Fat Free Mass flag. */ 213 uint8_t soft_lean_mass_present : 1; /**< Soft Lean Mass flag. */ 214 uint8_t body_water_mass_present : 1; /**< Body Water Mass flag. */ 215 uint8_t impedance_present : 1; /**< Impedance flag. */ 216 uint8_t weight_present : 1; /**< Weight flag. */ 217 uint8_t height_present : 1; /**< Height flag. */ 218 } bcs_meas_flag_t; 219 220 /**@brief Body composition Measurement data. */ 221 typedef struct { 222 uint16_t body_fat_percentage; /**< Body Fat Percentage data. */ 223 prf_date_time_t time_stamp; /**< Time Stamp data. */ 224 uint8_t user_id; /**< User Index data. */ 225 uint16_t basal_metabolism; /**< Basal Metabolism data. */ 226 uint16_t muscle_percentage; /**< Muscle Percentage data. */ 227 uint16_t muscle_mass; /**< Muscle Mass data. */ 228 uint16_t fat_free_mass; /**< Fat Free Mass data. */ 229 uint16_t soft_lean_mass; /**< Soft Lean Mass data. */ 230 uint16_t body_water_mass; /**< Body Water Mass data. */ 231 uint16_t impedance; /**< Impedance data. */ 232 uint16_t weight; /**< Weight data. */ 233 uint16_t height; /**< Height data. */ 234 } bcs_meas_val_t; 235 236 /**@brief Body Composition Service event. */ 237 typedef struct { 238 bcs_evt_type_t evt_type; /**< The BCS event type. */ 239 uint8_t conn_idx; /**< The index of the connection. */ 240 const uint8_t *p_data; /**< Pointer to event data. */ 241 uint16_t length; /**< Length of event data. */ 242 } bcs_evt_t; 243 /** @} */ 244 245 /** 246 * @defgroup BCS_TYPEDEF Typedefs 247 * @{ 248 */ 249 /**@brief Body Composition Service event handler type. */ 250 typedef void (*bcs_evt_handler_t)(bcs_evt_t *p_evt); 251 /** @} */ 252 253 /** 254 * @defgroup BCS_STRUCT Structures 255 * @{ 256 */ 257 /**@brief Body Composition Service Init variable. */ 258 typedef struct { 259 bcs_evt_handler_t evt_handler; /**< Body Composition Service event handler. */ 260 uint32_t feature; /**< Initial value for features. */ 261 uint8_t 262 char_mask; /**< Initial mask of Supported characteristics, and configured with \ref BCS_CHAR_MASK */ 263 bcs_unit_t bcs_unit; /**< Initial unit system as SI or Imperial. */ 264 bcs_meas_flag_t bcs_meas_flags; /**< Initial measurement flags. */ 265 bcs_mass_res_t bcs_mass_res; /**< Initial resolution of mass value. */ 266 bcs_height_res_t bcs_height_res; /**< Initial resolution of height value. */ 267 } bcs_init_t; 268 /** @} */ 269 270 /** 271 * @defgroup BCS_FUNCTION Functions 272 * @{ 273 */ 274 /** 275 ***************************************************************************************** 276 * @brief Initialize a Body Composition Service instance and add in the DB. 277 * 278 * @param[in] p_bcs_init: Pointer to BC Service initialization variable. 279 * 280 * @return Result of service initialization. 281 ***************************************************************************************** 282 */ 283 sdk_err_t bcs_service_init(bcs_init_t *p_bcs_init, uint16_t *p_bcs_start_handle); 284 285 /**@brief Send Body Composition Measurement indication. 286 ***************************************************************************************** 287 * 288 * @param[in] conn_idx: Connection index. 289 * @param[in] p_meas: Pointer to body composition measurement data. 290 * @param[in] cache_num: The number of measurement caches. 291 * 292 * @return Result of indicate value. 293 ***************************************************************************************** 294 */ 295 sdk_err_t bcs_measurement_send(uint8_t conn_idx, bcs_meas_val_t *p_meas, uint8_t cache_num); 296 /** @} */ 297 298 #endif 299 /** @} */ 300 /** @} */ 301 302