123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507 |
- #include "bcomdef.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 "heartrateservice.h"
- #include "log.h"
- #define HEARTRATE_MEAS_VALUE_POS 2
- CONST uint8 heartRateServUUID[ATT_BT_UUID_SIZE] =
- {
- LO_UINT16(HEARTRATE_SERV_UUID), HI_UINT16(HEARTRATE_SERV_UUID)
- };
- CONST uint8 heartRateMeasUUID[ATT_BT_UUID_SIZE] =
- {
- LO_UINT16(HEARTRATE_MEAS_UUID), HI_UINT16(HEARTRATE_MEAS_UUID)
- };
- CONST uint8 heartRateSensLocUUID[ATT_BT_UUID_SIZE] =
- {
- LO_UINT16(BODY_SENSOR_LOC_UUID), HI_UINT16(BODY_SENSOR_LOC_UUID)
- };
- CONST uint8 heartRateCommandUUID[ATT_BT_UUID_SIZE] =
- {
- LO_UINT16(HEARTRATE_CTRL_PT_UUID), HI_UINT16(HEARTRATE_CTRL_PT_UUID)
- };
- static heartRateServiceCB_t heartRateServiceCB;
- static CONST gattAttrType_t heartRateService = { ATT_BT_UUID_SIZE, heartRateServUUID };
- static uint8 heartRateMeasProps = GATT_PROP_NOTIFY;
- static uint8 heartRateMeas = 0;
- static gattCharCfg_t heartRateMeasClientCharCfg[GATT_MAX_NUM_CONN];
- static uint8 heartRateSensLocProps = GATT_PROP_READ;
- static uint8 heartRateSensLoc = 0;
- static uint8 heartRateCommandProps = GATT_PROP_WRITE;
- static uint8 heartRateCommand = 0;
- static gattAttribute_t heartRateAttrTbl[] =
- {
-
- {
- { ATT_BT_UUID_SIZE, primaryServiceUUID },
- GATT_PERMIT_READ,
- 0,
- (uint8 *)&heartRateService
- },
-
- {
- { ATT_BT_UUID_SIZE, characterUUID },
- GATT_PERMIT_READ,
- 0,
- &heartRateMeasProps
- },
-
- {
- { ATT_BT_UUID_SIZE, heartRateMeasUUID },
- 0,
- 0,
- &heartRateMeas
- },
-
- {
- { ATT_BT_UUID_SIZE, clientCharCfgUUID },
- GATT_PERMIT_READ | GATT_PERMIT_WRITE,
- 0,
- (uint8 *) &heartRateMeasClientCharCfg
- },
-
- {
- { ATT_BT_UUID_SIZE, characterUUID },
- GATT_PERMIT_READ,
- 0,
- &heartRateSensLocProps
- },
-
- {
- { ATT_BT_UUID_SIZE, heartRateSensLocUUID },
- GATT_PERMIT_READ,
- 0,
- &heartRateSensLoc
- },
-
- {
- { ATT_BT_UUID_SIZE, characterUUID },
- GATT_PERMIT_READ,
- 0,
- &heartRateCommandProps
- },
-
- {
- { ATT_BT_UUID_SIZE, heartRateCommandUUID },
- GATT_PERMIT_WRITE,
- 0,
- &heartRateCommand
- }
- };
- static uint8 heartRate_ReadAttrCB( uint16 connHandle, gattAttribute_t *pAttr,
- uint8 *pValue, uint8 *pLen, uint16 offset, uint8 maxLen );
- static bStatus_t heartRate_WriteAttrCB( uint16 connHandle, gattAttribute_t *pAttr,
- uint8 *pValue, uint8 len, uint16 offset );
- CONST gattServiceCBs_t heartRateCBs =
- {
- heartRate_ReadAttrCB,
- heartRate_WriteAttrCB,
- NULL
- };
- bStatus_t HeartRate_AddService( uint32 services )
- {
- uint8 status = SUCCESS;
-
- GATTServApp_InitCharCfg( INVALID_CONNHANDLE, heartRateMeasClientCharCfg );
- if ( services & HEARTRATE_SERVICE )
- {
-
- status = GATTServApp_RegisterService( heartRateAttrTbl,
- GATT_NUM_ATTRS( heartRateAttrTbl ),
- &heartRateCBs );
- }
- return ( status );
- }
- extern void HeartRate_Register( heartRateServiceCB_t pfnServiceCB )
- {
- heartRateServiceCB = pfnServiceCB;
- }
- bStatus_t HeartRate_SetParameter( uint8 param, uint8 len, void *value )
- {
- bStatus_t ret = SUCCESS;
- switch ( param )
- {
- case HEARTRATE_MEAS_CHAR_CFG:
-
-
- break;
- case HEARTRATE_SENS_LOC:
- heartRateSensLoc = *((uint8*)value);
- break;
- default:
- ret = INVALIDPARAMETER;
- break;
- }
-
- return ( ret );
- }
- bStatus_t HeartRate_GetParameter( uint8 param, void *value )
- {
- bStatus_t ret = SUCCESS;
- switch ( param )
- {
- case HEARTRATE_MEAS_CHAR_CFG:
-
-
- break;
- case HEARTRATE_SENS_LOC:
- *((uint8*)value) = heartRateSensLoc;
- break;
-
- case HEARTRATE_COMMAND:
- *((uint8*)value) = heartRateCommand;
- break;
- default:
- ret = INVALIDPARAMETER;
- break;
- }
-
- return ( ret );
- }
- bStatus_t HeartRate_MeasNotify( uint16 connHandle, attHandleValueNoti_t *pNoti )
- {
- uint16 value = GATTServApp_ReadCharCfg( connHandle, heartRateMeasClientCharCfg );
-
- if ( value & GATT_CLIENT_CFG_NOTIFY )
- {
-
- pNoti->handle = heartRateAttrTbl[HEARTRATE_MEAS_VALUE_POS].handle;
-
-
- return GATT_Notification( connHandle, pNoti, FALSE );
- }
- return bleIncorrectMode;
- }
-
- static uint8 heartRate_ReadAttrCB( 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 == BODY_SENSOR_LOC_UUID)
- {
- *pLen = 1;
- pValue[0] = *pAttr->pValue;
- }
- else
- {
- status = ATT_ERR_ATTR_NOT_FOUND;
- }
- return ( status );
- }
- static bStatus_t heartRate_WriteAttrCB( 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 HEARTRATE_CTRL_PT_UUID:
- if ( offset > 0 )
- {
- status = ATT_ERR_ATTR_NOT_LONG;
- }
- else if (len != 1)
- {
- status = ATT_ERR_INVALID_VALUE_SIZE;
- }
- else if (*pValue != HEARTRATE_COMMAND_ENERGY_EXP)
- {
- status = HEARTRATE_ERR_NOT_SUP;
- }
- else
- {
- *(pAttr->pValue) = pValue[0];
-
- (*heartRateServiceCB)(HEARTRATE_COMMAND_SET);
-
- }
- break;
- 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] );
- (*heartRateServiceCB)( (charCfg == GATT_CFG_NO_OPERATION) ?
- HEARTRATE_MEAS_NOTI_DISABLED :
- HEARTRATE_MEAS_NOTI_ENABLED );
- }
- break;
-
- default:
- status = ATT_ERR_ATTR_NOT_FOUND;
- break;
- }
- return ( status );
- }
- void HeartRate_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, heartRateMeasClientCharCfg );
- }
- }
- }
|