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 "bt_common.h"
40 /*****************************************************************************
42 *****************************************************************************/
44 static const UINT8 base_uuid[LEN_UUID_128] = {0xFB, 0x34, 0x9B, 0x5F, 0x80, 0x00, 0x00, 0x80,
45 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
47 static const BD_ADDR dummy_bda = {0,0,0,0,0,0};
49 /*******************************************************************************
51 ** Function bta_gatt_convert_uuid16_to_uuid128
53 ** Description Convert a 16 bits UUID to be an standard 128 bits one.
55 ** Returns TRUE if two uuid match; FALSE otherwise.
57 *******************************************************************************/
58 void bta_gatt_convert_uuid16_to_uuid128(UINT8 uuid_128[LEN_UUID_128], UINT16 uuid_16)
60 UINT8 *p = &uuid_128[LEN_UUID_128 - 4];
62 memcpy (uuid_128, base_uuid, LEN_UUID_128);
64 UINT16_TO_STREAM(p, uuid_16);
66 /*******************************************************************************
68 ** Function bta_gattc_uuid_compare
70 ** Description Compare two UUID to see if they are the same.
72 ** Returns TRUE if two uuid match; FALSE otherwise.
74 *******************************************************************************/
75 BOOLEAN bta_gattc_uuid_compare (const tBT_UUID *p_src, const tBT_UUID *p_tar, BOOLEAN is_precise)
77 UINT8 su[LEN_UUID_128], tu[LEN_UUID_128];
80 /* any of the UUID is unspecified */
81 if (p_src == 0 || p_tar == 0)
89 /* If both are 16-bit, we can do a simple compare */
90 if (p_src->len == 2 && p_tar->len == 2)
92 return p_src->uu.uuid16 == p_tar->uu.uuid16;
95 /* One or both of the UUIDs is 128-bit */
96 if (p_src->len == LEN_UUID_16)
98 /* convert a 16 bits UUID to 128 bits value */
99 bta_gatt_convert_uuid16_to_uuid128(su, p_src->uu.uuid16);
103 ps = p_src->uu.uuid128;
105 if (p_tar->len == LEN_UUID_16)
107 /* convert a 16 bits UUID to 128 bits value */
108 bta_gatt_convert_uuid16_to_uuid128(tu, p_tar->uu.uuid16);
112 pt = p_tar->uu.uuid128;
114 return(memcmp(ps, pt, LEN_UUID_128) == 0);
117 /*******************************************************************************
119 ** Function bta_gattc_cl_get_regcb
121 ** Description get registration control block by client interface.
123 ** Returns pointer to the regcb
125 *******************************************************************************/
126 tBTA_GATTC_RCB * bta_gattc_cl_get_regcb(UINT8 client_if)
129 tBTA_GATTC_RCB *p_clrcb = &bta_gattc_cb.cl_rcb[0];
131 for (i = 0; i < BTA_GATTC_CL_MAX; i ++, p_clrcb ++)
133 if (p_clrcb->in_use &&
134 p_clrcb->client_if == client_if)
139 /*******************************************************************************
141 ** Function bta_gattc_num_reg_app
143 ** Description find the number of registered application.
145 ** Returns pointer to the regcb
147 *******************************************************************************/
148 UINT8 bta_gattc_num_reg_app(void)
152 for (i = 0; i < BTA_GATTC_CL_MAX; i ++)
154 if (bta_gattc_cb.cl_rcb[i].in_use)
159 /*******************************************************************************
161 ** Function bta_gattc_find_clcb_by_cif
163 ** Description get clcb by client interface and remote bd adddress
165 ** Returns pointer to the clcb
167 *******************************************************************************/
168 tBTA_GATTC_CLCB * bta_gattc_find_clcb_by_cif (UINT8 client_if, BD_ADDR remote_bda,
169 tBTA_TRANSPORT transport)
171 tBTA_GATTC_CLCB *p_clcb = &bta_gattc_cb.clcb[0];
174 for (i = 0; i < BTA_GATTC_CLCB_MAX; i ++, p_clcb ++)
176 if (p_clcb->in_use &&
177 p_clcb->p_rcb->client_if == client_if &&
178 p_clcb->transport == transport &&
179 bdcmp(p_clcb->bda, remote_bda) == 0)
184 /*******************************************************************************
186 ** Function bta_gattc_find_clcb_by_conn_id
188 ** Description get clcb by connection ID
190 ** Returns pointer to the clcb
192 *******************************************************************************/
193 tBTA_GATTC_CLCB * bta_gattc_find_clcb_by_conn_id (UINT16 conn_id)
195 tBTA_GATTC_CLCB *p_clcb = &bta_gattc_cb.clcb[0];
198 for (i = 0; i < BTA_GATTC_CLCB_MAX; i ++, p_clcb ++)
200 if (p_clcb->in_use &&
201 p_clcb->bta_conn_id == conn_id)
207 /*******************************************************************************
209 ** Function bta_gattc_clcb_alloc
211 ** Description allocate CLCB
213 ** Returns pointer to the clcb
215 *******************************************************************************/
216 tBTA_GATTC_CLCB * bta_gattc_clcb_alloc(tBTA_GATTC_IF client_if, BD_ADDR remote_bda,
217 tBTA_TRANSPORT transport)
220 tBTA_GATTC_CLCB *p_clcb = NULL;
222 for (i_clcb = 0; i_clcb < BTA_GATTC_CLCB_MAX; i_clcb++)
224 if (!bta_gattc_cb.clcb[i_clcb].in_use)
226 #if BTA_GATT_DEBUG == TRUE
227 APPL_TRACE_DEBUG("bta_gattc_clcb_alloc: found clcb[%d] available",i_clcb);
229 p_clcb = &bta_gattc_cb.clcb[i_clcb];
230 p_clcb->in_use = TRUE;
231 p_clcb->status = BTA_GATT_OK;
232 p_clcb->transport = transport;
233 bdcpy(p_clcb->bda, remote_bda);
235 p_clcb->p_rcb = bta_gattc_cl_get_regcb(client_if);
237 if ((p_clcb->p_srcb = bta_gattc_find_srcb(remote_bda)) == NULL)
238 p_clcb->p_srcb = bta_gattc_srcb_alloc(remote_bda);
240 if (p_clcb->p_rcb != NULL && p_clcb->p_srcb != NULL)
242 p_clcb->p_srcb->num_clcb ++;
243 p_clcb->p_rcb->num_clcb ++;
247 /* release this clcb if clcb or srcb allocation failed */
248 p_clcb->in_use = FALSE;
256 /*******************************************************************************
258 ** Function bta_gattc_find_alloc_clcb
260 ** Description find or allocate CLCB if not found.
262 ** Returns pointer to the clcb
264 *******************************************************************************/
265 tBTA_GATTC_CLCB *bta_gattc_find_alloc_clcb(tBTA_GATTC_IF client_if, BD_ADDR remote_bda,
266 tBTA_TRANSPORT transport)
268 tBTA_GATTC_CLCB *p_clcb ;
270 if ((p_clcb = bta_gattc_find_clcb_by_cif(client_if, remote_bda, transport)) == NULL)
272 p_clcb = bta_gattc_clcb_alloc(client_if, remote_bda, transport);
277 /*******************************************************************************
279 ** Function bta_gattc_clcb_dealloc
281 ** Description Deallocte a clcb
283 ** Returns pointer to the clcb
285 *******************************************************************************/
286 void bta_gattc_clcb_dealloc(tBTA_GATTC_CLCB *p_clcb)
288 tBTA_GATTC_SERV *p_srcb = NULL;
292 p_srcb = p_clcb->p_srcb;
293 if (p_srcb->num_clcb)
296 if (p_clcb->p_rcb->num_clcb)
297 p_clcb->p_rcb->num_clcb --;
299 /* if the srcb is no longer needed, reset the state */
300 if ( p_srcb->num_clcb == 0)
302 p_srcb->connected = FALSE;
303 p_srcb->state = BTA_GATTC_SERV_IDLE;
307 osi_free_and_reset((void **)&p_clcb->p_q_cmd);
308 memset(p_clcb, 0, sizeof(tBTA_GATTC_CLCB));
310 APPL_TRACE_ERROR("bta_gattc_clcb_dealloc p_clcb=NULL");
314 /*******************************************************************************
316 ** Function bta_gattc_find_srcb
318 ** Description find server cache by remote bd address currently in use
320 ** Returns pointer to the server cache.
322 *******************************************************************************/
323 tBTA_GATTC_SERV * bta_gattc_find_srcb(BD_ADDR bda)
325 tBTA_GATTC_SERV *p_srcb = &bta_gattc_cb.known_server[0];
328 for (i = 0; i < BTA_GATTC_KNOWN_SR_MAX; i ++, p_srcb ++)
330 if (p_srcb->in_use && bdcmp(p_srcb->server_bda, bda) == 0)
336 /*******************************************************************************
338 ** Function bta_gattc_find_srvr_cache
340 ** Description find server cache by remote bd address
342 ** Returns pointer to the server cache.
344 *******************************************************************************/
345 tBTA_GATTC_SERV * bta_gattc_find_srvr_cache(BD_ADDR bda)
347 tBTA_GATTC_SERV *p_srcb = &bta_gattc_cb.known_server[0];
350 for (i = 0; i < BTA_GATTC_KNOWN_SR_MAX; i ++, p_srcb ++)
352 if (bdcmp(p_srcb->server_bda, bda) == 0)
357 /*******************************************************************************
359 ** Function bta_gattc_find_scb_by_cid
361 ** Description find server control block by connection ID
363 ** Returns pointer to the server cache.
365 *******************************************************************************/
366 tBTA_GATTC_SERV * bta_gattc_find_scb_by_cid (UINT16 conn_id)
368 tBTA_GATTC_CLCB *p_clcb = bta_gattc_find_clcb_by_conn_id(conn_id);
371 return p_clcb->p_srcb;
375 /*******************************************************************************
377 ** Function bta_gattc_srcb_alloc
379 ** Description allocate server cache control block
381 ** Returns pointer to the server cache.
383 *******************************************************************************/
384 tBTA_GATTC_SERV * bta_gattc_srcb_alloc(BD_ADDR bda)
386 tBTA_GATTC_SERV *p_tcb = &bta_gattc_cb.known_server[0],
388 BOOLEAN found = FALSE;
391 for (i = 0; i < BTA_GATTC_KNOWN_SR_MAX; i ++, p_tcb ++)
398 else if (!p_tcb->connected)
404 /* if not found, try to recycle one known device */
405 if (!found && !p_recycle)
407 else if (!found && p_recycle)
412 if (p_tcb->p_srvc_cache != NULL)
413 list_free(p_tcb->p_srvc_cache);
415 osi_free_and_reset((void **)&p_tcb->p_srvc_list);
416 memset(p_tcb, 0 , sizeof(tBTA_GATTC_SERV));
418 p_tcb->in_use = TRUE;
419 bdcpy(p_tcb->server_bda, bda);
423 /*******************************************************************************
425 ** Function bta_gattc_enqueue
427 ** Description enqueue a client request in clcb.
429 ** Returns success or failure.
431 *******************************************************************************/
432 BOOLEAN bta_gattc_enqueue(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
435 if (p_clcb->p_q_cmd == NULL)
437 p_clcb->p_q_cmd = p_data;
441 APPL_TRACE_ERROR ("%s: already has a pending command!!", __func__);
442 /* skip the callback now. ----- need to send callback ? */
446 /*******************************************************************************
448 ** Function bta_gattc_check_notif_registry
450 ** Description check if the service notificaition has been registered.
454 *******************************************************************************/
455 BOOLEAN bta_gattc_check_notif_registry(tBTA_GATTC_RCB *p_clreg, tBTA_GATTC_SERV *p_srcb,
456 tBTA_GATTC_NOTIFY *p_notify)
460 for (i = 0 ; i < BTA_GATTC_NOTIF_REG_MAX; i ++)
462 if (p_clreg->notif_reg[i].in_use &&
463 bdcmp(p_clreg->notif_reg[i].remote_bda, p_srcb->server_bda) == 0 &&
464 p_clreg->notif_reg[i].handle == p_notify->handle)
466 APPL_TRACE_DEBUG("Notification registered!");
473 /*******************************************************************************
475 ** Function bta_gattc_clear_notif_registration
477 ** Description Clear up the notification registration information by BD_ADDR.
478 ** Where handle is between start_handle and end_handle, and
479 ** start_handle and end_handle are boundaries of service
480 ** containing characteristic.
484 *******************************************************************************/
485 void bta_gattc_clear_notif_registration(tBTA_GATTC_SERV *p_srcb, UINT16 conn_id,
486 UINT16 start_handle, UINT16 end_handle)
489 tBTA_GATTC_IF gatt_if;
490 tBTA_GATTC_RCB *p_clrcb ;
492 tGATT_TRANSPORT transport;
495 if (GATT_GetConnectionInfor(conn_id, &gatt_if, remote_bda, &transport)) {
496 if ((p_clrcb = bta_gattc_cl_get_regcb(gatt_if)) != NULL) {
497 for (i = 0 ; i < BTA_GATTC_NOTIF_REG_MAX; i ++) {
498 if (p_clrcb->notif_reg[i].in_use &&
499 !bdcmp(p_clrcb->notif_reg[i].remote_bda, remote_bda))
501 /* It's enough to get service or characteristic handle, as
502 * clear boundaries are always around service.
504 handle = p_clrcb->notif_reg[i].handle;
505 if (handle >= start_handle && handle <= end_handle)
506 memset(&p_clrcb->notif_reg[i], 0, sizeof(tBTA_GATTC_NOTIF_REG));
510 APPL_TRACE_ERROR("can not clear indication/notif registration for unknown app");
515 /*******************************************************************************
517 ** Function bta_gattc_mark_bg_conn
519 ** Description mark background connection status when a bg connection is initiated
522 ** Returns TRUE if success; FALSE otherwise.
524 *******************************************************************************/
525 BOOLEAN bta_gattc_mark_bg_conn (tBTA_GATTC_IF client_if, BD_ADDR_PTR remote_bda_ptr,
526 BOOLEAN add, BOOLEAN is_listen)
528 tBTA_GATTC_BG_TCK *p_bg_tck = &bta_gattc_cb.bg_track[0];
530 tBTA_GATTC_CIF_MASK *p_cif_mask;
532 for (i = 0; i < BTA_GATTC_KNOWN_SR_MAX; i ++, p_bg_tck ++)
534 if (p_bg_tck->in_use &&
535 ((remote_bda_ptr != NULL && bdcmp(p_bg_tck->remote_bda, remote_bda_ptr) == 0) ||
536 (remote_bda_ptr == NULL && bdcmp(p_bg_tck->remote_bda, dummy_bda) == 0)))
538 p_cif_mask = is_listen ? &p_bg_tck->cif_adv_mask : &p_bg_tck->cif_mask;
541 /* mask on the cif bit */
542 *p_cif_mask |= (1 <<(client_if - 1));
546 *p_cif_mask &= (~(1 <<(client_if - 1)));
550 /* no BG connection for this device, make it available */
551 if (p_bg_tck->cif_mask == 0 && p_bg_tck->cif_adv_mask == 0)
553 memset(p_bg_tck, 0, sizeof(tBTA_GATTC_BG_TCK));
563 APPL_TRACE_ERROR("%s unable to find the bg connection mask for: %s", __func__,
564 bdaddr_to_string((bt_bdaddr_t *)remote_bda_ptr, bdstr, sizeof(bdstr)));
568 else /* adding a new device mask */
570 for (i = 0, p_bg_tck = &bta_gattc_cb.bg_track[0];
571 i < BTA_GATTC_KNOWN_SR_MAX; i ++, p_bg_tck ++)
573 if (!p_bg_tck->in_use)
575 p_bg_tck->in_use = TRUE;
577 bdcpy(p_bg_tck->remote_bda, remote_bda_ptr);
579 bdcpy(p_bg_tck->remote_bda, dummy_bda);
581 p_cif_mask = is_listen ? &p_bg_tck->cif_adv_mask : &p_bg_tck->cif_mask;
583 *p_cif_mask = (1 <<(client_if - 1));
587 APPL_TRACE_ERROR("no available space to mark the bg connection status");
591 /*******************************************************************************
593 ** Function bta_gattc_check_bg_conn
595 ** Description check if this is a background connection background connection.
597 ** Returns TRUE if success; FALSE otherwise.
599 *******************************************************************************/
600 BOOLEAN bta_gattc_check_bg_conn (tBTA_GATTC_IF client_if, BD_ADDR remote_bda, UINT8 role)
602 tBTA_GATTC_BG_TCK *p_bg_tck = &bta_gattc_cb.bg_track[0];
604 BOOLEAN is_bg_conn = FALSE;
606 for (i = 0; i < BTA_GATTC_KNOWN_SR_MAX && !is_bg_conn; i ++, p_bg_tck ++)
608 if (p_bg_tck->in_use &&
609 (bdcmp(p_bg_tck->remote_bda, remote_bda) == 0 ||
610 bdcmp(p_bg_tck->remote_bda, dummy_bda) == 0))
612 if (((p_bg_tck->cif_mask &(1 <<(client_if - 1))) != 0) &&
613 role == HCI_ROLE_MASTER)
616 if (((p_bg_tck->cif_adv_mask &(1 <<(client_if - 1))) != 0) &&
617 role == HCI_ROLE_SLAVE)
623 /*******************************************************************************
625 ** Function bta_gattc_send_open_cback
627 ** Description send open callback
631 *******************************************************************************/
632 void bta_gattc_send_open_cback( tBTA_GATTC_RCB *p_clreg, tBTA_GATT_STATUS status,
633 BD_ADDR remote_bda, UINT16 conn_id,
634 tBTA_TRANSPORT transport, UINT16 mtu)
638 if (p_clreg->p_cback)
640 memset(&cb_data, 0, sizeof(tBTA_GATTC));
642 cb_data.open.status = status;
643 cb_data.open.client_if = p_clreg->client_if;
644 cb_data.open.conn_id = conn_id;
645 cb_data.open.mtu = mtu;
646 cb_data.open.transport = transport;
647 bdcpy(cb_data.open.remote_bda, remote_bda);
649 (*p_clreg->p_cback)(BTA_GATTC_OPEN_EVT, &cb_data);
652 /*******************************************************************************
654 ** Function bta_gattc_conn_alloc
656 ** Description allocate connection tracking spot
658 ** Returns pointer to the clcb
660 *******************************************************************************/
661 tBTA_GATTC_CONN * bta_gattc_conn_alloc(BD_ADDR remote_bda)
664 tBTA_GATTC_CONN *p_conn = &bta_gattc_cb.conn_track[0];
666 for (i_conn = 0; i_conn < BTA_GATTC_CONN_MAX; i_conn++, p_conn ++)
670 #if BTA_GATT_DEBUG == TRUE
671 APPL_TRACE_DEBUG("bta_gattc_conn_alloc: found conn_track[%d] available",i_conn);
673 p_conn->in_use = TRUE;
674 bdcpy(p_conn->remote_bda, remote_bda);
681 /*******************************************************************************
683 ** Function bta_gattc_conn_find
685 ** Description allocate connection tracking spot
687 ** Returns pointer to the clcb
689 *******************************************************************************/
690 tBTA_GATTC_CONN * bta_gattc_conn_find(BD_ADDR remote_bda)
693 tBTA_GATTC_CONN *p_conn = &bta_gattc_cb.conn_track[0];
695 for (i_conn = 0; i_conn < BTA_GATTC_CONN_MAX; i_conn++, p_conn ++)
697 if (p_conn->in_use && bdcmp(remote_bda, p_conn->remote_bda) == 0)
699 #if BTA_GATT_DEBUG == TRUE
700 APPL_TRACE_DEBUG("bta_gattc_conn_find: found conn_track[%d] matched",i_conn);
708 /*******************************************************************************
710 ** Function bta_gattc_conn_find_alloc
712 ** Description find or allocate connection tracking spot
714 ** Returns pointer to the clcb
716 *******************************************************************************/
717 tBTA_GATTC_CONN * bta_gattc_conn_find_alloc(BD_ADDR remote_bda)
719 tBTA_GATTC_CONN *p_conn = bta_gattc_conn_find (remote_bda);
723 p_conn = bta_gattc_conn_alloc(remote_bda);
728 /*******************************************************************************
730 ** Function bta_gattc_conn_dealloc
732 ** Description de-allocate connection tracking spot
734 ** Returns pointer to the clcb
736 *******************************************************************************/
737 BOOLEAN bta_gattc_conn_dealloc(BD_ADDR remote_bda)
739 tBTA_GATTC_CONN *p_conn = bta_gattc_conn_find (remote_bda);
743 p_conn->in_use = FALSE;
744 memset(p_conn->remote_bda, 0, BD_ADDR_LEN);
750 /*******************************************************************************
752 ** Function bta_gattc_find_int_conn_clcb
754 ** Description try to locate a clcb when an internal connecion event arrives.
756 ** Returns pointer to the clcb
758 *******************************************************************************/
759 tBTA_GATTC_CLCB * bta_gattc_find_int_conn_clcb(tBTA_GATTC_DATA *p_msg)
761 tBTA_GATTC_CLCB *p_clcb = NULL;
763 if (p_msg->int_conn.role == HCI_ROLE_SLAVE)
764 bta_gattc_conn_find_alloc(p_msg->int_conn.remote_bda);
766 /* try to locate a logic channel */
767 if ((p_clcb = bta_gattc_find_clcb_by_cif(p_msg->int_conn.client_if,
768 p_msg->int_conn.remote_bda,
769 p_msg->int_conn.transport)) == NULL)
771 /* for a background connection or listening connection */
772 if (/*p_msg->int_conn.role == HCI_ROLE_SLAVE || */
773 bta_gattc_check_bg_conn(p_msg->int_conn.client_if,
774 p_msg->int_conn.remote_bda,
775 p_msg->int_conn.role))
777 /* allocate a new channel */
778 p_clcb = bta_gattc_clcb_alloc(p_msg->int_conn.client_if,
779 p_msg->int_conn.remote_bda,
780 p_msg->int_conn.transport);
786 /*******************************************************************************
788 ** Function bta_gattc_find_int_disconn_clcb
790 ** Description try to locate a clcb when an internal disconnect callback arrives.
792 ** Returns pointer to the clcb
794 *******************************************************************************/
795 tBTA_GATTC_CLCB * bta_gattc_find_int_disconn_clcb(tBTA_GATTC_DATA *p_msg)
797 tBTA_GATTC_CLCB *p_clcb = NULL;
799 bta_gattc_conn_dealloc(p_msg->int_conn.remote_bda);
800 if ((p_clcb = bta_gattc_find_clcb_by_conn_id(p_msg->int_conn.hdr.layer_specific)) == NULL)
802 /* connection attempt failed, send connection callback event */
803 p_clcb = bta_gattc_find_clcb_by_cif(p_msg->int_conn.client_if,
804 p_msg->int_conn.remote_bda,
805 p_msg->int_conn.transport);
809 APPL_TRACE_DEBUG(" disconnection ID: [%d] not used by BTA",
810 p_msg->int_conn.hdr.layer_specific);
815 #endif /* BTA_GATT_INCLUDED */