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"
36 #include "osi/include/log.h"
39 #if (defined BTA_HH_LE_INCLUDED && BTA_HH_LE_INCLUDED == TRUE)
40 #include "bta_hh_int.h"
43 #if BTA_GATT_INCLUDED && BLE_INCLUDED == TRUE
45 /*****************************************************************************
47 *****************************************************************************/
48 static void bta_gattc_conn_cback(tGATT_IF gattc_if, BD_ADDR bda, UINT16 conn_id,
49 BOOLEAN connected, tGATT_DISCONN_REASON reason,
50 tBT_TRANSPORT transport);
52 static void bta_gattc_cmpl_cback(UINT16 conn_id, tGATTC_OPTYPE op, tGATT_STATUS status,
53 tGATT_CL_COMPLETE *p_data);
54 static void bta_gattc_cmpl_sendmsg(UINT16 conn_id, tGATTC_OPTYPE op,
55 tBTA_GATT_STATUS status,
56 tGATT_CL_COMPLETE *p_data);
58 static void bta_gattc_deregister_cmpl(tBTA_GATTC_RCB *p_clreg);
59 static void bta_gattc_enc_cmpl_cback(tGATT_IF gattc_if, BD_ADDR bda);
60 static void bta_gattc_cong_cback (UINT16 conn_id, BOOLEAN congested);
62 static tGATT_CBACK bta_gattc_cl_cback =
66 bta_gattc_disc_res_cback,
67 bta_gattc_disc_cmpl_cback,
69 bta_gattc_enc_cmpl_cback,
73 /* opcode(tGATTC_OPTYPE) order has to be comply with internal event order */
74 static UINT16 bta_gattc_opcode_to_int_evt[] =
76 BTA_GATTC_API_READ_EVT,
77 BTA_GATTC_API_WRITE_EVT,
78 BTA_GATTC_API_EXEC_EVT,
79 BTA_GATTC_API_CFG_MTU_EVT
82 #if (BT_TRACE_VERBOSE == TRUE)
83 static const char *bta_gattc_op_code_name[] =
95 /*****************************************************************************
97 *****************************************************************************/
99 /*******************************************************************************
101 ** Function bta_gattc_enable
103 ** Description Enables GATTC module
108 *******************************************************************************/
109 static void bta_gattc_enable(tBTA_GATTC_CB *p_cb)
111 APPL_TRACE_DEBUG("bta_gattc_enable");
113 if (p_cb->state == BTA_GATTC_STATE_DISABLED)
115 /* initialize control block */
116 memset(&bta_gattc_cb, 0, sizeof(tBTA_GATTC_CB));
117 p_cb->state = BTA_GATTC_STATE_ENABLED;
121 APPL_TRACE_DEBUG("GATTC is arelady enabled");
125 /*******************************************************************************
127 ** Function bta_gattc_disable
129 ** Description Disable GATTC module by cleaning up all active connections
130 ** and deregister all application.
134 *******************************************************************************/
135 void bta_gattc_disable(tBTA_GATTC_CB *p_cb)
139 APPL_TRACE_DEBUG("bta_gattc_disable");
141 if (p_cb->state != BTA_GATTC_STATE_ENABLED)
143 APPL_TRACE_ERROR("not enabled or disable in pogress");
147 for (i = 0; i <BTA_GATTC_CL_MAX; i ++)
149 if (p_cb->cl_rcb[i].in_use)
151 p_cb->state = BTA_GATTC_STATE_DISABLING;
152 /* don't deregister HH GATT IF */
153 /* HH GATT IF will be deregistered by bta_hh_le_deregister when disable HH */
154 #if (defined BTA_HH_LE_INCLUDED && BTA_HH_LE_INCLUDED == TRUE)
155 if (!bta_hh_le_is_hh_gatt_if(p_cb->cl_rcb[i].client_if)) {
157 bta_gattc_deregister(p_cb, &p_cb->cl_rcb[i]);
158 #if (defined BTA_HH_LE_INCLUDED && BTA_HH_LE_INCLUDED == TRUE)
164 /* no registered apps, indicate disable completed */
165 if (p_cb->state != BTA_GATTC_STATE_DISABLING)
167 p_cb->state = BTA_GATTC_STATE_DISABLED;
168 memset(p_cb, 0, sizeof(tBTA_GATTC_CB));
172 /*******************************************************************************
174 ** Function bta_gattc_register
176 ** Description Register a GATT client application with BTA.
180 *******************************************************************************/
181 void bta_gattc_register(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA *p_data)
185 tBT_UUID *p_app_uuid = &p_data->api_reg.app_uuid;
186 tBTA_GATTC_INT_START_IF *p_buf;
187 tBTA_GATT_STATUS status = BTA_GATT_NO_RESOURCES;
189 APPL_TRACE_DEBUG("bta_gattc_register state %d",p_cb->state);
190 memset(&cb_data, 0, sizeof(cb_data));
191 cb_data.reg_oper.status = BTA_GATT_NO_RESOURCES;
193 /* check if GATTC module is already enabled . Else enable */
194 if (p_cb->state == BTA_GATTC_STATE_DISABLED)
196 bta_gattc_enable (p_cb);
198 /* todo need to check duplicate uuid */
199 for (i = 0; i < BTA_GATTC_CL_MAX; i ++)
201 if (!p_cb->cl_rcb[i].in_use)
203 if ((p_app_uuid == NULL) || (p_cb->cl_rcb[i].client_if = GATT_Register(p_app_uuid, &bta_gattc_cl_cback)) == 0)
205 APPL_TRACE_ERROR("Register with GATT stack failed.");
206 status = BTA_GATT_ERROR;
210 p_cb->cl_rcb[i].in_use = TRUE;
211 p_cb->cl_rcb[i].p_cback = p_data->api_reg.p_cback;
212 memcpy(&p_cb->cl_rcb[i].app_uuid, p_app_uuid, sizeof(tBT_UUID));
214 /* BTA use the same client interface as BTE GATT statck */
215 cb_data.reg_oper.client_if = p_cb->cl_rcb[i].client_if;
217 if ((p_buf = (tBTA_GATTC_INT_START_IF *) GKI_getbuf(sizeof(tBTA_GATTC_INT_START_IF))) != NULL)
219 p_buf->hdr.event = BTA_GATTC_INT_START_IF_EVT;
220 p_buf->client_if = p_cb->cl_rcb[i].client_if;
222 bta_sys_sendmsg(p_buf);
223 status = BTA_GATT_OK;
227 GATT_Deregister(p_cb->cl_rcb[i].client_if);
229 status = BTA_GATT_NO_RESOURCES;
230 memset( &p_cb->cl_rcb[i], 0 , sizeof(tBTA_GATTC_RCB));
237 /* callback with register event */
238 if (p_data->api_reg.p_cback)
240 if (p_app_uuid != NULL)
241 memcpy(&(cb_data.reg_oper.app_uuid),p_app_uuid,sizeof(tBT_UUID));
243 cb_data.reg_oper.status = status;
244 (*p_data->api_reg.p_cback)(BTA_GATTC_REG_EVT, (tBTA_GATTC *)&cb_data);
247 /*******************************************************************************
249 ** Function bta_gattc_start_if
251 ** Description start an application interface.
255 *******************************************************************************/
256 void bta_gattc_start_if(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA *p_msg)
260 if (bta_gattc_cl_get_regcb(p_msg->int_start_if.client_if) !=NULL )
262 GATT_StartIf(p_msg->int_start_if.client_if);
266 APPL_TRACE_ERROR("Unable to start app.: Unknown interface =%d",p_msg->int_start_if.client_if );
269 /*******************************************************************************
271 ** Function bta_gattc_deregister
273 ** Description De-Register a GATT client application with BTA.
277 *******************************************************************************/
278 void bta_gattc_deregister(tBTA_GATTC_CB *p_cb, tBTA_GATTC_RCB *p_clreg)
285 /* remove bg connection associated with this rcb */
286 for (i = 0; i < BTA_GATTC_KNOWN_SR_MAX; i ++)
288 if (p_cb->bg_track[i].in_use)
290 if (p_cb->bg_track[i].cif_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, FALSE);
293 GATT_CancelConnect(p_clreg->client_if, p_cb->bg_track[i].remote_bda, FALSE);
295 if (p_cb->bg_track[i].cif_adv_mask & (1 <<(p_clreg->client_if - 1)))
297 bta_gattc_mark_bg_conn(p_clreg->client_if, p_cb->bg_track[i].remote_bda, FALSE, TRUE);
302 if (p_clreg->num_clcb > 0)
304 /* close all CLCB related to this app */
305 for (i= 0; i < BTA_GATTC_CLCB_MAX; i ++)
307 if (p_cb->clcb[i].in_use && (p_cb->clcb[i].p_rcb == p_clreg))
309 p_clreg->dereg_pending = TRUE;
311 buf.event = BTA_GATTC_API_CLOSE_EVT;
312 buf.layer_specific = p_cb->clcb[i].bta_conn_id;
313 bta_gattc_close(&p_cb->clcb[i], (tBTA_GATTC_DATA *)&buf) ;
318 bta_gattc_deregister_cmpl(p_clreg);
322 APPL_TRACE_ERROR("bta_gattc_deregister Deregister Failedm unknown client cif");
325 /*******************************************************************************
327 ** Function bta_gattc_process_api_open
329 ** Description process connect API request.
333 *******************************************************************************/
334 void bta_gattc_process_api_open (tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA * p_msg)
336 UINT16 event = ((BT_HDR *)p_msg)->event;
337 tBTA_GATTC_CLCB *p_clcb = NULL;
338 tBTA_GATTC_RCB *p_clreg = bta_gattc_cl_get_regcb(p_msg->api_conn.client_if);
343 if (p_msg->api_conn.is_direct)
345 if ((p_clcb = bta_gattc_find_alloc_clcb(p_msg->api_conn.client_if,
346 p_msg->api_conn.remote_bda,
347 p_msg->api_conn.transport)) != NULL)
349 bta_gattc_sm_execute(p_clcb, event, p_msg);
353 APPL_TRACE_ERROR("No resources to open a new connection.");
355 bta_gattc_send_open_cback(p_clreg,
356 BTA_GATT_NO_RESOURCES,
357 p_msg->api_conn.remote_bda,
358 BTA_GATT_INVALID_CONN_ID,
359 p_msg->api_conn.transport, 0);
364 bta_gattc_init_bk_conn(&p_msg->api_conn, p_clreg);
369 APPL_TRACE_ERROR("bta_gattc_process_api_open Failed, unknown client_if: %d",
370 p_msg->api_conn.client_if);
373 /*******************************************************************************
375 ** Function bta_gattc_process_api_open_cancel
377 ** Description process connect API request.
381 *******************************************************************************/
382 void bta_gattc_process_api_open_cancel (tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA * p_msg)
384 UINT16 event = ((BT_HDR *)p_msg)->event;
385 tBTA_GATTC_CLCB *p_clcb = NULL;
386 tBTA_GATTC_RCB *p_clreg;
390 if (p_msg->api_cancel_conn.is_direct)
392 if ((p_clcb = bta_gattc_find_clcb_by_cif(p_msg->api_cancel_conn.client_if,
393 p_msg->api_cancel_conn.remote_bda,
394 BTA_GATT_TRANSPORT_LE)) != NULL)
396 bta_gattc_sm_execute(p_clcb, event, p_msg);
400 APPL_TRACE_ERROR("No such connection need to be cancelled");
402 p_clreg = bta_gattc_cl_get_regcb(p_msg->api_cancel_conn.client_if);
404 if (p_clreg && p_clreg->p_cback)
406 cb_data.status = BTA_GATT_ERROR;
407 (*p_clreg->p_cback)(BTA_GATTC_CANCEL_OPEN_EVT, &cb_data);
413 bta_gattc_cancel_bk_conn(&p_msg->api_cancel_conn);
418 /*******************************************************************************
420 ** Function bta_gattc_process_enc_cmpl
422 ** Description process encryption complete message.
426 *******************************************************************************/
427 void bta_gattc_process_enc_cmpl(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA *p_msg)
429 tBTA_GATTC_RCB *p_clreg;
433 p_clreg = bta_gattc_cl_get_regcb(p_msg->enc_cmpl.client_if);
435 if (p_clreg && p_clreg->p_cback)
437 memset(&cb_data, 0, sizeof(tBTA_GATTC));
439 cb_data.enc_cmpl.client_if = p_msg->enc_cmpl.client_if;
440 bdcpy(cb_data.enc_cmpl.remote_bda, p_msg->enc_cmpl.remote_bda);
442 (*p_clreg->p_cback)(BTA_GATTC_ENC_CMPL_CB_EVT, &cb_data);
446 /*******************************************************************************
448 ** Function bta_gattc_cancel_open_error
454 *******************************************************************************/
455 void bta_gattc_cancel_open_error(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
460 cb_data.status=BTA_GATT_ERROR;
462 if ( p_clcb && p_clcb->p_rcb && p_clcb->p_rcb->p_cback )
463 (*p_clcb->p_rcb->p_cback)(BTA_GATTC_CANCEL_OPEN_EVT, &cb_data);
466 /*******************************************************************************
468 ** Function bta_gattc_open_error
474 *******************************************************************************/
475 void bta_gattc_open_error(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
479 APPL_TRACE_ERROR("Connection already opened. wrong state");
481 bta_gattc_send_open_cback(p_clcb->p_rcb,
488 /*******************************************************************************
490 ** Function bta_gattc_open_fail
496 *******************************************************************************/
497 void bta_gattc_open_fail(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
501 bta_gattc_send_open_cback(p_clcb->p_rcb,
507 /* open failure, remove clcb */
508 bta_gattc_clcb_dealloc(p_clcb);
511 /*******************************************************************************
513 ** Function bta_gattc_open
515 ** Description Process API connection function.
519 *******************************************************************************/
520 void bta_gattc_open(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
522 tBTA_GATTC_DATA gattc_data;
524 /* open/hold a connection */
525 if (!GATT_Connect(p_clcb->p_rcb->client_if, p_data->api_conn.remote_bda,
526 TRUE, p_data->api_conn.transport))
528 APPL_TRACE_ERROR("Connection open failure");
530 bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_OPEN_FAIL_EVT, p_data);
534 /* a connected remote device */
535 if (GATT_GetConnIdIfConnected(p_clcb->p_rcb->client_if,
536 p_data->api_conn.remote_bda,
537 &p_clcb->bta_conn_id,
538 p_data->api_conn.transport))
540 gattc_data.int_conn.hdr.layer_specific = p_clcb->bta_conn_id;
542 bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_CONN_EVT, &gattc_data);
544 /* else wait for the callback event */
547 /*******************************************************************************
549 ** Function bta_gattc_init_bk_conn
551 ** Description Process API Open for a background connection
555 *******************************************************************************/
556 void bta_gattc_init_bk_conn(tBTA_GATTC_API_OPEN *p_data, tBTA_GATTC_RCB *p_clreg)
558 tBTA_GATT_STATUS status = BTA_GATT_NO_RESOURCES;
560 tBTA_GATTC_CLCB *p_clcb;
561 tBTA_GATTC_DATA gattc_data;
563 if (bta_gattc_mark_bg_conn(p_data->client_if, p_data->remote_bda, TRUE, FALSE))
565 /* always call open to hold a connection */
566 if (!GATT_Connect(p_data->client_if, p_data->remote_bda, FALSE, p_data->transport))
568 uint8_t *bda = (uint8_t *)p_data->remote_bda;
569 status = BTA_GATT_ERROR;
570 APPL_TRACE_ERROR("%s unable to connect to remote bd_addr:%02x:%02x:%02x:%02x:%02x:%02x",
571 __func__, bda[0], bda[1], bda[2], bda[3], bda[4], bda[5]);
576 status = BTA_GATT_OK;
578 /* if is a connected remote device */
579 if (GATT_GetConnIdIfConnected(p_data->client_if,
584 if ((p_clcb = bta_gattc_find_alloc_clcb(p_data->client_if, p_data->remote_bda,
585 BTA_GATT_TRANSPORT_LE)) != NULL)
587 gattc_data.hdr.layer_specific = p_clcb->bta_conn_id = conn_id;
589 /* open connection */
590 bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_CONN_EVT, &gattc_data);
591 status = BTA_GATT_OK;
597 /* open failure, report OPEN_EVT */
598 if (status != BTA_GATT_OK)
600 bta_gattc_send_open_cback(p_clreg, status, p_data->remote_bda,
601 BTA_GATT_INVALID_CONN_ID, BTA_GATT_TRANSPORT_LE, 0);
604 /*******************************************************************************
606 ** Function bta_gattc_cancel_bk_conn
608 ** Description Process API Cancel Open for a background connection
612 *******************************************************************************/
613 void bta_gattc_cancel_bk_conn(tBTA_GATTC_API_CANCEL_OPEN *p_data)
615 tBTA_GATTC_RCB *p_clreg;
617 cb_data.status = BTA_GATT_ERROR;
619 /* remove the device from the bg connection mask */
620 if (bta_gattc_mark_bg_conn(p_data->client_if, p_data->remote_bda, FALSE, FALSE))
622 if (GATT_CancelConnect(p_data->client_if, p_data->remote_bda, FALSE))
624 cb_data.status = BTA_GATT_OK;
628 APPL_TRACE_ERROR("bta_gattc_cancel_bk_conn failed");
631 p_clreg = bta_gattc_cl_get_regcb(p_data->client_if);
633 if (p_clreg && p_clreg->p_cback)
635 (*p_clreg->p_cback)(BTA_GATTC_CANCEL_OPEN_EVT, &cb_data);
639 /*******************************************************************************
641 ** Function bta_gattc_int_cancel_open_ok
647 *******************************************************************************/
648 void bta_gattc_cancel_open_ok(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
653 if ( p_clcb->p_rcb->p_cback )
655 cb_data.status = BTA_GATT_OK;
656 (*p_clcb->p_rcb->p_cback)(BTA_GATTC_CANCEL_OPEN_EVT, &cb_data);
659 bta_gattc_clcb_dealloc(p_clcb);
661 /*******************************************************************************
663 ** Function bta_gattc_cancel_open
669 *******************************************************************************/
670 void bta_gattc_cancel_open(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
674 if (GATT_CancelConnect(p_clcb->p_rcb->client_if, p_data->api_cancel_conn.remote_bda, TRUE))
676 bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_CANCEL_OPEN_OK_EVT, p_data);
680 if ( p_clcb->p_rcb->p_cback )
682 cb_data.status = BTA_GATT_ERROR;
683 (*p_clcb->p_rcb->p_cback)(BTA_GATTC_CANCEL_OPEN_EVT, &cb_data);
687 /*******************************************************************************
689 ** Function bta_gattc_conn
691 ** Description receive connection callback from stack
695 *******************************************************************************/
696 void bta_gattc_conn(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
698 tBTA_GATTC_IF gatt_if;
699 APPL_TRACE_DEBUG("bta_gattc_conn server cache state=%d",p_clcb->p_srcb->state);
703 APPL_TRACE_DEBUG("bta_gattc_conn conn_id=%d",p_data->hdr.layer_specific);
704 p_clcb->bta_conn_id = p_data->int_conn.hdr.layer_specific;
706 GATT_GetConnectionInfor(p_data->hdr.layer_specific,
707 &gatt_if, p_clcb->bda, &p_clcb->transport);
710 p_clcb->p_srcb->connected = TRUE;
712 if (p_clcb->p_srcb->mtu == 0)
713 p_clcb->p_srcb->mtu = GATT_DEF_BLE_MTU_SIZE;
715 /* start database cache if needed */
716 if (p_clcb->p_srcb->p_srvc_cache == NULL ||
717 p_clcb->p_srcb->state != BTA_GATTC_SERV_IDLE)
719 if (p_clcb->p_srcb->state == BTA_GATTC_SERV_IDLE)
721 p_clcb->p_srcb->state = BTA_GATTC_SERV_LOAD;
722 bta_gattc_sm_execute(p_clcb, BTA_GATTC_START_CACHE_EVT, NULL);
724 else /* cache is building */
725 p_clcb->state = BTA_GATTC_DISCOVER_ST;
730 /* a pending service handle change indication */
731 if (p_clcb->p_srcb->srvc_hdl_chg)
733 p_clcb->p_srcb->srvc_hdl_chg = FALSE;
734 /* start discovery */
735 bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_DISCOVER_EVT, NULL);
741 /* there is no RM for GATT */
742 if (p_clcb->transport == BTA_TRANSPORT_BR_EDR)
743 bta_sys_conn_open(BTA_ID_GATTC, BTA_ALL_APP_ID, p_clcb->bda);
745 bta_gattc_send_open_cback(p_clcb->p_rcb,
750 p_clcb->p_srcb->mtu);
753 /*******************************************************************************
755 ** Function bta_gattc_close_fail
757 ** Description close a connection.
761 *******************************************************************************/
762 void bta_gattc_close_fail(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
766 if ( p_clcb->p_rcb->p_cback )
768 memset(&cb_data, 0, sizeof(tBTA_GATTC));
769 cb_data.close.client_if = p_clcb->p_rcb->client_if;
770 cb_data.close.conn_id = p_data->hdr.layer_specific;
771 bdcpy(cb_data.close.remote_bda, p_clcb->bda);
772 cb_data.close.status = BTA_GATT_ERROR;
773 cb_data.close.reason = BTA_GATT_CONN_NONE;
775 (*p_clcb->p_rcb->p_cback)(BTA_GATTC_CLOSE_EVT, &cb_data);
778 /*******************************************************************************
780 ** Function bta_gattc_api_close
782 ** Description close a GATTC connection.
786 *******************************************************************************/
787 void bta_gattc_close(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
789 tBTA_GATTC_CBACK *p_cback = p_clcb->p_rcb->p_cback;
790 tBTA_GATTC_RCB *p_clreg = p_clcb->p_rcb;
793 APPL_TRACE_DEBUG("bta_gattc_close conn_id=%d",p_clcb->bta_conn_id);
795 cb_data.close.client_if = p_clcb->p_rcb->client_if;
796 cb_data.close.conn_id = p_clcb->bta_conn_id;
797 cb_data.close.reason = p_clcb->reason;
798 cb_data.close.status = p_clcb->status;
799 bdcpy(cb_data.close.remote_bda, p_clcb->bda);
801 if (p_clcb->transport == BTA_TRANSPORT_BR_EDR)
802 bta_sys_conn_close( BTA_ID_GATTC ,BTA_ALL_APP_ID, p_clcb->bda);
804 bta_gattc_clcb_dealloc(p_clcb);
806 if (p_data->hdr.event == BTA_GATTC_API_CLOSE_EVT)
808 cb_data.close.status = GATT_Disconnect(p_data->hdr.layer_specific);
810 else if (p_data->hdr.event == BTA_GATTC_INT_DISCONN_EVT)
812 cb_data.close.status = p_data->int_conn.reason;
813 cb_data.close.reason = p_data->int_conn.reason;
817 (* p_cback)(BTA_GATTC_CLOSE_EVT, (tBTA_GATTC *)&cb_data);
819 if (p_clreg->num_clcb == 0 && p_clreg->dereg_pending)
821 bta_gattc_deregister_cmpl(p_clreg);
824 /*******************************************************************************
826 ** Function bta_gattc_reset_discover_st
828 ** Description when a SRCB finished discovery, tell all related clcb.
832 *******************************************************************************/
833 void bta_gattc_reset_discover_st(tBTA_GATTC_SERV *p_srcb, tBTA_GATT_STATUS status)
835 tBTA_GATTC_CB *p_cb = &bta_gattc_cb;
838 for (i = 0; i < BTA_GATTC_CLCB_MAX; i ++)
840 if (p_cb->clcb[i].p_srcb == p_srcb)
842 p_cb->clcb[i].status = status;
843 bta_gattc_sm_execute(&p_cb->clcb[i], BTA_GATTC_DISCOVER_CMPL_EVT, NULL);
847 /*******************************************************************************
849 ** Function bta_gattc_disc_close
851 ** Description close a GATTC connection while in discovery state.
855 *******************************************************************************/
856 void bta_gattc_disc_close(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
858 APPL_TRACE_DEBUG("%s: Discovery cancel conn_id=%d", __func__,
859 p_clcb->bta_conn_id);
861 if (p_clcb->disc_active)
862 bta_gattc_reset_discover_st(p_clcb->p_srcb, BTA_GATT_ERROR);
864 p_clcb->state = BTA_GATTC_CONN_ST;
866 // This function only gets called as the result of a BTA_GATTC_API_CLOSE_EVT
867 // while in the BTA_GATTC_DISCOVER_ST state. Once the state changes, the
868 // connection itself still needs to be closed to resolve the original event.
869 if (p_clcb->state == BTA_GATTC_CONN_ST)
871 APPL_TRACE_DEBUG("State is back to BTA_GATTC_CONN_ST. "
872 "Trigger connection close");
873 bta_gattc_close(p_clcb, p_data);
876 /*******************************************************************************
878 ** Function bta_gattc_set_discover_st
880 ** Description when a SRCB start discovery, tell all related clcb and set
885 *******************************************************************************/
886 void bta_gattc_set_discover_st(tBTA_GATTC_SERV *p_srcb)
888 tBTA_GATTC_CB *p_cb = &bta_gattc_cb;
891 #if BLE_INCLUDED == TRUE
892 L2CA_EnableUpdateBleConnParams(p_srcb->server_bda, FALSE);
894 for (i = 0; i < BTA_GATTC_CLCB_MAX; i ++)
896 if (p_cb->clcb[i].p_srcb == p_srcb)
898 p_cb->clcb[i].status = BTA_GATT_OK;
899 p_cb->clcb[i].state = BTA_GATTC_DISCOVER_ST;
903 /*******************************************************************************
905 ** Function bta_gattc_restart_discover
907 ** Description process service change in discovery state, mark up the auto
908 ** update flag and set status to be discovery cancel for current
913 *******************************************************************************/
914 void bta_gattc_restart_discover(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
918 p_clcb->status = BTA_GATT_CANCEL;
919 p_clcb->auto_update = BTA_GATTC_DISC_WAITING;
922 /*******************************************************************************
924 ** Function bta_gattc_cfg_mtu
926 ** Description Configure MTU size on the GATT connection.
930 *******************************************************************************/
931 void bta_gattc_cfg_mtu(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
933 tBTA_GATT_STATUS status;
935 if (bta_gattc_enqueue(p_clcb, p_data))
937 status = GATTC_ConfigureMTU (p_clcb->bta_conn_id, p_data->api_mtu.mtu);
939 /* if failed, return callback here */
940 if (status != GATT_SUCCESS && status != GATT_CMD_STARTED)
942 /* Dequeue the data, if it was enqueued */
943 if (p_clcb->p_q_cmd == p_data)
944 p_clcb->p_q_cmd = NULL;
946 bta_gattc_cmpl_sendmsg(p_clcb->bta_conn_id, GATTC_OPTYPE_CONFIG, status, NULL);
950 /*******************************************************************************
952 ** Function bta_gattc_start_discover
954 ** Description Start a discovery on server.
958 *******************************************************************************/
959 void bta_gattc_start_discover(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
963 APPL_TRACE_DEBUG("bta_gattc_start_discover conn_id=%d p_clcb->p_srcb->state = %d ",
964 p_clcb->bta_conn_id, p_clcb->p_srcb->state);
966 if (((p_clcb->p_q_cmd == NULL || p_clcb->auto_update == BTA_GATTC_REQ_WAITING) &&
967 p_clcb->p_srcb->state == BTA_GATTC_SERV_IDLE) ||
968 p_clcb->p_srcb->state == BTA_GATTC_SERV_DISC)
969 /* no pending operation, start discovery right away */
971 p_clcb->auto_update = BTA_GATTC_NO_SCHEDULE;
973 if (p_clcb->p_srcb != NULL)
975 /* clear the service change mask */
976 p_clcb->p_srcb->srvc_hdl_chg = FALSE;
977 p_clcb->p_srcb->update_count = 0;
978 p_clcb->p_srcb->state = BTA_GATTC_SERV_DISC_ACT;
980 if (p_clcb->transport == BTA_TRANSPORT_LE)
981 L2CA_EnableUpdateBleConnParams(p_clcb->p_srcb->server_bda, FALSE);
983 /* set all srcb related clcb into discovery ST */
984 bta_gattc_set_discover_st(p_clcb->p_srcb);
986 if ((p_clcb->status = bta_gattc_init_cache(p_clcb->p_srcb)) == BTA_GATT_OK)
988 p_clcb->status = bta_gattc_discover_pri_service(p_clcb->bta_conn_id,
989 p_clcb->p_srcb, GATT_DISC_SRVC_ALL);
991 if (p_clcb->status != BTA_GATT_OK)
993 APPL_TRACE_ERROR("discovery on server failed");
994 bta_gattc_reset_discover_st(p_clcb->p_srcb, p_clcb->status);
997 p_clcb->disc_active = TRUE;
1001 APPL_TRACE_ERROR("unknown device, can not start discovery");
1004 /* pending operation, wait until it finishes */
1007 p_clcb->auto_update = BTA_GATTC_DISC_WAITING;
1009 if (p_clcb->p_srcb->state == BTA_GATTC_SERV_IDLE)
1010 p_clcb->state = BTA_GATTC_CONN_ST; /* set clcb state */
1014 /*******************************************************************************
1016 ** Function bta_gattc_disc_cmpl
1018 ** Description discovery on server is finished
1022 *******************************************************************************/
1023 void bta_gattc_disc_cmpl(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1025 tBTA_GATTC_DATA *p_q_cmd = p_clcb->p_q_cmd;
1028 APPL_TRACE_DEBUG("bta_gattc_disc_cmpl conn_id=%d",p_clcb->bta_conn_id);
1030 #if BLE_INCLUDED == TRUE
1031 if(p_clcb->transport == BTA_TRANSPORT_LE)
1032 L2CA_EnableUpdateBleConnParams(p_clcb->p_srcb->server_bda, TRUE);
1034 p_clcb->p_srcb->state = BTA_GATTC_SERV_IDLE;
1035 p_clcb->disc_active = FALSE;
1037 if (p_clcb->status != GATT_SUCCESS)
1039 /* clean up cache */
1040 if(p_clcb->p_srcb && p_clcb->p_srcb->p_srvc_cache)
1042 while (!GKI_queue_is_empty(&p_clcb->p_srcb->cache_buffer))
1044 GKI_freebuf (GKI_dequeue (&p_clcb->p_srcb->cache_buffer));
1046 p_clcb->p_srcb->p_srvc_cache = NULL;
1049 /* used to reset cache in application */
1050 bta_gattc_co_cache_reset(p_clcb->p_srcb->server_bda);
1052 /* release pending attribute list buffer */
1053 utl_freebuf((void **)&p_clcb->p_srcb->p_srvc_list);
1055 if (p_clcb->auto_update == BTA_GATTC_DISC_WAITING)
1057 /* start discovery again */
1058 bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_DISCOVER_EVT, NULL);
1060 /* get any queued command to proceed */
1061 else if (p_q_cmd != NULL)
1063 p_clcb->p_q_cmd = NULL;
1065 bta_gattc_sm_execute(p_clcb, p_q_cmd->hdr.event, p_q_cmd);
1066 /* if the command executed requeued the cmd, we don't
1067 * want to free the underlying buffer that's being
1068 * referenced by p_clcb->p_q_cmd
1070 if (p_q_cmd != p_clcb->p_q_cmd) {
1071 utl_freebuf((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)
1087 tGATT_READ_PARAM read_param;
1088 tBTA_GATT_STATUS status;
1090 memset (&read_param, 0 ,sizeof(tGATT_READ_PARAM));
1092 if (bta_gattc_enqueue(p_clcb, p_data))
1094 if ((handle = bta_gattc_id2handle(p_clcb->p_srcb,
1095 &p_data->api_read.srvc_id,
1096 &p_data->api_read.char_id,
1097 p_data->api_read.p_descr_type)) == 0)
1099 status = BTA_GATT_ERROR;
1103 read_param.by_handle.handle = handle;
1104 read_param.by_handle.auth_req = p_data->api_read.auth_req;
1106 status = GATTC_Read(p_clcb->bta_conn_id, GATT_READ_BY_HANDLE, &read_param);
1110 if (status != BTA_GATT_OK)
1112 /* Dequeue the data, if it was enqueued */
1113 if (p_clcb->p_q_cmd == p_data)
1114 p_clcb->p_q_cmd = NULL;
1116 bta_gattc_cmpl_sendmsg(p_clcb->bta_conn_id, GATTC_OPTYPE_READ, status, NULL);
1120 /*******************************************************************************
1122 ** Function bta_gattc_read_multi
1124 ** Description read multiple
1127 *********************************************************************************/
1128 void bta_gattc_read_multi(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1131 tBTA_GATT_STATUS status = BTA_GATT_OK;
1132 tGATT_READ_PARAM read_param;
1133 tBTA_GATTC_ATTR_ID *p_id;
1135 if (bta_gattc_enqueue(p_clcb, p_data))
1137 memset(&read_param, 0, sizeof(tGATT_READ_PARAM));
1139 p_id = p_data->api_read_multi.p_id_list;
1141 for (i = 0; i < p_data->api_read_multi.num_attr && p_id; i ++, p_id ++)
1145 if (p_id->id_type == BTA_GATT_TYPE_CHAR)
1147 handle = bta_gattc_id2handle(p_clcb->p_srcb,
1148 &p_id->id_value.char_id.srvc_id,
1149 &p_id->id_value.char_id.char_id,
1152 else if (p_id->id_type == BTA_GATT_TYPE_CHAR_DESCR)
1154 handle = bta_gattc_id2handle(p_clcb->p_srcb,
1155 &p_id->id_value.char_descr_id.char_id.srvc_id,
1156 &p_id->id_value.char_descr_id.char_id.char_id,
1157 &p_id->id_value.char_descr_id.descr_id);
1161 APPL_TRACE_ERROR("invalud ID type: %d", p_id->id_type);
1166 status = BTA_GATT_ERROR;
1170 if (status == BTA_GATT_OK)
1172 read_param.read_multiple.num_handles = p_data->api_read_multi.num_attr;
1173 read_param.read_multiple.auth_req = p_data->api_read_multi.auth_req;
1175 status = GATTC_Read(p_clcb->bta_conn_id, GATT_READ_MULTIPLE, &read_param);
1179 if (status != BTA_GATT_OK)
1181 /* Dequeue the data, if it was enqueued */
1182 if (p_clcb->p_q_cmd == p_data)
1183 p_clcb->p_q_cmd = NULL;
1185 bta_gattc_cmpl_sendmsg(p_clcb->bta_conn_id, GATTC_OPTYPE_READ, status, NULL);
1189 /*******************************************************************************
1191 ** Function bta_gattc_write
1193 ** Description Write an attribute
1197 *******************************************************************************/
1198 void bta_gattc_write(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1201 tGATT_VALUE attr = {0};
1202 tBTA_GATT_STATUS status = BTA_GATT_OK;
1204 if (bta_gattc_enqueue(p_clcb, p_data))
1206 if ((handle = bta_gattc_id2handle(p_clcb->p_srcb,
1207 &p_data->api_write.srvc_id,
1208 &p_data->api_write.char_id,
1209 p_data->api_write.p_descr_type)) == 0)
1211 status = BTA_GATT_ERROR;
1215 attr.handle= handle;
1216 attr.offset = p_data->api_write.offset;
1217 attr.len = p_data->api_write.len;
1218 attr.auth_req = p_data->api_write.auth_req;
1220 if (p_data->api_write.p_value)
1221 memcpy(attr.value, p_data->api_write.p_value, p_data->api_write.len);
1223 status = GATTC_Write(p_clcb->bta_conn_id, p_data->api_write.write_type, &attr);
1227 if (status != BTA_GATT_OK)
1229 /* Dequeue the data, if it was enqueued */
1230 if (p_clcb->p_q_cmd == p_data)
1231 p_clcb->p_q_cmd = NULL;
1233 bta_gattc_cmpl_sendmsg(p_clcb->bta_conn_id, GATTC_OPTYPE_WRITE, status, NULL);
1237 /*******************************************************************************
1239 ** Function bta_gattc_execute
1241 ** Description send execute write
1244 *********************************************************************************/
1245 void bta_gattc_execute(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1247 tBTA_GATT_STATUS status;
1249 if (bta_gattc_enqueue(p_clcb, p_data))
1251 status = GATTC_ExecuteWrite(p_clcb->bta_conn_id, p_data->api_exec.is_execute);
1253 if (status != BTA_GATT_OK)
1255 /* Dequeue the data, if it was enqueued */
1256 if (p_clcb->p_q_cmd == p_data)
1257 p_clcb->p_q_cmd = NULL;
1259 bta_gattc_cmpl_sendmsg(p_clcb->bta_conn_id, GATTC_OPTYPE_EXE_WRITE, status, NULL);
1263 /*******************************************************************************
1265 ** Function bta_gattc_confirm
1267 ** Description send handle value confirmation
1271 *******************************************************************************/
1272 void bta_gattc_confirm(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1276 if ((handle = bta_gattc_id2handle(p_clcb->p_srcb,
1277 &p_data->api_confirm.srvc_id,
1278 &p_data->api_confirm.char_id,
1281 APPL_TRACE_ERROR("Can not map service/char ID into valid handle");
1285 if (GATTC_SendHandleValueConfirm(p_data->api_confirm.hdr.layer_specific, handle)
1288 APPL_TRACE_ERROR("bta_gattc_confirm to handle [0x%04x] failed", handle);
1292 /* if over BR_EDR, inform PM for mode change */
1293 if (p_clcb->transport == BTA_TRANSPORT_BR_EDR)
1295 bta_sys_busy(BTA_ID_GATTC, BTA_ALL_APP_ID, p_clcb->bda);
1296 bta_sys_idle(BTA_ID_GATTC, BTA_ALL_APP_ID, p_clcb->bda);
1301 /*******************************************************************************
1303 ** Function bta_gattc_read_cmpl
1305 ** Description read complete
1309 *******************************************************************************/
1310 void bta_gattc_read_cmpl(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_OP_CMPL *p_data)
1314 tBTA_GATT_READ_VAL read_value;
1316 memset(&cb_data, 0, sizeof(tBTA_GATTC));
1317 memset(&read_value, 0, sizeof(tBTA_GATT_READ_VAL));
1319 cb_data.read.status = p_data->status;
1321 if (p_data->p_cmpl != NULL && p_data->status == BTA_GATT_OK)
1323 if (bta_gattc_handle2id(p_clcb->p_srcb,
1324 p_data->p_cmpl->att_value.handle,
1325 &cb_data.read.srvc_id,
1326 &cb_data.read.char_id,
1327 &cb_data.read.descr_type) == FALSE)
1329 cb_data.read.status = BTA_GATT_INTERNAL_ERROR;
1330 APPL_TRACE_ERROR("can not map to GATT ID. handle = 0x%04x",
1331 p_data->p_cmpl->att_value.handle);
1335 cb_data.read.status = bta_gattc_pack_read_cb_data(p_clcb->p_srcb,
1336 &cb_data.read.descr_type.uuid,
1337 &p_data->p_cmpl->att_value,
1339 cb_data.read.p_value = &read_value;
1344 cb_data.read.srvc_id = p_clcb->p_q_cmd->api_read.srvc_id;
1345 cb_data.read.char_id = p_clcb->p_q_cmd->api_read.char_id;
1346 if (p_clcb->p_q_cmd->api_read.p_descr_type)
1347 memcpy(&cb_data.read.descr_type, p_clcb->p_q_cmd->api_read.p_descr_type,
1348 sizeof(tBTA_GATT_ID));
1351 event = (p_clcb->p_q_cmd->api_read.p_descr_type == NULL) ?
1352 BTA_GATTC_READ_CHAR_EVT: BTA_GATTC_READ_DESCR_EVT;
1353 cb_data.read.conn_id = p_clcb->bta_conn_id;
1355 utl_freebuf((void **)&p_clcb->p_q_cmd);
1356 /* read complete, callback */
1357 ( *p_clcb->p_rcb->p_cback)(event, (tBTA_GATTC *)&cb_data);
1360 /*******************************************************************************
1362 ** Function bta_gattc_write_cmpl
1364 ** Description write complete
1368 *******************************************************************************/
1369 void bta_gattc_write_cmpl(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_OP_CMPL *p_data)
1371 tBTA_GATTC cb_data = {0};
1374 memset(&cb_data, 0, sizeof(tBTA_GATTC));
1376 cb_data.write.status = p_data->status;
1378 if (p_data->p_cmpl != NULL)
1380 bta_gattc_handle2id(p_clcb->p_srcb, p_data->p_cmpl->att_value.handle,
1381 &cb_data.write.srvc_id, &cb_data.write.char_id,
1382 &cb_data.write.descr_type);
1386 memcpy(&cb_data.write.srvc_id, &p_clcb->p_q_cmd->api_write.srvc_id,
1387 sizeof(tBTA_GATT_SRVC_ID));
1388 memcpy(&cb_data.write.char_id, &p_clcb->p_q_cmd->api_write.char_id,
1389 sizeof(tBTA_GATT_ID));
1390 if (p_clcb->p_q_cmd->api_write.p_descr_type)
1391 memcpy(&cb_data.write.descr_type, p_clcb->p_q_cmd->api_write.p_descr_type,
1392 sizeof(tBTA_GATT_ID));
1395 if (p_clcb->p_q_cmd->api_write.hdr.event == BTA_GATTC_API_WRITE_EVT &&
1396 p_clcb->p_q_cmd->api_write.write_type == BTA_GATTC_WRITE_PREPARE)
1398 event = BTA_GATTC_PREP_WRITE_EVT;
1400 else if (p_clcb->p_q_cmd->api_write.p_descr_type == NULL)
1402 event = BTA_GATTC_WRITE_CHAR_EVT;
1405 event = BTA_GATTC_WRITE_DESCR_EVT;
1407 utl_freebuf((void **)&p_clcb->p_q_cmd);
1408 cb_data.write.conn_id = p_clcb->bta_conn_id;
1409 /* write complete, callback */
1410 ( *p_clcb->p_rcb->p_cback)(event, (tBTA_GATTC *)&cb_data);
1413 /*******************************************************************************
1415 ** Function bta_gattc_exec_cmpl
1417 ** Description execute write complete
1421 *******************************************************************************/
1422 void bta_gattc_exec_cmpl(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_OP_CMPL *p_data)
1426 utl_freebuf((void **)&p_clcb->p_q_cmd);
1428 p_clcb->status = BTA_GATT_OK;
1430 /* execute complete, callback */
1431 cb_data.exec_cmpl.conn_id = p_clcb->bta_conn_id;
1432 cb_data.exec_cmpl.status = p_data->status;
1434 ( *p_clcb->p_rcb->p_cback)(BTA_GATTC_EXEC_EVT, &cb_data);
1438 /*******************************************************************************
1440 ** Function bta_gattc_cfg_mtu_cmpl
1442 ** Description configure MTU operation complete
1446 *******************************************************************************/
1447 void bta_gattc_cfg_mtu_cmpl(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_OP_CMPL *p_data)
1451 utl_freebuf((void **)&p_clcb->p_q_cmd);
1453 if (p_data->p_cmpl && p_data->status == BTA_GATT_OK)
1454 p_clcb->p_srcb->mtu = p_data->p_cmpl->mtu;
1456 /* configure MTU complete, callback */
1457 p_clcb->status = p_data->status;
1458 cb_data.cfg_mtu.conn_id = p_clcb->bta_conn_id;
1459 cb_data.cfg_mtu.status = p_data->status;
1460 cb_data.cfg_mtu.mtu = p_clcb->p_srcb->mtu;
1462 (*p_clcb->p_rcb->p_cback) (BTA_GATTC_CFG_MTU_EVT, &cb_data);
1465 /*******************************************************************************
1467 ** Function bta_gattc_op_cmpl
1469 ** Description operation completed.
1473 *******************************************************************************/
1474 void bta_gattc_op_cmpl(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1476 UINT8 op = (UINT8)p_data->op_cmpl.op_code;
1477 UINT8 mapped_op = 0;
1479 APPL_TRACE_DEBUG("bta_gattc_op_cmpl op = %d", op);
1481 if (op == GATTC_OPTYPE_INDICATION || op == GATTC_OPTYPE_NOTIFICATION)
1483 APPL_TRACE_ERROR("unexpected operation, ignored");
1485 else if (op >= GATTC_OPTYPE_READ)
1487 if (p_clcb->p_q_cmd == NULL)
1489 APPL_TRACE_ERROR("No pending command");
1492 if (p_clcb->p_q_cmd->hdr.event != bta_gattc_opcode_to_int_evt[op - GATTC_OPTYPE_READ])
1494 mapped_op = p_clcb->p_q_cmd->hdr.event - BTA_GATTC_API_READ_EVT + GATTC_OPTYPE_READ;
1495 if ( mapped_op > GATTC_OPTYPE_INDICATION) mapped_op = 0;
1497 #if (BT_TRACE_VERBOSE == TRUE)
1498 APPL_TRACE_ERROR("expect op:(%s :0x%04x), receive unexpected operation (%s).",
1499 bta_gattc_op_code_name[mapped_op] , p_clcb->p_q_cmd->hdr.event,
1500 bta_gattc_op_code_name[op]);
1502 APPL_TRACE_ERROR("expect op:(%u :0x%04x), receive unexpected operation (%u).",
1503 mapped_op , p_clcb->p_q_cmd->hdr.event, op);
1508 /* discard responses if service change indication is received before operation completed */
1509 if (p_clcb->auto_update == BTA_GATTC_DISC_WAITING && p_clcb->p_srcb->srvc_hdl_chg)
1511 APPL_TRACE_DEBUG("Discard all responses when service change indication is received.");
1512 p_data->op_cmpl.status = GATT_ERROR;
1515 /* service handle change void the response, discard it */
1516 if (op == GATTC_OPTYPE_READ)
1517 bta_gattc_read_cmpl(p_clcb, &p_data->op_cmpl);
1519 else if (op == GATTC_OPTYPE_WRITE)
1520 bta_gattc_write_cmpl(p_clcb, &p_data->op_cmpl);
1522 else if (op == GATTC_OPTYPE_EXE_WRITE)
1523 bta_gattc_exec_cmpl(p_clcb, &p_data->op_cmpl);
1525 else if (op == GATTC_OPTYPE_CONFIG)
1526 bta_gattc_cfg_mtu_cmpl(p_clcb, &p_data->op_cmpl);
1528 if (p_clcb->auto_update == BTA_GATTC_DISC_WAITING)
1530 p_clcb->auto_update = BTA_GATTC_REQ_WAITING;
1531 bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_DISCOVER_EVT, NULL);
1535 /*******************************************************************************
1537 ** Function bta_gattc_op_cmpl
1539 ** Description operation completed.
1543 *******************************************************************************/
1544 void bta_gattc_ignore_op_cmpl(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1548 /* receive op complete when discovery is started, ignore the response,
1549 and wait for discovery finish and resent */
1550 APPL_TRACE_DEBUG("bta_gattc_ignore_op_cmpl op = %d", p_data->hdr.layer_specific);
1553 /*******************************************************************************
1555 ** Function bta_gattc_search
1557 ** Description start a search in the local server cache
1561 *******************************************************************************/
1562 void bta_gattc_search(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1564 tBTA_GATT_STATUS status = GATT_INTERNAL_ERROR;
1566 APPL_TRACE_DEBUG("bta_gattc_search conn_id=%d",p_clcb->bta_conn_id);
1567 if (p_clcb->p_srcb && p_clcb->p_srcb->p_srvc_cache)
1569 status = BTA_GATT_OK;
1570 /* search the local cache of a server device */
1571 bta_gattc_search_service(p_clcb, p_data->api_search.p_srvc_uuid);
1573 cb_data.search_cmpl.status = status;
1574 cb_data.search_cmpl.conn_id = p_clcb->bta_conn_id;
1576 /* end of search or no server cache available */
1577 ( *p_clcb->p_rcb->p_cback)(BTA_GATTC_SEARCH_CMPL_EVT, &cb_data);
1579 /*******************************************************************************
1581 ** Function bta_gattc_q_cmd
1583 ** Description enqueue a command into control block, usually because discovery
1584 ** operation is busy.
1588 *******************************************************************************/
1589 void bta_gattc_q_cmd(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1591 bta_gattc_enqueue(p_clcb, p_data);
1593 /*******************************************************************************
1595 ** Function bta_gattc_cache_open
1597 ** Description open a NV cache for loading
1601 *******************************************************************************/
1602 void bta_gattc_cache_open(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1606 bta_gattc_set_discover_st(p_clcb->p_srcb);
1608 APPL_TRACE_DEBUG("bta_gattc_cache_open conn_id=%d",p_clcb->bta_conn_id);
1609 bta_gattc_co_cache_open(p_clcb->p_srcb->server_bda, BTA_GATTC_CI_CACHE_OPEN_EVT,
1610 p_clcb->bta_conn_id, FALSE);
1612 /*******************************************************************************
1614 ** Function bta_gattc_start_load
1616 ** Description start cache loading by sending callout open cache
1620 *******************************************************************************/
1621 void bta_gattc_ci_open(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1623 APPL_TRACE_DEBUG("bta_gattc_ci_open conn_id=%d server state=%d" ,
1624 p_clcb->bta_conn_id, p_clcb->p_srcb->state);
1625 if (p_clcb->p_srcb->state == BTA_GATTC_SERV_LOAD)
1627 if (p_data->ci_open.status == BTA_GATT_OK)
1629 p_clcb->p_srcb->attr_index = 0;
1630 bta_gattc_co_cache_load(p_clcb->p_srcb->server_bda,
1631 BTA_GATTC_CI_CACHE_LOAD_EVT,
1632 p_clcb->p_srcb->attr_index,
1633 p_clcb->bta_conn_id);
1637 p_clcb->p_srcb->state = BTA_GATTC_SERV_DISC;
1638 /* cache open failure, start discovery */
1639 bta_gattc_start_discover(p_clcb, NULL);
1642 if (p_clcb->p_srcb->state == BTA_GATTC_SERV_SAVE)
1644 if (p_data->ci_open.status == BTA_GATT_OK)
1646 if (!bta_gattc_cache_save(p_clcb->p_srcb, p_clcb->bta_conn_id))
1648 p_data->ci_open.status = BTA_GATT_ERROR;
1651 if (p_data->ci_open.status != BTA_GATT_OK)
1653 p_clcb->p_srcb->attr_index = 0;
1654 bta_gattc_co_cache_close(p_clcb->p_srcb->server_bda, p_clcb->bta_conn_id);
1655 bta_gattc_reset_discover_st(p_clcb->p_srcb, p_clcb->status);
1660 /*******************************************************************************
1662 ** Function bta_gattc_ci_load
1664 ** Description cache loading received.
1668 *******************************************************************************/
1669 void bta_gattc_ci_load(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1672 APPL_TRACE_DEBUG("bta_gattc_ci_load conn_id=%d load status=%d",
1673 p_clcb->bta_conn_id, p_data->ci_load.status);
1675 if (p_data->ci_load.status == BTA_GATT_OK ||
1676 p_data->ci_load.status == BTA_GATT_MORE)
1678 if (p_data->ci_load.num_attr != 0)
1679 bta_gattc_rebuild_cache(p_clcb->p_srcb, p_data->ci_load.num_attr,
1680 p_data->ci_load.attr, p_clcb->p_srcb->attr_index);
1682 if (p_data->ci_load.status == BTA_GATT_OK)
1684 p_clcb->p_srcb->attr_index = 0;
1685 bta_gattc_reset_discover_st(p_clcb->p_srcb, BTA_GATT_OK);
1686 bta_gattc_co_cache_close(p_clcb->p_srcb->server_bda, 0);
1688 else /* load more */
1690 p_clcb->p_srcb->attr_index += p_data->ci_load.num_attr;
1692 bta_gattc_co_cache_load(p_clcb->p_srcb->server_bda,
1693 BTA_GATTC_CI_CACHE_LOAD_EVT,
1694 p_clcb->p_srcb->attr_index,
1695 p_clcb->bta_conn_id);
1700 bta_gattc_co_cache_close(p_clcb->p_srcb->server_bda, 0);
1701 p_clcb->p_srcb->state = BTA_GATTC_SERV_DISC;
1702 p_clcb->p_srcb->attr_index = 0;
1703 /* cache load failure, start discovery */
1704 bta_gattc_start_discover(p_clcb, NULL);
1707 /*******************************************************************************
1709 ** Function bta_gattc_ci_save
1711 ** Description cache loading received.
1715 *******************************************************************************/
1716 void bta_gattc_ci_save(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1720 APPL_TRACE_DEBUG("bta_gattc_ci_save conn_id=%d " ,
1721 p_clcb->bta_conn_id );
1723 if (!bta_gattc_cache_save(p_clcb->p_srcb, p_clcb->bta_conn_id))
1725 p_clcb->p_srcb->attr_index = 0;
1726 bta_gattc_co_cache_close(p_clcb->p_srcb->server_bda, 0);
1727 bta_gattc_reset_discover_st(p_clcb->p_srcb, p_clcb->status);
1730 /*******************************************************************************
1732 ** Function bta_gattc_fail
1734 ** Description report API call failure back to apps
1738 *******************************************************************************/
1739 void bta_gattc_fail(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1743 if (p_clcb->status == BTA_GATT_OK)
1745 APPL_TRACE_ERROR("operation not supported at current state [%d]", p_clcb->state);
1749 /*******************************************************************************
1751 ** Function bta_gattc_deregister_cmpl
1753 ** Description De-Register a GATT client application with BTA completed.
1757 *******************************************************************************/
1758 static void bta_gattc_deregister_cmpl(tBTA_GATTC_RCB *p_clreg)
1760 tBTA_GATTC_CB *p_cb = &bta_gattc_cb;
1761 tBTA_GATTC_IF client_if = p_clreg->client_if;
1763 tBTA_GATTC_CBACK *p_cback = p_clreg->p_cback;
1765 memset(&cb_data, 0, sizeof(tBTA_GATTC));
1767 GATT_Deregister(p_clreg->client_if);
1768 memset(p_clreg, 0, sizeof(tBTA_GATTC_RCB));
1770 cb_data.reg_oper.client_if = client_if;
1771 cb_data.reg_oper.status = BTA_GATT_OK;
1774 /* callback with de-register event */
1775 (*p_cback)(BTA_GATTC_DEREG_EVT, (tBTA_GATTC *)&cb_data);
1777 if (bta_gattc_num_reg_app() == 0 && p_cb->state == BTA_GATTC_STATE_DISABLING)
1779 p_cb->state = BTA_GATTC_STATE_DISABLED;
1782 /*******************************************************************************
1784 ** Function bta_gattc_conn_cback
1786 ** Description callback functions to GATT client stack.
1790 *******************************************************************************/
1791 static void bta_gattc_conn_cback(tGATT_IF gattc_if, BD_ADDR bda, UINT16 conn_id,
1792 BOOLEAN connected, tGATT_DISCONN_REASON reason,
1793 tBT_TRANSPORT transport)
1795 tBTA_GATTC_DATA *p_buf;
1799 APPL_TRACE_WARNING("%s() - cif=%d connected=%d conn_id=%d reason=0x%04x",
1800 __FUNCTION__, gattc_if, connected, conn_id, reason);
1804 bdcpy(bdaddr.address, bda);
1806 btif_debug_conn_state(bdaddr, BTIF_DEBUG_CONNECTED, GATT_CONN_UNKNOWN);
1808 btif_debug_conn_state(bdaddr, BTIF_DEBUG_DISCONNECTED, reason);
1810 if ((p_buf = (tBTA_GATTC_DATA *) GKI_getbuf(sizeof(tBTA_GATTC_DATA))) != NULL)
1812 memset(p_buf, 0, sizeof(tBTA_GATTC_DATA));
1814 p_buf->int_conn.hdr.event = connected ? BTA_GATTC_INT_CONN_EVT:
1815 BTA_GATTC_INT_DISCONN_EVT;
1816 p_buf->int_conn.hdr.layer_specific = conn_id;
1817 p_buf->int_conn.client_if = gattc_if;
1818 p_buf->int_conn.role = L2CA_GetBleConnRole(bda);
1819 p_buf->int_conn.reason = reason;
1820 p_buf->int_conn.transport = transport;
1821 bdcpy(p_buf->int_conn.remote_bda, bda);
1823 bta_sys_sendmsg(p_buf);
1827 /*******************************************************************************
1829 ** Function bta_gattc_enc_cmpl_cback
1831 ** Description encryption complete callback function to GATT client stack.
1835 *******************************************************************************/
1836 static void bta_gattc_enc_cmpl_cback(tGATT_IF gattc_if, BD_ADDR bda)
1838 tBTA_GATTC_DATA *p_buf;
1839 tBTA_GATTC_CLCB *p_clcb = NULL;
1841 if ((p_clcb = bta_gattc_find_clcb_by_cif(gattc_if, bda, BTA_GATT_TRANSPORT_LE)) == NULL)
1846 #if (defined BTA_HH_LE_INCLUDED && BTA_HH_LE_INCLUDED == TRUE)
1847 /* filter this event just for BTA HH LE GATT client,
1848 In the future, if we want to enable encryption complete event
1849 for all GATT clients, we can remove this code */
1850 if (!bta_hh_le_is_hh_gatt_if(gattc_if))
1856 APPL_TRACE_DEBUG("bta_gattc_enc_cmpl_cback: cif = %d", gattc_if);
1858 if ((p_buf = (tBTA_GATTC_DATA *) GKI_getbuf(sizeof(tBTA_GATTC_DATA))) != NULL)
1860 memset(p_buf, 0, sizeof(tBTA_GATTC_DATA));
1862 p_buf->enc_cmpl.hdr.event = BTA_GATTC_ENC_CMPL_EVT;
1863 p_buf->enc_cmpl.hdr.layer_specific = p_clcb->bta_conn_id;
1864 p_buf->enc_cmpl.client_if = gattc_if;
1865 bdcpy(p_buf->enc_cmpl.remote_bda, bda);
1867 bta_sys_sendmsg(p_buf);
1871 /*******************************************************************************
1873 ** Function bta_gattc_process_api_refresh
1875 ** Description process refresh API to delete cache and start a new discovery
1876 ** if currently connected.
1880 *******************************************************************************/
1881 void bta_gattc_process_api_refresh(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA * p_msg)
1883 tBTA_GATTC_SERV *p_srvc_cb = bta_gattc_find_srvr_cache(p_msg->api_conn.remote_bda);
1884 tBTA_GATTC_CLCB *p_clcb = &bta_gattc_cb.clcb[0];
1885 BOOLEAN found = FALSE;
1889 if (p_srvc_cb != NULL)
1891 /* try to find a CLCB */
1892 if (p_srvc_cb->connected && p_srvc_cb->num_clcb != 0)
1894 for (i = 0; i < BTA_GATTC_CLCB_MAX; i ++, p_clcb ++)
1896 if (p_clcb->in_use && p_clcb->p_srcb == p_srvc_cb)
1904 bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_DISCOVER_EVT, NULL);
1908 /* in all other cases, mark it and delete the cache */
1909 if (p_srvc_cb->p_srvc_cache != NULL)
1911 while (!GKI_queue_is_empty(&p_srvc_cb->cache_buffer))
1912 GKI_freebuf (GKI_dequeue (&p_srvc_cb->cache_buffer));
1914 p_srvc_cb->p_srvc_cache = NULL;
1917 /* used to reset cache in application */
1918 bta_gattc_co_cache_reset(p_msg->api_conn.remote_bda);
1921 /*******************************************************************************
1923 ** Function bta_gattc_process_srvc_chg_ind
1925 ** Description process service change indication.
1929 *******************************************************************************/
1930 BOOLEAN bta_gattc_process_srvc_chg_ind(UINT16 conn_id,
1931 tBTA_GATTC_RCB *p_clrcb,
1932 tBTA_GATTC_SERV *p_srcb,
1933 tBTA_GATTC_CLCB *p_clcb,
1934 tBTA_GATTC_NOTIFY *p_notify,
1937 tBT_UUID gattp_uuid, srvc_chg_uuid;
1938 BOOLEAN processed = FALSE;
1942 gattp_uuid.uu.uuid16 = UUID_SERVCLASS_GATT_SERVER;
1944 srvc_chg_uuid.len = 2;
1945 srvc_chg_uuid.uu.uuid16 = GATT_UUID_GATT_SRV_CHGD;
1947 if (bta_gattc_uuid_compare(&p_notify->char_id.srvc_id.id.uuid, &gattp_uuid, TRUE) &&
1948 bta_gattc_uuid_compare(&p_notify->char_id.char_id.uuid, &srvc_chg_uuid, TRUE))
1951 /* mark service handle change pending */
1952 p_srcb->srvc_hdl_chg = TRUE;
1953 /* clear up all notification/indication registration */
1954 bta_gattc_clear_notif_registration(conn_id);
1955 /* service change indication all received, do discovery update */
1956 if ( ++ p_srcb->update_count == bta_gattc_num_reg_app())
1958 /* not an opened connection; or connection busy */
1959 /* search for first available clcb and start discovery */
1960 if (p_clcb == NULL || (p_clcb && p_clcb->p_q_cmd != NULL))
1962 for (i = 0 ; i < BTA_GATTC_CLCB_MAX; i ++)
1964 if (bta_gattc_cb.clcb[i].in_use &&
1965 bta_gattc_cb.clcb[i].p_srcb == p_srcb &&
1966 bta_gattc_cb.clcb[i].p_q_cmd == NULL)
1968 p_clcb = &bta_gattc_cb.clcb[i];
1973 /* send confirmation here if this is an indication, it should always be */
1974 GATTC_SendHandleValueConfirm(conn_id, handle);
1976 /* if connection available, refresh cache by doing discovery now */
1978 bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_DISCOVER_EVT, NULL);
1980 /* notify applicationf or service change */
1981 if (p_clrcb->p_cback != NULL)
1983 (* p_clrcb->p_cback)(BTA_GATTC_SRVC_CHG_EVT, (tBTA_GATTC *)p_srcb->server_bda);
1991 /*******************************************************************************
1993 ** Function bta_gattc_proc_other_indication
1995 ** Description process all non-service change indication/notification.
1999 *******************************************************************************/
2000 void bta_gattc_proc_other_indication(tBTA_GATTC_CLCB *p_clcb, UINT8 op,
2001 tGATT_CL_COMPLETE *p_data,
2002 tBTA_GATTC_NOTIFY *p_notify)
2004 APPL_TRACE_DEBUG("bta_gattc_proc_other_indication check \
2005 p_data->att_value.handle=%d p_data->handle=%d",
2006 p_data->att_value.handle, p_data->handle);
2007 APPL_TRACE_DEBUG("is_notify", p_notify->is_notify);
2009 p_notify->is_notify = (op == GATTC_OPTYPE_INDICATION) ? FALSE : TRUE;
2010 p_notify->len = p_data->att_value.len;
2011 bdcpy(p_notify->bda, p_clcb->bda);
2012 memcpy(p_notify->value, p_data->att_value.value, p_data->att_value.len);
2013 p_notify->conn_id = p_clcb->bta_conn_id;
2015 if (p_clcb->p_rcb->p_cback)
2016 (*p_clcb->p_rcb->p_cback)(BTA_GATTC_NOTIF_EVT, (tBTA_GATTC *)p_notify);
2019 /*******************************************************************************
2021 ** Function bta_gattc_process_indicate
2023 ** Description process indication/notification.
2027 *******************************************************************************/
2028 void bta_gattc_process_indicate(UINT16 conn_id, tGATTC_OPTYPE op, tGATT_CL_COMPLETE *p_data)
2030 UINT16 handle = p_data->att_value.handle;
2031 tBTA_GATTC_CLCB *p_clcb ;
2032 tBTA_GATTC_RCB *p_clrcb = NULL;
2033 tBTA_GATTC_SERV *p_srcb = NULL;
2034 tBTA_GATTC_NOTIFY notify;
2036 tBTA_GATTC_IF gatt_if;
2037 tBTA_TRANSPORT transport;
2039 if (!GATT_GetConnectionInfor(conn_id, &gatt_if, remote_bda, &transport))
2041 APPL_TRACE_ERROR("%s indication/notif for unknown app", __func__);
2042 if (op == GATTC_OPTYPE_INDICATION)
2043 GATTC_SendHandleValueConfirm(conn_id, handle);
2047 if ((p_clrcb = bta_gattc_cl_get_regcb(gatt_if)) == NULL)
2049 APPL_TRACE_ERROR("%s indication/notif for unregistered app", __func__);
2050 if (op == GATTC_OPTYPE_INDICATION)
2051 GATTC_SendHandleValueConfirm(conn_id, handle);
2055 if ((p_srcb = bta_gattc_find_srcb(remote_bda)) == NULL)
2057 APPL_TRACE_ERROR("%s indication/notif for unknown device, ignore", __func__);
2058 if (op == GATTC_OPTYPE_INDICATION)
2059 GATTC_SendHandleValueConfirm(conn_id, handle);
2063 p_clcb = bta_gattc_find_clcb_by_conn_id(conn_id);
2065 if (bta_gattc_handle2id(p_srcb, handle,
2066 ¬ify.char_id.srvc_id,
2067 ¬ify.char_id.char_id,
2068 ¬ify.descr_type))
2070 /* if non-service change indication/notification, forward to application */
2071 if (!bta_gattc_process_srvc_chg_ind(conn_id, p_clrcb, p_srcb, p_clcb, ¬ify, handle))
2073 /* if app registered for the notification */
2074 if (bta_gattc_check_notif_registry(p_clrcb, p_srcb, ¬ify))
2076 /* connection not open yet */
2079 if ((p_clcb = bta_gattc_clcb_alloc(gatt_if, remote_bda, transport)) != NULL)
2081 p_clcb->bta_conn_id = conn_id;
2082 p_clcb->transport = transport;
2084 bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_CONN_EVT, NULL);
2088 APPL_TRACE_ERROR("No resources");
2093 bta_gattc_proc_other_indication(p_clcb, op, p_data, ¬ify);
2095 /* no one intersted and need ack? */
2096 else if (op == GATTC_OPTYPE_INDICATION)
2098 APPL_TRACE_DEBUG("%s no one interested, ack now", __func__);
2099 GATTC_SendHandleValueConfirm(conn_id, handle);
2105 APPL_TRACE_ERROR("%s Indi/Notif for Unknown handle[0x%04x], can not find in local cache.",
2107 if (op == GATTC_OPTYPE_INDICATION)
2108 GATTC_SendHandleValueConfirm(conn_id, handle);
2111 /*******************************************************************************
2113 ** Function bta_gattc_cmpl_cback
2115 ** Description client operation complete callback register with BTE GATT.
2119 *******************************************************************************/
2120 static void bta_gattc_cmpl_cback(UINT16 conn_id, tGATTC_OPTYPE op, tGATT_STATUS status,
2121 tGATT_CL_COMPLETE *p_data)
2123 tBTA_GATTC_CLCB *p_clcb;
2124 APPL_TRACE_DEBUG("bta_gattc_cmpl_cback: conn_id = %d op = %d status = %d",
2125 conn_id, op, status);
2127 /* notification and indication processed right away */
2128 if (op == GATTC_OPTYPE_NOTIFICATION || op == GATTC_OPTYPE_INDICATION)
2130 bta_gattc_process_indicate(conn_id, op, p_data);
2133 /* for all other operation, not expected if w/o connection */
2134 else if ((p_clcb = bta_gattc_find_clcb_by_conn_id(conn_id)) == NULL)
2136 APPL_TRACE_ERROR("bta_gattc_cmpl_cback unknown conn_id = %d, ignore data", conn_id);
2140 /* if over BR_EDR, inform PM for mode change */
2141 if (p_clcb->transport == BTA_TRANSPORT_BR_EDR)
2143 bta_sys_busy(BTA_ID_GATTC, BTA_ALL_APP_ID, p_clcb->bda);
2144 bta_sys_idle(BTA_ID_GATTC, BTA_ALL_APP_ID, p_clcb->bda);
2147 bta_gattc_cmpl_sendmsg(conn_id, op, status, p_data);
2150 /*******************************************************************************
2152 ** Function bta_gattc_cmpl_sendmsg
2154 ** Description client operation complete send message
2158 *******************************************************************************/
2159 static void bta_gattc_cmpl_sendmsg(UINT16 conn_id, tGATTC_OPTYPE op,
2160 tBTA_GATT_STATUS status,
2161 tGATT_CL_COMPLETE *p_data)
2163 const UINT16 len = sizeof(tBTA_GATTC_OP_CMPL) + sizeof(tGATT_CL_COMPLETE);
2164 tBTA_GATTC_OP_CMPL *p_buf = (tBTA_GATTC_OP_CMPL *) GKI_getbuf(len);
2168 memset(p_buf, 0, len);
2169 p_buf->hdr.event = BTA_GATTC_OP_CMPL_EVT;
2170 p_buf->hdr.layer_specific = conn_id;
2171 p_buf->status = status;
2172 p_buf->op_code = op;
2176 p_buf->p_cmpl = (tGATT_CL_COMPLETE *)(p_buf + 1);
2177 memcpy(p_buf->p_cmpl, p_data, sizeof(tGATT_CL_COMPLETE));
2180 bta_sys_sendmsg(p_buf);
2184 /*******************************************************************************
2186 ** Function bta_gattc_cong_cback
2188 ** Description congestion callback for BTA GATT client.
2192 ********************************************************************************/
2193 static void bta_gattc_cong_cback (UINT16 conn_id, BOOLEAN congested)
2195 tBTA_GATTC_CLCB *p_clcb;
2198 if ((p_clcb = bta_gattc_find_clcb_by_conn_id(conn_id)) != NULL)
2200 if (p_clcb->p_rcb->p_cback)
2202 cb_data.congest.conn_id = conn_id;
2203 cb_data.congest.congested = congested;
2205 (*p_clcb->p_rcb->p_cback)(BTA_GATTC_CONGEST_EVT, &cb_data);
2210 #if BLE_INCLUDED == TRUE
2211 /*******************************************************************************
2213 ** Function bta_gattc_init_clcb_conn
2215 ** Description Initaite a BTA CLCB connection
2219 ********************************************************************************/
2220 void bta_gattc_init_clcb_conn(UINT8 cif, BD_ADDR remote_bda)
2222 tBTA_GATTC_CLCB *p_clcb = NULL;
2223 tBTA_GATTC_DATA gattc_data;
2226 /* should always get the connection ID */
2227 if (GATT_GetConnIdIfConnected(cif, remote_bda, &conn_id, BTA_GATT_TRANSPORT_LE) == FALSE)
2229 APPL_TRACE_ERROR("bta_gattc_init_clcb_conn ERROR: not a connected device");
2233 /* initaite a new connection here */
2234 if ((p_clcb = bta_gattc_clcb_alloc(cif, remote_bda, BTA_GATT_TRANSPORT_LE)) != NULL)
2236 gattc_data.hdr.layer_specific = p_clcb->bta_conn_id = conn_id;
2238 gattc_data.api_conn.client_if = cif;
2239 memcpy(gattc_data.api_conn.remote_bda, remote_bda, BD_ADDR_LEN);
2240 gattc_data.api_conn.is_direct = TRUE;
2242 bta_gattc_sm_execute(p_clcb, BTA_GATTC_API_OPEN_EVT, &gattc_data);
2246 APPL_TRACE_ERROR("No resources");
2249 /*******************************************************************************
2251 ** Function bta_gattc_process_listen_all
2253 ** Description process listen all, send open callback to application for all
2254 ** connected slave LE link.
2258 ********************************************************************************/
2259 void bta_gattc_process_listen_all(UINT8 cif)
2262 tBTA_GATTC_CONN *p_conn = &bta_gattc_cb.conn_track[0];
2264 for (i_conn = 0; i_conn < BTA_GATTC_CONN_MAX; i_conn++, p_conn ++)
2266 if (p_conn->in_use )
2268 if (bta_gattc_find_clcb_by_cif(cif, p_conn->remote_bda, BTA_GATT_TRANSPORT_LE) == NULL)
2270 bta_gattc_init_clcb_conn(cif, p_conn->remote_bda);
2272 /* else already connected */
2276 /*******************************************************************************
2278 ** Function bta_gattc_listen
2280 ** Description Start or stop a listen for connection
2284 ********************************************************************************/
2285 void bta_gattc_listen(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA * p_msg)
2287 tBTA_GATTC_RCB *p_clreg = bta_gattc_cl_get_regcb(p_msg->api_listen.client_if);
2291 cb_data.reg_oper.status = BTA_GATT_ERROR;
2292 cb_data.reg_oper.client_if = p_msg->api_listen.client_if;
2294 if (p_clreg == NULL)
2296 APPL_TRACE_ERROR("bta_gattc_listen failed, unknown client_if: %d",
2297 p_msg->api_listen.client_if);
2300 /* mark bg conn record */
2301 if (bta_gattc_mark_bg_conn(p_msg->api_listen.client_if,
2302 (BD_ADDR_PTR) p_msg->api_listen.remote_bda,
2303 p_msg->api_listen.start,
2306 if (!GATT_Listen(p_msg->api_listen.client_if,
2307 p_msg->api_listen.start,
2308 p_msg->api_listen.remote_bda))
2310 APPL_TRACE_ERROR("Listen failure");
2311 (*p_clreg->p_cback)(BTA_GATTC_LISTEN_EVT, &cb_data);
2315 cb_data.status = BTA_GATT_OK;
2317 (*p_clreg->p_cback)(BTA_GATTC_LISTEN_EVT, &cb_data);
2319 if (p_msg->api_listen.start)
2321 /* if listen to a specific target */
2322 if (p_msg->api_listen.remote_bda != NULL)
2325 /* if is a connected remote device */
2326 if (L2CA_GetBleConnRole(p_msg->api_listen.remote_bda) == HCI_ROLE_SLAVE &&
2327 bta_gattc_find_clcb_by_cif(p_msg->api_listen.client_if,
2328 p_msg->api_listen.remote_bda,
2329 BTA_GATT_TRANSPORT_LE) == NULL)
2332 bta_gattc_init_clcb_conn(p_msg->api_listen.client_if,
2333 p_msg->api_listen.remote_bda);
2336 /* if listen to all */
2339 LOG_DEBUG(LOG_TAG, "Listen For All now");
2340 /* go through all connected device and send
2341 callback for all connected slave connection */
2342 bta_gattc_process_listen_all(p_msg->api_listen.client_if);
2349 /*******************************************************************************
2351 ** Function bta_gattc_broadcast
2353 ** Description Start or stop broadcasting
2357 ********************************************************************************/
2358 void bta_gattc_broadcast(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA * p_msg)
2360 tBTA_GATTC_RCB *p_clreg = bta_gattc_cl_get_regcb(p_msg->api_listen.client_if);
2364 cb_data.reg_oper.client_if = p_msg->api_listen.client_if;
2365 cb_data.reg_oper.status = BTM_BleBroadcast(p_msg->api_listen.start);
2367 if (p_clreg && p_clreg->p_cback)
2368 (*p_clreg->p_cback)(BTA_GATTC_LISTEN_EVT, &cb_data);