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"
32 #include "bta_gattc_int.h"
34 #include "btif/include/btif_debug_conn.h"
37 #include "osi/include/log.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);
56 static void bta_gattc_deregister_cmpl(tBTA_GATTC_RCB *p_clreg);
57 static void bta_gattc_enc_cmpl_cback(tGATT_IF gattc_if, BD_ADDR bda);
58 static void bta_gattc_cong_cback (UINT16 conn_id, BOOLEAN congested);
60 static tGATT_CBACK bta_gattc_cl_cback =
64 bta_gattc_disc_res_cback,
65 bta_gattc_disc_cmpl_cback,
67 bta_gattc_enc_cmpl_cback,
71 /* opcode(tGATTC_OPTYPE) order has to be comply with internal event order */
72 static UINT16 bta_gattc_opcode_to_int_evt[] =
74 BTA_GATTC_API_READ_EVT,
75 BTA_GATTC_API_WRITE_EVT,
76 BTA_GATTC_API_EXEC_EVT,
77 BTA_GATTC_API_CFG_MTU_EVT
80 #if (BT_TRACE_VERBOSE == TRUE)
81 static const char *bta_gattc_op_code_name[] =
93 /*****************************************************************************
95 *****************************************************************************/
97 /*******************************************************************************
99 ** Function bta_gattc_enable
101 ** Description Enables GATTC module
106 *******************************************************************************/
107 static void bta_gattc_enable(tBTA_GATTC_CB *p_cb)
109 APPL_TRACE_DEBUG("bta_gattc_enable");
111 if (p_cb->state == BTA_GATTC_STATE_DISABLED)
113 /* initialize control block */
114 memset(&bta_gattc_cb, 0, sizeof(tBTA_GATTC_CB));
115 p_cb->state = BTA_GATTC_STATE_ENABLED;
119 APPL_TRACE_DEBUG("GATTC is arelady enabled");
124 /*******************************************************************************
126 ** Function bta_gattc_disable
128 ** Description Disable GATTC module by cleaning up all active connections
129 ** and deregister all application.
133 *******************************************************************************/
134 void bta_gattc_disable(tBTA_GATTC_CB *p_cb)
138 APPL_TRACE_DEBUG("bta_gattc_disable");
140 if (p_cb->state != BTA_GATTC_STATE_ENABLED)
142 APPL_TRACE_ERROR("not enabled or disable in pogress");
146 for (i = 0; i <BTA_GATTC_CL_MAX; i ++)
148 if (p_cb->cl_rcb[i].in_use)
150 p_cb->state = BTA_GATTC_STATE_DISABLING;
151 /* don't deregister HH GATT IF */
152 /* HH GATT IF will be deregistered by bta_hh_le_deregister when disable HH */
153 #if (defined BTA_HH_LE_INCLUDED && BTA_HH_LE_INCLUDED == TRUE)
154 if (!bta_hh_le_is_hh_gatt_if(p_cb->cl_rcb[i].client_if)) {
156 bta_gattc_deregister(p_cb, &p_cb->cl_rcb[i]);
157 #if (defined BTA_HH_LE_INCLUDED && BTA_HH_LE_INCLUDED == TRUE)
163 /* no registered apps, indicate disable completed */
164 if (p_cb->state != BTA_GATTC_STATE_DISABLING)
166 p_cb->state = BTA_GATTC_STATE_DISABLED;
167 memset(p_cb, 0, sizeof(tBTA_GATTC_CB));
171 /*******************************************************************************
173 ** Function bta_gattc_register
175 ** Description Register a GATT client application with BTA.
179 *******************************************************************************/
180 void bta_gattc_register(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA *p_data)
184 tBT_UUID *p_app_uuid = &p_data->api_reg.app_uuid;
185 tBTA_GATTC_INT_START_IF *p_buf;
186 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;
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("Discovery cancel conn_id=%d",p_clcb->bta_conn_id);
860 if (p_clcb->disc_active)
861 bta_gattc_reset_discover_st(p_clcb->p_srcb, BTA_GATT_ERROR);
863 p_clcb->state = BTA_GATTC_CONN_ST;
865 /*******************************************************************************
867 ** Function bta_gattc_set_discover_st
869 ** Description when a SRCB start discovery, tell all related clcb and set
874 *******************************************************************************/
875 void bta_gattc_set_discover_st(tBTA_GATTC_SERV *p_srcb)
877 tBTA_GATTC_CB *p_cb = &bta_gattc_cb;
880 #if BLE_INCLUDED == TRUE
881 L2CA_EnableUpdateBleConnParams(p_srcb->server_bda, FALSE);
883 for (i = 0; i < BTA_GATTC_CLCB_MAX; i ++)
885 if (p_cb->clcb[i].p_srcb == p_srcb)
887 p_cb->clcb[i].status = BTA_GATT_OK;
888 p_cb->clcb[i].state = BTA_GATTC_DISCOVER_ST;
892 /*******************************************************************************
894 ** Function bta_gattc_restart_discover
896 ** Description process service change in discovery state, mark up the auto
897 ** update flag and set status to be discovery cancel for current
902 *******************************************************************************/
903 void bta_gattc_restart_discover(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
907 p_clcb->status = BTA_GATT_CANCEL;
908 p_clcb->auto_update = BTA_GATTC_DISC_WAITING;
911 /*******************************************************************************
913 ** Function bta_gattc_cfg_mtu
915 ** Description Configure MTU size on the GATT connection.
919 *******************************************************************************/
920 void bta_gattc_cfg_mtu(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
922 tBTA_GATTC_OP_CMPL op_cmpl;
923 tBTA_GATT_STATUS status;
925 if (bta_gattc_enqueue(p_clcb, p_data))
927 status = GATTC_ConfigureMTU (p_clcb->bta_conn_id, p_data->api_mtu.mtu);
929 /* if failed, return callback here */
930 if (status != GATT_SUCCESS && status != GATT_CMD_STARTED)
932 memset(&op_cmpl, 0, sizeof(tBTA_GATTC_OP_CMPL));
934 op_cmpl.status = status;
935 op_cmpl.op_code = GATTC_OPTYPE_CONFIG;
936 op_cmpl.p_cmpl = NULL;
938 bta_gattc_sm_execute(p_clcb, BTA_GATTC_OP_CMPL_EVT, (tBTA_GATTC_DATA *)&op_cmpl);
942 /*******************************************************************************
944 ** Function bta_gattc_start_discover
946 ** Description Start a discovery on server.
950 *******************************************************************************/
951 void bta_gattc_start_discover(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
955 APPL_TRACE_DEBUG("bta_gattc_start_discover conn_id=%d p_clcb->p_srcb->state = %d ",
956 p_clcb->bta_conn_id, p_clcb->p_srcb->state);
958 if (((p_clcb->p_q_cmd == NULL || p_clcb->auto_update == BTA_GATTC_REQ_WAITING) &&
959 p_clcb->p_srcb->state == BTA_GATTC_SERV_IDLE) ||
960 p_clcb->p_srcb->state == BTA_GATTC_SERV_DISC)
961 /* no pending operation, start discovery right away */
963 p_clcb->auto_update = BTA_GATTC_NO_SCHEDULE;
965 if (p_clcb->p_srcb != NULL)
967 /* clear the service change mask */
968 p_clcb->p_srcb->srvc_hdl_chg = FALSE;
969 p_clcb->p_srcb->update_count = 0;
970 p_clcb->p_srcb->state = BTA_GATTC_SERV_DISC_ACT;
972 if (p_clcb->transport == BTA_TRANSPORT_LE)
973 L2CA_EnableUpdateBleConnParams(p_clcb->p_srcb->server_bda, FALSE);
975 /* set all srcb related clcb into discovery ST */
976 bta_gattc_set_discover_st(p_clcb->p_srcb);
978 if ((p_clcb->status = bta_gattc_init_cache(p_clcb->p_srcb)) == BTA_GATT_OK)
980 p_clcb->status = bta_gattc_discover_pri_service(p_clcb->bta_conn_id,
981 p_clcb->p_srcb, GATT_DISC_SRVC_ALL);
983 if (p_clcb->status != BTA_GATT_OK)
985 APPL_TRACE_ERROR("discovery on server failed");
986 bta_gattc_reset_discover_st(p_clcb->p_srcb, p_clcb->status);
989 p_clcb->disc_active = TRUE;
993 APPL_TRACE_ERROR("unknown device, can not start discovery");
996 /* pending operation, wait until it finishes */
999 p_clcb->auto_update = BTA_GATTC_DISC_WAITING;
1001 if (p_clcb->p_srcb->state == BTA_GATTC_SERV_IDLE)
1002 p_clcb->state = BTA_GATTC_CONN_ST; /* set clcb state */
1006 /*******************************************************************************
1008 ** Function bta_gattc_disc_cmpl
1010 ** Description discovery on server is finished
1014 *******************************************************************************/
1015 void bta_gattc_disc_cmpl(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1017 tBTA_GATTC_DATA *p_q_cmd = p_clcb->p_q_cmd;
1020 APPL_TRACE_DEBUG("bta_gattc_disc_cmpl conn_id=%d",p_clcb->bta_conn_id);
1022 #if BLE_INCLUDED == TRUE
1023 if(p_clcb->transport == BTA_TRANSPORT_LE)
1024 L2CA_EnableUpdateBleConnParams(p_clcb->p_srcb->server_bda, TRUE);
1026 p_clcb->p_srcb->state = BTA_GATTC_SERV_IDLE;
1027 p_clcb->disc_active = FALSE;
1029 if (p_clcb->status != GATT_SUCCESS)
1031 /* clean up cache */
1032 if(p_clcb->p_srcb && p_clcb->p_srcb->p_srvc_cache)
1034 while (!GKI_queue_is_empty(&p_clcb->p_srcb->cache_buffer))
1036 GKI_freebuf (GKI_dequeue (&p_clcb->p_srcb->cache_buffer));
1038 p_clcb->p_srcb->p_srvc_cache = NULL;
1041 /* used to reset cache in application */
1042 bta_gattc_co_cache_reset(p_clcb->p_srcb->server_bda);
1044 /* release pending attribute list buffer */
1045 utl_freebuf((void **)&p_clcb->p_srcb->p_srvc_list);
1047 if (p_clcb->auto_update == BTA_GATTC_DISC_WAITING)
1049 /* start discovery again */
1050 bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_DISCOVER_EVT, NULL);
1052 /* get any queued command to proceed */
1053 else if (p_q_cmd != NULL)
1055 p_clcb->p_q_cmd = NULL;
1057 bta_gattc_sm_execute(p_clcb, p_q_cmd->hdr.event, p_q_cmd);
1058 /* if the command executed requeued the cmd, we don't
1059 * want to free the underlying buffer that's being
1060 * referenced by p_clcb->p_q_cmd
1062 if (p_q_cmd != p_clcb->p_q_cmd) {
1063 utl_freebuf((void **)&p_q_cmd);
1067 /*******************************************************************************
1069 ** Function bta_gattc_read
1071 ** Description Read an attribute
1075 *******************************************************************************/
1076 void bta_gattc_read(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1079 tGATT_READ_PARAM read_param;
1080 tBTA_GATTC_OP_CMPL op_cmpl;
1082 memset (&read_param, 0 ,sizeof(tGATT_READ_PARAM));
1083 memset (&op_cmpl, 0 ,sizeof(tBTA_GATTC_OP_CMPL));
1085 if (bta_gattc_enqueue(p_clcb, p_data))
1087 if ((handle = bta_gattc_id2handle(p_clcb->p_srcb,
1088 &p_data->api_read.srvc_id,
1089 &p_data->api_read.char_id,
1090 p_data->api_read.p_descr_type)) == 0)
1092 op_cmpl.status = BTA_GATT_ERROR;
1096 read_param.by_handle.handle = handle;
1097 read_param.by_handle.auth_req = p_data->api_read.auth_req;
1099 op_cmpl.status = GATTC_Read(p_clcb->bta_conn_id, GATT_READ_BY_HANDLE, &read_param);
1103 if (op_cmpl.status != BTA_GATT_OK)
1105 op_cmpl.op_code = GATTC_OPTYPE_READ;
1106 op_cmpl.p_cmpl = NULL;
1108 bta_gattc_sm_execute(p_clcb, BTA_GATTC_OP_CMPL_EVT, (tBTA_GATTC_DATA *)&op_cmpl);
1112 /*******************************************************************************
1114 ** Function bta_gattc_read_multi
1116 ** Description read multiple
1119 *********************************************************************************/
1120 void bta_gattc_read_multi(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1123 tBTA_GATT_STATUS status = BTA_GATT_OK;
1124 tGATT_READ_PARAM read_param;
1125 tBTA_GATTC_OP_CMPL op_cmpl;
1126 tBTA_GATTC_ATTR_ID *p_id;
1128 if (bta_gattc_enqueue(p_clcb, p_data))
1130 memset(&read_param, 0, sizeof(tGATT_READ_PARAM));
1132 p_id = p_data->api_read_multi.p_id_list;
1134 for (i = 0; i < p_data->api_read_multi.num_attr && p_id; i ++, p_id ++)
1138 if (p_id->id_type == BTA_GATT_TYPE_CHAR)
1140 handle = bta_gattc_id2handle(p_clcb->p_srcb,
1141 &p_id->id_value.char_id.srvc_id,
1142 &p_id->id_value.char_id.char_id,
1145 else if (p_id->id_type == BTA_GATT_TYPE_CHAR_DESCR)
1147 handle = bta_gattc_id2handle(p_clcb->p_srcb,
1148 &p_id->id_value.char_descr_id.char_id.srvc_id,
1149 &p_id->id_value.char_descr_id.char_id.char_id,
1150 &p_id->id_value.char_descr_id.descr_id);
1154 APPL_TRACE_ERROR("invalud ID type: %d", p_id->id_type);
1159 status = BTA_GATT_ERROR;
1163 if (status == BTA_GATT_OK)
1165 read_param.read_multiple.num_handles = p_data->api_read_multi.num_attr;
1166 read_param.read_multiple.auth_req = p_data->api_read_multi.auth_req;
1168 status = GATTC_Read(p_clcb->bta_conn_id, GATT_READ_MULTIPLE, &read_param);
1172 if (status != BTA_GATT_OK)
1174 memset(&op_cmpl, 0, sizeof(tBTA_GATTC_OP_CMPL));
1176 op_cmpl.status = status;
1177 op_cmpl.op_code = GATTC_OPTYPE_READ;
1178 op_cmpl.p_cmpl = NULL;
1180 bta_gattc_sm_execute(p_clcb, BTA_GATTC_OP_CMPL_EVT, (tBTA_GATTC_DATA *)&op_cmpl);
1184 /*******************************************************************************
1186 ** Function bta_gattc_write
1188 ** Description Write an attribute
1192 *******************************************************************************/
1193 void bta_gattc_write(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1196 tGATT_VALUE attr = {0};
1197 tBTA_GATTC_OP_CMPL op_cmpl;
1198 tBTA_GATT_STATUS status = BTA_GATT_OK;
1200 if (bta_gattc_enqueue(p_clcb, p_data))
1202 if ((handle = bta_gattc_id2handle(p_clcb->p_srcb,
1203 &p_data->api_write.srvc_id,
1204 &p_data->api_write.char_id,
1205 p_data->api_write.p_descr_type)) == 0)
1207 status = BTA_GATT_ERROR;
1211 attr.handle= handle;
1212 attr.offset = p_data->api_write.offset;
1213 attr.len = p_data->api_write.len;
1214 attr.auth_req = p_data->api_write.auth_req;
1216 if (p_data->api_write.p_value)
1217 memcpy(attr.value, p_data->api_write.p_value, p_data->api_write.len);
1219 status = GATTC_Write(p_clcb->bta_conn_id, p_data->api_write.write_type, &attr);
1223 if (status != BTA_GATT_OK)
1225 memset(&op_cmpl, 0, sizeof(tBTA_GATTC_OP_CMPL));
1227 op_cmpl.status = status;
1228 op_cmpl.op_code = GATTC_OPTYPE_WRITE;
1229 op_cmpl.p_cmpl = NULL;
1231 bta_gattc_sm_execute(p_clcb, BTA_GATTC_OP_CMPL_EVT, (tBTA_GATTC_DATA *)&op_cmpl);
1235 /*******************************************************************************
1237 ** Function bta_gattc_execute
1239 ** Description send execute write
1242 *********************************************************************************/
1243 void bta_gattc_execute(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1245 tBTA_GATTC_OP_CMPL op_cmpl;
1246 tBTA_GATT_STATUS status;
1248 if (bta_gattc_enqueue(p_clcb, p_data))
1250 status = GATTC_ExecuteWrite(p_clcb->bta_conn_id, p_data->api_exec.is_execute);
1252 if (status != BTA_GATT_OK)
1254 memset(&op_cmpl, 0, sizeof(tBTA_GATTC_OP_CMPL));
1256 op_cmpl.status = status;
1257 op_cmpl.op_code = GATTC_OPTYPE_EXE_WRITE;
1258 op_cmpl.p_cmpl = NULL;
1260 bta_gattc_sm_execute(p_clcb, BTA_GATTC_OP_CMPL_EVT, (tBTA_GATTC_DATA *)&op_cmpl);
1264 /*******************************************************************************
1266 ** Function bta_gattc_confirm
1268 ** Description send handle value confirmation
1272 *******************************************************************************/
1273 void bta_gattc_confirm(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1277 if ((handle = bta_gattc_id2handle(p_clcb->p_srcb,
1278 &p_data->api_confirm.srvc_id,
1279 &p_data->api_confirm.char_id,
1282 APPL_TRACE_ERROR("Can not map service/char ID into valid handle");
1286 if (GATTC_SendHandleValueConfirm(p_data->api_confirm.hdr.layer_specific, handle)
1289 APPL_TRACE_ERROR("bta_gattc_confirm to handle [0x%04x] failed", handle);
1293 /* if over BR_EDR, inform PM for mode change */
1294 if (p_clcb->transport == BTA_TRANSPORT_BR_EDR)
1296 bta_sys_busy(BTA_ID_GATTC, BTA_ALL_APP_ID, p_clcb->bda);
1297 bta_sys_idle(BTA_ID_GATTC, BTA_ALL_APP_ID, p_clcb->bda);
1302 /*******************************************************************************
1304 ** Function bta_gattc_read_cmpl
1306 ** Description read complete
1310 *******************************************************************************/
1311 void bta_gattc_read_cmpl(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_OP_CMPL *p_data)
1315 tBTA_GATT_READ_VAL read_value;
1317 memset(&cb_data, 0, sizeof(tBTA_GATTC));
1318 memset(&read_value, 0, sizeof(tBTA_GATT_READ_VAL));
1320 cb_data.read.status = p_data->status;
1322 if (p_data->p_cmpl != NULL && p_data->status == BTA_GATT_OK)
1324 if (bta_gattc_handle2id(p_clcb->p_srcb,
1325 p_data->p_cmpl->att_value.handle,
1326 &cb_data.read.srvc_id,
1327 &cb_data.read.char_id,
1328 &cb_data.read.descr_type) == FALSE)
1330 cb_data.read.status = BTA_GATT_INTERNAL_ERROR;
1331 APPL_TRACE_ERROR("can not map to GATT ID. handle = 0x%04x",
1332 p_data->p_cmpl->att_value.handle);
1336 cb_data.read.status = bta_gattc_pack_read_cb_data(p_clcb->p_srcb,
1337 &cb_data.read.descr_type.uuid,
1338 &p_data->p_cmpl->att_value,
1340 cb_data.read.p_value = &read_value;
1345 cb_data.read.srvc_id = p_clcb->p_q_cmd->api_read.srvc_id;
1346 cb_data.read.char_id = p_clcb->p_q_cmd->api_read.char_id;
1347 if (p_clcb->p_q_cmd->api_read.p_descr_type)
1348 memcpy(&cb_data.read.descr_type, p_clcb->p_q_cmd->api_read.p_descr_type,
1349 sizeof(tBTA_GATT_ID));
1352 event = (p_clcb->p_q_cmd->api_read.p_descr_type == NULL) ?
1353 BTA_GATTC_READ_CHAR_EVT: BTA_GATTC_READ_DESCR_EVT;
1354 cb_data.read.conn_id = p_clcb->bta_conn_id;
1356 utl_freebuf((void **)&p_clcb->p_q_cmd);
1357 /* read complete, callback */
1358 ( *p_clcb->p_rcb->p_cback)(event, (tBTA_GATTC *)&cb_data);
1361 /*******************************************************************************
1363 ** Function bta_gattc_write_cmpl
1365 ** Description write complete
1369 *******************************************************************************/
1370 void bta_gattc_write_cmpl(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_OP_CMPL *p_data)
1372 tBTA_GATTC cb_data = {0};
1375 memset(&cb_data, 0, sizeof(tBTA_GATTC));
1377 cb_data.write.status = p_data->status;
1379 if (p_data->p_cmpl != NULL)
1381 bta_gattc_handle2id(p_clcb->p_srcb, p_data->p_cmpl->att_value.handle,
1382 &cb_data.write.srvc_id, &cb_data.write.char_id,
1383 &cb_data.write.descr_type);
1387 memcpy(&cb_data.write.srvc_id, &p_clcb->p_q_cmd->api_write.srvc_id,
1388 sizeof(tBTA_GATT_SRVC_ID));
1389 memcpy(&cb_data.write.char_id, &p_clcb->p_q_cmd->api_write.char_id,
1390 sizeof(tBTA_GATT_ID));
1391 if (p_clcb->p_q_cmd->api_write.p_descr_type)
1392 memcpy(&cb_data.write.descr_type, p_clcb->p_q_cmd->api_write.p_descr_type,
1393 sizeof(tBTA_GATT_ID));
1396 if (p_clcb->p_q_cmd->api_write.hdr.event == BTA_GATTC_API_WRITE_EVT &&
1397 p_clcb->p_q_cmd->api_write.write_type == BTA_GATTC_WRITE_PREPARE)
1399 event = BTA_GATTC_PREP_WRITE_EVT;
1401 else if (p_clcb->p_q_cmd->api_write.p_descr_type == NULL)
1403 event = BTA_GATTC_WRITE_CHAR_EVT;
1406 event = BTA_GATTC_WRITE_DESCR_EVT;
1408 utl_freebuf((void **)&p_clcb->p_q_cmd);
1409 cb_data.write.conn_id = p_clcb->bta_conn_id;
1410 /* write complete, callback */
1411 ( *p_clcb->p_rcb->p_cback)(event, (tBTA_GATTC *)&cb_data);
1414 /*******************************************************************************
1416 ** Function bta_gattc_exec_cmpl
1418 ** Description execute write complete
1422 *******************************************************************************/
1423 void bta_gattc_exec_cmpl(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_OP_CMPL *p_data)
1427 utl_freebuf((void **)&p_clcb->p_q_cmd);
1429 p_clcb->status = BTA_GATT_OK;
1431 /* execute complete, callback */
1432 cb_data.exec_cmpl.conn_id = p_clcb->bta_conn_id;
1433 cb_data.exec_cmpl.status = p_data->status;
1435 ( *p_clcb->p_rcb->p_cback)(BTA_GATTC_EXEC_EVT, &cb_data);
1439 /*******************************************************************************
1441 ** Function bta_gattc_cfg_mtu_cmpl
1443 ** Description configure MTU operation complete
1447 *******************************************************************************/
1448 void bta_gattc_cfg_mtu_cmpl(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_OP_CMPL *p_data)
1452 utl_freebuf((void **)&p_clcb->p_q_cmd);
1455 if (p_data->p_cmpl && p_data->status == BTA_GATT_OK)
1456 p_clcb->p_srcb->mtu = p_data->p_cmpl->mtu;
1458 /* configure MTU complete, callback */
1459 p_clcb->status = p_data->status;
1460 cb_data.cfg_mtu.conn_id = p_clcb->bta_conn_id;
1461 cb_data.cfg_mtu.status = p_data->status;
1462 cb_data.cfg_mtu.mtu = p_clcb->p_srcb->mtu;
1464 (*p_clcb->p_rcb->p_cback) (BTA_GATTC_CFG_MTU_EVT, &cb_data);
1467 /*******************************************************************************
1469 ** Function bta_gattc_op_cmpl
1471 ** Description operation completed.
1475 *******************************************************************************/
1476 void bta_gattc_op_cmpl(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1478 UINT8 op = (UINT8)p_data->op_cmpl.op_code;
1479 UINT8 mapped_op = 0;
1481 APPL_TRACE_DEBUG("bta_gattc_op_cmpl op = %d", op);
1483 if (op == GATTC_OPTYPE_INDICATION || op == GATTC_OPTYPE_NOTIFICATION)
1485 APPL_TRACE_ERROR("unexpected operation, ignored");
1487 else if (op >= GATTC_OPTYPE_READ)
1489 if (p_clcb->p_q_cmd == NULL)
1491 APPL_TRACE_ERROR("No pending command");
1494 if (p_clcb->p_q_cmd->hdr.event != bta_gattc_opcode_to_int_evt[op - GATTC_OPTYPE_READ])
1496 mapped_op = p_clcb->p_q_cmd->hdr.event - BTA_GATTC_API_READ_EVT + GATTC_OPTYPE_READ;
1497 if ( mapped_op > GATTC_OPTYPE_INDICATION) mapped_op = 0;
1499 #if (BT_TRACE_VERBOSE == TRUE)
1500 APPL_TRACE_ERROR("expect op:(%s :0x%04x), receive unexpected operation (%s).",
1501 bta_gattc_op_code_name[mapped_op] , p_clcb->p_q_cmd->hdr.event,
1502 bta_gattc_op_code_name[op]);
1504 APPL_TRACE_ERROR("expect op:(%u :0x%04x), receive unexpected operation (%u).",
1505 mapped_op , p_clcb->p_q_cmd->hdr.event, op);
1510 /* discard responses if service change indication is received before operation completed */
1511 if (p_clcb->auto_update == BTA_GATTC_DISC_WAITING && p_clcb->p_srcb->srvc_hdl_chg)
1513 APPL_TRACE_DEBUG("Discard all responses when service change indication is received.");
1514 p_data->op_cmpl.status = GATT_ERROR;
1517 /* service handle change void the response, discard it */
1518 if (op == GATTC_OPTYPE_READ)
1519 bta_gattc_read_cmpl(p_clcb, &p_data->op_cmpl);
1521 else if (op == GATTC_OPTYPE_WRITE)
1522 bta_gattc_write_cmpl(p_clcb, &p_data->op_cmpl);
1524 else if (op == GATTC_OPTYPE_EXE_WRITE)
1525 bta_gattc_exec_cmpl(p_clcb, &p_data->op_cmpl);
1527 else if (op == GATTC_OPTYPE_CONFIG)
1528 bta_gattc_cfg_mtu_cmpl(p_clcb, &p_data->op_cmpl);
1530 if (p_clcb->auto_update == BTA_GATTC_DISC_WAITING)
1532 p_clcb->auto_update = BTA_GATTC_REQ_WAITING;
1533 bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_DISCOVER_EVT, NULL);
1537 /*******************************************************************************
1539 ** Function bta_gattc_op_cmpl
1541 ** Description operation completed.
1545 *******************************************************************************/
1546 void bta_gattc_ignore_op_cmpl(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1550 /* receive op complete when discovery is started, ignore the response,
1551 and wait for discovery finish and resent */
1552 APPL_TRACE_DEBUG("bta_gattc_ignore_op_cmpl op = %d", p_data->hdr.layer_specific);
1555 /*******************************************************************************
1557 ** Function bta_gattc_search
1559 ** Description start a search in the local server cache
1563 *******************************************************************************/
1564 void bta_gattc_search(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1566 tBTA_GATT_STATUS status = GATT_INTERNAL_ERROR;
1568 APPL_TRACE_DEBUG("bta_gattc_search conn_id=%d",p_clcb->bta_conn_id);
1569 if (p_clcb->p_srcb && p_clcb->p_srcb->p_srvc_cache)
1571 status = BTA_GATT_OK;
1572 /* search the local cache of a server device */
1573 bta_gattc_search_service(p_clcb, p_data->api_search.p_srvc_uuid);
1575 cb_data.search_cmpl.status = status;
1576 cb_data.search_cmpl.conn_id = p_clcb->bta_conn_id;
1578 /* end of search or no server cache available */
1579 ( *p_clcb->p_rcb->p_cback)(BTA_GATTC_SEARCH_CMPL_EVT, &cb_data);
1581 /*******************************************************************************
1583 ** Function bta_gattc_q_cmd
1585 ** Description enqueue a command into control block, usually because discovery
1586 ** operation is busy.
1590 *******************************************************************************/
1591 void bta_gattc_q_cmd(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1593 bta_gattc_enqueue(p_clcb, p_data);
1595 /*******************************************************************************
1597 ** Function bta_gattc_cache_open
1599 ** Description open a NV cache for loading
1603 *******************************************************************************/
1604 void bta_gattc_cache_open(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1608 bta_gattc_set_discover_st(p_clcb->p_srcb);
1610 APPL_TRACE_DEBUG("bta_gattc_cache_open conn_id=%d",p_clcb->bta_conn_id);
1611 bta_gattc_co_cache_open(p_clcb->p_srcb->server_bda, BTA_GATTC_CI_CACHE_OPEN_EVT,
1612 p_clcb->bta_conn_id, FALSE);
1614 /*******************************************************************************
1616 ** Function bta_gattc_start_load
1618 ** Description start cache loading by sending callout open cache
1622 *******************************************************************************/
1623 void bta_gattc_ci_open(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1625 APPL_TRACE_DEBUG("bta_gattc_ci_open conn_id=%d server state=%d" ,
1626 p_clcb->bta_conn_id, p_clcb->p_srcb->state);
1627 if (p_clcb->p_srcb->state == BTA_GATTC_SERV_LOAD)
1629 if (p_data->ci_open.status == BTA_GATT_OK)
1631 p_clcb->p_srcb->attr_index = 0;
1632 bta_gattc_co_cache_load(p_clcb->p_srcb->server_bda,
1633 BTA_GATTC_CI_CACHE_LOAD_EVT,
1634 p_clcb->p_srcb->attr_index,
1635 p_clcb->bta_conn_id);
1639 p_clcb->p_srcb->state = BTA_GATTC_SERV_DISC;
1640 /* cache open failure, start discovery */
1641 bta_gattc_start_discover(p_clcb, NULL);
1644 if (p_clcb->p_srcb->state == BTA_GATTC_SERV_SAVE)
1646 if (p_data->ci_open.status == BTA_GATT_OK)
1648 if (!bta_gattc_cache_save(p_clcb->p_srcb, p_clcb->bta_conn_id))
1650 p_data->ci_open.status = BTA_GATT_ERROR;
1653 if (p_data->ci_open.status != BTA_GATT_OK)
1655 p_clcb->p_srcb->attr_index = 0;
1656 bta_gattc_co_cache_close(p_clcb->p_srcb->server_bda, p_clcb->bta_conn_id);
1657 bta_gattc_reset_discover_st(p_clcb->p_srcb, p_clcb->status);
1662 /*******************************************************************************
1664 ** Function bta_gattc_ci_load
1666 ** Description cache loading received.
1670 *******************************************************************************/
1671 void bta_gattc_ci_load(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1674 APPL_TRACE_DEBUG("bta_gattc_ci_load conn_id=%d load status=%d",
1675 p_clcb->bta_conn_id, p_data->ci_load.status);
1677 if (p_data->ci_load.status == BTA_GATT_OK ||
1678 p_data->ci_load.status == BTA_GATT_MORE)
1680 if (p_data->ci_load.num_attr != 0)
1681 bta_gattc_rebuild_cache(p_clcb->p_srcb, p_data->ci_load.num_attr,
1682 p_data->ci_load.attr, p_clcb->p_srcb->attr_index);
1684 if (p_data->ci_load.status == BTA_GATT_OK)
1686 p_clcb->p_srcb->attr_index = 0;
1687 bta_gattc_reset_discover_st(p_clcb->p_srcb, BTA_GATT_OK);
1688 bta_gattc_co_cache_close(p_clcb->p_srcb->server_bda, 0);
1690 else /* load more */
1692 p_clcb->p_srcb->attr_index += p_data->ci_load.num_attr;
1694 bta_gattc_co_cache_load(p_clcb->p_srcb->server_bda,
1695 BTA_GATTC_CI_CACHE_LOAD_EVT,
1696 p_clcb->p_srcb->attr_index,
1697 p_clcb->bta_conn_id);
1702 bta_gattc_co_cache_close(p_clcb->p_srcb->server_bda, 0);
1703 p_clcb->p_srcb->state = BTA_GATTC_SERV_DISC;
1704 p_clcb->p_srcb->attr_index = 0;
1705 /* cache load failure, start discovery */
1706 bta_gattc_start_discover(p_clcb, NULL);
1709 /*******************************************************************************
1711 ** Function bta_gattc_ci_save
1713 ** Description cache loading received.
1717 *******************************************************************************/
1718 void bta_gattc_ci_save(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1722 APPL_TRACE_DEBUG("bta_gattc_ci_save conn_id=%d " ,
1723 p_clcb->bta_conn_id );
1725 if (!bta_gattc_cache_save(p_clcb->p_srcb, p_clcb->bta_conn_id))
1727 p_clcb->p_srcb->attr_index = 0;
1728 bta_gattc_co_cache_close(p_clcb->p_srcb->server_bda, 0);
1729 bta_gattc_reset_discover_st(p_clcb->p_srcb, p_clcb->status);
1732 /*******************************************************************************
1734 ** Function bta_gattc_fail
1736 ** Description report API call failure back to apps
1740 *******************************************************************************/
1741 void bta_gattc_fail(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1745 if (p_clcb->status == BTA_GATT_OK)
1747 APPL_TRACE_ERROR("operation not supported at current state [%d]", p_clcb->state);
1751 /*******************************************************************************
1753 ** Function bta_gattc_deregister_cmpl
1755 ** Description De-Register a GATT client application with BTA completed.
1759 *******************************************************************************/
1760 static void bta_gattc_deregister_cmpl(tBTA_GATTC_RCB *p_clreg)
1762 tBTA_GATTC_CB *p_cb = &bta_gattc_cb;
1763 tBTA_GATTC_IF client_if = p_clreg->client_if;
1765 tBTA_GATTC_CBACK *p_cback = p_clreg->p_cback;
1767 memset(&cb_data, 0, sizeof(tBTA_GATTC));
1769 GATT_Deregister(p_clreg->client_if);
1770 memset(p_clreg, 0, sizeof(tBTA_GATTC_RCB));
1772 cb_data.reg_oper.client_if = client_if;
1773 cb_data.reg_oper.status = BTA_GATT_OK;
1776 /* callback with de-register event */
1777 (*p_cback)(BTA_GATTC_DEREG_EVT, (tBTA_GATTC *)&cb_data);
1779 if (bta_gattc_num_reg_app() == 0 && p_cb->state == BTA_GATTC_STATE_DISABLING)
1781 p_cb->state = BTA_GATTC_STATE_DISABLED;
1784 /*******************************************************************************
1786 ** Function bta_gattc_conn_cback
1787 ** bta_gattc_cmpl_cback
1789 ** Description callback functions to GATT client stack.
1793 *******************************************************************************/
1794 static void bta_gattc_conn_cback(tGATT_IF gattc_if, BD_ADDR bda, UINT16 conn_id,
1795 BOOLEAN connected, tGATT_DISCONN_REASON reason,
1796 tBT_TRANSPORT transport)
1798 tBTA_GATTC_DATA *p_buf;
1802 APPL_TRACE_WARNING("%s() - cif=%d connected=%d conn_id=%d reason=0x%04x",
1803 __FUNCTION__, gattc_if, connected, conn_id, reason);
1807 bdcpy(bdaddr.address, bda);
1809 btif_debug_conn_state(bdaddr, BTIF_DEBUG_CONNECTED, GATT_CONN_UNKNOWN);
1811 btif_debug_conn_state(bdaddr, BTIF_DEBUG_DISCONNECTED, reason);
1813 if ((p_buf = (tBTA_GATTC_DATA *) GKI_getbuf(sizeof(tBTA_GATTC_DATA))) != NULL)
1815 memset(p_buf, 0, sizeof(tBTA_GATTC_DATA));
1817 p_buf->int_conn.hdr.event = connected ? BTA_GATTC_INT_CONN_EVT:
1818 BTA_GATTC_INT_DISCONN_EVT;
1819 p_buf->int_conn.hdr.layer_specific = conn_id;
1820 p_buf->int_conn.client_if = gattc_if;
1821 p_buf->int_conn.role = L2CA_GetBleConnRole(bda);
1822 p_buf->int_conn.reason = reason;
1823 p_buf->int_conn.transport = transport;
1824 bdcpy(p_buf->int_conn.remote_bda, bda);
1826 bta_sys_sendmsg(p_buf);
1830 /*******************************************************************************
1832 ** Function bta_gattc_enc_cmpl_cback
1834 ** Description encryption complete callback function to GATT client stack.
1838 *******************************************************************************/
1839 static void bta_gattc_enc_cmpl_cback(tGATT_IF gattc_if, BD_ADDR bda)
1841 tBTA_GATTC_DATA *p_buf;
1842 tBTA_GATTC_CLCB *p_clcb = NULL;
1844 if ((p_clcb = bta_gattc_find_clcb_by_cif(gattc_if, bda, BTA_GATT_TRANSPORT_LE)) == NULL)
1849 #if (defined BTA_HH_LE_INCLUDED && BTA_HH_LE_INCLUDED == TRUE)
1850 /* filter this event just for BTA HH LE GATT client,
1851 In the future, if we want to enable encryption complete event
1852 for all GATT clients, we can remove this code */
1853 if (!bta_hh_le_is_hh_gatt_if(gattc_if))
1859 APPL_TRACE_DEBUG("bta_gattc_enc_cmpl_cback: cif = %d", gattc_if);
1861 if ((p_buf = (tBTA_GATTC_DATA *) GKI_getbuf(sizeof(tBTA_GATTC_DATA))) != NULL)
1863 memset(p_buf, 0, sizeof(tBTA_GATTC_DATA));
1865 p_buf->enc_cmpl.hdr.event = BTA_GATTC_ENC_CMPL_EVT;
1866 p_buf->enc_cmpl.hdr.layer_specific = p_clcb->bta_conn_id;
1867 p_buf->enc_cmpl.client_if = gattc_if;
1868 bdcpy(p_buf->enc_cmpl.remote_bda, bda);
1870 bta_sys_sendmsg(p_buf);
1874 /*******************************************************************************
1876 ** Function bta_gattc_process_api_refresh
1878 ** Description process refresh API to delete cache and start a new discovery
1879 ** if currently connected.
1883 *******************************************************************************/
1884 void bta_gattc_process_api_refresh(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA * p_msg)
1886 tBTA_GATTC_SERV *p_srvc_cb = bta_gattc_find_srvr_cache(p_msg->api_conn.remote_bda);
1887 tBTA_GATTC_CLCB *p_clcb = &bta_gattc_cb.clcb[0];
1888 BOOLEAN found = FALSE;
1892 if (p_srvc_cb != NULL)
1894 /* try to find a CLCB */
1895 if (p_srvc_cb->connected && p_srvc_cb->num_clcb != 0)
1897 for (i = 0; i < BTA_GATTC_CLCB_MAX; i ++, p_clcb ++)
1899 if (p_clcb->in_use && p_clcb->p_srcb == p_srvc_cb)
1907 bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_DISCOVER_EVT, NULL);
1911 /* in all other cases, mark it and delete the cache */
1912 if (p_srvc_cb->p_srvc_cache != NULL)
1914 while (!GKI_queue_is_empty(&p_srvc_cb->cache_buffer))
1915 GKI_freebuf (GKI_dequeue (&p_srvc_cb->cache_buffer));
1917 p_srvc_cb->p_srvc_cache = NULL;
1920 /* used to reset cache in application */
1921 bta_gattc_co_cache_reset(p_msg->api_conn.remote_bda);
1924 /*******************************************************************************
1926 ** Function bta_gattc_process_srvc_chg_ind
1928 ** Description process service change indication.
1932 *******************************************************************************/
1933 BOOLEAN bta_gattc_process_srvc_chg_ind(UINT16 conn_id,
1934 tBTA_GATTC_RCB *p_clrcb,
1935 tBTA_GATTC_SERV *p_srcb,
1936 tBTA_GATTC_CLCB *p_clcb,
1937 tBTA_GATTC_NOTIFY *p_notify,
1940 tBT_UUID gattp_uuid, srvc_chg_uuid;
1941 BOOLEAN processed = FALSE;
1945 gattp_uuid.uu.uuid16 = UUID_SERVCLASS_GATT_SERVER;
1947 srvc_chg_uuid.len = 2;
1948 srvc_chg_uuid.uu.uuid16 = GATT_UUID_GATT_SRV_CHGD;
1950 if (bta_gattc_uuid_compare(&p_notify->char_id.srvc_id.id.uuid, &gattp_uuid, TRUE) &&
1951 bta_gattc_uuid_compare(&p_notify->char_id.char_id.uuid, &srvc_chg_uuid, TRUE))
1954 /* mark service handle change pending */
1955 p_srcb->srvc_hdl_chg = TRUE;
1956 /* clear up all notification/indication registration */
1957 bta_gattc_clear_notif_registration(conn_id);
1958 /* service change indication all received, do discovery update */
1959 if ( ++ p_srcb->update_count == bta_gattc_num_reg_app())
1961 /* not an opened connection; or connection busy */
1962 /* search for first available clcb and start discovery */
1963 if (p_clcb == NULL || (p_clcb && p_clcb->p_q_cmd != NULL))
1965 for (i = 0 ; i < BTA_GATTC_CLCB_MAX; i ++)
1967 if (bta_gattc_cb.clcb[i].in_use &&
1968 bta_gattc_cb.clcb[i].p_srcb == p_srcb &&
1969 bta_gattc_cb.clcb[i].p_q_cmd == NULL)
1971 p_clcb = &bta_gattc_cb.clcb[i];
1976 /* send confirmation here if this is an indication, it should always be */
1977 GATTC_SendHandleValueConfirm(conn_id, handle);
1979 /* if connection available, refresh cache by doing discovery now */
1981 bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_DISCOVER_EVT, NULL);
1983 /* notify applicationf or service change */
1984 if (p_clrcb->p_cback != NULL)
1986 (* p_clrcb->p_cback)(BTA_GATTC_SRVC_CHG_EVT, (tBTA_GATTC *)p_srcb->server_bda);
1994 /*******************************************************************************
1996 ** Function bta_gattc_proc_other_indication
1998 ** Description process all non-service change indication/notification.
2002 *******************************************************************************/
2003 void bta_gattc_proc_other_indication(tBTA_GATTC_CLCB *p_clcb, UINT8 op,
2004 tGATT_CL_COMPLETE *p_data,
2005 tBTA_GATTC_NOTIFY *p_notify)
2007 APPL_TRACE_DEBUG("bta_gattc_proc_other_indication check \
2008 p_data->att_value.handle=%d p_data->handle=%d",
2009 p_data->att_value.handle, p_data->handle);
2010 APPL_TRACE_DEBUG("is_notify", p_notify->is_notify);
2012 p_notify->is_notify = (op == GATTC_OPTYPE_INDICATION) ? FALSE : TRUE;
2013 p_notify->len = p_data->att_value.len;
2014 bdcpy(p_notify->bda, p_clcb->bda);
2015 memcpy(p_notify->value, p_data->att_value.value, p_data->att_value.len);
2016 p_notify->conn_id = p_clcb->bta_conn_id;
2018 if (p_clcb->p_rcb->p_cback)
2019 (*p_clcb->p_rcb->p_cback)(BTA_GATTC_NOTIF_EVT, (tBTA_GATTC *)p_notify);
2022 /*******************************************************************************
2024 ** Function bta_gattc_process_indicate
2026 ** Description process indication/notification.
2030 *******************************************************************************/
2031 void bta_gattc_process_indicate(UINT16 conn_id, tGATTC_OPTYPE op, tGATT_CL_COMPLETE *p_data)
2033 UINT16 handle = p_data->att_value.handle;
2034 tBTA_GATTC_CLCB *p_clcb ;
2035 tBTA_GATTC_RCB *p_clrcb = NULL;
2036 tBTA_GATTC_SERV *p_srcb = NULL;
2037 tBTA_GATTC_NOTIFY notify;
2039 tBTA_GATTC_IF gatt_if;
2040 tBTA_TRANSPORT transport;
2042 if (!GATT_GetConnectionInfor(conn_id, &gatt_if, remote_bda, &transport))
2044 APPL_TRACE_ERROR("%s indication/notif for unknown app", __func__);
2045 if (op == GATTC_OPTYPE_INDICATION)
2046 GATTC_SendHandleValueConfirm(conn_id, handle);
2050 if ((p_clrcb = bta_gattc_cl_get_regcb(gatt_if)) == NULL)
2052 APPL_TRACE_ERROR("%s indication/notif for unregistered app", __func__);
2053 if (op == GATTC_OPTYPE_INDICATION)
2054 GATTC_SendHandleValueConfirm(conn_id, handle);
2058 if ((p_srcb = bta_gattc_find_srcb(remote_bda)) == NULL)
2060 APPL_TRACE_ERROR("%s indication/notif for unknown device, ignore", __func__);
2061 if (op == GATTC_OPTYPE_INDICATION)
2062 GATTC_SendHandleValueConfirm(conn_id, handle);
2066 p_clcb = bta_gattc_find_clcb_by_conn_id(conn_id);
2068 if (bta_gattc_handle2id(p_srcb, handle,
2069 ¬ify.char_id.srvc_id,
2070 ¬ify.char_id.char_id,
2071 ¬ify.descr_type))
2073 /* if non-service change indication/notification, forward to application */
2074 if (!bta_gattc_process_srvc_chg_ind(conn_id, p_clrcb, p_srcb, p_clcb, ¬ify, handle))
2076 /* if app registered for the notification */
2077 if (bta_gattc_check_notif_registry(p_clrcb, p_srcb, ¬ify))
2079 /* connection not open yet */
2082 if ((p_clcb = bta_gattc_clcb_alloc(gatt_if, remote_bda, transport)) != NULL)
2084 p_clcb->bta_conn_id = conn_id;
2085 p_clcb->transport = transport;
2087 bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_CONN_EVT, NULL);
2091 APPL_TRACE_ERROR("No resources");
2096 bta_gattc_proc_other_indication(p_clcb, op, p_data, ¬ify);
2098 /* no one intersted and need ack? */
2099 else if (op == GATTC_OPTYPE_INDICATION)
2101 APPL_TRACE_DEBUG("%s no one interested, ack now", __func__);
2102 GATTC_SendHandleValueConfirm(conn_id, handle);
2108 APPL_TRACE_ERROR("%s Indi/Notif for Unknown handle[0x%04x], can not find in local cache.",
2110 if (op == GATTC_OPTYPE_INDICATION)
2111 GATTC_SendHandleValueConfirm(conn_id, handle);
2114 /*******************************************************************************
2116 ** Function bta_gattc_cmpl_cback
2118 ** Description client operation complete callback register with BTE GATT.
2122 *******************************************************************************/
2123 static void bta_gattc_cmpl_cback(UINT16 conn_id, tGATTC_OPTYPE op, tGATT_STATUS status,
2124 tGATT_CL_COMPLETE *p_data)
2126 tBTA_GATTC_CLCB *p_clcb ;
2127 tBTA_GATTC_OP_CMPL *p_buf;
2128 UINT16 len = sizeof(tBTA_GATTC_OP_CMPL) + sizeof(tGATT_CL_COMPLETE);
2130 APPL_TRACE_DEBUG("bta_gattc_cmpl_cback: conn_id = %d op = %d status = %d",
2131 conn_id, op, status);
2133 /* notification and indication processed right away */
2134 if (op == GATTC_OPTYPE_NOTIFICATION || op == GATTC_OPTYPE_INDICATION)
2136 bta_gattc_process_indicate(conn_id, op, p_data);
2139 /* for all other operation, not expected if w/o connection */
2140 else if ((p_clcb = bta_gattc_find_clcb_by_conn_id(conn_id)) == NULL)
2142 APPL_TRACE_ERROR("bta_gattc_cmpl_cback unknown conn_id = %d, ignore data", conn_id);
2146 /* if over BR_EDR, inform PM for mode change */
2147 if (p_clcb->transport == BTA_TRANSPORT_BR_EDR)
2149 bta_sys_busy(BTA_ID_GATTC, BTA_ALL_APP_ID, p_clcb->bda);
2150 bta_sys_idle(BTA_ID_GATTC, BTA_ALL_APP_ID, p_clcb->bda);
2153 if ((p_buf = (tBTA_GATTC_OP_CMPL *) GKI_getbuf(len)) != NULL)
2155 memset(p_buf, 0, len);
2156 p_buf->hdr.event = BTA_GATTC_OP_CMPL_EVT;
2157 p_buf->hdr.layer_specific = conn_id;
2158 p_buf->status = status;
2159 p_buf->op_code = op;
2163 p_buf->p_cmpl = (tGATT_CL_COMPLETE *)(p_buf + 1);
2164 memcpy(p_buf->p_cmpl, p_data, sizeof(tGATT_CL_COMPLETE));
2167 bta_sys_sendmsg(p_buf);
2173 /*******************************************************************************
2175 ** Function bta_gattc_cong_cback
2177 ** Description congestion callback for BTA GATT client.
2181 ********************************************************************************/
2182 static void bta_gattc_cong_cback (UINT16 conn_id, BOOLEAN congested)
2184 tBTA_GATTC_CLCB *p_clcb;
2187 if ((p_clcb = bta_gattc_find_clcb_by_conn_id(conn_id)) != NULL)
2189 if (p_clcb->p_rcb->p_cback)
2191 cb_data.congest.conn_id = conn_id;
2192 cb_data.congest.congested = congested;
2194 (*p_clcb->p_rcb->p_cback)(BTA_GATTC_CONGEST_EVT, &cb_data);
2199 #if BLE_INCLUDED == TRUE
2200 /*******************************************************************************
2202 ** Function bta_gattc_init_clcb_conn
2204 ** Description Initaite a BTA CLCB connection
2208 ********************************************************************************/
2209 void bta_gattc_init_clcb_conn(UINT8 cif, BD_ADDR remote_bda)
2211 tBTA_GATTC_CLCB *p_clcb = NULL;
2212 tBTA_GATTC_DATA gattc_data;
2215 /* should always get the connection ID */
2216 if (GATT_GetConnIdIfConnected(cif, remote_bda, &conn_id, BTA_GATT_TRANSPORT_LE) == FALSE)
2218 APPL_TRACE_ERROR("bta_gattc_init_clcb_conn ERROR: not a connected device");
2222 /* initaite a new connection here */
2223 if ((p_clcb = bta_gattc_clcb_alloc(cif, remote_bda, BTA_GATT_TRANSPORT_LE)) != NULL)
2225 gattc_data.hdr.layer_specific = p_clcb->bta_conn_id = conn_id;
2227 gattc_data.api_conn.client_if = cif;
2228 memcpy(gattc_data.api_conn.remote_bda, remote_bda, BD_ADDR_LEN);
2229 gattc_data.api_conn.is_direct = TRUE;
2231 bta_gattc_sm_execute(p_clcb, BTA_GATTC_API_OPEN_EVT, &gattc_data);
2235 APPL_TRACE_ERROR("No resources");
2238 /*******************************************************************************
2240 ** Function bta_gattc_process_listen_all
2242 ** Description process listen all, send open callback to application for all
2243 ** connected slave LE link.
2247 ********************************************************************************/
2248 void bta_gattc_process_listen_all(UINT8 cif)
2251 tBTA_GATTC_CONN *p_conn = &bta_gattc_cb.conn_track[0];
2253 for (i_conn = 0; i_conn < BTA_GATTC_CONN_MAX; i_conn++, p_conn ++)
2255 if (p_conn->in_use )
2257 if (bta_gattc_find_clcb_by_cif(cif, p_conn->remote_bda, BTA_GATT_TRANSPORT_LE) == NULL)
2259 bta_gattc_init_clcb_conn(cif, p_conn->remote_bda);
2261 /* else already connected */
2265 /*******************************************************************************
2267 ** Function bta_gattc_listen
2269 ** Description Start or stop a listen for connection
2273 ********************************************************************************/
2274 void bta_gattc_listen(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA * p_msg)
2276 tBTA_GATTC_RCB *p_clreg = bta_gattc_cl_get_regcb(p_msg->api_listen.client_if);
2280 cb_data.reg_oper.status = BTA_GATT_ERROR;
2281 cb_data.reg_oper.client_if = p_msg->api_listen.client_if;
2283 if (p_clreg == NULL)
2285 APPL_TRACE_ERROR("bta_gattc_listen failed, unknown client_if: %d",
2286 p_msg->api_listen.client_if);
2289 /* mark bg conn record */
2290 if (bta_gattc_mark_bg_conn(p_msg->api_listen.client_if,
2291 (BD_ADDR_PTR) p_msg->api_listen.remote_bda,
2292 p_msg->api_listen.start,
2295 if (!GATT_Listen(p_msg->api_listen.client_if,
2296 p_msg->api_listen.start,
2297 p_msg->api_listen.remote_bda))
2299 APPL_TRACE_ERROR("Listen failure");
2300 (*p_clreg->p_cback)(BTA_GATTC_LISTEN_EVT, &cb_data);
2304 cb_data.status = BTA_GATT_OK;
2306 (*p_clreg->p_cback)(BTA_GATTC_LISTEN_EVT, &cb_data);
2308 if (p_msg->api_listen.start)
2310 /* if listen to a specific target */
2311 if (p_msg->api_listen.remote_bda != NULL)
2314 /* if is a connected remote device */
2315 if (L2CA_GetBleConnRole(p_msg->api_listen.remote_bda) == HCI_ROLE_SLAVE &&
2316 bta_gattc_find_clcb_by_cif(p_msg->api_listen.client_if,
2317 p_msg->api_listen.remote_bda,
2318 BTA_GATT_TRANSPORT_LE) == NULL)
2321 bta_gattc_init_clcb_conn(p_msg->api_listen.client_if,
2322 p_msg->api_listen.remote_bda);
2325 /* if listen to all */
2328 LOG_DEBUG(LOG_TAG, "Listen For All now");
2329 /* go through all connected device and send
2330 callback for all connected slave connection */
2331 bta_gattc_process_listen_all(p_msg->api_listen.client_if);
2338 /*******************************************************************************
2340 ** Function bta_gattc_broadcast
2342 ** Description Start or stop broadcasting
2346 ********************************************************************************/
2347 void bta_gattc_broadcast(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA * p_msg)
2349 tBTA_GATTC_RCB *p_clreg = bta_gattc_cl_get_regcb(p_msg->api_listen.client_if);
2353 cb_data.reg_oper.client_if = p_msg->api_listen.client_if;
2354 cb_data.reg_oper.status = BTM_BleBroadcast(p_msg->api_listen.start);
2356 if (p_clreg && p_clreg->p_cback)
2357 (*p_clreg->p_cback)(BTA_GATTC_LISTEN_EVT, &cb_data);