12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601 |
- #include "bcomdef.h"
- #include "OSAL.h"
- #include "hci_tl.h"
- #include "l2cap.h"
- #include "gap.h"
- #include "linkdb.h"
- #include "att.h"
- #include "gatt.h"
- #include "osal_snv.h"
- #include "log.h"
- #include "peripheral.h"
- #include "gapbondmgr.h"
- #ifdef EXT_ADV_ENABLE
- #include "rf_phy_driver.h"
- #endif
- #define START_ADVERTISING_EVT 0x0001
- #define RSSI_READ_EVT 0x0002
- #define START_CONN_UPDATE_EVT 0x0004
- #define CONN_PARAM_TIMEOUT_EVT 0x0008
- #ifdef EXT_ADV_ENABLE
- #define START_SECOND_ADVERTISING_EVENT 0x0010
- #endif
- #define DEFAULT_ADVERT_OFF_TIME 30000
- #define RSSI_NOT_AVAILABLE 127
- #define DEFAULT_MIN_CONN_INTERVAL 0x0006
- #define DEFAULT_MAX_CONN_INTERVAL 0x0C80
- #define MIN_CONN_INTERVAL 0x0006
- #define MAX_CONN_INTERVAL 0x0C80
- #define DEFAULT_TIMEOUT_MULTIPLIER 1000
- #define CONN_INTERVAL_MULTIPLIER 6
- #define MIN_SLAVE_LATENCY 0
- #define MAX_SLAVE_LATENCY 500
- #define MIN_TIMEOUT_MULTIPLIER 0x000a
- #define MAX_TIMEOUT_MULTIPLIER 0x0c80
- #define MAX_TIMEOUT_VALUE 0xFFFF
- static uint8 gapRole_TaskID;
- static gaprole_States_t gapRole_state;
- static uint8 gapRole_profileRole;
- static uint8 gapRole_IRK[KEYLEN];
- static uint8 gapRole_SRK[KEYLEN];
- static uint32 gapRole_signCounter;
- static uint8 gapRole_bdAddr[B_ADDR_LEN];
- uint8 gapRole_AdvEnabled = FALSE;
- #ifdef EXT_ADV_ENABLE
- static uint8 gapRole_ExtAdvEnabled = FALSE;
- #endif
- static uint16 gapRole_AdvertOffTime = DEFAULT_ADVERT_OFF_TIME;
- static uint8 gapRole_AdvertDataLen = 3;
- static uint8 gapRole_AdvertData[B_MAX_ADV_LEN] =
- {
- 0x02,
- GAP_ADTYPE_FLAGS,
-
- (GAP_ADTYPE_FLAGS_GENERAL | GAP_ADTYPE_FLAGS_BREDR_NOT_SUPPORTED),
- 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
- };
- static uint8 gapRole_ScanRspDataLen = 0;
- static uint8 gapRole_ScanRspData[B_MAX_ADV_LEN] = {0};
- uint8 gapRole_AdvEventType;
- static uint8 gapRole_AdvDirectType;
- static uint8 gapRole_AdvDirectAddr[B_ADDR_LEN] = {0};
- static uint8 gapRole_AdvChanMap;
- static uint8 gapRole_AdvFilterPolicy;
- static uint16 gapRole_ConnectionHandle = INVALID_CONNHANDLE;
- static uint16 gapRole_ConnectionInterval = 0;
- static uint16 gapRole_ConnectionLatency = 0;
- static uint16 gapRole_RSSIReadRate = 0;
- static uint8 gapRole_ConnectedDevAddr[B_ADDR_LEN] = {0};
- static uint8 gapRole_ParamUpdateEnable = FALSE;
- static uint16 gapRole_MinConnInterval = DEFAULT_MIN_CONN_INTERVAL;
- static uint16 gapRole_MaxConnInterval = DEFAULT_MAX_CONN_INTERVAL;
- static uint16 gapRole_SlaveLatency = MIN_SLAVE_LATENCY;
- static uint16 gapRole_TimeoutMultiplier = DEFAULT_TIMEOUT_MULTIPLIER;
- static uint16 gapRole_ConnInterval = 0;
- static uint16 gapRole_ConnSlaveLatency = 0;
- static uint16 gapRole_ConnTimeout = 0;
- static uint8 paramUpdateNoSuccessOption = GAPROLE_NO_ACTION;
- static gapRolesCBs_t *pGapRoles_AppCGs = NULL;
- static gapRolesParamUpdateCB_t *pGapRoles_ParamUpdateCB = NULL;
- static void gapRole_ProcessOSALMsg( osal_event_hdr_t *pMsg );
- static void gapRole_ProcessGAPMsg( gapEventHdr_t *pMsg );
- static void gapRole_SetupGAP( void );
- static void gapRole_HandleParamUpdateNoSuccess( void );
- static void gapRole_startConnUpdate( uint8 handleFailure );
- #ifdef EXT_ADV_ENABLE
- extern volatile uint8_t g_rfPhyPktFmt;
- #endif
- bStatus_t GAPRole_SetParameter( uint16 param, uint8 len, void *pValue )
- {
- bStatus_t ret = SUCCESS;
- switch ( param )
- {
- case GAPROLE_IRK:
- if ( len == KEYLEN )
- {
- VOID osal_memcpy( gapRole_IRK, pValue, KEYLEN ) ;
- }
- else
- {
- ret = bleInvalidRange;
- }
- break;
- case GAPROLE_SRK:
- if ( len == KEYLEN )
- {
- VOID osal_memcpy( gapRole_SRK, pValue, KEYLEN ) ;
- }
- else
- {
- ret = bleInvalidRange;
- }
- break;
- case GAPROLE_SIGNCOUNTER:
- if ( len == sizeof ( uint32 ) )
- {
- gapRole_signCounter = *((uint32*)pValue);
- }
- else
- {
- ret = bleInvalidRange;
- }
- break;
- case GAPROLE_ADVERT_ENABLED:
- if ( len == sizeof( uint8 ) )
- {
- uint8 oldAdvEnabled = gapRole_AdvEnabled;
- gapRole_AdvEnabled = *((uint8*)pValue);
- if ( (oldAdvEnabled) && (gapRole_AdvEnabled == FALSE) )
- {
-
- if ( ( gapRole_state == GAPROLE_ADVERTISING )
- || ( gapRole_state == GAPROLE_CONNECTED_ADV )
- || ( gapRole_state == GAPROLE_WAITING_AFTER_TIMEOUT ) )
- {
- VOID GAP_EndDiscoverable( gapRole_TaskID );
- }
- }
- else if ( (oldAdvEnabled == FALSE) && (gapRole_AdvEnabled) )
- {
-
- if ( (gapRole_state == GAPROLE_STARTED)
- || (gapRole_state == GAPROLE_WAITING)
- || (gapRole_state == GAPROLE_CONNECTED)
- || (gapRole_state == GAPROLE_WAITING_AFTER_TIMEOUT) )
- {
- VOID osal_set_event( gapRole_TaskID, START_ADVERTISING_EVT );
- }
- }
- }
- else
- {
- ret = bleInvalidRange;
- }
- break;
- case GAPROLE_ADVERT_OFF_TIME:
- if ( len == sizeof ( uint16 ) )
- {
- gapRole_AdvertOffTime = *((uint16*)pValue);
- }
- else
- {
- ret = bleInvalidRange;
- }
- break;
- case GAPROLE_ADVERT_DATA:
- if ( len <= B_MAX_ADV_LEN )
- {
- VOID osal_memset( gapRole_AdvertData, 0, B_MAX_ADV_LEN );
- VOID osal_memcpy( gapRole_AdvertData, pValue, len );
- gapRole_AdvertDataLen = len;
-
-
- ret = GAP_UpdateAdvertisingData( gapRole_TaskID,
- TRUE, len, gapRole_AdvertData );
- }
- else
- {
- ret = bleInvalidRange;
- }
- break;
- case GAPROLE_SCAN_RSP_DATA:
- if ( len <= B_MAX_ADV_LEN )
- {
- VOID osal_memset( gapRole_ScanRspData, 0, B_MAX_ADV_LEN );
- VOID osal_memcpy( gapRole_ScanRspData, pValue, len );
- gapRole_ScanRspDataLen = len;
-
-
- ret = GAP_UpdateAdvertisingData( gapRole_TaskID,
- FALSE, gapRole_ScanRspDataLen, gapRole_ScanRspData );
- }
- else
- {
- ret = bleInvalidRange;
- }
- break;
- case GAPROLE_ADV_EVENT_TYPE:
- if ( (len == sizeof ( uint8 )) && (*((uint8*)pValue) <= GAP_ADTYPE_ADV_LDC_DIRECT_IND) )
- {
- gapRole_AdvEventType = *((uint8*)pValue);
- }
- else
- {
- ret = bleInvalidRange;
- }
- break;
- case GAPROLE_ADV_DIRECT_TYPE:
- if ( (len == sizeof ( uint8 )) && (*((uint8*)pValue) <= ADDRTYPE_PRIVATE_RESOLVE) )
- {
- gapRole_AdvDirectType = *((uint8*)pValue);
- }
- else
- {
- ret = bleInvalidRange;
- }
- break;
- case GAPROLE_ADV_DIRECT_ADDR:
- if ( len == B_ADDR_LEN )
- {
- VOID osal_memcpy( gapRole_AdvDirectAddr, pValue, B_ADDR_LEN ) ;
- }
- else
- {
- ret = bleInvalidRange;
- }
- break;
- case GAPROLE_ADV_CHANNEL_MAP:
- if ( (len == sizeof ( uint8 )) && (*((uint8*)pValue) <= 0x07) )
- {
- gapRole_AdvChanMap = *((uint8*)pValue);
- }
- else
- {
- ret = bleInvalidRange;
- }
- break;
- case GAPROLE_ADV_FILTER_POLICY:
- if ( (len == sizeof ( uint8 )) && (*((uint8*)pValue) <= GAP_FILTER_POLICY_WHITE) )
- {
- gapRole_AdvFilterPolicy = *((uint8*)pValue);
- }
- else
- {
- ret = bleInvalidRange;
- }
- break;
- case GAPROLE_RSSI_READ_RATE:
- if ( len == sizeof ( uint16 ) )
- {
- gapRole_RSSIReadRate = *((uint16*)pValue);
- if ( (gapRole_RSSIReadRate) && (gapRole_state == GAPROLE_CONNECTED) )
- {
-
- VOID osal_start_timerEx( gapRole_TaskID, RSSI_READ_EVT, gapRole_RSSIReadRate );
- }
- }
- else
- {
- ret = bleInvalidRange;
- }
- break;
- case GAPROLE_PARAM_UPDATE_ENABLE:
- if ( (len == sizeof ( uint8 )) && (*((uint8*)pValue) <= TRUE) )
- {
- gapRole_ParamUpdateEnable = *((uint8*)pValue);
- }
- else
- {
- ret = bleInvalidRange;
- }
- break;
- case GAPROLE_MIN_CONN_INTERVAL:
- {
- uint16 newInterval = *((uint16*)pValue);
- if ( len == sizeof ( uint16 ) &&
- ( newInterval >= MIN_CONN_INTERVAL ) &&
- ( newInterval <= MAX_CONN_INTERVAL ) )
- {
- gapRole_MinConnInterval = newInterval;
- }
- else
- {
- ret = bleInvalidRange;
- }
- }
- break;
- case GAPROLE_MAX_CONN_INTERVAL:
- {
- uint16 newInterval = *((uint16*)pValue);
- if ( len == sizeof ( uint16 ) &&
- ( newInterval >= MIN_CONN_INTERVAL) &&
- ( newInterval <= MAX_CONN_INTERVAL) )
- {
- gapRole_MaxConnInterval = newInterval;
- }
- else
- {
- ret = bleInvalidRange;
- }
- }
- break;
- case GAPROLE_SLAVE_LATENCY:
- {
- uint16 latency = *((uint16*)pValue);
- if ( len == sizeof ( uint16 ) && (latency < MAX_SLAVE_LATENCY) )
- {
- gapRole_SlaveLatency = latency;
- }
- else
- {
- ret = bleInvalidRange;
- }
- }
- break;
- case GAPROLE_TIMEOUT_MULTIPLIER:
- {
- uint16 newTimeout = *((uint16*)pValue);
- if ( len == sizeof ( uint16 )
- && (newTimeout >= MIN_TIMEOUT_MULTIPLIER) && (newTimeout <= MAX_TIMEOUT_MULTIPLIER) )
- {
- gapRole_TimeoutMultiplier = newTimeout;
- }
- else
- {
- ret = bleInvalidRange;
- }
- }
- break;
- case GAPROLE_PARAM_UPDATE_REQ:
- {
- uint8 req = *((uint8*)pValue);
- if ( len == sizeof ( uint8 ) && (req == TRUE) )
- {
-
-
-
- if ( osal_get_timeoutEx( gapRole_TaskID, CONN_PARAM_TIMEOUT_EVT ) == 0 )
- {
-
- gapRole_startConnUpdate( GAPROLE_NO_ACTION );
-
-
- VOID osal_stop_timerEx( gapRole_TaskID, START_CONN_UPDATE_EVT );
- }
- else
- {
- ret = blePending;
- }
- }
- else
- {
- ret = bleInvalidRange;
- }
- }
- break;
- #ifdef EXT_ADV_ENABLE
- case GAPROLE_EXT_ADVERT_ENABLED:
- if ( len == sizeof( uint8 ) )
- {
- gapRole_ExtAdvEnabled = *((uint8*)pValue);
- }
- else
- {
- ret = bleInvalidRange;
- }
- break;
- #endif
- default:
-
- if ( (param < TGAP_PARAMID_MAX) && (len == sizeof ( uint16 )) )
- {
- ret = GAP_SetParamValue( param, *((uint16*)pValue) );
- }
- else
- {
- ret = INVALIDPARAMETER;
- }
- break;
- }
- return ( ret );
- }
- bStatus_t GAPRole_GetParameter( uint16 param, void *pValue )
- {
- bStatus_t ret = SUCCESS;
- switch ( param )
- {
- case GAPROLE_PROFILEROLE:
- *((uint8*)pValue) = gapRole_profileRole;
- break;
- case GAPROLE_IRK:
- VOID osal_memcpy( pValue, gapRole_IRK, KEYLEN ) ;
- break;
- case GAPROLE_SRK:
- VOID osal_memcpy( pValue, gapRole_SRK, KEYLEN ) ;
- break;
- case GAPROLE_SIGNCOUNTER:
- *((uint32*)pValue) = gapRole_signCounter;
- break;
- case GAPROLE_BD_ADDR:
- VOID osal_memcpy( pValue, gapRole_bdAddr, B_ADDR_LEN ) ;
- break;
- case GAPROLE_ADVERT_ENABLED:
- *((uint8*)pValue) = gapRole_AdvEnabled;
- break;
- case GAPROLE_ADVERT_OFF_TIME:
- *((uint16*)pValue) = gapRole_AdvertOffTime;
- break;
- case GAPROLE_ADVERT_DATA:
- VOID osal_memcpy( pValue , gapRole_AdvertData, gapRole_AdvertDataLen );
- break;
- case GAPROLE_SCAN_RSP_DATA:
- VOID osal_memcpy( pValue, gapRole_ScanRspData, gapRole_ScanRspDataLen ) ;
- break;
- case GAPROLE_ADV_EVENT_TYPE:
- *((uint8*)pValue) = gapRole_AdvEventType;
- break;
- case GAPROLE_ADV_DIRECT_TYPE:
- *((uint8*)pValue) = gapRole_AdvDirectType;
- break;
- case GAPROLE_ADV_DIRECT_ADDR:
- VOID osal_memcpy( pValue, gapRole_AdvDirectAddr, B_ADDR_LEN ) ;
- break;
- case GAPROLE_ADV_CHANNEL_MAP:
- *((uint8*)pValue) = gapRole_AdvChanMap;
- break;
- case GAPROLE_ADV_FILTER_POLICY:
- *((uint8*)pValue) = gapRole_AdvFilterPolicy;
- break;
- case GAPROLE_CONNHANDLE:
- *((uint16*)pValue) = gapRole_ConnectionHandle;
- break;
- case GAPROLE_RSSI_READ_RATE:
- *((uint16*)pValue) = gapRole_RSSIReadRate;
- break;
- case GAPROLE_PARAM_UPDATE_ENABLE:
- *((uint16*)pValue) = gapRole_ParamUpdateEnable;
- break;
- case GAPROLE_MIN_CONN_INTERVAL:
- *((uint16*)pValue) = gapRole_MinConnInterval;
- break;
- case GAPROLE_MAX_CONN_INTERVAL:
- *((uint16*)pValue) = gapRole_MaxConnInterval;
- break;
- case GAPROLE_SLAVE_LATENCY:
- *((uint16*)pValue) = gapRole_SlaveLatency;
- break;
- case GAPROLE_TIMEOUT_MULTIPLIER:
- *((uint16*)pValue) = gapRole_TimeoutMultiplier;
- break;
- case GAPROLE_CONN_BD_ADDR:
- VOID osal_memcpy( pValue, gapRole_ConnectedDevAddr, B_ADDR_LEN ) ;
- break;
- case GAPROLE_CONN_INTERVAL:
- *((uint16*)pValue) = gapRole_ConnInterval;
- break;
- case GAPROLE_CONN_LATENCY:
- *((uint16*)pValue) = gapRole_ConnSlaveLatency;
- break;
- case GAPROLE_CONN_TIMEOUT:
- *((uint16*)pValue) = gapRole_ConnTimeout;
- break;
- case GAPROLE_STATE:
- *((uint8*)pValue) = gapRole_state;
- break;
- case GAPROLE_CONNECTION_INTERVAL:
- *((uint16*)pValue) = gapRole_ConnectionInterval;
- break;
- case GAPROLE_CONNECTION_LATENCY:
- *((uint16*)pValue) = gapRole_ConnectionLatency;
- break;
- #ifdef EXT_ADV_ENABLE
- case GAPROLE_EXT_ADVERT_ENABLED:
- *((uint8*)pValue) = gapRole_ExtAdvEnabled;
- break;
- #endif
- default:
-
- if ( param < TGAP_PARAMID_MAX )
- {
- *((uint16*)pValue) = GAP_GetParamValue( param );
- }
- else
- {
- ret = INVALIDPARAMETER;
- }
- break;
- }
- return ( ret );
- }
- bStatus_t GAPRole_StartDevice( gapRolesCBs_t *pAppCallbacks )
- {
- if ( gapRole_state == GAPROLE_INIT )
- {
-
- if ( pAppCallbacks )
- {
- pGapRoles_AppCGs = pAppCallbacks;
- }
-
- gapRole_SetupGAP();
- return ( SUCCESS );
- }
- else
- {
- return ( bleAlreadyInRequestedMode );
- }
- }
- void GAPRole_RegisterAppCBs( gapRolesParamUpdateCB_t *pParamUpdateCB )
- {
- if ( pParamUpdateCB != NULL )
- {
- pGapRoles_ParamUpdateCB = pParamUpdateCB;
- }
- }
- bStatus_t GAPRole_TerminateConnection( void )
- {
- if ( gapRole_state == GAPROLE_CONNECTED )
- {
- return ( GAP_TerminateLinkReq( gapRole_TaskID, gapRole_ConnectionHandle,
- HCI_DISCONNECT_REMOTE_USER_TERM ) );
- }
- else
- {
- return ( bleIncorrectMode );
- }
- }
- void GAPRole_Init( uint8 task_id )
- {
- gapRole_TaskID = task_id;
- gapRole_state = GAPROLE_INIT;
- gapRole_ConnectionHandle = INVALID_CONNHANDLE;
- GAP_RegisterForHCIMsgs( gapRole_TaskID );
-
- gapRole_profileRole = GAP_PROFILE_PERIPHERAL;
- VOID osal_memset( gapRole_IRK, 0, KEYLEN );
- VOID osal_memset( gapRole_SRK, 0, KEYLEN );
- gapRole_signCounter = 0;
- gapRole_AdvEventType = GAP_ADTYPE_ADV_IND;
- gapRole_AdvDirectType = ADDRTYPE_PUBLIC;
- gapRole_AdvChanMap = GAP_ADVCHAN_37;
- gapRole_AdvFilterPolicy = GAP_FILTER_POLICY_ALL;
-
-
-
- }
- uint16 GAPRole_ProcessEvent( uint8 task_id, uint16 events )
- {
- VOID task_id;
- if ( events & SYS_EVENT_MSG )
- {
- uint8 *pMsg;
- if ( (pMsg = osal_msg_receive( gapRole_TaskID )) != NULL )
- {
- gapRole_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 );
- }
- if ( events & START_ADVERTISING_EVT )
- {
- if ( gapRole_AdvEnabled )
- {
- gapAdvertisingParams_t params;
-
- if ( gapRole_state == GAPROLE_CONNECTED )
- {
-
- params.eventType = GAP_ADTYPE_ADV_NONCONN_IND;
- }
- else
- {
- params.eventType = gapRole_AdvEventType;
- params.initiatorAddrType = gapRole_AdvDirectType;
- VOID osal_memcpy( params.initiatorAddr, gapRole_AdvDirectAddr, B_ADDR_LEN );
- }
- params.channelMap = gapRole_AdvChanMap;
- params.filterPolicy = gapRole_AdvFilterPolicy;
- uint8 ret = GAP_MakeDiscoverable( gapRole_TaskID, ¶ms );
- if ( ret != SUCCESS )
- {
- gapRole_state = GAPROLE_ERROR;
-
-
- if ( pGapRoles_AppCGs && pGapRoles_AppCGs->pfnStateChange )
- {
- pGapRoles_AppCGs->pfnStateChange( gapRole_state );
- }
- }
- }
- return ( events ^ START_ADVERTISING_EVT );
- }
- #ifdef EXT_ADV_ENABLE
- if( events & START_SECOND_ADVERTISING_EVENT)
- {
- if( gapRole_ExtAdvEnabled )
- {
- if( GAP_EXTADV_MAKEDiscoverable(gapRole_TaskID, TRUE) != SUCCESS )
- {
- gapRole_state = GAPROLE_ERROR;
-
- if ( pGapRoles_AppCGs && pGapRoles_AppCGs->pfnStateChange )
- {
- pGapRoles_AppCGs->pfnStateChange( gapRole_state );
- }
- }
- }
- return ( events ^ START_SECOND_ADVERTISING_EVENT );
- }
- #endif
- if ( events & RSSI_READ_EVT )
- {
-
- if ( gapRole_state == GAPROLE_CONNECTED )
- {
-
- VOID HCI_ReadRssiCmd( gapRole_ConnectionHandle );
-
- if ( gapRole_RSSIReadRate )
- {
- VOID osal_start_timerEx( gapRole_TaskID, RSSI_READ_EVT, gapRole_RSSIReadRate );
- }
- }
- return ( events ^ RSSI_READ_EVT );
- }
- if ( events & START_CONN_UPDATE_EVT )
- {
-
- gapRole_startConnUpdate( GAPROLE_NO_ACTION );
- return ( events ^ START_CONN_UPDATE_EVT );
- }
-
- if ( events & CONN_PARAM_TIMEOUT_EVT )
- {
-
- gapRole_HandleParamUpdateNoSuccess();
- return ( events ^ CONN_PARAM_TIMEOUT_EVT );
- }
-
-
- return 0;
- }
- static void gapRole_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 )
- {
- int8 rssi = (int8)pPkt->pReturnParam[3];
- if ( (gapRole_state == GAPROLE_CONNECTED) && (rssi != RSSI_NOT_AVAILABLE) )
- {
-
- if ( pGapRoles_AppCGs && pGapRoles_AppCGs->pfnRssiRead )
- {
- pGapRoles_AppCGs->pfnRssiRead( rssi );
- }
- }
- }
- else if(pPkt->cmdOpcode == HCI_LE_SET_DATA_LENGTH)
- {
-
- LOG("[HCI DLE]%d %d %d\n",pPkt->pReturnParam[0],pPkt->pReturnParam[1],pPkt->pReturnParam[2]);
-
- }
- }
- else if(pMsg->status==HCI_COMMAND_STATUS_EVENT_CODE)
- {
- hciEvt_CommandStatus_t *pPkt = (hciEvt_CommandStatus_t *)pMsg;
-
- if ( pPkt->cmdOpcode == HCI_LE_SET_PHY )
- {
-
- AT_LOG("[HCI PHY] %2x \n",pPkt->cmdStatus);
- }
- }
- else if(pMsg->status==HCI_LE_EVENT_CODE)
- {
- hciEvt_BLEEvent_Hdr_t *pPkt = (hciEvt_BLEEvent_Hdr_t *)pMsg;
- if ( pPkt->BLEEventCode == HCI_BLE_DATA_LENGTH_CHANGE_EVENT )
- {
- }
- else if(pPkt->BLEEventCode == HCI_BLE_PHY_UPDATE_COMPLETE_EVENT )
- {
- }
- }
- break;
- case GAP_MSG_EVENT:
- gapRole_ProcessGAPMsg( (gapEventHdr_t *)pMsg );
- break;
- case L2CAP_SIGNAL_EVENT:
- {
- l2capSignalEvent_t *pPkt = (l2capSignalEvent_t *)pMsg;
-
- if ( pPkt->opcode == L2CAP_PARAM_UPDATE_RSP )
- {
- l2capParamUpdateRsp_t *pRsp = (l2capParamUpdateRsp_t *)&(pPkt->cmd.updateRsp);
-
- if ( ( pRsp->result == L2CAP_CONN_PARAMS_REJECTED ) &&
- ( paramUpdateNoSuccessOption == GAPROLE_TERMINATE_LINK ) )
- {
-
- VOID osal_stop_timerEx( gapRole_TaskID, CONN_PARAM_TIMEOUT_EVT );
-
-
- GAPRole_TerminateConnection();
- }
- else
- {
- uint16 timeout = GAP_GetParamValue( TGAP_CONN_PARAM_TIMEOUT );
-
-
-
- VOID osal_start_timerEx( gapRole_TaskID, CONN_PARAM_TIMEOUT_EVT, timeout );
- }
- }
- }
- break;
- default:
- break;
- }
- }
- static void gapRole_ProcessGAPMsg( gapEventHdr_t *pMsg )
- {
- uint8 notify = FALSE;
-
- #ifdef EXT_ADV_ENABLE
- uint8 LegacyAdvEnable = FALSE;
- uint8 SecondAdvEnable = FALSE;
- gapDeviceInitDoneEvent_t *pPktsec = (gapDeviceInitDoneEvent_t *)pMsg;
- bStatus_t statsec = pPktsec->hdr.status;
-
- GAPRole_GetParameter(GAPROLE_ADVERT_ENABLED,&LegacyAdvEnable);
- GAPRole_GetParameter(GAPROLE_EXT_ADVERT_ENABLED , &SecondAdvEnable);
- if( LegacyAdvEnable )
- {
- #endif
- switch ( pMsg->opcode )
- {
- case GAP_DEVICE_INIT_DONE_EVENT:
- {
- gapDeviceInitDoneEvent_t *pPkt = (gapDeviceInitDoneEvent_t *)pMsg;
- bStatus_t stat = pPkt->hdr.status;
- if ( stat == SUCCESS )
- {
-
-
-
-
- VOID osal_memcpy( gapRole_bdAddr, pPkt->devAddr, B_ADDR_LEN );
- gapRole_state = GAPROLE_STARTED;
-
- stat = GAP_UpdateAdvertisingData( gapRole_TaskID,
- TRUE, gapRole_AdvertDataLen, gapRole_AdvertData );
- }
- if ( stat != SUCCESS )
- {
- gapRole_state = GAPROLE_ERROR;
- }
- notify = TRUE;
- }
- break;
- case GAP_ADV_DATA_UPDATE_DONE_EVENT:
- {
- gapAdvDataUpdateEvent_t *pPkt = (gapAdvDataUpdateEvent_t *)pMsg;
- if ( pPkt->hdr.status == SUCCESS )
- {
- if ( pPkt->adType )
- {
-
- pPkt->hdr.status = GAP_UpdateAdvertisingData( gapRole_TaskID,
- FALSE, gapRole_ScanRspDataLen, gapRole_ScanRspData );
- }
- else if ( ( gapRole_state != GAPROLE_ADVERTISING ) &&
- ( osal_get_timeoutEx( gapRole_TaskID, START_ADVERTISING_EVT ) == 0 ) )
- {
-
- VOID osal_set_event( gapRole_TaskID, START_ADVERTISING_EVT );
- }
- }
- if ( pPkt->hdr.status != SUCCESS )
- {
-
- gapRole_state = GAPROLE_ERROR;
- notify = TRUE;
- }
- }
- break;
- case GAP_MAKE_DISCOVERABLE_DONE_EVENT:
- case GAP_END_DISCOVERABLE_DONE_EVENT:
- {
- gapMakeDiscoverableRspEvent_t *pPkt = (gapMakeDiscoverableRspEvent_t *)pMsg;
- if ( pPkt->hdr.status == SUCCESS )
- {
- if ( pMsg->opcode == GAP_MAKE_DISCOVERABLE_DONE_EVENT )
- {
- if (gapRole_state == GAPROLE_CONNECTED)
- {
- gapRole_state = GAPROLE_CONNECTED_ADV;
- }
- else
- {
- gapRole_state = GAPROLE_ADVERTISING;
- }
- }
- else
- {
- if ( gapRole_AdvertOffTime != 0 )
- {
- if ( ( gapRole_AdvEnabled ) )
- {
- VOID osal_start_timerEx( gapRole_TaskID, START_ADVERTISING_EVT, gapRole_AdvertOffTime );
- }
- }
- else
- {
-
-
-
-
- gapRole_AdvEnabled = FALSE;
- }
-
- if (gapRole_state == GAPROLE_CONNECTED_ADV)
- {
-
- gapRole_state = GAPROLE_CONNECTED;
- }
- else if (gapRole_state == GAPROLE_WAITING_AFTER_TIMEOUT)
- {
-
-
- gapRole_AdvEnabled = TRUE;
-
-
- VOID osal_set_event( gapRole_TaskID, START_ADVERTISING_EVT );
- }
- else
- {
-
- gapRole_state = GAPROLE_WAITING;
- }
- }
- }
- else
- {
- gapRole_state = GAPROLE_ERROR;
- }
- notify = TRUE;
- }
- break;
- case GAP_LINK_ESTABLISHED_EVENT:
- {
- gapEstLinkReqEvent_t *pPkt = (gapEstLinkReqEvent_t *)pMsg;
- if ( pPkt->hdr.status == SUCCESS )
- {
- VOID osal_memcpy( gapRole_ConnectedDevAddr, pPkt->devAddr, B_ADDR_LEN );
- gapRole_ConnectionHandle = pPkt->connectionHandle;
- gapRole_ConnectionInterval = pPkt->connInterval;
- gapRole_ConnectionLatency = pPkt->connLatency;
- gapRole_state = GAPROLE_CONNECTED;
-
-
-
-
- if ( gapRole_RSSIReadRate )
- {
-
- VOID osal_start_timerEx( gapRole_TaskID, RSSI_READ_EVT, gapRole_RSSIReadRate );
- }
-
- gapRole_ConnInterval = pPkt->connInterval;
- gapRole_ConnSlaveLatency = pPkt->connLatency;
- gapRole_ConnTimeout = pPkt->connTimeout;
-
- if ( gapRole_ParamUpdateEnable == TRUE )
- {
-
-
- uint16 timeout = GAP_GetParamValue( TGAP_CONN_PAUSE_PERIPHERAL );
-
- osal_start_timerEx( gapRole_TaskID, START_CONN_UPDATE_EVT, timeout*1000 );
- }
-
- VOID GAPBondMgr_LinkEst( pPkt->devAddrType, pPkt->devAddr, pPkt->connectionHandle, GAP_PROFILE_PERIPHERAL );
-
-
-
- gapRole_AdvEnabled = FALSE;
- }
- else if ( pPkt->hdr.status == bleGAPConnNotAcceptable )
- {
-
-
- gapRole_AdvEnabled = FALSE;
-
- gapRole_state = GAPROLE_WAITING;
- }
- else
- {
- gapRole_state = GAPROLE_ERROR;
- }
- notify = TRUE;
- }
- break;
- case GAP_LINK_TERMINATED_EVENT:
- {
- gapTerminateLinkEvent_t *pPkt = (gapTerminateLinkEvent_t *)pMsg;
-
- osal_memset( gapRole_ConnectedDevAddr, 0, B_ADDR_LEN );
-
- gapRole_ConnInterval = 0;
- gapRole_ConnSlaveLatency = 0;
- gapRole_ConnTimeout = 0;
-
- VOID osal_stop_timerEx( gapRole_TaskID, START_CONN_UPDATE_EVT );
- VOID osal_stop_timerEx( gapRole_TaskID, CONN_PARAM_TIMEOUT_EVT );
-
-
- if( pPkt->reason == LL_SUPERVISION_TIMEOUT_TERM )
- {
- gapRole_state = GAPROLE_WAITING_AFTER_TIMEOUT;
- }
- else
- {
- gapRole_state = GAPROLE_WAITING;
- }
- AT_LOG("[DISC].reason %02x\n",pPkt->reason);
- notify = TRUE;
-
-
- if ( gapRole_AdvEnabled)
- {
-
-
- VOID GAP_EndDiscoverable( gapRole_TaskID );
- }
- else
- {
- gapRole_AdvEnabled = TRUE;
- VOID osal_set_event( gapRole_TaskID, START_ADVERTISING_EVT);
- }
- gapRole_ConnectionHandle = INVALID_CONNHANDLE;
- }
- break;
- case GAP_LINK_PARAM_UPDATE_EVENT:
- {
- gapLinkUpdateEvent_t *pPkt = (gapLinkUpdateEvent_t *)pMsg;
-
- VOID osal_stop_timerEx( gapRole_TaskID, CONN_PARAM_TIMEOUT_EVT );
-
- if ( pPkt->hdr.status == SUCCESS )
- {
-
- gapRole_ConnInterval = pPkt->connInterval;
- gapRole_ConnSlaveLatency = pPkt->connLatency;
- gapRole_ConnTimeout = pPkt->connTimeout;
-
-
- if ( osal_get_timeoutEx( gapRole_TaskID, START_CONN_UPDATE_EVT ) == 0 )
- {
-
- if ( pGapRoles_ParamUpdateCB != NULL )
- {
- (*pGapRoles_ParamUpdateCB)( gapRole_ConnInterval,
- gapRole_ConnSlaveLatency,
- gapRole_ConnTimeout );
- }
- }
- }
- }
- break;
- default:
- break;
- }
- #ifdef EXT_ADV_ENABLE
- }
- else if( SecondAdvEnable )
- {
- switch ( pMsg->opcode )
- {
- case GAP_DEVICE_INIT_DONE_EVENT:
- if ( statsec == SUCCESS )
- {
-
- VOID osal_memcpy( gapRole_bdAddr, pPktsec->devAddr, B_ADDR_LEN );
- gapRole_state = GAPROLE_STARTED;
- GapAdv_UpdateParameter(gapRole_bdAddr);
-
- VOID osal_set_event( gapRole_TaskID, START_SECOND_ADVERTISING_EVENT );
- }
- if ( statsec != SUCCESS )
- {
- gapRole_state = GAPROLE_ERROR;
- }
- notify = TRUE;
- break;
- case GAP_LINK_ESTABLISHED_EVENT:
- gapRole_state = GAPROLE_CONNECTED;
- g_rfPhyPktFmt = PKT_FMT_BLR125K;
- notify = TRUE;
- break;
- case GAP_LINK_TERMINATED_EVENT:
- {
- gapTerminateLinkEvent_t *pPkt = (gapTerminateLinkEvent_t *)pMsg;
- g_rfPhyPktFmt = PKT_FMT_BLR125K;
-
- osal_memset( gapRole_ConnectedDevAddr, 0, B_ADDR_LEN );
-
-
- gapRole_ConnInterval = 0;
- gapRole_ConnSlaveLatency = 0;
- gapRole_ConnTimeout = 0;
-
-
- VOID osal_stop_timerEx( gapRole_TaskID, START_CONN_UPDATE_EVT );
- VOID osal_stop_timerEx( gapRole_TaskID, CONN_PARAM_TIMEOUT_EVT );
-
-
- if( pPkt->reason == LL_SUPERVISION_TIMEOUT_TERM )
- {
- gapRole_state = GAPROLE_WAITING_AFTER_TIMEOUT;
- }
- else
- {
- gapRole_state = GAPROLE_WAITING;
- }
-
- AT_LOG("[DISC].reason %02x\n",pPkt->reason);
-
- notify = TRUE;
-
-
-
- gapRole_ExtAdvEnabled = FALSE;
- if ( gapRole_ExtAdvEnabled)
- {
-
-
- VOID GAP_EndDiscoverable( gapRole_TaskID );
- }
- else
- {
- gapRole_ExtAdvEnabled = TRUE;
- VOID osal_set_event( gapRole_TaskID, START_SECOND_ADVERTISING_EVENT);
- }
-
- gapRole_ConnectionHandle = INVALID_CONNHANDLE;
- }
- break;
- case GAP_LINK_PARAM_UPDATE_EVENT:
- {
- gapLinkUpdateEvent_t *pPkt = (gapLinkUpdateEvent_t *)pMsg;
-
- VOID osal_stop_timerEx( gapRole_TaskID, CONN_PARAM_TIMEOUT_EVT );
-
- if ( pPkt->hdr.status == SUCCESS )
- {
-
- gapRole_ConnInterval = pPkt->connInterval;
- gapRole_ConnSlaveLatency = pPkt->connLatency;
- gapRole_ConnTimeout = pPkt->connTimeout;
-
-
- if ( osal_get_timeoutEx( gapRole_TaskID, START_CONN_UPDATE_EVT ) == 0 )
- {
-
- if ( pGapRoles_ParamUpdateCB != NULL )
- {
- (*pGapRoles_ParamUpdateCB)( gapRole_ConnInterval,
- gapRole_ConnSlaveLatency,
- gapRole_ConnTimeout );
- }
- }
- }
- }
- break;
- default:
- break;
- }
-
- }
- #endif
- if ( notify == TRUE )
- {
-
- if ( pGapRoles_AppCGs && pGapRoles_AppCGs->pfnStateChange )
- {
- pGapRoles_AppCGs->pfnStateChange( gapRole_state );
- }
- }
- }
- static void gapRole_SetupGAP( void )
- {
- VOID GAP_DeviceInit( gapRole_TaskID,
- gapRole_profileRole, 0,
- gapRole_IRK, gapRole_SRK,
- &gapRole_signCounter );
- }
- static void gapRole_HandleParamUpdateNoSuccess( void )
- {
-
- switch ( paramUpdateNoSuccessOption )
- {
- case GAPROLE_RESEND_PARAM_UPDATE:
- GAPRole_SendUpdateParam( gapRole_MinConnInterval, gapRole_MaxConnInterval,
- gapRole_SlaveLatency, gapRole_TimeoutMultiplier,
- GAPROLE_RESEND_PARAM_UPDATE );
- break;
- case GAPROLE_TERMINATE_LINK:
- GAPRole_TerminateConnection();
- break;
- case GAPROLE_NO_ACTION:
-
- default:
-
- break;
- }
- }
- static void gapRole_startConnUpdate( uint8 handleFailure )
- {
-
- if ( (gapRole_ConnInterval < gapRole_MinConnInterval) ||
- (gapRole_ConnInterval > gapRole_MaxConnInterval) ||
- (gapRole_ConnSlaveLatency != gapRole_SlaveLatency) ||
- (gapRole_ConnTimeout != gapRole_TimeoutMultiplier) )
- {
- l2capParamUpdateReq_t updateReq;
- uint16 timeout = GAP_GetParamValue( TGAP_CONN_PARAM_TIMEOUT );
-
- updateReq.intervalMin = gapRole_MinConnInterval;
- updateReq.intervalMax = gapRole_MaxConnInterval;
- updateReq.slaveLatency = gapRole_SlaveLatency;
- updateReq.timeoutMultiplier = gapRole_TimeoutMultiplier;
-
- L2CAP_ConnParamUpdateReq( gapRole_ConnectionHandle, &updateReq, gapRole_TaskID );
-
- paramUpdateNoSuccessOption = handleFailure;
-
-
- VOID osal_start_timerEx( gapRole_TaskID, CONN_PARAM_TIMEOUT_EVT, timeout );
- }
- }
- bStatus_t GAPRole_SendUpdateParam( uint16 minConnInterval, uint16 maxConnInterval,
- uint16 latency, uint16 connTimeout, uint8 handleFailure )
- {
-
- if ( gapRole_state != GAPROLE_CONNECTED )
- {
- return ( bleNotConnected );
- }
-
-
- if ( ( minConnInterval >= DEFAULT_MIN_CONN_INTERVAL ) &&
- ( minConnInterval < DEFAULT_MAX_CONN_INTERVAL ) &&
- ( maxConnInterval >= DEFAULT_MIN_CONN_INTERVAL ) &&
- ( maxConnInterval < DEFAULT_MAX_CONN_INTERVAL ) &&
- ( latency < MAX_SLAVE_LATENCY ) &&
- ( connTimeout >= MIN_TIMEOUT_MULTIPLIER ) &&
- ( connTimeout < MAX_TIMEOUT_MULTIPLIER ) )
- {
- gapRole_MinConnInterval = minConnInterval;
- gapRole_MaxConnInterval = maxConnInterval;
- gapRole_SlaveLatency = latency;
- gapRole_TimeoutMultiplier = connTimeout;
-
- gapRole_startConnUpdate( handleFailure );
-
- VOID osal_stop_timerEx( gapRole_TaskID, START_CONN_UPDATE_EVT );
-
- return ( SUCCESS );
- }
- return ( bleInvalidRange );
- }
- #ifdef EXT_ADV_ENABLE
- bStatus_t GapAdv_create(pfnGapCB_t *cb, Gap_ExtAdv_Param *advParam,uint8 *advHandle)
- {
- return(HCI_LE_SetExtAdvParamCmd(advParam));
- }
- bStatus_t GapAdv_loadByHandle(uint8 handle, GapAdv_dataTypes_t dataType,uint16 len, uint8 *pBuf)
- {
- switch (dataType)
- {
- case GAP_ADV_DATA_TYPE_ADV:
- HCI_LE_SetExtAdvDataCmd(handle,len,pBuf);
- break;
- case GAP_ADV_DATA_TYPE_SCAN_RSP:
- HCI_LE_SetExtScanRspDataCmd(handle,len,pBuf);
- break;
- default:
- break;
- }
- return HCI_SUCCESS;
- }
- bStatus_t GapAdv_setEventMask(uint8 handle, GapAdv_eventMaskFlags_t mask)
- {
- return HCI_SUCCESS;
- }
- bStatus_t GapAdv_enable(uint8 handle,
- GapAdv_enableOptions_t enableOptions,
- uint16 durationOrMaxEvents)
- {
- Gap_ExtAdv_EnableParam param;
- param.Enable = TRUE;
- param.AdvHandle = handle;
- switch (enableOptions)
- {
- case GAP_ADV_ENABLE_OPTIONS_USE_MAX:
- param.Duration = enableOptions;
- param.MaxExtAdv_Events = durationOrMaxEvents;
- break;
- case GAP_ADV_ENABLE_OPTIONS_USE_DURATION:
- break;
- case GAP_ADV_ENABLE_OPTIONS_USE_MAX_EVENTS:
- break;
- default:
- break;
- }
- HCI_LE_SetExtAdvEnableCmd(¶m);
- return HCI_SUCCESS;
- }
- #endif
|