heartrateservice.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. /**
  2. * @file
  3. * @author chipsea
  4. * @brief
  5. * @version 0.1
  6. * @date 2020-11-30
  7. * @copyright Copyright (c) 2020, CHIPSEA Co., Ltd.
  8. * @note
  9. */
  10. /**************************************************************************************************
  11. Filename: heartrateservice.c
  12. Revised:
  13. Revision:
  14. Description: This file contains the Heart Rate sample service
  15. for use with the Heart Rate sample application.
  16. **************************************************************************************************/
  17. /*********************************************************************
  18. * INCLUDES
  19. */
  20. #include "bcomdef.h"
  21. #include "OSAL.h"
  22. #include "linkdb.h"
  23. #include "att.h"
  24. #include "gatt.h"
  25. #include "gatt_uuid.h"
  26. #include "gatt_profile_uuid.h"
  27. #include "gattservapp.h"
  28. #include "heartrateservice.h"
  29. #include "log.h"
  30. /*********************************************************************
  31. * MACROS
  32. */
  33. /*********************************************************************
  34. * CONSTANTS
  35. */
  36. // Position of heart rate measurement value in attribute array
  37. #define HEARTRATE_MEAS_VALUE_POS 2
  38. /*********************************************************************
  39. * TYPEDEFS
  40. */
  41. /*********************************************************************
  42. * GLOBAL VARIABLES
  43. */
  44. // Heart rate service
  45. CONST uint8 heartRateServUUID[ATT_BT_UUID_SIZE] =
  46. {
  47. LO_UINT16(HEARTRATE_SERV_UUID), HI_UINT16(HEARTRATE_SERV_UUID)
  48. };
  49. // Heart rate measurement characteristic
  50. CONST uint8 heartRateMeasUUID[ATT_BT_UUID_SIZE] =
  51. {
  52. LO_UINT16(HEARTRATE_MEAS_UUID), HI_UINT16(HEARTRATE_MEAS_UUID)
  53. };
  54. // Sensor location characteristic
  55. CONST uint8 heartRateSensLocUUID[ATT_BT_UUID_SIZE] =
  56. {
  57. LO_UINT16(BODY_SENSOR_LOC_UUID), HI_UINT16(BODY_SENSOR_LOC_UUID)
  58. };
  59. // Command characteristic
  60. CONST uint8 heartRateCommandUUID[ATT_BT_UUID_SIZE] =
  61. {
  62. LO_UINT16(HEARTRATE_CTRL_PT_UUID), HI_UINT16(HEARTRATE_CTRL_PT_UUID)
  63. };
  64. /*********************************************************************
  65. * EXTERNAL VARIABLES
  66. */
  67. /*********************************************************************
  68. * EXTERNAL FUNCTIONS
  69. */
  70. /*********************************************************************
  71. * LOCAL VARIABLES
  72. */
  73. static heartRateServiceCB_t heartRateServiceCB;
  74. /*********************************************************************
  75. * Profile Attributes - variables
  76. */
  77. // Heart Rate Service attribute
  78. static CONST gattAttrType_t heartRateService = { ATT_BT_UUID_SIZE, heartRateServUUID };
  79. // Heart Rate Measurement Characteristic
  80. // Note characteristic value is not stored here
  81. static uint8 heartRateMeasProps = GATT_PROP_NOTIFY;
  82. static uint8 heartRateMeas = 0;
  83. static gattCharCfg_t heartRateMeasClientCharCfg[GATT_MAX_NUM_CONN];
  84. // Sensor Location Characteristic
  85. static uint8 heartRateSensLocProps = GATT_PROP_READ;
  86. static uint8 heartRateSensLoc = 0;
  87. // Command Characteristic
  88. static uint8 heartRateCommandProps = GATT_PROP_WRITE;
  89. static uint8 heartRateCommand = 0;
  90. /*********************************************************************
  91. * Profile Attributes - Table
  92. */
  93. static gattAttribute_t heartRateAttrTbl[] =
  94. {
  95. // Heart Rate Service
  96. {
  97. { ATT_BT_UUID_SIZE, primaryServiceUUID }, /* type */
  98. GATT_PERMIT_READ, /* permissions */
  99. 0, /* handle */
  100. (uint8 *)&heartRateService /* pValue */
  101. },
  102. // Heart Rate Measurement Declaration
  103. {
  104. { ATT_BT_UUID_SIZE, characterUUID },
  105. GATT_PERMIT_READ,
  106. 0,
  107. &heartRateMeasProps
  108. },
  109. // Heart Rate Measurement Value
  110. {
  111. { ATT_BT_UUID_SIZE, heartRateMeasUUID },
  112. 0,
  113. 0,
  114. &heartRateMeas
  115. },
  116. // Heart Rate Measurement Client Characteristic Configuration
  117. {
  118. { ATT_BT_UUID_SIZE, clientCharCfgUUID },
  119. GATT_PERMIT_READ | GATT_PERMIT_WRITE,
  120. 0,
  121. (uint8 *) &heartRateMeasClientCharCfg
  122. },
  123. // Sensor Location Declaration
  124. {
  125. { ATT_BT_UUID_SIZE, characterUUID },
  126. GATT_PERMIT_READ,
  127. 0,
  128. &heartRateSensLocProps
  129. },
  130. // Sensor Location Value
  131. {
  132. { ATT_BT_UUID_SIZE, heartRateSensLocUUID },
  133. GATT_PERMIT_READ,
  134. 0,
  135. &heartRateSensLoc
  136. },
  137. // Command Declaration
  138. {
  139. { ATT_BT_UUID_SIZE, characterUUID },
  140. GATT_PERMIT_READ,
  141. 0,
  142. &heartRateCommandProps
  143. },
  144. // Command Value
  145. {
  146. { ATT_BT_UUID_SIZE, heartRateCommandUUID },
  147. GATT_PERMIT_WRITE,
  148. 0,
  149. &heartRateCommand
  150. }
  151. };
  152. /*********************************************************************
  153. * LOCAL FUNCTIONS
  154. */
  155. static uint8 heartRate_ReadAttrCB( uint16 connHandle, gattAttribute_t *pAttr,
  156. uint8 *pValue, uint8 *pLen, uint16 offset, uint8 maxLen );
  157. static bStatus_t heartRate_WriteAttrCB( uint16 connHandle, gattAttribute_t *pAttr,
  158. uint8 *pValue, uint8 len, uint16 offset );
  159. /*********************************************************************
  160. * PROFILE CALLBACKS
  161. */
  162. // Heart Rate Service Callbacks
  163. CONST gattServiceCBs_t heartRateCBs =
  164. {
  165. heartRate_ReadAttrCB, // Read callback function pointer
  166. heartRate_WriteAttrCB, // Write callback function pointer
  167. NULL // Authorization callback function pointer
  168. };
  169. /*********************************************************************
  170. * PUBLIC FUNCTIONS
  171. */
  172. /*********************************************************************
  173. * @fn HeartRate_AddService
  174. *
  175. * @brief Initializes the Heart Rate service by registering
  176. * GATT attributes with the GATT server.
  177. *
  178. * @param services - services to add. This is a bit map and can
  179. * contain more than one service.
  180. *
  181. * @return Success or Failure
  182. */
  183. bStatus_t HeartRate_AddService( uint32 services )
  184. {
  185. uint8 status = SUCCESS;
  186. // Initialize Client Characteristic Configuration attributes
  187. GATTServApp_InitCharCfg( INVALID_CONNHANDLE, heartRateMeasClientCharCfg );
  188. if ( services & HEARTRATE_SERVICE )
  189. {
  190. // Register GATT attribute list and CBs with GATT Server App
  191. status = GATTServApp_RegisterService( heartRateAttrTbl,
  192. GATT_NUM_ATTRS( heartRateAttrTbl ),
  193. &heartRateCBs );
  194. }
  195. return ( status );
  196. }
  197. /*********************************************************************
  198. * @fn HeartRate_Register
  199. *
  200. * @brief Register a callback function with the Heart Rate Service.
  201. *
  202. * @param pfnServiceCB - Callback function.
  203. *
  204. * @return None.
  205. */
  206. extern void HeartRate_Register( heartRateServiceCB_t pfnServiceCB )
  207. {
  208. heartRateServiceCB = pfnServiceCB;
  209. }
  210. /*********************************************************************
  211. * @fn HeartRate_SetParameter
  212. *
  213. * @brief Set a Heart Rate parameter.
  214. *
  215. * @param param - Profile parameter ID
  216. * @param len - length of data to right
  217. * @param value - pointer to data to write. This is dependent on
  218. * the parameter ID and WILL be cast to the appropriate
  219. * data type (example: data type of uint16 will be cast to
  220. * uint16 pointer).
  221. *
  222. * @return bStatus_t
  223. */
  224. bStatus_t HeartRate_SetParameter( uint8 param, uint8 len, void *value )
  225. {
  226. bStatus_t ret = SUCCESS;
  227. switch ( param )
  228. {
  229. case HEARTRATE_MEAS_CHAR_CFG:
  230. // Need connection handle
  231. //heartRateMeasClientCharCfg.value = *((uint16*)value);
  232. break;
  233. case HEARTRATE_SENS_LOC:
  234. heartRateSensLoc = *((uint8*)value);
  235. break;
  236. default:
  237. ret = INVALIDPARAMETER;
  238. break;
  239. }
  240. return ( ret );
  241. }
  242. /*********************************************************************
  243. * @fn HeartRate_GetParameter
  244. *
  245. * @brief Get a Heart Rate parameter.
  246. *
  247. * @param param - Profile parameter ID
  248. * @param value - pointer to data to get. This is dependent on
  249. * the parameter ID and WILL be cast to the appropriate
  250. * data type (example: data type of uint16 will be cast to
  251. * uint16 pointer).
  252. *
  253. * @return bStatus_t
  254. */
  255. bStatus_t HeartRate_GetParameter( uint8 param, void *value )
  256. {
  257. bStatus_t ret = SUCCESS;
  258. switch ( param )
  259. {
  260. case HEARTRATE_MEAS_CHAR_CFG:
  261. // Need connection handle
  262. //*((uint16*)value) = heartRateMeasClientCharCfg.value;
  263. break;
  264. case HEARTRATE_SENS_LOC:
  265. *((uint8*)value) = heartRateSensLoc;
  266. break;
  267. case HEARTRATE_COMMAND:
  268. *((uint8*)value) = heartRateCommand;
  269. break;
  270. default:
  271. ret = INVALIDPARAMETER;
  272. break;
  273. }
  274. return ( ret );
  275. }
  276. /*********************************************************************
  277. * @fn HeartRate_MeasNotify
  278. *
  279. * @brief Send a notification containing a heart rate
  280. * measurement.
  281. *
  282. * @param connHandle - connection handle
  283. * @param pNoti - pointer to notification structure
  284. *
  285. * @return Success or Failure
  286. */
  287. bStatus_t HeartRate_MeasNotify( uint16 connHandle, attHandleValueNoti_t *pNoti )
  288. {
  289. uint16 value = GATTServApp_ReadCharCfg( connHandle, heartRateMeasClientCharCfg );
  290. // If notifications enabled
  291. if ( value & GATT_CLIENT_CFG_NOTIFY )
  292. {
  293. // Set the handle
  294. pNoti->handle = heartRateAttrTbl[HEARTRATE_MEAS_VALUE_POS].handle;
  295. // Send the notification
  296. return GATT_Notification( connHandle, pNoti, FALSE );
  297. }
  298. return bleIncorrectMode;
  299. }
  300. /*********************************************************************
  301. * @fn heartRate_ReadAttrCB
  302. *
  303. * @brief Read an attribute.
  304. *
  305. * @param connHandle - connection message was received on
  306. * @param pAttr - pointer to attribute
  307. * @param pValue - pointer to data to be read
  308. * @param pLen - length of data to be read
  309. * @param offset - offset of the first octet to be read
  310. * @param maxLen - maximum length of data to be read
  311. *
  312. * @return Success or Failure
  313. */
  314. static uint8 heartRate_ReadAttrCB( uint16 connHandle, gattAttribute_t *pAttr,
  315. uint8 *pValue, uint8 *pLen, uint16 offset, uint8 maxLen )
  316. {
  317. bStatus_t status = SUCCESS;
  318. // Make sure it's not a blob operation (no attributes in the profile are long)
  319. if ( offset > 0 )
  320. {
  321. return ( ATT_ERR_ATTR_NOT_LONG );
  322. }
  323. uint16 uuid = BUILD_UINT16( pAttr->type.uuid[0], pAttr->type.uuid[1]);
  324. if (uuid == BODY_SENSOR_LOC_UUID)
  325. {
  326. *pLen = 1;
  327. pValue[0] = *pAttr->pValue;
  328. }
  329. else
  330. {
  331. status = ATT_ERR_ATTR_NOT_FOUND;
  332. }
  333. return ( status );
  334. }
  335. /*********************************************************************
  336. * @fn heartRate_WriteAttrCB
  337. *
  338. * @brief Validate attribute data prior to a write operation
  339. *
  340. * @param connHandle - connection message was received on
  341. * @param pAttr - pointer to attribute
  342. * @param pValue - pointer to data to be written
  343. * @param len - length of data
  344. * @param offset - offset of the first octet to be written
  345. *
  346. * @return Success or Failure
  347. */
  348. static bStatus_t heartRate_WriteAttrCB( uint16 connHandle, gattAttribute_t *pAttr,
  349. uint8 *pValue, uint8 len, uint16 offset )
  350. {
  351. bStatus_t status = SUCCESS;
  352. uint16 uuid = BUILD_UINT16( pAttr->type.uuid[0], pAttr->type.uuid[1]);
  353. switch ( uuid )
  354. {
  355. case HEARTRATE_CTRL_PT_UUID:
  356. if ( offset > 0 )
  357. {
  358. status = ATT_ERR_ATTR_NOT_LONG;
  359. }
  360. else if (len != 1)
  361. {
  362. status = ATT_ERR_INVALID_VALUE_SIZE;
  363. }
  364. else if (*pValue != HEARTRATE_COMMAND_ENERGY_EXP)
  365. {
  366. status = HEARTRATE_ERR_NOT_SUP;
  367. }
  368. else
  369. {
  370. *(pAttr->pValue) = pValue[0];
  371. (*heartRateServiceCB)(HEARTRATE_COMMAND_SET);
  372. }
  373. break;
  374. case GATT_CLIENT_CHAR_CFG_UUID:
  375. status = GATTServApp_ProcessCCCWriteReq( connHandle, pAttr, pValue, len,
  376. offset, GATT_CLIENT_CFG_NOTIFY );
  377. if ( status == SUCCESS )
  378. {
  379. uint16 charCfg = BUILD_UINT16( pValue[0], pValue[1] );
  380. (*heartRateServiceCB)( (charCfg == GATT_CFG_NO_OPERATION) ?
  381. HEARTRATE_MEAS_NOTI_DISABLED :
  382. HEARTRATE_MEAS_NOTI_ENABLED );
  383. }
  384. break;
  385. default:
  386. status = ATT_ERR_ATTR_NOT_FOUND;
  387. break;
  388. }
  389. return ( status );
  390. }
  391. /*********************************************************************
  392. * @fn HeartRate_HandleConnStatusCB
  393. *
  394. * @brief Heart Rate Service link status change handler function.
  395. *
  396. * @param connHandle - connection handle
  397. * @param changeType - type of change
  398. *
  399. * @return none
  400. */
  401. void HeartRate_HandleConnStatusCB( uint16 connHandle, uint8 changeType )
  402. {
  403. // Make sure this is not loopback connection
  404. if ( connHandle != LOOPBACK_CONNHANDLE )
  405. {
  406. // Reset Client Char Config if connection has dropped
  407. if ( ( changeType == LINKDB_STATUS_UPDATE_REMOVED ) ||
  408. ( ( changeType == LINKDB_STATUS_UPDATE_STATEFLAGS ) &&
  409. ( !linkDB_Up( connHandle ) ) ) )
  410. {
  411. GATTServApp_InitCharCfg( connHandle, heartRateMeasClientCharCfg );
  412. }
  413. }
  414. }
  415. /*********************************************************************
  416. *********************************************************************/