gatt_utils.c 87 KB

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