123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686 |
- #include "bcomdef.h"
- #include "OSAL.h"
- #include "osal_cbTimer.h"
- #include "osal_snv.h"
- #include "hci_tl.h"
- #include "l2cap.h"
- #include "linkdb.h"
- #include "gap.h"
- #include "gapbondmgr.h"
- #include "central.h"
- #define START_ADVERTISING_EVT 0x0001
- #define RSSI_READ_EVT 0x0002
- #define UPDATE_PARAMS_TIMEOUT_EVT 0x0004
- #define GAPCENTRALROLE_RSSI_MSG_EVT 0xE0
- typedef struct
- {
- uint16 period;
- uint16 connHandle;
- uint8 timerId;
- } gapCentralRoleRssi_t;
- typedef struct
- {
- osal_event_hdr_t hdr;
- gapCentralRoleRssi_t *pRssi;
- } gapCentralRoleRssiEvent_t;
- static uint8 gapCentralRoleTaskId;
- static gapCentralRoleCB_t *pGapCentralRoleCB;
- static gapCentralRoleRssi_t gapCentralRoleRssi[GAPCENTRALROLE_NUM_RSSI_LINKS];
- static uint8 gapCentralRoleIRK[KEYLEN];
- static uint8 gapCentralRoleSRK[KEYLEN];
- static uint32 gapCentralRoleSignCounter;
- static uint8 gapCentralRoleBdAddr[B_ADDR_LEN];
- static uint8 gapCentralRoleMaxScanRes = 0;
- static void gapCentralRole_ProcessOSALMsg( osal_event_hdr_t *pMsg );
- static void gapCentralRole_ProcessGAPMsg( gapEventHdr_t *pMsg );
- static gapCentralRoleRssi_t *gapCentralRole_RssiAlloc( uint16 connHandle );
- static gapCentralRoleRssi_t *gapCentralRole_RssiFind( uint16 connHandle );
- static void gapCentralRole_RssiFree( uint16 connHandle );
- static void gapCentralRole_timerCB( uint8 *pData );
- bStatus_t GAPCentralRole_StartDevice( gapCentralRoleCB_t *pAppCallbacks )
- {
- if ( pAppCallbacks )
- {
- pGapCentralRoleCB = pAppCallbacks;
- }
- return GAP_DeviceInit( gapCentralRoleTaskId, GAP_PROFILE_CENTRAL,
- gapCentralRoleMaxScanRes, gapCentralRoleIRK,
- gapCentralRoleSRK, &gapCentralRoleSignCounter );
- }
- bStatus_t GAPCentralRole_SetParameter( uint16 param, uint8 len, void *pValue )
- {
- bStatus_t ret = SUCCESS;
- switch ( param )
- {
- case GAPCENTRALROLE_IRK:
- if ( len == KEYLEN )
- {
- VOID osal_memcpy( gapCentralRoleIRK, pValue, KEYLEN ) ;
- }
- else
- {
- ret = bleInvalidRange;
- }
- break;
- case GAPCENTRALROLE_SRK:
- if ( len == KEYLEN )
- {
- VOID osal_memcpy( gapCentralRoleSRK, pValue, KEYLEN ) ;
- }
- else
- {
- ret = bleInvalidRange;
- }
- break;
- case GAPCENTRALROLE_SIGNCOUNTER:
- if ( len == sizeof ( uint32 ) )
- {
- gapCentralRoleSignCounter = *((uint32*)pValue);
- }
- else
- {
- ret = bleInvalidRange;
- }
- break;
- case GAPCENTRALROLE_MAX_SCAN_RES:
- if ( len == sizeof ( uint8 ) )
- {
- gapCentralRoleMaxScanRes = *((uint8*)pValue);
- }
- else
- {
- ret = bleInvalidRange;
- }
- break;
- default:
- ret = INVALIDPARAMETER;
- break;
- }
- return ret;
- }
- bStatus_t GAPCentralRole_GetParameter( uint16 param, void *pValue )
- {
- bStatus_t ret = SUCCESS;
- switch ( param )
- {
- case GAPCENTRALROLE_IRK:
- VOID osal_memcpy( pValue, gapCentralRoleIRK, KEYLEN ) ;
- break;
- case GAPCENTRALROLE_SRK:
- VOID osal_memcpy( pValue, gapCentralRoleSRK, KEYLEN ) ;
- break;
- case GAPCENTRALROLE_SIGNCOUNTER:
- *((uint32*)pValue) = gapCentralRoleSignCounter;
- break;
- case GAPCENTRALROLE_BD_ADDR:
- VOID osal_memcpy( pValue, gapCentralRoleBdAddr, B_ADDR_LEN ) ;
- break;
- case GAPCENTRALROLE_MAX_SCAN_RES:
- *((uint8*)pValue) = gapCentralRoleMaxScanRes;
- break;
- default:
- ret = INVALIDPARAMETER;
- break;
- }
- return ret;
- }
- bStatus_t GAPCentralRole_TerminateLink( uint16 connHandle )
- {
- return GAP_TerminateLinkReq( gapCentralRoleTaskId, connHandle, HCI_DISCONNECT_REMOTE_USER_TERM ) ;
- }
- bStatus_t GAPCentralRole_EstablishLink( uint8 highDutyCycle, uint8 whiteList,
- uint8 addrTypePeer, uint8 *peerAddr )
- {
- gapEstLinkReq_t params;
- params.taskID = gapCentralRoleTaskId;
- params.highDutyCycle = highDutyCycle;
- params.whiteList = whiteList;
- params.addrTypePeer = addrTypePeer;
- VOID osal_memcpy( params.peerAddr, peerAddr, B_ADDR_LEN );
- return GAP_EstablishLinkReq( ¶ms );
- }
- bStatus_t GAPCentralRole_UpdateLink( uint16 connHandle, uint16 connIntervalMin,
- uint16 connIntervalMax, uint16 connLatency,
- uint16 connTimeout )
- {
- return (bStatus_t) HCI_LE_ConnUpdateCmd( connHandle, connIntervalMin,
- connIntervalMax, connLatency,
- connTimeout, 0, 0 );
- }
- bStatus_t GAPCentralRole_StartDiscovery( uint8 mode, uint8 activeScan, uint8 whiteList )
- {
- gapDevDiscReq_t params;
- params.taskID = gapCentralRoleTaskId;
- params.mode = mode;
- params.activeScan = activeScan;
- params.whiteList = whiteList;
- return GAP_DeviceDiscoveryRequest( ¶ms );
- }
- bStatus_t GAPCentralRole_CancelDiscovery( void )
- {
- return GAP_DeviceDiscoveryCancel( gapCentralRoleTaskId );
- }
- bStatus_t GAPCentralRole_StartRssi( uint16 connHandle, uint16 period )
- {
- gapCentralRoleRssi_t *pRssi;
-
- if (!linkDB_Up(connHandle))
- {
- return bleIncorrectMode;
- }
-
- if ((pRssi = gapCentralRole_RssiFind( connHandle )) != NULL)
- {
-
- osal_CbTimerStop( pRssi->timerId );
- }
-
- else if ((pRssi = gapCentralRole_RssiAlloc( connHandle )) != NULL)
- {
- pRssi->period = period;
- }
-
- else
- {
- return bleNoResources;
- }
-
- osal_CbTimerStart( gapCentralRole_timerCB, (uint8 *) pRssi,
- period, &pRssi->timerId );
- return SUCCESS;
- }
- bStatus_t GAPCentralRole_CancelRssi(uint16 connHandle )
- {
- gapCentralRoleRssi_t *pRssi;
- if ((pRssi = gapCentralRole_RssiFind( connHandle )) != NULL)
- {
-
- osal_CbTimerStop( pRssi->timerId );
-
- gapCentralRole_RssiFree( connHandle );
- return SUCCESS;
- }
-
- return bleIncorrectMode;
- }
- void GAPCentralRole_Init( uint8 taskId )
- {
- uint8 i;
- gapCentralRoleTaskId = taskId;
-
- for ( i = 0; i < GAPCENTRALROLE_NUM_RSSI_LINKS; i++ )
- {
- gapCentralRoleRssi[i].connHandle = GAP_CONNHANDLE_ALL;
- gapCentralRoleRssi[i].timerId = INVALID_TIMER_ID;
- }
-
-
-
- GAP_RegisterForHCIMsgs( taskId );
- }
- uint16 GAPCentralRole_ProcessEvent( uint8 taskId, uint16 events )
- {
- if ( events & SYS_EVENT_MSG )
- {
- uint8 *pMsg;
- if ( (pMsg = osal_msg_receive( gapCentralRoleTaskId )) != NULL )
- {
- gapCentralRole_ProcessOSALMsg( (osal_event_hdr_t *) pMsg );
-
- VOID osal_msg_deallocate( pMsg );
- }
-
- return (events ^ SYS_EVENT_MSG);
- }
- if ( events & GAP_EVENT_SIGN_COUNTER_CHANGED )
- {
-
-
- return ( events ^ GAP_EVENT_SIGN_COUNTER_CHANGED );
- }
-
- return 0;
- }
- static void gapCentralRole_ProcessOSALMsg( osal_event_hdr_t *pMsg )
- {
- switch ( pMsg->event )
- {
- case HCI_GAP_EVENT_EVENT:
- if ( pMsg->status == HCI_COMMAND_COMPLETE_EVENT_CODE )
- {
- hciEvt_CmdComplete_t *pPkt = (hciEvt_CmdComplete_t *) pMsg;
- if ( pPkt->cmdOpcode == HCI_READ_RSSI )
- {
- uint16 connHandle = BUILD_UINT16( pPkt->pReturnParam[1], pPkt->pReturnParam[2] );
- int8 rssi = (int8) pPkt->pReturnParam[3];
-
- if ( pGapCentralRoleCB && pGapCentralRoleCB->rssiCB )
- {
- pGapCentralRoleCB->rssiCB( connHandle, rssi );
- }
- }
- }
- break;
- case GAP_MSG_EVENT:
- gapCentralRole_ProcessGAPMsg( (gapEventHdr_t *) pMsg );
- break;
- case GAPCENTRALROLE_RSSI_MSG_EVT:
- {
- gapCentralRoleRssi_t *pRssi = ((gapCentralRoleRssiEvent_t *) pMsg)->pRssi;
-
- if (pRssi->connHandle != GAP_CONNHANDLE_ALL &&
- linkDB_Up(pRssi->connHandle))
- {
-
- osal_CbTimerStart( gapCentralRole_timerCB, (uint8 *) pRssi,
- pRssi->period, &pRssi->timerId );
-
- VOID HCI_ReadRssiCmd( pRssi->connHandle );
- }
- }
- break;
- default:
- break;
- }
- }
- static void gapCentralRole_ProcessGAPMsg( gapEventHdr_t *pMsg )
- {
- switch ( pMsg->opcode )
- {
- case GAP_DEVICE_INIT_DONE_EVENT:
- {
- gapDeviceInitDoneEvent_t *pPkt = (gapDeviceInitDoneEvent_t *) pMsg;
- if ( pPkt->hdr.status == SUCCESS )
- {
-
-
-
-
- VOID osal_memcpy( gapCentralRoleBdAddr, pPkt->devAddr, B_ADDR_LEN );
- }
- }
- break;
- case GAP_LINK_ESTABLISHED_EVENT:
- {
- gapEstLinkReqEvent_t *pPkt = (gapEstLinkReqEvent_t *) pMsg;
- if (pPkt->hdr.status == SUCCESS)
- {
-
- VOID GAPBondMgr_LinkEst( pPkt->devAddrType, pPkt->devAddr,
- pPkt->connectionHandle, GAP_PROFILE_CENTRAL );
- }
- }
- break;
- case GAP_LINK_TERMINATED_EVENT:
- {
- uint16 connHandle = ((gapTerminateLinkEvent_t *) pMsg)->connectionHandle;
- VOID GAPBondMgr_ProcessGAPMsg( (gapEventHdr_t *)pMsg );
-
- GAPCentralRole_CancelRssi( connHandle );
- }
- break;
-
- case GAP_SLAVE_REQUESTED_SECURITY_EVENT:
- VOID GAPBondMgr_ProcessGAPMsg( pMsg );
- break;
- default:
- break;
- }
-
- if ( pGapCentralRoleCB && pGapCentralRoleCB->eventCB )
- {
- pGapCentralRoleCB->eventCB( (gapCentralRoleEvent_t *) pMsg );
- }
- }
- static gapCentralRoleRssi_t *gapCentralRole_RssiAlloc( uint16 connHandle )
- {
- uint8 i;
-
- for ( i = 0; i < GAPCENTRALROLE_NUM_RSSI_LINKS; i++ )
- {
- if ( gapCentralRoleRssi[i].connHandle == GAP_CONNHANDLE_ALL )
- {
- gapCentralRoleRssi[i].connHandle = connHandle;
- return &gapCentralRoleRssi[i];
- }
- }
-
- return NULL;
- }
- static gapCentralRoleRssi_t *gapCentralRole_RssiFind( uint16 connHandle )
- {
- uint8 i;
-
- for ( i = 0; i < GAPCENTRALROLE_NUM_RSSI_LINKS; i++ )
- {
- if ( gapCentralRoleRssi[i].connHandle == connHandle )
- {
- return &gapCentralRoleRssi[i];
- }
- }
-
- return NULL;
- }
- static void gapCentralRole_RssiFree( uint16 connHandle )
- {
- uint8 i;
-
- for ( i = 0; i < GAPCENTRALROLE_NUM_RSSI_LINKS; i++ )
- {
- if ( gapCentralRoleRssi[i].connHandle == connHandle )
- {
- gapCentralRoleRssi[i].connHandle = GAP_CONNHANDLE_ALL;
- break;
- }
- }
- }
- static void gapCentralRole_timerCB( uint8 *pData )
- {
- gapCentralRoleRssiEvent_t *pMsg;
-
- ((gapCentralRoleRssi_t *) pData)->timerId = INVALID_TIMER_ID;
-
- pMsg = (gapCentralRoleRssiEvent_t *) osal_msg_allocate( sizeof(gapCentralRoleRssiEvent_t) );
- if ( pMsg )
- {
- pMsg->hdr.event = GAPCENTRALROLE_RSSI_MSG_EVT;
- pMsg->pRssi = (gapCentralRoleRssi_t *) pData;
- osal_msg_send ( gapCentralRoleTaskId, (uint8 *) pMsg );
- }
- }
|