123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565 |
- #if ( HOST_CONFIG & ( CENTRAL_CFG | PERIPHERAL_CFG ) )
- #include "bcomdef.h"
- #include "linkdb.h"
- #include "gatt.h"
- #include "gatt_uuid.h"
- #include "gattservapp.h"
- typedef struct
- {
- uint16 connHandle;
- attPrepareWriteReq_t *pPrepareWriteQ;
- } prepareWrites_t;
- typedef struct
- {
- uint16 handle;
- CONST gattServiceCBs_t *pCBs;
- } gattServiceCBsInfo_t;
- typedef struct _serviceCBsList
- {
- struct _serviceCBsList *next;
- gattServiceCBsInfo_t serviceInfo;
- } serviceCBsList_t;
- extern l2capSegmentBuff_t l2capSegmentPkt;
- uint8 GATTServApp_TaskID;
- uint8 appTaskID = INVALID_TASK_ID;
-
- static prepareWrites_t prepareWritesTbl[MAX_NUM_LL_CONN];
- static uint8 maxNumPrepareWrites = 0;
- #ifdef PREPARE_QUEUE_STATIC
- static attPrepareWriteReq_t prepareQueue[MAX_NUM_LL_CONN*GATT_MAX_NUM_PREPARE_WRITES];
- #endif
- static serviceCBsList_t *serviceCBsList = NULL;
- static uint8 attrLen;
- static uint8 attrValue[ATT_MTU_SIZE-1];
- static attMsg_t rsp;
- static CONST gattAttrType_t gattService = { ATT_BT_UUID_SIZE, gattServiceUUID };
- #ifndef HID_VOICE_SPEC
- static uint8 serviceChangedCharProps = GATT_PROP_INDICATE;
- #endif
- static gattCharCfg_t indCharCfg[GATT_MAX_NUM_CONN];
- #if defined ( TESTMODES )
- static uint16 paramValue = 0;
- #endif
- static gattAttribute_t gattAttrTbl[] = {
-
- {
- { ATT_BT_UUID_SIZE, primaryServiceUUID },
- GATT_PERMIT_READ,
- 0,
- (uint8 *)&gattService
- },
- #ifndef HID_VOICE_SPEC
-
- {
- { ATT_BT_UUID_SIZE, characterUUID },
- GATT_PERMIT_READ,
- 0,
- &serviceChangedCharProps
- },
-
- {
- { ATT_BT_UUID_SIZE, serviceChangedUUID },
- 0,
- 0,
- NULL
- },
-
- {
- { ATT_BT_UUID_SIZE, clientCharCfgUUID },
- GATT_PERMIT_READ | GATT_PERMIT_WRITE,
- 0,
- (uint8 *)indCharCfg
- }
- #endif
- };
- static void gattServApp_ProcessMsg( gattMsgEvent_t *pMsg );
- static bStatus_t gattServApp_ProcessExchangeMTUReq( gattMsgEvent_t *pMsg );
- static bStatus_t gattServApp_ProcessFindByTypeValueReq( gattMsgEvent_t *pMsg, uint16 *pErrHandle );
- static bStatus_t gattServApp_ProcessReadByTypeReq( gattMsgEvent_t *pMsg, uint16 *pErrHandle );
- static bStatus_t gattServApp_ProcessReadReq( gattMsgEvent_t *pMsg, uint16 *pErrHandle );
- static bStatus_t gattServApp_ProcessReadBlobReq( gattMsgEvent_t *pMsg, uint16 *pErrHandle );
- static bStatus_t gattServApp_ProcessReadMultiReq( gattMsgEvent_t *pMsg, uint16 *pErrHandle );
- static bStatus_t gattServApp_ProcessReadByGrpTypeReq( gattMsgEvent_t *pMsg, uint16 *pErrHandle );
- static bStatus_t gattServApp_ProcessWriteReq( gattMsgEvent_t *pMsg, uint16 *pErrHandle );
- static bStatus_t gattServApp_ProcessPrepareWriteReq( gattMsgEvent_t *pMsg, uint16 *pErrHandle );
- static bStatus_t gattServApp_ProcessExecuteWriteReq( gattMsgEvent_t *pMsg, uint16 *pErrHandle );
- static bStatus_t gattServApp_RegisterServiceCBs( uint16 handle, CONST gattServiceCBs_t *pServiceCBs );
- static bStatus_t gattServApp_DeregisterServiceCBs( uint16 handle );
- static bStatus_t gattServApp_SetNumPrepareWrites( uint8 numPrepareWrites );
- static uint8 gattServApp_PrepareWriteQInUse( void );
- static CONST gattServiceCBs_t *gattServApp_FindServiceCBs( uint16 service );
- static bStatus_t gattServApp_EnqueuePrepareWriteReq( uint16 connHandle, attPrepareWriteReq_t *pReq );
- static prepareWrites_t *gattServApp_FindPrepareWriteQ( uint16 connHandle );
- static gattCharCfg_t *gattServApp_FindCharCfgItem( uint16 connHandle,
- gattCharCfg_t *charCfgTbl );
- static pfnGATTReadAttrCB_t gattServApp_FindReadAttrCB( uint16 handle );
- static pfnGATTWriteAttrCB_t gattServApp_FindWriteAttrCB( uint16 handle );
- static pfnGATTAuthorizeAttrCB_t gattServApp_FindAuthorizeAttrCB( uint16 handle );
- static void gattServApp_HandleConnStatusCB( uint16 connHandle, uint8 changeType );
- static bStatus_t gattServApp_WriteAttrCB( uint16 connHandle, gattAttribute_t *pAttr,
- uint8 *pValue, uint8 len, uint16 offset );
- CONST gattServiceCBs_t gattServiceCBs =
- {
- NULL,
- gattServApp_WriteAttrCB,
- NULL
- };
- static gattServMsgCB_t s_GATTServCB = NULL;
- void GATTServApp_RegisterForMsg( uint8 taskID )
- {
- appTaskID = taskID;
- }
- void GATTServApp_Init( uint8 taskId )
- {
- GATTServApp_TaskID = taskId;
-
- GATTServApp_InitCharCfg( INVALID_CONNHANDLE, indCharCfg );
-
- for ( uint8 i = 0; i < MAX_NUM_LL_CONN; i++ )
- {
-
- prepareWritesTbl[i].connHandle = INVALID_CONNHANDLE;
-
- prepareWritesTbl[i].pPrepareWriteQ = NULL;
- }
-
- gattServApp_SetNumPrepareWrites( GATT_MAX_NUM_PREPARE_WRITES );
-
- GATT_RegisterForReq( GATTServApp_TaskID );
-
- VOID linkDB_Register( gattServApp_HandleConnStatusCB );
- }
- uint16 GATTServApp_ProcessEvent( uint8 task_id, uint16 events )
- {
- if ( events & SYS_EVENT_MSG )
- {
- osal_event_hdr_t *pMsg;
- if ( (pMsg = ( osal_event_hdr_t *)osal_msg_receive( GATTServApp_TaskID )) != NULL )
- {
-
- switch ( pMsg->event )
- {
-
- case GATT_MSG_EVENT:
- gattServApp_ProcessMsg( (gattMsgEvent_t *)pMsg );
- break;
- default:
-
- break;
- }
-
- VOID osal_msg_deallocate( (uint8 *)pMsg );
- }
-
- return (events ^ SYS_EVENT_MSG);
- }
-
- return 0;
- }
- bStatus_t GATTServApp_RegisterService( gattAttribute_t *pAttrs, uint16 numAttrs,
- CONST gattServiceCBs_t *pServiceCBs )
- {
- uint8 status;
-
- if ( pAttrs != NULL )
- {
- gattService_t service;
- service.attrs = pAttrs;
- service.numAttrs = numAttrs;
- status = GATT_RegisterService( &service );
- if ( ( status == SUCCESS ) && ( pServiceCBs != NULL ) )
- {
-
- status = gattServApp_RegisterServiceCBs( GATT_SERVICE_HANDLE( pAttrs ),
- pServiceCBs );
- }
- }
- else
- {
- status = INVALIDPARAMETER;
- }
- return ( status );
- }
- bStatus_t GATTServApp_DeregisterService( uint16 handle, gattAttribute_t **p2pAttrs )
- {
- uint8 status;
-
- status = gattServApp_DeregisterServiceCBs( handle );
- if ( status == SUCCESS )
- {
- gattService_t service;
-
- status = GATT_DeregisterService( handle, &service );
- if ( status == SUCCESS )
- {
- if ( p2pAttrs != NULL )
- {
- *p2pAttrs = service.attrs;
- }
- }
- }
- return ( status );
- }
- bStatus_t GATTServApp_SetParameter( uint8 param, uint8 len, void *pValue )
- {
- bStatus_t status = SUCCESS;
- switch ( param )
- {
- case GATT_PARAM_NUM_PREPARE_WRITES:
- if ( len == sizeof ( uint8 ) )
- {
- if ( !gattServApp_PrepareWriteQInUse() )
- {
-
- status = gattServApp_SetNumPrepareWrites( *((uint8*)pValue) );
- }
- else
- {
- status = FAILURE;
- }
- }
- else
- {
- status = bleInvalidRange;
- }
- break;
- default:
- status = INVALIDPARAMETER;
- break;
- }
- return ( status );
- }
- bStatus_t GATTServApp_GetParameter( uint8 param, void *pValue )
- {
- bStatus_t status = SUCCESS;
- switch ( param )
- {
- case GATT_PARAM_NUM_PREPARE_WRITES:
- *((uint8*)pValue) = maxNumPrepareWrites;
- break;
- default:
- status = INVALIDPARAMETER;
- break;
- }
- return ( status );
- }
- static bStatus_t gattServApp_SetNumPrepareWrites( uint8 numPrepareWrites )
- {
- attPrepareWriteReq_t *pQueue;
- uint16 queueSize = ( MAX_NUM_LL_CONN * numPrepareWrites * sizeof( attPrepareWriteReq_t ) );
-
- maxNumPrepareWrites = 0;
-
- if ( prepareWritesTbl[0].pPrepareWriteQ != NULL )
- {
- #ifndef PREPARE_QUEUE_STATIC
- osal_mem_free( prepareWritesTbl[0].pPrepareWriteQ );
- #endif
-
- for ( uint8 i = 0; i < MAX_NUM_LL_CONN; i++ )
- {
- prepareWritesTbl[i].pPrepareWriteQ = NULL;
- }
- }
-
- #ifdef PREPARE_QUEUE_STATIC
- pQueue = prepareQueue;
- #else
- pQueue = osal_mem_alloc( queueSize );
- #endif
- if ( pQueue != NULL )
- {
-
- VOID osal_memset( pQueue, 0, queueSize );
-
- for ( uint8 i = 0; i < MAX_NUM_LL_CONN; i++ )
- {
- uint8 nextQ = i * numPrepareWrites;
- prepareWritesTbl[i].pPrepareWriteQ = &(pQueue[nextQ]);
-
- for ( uint8 j = 0; j < numPrepareWrites; j++ )
- {
- prepareWritesTbl[i].pPrepareWriteQ[j].handle = GATT_INVALID_HANDLE;
- }
- }
-
- maxNumPrepareWrites = numPrepareWrites;
- return ( SUCCESS );
- }
- return ( bleMemAllocError );
- }
- gattAttribute_t *GATTServApp_FindAttr( gattAttribute_t *pAttrTbl, uint16 numAttrs, uint8 *pValue )
- {
- for ( uint16 i = 0; i < numAttrs; i++ )
- {
- if ( pAttrTbl[i].pValue == pValue )
- {
-
- return ( &(pAttrTbl[i]) );
- }
- }
- return ( (gattAttribute_t *)NULL );
- }
- bStatus_t GATTServApp_AddService( uint32 services )
- {
- uint8 status = SUCCESS;
- if ( services & GATT_SERVICE )
- {
-
- status = GATTServApp_RegisterService( gattAttrTbl, GATT_NUM_ATTRS( gattAttrTbl ),
- &gattServiceCBs );
- }
- return ( status );
- }
- bStatus_t GATTServApp_DelService( uint32 services )
- {
- uint8 status = SUCCESS;
- if ( services & GATT_SERVICE )
- {
-
- status = GATTServApp_DeregisterService( GATT_SERVICE_HANDLE( gattAttrTbl ), NULL );
- }
- return ( status );
- }
- static bStatus_t gattServApp_RegisterServiceCBs( uint16 handle,
- CONST gattServiceCBs_t *pServiceCBs )
- {
- serviceCBsList_t *pNewItem;
-
- if ( handle == GATT_INVALID_HANDLE )
- {
- return ( INVALIDPARAMETER );
- }
-
- pNewItem = (serviceCBsList_t *)osal_mem_alloc( sizeof( serviceCBsList_t ) );
- if ( pNewItem == NULL )
- {
-
- return ( bleMemAllocError );
- }
-
- pNewItem->next = NULL;
- pNewItem->serviceInfo.handle = handle;
- pNewItem->serviceInfo.pCBs = pServiceCBs;
-
- if ( serviceCBsList == NULL )
- {
-
- serviceCBsList = pNewItem;
- }
- else
- {
- serviceCBsList_t *pLoop = serviceCBsList;
-
- while ( pLoop->next != NULL )
- {
- pLoop = pLoop->next;
- }
-
- pLoop->next = pNewItem;
- }
- return ( SUCCESS );
- }
- static bStatus_t gattServApp_DeregisterServiceCBs( uint16 handle )
- {
- serviceCBsList_t *pLoop = serviceCBsList;
- serviceCBsList_t *pPrev = NULL;
-
- while ( pLoop != NULL )
- {
- if ( pLoop->serviceInfo.handle == handle )
- {
-
- if ( pPrev == NULL )
- {
-
- serviceCBsList = pLoop->next;
- }
- else
- {
- pPrev->next = pLoop->next;
- }
-
- osal_mem_free( pLoop );
- return ( SUCCESS );
- }
- pPrev = pLoop;
- pLoop = pLoop->next;
- }
-
- return ( FAILURE );
- }
- static CONST gattServiceCBs_t *gattServApp_FindServiceCBs( uint16 handle )
- {
- serviceCBsList_t *pLoop = serviceCBsList;
- while ( pLoop != NULL )
- {
- if ( pLoop->serviceInfo.handle == handle )
- {
- return ( pLoop->serviceInfo.pCBs );
- }
-
- pLoop = pLoop->next;
- }
- return ( (gattServiceCBs_t *)NULL );
- }
- static void gattServApp_ProcessMsg( gattMsgEvent_t *pMsg )
- {
- uint16 errHandle = GATT_INVALID_HANDLE;
- uint8 status;
- #if defined ( TESTMODES )
- if ( paramValue == GATT_TESTMODE_NO_RSP )
- {
-
-
- GATT_AppCompletedMsg( pMsg );
-
-
- return;
- }
- #endif
-
- switch ( pMsg->method )
- {
- case ATT_EXCHANGE_MTU_REQ:
- status = gattServApp_ProcessExchangeMTUReq( pMsg );
- break;
- case ATT_FIND_BY_TYPE_VALUE_REQ:
- status = gattServApp_ProcessFindByTypeValueReq( pMsg, &errHandle );
- break;
- case ATT_READ_BY_TYPE_REQ:
- status = gattServApp_ProcessReadByTypeReq( pMsg, &errHandle );
- break;
- case ATT_READ_REQ:
- status = gattServApp_ProcessReadReq( pMsg, &errHandle );
- break;
- case ATT_READ_BLOB_REQ:
- status = gattServApp_ProcessReadBlobReq( pMsg, &errHandle );
- break;
- case ATT_READ_MULTI_REQ:
- status = gattServApp_ProcessReadMultiReq( pMsg, &errHandle );
- break;
- case ATT_READ_BY_GRP_TYPE_REQ:
- status = gattServApp_ProcessReadByGrpTypeReq( pMsg, &errHandle );
- break;
- case ATT_WRITE_REQ:
- status = gattServApp_ProcessWriteReq( pMsg, &errHandle );
- break;
- case ATT_PREPARE_WRITE_REQ:
- status = gattServApp_ProcessPrepareWriteReq( pMsg, &errHandle );
- break;
- case ATT_EXECUTE_WRITE_REQ:
- status = gattServApp_ProcessExecuteWriteReq( pMsg, &errHandle );
- break;
- default:
-
- status = SUCCESS;
- break;
- }
-
- if ( status != SUCCESS )
- {
-
- if ( pMsg->hdr.status != bleNotConnected )
- {
- attErrorRsp_t *pRsp = &rsp.errorRsp;
- pRsp->reqOpcode = pMsg->method;
- pRsp->handle = errHandle;
- pRsp->errCode = status;
- VOID ATT_ErrorRsp( pMsg->connHandle, pRsp );
- }
- }
-
-
-
- GATT_AppCompletedMsg( pMsg );
-
- if(s_GATTServCB)
- s_GATTServCB(pMsg);
- }
- static bStatus_t gattServApp_ProcessExchangeMTUReq( gattMsgEvent_t *pMsg )
- {
- attExchangeMTURsp_t *pRsp = &rsp.exchangeMTURsp;
-
-
- #if defined ( TESTMODES )
- if ( paramValue == GATT_TESTMODE_MAX_MTU_SIZE )
- {
- pRsp->serverRxMTU = ATT_MAX_MTU_SIZE;
- }
- else
- #endif
- pRsp->serverRxMTU = g_ATT_MTU_SIZE_MAX;
-
- VOID ATT_ExchangeMTURsp( pMsg->connHandle, pRsp );
- return ( SUCCESS );
- }
- static bStatus_t gattServApp_ProcessFindByTypeValueReq( gattMsgEvent_t *pMsg, uint16 *pErrHandle )
- {
- attFindByTypeValueReq_t *pReq = &pMsg->msg.findByTypeValueReq;
- attFindByTypeValueRsp_t *pRsp = &rsp.findByTypeValueRsp;
- gattAttribute_t *pAttr;
- uint16 service;
-
- VOID osal_memset( pRsp, 0, sizeof( attFindByTypeValueRsp_t ) );
-
-
-
-
-
-
- pAttr = GATT_FindHandleUUID( pReq->startHandle, pReq->endHandle,
- pReq->type.uuid, pReq->type.len, &service );
- while ( ( pAttr != NULL ) && ( pRsp->numInfo < g_ATT_MAX_NUM_HANDLES_INFO ) )
- {
- uint16 grpEndHandle;
-
-
- if ( GATTServApp_ReadAttr( pMsg->connHandle, pAttr, service, attrValue,
- &attrLen, 0, (g_ATT_MTU_SIZE-7) ) == SUCCESS )
- {
-
- if ( ( pReq->len == attrLen ) && osal_memcmp( pReq->value, attrValue, attrLen) )
- {
-
-
-
- pRsp->handlesInfo[pRsp->numInfo].handle = pAttr->handle;
- }
- }
-
- pAttr = GATT_FindNextAttr( pAttr, pReq->endHandle, service, &grpEndHandle );
-
- if ( pRsp->handlesInfo[pRsp->numInfo].handle != 0 )
- {
-
-
-
-
- if ( pAttr != NULL )
- {
- pRsp->handlesInfo[pRsp->numInfo++].grpEndHandle = grpEndHandle;
- }
- else
- {
-
-
- pRsp->handlesInfo[pRsp->numInfo++].grpEndHandle = GATT_MAX_HANDLE;
- }
- }
- }
- if ( pRsp->numInfo > 0 )
- {
-
- VOID ATT_FindByTypeValueRsp( pMsg->connHandle, pRsp );
- return ( SUCCESS );
- }
- *pErrHandle = pReq->startHandle;
- return ( ATT_ERR_ATTR_NOT_FOUND );
- }
- static bStatus_t gattServApp_ProcessReadByTypeReq( gattMsgEvent_t *pMsg, uint16 *pErrHandle )
- {
- attReadByTypeReq_t *pReq = &pMsg->msg.readByTypeReq;
- attReadByTypeRsp_t *pRsp = &rsp.readByTypeRsp;
- uint16 startHandle = pReq->startHandle;
- uint8 dataLen = 0;
- uint8 status = SUCCESS;
-
-
-
-
- while ( dataLen <= (g_ATT_MTU_SIZE-4) )
- {
- uint16 service;
- gattAttribute_t *pAttr;
-
-
- pAttr = GATT_FindHandleUUID( startHandle, pReq->endHandle, pReq->type.uuid,
- pReq->type.len, &service );
- if ( pAttr == NULL )
- {
- break;
- }
-
- startHandle = pAttr->handle;
-
- status = GATT_VerifyReadPermissions( pMsg->connHandle, pAttr->permissions );
- if ( status != SUCCESS )
- {
- break;
- }
-
-
-
- status = GATTServApp_ReadAttr( pMsg->connHandle, pAttr, service, attrValue,
- &attrLen, 0, (g_ATT_MTU_SIZE-4) );
- if ( status != SUCCESS )
- {
- break;
- }
-
- if ( dataLen == 0 )
- {
-
- pRsp->len = 2 + attrLen;
- }
- else
- {
-
-
- if ( pRsp->len != 2 + attrLen )
- {
- break;
- }
- }
-
- if ( dataLen + attrLen > (g_ATT_MTU_SIZE-4) )
- {
- break;
- }
-
- pRsp->dataList[dataLen++] = LO_UINT16( pAttr->handle );
- pRsp->dataList[dataLen++] = HI_UINT16( pAttr->handle );
- VOID osal_memcpy( &(pRsp->dataList[dataLen]), attrValue, attrLen );
- dataLen += attrLen;
- if ( startHandle == GATT_MAX_HANDLE )
- {
- break;
- }
-
- startHandle++;
- }
-
- if ( dataLen > 0 )
- {
-
- pRsp->numPairs = dataLen / pRsp->len;
-
- VOID ATT_ReadByTypeRsp( pMsg->connHandle, pRsp );
- return ( SUCCESS );
- }
- if ( status == SUCCESS )
- {
-
- status = ATT_ERR_ATTR_NOT_FOUND;
- }
- *pErrHandle = startHandle;
- return ( status );
- }
- static bStatus_t gattServApp_ProcessReadReq( gattMsgEvent_t *pMsg, uint16 *pErrHandle )
- {
- attReadReq_t *pReq = &pMsg->msg.readReq;
- gattAttribute_t *pAttr;
- uint16 service;
- uint8 status;
- pAttr = GATT_FindHandle( pReq->handle, &service );
- if ( pAttr != NULL )
- {
- attReadRsp_t *pRsp = &rsp.readRsp;
-
-
-
- status = GATTServApp_ReadAttr( pMsg->connHandle, pAttr, service, pRsp->value,
- &pRsp->len, 0, (g_ATT_MTU_SIZE-1) );
- if ( status == SUCCESS )
- {
-
- VOID ATT_ReadRsp( pMsg->connHandle, pRsp );
- }
- }
- else
- {
- status = ATT_ERR_INVALID_HANDLE;
- }
- if ( status != SUCCESS )
- {
- *pErrHandle = pReq->handle;
- }
- return ( status );
- }
- static bStatus_t gattServApp_ProcessReadBlobReq( gattMsgEvent_t *pMsg, uint16 *pErrHandle )
- {
- attReadBlobReq_t *pReq = &pMsg->msg.readBlobReq;
- gattAttribute_t *pAttr;
- uint16 service;
- uint8 status;
- pAttr = GATT_FindHandle( pReq->handle, &service );
- if ( pAttr != NULL )
- {
- attReadBlobRsp_t *pRsp = &rsp.readBlobRsp;
-
-
-
- status = GATTServApp_ReadAttr( pMsg->connHandle, pAttr, service, pRsp->value,
- &pRsp->len, pReq->offset, (g_ATT_MTU_SIZE-1) );
- if ( status == SUCCESS )
- {
-
- VOID ATT_ReadBlobRsp( pMsg->connHandle, pRsp );
- }
- }
- else
- {
- status = ATT_ERR_INVALID_HANDLE;
- }
- if ( status != SUCCESS )
- {
- *pErrHandle = pReq->handle;
- }
- return ( status );
- }
- static bStatus_t gattServApp_ProcessReadMultiReq( gattMsgEvent_t *pMsg, uint16 *pErrHandle )
- {
- attReadMultiReq_t *pReq = &pMsg->msg.readMultiReq;
- attReadMultiRsp_t *pRsp = &rsp.readMultiRsp;
- uint8 status = SUCCESS;
-
- pRsp->len = 0;
- for ( uint8 i = 0; ( i < pReq->numHandles ) && ( pRsp->len < (g_ATT_MTU_SIZE-1) ); i++ )
- {
- gattAttribute_t *pAttr;
- uint16 service;
- pAttr = GATT_FindHandle( pReq->handle[i], &service );
- if ( pAttr == NULL )
- {
-
- status = ATT_ERR_INVALID_HANDLE;
-
- *pErrHandle = pReq->handle[i];
- break;
- }
-
-
- status = GATTServApp_ReadAttr( pMsg->connHandle, pAttr, service, attrValue,
- &attrLen, 0, (g_ATT_MTU_SIZE-1) );
- if ( status != SUCCESS )
- {
-
- *pErrHandle = pReq->handle[i];
- break;
- }
-
- if ( pRsp->len + attrLen > (g_ATT_MTU_SIZE-1) )
- {
- attrLen = (g_ATT_MTU_SIZE-1) - pRsp->len;
- }
-
- VOID osal_memcpy( &(pRsp->values[pRsp->len]), attrValue, attrLen );
- pRsp->len += attrLen;
- }
- if ( status == SUCCESS )
- {
-
- VOID ATT_ReadMultiRsp( pMsg->connHandle, pRsp );
- }
- return ( status );
- }
- static bStatus_t gattServApp_ProcessReadByGrpTypeReq( gattMsgEvent_t *pMsg, uint16 *pErrHandle )
- {
- attReadByGrpTypeReq_t *pReq = &pMsg->msg.readByGrpTypeReq;
- attReadByGrpTypeRsp_t *pRsp = &rsp.readByGrpTypeRsp;
- uint16 service;
- gattAttribute_t *pAttr;
- uint8 dataLen = 0;
- uint8 status = SUCCESS;
-
-
-
-
-
-
- pAttr = GATT_FindHandleUUID( pReq->startHandle, pReq->endHandle,
- pReq->type.uuid, pReq->type.len, &service );
- while ( pAttr != NULL )
- {
- uint16 endGrpHandle;
-
-
-
- status = GATT_VerifyReadPermissions( pMsg->connHandle, pAttr->permissions );
- if ( status != SUCCESS )
- {
- *pErrHandle = pAttr->handle;
- break;
- }
-
-
-
- status = GATTServApp_ReadAttr( pMsg->connHandle, pAttr, service, attrValue,
- &attrLen, 0, (g_ATT_MTU_SIZE-6) );
- if ( status != SUCCESS )
- {
-
- *pErrHandle = pAttr->handle;
- break;
- }
-
- if ( dataLen == 0 )
- {
-
- pRsp->len = 2 + 2 + attrLen;
- }
- else
- {
-
-
- if ( pRsp->len != 2 + 2 + attrLen )
- {
- break;
- }
-
- if ( dataLen + attrLen > (g_ATT_MTU_SIZE-6) )
- {
- break;
- }
- }
-
- pRsp->dataList[dataLen++] = LO_UINT16( pAttr->handle );
- pRsp->dataList[dataLen++] = HI_UINT16( pAttr->handle );
-
- pAttr = GATT_FindNextAttr( pAttr, pReq->endHandle, service, &endGrpHandle );
-
- if ( pAttr != NULL )
- {
-
-
- pRsp->dataList[dataLen++] = LO_UINT16( endGrpHandle );
- pRsp->dataList[dataLen++] = HI_UINT16( endGrpHandle );
- }
- else
- {
-
- pRsp->dataList[dataLen++] = LO_UINT16( GATT_MAX_HANDLE );
- pRsp->dataList[dataLen++] = HI_UINT16( GATT_MAX_HANDLE );
- }
-
- VOID osal_memcpy( &(pRsp->dataList[dataLen]), attrValue, attrLen );
- dataLen += attrLen;
- }
-
- if ( dataLen > 0 )
- {
-
- pRsp->numGrps = dataLen / pRsp->len;
-
- VOID ATT_ReadByGrpTypeRsp( pMsg->connHandle, pRsp );
- return ( SUCCESS );
- }
- if ( status == SUCCESS )
- {
-
- status = ATT_ERR_ATTR_NOT_FOUND;
- }
- *pErrHandle = pReq->startHandle;
- return ( status );
- }
- static bStatus_t gattServApp_ProcessWriteReq( gattMsgEvent_t *pMsg, uint16 *pErrHandle )
- {
- attWriteReq_t *pReq = &(pMsg->msg.writeReq);
- gattAttribute_t *pAttr;
- uint16 service;
- uint8 status = SUCCESS;
-
-
-
- pAttr = GATT_FindHandle( pReq->handle, &service );
- if ( pAttr != NULL )
- {
-
- if ( gattPermitAuthorWrite( pAttr->permissions ) )
- {
-
- pfnGATTAuthorizeAttrCB_t pfnCB = gattServApp_FindAuthorizeAttrCB( service );
- if ( pfnCB != NULL )
- {
- status = (*pfnCB)( pMsg->connHandle, pAttr, ATT_WRITE_REQ );
- }
- else
- {
- status = ATT_ERR_UNLIKELY;
- }
- }
-
- if ( status == SUCCESS )
- {
-
- status = GATTServApp_WriteAttr( pMsg->connHandle, pReq->handle,
- pReq->value, pReq->len, 0 );
- if ( ( status == SUCCESS ) && ( pReq->cmd == FALSE ) )
- {
-
-
- uint8 st=ATT_WriteRsp( pMsg->connHandle );
-
- }
- }
- }
- else
- {
- status = ATT_ERR_INVALID_HANDLE;
- }
- if ( status != SUCCESS )
- {
- *pErrHandle = pReq->handle;
- }
- return ( pReq->cmd ? SUCCESS : status );
- }
- static bStatus_t gattServApp_ProcessPrepareWriteReq( gattMsgEvent_t *pMsg, uint16 *pErrHandle )
- {
- attPrepareWriteReq_t *pReq = &pMsg->msg.prepareWriteReq;
- gattAttribute_t *pAttr;
- uint16 service;
- uint8 status = SUCCESS;
- pAttr = GATT_FindHandle( pReq->handle, &service );
- if ( pAttr != NULL )
- {
-
- if ( gattPermitAuthorWrite( pAttr->permissions ) )
- {
-
- pfnGATTAuthorizeAttrCB_t pfnCB = gattServApp_FindAuthorizeAttrCB( service );
- if ( pfnCB != NULL )
- {
- status = (*pfnCB)( pMsg->connHandle, pAttr, ATT_WRITE_REQ );
- }
- else
- {
- status = ATT_ERR_UNLIKELY;
- }
- }
- if ( status == SUCCESS )
- {
- #if defined ( TESTMODES )
- if ( paramValue == GATT_TESTMODE_CORRUPT_PW_DATA )
- {
- pReq->value[0] = ~(pReq->value[0]);
- }
- #endif
-
- status = gattServApp_EnqueuePrepareWriteReq( pMsg->connHandle, pReq );
- if ( status == SUCCESS )
- {
-
-
- VOID ATT_PrepareWriteRsp( pMsg->connHandle, (attPrepareWriteRsp_t *)pReq );
- }
- }
- }
- else
- {
- status = ATT_ERR_INVALID_HANDLE;
- }
- if ( status != SUCCESS )
- {
- *pErrHandle = pReq->handle;
- }
- return ( status );
- }
- static bStatus_t gattServApp_ProcessExecuteWriteReq( gattMsgEvent_t *pMsg, uint16 *pErrHandle )
- {
- attExecuteWriteReq_t *pReq = &pMsg->msg.executeWriteReq;
- prepareWrites_t *pQueue;
- uint8 status = SUCCESS;
-
- pQueue = gattServApp_FindPrepareWriteQ( pMsg->connHandle );
- if ( pQueue != NULL )
- {
- for ( uint8 i = 0; i < maxNumPrepareWrites; i++ )
- {
- attPrepareWriteReq_t *pWriteReq = &(pQueue->pPrepareWriteQ[i]);
-
- if ( pWriteReq->handle == GATT_INVALID_HANDLE )
- {
- break;
- }
-
- if ( pReq->flags == ATT_WRITE_PREPARED_VALUES )
- {
- status = GATTServApp_WriteAttr( pMsg->connHandle, pWriteReq->handle,
- pWriteReq->value, pWriteReq->len,
- pWriteReq->offset );
-
-
-
-
- if ( status != SUCCESS )
- {
-
- pReq->flags = ATT_CANCEL_PREPARED_WRITES;
-
-
-
- *pErrHandle = pWriteReq->handle;
- }
- }
- else
- {
-
- }
-
- VOID osal_memset( pWriteReq, 0, sizeof( attPrepareWriteRsp_t ) );
-
- pWriteReq->handle = GATT_INVALID_HANDLE;
- }
-
- pQueue->connHandle = INVALID_CONNHANDLE;
- }
-
- if ( status == SUCCESS )
- {
- VOID ATT_ExecuteWriteRsp( pMsg->connHandle );
- }
- return ( status );
- }
- static bStatus_t gattServApp_EnqueuePrepareWriteReq( uint16 connHandle, attPrepareWriteReq_t *pReq )
- {
- prepareWrites_t *pQueue;
-
- pQueue = gattServApp_FindPrepareWriteQ( connHandle );
- if ( pQueue == NULL )
- {
-
- pQueue = gattServApp_FindPrepareWriteQ( INVALID_CONNHANDLE );
- if ( pQueue != NULL )
- {
- pQueue->connHandle = connHandle;
- }
- }
-
- if ( pQueue != NULL )
- {
- for ( uint8 i = 0; i < maxNumPrepareWrites; i++ )
- {
- if ( pQueue->pPrepareWriteQ[i].handle == GATT_INVALID_HANDLE )
- {
-
- VOID osal_memcpy( &(pQueue->pPrepareWriteQ[i]), pReq, sizeof ( attPrepareWriteReq_t ) );
-
- return ( SUCCESS );
- }
- }
- }
- return ( ATT_ERR_PREPARE_QUEUE_FULL );
- }
- static prepareWrites_t *gattServApp_FindPrepareWriteQ( uint16 connHandle )
- {
-
- for ( uint8 i = 0; i < MAX_NUM_LL_CONN; i++ )
- {
- if ( prepareWritesTbl[i].connHandle == connHandle )
- {
-
- return ( &(prepareWritesTbl[i]) );
- }
- }
- return ( (prepareWrites_t *)NULL );
- }
- static uint8 gattServApp_PrepareWriteQInUse( void )
- {
-
- for ( uint8 i = 0; i < MAX_NUM_LL_CONN; i++ )
- {
- if ( prepareWritesTbl[i].connHandle != INVALID_CONNHANDLE )
- {
- for ( uint8 j = 0; j < maxNumPrepareWrites; j++ )
- {
- if ( prepareWritesTbl[i].pPrepareWriteQ[j].handle != GATT_INVALID_HANDLE )
- {
-
- return ( TRUE );
- }
- }
- }
- }
- return ( FALSE );
- }
- static gattCharCfg_t *gattServApp_FindCharCfgItem( uint16 connHandle,
- gattCharCfg_t *charCfgTbl )
- {
- for ( uint8 i = 0; i < GATT_MAX_NUM_CONN; i++ )
- {
- if ( charCfgTbl[i].connHandle == connHandle )
- {
-
- return ( &(charCfgTbl[i]) );
- }
- }
- return ( (gattCharCfg_t *)NULL );
- }
- static pfnGATTReadAttrCB_t gattServApp_FindReadAttrCB( uint16 handle )
- {
- CONST gattServiceCBs_t *pCBs = gattServApp_FindServiceCBs( handle );
- return ( ( pCBs == NULL ) ? NULL : pCBs->pfnReadAttrCB );
- }
- static pfnGATTWriteAttrCB_t gattServApp_FindWriteAttrCB( uint16 handle )
- {
- CONST gattServiceCBs_t *pCBs = gattServApp_FindServiceCBs( handle );
- return ( ( pCBs == NULL ) ? NULL : pCBs->pfnWriteAttrCB );
- }
- static pfnGATTAuthorizeAttrCB_t gattServApp_FindAuthorizeAttrCB( uint16 handle )
- {
- CONST gattServiceCBs_t *pCBs = gattServApp_FindServiceCBs( handle );
- return ( ( pCBs == NULL ) ? NULL : pCBs->pfnAuthorizeAttrCB );
- }
- static bStatus_t gattServApp_WriteAttrCB( uint16 connHandle, gattAttribute_t *pAttr,
- uint8 *pValue, uint8 len, uint16 offset )
- {
- bStatus_t status = SUCCESS;
- if ( pAttr->type.len == ATT_BT_UUID_SIZE )
- {
-
- 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_INDICATE );
-
- break;
- default:
-
-
- status = ATT_ERR_INVALID_HANDLE;
- }
- }
- else
- {
-
- status = ATT_ERR_INVALID_HANDLE;
- }
- return ( status );
- }
- uint8 GATTServApp_ReadAttr( uint16 connHandle, gattAttribute_t *pAttr,
- uint16 service, uint8 *pValue, uint8 *pLen,
- uint16 offset, uint8 maxLen )
- {
- uint8 useCB = FALSE;
- bStatus_t status = SUCCESS;
-
- if ( gattPermitAuthorRead( pAttr->permissions ) )
- {
-
- pfnGATTAuthorizeAttrCB_t pfnCB = gattServApp_FindAuthorizeAttrCB( service );
- if ( pfnCB != NULL )
- {
- status = (*pfnCB)( connHandle, pAttr, ATT_READ_REQ );
- }
- else
- {
- status = ATT_ERR_UNLIKELY;
- }
- if ( status != SUCCESS )
- {
-
- return ( status );
- }
- }
-
- if ( pAttr->type.len == ATT_BT_UUID_SIZE )
- {
-
- uint16 uuid = BUILD_UINT16( pAttr->type.uuid[0], pAttr->type.uuid[1]);
- switch ( uuid )
- {
- case GATT_PRIMARY_SERVICE_UUID:
- case GATT_SECONDARY_SERVICE_UUID:
-
- if ( offset == 0 )
- {
- gattAttrType_t *pType = (gattAttrType_t *)(pAttr->pValue);
- *pLen = pType->len;
- VOID osal_memcpy( pValue, pType->uuid, pType->len );
- }
- else
- {
- status = ATT_ERR_ATTR_NOT_LONG;
- }
- break;
- case GATT_CHARACTER_UUID:
-
- if ( offset == 0 )
- {
- gattAttribute_t *pCharValue;
-
-
-
- *pLen = 1;
- pValue[0] = *pAttr->pValue;
-
-
- pCharValue = GATT_FindHandle( pAttr->handle+1, NULL );
- if ( pCharValue != NULL )
- {
-
- *pLen += (2 + pCharValue->type.len);
-
- pValue[1] = LO_UINT16( pCharValue->handle );
- pValue[2] = HI_UINT16( pCharValue->handle );
-
- VOID osal_memcpy( &(pValue[3]), pCharValue->type.uuid, pCharValue->type.len );
- }
- else
- {
-
- *pLen += (2 + ATT_BT_UUID_SIZE);
-
- VOID osal_memset( &(pValue[1]), 0, (2 + ATT_BT_UUID_SIZE) );
- }
- }
- else
- {
- status = ATT_ERR_ATTR_NOT_LONG;
- }
- break;
- case GATT_INCLUDE_UUID:
-
- if ( offset == 0 )
- {
- uint16 servHandle;
- uint16 endGrpHandle;
- gattAttribute_t *pIncluded;
- uint16 handle = *((uint16 *)(pAttr->pValue));
-
-
-
-
- *pLen = 4;
- pValue[0] = LO_UINT16( handle );
- pValue[1] = HI_UINT16( handle );
-
- pIncluded = GATT_FindHandle( handle, &servHandle );
- if ( pIncluded != NULL )
- {
- gattAttrType_t *pServiceUUID = (gattAttrType_t *)pIncluded->pValue;
-
- if ( ( GATT_FindNextAttr( pIncluded, GATT_MAX_HANDLE,
- servHandle, &endGrpHandle ) == NULL ) &&
- ( !gattSecondaryServiceType( pIncluded->type ) ) )
- {
-
- endGrpHandle = GATT_MAX_HANDLE;
- }
-
- if ( pServiceUUID->len == ATT_BT_UUID_SIZE )
- {
- VOID osal_memcpy( &(pValue[4]), pServiceUUID->uuid, ATT_BT_UUID_SIZE );
- *pLen += ATT_BT_UUID_SIZE;
- }
- }
- else
- {
-
- endGrpHandle = handle;
- }
-
- pValue[2] = LO_UINT16( endGrpHandle );
- pValue[3] = HI_UINT16( endGrpHandle );
- }
- else
- {
- status = ATT_ERR_ATTR_NOT_LONG;
- }
- break;
- case GATT_CLIENT_CHAR_CFG_UUID:
-
- if ( offset == 0 )
- {
- uint16 value = GATTServApp_ReadCharCfg( connHandle,
- (gattCharCfg_t *)(pAttr->pValue) );
- *pLen = 2;
- pValue[0] = LO_UINT16( value );
- pValue[1] = HI_UINT16( value );
- }
- else
- {
- status = ATT_ERR_ATTR_NOT_LONG;
- }
- break;
- case GATT_CHAR_EXT_PROPS_UUID:
- case GATT_SERV_CHAR_CFG_UUID:
-
- if ( offset == 0 )
- {
- uint16 value = *((uint16 *)(pAttr->pValue));
- *pLen = 2;
- pValue[0] = LO_UINT16( value );
- pValue[1] = HI_UINT16( value );
- }
- else
- {
- status = ATT_ERR_ATTR_NOT_LONG;
- }
- break;
- case GATT_CHAR_USER_DESC_UUID:
- {
- uint8 len = osal_strlen( (char *)(pAttr->pValue) );
-
-
-
- if ( offset <= len )
- {
-
-
- if ( offset == len )
- {
- len = 0;
- }
- else
- {
-
-
-
- if ( len > ( offset + maxLen ) )
- {
- len = maxLen;
- }
- else
- {
- len -= offset;
- }
- }
- *pLen = len;
- VOID osal_memcpy( pValue, &(pAttr->pValue[offset]), len );
- }
- else
- {
- status = ATT_ERR_INVALID_OFFSET;
- }
- }
- break;
- case GATT_CHAR_FORMAT_UUID:
-
- if ( offset == 0 )
- {
- gattCharFormat_t *pFormat = (gattCharFormat_t *)(pAttr->pValue);
- *pLen = 7;
- pValue[0] = pFormat->format;
- pValue[1] = pFormat->exponent;
- pValue[2] = LO_UINT16( pFormat->unit );
- pValue[3] = HI_UINT16( pFormat->unit );
- pValue[4] = pFormat->nameSpace;
- pValue[5] = LO_UINT16( pFormat->desc );
- pValue[6] = HI_UINT16( pFormat->desc );
- }
- else
- {
- status = ATT_ERR_ATTR_NOT_LONG;
- }
- break;
- default:
- useCB = TRUE;
- break;
- }
- }
- else
- {
- useCB = TRUE;
- }
- if ( useCB == TRUE )
- {
-
- pfnGATTReadAttrCB_t pfnCB = gattServApp_FindReadAttrCB( service );
- if ( pfnCB != NULL )
- {
-
- status = (*pfnCB)( connHandle, pAttr, pValue, pLen, offset, maxLen );
- }
- else
- {
- status = ATT_ERR_UNLIKELY;
- }
- }
- return ( status );
- }
- uint8 GATTServApp_WriteAttr( uint16 connHandle, uint16 handle,
- uint8 *pValue, uint16 len, uint16 offset )
- {
- uint16 service;
- gattAttribute_t *pAttr;
- bStatus_t status;
-
- pAttr = GATT_FindHandle( handle, &service );
- if ( pAttr != NULL )
- {
-
- pfnGATTWriteAttrCB_t pfnCB = gattServApp_FindWriteAttrCB( service );
- if ( pfnCB != NULL )
- {
-
- status = (*pfnCB)( connHandle, pAttr, pValue, len, offset );
- }
- else
- {
- status = ATT_ERR_UNLIKELY;
- }
- }
- else
- {
- status = ATT_ERR_INVALID_HANDLE;
- }
- return ( status );
- }
- void GATTServApp_SetParamValue( uint16 value )
- {
- #if defined ( TESTMODES )
- paramValue = value;
- #else
- VOID value;
- #endif
- }
- uint16 GATTServApp_GetParamValue( void )
- {
- #if defined ( TESTMODES )
- return ( paramValue );
- #else
- return ( 0 );
- #endif
- }
- bStatus_t GATTServApp_UpdateCharCfg( uint16 connHandle, uint16 attrHandle, uint16 value )
- {
- uint8 buf[2];
- buf[0] = LO_UINT16( value );
- buf[1] = HI_UINT16( value );
- return ( GATTServApp_WriteAttr( connHandle, attrHandle, buf, 2, 0 ) );
- }
- bStatus_t GATTServApp_SendServiceChangedInd( uint16 connHandle, uint8 taskId )
- {
- uint16 value = GATTServApp_ReadCharCfg( connHandle, indCharCfg );
- if ( value & GATT_CLIENT_CFG_INDICATE )
- {
- return ( GATT_ServiceChangedInd( connHandle, taskId ) );
- }
- return ( FAILURE );
- }
- void GATTServApp_InitCharCfg( uint16 connHandle, gattCharCfg_t *charCfgTbl )
- {
-
- if ( connHandle == INVALID_CONNHANDLE )
- {
- for ( uint8 i = 0; i < GATT_MAX_NUM_CONN; i++ )
- {
- charCfgTbl[i].connHandle = INVALID_CONNHANDLE;
- charCfgTbl[i].value = GATT_CFG_NO_OPERATION;
- }
- }
- else
- {
- gattCharCfg_t *pItem = gattServApp_FindCharCfgItem( connHandle, charCfgTbl );
- if ( pItem != NULL )
- {
- pItem->connHandle = INVALID_CONNHANDLE;
- pItem->value = GATT_CFG_NO_OPERATION;
- }
- }
- }
- uint16 GATTServApp_ReadCharCfg( uint16 connHandle, gattCharCfg_t *charCfgTbl )
- {
- gattCharCfg_t *pItem;
- pItem = gattServApp_FindCharCfgItem( connHandle, charCfgTbl );
- if ( pItem != NULL )
- {
- return ( (uint16)(pItem->value) );
- }
- return ( (uint16)GATT_CFG_NO_OPERATION );
- }
- uint8 GATTServApp_WriteCharCfg( uint16 connHandle, gattCharCfg_t *charCfgTbl,
- uint16 value )
- {
- gattCharCfg_t *pItem;
- pItem = gattServApp_FindCharCfgItem( connHandle, charCfgTbl );
- if ( pItem == NULL )
- {
- pItem = gattServApp_FindCharCfgItem( INVALID_CONNHANDLE, charCfgTbl );
- if ( pItem == NULL )
- {
- return ( ATT_ERR_INSUFFICIENT_RESOURCES );
- }
- pItem->connHandle = connHandle;
- }
-
- pItem->value = value;
- return ( SUCCESS );
- }
- bStatus_t GATTServApp_ProcessCCCWriteReq( uint16 connHandle, gattAttribute_t *pAttr,
- uint8 *pValue, uint8 len, uint16 offset,
- uint16 validCfg )
- {
- bStatus_t status = SUCCESS;
-
- if ( offset == 0 )
- {
- if ( len == 2 )
- {
- uint16 value = BUILD_UINT16( pValue[0], pValue[1] );
-
- if ( ( value & ~validCfg ) == 0 )
- {
-
- if ( GATTServApp_ReadCharCfg( connHandle,
- (gattCharCfg_t *)(pAttr->pValue) ) != value )
- {
- status = GATTServApp_WriteCharCfg( connHandle,
- (gattCharCfg_t *)(pAttr->pValue),
- value );
- if ( status == SUCCESS )
- {
-
- GATTServApp_SendCCCUpdatedEvent( connHandle, pAttr->handle, value );
- }
- }
- }
- else
- {
- status = ATT_ERR_INVALID_VALUE;
- }
- }
- else
- {
- status = ATT_ERR_INVALID_VALUE_SIZE;
- }
- }
- else
- {
- status = ATT_ERR_ATTR_NOT_LONG;
- }
- return ( status );
- }
- bStatus_t GATTServApp_ProcessCharCfg( gattCharCfg_t *charCfgTbl, uint8 *pValue,
- uint8 authenticated, gattAttribute_t *attrTbl,
- uint16 numAttrs, uint8 taskId )
- {
- bStatus_t status = SUCCESS;
- for ( uint8 i = 0; i < GATT_MAX_NUM_CONN; i++ )
- {
- gattCharCfg_t *pItem = &(charCfgTbl[i]);
- if ( ( pItem->connHandle != INVALID_CONNHANDLE ) &&
- ( pItem->value != GATT_CFG_NO_OPERATION ) )
- {
- gattAttribute_t *pAttr;
-
- pAttr = GATTServApp_FindAttr( attrTbl, numAttrs, pValue );
- if ( pAttr != NULL )
- {
- attHandleValueNoti_t noti;
-
-
-
- if ( GATTServApp_ReadAttr( pItem->connHandle, pAttr,
- GATT_SERVICE_HANDLE( attrTbl ), noti.value,
- ¬i.len, 0, (g_ATT_MTU_SIZE-3) ) == SUCCESS )
- {
- noti.handle = pAttr->handle;
- if ( pItem->value & GATT_CLIENT_CFG_NOTIFY )
- {
- status |= GATT_Notification( pItem->connHandle, ¬i, authenticated );
- }
- if ( pItem->value & GATT_CLIENT_CFG_INDICATE )
- {
- status |= GATT_Indication( pItem->connHandle, (attHandleValueInd_t *)¬i,
- authenticated, taskId );
- }
- }
- }
- }
- }
- return ( status );
- }
- static void gattServApp_HandleConnStatusCB( uint16 connHandle, uint8 changeType )
- {
-
- if ( ( changeType == LINKDB_STATUS_UPDATE_REMOVED ) ||
- ( ( changeType == LINKDB_STATUS_UPDATE_STATEFLAGS ) &&
- ( !linkDB_Up( connHandle ) ) ) )
- {
- prepareWrites_t *pQueue = gattServApp_FindPrepareWriteQ( connHandle );
-
- if ( pQueue != NULL )
- {
- for ( uint8 i = 0; i < maxNumPrepareWrites; i++ )
- {
- attPrepareWriteReq_t *pWriteReq = &(pQueue->pPrepareWriteQ[i]);
-
- if ( pWriteReq->handle == GATT_INVALID_HANDLE )
- {
- break;
- }
-
- VOID osal_memset( pWriteReq, 0, sizeof( attPrepareWriteRsp_t ) );
- }
-
- pQueue->connHandle = INVALID_CONNHANDLE;
- }
-
- GATTServApp_InitCharCfg( connHandle, indCharCfg );
- }
- }
- void GATTServApp_SendCCCUpdatedEvent( uint16 connHandle, uint16 attrHandle, uint16 value )
- {
- if ( appTaskID != INVALID_TASK_ID )
- {
-
- gattClientCharCfgUpdatedEvent_t *pEvent =
- (gattClientCharCfgUpdatedEvent_t *)osal_msg_allocate( (uint16)(sizeof ( gattClientCharCfgUpdatedEvent_t )) );
- if ( pEvent )
- {
- pEvent->hdr.event = GATT_SERV_MSG_EVENT;
- pEvent->hdr.status = SUCCESS;
- pEvent->method = GATT_CLIENT_CHAR_CFG_UPDATED_EVENT;
- pEvent->connHandle = connHandle;
- pEvent->attrHandle = attrHandle;
- pEvent->value = value;
- VOID osal_msg_send( appTaskID, (uint8 *)pEvent );
- }
- }
- }
- bStatus_t gattServApp_RegisterCB(gattServMsgCB_t cb)
- {
- s_GATTServCB = cb;
- return SUCCESS;
- }
- #endif
|