heartrateservice.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. /**************************************************************************************************
  2. Phyplus Microelectronics Limited confidential and proprietary.
  3. All rights reserved.
  4. IMPORTANT: All rights of this software belong to Phyplus Microelectronics
  5. Limited ("Phyplus"). Your use of this Software is limited to those
  6. specific rights granted under the terms of the business contract, the
  7. confidential agreement, the non-disclosure agreement and any other forms
  8. of agreements as a customer or a partner of Phyplus. You may not use this
  9. Software unless you agree to abide by the terms of these agreements.
  10. You acknowledge that the Software may not be modified, copied,
  11. distributed or disclosed unless embedded on a Phyplus Bluetooth Low Energy
  12. (BLE) integrated circuit, either as a product or is integrated into your
  13. products. Other than for the aforementioned purposes, you may not use,
  14. reproduce, copy, prepare derivative works of, modify, distribute, perform,
  15. display or sell this Software and/or its documentation for any purposes.
  16. YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
  17. PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
  18. INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
  19. NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
  20. PHYPLUS OR ITS SUBSIDIARIES BE LIABLE OR OBLIGATED UNDER CONTRACT,
  21. NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
  22. LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
  23. INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
  24. OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
  25. OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
  26. (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
  27. **************************************************************************************************/
  28. /**************************************************************************************************
  29. Filename: heartrateservice.c
  30. Revised:
  31. Revision:
  32. Description: This file contains the Heart Rate sample service
  33. for use with the Heart Rate sample application.
  34. **************************************************************************************************/
  35. /*********************************************************************
  36. * INCLUDES
  37. */
  38. #include "bcomdef.h"
  39. #include "OSAL.h"
  40. #include "linkdb.h"
  41. #include "att.h"
  42. #include "gatt.h"
  43. #include "gatt_uuid.h"
  44. #include "gatt_profile_uuid.h"
  45. #include "gattservapp.h"
  46. #include "heartrateservice.h"
  47. #include "log.h"
  48. /*********************************************************************
  49. * MACROS
  50. */
  51. /*********************************************************************
  52. * CONSTANTS
  53. */
  54. // Position of heart rate measurement value in attribute array
  55. #define HEARTRATE_MEAS_VALUE_POS 2
  56. /*********************************************************************
  57. * TYPEDEFS
  58. */
  59. /*********************************************************************
  60. * GLOBAL VARIABLES
  61. */
  62. // Heart rate service
  63. CONST uint8 heartRateServUUID[ATT_BT_UUID_SIZE] =
  64. {
  65. LO_UINT16(HEARTRATE_SERV_UUID), HI_UINT16(HEARTRATE_SERV_UUID)
  66. };
  67. // Heart rate measurement characteristic
  68. CONST uint8 heartRateMeasUUID[ATT_BT_UUID_SIZE] =
  69. {
  70. LO_UINT16(HEARTRATE_MEAS_UUID), HI_UINT16(HEARTRATE_MEAS_UUID)
  71. };
  72. // Sensor location characteristic
  73. CONST uint8 heartRateSensLocUUID[ATT_BT_UUID_SIZE] =
  74. {
  75. LO_UINT16(BODY_SENSOR_LOC_UUID), HI_UINT16(BODY_SENSOR_LOC_UUID)
  76. };
  77. // Command characteristic
  78. CONST uint8 heartRateCommandUUID[ATT_BT_UUID_SIZE] =
  79. {
  80. LO_UINT16(HEARTRATE_CTRL_PT_UUID), HI_UINT16(HEARTRATE_CTRL_PT_UUID)
  81. };
  82. /*********************************************************************
  83. * EXTERNAL VARIABLES
  84. */
  85. /*********************************************************************
  86. * EXTERNAL FUNCTIONS
  87. */
  88. /*********************************************************************
  89. * LOCAL VARIABLES
  90. */
  91. static heartRateServiceCB_t heartRateServiceCB;
  92. /*********************************************************************
  93. * Profile Attributes - variables
  94. */
  95. // Heart Rate Service attribute
  96. static CONST gattAttrType_t heartRateService = { ATT_BT_UUID_SIZE, heartRateServUUID };
  97. // Heart Rate Measurement Characteristic
  98. // Note characteristic value is not stored here
  99. static uint8 heartRateMeasProps = GATT_PROP_NOTIFY;
  100. static uint8 heartRateMeas = 0;
  101. static gattCharCfg_t heartRateMeasClientCharCfg[GATT_MAX_NUM_CONN];
  102. // Sensor Location Characteristic
  103. static uint8 heartRateSensLocProps = GATT_PROP_READ;
  104. static uint8 heartRateSensLoc = 0;
  105. // Command Characteristic
  106. static uint8 heartRateCommandProps = GATT_PROP_WRITE;
  107. static uint8 heartRateCommand = 0;
  108. /*********************************************************************
  109. * Profile Attributes - Table
  110. */
  111. static gattAttribute_t heartRateAttrTbl[] =
  112. {
  113. // Heart Rate Service
  114. {
  115. { ATT_BT_UUID_SIZE, primaryServiceUUID }, /* type */
  116. GATT_PERMIT_READ, /* permissions */
  117. 0, /* handle */
  118. (uint8 *)&heartRateService /* pValue */
  119. },
  120. // Heart Rate Measurement Declaration
  121. {
  122. { ATT_BT_UUID_SIZE, characterUUID },
  123. GATT_PERMIT_READ,
  124. 0,
  125. &heartRateMeasProps
  126. },
  127. // Heart Rate Measurement Value
  128. {
  129. { ATT_BT_UUID_SIZE, heartRateMeasUUID },
  130. 0,
  131. 0,
  132. &heartRateMeas
  133. },
  134. // Heart Rate Measurement Client Characteristic Configuration
  135. {
  136. { ATT_BT_UUID_SIZE, clientCharCfgUUID },
  137. GATT_PERMIT_READ | GATT_PERMIT_WRITE,
  138. 0,
  139. (uint8 *) &heartRateMeasClientCharCfg
  140. },
  141. // Sensor Location Declaration
  142. {
  143. { ATT_BT_UUID_SIZE, characterUUID },
  144. GATT_PERMIT_READ,
  145. 0,
  146. &heartRateSensLocProps
  147. },
  148. // Sensor Location Value
  149. {
  150. { ATT_BT_UUID_SIZE, heartRateSensLocUUID },
  151. GATT_PERMIT_READ,
  152. 0,
  153. &heartRateSensLoc
  154. },
  155. // Command Declaration
  156. {
  157. { ATT_BT_UUID_SIZE, characterUUID },
  158. GATT_PERMIT_READ,
  159. 0,
  160. &heartRateCommandProps
  161. },
  162. // Command Value
  163. {
  164. { ATT_BT_UUID_SIZE, heartRateCommandUUID },
  165. GATT_PERMIT_WRITE,
  166. 0,
  167. &heartRateCommand
  168. }
  169. };
  170. /*********************************************************************
  171. * LOCAL FUNCTIONS
  172. */
  173. static uint8 heartRate_ReadAttrCB( uint16 connHandle, gattAttribute_t *pAttr,
  174. uint8 *pValue, uint8 *pLen, uint16 offset, uint8 maxLen );
  175. static bStatus_t heartRate_WriteAttrCB( uint16 connHandle, gattAttribute_t *pAttr,
  176. uint8 *pValue, uint8 len, uint16 offset );
  177. /*********************************************************************
  178. * PROFILE CALLBACKS
  179. */
  180. // Heart Rate Service Callbacks
  181. CONST gattServiceCBs_t heartRateCBs =
  182. {
  183. heartRate_ReadAttrCB, // Read callback function pointer
  184. heartRate_WriteAttrCB, // Write callback function pointer
  185. NULL // Authorization callback function pointer
  186. };
  187. /*********************************************************************
  188. * PUBLIC FUNCTIONS
  189. */
  190. /*********************************************************************
  191. * @fn HeartRate_AddService
  192. *
  193. * @brief Initializes the Heart Rate service by registering
  194. * GATT attributes with the GATT server.
  195. *
  196. * @param services - services to add. This is a bit map and can
  197. * contain more than one service.
  198. *
  199. * @return Success or Failure
  200. */
  201. bStatus_t HeartRate_AddService( uint32 services )
  202. {
  203. uint8 status = SUCCESS;
  204. // Initialize Client Characteristic Configuration attributes
  205. GATTServApp_InitCharCfg( INVALID_CONNHANDLE, heartRateMeasClientCharCfg );
  206. if ( services & HEARTRATE_SERVICE )
  207. {
  208. // Register GATT attribute list and CBs with GATT Server App
  209. status = GATTServApp_RegisterService( heartRateAttrTbl,
  210. GATT_NUM_ATTRS( heartRateAttrTbl ),
  211. &heartRateCBs );
  212. }
  213. return ( status );
  214. }
  215. /*********************************************************************
  216. * @fn HeartRate_Register
  217. *
  218. * @brief Register a callback function with the Heart Rate Service.
  219. *
  220. * @param pfnServiceCB - Callback function.
  221. *
  222. * @return None.
  223. */
  224. extern void HeartRate_Register( heartRateServiceCB_t pfnServiceCB )
  225. {
  226. heartRateServiceCB = pfnServiceCB;
  227. }
  228. /*********************************************************************
  229. * @fn HeartRate_SetParameter
  230. *
  231. * @brief Set a Heart Rate parameter.
  232. *
  233. * @param param - Profile parameter ID
  234. * @param len - length of data to right
  235. * @param value - pointer to data to write. This is dependent on
  236. * the parameter ID and WILL be cast to the appropriate
  237. * data type (example: data type of uint16 will be cast to
  238. * uint16 pointer).
  239. *
  240. * @return bStatus_t
  241. */
  242. bStatus_t HeartRate_SetParameter( uint8 param, uint8 len, void *value )
  243. {
  244. bStatus_t ret = SUCCESS;
  245. switch ( param )
  246. {
  247. case HEARTRATE_MEAS_CHAR_CFG:
  248. // Need connection handle
  249. //heartRateMeasClientCharCfg.value = *((uint16*)value);
  250. break;
  251. case HEARTRATE_SENS_LOC:
  252. heartRateSensLoc = *((uint8*)value);
  253. break;
  254. default:
  255. ret = INVALIDPARAMETER;
  256. break;
  257. }
  258. return ( ret );
  259. }
  260. /*********************************************************************
  261. * @fn HeartRate_GetParameter
  262. *
  263. * @brief Get a Heart Rate parameter.
  264. *
  265. * @param param - Profile parameter ID
  266. * @param value - pointer to data to get. This is dependent on
  267. * the parameter ID and WILL be cast to the appropriate
  268. * data type (example: data type of uint16 will be cast to
  269. * uint16 pointer).
  270. *
  271. * @return bStatus_t
  272. */
  273. bStatus_t HeartRate_GetParameter( uint8 param, void *value )
  274. {
  275. bStatus_t ret = SUCCESS;
  276. switch ( param )
  277. {
  278. case HEARTRATE_MEAS_CHAR_CFG:
  279. // Need connection handle
  280. //*((uint16*)value) = heartRateMeasClientCharCfg.value;
  281. break;
  282. case HEARTRATE_SENS_LOC:
  283. *((uint8*)value) = heartRateSensLoc;
  284. break;
  285. case HEARTRATE_COMMAND:
  286. *((uint8*)value) = heartRateCommand;
  287. break;
  288. default:
  289. ret = INVALIDPARAMETER;
  290. break;
  291. }
  292. return ( ret );
  293. }
  294. /*********************************************************************
  295. * @fn HeartRate_MeasNotify
  296. *
  297. * @brief Send a notification containing a heart rate
  298. * measurement.
  299. *
  300. * @param connHandle - connection handle
  301. * @param pNoti - pointer to notification structure
  302. *
  303. * @return Success or Failure
  304. */
  305. bStatus_t HeartRate_MeasNotify( uint16 connHandle, attHandleValueNoti_t *pNoti )
  306. {
  307. uint16 value = GATTServApp_ReadCharCfg( connHandle, heartRateMeasClientCharCfg );
  308. // If notifications enabled
  309. if ( value & GATT_CLIENT_CFG_NOTIFY )
  310. {
  311. // Set the handle
  312. pNoti->handle = heartRateAttrTbl[HEARTRATE_MEAS_VALUE_POS].handle;
  313. // Send the notification
  314. return GATT_Notification( connHandle, pNoti, FALSE );
  315. }
  316. return bleIncorrectMode;
  317. }
  318. /*********************************************************************
  319. * @fn heartRate_ReadAttrCB
  320. *
  321. * @brief Read an attribute.
  322. *
  323. * @param connHandle - connection message was received on
  324. * @param pAttr - pointer to attribute
  325. * @param pValue - pointer to data to be read
  326. * @param pLen - length of data to be read
  327. * @param offset - offset of the first octet to be read
  328. * @param maxLen - maximum length of data to be read
  329. *
  330. * @return Success or Failure
  331. */
  332. static uint8 heartRate_ReadAttrCB( uint16 connHandle, gattAttribute_t *pAttr,
  333. uint8 *pValue, uint8 *pLen, uint16 offset, uint8 maxLen )
  334. {
  335. bStatus_t status = SUCCESS;
  336. // Make sure it's not a blob operation (no attributes in the profile are long)
  337. if ( offset > 0 )
  338. {
  339. return ( ATT_ERR_ATTR_NOT_LONG );
  340. }
  341. uint16 uuid = BUILD_UINT16( pAttr->type.uuid[0], pAttr->type.uuid[1]);
  342. if (uuid == BODY_SENSOR_LOC_UUID)
  343. {
  344. *pLen = 1;
  345. pValue[0] = *pAttr->pValue;
  346. }
  347. else
  348. {
  349. status = ATT_ERR_ATTR_NOT_FOUND;
  350. }
  351. return ( status );
  352. }
  353. /*********************************************************************
  354. * @fn heartRate_WriteAttrCB
  355. *
  356. * @brief Validate attribute data prior to a write operation
  357. *
  358. * @param connHandle - connection message was received on
  359. * @param pAttr - pointer to attribute
  360. * @param pValue - pointer to data to be written
  361. * @param len - length of data
  362. * @param offset - offset of the first octet to be written
  363. *
  364. * @return Success or Failure
  365. */
  366. static bStatus_t heartRate_WriteAttrCB( uint16 connHandle, gattAttribute_t *pAttr,
  367. uint8 *pValue, uint8 len, uint16 offset )
  368. {
  369. bStatus_t status = SUCCESS;
  370. uint16 uuid = BUILD_UINT16( pAttr->type.uuid[0], pAttr->type.uuid[1]);
  371. switch ( uuid )
  372. {
  373. case HEARTRATE_CTRL_PT_UUID:
  374. if ( offset > 0 )
  375. {
  376. status = ATT_ERR_ATTR_NOT_LONG;
  377. }
  378. else if (len != 1)
  379. {
  380. status = ATT_ERR_INVALID_VALUE_SIZE;
  381. }
  382. else if (*pValue != HEARTRATE_COMMAND_ENERGY_EXP)
  383. {
  384. status = HEARTRATE_ERR_NOT_SUP;
  385. }
  386. else
  387. {
  388. *(pAttr->pValue) = pValue[0];
  389. (*heartRateServiceCB)(HEARTRATE_COMMAND_SET);
  390. }
  391. break;
  392. case GATT_CLIENT_CHAR_CFG_UUID:
  393. status = GATTServApp_ProcessCCCWriteReq( connHandle, pAttr, pValue, len,
  394. offset, GATT_CLIENT_CFG_NOTIFY );
  395. if ( status == SUCCESS )
  396. {
  397. uint16 charCfg = BUILD_UINT16( pValue[0], pValue[1] );
  398. (*heartRateServiceCB)( (charCfg == GATT_CFG_NO_OPERATION) ?
  399. HEARTRATE_MEAS_NOTI_DISABLED :
  400. HEARTRATE_MEAS_NOTI_ENABLED );
  401. }
  402. break;
  403. default:
  404. status = ATT_ERR_ATTR_NOT_FOUND;
  405. break;
  406. }
  407. return ( status );
  408. }
  409. /*********************************************************************
  410. * @fn HeartRate_HandleConnStatusCB
  411. *
  412. * @brief Heart Rate Service link status change handler function.
  413. *
  414. * @param connHandle - connection handle
  415. * @param changeType - type of change
  416. *
  417. * @return none
  418. */
  419. void HeartRate_HandleConnStatusCB( uint16 connHandle, uint8 changeType )
  420. {
  421. // Make sure this is not loopback connection
  422. if ( connHandle != LOOPBACK_CONNHANDLE )
  423. {
  424. // Reset Client Char Config if connection has dropped
  425. if ( ( changeType == LINKDB_STATUS_UPDATE_REMOVED ) ||
  426. ( ( changeType == LINKDB_STATUS_UPDATE_STATEFLAGS ) &&
  427. ( !linkDB_Up( connHandle ) ) ) )
  428. {
  429. GATTServApp_InitCharCfg( connHandle, heartRateMeasClientCharCfg );
  430. }
  431. }
  432. }
  433. /*********************************************************************
  434. *********************************************************************/