gatt_utils.c 93 KB

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