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("%s: Discovery cancel conn_id=%d", __func__,
860 p_clcb->bta_conn_id);
862 if (p_clcb->disc_active)
863 bta_gattc_reset_discover_st(p_clcb->p_srcb, BTA_GATT_ERROR);
865 p_clcb->state = BTA_GATTC_CONN_ST;
867 // This function only gets called as the result of a BTA_GATTC_API_CLOSE_EVT
868 // while in the BTA_GATTC_DISCOVER_ST state. Once the state changes, the
869 // connection itself still needs to be closed to resolve the original event.
870 if (p_clcb->state == BTA_GATTC_CONN_ST)
872 APPL_TRACE_DEBUG("State is back to BTA_GATTC_CONN_ST. "
873 "Trigger connection close");
874 bta_gattc_close(p_clcb, p_data);
877 /*******************************************************************************
879 ** Function bta_gattc_set_discover_st
881 ** Description when a SRCB start discovery, tell all related clcb and set
886 *******************************************************************************/
887 void bta_gattc_set_discover_st(tBTA_GATTC_SERV *p_srcb)
889 tBTA_GATTC_CB *p_cb = &bta_gattc_cb;
892 #if BLE_INCLUDED == TRUE
893 L2CA_EnableUpdateBleConnParams(p_srcb->server_bda, FALSE);
895 for (i = 0; i < BTA_GATTC_CLCB_MAX; i ++)
897 if (p_cb->clcb[i].p_srcb == p_srcb)
899 p_cb->clcb[i].status = BTA_GATT_OK;
900 p_cb->clcb[i].state = BTA_GATTC_DISCOVER_ST;
904 /*******************************************************************************
906 ** Function bta_gattc_restart_discover
908 ** Description process service change in discovery state, mark up the auto
909 ** update flag and set status to be discovery cancel for current
914 *******************************************************************************/
915 void bta_gattc_restart_discover(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
919 p_clcb->status = BTA_GATT_CANCEL;
920 p_clcb->auto_update = BTA_GATTC_DISC_WAITING;
923 /*******************************************************************************
925 ** Function bta_gattc_cfg_mtu
927 ** Description Configure MTU size on the GATT connection.
931 *******************************************************************************/
932 void bta_gattc_cfg_mtu(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
934 tBTA_GATTC_OP_CMPL op_cmpl;
935 tBTA_GATT_STATUS status;
937 if (bta_gattc_enqueue(p_clcb, p_data))
939 status = GATTC_ConfigureMTU (p_clcb->bta_conn_id, p_data->api_mtu.mtu);
941 /* if failed, return callback here */
942 if (status != GATT_SUCCESS && status != GATT_CMD_STARTED)
944 memset(&op_cmpl, 0, sizeof(tBTA_GATTC_OP_CMPL));
946 op_cmpl.status = status;
947 op_cmpl.op_code = GATTC_OPTYPE_CONFIG;
948 op_cmpl.p_cmpl = NULL;
950 bta_gattc_sm_execute(p_clcb, BTA_GATTC_OP_CMPL_EVT, (tBTA_GATTC_DATA *)&op_cmpl);
954 /*******************************************************************************
956 ** Function bta_gattc_start_discover
958 ** Description Start a discovery on server.
962 *******************************************************************************/
963 void bta_gattc_start_discover(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
967 APPL_TRACE_DEBUG("bta_gattc_start_discover conn_id=%d p_clcb->p_srcb->state = %d ",
968 p_clcb->bta_conn_id, p_clcb->p_srcb->state);
970 if (((p_clcb->p_q_cmd == NULL || p_clcb->auto_update == BTA_GATTC_REQ_WAITING) &&
971 p_clcb->p_srcb->state == BTA_GATTC_SERV_IDLE) ||
972 p_clcb->p_srcb->state == BTA_GATTC_SERV_DISC)
973 /* no pending operation, start discovery right away */
975 p_clcb->auto_update = BTA_GATTC_NO_SCHEDULE;
977 if (p_clcb->p_srcb != NULL)
979 /* clear the service change mask */
980 p_clcb->p_srcb->srvc_hdl_chg = FALSE;
981 p_clcb->p_srcb->update_count = 0;
982 p_clcb->p_srcb->state = BTA_GATTC_SERV_DISC_ACT;
984 if (p_clcb->transport == BTA_TRANSPORT_LE)
985 L2CA_EnableUpdateBleConnParams(p_clcb->p_srcb->server_bda, FALSE);
987 /* set all srcb related clcb into discovery ST */
988 bta_gattc_set_discover_st(p_clcb->p_srcb);
990 if ((p_clcb->status = bta_gattc_init_cache(p_clcb->p_srcb)) == BTA_GATT_OK)
992 p_clcb->status = bta_gattc_discover_pri_service(p_clcb->bta_conn_id,
993 p_clcb->p_srcb, GATT_DISC_SRVC_ALL);
995 if (p_clcb->status != BTA_GATT_OK)
997 APPL_TRACE_ERROR("discovery on server failed");
998 bta_gattc_reset_discover_st(p_clcb->p_srcb, p_clcb->status);
1001 p_clcb->disc_active = TRUE;
1005 APPL_TRACE_ERROR("unknown device, can not start discovery");
1008 /* pending operation, wait until it finishes */
1011 p_clcb->auto_update = BTA_GATTC_DISC_WAITING;
1013 if (p_clcb->p_srcb->state == BTA_GATTC_SERV_IDLE)
1014 p_clcb->state = BTA_GATTC_CONN_ST; /* set clcb state */
1018 /*******************************************************************************
1020 ** Function bta_gattc_disc_cmpl
1022 ** Description discovery on server is finished
1026 *******************************************************************************/
1027 void bta_gattc_disc_cmpl(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1029 tBTA_GATTC_DATA *p_q_cmd = p_clcb->p_q_cmd;
1032 APPL_TRACE_DEBUG("bta_gattc_disc_cmpl conn_id=%d",p_clcb->bta_conn_id);
1034 #if BLE_INCLUDED == TRUE
1035 if(p_clcb->transport == BTA_TRANSPORT_LE)
1036 L2CA_EnableUpdateBleConnParams(p_clcb->p_srcb->server_bda, TRUE);
1038 p_clcb->p_srcb->state = BTA_GATTC_SERV_IDLE;
1039 p_clcb->disc_active = FALSE;
1041 if (p_clcb->status != GATT_SUCCESS)
1043 /* clean up cache */
1044 if(p_clcb->p_srcb && p_clcb->p_srcb->p_srvc_cache)
1046 while (!GKI_queue_is_empty(&p_clcb->p_srcb->cache_buffer))
1048 GKI_freebuf (GKI_dequeue (&p_clcb->p_srcb->cache_buffer));
1050 p_clcb->p_srcb->p_srvc_cache = NULL;
1053 /* used to reset cache in application */
1054 bta_gattc_co_cache_reset(p_clcb->p_srcb->server_bda);
1056 /* release pending attribute list buffer */
1057 utl_freebuf((void **)&p_clcb->p_srcb->p_srvc_list);
1059 if (p_clcb->auto_update == BTA_GATTC_DISC_WAITING)
1061 /* start discovery again */
1062 bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_DISCOVER_EVT, NULL);
1064 /* get any queued command to proceed */
1065 else if (p_q_cmd != NULL)
1067 p_clcb->p_q_cmd = NULL;
1069 bta_gattc_sm_execute(p_clcb, p_q_cmd->hdr.event, p_q_cmd);
1070 /* if the command executed requeued the cmd, we don't
1071 * want to free the underlying buffer that's being
1072 * referenced by p_clcb->p_q_cmd
1074 if (p_q_cmd != p_clcb->p_q_cmd) {
1075 utl_freebuf((void **)&p_q_cmd);
1079 /*******************************************************************************
1081 ** Function bta_gattc_read
1083 ** Description Read an attribute
1087 *******************************************************************************/
1088 void bta_gattc_read(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1091 tGATT_READ_PARAM read_param;
1092 tBTA_GATTC_OP_CMPL op_cmpl;
1094 memset (&read_param, 0 ,sizeof(tGATT_READ_PARAM));
1095 memset (&op_cmpl, 0 ,sizeof(tBTA_GATTC_OP_CMPL));
1097 if (bta_gattc_enqueue(p_clcb, p_data))
1099 if ((handle = bta_gattc_id2handle(p_clcb->p_srcb,
1100 &p_data->api_read.srvc_id,
1101 &p_data->api_read.char_id,
1102 p_data->api_read.p_descr_type)) == 0)
1104 op_cmpl.status = BTA_GATT_ERROR;
1108 read_param.by_handle.handle = handle;
1109 read_param.by_handle.auth_req = p_data->api_read.auth_req;
1111 op_cmpl.status = GATTC_Read(p_clcb->bta_conn_id, GATT_READ_BY_HANDLE, &read_param);
1115 if (op_cmpl.status != BTA_GATT_OK)
1117 op_cmpl.op_code = GATTC_OPTYPE_READ;
1118 op_cmpl.p_cmpl = NULL;
1120 bta_gattc_sm_execute(p_clcb, BTA_GATTC_OP_CMPL_EVT, (tBTA_GATTC_DATA *)&op_cmpl);
1124 /*******************************************************************************
1126 ** Function bta_gattc_read_multi
1128 ** Description read multiple
1131 *********************************************************************************/
1132 void bta_gattc_read_multi(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1135 tBTA_GATT_STATUS status = BTA_GATT_OK;
1136 tGATT_READ_PARAM read_param;
1137 tBTA_GATTC_OP_CMPL op_cmpl;
1138 tBTA_GATTC_ATTR_ID *p_id;
1140 if (bta_gattc_enqueue(p_clcb, p_data))
1142 memset(&read_param, 0, sizeof(tGATT_READ_PARAM));
1144 p_id = p_data->api_read_multi.p_id_list;
1146 for (i = 0; i < p_data->api_read_multi.num_attr && p_id; i ++, p_id ++)
1150 if (p_id->id_type == BTA_GATT_TYPE_CHAR)
1152 handle = bta_gattc_id2handle(p_clcb->p_srcb,
1153 &p_id->id_value.char_id.srvc_id,
1154 &p_id->id_value.char_id.char_id,
1157 else if (p_id->id_type == BTA_GATT_TYPE_CHAR_DESCR)
1159 handle = bta_gattc_id2handle(p_clcb->p_srcb,
1160 &p_id->id_value.char_descr_id.char_id.srvc_id,
1161 &p_id->id_value.char_descr_id.char_id.char_id,
1162 &p_id->id_value.char_descr_id.descr_id);
1166 APPL_TRACE_ERROR("invalud ID type: %d", p_id->id_type);
1171 status = BTA_GATT_ERROR;
1175 if (status == BTA_GATT_OK)
1177 read_param.read_multiple.num_handles = p_data->api_read_multi.num_attr;
1178 read_param.read_multiple.auth_req = p_data->api_read_multi.auth_req;
1180 status = GATTC_Read(p_clcb->bta_conn_id, GATT_READ_MULTIPLE, &read_param);
1184 if (status != BTA_GATT_OK)
1186 memset(&op_cmpl, 0, sizeof(tBTA_GATTC_OP_CMPL));
1188 op_cmpl.status = status;
1189 op_cmpl.op_code = GATTC_OPTYPE_READ;
1190 op_cmpl.p_cmpl = NULL;
1192 bta_gattc_sm_execute(p_clcb, BTA_GATTC_OP_CMPL_EVT, (tBTA_GATTC_DATA *)&op_cmpl);
1196 /*******************************************************************************
1198 ** Function bta_gattc_write
1200 ** Description Write an attribute
1204 *******************************************************************************/
1205 void bta_gattc_write(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1208 tGATT_VALUE attr = {0};
1209 tBTA_GATTC_OP_CMPL op_cmpl;
1210 tBTA_GATT_STATUS status = BTA_GATT_OK;
1212 if (bta_gattc_enqueue(p_clcb, p_data))
1214 if ((handle = bta_gattc_id2handle(p_clcb->p_srcb,
1215 &p_data->api_write.srvc_id,
1216 &p_data->api_write.char_id,
1217 p_data->api_write.p_descr_type)) == 0)
1219 status = BTA_GATT_ERROR;
1223 attr.handle= handle;
1224 attr.offset = p_data->api_write.offset;
1225 attr.len = p_data->api_write.len;
1226 attr.auth_req = p_data->api_write.auth_req;
1228 if (p_data->api_write.p_value)
1229 memcpy(attr.value, p_data->api_write.p_value, p_data->api_write.len);
1231 status = GATTC_Write(p_clcb->bta_conn_id, p_data->api_write.write_type, &attr);
1235 if (status != BTA_GATT_OK)
1237 memset(&op_cmpl, 0, sizeof(tBTA_GATTC_OP_CMPL));
1239 op_cmpl.status = status;
1240 op_cmpl.op_code = GATTC_OPTYPE_WRITE;
1241 op_cmpl.p_cmpl = NULL;
1243 bta_gattc_sm_execute(p_clcb, BTA_GATTC_OP_CMPL_EVT, (tBTA_GATTC_DATA *)&op_cmpl);
1247 /*******************************************************************************
1249 ** Function bta_gattc_execute
1251 ** Description send execute write
1254 *********************************************************************************/
1255 void bta_gattc_execute(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1257 tBTA_GATTC_OP_CMPL op_cmpl;
1258 tBTA_GATT_STATUS status;
1260 if (bta_gattc_enqueue(p_clcb, p_data))
1262 status = GATTC_ExecuteWrite(p_clcb->bta_conn_id, p_data->api_exec.is_execute);
1264 if (status != BTA_GATT_OK)
1266 memset(&op_cmpl, 0, sizeof(tBTA_GATTC_OP_CMPL));
1268 op_cmpl.status = status;
1269 op_cmpl.op_code = GATTC_OPTYPE_EXE_WRITE;
1270 op_cmpl.p_cmpl = NULL;
1272 bta_gattc_sm_execute(p_clcb, BTA_GATTC_OP_CMPL_EVT, (tBTA_GATTC_DATA *)&op_cmpl);
1276 /*******************************************************************************
1278 ** Function bta_gattc_confirm
1280 ** Description send handle value confirmation
1284 *******************************************************************************/
1285 void bta_gattc_confirm(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1289 if ((handle = bta_gattc_id2handle(p_clcb->p_srcb,
1290 &p_data->api_confirm.srvc_id,
1291 &p_data->api_confirm.char_id,
1294 APPL_TRACE_ERROR("Can not map service/char ID into valid handle");
1298 if (GATTC_SendHandleValueConfirm(p_data->api_confirm.hdr.layer_specific, handle)
1301 APPL_TRACE_ERROR("bta_gattc_confirm to handle [0x%04x] failed", handle);
1305 /* if over BR_EDR, inform PM for mode change */
1306 if (p_clcb->transport == BTA_TRANSPORT_BR_EDR)
1308 bta_sys_busy(BTA_ID_GATTC, BTA_ALL_APP_ID, p_clcb->bda);
1309 bta_sys_idle(BTA_ID_GATTC, BTA_ALL_APP_ID, p_clcb->bda);
1314 /*******************************************************************************
1316 ** Function bta_gattc_read_cmpl
1318 ** Description read complete
1322 *******************************************************************************/
1323 void bta_gattc_read_cmpl(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_OP_CMPL *p_data)
1327 tBTA_GATT_READ_VAL read_value;
1329 memset(&cb_data, 0, sizeof(tBTA_GATTC));
1330 memset(&read_value, 0, sizeof(tBTA_GATT_READ_VAL));
1332 cb_data.read.status = p_data->status;
1334 if (p_data->p_cmpl != NULL && p_data->status == BTA_GATT_OK)
1336 if (bta_gattc_handle2id(p_clcb->p_srcb,
1337 p_data->p_cmpl->att_value.handle,
1338 &cb_data.read.srvc_id,
1339 &cb_data.read.char_id,
1340 &cb_data.read.descr_type) == FALSE)
1342 cb_data.read.status = BTA_GATT_INTERNAL_ERROR;
1343 APPL_TRACE_ERROR("can not map to GATT ID. handle = 0x%04x",
1344 p_data->p_cmpl->att_value.handle);
1348 cb_data.read.status = bta_gattc_pack_read_cb_data(p_clcb->p_srcb,
1349 &cb_data.read.descr_type.uuid,
1350 &p_data->p_cmpl->att_value,
1352 cb_data.read.p_value = &read_value;
1357 cb_data.read.srvc_id = p_clcb->p_q_cmd->api_read.srvc_id;
1358 cb_data.read.char_id = p_clcb->p_q_cmd->api_read.char_id;
1359 if (p_clcb->p_q_cmd->api_read.p_descr_type)
1360 memcpy(&cb_data.read.descr_type, p_clcb->p_q_cmd->api_read.p_descr_type,
1361 sizeof(tBTA_GATT_ID));
1364 event = (p_clcb->p_q_cmd->api_read.p_descr_type == NULL) ?
1365 BTA_GATTC_READ_CHAR_EVT: BTA_GATTC_READ_DESCR_EVT;
1366 cb_data.read.conn_id = p_clcb->bta_conn_id;
1368 utl_freebuf((void **)&p_clcb->p_q_cmd);
1369 /* read complete, callback */
1370 ( *p_clcb->p_rcb->p_cback)(event, (tBTA_GATTC *)&cb_data);
1373 /*******************************************************************************
1375 ** Function bta_gattc_write_cmpl
1377 ** Description write complete
1381 *******************************************************************************/
1382 void bta_gattc_write_cmpl(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_OP_CMPL *p_data)
1384 tBTA_GATTC cb_data = {0};
1387 memset(&cb_data, 0, sizeof(tBTA_GATTC));
1389 cb_data.write.status = p_data->status;
1391 if (p_data->p_cmpl != NULL)
1393 bta_gattc_handle2id(p_clcb->p_srcb, p_data->p_cmpl->att_value.handle,
1394 &cb_data.write.srvc_id, &cb_data.write.char_id,
1395 &cb_data.write.descr_type);
1399 memcpy(&cb_data.write.srvc_id, &p_clcb->p_q_cmd->api_write.srvc_id,
1400 sizeof(tBTA_GATT_SRVC_ID));
1401 memcpy(&cb_data.write.char_id, &p_clcb->p_q_cmd->api_write.char_id,
1402 sizeof(tBTA_GATT_ID));
1403 if (p_clcb->p_q_cmd->api_write.p_descr_type)
1404 memcpy(&cb_data.write.descr_type, p_clcb->p_q_cmd->api_write.p_descr_type,
1405 sizeof(tBTA_GATT_ID));
1408 if (p_clcb->p_q_cmd->api_write.hdr.event == BTA_GATTC_API_WRITE_EVT &&
1409 p_clcb->p_q_cmd->api_write.write_type == BTA_GATTC_WRITE_PREPARE)
1411 event = BTA_GATTC_PREP_WRITE_EVT;
1413 else if (p_clcb->p_q_cmd->api_write.p_descr_type == NULL)
1415 event = BTA_GATTC_WRITE_CHAR_EVT;
1418 event = BTA_GATTC_WRITE_DESCR_EVT;
1420 utl_freebuf((void **)&p_clcb->p_q_cmd);
1421 cb_data.write.conn_id = p_clcb->bta_conn_id;
1422 /* write complete, callback */
1423 ( *p_clcb->p_rcb->p_cback)(event, (tBTA_GATTC *)&cb_data);
1426 /*******************************************************************************
1428 ** Function bta_gattc_exec_cmpl
1430 ** Description execute write complete
1434 *******************************************************************************/
1435 void bta_gattc_exec_cmpl(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_OP_CMPL *p_data)
1439 utl_freebuf((void **)&p_clcb->p_q_cmd);
1441 p_clcb->status = BTA_GATT_OK;
1443 /* execute complete, callback */
1444 cb_data.exec_cmpl.conn_id = p_clcb->bta_conn_id;
1445 cb_data.exec_cmpl.status = p_data->status;
1447 ( *p_clcb->p_rcb->p_cback)(BTA_GATTC_EXEC_EVT, &cb_data);
1451 /*******************************************************************************
1453 ** Function bta_gattc_cfg_mtu_cmpl
1455 ** Description configure MTU operation complete
1459 *******************************************************************************/
1460 void bta_gattc_cfg_mtu_cmpl(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_OP_CMPL *p_data)
1464 utl_freebuf((void **)&p_clcb->p_q_cmd);
1467 if (p_data->p_cmpl && p_data->status == BTA_GATT_OK)
1468 p_clcb->p_srcb->mtu = p_data->p_cmpl->mtu;
1470 /* configure MTU complete, callback */
1471 p_clcb->status = p_data->status;
1472 cb_data.cfg_mtu.conn_id = p_clcb->bta_conn_id;
1473 cb_data.cfg_mtu.status = p_data->status;
1474 cb_data.cfg_mtu.mtu = p_clcb->p_srcb->mtu;
1476 (*p_clcb->p_rcb->p_cback) (BTA_GATTC_CFG_MTU_EVT, &cb_data);
1479 /*******************************************************************************
1481 ** Function bta_gattc_op_cmpl
1483 ** Description operation completed.
1487 *******************************************************************************/
1488 void bta_gattc_op_cmpl(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1490 UINT8 op = (UINT8)p_data->op_cmpl.op_code;
1491 UINT8 mapped_op = 0;
1493 APPL_TRACE_DEBUG("bta_gattc_op_cmpl op = %d", op);
1495 if (op == GATTC_OPTYPE_INDICATION || op == GATTC_OPTYPE_NOTIFICATION)
1497 APPL_TRACE_ERROR("unexpected operation, ignored");
1499 else if (op >= GATTC_OPTYPE_READ)
1501 if (p_clcb->p_q_cmd == NULL)
1503 APPL_TRACE_ERROR("No pending command");
1506 if (p_clcb->p_q_cmd->hdr.event != bta_gattc_opcode_to_int_evt[op - GATTC_OPTYPE_READ])
1508 mapped_op = p_clcb->p_q_cmd->hdr.event - BTA_GATTC_API_READ_EVT + GATTC_OPTYPE_READ;
1509 if ( mapped_op > GATTC_OPTYPE_INDICATION) mapped_op = 0;
1511 #if (BT_TRACE_VERBOSE == TRUE)
1512 APPL_TRACE_ERROR("expect op:(%s :0x%04x), receive unexpected operation (%s).",
1513 bta_gattc_op_code_name[mapped_op] , p_clcb->p_q_cmd->hdr.event,
1514 bta_gattc_op_code_name[op]);
1516 APPL_TRACE_ERROR("expect op:(%u :0x%04x), receive unexpected operation (%u).",
1517 mapped_op , p_clcb->p_q_cmd->hdr.event, op);
1522 /* discard responses if service change indication is received before operation completed */
1523 if (p_clcb->auto_update == BTA_GATTC_DISC_WAITING && p_clcb->p_srcb->srvc_hdl_chg)
1525 APPL_TRACE_DEBUG("Discard all responses when service change indication is received.");
1526 p_data->op_cmpl.status = GATT_ERROR;
1529 /* service handle change void the response, discard it */
1530 if (op == GATTC_OPTYPE_READ)
1531 bta_gattc_read_cmpl(p_clcb, &p_data->op_cmpl);
1533 else if (op == GATTC_OPTYPE_WRITE)
1534 bta_gattc_write_cmpl(p_clcb, &p_data->op_cmpl);
1536 else if (op == GATTC_OPTYPE_EXE_WRITE)
1537 bta_gattc_exec_cmpl(p_clcb, &p_data->op_cmpl);
1539 else if (op == GATTC_OPTYPE_CONFIG)
1540 bta_gattc_cfg_mtu_cmpl(p_clcb, &p_data->op_cmpl);
1542 if (p_clcb->auto_update == BTA_GATTC_DISC_WAITING)
1544 p_clcb->auto_update = BTA_GATTC_REQ_WAITING;
1545 bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_DISCOVER_EVT, NULL);
1549 /*******************************************************************************
1551 ** Function bta_gattc_op_cmpl
1553 ** Description operation completed.
1557 *******************************************************************************/
1558 void bta_gattc_ignore_op_cmpl(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1562 /* receive op complete when discovery is started, ignore the response,
1563 and wait for discovery finish and resent */
1564 APPL_TRACE_DEBUG("bta_gattc_ignore_op_cmpl op = %d", p_data->hdr.layer_specific);
1567 /*******************************************************************************
1569 ** Function bta_gattc_search
1571 ** Description start a search in the local server cache
1575 *******************************************************************************/
1576 void bta_gattc_search(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1578 tBTA_GATT_STATUS status = GATT_INTERNAL_ERROR;
1580 APPL_TRACE_DEBUG("bta_gattc_search conn_id=%d",p_clcb->bta_conn_id);
1581 if (p_clcb->p_srcb && p_clcb->p_srcb->p_srvc_cache)
1583 status = BTA_GATT_OK;
1584 /* search the local cache of a server device */
1585 bta_gattc_search_service(p_clcb, p_data->api_search.p_srvc_uuid);
1587 cb_data.search_cmpl.status = status;
1588 cb_data.search_cmpl.conn_id = p_clcb->bta_conn_id;
1590 /* end of search or no server cache available */
1591 ( *p_clcb->p_rcb->p_cback)(BTA_GATTC_SEARCH_CMPL_EVT, &cb_data);
1593 /*******************************************************************************
1595 ** Function bta_gattc_q_cmd
1597 ** Description enqueue a command into control block, usually because discovery
1598 ** operation is busy.
1602 *******************************************************************************/
1603 void bta_gattc_q_cmd(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1605 bta_gattc_enqueue(p_clcb, p_data);
1607 /*******************************************************************************
1609 ** Function bta_gattc_cache_open
1611 ** Description open a NV cache for loading
1615 *******************************************************************************/
1616 void bta_gattc_cache_open(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1620 bta_gattc_set_discover_st(p_clcb->p_srcb);
1622 APPL_TRACE_DEBUG("bta_gattc_cache_open conn_id=%d",p_clcb->bta_conn_id);
1623 bta_gattc_co_cache_open(p_clcb->p_srcb->server_bda, BTA_GATTC_CI_CACHE_OPEN_EVT,
1624 p_clcb->bta_conn_id, FALSE);
1626 /*******************************************************************************
1628 ** Function bta_gattc_start_load
1630 ** Description start cache loading by sending callout open cache
1634 *******************************************************************************/
1635 void bta_gattc_ci_open(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1637 APPL_TRACE_DEBUG("bta_gattc_ci_open conn_id=%d server state=%d" ,
1638 p_clcb->bta_conn_id, p_clcb->p_srcb->state);
1639 if (p_clcb->p_srcb->state == BTA_GATTC_SERV_LOAD)
1641 if (p_data->ci_open.status == BTA_GATT_OK)
1643 p_clcb->p_srcb->attr_index = 0;
1644 bta_gattc_co_cache_load(p_clcb->p_srcb->server_bda,
1645 BTA_GATTC_CI_CACHE_LOAD_EVT,
1646 p_clcb->p_srcb->attr_index,
1647 p_clcb->bta_conn_id);
1651 p_clcb->p_srcb->state = BTA_GATTC_SERV_DISC;
1652 /* cache open failure, start discovery */
1653 bta_gattc_start_discover(p_clcb, NULL);
1656 if (p_clcb->p_srcb->state == BTA_GATTC_SERV_SAVE)
1658 if (p_data->ci_open.status == BTA_GATT_OK)
1660 if (!bta_gattc_cache_save(p_clcb->p_srcb, p_clcb->bta_conn_id))
1662 p_data->ci_open.status = BTA_GATT_ERROR;
1665 if (p_data->ci_open.status != BTA_GATT_OK)
1667 p_clcb->p_srcb->attr_index = 0;
1668 bta_gattc_co_cache_close(p_clcb->p_srcb->server_bda, p_clcb->bta_conn_id);
1669 bta_gattc_reset_discover_st(p_clcb->p_srcb, p_clcb->status);
1674 /*******************************************************************************
1676 ** Function bta_gattc_ci_load
1678 ** Description cache loading received.
1682 *******************************************************************************/
1683 void bta_gattc_ci_load(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1686 APPL_TRACE_DEBUG("bta_gattc_ci_load conn_id=%d load status=%d",
1687 p_clcb->bta_conn_id, p_data->ci_load.status);
1689 if (p_data->ci_load.status == BTA_GATT_OK ||
1690 p_data->ci_load.status == BTA_GATT_MORE)
1692 if (p_data->ci_load.num_attr != 0)
1693 bta_gattc_rebuild_cache(p_clcb->p_srcb, p_data->ci_load.num_attr,
1694 p_data->ci_load.attr, p_clcb->p_srcb->attr_index);
1696 if (p_data->ci_load.status == BTA_GATT_OK)
1698 p_clcb->p_srcb->attr_index = 0;
1699 bta_gattc_reset_discover_st(p_clcb->p_srcb, BTA_GATT_OK);
1700 bta_gattc_co_cache_close(p_clcb->p_srcb->server_bda, 0);
1702 else /* load more */
1704 p_clcb->p_srcb->attr_index += p_data->ci_load.num_attr;
1706 bta_gattc_co_cache_load(p_clcb->p_srcb->server_bda,
1707 BTA_GATTC_CI_CACHE_LOAD_EVT,
1708 p_clcb->p_srcb->attr_index,
1709 p_clcb->bta_conn_id);
1714 bta_gattc_co_cache_close(p_clcb->p_srcb->server_bda, 0);
1715 p_clcb->p_srcb->state = BTA_GATTC_SERV_DISC;
1716 p_clcb->p_srcb->attr_index = 0;
1717 /* cache load failure, start discovery */
1718 bta_gattc_start_discover(p_clcb, NULL);
1721 /*******************************************************************************
1723 ** Function bta_gattc_ci_save
1725 ** Description cache loading received.
1729 *******************************************************************************/
1730 void bta_gattc_ci_save(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1734 APPL_TRACE_DEBUG("bta_gattc_ci_save conn_id=%d " ,
1735 p_clcb->bta_conn_id );
1737 if (!bta_gattc_cache_save(p_clcb->p_srcb, p_clcb->bta_conn_id))
1739 p_clcb->p_srcb->attr_index = 0;
1740 bta_gattc_co_cache_close(p_clcb->p_srcb->server_bda, 0);
1741 bta_gattc_reset_discover_st(p_clcb->p_srcb, p_clcb->status);
1744 /*******************************************************************************
1746 ** Function bta_gattc_fail
1748 ** Description report API call failure back to apps
1752 *******************************************************************************/
1753 void bta_gattc_fail(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1757 if (p_clcb->status == BTA_GATT_OK)
1759 APPL_TRACE_ERROR("operation not supported at current state [%d]", p_clcb->state);
1763 /*******************************************************************************
1765 ** Function bta_gattc_deregister_cmpl
1767 ** Description De-Register a GATT client application with BTA completed.
1771 *******************************************************************************/
1772 static void bta_gattc_deregister_cmpl(tBTA_GATTC_RCB *p_clreg)
1774 tBTA_GATTC_CB *p_cb = &bta_gattc_cb;
1775 tBTA_GATTC_IF client_if = p_clreg->client_if;
1777 tBTA_GATTC_CBACK *p_cback = p_clreg->p_cback;
1779 memset(&cb_data, 0, sizeof(tBTA_GATTC));
1781 GATT_Deregister(p_clreg->client_if);
1782 memset(p_clreg, 0, sizeof(tBTA_GATTC_RCB));
1784 cb_data.reg_oper.client_if = client_if;
1785 cb_data.reg_oper.status = BTA_GATT_OK;
1788 /* callback with de-register event */
1789 (*p_cback)(BTA_GATTC_DEREG_EVT, (tBTA_GATTC *)&cb_data);
1791 if (bta_gattc_num_reg_app() == 0 && p_cb->state == BTA_GATTC_STATE_DISABLING)
1793 p_cb->state = BTA_GATTC_STATE_DISABLED;
1796 /*******************************************************************************
1798 ** Function bta_gattc_conn_cback
1799 ** bta_gattc_cmpl_cback
1801 ** Description callback functions to GATT client stack.
1805 *******************************************************************************/
1806 static void bta_gattc_conn_cback(tGATT_IF gattc_if, BD_ADDR bda, UINT16 conn_id,
1807 BOOLEAN connected, tGATT_DISCONN_REASON reason,
1808 tBT_TRANSPORT transport)
1810 tBTA_GATTC_DATA *p_buf;
1814 APPL_TRACE_WARNING("%s() - cif=%d connected=%d conn_id=%d reason=0x%04x",
1815 __FUNCTION__, gattc_if, connected, conn_id, reason);
1819 bdcpy(bdaddr.address, bda);
1821 btif_debug_conn_state(bdaddr, BTIF_DEBUG_CONNECTED, GATT_CONN_UNKNOWN);
1823 btif_debug_conn_state(bdaddr, BTIF_DEBUG_DISCONNECTED, reason);
1825 if ((p_buf = (tBTA_GATTC_DATA *) GKI_getbuf(sizeof(tBTA_GATTC_DATA))) != NULL)
1827 memset(p_buf, 0, sizeof(tBTA_GATTC_DATA));
1829 p_buf->int_conn.hdr.event = connected ? BTA_GATTC_INT_CONN_EVT:
1830 BTA_GATTC_INT_DISCONN_EVT;
1831 p_buf->int_conn.hdr.layer_specific = conn_id;
1832 p_buf->int_conn.client_if = gattc_if;
1833 p_buf->int_conn.role = L2CA_GetBleConnRole(bda);
1834 p_buf->int_conn.reason = reason;
1835 p_buf->int_conn.transport = transport;
1836 bdcpy(p_buf->int_conn.remote_bda, bda);
1838 bta_sys_sendmsg(p_buf);
1842 /*******************************************************************************
1844 ** Function bta_gattc_enc_cmpl_cback
1846 ** Description encryption complete callback function to GATT client stack.
1850 *******************************************************************************/
1851 static void bta_gattc_enc_cmpl_cback(tGATT_IF gattc_if, BD_ADDR bda)
1853 tBTA_GATTC_DATA *p_buf;
1854 tBTA_GATTC_CLCB *p_clcb = NULL;
1856 if ((p_clcb = bta_gattc_find_clcb_by_cif(gattc_if, bda, BTA_GATT_TRANSPORT_LE)) == NULL)
1861 #if (defined BTA_HH_LE_INCLUDED && BTA_HH_LE_INCLUDED == TRUE)
1862 /* filter this event just for BTA HH LE GATT client,
1863 In the future, if we want to enable encryption complete event
1864 for all GATT clients, we can remove this code */
1865 if (!bta_hh_le_is_hh_gatt_if(gattc_if))
1871 APPL_TRACE_DEBUG("bta_gattc_enc_cmpl_cback: cif = %d", gattc_if);
1873 if ((p_buf = (tBTA_GATTC_DATA *) GKI_getbuf(sizeof(tBTA_GATTC_DATA))) != NULL)
1875 memset(p_buf, 0, sizeof(tBTA_GATTC_DATA));
1877 p_buf->enc_cmpl.hdr.event = BTA_GATTC_ENC_CMPL_EVT;
1878 p_buf->enc_cmpl.hdr.layer_specific = p_clcb->bta_conn_id;
1879 p_buf->enc_cmpl.client_if = gattc_if;
1880 bdcpy(p_buf->enc_cmpl.remote_bda, bda);
1882 bta_sys_sendmsg(p_buf);
1886 /*******************************************************************************
1888 ** Function bta_gattc_process_api_refresh
1890 ** Description process refresh API to delete cache and start a new discovery
1891 ** if currently connected.
1895 *******************************************************************************/
1896 void bta_gattc_process_api_refresh(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA * p_msg)
1898 tBTA_GATTC_SERV *p_srvc_cb = bta_gattc_find_srvr_cache(p_msg->api_conn.remote_bda);
1899 tBTA_GATTC_CLCB *p_clcb = &bta_gattc_cb.clcb[0];
1900 BOOLEAN found = FALSE;
1904 if (p_srvc_cb != NULL)
1906 /* try to find a CLCB */
1907 if (p_srvc_cb->connected && p_srvc_cb->num_clcb != 0)
1909 for (i = 0; i < BTA_GATTC_CLCB_MAX; i ++, p_clcb ++)
1911 if (p_clcb->in_use && p_clcb->p_srcb == p_srvc_cb)
1919 bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_DISCOVER_EVT, NULL);
1923 /* in all other cases, mark it and delete the cache */
1924 if (p_srvc_cb->p_srvc_cache != NULL)
1926 while (!GKI_queue_is_empty(&p_srvc_cb->cache_buffer))
1927 GKI_freebuf (GKI_dequeue (&p_srvc_cb->cache_buffer));
1929 p_srvc_cb->p_srvc_cache = NULL;
1932 /* used to reset cache in application */
1933 bta_gattc_co_cache_reset(p_msg->api_conn.remote_bda);
1936 /*******************************************************************************
1938 ** Function bta_gattc_process_srvc_chg_ind
1940 ** Description process service change indication.
1944 *******************************************************************************/
1945 BOOLEAN bta_gattc_process_srvc_chg_ind(UINT16 conn_id,
1946 tBTA_GATTC_RCB *p_clrcb,
1947 tBTA_GATTC_SERV *p_srcb,
1948 tBTA_GATTC_CLCB *p_clcb,
1949 tBTA_GATTC_NOTIFY *p_notify,
1952 tBT_UUID gattp_uuid, srvc_chg_uuid;
1953 BOOLEAN processed = FALSE;
1957 gattp_uuid.uu.uuid16 = UUID_SERVCLASS_GATT_SERVER;
1959 srvc_chg_uuid.len = 2;
1960 srvc_chg_uuid.uu.uuid16 = GATT_UUID_GATT_SRV_CHGD;
1962 if (bta_gattc_uuid_compare(&p_notify->char_id.srvc_id.id.uuid, &gattp_uuid, TRUE) &&
1963 bta_gattc_uuid_compare(&p_notify->char_id.char_id.uuid, &srvc_chg_uuid, TRUE))
1966 /* mark service handle change pending */
1967 p_srcb->srvc_hdl_chg = TRUE;
1968 /* clear up all notification/indication registration */
1969 bta_gattc_clear_notif_registration(conn_id);
1970 /* service change indication all received, do discovery update */
1971 if ( ++ p_srcb->update_count == bta_gattc_num_reg_app())
1973 /* not an opened connection; or connection busy */
1974 /* search for first available clcb and start discovery */
1975 if (p_clcb == NULL || (p_clcb && p_clcb->p_q_cmd != NULL))
1977 for (i = 0 ; i < BTA_GATTC_CLCB_MAX; i ++)
1979 if (bta_gattc_cb.clcb[i].in_use &&
1980 bta_gattc_cb.clcb[i].p_srcb == p_srcb &&
1981 bta_gattc_cb.clcb[i].p_q_cmd == NULL)
1983 p_clcb = &bta_gattc_cb.clcb[i];
1988 /* send confirmation here if this is an indication, it should always be */
1989 GATTC_SendHandleValueConfirm(conn_id, handle);
1991 /* if connection available, refresh cache by doing discovery now */
1993 bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_DISCOVER_EVT, NULL);
1995 /* notify applicationf or service change */
1996 if (p_clrcb->p_cback != NULL)
1998 (* p_clrcb->p_cback)(BTA_GATTC_SRVC_CHG_EVT, (tBTA_GATTC *)p_srcb->server_bda);
2006 /*******************************************************************************
2008 ** Function bta_gattc_proc_other_indication
2010 ** Description process all non-service change indication/notification.
2014 *******************************************************************************/
2015 void bta_gattc_proc_other_indication(tBTA_GATTC_CLCB *p_clcb, UINT8 op,
2016 tGATT_CL_COMPLETE *p_data,
2017 tBTA_GATTC_NOTIFY *p_notify)
2019 APPL_TRACE_DEBUG("bta_gattc_proc_other_indication check \
2020 p_data->att_value.handle=%d p_data->handle=%d",
2021 p_data->att_value.handle, p_data->handle);
2022 APPL_TRACE_DEBUG("is_notify", p_notify->is_notify);
2024 p_notify->is_notify = (op == GATTC_OPTYPE_INDICATION) ? FALSE : TRUE;
2025 p_notify->len = p_data->att_value.len;
2026 bdcpy(p_notify->bda, p_clcb->bda);
2027 memcpy(p_notify->value, p_data->att_value.value, p_data->att_value.len);
2028 p_notify->conn_id = p_clcb->bta_conn_id;
2030 if (p_clcb->p_rcb->p_cback)
2031 (*p_clcb->p_rcb->p_cback)(BTA_GATTC_NOTIF_EVT, (tBTA_GATTC *)p_notify);
2034 /*******************************************************************************
2036 ** Function bta_gattc_process_indicate
2038 ** Description process indication/notification.
2042 *******************************************************************************/
2043 void bta_gattc_process_indicate(UINT16 conn_id, tGATTC_OPTYPE op, tGATT_CL_COMPLETE *p_data)
2045 UINT16 handle = p_data->att_value.handle;
2046 tBTA_GATTC_CLCB *p_clcb ;
2047 tBTA_GATTC_RCB *p_clrcb = NULL;
2048 tBTA_GATTC_SERV *p_srcb = NULL;
2049 tBTA_GATTC_NOTIFY notify;
2051 tBTA_GATTC_IF gatt_if;
2052 tBTA_TRANSPORT transport;
2054 if (!GATT_GetConnectionInfor(conn_id, &gatt_if, remote_bda, &transport))
2056 APPL_TRACE_ERROR("%s indication/notif for unknown app", __func__);
2057 if (op == GATTC_OPTYPE_INDICATION)
2058 GATTC_SendHandleValueConfirm(conn_id, handle);
2062 if ((p_clrcb = bta_gattc_cl_get_regcb(gatt_if)) == NULL)
2064 APPL_TRACE_ERROR("%s indication/notif for unregistered app", __func__);
2065 if (op == GATTC_OPTYPE_INDICATION)
2066 GATTC_SendHandleValueConfirm(conn_id, handle);
2070 if ((p_srcb = bta_gattc_find_srcb(remote_bda)) == NULL)
2072 APPL_TRACE_ERROR("%s indication/notif for unknown device, ignore", __func__);
2073 if (op == GATTC_OPTYPE_INDICATION)
2074 GATTC_SendHandleValueConfirm(conn_id, handle);
2078 p_clcb = bta_gattc_find_clcb_by_conn_id(conn_id);
2080 if (bta_gattc_handle2id(p_srcb, handle,
2081 ¬ify.char_id.srvc_id,
2082 ¬ify.char_id.char_id,
2083 ¬ify.descr_type))
2085 /* if non-service change indication/notification, forward to application */
2086 if (!bta_gattc_process_srvc_chg_ind(conn_id, p_clrcb, p_srcb, p_clcb, ¬ify, handle))
2088 /* if app registered for the notification */
2089 if (bta_gattc_check_notif_registry(p_clrcb, p_srcb, ¬ify))
2091 /* connection not open yet */
2094 if ((p_clcb = bta_gattc_clcb_alloc(gatt_if, remote_bda, transport)) != NULL)
2096 p_clcb->bta_conn_id = conn_id;
2097 p_clcb->transport = transport;
2099 bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_CONN_EVT, NULL);
2103 APPL_TRACE_ERROR("No resources");
2108 bta_gattc_proc_other_indication(p_clcb, op, p_data, ¬ify);
2110 /* no one intersted and need ack? */
2111 else if (op == GATTC_OPTYPE_INDICATION)
2113 APPL_TRACE_DEBUG("%s no one interested, ack now", __func__);
2114 GATTC_SendHandleValueConfirm(conn_id, handle);
2120 APPL_TRACE_ERROR("%s Indi/Notif for Unknown handle[0x%04x], can not find in local cache.",
2122 if (op == GATTC_OPTYPE_INDICATION)
2123 GATTC_SendHandleValueConfirm(conn_id, handle);
2126 /*******************************************************************************
2128 ** Function bta_gattc_cmpl_cback
2130 ** Description client operation complete callback register with BTE GATT.
2134 *******************************************************************************/
2135 static void bta_gattc_cmpl_cback(UINT16 conn_id, tGATTC_OPTYPE op, tGATT_STATUS status,
2136 tGATT_CL_COMPLETE *p_data)
2138 tBTA_GATTC_CLCB *p_clcb ;
2139 tBTA_GATTC_OP_CMPL *p_buf;
2140 UINT16 len = sizeof(tBTA_GATTC_OP_CMPL) + sizeof(tGATT_CL_COMPLETE);
2142 APPL_TRACE_DEBUG("bta_gattc_cmpl_cback: conn_id = %d op = %d status = %d",
2143 conn_id, op, status);
2145 /* notification and indication processed right away */
2146 if (op == GATTC_OPTYPE_NOTIFICATION || op == GATTC_OPTYPE_INDICATION)
2148 bta_gattc_process_indicate(conn_id, op, p_data);
2151 /* for all other operation, not expected if w/o connection */
2152 else if ((p_clcb = bta_gattc_find_clcb_by_conn_id(conn_id)) == NULL)
2154 APPL_TRACE_ERROR("bta_gattc_cmpl_cback unknown conn_id = %d, ignore data", conn_id);
2158 /* if over BR_EDR, inform PM for mode change */
2159 if (p_clcb->transport == BTA_TRANSPORT_BR_EDR)
2161 bta_sys_busy(BTA_ID_GATTC, BTA_ALL_APP_ID, p_clcb->bda);
2162 bta_sys_idle(BTA_ID_GATTC, BTA_ALL_APP_ID, p_clcb->bda);
2165 if ((p_buf = (tBTA_GATTC_OP_CMPL *) GKI_getbuf(len)) != NULL)
2167 memset(p_buf, 0, len);
2168 p_buf->hdr.event = BTA_GATTC_OP_CMPL_EVT;
2169 p_buf->hdr.layer_specific = conn_id;
2170 p_buf->status = status;
2171 p_buf->op_code = op;
2175 p_buf->p_cmpl = (tGATT_CL_COMPLETE *)(p_buf + 1);
2176 memcpy(p_buf->p_cmpl, p_data, sizeof(tGATT_CL_COMPLETE));
2179 bta_sys_sendmsg(p_buf);
2185 /*******************************************************************************
2187 ** Function bta_gattc_cong_cback
2189 ** Description congestion callback for BTA GATT client.
2193 ********************************************************************************/
2194 static void bta_gattc_cong_cback (UINT16 conn_id, BOOLEAN congested)
2196 tBTA_GATTC_CLCB *p_clcb;
2199 if ((p_clcb = bta_gattc_find_clcb_by_conn_id(conn_id)) != NULL)
2201 if (p_clcb->p_rcb->p_cback)
2203 cb_data.congest.conn_id = conn_id;
2204 cb_data.congest.congested = congested;
2206 (*p_clcb->p_rcb->p_cback)(BTA_GATTC_CONGEST_EVT, &cb_data);
2211 #if BLE_INCLUDED == TRUE
2212 /*******************************************************************************
2214 ** Function bta_gattc_init_clcb_conn
2216 ** Description Initaite a BTA CLCB connection
2220 ********************************************************************************/
2221 void bta_gattc_init_clcb_conn(UINT8 cif, BD_ADDR remote_bda)
2223 tBTA_GATTC_CLCB *p_clcb = NULL;
2224 tBTA_GATTC_DATA gattc_data;
2227 /* should always get the connection ID */
2228 if (GATT_GetConnIdIfConnected(cif, remote_bda, &conn_id, BTA_GATT_TRANSPORT_LE) == FALSE)
2230 APPL_TRACE_ERROR("bta_gattc_init_clcb_conn ERROR: not a connected device");
2234 /* initaite a new connection here */
2235 if ((p_clcb = bta_gattc_clcb_alloc(cif, remote_bda, BTA_GATT_TRANSPORT_LE)) != NULL)
2237 gattc_data.hdr.layer_specific = p_clcb->bta_conn_id = conn_id;
2239 gattc_data.api_conn.client_if = cif;
2240 memcpy(gattc_data.api_conn.remote_bda, remote_bda, BD_ADDR_LEN);
2241 gattc_data.api_conn.is_direct = TRUE;
2243 bta_gattc_sm_execute(p_clcb, BTA_GATTC_API_OPEN_EVT, &gattc_data);
2247 APPL_TRACE_ERROR("No resources");
2250 /*******************************************************************************
2252 ** Function bta_gattc_process_listen_all
2254 ** Description process listen all, send open callback to application for all
2255 ** connected slave LE link.
2259 ********************************************************************************/
2260 void bta_gattc_process_listen_all(UINT8 cif)
2263 tBTA_GATTC_CONN *p_conn = &bta_gattc_cb.conn_track[0];
2265 for (i_conn = 0; i_conn < BTA_GATTC_CONN_MAX; i_conn++, p_conn ++)
2267 if (p_conn->in_use )
2269 if (bta_gattc_find_clcb_by_cif(cif, p_conn->remote_bda, BTA_GATT_TRANSPORT_LE) == NULL)
2271 bta_gattc_init_clcb_conn(cif, p_conn->remote_bda);
2273 /* else already connected */
2277 /*******************************************************************************
2279 ** Function bta_gattc_listen
2281 ** Description Start or stop a listen for connection
2285 ********************************************************************************/
2286 void bta_gattc_listen(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA * p_msg)
2288 tBTA_GATTC_RCB *p_clreg = bta_gattc_cl_get_regcb(p_msg->api_listen.client_if);
2292 cb_data.reg_oper.status = BTA_GATT_ERROR;
2293 cb_data.reg_oper.client_if = p_msg->api_listen.client_if;
2295 if (p_clreg == NULL)
2297 APPL_TRACE_ERROR("bta_gattc_listen failed, unknown client_if: %d",
2298 p_msg->api_listen.client_if);
2301 /* mark bg conn record */
2302 if (bta_gattc_mark_bg_conn(p_msg->api_listen.client_if,
2303 (BD_ADDR_PTR) p_msg->api_listen.remote_bda,
2304 p_msg->api_listen.start,
2307 if (!GATT_Listen(p_msg->api_listen.client_if,
2308 p_msg->api_listen.start,
2309 p_msg->api_listen.remote_bda))
2311 APPL_TRACE_ERROR("Listen failure");
2312 (*p_clreg->p_cback)(BTA_GATTC_LISTEN_EVT, &cb_data);
2316 cb_data.status = BTA_GATT_OK;
2318 (*p_clreg->p_cback)(BTA_GATTC_LISTEN_EVT, &cb_data);
2320 if (p_msg->api_listen.start)
2322 /* if listen to a specific target */
2323 if (p_msg->api_listen.remote_bda != NULL)
2326 /* if is a connected remote device */
2327 if (L2CA_GetBleConnRole(p_msg->api_listen.remote_bda) == HCI_ROLE_SLAVE &&
2328 bta_gattc_find_clcb_by_cif(p_msg->api_listen.client_if,
2329 p_msg->api_listen.remote_bda,
2330 BTA_GATT_TRANSPORT_LE) == NULL)
2333 bta_gattc_init_clcb_conn(p_msg->api_listen.client_if,
2334 p_msg->api_listen.remote_bda);
2337 /* if listen to all */
2340 LOG_DEBUG(LOG_TAG, "Listen For All now");
2341 /* go through all connected device and send
2342 callback for all connected slave connection */
2343 bta_gattc_process_listen_all(p_msg->api_listen.client_if);
2350 /*******************************************************************************
2352 ** Function bta_gattc_broadcast
2354 ** Description Start or stop broadcasting
2358 ********************************************************************************/
2359 void bta_gattc_broadcast(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA * p_msg)
2361 tBTA_GATTC_RCB *p_clreg = bta_gattc_cl_get_regcb(p_msg->api_listen.client_if);
2365 cb_data.reg_oper.client_if = p_msg->api_listen.client_if;
2366 cb_data.reg_oper.status = BTM_BleBroadcast(p_msg->api_listen.start);
2368 if (p_clreg && p_clreg->p_cback)
2369 (*p_clreg->p_cback)(BTA_GATTC_LISTEN_EVT, &cb_data);