gatt_utils.c 92 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892
  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_find_i_tcb_by_addr
  808. **
  809. ** Description The function searches for an empty tcb entry, and return the index.
  810. **
  811. ** Returns GATT_INDEX_INVALID if not found. Otherwise index to the tcb.
  812. **
  813. *******************************************************************************/
  814. UINT8 gatt_find_i_tcb_by_addr(BD_ADDR bda, tBT_TRANSPORT transport)
  815. {
  816. UINT8 i = 0;
  817. list_node_t *p_node = NULL;
  818. tGATT_TCB *p_tcb = NULL;
  819. for(p_node = list_begin(gatt_cb.p_tcb_list); p_node; p_node = list_next(p_node)) {
  820. p_tcb = list_node(p_node);
  821. if (!memcmp(p_tcb->peer_bda, bda, BD_ADDR_LEN) &&
  822. p_tcb->transport == transport) {
  823. i = p_tcb->tcb_idx;
  824. return i;
  825. }
  826. }
  827. return GATT_INDEX_INVALID;
  828. }
  829. /*******************************************************************************
  830. **
  831. ** Function gatt_get_tcb_by_idx
  832. **
  833. ** Description The function get TCB using the TCB index
  834. **
  835. ** Returns NULL if not found. Otherwise index to the tcb.
  836. **
  837. *******************************************************************************/
  838. tGATT_TCB *gatt_get_tcb_by_idx(UINT8 tcb_idx)
  839. {
  840. tGATT_TCB *p_tcb = NULL;
  841. list_node_t *p_node = NULL;
  842. for(p_node = list_begin(gatt_cb.p_tcb_list); p_node; p_node = list_next(p_node)) {
  843. p_tcb = list_node(p_node);
  844. if ( (tcb_idx < GATT_MAX_PHY_CHANNEL) && p_tcb->in_use && p_tcb->tcb_idx == tcb_idx ) {
  845. break;
  846. } else {
  847. p_tcb = NULL;
  848. }
  849. }
  850. return p_tcb;
  851. }
  852. /*******************************************************************************
  853. **
  854. ** Function gatt_find_tcb_by_addr
  855. **
  856. ** Description The function searches for an empty tcb entry, and return pointer.
  857. **
  858. ** Returns NULL if not found. Otherwise index to the tcb.
  859. **
  860. *******************************************************************************/
  861. tGATT_TCB *gatt_find_tcb_by_addr(BD_ADDR bda, tBT_TRANSPORT transport)
  862. {
  863. tGATT_TCB *p_tcb = NULL;
  864. UINT8 i = 0;
  865. if ((i = gatt_find_i_tcb_by_addr(bda, transport)) != GATT_INDEX_INVALID) {
  866. p_tcb = gatt_get_tcb_by_idx(i);
  867. }
  868. return p_tcb;
  869. }
  870. /*******************************************************************************
  871. **
  872. ** Function gatt_find_i_tcb_free
  873. **
  874. ** Description The function searches for an empty tcb entry, and return the index.
  875. **
  876. ** Returns GATT_INDEX_INVALID if not found. Otherwise index to the tcb.
  877. **
  878. *******************************************************************************/
  879. UINT8 gatt_find_i_tcb_free(void)
  880. {
  881. UINT8 i = 0, j = GATT_INDEX_INVALID;
  882. for (i = 0; i < GATT_MAX_PHY_CHANNEL; i ++) {
  883. if (!((1 << i) & gatt_tcb_id)) {
  884. j = i;
  885. break;
  886. }
  887. }
  888. return j;
  889. }
  890. /*******************************************************************************
  891. **
  892. ** Function gatt_tcb_alloc
  893. **
  894. ** Description The function allocates tcb for given tcb_idx and update tcb_id
  895. **
  896. ** Returns Allocated tcb block
  897. **
  898. *******************************************************************************/
  899. tGATT_TCB *gatt_tcb_alloc(UINT8 tcb_idx)
  900. {
  901. /* Allocate tcb block */
  902. tGATT_TCB *p_tcb = (tGATT_TCB *)osi_malloc(sizeof(tGATT_TCB));
  903. if (p_tcb && list_length(gatt_cb.p_tcb_list) < GATT_MAX_PHY_CHANNEL) {
  904. memset(p_tcb, 0, sizeof(tGATT_TCB));
  905. /* Add tcb block to list in gatt_cb */
  906. list_append(gatt_cb.p_tcb_list, p_tcb);
  907. /* Update tcb id */
  908. gatt_tcb_id |= 1 << tcb_idx;
  909. } else if(p_tcb) {
  910. osi_free(p_tcb);
  911. p_tcb = NULL;
  912. }
  913. return p_tcb;
  914. }
  915. /*******************************************************************************
  916. **
  917. ** Function gatt_tcb_free
  918. **
  919. ** Description The function free the given tcb block and update tcb id
  920. **
  921. ** Returns void
  922. **
  923. *******************************************************************************/
  924. void gatt_tcb_free( tGATT_TCB *p_tcb)
  925. {
  926. UINT8 tcb_idx = p_tcb->tcb_idx;
  927. if (list_remove(gatt_cb.p_tcb_list, p_tcb)) {
  928. gatt_tcb_id &= ~(1 << tcb_idx);
  929. }
  930. }
  931. /*******************************************************************************
  932. **
  933. ** Function gatt_allocate_tcb_by_bdaddr
  934. **
  935. ** Description The function locate or allocate new tcb entry for matching bda.
  936. **
  937. ** Returns GATT_INDEX_INVALID if not found. Otherwise index to the tcb.
  938. **
  939. *******************************************************************************/
  940. tGATT_TCB *gatt_allocate_tcb_by_bdaddr(BD_ADDR bda, tBT_TRANSPORT transport)
  941. {
  942. UINT8 i = 0;
  943. BOOLEAN allocated = FALSE;
  944. tGATT_TCB *p_tcb = NULL;
  945. /* search for existing tcb with matching bda */
  946. i = gatt_find_i_tcb_by_addr(bda, transport);
  947. /* find free tcb */
  948. if (i == GATT_INDEX_INVALID) {
  949. i = gatt_find_i_tcb_free();
  950. allocated = TRUE;
  951. }
  952. if (i != GATT_INDEX_INVALID) {
  953. p_tcb = gatt_tcb_alloc(i);
  954. if (!p_tcb) {
  955. return NULL;
  956. }
  957. if (allocated) {
  958. memset(p_tcb, 0, sizeof(tGATT_TCB));
  959. p_tcb->pending_enc_clcb = fixed_queue_new(QUEUE_SIZE_MAX);
  960. p_tcb->pending_ind_q = fixed_queue_new(QUEUE_SIZE_MAX);
  961. p_tcb->in_use = TRUE;
  962. p_tcb->tcb_idx = i;
  963. p_tcb->transport = transport;
  964. }
  965. memcpy(p_tcb->peer_bda, bda, BD_ADDR_LEN);
  966. }
  967. return p_tcb;
  968. }
  969. /*******************************************************************************
  970. **
  971. ** Function gatt_convert_uuid16_to_uuid128
  972. **
  973. ** Description Convert a 16 bits UUID to be an standard 128 bits one.
  974. **
  975. ** Returns TRUE if two uuid match; FALSE otherwise.
  976. **
  977. *******************************************************************************/
  978. void gatt_convert_uuid16_to_uuid128(UINT8 uuid_128[LEN_UUID_128], UINT16 uuid_16)
  979. {
  980. UINT8 *p = &uuid_128[LEN_UUID_128 - 4];
  981. memcpy (uuid_128, base_uuid, LEN_UUID_128);
  982. UINT16_TO_STREAM(p, uuid_16);
  983. }
  984. /*******************************************************************************
  985. **
  986. ** Function gatt_convert_uuid32_to_uuid128
  987. **
  988. ** Description Convert a 32 bits UUID to be an standard 128 bits one.
  989. **
  990. ** Returns TRUE if two uuid match; FALSE otherwise.
  991. **
  992. *******************************************************************************/
  993. void gatt_convert_uuid32_to_uuid128(UINT8 uuid_128[LEN_UUID_128], UINT32 uuid_32)
  994. {
  995. UINT8 *p = &uuid_128[LEN_UUID_128 - 4];
  996. memcpy (uuid_128, base_uuid, LEN_UUID_128);
  997. UINT32_TO_STREAM(p, uuid_32);
  998. }
  999. /*******************************************************************************
  1000. **
  1001. ** Function gatt_uuid_compare
  1002. **
  1003. ** Description Compare two UUID to see if they are the same.
  1004. **
  1005. ** Returns TRUE if two uuid match; FALSE otherwise.
  1006. **
  1007. *******************************************************************************/
  1008. BOOLEAN gatt_uuid_compare (tBT_UUID src, tBT_UUID tar)
  1009. {
  1010. UINT8 su[LEN_UUID_128], tu[LEN_UUID_128];
  1011. UINT8 *ps, *pt;
  1012. /* any of the UUID is unspecified */
  1013. if (src.len == 0 || tar.len == 0) {
  1014. return TRUE;
  1015. }
  1016. /* If both are 16-bit, we can do a simple compare */
  1017. if (src.len == LEN_UUID_16 && tar.len == LEN_UUID_16) {
  1018. return src.uu.uuid16 == tar.uu.uuid16;
  1019. }
  1020. /* If both are 32-bit, we can do a simple compare */
  1021. if (src.len == LEN_UUID_32 && tar.len == LEN_UUID_32) {
  1022. return src.uu.uuid32 == tar.uu.uuid32;
  1023. }
  1024. /* One or both of the UUIDs is 128-bit */
  1025. if (src.len == LEN_UUID_16) {
  1026. /* convert a 16 bits UUID to 128 bits value */
  1027. gatt_convert_uuid16_to_uuid128(su, src.uu.uuid16);
  1028. ps = su;
  1029. } else if (src.len == LEN_UUID_32) {
  1030. gatt_convert_uuid32_to_uuid128(su, src.uu.uuid32);
  1031. ps = su;
  1032. } else {
  1033. ps = src.uu.uuid128;
  1034. }
  1035. if (tar.len == LEN_UUID_16) {
  1036. /* convert a 16 bits UUID to 128 bits value */
  1037. gatt_convert_uuid16_to_uuid128(tu, tar.uu.uuid16);
  1038. pt = tu;
  1039. } else if (tar.len == LEN_UUID_32) {
  1040. /* convert a 32 bits UUID to 128 bits value */
  1041. gatt_convert_uuid32_to_uuid128(tu, tar.uu.uuid32);
  1042. pt = tu;
  1043. } else {
  1044. pt = tar.uu.uuid128;
  1045. }
  1046. return (memcmp(ps, pt, LEN_UUID_128) == 0);
  1047. }
  1048. /*******************************************************************************
  1049. **
  1050. ** Function gatt_build_uuid_to_stream
  1051. **
  1052. ** Description Add UUID into stream.
  1053. **
  1054. ** Returns UUID length.
  1055. **
  1056. *******************************************************************************/
  1057. UINT8 gatt_build_uuid_to_stream(UINT8 **p_dst, tBT_UUID uuid)
  1058. {
  1059. UINT8 *p = *p_dst;
  1060. UINT8 len = 0;
  1061. if (uuid.len == LEN_UUID_16) {
  1062. UINT16_TO_STREAM (p, uuid.uu.uuid16);
  1063. len = LEN_UUID_16;
  1064. } else if (uuid.len == LEN_UUID_32) { /* always convert 32 bits into 128 bits as alwats */
  1065. gatt_convert_uuid32_to_uuid128(p, uuid.uu.uuid32);
  1066. p += LEN_UUID_128;
  1067. len = LEN_UUID_128;
  1068. } else if (uuid.len == LEN_UUID_128) {
  1069. ARRAY_TO_STREAM (p, uuid.uu.uuid128, LEN_UUID_128);
  1070. len = LEN_UUID_128;
  1071. }
  1072. *p_dst = p;
  1073. return len;
  1074. }
  1075. /*******************************************************************************
  1076. **
  1077. ** Function gatt_parse_uuid_from_cmd
  1078. **
  1079. ** Description Convert a 128 bits UUID into a 16 bits UUID.
  1080. **
  1081. ** Returns TRUE if command sent, otherwise FALSE.
  1082. **
  1083. *******************************************************************************/
  1084. BOOLEAN gatt_parse_uuid_from_cmd(tBT_UUID *p_uuid_rec, UINT16 uuid_size, UINT8 **p_data)
  1085. {
  1086. BOOLEAN is_base_uuid, ret = TRUE;
  1087. UINT8 xx;
  1088. UINT8 *p_uuid = *p_data;
  1089. memset(p_uuid_rec, 0, sizeof(tBT_UUID));
  1090. switch (uuid_size) {
  1091. case LEN_UUID_16:
  1092. p_uuid_rec->len = uuid_size;
  1093. STREAM_TO_UINT16 (p_uuid_rec->uu.uuid16, p_uuid);
  1094. *p_data += LEN_UUID_16;
  1095. break;
  1096. case LEN_UUID_128:
  1097. /* See if we can compress his UUID down to 16 or 32bit UUIDs */
  1098. is_base_uuid = TRUE;
  1099. for (xx = 0; xx < LEN_UUID_128 - 4; xx++) {
  1100. if (p_uuid[xx] != base_uuid[xx]) {
  1101. is_base_uuid = FALSE;
  1102. break;
  1103. }
  1104. }
  1105. if (is_base_uuid) {
  1106. if ((p_uuid[LEN_UUID_128 - 1] == 0) && (p_uuid[LEN_UUID_128 - 2] == 0)) {
  1107. p_uuid += (LEN_UUID_128 - 4);
  1108. p_uuid_rec->len = LEN_UUID_16;
  1109. STREAM_TO_UINT16(p_uuid_rec->uu.uuid16, p_uuid);
  1110. } else {
  1111. p_uuid += (LEN_UUID_128 - LEN_UUID_32);
  1112. p_uuid_rec->len = LEN_UUID_32;
  1113. STREAM_TO_UINT32(p_uuid_rec->uu.uuid32, p_uuid);
  1114. }
  1115. }
  1116. if (!is_base_uuid) {
  1117. p_uuid_rec->len = LEN_UUID_128;
  1118. memcpy(p_uuid_rec->uu.uuid128, p_uuid, LEN_UUID_128);
  1119. }
  1120. *p_data += LEN_UUID_128;
  1121. break;
  1122. /* do not allow 32 bits UUID in ATT PDU now */
  1123. case LEN_UUID_32:
  1124. GATT_TRACE_ERROR("DO NOT ALLOW 32 BITS UUID IN ATT PDU");
  1125. case 0:
  1126. default:
  1127. if (uuid_size != 0) {
  1128. ret = FALSE;
  1129. }
  1130. GATT_TRACE_WARNING("gatt_parse_uuid_from_cmd invalid uuid size");
  1131. break;
  1132. }
  1133. return ( ret);
  1134. }
  1135. /*******************************************************************************
  1136. **
  1137. ** Function gatt_start_rsp_timer
  1138. **
  1139. ** Description Start a wait_for_response timer.
  1140. **
  1141. ** Returns TRUE if command sent, otherwise FALSE.
  1142. **
  1143. *******************************************************************************/
  1144. void gatt_start_rsp_timer(UINT16 clcb_idx)
  1145. {
  1146. tGATT_CLCB *p_clcb = gatt_clcb_find_by_idx(clcb_idx);
  1147. UINT32 timeout = GATT_WAIT_FOR_RSP_TOUT;
  1148. p_clcb->rsp_timer_ent.param = (TIMER_PARAM_TYPE)p_clcb;
  1149. if (p_clcb->operation == GATTC_OPTYPE_DISCOVERY &&
  1150. p_clcb->op_subtype == GATT_DISC_SRVC_ALL) {
  1151. timeout = GATT_WAIT_FOR_DISC_RSP_TOUT;
  1152. }
  1153. btu_start_timer (&p_clcb->rsp_timer_ent, BTU_TTYPE_ATT_WAIT_FOR_RSP,
  1154. timeout);
  1155. }
  1156. /*******************************************************************************
  1157. **
  1158. ** Function gatt_start_conf_timer
  1159. **
  1160. ** Description Start a wait_for_confirmation timer.
  1161. **
  1162. ** Returns TRUE if command sent, otherwise FALSE.
  1163. **
  1164. *******************************************************************************/
  1165. void gatt_start_conf_timer(tGATT_TCB *p_tcb)
  1166. {
  1167. p_tcb->conf_timer_ent.param = (TIMER_PARAM_TYPE)p_tcb;
  1168. btu_start_timer (&p_tcb->conf_timer_ent, BTU_TTYPE_ATT_WAIT_FOR_RSP,
  1169. GATT_WAIT_FOR_RSP_TOUT);
  1170. }
  1171. /*******************************************************************************
  1172. **
  1173. ** Function gatt_start_ind_ack_timer
  1174. **
  1175. ** Description start the application ack timer
  1176. **
  1177. ** Returns void
  1178. **
  1179. *******************************************************************************/
  1180. void gatt_start_ind_ack_timer(tGATT_TCB *p_tcb)
  1181. {
  1182. p_tcb->ind_ack_timer_ent.param = (TIMER_PARAM_TYPE)p_tcb;
  1183. /* start notification cache timer */
  1184. btu_start_timer (&p_tcb->ind_ack_timer_ent, BTU_TTYPE_ATT_WAIT_FOR_IND_ACK,
  1185. GATT_WAIT_FOR_IND_ACK_TOUT);
  1186. }
  1187. /*******************************************************************************
  1188. **
  1189. ** Function gatt_rsp_timeout
  1190. **
  1191. ** Description Called when GATT wait for ATT command response timer expires
  1192. **
  1193. ** Returns void
  1194. **
  1195. *******************************************************************************/
  1196. void gatt_rsp_timeout(TIMER_LIST_ENT *p_tle)
  1197. {
  1198. tGATT_CLCB *p_clcb = (tGATT_CLCB *)p_tle->param;
  1199. if (p_clcb == NULL || p_clcb->p_tcb == NULL) {
  1200. GATT_TRACE_WARNING("gatt_rsp_timeout clcb is already deleted");
  1201. return;
  1202. }
  1203. if (p_clcb->operation == GATTC_OPTYPE_DISCOVERY &&
  1204. p_clcb->op_subtype == GATT_DISC_SRVC_ALL &&
  1205. p_clcb->retry_count < GATT_REQ_RETRY_LIMIT) {
  1206. UINT8 rsp_code;
  1207. GATT_TRACE_WARNING("gatt_rsp_timeout retry discovery primary service");
  1208. if (p_clcb != gatt_cmd_dequeue(p_clcb->p_tcb, &rsp_code)) {
  1209. GATT_TRACE_ERROR("gatt_rsp_timeout command queue out of sync, disconnect");
  1210. } else {
  1211. p_clcb->retry_count++;
  1212. #if (GATTC_INCLUDED == TRUE)
  1213. gatt_act_discovery(p_clcb);
  1214. #endif ///GATTC_INCLUDED == TRUE
  1215. return;
  1216. }
  1217. }
  1218. GATT_TRACE_WARNING("gatt_rsp_timeout disconnecting...");
  1219. gatt_disconnect (p_clcb->p_tcb);
  1220. }
  1221. /*******************************************************************************
  1222. **
  1223. ** Function gatt_ind_ack_timeout
  1224. **
  1225. ** Description Called when GATT wait for ATT handle confirmation timeout
  1226. **
  1227. ** Returns void
  1228. **
  1229. *******************************************************************************/
  1230. void gatt_ind_ack_timeout(TIMER_LIST_ENT *p_tle)
  1231. {
  1232. tGATT_TCB *p_tcb = (tGATT_TCB *)p_tle->param;
  1233. GATT_TRACE_WARNING("gatt_ind_ack_timeout send ack now");
  1234. if (p_tcb != NULL) {
  1235. p_tcb->ind_count = 0;
  1236. }
  1237. attp_send_cl_msg(((tGATT_TCB *)p_tle->param), 0, GATT_HANDLE_VALUE_CONF, NULL);
  1238. }
  1239. /*******************************************************************************
  1240. **
  1241. ** Function gatt_sr_find_i_rcb_by_handle
  1242. **
  1243. ** Description The function searches for a service that owns a specific handle.
  1244. **
  1245. ** Returns GATT_MAX_SR_PROFILES if not found. Otherwise index of th eservice.
  1246. **
  1247. *******************************************************************************/
  1248. UINT8 gatt_sr_find_i_rcb_by_handle(UINT16 handle)
  1249. {
  1250. UINT8 i_rcb = 0;
  1251. for ( ; i_rcb < GATT_MAX_SR_PROFILES; i_rcb++) {
  1252. if (gatt_cb.sr_reg[i_rcb].in_use &&
  1253. gatt_cb.sr_reg[i_rcb].s_hdl <= handle &&
  1254. gatt_cb.sr_reg[i_rcb].e_hdl >= handle ) {
  1255. break;
  1256. }
  1257. }
  1258. return i_rcb;
  1259. }
  1260. /*******************************************************************************
  1261. **
  1262. ** Function gatt_sr_find_i_rcb_by_handle
  1263. **
  1264. ** Description The function searches for a service that owns a specific handle.
  1265. **
  1266. ** Returns 0 if not found. Otherwise index of th eservice.
  1267. **
  1268. *******************************************************************************/
  1269. #if (GATTS_INCLUDED == TRUE)
  1270. UINT8 gatt_sr_find_i_rcb_by_app_id(tBT_UUID *p_app_uuid128, tBT_UUID *p_svc_uuid, UINT16 svc_inst)
  1271. {
  1272. UINT8 i_rcb = 0;
  1273. tGATT_SR_REG *p_sreg;
  1274. tBT_UUID *p_this_uuid;
  1275. for (i_rcb = 0, p_sreg = gatt_cb.sr_reg; i_rcb < GATT_MAX_SR_PROFILES; i_rcb++, p_sreg++) {
  1276. if ( p_sreg->in_use ) {
  1277. p_this_uuid = gatts_get_service_uuid (p_sreg->p_db);
  1278. if (p_this_uuid &&
  1279. gatt_uuid_compare (*p_app_uuid128, p_sreg->app_uuid ) &&
  1280. gatt_uuid_compare (*p_svc_uuid, *p_this_uuid) &&
  1281. (svc_inst == p_sreg->service_instance)) {
  1282. GATT_TRACE_ERROR ("Active Service Found ");
  1283. gatt_dbg_display_uuid(*p_svc_uuid);
  1284. break;
  1285. }
  1286. }
  1287. }
  1288. return i_rcb;
  1289. }
  1290. #endif ///GATTS_INCLUDED == TRUE
  1291. /*******************************************************************************
  1292. **
  1293. ** Function gatt_sr_find_i_rcb_by_handle
  1294. **
  1295. ** Description The function searches for a service that owns a specific handle.
  1296. **
  1297. ** Returns 0 if not found. Otherwise index of th eservice.
  1298. **
  1299. *******************************************************************************/
  1300. UINT8 gatt_sr_alloc_rcb(tGATT_HDL_LIST_ELEM *p_list )
  1301. {
  1302. UINT8 ii = 0;
  1303. tGATT_SR_REG *p_sreg = NULL;
  1304. /*this is a new application servoce start */
  1305. for (ii = 0, p_sreg = gatt_cb.sr_reg; ii < GATT_MAX_SR_PROFILES; ii++, p_sreg++) {
  1306. if (!p_sreg->in_use) {
  1307. memset (p_sreg, 0, sizeof(tGATT_SR_REG));
  1308. p_sreg->in_use = TRUE;
  1309. memcpy (&p_sreg->app_uuid, &p_list->asgn_range.app_uuid128, sizeof(tBT_UUID));
  1310. p_sreg->service_instance = p_list->asgn_range.svc_inst;
  1311. p_sreg->type = p_list->asgn_range.is_primary ? GATT_UUID_PRI_SERVICE : GATT_UUID_SEC_SERVICE;
  1312. p_sreg->s_hdl = p_list->asgn_range.s_handle;
  1313. p_sreg->e_hdl = p_list->asgn_range.e_handle;
  1314. p_sreg->p_db = &p_list->svc_db;
  1315. GATT_TRACE_DEBUG ("total buffer in db [%d]", fixed_queue_length(p_sreg->p_db->svc_buffer));
  1316. break;
  1317. }
  1318. }
  1319. return ii;
  1320. }
  1321. /*******************************************************************************
  1322. **
  1323. ** Function gatt_sr_get_sec_info
  1324. **
  1325. ** Description Get the security flag and key size information for the peer
  1326. ** device.
  1327. **
  1328. ** Returns void
  1329. **
  1330. *******************************************************************************/
  1331. void gatt_sr_get_sec_info(BD_ADDR rem_bda, tBT_TRANSPORT transport, UINT8 *p_sec_flag, UINT8 *p_key_size)
  1332. {
  1333. UINT8 sec_flag = 0;
  1334. BTM_GetSecurityFlagsByTransport(rem_bda, &sec_flag, transport);
  1335. sec_flag &= (GATT_SEC_FLAG_LKEY_UNAUTHED | GATT_SEC_FLAG_LKEY_AUTHED | GATT_SEC_FLAG_ENCRYPTED | GATT_SEC_FLAG_AUTHORIZATION);
  1336. #if (SMP_INCLUDED == TRUE)
  1337. *p_key_size = btm_ble_read_sec_key_size(rem_bda);
  1338. #endif ///SMP_INCLUDED == TRUE
  1339. *p_sec_flag = sec_flag;
  1340. }
  1341. /*******************************************************************************
  1342. **
  1343. ** Function gatt_sr_send_req_callback
  1344. **
  1345. ** Description
  1346. **
  1347. **
  1348. ** Returns void
  1349. **
  1350. *******************************************************************************/
  1351. void gatt_sr_send_req_callback(UINT16 conn_id,
  1352. UINT32 trans_id,
  1353. tGATTS_REQ_TYPE type, tGATTS_DATA *p_data)
  1354. {
  1355. tGATT_IF gatt_if = GATT_GET_GATT_IF(conn_id);
  1356. tGATT_REG *p_reg = gatt_get_regcb(gatt_if);
  1357. if (!p_reg ) {
  1358. GATT_TRACE_ERROR ("p_reg not found discard request");
  1359. return;
  1360. }
  1361. if ( p_reg->in_use &&
  1362. p_reg->app_cb.p_req_cb) {
  1363. (*p_reg->app_cb.p_req_cb)(conn_id, trans_id, type, p_data);
  1364. } else {
  1365. GATT_TRACE_WARNING("Call back not found for application conn_id=%d", conn_id);
  1366. }
  1367. }
  1368. /*******************************************************************************
  1369. **
  1370. ** Function gatt_send_error_rsp
  1371. **
  1372. ** Description This function sends an error response.
  1373. **
  1374. ** Returns void
  1375. **
  1376. *******************************************************************************/
  1377. tGATT_STATUS gatt_send_error_rsp (tGATT_TCB *p_tcb, UINT8 err_code, UINT8 op_code,
  1378. UINT16 handle, BOOLEAN deq)
  1379. {
  1380. tGATT_ERROR error;
  1381. tGATT_STATUS status;
  1382. BT_HDR *p_buf;
  1383. error.cmd_code = op_code;
  1384. error.reason = err_code;
  1385. error.handle = handle;
  1386. if ((p_buf = attp_build_sr_msg(p_tcb, GATT_RSP_ERROR, (tGATT_SR_MSG *)&error)) != NULL) {
  1387. status = attp_send_sr_msg (p_tcb, p_buf);
  1388. } else {
  1389. status = GATT_INSUF_RESOURCE;
  1390. }
  1391. #if (GATTS_INCLUDED == TRUE)
  1392. if (deq) {
  1393. gatt_dequeue_sr_cmd(p_tcb);
  1394. }
  1395. #endif ///GATTS_INCLUDED == TRUE
  1396. return status;
  1397. }
  1398. #if (SDP_INCLUDED == TRUE && CLASSIC_BT_GATT_INCLUDED == TRUE)
  1399. /*******************************************************************************
  1400. **
  1401. ** Function gatt_add_sdp_record
  1402. **
  1403. ** Description This function add a SDP record for a GATT primary service
  1404. **
  1405. ** Returns 0 if error else sdp handle for the record.
  1406. **
  1407. *******************************************************************************/
  1408. UINT32 gatt_add_sdp_record (tBT_UUID *p_uuid, UINT16 start_hdl, UINT16 end_hdl)
  1409. {
  1410. tSDP_PROTOCOL_ELEM proto_elem_list[2];
  1411. UINT32 sdp_handle;
  1412. UINT16 list = UUID_SERVCLASS_PUBLIC_BROWSE_GROUP;
  1413. UINT8 buff[60];
  1414. UINT8 *p = buff;
  1415. GATT_TRACE_DEBUG("gatt_add_sdp_record s_hdl=0x%x s_hdl=0x%x", start_hdl, end_hdl);
  1416. if ((sdp_handle = SDP_CreateRecord()) == 0) {
  1417. return 0;
  1418. }
  1419. switch (p_uuid->len) {
  1420. case LEN_UUID_16:
  1421. SDP_AddServiceClassIdList(sdp_handle, 1, &p_uuid->uu.uuid16);
  1422. break;
  1423. case LEN_UUID_32:
  1424. UINT8_TO_BE_STREAM (p, (UUID_DESC_TYPE << 3) | SIZE_FOUR_BYTES);
  1425. UINT32_TO_BE_STREAM (p, p_uuid->uu.uuid32);
  1426. SDP_AddAttribute (sdp_handle, ATTR_ID_SERVICE_CLASS_ID_LIST, DATA_ELE_SEQ_DESC_TYPE,
  1427. (UINT32) (p - buff), buff);
  1428. break;
  1429. case LEN_UUID_128:
  1430. UINT8_TO_BE_STREAM (p, (UUID_DESC_TYPE << 3) | SIZE_SIXTEEN_BYTES);
  1431. ARRAY_TO_BE_STREAM_REVERSE (p, p_uuid->uu.uuid128, LEN_UUID_128);
  1432. SDP_AddAttribute (sdp_handle, ATTR_ID_SERVICE_CLASS_ID_LIST, DATA_ELE_SEQ_DESC_TYPE,
  1433. (UINT32) (p - buff), buff);
  1434. break;
  1435. default:
  1436. GATT_TRACE_ERROR("inavlid UUID len=%d", p_uuid->len);
  1437. SDP_DeleteRecord(sdp_handle);
  1438. return 0;
  1439. break;
  1440. }
  1441. /*** Fill out the protocol element sequence for SDP ***/
  1442. proto_elem_list[0].protocol_uuid = UUID_PROTOCOL_L2CAP;
  1443. proto_elem_list[0].num_params = 1;
  1444. proto_elem_list[0].params[0] = BT_PSM_ATT;
  1445. proto_elem_list[1].protocol_uuid = UUID_PROTOCOL_ATT;
  1446. proto_elem_list[1].num_params = 2;
  1447. proto_elem_list[1].params[0] = start_hdl;
  1448. proto_elem_list[1].params[1] = end_hdl;
  1449. SDP_AddProtocolList(sdp_handle, 2, proto_elem_list);
  1450. /* Make the service browseable */
  1451. SDP_AddUuidSequence (sdp_handle, ATTR_ID_BROWSE_GROUP_LIST, 1, &list);
  1452. return (sdp_handle);
  1453. }
  1454. #endif ///SDP_INCLUDED == TRUE && CLASSIC_BT_GATT_INCLUDED == TRUE
  1455. #if GATT_CONFORMANCE_TESTING == TRUE
  1456. /*******************************************************************************
  1457. **
  1458. ** Function gatt_set_err_rsp
  1459. **
  1460. ** Description This function is called to set the test confirm value
  1461. **
  1462. ** Returns void
  1463. **
  1464. *******************************************************************************/
  1465. void gatt_set_err_rsp(BOOLEAN enable, UINT8 req_op_code, UINT8 err_status)
  1466. {
  1467. GATT_TRACE_DEBUG("gatt_set_err_rsp enable=%d op_code=%d, err_status=%d", enable, req_op_code, err_status);
  1468. gatt_cb.enable_err_rsp = enable;
  1469. gatt_cb.req_op_code = req_op_code;
  1470. gatt_cb.err_status = err_status;
  1471. }
  1472. #endif
  1473. /*******************************************************************************
  1474. **
  1475. ** Function gatt_get_regcb
  1476. **
  1477. ** Description The function returns the registration control block.
  1478. **
  1479. ** Returns pointer to the registration control block or NULL
  1480. **
  1481. *******************************************************************************/
  1482. tGATT_REG *gatt_get_regcb (tGATT_IF gatt_if)
  1483. {
  1484. UINT8 ii = (UINT8)gatt_if;
  1485. tGATT_REG *p_reg = NULL;
  1486. if (ii < 1 || ii > GATT_MAX_APPS) {
  1487. GATT_TRACE_WARNING("gatt_if out of range [ = %d]", ii);
  1488. return NULL;
  1489. }
  1490. // Index for cl_rcb is always 1 less than gatt_if.
  1491. p_reg = &gatt_cb.cl_rcb[ii - 1];
  1492. if (!p_reg->in_use) {
  1493. GATT_TRACE_WARNING("gatt_if found but not in use.\n");
  1494. return NULL;
  1495. }
  1496. return p_reg;
  1497. }
  1498. /*******************************************************************************
  1499. **
  1500. ** Function gatt_is_clcb_allocated
  1501. **
  1502. ** Description The function check clcb for conn_id is allocated or not
  1503. **
  1504. ** Returns True already allocated
  1505. **
  1506. *******************************************************************************/
  1507. BOOLEAN gatt_is_clcb_allocated (UINT16 conn_id)
  1508. {
  1509. BOOLEAN is_allocated = FALSE;
  1510. tGATT_CLCB *p_clcb = gatt_clcb_find_by_conn_id(conn_id);
  1511. if (p_clcb) {
  1512. is_allocated = TRUE;
  1513. }
  1514. return is_allocated;
  1515. }
  1516. /*******************************************************************************
  1517. **
  1518. ** Function gatt_clcb_find_by_conn_id
  1519. **
  1520. ** Description Find clcb block using clcb_idx stored at the time of alloc
  1521. **
  1522. ** Returns pointer to clcb corresponding to conn_id
  1523. **
  1524. *******************************************************************************/
  1525. tGATT_CLCB *gatt_clcb_find_by_conn_id(UINT16 conn_id)
  1526. {
  1527. list_node_t *p_node = NULL;
  1528. tGATT_CLCB *p_clcb = NULL;
  1529. tGATT_CLCB *p_clcb_ret = NULL;
  1530. for(p_node = list_begin(gatt_cb.p_clcb_list); p_node; p_node = list_next(p_node)) {
  1531. p_clcb = list_node(p_node);
  1532. if (p_clcb->conn_id == conn_id) {
  1533. p_clcb_ret = p_clcb;
  1534. break;
  1535. }
  1536. }
  1537. return p_clcb_ret;
  1538. }
  1539. /*******************************************************************************
  1540. **
  1541. ** Function gatt_clcb_find_by_idx
  1542. **
  1543. ** Description Find clcb block using clcb_idx stored at the time of alloc
  1544. **
  1545. ** Returns pointer to clcb corresponding to clcb_idx
  1546. **
  1547. *******************************************************************************/
  1548. tGATT_CLCB *gatt_clcb_find_by_idx(UINT16 clcb_idx)
  1549. {
  1550. list_node_t *p_node = NULL;
  1551. tGATT_CLCB *p_clcb = NULL;
  1552. tGATT_CLCB *p_clcb_ret = NULL;
  1553. for(p_node = list_begin(gatt_cb.p_clcb_list); p_node; p_node = list_next(p_node)) {
  1554. p_clcb = list_node(p_node);
  1555. if (p_clcb->clcb_idx == clcb_idx) {
  1556. p_clcb_ret = p_clcb;
  1557. break;
  1558. }
  1559. }
  1560. return p_clcb_ret;
  1561. }
  1562. /*******************************************************************************
  1563. **
  1564. ** Function gatt_clcb_alloc
  1565. **
  1566. ** Description The function allocates a GATT connection link control block
  1567. **
  1568. ** Returns NULL if not found. Otherwise pointer to the connection link block.
  1569. **
  1570. *******************************************************************************/
  1571. tGATT_CLCB *gatt_clcb_alloc (UINT16 conn_id)
  1572. {
  1573. tGATT_CLCB *p_clcb = NULL;
  1574. tGATT_IF gatt_if = GATT_GET_GATT_IF(conn_id);
  1575. UINT8 tcb_idx = GATT_GET_TCB_IDX(conn_id);
  1576. tGATT_TCB *p_tcb = gatt_get_tcb_by_idx(tcb_idx);
  1577. tGATT_REG *p_reg = gatt_get_regcb(gatt_if);
  1578. if (list_length(gatt_cb.p_clcb_list) < GATT_CL_MAX_LCB) {
  1579. p_clcb = (tGATT_CLCB *)osi_malloc(sizeof(tGATT_CLCB));
  1580. if (p_clcb) {
  1581. list_append(gatt_cb.p_clcb_list, p_clcb);
  1582. memset(p_clcb, 0, sizeof(tGATT_CLCB));
  1583. p_clcb->in_use = TRUE;
  1584. p_clcb->conn_id = conn_id;
  1585. //Add index of the clcb same as conn_id
  1586. p_clcb->clcb_idx = conn_id;
  1587. p_clcb->p_reg = p_reg;
  1588. p_clcb->p_tcb = p_tcb;
  1589. }
  1590. }
  1591. return p_clcb;
  1592. }
  1593. /*******************************************************************************
  1594. **
  1595. ** Function gatt_clcb_dealloc
  1596. **
  1597. ** Description The function de allocates a GATT connection link control block
  1598. **
  1599. ** Returns None
  1600. **
  1601. *******************************************************************************/
  1602. void gatt_clcb_dealloc (tGATT_CLCB *p_clcb)
  1603. {
  1604. if (p_clcb && p_clcb->in_use) {
  1605. btu_free_timer(&p_clcb->rsp_timer_ent);
  1606. memset(p_clcb, 0, sizeof(tGATT_CLCB));
  1607. list_remove(gatt_cb.p_clcb_list, p_clcb);
  1608. p_clcb = NULL;
  1609. }
  1610. }
  1611. /*******************************************************************************
  1612. **
  1613. ** Function gatt_find_tcb_by_cid
  1614. **
  1615. ** Description The function searches for an empty entry
  1616. ** in registration info table for GATT client
  1617. **
  1618. ** Returns NULL if not found. Otherwise pointer to the rcb.
  1619. **
  1620. *******************************************************************************/
  1621. tGATT_TCB *gatt_find_tcb_by_cid (UINT16 lcid)
  1622. {
  1623. tGATT_TCB *p_tcb = NULL;
  1624. list_node_t *p_node = NULL;
  1625. for(p_node = list_begin(gatt_cb.p_tcb_list); p_node; p_node = list_next(p_node)) {
  1626. p_tcb = list_node(p_node);
  1627. if (p_tcb->in_use && p_tcb->att_lcid == lcid) {
  1628. break;
  1629. }
  1630. }
  1631. return p_tcb;
  1632. }
  1633. /*******************************************************************************
  1634. **
  1635. ** Function gatt_num_apps_hold_link
  1636. **
  1637. ** Description The function find the number of applcaitions is holding the link
  1638. **
  1639. ** Returns total number of applications holding this acl link.
  1640. **
  1641. *******************************************************************************/
  1642. UINT8 gatt_num_apps_hold_link(tGATT_TCB *p_tcb)
  1643. {
  1644. UINT8 i, num = 0;
  1645. for (i = 0; i < GATT_MAX_APPS; i ++) {
  1646. if (p_tcb->app_hold_link[i]) {
  1647. num ++;
  1648. }
  1649. }
  1650. GATT_TRACE_DEBUG("gatt_num_apps_hold_link num=%d", num);
  1651. return num;
  1652. }
  1653. /*******************************************************************************
  1654. **
  1655. ** Function gatt_num_clcb_by_bd_addr
  1656. **
  1657. ** Description The function searches all LCB with macthing bd address
  1658. **
  1659. ** Returns total number of clcb found.
  1660. **
  1661. *******************************************************************************/
  1662. UINT8 gatt_num_clcb_by_bd_addr(BD_ADDR bda)
  1663. {
  1664. UINT8 num = 0;
  1665. list_node_t *p_node = NULL;
  1666. tGATT_CLCB *p_clcb = NULL;
  1667. for(p_node = list_begin(gatt_cb.p_clcb_list); p_node; p_node = list_next(p_node)) {
  1668. p_clcb = list_node(p_node);
  1669. if (memcmp(p_clcb->p_tcb->peer_bda, bda, BD_ADDR_LEN) == 0) {
  1670. num++;
  1671. }
  1672. }
  1673. return num;
  1674. }
  1675. /*******************************************************************************
  1676. **
  1677. ** Function gatt_sr_update_cback_cnt
  1678. **
  1679. ** Description The function searches all LCB with macthing bd address
  1680. **
  1681. ** Returns total number of clcb found.
  1682. **
  1683. *******************************************************************************/
  1684. void gatt_sr_copy_prep_cnt_to_cback_cnt(tGATT_TCB *p_tcb )
  1685. {
  1686. #if (GATTS_INCLUDED == TRUE)
  1687. UINT8 i;
  1688. if (p_tcb) {
  1689. for (i = 0; i < GATT_MAX_APPS; i ++) {
  1690. if (p_tcb->prep_cnt[i]) {
  1691. p_tcb->sr_cmd.cback_cnt[i] = 1;
  1692. }
  1693. }
  1694. }
  1695. #endif ///GATTS_INCLUDED == TRUE
  1696. }
  1697. /*******************************************************************************
  1698. **
  1699. ** Function gatt_sr_is_cback_cnt_zero
  1700. **
  1701. ** Description The function searches all LCB with macthing bd address
  1702. **
  1703. ** Returns True if thetotal application callback count is zero
  1704. **
  1705. *******************************************************************************/
  1706. BOOLEAN gatt_sr_is_cback_cnt_zero(tGATT_TCB *p_tcb )
  1707. {
  1708. BOOLEAN status = TRUE;
  1709. #if (GATTS_INCLUDED == TRUE)
  1710. UINT8 i;
  1711. if (p_tcb) {
  1712. for (i = 0; i < GATT_MAX_APPS; i ++) {
  1713. if (p_tcb->sr_cmd.cback_cnt[i]) {
  1714. status = FALSE;
  1715. break;
  1716. }
  1717. }
  1718. } else {
  1719. status = FALSE;
  1720. }
  1721. #endif ///GATTS_INCLUDED == TRUE
  1722. return status;
  1723. }
  1724. /*******************************************************************************
  1725. **
  1726. ** Function gatt_sr_is_prep_cnt_zero
  1727. **
  1728. ** Description Check the prepare write request count is zero or not
  1729. **
  1730. ** Returns True no prepare write request
  1731. **
  1732. *******************************************************************************/
  1733. BOOLEAN gatt_sr_is_prep_cnt_zero(tGATT_TCB *p_tcb)
  1734. {
  1735. BOOLEAN status = TRUE;
  1736. UINT8 i;
  1737. if (p_tcb) {
  1738. for (i = 0; i < GATT_MAX_APPS; i ++) {
  1739. if (p_tcb->prep_cnt[i]) {
  1740. status = FALSE;
  1741. break;
  1742. }
  1743. }
  1744. } else {
  1745. status = FALSE;
  1746. }
  1747. return status;
  1748. }
  1749. /*******************************************************************************
  1750. **
  1751. ** Function gatt_sr_reset_cback_cnt
  1752. **
  1753. ** Description Reset the application callback count to zero
  1754. **
  1755. ** Returns None
  1756. **
  1757. *******************************************************************************/
  1758. void gatt_sr_reset_cback_cnt(tGATT_TCB *p_tcb )
  1759. {
  1760. #if (GATTS_INCLUDED == TRUE)
  1761. UINT8 i;
  1762. if (p_tcb) {
  1763. for (i = 0; i < GATT_MAX_APPS; i ++) {
  1764. p_tcb->sr_cmd.cback_cnt[i] = 0;
  1765. }
  1766. }
  1767. #endif ///GATTS_INCLUDED == TRUE
  1768. }
  1769. /*******************************************************************************
  1770. **
  1771. ** Function gatt_sr_reset_prep_cnt
  1772. **
  1773. ** Description Reset the prep write count to zero
  1774. **
  1775. ** Returns None
  1776. **
  1777. *******************************************************************************/
  1778. void gatt_sr_reset_prep_cnt(tGATT_TCB *p_tcb )
  1779. {
  1780. UINT8 i;
  1781. if (p_tcb) {
  1782. for (i = 0; i < GATT_MAX_APPS; i ++) {
  1783. p_tcb->prep_cnt[i] = 0;
  1784. }
  1785. }
  1786. }
  1787. /*******************************************************************************
  1788. **
  1789. ** Function gatt_sr_update_cback_cnt
  1790. **
  1791. ** Description Update the teh application callback count
  1792. **
  1793. ** Returns None
  1794. **
  1795. *******************************************************************************/
  1796. void gatt_sr_update_cback_cnt(tGATT_TCB *p_tcb, tGATT_IF gatt_if, BOOLEAN is_inc, BOOLEAN is_reset_first)
  1797. {
  1798. #if (GATTS_INCLUDED == TRUE)
  1799. UINT8 idx = ((UINT8) gatt_if) - 1 ;
  1800. if (p_tcb) {
  1801. if (is_reset_first) {
  1802. gatt_sr_reset_cback_cnt(p_tcb);
  1803. }
  1804. if (is_inc) {
  1805. p_tcb->sr_cmd.cback_cnt[idx]++;
  1806. } else {
  1807. if ( p_tcb->sr_cmd.cback_cnt[idx]) {
  1808. p_tcb->sr_cmd.cback_cnt[idx]--;
  1809. }
  1810. }
  1811. }
  1812. #endif ///GATTS_INCLUDED == TRUE
  1813. }
  1814. /*******************************************************************************
  1815. **
  1816. ** Function gatt_sr_update_prep_cnt
  1817. **
  1818. ** Description Update the teh prepare write request count
  1819. **
  1820. ** Returns None
  1821. **
  1822. *******************************************************************************/
  1823. void gatt_sr_update_prep_cnt(tGATT_TCB *p_tcb, tGATT_IF gatt_if, BOOLEAN is_inc, BOOLEAN is_reset_first)
  1824. {
  1825. UINT8 idx = ((UINT8) gatt_if) - 1 ;
  1826. GATT_TRACE_DEBUG("gatt_sr_update_prep_cnt tcb idx=%d gatt_if=%d is_inc=%d is_reset_first=%d",
  1827. p_tcb->tcb_idx, gatt_if, is_inc, is_reset_first);
  1828. if (p_tcb) {
  1829. if (is_reset_first) {
  1830. gatt_sr_reset_prep_cnt(p_tcb);
  1831. }
  1832. if (is_inc) {
  1833. p_tcb->prep_cnt[idx]++;
  1834. } else {
  1835. if (p_tcb->prep_cnt[idx]) {
  1836. p_tcb->prep_cnt[idx]--;
  1837. }
  1838. }
  1839. }
  1840. }
  1841. /*******************************************************************************
  1842. **
  1843. ** Function gatt_cancel_open
  1844. **
  1845. ** Description Cancel open request
  1846. **
  1847. ** Returns Boolean
  1848. **
  1849. *******************************************************************************/
  1850. BOOLEAN gatt_cancel_open(tGATT_IF gatt_if, BD_ADDR bda)
  1851. {
  1852. tGATT_TCB *p_tcb = NULL;
  1853. BOOLEAN status = TRUE;
  1854. p_tcb = gatt_find_tcb_by_addr(bda, BT_TRANSPORT_LE);
  1855. if (p_tcb) {
  1856. if (gatt_get_ch_state(p_tcb) == GATT_CH_OPEN) {
  1857. GATT_TRACE_ERROR("GATT_CancelConnect - link connected Too late to cancel");
  1858. status = FALSE;
  1859. } else {
  1860. gatt_update_app_use_link_flag(gatt_if, p_tcb, FALSE, FALSE);
  1861. if (!gatt_num_apps_hold_link(p_tcb)) {
  1862. gatt_disconnect(p_tcb);
  1863. }
  1864. }
  1865. }
  1866. return status;
  1867. }
  1868. /*******************************************************************************
  1869. **
  1870. ** Function gatt_find_app_hold_link
  1871. **
  1872. ** Description find the application that is holding the specified link
  1873. **
  1874. ** Returns Boolean
  1875. **
  1876. *******************************************************************************/
  1877. BOOLEAN gatt_find_app_hold_link(tGATT_TCB *p_tcb, UINT8 start_idx, UINT8 *p_found_idx, tGATT_IF *p_gatt_if)
  1878. {
  1879. UINT8 i;
  1880. BOOLEAN found = FALSE;
  1881. for (i = start_idx; i < GATT_MAX_APPS; i ++) {
  1882. if (p_tcb->app_hold_link[i]) {
  1883. *p_gatt_if = p_tcb->app_hold_link[i];
  1884. *p_found_idx = i;
  1885. found = TRUE;
  1886. break;
  1887. }
  1888. }
  1889. return found;
  1890. }
  1891. /*******************************************************************************
  1892. **
  1893. ** Function gatt_find_specific_app_in_hold_link
  1894. **
  1895. ** Description find the specific application that is holding the specified link
  1896. **
  1897. ** Returns Boolean
  1898. **
  1899. *******************************************************************************/
  1900. BOOLEAN gatt_find_specific_app_in_hold_link(tGATT_TCB *p_tcb, tGATT_IF p_gatt_if)
  1901. {
  1902. UINT8 i;
  1903. BOOLEAN found = FALSE;
  1904. for (i = 0; i < GATT_MAX_APPS; i ++) {
  1905. if (p_tcb->app_hold_link[i] && p_tcb->app_hold_link[i] == p_gatt_if) {
  1906. found = TRUE;
  1907. break;
  1908. }
  1909. }
  1910. return found;
  1911. }
  1912. /*******************************************************************************
  1913. **
  1914. ** Function gatt_cmd_enq
  1915. **
  1916. ** Description Enqueue this command.
  1917. **
  1918. ** Returns None.
  1919. **
  1920. *******************************************************************************/
  1921. BOOLEAN gatt_cmd_enq(tGATT_TCB *p_tcb, UINT16 clcb_idx, BOOLEAN to_send, UINT8 op_code, BT_HDR *p_buf)
  1922. {
  1923. tGATT_CMD_Q *p_cmd = &p_tcb->cl_cmd_q[p_tcb->next_slot_inq];
  1924. p_cmd->to_send = to_send; /* waiting to be sent */
  1925. p_cmd->op_code = op_code;
  1926. p_cmd->p_cmd = p_buf;
  1927. p_cmd->clcb_idx = clcb_idx;
  1928. if (!to_send) {
  1929. p_tcb->pending_cl_req = p_tcb->next_slot_inq;
  1930. }
  1931. p_tcb->next_slot_inq ++;
  1932. p_tcb->next_slot_inq %= GATT_CL_MAX_LCB;
  1933. return TRUE;
  1934. }
  1935. /*******************************************************************************
  1936. **
  1937. ** Function gatt_cmd_dequeue
  1938. **
  1939. ** Description dequeue the command in the client CCB command queue.
  1940. **
  1941. ** Returns total number of clcb found.
  1942. **
  1943. *******************************************************************************/
  1944. tGATT_CLCB *gatt_cmd_dequeue(tGATT_TCB *p_tcb, UINT8 *p_op_code)
  1945. {
  1946. tGATT_CMD_Q *p_cmd = &p_tcb->cl_cmd_q[p_tcb->pending_cl_req];
  1947. tGATT_CLCB *p_clcb = NULL;
  1948. if (p_tcb->pending_cl_req != p_tcb->next_slot_inq) {
  1949. p_clcb = gatt_clcb_find_by_idx(p_cmd->clcb_idx);
  1950. *p_op_code = p_cmd->op_code;
  1951. p_tcb->pending_cl_req ++;
  1952. p_tcb->pending_cl_req %= GATT_CL_MAX_LCB;
  1953. }
  1954. return p_clcb;
  1955. }
  1956. /*******************************************************************************
  1957. **
  1958. ** Function gatt_send_write_msg
  1959. **
  1960. ** Description This real function send out the ATT message for write.
  1961. **
  1962. ** Returns status code
  1963. **
  1964. *******************************************************************************/
  1965. UINT8 gatt_send_write_msg (tGATT_TCB *p_tcb, UINT16 clcb_idx, UINT8 op_code,
  1966. UINT16 handle, UINT16 len,
  1967. UINT16 offset, UINT8 *p_data)
  1968. {
  1969. tGATT_CL_MSG msg;
  1970. msg.attr_value.handle = handle;
  1971. msg.attr_value.len = len;
  1972. msg.attr_value.offset = offset;
  1973. memcpy (msg.attr_value.value, p_data, len);
  1974. /* write by handle */
  1975. return attp_send_cl_msg(p_tcb, clcb_idx, op_code, &msg);
  1976. }
  1977. /*******************************************************************************
  1978. **
  1979. ** Function gatt_act_send_browse
  1980. **
  1981. ** Description This function ends a browse command request, including read
  1982. ** information request and read by type request.
  1983. **
  1984. ** Returns status code
  1985. **
  1986. *******************************************************************************/
  1987. UINT8 gatt_act_send_browse(tGATT_TCB *p_tcb, UINT16 index, UINT8 op, UINT16 s_handle,
  1988. UINT16 e_handle, tBT_UUID uuid)
  1989. {
  1990. tGATT_CL_MSG msg;
  1991. msg.browse.s_handle = s_handle;
  1992. msg.browse.e_handle = e_handle;
  1993. memcpy(&msg.browse.uuid, &uuid, sizeof(tBT_UUID));
  1994. /* write by handle */
  1995. return attp_send_cl_msg(p_tcb, index, op, &msg);
  1996. }
  1997. /*******************************************************************************
  1998. **
  1999. ** Function gatt_end_operation
  2000. **
  2001. ** Description This function ends a discovery, send callback and finalize
  2002. ** some control value.
  2003. **
  2004. ** Returns 16 bits uuid.
  2005. **
  2006. *******************************************************************************/
  2007. void gatt_end_operation(tGATT_CLCB *p_clcb, tGATT_STATUS status, void *p_data)
  2008. {
  2009. tGATT_CL_COMPLETE cb_data;
  2010. tGATT_CMPL_CBACK *p_cmpl_cb = (p_clcb->p_reg) ? p_clcb->p_reg->app_cb.p_cmpl_cb : NULL;
  2011. UINT8 op = p_clcb->operation, disc_type = GATT_DISC_MAX;
  2012. tGATT_DISC_CMPL_CB *p_disc_cmpl_cb = (p_clcb->p_reg) ? p_clcb->p_reg->app_cb.p_disc_cmpl_cb : NULL;
  2013. UINT16 conn_id;
  2014. #if (!CONFIG_BT_STACK_NO_LOG)
  2015. UINT8 operation;
  2016. #endif
  2017. GATT_TRACE_DEBUG ("gatt_end_operation status=%d op=%d subtype=%d",
  2018. status, p_clcb->operation, p_clcb->op_subtype);
  2019. memset(&cb_data.att_value, 0, sizeof(tGATT_VALUE));
  2020. if (p_cmpl_cb != NULL && p_clcb->operation != 0) {
  2021. if (p_clcb->operation == GATTC_OPTYPE_READ) {
  2022. cb_data.att_value.handle = p_clcb->s_handle;
  2023. cb_data.att_value.len = p_clcb->counter;
  2024. if (p_data && p_clcb->counter) {
  2025. memcpy (cb_data.att_value.value, p_data, cb_data.att_value.len);
  2026. }
  2027. }
  2028. if (p_clcb->operation == GATTC_OPTYPE_WRITE) {
  2029. memset(&cb_data.att_value, 0, sizeof(tGATT_VALUE));
  2030. cb_data.handle =
  2031. cb_data.att_value.handle = p_clcb->s_handle;
  2032. if (p_clcb->op_subtype == GATT_WRITE_PREPARE) {
  2033. if (p_data) {
  2034. cb_data.att_value = *((tGATT_VALUE *) p_data);
  2035. } else {
  2036. GATT_TRACE_DEBUG("Rcv Prepare write rsp but no data");
  2037. }
  2038. }
  2039. }
  2040. if (p_clcb->operation == GATTC_OPTYPE_CONFIG) {
  2041. cb_data.mtu = p_clcb->p_tcb->payload_size;
  2042. }
  2043. if (p_clcb->operation == GATTC_OPTYPE_DISCOVERY) {
  2044. disc_type = p_clcb->op_subtype;
  2045. }
  2046. }
  2047. if (p_clcb->p_attr_buf) {
  2048. osi_free(p_clcb->p_attr_buf);
  2049. }
  2050. #if !CONFIG_BT_STACK_NO_LOG
  2051. operation = p_clcb->operation;
  2052. #endif
  2053. conn_id = p_clcb->conn_id;
  2054. btu_stop_timer(&p_clcb->rsp_timer_ent);
  2055. gatt_clcb_dealloc(p_clcb);
  2056. if (p_disc_cmpl_cb && (op == GATTC_OPTYPE_DISCOVERY)) {
  2057. (*p_disc_cmpl_cb)(conn_id, disc_type, status);
  2058. } else if (p_cmpl_cb && op) {
  2059. (*p_cmpl_cb)(conn_id, op, status, &cb_data);
  2060. } else {
  2061. GATT_TRACE_WARNING ("gatt_end_operation not sent out op=%d p_disc_cmpl_cb:%p p_cmpl_cb:%p",
  2062. operation, p_disc_cmpl_cb, p_cmpl_cb);
  2063. }
  2064. }
  2065. /*******************************************************************************
  2066. **
  2067. ** Function gatt_cleanup_upon_disc
  2068. **
  2069. ** Description This function cleans up the control blocks when L2CAP channel
  2070. ** disconnect.
  2071. **
  2072. ** Returns 16 bits uuid.
  2073. **
  2074. *******************************************************************************/
  2075. void gatt_cleanup_upon_disc(BD_ADDR bda, UINT16 reason, tBT_TRANSPORT transport)
  2076. {
  2077. tGATT_TCB *p_tcb = NULL;
  2078. tGATT_CLCB *p_clcb;
  2079. UINT8 i;
  2080. UINT16 conn_id;
  2081. tGATT_REG *p_reg = NULL;
  2082. GATT_TRACE_DEBUG ("gatt_cleanup_upon_disc ");
  2083. if ((p_tcb = gatt_find_tcb_by_addr(bda, transport)) != NULL) {
  2084. GATT_TRACE_DEBUG ("found p_tcb ");
  2085. gatt_set_ch_state(p_tcb, GATT_CH_CLOSE);
  2086. list_node_t *p_node = NULL;
  2087. list_node_t *p_node_next = NULL;
  2088. for(p_node = list_begin(gatt_cb.p_clcb_list); p_node; p_node = p_node_next) {
  2089. p_clcb = list_node(p_node);
  2090. p_node_next = list_next(p_node);
  2091. if (p_clcb->in_use && p_clcb->p_tcb == p_tcb) {
  2092. btu_stop_timer(&p_clcb->rsp_timer_ent);
  2093. GATT_TRACE_DEBUG ("found p_clcb conn_id=%d clcb_idx=%d", p_clcb->conn_id, p_clcb->clcb_idx);
  2094. if (p_clcb->operation != GATTC_OPTYPE_NONE) {
  2095. gatt_end_operation(p_clcb, GATT_ERROR, NULL);
  2096. p_clcb = NULL;
  2097. }
  2098. gatt_clcb_dealloc(p_clcb);
  2099. }
  2100. }
  2101. btu_free_timer (&p_tcb->ind_ack_timer_ent);
  2102. btu_free_timer (&p_tcb->conf_timer_ent);
  2103. gatt_free_pending_ind(p_tcb);
  2104. gatt_free_pending_enc_queue(p_tcb);
  2105. gatt_free_pending_prepare_write_queue(p_tcb);
  2106. #if (GATTS_INCLUDED)
  2107. fixed_queue_free(p_tcb->sr_cmd.multi_rsp_q, osi_free_func);
  2108. p_tcb->sr_cmd.multi_rsp_q = NULL;
  2109. #endif /* #if (GATTS_INCLUDED) */
  2110. for (i = 0; i < GATT_MAX_APPS; i ++) {
  2111. p_reg = &gatt_cb.cl_rcb[i];
  2112. if (p_reg->in_use && p_reg->app_cb.p_conn_cb) {
  2113. conn_id = GATT_CREATE_CONN_ID(p_tcb->tcb_idx, p_reg->gatt_if);
  2114. 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);
  2115. (*p_reg->app_cb.p_conn_cb)(p_reg->gatt_if, bda, conn_id, FALSE, reason, transport);
  2116. }
  2117. }
  2118. gatt_tcb_free(p_tcb);
  2119. } else {
  2120. GATT_TRACE_DEBUG ("exit gatt_cleanup_upon_disc ");
  2121. BTM_Recovery_Pre_State();
  2122. }
  2123. gatt_delete_dev_from_srv_chg_clt_list(bda);
  2124. }
  2125. /*******************************************************************************
  2126. **
  2127. ** Function gatt_dbg_req_op_name
  2128. **
  2129. ** Description Get op code description name, for debug information.
  2130. **
  2131. ** Returns UINT8 *: name of the operation.
  2132. **
  2133. *******************************************************************************/
  2134. UINT8 *gatt_dbg_op_name(UINT8 op_code)
  2135. {
  2136. UINT8 pseduo_op_code_idx = op_code & (~GATT_WRITE_CMD_MASK);
  2137. if (op_code == GATT_CMD_WRITE ) {
  2138. pseduo_op_code_idx = 0x14; /* just an index to op_code_name */
  2139. }
  2140. if (op_code == GATT_SIGN_CMD_WRITE) {
  2141. pseduo_op_code_idx = 0x15; /* just an index to op_code_name */
  2142. }
  2143. if (pseduo_op_code_idx <= GATT_OP_CODE_MAX) {
  2144. return (UINT8 *) op_code_name[pseduo_op_code_idx];
  2145. } else {
  2146. return (UINT8 *)"Op Code Exceed Max";
  2147. }
  2148. }
  2149. /*******************************************************************************
  2150. **
  2151. ** Function gatt_dbg_display_uuid
  2152. **
  2153. ** Description Disaplay the UUID
  2154. **
  2155. ** Returns None
  2156. **
  2157. *******************************************************************************/
  2158. void gatt_dbg_display_uuid(tBT_UUID bt_uuid)
  2159. {
  2160. char str_buf[50];
  2161. int x = 0;
  2162. if (bt_uuid.len == LEN_UUID_16) {
  2163. sprintf(str_buf, "0x%04x", bt_uuid.uu.uuid16);
  2164. } else if (bt_uuid.len == LEN_UUID_32) {
  2165. sprintf(str_buf, "0x%08x", (unsigned int)bt_uuid.uu.uuid32);
  2166. } else if (bt_uuid.len == LEN_UUID_128) {
  2167. x += sprintf(&str_buf[x], "0x%02x%02x%02x%02x%02x%02x%02x%02x",
  2168. bt_uuid.uu.uuid128[15], bt_uuid.uu.uuid128[14],
  2169. bt_uuid.uu.uuid128[13], bt_uuid.uu.uuid128[12],
  2170. bt_uuid.uu.uuid128[11], bt_uuid.uu.uuid128[10],
  2171. bt_uuid.uu.uuid128[9], bt_uuid.uu.uuid128[8]);
  2172. sprintf(&str_buf[x], "%02x%02x%02x%02x%02x%02x%02x%02x",
  2173. bt_uuid.uu.uuid128[7], bt_uuid.uu.uuid128[6],
  2174. bt_uuid.uu.uuid128[5], bt_uuid.uu.uuid128[4],
  2175. bt_uuid.uu.uuid128[3], bt_uuid.uu.uuid128[2],
  2176. bt_uuid.uu.uuid128[1], bt_uuid.uu.uuid128[0]);
  2177. } else {
  2178. BCM_STRNCPY_S(str_buf, "Unknown UUID 0", 15);
  2179. }
  2180. GATT_TRACE_DEBUG ("UUID=[%s]", str_buf);
  2181. }
  2182. /*******************************************************************************
  2183. **
  2184. ** Function gatt_is_bg_dev_for_app
  2185. **
  2186. ** Description find is this one of the background devices for the application
  2187. **
  2188. ** Returns TRUE this is one of the background devices for the application
  2189. **
  2190. *******************************************************************************/
  2191. BOOLEAN gatt_is_bg_dev_for_app(tGATT_BG_CONN_DEV *p_dev, tGATT_IF gatt_if)
  2192. {
  2193. UINT8 i;
  2194. for (i = 0; i < GATT_MAX_APPS; i ++ ) {
  2195. if (p_dev->in_use && (p_dev->gatt_if[i] == gatt_if)) {
  2196. return TRUE;
  2197. }
  2198. }
  2199. return FALSE;
  2200. }
  2201. /*******************************************************************************
  2202. **
  2203. ** Function gatt_find_bg_dev
  2204. **
  2205. ** Description find background connection device from the list.
  2206. **
  2207. ** Returns pointer to the device record
  2208. **
  2209. *******************************************************************************/
  2210. tGATT_BG_CONN_DEV *gatt_find_bg_dev(BD_ADDR remote_bda)
  2211. {
  2212. tGATT_BG_CONN_DEV *p_dev_list = &gatt_cb.bgconn_dev[0];
  2213. UINT8 i;
  2214. for (i = 0; i < GATT_MAX_BG_CONN_DEV; i ++, p_dev_list ++) {
  2215. if (p_dev_list->in_use && !memcmp(p_dev_list->remote_bda, remote_bda, BD_ADDR_LEN)) {
  2216. return p_dev_list;
  2217. }
  2218. }
  2219. return NULL;
  2220. }
  2221. /*******************************************************************************
  2222. **
  2223. ** Function gatt_alloc_bg_dev
  2224. **
  2225. ** Description allocate a background connection device record
  2226. **
  2227. ** Returns pointer to the device record
  2228. **
  2229. *******************************************************************************/
  2230. tGATT_BG_CONN_DEV *gatt_alloc_bg_dev(BD_ADDR remote_bda)
  2231. {
  2232. tGATT_BG_CONN_DEV *p_dev_list = &gatt_cb.bgconn_dev[0];
  2233. UINT8 i;
  2234. for (i = 0; i < GATT_MAX_BG_CONN_DEV; i ++, p_dev_list ++) {
  2235. if (!p_dev_list->in_use) {
  2236. p_dev_list->in_use = TRUE;
  2237. memcpy(p_dev_list->remote_bda, remote_bda, BD_ADDR_LEN);
  2238. return p_dev_list;
  2239. }
  2240. }
  2241. return NULL;
  2242. }
  2243. /*******************************************************************************
  2244. **
  2245. ** Function gatt_add_bg_dev_list
  2246. **
  2247. ** Description add/remove device from the background connection device list
  2248. **
  2249. ** Returns TRUE if device added to the list; FALSE failed
  2250. **
  2251. *******************************************************************************/
  2252. BOOLEAN gatt_add_bg_dev_list(tGATT_REG *p_reg, BD_ADDR bd_addr, BOOLEAN is_initator)
  2253. {
  2254. tGATT_IF gatt_if = p_reg->gatt_if;
  2255. tGATT_BG_CONN_DEV *p_dev = NULL;
  2256. UINT8 i;
  2257. BOOLEAN ret = FALSE;
  2258. if ((p_dev = gatt_find_bg_dev(bd_addr)) == NULL) {
  2259. p_dev = gatt_alloc_bg_dev(bd_addr);
  2260. }
  2261. if (p_dev) {
  2262. for (i = 0; i < GATT_MAX_APPS; i ++) {
  2263. if (is_initator) {
  2264. if (p_dev->gatt_if[i] == gatt_if) {
  2265. GATT_TRACE_ERROR("device already in iniator white list");
  2266. return TRUE;
  2267. } else if (p_dev->gatt_if[i] == 0) {
  2268. p_dev->gatt_if[i] = gatt_if;
  2269. if (i == 0) {
  2270. ret = BTM_BleUpdateBgConnDev(TRUE, bd_addr);
  2271. } else {
  2272. ret = TRUE;
  2273. }
  2274. break;
  2275. }
  2276. } else {
  2277. if (p_dev->listen_gif[i] == gatt_if) {
  2278. GATT_TRACE_ERROR("device already in adv white list");
  2279. return TRUE;
  2280. } else if (p_dev->listen_gif[i] == 0) {
  2281. if (p_reg->listening == GATT_LISTEN_TO_ALL) {
  2282. p_reg->listening = GATT_LISTEN_TO_NONE;
  2283. }
  2284. p_reg->listening ++;
  2285. p_dev->listen_gif[i] = gatt_if;
  2286. if (i == 0) {
  2287. // To check, we do not support background connection, code will not be called here
  2288. ret = BTM_BleUpdateAdvWhitelist(TRUE, bd_addr, 0, NULL);
  2289. } else {
  2290. ret = TRUE;
  2291. }
  2292. break;
  2293. }
  2294. }
  2295. }
  2296. } else {
  2297. GATT_TRACE_ERROR("no device record available");
  2298. }
  2299. return ret;
  2300. }
  2301. /*******************************************************************************
  2302. **
  2303. ** Function gatt_remove_bg_dev_for_app
  2304. **
  2305. ** Description Remove the application interface for the specified background device
  2306. **
  2307. ** Returns Boolean
  2308. **
  2309. *******************************************************************************/
  2310. BOOLEAN gatt_remove_bg_dev_for_app(tGATT_IF gatt_if, BD_ADDR bd_addr)
  2311. {
  2312. tGATT_TCB *p_tcb = gatt_find_tcb_by_addr(bd_addr, BT_TRANSPORT_LE);
  2313. BOOLEAN status;
  2314. if (p_tcb) {
  2315. gatt_update_app_use_link_flag(gatt_if, p_tcb, FALSE, FALSE);
  2316. }
  2317. status = gatt_update_auto_connect_dev(gatt_if, FALSE, bd_addr, TRUE);
  2318. return status;
  2319. }
  2320. /*******************************************************************************
  2321. **
  2322. ** Function gatt_get_num_apps_for_bg_dev
  2323. **
  2324. ** Description Gte the number of applciations for the specified background device
  2325. **
  2326. ** Returns UINT8 total number fo applications
  2327. **
  2328. *******************************************************************************/
  2329. UINT8 gatt_get_num_apps_for_bg_dev(BD_ADDR bd_addr)
  2330. {
  2331. tGATT_BG_CONN_DEV *p_dev = NULL;
  2332. UINT8 i;
  2333. UINT8 cnt = 0;
  2334. if ((p_dev = gatt_find_bg_dev(bd_addr)) != NULL) {
  2335. for (i = 0; i < GATT_MAX_APPS; i ++) {
  2336. if (p_dev->gatt_if[i]) {
  2337. cnt++;
  2338. }
  2339. }
  2340. }
  2341. return cnt;
  2342. }
  2343. /*******************************************************************************
  2344. **
  2345. ** Function gatt_find_app_for_bg_dev
  2346. **
  2347. ** Description find the application interface for the specified background device
  2348. **
  2349. ** Returns Boolean
  2350. **
  2351. *******************************************************************************/
  2352. BOOLEAN gatt_find_app_for_bg_dev(BD_ADDR bd_addr, tGATT_IF *p_gatt_if)
  2353. {
  2354. tGATT_BG_CONN_DEV *p_dev = NULL;
  2355. UINT8 i;
  2356. BOOLEAN ret = FALSE;
  2357. if ((p_dev = gatt_find_bg_dev(bd_addr)) == NULL) {
  2358. return ret;
  2359. }
  2360. for (i = 0; i < GATT_MAX_APPS; i ++) {
  2361. if (p_dev->gatt_if[i] != 0 ) {
  2362. *p_gatt_if = p_dev->gatt_if[i];
  2363. ret = TRUE;
  2364. break;
  2365. }
  2366. }
  2367. return ret;
  2368. }
  2369. /*******************************************************************************
  2370. **
  2371. ** Function gatt_remove_bg_dev_from_list
  2372. **
  2373. ** Description add/remove device from the background connection device list or
  2374. ** listening to advertising list.
  2375. **
  2376. ** Returns pointer to the device record
  2377. **
  2378. *******************************************************************************/
  2379. BOOLEAN gatt_remove_bg_dev_from_list(tGATT_REG *p_reg, BD_ADDR bd_addr, BOOLEAN is_initiator)
  2380. {
  2381. tGATT_IF gatt_if = p_reg->gatt_if;
  2382. tGATT_BG_CONN_DEV *p_dev = NULL;
  2383. UINT8 i, j;
  2384. BOOLEAN ret = FALSE;
  2385. if ((p_dev = gatt_find_bg_dev(bd_addr)) == NULL) {
  2386. return ret;
  2387. }
  2388. for (i = 0; i < GATT_MAX_APPS && (p_dev->gatt_if[i] > 0 || p_dev->listen_gif[i]); i ++) {
  2389. if (is_initiator) {
  2390. if (p_dev->gatt_if[i] == gatt_if) {
  2391. p_dev->gatt_if[i] = 0;
  2392. /* move all element behind one forward */
  2393. for (j = i + 1; j < GATT_MAX_APPS; j ++) {
  2394. p_dev->gatt_if[j - 1] = p_dev->gatt_if[j];
  2395. }
  2396. if (p_dev->gatt_if[0] == 0) {
  2397. ret = BTM_BleUpdateBgConnDev(FALSE, p_dev->remote_bda);
  2398. } else {
  2399. ret = TRUE;
  2400. }
  2401. break;
  2402. }
  2403. } else {
  2404. if (p_dev->listen_gif[i] == gatt_if) {
  2405. p_dev->listen_gif[i] = 0;
  2406. p_reg->listening --;
  2407. /* move all element behind one forward */
  2408. for (j = i + 1; j < GATT_MAX_APPS; j ++) {
  2409. p_dev->listen_gif[j - 1] = p_dev->listen_gif[j];
  2410. }
  2411. if (p_dev->listen_gif[0] == 0) {
  2412. // To check, we do not support background connection, code will not be called here
  2413. ret = BTM_BleUpdateAdvWhitelist(FALSE, p_dev->remote_bda, 0, NULL);
  2414. } else {
  2415. ret = TRUE;
  2416. }
  2417. break;
  2418. }
  2419. }
  2420. }
  2421. if (i != GATT_MAX_APPS && p_dev->gatt_if[0] == 0 && p_dev->listen_gif[0] == 0) {
  2422. memset(p_dev, 0, sizeof(tGATT_BG_CONN_DEV));
  2423. }
  2424. return ret;
  2425. }
  2426. /*******************************************************************************
  2427. **
  2428. ** Function gatt_deregister_bgdev_list
  2429. **
  2430. ** Description deregister all related background connection device.
  2431. **
  2432. ** Returns pointer to the device record
  2433. **
  2434. *******************************************************************************/
  2435. void gatt_deregister_bgdev_list(tGATT_IF gatt_if)
  2436. {
  2437. tGATT_BG_CONN_DEV *p_dev_list = &gatt_cb.bgconn_dev[0];
  2438. UINT8 i , j, k;
  2439. tGATT_REG *p_reg = gatt_get_regcb(gatt_if);
  2440. /* update the BG conn device list */
  2441. for (i = 0 ; i < GATT_MAX_BG_CONN_DEV; i ++, p_dev_list ++ ) {
  2442. if (p_dev_list->in_use) {
  2443. for (j = 0; j < GATT_MAX_APPS; j ++) {
  2444. if (p_dev_list->gatt_if[j] == 0 && p_dev_list->listen_gif[j] == 0) {
  2445. break;
  2446. }
  2447. if (p_dev_list->gatt_if[j] == gatt_if) {
  2448. for (k = j + 1; k < GATT_MAX_APPS; k ++) {
  2449. p_dev_list->gatt_if[k - 1] = p_dev_list->gatt_if[k];
  2450. }
  2451. if (p_dev_list->gatt_if[0] == 0) {
  2452. BTM_BleUpdateBgConnDev(FALSE, p_dev_list->remote_bda);
  2453. }
  2454. }
  2455. if (p_dev_list->listen_gif[j] == gatt_if) {
  2456. p_dev_list->listen_gif[j] = 0;
  2457. if (p_reg != NULL && p_reg->listening > 0) {
  2458. p_reg->listening --;
  2459. }
  2460. /* move all element behind one forward */
  2461. for (k = j + 1; k < GATT_MAX_APPS; k ++) {
  2462. p_dev_list->listen_gif[k - 1] = p_dev_list->listen_gif[k];
  2463. }
  2464. if (p_dev_list->listen_gif[0] == 0) {
  2465. // To check, we do not support background connection, code will not be called here
  2466. BTM_BleUpdateAdvWhitelist(FALSE, p_dev_list->remote_bda, 0, NULL);
  2467. }
  2468. }
  2469. }
  2470. }
  2471. }
  2472. }
  2473. /*******************************************************************************
  2474. **
  2475. ** Function gatt_reset_bgdev_list
  2476. **
  2477. ** Description reset bg device list
  2478. **
  2479. ** Returns pointer to the device record
  2480. **
  2481. *******************************************************************************/
  2482. void gatt_reset_bgdev_list(void)
  2483. {
  2484. memset(&gatt_cb.bgconn_dev, 0 , sizeof(tGATT_BG_CONN_DEV)*GATT_MAX_BG_CONN_DEV);
  2485. }
  2486. /*******************************************************************************
  2487. **
  2488. ** Function gatt_update_auto_connect_dev
  2489. **
  2490. ** Description This function add or remove a device for background connection
  2491. ** procedure.
  2492. **
  2493. ** Parameters gatt_if: Application ID.
  2494. ** add: add peer device
  2495. ** bd_addr: peer device address.
  2496. **
  2497. ** Returns TRUE if connection started; FALSE if connection start failure.
  2498. **
  2499. *******************************************************************************/
  2500. BOOLEAN gatt_update_auto_connect_dev (tGATT_IF gatt_if, BOOLEAN add, BD_ADDR bd_addr, BOOLEAN is_initator)
  2501. {
  2502. BOOLEAN ret = FALSE;
  2503. tGATT_REG *p_reg;
  2504. tGATT_TCB *p_tcb = gatt_find_tcb_by_addr(bd_addr, BT_TRANSPORT_LE);
  2505. GATT_TRACE_API ("gatt_update_auto_connect_dev ");
  2506. /* Make sure app is registered */
  2507. if ((p_reg = gatt_get_regcb(gatt_if)) == NULL) {
  2508. GATT_TRACE_ERROR("gatt_update_auto_connect_dev - gatt_if %d is not registered", gatt_if);
  2509. return (FALSE);
  2510. }
  2511. if (add) {
  2512. ret = gatt_add_bg_dev_list(p_reg, bd_addr, is_initator);
  2513. if (ret && p_tcb != NULL) {
  2514. /* if a connected device, update the link holding number */
  2515. gatt_update_app_use_link_flag(gatt_if, p_tcb, TRUE, TRUE);
  2516. }
  2517. } else {
  2518. ret = gatt_remove_bg_dev_from_list(p_reg, bd_addr, is_initator);
  2519. }
  2520. return ret;
  2521. }
  2522. /*******************************************************************************
  2523. **
  2524. ** Function gatt_add_pending_new_srv_start
  2525. **
  2526. ** Description Add a pending new srv start to the new service start queue
  2527. **
  2528. ** Returns Pointer to the new service start buffer, NULL no buffer available
  2529. **
  2530. *******************************************************************************/
  2531. tGATT_PENDING_ENC_CLCB *gatt_add_pending_enc_channel_clcb(tGATT_TCB *p_tcb, tGATT_CLCB *p_clcb )
  2532. {
  2533. tGATT_PENDING_ENC_CLCB *p_buf;
  2534. GATT_TRACE_DEBUG ("gatt_add_pending_new_srv_start");
  2535. if ((p_buf = (tGATT_PENDING_ENC_CLCB *)osi_malloc((UINT16)sizeof(tGATT_PENDING_ENC_CLCB))) != NULL) {
  2536. GATT_TRACE_DEBUG ("enqueue a new pending encryption channel clcb");
  2537. p_buf->p_clcb = p_clcb;
  2538. fixed_queue_enqueue(p_tcb->pending_enc_clcb, p_buf, FIXED_QUEUE_MAX_TIMEOUT);
  2539. }
  2540. return p_buf;
  2541. }
  2542. /*******************************************************************************
  2543. **
  2544. ** Function gatt_update_listen_mode
  2545. **
  2546. ** Description update peripheral role listening mode
  2547. **
  2548. ** Returns Pointer to the new service start buffer, NULL no buffer available
  2549. **
  2550. *******************************************************************************/
  2551. BOOLEAN gatt_update_listen_mode(void)
  2552. {
  2553. UINT8 ii = 0;
  2554. tGATT_REG *p_reg = &gatt_cb.cl_rcb[0];
  2555. UINT8 listening = 0;
  2556. UINT16 connectability, window, interval;
  2557. BOOLEAN rt = TRUE;
  2558. for (; ii < GATT_MAX_APPS; ii ++, p_reg ++) {
  2559. if ( p_reg->in_use && p_reg->listening > listening) {
  2560. listening = p_reg->listening;
  2561. }
  2562. }
  2563. if (listening == GATT_LISTEN_TO_ALL ||
  2564. listening == GATT_LISTEN_TO_NONE) {
  2565. BTM_BleUpdateAdvFilterPolicy (AP_SCAN_CONN_ALL);
  2566. } else {
  2567. BTM_BleUpdateAdvFilterPolicy (AP_SCAN_CONN_WL);
  2568. }
  2569. if (rt) {
  2570. connectability = BTM_ReadConnectability (&window, &interval);
  2571. if (listening != GATT_LISTEN_TO_NONE) {
  2572. connectability |= BTM_BLE_CONNECTABLE;
  2573. } else {
  2574. if ((connectability & BTM_BLE_CONNECTABLE) == 0) {
  2575. connectability &= ~BTM_BLE_CONNECTABLE;
  2576. }
  2577. }
  2578. /* turning on the adv now */
  2579. btm_ble_set_connectability(connectability);
  2580. }
  2581. return rt;
  2582. }
  2583. #endif