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);
55 static void bta_gattc_deregister_cmpl(tBTA_GATTC_RCB *p_clreg);
56 static void bta_gattc_enc_cmpl_cback(tGATT_IF gattc_if, BD_ADDR bda);
57 static void bta_gattc_cong_cback (UINT16 conn_id, BOOLEAN congested);
59 static tGATT_CBACK bta_gattc_cl_cback =
63 bta_gattc_disc_res_cback,
64 bta_gattc_disc_cmpl_cback,
66 bta_gattc_enc_cmpl_cback,
70 /* opcode(tGATTC_OPTYPE) order has to be comply with internal event order */
71 static UINT16 bta_gattc_opcode_to_int_evt[] =
73 BTA_GATTC_API_READ_EVT,
74 BTA_GATTC_API_WRITE_EVT,
75 BTA_GATTC_API_EXEC_EVT,
76 BTA_GATTC_API_CFG_MTU_EVT
79 #if (BT_TRACE_VERBOSE == TRUE)
80 static const char *bta_gattc_op_code_name[] =
92 /*****************************************************************************
94 *****************************************************************************/
96 /*******************************************************************************
98 ** Function bta_gattc_enable
100 ** Description Enables GATTC module
105 *******************************************************************************/
106 static void bta_gattc_enable(tBTA_GATTC_CB *p_cb)
108 APPL_TRACE_DEBUG("bta_gattc_enable");
110 if (p_cb->state == BTA_GATTC_STATE_DISABLED)
112 /* initialize control block */
113 memset(&bta_gattc_cb, 0, sizeof(tBTA_GATTC_CB));
114 p_cb->state = BTA_GATTC_STATE_ENABLED;
118 APPL_TRACE_DEBUG("GATTC is arelady enabled");
122 /*******************************************************************************
124 ** Function bta_gattc_disable
126 ** Description Disable GATTC module by cleaning up all active connections
127 ** and deregister all application.
131 *******************************************************************************/
132 void bta_gattc_disable(tBTA_GATTC_CB *p_cb)
136 APPL_TRACE_DEBUG("bta_gattc_disable");
138 if (p_cb->state != BTA_GATTC_STATE_ENABLED)
140 APPL_TRACE_ERROR("not enabled or disable in pogress");
144 for (i = 0; i <BTA_GATTC_CL_MAX; i ++)
146 if (p_cb->cl_rcb[i].in_use)
148 p_cb->state = BTA_GATTC_STATE_DISABLING;
149 /* don't deregister HH GATT IF */
150 /* HH GATT IF will be deregistered by bta_hh_le_deregister when disable HH */
151 #if (defined BTA_HH_LE_INCLUDED && BTA_HH_LE_INCLUDED == TRUE)
152 if (!bta_hh_le_is_hh_gatt_if(p_cb->cl_rcb[i].client_if)) {
154 bta_gattc_deregister(p_cb, &p_cb->cl_rcb[i]);
155 #if (defined BTA_HH_LE_INCLUDED && BTA_HH_LE_INCLUDED == TRUE)
161 /* no registered apps, indicate disable completed */
162 if (p_cb->state != BTA_GATTC_STATE_DISABLING)
164 p_cb->state = BTA_GATTC_STATE_DISABLED;
165 memset(p_cb, 0, sizeof(tBTA_GATTC_CB));
169 /*******************************************************************************
171 ** Function bta_gattc_register
173 ** Description Register a GATT client application with BTA.
177 *******************************************************************************/
178 void bta_gattc_register(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA *p_data)
182 tBT_UUID *p_app_uuid = &p_data->api_reg.app_uuid;
183 tBTA_GATTC_INT_START_IF *p_buf;
184 tBTA_GATT_STATUS status = BTA_GATT_NO_RESOURCES;
186 APPL_TRACE_DEBUG("bta_gattc_register state %d",p_cb->state);
187 memset(&cb_data, 0, sizeof(cb_data));
188 cb_data.reg_oper.status = BTA_GATT_NO_RESOURCES;
190 /* check if GATTC module is already enabled . Else enable */
191 if (p_cb->state == BTA_GATTC_STATE_DISABLED)
193 bta_gattc_enable (p_cb);
195 /* todo need to check duplicate uuid */
196 for (i = 0; i < BTA_GATTC_CL_MAX; i ++)
198 if (!p_cb->cl_rcb[i].in_use)
200 if ((p_app_uuid == NULL) || (p_cb->cl_rcb[i].client_if = GATT_Register(p_app_uuid, &bta_gattc_cl_cback)) == 0)
202 APPL_TRACE_ERROR("Register with GATT stack failed.");
203 status = BTA_GATT_ERROR;
207 p_cb->cl_rcb[i].in_use = TRUE;
208 p_cb->cl_rcb[i].p_cback = p_data->api_reg.p_cback;
209 memcpy(&p_cb->cl_rcb[i].app_uuid, p_app_uuid, sizeof(tBT_UUID));
211 /* BTA use the same client interface as BTE GATT statck */
212 cb_data.reg_oper.client_if = p_cb->cl_rcb[i].client_if;
214 if ((p_buf = (tBTA_GATTC_INT_START_IF *) GKI_getbuf(sizeof(tBTA_GATTC_INT_START_IF))) != NULL)
216 p_buf->hdr.event = BTA_GATTC_INT_START_IF_EVT;
217 p_buf->client_if = p_cb->cl_rcb[i].client_if;
219 bta_sys_sendmsg(p_buf);
220 status = BTA_GATT_OK;
224 GATT_Deregister(p_cb->cl_rcb[i].client_if);
226 status = BTA_GATT_NO_RESOURCES;
227 memset( &p_cb->cl_rcb[i], 0 , sizeof(tBTA_GATTC_RCB));
234 /* callback with register event */
235 if (p_data->api_reg.p_cback)
237 if (p_app_uuid != NULL)
238 memcpy(&(cb_data.reg_oper.app_uuid),p_app_uuid,sizeof(tBT_UUID));
240 cb_data.reg_oper.status = status;
241 (*p_data->api_reg.p_cback)(BTA_GATTC_REG_EVT, (tBTA_GATTC *)&cb_data);
244 /*******************************************************************************
246 ** Function bta_gattc_start_if
248 ** Description start an application interface.
252 *******************************************************************************/
253 void bta_gattc_start_if(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA *p_msg)
257 if (bta_gattc_cl_get_regcb(p_msg->int_start_if.client_if) !=NULL )
259 GATT_StartIf(p_msg->int_start_if.client_if);
263 APPL_TRACE_ERROR("Unable to start app.: Unknown interface =%d",p_msg->int_start_if.client_if );
266 /*******************************************************************************
268 ** Function bta_gattc_deregister
270 ** Description De-Register a GATT client application with BTA.
274 *******************************************************************************/
275 void bta_gattc_deregister(tBTA_GATTC_CB *p_cb, tBTA_GATTC_RCB *p_clreg)
282 /* remove bg connection associated with this rcb */
283 for (i = 0; i < BTA_GATTC_KNOWN_SR_MAX; i ++)
285 if (p_cb->bg_track[i].in_use)
287 if (p_cb->bg_track[i].cif_mask & (1 <<(p_clreg->client_if - 1)))
289 bta_gattc_mark_bg_conn(p_clreg->client_if, p_cb->bg_track[i].remote_bda, FALSE, FALSE);
290 GATT_CancelConnect(p_clreg->client_if, p_cb->bg_track[i].remote_bda, FALSE);
292 if (p_cb->bg_track[i].cif_adv_mask & (1 <<(p_clreg->client_if - 1)))
294 bta_gattc_mark_bg_conn(p_clreg->client_if, p_cb->bg_track[i].remote_bda, FALSE, TRUE);
299 if (p_clreg->num_clcb > 0)
301 /* close all CLCB related to this app */
302 for (i= 0; i < BTA_GATTC_CLCB_MAX; i ++)
304 if (p_cb->clcb[i].in_use && (p_cb->clcb[i].p_rcb == p_clreg))
306 p_clreg->dereg_pending = TRUE;
308 buf.event = BTA_GATTC_API_CLOSE_EVT;
309 buf.layer_specific = p_cb->clcb[i].bta_conn_id;
310 bta_gattc_close(&p_cb->clcb[i], (tBTA_GATTC_DATA *)&buf) ;
315 bta_gattc_deregister_cmpl(p_clreg);
319 APPL_TRACE_ERROR("bta_gattc_deregister Deregister Failedm unknown client cif");
322 /*******************************************************************************
324 ** Function bta_gattc_process_api_open
326 ** Description process connect API request.
330 *******************************************************************************/
331 void bta_gattc_process_api_open (tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA * p_msg)
333 UINT16 event = ((BT_HDR *)p_msg)->event;
334 tBTA_GATTC_CLCB *p_clcb = NULL;
335 tBTA_GATTC_RCB *p_clreg = bta_gattc_cl_get_regcb(p_msg->api_conn.client_if);
340 if (p_msg->api_conn.is_direct)
342 if ((p_clcb = bta_gattc_find_alloc_clcb(p_msg->api_conn.client_if,
343 p_msg->api_conn.remote_bda,
344 p_msg->api_conn.transport)) != NULL)
346 bta_gattc_sm_execute(p_clcb, event, p_msg);
350 APPL_TRACE_ERROR("No resources to open a new connection.");
352 bta_gattc_send_open_cback(p_clreg,
353 BTA_GATT_NO_RESOURCES,
354 p_msg->api_conn.remote_bda,
355 BTA_GATT_INVALID_CONN_ID,
356 p_msg->api_conn.transport, 0);
361 bta_gattc_init_bk_conn(&p_msg->api_conn, p_clreg);
366 APPL_TRACE_ERROR("bta_gattc_process_api_open Failed, unknown client_if: %d",
367 p_msg->api_conn.client_if);
370 /*******************************************************************************
372 ** Function bta_gattc_process_api_open_cancel
374 ** Description process connect API request.
378 *******************************************************************************/
379 void bta_gattc_process_api_open_cancel (tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA * p_msg)
381 UINT16 event = ((BT_HDR *)p_msg)->event;
382 tBTA_GATTC_CLCB *p_clcb = NULL;
383 tBTA_GATTC_RCB *p_clreg;
387 if (p_msg->api_cancel_conn.is_direct)
389 if ((p_clcb = bta_gattc_find_clcb_by_cif(p_msg->api_cancel_conn.client_if,
390 p_msg->api_cancel_conn.remote_bda,
391 BTA_GATT_TRANSPORT_LE)) != NULL)
393 bta_gattc_sm_execute(p_clcb, event, p_msg);
397 APPL_TRACE_ERROR("No such connection need to be cancelled");
399 p_clreg = bta_gattc_cl_get_regcb(p_msg->api_cancel_conn.client_if);
401 if (p_clreg && p_clreg->p_cback)
403 cb_data.status = BTA_GATT_ERROR;
404 (*p_clreg->p_cback)(BTA_GATTC_CANCEL_OPEN_EVT, &cb_data);
410 bta_gattc_cancel_bk_conn(&p_msg->api_cancel_conn);
415 /*******************************************************************************
417 ** Function bta_gattc_process_enc_cmpl
419 ** Description process encryption complete message.
423 *******************************************************************************/
424 void bta_gattc_process_enc_cmpl(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA *p_msg)
426 tBTA_GATTC_RCB *p_clreg;
430 p_clreg = bta_gattc_cl_get_regcb(p_msg->enc_cmpl.client_if);
432 if (p_clreg && p_clreg->p_cback)
434 memset(&cb_data, 0, sizeof(tBTA_GATTC));
436 cb_data.enc_cmpl.client_if = p_msg->enc_cmpl.client_if;
437 bdcpy(cb_data.enc_cmpl.remote_bda, p_msg->enc_cmpl.remote_bda);
439 (*p_clreg->p_cback)(BTA_GATTC_ENC_CMPL_CB_EVT, &cb_data);
443 /*******************************************************************************
445 ** Function bta_gattc_cancel_open_error
451 *******************************************************************************/
452 void bta_gattc_cancel_open_error(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
457 cb_data.status=BTA_GATT_ERROR;
459 if ( p_clcb && p_clcb->p_rcb && p_clcb->p_rcb->p_cback )
460 (*p_clcb->p_rcb->p_cback)(BTA_GATTC_CANCEL_OPEN_EVT, &cb_data);
463 /*******************************************************************************
465 ** Function bta_gattc_open_error
471 *******************************************************************************/
472 void bta_gattc_open_error(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
476 APPL_TRACE_ERROR("Connection already opened. wrong state");
478 bta_gattc_send_open_cback(p_clcb->p_rcb,
485 /*******************************************************************************
487 ** Function bta_gattc_open_fail
493 *******************************************************************************/
494 void bta_gattc_open_fail(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
498 bta_gattc_send_open_cback(p_clcb->p_rcb,
504 /* open failure, remove clcb */
505 bta_gattc_clcb_dealloc(p_clcb);
508 /*******************************************************************************
510 ** Function bta_gattc_open
512 ** Description Process API connection function.
516 *******************************************************************************/
517 void bta_gattc_open(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
519 tBTA_GATTC_DATA gattc_data;
521 /* open/hold a connection */
522 if (!GATT_Connect(p_clcb->p_rcb->client_if, p_data->api_conn.remote_bda,
523 TRUE, p_data->api_conn.transport))
525 APPL_TRACE_ERROR("Connection open failure");
527 bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_OPEN_FAIL_EVT, p_data);
531 /* a connected remote device */
532 if (GATT_GetConnIdIfConnected(p_clcb->p_rcb->client_if,
533 p_data->api_conn.remote_bda,
534 &p_clcb->bta_conn_id,
535 p_data->api_conn.transport))
537 gattc_data.int_conn.hdr.layer_specific = p_clcb->bta_conn_id;
539 bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_CONN_EVT, &gattc_data);
541 /* else wait for the callback event */
544 /*******************************************************************************
546 ** Function bta_gattc_init_bk_conn
548 ** Description Process API Open for a background connection
552 *******************************************************************************/
553 void bta_gattc_init_bk_conn(tBTA_GATTC_API_OPEN *p_data, tBTA_GATTC_RCB *p_clreg)
555 tBTA_GATT_STATUS status = BTA_GATT_NO_RESOURCES;
557 tBTA_GATTC_CLCB *p_clcb;
558 tBTA_GATTC_DATA gattc_data;
560 if (bta_gattc_mark_bg_conn(p_data->client_if, p_data->remote_bda, TRUE, FALSE))
562 /* always call open to hold a connection */
563 if (!GATT_Connect(p_data->client_if, p_data->remote_bda, FALSE, p_data->transport))
565 uint8_t *bda = (uint8_t *)p_data->remote_bda;
566 status = BTA_GATT_ERROR;
567 APPL_TRACE_ERROR("%s unable to connect to remote bd_addr:%02x:%02x:%02x:%02x:%02x:%02x",
568 __func__, bda[0], bda[1], bda[2], bda[3], bda[4], bda[5]);
573 status = BTA_GATT_OK;
575 /* if is a connected remote device */
576 if (GATT_GetConnIdIfConnected(p_data->client_if,
581 if ((p_clcb = bta_gattc_find_alloc_clcb(p_data->client_if, p_data->remote_bda,
582 BTA_GATT_TRANSPORT_LE)) != NULL)
584 gattc_data.hdr.layer_specific = p_clcb->bta_conn_id = conn_id;
586 /* open connection */
587 bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_CONN_EVT, &gattc_data);
588 status = BTA_GATT_OK;
594 /* open failure, report OPEN_EVT */
595 if (status != BTA_GATT_OK)
597 bta_gattc_send_open_cback(p_clreg, status, p_data->remote_bda,
598 BTA_GATT_INVALID_CONN_ID, BTA_GATT_TRANSPORT_LE, 0);
601 /*******************************************************************************
603 ** Function bta_gattc_cancel_bk_conn
605 ** Description Process API Cancel Open for a background connection
609 *******************************************************************************/
610 void bta_gattc_cancel_bk_conn(tBTA_GATTC_API_CANCEL_OPEN *p_data)
612 tBTA_GATTC_RCB *p_clreg;
614 cb_data.status = BTA_GATT_ERROR;
616 /* remove the device from the bg connection mask */
617 if (bta_gattc_mark_bg_conn(p_data->client_if, p_data->remote_bda, FALSE, FALSE))
619 if (GATT_CancelConnect(p_data->client_if, p_data->remote_bda, FALSE))
621 cb_data.status = BTA_GATT_OK;
625 APPL_TRACE_ERROR("bta_gattc_cancel_bk_conn failed");
628 p_clreg = bta_gattc_cl_get_regcb(p_data->client_if);
630 if (p_clreg && p_clreg->p_cback)
632 (*p_clreg->p_cback)(BTA_GATTC_CANCEL_OPEN_EVT, &cb_data);
636 /*******************************************************************************
638 ** Function bta_gattc_int_cancel_open_ok
644 *******************************************************************************/
645 void bta_gattc_cancel_open_ok(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
650 if ( p_clcb->p_rcb->p_cback )
652 cb_data.status = BTA_GATT_OK;
653 (*p_clcb->p_rcb->p_cback)(BTA_GATTC_CANCEL_OPEN_EVT, &cb_data);
656 bta_gattc_clcb_dealloc(p_clcb);
658 /*******************************************************************************
660 ** Function bta_gattc_cancel_open
666 *******************************************************************************/
667 void bta_gattc_cancel_open(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
671 if (GATT_CancelConnect(p_clcb->p_rcb->client_if, p_data->api_cancel_conn.remote_bda, TRUE))
673 bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_CANCEL_OPEN_OK_EVT, p_data);
677 if ( p_clcb->p_rcb->p_cback )
679 cb_data.status = BTA_GATT_ERROR;
680 (*p_clcb->p_rcb->p_cback)(BTA_GATTC_CANCEL_OPEN_EVT, &cb_data);
684 /*******************************************************************************
686 ** Function bta_gattc_conn
688 ** Description receive connection callback from stack
692 *******************************************************************************/
693 void bta_gattc_conn(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
695 tBTA_GATTC_IF gatt_if;
696 APPL_TRACE_DEBUG("bta_gattc_conn server cache state=%d",p_clcb->p_srcb->state);
700 APPL_TRACE_DEBUG("bta_gattc_conn conn_id=%d",p_data->hdr.layer_specific);
701 p_clcb->bta_conn_id = p_data->int_conn.hdr.layer_specific;
703 GATT_GetConnectionInfor(p_data->hdr.layer_specific,
704 &gatt_if, p_clcb->bda, &p_clcb->transport);
707 p_clcb->p_srcb->connected = TRUE;
709 if (p_clcb->p_srcb->mtu == 0)
710 p_clcb->p_srcb->mtu = GATT_DEF_BLE_MTU_SIZE;
712 /* start database cache if needed */
713 if (p_clcb->p_srcb->p_srvc_cache == NULL ||
714 p_clcb->p_srcb->state != BTA_GATTC_SERV_IDLE)
716 if (p_clcb->p_srcb->state == BTA_GATTC_SERV_IDLE)
718 p_clcb->p_srcb->state = BTA_GATTC_SERV_LOAD;
719 bta_gattc_sm_execute(p_clcb, BTA_GATTC_START_CACHE_EVT, NULL);
721 else /* cache is building */
722 p_clcb->state = BTA_GATTC_DISCOVER_ST;
727 /* a pending service handle change indication */
728 if (p_clcb->p_srcb->srvc_hdl_chg)
730 p_clcb->p_srcb->srvc_hdl_chg = FALSE;
731 /* start discovery */
732 bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_DISCOVER_EVT, NULL);
738 /* there is no RM for GATT */
739 if (p_clcb->transport == BTA_TRANSPORT_BR_EDR)
740 bta_sys_conn_open(BTA_ID_GATTC, BTA_ALL_APP_ID, p_clcb->bda);
742 bta_gattc_send_open_cback(p_clcb->p_rcb,
747 p_clcb->p_srcb->mtu);
750 /*******************************************************************************
752 ** Function bta_gattc_close_fail
754 ** Description close a connection.
758 *******************************************************************************/
759 void bta_gattc_close_fail(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
763 if ( p_clcb->p_rcb->p_cback )
765 memset(&cb_data, 0, sizeof(tBTA_GATTC));
766 cb_data.close.client_if = p_clcb->p_rcb->client_if;
767 cb_data.close.conn_id = p_data->hdr.layer_specific;
768 bdcpy(cb_data.close.remote_bda, p_clcb->bda);
769 cb_data.close.status = BTA_GATT_ERROR;
770 cb_data.close.reason = BTA_GATT_CONN_NONE;
772 (*p_clcb->p_rcb->p_cback)(BTA_GATTC_CLOSE_EVT, &cb_data);
775 /*******************************************************************************
777 ** Function bta_gattc_api_close
779 ** Description close a GATTC connection.
783 *******************************************************************************/
784 void bta_gattc_close(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
786 tBTA_GATTC_CBACK *p_cback = p_clcb->p_rcb->p_cback;
787 tBTA_GATTC_RCB *p_clreg = p_clcb->p_rcb;
790 APPL_TRACE_DEBUG("bta_gattc_close conn_id=%d",p_clcb->bta_conn_id);
792 cb_data.close.client_if = p_clcb->p_rcb->client_if;
793 cb_data.close.conn_id = p_clcb->bta_conn_id;
794 cb_data.close.reason = p_clcb->reason;
795 cb_data.close.status = p_clcb->status;
796 bdcpy(cb_data.close.remote_bda, p_clcb->bda);
798 if (p_clcb->transport == BTA_TRANSPORT_BR_EDR)
799 bta_sys_conn_close( BTA_ID_GATTC ,BTA_ALL_APP_ID, p_clcb->bda);
801 bta_gattc_clcb_dealloc(p_clcb);
803 if (p_data->hdr.event == BTA_GATTC_API_CLOSE_EVT)
805 cb_data.close.status = GATT_Disconnect(p_data->hdr.layer_specific);
807 else if (p_data->hdr.event == BTA_GATTC_INT_DISCONN_EVT)
809 cb_data.close.status = p_data->int_conn.reason;
810 cb_data.close.reason = p_data->int_conn.reason;
814 (* p_cback)(BTA_GATTC_CLOSE_EVT, (tBTA_GATTC *)&cb_data);
816 if (p_clreg->num_clcb == 0 && p_clreg->dereg_pending)
818 bta_gattc_deregister_cmpl(p_clreg);
821 /*******************************************************************************
823 ** Function bta_gattc_reset_discover_st
825 ** Description when a SRCB finished discovery, tell all related clcb.
829 *******************************************************************************/
830 void bta_gattc_reset_discover_st(tBTA_GATTC_SERV *p_srcb, tBTA_GATT_STATUS status)
832 tBTA_GATTC_CB *p_cb = &bta_gattc_cb;
835 for (i = 0; i < BTA_GATTC_CLCB_MAX; i ++)
837 if (p_cb->clcb[i].p_srcb == p_srcb)
839 p_cb->clcb[i].status = status;
840 bta_gattc_sm_execute(&p_cb->clcb[i], BTA_GATTC_DISCOVER_CMPL_EVT, NULL);
844 /*******************************************************************************
846 ** Function bta_gattc_disc_close
848 ** Description close a GATTC connection while in discovery state.
852 *******************************************************************************/
853 void bta_gattc_disc_close(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
855 APPL_TRACE_DEBUG("%s: Discovery cancel conn_id=%d", __func__,
856 p_clcb->bta_conn_id);
858 if (p_clcb->disc_active)
859 bta_gattc_reset_discover_st(p_clcb->p_srcb, BTA_GATT_ERROR);
861 p_clcb->state = BTA_GATTC_CONN_ST;
863 // This function only gets called as the result of a BTA_GATTC_API_CLOSE_EVT
864 // while in the BTA_GATTC_DISCOVER_ST state. Once the state changes, the
865 // connection itself still needs to be closed to resolve the original event.
866 if (p_clcb->state == BTA_GATTC_CONN_ST)
868 APPL_TRACE_DEBUG("State is back to BTA_GATTC_CONN_ST. "
869 "Trigger connection close");
870 bta_gattc_close(p_clcb, p_data);
873 /*******************************************************************************
875 ** Function bta_gattc_set_discover_st
877 ** Description when a SRCB start discovery, tell all related clcb and set
882 *******************************************************************************/
883 void bta_gattc_set_discover_st(tBTA_GATTC_SERV *p_srcb)
885 tBTA_GATTC_CB *p_cb = &bta_gattc_cb;
888 #if BLE_INCLUDED == TRUE
889 L2CA_EnableUpdateBleConnParams(p_srcb->server_bda, FALSE);
891 for (i = 0; i < BTA_GATTC_CLCB_MAX; i ++)
893 if (p_cb->clcb[i].p_srcb == p_srcb)
895 p_cb->clcb[i].status = BTA_GATT_OK;
896 p_cb->clcb[i].state = BTA_GATTC_DISCOVER_ST;
900 /*******************************************************************************
902 ** Function bta_gattc_restart_discover
904 ** Description process service change in discovery state, mark up the auto
905 ** update flag and set status to be discovery cancel for current
910 *******************************************************************************/
911 void bta_gattc_restart_discover(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
915 p_clcb->status = BTA_GATT_CANCEL;
916 p_clcb->auto_update = BTA_GATTC_DISC_WAITING;
919 /*******************************************************************************
921 ** Function bta_gattc_cfg_mtu
923 ** Description Configure MTU size on the GATT connection.
927 *******************************************************************************/
928 void bta_gattc_cfg_mtu(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
930 tBTA_GATTC_OP_CMPL op_cmpl;
931 tBTA_GATT_STATUS status;
933 if (bta_gattc_enqueue(p_clcb, p_data))
935 status = GATTC_ConfigureMTU (p_clcb->bta_conn_id, p_data->api_mtu.mtu);
937 /* if failed, return callback here */
938 if (status != GATT_SUCCESS && status != GATT_CMD_STARTED)
940 memset(&op_cmpl, 0, sizeof(tBTA_GATTC_OP_CMPL));
942 op_cmpl.status = status;
943 op_cmpl.op_code = GATTC_OPTYPE_CONFIG;
944 op_cmpl.p_cmpl = NULL;
946 bta_gattc_sm_execute(p_clcb, BTA_GATTC_OP_CMPL_EVT, (tBTA_GATTC_DATA *)&op_cmpl);
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_GATTC_OP_CMPL op_cmpl;
1090 memset (&read_param, 0 ,sizeof(tGATT_READ_PARAM));
1091 memset (&op_cmpl, 0 ,sizeof(tBTA_GATTC_OP_CMPL));
1093 if (bta_gattc_enqueue(p_clcb, p_data))
1095 if ((handle = bta_gattc_id2handle(p_clcb->p_srcb,
1096 &p_data->api_read.srvc_id,
1097 &p_data->api_read.char_id,
1098 p_data->api_read.p_descr_type)) == 0)
1100 op_cmpl.status = BTA_GATT_ERROR;
1104 read_param.by_handle.handle = handle;
1105 read_param.by_handle.auth_req = p_data->api_read.auth_req;
1107 op_cmpl.status = GATTC_Read(p_clcb->bta_conn_id, GATT_READ_BY_HANDLE, &read_param);
1111 if (op_cmpl.status != BTA_GATT_OK)
1113 op_cmpl.op_code = GATTC_OPTYPE_READ;
1114 op_cmpl.p_cmpl = NULL;
1116 bta_gattc_sm_execute(p_clcb, BTA_GATTC_OP_CMPL_EVT, (tBTA_GATTC_DATA *)&op_cmpl);
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_OP_CMPL op_cmpl;
1134 tBTA_GATTC_ATTR_ID *p_id;
1136 if (bta_gattc_enqueue(p_clcb, p_data))
1138 memset(&read_param, 0, sizeof(tGATT_READ_PARAM));
1140 p_id = p_data->api_read_multi.p_id_list;
1142 for (i = 0; i < p_data->api_read_multi.num_attr && p_id; i ++, p_id ++)
1146 if (p_id->id_type == BTA_GATT_TYPE_CHAR)
1148 handle = bta_gattc_id2handle(p_clcb->p_srcb,
1149 &p_id->id_value.char_id.srvc_id,
1150 &p_id->id_value.char_id.char_id,
1153 else if (p_id->id_type == BTA_GATT_TYPE_CHAR_DESCR)
1155 handle = bta_gattc_id2handle(p_clcb->p_srcb,
1156 &p_id->id_value.char_descr_id.char_id.srvc_id,
1157 &p_id->id_value.char_descr_id.char_id.char_id,
1158 &p_id->id_value.char_descr_id.descr_id);
1162 APPL_TRACE_ERROR("invalud ID type: %d", p_id->id_type);
1167 status = BTA_GATT_ERROR;
1171 if (status == BTA_GATT_OK)
1173 read_param.read_multiple.num_handles = p_data->api_read_multi.num_attr;
1174 read_param.read_multiple.auth_req = p_data->api_read_multi.auth_req;
1176 status = GATTC_Read(p_clcb->bta_conn_id, GATT_READ_MULTIPLE, &read_param);
1180 if (status != BTA_GATT_OK)
1182 memset(&op_cmpl, 0, sizeof(tBTA_GATTC_OP_CMPL));
1184 op_cmpl.status = status;
1185 op_cmpl.op_code = GATTC_OPTYPE_READ;
1186 op_cmpl.p_cmpl = NULL;
1188 bta_gattc_sm_execute(p_clcb, BTA_GATTC_OP_CMPL_EVT, (tBTA_GATTC_DATA *)&op_cmpl);
1192 /*******************************************************************************
1194 ** Function bta_gattc_write
1196 ** Description Write an attribute
1200 *******************************************************************************/
1201 void bta_gattc_write(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1204 tGATT_VALUE attr = {0};
1205 tBTA_GATTC_OP_CMPL op_cmpl;
1206 tBTA_GATT_STATUS status = BTA_GATT_OK;
1208 if (bta_gattc_enqueue(p_clcb, p_data))
1210 if ((handle = bta_gattc_id2handle(p_clcb->p_srcb,
1211 &p_data->api_write.srvc_id,
1212 &p_data->api_write.char_id,
1213 p_data->api_write.p_descr_type)) == 0)
1215 status = BTA_GATT_ERROR;
1219 attr.handle= handle;
1220 attr.offset = p_data->api_write.offset;
1221 attr.len = p_data->api_write.len;
1222 attr.auth_req = p_data->api_write.auth_req;
1224 if (p_data->api_write.p_value)
1225 memcpy(attr.value, p_data->api_write.p_value, p_data->api_write.len);
1227 status = GATTC_Write(p_clcb->bta_conn_id, p_data->api_write.write_type, &attr);
1231 if (status != BTA_GATT_OK)
1233 memset(&op_cmpl, 0, sizeof(tBTA_GATTC_OP_CMPL));
1235 op_cmpl.status = status;
1236 op_cmpl.op_code = GATTC_OPTYPE_WRITE;
1237 op_cmpl.p_cmpl = NULL;
1239 bta_gattc_sm_execute(p_clcb, BTA_GATTC_OP_CMPL_EVT, (tBTA_GATTC_DATA *)&op_cmpl);
1243 /*******************************************************************************
1245 ** Function bta_gattc_execute
1247 ** Description send execute write
1250 *********************************************************************************/
1251 void bta_gattc_execute(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1253 tBTA_GATTC_OP_CMPL op_cmpl;
1254 tBTA_GATT_STATUS status;
1256 if (bta_gattc_enqueue(p_clcb, p_data))
1258 status = GATTC_ExecuteWrite(p_clcb->bta_conn_id, p_data->api_exec.is_execute);
1260 if (status != BTA_GATT_OK)
1262 memset(&op_cmpl, 0, sizeof(tBTA_GATTC_OP_CMPL));
1264 op_cmpl.status = status;
1265 op_cmpl.op_code = GATTC_OPTYPE_EXE_WRITE;
1266 op_cmpl.p_cmpl = NULL;
1268 bta_gattc_sm_execute(p_clcb, BTA_GATTC_OP_CMPL_EVT, (tBTA_GATTC_DATA *)&op_cmpl);
1272 /*******************************************************************************
1274 ** Function bta_gattc_confirm
1276 ** Description send handle value confirmation
1280 *******************************************************************************/
1281 void bta_gattc_confirm(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1285 if ((handle = bta_gattc_id2handle(p_clcb->p_srcb,
1286 &p_data->api_confirm.srvc_id,
1287 &p_data->api_confirm.char_id,
1290 APPL_TRACE_ERROR("Can not map service/char ID into valid handle");
1294 if (GATTC_SendHandleValueConfirm(p_data->api_confirm.hdr.layer_specific, handle)
1297 APPL_TRACE_ERROR("bta_gattc_confirm to handle [0x%04x] failed", handle);
1301 /* if over BR_EDR, inform PM for mode change */
1302 if (p_clcb->transport == BTA_TRANSPORT_BR_EDR)
1304 bta_sys_busy(BTA_ID_GATTC, BTA_ALL_APP_ID, p_clcb->bda);
1305 bta_sys_idle(BTA_ID_GATTC, BTA_ALL_APP_ID, p_clcb->bda);
1310 /*******************************************************************************
1312 ** Function bta_gattc_read_cmpl
1314 ** Description read complete
1318 *******************************************************************************/
1319 void bta_gattc_read_cmpl(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_OP_CMPL *p_data)
1323 tBTA_GATT_READ_VAL read_value;
1325 memset(&cb_data, 0, sizeof(tBTA_GATTC));
1326 memset(&read_value, 0, sizeof(tBTA_GATT_READ_VAL));
1328 cb_data.read.status = p_data->status;
1330 if (p_data->p_cmpl != NULL && p_data->status == BTA_GATT_OK)
1332 if (bta_gattc_handle2id(p_clcb->p_srcb,
1333 p_data->p_cmpl->att_value.handle,
1334 &cb_data.read.srvc_id,
1335 &cb_data.read.char_id,
1336 &cb_data.read.descr_type) == FALSE)
1338 cb_data.read.status = BTA_GATT_INTERNAL_ERROR;
1339 APPL_TRACE_ERROR("can not map to GATT ID. handle = 0x%04x",
1340 p_data->p_cmpl->att_value.handle);
1344 cb_data.read.status = bta_gattc_pack_read_cb_data(p_clcb->p_srcb,
1345 &cb_data.read.descr_type.uuid,
1346 &p_data->p_cmpl->att_value,
1348 cb_data.read.p_value = &read_value;
1353 cb_data.read.srvc_id = p_clcb->p_q_cmd->api_read.srvc_id;
1354 cb_data.read.char_id = p_clcb->p_q_cmd->api_read.char_id;
1355 if (p_clcb->p_q_cmd->api_read.p_descr_type)
1356 memcpy(&cb_data.read.descr_type, p_clcb->p_q_cmd->api_read.p_descr_type,
1357 sizeof(tBTA_GATT_ID));
1360 event = (p_clcb->p_q_cmd->api_read.p_descr_type == NULL) ?
1361 BTA_GATTC_READ_CHAR_EVT: BTA_GATTC_READ_DESCR_EVT;
1362 cb_data.read.conn_id = p_clcb->bta_conn_id;
1364 utl_freebuf((void **)&p_clcb->p_q_cmd);
1365 /* read complete, callback */
1366 ( *p_clcb->p_rcb->p_cback)(event, (tBTA_GATTC *)&cb_data);
1369 /*******************************************************************************
1371 ** Function bta_gattc_write_cmpl
1373 ** Description write complete
1377 *******************************************************************************/
1378 void bta_gattc_write_cmpl(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_OP_CMPL *p_data)
1380 tBTA_GATTC cb_data = {0};
1383 memset(&cb_data, 0, sizeof(tBTA_GATTC));
1385 cb_data.write.status = p_data->status;
1387 if (p_data->p_cmpl != NULL)
1389 bta_gattc_handle2id(p_clcb->p_srcb, p_data->p_cmpl->att_value.handle,
1390 &cb_data.write.srvc_id, &cb_data.write.char_id,
1391 &cb_data.write.descr_type);
1395 memcpy(&cb_data.write.srvc_id, &p_clcb->p_q_cmd->api_write.srvc_id,
1396 sizeof(tBTA_GATT_SRVC_ID));
1397 memcpy(&cb_data.write.char_id, &p_clcb->p_q_cmd->api_write.char_id,
1398 sizeof(tBTA_GATT_ID));
1399 if (p_clcb->p_q_cmd->api_write.p_descr_type)
1400 memcpy(&cb_data.write.descr_type, p_clcb->p_q_cmd->api_write.p_descr_type,
1401 sizeof(tBTA_GATT_ID));
1404 if (p_clcb->p_q_cmd->api_write.hdr.event == BTA_GATTC_API_WRITE_EVT &&
1405 p_clcb->p_q_cmd->api_write.write_type == BTA_GATTC_WRITE_PREPARE)
1407 event = BTA_GATTC_PREP_WRITE_EVT;
1409 else if (p_clcb->p_q_cmd->api_write.p_descr_type == NULL)
1411 event = BTA_GATTC_WRITE_CHAR_EVT;
1414 event = BTA_GATTC_WRITE_DESCR_EVT;
1416 utl_freebuf((void **)&p_clcb->p_q_cmd);
1417 cb_data.write.conn_id = p_clcb->bta_conn_id;
1418 /* write complete, callback */
1419 ( *p_clcb->p_rcb->p_cback)(event, (tBTA_GATTC *)&cb_data);
1422 /*******************************************************************************
1424 ** Function bta_gattc_exec_cmpl
1426 ** Description execute write complete
1430 *******************************************************************************/
1431 void bta_gattc_exec_cmpl(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_OP_CMPL *p_data)
1435 utl_freebuf((void **)&p_clcb->p_q_cmd);
1437 p_clcb->status = BTA_GATT_OK;
1439 /* execute complete, callback */
1440 cb_data.exec_cmpl.conn_id = p_clcb->bta_conn_id;
1441 cb_data.exec_cmpl.status = p_data->status;
1443 ( *p_clcb->p_rcb->p_cback)(BTA_GATTC_EXEC_EVT, &cb_data);
1447 /*******************************************************************************
1449 ** Function bta_gattc_cfg_mtu_cmpl
1451 ** Description configure MTU operation complete
1455 *******************************************************************************/
1456 void bta_gattc_cfg_mtu_cmpl(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_OP_CMPL *p_data)
1460 utl_freebuf((void **)&p_clcb->p_q_cmd);
1462 if (p_data->p_cmpl && p_data->status == BTA_GATT_OK)
1463 p_clcb->p_srcb->mtu = p_data->p_cmpl->mtu;
1465 /* configure MTU complete, callback */
1466 p_clcb->status = p_data->status;
1467 cb_data.cfg_mtu.conn_id = p_clcb->bta_conn_id;
1468 cb_data.cfg_mtu.status = p_data->status;
1469 cb_data.cfg_mtu.mtu = p_clcb->p_srcb->mtu;
1471 (*p_clcb->p_rcb->p_cback) (BTA_GATTC_CFG_MTU_EVT, &cb_data);
1474 /*******************************************************************************
1476 ** Function bta_gattc_op_cmpl
1478 ** Description operation completed.
1482 *******************************************************************************/
1483 void bta_gattc_op_cmpl(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1485 UINT8 op = (UINT8)p_data->op_cmpl.op_code;
1486 UINT8 mapped_op = 0;
1488 APPL_TRACE_DEBUG("bta_gattc_op_cmpl op = %d", op);
1490 if (op == GATTC_OPTYPE_INDICATION || op == GATTC_OPTYPE_NOTIFICATION)
1492 APPL_TRACE_ERROR("unexpected operation, ignored");
1494 else if (op >= GATTC_OPTYPE_READ)
1496 if (p_clcb->p_q_cmd == NULL)
1498 APPL_TRACE_ERROR("No pending command");
1501 if (p_clcb->p_q_cmd->hdr.event != bta_gattc_opcode_to_int_evt[op - GATTC_OPTYPE_READ])
1503 mapped_op = p_clcb->p_q_cmd->hdr.event - BTA_GATTC_API_READ_EVT + GATTC_OPTYPE_READ;
1504 if ( mapped_op > GATTC_OPTYPE_INDICATION) mapped_op = 0;
1506 #if (BT_TRACE_VERBOSE == TRUE)
1507 APPL_TRACE_ERROR("expect op:(%s :0x%04x), receive unexpected operation (%s).",
1508 bta_gattc_op_code_name[mapped_op] , p_clcb->p_q_cmd->hdr.event,
1509 bta_gattc_op_code_name[op]);
1511 APPL_TRACE_ERROR("expect op:(%u :0x%04x), receive unexpected operation (%u).",
1512 mapped_op , p_clcb->p_q_cmd->hdr.event, op);
1517 /* discard responses if service change indication is received before operation completed */
1518 if (p_clcb->auto_update == BTA_GATTC_DISC_WAITING && p_clcb->p_srcb->srvc_hdl_chg)
1520 APPL_TRACE_DEBUG("Discard all responses when service change indication is received.");
1521 p_data->op_cmpl.status = GATT_ERROR;
1524 /* service handle change void the response, discard it */
1525 if (op == GATTC_OPTYPE_READ)
1526 bta_gattc_read_cmpl(p_clcb, &p_data->op_cmpl);
1528 else if (op == GATTC_OPTYPE_WRITE)
1529 bta_gattc_write_cmpl(p_clcb, &p_data->op_cmpl);
1531 else if (op == GATTC_OPTYPE_EXE_WRITE)
1532 bta_gattc_exec_cmpl(p_clcb, &p_data->op_cmpl);
1534 else if (op == GATTC_OPTYPE_CONFIG)
1535 bta_gattc_cfg_mtu_cmpl(p_clcb, &p_data->op_cmpl);
1537 if (p_clcb->auto_update == BTA_GATTC_DISC_WAITING)
1539 p_clcb->auto_update = BTA_GATTC_REQ_WAITING;
1540 bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_DISCOVER_EVT, NULL);
1544 /*******************************************************************************
1546 ** Function bta_gattc_op_cmpl
1548 ** Description operation completed.
1552 *******************************************************************************/
1553 void bta_gattc_ignore_op_cmpl(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1557 /* receive op complete when discovery is started, ignore the response,
1558 and wait for discovery finish and resent */
1559 APPL_TRACE_DEBUG("bta_gattc_ignore_op_cmpl op = %d", p_data->hdr.layer_specific);
1562 /*******************************************************************************
1564 ** Function bta_gattc_search
1566 ** Description start a search in the local server cache
1570 *******************************************************************************/
1571 void bta_gattc_search(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1573 tBTA_GATT_STATUS status = GATT_INTERNAL_ERROR;
1575 APPL_TRACE_DEBUG("bta_gattc_search conn_id=%d",p_clcb->bta_conn_id);
1576 if (p_clcb->p_srcb && p_clcb->p_srcb->p_srvc_cache)
1578 status = BTA_GATT_OK;
1579 /* search the local cache of a server device */
1580 bta_gattc_search_service(p_clcb, p_data->api_search.p_srvc_uuid);
1582 cb_data.search_cmpl.status = status;
1583 cb_data.search_cmpl.conn_id = p_clcb->bta_conn_id;
1585 /* end of search or no server cache available */
1586 ( *p_clcb->p_rcb->p_cback)(BTA_GATTC_SEARCH_CMPL_EVT, &cb_data);
1588 /*******************************************************************************
1590 ** Function bta_gattc_q_cmd
1592 ** Description enqueue a command into control block, usually because discovery
1593 ** operation is busy.
1597 *******************************************************************************/
1598 void bta_gattc_q_cmd(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1600 bta_gattc_enqueue(p_clcb, p_data);
1602 /*******************************************************************************
1604 ** Function bta_gattc_cache_open
1606 ** Description open a NV cache for loading
1610 *******************************************************************************/
1611 void bta_gattc_cache_open(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1615 bta_gattc_set_discover_st(p_clcb->p_srcb);
1617 APPL_TRACE_DEBUG("bta_gattc_cache_open conn_id=%d",p_clcb->bta_conn_id);
1618 bta_gattc_co_cache_open(p_clcb->p_srcb->server_bda, BTA_GATTC_CI_CACHE_OPEN_EVT,
1619 p_clcb->bta_conn_id, FALSE);
1621 /*******************************************************************************
1623 ** Function bta_gattc_start_load
1625 ** Description start cache loading by sending callout open cache
1629 *******************************************************************************/
1630 void bta_gattc_ci_open(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1632 APPL_TRACE_DEBUG("bta_gattc_ci_open conn_id=%d server state=%d" ,
1633 p_clcb->bta_conn_id, p_clcb->p_srcb->state);
1634 if (p_clcb->p_srcb->state == BTA_GATTC_SERV_LOAD)
1636 if (p_data->ci_open.status == BTA_GATT_OK)
1638 p_clcb->p_srcb->attr_index = 0;
1639 bta_gattc_co_cache_load(p_clcb->p_srcb->server_bda,
1640 BTA_GATTC_CI_CACHE_LOAD_EVT,
1641 p_clcb->p_srcb->attr_index,
1642 p_clcb->bta_conn_id);
1646 p_clcb->p_srcb->state = BTA_GATTC_SERV_DISC;
1647 /* cache open failure, start discovery */
1648 bta_gattc_start_discover(p_clcb, NULL);
1651 if (p_clcb->p_srcb->state == BTA_GATTC_SERV_SAVE)
1653 if (p_data->ci_open.status == BTA_GATT_OK)
1655 if (!bta_gattc_cache_save(p_clcb->p_srcb, p_clcb->bta_conn_id))
1657 p_data->ci_open.status = BTA_GATT_ERROR;
1660 if (p_data->ci_open.status != BTA_GATT_OK)
1662 p_clcb->p_srcb->attr_index = 0;
1663 bta_gattc_co_cache_close(p_clcb->p_srcb->server_bda, p_clcb->bta_conn_id);
1664 bta_gattc_reset_discover_st(p_clcb->p_srcb, p_clcb->status);
1669 /*******************************************************************************
1671 ** Function bta_gattc_ci_load
1673 ** Description cache loading received.
1677 *******************************************************************************/
1678 void bta_gattc_ci_load(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1681 APPL_TRACE_DEBUG("bta_gattc_ci_load conn_id=%d load status=%d",
1682 p_clcb->bta_conn_id, p_data->ci_load.status);
1684 if (p_data->ci_load.status == BTA_GATT_OK ||
1685 p_data->ci_load.status == BTA_GATT_MORE)
1687 if (p_data->ci_load.num_attr != 0)
1688 bta_gattc_rebuild_cache(p_clcb->p_srcb, p_data->ci_load.num_attr,
1689 p_data->ci_load.attr, p_clcb->p_srcb->attr_index);
1691 if (p_data->ci_load.status == BTA_GATT_OK)
1693 p_clcb->p_srcb->attr_index = 0;
1694 bta_gattc_reset_discover_st(p_clcb->p_srcb, BTA_GATT_OK);
1695 bta_gattc_co_cache_close(p_clcb->p_srcb->server_bda, 0);
1697 else /* load more */
1699 p_clcb->p_srcb->attr_index += p_data->ci_load.num_attr;
1701 bta_gattc_co_cache_load(p_clcb->p_srcb->server_bda,
1702 BTA_GATTC_CI_CACHE_LOAD_EVT,
1703 p_clcb->p_srcb->attr_index,
1704 p_clcb->bta_conn_id);
1709 bta_gattc_co_cache_close(p_clcb->p_srcb->server_bda, 0);
1710 p_clcb->p_srcb->state = BTA_GATTC_SERV_DISC;
1711 p_clcb->p_srcb->attr_index = 0;
1712 /* cache load failure, start discovery */
1713 bta_gattc_start_discover(p_clcb, NULL);
1716 /*******************************************************************************
1718 ** Function bta_gattc_ci_save
1720 ** Description cache loading received.
1724 *******************************************************************************/
1725 void bta_gattc_ci_save(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1729 APPL_TRACE_DEBUG("bta_gattc_ci_save conn_id=%d " ,
1730 p_clcb->bta_conn_id );
1732 if (!bta_gattc_cache_save(p_clcb->p_srcb, p_clcb->bta_conn_id))
1734 p_clcb->p_srcb->attr_index = 0;
1735 bta_gattc_co_cache_close(p_clcb->p_srcb->server_bda, 0);
1736 bta_gattc_reset_discover_st(p_clcb->p_srcb, p_clcb->status);
1739 /*******************************************************************************
1741 ** Function bta_gattc_fail
1743 ** Description report API call failure back to apps
1747 *******************************************************************************/
1748 void bta_gattc_fail(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1752 if (p_clcb->status == BTA_GATT_OK)
1754 APPL_TRACE_ERROR("operation not supported at current state [%d]", p_clcb->state);
1758 /*******************************************************************************
1760 ** Function bta_gattc_deregister_cmpl
1762 ** Description De-Register a GATT client application with BTA completed.
1766 *******************************************************************************/
1767 static void bta_gattc_deregister_cmpl(tBTA_GATTC_RCB *p_clreg)
1769 tBTA_GATTC_CB *p_cb = &bta_gattc_cb;
1770 tBTA_GATTC_IF client_if = p_clreg->client_if;
1772 tBTA_GATTC_CBACK *p_cback = p_clreg->p_cback;
1774 memset(&cb_data, 0, sizeof(tBTA_GATTC));
1776 GATT_Deregister(p_clreg->client_if);
1777 memset(p_clreg, 0, sizeof(tBTA_GATTC_RCB));
1779 cb_data.reg_oper.client_if = client_if;
1780 cb_data.reg_oper.status = BTA_GATT_OK;
1783 /* callback with de-register event */
1784 (*p_cback)(BTA_GATTC_DEREG_EVT, (tBTA_GATTC *)&cb_data);
1786 if (bta_gattc_num_reg_app() == 0 && p_cb->state == BTA_GATTC_STATE_DISABLING)
1788 p_cb->state = BTA_GATTC_STATE_DISABLED;
1791 /*******************************************************************************
1793 ** Function bta_gattc_conn_cback
1794 ** bta_gattc_cmpl_cback
1796 ** Description callback functions to GATT client stack.
1800 *******************************************************************************/
1801 static void bta_gattc_conn_cback(tGATT_IF gattc_if, BD_ADDR bda, UINT16 conn_id,
1802 BOOLEAN connected, tGATT_DISCONN_REASON reason,
1803 tBT_TRANSPORT transport)
1805 tBTA_GATTC_DATA *p_buf;
1809 APPL_TRACE_WARNING("%s() - cif=%d connected=%d conn_id=%d reason=0x%04x",
1810 __FUNCTION__, gattc_if, connected, conn_id, reason);
1814 bdcpy(bdaddr.address, bda);
1816 btif_debug_conn_state(bdaddr, BTIF_DEBUG_CONNECTED, GATT_CONN_UNKNOWN);
1818 btif_debug_conn_state(bdaddr, BTIF_DEBUG_DISCONNECTED, reason);
1820 if ((p_buf = (tBTA_GATTC_DATA *) GKI_getbuf(sizeof(tBTA_GATTC_DATA))) != NULL)
1822 memset(p_buf, 0, sizeof(tBTA_GATTC_DATA));
1824 p_buf->int_conn.hdr.event = connected ? BTA_GATTC_INT_CONN_EVT:
1825 BTA_GATTC_INT_DISCONN_EVT;
1826 p_buf->int_conn.hdr.layer_specific = conn_id;
1827 p_buf->int_conn.client_if = gattc_if;
1828 p_buf->int_conn.role = L2CA_GetBleConnRole(bda);
1829 p_buf->int_conn.reason = reason;
1830 p_buf->int_conn.transport = transport;
1831 bdcpy(p_buf->int_conn.remote_bda, bda);
1833 bta_sys_sendmsg(p_buf);
1837 /*******************************************************************************
1839 ** Function bta_gattc_enc_cmpl_cback
1841 ** Description encryption complete callback function to GATT client stack.
1845 *******************************************************************************/
1846 static void bta_gattc_enc_cmpl_cback(tGATT_IF gattc_if, BD_ADDR bda)
1848 tBTA_GATTC_DATA *p_buf;
1849 tBTA_GATTC_CLCB *p_clcb = NULL;
1851 if ((p_clcb = bta_gattc_find_clcb_by_cif(gattc_if, bda, BTA_GATT_TRANSPORT_LE)) == NULL)
1856 #if (defined BTA_HH_LE_INCLUDED && BTA_HH_LE_INCLUDED == TRUE)
1857 /* filter this event just for BTA HH LE GATT client,
1858 In the future, if we want to enable encryption complete event
1859 for all GATT clients, we can remove this code */
1860 if (!bta_hh_le_is_hh_gatt_if(gattc_if))
1866 APPL_TRACE_DEBUG("bta_gattc_enc_cmpl_cback: cif = %d", gattc_if);
1868 if ((p_buf = (tBTA_GATTC_DATA *) GKI_getbuf(sizeof(tBTA_GATTC_DATA))) != NULL)
1870 memset(p_buf, 0, sizeof(tBTA_GATTC_DATA));
1872 p_buf->enc_cmpl.hdr.event = BTA_GATTC_ENC_CMPL_EVT;
1873 p_buf->enc_cmpl.hdr.layer_specific = p_clcb->bta_conn_id;
1874 p_buf->enc_cmpl.client_if = gattc_if;
1875 bdcpy(p_buf->enc_cmpl.remote_bda, bda);
1877 bta_sys_sendmsg(p_buf);
1881 /*******************************************************************************
1883 ** Function bta_gattc_process_api_refresh
1885 ** Description process refresh API to delete cache and start a new discovery
1886 ** if currently connected.
1890 *******************************************************************************/
1891 void bta_gattc_process_api_refresh(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA * p_msg)
1893 tBTA_GATTC_SERV *p_srvc_cb = bta_gattc_find_srvr_cache(p_msg->api_conn.remote_bda);
1894 tBTA_GATTC_CLCB *p_clcb = &bta_gattc_cb.clcb[0];
1895 BOOLEAN found = FALSE;
1899 if (p_srvc_cb != NULL)
1901 /* try to find a CLCB */
1902 if (p_srvc_cb->connected && p_srvc_cb->num_clcb != 0)
1904 for (i = 0; i < BTA_GATTC_CLCB_MAX; i ++, p_clcb ++)
1906 if (p_clcb->in_use && p_clcb->p_srcb == p_srvc_cb)
1914 bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_DISCOVER_EVT, NULL);
1918 /* in all other cases, mark it and delete the cache */
1919 if (p_srvc_cb->p_srvc_cache != NULL)
1921 while (!GKI_queue_is_empty(&p_srvc_cb->cache_buffer))
1922 GKI_freebuf (GKI_dequeue (&p_srvc_cb->cache_buffer));
1924 p_srvc_cb->p_srvc_cache = NULL;
1927 /* used to reset cache in application */
1928 bta_gattc_co_cache_reset(p_msg->api_conn.remote_bda);
1931 /*******************************************************************************
1933 ** Function bta_gattc_process_srvc_chg_ind
1935 ** Description process service change indication.
1939 *******************************************************************************/
1940 BOOLEAN bta_gattc_process_srvc_chg_ind(UINT16 conn_id,
1941 tBTA_GATTC_RCB *p_clrcb,
1942 tBTA_GATTC_SERV *p_srcb,
1943 tBTA_GATTC_CLCB *p_clcb,
1944 tBTA_GATTC_NOTIFY *p_notify,
1947 tBT_UUID gattp_uuid, srvc_chg_uuid;
1948 BOOLEAN processed = FALSE;
1952 gattp_uuid.uu.uuid16 = UUID_SERVCLASS_GATT_SERVER;
1954 srvc_chg_uuid.len = 2;
1955 srvc_chg_uuid.uu.uuid16 = GATT_UUID_GATT_SRV_CHGD;
1957 if (bta_gattc_uuid_compare(&p_notify->char_id.srvc_id.id.uuid, &gattp_uuid, TRUE) &&
1958 bta_gattc_uuid_compare(&p_notify->char_id.char_id.uuid, &srvc_chg_uuid, TRUE))
1961 /* mark service handle change pending */
1962 p_srcb->srvc_hdl_chg = TRUE;
1963 /* clear up all notification/indication registration */
1964 bta_gattc_clear_notif_registration(conn_id);
1965 /* service change indication all received, do discovery update */
1966 if ( ++ p_srcb->update_count == bta_gattc_num_reg_app())
1968 /* not an opened connection; or connection busy */
1969 /* search for first available clcb and start discovery */
1970 if (p_clcb == NULL || (p_clcb && p_clcb->p_q_cmd != NULL))
1972 for (i = 0 ; i < BTA_GATTC_CLCB_MAX; i ++)
1974 if (bta_gattc_cb.clcb[i].in_use &&
1975 bta_gattc_cb.clcb[i].p_srcb == p_srcb &&
1976 bta_gattc_cb.clcb[i].p_q_cmd == NULL)
1978 p_clcb = &bta_gattc_cb.clcb[i];
1983 /* send confirmation here if this is an indication, it should always be */
1984 GATTC_SendHandleValueConfirm(conn_id, handle);
1986 /* if connection available, refresh cache by doing discovery now */
1988 bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_DISCOVER_EVT, NULL);
1990 /* notify applicationf or service change */
1991 if (p_clrcb->p_cback != NULL)
1993 (* p_clrcb->p_cback)(BTA_GATTC_SRVC_CHG_EVT, (tBTA_GATTC *)p_srcb->server_bda);
2001 /*******************************************************************************
2003 ** Function bta_gattc_proc_other_indication
2005 ** Description process all non-service change indication/notification.
2009 *******************************************************************************/
2010 void bta_gattc_proc_other_indication(tBTA_GATTC_CLCB *p_clcb, UINT8 op,
2011 tGATT_CL_COMPLETE *p_data,
2012 tBTA_GATTC_NOTIFY *p_notify)
2014 APPL_TRACE_DEBUG("bta_gattc_proc_other_indication check \
2015 p_data->att_value.handle=%d p_data->handle=%d",
2016 p_data->att_value.handle, p_data->handle);
2017 APPL_TRACE_DEBUG("is_notify", p_notify->is_notify);
2019 p_notify->is_notify = (op == GATTC_OPTYPE_INDICATION) ? FALSE : TRUE;
2020 p_notify->len = p_data->att_value.len;
2021 bdcpy(p_notify->bda, p_clcb->bda);
2022 memcpy(p_notify->value, p_data->att_value.value, p_data->att_value.len);
2023 p_notify->conn_id = p_clcb->bta_conn_id;
2025 if (p_clcb->p_rcb->p_cback)
2026 (*p_clcb->p_rcb->p_cback)(BTA_GATTC_NOTIF_EVT, (tBTA_GATTC *)p_notify);
2029 /*******************************************************************************
2031 ** Function bta_gattc_process_indicate
2033 ** Description process indication/notification.
2037 *******************************************************************************/
2038 void bta_gattc_process_indicate(UINT16 conn_id, tGATTC_OPTYPE op, tGATT_CL_COMPLETE *p_data)
2040 UINT16 handle = p_data->att_value.handle;
2041 tBTA_GATTC_CLCB *p_clcb ;
2042 tBTA_GATTC_RCB *p_clrcb = NULL;
2043 tBTA_GATTC_SERV *p_srcb = NULL;
2044 tBTA_GATTC_NOTIFY notify;
2046 tBTA_GATTC_IF gatt_if;
2047 tBTA_TRANSPORT transport;
2049 if (!GATT_GetConnectionInfor(conn_id, &gatt_if, remote_bda, &transport))
2051 APPL_TRACE_ERROR("%s indication/notif for unknown app", __func__);
2052 if (op == GATTC_OPTYPE_INDICATION)
2053 GATTC_SendHandleValueConfirm(conn_id, handle);
2057 if ((p_clrcb = bta_gattc_cl_get_regcb(gatt_if)) == NULL)
2059 APPL_TRACE_ERROR("%s indication/notif for unregistered app", __func__);
2060 if (op == GATTC_OPTYPE_INDICATION)
2061 GATTC_SendHandleValueConfirm(conn_id, handle);
2065 if ((p_srcb = bta_gattc_find_srcb(remote_bda)) == NULL)
2067 APPL_TRACE_ERROR("%s indication/notif for unknown device, ignore", __func__);
2068 if (op == GATTC_OPTYPE_INDICATION)
2069 GATTC_SendHandleValueConfirm(conn_id, handle);
2073 p_clcb = bta_gattc_find_clcb_by_conn_id(conn_id);
2075 if (bta_gattc_handle2id(p_srcb, handle,
2076 ¬ify.char_id.srvc_id,
2077 ¬ify.char_id.char_id,
2078 ¬ify.descr_type))
2080 /* if non-service change indication/notification, forward to application */
2081 if (!bta_gattc_process_srvc_chg_ind(conn_id, p_clrcb, p_srcb, p_clcb, ¬ify, handle))
2083 /* if app registered for the notification */
2084 if (bta_gattc_check_notif_registry(p_clrcb, p_srcb, ¬ify))
2086 /* connection not open yet */
2089 if ((p_clcb = bta_gattc_clcb_alloc(gatt_if, remote_bda, transport)) != NULL)
2091 p_clcb->bta_conn_id = conn_id;
2092 p_clcb->transport = transport;
2094 bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_CONN_EVT, NULL);
2098 APPL_TRACE_ERROR("No resources");
2103 bta_gattc_proc_other_indication(p_clcb, op, p_data, ¬ify);
2105 /* no one intersted and need ack? */
2106 else if (op == GATTC_OPTYPE_INDICATION)
2108 APPL_TRACE_DEBUG("%s no one interested, ack now", __func__);
2109 GATTC_SendHandleValueConfirm(conn_id, handle);
2115 APPL_TRACE_ERROR("%s Indi/Notif for Unknown handle[0x%04x], can not find in local cache.",
2117 if (op == GATTC_OPTYPE_INDICATION)
2118 GATTC_SendHandleValueConfirm(conn_id, handle);
2121 /*******************************************************************************
2123 ** Function bta_gattc_cmpl_cback
2125 ** Description client operation complete callback register with BTE GATT.
2129 *******************************************************************************/
2130 static void bta_gattc_cmpl_cback(UINT16 conn_id, tGATTC_OPTYPE op, tGATT_STATUS status,
2131 tGATT_CL_COMPLETE *p_data)
2133 tBTA_GATTC_CLCB *p_clcb ;
2134 tBTA_GATTC_OP_CMPL *p_buf;
2135 UINT16 len = sizeof(tBTA_GATTC_OP_CMPL) + sizeof(tGATT_CL_COMPLETE);
2137 APPL_TRACE_DEBUG("bta_gattc_cmpl_cback: conn_id = %d op = %d status = %d",
2138 conn_id, op, status);
2140 /* notification and indication processed right away */
2141 if (op == GATTC_OPTYPE_NOTIFICATION || op == GATTC_OPTYPE_INDICATION)
2143 bta_gattc_process_indicate(conn_id, op, p_data);
2146 /* for all other operation, not expected if w/o connection */
2147 else if ((p_clcb = bta_gattc_find_clcb_by_conn_id(conn_id)) == NULL)
2149 APPL_TRACE_ERROR("bta_gattc_cmpl_cback unknown conn_id = %d, ignore data", conn_id);
2153 /* if over BR_EDR, inform PM for mode change */
2154 if (p_clcb->transport == BTA_TRANSPORT_BR_EDR)
2156 bta_sys_busy(BTA_ID_GATTC, BTA_ALL_APP_ID, p_clcb->bda);
2157 bta_sys_idle(BTA_ID_GATTC, BTA_ALL_APP_ID, p_clcb->bda);
2160 if ((p_buf = (tBTA_GATTC_OP_CMPL *) GKI_getbuf(len)) != NULL)
2162 memset(p_buf, 0, len);
2163 p_buf->hdr.event = BTA_GATTC_OP_CMPL_EVT;
2164 p_buf->hdr.layer_specific = conn_id;
2165 p_buf->status = status;
2166 p_buf->op_code = op;
2170 p_buf->p_cmpl = (tGATT_CL_COMPLETE *)(p_buf + 1);
2171 memcpy(p_buf->p_cmpl, p_data, sizeof(tGATT_CL_COMPLETE));
2174 bta_sys_sendmsg(p_buf);
2180 /*******************************************************************************
2182 ** Function bta_gattc_cong_cback
2184 ** Description congestion callback for BTA GATT client.
2188 ********************************************************************************/
2189 static void bta_gattc_cong_cback (UINT16 conn_id, BOOLEAN congested)
2191 tBTA_GATTC_CLCB *p_clcb;
2194 if ((p_clcb = bta_gattc_find_clcb_by_conn_id(conn_id)) != NULL)
2196 if (p_clcb->p_rcb->p_cback)
2198 cb_data.congest.conn_id = conn_id;
2199 cb_data.congest.congested = congested;
2201 (*p_clcb->p_rcb->p_cback)(BTA_GATTC_CONGEST_EVT, &cb_data);
2206 #if BLE_INCLUDED == TRUE
2207 /*******************************************************************************
2209 ** Function bta_gattc_init_clcb_conn
2211 ** Description Initaite a BTA CLCB connection
2215 ********************************************************************************/
2216 void bta_gattc_init_clcb_conn(UINT8 cif, BD_ADDR remote_bda)
2218 tBTA_GATTC_CLCB *p_clcb = NULL;
2219 tBTA_GATTC_DATA gattc_data;
2222 /* should always get the connection ID */
2223 if (GATT_GetConnIdIfConnected(cif, remote_bda, &conn_id, BTA_GATT_TRANSPORT_LE) == FALSE)
2225 APPL_TRACE_ERROR("bta_gattc_init_clcb_conn ERROR: not a connected device");
2229 /* initaite a new connection here */
2230 if ((p_clcb = bta_gattc_clcb_alloc(cif, remote_bda, BTA_GATT_TRANSPORT_LE)) != NULL)
2232 gattc_data.hdr.layer_specific = p_clcb->bta_conn_id = conn_id;
2234 gattc_data.api_conn.client_if = cif;
2235 memcpy(gattc_data.api_conn.remote_bda, remote_bda, BD_ADDR_LEN);
2236 gattc_data.api_conn.is_direct = TRUE;
2238 bta_gattc_sm_execute(p_clcb, BTA_GATTC_API_OPEN_EVT, &gattc_data);
2242 APPL_TRACE_ERROR("No resources");
2245 /*******************************************************************************
2247 ** Function bta_gattc_process_listen_all
2249 ** Description process listen all, send open callback to application for all
2250 ** connected slave LE link.
2254 ********************************************************************************/
2255 void bta_gattc_process_listen_all(UINT8 cif)
2258 tBTA_GATTC_CONN *p_conn = &bta_gattc_cb.conn_track[0];
2260 for (i_conn = 0; i_conn < BTA_GATTC_CONN_MAX; i_conn++, p_conn ++)
2262 if (p_conn->in_use )
2264 if (bta_gattc_find_clcb_by_cif(cif, p_conn->remote_bda, BTA_GATT_TRANSPORT_LE) == NULL)
2266 bta_gattc_init_clcb_conn(cif, p_conn->remote_bda);
2268 /* else already connected */
2272 /*******************************************************************************
2274 ** Function bta_gattc_listen
2276 ** Description Start or stop a listen for connection
2280 ********************************************************************************/
2281 void bta_gattc_listen(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA * p_msg)
2283 tBTA_GATTC_RCB *p_clreg = bta_gattc_cl_get_regcb(p_msg->api_listen.client_if);
2287 cb_data.reg_oper.status = BTA_GATT_ERROR;
2288 cb_data.reg_oper.client_if = p_msg->api_listen.client_if;
2290 if (p_clreg == NULL)
2292 APPL_TRACE_ERROR("bta_gattc_listen failed, unknown client_if: %d",
2293 p_msg->api_listen.client_if);
2296 /* mark bg conn record */
2297 if (bta_gattc_mark_bg_conn(p_msg->api_listen.client_if,
2298 (BD_ADDR_PTR) p_msg->api_listen.remote_bda,
2299 p_msg->api_listen.start,
2302 if (!GATT_Listen(p_msg->api_listen.client_if,
2303 p_msg->api_listen.start,
2304 p_msg->api_listen.remote_bda))
2306 APPL_TRACE_ERROR("Listen failure");
2307 (*p_clreg->p_cback)(BTA_GATTC_LISTEN_EVT, &cb_data);
2311 cb_data.status = BTA_GATT_OK;
2313 (*p_clreg->p_cback)(BTA_GATTC_LISTEN_EVT, &cb_data);
2315 if (p_msg->api_listen.start)
2317 /* if listen to a specific target */
2318 if (p_msg->api_listen.remote_bda != NULL)
2321 /* if is a connected remote device */
2322 if (L2CA_GetBleConnRole(p_msg->api_listen.remote_bda) == HCI_ROLE_SLAVE &&
2323 bta_gattc_find_clcb_by_cif(p_msg->api_listen.client_if,
2324 p_msg->api_listen.remote_bda,
2325 BTA_GATT_TRANSPORT_LE) == NULL)
2328 bta_gattc_init_clcb_conn(p_msg->api_listen.client_if,
2329 p_msg->api_listen.remote_bda);
2332 /* if listen to all */
2335 LOG_DEBUG(LOG_TAG, "Listen For All now");
2336 /* go through all connected device and send
2337 callback for all connected slave connection */
2338 bta_gattc_process_listen_all(p_msg->api_listen.client_if);
2345 /*******************************************************************************
2347 ** Function bta_gattc_broadcast
2349 ** Description Start or stop broadcasting
2353 ********************************************************************************/
2354 void bta_gattc_broadcast(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA * p_msg)
2356 tBTA_GATTC_RCB *p_clreg = bta_gattc_cl_get_regcb(p_msg->api_listen.client_if);
2360 cb_data.reg_oper.client_if = p_msg->api_listen.client_if;
2361 cb_data.reg_oper.status = BTM_BleBroadcast(p_msg->api_listen.start);
2363 if (p_clreg && p_clreg->p_cback)
2364 (*p_clreg->p_cback)(BTA_GATTC_LISTEN_EVT, &cb_data);