123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572 |
- #include "bcomdef.h"
- #include "types.h"
- #include "OSAL.h"
- #include "linkdb.h"
- #include "att.h"
- #include "gatt.h"
- #include "gatt_uuid.h"
- #include "gatt_profile_uuid.h"
- #include "gattservapp.h"
- #include "peripheral.h"
- #include "hiddev.h"
- #include "battservice.h"
- #define BATT_ADC_LEVEL_3V 409
- #define BATT_ADC_LEVEL_2V 273
- #define BATT_LEVEL_VALUE_IDX 2
- #define BATT_LEVEL_VALUE_CCCD_IDX 3
- CONST uint8 battServUUID[ATT_BT_UUID_SIZE] =
- {
- LO_UINT16(BATT_SERV_UUID), HI_UINT16(BATT_SERV_UUID)
- };
- CONST uint8 battLevelUUID[ATT_BT_UUID_SIZE] =
- {
- LO_UINT16(BATT_LEVEL_UUID), HI_UINT16(BATT_LEVEL_UUID)
- };
- static battServiceCB_t battServiceCB;
- #ifndef HID_VOICE_SPEC
- #endif
- static uint8 battCriticalLevel;
- static CONST gattAttrType_t battService = { ATT_BT_UUID_SIZE, battServUUID };
- static uint8 battLevelProps = GATT_PROP_READ | GATT_PROP_NOTIFY;
- static uint8 battLevel = 100;
- #ifdef HID_VOICE_SPEC
- static gattCharFormat_t battLevelPresentation = {
- GATT_FORMAT_UINT8,
- 0,
- GATT_UNIT_PERCENTAGE_UUID,
- GATT_NS_BT_SIG,
- GATT_DESC_LENGTH_UUID
- };
- #endif
- static gattCharCfg_t battLevelClientCharCfg[GATT_MAX_NUM_CONN];
- static uint8 hidReportRefBattLevel[HID_REPORT_REF_LEN] =
- { HID_RPT_ID_BATT_LEVEL_IN, HID_REPORT_TYPE_INPUT };
- static gattAttribute_t battAttrTbl[] =
- {
-
- {
- { ATT_BT_UUID_SIZE, primaryServiceUUID },
- GATT_PERMIT_READ,
- 0,
- (uint8 *)&battService
- },
-
- {
- { ATT_BT_UUID_SIZE, characterUUID },
- GATT_PERMIT_READ,
- 0,
- &battLevelProps
- },
-
- {
- { ATT_BT_UUID_SIZE, battLevelUUID },
- GATT_PERMIT_READ,
- 0,
- &battLevel
- },
-
- {
- { ATT_BT_UUID_SIZE, clientCharCfgUUID },
- GATT_PERMIT_READ | GATT_PERMIT_WRITE,
- 0,
- (uint8 *) &battLevelClientCharCfg
- },
-
- {
- { ATT_BT_UUID_SIZE, reportRefUUID },
- GATT_PERMIT_READ,
- 0,
- hidReportRefBattLevel
- },
- #ifdef HID_VOICE_SPEC
-
-
- {
- { ATT_BT_UUID_SIZE, charFormatUUID },
- GATT_PERMIT_READ,
- 0,
- (uint8_t *)&battLevelPresentation
- },
- #endif
- };
- static uint8 battReadAttrCB( uint16 connHandle, gattAttribute_t *pAttr,
- uint8 *pValue, uint8 *pLen, uint16 offset, uint8 maxLen );
- static bStatus_t battWriteAttrCB( uint16 connHandle, gattAttribute_t *pAttr,
- uint8 *pValue, uint8 len, uint16 offset );
- static void battNotifyCB( linkDBItem_t *pLinkItem );
- static uint8 battMeasure( void );
- static void battNotifyLevel( void );
- CONST gattServiceCBs_t battCBs =
- {
- battReadAttrCB,
- battWriteAttrCB,
- NULL
- };
- bStatus_t Batt_AddService( void )
- {
- uint8 status = SUCCESS;
-
- GATTServApp_InitCharCfg( INVALID_CONNHANDLE, battLevelClientCharCfg );
-
- status = GATTServApp_RegisterService( battAttrTbl,
- GATT_NUM_ATTRS( battAttrTbl ),
- &battCBs );
- return ( status );
- }
- extern void Batt_Register( battServiceCB_t pfnServiceCB )
- {
- battServiceCB = pfnServiceCB;
- }
- bStatus_t Batt_SetParameter( uint8 param, uint8 len, void *value )
- {
- bStatus_t ret = SUCCESS;
- switch ( param )
- {
- case BATT_PARAM_CRITICAL_LEVEL:
- battCriticalLevel = *((uint8*)value);
-
- if ( battLevel < battCriticalLevel )
- {
- battNotifyLevel();
- }
- break;
- default:
- ret = INVALIDPARAMETER;
- break;
- }
- return ( ret );
- }
- bStatus_t Batt_GetParameter( uint8 param, void *value )
- {
- bStatus_t ret = SUCCESS;
- switch ( param )
- {
- case BATT_PARAM_LEVEL:
- *((uint8*)value) = battLevel;
- break;
- case BATT_PARAM_CRITICAL_LEVEL:
- *((uint8*)value) = battCriticalLevel;
- break;
- case BATT_PARAM_SERVICE_HANDLE:
- *((uint16*)value) = GATT_SERVICE_HANDLE( battAttrTbl );
- break;
- case BATT_PARAM_BATT_LEVEL_IN_REPORT:
- {
- hidRptMap_t *pRpt = (hidRptMap_t *)value;
- pRpt->id = hidReportRefBattLevel[0];
- pRpt->type = hidReportRefBattLevel[1];
- pRpt->handle = battAttrTbl[BATT_LEVEL_VALUE_IDX].handle;
- pRpt->cccdHandle = battAttrTbl[BATT_LEVEL_VALUE_CCCD_IDX].handle;
- pRpt->mode = HID_PROTOCOL_MODE_REPORT;
- }
- break;
- default:
- ret = INVALIDPARAMETER;
- break;
- }
- return ( ret );
- }
- bStatus_t Batt_MeasLevel( void )
- {
- uint8 level;
- level = battMeasure();
-
- if (level < battLevel)
- {
-
- battLevel = level;
-
- battNotifyLevel();
- }
- return SUCCESS;
- }
- static uint8 battReadAttrCB( uint16 connHandle, gattAttribute_t *pAttr,
- uint8 *pValue, uint8 *pLen, uint16 offset, uint8 maxLen )
- {
- bStatus_t status = SUCCESS;
-
- if ( offset > 0 )
- {
- return ( ATT_ERR_ATTR_NOT_LONG );
- }
- uint16 uuid = BUILD_UINT16( pAttr->type.uuid[0], pAttr->type.uuid[1] );
-
- if ( uuid == BATT_LEVEL_UUID )
- {
- uint8 level;
- level = battMeasure();
-
- if (level < battLevel)
- {
-
- battLevel = level;
- }
- *pLen = 1;
- pValue[0] = battLevel;
- }
- else if ( uuid == GATT_REPORT_REF_UUID )
- {
- *pLen = HID_REPORT_REF_LEN;
- osal_memcpy( pValue, pAttr->pValue, HID_REPORT_REF_LEN );
- }
- else
- {
- status = ATT_ERR_ATTR_NOT_FOUND;
- }
- return ( status );
- }
- static bStatus_t battWriteAttrCB( uint16 connHandle, gattAttribute_t *pAttr,
- uint8 *pValue, uint8 len, uint16 offset )
- {
- bStatus_t status = SUCCESS;
- uint16 uuid = BUILD_UINT16( pAttr->type.uuid[0], pAttr->type.uuid[1]);
- switch ( uuid )
- {
- case GATT_CLIENT_CHAR_CFG_UUID:
- status = GATTServApp_ProcessCCCWriteReq( connHandle, pAttr, pValue, len,
- offset, GATT_CLIENT_CFG_NOTIFY );
- if ( status == SUCCESS )
- {
- uint16 charCfg = BUILD_UINT16( pValue[0], pValue[1] );
- if ( battServiceCB )
- {
- (*battServiceCB)( (charCfg == GATT_CFG_NO_OPERATION) ?
- BATT_LEVEL_NOTI_DISABLED :
- BATT_LEVEL_NOTI_ENABLED);
- }
- }
- break;
- default:
- status = ATT_ERR_ATTR_NOT_FOUND;
- break;
- }
- return ( status );
- }
- static void battNotifyCB( linkDBItem_t *pLinkItem )
- {
- if ( pLinkItem->stateFlags & LINK_CONNECTED )
- {
- uint16 value = GATTServApp_ReadCharCfg( pLinkItem->connectionHandle,
- battLevelClientCharCfg );
- if ( value & GATT_CLIENT_CFG_NOTIFY )
- {
- attHandleValueNoti_t noti;
- noti.handle = battAttrTbl[BATT_LEVEL_VALUE_IDX].handle;
- noti.len = 1;
- noti.value[0] = battLevel;
- GATT_Notification( pLinkItem->connectionHandle, ¬i, FALSE );
- }
- }
- }
- static uint8 battMeasure( void )
- {
- uint8 percent;
- percent = 95;
- return percent;
- }
- static void battNotifyLevel( void )
- {
-
- linkDB_PerformFunc( battNotifyCB );
- }
- void Batt_HandleConnStatusCB( uint16 connHandle, uint8 changeType )
- {
-
- if ( connHandle != LOOPBACK_CONNHANDLE )
- {
-
- if ( ( changeType == LINKDB_STATUS_UPDATE_REMOVED ) ||
- ( ( changeType == LINKDB_STATUS_UPDATE_STATEFLAGS ) &&
- ( !linkDB_Up( connHandle ) ) ) )
- {
- GATTServApp_InitCharCfg( connHandle, battLevelClientCharCfg );
- }
- }
- }
|