battservice.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  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. * INCLUDES
  30. */
  31. #include "bcomdef.h"
  32. #include "types.h"
  33. #include "OSAL.h"
  34. #include "linkdb.h"
  35. #include "att.h"
  36. #include "gatt.h"
  37. #include "gatt_uuid.h"
  38. #include "gatt_profile_uuid.h"
  39. #include "gattservapp.h"
  40. #include "peripheral.h"
  41. #include "hiddev.h"
  42. #include "battservice.h"
  43. /*********************************************************************
  44. * MACROS
  45. */
  46. /*********************************************************************
  47. * CONSTANTS
  48. */
  49. // ADC voltage levels
  50. #define BATT_ADC_LEVEL_3V 409
  51. #define BATT_ADC_LEVEL_2V 273
  52. #define BATT_LEVEL_VALUE_IDX 2 // Position of battery level in attribute array
  53. #define BATT_LEVEL_VALUE_CCCD_IDX 3 // Position of battery level CCCD in attribute array
  54. /*********************************************************************
  55. * TYPEDEFS
  56. */
  57. /*********************************************************************
  58. * GLOBAL VARIABLES
  59. */
  60. // Battery service
  61. CONST uint8 battServUUID[ATT_BT_UUID_SIZE] =
  62. {
  63. LO_UINT16(BATT_SERV_UUID), HI_UINT16(BATT_SERV_UUID)
  64. };
  65. // Battery level characteristic
  66. CONST uint8 battLevelUUID[ATT_BT_UUID_SIZE] =
  67. {
  68. LO_UINT16(BATT_LEVEL_UUID), HI_UINT16(BATT_LEVEL_UUID)
  69. };
  70. /*********************************************************************
  71. * EXTERNAL VARIABLES
  72. */
  73. /*********************************************************************
  74. * EXTERNAL FUNCTIONS
  75. */
  76. /*********************************************************************
  77. * LOCAL VARIABLES
  78. */
  79. // Application callback
  80. static battServiceCB_t battServiceCB;
  81. #ifndef HID_VOICE_SPEC
  82. //static uint16 battMinLevel = BATT_ADC_LEVEL_2V; // For VDD/3 measurements
  83. //static uint16 battMaxLevel = BATT_ADC_LEVEL_3V; // For VDD/3 measurements
  84. #endif
  85. // Critical battery level setting
  86. static uint8 battCriticalLevel;
  87. // ADC channel to be used for reading
  88. /*********************************************************************
  89. * Profile Attributes - variables
  90. */
  91. // Battery Service attribute
  92. static CONST gattAttrType_t battService = { ATT_BT_UUID_SIZE, battServUUID };
  93. // Battery level characteristic
  94. static uint8 battLevelProps = GATT_PROP_READ | GATT_PROP_NOTIFY;
  95. static uint8 battLevel = 100;
  96. #ifdef HID_VOICE_SPEC
  97. // Characteristic Presentation Format of the Battery Level Characteristic.
  98. static gattCharFormat_t battLevelPresentation = {
  99. GATT_FORMAT_UINT8, /* format */
  100. 0, /* exponent */
  101. GATT_UNIT_PERCENTAGE_UUID, /* unit */
  102. GATT_NS_BT_SIG, /* name space */
  103. GATT_DESC_LENGTH_UUID /* desc */
  104. };
  105. #endif
  106. static gattCharCfg_t battLevelClientCharCfg[GATT_MAX_NUM_CONN];
  107. // HID Report Reference characteristic descriptor, battery level
  108. static uint8 hidReportRefBattLevel[HID_REPORT_REF_LEN] =
  109. { HID_RPT_ID_BATT_LEVEL_IN, HID_REPORT_TYPE_INPUT };
  110. /*********************************************************************
  111. * Profile Attributes - Table
  112. */
  113. static gattAttribute_t battAttrTbl[] =
  114. {
  115. // Battery Service
  116. {
  117. { ATT_BT_UUID_SIZE, primaryServiceUUID }, /* type */
  118. GATT_PERMIT_READ, /* permissions */
  119. 0, /* handle */
  120. (uint8 *)&battService /* pValue */
  121. },
  122. // Battery Level Declaration
  123. {
  124. { ATT_BT_UUID_SIZE, characterUUID },
  125. GATT_PERMIT_READ,
  126. 0,
  127. &battLevelProps
  128. },
  129. // Battery Level Value
  130. {
  131. { ATT_BT_UUID_SIZE, battLevelUUID },
  132. GATT_PERMIT_READ,
  133. 0,
  134. &battLevel
  135. },
  136. // Battery Level Client Characteristic Configuration
  137. {
  138. { ATT_BT_UUID_SIZE, clientCharCfgUUID },
  139. GATT_PERMIT_READ | GATT_PERMIT_WRITE,
  140. 0,
  141. (uint8 *) &battLevelClientCharCfg
  142. },
  143. // HID Report Reference characteristic descriptor, batter level input
  144. {
  145. { ATT_BT_UUID_SIZE, reportRefUUID },
  146. GATT_PERMIT_READ,
  147. 0,
  148. hidReportRefBattLevel
  149. },
  150. #ifdef HID_VOICE_SPEC
  151. // Characteristic Presentation format
  152. {
  153. { ATT_BT_UUID_SIZE, charFormatUUID },
  154. GATT_PERMIT_READ,
  155. 0,
  156. (uint8_t *)&battLevelPresentation
  157. },
  158. #endif
  159. };
  160. /*********************************************************************
  161. * LOCAL FUNCTIONS
  162. */
  163. static uint8 battReadAttrCB( uint16 connHandle, gattAttribute_t *pAttr,
  164. uint8 *pValue, uint8 *pLen, uint16 offset, uint8 maxLen );
  165. static bStatus_t battWriteAttrCB( uint16 connHandle, gattAttribute_t *pAttr,
  166. uint8 *pValue, uint8 len, uint16 offset );
  167. static void battNotifyCB( linkDBItem_t *pLinkItem );
  168. static uint8 battMeasure( void );
  169. static void battNotifyLevel( void );
  170. /*********************************************************************
  171. * PROFILE CALLBACKS
  172. */
  173. // Battery Service Callbacks
  174. CONST gattServiceCBs_t battCBs =
  175. {
  176. battReadAttrCB, // Read callback function pointer
  177. battWriteAttrCB, // Write callback function pointer
  178. NULL // Authorization callback function pointer
  179. };
  180. /*********************************************************************
  181. * PUBLIC FUNCTIONS
  182. */
  183. /*********************************************************************
  184. * @fn Batt_AddService
  185. *
  186. * @brief Initializes the Battery Service by registering
  187. * GATT attributes with the GATT server.
  188. *
  189. * @return Success or Failure
  190. */
  191. bStatus_t Batt_AddService( void )
  192. {
  193. uint8 status = SUCCESS;
  194. // Initialize Client Characteristic Configuration attributes
  195. GATTServApp_InitCharCfg( INVALID_CONNHANDLE, battLevelClientCharCfg );
  196. // Register GATT attribute list and CBs with GATT Server App
  197. status = GATTServApp_RegisterService( battAttrTbl,
  198. GATT_NUM_ATTRS( battAttrTbl ),
  199. &battCBs );
  200. return ( status );
  201. }
  202. /*********************************************************************
  203. * @fn Batt_Register
  204. *
  205. * @brief Register a callback function with the Battery Service.
  206. *
  207. * @param pfnServiceCB - Callback function.
  208. *
  209. * @return None.
  210. */
  211. extern void Batt_Register( battServiceCB_t pfnServiceCB )
  212. {
  213. battServiceCB = pfnServiceCB;
  214. }
  215. /*********************************************************************
  216. * @fn Batt_SetParameter
  217. *
  218. * @brief Set a Battery Service parameter.
  219. *
  220. * @param param - Profile parameter ID
  221. * @param len - length of data to right
  222. * @param value - pointer to data to write. This is dependent on
  223. * the parameter ID and WILL be cast to the appropriate
  224. * data type (example: data type of uint16 will be cast to
  225. * uint16 pointer).
  226. *
  227. * @return bStatus_t
  228. */
  229. bStatus_t Batt_SetParameter( uint8 param, uint8 len, void *value )
  230. {
  231. bStatus_t ret = SUCCESS;
  232. switch ( param )
  233. {
  234. case BATT_PARAM_CRITICAL_LEVEL:
  235. battCriticalLevel = *((uint8*)value);
  236. // If below the critical level and critical state not set, notify it
  237. if ( battLevel < battCriticalLevel )
  238. {
  239. battNotifyLevel();
  240. }
  241. break;
  242. default:
  243. ret = INVALIDPARAMETER;
  244. break;
  245. }
  246. return ( ret );
  247. }
  248. /*********************************************************************
  249. * @fn Batt_GetParameter
  250. *
  251. * @brief Get a Battery Service parameter.
  252. *
  253. * @param param - Profile parameter ID
  254. * @param value - pointer to data to get. This is dependent on
  255. * the parameter ID and WILL be cast to the appropriate
  256. * data type (example: data type of uint16 will be cast to
  257. * uint16 pointer).
  258. *
  259. * @return bStatus_t
  260. */
  261. bStatus_t Batt_GetParameter( uint8 param, void *value )
  262. {
  263. bStatus_t ret = SUCCESS;
  264. switch ( param )
  265. {
  266. case BATT_PARAM_LEVEL:
  267. *((uint8*)value) = battLevel;
  268. break;
  269. case BATT_PARAM_CRITICAL_LEVEL:
  270. *((uint8*)value) = battCriticalLevel;
  271. break;
  272. case BATT_PARAM_SERVICE_HANDLE:
  273. *((uint16*)value) = GATT_SERVICE_HANDLE( battAttrTbl );
  274. break;
  275. case BATT_PARAM_BATT_LEVEL_IN_REPORT:
  276. {
  277. hidRptMap_t *pRpt = (hidRptMap_t *)value;
  278. pRpt->id = hidReportRefBattLevel[0];
  279. pRpt->type = hidReportRefBattLevel[1];
  280. pRpt->handle = battAttrTbl[BATT_LEVEL_VALUE_IDX].handle;
  281. pRpt->cccdHandle = battAttrTbl[BATT_LEVEL_VALUE_CCCD_IDX].handle;
  282. pRpt->mode = HID_PROTOCOL_MODE_REPORT;
  283. }
  284. break;
  285. default:
  286. ret = INVALIDPARAMETER;
  287. break;
  288. }
  289. return ( ret );
  290. }
  291. /*********************************************************************
  292. * @fn Batt_MeasLevel
  293. *
  294. * @brief Measure the battery level and update the battery
  295. * level value in the service characteristics. If
  296. * the battery level-state characteristic is configured
  297. * for notification and the battery level has changed
  298. * since the last measurement, then a notification
  299. * will be sent.
  300. *
  301. * @return Success
  302. */
  303. bStatus_t Batt_MeasLevel( void )
  304. {
  305. uint8 level;
  306. level = battMeasure();
  307. // If level has gone down
  308. if (level < battLevel)
  309. {
  310. // Update level
  311. battLevel = level;
  312. // Send a notification
  313. battNotifyLevel();
  314. }
  315. return SUCCESS;
  316. }
  317. /*********************************************************************
  318. * @fn battReadAttrCB
  319. *
  320. * @brief Read an attribute.
  321. *
  322. * @param connHandle - connection message was received on
  323. * @param pAttr - pointer to attribute
  324. * @param pValue - pointer to data to be read
  325. * @param pLen - length of data to be read
  326. * @param offset - offset of the first octet to be read
  327. * @param maxLen - maximum length of data to be read
  328. *
  329. * @return Success or Failure
  330. */
  331. static uint8 battReadAttrCB( uint16 connHandle, gattAttribute_t *pAttr,
  332. uint8 *pValue, uint8 *pLen, uint16 offset, uint8 maxLen )
  333. {
  334. bStatus_t status = SUCCESS;
  335. // Make sure it's not a blob operation (no attributes in the profile are long)
  336. if ( offset > 0 )
  337. {
  338. return ( ATT_ERR_ATTR_NOT_LONG );
  339. }
  340. uint16 uuid = BUILD_UINT16( pAttr->type.uuid[0], pAttr->type.uuid[1] );
  341. // Measure battery level if reading level
  342. if ( uuid == BATT_LEVEL_UUID )
  343. {
  344. uint8 level;
  345. level = battMeasure();
  346. // If level has gone down
  347. if (level < battLevel)
  348. {
  349. // Update level
  350. battLevel = level;
  351. }
  352. *pLen = 1;
  353. pValue[0] = battLevel;
  354. }
  355. else if ( uuid == GATT_REPORT_REF_UUID )
  356. {
  357. *pLen = HID_REPORT_REF_LEN;
  358. osal_memcpy( pValue, pAttr->pValue, HID_REPORT_REF_LEN );
  359. }
  360. else
  361. {
  362. status = ATT_ERR_ATTR_NOT_FOUND;
  363. }
  364. return ( status );
  365. }
  366. /*********************************************************************
  367. * @fn battWriteAttrCB
  368. *
  369. * @brief Validate attribute data prior to a write operation
  370. *
  371. * @param connHandle - connection message was received on
  372. * @param pAttr - pointer to attribute
  373. * @param pValue - pointer to data to be written
  374. * @param len - length of data
  375. * @param offset - offset of the first octet to be written
  376. *
  377. * @return Success or Failure
  378. */
  379. static bStatus_t battWriteAttrCB( uint16 connHandle, gattAttribute_t *pAttr,
  380. uint8 *pValue, uint8 len, uint16 offset )
  381. {
  382. bStatus_t status = SUCCESS;
  383. uint16 uuid = BUILD_UINT16( pAttr->type.uuid[0], pAttr->type.uuid[1]);
  384. switch ( uuid )
  385. {
  386. case GATT_CLIENT_CHAR_CFG_UUID:
  387. status = GATTServApp_ProcessCCCWriteReq( connHandle, pAttr, pValue, len,
  388. offset, GATT_CLIENT_CFG_NOTIFY );
  389. if ( status == SUCCESS )
  390. {
  391. uint16 charCfg = BUILD_UINT16( pValue[0], pValue[1] );
  392. if ( battServiceCB )
  393. {
  394. (*battServiceCB)( (charCfg == GATT_CFG_NO_OPERATION) ?
  395. BATT_LEVEL_NOTI_DISABLED :
  396. BATT_LEVEL_NOTI_ENABLED);
  397. }
  398. }
  399. break;
  400. default:
  401. status = ATT_ERR_ATTR_NOT_FOUND;
  402. break;
  403. }
  404. return ( status );
  405. }
  406. /*********************************************************************
  407. * @fn battNotifyCB
  408. *
  409. * @brief Send a notification of the level state characteristic.
  410. *
  411. * @param connHandle - linkDB item
  412. *
  413. * @return None.
  414. */
  415. static void battNotifyCB( linkDBItem_t *pLinkItem )
  416. {
  417. if ( pLinkItem->stateFlags & LINK_CONNECTED )
  418. {
  419. uint16 value = GATTServApp_ReadCharCfg( pLinkItem->connectionHandle,
  420. battLevelClientCharCfg );
  421. if ( value & GATT_CLIENT_CFG_NOTIFY )
  422. {
  423. attHandleValueNoti_t noti;
  424. noti.handle = battAttrTbl[BATT_LEVEL_VALUE_IDX].handle;
  425. noti.len = 1;
  426. noti.value[0] = battLevel;
  427. GATT_Notification( pLinkItem->connectionHandle, &noti, FALSE );
  428. }
  429. }
  430. }
  431. /*********************************************************************
  432. * @fn battMeasure
  433. *
  434. * @brief Measure the battery level with the ADC and return
  435. * it as a percentage 0-100%.
  436. *
  437. * @return Battery level.
  438. */
  439. static uint8 battMeasure( void )
  440. {
  441. uint8 percent;
  442. percent = 95;
  443. return percent;
  444. }
  445. /*********************************************************************
  446. * @fn battNotifyLevelState
  447. *
  448. * @brief Send a notification of the battery level state
  449. * characteristic if a connection is established.
  450. *
  451. * @return None.
  452. */
  453. static void battNotifyLevel( void )
  454. {
  455. // Execute linkDB callback to send notification
  456. linkDB_PerformFunc( battNotifyCB );
  457. }
  458. /*********************************************************************
  459. * @fn Batt_HandleConnStatusCB
  460. *
  461. * @brief Battery Service link status change handler function.
  462. *
  463. * @param connHandle - connection handle
  464. * @param changeType - type of change
  465. *
  466. * @return none
  467. */
  468. void Batt_HandleConnStatusCB( uint16 connHandle, uint8 changeType )
  469. {
  470. // Make sure this is not loopback connection
  471. if ( connHandle != LOOPBACK_CONNHANDLE )
  472. {
  473. // Reset Client Char Config if connection has dropped
  474. if ( ( changeType == LINKDB_STATUS_UPDATE_REMOVED ) ||
  475. ( ( changeType == LINKDB_STATUS_UPDATE_STATEFLAGS ) &&
  476. ( !linkDB_Up( connHandle ) ) ) )
  477. {
  478. GATTServApp_InitCharCfg( connHandle, battLevelClientCharCfg );
  479. }
  480. }
  481. }
  482. /*********************************************************************
  483. *********************************************************************/