gatt_utils.c 84 KB

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