1 /******************************************************************************
3 * Copyright (C) 2003-2012 Broadcom Corporation
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 ******************************************************************************/
19 /******************************************************************************
21 * This file contains the GATT client utility function.
23 ******************************************************************************/
25 #define LOG_TAG "bt_bta_gattc"
27 #include "bt_target.h"
29 #if defined(BTA_GATT_INCLUDED) && (BTA_GATT_INCLUDED == TRUE)
33 #include "bta_gattc_int.h"
35 #include "btcore/include/bdaddr.h"
36 #include "btif/include/btif_util.h"
37 #include "bt_common.h"
41 /*****************************************************************************
43 *****************************************************************************/
45 static const UINT8 base_uuid[LEN_UUID_128] = {0xFB, 0x34, 0x9B, 0x5F, 0x80, 0x00, 0x00, 0x80,
46 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
48 static const BD_ADDR dummy_bda = {0,0,0,0,0,0};
50 /*******************************************************************************
52 ** Function bta_gatt_convert_uuid16_to_uuid128
54 ** Description Convert a 16 bits UUID to be an standard 128 bits one.
56 ** Returns TRUE if two uuid match; FALSE otherwise.
58 *******************************************************************************/
59 void bta_gatt_convert_uuid16_to_uuid128(UINT8 uuid_128[LEN_UUID_128], UINT16 uuid_16)
61 UINT8 *p = &uuid_128[LEN_UUID_128 - 4];
63 memcpy (uuid_128, base_uuid, LEN_UUID_128);
65 UINT16_TO_STREAM(p, uuid_16);
67 /*******************************************************************************
69 ** Function bta_gattc_uuid_compare
71 ** Description Compare two UUID to see if they are the same.
73 ** Returns TRUE if two uuid match; FALSE otherwise.
75 *******************************************************************************/
76 BOOLEAN bta_gattc_uuid_compare (tBT_UUID *p_src, tBT_UUID *p_tar, BOOLEAN is_precise)
78 UINT8 su[LEN_UUID_128], tu[LEN_UUID_128];
81 /* any of the UUID is unspecified */
82 if (p_src == 0 || p_tar == 0)
90 /* If both are 16-bit, we can do a simple compare */
91 if (p_src->len == 2 && p_tar->len == 2)
93 return p_src->uu.uuid16 == p_tar->uu.uuid16;
96 /* One or both of the UUIDs is 128-bit */
97 if (p_src->len == LEN_UUID_16)
99 /* convert a 16 bits UUID to 128 bits value */
100 bta_gatt_convert_uuid16_to_uuid128(su, p_src->uu.uuid16);
104 ps = p_src->uu.uuid128;
106 if (p_tar->len == LEN_UUID_16)
108 /* convert a 16 bits UUID to 128 bits value */
109 bta_gatt_convert_uuid16_to_uuid128(tu, p_tar->uu.uuid16);
113 pt = p_tar->uu.uuid128;
115 return(memcmp(ps, pt, LEN_UUID_128) == 0);
118 /*******************************************************************************
120 ** Function bta_gattc_cl_get_regcb
122 ** Description get registration control block by client interface.
124 ** Returns pointer to the regcb
126 *******************************************************************************/
127 tBTA_GATTC_RCB * bta_gattc_cl_get_regcb(UINT8 client_if)
130 tBTA_GATTC_RCB *p_clrcb = &bta_gattc_cb.cl_rcb[0];
132 for (i = 0; i < BTA_GATTC_CL_MAX; i ++, p_clrcb ++)
134 if (p_clrcb->in_use &&
135 p_clrcb->client_if == client_if)
140 /*******************************************************************************
142 ** Function bta_gattc_num_reg_app
144 ** Description find the number of registered application.
146 ** Returns pointer to the regcb
148 *******************************************************************************/
149 UINT8 bta_gattc_num_reg_app(void)
153 for (i = 0; i < BTA_GATTC_CL_MAX; i ++)
155 if (bta_gattc_cb.cl_rcb[i].in_use)
160 /*******************************************************************************
162 ** Function bta_gattc_find_clcb_by_cif
164 ** Description get clcb by client interface and remote bd adddress
166 ** Returns pointer to the clcb
168 *******************************************************************************/
169 tBTA_GATTC_CLCB * bta_gattc_find_clcb_by_cif (UINT8 client_if, BD_ADDR remote_bda,
170 tBTA_TRANSPORT transport)
172 tBTA_GATTC_CLCB *p_clcb = &bta_gattc_cb.clcb[0];
175 for (i = 0; i < BTA_GATTC_CLCB_MAX; i ++, p_clcb ++)
177 if (p_clcb->in_use &&
178 p_clcb->p_rcb->client_if == client_if &&
179 p_clcb->transport == transport &&
180 bdcmp(p_clcb->bda, remote_bda) == 0)
185 /*******************************************************************************
187 ** Function bta_gattc_find_clcb_by_conn_id
189 ** Description get clcb by connection ID
191 ** Returns pointer to the clcb
193 *******************************************************************************/
194 tBTA_GATTC_CLCB * bta_gattc_find_clcb_by_conn_id (UINT16 conn_id)
196 tBTA_GATTC_CLCB *p_clcb = &bta_gattc_cb.clcb[0];
199 for (i = 0; i < BTA_GATTC_CLCB_MAX; i ++, p_clcb ++)
201 if (p_clcb->in_use &&
202 p_clcb->bta_conn_id == conn_id)
208 /*******************************************************************************
210 ** Function bta_gattc_clcb_alloc
212 ** Description allocate CLCB
214 ** Returns pointer to the clcb
216 *******************************************************************************/
217 tBTA_GATTC_CLCB * bta_gattc_clcb_alloc(tBTA_GATTC_IF client_if, BD_ADDR remote_bda,
218 tBTA_TRANSPORT transport)
221 tBTA_GATTC_CLCB *p_clcb = NULL;
223 for (i_clcb = 0; i_clcb < BTA_GATTC_CLCB_MAX; i_clcb++)
225 if (!bta_gattc_cb.clcb[i_clcb].in_use)
227 #if BTA_GATT_DEBUG == TRUE
228 APPL_TRACE_DEBUG("bta_gattc_clcb_alloc: found clcb[%d] available",i_clcb);
230 p_clcb = &bta_gattc_cb.clcb[i_clcb];
231 p_clcb->in_use = TRUE;
232 p_clcb->status = BTA_GATT_OK;
233 p_clcb->transport = transport;
234 bdcpy(p_clcb->bda, remote_bda);
236 p_clcb->p_rcb = bta_gattc_cl_get_regcb(client_if);
238 if ((p_clcb->p_srcb = bta_gattc_find_srcb(remote_bda)) == NULL)
239 p_clcb->p_srcb = bta_gattc_srcb_alloc(remote_bda);
241 if (p_clcb->p_rcb != NULL && p_clcb->p_srcb != NULL)
243 p_clcb->p_srcb->num_clcb ++;
244 p_clcb->p_rcb->num_clcb ++;
248 /* release this clcb if clcb or srcb allocation failed */
249 p_clcb->in_use = FALSE;
257 /*******************************************************************************
259 ** Function bta_gattc_find_alloc_clcb
261 ** Description find or allocate CLCB if not found.
263 ** Returns pointer to the clcb
265 *******************************************************************************/
266 tBTA_GATTC_CLCB *bta_gattc_find_alloc_clcb(tBTA_GATTC_IF client_if, BD_ADDR remote_bda,
267 tBTA_TRANSPORT transport)
269 tBTA_GATTC_CLCB *p_clcb ;
271 if ((p_clcb = bta_gattc_find_clcb_by_cif(client_if, remote_bda, transport)) == NULL)
273 p_clcb = bta_gattc_clcb_alloc(client_if, remote_bda, transport);
278 /*******************************************************************************
280 ** Function bta_gattc_clcb_dealloc
282 ** Description Deallocte a clcb
284 ** Returns pointer to the clcb
286 *******************************************************************************/
287 void bta_gattc_clcb_dealloc(tBTA_GATTC_CLCB *p_clcb)
289 tBTA_GATTC_SERV *p_srcb = NULL;
293 p_srcb = p_clcb->p_srcb;
294 if (p_srcb->num_clcb)
297 if (p_clcb->p_rcb->num_clcb)
298 p_clcb->p_rcb->num_clcb --;
300 /* if the srcb is no longer needed, reset the state */
301 if ( p_srcb->num_clcb == 0)
303 p_srcb->connected = FALSE;
304 p_srcb->state = BTA_GATTC_SERV_IDLE;
308 utl_freebuf((void **)&p_clcb->p_q_cmd);
310 memset(p_clcb, 0, sizeof(tBTA_GATTC_CLCB));
314 APPL_TRACE_ERROR("bta_gattc_clcb_dealloc p_clcb=NULL");
318 /*******************************************************************************
320 ** Function bta_gattc_find_srcb
322 ** Description find server cache by remote bd address currently in use
324 ** Returns pointer to the server cache.
326 *******************************************************************************/
327 tBTA_GATTC_SERV * bta_gattc_find_srcb(BD_ADDR bda)
329 tBTA_GATTC_SERV *p_srcb = &bta_gattc_cb.known_server[0];
332 for (i = 0; i < BTA_GATTC_KNOWN_SR_MAX; i ++, p_srcb ++)
334 if (p_srcb->in_use && bdcmp(p_srcb->server_bda, bda) == 0)
340 /*******************************************************************************
342 ** Function bta_gattc_find_srvr_cache
344 ** Description find server cache by remote bd address
346 ** Returns pointer to the server cache.
348 *******************************************************************************/
349 tBTA_GATTC_SERV * bta_gattc_find_srvr_cache(BD_ADDR bda)
351 tBTA_GATTC_SERV *p_srcb = &bta_gattc_cb.known_server[0];
354 for (i = 0; i < BTA_GATTC_KNOWN_SR_MAX; i ++, p_srcb ++)
356 if (bdcmp(p_srcb->server_bda, bda) == 0)
361 /*******************************************************************************
363 ** Function bta_gattc_find_scb_by_cid
365 ** Description find server control block by connection ID
367 ** Returns pointer to the server cache.
369 *******************************************************************************/
370 tBTA_GATTC_SERV * bta_gattc_find_scb_by_cid (UINT16 conn_id)
372 tBTA_GATTC_CLCB *p_clcb = bta_gattc_find_clcb_by_conn_id(conn_id);
375 return p_clcb->p_srcb;
379 /*******************************************************************************
381 ** Function bta_gattc_srcb_alloc
383 ** Description allocate server cache control block
385 ** Returns pointer to the server cache.
387 *******************************************************************************/
388 tBTA_GATTC_SERV * bta_gattc_srcb_alloc(BD_ADDR bda)
390 tBTA_GATTC_SERV *p_tcb = &bta_gattc_cb.known_server[0],
392 BOOLEAN found = FALSE;
395 for (i = 0; i < BTA_GATTC_KNOWN_SR_MAX; i ++, p_tcb ++)
402 else if (!p_tcb->connected)
408 /* if not found, try to recycle one known device */
409 if (!found && !p_recycle)
411 else if (!found && p_recycle)
416 if (p_tcb->cache_buffer != NULL) {
417 while (! fixed_queue_is_empty(p_tcb->cache_buffer))
418 osi_freebuf(fixed_queue_try_dequeue(p_tcb->cache_buffer));
419 fixed_queue_free(p_tcb->cache_buffer, NULL);
422 utl_freebuf((void **)&p_tcb->p_srvc_list);
423 memset(p_tcb, 0 , sizeof(tBTA_GATTC_SERV));
424 p_tcb->cache_buffer = fixed_queue_new(SIZE_MAX);
426 p_tcb->in_use = TRUE;
427 bdcpy(p_tcb->server_bda, bda);
431 /*******************************************************************************
433 ** Function bta_gattc_enqueue
435 ** Description enqueue a client request in clcb.
437 ** Returns success or failure.
439 *******************************************************************************/
440 BOOLEAN bta_gattc_enqueue(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
443 if (p_clcb->p_q_cmd == NULL)
445 p_clcb->p_q_cmd = p_data;
449 APPL_TRACE_ERROR ("%s: already has a pending command!!", __func__);
450 /* skip the callback now. ----- need to send callback ? */
454 /*******************************************************************************
456 ** Function bta_gattc_pack_attr_uuid
458 ** Description pack UUID into a stream.
462 *******************************************************************************/
463 void bta_gattc_pack_attr_uuid(tBTA_GATTC_CACHE_ATTR *p_attr, tBT_UUID *p_uuid)
465 UINT8 *pp = (UINT8 *)p_attr->p_uuid;
467 memset(p_uuid, 0, sizeof(tBT_UUID));
469 p_uuid->len = p_attr->uuid_len;
471 if (p_attr->uuid_len == LEN_UUID_16)
473 STREAM_TO_UINT16(p_uuid->uu.uuid16, pp);
477 memcpy(p_uuid->uu.uuid128, pp, LEN_UUID_128);
482 /*******************************************************************************
484 ** Function bta_gattc_cpygattid
486 ** Description copy two tBTA_GATT_ID value
490 *******************************************************************************/
491 void bta_gattc_cpygattid(tBTA_GATT_ID *p_des, tBTA_GATT_ID *p_src)
493 memset ((void *)p_des, 0, sizeof(tBTA_GATT_ID));
495 p_des->inst_id = p_src->inst_id;
497 p_des->uuid.len = p_src->uuid.len;
499 if (p_des->uuid.len == LEN_UUID_16)
501 p_des->uuid.uu.uuid16 = p_src->uuid.uu.uuid16;
503 else if (p_des->uuid.len == LEN_UUID_128)
505 memcpy(p_des->uuid.uu.uuid128, p_src->uuid.uu.uuid128, LEN_UUID_128);
508 /*******************************************************************************
510 ** Function bta_gattc_gattid_compare
512 ** Description compare two tBTA_GATT_ID type of pointer
516 *******************************************************************************/
517 BOOLEAN bta_gattc_gattid_compare(tBTA_GATT_ID *p_src, tBTA_GATT_ID *p_tar)
519 if (p_src->inst_id == p_tar->inst_id &&
520 bta_gattc_uuid_compare (&p_src->uuid, &p_tar->uuid, TRUE ))
526 /*******************************************************************************
528 ** Function bta_gattc_srvcid_compare
530 ** Description compare two tBTA_GATT_SRVC_ID type of pointer
534 *******************************************************************************/
535 BOOLEAN bta_gattc_srvcid_compare(tBTA_GATT_SRVC_ID *p_src, tBTA_GATT_SRVC_ID *p_tar)
537 if (p_src->is_primary == p_tar->is_primary &&
538 bta_gattc_gattid_compare (&p_src->id, &p_tar->id))
543 /*******************************************************************************
545 ** Function bta_gattc_charid_compare
547 ** Description compare two tBTA_GATTC_CHAR_ID type of pointer
551 *******************************************************************************/
552 BOOLEAN bta_gattc_charid_compare(tBTA_GATTC_CHAR_ID *p_src, tBTA_GATTC_CHAR_ID *p_tar)
554 if (bta_gattc_gattid_compare (&p_src->char_id, &p_tar->char_id) &&
555 bta_gattc_srvcid_compare (&p_src->srvc_id, &p_tar->srvc_id))
561 /*******************************************************************************
563 ** Function bta_gattc_check_notif_registry
565 ** Description check if the service notificaition has been registered.
569 *******************************************************************************/
570 BOOLEAN bta_gattc_check_notif_registry(tBTA_GATTC_RCB *p_clreg, tBTA_GATTC_SERV *p_srcb,
571 tBTA_GATTC_NOTIFY *p_notify)
575 for (i = 0 ; i < BTA_GATTC_NOTIF_REG_MAX; i ++)
577 if (p_clreg->notif_reg[i].in_use &&
578 bdcmp(p_clreg->notif_reg[i].remote_bda, p_srcb->server_bda) == 0 &&
579 bta_gattc_charid_compare (&p_clreg->notif_reg[i].char_id, &p_notify->char_id))
581 APPL_TRACE_DEBUG("Notification registered!");
588 /*******************************************************************************
590 ** Function bta_gattc_clear_notif_registration
592 ** Description clear up the notification registration information by BD_ADDR.
596 *******************************************************************************/
597 void bta_gattc_clear_notif_registration(UINT16 conn_id)
600 tBTA_GATTC_IF gatt_if;
601 tBTA_GATTC_RCB *p_clrcb ;
603 tGATT_TRANSPORT transport;
605 if (GATT_GetConnectionInfor(conn_id, &gatt_if, remote_bda, &transport))
607 if ((p_clrcb = bta_gattc_cl_get_regcb(gatt_if)) != NULL)
609 for (i = 0 ; i < BTA_GATTC_NOTIF_REG_MAX; i ++)
611 if (p_clrcb->notif_reg[i].in_use &&
612 !bdcmp(p_clrcb->notif_reg[i].remote_bda, remote_bda))
613 memset(&p_clrcb->notif_reg[i], 0, sizeof(tBTA_GATTC_NOTIF_REG));
619 APPL_TRACE_ERROR("can not clear indication/notif registration for unknown app");
624 /*******************************************************************************
626 ** Function bta_gattc_pack_cb_data
628 ** Description pack the data from read response into callback data structure.
632 *******************************************************************************/
633 tBTA_GATT_STATUS bta_gattc_pack_read_cb_data(tBTA_GATTC_SERV *p_srcb,
634 tBT_UUID *p_descr_uuid,
636 tBTA_GATT_READ_VAL *p_value)
638 UINT8 i = 0, *pp = p_attr->value;
639 tBT_UUID uuid = {LEN_UUID_16, {GATT_UUID_CHAR_AGG_FORMAT}};
641 tBTA_GATT_STATUS status = BTA_GATT_OK;
643 /* GATT_UUID_CHAR_AGG_FORMAT */
644 if (bta_gattc_uuid_compare (&uuid, p_descr_uuid, TRUE))
646 while (p_attr->len >= 2 && i < BTA_GATTC_MULTI_MAX)
648 STREAM_TO_UINT16(handle, pp);
650 if (bta_gattc_handle2id(p_srcb,
652 &p_value->aggre_value.pre_format[i].char_id.srvc_id,
653 &p_value->aggre_value.pre_format[i].char_id.char_id,
654 &p_value->aggre_value.pre_format[i].descr_id) == FALSE)
656 status = BTA_GATT_INTERNAL_ERROR;
657 APPL_TRACE_ERROR("can not map to GATT ID. handle = 0x%04x", handle);
663 p_value->aggre_value.num_pres_fmt = i;
667 /* all others, take as raw format */
668 p_value->unformat.len = p_attr->len;
669 p_value->unformat.p_value = p_attr->value;
673 /*******************************************************************************
675 ** Function bta_gattc_mark_bg_conn
677 ** Description mark background connection status when a bg connection is initiated
680 ** Returns TRUE if success; FALSE otherwise.
682 *******************************************************************************/
683 BOOLEAN bta_gattc_mark_bg_conn (tBTA_GATTC_IF client_if, BD_ADDR_PTR remote_bda_ptr,
684 BOOLEAN add, BOOLEAN is_listen)
686 tBTA_GATTC_BG_TCK *p_bg_tck = &bta_gattc_cb.bg_track[0];
688 tBTA_GATTC_CIF_MASK *p_cif_mask;
690 for (i = 0; i < BTA_GATTC_KNOWN_SR_MAX; i ++, p_bg_tck ++)
692 if (p_bg_tck->in_use &&
693 ((remote_bda_ptr != NULL && bdcmp(p_bg_tck->remote_bda, remote_bda_ptr) == 0) ||
694 (remote_bda_ptr == NULL && bdcmp(p_bg_tck->remote_bda, dummy_bda) == 0)))
696 p_cif_mask = is_listen ? &p_bg_tck->cif_adv_mask : &p_bg_tck->cif_mask;
699 /* mask on the cif bit */
700 *p_cif_mask |= (1 <<(client_if - 1));
704 *p_cif_mask &= (~(1 <<(client_if - 1)));
708 /* no BG connection for this device, make it available */
709 if (p_bg_tck->cif_mask == 0 && p_bg_tck->cif_adv_mask == 0)
711 memset(p_bg_tck, 0, sizeof(tBTA_GATTC_BG_TCK));
721 APPL_TRACE_ERROR("%s unable to find the bg connection mask for: %s", __func__,
722 bdaddr_to_string((bt_bdaddr_t *)remote_bda_ptr, bdstr, sizeof(bdstr)));
726 else /* adding a new device mask */
728 for (i = 0, p_bg_tck = &bta_gattc_cb.bg_track[0];
729 i < BTA_GATTC_KNOWN_SR_MAX; i ++, p_bg_tck ++)
731 if (!p_bg_tck->in_use)
733 p_bg_tck->in_use = TRUE;
735 bdcpy(p_bg_tck->remote_bda, remote_bda_ptr);
737 bdcpy(p_bg_tck->remote_bda, dummy_bda);
739 p_cif_mask = is_listen ? &p_bg_tck->cif_adv_mask : &p_bg_tck->cif_mask;
741 *p_cif_mask = (1 <<(client_if - 1));
745 APPL_TRACE_ERROR("no available space to mark the bg connection status");
749 /*******************************************************************************
751 ** Function bta_gattc_check_bg_conn
753 ** Description check if this is a background connection background connection.
755 ** Returns TRUE if success; FALSE otherwise.
757 *******************************************************************************/
758 BOOLEAN bta_gattc_check_bg_conn (tBTA_GATTC_IF client_if, BD_ADDR remote_bda, UINT8 role)
760 tBTA_GATTC_BG_TCK *p_bg_tck = &bta_gattc_cb.bg_track[0];
762 BOOLEAN is_bg_conn = FALSE;
764 for (i = 0; i < BTA_GATTC_KNOWN_SR_MAX && !is_bg_conn; i ++, p_bg_tck ++)
766 if (p_bg_tck->in_use &&
767 (bdcmp(p_bg_tck->remote_bda, remote_bda) == 0 ||
768 bdcmp(p_bg_tck->remote_bda, dummy_bda) == 0))
770 if (((p_bg_tck->cif_mask &(1 <<(client_if - 1))) != 0) &&
771 role == HCI_ROLE_MASTER)
774 if (((p_bg_tck->cif_adv_mask &(1 <<(client_if - 1))) != 0) &&
775 role == HCI_ROLE_SLAVE)
781 /*******************************************************************************
783 ** Function bta_gattc_send_open_cback
785 ** Description send open callback
789 *******************************************************************************/
790 void bta_gattc_send_open_cback( tBTA_GATTC_RCB *p_clreg, tBTA_GATT_STATUS status,
791 BD_ADDR remote_bda, UINT16 conn_id,
792 tBTA_TRANSPORT transport, UINT16 mtu)
796 if (p_clreg->p_cback)
798 memset(&cb_data, 0, sizeof(tBTA_GATTC));
800 cb_data.open.status = status;
801 cb_data.open.client_if = p_clreg->client_if;
802 cb_data.open.conn_id = conn_id;
803 cb_data.open.mtu = mtu;
804 cb_data.open.transport = transport;
805 bdcpy(cb_data.open.remote_bda, remote_bda);
807 (*p_clreg->p_cback)(BTA_GATTC_OPEN_EVT, &cb_data);
810 /*******************************************************************************
812 ** Function bta_gattc_conn_alloc
814 ** Description allocate connection tracking spot
816 ** Returns pointer to the clcb
818 *******************************************************************************/
819 tBTA_GATTC_CONN * bta_gattc_conn_alloc(BD_ADDR remote_bda)
822 tBTA_GATTC_CONN *p_conn = &bta_gattc_cb.conn_track[0];
824 for (i_conn = 0; i_conn < BTA_GATTC_CONN_MAX; i_conn++, p_conn ++)
828 #if BTA_GATT_DEBUG == TRUE
829 APPL_TRACE_DEBUG("bta_gattc_conn_alloc: found conn_track[%d] available",i_conn);
831 p_conn->in_use = TRUE;
832 bdcpy(p_conn->remote_bda, remote_bda);
839 /*******************************************************************************
841 ** Function bta_gattc_conn_find
843 ** Description allocate connection tracking spot
845 ** Returns pointer to the clcb
847 *******************************************************************************/
848 tBTA_GATTC_CONN * bta_gattc_conn_find(BD_ADDR remote_bda)
851 tBTA_GATTC_CONN *p_conn = &bta_gattc_cb.conn_track[0];
853 for (i_conn = 0; i_conn < BTA_GATTC_CONN_MAX; i_conn++, p_conn ++)
855 if (p_conn->in_use && bdcmp(remote_bda, p_conn->remote_bda) == 0)
857 #if BTA_GATT_DEBUG == TRUE
858 APPL_TRACE_DEBUG("bta_gattc_conn_find: found conn_track[%d] matched",i_conn);
866 /*******************************************************************************
868 ** Function bta_gattc_conn_find_alloc
870 ** Description find or allocate connection tracking spot
872 ** Returns pointer to the clcb
874 *******************************************************************************/
875 tBTA_GATTC_CONN * bta_gattc_conn_find_alloc(BD_ADDR remote_bda)
877 tBTA_GATTC_CONN *p_conn = bta_gattc_conn_find (remote_bda);
881 p_conn = bta_gattc_conn_alloc(remote_bda);
886 /*******************************************************************************
888 ** Function bta_gattc_conn_dealloc
890 ** Description de-allocate connection tracking spot
892 ** Returns pointer to the clcb
894 *******************************************************************************/
895 BOOLEAN bta_gattc_conn_dealloc(BD_ADDR remote_bda)
897 tBTA_GATTC_CONN *p_conn = bta_gattc_conn_find (remote_bda);
901 p_conn->in_use = FALSE;
902 memset(p_conn->remote_bda, 0, BD_ADDR_LEN);
908 /*******************************************************************************
910 ** Function bta_gattc_find_int_conn_clcb
912 ** Description try to locate a clcb when an internal connecion event arrives.
914 ** Returns pointer to the clcb
916 *******************************************************************************/
917 tBTA_GATTC_CLCB * bta_gattc_find_int_conn_clcb(tBTA_GATTC_DATA *p_msg)
919 tBTA_GATTC_CLCB *p_clcb = NULL;
921 if (p_msg->int_conn.role == HCI_ROLE_SLAVE)
922 bta_gattc_conn_find_alloc(p_msg->int_conn.remote_bda);
924 /* try to locate a logic channel */
925 if ((p_clcb = bta_gattc_find_clcb_by_cif(p_msg->int_conn.client_if,
926 p_msg->int_conn.remote_bda,
927 p_msg->int_conn.transport)) == NULL)
929 /* for a background connection or listening connection */
930 if (/*p_msg->int_conn.role == HCI_ROLE_SLAVE || */
931 bta_gattc_check_bg_conn(p_msg->int_conn.client_if,
932 p_msg->int_conn.remote_bda,
933 p_msg->int_conn.role))
935 /* allocate a new channel */
936 p_clcb = bta_gattc_clcb_alloc(p_msg->int_conn.client_if,
937 p_msg->int_conn.remote_bda,
938 p_msg->int_conn.transport);
944 /*******************************************************************************
946 ** Function bta_gattc_find_int_disconn_clcb
948 ** Description try to locate a clcb when an internal disconnect callback arrives.
950 ** Returns pointer to the clcb
952 *******************************************************************************/
953 tBTA_GATTC_CLCB * bta_gattc_find_int_disconn_clcb(tBTA_GATTC_DATA *p_msg)
955 tBTA_GATTC_CLCB *p_clcb = NULL;
957 bta_gattc_conn_dealloc(p_msg->int_conn.remote_bda);
958 if ((p_clcb = bta_gattc_find_clcb_by_conn_id(p_msg->int_conn.hdr.layer_specific)) == NULL)
960 /* connection attempt failed, send connection callback event */
961 p_clcb = bta_gattc_find_clcb_by_cif(p_msg->int_conn.client_if,
962 p_msg->int_conn.remote_bda,
963 p_msg->int_conn.transport);
967 APPL_TRACE_DEBUG(" disconnection ID: [%d] not used by BTA",
968 p_msg->int_conn.hdr.layer_specific);
973 #endif /* BTA_GATT_INCLUDED */