gatt_utils.c 92 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913
  1. /******************************************************************************
  2. *
  3. * Copyright (C) 2009-2012 Broadcom Corporation
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at:
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. ******************************************************************************/
  18. /******************************************************************************
  19. *
  20. * this file contains GATT utility functions
  21. *
  22. ******************************************************************************/
  23. #include "common/bt_target.h"
  24. #include "osi/allocator.h"
  25. #if BLE_INCLUDED == TRUE
  26. #include <string.h>
  27. #include <stdio.h>
  28. #include "stack/l2cdefs.h"
  29. #include "gatt_int.h"
  30. #include "stack/gatt_api.h"
  31. #include "stack/gattdefs.h"
  32. #include "stack/sdp_api.h"
  33. #include "btm_int.h"
  34. /* check if [x, y] and [a, b] have overlapping range */
  35. #define GATT_VALIDATE_HANDLE_RANGE(x, y, a, b) (y >= a && x <= b)
  36. #define GATT_GET_NEXT_VALID_HANDLE(x) (((x)/10 + 1) * 10)
  37. const char *const op_code_name[] = {
  38. "UNKNOWN",
  39. "ATT_RSP_ERROR",
  40. "ATT_REQ_MTU",
  41. "ATT_RSP_MTU",
  42. "ATT_REQ_READ_INFO",
  43. "ATT_RSP_READ_INFO",
  44. "ATT_REQ_FIND_TYPE_VALUE",
  45. "ATT_RSP_FIND_TYPE_VALUE",
  46. "ATT_REQ_READ_BY_TYPE",
  47. "ATT_RSP_READ_BY_TYPE",
  48. "ATT_REQ_READ",
  49. "ATT_RSP_READ",
  50. "ATT_REQ_READ_BLOB",
  51. "ATT_RSP_READ_BLOB",
  52. "GATT_REQ_READ_MULTI",
  53. "GATT_RSP_READ_MULTI",
  54. "GATT_REQ_READ_BY_GRP_TYPE",
  55. "GATT_RSP_READ_BY_GRP_TYPE",
  56. "ATT_REQ_WRITE",
  57. "ATT_RSP_WRITE",
  58. "ATT_CMD_WRITE",
  59. "ATT_SIGN_CMD_WRITE",
  60. "ATT_REQ_PREPARE_WRITE",
  61. "ATT_RSP_PREPARE_WRITE",
  62. "ATT_REQ_EXEC_WRITE",
  63. "ATT_RSP_EXEC_WRITE",
  64. "Reserved",
  65. "ATT_HANDLE_VALUE_NOTIF",
  66. "Reserved",
  67. "ATT_HANDLE_VALUE_IND",
  68. "ATT_HANDLE_VALUE_CONF",
  69. "ATT_OP_CODE_MAX"
  70. };
  71. static const UINT8 base_uuid[LEN_UUID_128] = {0xFB, 0x34, 0x9B, 0x5F, 0x80, 0x00, 0x00, 0x80,
  72. 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  73. };
  74. static UINT32 gatt_tcb_id;
  75. /*******************************************************************************
  76. **
  77. ** Function gatt_free_pending_ind
  78. **
  79. ** Description Free all pending indications
  80. **
  81. ** Returns None
  82. **
  83. *******************************************************************************/
  84. void gatt_free_pending_ind(tGATT_TCB *p_tcb)
  85. {
  86. GATT_TRACE_DEBUG("gatt_free_pending_ind");
  87. if (p_tcb->pending_ind_q == NULL) {
  88. return;
  89. }
  90. /* release all queued indications */
  91. while (!fixed_queue_is_empty(p_tcb->pending_ind_q)) {
  92. osi_free(fixed_queue_dequeue(p_tcb->pending_ind_q, 0));
  93. }
  94. fixed_queue_free(p_tcb->pending_ind_q, NULL);
  95. p_tcb->pending_ind_q = NULL;
  96. }
  97. /*******************************************************************************
  98. **
  99. ** Function gatt_free_pending_enc_queue
  100. **
  101. ** Description Free all buffers in pending encyption queue
  102. **
  103. ** Returns None
  104. **
  105. *******************************************************************************/
  106. void gatt_free_pending_enc_queue(tGATT_TCB *p_tcb)
  107. {
  108. GATT_TRACE_DEBUG("gatt_free_pending_enc_queue");
  109. if (p_tcb->pending_enc_clcb == NULL) {
  110. return;
  111. }
  112. /* release all queued indications */
  113. while (!fixed_queue_is_empty(p_tcb->pending_enc_clcb)) {
  114. osi_free(fixed_queue_dequeue(p_tcb->pending_enc_clcb, 0));
  115. }
  116. fixed_queue_free(p_tcb->pending_enc_clcb, NULL);
  117. p_tcb->pending_enc_clcb = NULL;
  118. }
  119. /*******************************************************************************
  120. **
  121. ** Function gatt_free_pending_prepare_write_queue
  122. **
  123. ** Description Free all buffers in pending prepare write packets queue
  124. **
  125. ** Returns None
  126. **
  127. *******************************************************************************/
  128. void gatt_free_pending_prepare_write_queue(tGATT_TCB *p_tcb)
  129. {
  130. GATT_TRACE_DEBUG("gatt_free_pending_prepare_write_queue");
  131. if (p_tcb->prepare_write_record.queue) {
  132. /* release all queued prepare write packets */
  133. while (!fixed_queue_is_empty(p_tcb->prepare_write_record.queue)) {
  134. osi_free(fixed_queue_dequeue(p_tcb->prepare_write_record.queue, FIXED_QUEUE_MAX_TIMEOUT));
  135. }
  136. fixed_queue_free(p_tcb->prepare_write_record.queue, NULL);
  137. p_tcb->prepare_write_record.queue = NULL;
  138. }
  139. p_tcb->prepare_write_record.total_num = 0;
  140. p_tcb->prepare_write_record.error_code_app = GATT_SUCCESS;
  141. }
  142. /*******************************************************************************
  143. **
  144. ** Function gatt_delete_dev_from_srv_chg_clt_list
  145. **
  146. ** Description Delete a device from the service changed client lit
  147. **
  148. ** Returns None
  149. **
  150. *******************************************************************************/
  151. void gatt_delete_dev_from_srv_chg_clt_list(BD_ADDR bd_addr)
  152. {
  153. tGATTS_SRV_CHG *p_buf;
  154. tGATTS_SRV_CHG_REQ req;
  155. GATT_TRACE_DEBUG ("gatt_delete_dev_from_srv_chg_clt_list");
  156. if ((p_buf = gatt_is_bda_in_the_srv_chg_clt_list(bd_addr)) != NULL) {
  157. if (gatt_cb.cb_info.p_srv_chg_callback) {
  158. /* delete from NV */
  159. memcpy(req.srv_chg.bda, bd_addr, BD_ADDR_LEN);
  160. (*gatt_cb.cb_info.p_srv_chg_callback)(GATTS_SRV_CHG_CMD_REMOVE_CLIENT, &req, NULL);
  161. }
  162. osi_free(fixed_queue_try_remove_from_queue(gatt_cb.srv_chg_clt_q,
  163. p_buf));
  164. }
  165. }
  166. /*******************************************************************************
  167. **
  168. ** Function gatt_set_srv_chg
  169. **
  170. ** Description Set the service changed flag to TRUE
  171. **
  172. ** Returns None
  173. **
  174. *******************************************************************************/
  175. void gatt_set_srv_chg(void)
  176. {
  177. GATT_TRACE_DEBUG ("gatt_set_srv_chg");
  178. if (fixed_queue_is_empty(gatt_cb.srv_chg_clt_q)) {
  179. return;
  180. }
  181. list_t *list = fixed_queue_get_list(gatt_cb.srv_chg_clt_q);
  182. for (const list_node_t *node = list_begin(list); node != list_end(list);
  183. node = list_next(node)) {
  184. GATT_TRACE_DEBUG ("found a srv_chg clt");
  185. tGATTS_SRV_CHG *p_buf = (tGATTS_SRV_CHG *)list_node(node);
  186. if (!p_buf->srv_changed) {
  187. GATT_TRACE_DEBUG("set srv_changed to TRUE");
  188. p_buf->srv_changed = TRUE;
  189. tGATTS_SRV_CHG_REQ req;
  190. memcpy(&req.srv_chg, p_buf, sizeof(tGATTS_SRV_CHG));
  191. if (gatt_cb.cb_info.p_srv_chg_callback) {
  192. (*gatt_cb.cb_info.p_srv_chg_callback)(GATTS_SRV_CHG_CMD_UPDATE_CLIENT,&req, NULL);
  193. }
  194. }
  195. }
  196. }
  197. /*******************************************************************************
  198. **
  199. ** Function gatt_sr_is_new_srv_chg
  200. **
  201. ** Description Find the app id in on the new service changed list
  202. **
  203. ** Returns Pointer to the found new service changed item othwerwise NULL
  204. **
  205. *******************************************************************************/
  206. tGATTS_PENDING_NEW_SRV_START *gatt_sr_is_new_srv_chg(tBT_UUID *p_app_uuid128, tBT_UUID *p_svc_uuid, UINT16 svc_inst)
  207. {
  208. tGATTS_PENDING_NEW_SRV_START *p_buf = NULL;
  209. if (fixed_queue_is_empty(gatt_cb.pending_new_srv_start_q)) {
  210. return NULL;
  211. }
  212. list_t *list = fixed_queue_get_list(gatt_cb.pending_new_srv_start_q);
  213. for (const list_node_t *node = list_begin(list); node != list_end(list);
  214. node = list_next(node)) {
  215. p_buf = (tGATTS_PENDING_NEW_SRV_START *)list_node(node);
  216. tGATTS_HNDL_RANGE *p = p_buf->p_new_srv_start;
  217. if (gatt_uuid_compare(*p_app_uuid128, p->app_uuid128)
  218. && gatt_uuid_compare (*p_svc_uuid, p->svc_uuid)
  219. && (svc_inst == p->svc_inst)) {
  220. GATT_TRACE_DEBUG("gatt_sr_is_new_srv_chg: Yes");
  221. break;
  222. }
  223. }
  224. return p_buf;
  225. }
  226. /*******************************************************************************
  227. **
  228. ** Function gatt_add_pending_ind
  229. **
  230. ** Description Add a pending indication
  231. **
  232. ** Returns Pointer to the current pending indication buffer, NULL no buffer available
  233. **
  234. *******************************************************************************/
  235. tGATT_VALUE *gatt_add_pending_ind(tGATT_TCB *p_tcb, tGATT_VALUE *p_ind)
  236. {
  237. tGATT_VALUE *p_buf;
  238. GATT_TRACE_DEBUG ("gatt_add_pending_ind");
  239. if ((p_buf = (tGATT_VALUE *)osi_malloc((UINT16)sizeof(tGATT_VALUE))) != NULL) {
  240. GATT_TRACE_DEBUG ("enqueue a pending indication");
  241. memcpy(p_buf, p_ind, sizeof(tGATT_VALUE));
  242. fixed_queue_enqueue(p_tcb->pending_ind_q, p_buf, FIXED_QUEUE_MAX_TIMEOUT);
  243. }
  244. return p_buf;
  245. }
  246. /*******************************************************************************
  247. **
  248. ** Function gatt_add_pending_new_srv_start
  249. **
  250. ** Description Add a pending new srv start to the new service start queue
  251. **
  252. ** Returns Pointer to the new service start buffer, NULL no buffer available
  253. **
  254. *******************************************************************************/
  255. tGATTS_PENDING_NEW_SRV_START *gatt_add_pending_new_srv_start(tGATTS_HNDL_RANGE *p_new_srv_start)
  256. {
  257. tGATTS_PENDING_NEW_SRV_START *p_buf;
  258. GATT_TRACE_DEBUG ("gatt_add_pending_new_srv_start");
  259. if ((p_buf = (tGATTS_PENDING_NEW_SRV_START *)osi_malloc((UINT16)sizeof(tGATTS_PENDING_NEW_SRV_START))) != NULL) {
  260. GATT_TRACE_DEBUG ("enqueue a new pending new srv start");
  261. p_buf->p_new_srv_start = p_new_srv_start;
  262. fixed_queue_enqueue(gatt_cb.pending_new_srv_start_q, p_buf, FIXED_QUEUE_MAX_TIMEOUT);
  263. }
  264. return p_buf;
  265. }
  266. /*******************************************************************************
  267. **
  268. ** Function gatt_add_srv_chg_clt
  269. **
  270. ** Description Add a service chnage client to the service change client queue
  271. **
  272. ** Returns Pointer to the service change client buffer; Null no buffer available
  273. **
  274. *******************************************************************************/
  275. tGATTS_SRV_CHG *gatt_add_srv_chg_clt(tGATTS_SRV_CHG *p_srv_chg)
  276. {
  277. tGATTS_SRV_CHG *p_buf;
  278. GATT_TRACE_DEBUG ("gatt_add_srv_chg_clt");
  279. if ((p_buf = (tGATTS_SRV_CHG *)osi_malloc((UINT16)sizeof(tGATTS_SRV_CHG))) != NULL) {
  280. GATT_TRACE_DEBUG ("enqueue a srv chg client");
  281. memcpy(p_buf, p_srv_chg, sizeof(tGATTS_SRV_CHG));
  282. fixed_queue_enqueue(gatt_cb.srv_chg_clt_q, p_buf, FIXED_QUEUE_MAX_TIMEOUT);
  283. }
  284. return p_buf;
  285. }
  286. /*******************************************************************************
  287. **
  288. ** Function gatt_alloc_hdl_buffer
  289. **
  290. ** Description Allocate a handle buufer
  291. **
  292. ** Returns Pointer to the allocated buffer, NULL no buffer available
  293. **
  294. *******************************************************************************/
  295. #if (GATTS_INCLUDED == TRUE)
  296. tGATT_HDL_LIST_ELEM *gatt_alloc_hdl_buffer(void)
  297. {
  298. UINT8 i;
  299. tGATT_CB *p_cb = &gatt_cb;
  300. tGATT_HDL_LIST_ELEM *p_elem = &p_cb->hdl_list[0];
  301. for (i = 0; i < GATT_MAX_SR_PROFILES; i++, p_elem ++) {
  302. if (!p_cb->hdl_list[i].in_use) {
  303. memset(p_elem, 0, sizeof(tGATT_HDL_LIST_ELEM));
  304. p_elem->in_use = TRUE;
  305. p_elem->svc_db.svc_buffer = fixed_queue_new(QUEUE_SIZE_MAX);
  306. return p_elem;
  307. }
  308. }
  309. return NULL;
  310. }
  311. /*******************************************************************************
  312. **
  313. ** Function gatt_find_hdl_buffer_by_handle
  314. **
  315. ** Description Find handle range buffer by service handle.
  316. **
  317. ** Returns Pointer to the buffer, NULL no buffer available
  318. **
  319. *******************************************************************************/
  320. tGATT_HDL_LIST_ELEM *gatt_find_hdl_buffer_by_handle(UINT16 handle)
  321. {
  322. tGATT_HDL_LIST_INFO *p_list_info = &gatt_cb.hdl_list_info;
  323. tGATT_HDL_LIST_ELEM *p_list = NULL;
  324. p_list = p_list_info->p_first;
  325. while (p_list != NULL) {
  326. if (p_list->in_use && p_list->asgn_range.s_handle == handle) {
  327. return (p_list);
  328. }
  329. p_list = p_list->p_next;
  330. }
  331. return NULL;
  332. }
  333. /*******************************************************************************
  334. **
  335. ** Function gatt_find_hdl_buffer_by_attr_handle
  336. **
  337. ** Description Find handle range buffer by attribute handle.
  338. **
  339. ** Returns Pointer to the buffer, NULL no buffer available
  340. **
  341. *******************************************************************************/
  342. tGATT_HDL_LIST_ELEM *gatt_find_hdl_buffer_by_attr_handle(UINT16 attr_handle)
  343. {
  344. tGATT_HDL_LIST_INFO *p_list_info = &gatt_cb.hdl_list_info;
  345. tGATT_HDL_LIST_ELEM *p_list = NULL;
  346. p_list = p_list_info->p_first;
  347. while (p_list != NULL) {
  348. if (p_list->in_use && (p_list->asgn_range.s_handle <= attr_handle)
  349. && (p_list->asgn_range.e_handle >= attr_handle)) {
  350. return (p_list);
  351. }
  352. p_list = p_list->p_next;
  353. }
  354. return NULL;
  355. }
  356. /*******************************************************************************
  357. **
  358. ** Function gatt_find_hdl_buffer_by_app_id
  359. **
  360. ** Description Find handle range buffer by app ID, service and service instance ID.
  361. **
  362. ** Returns Pointer to the buffer, NULL no buffer available
  363. **
  364. *******************************************************************************/
  365. tGATT_HDL_LIST_ELEM *gatt_find_hdl_buffer_by_app_id (tBT_UUID *p_app_uuid128,
  366. tBT_UUID *p_svc_uuid,
  367. UINT16 svc_inst)
  368. {
  369. tGATT_HDL_LIST_INFO *p_list_info = &gatt_cb.hdl_list_info;
  370. tGATT_HDL_LIST_ELEM *p_list = NULL;
  371. p_list = p_list_info->p_first;
  372. while (p_list != NULL) {
  373. if ( gatt_uuid_compare (*p_app_uuid128, p_list->asgn_range.app_uuid128)
  374. && gatt_uuid_compare (*p_svc_uuid, p_list->asgn_range.svc_uuid)
  375. && (svc_inst == p_list->asgn_range.svc_inst) ) {
  376. GATT_TRACE_DEBUG ("Already allocated handles for this service before!!");
  377. return (p_list);
  378. }
  379. p_list = p_list->p_next;
  380. }
  381. return NULL;
  382. }
  383. #endif ///GATTS_INCLUDED == TRUE
  384. /*******************************************************************************
  385. **
  386. ** Function gatt_free_attr_value_buffer
  387. **
  388. ** Description free characteristic attribute value buffer in a service
  389. **
  390. ** Returns None
  391. **
  392. *******************************************************************************/
  393. void gatt_free_attr_value_buffer(tGATT_HDL_LIST_ELEM *p)
  394. {
  395. if (p){
  396. tGATT_SVC_DB *p_db = &(p->svc_db);
  397. tGATT_ATTR16 *p_attr = p_db->p_attr_list;
  398. tGATT_ATTR_VALUE *p_value = NULL;
  399. while(p_attr){
  400. if (p_attr->mask & GATT_ATTR_VALUE_ALLOCATED){
  401. p_value = p_attr->p_value;
  402. if ((p_value != NULL) && (p_value->attr_val.attr_val != NULL)){
  403. osi_free(p_value->attr_val.attr_val);
  404. }
  405. }
  406. p_attr = p_attr->p_next;
  407. }
  408. }
  409. }
  410. /*******************************************************************************
  411. **
  412. ** Function gatt_free_hdl_buffer
  413. **
  414. ** Description free a handle buffer
  415. **
  416. ** Returns None
  417. **
  418. *******************************************************************************/
  419. void gatt_free_hdl_buffer(tGATT_HDL_LIST_ELEM *p)
  420. {
  421. if (p) {
  422. while (!fixed_queue_is_empty(p->svc_db.svc_buffer)) {
  423. osi_free(fixed_queue_dequeue(p->svc_db.svc_buffer, 0));
  424. }
  425. fixed_queue_free(p->svc_db.svc_buffer, NULL);
  426. memset(p, 0, sizeof(tGATT_HDL_LIST_ELEM));
  427. }
  428. }
  429. /*******************************************************************************
  430. **
  431. ** Function gatt_free_srvc_db_buffer_app_id
  432. **
  433. ** Description free the service attribute database buffers by the owner of the
  434. ** service app ID.
  435. **
  436. ** Returns None
  437. **
  438. *******************************************************************************/
  439. #if (GATTS_INCLUDED == TRUE)
  440. void gatt_free_srvc_db_buffer_app_id(tBT_UUID *p_app_id)
  441. {
  442. tGATT_HDL_LIST_ELEM *p_elem = &gatt_cb.hdl_list[0];
  443. UINT8 i;
  444. for (i = 0; i < GATT_MAX_SR_PROFILES; i ++, p_elem ++) {
  445. if (memcmp(p_app_id, &p_elem->asgn_range.app_uuid128, sizeof(tBT_UUID)) == 0) {
  446. gatt_free_attr_value_buffer(p_elem);
  447. while (!fixed_queue_is_empty(p_elem->svc_db.svc_buffer)) {
  448. osi_free(fixed_queue_dequeue(p_elem->svc_db.svc_buffer, 0));
  449. }
  450. fixed_queue_free(p_elem->svc_db.svc_buffer, NULL);
  451. p_elem->svc_db.svc_buffer = NULL;
  452. p_elem->svc_db.mem_free = 0;
  453. p_elem->svc_db.p_attr_list = p_elem->svc_db.p_free_mem = NULL;
  454. }
  455. }
  456. }
  457. /*******************************************************************************
  458. **
  459. ** Function gatt_is_last_attribute
  460. **
  461. ** Description Check this is the last attribute of the specified value or not
  462. **
  463. ** Returns TRUE - yes this is the last attribute
  464. **
  465. *******************************************************************************/
  466. BOOLEAN gatt_is_last_attribute(tGATT_SRV_LIST_INFO *p_list, tGATT_SRV_LIST_ELEM *p_start, tBT_UUID value)
  467. {
  468. tGATT_SRV_LIST_ELEM *p_srv = p_start->p_next;
  469. BOOLEAN is_last_attribute = TRUE;
  470. tGATT_SR_REG *p_rcb = NULL;
  471. tBT_UUID *p_svc_uuid;
  472. p_list->p_last_primary = NULL;
  473. while (p_srv) {
  474. p_rcb = GATT_GET_SR_REG_PTR(p_srv->i_sreg);
  475. p_svc_uuid = gatts_get_service_uuid (p_rcb->p_db);
  476. if (gatt_uuid_compare(value, *p_svc_uuid)) {
  477. is_last_attribute = FALSE;
  478. break;
  479. }
  480. p_srv = p_srv->p_next;
  481. }
  482. return is_last_attribute;
  483. }
  484. /*******************************************************************************
  485. **
  486. ** Function gatt_update_last_pri_srv_info
  487. **
  488. ** Description Update the the last primary info for the service list info
  489. **
  490. ** Returns None
  491. **
  492. *******************************************************************************/
  493. void gatt_update_last_pri_srv_info(tGATT_SRV_LIST_INFO *p_list)
  494. {
  495. tGATT_SRV_LIST_ELEM *p_srv = p_list->p_first;
  496. p_list->p_last_primary = NULL;
  497. while (p_srv) {
  498. if (p_srv->is_primary) {
  499. p_list->p_last_primary = p_srv;
  500. }
  501. p_srv = p_srv->p_next;
  502. }
  503. }
  504. /*******************************************************************************
  505. **
  506. ** Function gatts_update_srv_list_elem
  507. **
  508. ** Description update an element in the service list.
  509. **
  510. ** Returns None.
  511. **
  512. *******************************************************************************/
  513. void gatts_update_srv_list_elem(UINT8 i_sreg, UINT16 handle, BOOLEAN is_primary)
  514. {
  515. UNUSED(handle);
  516. gatt_cb.srv_list[i_sreg].in_use = TRUE;
  517. gatt_cb.srv_list[i_sreg].i_sreg = i_sreg;
  518. gatt_cb.srv_list[i_sreg].s_hdl = gatt_cb.sr_reg[i_sreg].s_hdl;
  519. gatt_cb.srv_list[i_sreg].is_primary = is_primary;
  520. return;
  521. }
  522. #endif ///GATTS_INCLUDED == TRUE
  523. /*******************************************************************************
  524. **
  525. ** Function gatt_add_a_srv_to_list
  526. **
  527. ** Description add an service to the list in ascending
  528. ** order of the start handle
  529. **
  530. ** Returns BOOLEAN TRUE-if add is successful
  531. **
  532. *******************************************************************************/
  533. BOOLEAN gatt_add_a_srv_to_list(tGATT_SRV_LIST_INFO *p_list, tGATT_SRV_LIST_ELEM *p_new)
  534. {
  535. tGATT_SRV_LIST_ELEM *p_old;
  536. if (!p_new) {
  537. GATT_TRACE_DEBUG("p_new==NULL");
  538. return FALSE;
  539. }
  540. if (!p_list->p_first) {
  541. /* this is an empty list */
  542. p_list->p_first =
  543. p_list->p_last = p_new;
  544. p_new->p_next =
  545. p_new->p_prev = NULL;
  546. } else {
  547. p_old = p_list->p_first;
  548. while (1) {
  549. if (p_old == NULL) {
  550. p_list->p_last->p_next = p_new;
  551. p_new->p_prev = p_list->p_last;
  552. p_new->p_next = NULL;
  553. p_list->p_last = p_new;
  554. break;
  555. } else {
  556. if (p_new->s_hdl < p_old->s_hdl) {
  557. /* if not the first in list */
  558. if (p_old->p_prev != NULL) {
  559. p_old->p_prev->p_next = p_new;
  560. } else {
  561. p_list->p_first = p_new;
  562. }
  563. p_new->p_prev = p_old->p_prev;
  564. p_new->p_next = p_old;
  565. p_old->p_prev = p_new;
  566. break;
  567. }
  568. }
  569. p_old = p_old->p_next;
  570. }
  571. }
  572. p_list->count++;
  573. gatt_update_last_pri_srv_info(p_list);
  574. return TRUE;
  575. }
  576. /*******************************************************************************
  577. **
  578. ** Function gatt_remove_a_srv_from_list
  579. **
  580. ** Description Remove a service from the list
  581. **
  582. ** Returns BOOLEAN TRUE-if remove is successful
  583. **
  584. *******************************************************************************/
  585. BOOLEAN gatt_remove_a_srv_from_list(tGATT_SRV_LIST_INFO *p_list, tGATT_SRV_LIST_ELEM *p_remove)
  586. {
  587. if (!p_remove || !p_list->p_first) {
  588. GATT_TRACE_DEBUG("p_remove==NULL || p_list->p_first==NULL");
  589. return FALSE;
  590. }
  591. if (p_remove->p_prev == NULL) {
  592. p_list->p_first = p_remove->p_next;
  593. if (p_remove->p_next) {
  594. p_remove->p_next->p_prev = NULL;
  595. }
  596. } else if (p_remove->p_next == NULL) {
  597. p_list->p_last = p_remove->p_prev;
  598. p_remove->p_prev->p_next = NULL;
  599. } else {
  600. p_remove->p_next->p_prev = p_remove->p_prev;
  601. p_remove->p_prev->p_next = p_remove->p_next;
  602. }
  603. p_list->count--;
  604. gatt_update_last_pri_srv_info(p_list);
  605. return TRUE;
  606. }
  607. /*******************************************************************************
  608. **
  609. ** Function gatt_add_an_item_to_list
  610. **
  611. ** Description add an service handle range to the list in decending
  612. ** order of the start handle
  613. **
  614. ** Returns BOOLEAN TRUE-if add is successful
  615. **
  616. *******************************************************************************/
  617. BOOLEAN gatt_add_an_item_to_list(tGATT_HDL_LIST_INFO *p_list, tGATT_HDL_LIST_ELEM *p_new)
  618. {
  619. tGATT_HDL_LIST_ELEM *p_old;
  620. if (!p_new) {
  621. GATT_TRACE_DEBUG("p_new==NULL");
  622. return FALSE;
  623. }
  624. if (!p_list->p_first) {
  625. /* this is an empty list */
  626. p_list->p_first =
  627. p_list->p_last = p_new;
  628. p_new->p_next =
  629. p_new->p_prev = NULL;
  630. } else {
  631. p_old = p_list->p_first;
  632. while (1) {
  633. if (p_old == NULL) {
  634. p_list->p_last->p_next = p_new;
  635. p_new->p_prev = p_list->p_last;
  636. p_new->p_next = NULL;
  637. p_list->p_last = p_new;
  638. break;
  639. } else {
  640. if (p_new->asgn_range.s_handle > p_old->asgn_range.s_handle) {
  641. if (p_old == p_list->p_first) {
  642. p_list->p_first = p_new;
  643. }
  644. p_new->p_prev = p_old->p_prev;
  645. p_new->p_next = p_old;
  646. p_old->p_prev = p_new;
  647. break;
  648. }
  649. }
  650. p_old = p_old->p_next;
  651. }
  652. }
  653. p_list->count++;
  654. return TRUE;
  655. }
  656. /*******************************************************************************
  657. **
  658. ** Function gatt_remove_an_item_from_list
  659. **
  660. ** Description Remove an service handle range from the list
  661. **
  662. ** Returns BOOLEAN TRUE-if remove is successful
  663. **
  664. *******************************************************************************/
  665. BOOLEAN gatt_remove_an_item_from_list(tGATT_HDL_LIST_INFO *p_list, tGATT_HDL_LIST_ELEM *p_remove)
  666. {
  667. if (!p_remove || !p_list->p_first) {
  668. GATT_TRACE_DEBUG("p_remove==NULL || p_list->p_first==NULL");
  669. return FALSE;
  670. }
  671. if (p_remove->p_prev == NULL) {
  672. p_list->p_first = p_remove->p_next;
  673. if (p_remove->p_next) {
  674. p_remove->p_next->p_prev = NULL;
  675. }
  676. } else if (p_remove->p_next == NULL) {
  677. p_list->p_last = p_remove->p_prev;
  678. p_remove->p_prev->p_next = NULL;
  679. } else {
  680. p_remove->p_next->p_prev = p_remove->p_prev;
  681. p_remove->p_prev->p_next = p_remove->p_next;
  682. }
  683. p_list->count--;
  684. return TRUE;
  685. }
  686. /*******************************************************************************
  687. **
  688. ** Function gatt_find_the_connected_bda
  689. **
  690. ** Description This function find the connected bda
  691. **
  692. ** Returns TRUE if found
  693. **
  694. *******************************************************************************/
  695. BOOLEAN gatt_find_the_connected_bda(UINT8 start_idx, BD_ADDR bda, UINT8 *p_found_idx,
  696. tBT_TRANSPORT *p_transport)
  697. {
  698. BOOLEAN found = FALSE;
  699. GATT_TRACE_DEBUG("gatt_find_the_connected_bda start_idx=%d", start_idx);
  700. tGATT_TCB *p_tcb = NULL;
  701. list_node_t *p_node = NULL;
  702. p_tcb = gatt_get_tcb_by_idx(start_idx);
  703. if (p_tcb) {
  704. for(p_node = list_get_node(gatt_cb.p_tcb_list, p_tcb); p_node; p_node = list_next(p_node)) {
  705. p_tcb = list_node(p_node);
  706. if (p_tcb->in_use && p_tcb->ch_state == GATT_CH_OPEN) {
  707. memcpy( bda, p_tcb->peer_bda, BD_ADDR_LEN);
  708. *p_found_idx = p_tcb->tcb_idx;
  709. *p_transport = p_tcb->transport;
  710. found = TRUE;
  711. GATT_TRACE_DEBUG("gatt_find_the_connected_bda bda :%02x-%02x-%02x-%02x-%02x-%02x",
  712. bda[0], bda[1], bda[2], bda[3], bda[4], bda[5]);
  713. break;
  714. }
  715. }
  716. GATT_TRACE_DEBUG("gatt_find_the_connected_bda found=%d found_idx=%d", found, p_tcb->tcb_idx);
  717. }
  718. return found;
  719. }
  720. /*******************************************************************************
  721. **
  722. ** Function gatt_is_srv_chg_ind_pending
  723. **
  724. ** Description Check whether a service chnaged is in the indication pending queue
  725. ** or waiting for an Ack already
  726. **
  727. ** Returns BOOLEAN
  728. **
  729. *******************************************************************************/
  730. BOOLEAN gatt_is_srv_chg_ind_pending (tGATT_TCB *p_tcb)
  731. {
  732. BOOLEAN srv_chg_ind_pending = FALSE;
  733. GATT_TRACE_DEBUG("gatt_is_srv_chg_ind_pending is_queue_empty=%d",
  734. fixed_queue_is_empty(p_tcb->pending_ind_q));
  735. if (p_tcb->indicate_handle == gatt_cb.handle_of_h_r) {
  736. srv_chg_ind_pending = TRUE;
  737. } else if (! fixed_queue_is_empty(p_tcb->pending_ind_q)) {
  738. list_t *list = fixed_queue_get_list(p_tcb->pending_ind_q);
  739. for (const list_node_t *node = list_begin(list);
  740. node != list_end(list);
  741. node = list_next(node)) {
  742. tGATT_VALUE *p_buf = (tGATT_VALUE *)list_node(node);
  743. if (p_buf->handle == gatt_cb.handle_of_h_r)
  744. {
  745. srv_chg_ind_pending = TRUE;
  746. break;
  747. }
  748. }
  749. }
  750. GATT_TRACE_DEBUG("srv_chg_ind_pending = %d", srv_chg_ind_pending);
  751. return srv_chg_ind_pending;
  752. }
  753. /*******************************************************************************
  754. **
  755. ** Function gatt_is_bda_in_the_srv_chg_clt_list
  756. **
  757. ** Description This function check the specified bda is in the srv chg clinet list or not
  758. **
  759. ** Returns pointer to the found elemenet otherwise NULL
  760. **
  761. *******************************************************************************/
  762. tGATTS_SRV_CHG *gatt_is_bda_in_the_srv_chg_clt_list (BD_ADDR bda)
  763. {
  764. tGATTS_SRV_CHG *p_buf = NULL;
  765. GATT_TRACE_DEBUG("gatt_is_bda_in_the_srv_chg_clt_list :%02x-%02x-%02x-%02x-%02x-%02x",
  766. bda[0], bda[1], bda[2], bda[3], bda[4], bda[5]);
  767. if (fixed_queue_is_empty(gatt_cb.srv_chg_clt_q)) {
  768. return NULL;
  769. }
  770. list_t *list = fixed_queue_get_list(gatt_cb.srv_chg_clt_q);
  771. for (const list_node_t *node = list_begin(list); node != list_end(list);
  772. node = list_next(node)) {
  773. p_buf = (tGATTS_SRV_CHG *)list_node(node);
  774. if (!memcmp( bda, p_buf->bda, BD_ADDR_LEN)) {
  775. GATT_TRACE_DEBUG("bda is in the srv chg clt list");
  776. break;
  777. }
  778. }
  779. return p_buf;
  780. }
  781. /*******************************************************************************
  782. **
  783. ** Function gatt_is_bda_connected
  784. **
  785. ** Description
  786. **
  787. ** Returns GATT_INDEX_INVALID if not found. Otherwise index to the tcb.
  788. **
  789. *******************************************************************************/
  790. BOOLEAN gatt_is_bda_connected(BD_ADDR bda)
  791. {
  792. BOOLEAN connected = FALSE;
  793. tGATT_TCB *p_tcb = NULL;
  794. list_node_t *p_node = NULL;
  795. for(p_node = list_begin(gatt_cb.p_tcb_list); p_node; p_node = list_next(p_node)) {
  796. p_tcb = list_node(p_node);
  797. if (p_tcb->in_use &&
  798. !memcmp(p_tcb->peer_bda, bda, BD_ADDR_LEN)) {
  799. connected = TRUE;
  800. break;
  801. }
  802. }
  803. return connected;
  804. }
  805. /*******************************************************************************
  806. **
  807. ** Function gatt_check_connection_state_by_tcb
  808. **
  809. ** Description
  810. **
  811. ** Returns TRUE if connected. Otherwise connection not established.
  812. **
  813. *******************************************************************************/
  814. BOOLEAN gatt_check_connection_state_by_tcb(tGATT_TCB *p_tcb)
  815. {
  816. BOOLEAN connected = FALSE;
  817. if(p_tcb && gatt_get_ch_state(p_tcb) == GATT_CH_OPEN) {
  818. connected = TRUE;
  819. }
  820. return connected;
  821. }
  822. /*******************************************************************************
  823. **
  824. ** Function gatt_find_i_tcb_by_addr
  825. **
  826. ** Description The function searches for an empty tcb entry, and return the index.
  827. **
  828. ** Returns GATT_INDEX_INVALID if not found. Otherwise index to the tcb.
  829. **
  830. *******************************************************************************/
  831. UINT8 gatt_find_i_tcb_by_addr(BD_ADDR bda, tBT_TRANSPORT transport)
  832. {
  833. UINT8 i = 0;
  834. list_node_t *p_node = NULL;
  835. tGATT_TCB *p_tcb = NULL;
  836. for(p_node = list_begin(gatt_cb.p_tcb_list); p_node; p_node = list_next(p_node)) {
  837. p_tcb = list_node(p_node);
  838. if (!memcmp(p_tcb->peer_bda, bda, BD_ADDR_LEN) &&
  839. p_tcb->transport == transport) {
  840. i = p_tcb->tcb_idx;
  841. return i;
  842. }
  843. }
  844. return GATT_INDEX_INVALID;
  845. }
  846. /*******************************************************************************
  847. **
  848. ** Function gatt_get_tcb_by_idx
  849. **
  850. ** Description The function get TCB using the TCB index
  851. **
  852. ** Returns NULL if not found. Otherwise index to the tcb.
  853. **
  854. *******************************************************************************/
  855. tGATT_TCB *gatt_get_tcb_by_idx(UINT8 tcb_idx)
  856. {
  857. tGATT_TCB *p_tcb = NULL;
  858. list_node_t *p_node = NULL;
  859. for(p_node = list_begin(gatt_cb.p_tcb_list); p_node; p_node = list_next(p_node)) {
  860. p_tcb = list_node(p_node);
  861. if ( (tcb_idx < GATT_MAX_PHY_CHANNEL) && p_tcb->in_use && p_tcb->tcb_idx == tcb_idx ) {
  862. break;
  863. } else {
  864. p_tcb = NULL;
  865. }
  866. }
  867. return p_tcb;
  868. }
  869. /*******************************************************************************
  870. **
  871. ** Function gatt_find_tcb_by_addr
  872. **
  873. ** Description The function searches for an empty tcb entry, and return pointer.
  874. **
  875. ** Returns NULL if not found. Otherwise index to the tcb.
  876. **
  877. *******************************************************************************/
  878. tGATT_TCB *gatt_find_tcb_by_addr(BD_ADDR bda, tBT_TRANSPORT transport)
  879. {
  880. tGATT_TCB *p_tcb = NULL;
  881. UINT8 i = 0;
  882. if ((i = gatt_find_i_tcb_by_addr(bda, transport)) != GATT_INDEX_INVALID) {
  883. p_tcb = gatt_get_tcb_by_idx(i);
  884. }
  885. return p_tcb;
  886. }
  887. /*******************************************************************************
  888. **
  889. ** Function gatt_find_i_tcb_free
  890. **
  891. ** Description The function searches for an empty tcb entry, and return the index.
  892. **
  893. ** Returns GATT_INDEX_INVALID if not found. Otherwise index to the tcb.
  894. **
  895. *******************************************************************************/
  896. UINT8 gatt_find_i_tcb_free(void)
  897. {
  898. UINT8 i = 0, j = GATT_INDEX_INVALID;
  899. for (i = 0; i < GATT_MAX_PHY_CHANNEL; i ++) {
  900. if (!((1 << i) & gatt_tcb_id)) {
  901. j = i;
  902. break;
  903. }
  904. }
  905. return j;
  906. }
  907. /*******************************************************************************
  908. **
  909. ** Function gatt_tcb_alloc
  910. **
  911. ** Description The function allocates tcb for given tcb_idx and update tcb_id
  912. **
  913. ** Returns Allocated tcb block
  914. **
  915. *******************************************************************************/
  916. tGATT_TCB *gatt_tcb_alloc(UINT8 tcb_idx)
  917. {
  918. /* Allocate tcb block */
  919. tGATT_TCB *p_tcb = (tGATT_TCB *)osi_malloc(sizeof(tGATT_TCB));
  920. if (p_tcb && list_length(gatt_cb.p_tcb_list) < GATT_MAX_PHY_CHANNEL) {
  921. memset(p_tcb, 0, sizeof(tGATT_TCB));
  922. /* Add tcb block to list in gatt_cb */
  923. list_append(gatt_cb.p_tcb_list, p_tcb);
  924. /* Update tcb id */
  925. gatt_tcb_id |= 1 << tcb_idx;
  926. } else if(p_tcb) {
  927. osi_free(p_tcb);
  928. p_tcb = NULL;
  929. }
  930. return p_tcb;
  931. }
  932. /*******************************************************************************
  933. **
  934. ** Function gatt_tcb_free
  935. **
  936. ** Description The function free the given tcb block and update tcb id
  937. **
  938. ** Returns void
  939. **
  940. *******************************************************************************/
  941. void gatt_tcb_free( tGATT_TCB *p_tcb)
  942. {
  943. UINT8 tcb_idx = p_tcb->tcb_idx;
  944. if (list_remove(gatt_cb.p_tcb_list, p_tcb)) {
  945. gatt_tcb_id &= ~(1 << tcb_idx);
  946. }
  947. }
  948. /*******************************************************************************
  949. **
  950. ** Function gatt_allocate_tcb_by_bdaddr
  951. **
  952. ** Description The function locate or allocate new tcb entry for matching bda.
  953. **
  954. ** Returns GATT_INDEX_INVALID if not found. Otherwise index to the tcb.
  955. **
  956. *******************************************************************************/
  957. tGATT_TCB *gatt_allocate_tcb_by_bdaddr(BD_ADDR bda, tBT_TRANSPORT transport)
  958. {
  959. UINT8 i = 0;
  960. BOOLEAN allocated = FALSE;
  961. tGATT_TCB *p_tcb = NULL;
  962. /* search for existing tcb with matching bda */
  963. i = gatt_find_i_tcb_by_addr(bda, transport);
  964. /* find free tcb */
  965. if (i == GATT_INDEX_INVALID) {
  966. i = gatt_find_i_tcb_free();
  967. allocated = TRUE;
  968. }
  969. if (i != GATT_INDEX_INVALID) {
  970. p_tcb = gatt_tcb_alloc(i);
  971. if (!p_tcb) {
  972. return NULL;
  973. }
  974. if (allocated) {
  975. memset(p_tcb, 0, sizeof(tGATT_TCB));
  976. p_tcb->pending_enc_clcb = fixed_queue_new(QUEUE_SIZE_MAX);
  977. p_tcb->pending_ind_q = fixed_queue_new(QUEUE_SIZE_MAX);
  978. p_tcb->in_use = TRUE;
  979. p_tcb->tcb_idx = i;
  980. p_tcb->transport = transport;
  981. }
  982. memcpy(p_tcb->peer_bda, bda, BD_ADDR_LEN);
  983. }
  984. return p_tcb;
  985. }
  986. /*******************************************************************************
  987. **
  988. ** Function gatt_convert_uuid16_to_uuid128
  989. **
  990. ** Description Convert a 16 bits UUID to be an standard 128 bits one.
  991. **
  992. ** Returns TRUE if two uuid match; FALSE otherwise.
  993. **
  994. *******************************************************************************/
  995. void gatt_convert_uuid16_to_uuid128(UINT8 uuid_128[LEN_UUID_128], UINT16 uuid_16)
  996. {
  997. UINT8 *p = &uuid_128[LEN_UUID_128 - 4];
  998. memcpy (uuid_128, base_uuid, LEN_UUID_128);
  999. UINT16_TO_STREAM(p, uuid_16);
  1000. }
  1001. /*******************************************************************************
  1002. **
  1003. ** Function gatt_convert_uuid32_to_uuid128
  1004. **
  1005. ** Description Convert a 32 bits UUID to be an standard 128 bits one.
  1006. **
  1007. ** Returns TRUE if two uuid match; FALSE otherwise.
  1008. **
  1009. *******************************************************************************/
  1010. void gatt_convert_uuid32_to_uuid128(UINT8 uuid_128[LEN_UUID_128], UINT32 uuid_32)
  1011. {
  1012. UINT8 *p = &uuid_128[LEN_UUID_128 - 4];
  1013. memcpy (uuid_128, base_uuid, LEN_UUID_128);
  1014. UINT32_TO_STREAM(p, uuid_32);
  1015. }
  1016. /*******************************************************************************
  1017. **
  1018. ** Function gatt_uuid_compare
  1019. **
  1020. ** Description Compare two UUID to see if they are the same.
  1021. **
  1022. ** Returns TRUE if two uuid match; FALSE otherwise.
  1023. **
  1024. *******************************************************************************/
  1025. BOOLEAN gatt_uuid_compare (tBT_UUID src, tBT_UUID tar)
  1026. {
  1027. UINT8 su[LEN_UUID_128], tu[LEN_UUID_128];
  1028. UINT8 *ps, *pt;
  1029. /* any of the UUID is unspecified */
  1030. if (src.len == 0 || tar.len == 0) {
  1031. return TRUE;
  1032. }
  1033. /* If both are 16-bit, we can do a simple compare */
  1034. if (src.len == LEN_UUID_16 && tar.len == LEN_UUID_16) {
  1035. return src.uu.uuid16 == tar.uu.uuid16;
  1036. }
  1037. /* If both are 32-bit, we can do a simple compare */
  1038. if (src.len == LEN_UUID_32 && tar.len == LEN_UUID_32) {
  1039. return src.uu.uuid32 == tar.uu.uuid32;
  1040. }
  1041. /* One or both of the UUIDs is 128-bit */
  1042. if (src.len == LEN_UUID_16) {
  1043. /* convert a 16 bits UUID to 128 bits value */
  1044. gatt_convert_uuid16_to_uuid128(su, src.uu.uuid16);
  1045. ps = su;
  1046. } else if (src.len == LEN_UUID_32) {
  1047. gatt_convert_uuid32_to_uuid128(su, src.uu.uuid32);
  1048. ps = su;
  1049. } else {
  1050. ps = src.uu.uuid128;
  1051. }
  1052. if (tar.len == LEN_UUID_16) {
  1053. /* convert a 16 bits UUID to 128 bits value */
  1054. gatt_convert_uuid16_to_uuid128(tu, tar.uu.uuid16);
  1055. pt = tu;
  1056. } else if (tar.len == LEN_UUID_32) {
  1057. /* convert a 32 bits UUID to 128 bits value */
  1058. gatt_convert_uuid32_to_uuid128(tu, tar.uu.uuid32);
  1059. pt = tu;
  1060. } else {
  1061. pt = tar.uu.uuid128;
  1062. }
  1063. return (memcmp(ps, pt, LEN_UUID_128) == 0);
  1064. }
  1065. /*******************************************************************************
  1066. **
  1067. ** Function gatt_build_uuid_to_stream
  1068. **
  1069. ** Description Add UUID into stream.
  1070. **
  1071. ** Returns UUID length.
  1072. **
  1073. *******************************************************************************/
  1074. UINT8 gatt_build_uuid_to_stream(UINT8 **p_dst, tBT_UUID uuid)
  1075. {
  1076. UINT8 *p = *p_dst;
  1077. UINT8 len = 0;
  1078. if (uuid.len == LEN_UUID_16) {
  1079. UINT16_TO_STREAM (p, uuid.uu.uuid16);
  1080. len = LEN_UUID_16;
  1081. } else if (uuid.len == LEN_UUID_32) { /* always convert 32 bits into 128 bits as alwats */
  1082. gatt_convert_uuid32_to_uuid128(p, uuid.uu.uuid32);
  1083. p += LEN_UUID_128;
  1084. len = LEN_UUID_128;
  1085. } else if (uuid.len == LEN_UUID_128) {
  1086. ARRAY_TO_STREAM (p, uuid.uu.uuid128, LEN_UUID_128);
  1087. len = LEN_UUID_128;
  1088. }
  1089. *p_dst = p;
  1090. return len;
  1091. }
  1092. /*******************************************************************************
  1093. **
  1094. ** Function gatt_parse_uuid_from_cmd
  1095. **
  1096. ** Description Convert a 128 bits UUID into a 16 bits UUID.
  1097. **
  1098. ** Returns TRUE if command sent, otherwise FALSE.
  1099. **
  1100. *******************************************************************************/
  1101. BOOLEAN gatt_parse_uuid_from_cmd(tBT_UUID *p_uuid_rec, UINT16 uuid_size, UINT8 **p_data)
  1102. {
  1103. BOOLEAN is_base_uuid, ret = TRUE;
  1104. UINT8 xx;
  1105. UINT8 *p_uuid = *p_data;
  1106. memset(p_uuid_rec, 0, sizeof(tBT_UUID));
  1107. switch (uuid_size) {
  1108. case LEN_UUID_16:
  1109. p_uuid_rec->len = uuid_size;
  1110. STREAM_TO_UINT16 (p_uuid_rec->uu.uuid16, p_uuid);
  1111. *p_data += LEN_UUID_16;
  1112. break;
  1113. case LEN_UUID_128:
  1114. /* See if we can compress his UUID down to 16 or 32bit UUIDs */
  1115. is_base_uuid = TRUE;
  1116. for (xx = 0; xx < LEN_UUID_128 - 4; xx++) {
  1117. if (p_uuid[xx] != base_uuid[xx]) {
  1118. is_base_uuid = FALSE;
  1119. break;
  1120. }
  1121. }
  1122. if (is_base_uuid) {
  1123. if ((p_uuid[LEN_UUID_128 - 1] == 0) && (p_uuid[LEN_UUID_128 - 2] == 0)) {
  1124. p_uuid += (LEN_UUID_128 - 4);
  1125. p_uuid_rec->len = LEN_UUID_16;
  1126. STREAM_TO_UINT16(p_uuid_rec->uu.uuid16, p_uuid);
  1127. } else {
  1128. p_uuid += (LEN_UUID_128 - LEN_UUID_32);
  1129. p_uuid_rec->len = LEN_UUID_32;
  1130. STREAM_TO_UINT32(p_uuid_rec->uu.uuid32, p_uuid);
  1131. }
  1132. }
  1133. if (!is_base_uuid) {
  1134. p_uuid_rec->len = LEN_UUID_128;
  1135. memcpy(p_uuid_rec->uu.uuid128, p_uuid, LEN_UUID_128);
  1136. }
  1137. *p_data += LEN_UUID_128;
  1138. break;
  1139. /* do not allow 32 bits UUID in ATT PDU now */
  1140. case LEN_UUID_32:
  1141. GATT_TRACE_ERROR("DO NOT ALLOW 32 BITS UUID IN ATT PDU");
  1142. case 0:
  1143. default:
  1144. if (uuid_size != 0) {
  1145. ret = FALSE;
  1146. }
  1147. GATT_TRACE_WARNING("gatt_parse_uuid_from_cmd invalid uuid size");
  1148. break;
  1149. }
  1150. return ( ret);
  1151. }
  1152. /*******************************************************************************
  1153. **
  1154. ** Function gatt_start_rsp_timer
  1155. **
  1156. ** Description Start a wait_for_response timer.
  1157. **
  1158. ** Returns TRUE if command sent, otherwise FALSE.
  1159. **
  1160. *******************************************************************************/
  1161. void gatt_start_rsp_timer(UINT16 clcb_idx)
  1162. {
  1163. tGATT_CLCB *p_clcb = gatt_clcb_find_by_idx(clcb_idx);
  1164. UINT32 timeout = GATT_WAIT_FOR_RSP_TOUT;
  1165. p_clcb->rsp_timer_ent.param = (TIMER_PARAM_TYPE)p_clcb;
  1166. if (p_clcb->operation == GATTC_OPTYPE_DISCOVERY &&
  1167. p_clcb->op_subtype == GATT_DISC_SRVC_ALL) {
  1168. timeout = GATT_WAIT_FOR_DISC_RSP_TOUT;
  1169. }
  1170. btu_start_timer (&p_clcb->rsp_timer_ent, BTU_TTYPE_ATT_WAIT_FOR_RSP,
  1171. timeout);
  1172. }
  1173. /*******************************************************************************
  1174. **
  1175. ** Function gatt_start_conf_timer
  1176. **
  1177. ** Description Start a wait_for_confirmation timer.
  1178. **
  1179. ** Returns TRUE if command sent, otherwise FALSE.
  1180. **
  1181. *******************************************************************************/
  1182. void gatt_start_conf_timer(tGATT_TCB *p_tcb)
  1183. {
  1184. p_tcb->conf_timer_ent.param = (TIMER_PARAM_TYPE)p_tcb;
  1185. btu_start_timer (&p_tcb->conf_timer_ent, BTU_TTYPE_ATT_WAIT_FOR_RSP,
  1186. GATT_WAIT_FOR_RSP_TOUT);
  1187. }
  1188. /*******************************************************************************
  1189. **
  1190. ** Function gatt_start_ind_ack_timer
  1191. **
  1192. ** Description start the application ack timer
  1193. **
  1194. ** Returns void
  1195. **
  1196. *******************************************************************************/
  1197. void gatt_start_ind_ack_timer(tGATT_TCB *p_tcb)
  1198. {
  1199. p_tcb->ind_ack_timer_ent.param = (TIMER_PARAM_TYPE)p_tcb;
  1200. /* start notification cache timer */
  1201. btu_start_timer (&p_tcb->ind_ack_timer_ent, BTU_TTYPE_ATT_WAIT_FOR_IND_ACK,
  1202. GATT_WAIT_FOR_IND_ACK_TOUT);
  1203. }
  1204. /*******************************************************************************
  1205. **
  1206. ** Function gatt_rsp_timeout
  1207. **
  1208. ** Description Called when GATT wait for ATT command response timer expires
  1209. **
  1210. ** Returns void
  1211. **
  1212. *******************************************************************************/
  1213. void gatt_rsp_timeout(TIMER_LIST_ENT *p_tle)
  1214. {
  1215. tGATT_CLCB *p_clcb = (tGATT_CLCB *)p_tle->param;
  1216. if (p_clcb == NULL || p_clcb->p_tcb == NULL) {
  1217. GATT_TRACE_WARNING("gatt_rsp_timeout clcb is already deleted");
  1218. return;
  1219. }
  1220. if (p_clcb->operation == GATTC_OPTYPE_DISCOVERY &&
  1221. p_clcb->op_subtype == GATT_DISC_SRVC_ALL &&
  1222. p_clcb->retry_count < GATT_REQ_RETRY_LIMIT) {
  1223. UINT8 rsp_code;
  1224. GATT_TRACE_WARNING("gatt_rsp_timeout retry discovery primary service");
  1225. if (p_clcb != gatt_cmd_dequeue(p_clcb->p_tcb, &rsp_code)) {
  1226. GATT_TRACE_ERROR("gatt_rsp_timeout command queue out of sync, disconnect");
  1227. } else {
  1228. p_clcb->retry_count++;
  1229. #if (GATTC_INCLUDED == TRUE)
  1230. gatt_act_discovery(p_clcb);
  1231. #endif ///GATTC_INCLUDED == TRUE
  1232. return;
  1233. }
  1234. }
  1235. GATT_TRACE_WARNING("gatt_rsp_timeout disconnecting...");
  1236. gatt_disconnect (p_clcb->p_tcb);
  1237. }
  1238. /*******************************************************************************
  1239. **
  1240. ** Function gatt_ind_ack_timeout
  1241. **
  1242. ** Description Called when GATT wait for ATT handle confirmation timeout
  1243. **
  1244. ** Returns void
  1245. **
  1246. *******************************************************************************/
  1247. void gatt_ind_ack_timeout(TIMER_LIST_ENT *p_tle)
  1248. {
  1249. tGATT_TCB *p_tcb = (tGATT_TCB *)p_tle->param;
  1250. GATT_TRACE_WARNING("gatt_ind_ack_timeout send ack now");
  1251. if (p_tcb != NULL) {
  1252. p_tcb->ind_count = 0;
  1253. }
  1254. attp_send_cl_msg(((tGATT_TCB *)p_tle->param), 0, GATT_HANDLE_VALUE_CONF, NULL);
  1255. }
  1256. /*******************************************************************************
  1257. **
  1258. ** Function gatt_sr_find_i_rcb_by_handle
  1259. **
  1260. ** Description The function searches for a service that owns a specific handle.
  1261. **
  1262. ** Returns GATT_MAX_SR_PROFILES if not found. Otherwise index of th eservice.
  1263. **
  1264. *******************************************************************************/
  1265. UINT8 gatt_sr_find_i_rcb_by_handle(UINT16 handle)
  1266. {
  1267. UINT8 i_rcb = 0;
  1268. for ( ; i_rcb < GATT_MAX_SR_PROFILES; i_rcb++) {
  1269. if (gatt_cb.sr_reg[i_rcb].in_use &&
  1270. gatt_cb.sr_reg[i_rcb].s_hdl <= handle &&
  1271. gatt_cb.sr_reg[i_rcb].e_hdl >= handle ) {
  1272. break;
  1273. }
  1274. }
  1275. return i_rcb;
  1276. }
  1277. /*******************************************************************************
  1278. **
  1279. ** Function gatt_sr_find_i_rcb_by_handle
  1280. **
  1281. ** Description The function searches for a service that owns a specific handle.
  1282. **
  1283. ** Returns GATT_MAX_SR_PROFILES if not found. Otherwise index of th eservice.
  1284. **
  1285. *******************************************************************************/
  1286. #if (GATTS_INCLUDED == TRUE)
  1287. UINT8 gatt_sr_find_i_rcb_by_app_id(tBT_UUID *p_app_uuid128, tBT_UUID *p_svc_uuid, UINT16 svc_inst)
  1288. {
  1289. UINT8 i_rcb = 0;
  1290. tGATT_SR_REG *p_sreg;
  1291. tBT_UUID *p_this_uuid;
  1292. for (i_rcb = 0, p_sreg = gatt_cb.sr_reg; i_rcb < GATT_MAX_SR_PROFILES; i_rcb++, p_sreg++) {
  1293. if ( p_sreg->in_use ) {
  1294. p_this_uuid = gatts_get_service_uuid (p_sreg->p_db);
  1295. if (p_this_uuid &&
  1296. gatt_uuid_compare (*p_app_uuid128, p_sreg->app_uuid ) &&
  1297. gatt_uuid_compare (*p_svc_uuid, *p_this_uuid) &&
  1298. (svc_inst == p_sreg->service_instance)) {
  1299. GATT_TRACE_ERROR ("Active Service Found ");
  1300. gatt_dbg_display_uuid(*p_svc_uuid);
  1301. break;
  1302. }
  1303. }
  1304. }
  1305. return i_rcb;
  1306. }
  1307. #endif ///GATTS_INCLUDED == TRUE
  1308. /*******************************************************************************
  1309. **
  1310. ** Function gatt_sr_find_i_rcb_by_handle
  1311. **
  1312. ** Description The function searches for a service that owns a specific handle.
  1313. **
  1314. ** Returns 0 if not found. Otherwise index of th eservice.
  1315. **
  1316. *******************************************************************************/
  1317. UINT8 gatt_sr_alloc_rcb(tGATT_HDL_LIST_ELEM *p_list )
  1318. {
  1319. UINT8 ii = 0;
  1320. tGATT_SR_REG *p_sreg = NULL;
  1321. /*this is a new application servoce start */
  1322. for (ii = 0, p_sreg = gatt_cb.sr_reg; ii < GATT_MAX_SR_PROFILES; ii++, p_sreg++) {
  1323. if (!p_sreg->in_use) {
  1324. memset (p_sreg, 0, sizeof(tGATT_SR_REG));
  1325. p_sreg->in_use = TRUE;
  1326. memcpy (&p_sreg->app_uuid, &p_list->asgn_range.app_uuid128, sizeof(tBT_UUID));
  1327. p_sreg->service_instance = p_list->asgn_range.svc_inst;
  1328. p_sreg->type = p_list->asgn_range.is_primary ? GATT_UUID_PRI_SERVICE : GATT_UUID_SEC_SERVICE;
  1329. p_sreg->s_hdl = p_list->asgn_range.s_handle;
  1330. p_sreg->e_hdl = p_list->asgn_range.e_handle;
  1331. p_sreg->p_db = &p_list->svc_db;
  1332. GATT_TRACE_DEBUG ("total buffer in db [%d]", fixed_queue_length(p_sreg->p_db->svc_buffer));
  1333. break;
  1334. }
  1335. }
  1336. return ii;
  1337. }
  1338. /*******************************************************************************
  1339. **
  1340. ** Function gatt_sr_get_sec_info
  1341. **
  1342. ** Description Get the security flag and key size information for the peer
  1343. ** device.
  1344. **
  1345. ** Returns void
  1346. **
  1347. *******************************************************************************/
  1348. void gatt_sr_get_sec_info(BD_ADDR rem_bda, tBT_TRANSPORT transport, UINT8 *p_sec_flag, UINT8 *p_key_size)
  1349. {
  1350. UINT8 sec_flag = 0;
  1351. BTM_GetSecurityFlagsByTransport(rem_bda, &sec_flag, transport);
  1352. sec_flag &= (GATT_SEC_FLAG_LKEY_UNAUTHED | GATT_SEC_FLAG_LKEY_AUTHED | GATT_SEC_FLAG_ENCRYPTED | GATT_SEC_FLAG_AUTHORIZATION);
  1353. #if (SMP_INCLUDED == TRUE)
  1354. *p_key_size = btm_ble_read_sec_key_size(rem_bda);
  1355. #endif ///SMP_INCLUDED == TRUE
  1356. *p_sec_flag = sec_flag;
  1357. }
  1358. /*******************************************************************************
  1359. **
  1360. ** Function gatt_sr_send_req_callback
  1361. **
  1362. ** Description
  1363. **
  1364. **
  1365. ** Returns void
  1366. **
  1367. *******************************************************************************/
  1368. void gatt_sr_send_req_callback(UINT16 conn_id,
  1369. UINT32 trans_id,
  1370. tGATTS_REQ_TYPE type, tGATTS_DATA *p_data)
  1371. {
  1372. tGATT_IF gatt_if = GATT_GET_GATT_IF(conn_id);
  1373. tGATT_REG *p_reg = gatt_get_regcb(gatt_if);
  1374. if (!p_reg ) {
  1375. GATT_TRACE_ERROR ("p_reg not found discard request");
  1376. return;
  1377. }
  1378. if ( p_reg->in_use &&
  1379. p_reg->app_cb.p_req_cb) {
  1380. (*p_reg->app_cb.p_req_cb)(conn_id, trans_id, type, p_data);
  1381. } else {
  1382. GATT_TRACE_WARNING("Call back not found for application conn_id=%d", conn_id);
  1383. }
  1384. }
  1385. /*******************************************************************************
  1386. **
  1387. ** Function gatt_send_error_rsp
  1388. **
  1389. ** Description This function sends an error response.
  1390. **
  1391. ** Returns void
  1392. **
  1393. *******************************************************************************/
  1394. tGATT_STATUS gatt_send_error_rsp (tGATT_TCB *p_tcb, UINT8 err_code, UINT8 op_code,
  1395. UINT16 handle, BOOLEAN deq)
  1396. {
  1397. tGATT_ERROR error;
  1398. tGATT_STATUS status;
  1399. BT_HDR *p_buf;
  1400. error.cmd_code = op_code;
  1401. error.reason = err_code;
  1402. error.handle = handle;
  1403. if ((p_buf = attp_build_sr_msg(p_tcb, GATT_RSP_ERROR, (tGATT_SR_MSG *)&error)) != NULL) {
  1404. status = attp_send_sr_msg (p_tcb, p_buf);
  1405. } else {
  1406. status = GATT_INSUF_RESOURCE;
  1407. }
  1408. #if (GATTS_INCLUDED == TRUE)
  1409. if (deq) {
  1410. gatt_dequeue_sr_cmd(p_tcb);
  1411. }
  1412. #endif ///GATTS_INCLUDED == TRUE
  1413. return status;
  1414. }
  1415. #if (SDP_INCLUDED == TRUE && CLASSIC_BT_GATT_INCLUDED == TRUE)
  1416. /*******************************************************************************
  1417. **
  1418. ** Function gatt_add_sdp_record
  1419. **
  1420. ** Description This function add a SDP record for a GATT primary service
  1421. **
  1422. ** Returns 0 if error else sdp handle for the record.
  1423. **
  1424. *******************************************************************************/
  1425. UINT32 gatt_add_sdp_record (tBT_UUID *p_uuid, UINT16 start_hdl, UINT16 end_hdl)
  1426. {
  1427. tSDP_PROTOCOL_ELEM proto_elem_list[2];
  1428. UINT32 sdp_handle;
  1429. UINT16 list = UUID_SERVCLASS_PUBLIC_BROWSE_GROUP;
  1430. UINT8 buff[60];
  1431. UINT8 *p = buff;
  1432. GATT_TRACE_DEBUG("gatt_add_sdp_record s_hdl=0x%x s_hdl=0x%x", start_hdl, end_hdl);
  1433. if ((sdp_handle = SDP_CreateRecord()) == 0) {
  1434. return 0;
  1435. }
  1436. switch (p_uuid->len) {
  1437. case LEN_UUID_16:
  1438. SDP_AddServiceClassIdList(sdp_handle, 1, &p_uuid->uu.uuid16);
  1439. break;
  1440. case LEN_UUID_32:
  1441. UINT8_TO_BE_STREAM (p, (UUID_DESC_TYPE << 3) | SIZE_FOUR_BYTES);
  1442. UINT32_TO_BE_STREAM (p, p_uuid->uu.uuid32);
  1443. SDP_AddAttribute (sdp_handle, ATTR_ID_SERVICE_CLASS_ID_LIST, DATA_ELE_SEQ_DESC_TYPE,
  1444. (UINT32) (p - buff), buff);
  1445. break;
  1446. case LEN_UUID_128:
  1447. UINT8_TO_BE_STREAM (p, (UUID_DESC_TYPE << 3) | SIZE_SIXTEEN_BYTES);
  1448. ARRAY_TO_BE_STREAM_REVERSE (p, p_uuid->uu.uuid128, LEN_UUID_128);
  1449. SDP_AddAttribute (sdp_handle, ATTR_ID_SERVICE_CLASS_ID_LIST, DATA_ELE_SEQ_DESC_TYPE,
  1450. (UINT32) (p - buff), buff);
  1451. break;
  1452. default:
  1453. GATT_TRACE_ERROR("inavlid UUID len=%d", p_uuid->len);
  1454. SDP_DeleteRecord(sdp_handle);
  1455. return 0;
  1456. break;
  1457. }
  1458. /*** Fill out the protocol element sequence for SDP ***/
  1459. proto_elem_list[0].protocol_uuid = UUID_PROTOCOL_L2CAP;
  1460. proto_elem_list[0].num_params = 1;
  1461. proto_elem_list[0].params[0] = BT_PSM_ATT;
  1462. proto_elem_list[1].protocol_uuid = UUID_PROTOCOL_ATT;
  1463. proto_elem_list[1].num_params = 2;
  1464. proto_elem_list[1].params[0] = start_hdl;
  1465. proto_elem_list[1].params[1] = end_hdl;
  1466. SDP_AddProtocolList(sdp_handle, 2, proto_elem_list);
  1467. /* Make the service browseable */
  1468. SDP_AddUuidSequence (sdp_handle, ATTR_ID_BROWSE_GROUP_LIST, 1, &list);
  1469. return (sdp_handle);
  1470. }
  1471. #endif ///SDP_INCLUDED == TRUE && CLASSIC_BT_GATT_INCLUDED == TRUE
  1472. #if GATT_CONFORMANCE_TESTING == TRUE
  1473. /*******************************************************************************
  1474. **
  1475. ** Function gatt_set_err_rsp
  1476. **
  1477. ** Description This function is called to set the test confirm value
  1478. **
  1479. ** Returns void
  1480. **
  1481. *******************************************************************************/
  1482. void gatt_set_err_rsp(BOOLEAN enable, UINT8 req_op_code, UINT8 err_status)
  1483. {
  1484. GATT_TRACE_DEBUG("gatt_set_err_rsp enable=%d op_code=%d, err_status=%d", enable, req_op_code, err_status);
  1485. gatt_cb.enable_err_rsp = enable;
  1486. gatt_cb.req_op_code = req_op_code;
  1487. gatt_cb.err_status = err_status;
  1488. }
  1489. #endif
  1490. /*******************************************************************************
  1491. **
  1492. ** Function gatt_get_regcb
  1493. **
  1494. ** Description The function returns the registration control block.
  1495. **
  1496. ** Returns pointer to the registration control block or NULL
  1497. **
  1498. *******************************************************************************/
  1499. tGATT_REG *gatt_get_regcb (tGATT_IF gatt_if)
  1500. {
  1501. UINT8 ii = (UINT8)gatt_if;
  1502. tGATT_REG *p_reg = NULL;
  1503. if (ii < 1 || ii > GATT_MAX_APPS) {
  1504. GATT_TRACE_WARNING("gatt_if out of range [ = %d]", ii);
  1505. return NULL;
  1506. }
  1507. // Index for cl_rcb is always 1 less than gatt_if.
  1508. p_reg = &gatt_cb.cl_rcb[ii - 1];
  1509. if (!p_reg->in_use) {
  1510. GATT_TRACE_WARNING("gatt_if found but not in use.\n");
  1511. return NULL;
  1512. }
  1513. return p_reg;
  1514. }
  1515. /*******************************************************************************
  1516. **
  1517. ** Function gatt_is_clcb_allocated
  1518. **
  1519. ** Description The function check clcb for conn_id is allocated or not
  1520. **
  1521. ** Returns True already allocated
  1522. **
  1523. *******************************************************************************/
  1524. BOOLEAN gatt_is_clcb_allocated (UINT16 conn_id)
  1525. {
  1526. BOOLEAN is_allocated = FALSE;
  1527. tGATT_CLCB *p_clcb = gatt_clcb_find_by_conn_id(conn_id);
  1528. if (p_clcb) {
  1529. is_allocated = TRUE;
  1530. }
  1531. return is_allocated;
  1532. }
  1533. /*******************************************************************************
  1534. **
  1535. ** Function gatt_clcb_find_by_conn_id
  1536. **
  1537. ** Description Find clcb block using clcb_idx stored at the time of alloc
  1538. **
  1539. ** Returns pointer to clcb corresponding to conn_id
  1540. **
  1541. *******************************************************************************/
  1542. tGATT_CLCB *gatt_clcb_find_by_conn_id(UINT16 conn_id)
  1543. {
  1544. list_node_t *p_node = NULL;
  1545. tGATT_CLCB *p_clcb = NULL;
  1546. tGATT_CLCB *p_clcb_ret = NULL;
  1547. for(p_node = list_begin(gatt_cb.p_clcb_list); p_node; p_node = list_next(p_node)) {
  1548. p_clcb = list_node(p_node);
  1549. if (p_clcb->conn_id == conn_id) {
  1550. p_clcb_ret = p_clcb;
  1551. break;
  1552. }
  1553. }
  1554. return p_clcb_ret;
  1555. }
  1556. /*******************************************************************************
  1557. **
  1558. ** Function gatt_clcb_find_by_idx
  1559. **
  1560. ** Description Find clcb block using clcb_idx stored at the time of alloc
  1561. **
  1562. ** Returns pointer to clcb corresponding to clcb_idx
  1563. **
  1564. *******************************************************************************/
  1565. tGATT_CLCB *gatt_clcb_find_by_idx(UINT16 clcb_idx)
  1566. {
  1567. list_node_t *p_node = NULL;
  1568. tGATT_CLCB *p_clcb = NULL;
  1569. tGATT_CLCB *p_clcb_ret = NULL;
  1570. for(p_node = list_begin(gatt_cb.p_clcb_list); p_node; p_node = list_next(p_node)) {
  1571. p_clcb = list_node(p_node);
  1572. if (p_clcb->clcb_idx == clcb_idx) {
  1573. p_clcb_ret = p_clcb;
  1574. break;
  1575. }
  1576. }
  1577. return p_clcb_ret;
  1578. }
  1579. /*******************************************************************************
  1580. **
  1581. ** Function gatt_clcb_alloc
  1582. **
  1583. ** Description The function allocates a GATT connection link control block
  1584. **
  1585. ** Returns NULL if not found. Otherwise pointer to the connection link block.
  1586. **
  1587. *******************************************************************************/
  1588. tGATT_CLCB *gatt_clcb_alloc (UINT16 conn_id)
  1589. {
  1590. tGATT_CLCB *p_clcb = NULL;
  1591. tGATT_IF gatt_if = GATT_GET_GATT_IF(conn_id);
  1592. UINT8 tcb_idx = GATT_GET_TCB_IDX(conn_id);
  1593. tGATT_TCB *p_tcb = gatt_get_tcb_by_idx(tcb_idx);
  1594. tGATT_REG *p_reg = gatt_get_regcb(gatt_if);
  1595. if (list_length(gatt_cb.p_clcb_list) < GATT_CL_MAX_LCB) {
  1596. p_clcb = (tGATT_CLCB *)osi_malloc(sizeof(tGATT_CLCB));
  1597. if (p_clcb) {
  1598. list_append(gatt_cb.p_clcb_list, p_clcb);
  1599. memset(p_clcb, 0, sizeof(tGATT_CLCB));
  1600. p_clcb->in_use = TRUE;
  1601. p_clcb->conn_id = conn_id;
  1602. //Add index of the clcb same as conn_id
  1603. p_clcb->clcb_idx = conn_id;
  1604. p_clcb->p_reg = p_reg;
  1605. p_clcb->p_tcb = p_tcb;
  1606. }
  1607. }
  1608. return p_clcb;
  1609. }
  1610. /*******************************************************************************
  1611. **
  1612. ** Function gatt_clcb_dealloc
  1613. **
  1614. ** Description The function de allocates a GATT connection link control block
  1615. **
  1616. ** Returns None
  1617. **
  1618. *******************************************************************************/
  1619. void gatt_clcb_dealloc (tGATT_CLCB *p_clcb)
  1620. {
  1621. if (p_clcb && p_clcb->in_use) {
  1622. btu_free_timer(&p_clcb->rsp_timer_ent);
  1623. memset(p_clcb, 0, sizeof(tGATT_CLCB));
  1624. list_remove(gatt_cb.p_clcb_list, p_clcb);
  1625. p_clcb = NULL;
  1626. }
  1627. }
  1628. /*******************************************************************************
  1629. **
  1630. ** Function gatt_find_tcb_by_cid
  1631. **
  1632. ** Description The function searches for an empty entry
  1633. ** in registration info table for GATT client
  1634. **
  1635. ** Returns NULL if not found. Otherwise pointer to the rcb.
  1636. **
  1637. *******************************************************************************/
  1638. tGATT_TCB *gatt_find_tcb_by_cid (UINT16 lcid)
  1639. {
  1640. tGATT_TCB *p_tcb = NULL;
  1641. list_node_t *p_node = NULL;
  1642. for(p_node = list_begin(gatt_cb.p_tcb_list); p_node; p_node = list_next(p_node)) {
  1643. p_tcb = list_node(p_node);
  1644. if (p_tcb->in_use && p_tcb->att_lcid == lcid) {
  1645. break;
  1646. }
  1647. }
  1648. return p_tcb;
  1649. }
  1650. /*******************************************************************************
  1651. **
  1652. ** Function gatt_num_apps_hold_link
  1653. **
  1654. ** Description The function find the number of applcaitions is holding the link
  1655. **
  1656. ** Returns total number of applications holding this acl link.
  1657. **
  1658. *******************************************************************************/
  1659. UINT8 gatt_num_apps_hold_link(tGATT_TCB *p_tcb)
  1660. {
  1661. UINT8 i, num = 0;
  1662. for (i = 0; i < GATT_MAX_APPS; i ++) {
  1663. if (p_tcb->app_hold_link[i]) {
  1664. num ++;
  1665. }
  1666. }
  1667. GATT_TRACE_DEBUG("gatt_num_apps_hold_link num=%d", num);
  1668. return num;
  1669. }
  1670. /*******************************************************************************
  1671. **
  1672. ** Function gatt_num_clcb_by_bd_addr
  1673. **
  1674. ** Description The function searches all LCB with macthing bd address
  1675. **
  1676. ** Returns total number of clcb found.
  1677. **
  1678. *******************************************************************************/
  1679. UINT8 gatt_num_clcb_by_bd_addr(BD_ADDR bda)
  1680. {
  1681. UINT8 num = 0;
  1682. list_node_t *p_node = NULL;
  1683. tGATT_CLCB *p_clcb = NULL;
  1684. for(p_node = list_begin(gatt_cb.p_clcb_list); p_node; p_node = list_next(p_node)) {
  1685. p_clcb = list_node(p_node);
  1686. if (memcmp(p_clcb->p_tcb->peer_bda, bda, BD_ADDR_LEN) == 0) {
  1687. num++;
  1688. }
  1689. }
  1690. return num;
  1691. }
  1692. /*******************************************************************************
  1693. **
  1694. ** Function gatt_sr_update_cback_cnt
  1695. **
  1696. ** Description The function searches all LCB with macthing bd address
  1697. **
  1698. ** Returns total number of clcb found.
  1699. **
  1700. *******************************************************************************/
  1701. void gatt_sr_copy_prep_cnt_to_cback_cnt(tGATT_TCB *p_tcb )
  1702. {
  1703. #if (GATTS_INCLUDED == TRUE)
  1704. UINT8 i;
  1705. if (p_tcb) {
  1706. for (i = 0; i < GATT_MAX_APPS; i ++) {
  1707. if (p_tcb->prep_cnt[i]) {
  1708. p_tcb->sr_cmd.cback_cnt[i] = 1;
  1709. }
  1710. }
  1711. }
  1712. #endif ///GATTS_INCLUDED == TRUE
  1713. }
  1714. /*******************************************************************************
  1715. **
  1716. ** Function gatt_sr_is_cback_cnt_zero
  1717. **
  1718. ** Description The function searches all LCB with macthing bd address
  1719. **
  1720. ** Returns True if thetotal application callback count is zero
  1721. **
  1722. *******************************************************************************/
  1723. BOOLEAN gatt_sr_is_cback_cnt_zero(tGATT_TCB *p_tcb )
  1724. {
  1725. BOOLEAN status = TRUE;
  1726. #if (GATTS_INCLUDED == TRUE)
  1727. UINT8 i;
  1728. if (p_tcb) {
  1729. for (i = 0; i < GATT_MAX_APPS; i ++) {
  1730. if (p_tcb->sr_cmd.cback_cnt[i]) {
  1731. status = FALSE;
  1732. break;
  1733. }
  1734. }
  1735. } else {
  1736. status = FALSE;
  1737. }
  1738. #endif ///GATTS_INCLUDED == TRUE
  1739. return status;
  1740. }
  1741. /*******************************************************************************
  1742. **
  1743. ** Function gatt_sr_is_prep_cnt_zero
  1744. **
  1745. ** Description Check the prepare write request count is zero or not
  1746. **
  1747. ** Returns True no prepare write request
  1748. **
  1749. *******************************************************************************/
  1750. BOOLEAN gatt_sr_is_prep_cnt_zero(tGATT_TCB *p_tcb)
  1751. {
  1752. BOOLEAN status = TRUE;
  1753. UINT8 i;
  1754. if (p_tcb) {
  1755. for (i = 0; i < GATT_MAX_APPS; i ++) {
  1756. if (p_tcb->prep_cnt[i]) {
  1757. status = FALSE;
  1758. break;
  1759. }
  1760. }
  1761. } else {
  1762. status = FALSE;
  1763. }
  1764. return status;
  1765. }
  1766. /*******************************************************************************
  1767. **
  1768. ** Function gatt_sr_reset_cback_cnt
  1769. **
  1770. ** Description Reset the application callback count to zero
  1771. **
  1772. ** Returns None
  1773. **
  1774. *******************************************************************************/
  1775. void gatt_sr_reset_cback_cnt(tGATT_TCB *p_tcb )
  1776. {
  1777. #if (GATTS_INCLUDED == TRUE)
  1778. UINT8 i;
  1779. if (p_tcb) {
  1780. for (i = 0; i < GATT_MAX_APPS; i ++) {
  1781. p_tcb->sr_cmd.cback_cnt[i] = 0;
  1782. }
  1783. }
  1784. #endif ///GATTS_INCLUDED == TRUE
  1785. }
  1786. /*******************************************************************************
  1787. **
  1788. ** Function gatt_sr_reset_prep_cnt
  1789. **
  1790. ** Description Reset the prep write count to zero
  1791. **
  1792. ** Returns None
  1793. **
  1794. *******************************************************************************/
  1795. void gatt_sr_reset_prep_cnt(tGATT_TCB *p_tcb )
  1796. {
  1797. UINT8 i;
  1798. if (p_tcb) {
  1799. for (i = 0; i < GATT_MAX_APPS; i ++) {
  1800. p_tcb->prep_cnt[i] = 0;
  1801. }
  1802. }
  1803. }
  1804. /*******************************************************************************
  1805. **
  1806. ** Function gatt_sr_update_cback_cnt
  1807. **
  1808. ** Description Update the teh application callback count
  1809. **
  1810. ** Returns None
  1811. **
  1812. *******************************************************************************/
  1813. void gatt_sr_update_cback_cnt(tGATT_TCB *p_tcb, tGATT_IF gatt_if, BOOLEAN is_inc, BOOLEAN is_reset_first)
  1814. {
  1815. #if (GATTS_INCLUDED == TRUE)
  1816. UINT8 idx = ((UINT8) gatt_if) - 1 ;
  1817. if (p_tcb) {
  1818. if (is_reset_first) {
  1819. gatt_sr_reset_cback_cnt(p_tcb);
  1820. }
  1821. if (is_inc) {
  1822. p_tcb->sr_cmd.cback_cnt[idx]++;
  1823. } else {
  1824. if ( p_tcb->sr_cmd.cback_cnt[idx]) {
  1825. p_tcb->sr_cmd.cback_cnt[idx]--;
  1826. }
  1827. }
  1828. }
  1829. #endif ///GATTS_INCLUDED == TRUE
  1830. }
  1831. /*******************************************************************************
  1832. **
  1833. ** Function gatt_sr_update_prep_cnt
  1834. **
  1835. ** Description Update the teh prepare write request count
  1836. **
  1837. ** Returns None
  1838. **
  1839. *******************************************************************************/
  1840. void gatt_sr_update_prep_cnt(tGATT_TCB *p_tcb, tGATT_IF gatt_if, BOOLEAN is_inc, BOOLEAN is_reset_first)
  1841. {
  1842. UINT8 idx = ((UINT8) gatt_if) - 1 ;
  1843. GATT_TRACE_DEBUG("gatt_sr_update_prep_cnt tcb idx=%d gatt_if=%d is_inc=%d is_reset_first=%d",
  1844. p_tcb->tcb_idx, gatt_if, is_inc, is_reset_first);
  1845. if (p_tcb) {
  1846. if (is_reset_first) {
  1847. gatt_sr_reset_prep_cnt(p_tcb);
  1848. }
  1849. if (is_inc) {
  1850. p_tcb->prep_cnt[idx]++;
  1851. } else {
  1852. if (p_tcb->prep_cnt[idx]) {
  1853. p_tcb->prep_cnt[idx]--;
  1854. }
  1855. }
  1856. }
  1857. }
  1858. /*******************************************************************************
  1859. **
  1860. ** Function gatt_cancel_open
  1861. **
  1862. ** Description Cancel open request
  1863. **
  1864. ** Returns Boolean
  1865. **
  1866. *******************************************************************************/
  1867. BOOLEAN gatt_cancel_open(tGATT_IF gatt_if, BD_ADDR bda)
  1868. {
  1869. tGATT_TCB *p_tcb = NULL;
  1870. BOOLEAN status = TRUE;
  1871. p_tcb = gatt_find_tcb_by_addr(bda, BT_TRANSPORT_LE);
  1872. if (p_tcb) {
  1873. if (gatt_get_ch_state(p_tcb) == GATT_CH_OPEN) {
  1874. GATT_TRACE_ERROR("GATT_CancelConnect - link connected Too late to cancel");
  1875. status = FALSE;
  1876. } else {
  1877. gatt_update_app_use_link_flag(gatt_if, p_tcb, FALSE, FALSE);
  1878. if (!gatt_num_apps_hold_link(p_tcb)) {
  1879. gatt_disconnect(p_tcb);
  1880. }
  1881. }
  1882. }
  1883. return status;
  1884. }
  1885. /*******************************************************************************
  1886. **
  1887. ** Function gatt_find_app_hold_link
  1888. **
  1889. ** Description find the application that is holding the specified link
  1890. **
  1891. ** Returns Boolean
  1892. **
  1893. *******************************************************************************/
  1894. BOOLEAN gatt_find_app_hold_link(tGATT_TCB *p_tcb, UINT8 start_idx, UINT8 *p_found_idx, tGATT_IF *p_gatt_if)
  1895. {
  1896. UINT8 i;
  1897. BOOLEAN found = FALSE;
  1898. for (i = start_idx; i < GATT_MAX_APPS; i ++) {
  1899. if (p_tcb->app_hold_link[i]) {
  1900. *p_gatt_if = p_tcb->app_hold_link[i];
  1901. *p_found_idx = i;
  1902. found = TRUE;
  1903. break;
  1904. }
  1905. }
  1906. return found;
  1907. }
  1908. /*******************************************************************************
  1909. **
  1910. ** Function gatt_find_specific_app_in_hold_link
  1911. **
  1912. ** Description find the specific application that is holding the specified link
  1913. **
  1914. ** Returns Boolean
  1915. **
  1916. *******************************************************************************/
  1917. BOOLEAN gatt_find_specific_app_in_hold_link(tGATT_TCB *p_tcb, tGATT_IF p_gatt_if)
  1918. {
  1919. UINT8 i;
  1920. BOOLEAN found = FALSE;
  1921. for (i = 0; i < GATT_MAX_APPS; i ++) {
  1922. if (p_tcb->app_hold_link[i] && p_tcb->app_hold_link[i] == p_gatt_if) {
  1923. found = TRUE;
  1924. break;
  1925. }
  1926. }
  1927. return found;
  1928. }
  1929. /*******************************************************************************
  1930. **
  1931. ** Function gatt_cmd_enq
  1932. **
  1933. ** Description Enqueue this command.
  1934. **
  1935. ** Returns None.
  1936. **
  1937. *******************************************************************************/
  1938. BOOLEAN gatt_cmd_enq(tGATT_TCB *p_tcb, UINT16 clcb_idx, BOOLEAN to_send, UINT8 op_code, BT_HDR *p_buf)
  1939. {
  1940. tGATT_CMD_Q *p_cmd = &p_tcb->cl_cmd_q[p_tcb->next_slot_inq];
  1941. p_cmd->to_send = to_send; /* waiting to be sent */
  1942. p_cmd->op_code = op_code;
  1943. p_cmd->p_cmd = p_buf;
  1944. p_cmd->clcb_idx = clcb_idx;
  1945. if (!to_send) {
  1946. p_tcb->pending_cl_req = p_tcb->next_slot_inq;
  1947. }
  1948. p_tcb->next_slot_inq ++;
  1949. p_tcb->next_slot_inq %= GATT_CL_MAX_LCB;
  1950. return TRUE;
  1951. }
  1952. /*******************************************************************************
  1953. **
  1954. ** Function gatt_cmd_dequeue
  1955. **
  1956. ** Description dequeue the command in the client CCB command queue.
  1957. **
  1958. ** Returns total number of clcb found.
  1959. **
  1960. *******************************************************************************/
  1961. tGATT_CLCB *gatt_cmd_dequeue(tGATT_TCB *p_tcb, UINT8 *p_op_code)
  1962. {
  1963. tGATT_CMD_Q *p_cmd = &p_tcb->cl_cmd_q[p_tcb->pending_cl_req];
  1964. tGATT_CLCB *p_clcb = NULL;
  1965. if (p_tcb->pending_cl_req != p_tcb->next_slot_inq) {
  1966. p_clcb = gatt_clcb_find_by_idx(p_cmd->clcb_idx);
  1967. *p_op_code = p_cmd->op_code;
  1968. p_tcb->pending_cl_req ++;
  1969. p_tcb->pending_cl_req %= GATT_CL_MAX_LCB;
  1970. }
  1971. return p_clcb;
  1972. }
  1973. /*******************************************************************************
  1974. **
  1975. ** Function gatt_send_write_msg
  1976. **
  1977. ** Description This real function send out the ATT message for write.
  1978. **
  1979. ** Returns status code
  1980. **
  1981. *******************************************************************************/
  1982. UINT8 gatt_send_write_msg (tGATT_TCB *p_tcb, UINT16 clcb_idx, UINT8 op_code,
  1983. UINT16 handle, UINT16 len,
  1984. UINT16 offset, UINT8 *p_data)
  1985. {
  1986. tGATT_CL_MSG msg;
  1987. msg.attr_value.handle = handle;
  1988. msg.attr_value.len = len;
  1989. msg.attr_value.offset = offset;
  1990. memcpy (msg.attr_value.value, p_data, len);
  1991. /* write by handle */
  1992. return attp_send_cl_msg(p_tcb, clcb_idx, op_code, &msg);
  1993. }
  1994. /*******************************************************************************
  1995. **
  1996. ** Function gatt_act_send_browse
  1997. **
  1998. ** Description This function ends a browse command request, including read
  1999. ** information request and read by type request.
  2000. **
  2001. ** Returns status code
  2002. **
  2003. *******************************************************************************/
  2004. UINT8 gatt_act_send_browse(tGATT_TCB *p_tcb, UINT16 index, UINT8 op, UINT16 s_handle,
  2005. UINT16 e_handle, tBT_UUID uuid)
  2006. {
  2007. tGATT_CL_MSG msg;
  2008. msg.browse.s_handle = s_handle;
  2009. msg.browse.e_handle = e_handle;
  2010. memcpy(&msg.browse.uuid, &uuid, sizeof(tBT_UUID));
  2011. /* write by handle */
  2012. return attp_send_cl_msg(p_tcb, index, op, &msg);
  2013. }
  2014. /*******************************************************************************
  2015. **
  2016. ** Function gatt_end_operation
  2017. **
  2018. ** Description This function ends a discovery, send callback and finalize
  2019. ** some control value.
  2020. **
  2021. ** Returns 16 bits uuid.
  2022. **
  2023. *******************************************************************************/
  2024. void gatt_end_operation(tGATT_CLCB *p_clcb, tGATT_STATUS status, void *p_data)
  2025. {
  2026. tGATT_CL_COMPLETE cb_data;
  2027. tGATT_CMPL_CBACK *p_cmpl_cb = (p_clcb->p_reg) ? p_clcb->p_reg->app_cb.p_cmpl_cb : NULL;
  2028. UINT8 op = p_clcb->operation, disc_type = GATT_DISC_MAX;
  2029. tGATT_DISC_CMPL_CB *p_disc_cmpl_cb = (p_clcb->p_reg) ? p_clcb->p_reg->app_cb.p_disc_cmpl_cb : NULL;
  2030. UINT16 conn_id;
  2031. #if (!CONFIG_BT_STACK_NO_LOG)
  2032. UINT8 operation;
  2033. #endif
  2034. GATT_TRACE_DEBUG ("gatt_end_operation status=%d op=%d subtype=%d",
  2035. status, p_clcb->operation, p_clcb->op_subtype);
  2036. memset(&cb_data.att_value, 0, sizeof(tGATT_VALUE));
  2037. if (p_cmpl_cb != NULL && p_clcb->operation != 0) {
  2038. if (p_clcb->operation == GATTC_OPTYPE_READ) {
  2039. cb_data.att_value.handle = p_clcb->s_handle;
  2040. cb_data.att_value.len = p_clcb->counter;
  2041. if (p_data && p_clcb->counter) {
  2042. memcpy (cb_data.att_value.value, p_data, cb_data.att_value.len);
  2043. }
  2044. }
  2045. if (p_clcb->operation == GATTC_OPTYPE_WRITE) {
  2046. memset(&cb_data.att_value, 0, sizeof(tGATT_VALUE));
  2047. cb_data.handle =
  2048. cb_data.att_value.handle = p_clcb->s_handle;
  2049. if (p_clcb->op_subtype == GATT_WRITE_PREPARE) {
  2050. if (p_data) {
  2051. cb_data.att_value = *((tGATT_VALUE *) p_data);
  2052. } else {
  2053. GATT_TRACE_DEBUG("Rcv Prepare write rsp but no data");
  2054. }
  2055. }
  2056. }
  2057. if (p_clcb->operation == GATTC_OPTYPE_CONFIG) {
  2058. cb_data.mtu = p_clcb->p_tcb->payload_size;
  2059. }
  2060. if (p_clcb->operation == GATTC_OPTYPE_DISCOVERY) {
  2061. disc_type = p_clcb->op_subtype;
  2062. }
  2063. }
  2064. if (p_clcb->p_attr_buf) {
  2065. osi_free(p_clcb->p_attr_buf);
  2066. }
  2067. #if !CONFIG_BT_STACK_NO_LOG
  2068. operation = p_clcb->operation;
  2069. #endif
  2070. conn_id = p_clcb->conn_id;
  2071. btu_stop_timer(&p_clcb->rsp_timer_ent);
  2072. gatt_clcb_dealloc(p_clcb);
  2073. if (p_disc_cmpl_cb && (op == GATTC_OPTYPE_DISCOVERY)) {
  2074. (*p_disc_cmpl_cb)(conn_id, disc_type, status);
  2075. } else if (p_cmpl_cb && op) {
  2076. (*p_cmpl_cb)(conn_id, op, status, &cb_data);
  2077. } else {
  2078. GATT_TRACE_WARNING ("gatt_end_operation not sent out op=%d p_disc_cmpl_cb:%p p_cmpl_cb:%p",
  2079. operation, p_disc_cmpl_cb, p_cmpl_cb);
  2080. }
  2081. }
  2082. /*******************************************************************************
  2083. **
  2084. ** Function gatt_cleanup_upon_disc
  2085. **
  2086. ** Description This function cleans up the control blocks when L2CAP channel
  2087. ** disconnect.
  2088. **
  2089. ** Returns 16 bits uuid.
  2090. **
  2091. *******************************************************************************/
  2092. void gatt_cleanup_upon_disc(BD_ADDR bda, UINT16 reason, tBT_TRANSPORT transport)
  2093. {
  2094. tGATT_TCB *p_tcb = NULL;
  2095. tGATT_CLCB *p_clcb;
  2096. UINT8 i;
  2097. UINT16 conn_id;
  2098. tGATT_REG *p_reg = NULL;
  2099. GATT_TRACE_DEBUG ("gatt_cleanup_upon_disc ");
  2100. if ((p_tcb = gatt_find_tcb_by_addr(bda, transport)) != NULL) {
  2101. GATT_TRACE_DEBUG ("found p_tcb ");
  2102. gatt_set_ch_state(p_tcb, GATT_CH_CLOSE);
  2103. list_node_t *p_node = NULL;
  2104. list_node_t *p_node_next = NULL;
  2105. for(p_node = list_begin(gatt_cb.p_clcb_list); p_node; p_node = p_node_next) {
  2106. p_clcb = list_node(p_node);
  2107. p_node_next = list_next(p_node);
  2108. if (p_clcb->in_use && p_clcb->p_tcb == p_tcb) {
  2109. btu_stop_timer(&p_clcb->rsp_timer_ent);
  2110. GATT_TRACE_DEBUG ("found p_clcb conn_id=%d clcb_idx=%d", p_clcb->conn_id, p_clcb->clcb_idx);
  2111. if (p_clcb->operation != GATTC_OPTYPE_NONE) {
  2112. gatt_end_operation(p_clcb, GATT_ERROR, NULL);
  2113. p_clcb = NULL;
  2114. }
  2115. gatt_clcb_dealloc(p_clcb);
  2116. }
  2117. }
  2118. btu_free_timer (&p_tcb->ind_ack_timer_ent);
  2119. btu_free_timer (&p_tcb->conf_timer_ent);
  2120. gatt_free_pending_ind(p_tcb);
  2121. gatt_free_pending_enc_queue(p_tcb);
  2122. gatt_free_pending_prepare_write_queue(p_tcb);
  2123. #if (GATTS_INCLUDED)
  2124. fixed_queue_free(p_tcb->sr_cmd.multi_rsp_q, osi_free_func);
  2125. p_tcb->sr_cmd.multi_rsp_q = NULL;
  2126. #endif /* #if (GATTS_INCLUDED) */
  2127. for (i = 0; i < GATT_MAX_APPS; i ++) {
  2128. p_reg = &gatt_cb.cl_rcb[i];
  2129. if (p_reg->in_use && p_reg->app_cb.p_conn_cb) {
  2130. conn_id = GATT_CREATE_CONN_ID(p_tcb->tcb_idx, p_reg->gatt_if);
  2131. GATT_TRACE_DEBUG ("found p_reg tcb_idx=%d gatt_if=%d conn_id=0x%x", p_tcb->tcb_idx, p_reg->gatt_if, conn_id);
  2132. (*p_reg->app_cb.p_conn_cb)(p_reg->gatt_if, bda, conn_id, FALSE, reason, transport);
  2133. }
  2134. }
  2135. gatt_tcb_free(p_tcb);
  2136. } else {
  2137. GATT_TRACE_DEBUG ("exit gatt_cleanup_upon_disc ");
  2138. BTM_Recovery_Pre_State();
  2139. }
  2140. gatt_delete_dev_from_srv_chg_clt_list(bda);
  2141. }
  2142. /*******************************************************************************
  2143. **
  2144. ** Function gatt_dbg_req_op_name
  2145. **
  2146. ** Description Get op code description name, for debug information.
  2147. **
  2148. ** Returns UINT8 *: name of the operation.
  2149. **
  2150. *******************************************************************************/
  2151. UINT8 *gatt_dbg_op_name(UINT8 op_code)
  2152. {
  2153. UINT8 pseduo_op_code_idx = op_code & (~GATT_WRITE_CMD_MASK);
  2154. if (op_code == GATT_CMD_WRITE ) {
  2155. pseduo_op_code_idx = 0x14; /* just an index to op_code_name */
  2156. }
  2157. if (op_code == GATT_SIGN_CMD_WRITE) {
  2158. pseduo_op_code_idx = 0x15; /* just an index to op_code_name */
  2159. }
  2160. if (pseduo_op_code_idx <= GATT_OP_CODE_MAX) {
  2161. return (UINT8 *) op_code_name[pseduo_op_code_idx];
  2162. } else {
  2163. return (UINT8 *)"Op Code Exceed Max";
  2164. }
  2165. }
  2166. /*******************************************************************************
  2167. **
  2168. ** Function gatt_dbg_display_uuid
  2169. **
  2170. ** Description Disaplay the UUID
  2171. **
  2172. ** Returns None
  2173. **
  2174. *******************************************************************************/
  2175. void gatt_dbg_display_uuid(tBT_UUID bt_uuid)
  2176. {
  2177. char str_buf[50];
  2178. int x = 0;
  2179. if (bt_uuid.len == LEN_UUID_16) {
  2180. sprintf(str_buf, "0x%04x", bt_uuid.uu.uuid16);
  2181. } else if (bt_uuid.len == LEN_UUID_32) {
  2182. sprintf(str_buf, "0x%08x", (unsigned int)bt_uuid.uu.uuid32);
  2183. } else if (bt_uuid.len == LEN_UUID_128) {
  2184. x += sprintf(&str_buf[x], "0x%02x%02x%02x%02x%02x%02x%02x%02x",
  2185. bt_uuid.uu.uuid128[15], bt_uuid.uu.uuid128[14],
  2186. bt_uuid.uu.uuid128[13], bt_uuid.uu.uuid128[12],
  2187. bt_uuid.uu.uuid128[11], bt_uuid.uu.uuid128[10],
  2188. bt_uuid.uu.uuid128[9], bt_uuid.uu.uuid128[8]);
  2189. sprintf(&str_buf[x], "%02x%02x%02x%02x%02x%02x%02x%02x",
  2190. bt_uuid.uu.uuid128[7], bt_uuid.uu.uuid128[6],
  2191. bt_uuid.uu.uuid128[5], bt_uuid.uu.uuid128[4],
  2192. bt_uuid.uu.uuid128[3], bt_uuid.uu.uuid128[2],
  2193. bt_uuid.uu.uuid128[1], bt_uuid.uu.uuid128[0]);
  2194. } else {
  2195. BCM_STRNCPY_S(str_buf, "Unknown UUID 0", 15);
  2196. }
  2197. GATT_TRACE_DEBUG ("UUID=[%s]", str_buf);
  2198. }
  2199. /*******************************************************************************
  2200. **
  2201. ** Function gatt_is_bg_dev_for_app
  2202. **
  2203. ** Description find is this one of the background devices for the application
  2204. **
  2205. ** Returns TRUE this is one of the background devices for the application
  2206. **
  2207. *******************************************************************************/
  2208. BOOLEAN gatt_is_bg_dev_for_app(tGATT_BG_CONN_DEV *p_dev, tGATT_IF gatt_if)
  2209. {
  2210. UINT8 i;
  2211. for (i = 0; i < GATT_MAX_APPS; i ++ ) {
  2212. if (p_dev->in_use && (p_dev->gatt_if[i] == gatt_if)) {
  2213. return TRUE;
  2214. }
  2215. }
  2216. return FALSE;
  2217. }
  2218. /*******************************************************************************
  2219. **
  2220. ** Function gatt_find_bg_dev
  2221. **
  2222. ** Description find background connection device from the list.
  2223. **
  2224. ** Returns pointer to the device record
  2225. **
  2226. *******************************************************************************/
  2227. tGATT_BG_CONN_DEV *gatt_find_bg_dev(BD_ADDR remote_bda)
  2228. {
  2229. tGATT_BG_CONN_DEV *p_dev_list = &gatt_cb.bgconn_dev[0];
  2230. UINT8 i;
  2231. for (i = 0; i < GATT_MAX_BG_CONN_DEV; i ++, p_dev_list ++) {
  2232. if (p_dev_list->in_use && !memcmp(p_dev_list->remote_bda, remote_bda, BD_ADDR_LEN)) {
  2233. return p_dev_list;
  2234. }
  2235. }
  2236. return NULL;
  2237. }
  2238. /*******************************************************************************
  2239. **
  2240. ** Function gatt_alloc_bg_dev
  2241. **
  2242. ** Description allocate a background connection device record
  2243. **
  2244. ** Returns pointer to the device record
  2245. **
  2246. *******************************************************************************/
  2247. tGATT_BG_CONN_DEV *gatt_alloc_bg_dev(BD_ADDR remote_bda)
  2248. {
  2249. tGATT_BG_CONN_DEV *p_dev_list = &gatt_cb.bgconn_dev[0];
  2250. UINT8 i;
  2251. for (i = 0; i < GATT_MAX_BG_CONN_DEV; i ++, p_dev_list ++) {
  2252. if (!p_dev_list->in_use) {
  2253. p_dev_list->in_use = TRUE;
  2254. memcpy(p_dev_list->remote_bda, remote_bda, BD_ADDR_LEN);
  2255. return p_dev_list;
  2256. }
  2257. }
  2258. return NULL;
  2259. }
  2260. /*******************************************************************************
  2261. **
  2262. ** Function gatt_add_bg_dev_list
  2263. **
  2264. ** Description add/remove device from the background connection device list
  2265. **
  2266. ** Returns TRUE if device added to the list; FALSE failed
  2267. **
  2268. *******************************************************************************/
  2269. BOOLEAN gatt_add_bg_dev_list(tGATT_REG *p_reg, BD_ADDR bd_addr, BOOLEAN is_initator)
  2270. {
  2271. tGATT_IF gatt_if = p_reg->gatt_if;
  2272. tGATT_BG_CONN_DEV *p_dev = NULL;
  2273. UINT8 i;
  2274. BOOLEAN ret = FALSE;
  2275. if ((p_dev = gatt_find_bg_dev(bd_addr)) == NULL) {
  2276. p_dev = gatt_alloc_bg_dev(bd_addr);
  2277. }
  2278. if (p_dev) {
  2279. for (i = 0; i < GATT_MAX_APPS; i ++) {
  2280. if (is_initator) {
  2281. if (p_dev->gatt_if[i] == gatt_if) {
  2282. GATT_TRACE_ERROR("device already in iniator white list");
  2283. return TRUE;
  2284. } else if (p_dev->gatt_if[i] == 0) {
  2285. p_dev->gatt_if[i] = gatt_if;
  2286. if (i == 0) {
  2287. ret = BTM_BleUpdateBgConnDev(TRUE, bd_addr);
  2288. } else {
  2289. ret = TRUE;
  2290. }
  2291. break;
  2292. }
  2293. } else {
  2294. if (p_dev->listen_gif[i] == gatt_if) {
  2295. GATT_TRACE_ERROR("device already in adv white list");
  2296. return TRUE;
  2297. } else if (p_dev->listen_gif[i] == 0) {
  2298. if (p_reg->listening == GATT_LISTEN_TO_ALL) {
  2299. p_reg->listening = GATT_LISTEN_TO_NONE;
  2300. }
  2301. p_reg->listening ++;
  2302. p_dev->listen_gif[i] = gatt_if;
  2303. if (i == 0) {
  2304. // To check, we do not support background connection, code will not be called here
  2305. ret = BTM_BleUpdateAdvWhitelist(TRUE, bd_addr, 0, NULL);
  2306. } else {
  2307. ret = TRUE;
  2308. }
  2309. break;
  2310. }
  2311. }
  2312. }
  2313. } else {
  2314. GATT_TRACE_ERROR("no device record available");
  2315. }
  2316. return ret;
  2317. }
  2318. /*******************************************************************************
  2319. **
  2320. ** Function gatt_remove_bg_dev_for_app
  2321. **
  2322. ** Description Remove the application interface for the specified background device
  2323. **
  2324. ** Returns Boolean
  2325. **
  2326. *******************************************************************************/
  2327. BOOLEAN gatt_remove_bg_dev_for_app(tGATT_IF gatt_if, BD_ADDR bd_addr)
  2328. {
  2329. tGATT_TCB *p_tcb = gatt_find_tcb_by_addr(bd_addr, BT_TRANSPORT_LE);
  2330. BOOLEAN status;
  2331. if (p_tcb) {
  2332. gatt_update_app_use_link_flag(gatt_if, p_tcb, FALSE, FALSE);
  2333. }
  2334. status = gatt_update_auto_connect_dev(gatt_if, FALSE, bd_addr, TRUE);
  2335. return status;
  2336. }
  2337. /*******************************************************************************
  2338. **
  2339. ** Function gatt_get_num_apps_for_bg_dev
  2340. **
  2341. ** Description Gte the number of applciations for the specified background device
  2342. **
  2343. ** Returns UINT8 total number fo applications
  2344. **
  2345. *******************************************************************************/
  2346. UINT8 gatt_get_num_apps_for_bg_dev(BD_ADDR bd_addr)
  2347. {
  2348. tGATT_BG_CONN_DEV *p_dev = NULL;
  2349. UINT8 i;
  2350. UINT8 cnt = 0;
  2351. if ((p_dev = gatt_find_bg_dev(bd_addr)) != NULL) {
  2352. for (i = 0; i < GATT_MAX_APPS; i ++) {
  2353. if (p_dev->gatt_if[i]) {
  2354. cnt++;
  2355. }
  2356. }
  2357. }
  2358. return cnt;
  2359. }
  2360. /*******************************************************************************
  2361. **
  2362. ** Function gatt_find_app_for_bg_dev
  2363. **
  2364. ** Description find the application interface for the specified background device
  2365. **
  2366. ** Returns Boolean
  2367. **
  2368. *******************************************************************************/
  2369. BOOLEAN gatt_find_app_for_bg_dev(BD_ADDR bd_addr, tGATT_IF *p_gatt_if)
  2370. {
  2371. tGATT_BG_CONN_DEV *p_dev = NULL;
  2372. UINT8 i;
  2373. BOOLEAN ret = FALSE;
  2374. if ((p_dev = gatt_find_bg_dev(bd_addr)) == NULL) {
  2375. return ret;
  2376. }
  2377. for (i = 0; i < GATT_MAX_APPS; i ++) {
  2378. if (p_dev->gatt_if[i] != 0 ) {
  2379. *p_gatt_if = p_dev->gatt_if[i];
  2380. ret = TRUE;
  2381. break;
  2382. }
  2383. }
  2384. return ret;
  2385. }
  2386. /*******************************************************************************
  2387. **
  2388. ** Function gatt_remove_bg_dev_from_list
  2389. **
  2390. ** Description add/remove device from the background connection device list or
  2391. ** listening to advertising list.
  2392. **
  2393. ** Returns pointer to the device record
  2394. **
  2395. *******************************************************************************/
  2396. BOOLEAN gatt_remove_bg_dev_from_list(tGATT_REG *p_reg, BD_ADDR bd_addr, BOOLEAN is_initiator)
  2397. {
  2398. tGATT_IF gatt_if = p_reg->gatt_if;
  2399. tGATT_BG_CONN_DEV *p_dev = NULL;
  2400. UINT8 i, j;
  2401. BOOLEAN ret = FALSE;
  2402. if ((p_dev = gatt_find_bg_dev(bd_addr)) == NULL) {
  2403. return ret;
  2404. }
  2405. for (i = 0; i < GATT_MAX_APPS && (p_dev->gatt_if[i] > 0 || p_dev->listen_gif[i]); i ++) {
  2406. if (is_initiator) {
  2407. if (p_dev->gatt_if[i] == gatt_if) {
  2408. p_dev->gatt_if[i] = 0;
  2409. /* move all element behind one forward */
  2410. for (j = i + 1; j < GATT_MAX_APPS; j ++) {
  2411. p_dev->gatt_if[j - 1] = p_dev->gatt_if[j];
  2412. }
  2413. if (p_dev->gatt_if[0] == 0) {
  2414. ret = BTM_BleUpdateBgConnDev(FALSE, p_dev->remote_bda);
  2415. } else {
  2416. ret = TRUE;
  2417. }
  2418. break;
  2419. }
  2420. } else {
  2421. if (p_dev->listen_gif[i] == gatt_if) {
  2422. p_dev->listen_gif[i] = 0;
  2423. p_reg->listening --;
  2424. /* move all element behind one forward */
  2425. for (j = i + 1; j < GATT_MAX_APPS; j ++) {
  2426. p_dev->listen_gif[j - 1] = p_dev->listen_gif[j];
  2427. }
  2428. if (p_dev->listen_gif[0] == 0) {
  2429. // To check, we do not support background connection, code will not be called here
  2430. ret = BTM_BleUpdateAdvWhitelist(FALSE, p_dev->remote_bda, 0, NULL);
  2431. } else {
  2432. ret = TRUE;
  2433. }
  2434. break;
  2435. }
  2436. }
  2437. }
  2438. if (i != GATT_MAX_APPS && p_dev->gatt_if[0] == 0 && p_dev->listen_gif[0] == 0) {
  2439. memset(p_dev, 0, sizeof(tGATT_BG_CONN_DEV));
  2440. }
  2441. return ret;
  2442. }
  2443. /*******************************************************************************
  2444. **
  2445. ** Function gatt_deregister_bgdev_list
  2446. **
  2447. ** Description deregister all related background connection device.
  2448. **
  2449. ** Returns pointer to the device record
  2450. **
  2451. *******************************************************************************/
  2452. void gatt_deregister_bgdev_list(tGATT_IF gatt_if)
  2453. {
  2454. tGATT_BG_CONN_DEV *p_dev_list = &gatt_cb.bgconn_dev[0];
  2455. UINT8 i , j, k;
  2456. tGATT_REG *p_reg = gatt_get_regcb(gatt_if);
  2457. /* update the BG conn device list */
  2458. for (i = 0 ; i < GATT_MAX_BG_CONN_DEV; i ++, p_dev_list ++ ) {
  2459. if (p_dev_list->in_use) {
  2460. for (j = 0; j < GATT_MAX_APPS; j ++) {
  2461. if (p_dev_list->gatt_if[j] == 0 && p_dev_list->listen_gif[j] == 0) {
  2462. break;
  2463. }
  2464. if (p_dev_list->gatt_if[j] == gatt_if) {
  2465. for (k = j + 1; k < GATT_MAX_APPS; k ++) {
  2466. p_dev_list->gatt_if[k - 1] = p_dev_list->gatt_if[k];
  2467. }
  2468. if (p_dev_list->gatt_if[0] == 0) {
  2469. BTM_BleUpdateBgConnDev(FALSE, p_dev_list->remote_bda);
  2470. }
  2471. }
  2472. if (p_dev_list->listen_gif[j] == gatt_if) {
  2473. p_dev_list->listen_gif[j] = 0;
  2474. if (p_reg != NULL && p_reg->listening > 0) {
  2475. p_reg->listening --;
  2476. }
  2477. /* move all element behind one forward */
  2478. for (k = j + 1; k < GATT_MAX_APPS; k ++) {
  2479. p_dev_list->listen_gif[k - 1] = p_dev_list->listen_gif[k];
  2480. }
  2481. if (p_dev_list->listen_gif[0] == 0) {
  2482. // To check, we do not support background connection, code will not be called here
  2483. BTM_BleUpdateAdvWhitelist(FALSE, p_dev_list->remote_bda, 0, NULL);
  2484. }
  2485. }
  2486. }
  2487. }
  2488. }
  2489. }
  2490. /*******************************************************************************
  2491. **
  2492. ** Function gatt_reset_bgdev_list
  2493. **
  2494. ** Description reset bg device list
  2495. **
  2496. ** Returns pointer to the device record
  2497. **
  2498. *******************************************************************************/
  2499. void gatt_reset_bgdev_list(void)
  2500. {
  2501. memset(&gatt_cb.bgconn_dev, 0 , sizeof(tGATT_BG_CONN_DEV)*GATT_MAX_BG_CONN_DEV);
  2502. }
  2503. /*******************************************************************************
  2504. **
  2505. ** Function gatt_update_auto_connect_dev
  2506. **
  2507. ** Description This function add or remove a device for background connection
  2508. ** procedure.
  2509. **
  2510. ** Parameters gatt_if: Application ID.
  2511. ** add: add peer device
  2512. ** bd_addr: peer device address.
  2513. **
  2514. ** Returns TRUE if connection started; FALSE if connection start failure.
  2515. **
  2516. *******************************************************************************/
  2517. BOOLEAN gatt_update_auto_connect_dev (tGATT_IF gatt_if, BOOLEAN add, BD_ADDR bd_addr, BOOLEAN is_initator)
  2518. {
  2519. BOOLEAN ret = FALSE;
  2520. tGATT_REG *p_reg;
  2521. tGATT_TCB *p_tcb = gatt_find_tcb_by_addr(bd_addr, BT_TRANSPORT_LE);
  2522. GATT_TRACE_API ("gatt_update_auto_connect_dev ");
  2523. /* Make sure app is registered */
  2524. if ((p_reg = gatt_get_regcb(gatt_if)) == NULL) {
  2525. GATT_TRACE_ERROR("gatt_update_auto_connect_dev - gatt_if %d is not registered", gatt_if);
  2526. return (FALSE);
  2527. }
  2528. if (add) {
  2529. ret = gatt_add_bg_dev_list(p_reg, bd_addr, is_initator);
  2530. if (ret && p_tcb != NULL) {
  2531. /* if a connected device, update the link holding number */
  2532. gatt_update_app_use_link_flag(gatt_if, p_tcb, TRUE, TRUE);
  2533. }
  2534. } else {
  2535. ret = gatt_remove_bg_dev_from_list(p_reg, bd_addr, is_initator);
  2536. }
  2537. return ret;
  2538. }
  2539. /*******************************************************************************
  2540. **
  2541. ** Function gatt_add_pending_new_srv_start
  2542. **
  2543. ** Description Add a pending new srv start to the new service start queue
  2544. **
  2545. ** Returns Pointer to the new service start buffer, NULL no buffer available
  2546. **
  2547. *******************************************************************************/
  2548. tGATT_PENDING_ENC_CLCB *gatt_add_pending_enc_channel_clcb(tGATT_TCB *p_tcb, tGATT_CLCB *p_clcb )
  2549. {
  2550. tGATT_PENDING_ENC_CLCB *p_buf;
  2551. GATT_TRACE_DEBUG ("gatt_add_pending_new_srv_start");
  2552. if ((p_buf = (tGATT_PENDING_ENC_CLCB *)osi_malloc((UINT16)sizeof(tGATT_PENDING_ENC_CLCB))) != NULL) {
  2553. GATT_TRACE_DEBUG ("enqueue a new pending encryption channel clcb");
  2554. p_buf->p_clcb = p_clcb;
  2555. fixed_queue_enqueue(p_tcb->pending_enc_clcb, p_buf, FIXED_QUEUE_MAX_TIMEOUT);
  2556. }
  2557. return p_buf;
  2558. }
  2559. /*******************************************************************************
  2560. **
  2561. ** Function gatt_update_listen_mode
  2562. **
  2563. ** Description update peripheral role listening mode
  2564. **
  2565. ** Returns Pointer to the new service start buffer, NULL no buffer available
  2566. **
  2567. *******************************************************************************/
  2568. BOOLEAN gatt_update_listen_mode(void)
  2569. {
  2570. UINT8 ii = 0;
  2571. tGATT_REG *p_reg = &gatt_cb.cl_rcb[0];
  2572. UINT8 listening = 0;
  2573. UINT16 connectability, window, interval;
  2574. BOOLEAN rt = TRUE;
  2575. for (; ii < GATT_MAX_APPS; ii ++, p_reg ++) {
  2576. if ( p_reg->in_use && p_reg->listening > listening) {
  2577. listening = p_reg->listening;
  2578. }
  2579. }
  2580. if (listening == GATT_LISTEN_TO_ALL ||
  2581. listening == GATT_LISTEN_TO_NONE) {
  2582. BTM_BleUpdateAdvFilterPolicy (AP_SCAN_CONN_ALL);
  2583. } else {
  2584. BTM_BleUpdateAdvFilterPolicy (AP_SCAN_CONN_WL);
  2585. }
  2586. if (rt) {
  2587. connectability = BTM_ReadConnectability (&window, &interval);
  2588. if (listening != GATT_LISTEN_TO_NONE) {
  2589. connectability |= BTM_BLE_CONNECTABLE;
  2590. } else {
  2591. if ((connectability & BTM_BLE_CONNECTABLE) == 0) {
  2592. connectability &= ~BTM_BLE_CONNECTABLE;
  2593. }
  2594. }
  2595. /* turning on the adv now */
  2596. btm_ble_set_connectability(connectability);
  2597. }
  2598. return rt;
  2599. }
  2600. #endif