gatt_utils.c 88 KB

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