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 action functions for the state
24 ******************************************************************************/
26 #define LOG_TAG "bt_bta_gattc"
30 #include "bt_target.h"
31 #include "bta_gattc_int.h"
33 #include "btif/include/btif_debug_conn.h"
34 #include "bt_common.h"
36 #include "osi/include/log.h"
37 #include "stack/l2cap/l2c_int.h"
40 #if (defined BTA_HH_LE_INCLUDED && BTA_HH_LE_INCLUDED == TRUE)
41 #include "bta_hh_int.h"
44 #if BTA_GATT_INCLUDED && BLE_INCLUDED == TRUE
46 /*****************************************************************************
48 *****************************************************************************/
49 static void bta_gattc_conn_cback(tGATT_IF gattc_if, BD_ADDR bda, UINT16 conn_id,
50 BOOLEAN connected, tGATT_DISCONN_REASON reason,
51 tBT_TRANSPORT transport);
53 static void bta_gattc_cmpl_cback(UINT16 conn_id, tGATTC_OPTYPE op, tGATT_STATUS status,
54 tGATT_CL_COMPLETE *p_data);
55 static void bta_gattc_cmpl_sendmsg(UINT16 conn_id, tGATTC_OPTYPE op,
56 tBTA_GATT_STATUS status,
57 tGATT_CL_COMPLETE *p_data);
59 static void bta_gattc_deregister_cmpl(tBTA_GATTC_RCB *p_clreg);
60 static void bta_gattc_enc_cmpl_cback(tGATT_IF gattc_if, BD_ADDR bda);
61 static void bta_gattc_cong_cback (UINT16 conn_id, BOOLEAN congested);
63 static tGATT_CBACK bta_gattc_cl_cback =
67 bta_gattc_disc_res_cback,
68 bta_gattc_disc_cmpl_cback,
70 bta_gattc_enc_cmpl_cback,
74 /* opcode(tGATTC_OPTYPE) order has to be comply with internal event order */
75 static UINT16 bta_gattc_opcode_to_int_evt[] =
77 BTA_GATTC_API_READ_EVT,
78 BTA_GATTC_API_WRITE_EVT,
79 BTA_GATTC_API_EXEC_EVT,
80 BTA_GATTC_API_CFG_MTU_EVT
83 #if (BT_TRACE_VERBOSE == TRUE)
84 static const char *bta_gattc_op_code_name[] =
96 /*****************************************************************************
98 *****************************************************************************/
101 void bta_gattc_reset_discover_st(tBTA_GATTC_SERV *p_srcb, tBTA_GATT_STATUS status);
103 /*******************************************************************************
105 ** Function bta_gattc_enable
107 ** Description Enables GATTC module
112 *******************************************************************************/
113 static void bta_gattc_enable(tBTA_GATTC_CB *p_cb)
115 APPL_TRACE_DEBUG("bta_gattc_enable");
117 if (p_cb->state == BTA_GATTC_STATE_DISABLED)
119 /* initialize control block */
120 memset(&bta_gattc_cb, 0, sizeof(tBTA_GATTC_CB));
121 p_cb->state = BTA_GATTC_STATE_ENABLED;
125 APPL_TRACE_DEBUG("GATTC is arelady enabled");
129 /*******************************************************************************
131 ** Function bta_gattc_disable
133 ** Description Disable GATTC module by cleaning up all active connections
134 ** and deregister all application.
138 *******************************************************************************/
139 void bta_gattc_disable(tBTA_GATTC_CB *p_cb)
143 APPL_TRACE_DEBUG("bta_gattc_disable");
145 if (p_cb->state != BTA_GATTC_STATE_ENABLED)
147 APPL_TRACE_ERROR("not enabled or disable in pogress");
151 for (i = 0; i <BTA_GATTC_CL_MAX; i ++)
153 if (p_cb->cl_rcb[i].in_use)
155 p_cb->state = BTA_GATTC_STATE_DISABLING;
156 /* don't deregister HH GATT IF */
157 /* HH GATT IF will be deregistered by bta_hh_le_deregister when disable HH */
158 #if (defined BTA_HH_LE_INCLUDED && BTA_HH_LE_INCLUDED == TRUE)
159 if (!bta_hh_le_is_hh_gatt_if(p_cb->cl_rcb[i].client_if)) {
161 bta_gattc_deregister(p_cb, &p_cb->cl_rcb[i]);
162 #if (defined BTA_HH_LE_INCLUDED && BTA_HH_LE_INCLUDED == TRUE)
168 /* no registered apps, indicate disable completed */
169 if (p_cb->state != BTA_GATTC_STATE_DISABLING)
171 p_cb->state = BTA_GATTC_STATE_DISABLED;
172 memset(p_cb, 0, sizeof(tBTA_GATTC_CB));
176 /*******************************************************************************
178 ** Function bta_gattc_register
180 ** Description Register a GATT client application with BTA.
184 *******************************************************************************/
185 void bta_gattc_register(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA *p_data)
189 tBT_UUID *p_app_uuid = &p_data->api_reg.app_uuid;
190 tBTA_GATT_STATUS status = BTA_GATT_NO_RESOURCES;
192 APPL_TRACE_DEBUG("bta_gattc_register state %d",p_cb->state);
193 memset(&cb_data, 0, sizeof(cb_data));
194 cb_data.reg_oper.status = BTA_GATT_NO_RESOURCES;
196 /* check if GATTC module is already enabled . Else enable */
197 if (p_cb->state == BTA_GATTC_STATE_DISABLED)
199 bta_gattc_enable (p_cb);
201 /* todo need to check duplicate uuid */
202 for (i = 0; i < BTA_GATTC_CL_MAX; i ++)
204 if (!p_cb->cl_rcb[i].in_use)
206 if ((p_app_uuid == NULL) || (p_cb->cl_rcb[i].client_if = GATT_Register(p_app_uuid, &bta_gattc_cl_cback)) == 0)
208 APPL_TRACE_ERROR("Register with GATT stack failed.");
209 status = BTA_GATT_ERROR;
213 p_cb->cl_rcb[i].in_use = TRUE;
214 p_cb->cl_rcb[i].p_cback = p_data->api_reg.p_cback;
215 memcpy(&p_cb->cl_rcb[i].app_uuid, p_app_uuid, sizeof(tBT_UUID));
217 /* BTA use the same client interface as BTE GATT statck */
218 cb_data.reg_oper.client_if = p_cb->cl_rcb[i].client_if;
220 tBTA_GATTC_INT_START_IF *p_buf =
221 (tBTA_GATTC_INT_START_IF *)osi_malloc(sizeof(tBTA_GATTC_INT_START_IF));
222 p_buf->hdr.event = BTA_GATTC_INT_START_IF_EVT;
223 p_buf->client_if = p_cb->cl_rcb[i].client_if;
225 bta_sys_sendmsg(p_buf);
226 status = BTA_GATT_OK;
232 /* callback with register event */
233 if (p_data->api_reg.p_cback)
235 if (p_app_uuid != NULL)
236 memcpy(&(cb_data.reg_oper.app_uuid),p_app_uuid,sizeof(tBT_UUID));
238 cb_data.reg_oper.status = status;
239 (*p_data->api_reg.p_cback)(BTA_GATTC_REG_EVT, (tBTA_GATTC *)&cb_data);
242 /*******************************************************************************
244 ** Function bta_gattc_start_if
246 ** Description start an application interface.
250 *******************************************************************************/
251 void bta_gattc_start_if(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA *p_msg)
255 if (bta_gattc_cl_get_regcb(p_msg->int_start_if.client_if) !=NULL )
257 GATT_StartIf(p_msg->int_start_if.client_if);
261 APPL_TRACE_ERROR("Unable to start app.: Unknown interface =%d",p_msg->int_start_if.client_if );
264 /*******************************************************************************
266 ** Function bta_gattc_deregister
268 ** Description De-Register a GATT client application with BTA.
272 *******************************************************************************/
273 void bta_gattc_deregister(tBTA_GATTC_CB *p_cb, tBTA_GATTC_RCB *p_clreg)
280 /* remove bg connection associated with this rcb */
281 for (i = 0; i < BTA_GATTC_KNOWN_SR_MAX; i ++)
283 if (p_cb->bg_track[i].in_use)
285 if (p_cb->bg_track[i].cif_mask & (1 <<(p_clreg->client_if - 1)))
287 bta_gattc_mark_bg_conn(p_clreg->client_if, p_cb->bg_track[i].remote_bda, FALSE, FALSE);
288 GATT_CancelConnect(p_clreg->client_if, p_cb->bg_track[i].remote_bda, FALSE);
290 if (p_cb->bg_track[i].cif_adv_mask & (1 <<(p_clreg->client_if - 1)))
292 bta_gattc_mark_bg_conn(p_clreg->client_if, p_cb->bg_track[i].remote_bda, FALSE, TRUE);
297 if (p_clreg->num_clcb > 0)
299 /* close all CLCB related to this app */
300 for (i= 0; i < BTA_GATTC_CLCB_MAX; i ++)
302 if (p_cb->clcb[i].in_use && (p_cb->clcb[i].p_rcb == p_clreg))
304 p_clreg->dereg_pending = TRUE;
306 buf.event = BTA_GATTC_API_CLOSE_EVT;
307 buf.layer_specific = p_cb->clcb[i].bta_conn_id;
308 bta_gattc_close(&p_cb->clcb[i], (tBTA_GATTC_DATA *)&buf) ;
313 bta_gattc_deregister_cmpl(p_clreg);
317 APPL_TRACE_ERROR("bta_gattc_deregister Deregister Failedm unknown client cif");
320 /*******************************************************************************
322 ** Function bta_gattc_process_api_open
324 ** Description process connect API request.
328 *******************************************************************************/
329 void bta_gattc_process_api_open (tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA * p_msg)
331 UINT16 event = ((BT_HDR *)p_msg)->event;
332 tBTA_GATTC_CLCB *p_clcb = NULL;
333 tBTA_GATTC_RCB *p_clreg = bta_gattc_cl_get_regcb(p_msg->api_conn.client_if);
338 if (p_msg->api_conn.is_direct)
340 if ((p_clcb = bta_gattc_find_alloc_clcb(p_msg->api_conn.client_if,
341 p_msg->api_conn.remote_bda,
342 p_msg->api_conn.transport)) != NULL)
344 bta_gattc_sm_execute(p_clcb, event, p_msg);
348 APPL_TRACE_ERROR("No resources to open a new connection.");
350 bta_gattc_send_open_cback(p_clreg,
351 BTA_GATT_NO_RESOURCES,
352 p_msg->api_conn.remote_bda,
353 BTA_GATT_INVALID_CONN_ID,
354 p_msg->api_conn.transport, 0);
359 bta_gattc_init_bk_conn(&p_msg->api_conn, p_clreg);
364 APPL_TRACE_ERROR("bta_gattc_process_api_open Failed, unknown client_if: %d",
365 p_msg->api_conn.client_if);
368 /*******************************************************************************
370 ** Function bta_gattc_process_api_open_cancel
372 ** Description process connect API request.
376 *******************************************************************************/
377 void bta_gattc_process_api_open_cancel (tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA * p_msg)
379 UINT16 event = ((BT_HDR *)p_msg)->event;
380 tBTA_GATTC_CLCB *p_clcb = NULL;
381 tBTA_GATTC_RCB *p_clreg;
385 if (p_msg->api_cancel_conn.is_direct)
387 if ((p_clcb = bta_gattc_find_clcb_by_cif(p_msg->api_cancel_conn.client_if,
388 p_msg->api_cancel_conn.remote_bda,
389 BTA_GATT_TRANSPORT_LE)) != NULL)
391 bta_gattc_sm_execute(p_clcb, event, p_msg);
395 APPL_TRACE_ERROR("No such connection need to be cancelled");
397 p_clreg = bta_gattc_cl_get_regcb(p_msg->api_cancel_conn.client_if);
399 if (p_clreg && p_clreg->p_cback)
401 cb_data.status = BTA_GATT_ERROR;
402 (*p_clreg->p_cback)(BTA_GATTC_CANCEL_OPEN_EVT, &cb_data);
408 bta_gattc_cancel_bk_conn(&p_msg->api_cancel_conn);
413 /*******************************************************************************
415 ** Function bta_gattc_process_enc_cmpl
417 ** Description process encryption complete message.
421 *******************************************************************************/
422 void bta_gattc_process_enc_cmpl(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA *p_msg)
424 tBTA_GATTC_RCB *p_clreg;
428 p_clreg = bta_gattc_cl_get_regcb(p_msg->enc_cmpl.client_if);
430 if (p_clreg && p_clreg->p_cback)
432 memset(&cb_data, 0, sizeof(tBTA_GATTC));
434 cb_data.enc_cmpl.client_if = p_msg->enc_cmpl.client_if;
435 bdcpy(cb_data.enc_cmpl.remote_bda, p_msg->enc_cmpl.remote_bda);
437 (*p_clreg->p_cback)(BTA_GATTC_ENC_CMPL_CB_EVT, &cb_data);
441 /*******************************************************************************
443 ** Function bta_gattc_cancel_open_error
449 *******************************************************************************/
450 void bta_gattc_cancel_open_error(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
455 cb_data.status=BTA_GATT_ERROR;
457 if ( p_clcb && p_clcb->p_rcb && p_clcb->p_rcb->p_cback )
458 (*p_clcb->p_rcb->p_cback)(BTA_GATTC_CANCEL_OPEN_EVT, &cb_data);
461 /*******************************************************************************
463 ** Function bta_gattc_open_error
469 *******************************************************************************/
470 void bta_gattc_open_error(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
474 APPL_TRACE_ERROR("Connection already opened. wrong state");
476 bta_gattc_send_open_cback(p_clcb->p_rcb,
483 /*******************************************************************************
485 ** Function bta_gattc_open_fail
491 *******************************************************************************/
492 void bta_gattc_open_fail(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
496 bta_gattc_send_open_cback(p_clcb->p_rcb,
502 /* open failure, remove clcb */
503 bta_gattc_clcb_dealloc(p_clcb);
506 /*******************************************************************************
508 ** Function bta_gattc_open
510 ** Description Process API connection function.
514 *******************************************************************************/
515 void bta_gattc_open(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
517 tBTA_GATTC_DATA gattc_data;
519 /* open/hold a connection */
520 if (!GATT_Connect(p_clcb->p_rcb->client_if, p_data->api_conn.remote_bda,
521 TRUE, p_data->api_conn.transport))
523 APPL_TRACE_ERROR("Connection open failure");
525 bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_OPEN_FAIL_EVT, p_data);
529 /* a connected remote device */
530 if (GATT_GetConnIdIfConnected(p_clcb->p_rcb->client_if,
531 p_data->api_conn.remote_bda,
532 &p_clcb->bta_conn_id,
533 p_data->api_conn.transport))
535 gattc_data.int_conn.hdr.layer_specific = p_clcb->bta_conn_id;
537 bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_CONN_EVT, &gattc_data);
539 /* else wait for the callback event */
542 /*******************************************************************************
544 ** Function bta_gattc_init_bk_conn
546 ** Description Process API Open for a background connection
550 *******************************************************************************/
551 void bta_gattc_init_bk_conn(tBTA_GATTC_API_OPEN *p_data, tBTA_GATTC_RCB *p_clreg)
553 tBTA_GATT_STATUS status = BTA_GATT_NO_RESOURCES;
555 tBTA_GATTC_CLCB *p_clcb;
556 tBTA_GATTC_DATA gattc_data;
558 if (bta_gattc_mark_bg_conn(p_data->client_if, p_data->remote_bda, TRUE, FALSE))
560 /* always call open to hold a connection */
561 if (!GATT_Connect(p_data->client_if, p_data->remote_bda, FALSE, p_data->transport))
563 uint8_t *bda = (uint8_t *)p_data->remote_bda;
564 status = BTA_GATT_ERROR;
565 APPL_TRACE_ERROR("%s unable to connect to remote bd_addr:%02x:%02x:%02x:%02x:%02x:%02x",
566 __func__, bda[0], bda[1], bda[2], bda[3], bda[4], bda[5]);
571 status = BTA_GATT_OK;
573 /* if is a connected remote device */
574 if (GATT_GetConnIdIfConnected(p_data->client_if,
579 if ((p_clcb = bta_gattc_find_alloc_clcb(p_data->client_if, p_data->remote_bda,
580 BTA_GATT_TRANSPORT_LE)) != NULL)
582 gattc_data.hdr.layer_specific = p_clcb->bta_conn_id = conn_id;
584 /* open connection */
585 bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_CONN_EVT, &gattc_data);
586 status = BTA_GATT_OK;
592 /* open failure, report OPEN_EVT */
593 if (status != BTA_GATT_OK)
595 bta_gattc_send_open_cback(p_clreg, status, p_data->remote_bda,
596 BTA_GATT_INVALID_CONN_ID, BTA_GATT_TRANSPORT_LE, 0);
599 /*******************************************************************************
601 ** Function bta_gattc_cancel_bk_conn
603 ** Description Process API Cancel Open for a background connection
607 *******************************************************************************/
608 void bta_gattc_cancel_bk_conn(tBTA_GATTC_API_CANCEL_OPEN *p_data)
610 tBTA_GATTC_RCB *p_clreg;
612 cb_data.status = BTA_GATT_ERROR;
614 /* remove the device from the bg connection mask */
615 if (bta_gattc_mark_bg_conn(p_data->client_if, p_data->remote_bda, FALSE, FALSE))
617 if (GATT_CancelConnect(p_data->client_if, p_data->remote_bda, FALSE))
619 cb_data.status = BTA_GATT_OK;
623 APPL_TRACE_ERROR("bta_gattc_cancel_bk_conn failed");
626 p_clreg = bta_gattc_cl_get_regcb(p_data->client_if);
628 if (p_clreg && p_clreg->p_cback)
630 (*p_clreg->p_cback)(BTA_GATTC_CANCEL_OPEN_EVT, &cb_data);
634 /*******************************************************************************
636 ** Function bta_gattc_int_cancel_open_ok
642 *******************************************************************************/
643 void bta_gattc_cancel_open_ok(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
648 if ( p_clcb->p_rcb->p_cback )
650 cb_data.status = BTA_GATT_OK;
651 (*p_clcb->p_rcb->p_cback)(BTA_GATTC_CANCEL_OPEN_EVT, &cb_data);
654 bta_gattc_clcb_dealloc(p_clcb);
656 /*******************************************************************************
658 ** Function bta_gattc_cancel_open
664 *******************************************************************************/
665 void bta_gattc_cancel_open(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
669 if (GATT_CancelConnect(p_clcb->p_rcb->client_if, p_data->api_cancel_conn.remote_bda, TRUE))
671 bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_CANCEL_OPEN_OK_EVT, p_data);
675 if ( p_clcb->p_rcb->p_cback )
677 cb_data.status = BTA_GATT_ERROR;
678 (*p_clcb->p_rcb->p_cback)(BTA_GATTC_CANCEL_OPEN_EVT, &cb_data);
682 /*******************************************************************************
684 ** Function bta_gattc_conn
686 ** Description receive connection callback from stack
690 *******************************************************************************/
691 void bta_gattc_conn(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
693 tBTA_GATTC_IF gatt_if;
694 APPL_TRACE_DEBUG("bta_gattc_conn server cache state=%d",p_clcb->p_srcb->state);
698 APPL_TRACE_DEBUG("bta_gattc_conn conn_id=%d",p_data->hdr.layer_specific);
699 p_clcb->bta_conn_id = p_data->int_conn.hdr.layer_specific;
701 GATT_GetConnectionInfor(p_data->hdr.layer_specific,
702 &gatt_if, p_clcb->bda, &p_clcb->transport);
705 p_clcb->p_srcb->connected = TRUE;
707 if (p_clcb->p_srcb->mtu == 0)
708 p_clcb->p_srcb->mtu = GATT_DEF_BLE_MTU_SIZE;
710 /* start database cache if needed */
711 if (p_clcb->p_srcb->p_srvc_cache == NULL ||
712 p_clcb->p_srcb->state != BTA_GATTC_SERV_IDLE)
714 if (p_clcb->p_srcb->state == BTA_GATTC_SERV_IDLE)
716 p_clcb->p_srcb->state = BTA_GATTC_SERV_LOAD;
717 if (bta_gattc_cache_load(p_clcb)) {
718 bta_gattc_reset_discover_st(p_clcb->p_srcb, BTA_GATT_OK);
720 p_clcb->p_srcb->state = BTA_GATTC_SERV_DISC;
721 /* cache load failure, start discovery */
722 bta_gattc_start_discover(p_clcb, NULL);
725 else /* cache is building */
726 p_clcb->state = BTA_GATTC_DISCOVER_ST;
731 /* a pending service handle change indication */
732 if (p_clcb->p_srcb->srvc_hdl_chg)
734 p_clcb->p_srcb->srvc_hdl_chg = FALSE;
735 /* start discovery */
736 bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_DISCOVER_EVT, NULL);
742 /* there is no RM for GATT */
743 if (p_clcb->transport == BTA_TRANSPORT_BR_EDR)
744 bta_sys_conn_open(BTA_ID_GATTC, BTA_ALL_APP_ID, p_clcb->bda);
746 bta_gattc_send_open_cback(p_clcb->p_rcb,
751 p_clcb->p_srcb->mtu);
754 /*******************************************************************************
756 ** Function bta_gattc_close_fail
758 ** Description close a connection.
762 *******************************************************************************/
763 void bta_gattc_close_fail(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
767 if ( p_clcb->p_rcb->p_cback )
769 memset(&cb_data, 0, sizeof(tBTA_GATTC));
770 cb_data.close.client_if = p_clcb->p_rcb->client_if;
771 cb_data.close.conn_id = p_data->hdr.layer_specific;
772 bdcpy(cb_data.close.remote_bda, p_clcb->bda);
773 cb_data.close.status = BTA_GATT_ERROR;
774 cb_data.close.reason = BTA_GATT_CONN_NONE;
776 (*p_clcb->p_rcb->p_cback)(BTA_GATTC_CLOSE_EVT, &cb_data);
779 /*******************************************************************************
781 ** Function bta_gattc_api_close
783 ** Description close a GATTC connection.
787 *******************************************************************************/
788 void bta_gattc_close(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
790 tBTA_GATTC_CBACK *p_cback = p_clcb->p_rcb->p_cback;
791 tBTA_GATTC_RCB *p_clreg = p_clcb->p_rcb;
794 APPL_TRACE_DEBUG("bta_gattc_close conn_id=%d",p_clcb->bta_conn_id);
796 cb_data.close.client_if = p_clcb->p_rcb->client_if;
797 cb_data.close.conn_id = p_clcb->bta_conn_id;
798 cb_data.close.reason = p_clcb->reason;
799 cb_data.close.status = p_clcb->status;
800 bdcpy(cb_data.close.remote_bda, p_clcb->bda);
802 if (p_clcb->transport == BTA_TRANSPORT_BR_EDR)
803 bta_sys_conn_close( BTA_ID_GATTC ,BTA_ALL_APP_ID, p_clcb->bda);
805 bta_gattc_clcb_dealloc(p_clcb);
807 if (p_data->hdr.event == BTA_GATTC_API_CLOSE_EVT)
809 cb_data.close.status = GATT_Disconnect(p_data->hdr.layer_specific);
811 else if (p_data->hdr.event == BTA_GATTC_INT_DISCONN_EVT)
813 cb_data.close.status = p_data->int_conn.reason;
814 cb_data.close.reason = p_data->int_conn.reason;
818 (* p_cback)(BTA_GATTC_CLOSE_EVT, (tBTA_GATTC *)&cb_data);
820 if (p_clreg->num_clcb == 0 && p_clreg->dereg_pending)
822 bta_gattc_deregister_cmpl(p_clreg);
825 /*******************************************************************************
827 ** Function bta_gattc_reset_discover_st
829 ** Description when a SRCB finished discovery, tell all related clcb.
833 *******************************************************************************/
834 void bta_gattc_reset_discover_st(tBTA_GATTC_SERV *p_srcb, tBTA_GATT_STATUS status)
836 tBTA_GATTC_CB *p_cb = &bta_gattc_cb;
839 for (i = 0; i < BTA_GATTC_CLCB_MAX; i ++)
841 if (p_cb->clcb[i].p_srcb == p_srcb)
843 p_cb->clcb[i].status = status;
844 bta_gattc_sm_execute(&p_cb->clcb[i], BTA_GATTC_DISCOVER_CMPL_EVT, NULL);
848 /*******************************************************************************
850 ** Function bta_gattc_disc_close
852 ** Description close a GATTC connection while in discovery state.
856 *******************************************************************************/
857 void bta_gattc_disc_close(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
859 APPL_TRACE_DEBUG("%s: Discovery cancel conn_id=%d", __func__,
860 p_clcb->bta_conn_id);
862 if (p_clcb->disc_active)
863 bta_gattc_reset_discover_st(p_clcb->p_srcb, BTA_GATT_ERROR);
865 p_clcb->state = BTA_GATTC_CONN_ST;
867 // This function only gets called as the result of a BTA_GATTC_API_CLOSE_EVT
868 // while in the BTA_GATTC_DISCOVER_ST state. Once the state changes, the
869 // connection itself still needs to be closed to resolve the original event.
870 if (p_clcb->state == BTA_GATTC_CONN_ST)
872 APPL_TRACE_DEBUG("State is back to BTA_GATTC_CONN_ST. "
873 "Trigger connection close");
874 bta_gattc_close(p_clcb, p_data);
877 /*******************************************************************************
879 ** Function bta_gattc_set_discover_st
881 ** Description when a SRCB start discovery, tell all related clcb and set
886 *******************************************************************************/
887 void bta_gattc_set_discover_st(tBTA_GATTC_SERV *p_srcb)
889 tBTA_GATTC_CB *p_cb = &bta_gattc_cb;
892 #if BLE_INCLUDED == TRUE
893 L2CA_EnableUpdateBleConnParams(p_srcb->server_bda, FALSE);
895 for (i = 0; i < BTA_GATTC_CLCB_MAX; i ++)
897 if (p_cb->clcb[i].p_srcb == p_srcb)
899 p_cb->clcb[i].status = BTA_GATT_OK;
900 p_cb->clcb[i].state = BTA_GATTC_DISCOVER_ST;
904 /*******************************************************************************
906 ** Function bta_gattc_restart_discover
908 ** Description process service change in discovery state, mark up the auto
909 ** update flag and set status to be discovery cancel for current
914 *******************************************************************************/
915 void bta_gattc_restart_discover(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
919 p_clcb->status = BTA_GATT_CANCEL;
920 p_clcb->auto_update = BTA_GATTC_DISC_WAITING;
923 /*******************************************************************************
925 ** Function bta_gattc_cfg_mtu
927 ** Description Configure MTU size on the GATT connection.
931 *******************************************************************************/
932 void bta_gattc_cfg_mtu(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
934 tBTA_GATT_STATUS status;
936 if (bta_gattc_enqueue(p_clcb, p_data))
938 status = GATTC_ConfigureMTU (p_clcb->bta_conn_id, p_data->api_mtu.mtu);
940 /* if failed, return callback here */
941 if (status != GATT_SUCCESS && status != GATT_CMD_STARTED)
943 /* Dequeue the data, if it was enqueued */
944 if (p_clcb->p_q_cmd == p_data)
945 p_clcb->p_q_cmd = NULL;
947 bta_gattc_cmpl_sendmsg(p_clcb->bta_conn_id, GATTC_OPTYPE_CONFIG, status, NULL);
951 /*******************************************************************************
953 ** Function bta_gattc_start_discover
955 ** Description Start a discovery on server.
959 *******************************************************************************/
960 void bta_gattc_start_discover(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
964 APPL_TRACE_DEBUG("bta_gattc_start_discover conn_id=%d p_clcb->p_srcb->state = %d ",
965 p_clcb->bta_conn_id, p_clcb->p_srcb->state);
967 if (((p_clcb->p_q_cmd == NULL || p_clcb->auto_update == BTA_GATTC_REQ_WAITING) &&
968 p_clcb->p_srcb->state == BTA_GATTC_SERV_IDLE) ||
969 p_clcb->p_srcb->state == BTA_GATTC_SERV_DISC)
970 /* no pending operation, start discovery right away */
972 p_clcb->auto_update = BTA_GATTC_NO_SCHEDULE;
974 if (p_clcb->p_srcb != NULL)
976 /* clear the service change mask */
977 p_clcb->p_srcb->srvc_hdl_chg = FALSE;
978 p_clcb->p_srcb->update_count = 0;
979 p_clcb->p_srcb->state = BTA_GATTC_SERV_DISC_ACT;
981 if (p_clcb->transport == BTA_TRANSPORT_LE)
982 L2CA_EnableUpdateBleConnParams(p_clcb->p_srcb->server_bda, FALSE);
984 /* set all srcb related clcb into discovery ST */
985 bta_gattc_set_discover_st(p_clcb->p_srcb);
987 if ((p_clcb->status = bta_gattc_init_cache(p_clcb->p_srcb)) == BTA_GATT_OK)
989 p_clcb->status = bta_gattc_discover_pri_service(p_clcb->bta_conn_id,
990 p_clcb->p_srcb, GATT_DISC_SRVC_ALL);
992 if (p_clcb->status != BTA_GATT_OK)
994 APPL_TRACE_ERROR("discovery on server failed");
995 bta_gattc_reset_discover_st(p_clcb->p_srcb, p_clcb->status);
998 p_clcb->disc_active = TRUE;
1002 APPL_TRACE_ERROR("unknown device, can not start discovery");
1005 /* pending operation, wait until it finishes */
1008 p_clcb->auto_update = BTA_GATTC_DISC_WAITING;
1010 if (p_clcb->p_srcb->state == BTA_GATTC_SERV_IDLE)
1011 p_clcb->state = BTA_GATTC_CONN_ST; /* set clcb state */
1015 /*******************************************************************************
1017 ** Function bta_gattc_disc_cmpl
1019 ** Description discovery on server is finished
1023 *******************************************************************************/
1024 void bta_gattc_disc_cmpl(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1026 tBTA_GATTC_DATA *p_q_cmd = p_clcb->p_q_cmd;
1029 APPL_TRACE_DEBUG("bta_gattc_disc_cmpl conn_id=%d",p_clcb->bta_conn_id);
1031 #if BLE_INCLUDED == TRUE
1032 if(p_clcb->transport == BTA_TRANSPORT_LE)
1033 L2CA_EnableUpdateBleConnParams(p_clcb->p_srcb->server_bda, TRUE);
1035 p_clcb->p_srcb->state = BTA_GATTC_SERV_IDLE;
1036 p_clcb->disc_active = FALSE;
1038 if (p_clcb->status != GATT_SUCCESS)
1040 /* clean up cache */
1041 if(p_clcb->p_srcb && p_clcb->p_srcb->p_srvc_cache) {
1042 list_free(p_clcb->p_srcb->p_srvc_cache);
1043 p_clcb->p_srcb->p_srvc_cache = NULL;
1046 /* used to reset cache in application */
1047 bta_gattc_cache_reset(p_clcb->p_srcb->server_bda);
1049 if (p_clcb->p_srcb && p_clcb->p_srcb->p_srvc_list) {
1050 /* release pending attribute list buffer */
1051 osi_free_and_reset((void **)&p_clcb->p_srcb->p_srvc_list);
1054 if (p_clcb->auto_update == BTA_GATTC_DISC_WAITING)
1056 /* start discovery again */
1057 bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_DISCOVER_EVT, NULL);
1059 /* get any queued command to proceed */
1060 else if (p_q_cmd != NULL)
1062 p_clcb->p_q_cmd = NULL;
1063 /* execute pending operation of link block still present */
1064 if (l2cu_find_lcb_by_bd_addr(p_clcb->p_srcb->server_bda, BT_TRANSPORT_LE) != NULL) {
1065 bta_gattc_sm_execute(p_clcb, p_q_cmd->hdr.event, p_q_cmd);
1067 /* if the command executed requeued the cmd, we don't
1068 * want to free the underlying buffer that's being
1069 * referenced by p_clcb->p_q_cmd
1071 if (p_q_cmd != p_clcb->p_q_cmd)
1072 osi_free_and_reset((void **)&p_q_cmd);
1075 /*******************************************************************************
1077 ** Function bta_gattc_read
1079 ** Description Read an attribute
1083 *******************************************************************************/
1084 void bta_gattc_read(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1086 if (!bta_gattc_enqueue(p_clcb, p_data))
1089 tGATT_READ_PARAM read_param;
1090 memset (&read_param, 0 ,sizeof(tGATT_READ_PARAM));
1091 read_param.by_handle.handle = p_data->api_read.handle;
1092 read_param.by_handle.auth_req = p_data->api_read.auth_req;
1094 tBTA_GATT_STATUS status = GATTC_Read(p_clcb->bta_conn_id, GATT_READ_BY_HANDLE, &read_param);
1097 if (status != BTA_GATT_OK)
1099 /* Dequeue the data, if it was enqueued */
1100 if (p_clcb->p_q_cmd == p_data)
1101 p_clcb->p_q_cmd = NULL;
1103 bta_gattc_cmpl_sendmsg(p_clcb->bta_conn_id, GATTC_OPTYPE_READ, status, NULL);
1106 /*******************************************************************************
1108 ** Function bta_gattc_read_multi
1110 ** Description read multiple
1113 *********************************************************************************/
1114 void bta_gattc_read_multi(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1116 tBTA_GATT_STATUS status = BTA_GATT_OK;
1117 tGATT_READ_PARAM read_param;
1119 if (bta_gattc_enqueue(p_clcb, p_data))
1121 memset(&read_param, 0, sizeof(tGATT_READ_PARAM));
1123 if (status == BTA_GATT_OK)
1125 read_param.read_multiple.num_handles = p_data->api_read_multi.num_attr;
1126 read_param.read_multiple.auth_req = p_data->api_read_multi.auth_req;
1127 memcpy(&read_param.read_multiple.handles, p_data->api_read_multi.handles,
1128 sizeof(UINT16) * p_data->api_read_multi.num_attr);
1130 status = GATTC_Read(p_clcb->bta_conn_id, GATT_READ_MULTIPLE, &read_param);
1134 if (status != BTA_GATT_OK)
1136 /* Dequeue the data, if it was enqueued */
1137 if (p_clcb->p_q_cmd == p_data)
1138 p_clcb->p_q_cmd = NULL;
1140 bta_gattc_cmpl_sendmsg(p_clcb->bta_conn_id, GATTC_OPTYPE_READ, status, NULL);
1144 /*******************************************************************************
1146 ** Function bta_gattc_write
1148 ** Description Write an attribute
1152 *******************************************************************************/
1153 void bta_gattc_write(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1155 if (!bta_gattc_enqueue(p_clcb, p_data))
1158 tBTA_GATT_STATUS status = BTA_GATT_OK;
1161 attr.conn_id = p_clcb->bta_conn_id;
1162 attr.handle = p_data->api_write.handle;
1163 attr.offset = p_data->api_write.offset;
1164 attr.len = p_data->api_write.len;
1165 attr.auth_req = p_data->api_write.auth_req;
1167 if (p_data->api_write.p_value)
1168 memcpy(attr.value, p_data->api_write.p_value, p_data->api_write.len);
1170 status = GATTC_Write(p_clcb->bta_conn_id, p_data->api_write.write_type, &attr);
1173 if (status != BTA_GATT_OK)
1175 /* Dequeue the data, if it was enqueued */
1176 if (p_clcb->p_q_cmd == p_data)
1177 p_clcb->p_q_cmd = NULL;
1179 bta_gattc_cmpl_sendmsg(p_clcb->bta_conn_id, GATTC_OPTYPE_WRITE, status, NULL);
1182 /*******************************************************************************
1184 ** Function bta_gattc_execute
1186 ** Description send execute write
1189 *********************************************************************************/
1190 void bta_gattc_execute(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1192 tBTA_GATT_STATUS status;
1194 if (bta_gattc_enqueue(p_clcb, p_data))
1196 status = GATTC_ExecuteWrite(p_clcb->bta_conn_id, p_data->api_exec.is_execute);
1198 if (status != BTA_GATT_OK)
1200 /* Dequeue the data, if it was enqueued */
1201 if (p_clcb->p_q_cmd == p_data)
1202 p_clcb->p_q_cmd = NULL;
1204 bta_gattc_cmpl_sendmsg(p_clcb->bta_conn_id, GATTC_OPTYPE_EXE_WRITE, status, NULL);
1208 /*******************************************************************************
1210 ** Function bta_gattc_confirm
1212 ** Description send handle value confirmation
1216 *******************************************************************************/
1217 void bta_gattc_confirm(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1219 UINT16 handle = p_data->api_confirm.handle;
1221 if (GATTC_SendHandleValueConfirm(p_data->api_confirm.hdr.layer_specific, handle)
1223 APPL_TRACE_ERROR("bta_gattc_confirm to handle [0x%04x] failed", handle);
1225 /* if over BR_EDR, inform PM for mode change */
1226 if (p_clcb->transport == BTA_TRANSPORT_BR_EDR) {
1227 bta_sys_busy(BTA_ID_GATTC, BTA_ALL_APP_ID, p_clcb->bda);
1228 bta_sys_idle(BTA_ID_GATTC, BTA_ALL_APP_ID, p_clcb->bda);
1232 /*******************************************************************************
1234 ** Function bta_gattc_read_cmpl
1236 ** Description read complete
1240 *******************************************************************************/
1241 void bta_gattc_read_cmpl(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_OP_CMPL *p_data)
1245 tBTA_GATT_UNFMT read_value;
1247 memset(&cb_data, 0, sizeof(tBTA_GATTC));
1248 memset(&read_value, 0, sizeof(tBTA_GATT_UNFMT));
1250 cb_data.read.status = p_data->status;
1252 if (p_data->p_cmpl != NULL && p_data->status == BTA_GATT_OK)
1254 cb_data.read.handle = p_data->p_cmpl->att_value.handle;
1256 read_value.len = p_data->p_cmpl->att_value.len;
1257 read_value.p_value = p_data->p_cmpl->att_value.value;
1258 cb_data.read.p_value = &read_value;
1260 cb_data.read.handle = p_clcb->p_q_cmd->api_read.handle;
1263 event = p_clcb->p_q_cmd->api_read.cmpl_evt;
1264 cb_data.read.conn_id = p_clcb->bta_conn_id;
1266 osi_free_and_reset((void **)&p_clcb->p_q_cmd);
1267 /* read complete, callback */
1268 ( *p_clcb->p_rcb->p_cback)(event, (tBTA_GATTC *)&cb_data);
1271 /*******************************************************************************
1273 ** Function bta_gattc_write_cmpl
1275 ** Description write complete
1279 *******************************************************************************/
1280 void bta_gattc_write_cmpl(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_OP_CMPL *p_data)
1282 tBTA_GATTC cb_data = {0};
1285 memset(&cb_data, 0, sizeof(tBTA_GATTC));
1287 cb_data.write.status = p_data->status;
1288 cb_data.write.handle = p_data->p_cmpl->att_value.handle;
1290 if (p_clcb->p_q_cmd->api_write.hdr.event == BTA_GATTC_API_WRITE_EVT &&
1291 p_clcb->p_q_cmd->api_write.write_type == BTA_GATTC_WRITE_PREPARE)
1293 event = BTA_GATTC_PREP_WRITE_EVT;
1296 event = p_clcb->p_q_cmd->api_write.cmpl_evt;
1298 osi_free_and_reset((void **)&p_clcb->p_q_cmd);
1299 cb_data.write.conn_id = p_clcb->bta_conn_id;
1300 /* write complete, callback */
1301 ( *p_clcb->p_rcb->p_cback)(event, (tBTA_GATTC *)&cb_data);
1304 /*******************************************************************************
1306 ** Function bta_gattc_exec_cmpl
1308 ** Description execute write complete
1312 *******************************************************************************/
1313 void bta_gattc_exec_cmpl(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_OP_CMPL *p_data)
1317 osi_free_and_reset((void **)&p_clcb->p_q_cmd);
1318 p_clcb->status = BTA_GATT_OK;
1320 /* execute complete, callback */
1321 cb_data.exec_cmpl.conn_id = p_clcb->bta_conn_id;
1322 cb_data.exec_cmpl.status = p_data->status;
1324 ( *p_clcb->p_rcb->p_cback)(BTA_GATTC_EXEC_EVT, &cb_data);
1328 /*******************************************************************************
1330 ** Function bta_gattc_cfg_mtu_cmpl
1332 ** Description configure MTU operation complete
1336 *******************************************************************************/
1337 void bta_gattc_cfg_mtu_cmpl(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_OP_CMPL *p_data)
1341 osi_free_and_reset((void **)&p_clcb->p_q_cmd);
1343 if (p_data->p_cmpl && p_data->status == BTA_GATT_OK)
1344 p_clcb->p_srcb->mtu = p_data->p_cmpl->mtu;
1346 /* configure MTU complete, callback */
1347 p_clcb->status = p_data->status;
1348 cb_data.cfg_mtu.conn_id = p_clcb->bta_conn_id;
1349 cb_data.cfg_mtu.status = p_data->status;
1350 cb_data.cfg_mtu.mtu = p_clcb->p_srcb->mtu;
1352 (*p_clcb->p_rcb->p_cback) (BTA_GATTC_CFG_MTU_EVT, &cb_data);
1355 /*******************************************************************************
1357 ** Function bta_gattc_op_cmpl
1359 ** Description operation completed.
1363 *******************************************************************************/
1364 void bta_gattc_op_cmpl(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1366 UINT8 op = (UINT8)p_data->op_cmpl.op_code;
1367 UINT8 mapped_op = 0;
1369 APPL_TRACE_DEBUG("bta_gattc_op_cmpl op = %d", op);
1371 if (op == GATTC_OPTYPE_INDICATION || op == GATTC_OPTYPE_NOTIFICATION)
1373 APPL_TRACE_ERROR("unexpected operation, ignored");
1375 else if (op >= GATTC_OPTYPE_READ)
1377 if (p_clcb->p_q_cmd == NULL)
1379 APPL_TRACE_ERROR("No pending command");
1382 if (p_clcb->p_q_cmd->hdr.event != bta_gattc_opcode_to_int_evt[op - GATTC_OPTYPE_READ])
1384 mapped_op = p_clcb->p_q_cmd->hdr.event - BTA_GATTC_API_READ_EVT + GATTC_OPTYPE_READ;
1385 if ( mapped_op > GATTC_OPTYPE_INDICATION) mapped_op = 0;
1387 #if (BT_TRACE_VERBOSE == TRUE)
1388 APPL_TRACE_ERROR("expect op:(%s :0x%04x), receive unexpected operation (%s).",
1389 bta_gattc_op_code_name[mapped_op] , p_clcb->p_q_cmd->hdr.event,
1390 bta_gattc_op_code_name[op]);
1392 APPL_TRACE_ERROR("expect op:(%u :0x%04x), receive unexpected operation (%u).",
1393 mapped_op , p_clcb->p_q_cmd->hdr.event, op);
1398 /* discard responses if service change indication is received before operation completed */
1399 if (p_clcb->auto_update == BTA_GATTC_DISC_WAITING && p_clcb->p_srcb->srvc_hdl_chg)
1401 APPL_TRACE_DEBUG("Discard all responses when service change indication is received.");
1402 p_data->op_cmpl.status = GATT_ERROR;
1405 /* service handle change void the response, discard it */
1406 if (op == GATTC_OPTYPE_READ)
1407 bta_gattc_read_cmpl(p_clcb, &p_data->op_cmpl);
1409 else if (op == GATTC_OPTYPE_WRITE)
1410 bta_gattc_write_cmpl(p_clcb, &p_data->op_cmpl);
1412 else if (op == GATTC_OPTYPE_EXE_WRITE)
1413 bta_gattc_exec_cmpl(p_clcb, &p_data->op_cmpl);
1415 else if (op == GATTC_OPTYPE_CONFIG)
1416 bta_gattc_cfg_mtu_cmpl(p_clcb, &p_data->op_cmpl);
1418 if (p_clcb->auto_update == BTA_GATTC_DISC_WAITING)
1420 p_clcb->auto_update = BTA_GATTC_REQ_WAITING;
1421 bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_DISCOVER_EVT, NULL);
1425 /*******************************************************************************
1427 ** Function bta_gattc_op_cmpl
1429 ** Description operation completed.
1433 *******************************************************************************/
1434 void bta_gattc_ignore_op_cmpl(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1438 /* receive op complete when discovery is started, ignore the response,
1439 and wait for discovery finish and resent */
1440 APPL_TRACE_DEBUG("bta_gattc_ignore_op_cmpl op = %d", p_data->hdr.layer_specific);
1443 /*******************************************************************************
1445 ** Function bta_gattc_search
1447 ** Description start a search in the local server cache
1451 *******************************************************************************/
1452 void bta_gattc_search(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1454 tBTA_GATT_STATUS status = GATT_INTERNAL_ERROR;
1456 APPL_TRACE_DEBUG("bta_gattc_search conn_id=%d",p_clcb->bta_conn_id);
1457 if (p_clcb->p_srcb && p_clcb->p_srcb->p_srvc_cache)
1459 status = BTA_GATT_OK;
1460 /* search the local cache of a server device */
1461 bta_gattc_search_service(p_clcb, p_data->api_search.p_srvc_uuid);
1463 cb_data.search_cmpl.status = status;
1464 cb_data.search_cmpl.conn_id = p_clcb->bta_conn_id;
1466 /* end of search or no server cache available */
1467 ( *p_clcb->p_rcb->p_cback)(BTA_GATTC_SEARCH_CMPL_EVT, &cb_data);
1469 /*******************************************************************************
1471 ** Function bta_gattc_q_cmd
1473 ** Description enqueue a command into control block, usually because discovery
1474 ** operation is busy.
1478 *******************************************************************************/
1479 void bta_gattc_q_cmd(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1481 bta_gattc_enqueue(p_clcb, p_data);
1484 /*******************************************************************************
1486 ** Function bta_gattc_fail
1488 ** Description report API call failure back to apps
1492 *******************************************************************************/
1493 void bta_gattc_fail(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1497 if (p_clcb->status == BTA_GATT_OK)
1499 APPL_TRACE_ERROR("operation not supported at current state [%d]", p_clcb->state);
1503 /*******************************************************************************
1505 ** Function bta_gattc_deregister_cmpl
1507 ** Description De-Register a GATT client application with BTA completed.
1511 *******************************************************************************/
1512 static void bta_gattc_deregister_cmpl(tBTA_GATTC_RCB *p_clreg)
1514 tBTA_GATTC_CB *p_cb = &bta_gattc_cb;
1515 tBTA_GATTC_IF client_if = p_clreg->client_if;
1517 tBTA_GATTC_CBACK *p_cback = p_clreg->p_cback;
1519 memset(&cb_data, 0, sizeof(tBTA_GATTC));
1521 GATT_Deregister(p_clreg->client_if);
1522 memset(p_clreg, 0, sizeof(tBTA_GATTC_RCB));
1524 cb_data.reg_oper.client_if = client_if;
1525 cb_data.reg_oper.status = BTA_GATT_OK;
1528 /* callback with de-register event */
1529 (*p_cback)(BTA_GATTC_DEREG_EVT, (tBTA_GATTC *)&cb_data);
1531 if (bta_gattc_num_reg_app() == 0 && p_cb->state == BTA_GATTC_STATE_DISABLING)
1533 p_cb->state = BTA_GATTC_STATE_DISABLED;
1536 /*******************************************************************************
1538 ** Function bta_gattc_conn_cback
1540 ** Description callback functions to GATT client stack.
1544 *******************************************************************************/
1545 static void bta_gattc_conn_cback(tGATT_IF gattc_if, BD_ADDR bda, UINT16 conn_id,
1546 BOOLEAN connected, tGATT_DISCONN_REASON reason,
1547 tBT_TRANSPORT transport)
1550 APPL_TRACE_WARNING("%s() - cif=%d connected=%d conn_id=%d reason=0x%04x",
1551 __func__, gattc_if, connected, conn_id, reason);
1555 bdcpy(bdaddr.address, bda);
1557 btif_debug_conn_state(bdaddr, BTIF_DEBUG_CONNECTED, GATT_CONN_UNKNOWN);
1559 btif_debug_conn_state(bdaddr, BTIF_DEBUG_DISCONNECTED, reason);
1561 tBTA_GATTC_DATA *p_buf =
1562 (tBTA_GATTC_DATA *)osi_calloc(sizeof(tBTA_GATTC_DATA));
1563 p_buf->int_conn.hdr.event = connected ? BTA_GATTC_INT_CONN_EVT :
1564 BTA_GATTC_INT_DISCONN_EVT;
1565 p_buf->int_conn.hdr.layer_specific = conn_id;
1566 p_buf->int_conn.client_if = gattc_if;
1567 p_buf->int_conn.role = L2CA_GetBleConnRole(bda);
1568 p_buf->int_conn.reason = reason;
1569 p_buf->int_conn.transport = transport;
1570 bdcpy(p_buf->int_conn.remote_bda, bda);
1572 bta_sys_sendmsg(p_buf);
1575 /*******************************************************************************
1577 ** Function bta_gattc_enc_cmpl_cback
1579 ** Description encryption complete callback function to GATT client stack.
1583 *******************************************************************************/
1584 static void bta_gattc_enc_cmpl_cback(tGATT_IF gattc_if, BD_ADDR bda)
1586 tBTA_GATTC_CLCB *p_clcb =
1587 bta_gattc_find_clcb_by_cif(gattc_if, bda, BTA_GATT_TRANSPORT_LE);
1592 #if (defined BTA_HH_LE_INCLUDED && BTA_HH_LE_INCLUDED == TRUE)
1593 /* filter this event just for BTA HH LE GATT client,
1594 In the future, if we want to enable encryption complete event
1595 for all GATT clients, we can remove this code */
1596 if (!bta_hh_le_is_hh_gatt_if(gattc_if))
1602 APPL_TRACE_DEBUG("%s: cif = %d", __func__, gattc_if);
1604 tBTA_GATTC_DATA *p_buf =
1605 (tBTA_GATTC_DATA *)osi_calloc(sizeof(tBTA_GATTC_DATA));
1606 p_buf->enc_cmpl.hdr.event = BTA_GATTC_ENC_CMPL_EVT;
1607 p_buf->enc_cmpl.hdr.layer_specific = p_clcb->bta_conn_id;
1608 p_buf->enc_cmpl.client_if = gattc_if;
1609 bdcpy(p_buf->enc_cmpl.remote_bda, bda);
1611 bta_sys_sendmsg(p_buf);
1614 /*******************************************************************************
1616 ** Function bta_gattc_process_api_refresh
1618 ** Description process refresh API to delete cache and start a new discovery
1619 ** if currently connected.
1623 *******************************************************************************/
1624 void bta_gattc_process_api_refresh(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA * p_msg)
1626 tBTA_GATTC_SERV *p_srvc_cb = bta_gattc_find_srvr_cache(p_msg->api_conn.remote_bda);
1627 tBTA_GATTC_CLCB *p_clcb = &bta_gattc_cb.clcb[0];
1628 BOOLEAN found = FALSE;
1632 if (p_srvc_cb != NULL)
1634 /* try to find a CLCB */
1635 if (p_srvc_cb->connected && p_srvc_cb->num_clcb != 0)
1637 for (i = 0; i < BTA_GATTC_CLCB_MAX; i ++, p_clcb ++)
1639 if (p_clcb->in_use && p_clcb->p_srcb == p_srvc_cb)
1647 bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_DISCOVER_EVT, NULL);
1651 /* in all other cases, mark it and delete the cache */
1652 if (p_srvc_cb->p_srvc_cache != NULL) {
1653 list_free(p_srvc_cb->p_srvc_cache);
1654 p_srvc_cb->p_srvc_cache = NULL;
1657 /* used to reset cache in application */
1658 bta_gattc_cache_reset(p_msg->api_conn.remote_bda);
1661 /*******************************************************************************
1663 ** Function bta_gattc_process_srvc_chg_ind
1665 ** Description process service change indication.
1669 *******************************************************************************/
1670 BOOLEAN bta_gattc_process_srvc_chg_ind(UINT16 conn_id,
1671 tBTA_GATTC_RCB *p_clrcb,
1672 tBTA_GATTC_SERV *p_srcb,
1673 tBTA_GATTC_CLCB *p_clcb,
1674 tBTA_GATTC_NOTIFY *p_notify,
1675 tGATT_VALUE *att_value)
1677 tBT_UUID gattp_uuid, srvc_chg_uuid;
1678 BOOLEAN processed = FALSE;
1682 gattp_uuid.uu.uuid16 = UUID_SERVCLASS_GATT_SERVER;
1684 srvc_chg_uuid.len = 2;
1685 srvc_chg_uuid.uu.uuid16 = GATT_UUID_GATT_SRV_CHGD;
1687 const tBTA_GATTC_CHARACTERISTIC *p_char = bta_gattc_get_characteristic_srcb(p_srcb, p_notify->handle);
1688 if (p_char && bta_gattc_uuid_compare(&p_char->service->uuid, &gattp_uuid, TRUE) &&
1689 bta_gattc_uuid_compare(&p_char->uuid, &srvc_chg_uuid, TRUE))
1691 if (att_value->len != BTA_GATTC_SERVICE_CHANGED_LEN) {
1692 APPL_TRACE_ERROR("%s: received malformed service changed indication, skipping", __func__);
1696 UINT8 *p = att_value->value;
1697 UINT16 s_handle = ((UINT16)(*(p )) + (((UINT16)(*(p + 1))) << 8));
1698 UINT16 e_handle = ((UINT16)(*(p + 2)) + (((UINT16)(*(p + 3))) << 8));
1700 APPL_TRACE_ERROR("%s: service changed s_handle:0x%04x e_handle:0x%04x",
1701 __func__, s_handle, e_handle);
1704 /* mark service handle change pending */
1705 p_srcb->srvc_hdl_chg = TRUE;
1706 /* clear up all notification/indication registration */
1707 bta_gattc_clear_notif_registration(p_srcb, conn_id, s_handle, e_handle);
1708 /* service change indication all received, do discovery update */
1709 if ( ++ p_srcb->update_count == bta_gattc_num_reg_app())
1711 /* not an opened connection; or connection busy */
1712 /* search for first available clcb and start discovery */
1713 if (p_clcb == NULL || (p_clcb && p_clcb->p_q_cmd != NULL))
1715 for (i = 0 ; i < BTA_GATTC_CLCB_MAX; i ++)
1717 if (bta_gattc_cb.clcb[i].in_use &&
1718 bta_gattc_cb.clcb[i].p_srcb == p_srcb &&
1719 bta_gattc_cb.clcb[i].p_q_cmd == NULL)
1721 p_clcb = &bta_gattc_cb.clcb[i];
1726 /* send confirmation here if this is an indication, it should always be */
1727 GATTC_SendHandleValueConfirm(conn_id, att_value->handle);
1729 /* if connection available, refresh cache by doing discovery now */
1731 bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_DISCOVER_EVT, NULL);
1733 /* notify applicationf or service change */
1734 if (p_clrcb->p_cback != NULL)
1736 (* p_clrcb->p_cback)(BTA_GATTC_SRVC_CHG_EVT, (tBTA_GATTC *)p_srcb->server_bda);
1744 /*******************************************************************************
1746 ** Function bta_gattc_proc_other_indication
1748 ** Description process all non-service change indication/notification.
1752 *******************************************************************************/
1753 void bta_gattc_proc_other_indication(tBTA_GATTC_CLCB *p_clcb, UINT8 op,
1754 tGATT_CL_COMPLETE *p_data,
1755 tBTA_GATTC_NOTIFY *p_notify)
1757 APPL_TRACE_DEBUG("bta_gattc_proc_other_indication check \
1758 p_data->att_value.handle=%d p_data->handle=%d",
1759 p_data->att_value.handle, p_data->handle);
1760 APPL_TRACE_DEBUG("is_notify", p_notify->is_notify);
1762 p_notify->is_notify = (op == GATTC_OPTYPE_INDICATION) ? FALSE : TRUE;
1763 p_notify->len = p_data->att_value.len;
1764 bdcpy(p_notify->bda, p_clcb->bda);
1765 memcpy(p_notify->value, p_data->att_value.value, p_data->att_value.len);
1766 p_notify->conn_id = p_clcb->bta_conn_id;
1768 if (p_clcb->p_rcb->p_cback)
1769 (*p_clcb->p_rcb->p_cback)(BTA_GATTC_NOTIF_EVT, (tBTA_GATTC *)p_notify);
1772 /*******************************************************************************
1774 ** Function bta_gattc_process_indicate
1776 ** Description process indication/notification.
1780 *******************************************************************************/
1781 void bta_gattc_process_indicate(UINT16 conn_id, tGATTC_OPTYPE op, tGATT_CL_COMPLETE *p_data)
1783 UINT16 handle = p_data->att_value.handle;
1784 tBTA_GATTC_CLCB *p_clcb ;
1785 tBTA_GATTC_RCB *p_clrcb = NULL;
1786 tBTA_GATTC_SERV *p_srcb = NULL;
1787 tBTA_GATTC_NOTIFY notify;
1789 tBTA_GATTC_IF gatt_if;
1790 tBTA_TRANSPORT transport;
1792 if (!GATT_GetConnectionInfor(conn_id, &gatt_if, remote_bda, &transport))
1794 APPL_TRACE_ERROR("%s indication/notif for unknown app", __func__);
1795 if (op == GATTC_OPTYPE_INDICATION)
1796 GATTC_SendHandleValueConfirm(conn_id, handle);
1800 if ((p_clrcb = bta_gattc_cl_get_regcb(gatt_if)) == NULL)
1802 APPL_TRACE_ERROR("%s indication/notif for unregistered app", __func__);
1803 if (op == GATTC_OPTYPE_INDICATION)
1804 GATTC_SendHandleValueConfirm(conn_id, handle);
1808 if ((p_srcb = bta_gattc_find_srcb(remote_bda)) == NULL)
1810 APPL_TRACE_ERROR("%s indication/notif for unknown device, ignore", __func__);
1811 if (op == GATTC_OPTYPE_INDICATION)
1812 GATTC_SendHandleValueConfirm(conn_id, handle);
1816 p_clcb = bta_gattc_find_clcb_by_conn_id(conn_id);
1818 notify.handle = handle;
1819 /* if non-service change indication/notification, forward to application */
1820 if (!bta_gattc_process_srvc_chg_ind(conn_id, p_clrcb, p_srcb, p_clcb, ¬ify, &p_data->att_value))
1822 /* if app registered for the notification */
1823 if (bta_gattc_check_notif_registry(p_clrcb, p_srcb, ¬ify))
1825 /* connection not open yet */
1828 p_clcb = bta_gattc_clcb_alloc(gatt_if, remote_bda, transport);
1830 if (p_clcb == NULL) {
1831 APPL_TRACE_ERROR("No resources");
1835 p_clcb->bta_conn_id = conn_id;
1836 p_clcb->transport = transport;
1838 bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_CONN_EVT, NULL);
1842 bta_gattc_proc_other_indication(p_clcb, op, p_data, ¬ify);
1844 /* no one intersted and need ack? */
1845 else if (op == GATTC_OPTYPE_INDICATION)
1847 APPL_TRACE_DEBUG("%s no one interested, ack now", __func__);
1848 GATTC_SendHandleValueConfirm(conn_id, handle);
1852 /*******************************************************************************
1854 ** Function bta_gattc_cmpl_cback
1856 ** Description client operation complete callback register with BTE GATT.
1860 *******************************************************************************/
1861 static void bta_gattc_cmpl_cback(UINT16 conn_id, tGATTC_OPTYPE op, tGATT_STATUS status,
1862 tGATT_CL_COMPLETE *p_data)
1864 tBTA_GATTC_CLCB *p_clcb;
1865 APPL_TRACE_DEBUG("bta_gattc_cmpl_cback: conn_id = %d op = %d status = %d",
1866 conn_id, op, status);
1868 /* notification and indication processed right away */
1869 if (op == GATTC_OPTYPE_NOTIFICATION || op == GATTC_OPTYPE_INDICATION)
1871 bta_gattc_process_indicate(conn_id, op, p_data);
1874 /* for all other operation, not expected if w/o connection */
1875 else if ((p_clcb = bta_gattc_find_clcb_by_conn_id(conn_id)) == NULL)
1877 APPL_TRACE_ERROR("bta_gattc_cmpl_cback unknown conn_id = %d, ignore data", conn_id);
1881 /* if over BR_EDR, inform PM for mode change */
1882 if (p_clcb->transport == BTA_TRANSPORT_BR_EDR)
1884 bta_sys_busy(BTA_ID_GATTC, BTA_ALL_APP_ID, p_clcb->bda);
1885 bta_sys_idle(BTA_ID_GATTC, BTA_ALL_APP_ID, p_clcb->bda);
1888 bta_gattc_cmpl_sendmsg(conn_id, op, status, p_data);
1891 /*******************************************************************************
1893 ** Function bta_gattc_cmpl_sendmsg
1895 ** Description client operation complete send message
1899 *******************************************************************************/
1900 static void bta_gattc_cmpl_sendmsg(UINT16 conn_id, tGATTC_OPTYPE op,
1901 tBTA_GATT_STATUS status,
1902 tGATT_CL_COMPLETE *p_data)
1904 const size_t len = sizeof(tBTA_GATTC_OP_CMPL) + sizeof(tGATT_CL_COMPLETE);
1905 tBTA_GATTC_OP_CMPL *p_buf = (tBTA_GATTC_OP_CMPL *)osi_calloc(len);
1907 p_buf->hdr.event = BTA_GATTC_OP_CMPL_EVT;
1908 p_buf->hdr.layer_specific = conn_id;
1909 p_buf->status = status;
1910 p_buf->op_code = op;
1912 if (p_data != NULL) {
1913 p_buf->p_cmpl = (tGATT_CL_COMPLETE *)(p_buf + 1);
1914 memcpy(p_buf->p_cmpl, p_data, sizeof(tGATT_CL_COMPLETE));
1917 bta_sys_sendmsg(p_buf);
1920 /*******************************************************************************
1922 ** Function bta_gattc_cong_cback
1924 ** Description congestion callback for BTA GATT client.
1928 ********************************************************************************/
1929 static void bta_gattc_cong_cback (UINT16 conn_id, BOOLEAN congested)
1931 tBTA_GATTC_CLCB *p_clcb;
1934 if ((p_clcb = bta_gattc_find_clcb_by_conn_id(conn_id)) != NULL)
1936 if (p_clcb->p_rcb->p_cback)
1938 cb_data.congest.conn_id = conn_id;
1939 cb_data.congest.congested = congested;
1941 (*p_clcb->p_rcb->p_cback)(BTA_GATTC_CONGEST_EVT, &cb_data);
1946 #if BLE_INCLUDED == TRUE
1947 /*******************************************************************************
1949 ** Function bta_gattc_init_clcb_conn
1951 ** Description Initaite a BTA CLCB connection
1955 ********************************************************************************/
1956 void bta_gattc_init_clcb_conn(UINT8 cif, BD_ADDR remote_bda)
1958 tBTA_GATTC_CLCB *p_clcb = NULL;
1959 tBTA_GATTC_DATA gattc_data;
1962 /* should always get the connection ID */
1963 if (GATT_GetConnIdIfConnected(cif, remote_bda, &conn_id, BTA_GATT_TRANSPORT_LE) == FALSE)
1965 APPL_TRACE_ERROR("bta_gattc_init_clcb_conn ERROR: not a connected device");
1969 /* initaite a new connection here */
1970 if ((p_clcb = bta_gattc_clcb_alloc(cif, remote_bda, BTA_GATT_TRANSPORT_LE)) != NULL)
1972 gattc_data.hdr.layer_specific = p_clcb->bta_conn_id = conn_id;
1974 gattc_data.api_conn.client_if = cif;
1975 memcpy(gattc_data.api_conn.remote_bda, remote_bda, BD_ADDR_LEN);
1976 gattc_data.api_conn.is_direct = TRUE;
1978 bta_gattc_sm_execute(p_clcb, BTA_GATTC_API_OPEN_EVT, &gattc_data);
1982 APPL_TRACE_ERROR("No resources");
1985 /*******************************************************************************
1987 ** Function bta_gattc_process_listen_all
1989 ** Description process listen all, send open callback to application for all
1990 ** connected slave LE link.
1994 ********************************************************************************/
1995 void bta_gattc_process_listen_all(UINT8 cif)
1998 tBTA_GATTC_CONN *p_conn = &bta_gattc_cb.conn_track[0];
2000 for (i_conn = 0; i_conn < BTA_GATTC_CONN_MAX; i_conn++, p_conn ++)
2002 if (p_conn->in_use )
2004 if (bta_gattc_find_clcb_by_cif(cif, p_conn->remote_bda, BTA_GATT_TRANSPORT_LE) == NULL)
2006 bta_gattc_init_clcb_conn(cif, p_conn->remote_bda);
2008 /* else already connected */
2012 /*******************************************************************************
2014 ** Function bta_gattc_listen
2016 ** Description Start or stop a listen for connection
2020 ********************************************************************************/
2021 void bta_gattc_listen(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA * p_msg)
2023 tBTA_GATTC_RCB *p_clreg = bta_gattc_cl_get_regcb(p_msg->api_listen.client_if);
2027 cb_data.reg_oper.status = BTA_GATT_ERROR;
2028 cb_data.reg_oper.client_if = p_msg->api_listen.client_if;
2030 if (p_clreg == NULL)
2032 APPL_TRACE_ERROR("bta_gattc_listen failed, unknown client_if: %d",
2033 p_msg->api_listen.client_if);
2036 /* mark bg conn record */
2037 if (bta_gattc_mark_bg_conn(p_msg->api_listen.client_if,
2038 (BD_ADDR_PTR) p_msg->api_listen.remote_bda,
2039 p_msg->api_listen.start,
2042 if (!GATT_Listen(p_msg->api_listen.client_if,
2043 p_msg->api_listen.start,
2044 p_msg->api_listen.remote_bda))
2046 APPL_TRACE_ERROR("Listen failure");
2047 (*p_clreg->p_cback)(BTA_GATTC_LISTEN_EVT, &cb_data);
2051 cb_data.status = BTA_GATT_OK;
2053 (*p_clreg->p_cback)(BTA_GATTC_LISTEN_EVT, &cb_data);
2055 if (p_msg->api_listen.start)
2057 /* if listen to a specific target */
2058 if (p_msg->api_listen.remote_bda != NULL)
2061 /* if is a connected remote device */
2062 if (L2CA_GetBleConnRole(p_msg->api_listen.remote_bda) == HCI_ROLE_SLAVE &&
2063 bta_gattc_find_clcb_by_cif(p_msg->api_listen.client_if,
2064 p_msg->api_listen.remote_bda,
2065 BTA_GATT_TRANSPORT_LE) == NULL)
2068 bta_gattc_init_clcb_conn(p_msg->api_listen.client_if,
2069 p_msg->api_listen.remote_bda);
2072 /* if listen to all */
2075 LOG_DEBUG(LOG_TAG, "Listen For All now");
2076 /* go through all connected device and send
2077 callback for all connected slave connection */
2078 bta_gattc_process_listen_all(p_msg->api_listen.client_if);
2085 /*******************************************************************************
2087 ** Function bta_gattc_broadcast
2089 ** Description Start or stop broadcasting
2093 ********************************************************************************/
2094 void bta_gattc_broadcast(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA * p_msg)
2096 tBTA_GATTC_RCB *p_clreg = bta_gattc_cl_get_regcb(p_msg->api_listen.client_if);
2100 cb_data.reg_oper.client_if = p_msg->api_listen.client_if;
2101 cb_data.reg_oper.status = BTM_BleBroadcast(p_msg->api_listen.start);
2103 if (p_clreg && p_clreg->p_cback)
2104 (*p_clreg->p_cback)(BTA_GATTC_LISTEN_EVT, &cb_data);