]> granicus.if.org Git - esp-idf/blob - components/bt/bluedroid/bta/gatt/bta_gattc_act.c
component/bt: fix the issue where no gatt server request callback warning caused...
[esp-idf] / components / bt / bluedroid / bta / gatt / bta_gattc_act.c
1 /******************************************************************************
2  *
3  *  Copyright (C) 2003-2012 Broadcom Corporation
4  *
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:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  ******************************************************************************/
18
19 /******************************************************************************
20  *
21  *  This file contains the GATT client action functions for the state
22  *  machine.
23  *
24  ******************************************************************************/
25 #define LOG_TAG "bt_bta_gattc"
26
27 #include "common/bt_target.h"
28
29 #include "bta/utl.h"
30 #include "bta/bta_sys.h"
31
32 #include "bta_gattc_int.h"
33 #include "stack/l2c_api.h"
34 #include "l2c_int.h"
35 #include "gatt_int.h"
36 #include "osi/allocator.h"
37 #include "osi/mutex.h"
38
39 #if (defined BTA_HH_LE_INCLUDED && BTA_HH_LE_INCLUDED == TRUE)
40 #include "bta_hh_int.h"
41 #endif
42
43 // #include "btif/include/btif_debug_conn.h"
44
45 #include <string.h>
46
47 // #include "osi/include/log.h"
48
49 #if GATTC_INCLUDED == TRUE && BLE_INCLUDED == TRUE
50
51 /*****************************************************************************
52 **  Constants
53 *****************************************************************************/
54 static void bta_gattc_conn_cback(tGATT_IF gattc_if, BD_ADDR bda, UINT16 conn_id,
55                                  BOOLEAN connected, tGATT_DISCONN_REASON reason,
56                                  tBT_TRANSPORT transport);
57
58 static void  bta_gattc_cmpl_cback(UINT16 conn_id, tGATTC_OPTYPE op, tGATT_STATUS status,
59                                   tGATT_CL_COMPLETE *p_data);
60 static void bta_gattc_cmpl_sendmsg(UINT16 conn_id, tGATTC_OPTYPE op,
61                                    tBTA_GATT_STATUS status,
62                                    tGATT_CL_COMPLETE *p_data);
63 static void bta_gattc_pop_command_to_send(tBTA_GATTC_CLCB *p_clcb);
64
65 static void bta_gattc_deregister_cmpl(tBTA_GATTC_RCB *p_clreg);
66 static void bta_gattc_enc_cmpl_cback(tGATT_IF gattc_if, BD_ADDR bda);
67 static void bta_gattc_cong_cback (UINT16 conn_id, BOOLEAN congested);
68 static void bta_gattc_req_cback (UINT16 conn_id, UINT32 trans_id, tGATTS_REQ_TYPE type, tGATTS_DATA *p_data);
69 static tBTA_GATTC_FIND_SERVICE_CB bta_gattc_register_service_change_notify(UINT16 conn_id, BD_ADDR remote_bda);
70
71 static tGATT_CBACK bta_gattc_cl_cback = {
72     bta_gattc_conn_cback,
73     bta_gattc_cmpl_cback,
74     bta_gattc_disc_res_cback,
75     bta_gattc_disc_cmpl_cback,
76     bta_gattc_req_cback,
77     bta_gattc_enc_cmpl_cback,
78     bta_gattc_cong_cback
79 };
80
81 /* opcode(tGATTC_OPTYPE) order has to be comply with internal event order */
82 static UINT16 bta_gattc_opcode_to_int_evt[] = {
83     BTA_GATTC_API_READ_EVT,
84     BTA_GATTC_API_WRITE_EVT,
85     BTA_GATTC_API_EXEC_EVT,
86     BTA_GATTC_API_CFG_MTU_EVT,
87     BTA_GATTC_API_READ_MULTI_EVT
88 };
89
90 #if (BT_TRACE_VERBOSE == TRUE)
91 static const char *bta_gattc_op_code_name[] = {
92     "Unknown",
93     "Discovery",
94     "Read",
95     "Write",
96     "Exec",
97     "Config",
98     "Notification",
99     "Indication"
100 };
101 #endif
102 /*****************************************************************************
103 **  Action Functions
104 *****************************************************************************/
105
106
107 void bta_gattc_reset_discover_st(tBTA_GATTC_SERV *p_srcb, tBTA_GATT_STATUS status);
108
109 /*******************************************************************************
110 **
111 ** Function         bta_gattc_enable
112 **
113 ** Description      Enables GATTC module
114 **
115 **
116 ** Returns          void
117 **
118 *******************************************************************************/
119 static void bta_gattc_enable(tBTA_GATTC_CB *p_cb)
120 {
121     APPL_TRACE_DEBUG("bta_gattc_enable");
122
123     if (p_cb->state == BTA_GATTC_STATE_DISABLED) {
124         /* initialize control block */
125         memset(&bta_gattc_cb, 0, sizeof(tBTA_GATTC_CB));
126         p_cb->state = BTA_GATTC_STATE_ENABLED;
127     } else {
128         APPL_TRACE_DEBUG("GATTC is already enabled");
129     }
130 }
131
132
133 /*******************************************************************************
134 **
135 ** Function         bta_gattc_disable
136 **
137 ** Description      Disable GATTC module by cleaning up all active connections
138 **                  and deregister all application.
139 **
140 ** Returns          void
141 **
142 *******************************************************************************/
143 void bta_gattc_disable(tBTA_GATTC_CB *p_cb)
144 {
145     UINT8           i;
146
147     APPL_TRACE_DEBUG("bta_gattc_disable");
148
149     if (p_cb->state != BTA_GATTC_STATE_ENABLED) {
150         APPL_TRACE_ERROR("not enabled or disable in pogress");
151         return;
152     }
153
154     for (i = 0; i < BTA_GATTC_CL_MAX; i ++) {
155         if (p_cb->cl_rcb[i].in_use) {
156             p_cb->state = BTA_GATTC_STATE_DISABLING;
157             /* don't deregister HH GATT IF */
158             /* HH GATT IF will be deregistered by bta_hh_le_deregister when disable HH */
159 #if (defined BTA_HH_LE_INCLUDED && BTA_HH_LE_INCLUDED == TRUE)
160             if (!bta_hh_le_is_hh_gatt_if(p_cb->cl_rcb[i].client_if)) {
161 #endif
162                 bta_gattc_deregister(p_cb, &p_cb->cl_rcb[i]);
163 #if (defined BTA_HH_LE_INCLUDED && BTA_HH_LE_INCLUDED == TRUE)
164             }
165 #endif
166         }
167     }
168
169     /* no registered apps, indicate disable completed */
170     if (p_cb->state != BTA_GATTC_STATE_DISABLING) {
171         p_cb->state = BTA_GATTC_STATE_DISABLED;
172         memset(p_cb, 0, sizeof(tBTA_GATTC_CB));
173     }
174 }
175
176 /*******************************************************************************
177 **
178 ** Function         bta_gattc_register
179 **
180 ** Description      Register a GATT client application with BTA.
181 **
182 ** Returns          void
183 **
184 *******************************************************************************/
185 void bta_gattc_register(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA *p_data)
186 {
187     tBTA_GATTC               cb_data;
188     UINT8                    i;
189     tBT_UUID                 *p_app_uuid = &p_data->api_reg.app_uuid;
190     tBTA_GATTC_INT_START_IF  *p_buf;
191     tBTA_GATT_STATUS         status = BTA_GATT_NO_RESOURCES;
192
193
194     APPL_TRACE_DEBUG("bta_gattc_register state %d\n", p_cb->state);
195     memset(&cb_data, 0, sizeof(cb_data));
196     cb_data.reg_oper.status = BTA_GATT_NO_RESOURCES;
197
198     /* check if  GATTC module is already enabled . Else enable */
199     if (p_cb->state == BTA_GATTC_STATE_DISABLED) {
200         bta_gattc_enable (p_cb);
201     }
202     /* todo need to check duplicate uuid */
203     for (i = 0; i < BTA_GATTC_CL_MAX; i ++) {
204         if (!p_cb->cl_rcb[i].in_use) {
205             if ((p_app_uuid == NULL) || (p_cb->cl_rcb[i].client_if = GATT_Register(p_app_uuid, &bta_gattc_cl_cback)) == 0) {
206                 APPL_TRACE_ERROR("Register with GATT stack failed.\n");
207                 status = BTA_GATT_ERROR;
208             } else {
209                 p_cb->cl_rcb[i].in_use = TRUE;
210                 p_cb->cl_rcb[i].p_cback = p_data->api_reg.p_cback;
211                 memcpy(&p_cb->cl_rcb[i].app_uuid, p_app_uuid, sizeof(tBT_UUID));
212
213                 /* BTA use the same client interface as BTE GATT statck */
214                 cb_data.reg_oper.client_if = p_cb->cl_rcb[i].client_if;
215
216                 if ((p_buf = (tBTA_GATTC_INT_START_IF *) osi_malloc(sizeof(tBTA_GATTC_INT_START_IF))) != NULL) {
217                     p_buf->hdr.event    = BTA_GATTC_INT_START_IF_EVT;
218                     p_buf->client_if    = p_cb->cl_rcb[i].client_if;
219                     APPL_TRACE_DEBUG("GATTC getbuf sucess.\n");
220                     bta_sys_sendmsg(p_buf);
221                     status = BTA_GATT_OK;
222                 } else {
223                     GATT_Deregister(p_cb->cl_rcb[i].client_if);
224
225                     status = BTA_GATT_NO_RESOURCES;
226                     memset( &p_cb->cl_rcb[i], 0 , sizeof(tBTA_GATTC_RCB));
227                 }
228                 break;
229             }
230         }
231     }
232
233     /* callback with register event */
234     if (p_data->api_reg.p_cback) {
235         if (p_app_uuid != NULL) {
236             memcpy(&(cb_data.reg_oper.app_uuid), p_app_uuid, sizeof(tBT_UUID));
237         }
238         cb_data.reg_oper.status = status;
239         (*p_data->api_reg.p_cback)(BTA_GATTC_REG_EVT,  (tBTA_GATTC *)&cb_data);
240     }
241 }
242 /*******************************************************************************
243 **
244 ** Function         bta_gattc_start_if
245 **
246 ** Description      start an application interface.
247 **
248 ** Returns          none.
249 **
250 *******************************************************************************/
251 void bta_gattc_start_if(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA *p_msg)
252 {
253     UNUSED(p_cb);
254
255     if (bta_gattc_cl_get_regcb(p_msg->int_start_if.client_if) != NULL ) {
256         GATT_StartIf(p_msg->int_start_if.client_if);
257     } else {
258         APPL_TRACE_ERROR("Unable to start app.: Unknown interface =%d", p_msg->int_start_if.client_if );
259     }
260 }
261
262 /*******************************************************************************
263 **
264 ** Function         bta_gattc_deregister
265 **
266 ** Description      De-Register a GATT client application with BTA.
267 **
268 ** Returns          void
269 **
270 *******************************************************************************/
271 void bta_gattc_deregister(tBTA_GATTC_CB *p_cb, tBTA_GATTC_RCB  *p_clreg)
272 {
273     UINT8               i;
274     BT_HDR              buf;
275
276     if (p_clreg != NULL) {
277         /* remove bg connection associated with this rcb */
278         for (i = 0; i < BTA_GATTC_KNOWN_SR_MAX; i ++) {
279             if (p_cb->bg_track[i].in_use) {
280                 if (p_cb->bg_track[i].cif_mask & (1 << (p_clreg->client_if - 1))) {
281                     bta_gattc_mark_bg_conn(p_clreg->client_if, p_cb->bg_track[i].remote_bda, FALSE, FALSE);
282                     GATT_CancelConnect(p_clreg->client_if, p_cb->bg_track[i].remote_bda, FALSE);
283                 }
284                 if (p_cb->bg_track[i].cif_adv_mask & (1 << (p_clreg->client_if - 1))) {
285                     bta_gattc_mark_bg_conn(p_clreg->client_if, p_cb->bg_track[i].remote_bda, FALSE, TRUE);
286                 }
287             }
288         }
289
290         if (p_clreg->num_clcb > 0) {
291             /* close all CLCB related to this app */
292             for (i = 0; i < BTA_GATTC_CLCB_MAX; i ++) {
293                 if (p_cb->clcb[i].in_use && (p_cb->clcb[i].p_rcb == p_clreg)) {
294                     p_clreg->dereg_pending = TRUE;
295
296                     buf.event = BTA_GATTC_API_CLOSE_EVT;
297                     buf.layer_specific = p_cb->clcb[i].bta_conn_id;
298                     bta_gattc_close(&p_cb->clcb[i], (tBTA_GATTC_DATA *)&buf)  ;
299                 }
300             }
301         } else {
302             bta_gattc_deregister_cmpl(p_clreg);
303         }
304     } else {
305         APPL_TRACE_ERROR("bta_gattc_deregister Deregister Failedm unknown client cif");
306     }
307 }
308 /*******************************************************************************
309 **
310 ** Function         bta_gattc_process_api_open
311 **
312 ** Description      process connect API request.
313 **
314 ** Returns          void
315 **
316 *******************************************************************************/
317 void bta_gattc_process_api_open (tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA *p_msg)
318 {
319     UINT16 event = ((BT_HDR *)p_msg)->event;
320     tBTA_GATTC_CLCB *p_clcb = NULL;
321     tBTA_GATTC_RCB *p_clreg = bta_gattc_cl_get_regcb(p_msg->api_conn.client_if);
322     UNUSED(p_cb);
323
324     if (p_clreg != NULL) {
325         if (p_msg->api_conn.is_direct) {
326             if ((p_clcb = bta_gattc_find_alloc_clcb(p_msg->api_conn.client_if,
327                                                     p_msg->api_conn.remote_bda,
328                                                     p_msg->api_conn.transport)) != NULL) {
329                 bta_gattc_sm_execute(p_clcb, event, p_msg);
330             } else {
331                 APPL_TRACE_ERROR("No resources to open a new connection.");
332
333                 bta_gattc_send_open_cback(p_clreg,
334                                           BTA_GATT_NO_RESOURCES,
335                                           p_msg->api_conn.remote_bda,
336                                           BTA_GATT_INVALID_CONN_ID,
337                                           p_msg->api_conn.transport, 0);
338             }
339         } else {
340             bta_gattc_init_bk_conn(&p_msg->api_conn, p_clreg);
341         }
342     } else {
343         APPL_TRACE_ERROR("bta_gattc_process_api_open Failed, unknown client_if: %d",
344                          p_msg->api_conn.client_if);
345     }
346 }
347 /*******************************************************************************
348 **
349 ** Function         bta_gattc_process_api_open_cancel
350 **
351 ** Description      process connect API request.
352 **
353 ** Returns          void
354 **
355 *******************************************************************************/
356 void bta_gattc_process_api_open_cancel (tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA *p_msg)
357 {
358     UINT16 event = ((BT_HDR *)p_msg)->event;
359     tBTA_GATTC_CLCB *p_clcb = NULL;
360     tBTA_GATTC_RCB *p_clreg;
361     tBTA_GATTC cb_data;
362     UNUSED(p_cb);
363
364     if (p_msg->api_cancel_conn.is_direct) {
365         if ((p_clcb = bta_gattc_find_clcb_by_cif(p_msg->api_cancel_conn.client_if,
366                       p_msg->api_cancel_conn.remote_bda,
367                       BTA_GATT_TRANSPORT_LE)) != NULL) {
368             bta_gattc_sm_execute(p_clcb, event, p_msg);
369         } else {
370             APPL_TRACE_ERROR("No such connection need to be cancelled");
371
372             p_clreg = bta_gattc_cl_get_regcb(p_msg->api_cancel_conn.client_if);
373
374             if (p_clreg && p_clreg->p_cback) {
375                 cb_data.status = BTA_GATT_ERROR;
376                 (*p_clreg->p_cback)(BTA_GATTC_CANCEL_OPEN_EVT, &cb_data);
377             }
378         }
379     } else {
380         bta_gattc_cancel_bk_conn(&p_msg->api_cancel_conn);
381
382     }
383 }
384
385 /*******************************************************************************
386 **
387 ** Function         bta_gattc_process_enc_cmpl
388 **
389 ** Description      process encryption complete message.
390 **
391 ** Returns          void
392 **
393 *******************************************************************************/
394 void bta_gattc_process_enc_cmpl(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA *p_msg)
395 {
396     tBTA_GATTC_RCB *p_clreg;
397     tBTA_GATTC cb_data;
398     UNUSED(p_cb);
399
400     p_clreg = bta_gattc_cl_get_regcb(p_msg->enc_cmpl.client_if);
401
402     if (p_clreg && p_clreg->p_cback) {
403         memset(&cb_data, 0, sizeof(tBTA_GATTC));
404
405         cb_data.enc_cmpl.client_if = p_msg->enc_cmpl.client_if;
406         bdcpy(cb_data.enc_cmpl.remote_bda, p_msg->enc_cmpl.remote_bda);
407
408         (*p_clreg->p_cback)(BTA_GATTC_ENC_CMPL_CB_EVT, &cb_data);
409     }
410 }
411
412 /*******************************************************************************
413 **
414 ** Function         bta_gattc_cancel_open_error
415 **
416 ** Description
417 **
418 ** Returns          void
419 **
420 *******************************************************************************/
421 void bta_gattc_cancel_open_error(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
422 {
423     tBTA_GATTC cb_data;
424     UNUSED(p_data);
425
426     cb_data.status = BTA_GATT_ERROR;
427
428     if ( p_clcb && p_clcb->p_rcb && p_clcb->p_rcb->p_cback ) {
429         (*p_clcb->p_rcb->p_cback)(BTA_GATTC_CANCEL_OPEN_EVT, &cb_data);
430     }
431 }
432
433 /*******************************************************************************
434 **
435 ** Function         bta_gattc_open_error
436 **
437 ** Description
438 **
439 ** Returns          void
440 **
441 *******************************************************************************/
442 void bta_gattc_open_error(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
443 {
444     UNUSED(p_data);
445
446     APPL_TRACE_ERROR("Connection already opened. wrong state");
447
448     bta_gattc_send_open_cback(p_clcb->p_rcb,
449                               BTA_GATT_OK,
450                               p_clcb->bda,
451                               p_clcb->bta_conn_id,
452                               p_clcb->transport,
453                               0);
454 }
455 /*******************************************************************************
456 **
457 ** Function         bta_gattc_open_fail
458 **
459 ** Description
460 **
461 ** Returns          void
462 **
463 *******************************************************************************/
464 void bta_gattc_open_fail(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
465 {
466     UNUSED(p_data);
467
468     bta_gattc_send_open_cback(p_clcb->p_rcb,
469                               BTA_GATT_ERROR,
470                               p_clcb->bda,
471                               p_clcb->bta_conn_id,
472                               p_clcb->transport,
473                               0);
474     /* open failure, remove clcb */
475     bta_gattc_clcb_dealloc(p_clcb);
476 }
477
478 /*******************************************************************************
479 **
480 ** Function         bta_gattc_open
481 **
482 ** Description      Process API connection function.
483 **
484 ** Returns          void
485 **
486 *******************************************************************************/
487 void bta_gattc_open(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
488 {
489     tBTA_GATTC_DATA gattc_data;
490     BOOLEAN found_app = FALSE;
491
492     tGATT_TCB *p_tcb = gatt_find_tcb_by_addr(p_data->api_conn.remote_bda, BT_TRANSPORT_LE);
493     if(p_tcb && p_clcb && p_data) {
494         found_app = gatt_find_specific_app_in_hold_link(p_tcb, p_clcb->p_rcb->client_if);
495     }
496     /* open/hold a connection */
497     if (!GATT_Connect(p_clcb->p_rcb->client_if, p_data->api_conn.remote_bda, p_data->api_conn.remote_addr_type,
498                       TRUE, p_data->api_conn.transport)) {
499         APPL_TRACE_ERROR("Connection open failure");
500
501         bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_OPEN_FAIL_EVT, p_data);
502     } else {
503         /* a connected remote device */
504         if (GATT_GetConnIdIfConnected(p_clcb->p_rcb->client_if,
505                                       p_data->api_conn.remote_bda,
506                                       &p_clcb->bta_conn_id,
507                                       p_data->api_conn.transport)) {
508             gattc_data.int_conn.hdr.layer_specific = p_clcb->bta_conn_id;
509             gattc_data.int_conn.already_connect = found_app;
510             bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_CONN_EVT, &gattc_data);
511         }
512         /* else wait for the callback event */
513     }
514 }
515 /*******************************************************************************
516 **
517 ** Function         bta_gattc_init_bk_conn
518 **
519 ** Description      Process API Open for a background connection
520 **
521 ** Returns          void
522 **
523 *******************************************************************************/
524 void bta_gattc_init_bk_conn(tBTA_GATTC_API_OPEN *p_data, tBTA_GATTC_RCB *p_clreg)
525 {
526     tBTA_GATT_STATUS         status = BTA_GATT_NO_RESOURCES;
527     UINT16                   conn_id;
528     tBTA_GATTC_CLCB         *p_clcb;
529     tBTA_GATTC_DATA         gattc_data;
530
531     if (bta_gattc_mark_bg_conn(p_data->client_if, p_data->remote_bda, TRUE, FALSE)) {
532         /* always call open to hold a connection */
533         if (!GATT_Connect(p_data->client_if, p_data->remote_bda, p_data->remote_addr_type, FALSE, p_data->transport)) {
534             uint8_t *bda = (uint8_t *)p_data->remote_bda;
535             status = BTA_GATT_ERROR;
536             APPL_TRACE_ERROR("%s unable to connect to remote bd_addr:%02x:%02x:%02x:%02x:%02x:%02x",
537                              __func__, bda[0], bda[1], bda[2], bda[3], bda[4], bda[5]);
538
539         } else {
540             status = BTA_GATT_OK;
541
542             /* if is a connected remote device */
543             if (GATT_GetConnIdIfConnected(p_data->client_if,
544                                           p_data->remote_bda,
545                                           &conn_id,
546                                           p_data->transport)) {
547                 if ((p_clcb = bta_gattc_find_alloc_clcb(p_data->client_if, p_data->remote_bda,
548                                                         BTA_GATT_TRANSPORT_LE)) != NULL) {
549                     gattc_data.hdr.layer_specific = p_clcb->bta_conn_id = conn_id;
550
551                     /* open connection */
552                     bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_CONN_EVT, &gattc_data);
553                     status = BTA_GATT_OK;
554                 }
555             }
556         }
557     }
558
559     /* open failure, report OPEN_EVT */
560     if (status != BTA_GATT_OK) {
561         bta_gattc_send_open_cback(p_clreg, status, p_data->remote_bda,
562                                   BTA_GATT_INVALID_CONN_ID, BTA_GATT_TRANSPORT_LE, 0);
563     }
564 }
565 /*******************************************************************************
566 **
567 ** Function         bta_gattc_cancel_bk_conn
568 **
569 ** Description      Process API Cancel Open for a background connection
570 **
571 ** Returns          void
572 **
573 *******************************************************************************/
574 void bta_gattc_cancel_bk_conn(tBTA_GATTC_API_CANCEL_OPEN *p_data)
575 {
576     tBTA_GATTC_RCB      *p_clreg;
577     tBTA_GATTC          cb_data;
578     cb_data.status = BTA_GATT_ERROR;
579
580     /* remove the device from the bg connection mask */
581     if (bta_gattc_mark_bg_conn(p_data->client_if, p_data->remote_bda, FALSE, FALSE)) {
582         if (GATT_CancelConnect(p_data->client_if, p_data->remote_bda, FALSE)) {
583             cb_data.status = BTA_GATT_OK;
584         } else {
585             APPL_TRACE_ERROR("bta_gattc_cancel_bk_conn failed");
586         }
587     }
588     p_clreg = bta_gattc_cl_get_regcb(p_data->client_if);
589
590     if (p_clreg && p_clreg->p_cback) {
591         (*p_clreg->p_cback)(BTA_GATTC_CANCEL_OPEN_EVT, &cb_data);
592     }
593
594 }
595 /*******************************************************************************
596 **
597 ** Function         bta_gattc_int_cancel_open_ok
598 **
599 ** Description
600 **
601 ** Returns          void
602 **
603 *******************************************************************************/
604 void bta_gattc_cancel_open_ok(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
605 {
606     tBTA_GATTC          cb_data;
607     UNUSED(p_data);
608
609     if ( p_clcb->p_rcb->p_cback ) {
610         cb_data.status = BTA_GATT_OK;
611         (*p_clcb->p_rcb->p_cback)(BTA_GATTC_CANCEL_OPEN_EVT, &cb_data);
612     }
613
614     bta_gattc_clcb_dealloc(p_clcb);
615 }
616 /*******************************************************************************
617 **
618 ** Function         bta_gattc_cancel_open
619 **
620 ** Description
621 **
622 ** Returns          void
623 **
624 *******************************************************************************/
625 void bta_gattc_cancel_open(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
626 {
627     tBTA_GATTC          cb_data;
628
629     if (GATT_CancelConnect(p_clcb->p_rcb->client_if, p_data->api_cancel_conn.remote_bda, TRUE)) {
630         bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_CANCEL_OPEN_OK_EVT, p_data);
631     } else {
632         if ( p_clcb->p_rcb->p_cback ) {
633             cb_data.status = BTA_GATT_ERROR;
634             (*p_clcb->p_rcb->p_cback)(BTA_GATTC_CANCEL_OPEN_EVT, &cb_data);
635         }
636     }
637 }
638 /*******************************************************************************
639 **
640 ** Function         bta_gattc_conn
641 **
642 ** Description      receive connection callback from stack
643 **
644 ** Returns          void
645 **
646 *******************************************************************************/
647 void bta_gattc_conn(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
648 {
649     tBTA_GATTC_IF   gatt_if;
650     APPL_TRACE_DEBUG("bta_gattc_conn server cache state=%d", p_clcb->p_srcb->state);
651
652     if (p_data != NULL) {
653         APPL_TRACE_DEBUG("bta_gattc_conn conn_id=%d", p_data->hdr.layer_specific);
654         p_clcb->bta_conn_id  = p_data->int_conn.hdr.layer_specific;
655
656         GATT_GetConnectionInfor(p_data->hdr.layer_specific,
657                                 &gatt_if, p_clcb->bda, &p_clcb->transport);
658     }
659
660     p_clcb->p_srcb->connected = TRUE;
661
662     if (p_clcb->p_srcb->mtu == 0) {
663         p_clcb->p_srcb->mtu = GATT_DEF_BLE_MTU_SIZE;
664     }
665
666     /* start database cache if needed */
667     if (p_clcb->p_srcb->p_srvc_cache == NULL ||
668             p_clcb->p_srcb->state != BTA_GATTC_SERV_IDLE) {
669         if (p_clcb->p_srcb->state == BTA_GATTC_SERV_IDLE) {
670             p_clcb->p_srcb->state = BTA_GATTC_SERV_LOAD;
671             if (bta_gattc_cache_load(p_clcb)) {
672                 p_clcb->p_srcb->state = BTA_GATTC_SERV_IDLE;
673                 bta_gattc_reset_discover_st(p_clcb->p_srcb, BTA_GATT_OK);
674             } else { /* cache is building */
675                 p_clcb->p_srcb->state = BTA_GATTC_SERV_DISC;
676                 /* cache load failure, start discovery */
677                 bta_gattc_start_discover(p_clcb, NULL);
678             }
679         } else { /* cache is building */
680             p_clcb->state = BTA_GATTC_DISCOVER_ST;
681         }
682     } else {
683         /* a pending service handle change indication */
684         if (p_clcb->p_srcb->srvc_hdl_chg) {
685             p_clcb->p_srcb->srvc_hdl_chg = FALSE;
686             /* start discovery */
687             bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_DISCOVER_EVT, NULL);
688         }
689     }
690
691     if (p_clcb->p_rcb) {
692         /* there is no RM for GATT */
693         if (p_clcb->transport == BTA_TRANSPORT_BR_EDR) {
694             bta_sys_conn_open(BTA_ID_GATTC, BTA_ALL_APP_ID, p_clcb->bda);
695         }
696         tBTA_GATT_STATUS status = BTA_GATT_OK;
697         if (p_data && p_data->int_conn.already_connect) {
698             //clear already_connect
699             p_data->int_conn.already_connect = FALSE;
700             status = BTA_GATT_ALREADY_OPEN;
701         }
702         bta_gattc_send_open_cback(p_clcb->p_rcb,
703                                   status,
704                                   p_clcb->bda,
705                                   p_clcb->bta_conn_id,
706                                   p_clcb->transport,
707                                   p_clcb->p_srcb->mtu);
708
709     }
710 }
711 /*******************************************************************************
712 **
713 ** Function         bta_gattc_conncback
714 **
715 ** Description      receive connection callback from stack
716 **
717 ** Returns          void
718 **
719 *******************************************************************************/
720 void bta_gattc_conncback(tBTA_GATTC_RCB *p_rcb, tBTA_GATTC_DATA *p_data)
721 {
722     if (p_rcb) {
723         bta_gattc_send_connect_cback(p_rcb,
724                                      p_data->int_conn.remote_bda,
725                                      p_data->int_conn.hdr.layer_specific);
726
727     }
728 }
729 /*******************************************************************************
730 **
731 ** Function         bta_gattc_disconncback
732 **
733 ** Description      receive disconnection callback from stack
734 **
735 ** Returns          void
736 **
737 *******************************************************************************/
738 void bta_gattc_disconncback(tBTA_GATTC_RCB *p_rcb, tBTA_GATTC_DATA *p_data)
739 {
740     if (p_rcb) {
741         bta_gattc_send_disconnect_cback(p_rcb,
742                                      p_data->int_conn.reason,
743                                      p_data->int_conn.remote_bda,
744                                      p_data->int_conn.hdr.layer_specific);
745
746     }
747 }
748 /*******************************************************************************
749 **
750 ** Function         bta_gattc_close_fail
751 **
752 ** Description      close a  connection.
753 **
754 ** Returns          void
755 **
756 *******************************************************************************/
757 void bta_gattc_close_fail(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
758 {
759     tBTA_GATTC           cb_data;
760
761     if ( p_clcb->p_rcb->p_cback ) {
762         memset(&cb_data, 0, sizeof(tBTA_GATTC));
763         cb_data.close.client_if = p_clcb->p_rcb->client_if;
764         cb_data.close.conn_id   = p_data->hdr.layer_specific;
765         bdcpy(cb_data.close.remote_bda, p_clcb->bda);
766         cb_data.close.status    = BTA_GATT_ERROR;
767         cb_data.close.reason    = BTA_GATT_CONN_NONE;
768
769
770         (*p_clcb->p_rcb->p_cback)(BTA_GATTC_CLOSE_EVT, &cb_data);
771     }
772 }
773 /*******************************************************************************
774 **
775 ** Function         bta_gattc_api_close
776 **
777 ** Description      close a GATTC connection.
778 **
779 ** Returns          void
780 **
781 *******************************************************************************/
782 void bta_gattc_close(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
783 {
784     tBTA_GATTC_CBACK    *p_cback = p_clcb->p_rcb->p_cback;
785     tBTA_GATTC_RCB      *p_clreg = p_clcb->p_rcb;
786     tBTA_GATTC           cb_data;
787
788     APPL_TRACE_DEBUG("bta_gattc_close conn_id=%d", p_clcb->bta_conn_id);
789
790     cb_data.close.client_if = p_clcb->p_rcb->client_if;
791     cb_data.close.conn_id   = p_clcb->bta_conn_id;
792     cb_data.close.reason    = p_clcb->reason;
793     cb_data.close.status    = p_clcb->status;
794     bdcpy(cb_data.close.remote_bda, p_clcb->bda);
795
796     if (p_clcb->transport == BTA_TRANSPORT_BR_EDR) {
797         bta_sys_conn_close( BTA_ID_GATTC , BTA_ALL_APP_ID, p_clcb->bda);
798     }
799
800     bta_gattc_clcb_dealloc(p_clcb);
801
802     if (p_data->hdr.event == BTA_GATTC_API_CLOSE_EVT) {
803         cb_data.close.status = GATT_Disconnect(p_data->hdr.layer_specific);
804     } else if (p_data->hdr.event == BTA_GATTC_INT_DISCONN_EVT) {
805         cb_data.close.status = BTA_GATT_OK;
806         cb_data.close.reason = p_data->int_conn.reason;
807     }
808
809     if (p_cback) {
810         (* p_cback)(BTA_GATTC_CLOSE_EVT,   (tBTA_GATTC *)&cb_data);
811     }
812
813     if (p_clreg->num_clcb == 0 && p_clreg->dereg_pending) {
814         bta_gattc_deregister_cmpl(p_clreg);
815     }
816 }
817 /*******************************************************************************
818 **
819 ** Function         bta_gattc_reset_discover_st
820 **
821 ** Description      when a SRCB finished discovery, tell all related clcb.
822 **
823 ** Returns          None.
824 **
825 *******************************************************************************/
826 void bta_gattc_reset_discover_st(tBTA_GATTC_SERV *p_srcb, tBTA_GATT_STATUS status)
827 {
828     tBTA_GATTC_CB   *p_cb = &bta_gattc_cb;
829     UINT8 i;
830
831     for (i = 0; i < BTA_GATTC_CLCB_MAX; i ++) {
832         if (p_cb->clcb[i].p_srcb == p_srcb) {
833             p_cb->clcb[i].status = status;
834             bta_gattc_sm_execute(&p_cb->clcb[i], BTA_GATTC_DISCOVER_CMPL_EVT, NULL);
835         }
836     }
837 }
838 /*******************************************************************************
839 **
840 ** Function         bta_gattc_disc_close
841 **
842 ** Description      close a GATTC connection while in discovery state.
843 **
844 ** Returns          void
845 **
846 *******************************************************************************/
847 void bta_gattc_disc_close(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
848 {
849     APPL_TRACE_DEBUG("%s: Discovery cancel conn_id=%d", __func__,
850                      p_clcb->bta_conn_id);
851
852     if (p_clcb->disc_active) {
853         bta_gattc_reset_discover_st(p_clcb->p_srcb, BTA_GATT_ERROR);
854     } else {
855         p_clcb->state = BTA_GATTC_CONN_ST;
856     }
857
858     // This function only gets called as the result of a BTA_GATTC_API_CLOSE_EVT
859     // while in the BTA_GATTC_DISCOVER_ST state. Once the state changes, the
860     // connection itself still needs to be closed to resolve the original event.
861     if (p_clcb->state == BTA_GATTC_CONN_ST) {
862         APPL_TRACE_DEBUG("State is back to BTA_GATTC_CONN_ST. "
863                          "Trigger connection close");
864         bta_gattc_close(p_clcb, p_data);
865     }
866 }
867 /*******************************************************************************
868 **
869 ** Function         bta_gattc_set_discover_st
870 **
871 ** Description      when a SRCB start discovery, tell all related clcb and set
872 **                  the state.
873 **
874 ** Returns          None.
875 **
876 *******************************************************************************/
877 void bta_gattc_set_discover_st(tBTA_GATTC_SERV *p_srcb)
878 {
879     tBTA_GATTC_CB   *p_cb = &bta_gattc_cb;
880     UINT8   i;
881
882     for (i = 0; i < BTA_GATTC_CLCB_MAX; i ++) {
883         if (p_cb->clcb[i].p_srcb == p_srcb) {
884             p_cb->clcb[i].status = BTA_GATT_OK;
885             p_cb->clcb[i].state = BTA_GATTC_DISCOVER_ST;
886         }
887     }
888 }
889 /*******************************************************************************
890 **
891 ** Function         bta_gattc_restart_discover
892 **
893 ** Description      process service change in discovery state, mark up the auto
894 **                  update flag and set status to be discovery cancel for current
895 **                  discovery.
896 **
897 ** Returns          None.
898 **
899 *******************************************************************************/
900 void bta_gattc_restart_discover(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
901 {
902     UNUSED(p_data);
903
904     p_clcb->status      = BTA_GATT_CANCEL;
905     p_clcb->auto_update = BTA_GATTC_DISC_WAITING;
906 }
907
908 /*******************************************************************************
909 **
910 ** Function         bta_gattc_cfg_mtu
911 **
912 ** Description      Configure MTU size on the GATT connection.
913 **
914 ** Returns          None.
915 **
916 *******************************************************************************/
917 void bta_gattc_cfg_mtu(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
918 {
919     tBTA_GATT_STATUS    status;
920
921     if (bta_gattc_enqueue(p_clcb, p_data)) {
922         status = GATTC_ConfigureMTU (p_clcb->bta_conn_id);
923
924         /* if failed, return callback here */
925         if (status != GATT_SUCCESS && status != GATT_CMD_STARTED) {
926             /* Dequeue the data, if it was enqueued */
927             if (p_clcb->p_q_cmd == p_data) {
928                 p_clcb->p_q_cmd = NULL;
929                 bta_gattc_pop_command_to_send(p_clcb);
930             }
931
932             bta_gattc_cmpl_sendmsg(p_clcb->bta_conn_id, GATTC_OPTYPE_CONFIG, status, NULL);
933         }
934     }
935 }
936 /*******************************************************************************
937 **
938 ** Function         bta_gattc_start_discover
939 **
940 ** Description      Start a discovery send to server.
941 **
942 ** Returns          None.
943 **
944 *******************************************************************************/
945 void bta_gattc_start_discover(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
946 {
947     UNUSED(p_data);
948
949     APPL_TRACE_DEBUG("bta_gattc_start_discover conn_id=%d p_clcb->p_srcb->state = %d ",
950                      p_clcb->bta_conn_id, p_clcb->p_srcb->state);
951
952     if (((p_clcb->p_q_cmd == NULL || p_clcb->auto_update == BTA_GATTC_REQ_WAITING) &&
953             p_clcb->p_srcb->state == BTA_GATTC_SERV_IDLE) ||
954             p_clcb->p_srcb->state == BTA_GATTC_SERV_DISC) {
955         /* no pending operation, start discovery right away */
956         p_clcb->auto_update = BTA_GATTC_NO_SCHEDULE;
957
958         if (p_clcb->p_srcb != NULL) {
959             /* clear the service change mask */
960             p_clcb->p_srcb->srvc_hdl_chg = FALSE;
961             p_clcb->p_srcb->update_count = 0;
962             p_clcb->p_srcb->state = BTA_GATTC_SERV_DISC_ACT;
963
964             if (p_clcb->transport == BTA_TRANSPORT_LE) {
965                 L2CA_EnableUpdateBleConnParams(p_clcb->p_srcb->server_bda, FALSE);
966             }
967
968             /* set all srcb related clcb into discovery ST */
969             bta_gattc_set_discover_st(p_clcb->p_srcb);
970
971             if ((p_clcb->status = bta_gattc_init_cache(p_clcb->p_srcb)) == BTA_GATT_OK) {
972                 p_clcb->status = bta_gattc_discover_pri_service(p_clcb->bta_conn_id,
973                                  p_clcb->p_srcb, GATT_DISC_SRVC_ALL);
974             }
975             if (p_clcb->status != BTA_GATT_OK) {
976                 APPL_TRACE_ERROR("discovery on server failed");
977                 bta_gattc_reset_discover_st(p_clcb->p_srcb, p_clcb->status);
978             } else {
979                 p_clcb->disc_active = TRUE;
980             }
981         } else {
982             APPL_TRACE_ERROR("unknown device, can not start discovery");
983         }
984     }
985     /* pending operation, wait until it finishes */
986     else {
987         p_clcb->auto_update = BTA_GATTC_DISC_WAITING;
988
989         if (p_clcb->p_srcb->state == BTA_GATTC_SERV_IDLE) {
990             p_clcb->state = BTA_GATTC_CONN_ST;    /* set clcb state */
991         }
992     }
993
994 }
995 /*******************************************************************************
996 **
997 ** Function         bta_gattc_disc_cmpl
998 **
999 ** Description      discovery on server is finished
1000 **
1001 ** Returns          None.
1002 **
1003 *******************************************************************************/
1004 void bta_gattc_disc_cmpl(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1005 {
1006     tBTA_GATTC_DATA *p_q_cmd = p_clcb->p_q_cmd;
1007     UNUSED(p_data);
1008
1009     APPL_TRACE_DEBUG("bta_gattc_disc_cmpl conn_id=%d, status = %d", p_clcb->bta_conn_id, p_clcb->status);
1010
1011     p_clcb->p_srcb->state = BTA_GATTC_SERV_IDLE;
1012     p_clcb->disc_active = FALSE;
1013
1014     if (p_clcb->status != GATT_SUCCESS) {
1015         /* clean up cache */
1016         if (p_clcb->p_srcb && p_clcb->p_srcb->p_srvc_cache) {
1017             list_free(p_clcb->p_srcb->p_srvc_cache);
1018             p_clcb->p_srcb->p_srvc_cache = NULL;
1019         }
1020
1021         /* used to reset cache in application */
1022         bta_gattc_cache_reset(p_clcb->p_srcb->server_bda);
1023     }
1024     if (p_clcb->p_srcb && p_clcb->p_srcb->p_srvc_list) {
1025         /* release pending attribute list buffer */
1026         osi_free(p_clcb->p_srcb->p_srvc_list);
1027         p_clcb->p_srcb->p_srvc_list = NULL;
1028         //osi_free_and_reset((void **)&p_clcb->p_srcb->p_srvc_list);
1029     }
1030
1031     if (p_clcb->auto_update == BTA_GATTC_DISC_WAITING) {
1032         /* start discovery again */
1033         bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_DISCOVER_EVT, NULL);
1034     }
1035     /* get any queued command to proceed */
1036     else if (p_q_cmd != NULL) {
1037         p_clcb->p_q_cmd = NULL;
1038         /* execute pending operation of link block still present */
1039         if (l2cu_find_lcb_by_bd_addr(p_clcb->p_srcb->server_bda, BT_TRANSPORT_LE) != NULL) {
1040             bta_gattc_sm_execute(p_clcb, p_q_cmd->hdr.event, p_q_cmd);
1041         }
1042         /* if the command executed requeued the cmd, we don't
1043          * want to free the underlying buffer that's being
1044          * referenced by p_clcb->p_q_cmd
1045          */
1046         if (p_q_cmd != p_clcb->p_q_cmd) {
1047             osi_free(p_q_cmd);
1048             p_q_cmd = NULL;
1049         }
1050     }
1051
1052     //register service change
1053     bta_gattc_register_service_change_notify(p_clcb->bta_conn_id, p_clcb->bda);
1054
1055 }
1056 /*******************************************************************************
1057 **
1058 ** Function         bta_gattc_read
1059 **
1060 ** Description      Read an attribute
1061 **
1062 ** Returns          None.
1063 **
1064 *******************************************************************************/
1065 void bta_gattc_read(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1066 {
1067     if (!bta_gattc_enqueue(p_clcb, p_data))
1068         return;
1069
1070     tGATT_READ_PARAM    read_param;
1071     memset (&read_param, 0 ,sizeof(tGATT_READ_PARAM));
1072     read_param.by_handle.handle = p_data->api_read.handle;
1073     read_param.by_handle.auth_req = p_data->api_read.auth_req;
1074
1075     tBTA_GATT_STATUS status = GATTC_Read(p_clcb->bta_conn_id, GATT_READ_BY_HANDLE, &read_param);
1076
1077         /* read fail */
1078         if (status != BTA_GATT_OK) {
1079             /* Dequeue the data, if it was enqueued */
1080             if (p_clcb->p_q_cmd == p_data) {
1081                 p_clcb->p_q_cmd = NULL;
1082                 bta_gattc_pop_command_to_send(p_clcb);
1083             }
1084
1085             bta_gattc_cmpl_sendmsg(p_clcb->bta_conn_id, GATTC_OPTYPE_READ, status, NULL);
1086         }
1087 }
1088 /*******************************************************************************
1089 **
1090 ** Function         bta_gattc_read_multi
1091 **
1092 ** Description      read multiple
1093 **
1094 ** Returns          None.
1095 *********************************************************************************/
1096 void bta_gattc_read_multi(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1097 {
1098     tBTA_GATT_STATUS    status = BTA_GATT_OK;
1099     tGATT_READ_PARAM    read_param;
1100
1101     if (bta_gattc_enqueue(p_clcb, p_data)) {
1102         memset(&read_param, 0, sizeof(tGATT_READ_PARAM));
1103
1104         if (status == BTA_GATT_OK) {
1105             read_param.read_multiple.num_handles = p_data->api_read_multi.num_attr;
1106             read_param.read_multiple.auth_req = p_data->api_read_multi.auth_req;
1107             memcpy(&read_param.read_multiple.handles, p_data->api_read_multi.handles,
1108                                         sizeof(UINT16) * p_data->api_read_multi.num_attr);
1109
1110             status = GATTC_Read(p_clcb->bta_conn_id, GATT_READ_MULTIPLE, &read_param);
1111         }
1112
1113         /* read fail */
1114         if (status != BTA_GATT_OK) {
1115             /* Dequeue the data, if it was enqueued */
1116             if (p_clcb->p_q_cmd == p_data) {
1117                 p_clcb->p_q_cmd = NULL;
1118                 bta_gattc_pop_command_to_send(p_clcb);
1119             }
1120
1121             bta_gattc_cmpl_sendmsg(p_clcb->bta_conn_id, GATTC_OPTYPE_READ, status, NULL);
1122         }
1123     }
1124 }
1125 /*******************************************************************************
1126 **
1127 ** Function         bta_gattc_write
1128 **
1129 ** Description      Write an attribute
1130 **
1131 ** Returns          None.
1132 **
1133 *******************************************************************************/
1134 void bta_gattc_write(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1135 {
1136     if (!bta_gattc_enqueue(p_clcb, p_data))
1137         return;
1138
1139     tBTA_GATT_STATUS    status = BTA_GATT_OK;
1140     tGATT_VALUE attr;
1141
1142     attr.conn_id = p_clcb->bta_conn_id;
1143     attr.handle = p_data->api_write.handle;
1144     attr.offset = p_data->api_write.offset;
1145     attr.len    = p_data->api_write.len;
1146     attr.auth_req = p_data->api_write.auth_req;
1147
1148     if (p_data->api_write.p_value) {
1149         memcpy(attr.value, p_data->api_write.p_value, p_data->api_write.len);
1150     }
1151
1152     status = GATTC_Write(p_clcb->bta_conn_id, p_data->api_write.write_type, &attr);
1153
1154     /* write fail */
1155     if (status != BTA_GATT_OK) {
1156         /* Dequeue the data, if it was enqueued */
1157         if (p_clcb->p_q_cmd == p_data) {
1158             p_clcb->p_q_cmd = NULL;
1159             bta_gattc_pop_command_to_send(p_clcb);
1160         }
1161
1162         bta_gattc_cmpl_sendmsg(p_clcb->bta_conn_id, GATTC_OPTYPE_WRITE, status, NULL);
1163     }
1164 }
1165 /*******************************************************************************
1166 **
1167 ** Function         bta_gattc_execute
1168 **
1169 ** Description      send execute write
1170 **
1171 ** Returns          None.
1172 *********************************************************************************/
1173 void bta_gattc_execute(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1174 {
1175     tBTA_GATT_STATUS    status;
1176
1177     if (bta_gattc_enqueue(p_clcb, p_data)) {
1178         status = GATTC_ExecuteWrite(p_clcb->bta_conn_id, p_data->api_exec.is_execute);
1179
1180         if (status != BTA_GATT_OK) {
1181             /* Dequeue the data, if it was enqueued */
1182             if (p_clcb->p_q_cmd == p_data) {
1183                 p_clcb->p_q_cmd = NULL;
1184                 bta_gattc_pop_command_to_send(p_clcb);
1185             }
1186
1187             bta_gattc_cmpl_sendmsg(p_clcb->bta_conn_id, GATTC_OPTYPE_EXE_WRITE, status, NULL);
1188         }
1189     }
1190 }
1191 /*******************************************************************************
1192 **
1193 ** Function         bta_gattc_confirm
1194 **
1195 ** Description      send handle value confirmation
1196 **
1197 ** Returns          None.
1198 **
1199 *******************************************************************************/
1200 void bta_gattc_confirm(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1201 {
1202     UINT16 handle = p_data->api_confirm.handle;
1203
1204     if (GATTC_SendHandleValueConfirm(p_data->api_confirm.hdr.layer_specific, handle)
1205         != GATT_SUCCESS) {
1206             APPL_TRACE_ERROR("bta_gattc_confirm to handle [0x%04x] failed", handle);
1207     } else {
1208         /* if over BR_EDR, inform PM for mode change */
1209         if (p_clcb->transport == BTA_TRANSPORT_BR_EDR) {
1210             bta_sys_busy(BTA_ID_GATTC, BTA_ALL_APP_ID, p_clcb->bda);
1211             bta_sys_idle(BTA_ID_GATTC, BTA_ALL_APP_ID, p_clcb->bda);
1212         }
1213     }
1214 }
1215 /*******************************************************************************
1216 **
1217 ** Function         bta_gattc_read_cmpl
1218 **
1219 ** Description      read complete
1220 **
1221 ** Returns          None.
1222 **
1223 *******************************************************************************/
1224 void bta_gattc_read_cmpl(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_OP_CMPL *p_data)
1225 {
1226     UINT8               event;
1227     tBTA_GATTC          cb_data;
1228     tBTA_GATT_UNFMT     read_value;
1229
1230     memset(&cb_data, 0, sizeof(tBTA_GATTC));
1231     memset(&read_value, 0, sizeof(tBTA_GATT_UNFMT));
1232
1233     cb_data.read.status     = p_data->status;
1234
1235     if (p_data->p_cmpl != NULL && p_data->status == BTA_GATT_OK) {
1236         cb_data.read.handle = p_data->p_cmpl->att_value.handle;
1237
1238         read_value.len = p_data->p_cmpl->att_value.len;
1239         read_value.p_value = p_data->p_cmpl->att_value.value;
1240         cb_data.read.p_value = &read_value;
1241     } else {
1242         cb_data.read.handle = p_clcb->p_q_cmd->api_read.handle;
1243     }
1244
1245     if (p_clcb->p_q_cmd->hdr.event != BTA_GATTC_API_READ_MULTI_EVT) {
1246         event = p_clcb->p_q_cmd->api_read.cmpl_evt;
1247     } else {
1248         event = p_clcb->p_q_cmd->api_read_multi.cmpl_evt;
1249     }
1250     cb_data.read.conn_id = p_clcb->bta_conn_id;
1251     //free the command data store in the queue.
1252     bta_gattc_free_command_data(p_clcb);
1253     bta_gattc_pop_command_to_send(p_clcb);
1254     /* read complete, callback */
1255     ( *p_clcb->p_rcb->p_cback)(event, (tBTA_GATTC *)&cb_data);
1256
1257 }
1258 /*******************************************************************************
1259 **
1260 ** Function         bta_gattc_write_cmpl
1261 **
1262 ** Description      write complete
1263 **
1264 ** Returns          None.
1265 **
1266 *******************************************************************************/
1267 void bta_gattc_write_cmpl(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_OP_CMPL *p_data)
1268 {
1269     tBTA_GATTC      cb_data = {0};
1270     UINT8          event;
1271     tBTA_GATTC_CONN *p_conn = bta_gattc_conn_find(p_clcb->bda);
1272
1273     memset(&cb_data, 0, sizeof(tBTA_GATTC));
1274
1275     cb_data.write.status     = p_data->status;
1276     cb_data.write.handle = p_data->p_cmpl->att_value.handle;
1277     if (p_clcb->p_q_cmd->api_write.hdr.event == BTA_GATTC_API_WRITE_EVT &&
1278         p_clcb->p_q_cmd->api_write.write_type == BTA_GATTC_WRITE_PREPARE) {
1279         // Should check the value received from the peer device is correct or not.
1280         if (memcmp(p_clcb->p_q_cmd->api_write.p_value, p_data->p_cmpl->att_value.value,
1281                    p_data->p_cmpl->att_value.len) != 0) {
1282             cb_data.write.status = BTA_GATT_INVALID_PDU;
1283         }
1284
1285         event = BTA_GATTC_PREP_WRITE_EVT;
1286     } else {
1287         event = p_clcb->p_q_cmd->api_write.cmpl_evt;
1288     }
1289     //free the command data store in the queue.
1290     bta_gattc_free_command_data(p_clcb);
1291     bta_gattc_pop_command_to_send(p_clcb);
1292     cb_data.write.conn_id = p_clcb->bta_conn_id;
1293     if (p_conn && p_conn->svc_change_descr_handle == cb_data.write.handle) {
1294         if(cb_data.write.status != BTA_GATT_OK) {
1295             APPL_TRACE_ERROR("service change write ccc failed");
1296         }
1297         return;
1298     }
1299     /* write complete, callback */
1300     ( *p_clcb->p_rcb->p_cback)(event, (tBTA_GATTC *)&cb_data);
1301
1302 }
1303 /*******************************************************************************
1304 **
1305 ** Function         bta_gattc_exec_cmpl
1306 **
1307 ** Description      execute write complete
1308 **
1309 ** Returns          None.
1310 **
1311 *******************************************************************************/
1312 void bta_gattc_exec_cmpl(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_OP_CMPL *p_data)
1313 {
1314     tBTA_GATTC          cb_data;
1315     //free the command data store in the queue.
1316     bta_gattc_free_command_data(p_clcb);
1317     bta_gattc_pop_command_to_send(p_clcb);
1318     p_clcb->status      = BTA_GATT_OK;
1319
1320     /* execute complete, callback */
1321     cb_data.exec_cmpl.conn_id = p_clcb->bta_conn_id;
1322     cb_data.exec_cmpl.status = p_data->status;
1323
1324     ( *p_clcb->p_rcb->p_cback)(BTA_GATTC_EXEC_EVT,  &cb_data);
1325
1326 }
1327
1328 /*******************************************************************************
1329 **
1330 ** Function         bta_gattc_cfg_mtu_cmpl
1331 **
1332 ** Description      configure MTU operation complete
1333 **
1334 ** Returns          None.
1335 **
1336 *******************************************************************************/
1337 void bta_gattc_cfg_mtu_cmpl(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_OP_CMPL *p_data)
1338 {
1339     tBTA_GATTC          cb_data;
1340     //free the command data store in the queue.
1341     bta_gattc_free_command_data(p_clcb);
1342     bta_gattc_pop_command_to_send(p_clcb);
1343
1344     if (p_data->p_cmpl  &&  p_data->status == BTA_GATT_OK) {
1345         p_clcb->p_srcb->mtu  = p_data->p_cmpl->mtu;
1346     }
1347
1348     /* configure MTU complete, callback */
1349     p_clcb->status          = p_data->status;
1350     cb_data.cfg_mtu.conn_id = p_clcb->bta_conn_id;
1351     cb_data.cfg_mtu.status  = p_data->status;
1352     cb_data.cfg_mtu.mtu     = p_clcb->p_srcb->mtu;
1353
1354     (*p_clcb->p_rcb->p_cback) (BTA_GATTC_CFG_MTU_EVT,  &cb_data);
1355
1356 }
1357 /*******************************************************************************
1358 **
1359 ** Function         bta_gattc_op_cmpl
1360 **
1361 ** Description      operation completed.
1362 **
1363 ** Returns          None.
1364 **
1365 *******************************************************************************/
1366 void  bta_gattc_op_cmpl(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1367 {
1368     UINT8           op = (UINT8)p_data->op_cmpl.op_code;
1369     UINT8           mapped_op = 0;
1370
1371     APPL_TRACE_DEBUG("bta_gattc_op_cmpl op = %d", op);
1372
1373     if (op == GATTC_OPTYPE_INDICATION || op == GATTC_OPTYPE_NOTIFICATION) {
1374         APPL_TRACE_ERROR("unexpected operation, ignored");
1375     } else if (op >= GATTC_OPTYPE_READ) {
1376         if (p_clcb->p_q_cmd == NULL) {
1377             APPL_TRACE_ERROR("No pending command");
1378             return;
1379         }
1380         if (p_clcb->p_q_cmd->hdr.event != bta_gattc_opcode_to_int_evt[op - GATTC_OPTYPE_READ]) {
1381             if (p_clcb->p_q_cmd->hdr.event != BTA_GATTC_API_READ_MULTI_EVT) {
1382                 mapped_op = p_clcb->p_q_cmd->hdr.event - BTA_GATTC_API_READ_EVT + GATTC_OPTYPE_READ;
1383                 if ( mapped_op > GATTC_OPTYPE_INDICATION) {
1384                     mapped_op = 0;
1385                 }
1386
1387 #if (BT_TRACE_VERBOSE == TRUE)
1388                 APPL_TRACE_ERROR("expect op:(%s :0x%04x), receive unexpected operation (%s).",
1389                                  bta_gattc_op_code_name[mapped_op] , p_clcb->p_q_cmd->hdr.event,
1390                                  bta_gattc_op_code_name[op]);
1391 #else
1392                 APPL_TRACE_ERROR("expect op:(%u :0x%04x), receive unexpected operation (%u).",
1393                                  mapped_op , p_clcb->p_q_cmd->hdr.event, op);
1394 #endif
1395                 return;
1396             }
1397         }
1398
1399         /* discard responses if service change indication is received before operation completed */
1400         if (p_clcb->auto_update == BTA_GATTC_DISC_WAITING && p_clcb->p_srcb->srvc_hdl_chg) {
1401             APPL_TRACE_DEBUG("Discard all responses when service change indication is received.");
1402             p_data->op_cmpl.status = GATT_ERROR;
1403         }
1404
1405         /* service handle change void the response, discard it */
1406         if (op == GATTC_OPTYPE_READ) {
1407             bta_gattc_read_cmpl(p_clcb, &p_data->op_cmpl);
1408         }
1409
1410         else if (op == GATTC_OPTYPE_WRITE) {
1411             bta_gattc_write_cmpl(p_clcb, &p_data->op_cmpl);
1412         }
1413
1414         else if (op == GATTC_OPTYPE_EXE_WRITE) {
1415             bta_gattc_exec_cmpl(p_clcb, &p_data->op_cmpl);
1416         }
1417
1418         else if (op == GATTC_OPTYPE_CONFIG) {
1419             bta_gattc_cfg_mtu_cmpl(p_clcb, &p_data->op_cmpl);
1420         }
1421
1422         if (p_clcb->auto_update == BTA_GATTC_DISC_WAITING) {
1423             p_clcb->auto_update = BTA_GATTC_REQ_WAITING;
1424             bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_DISCOVER_EVT, NULL);
1425         }
1426     }
1427 }
1428 /*******************************************************************************
1429 **
1430 ** Function         bta_gattc_op_cmpl
1431 **
1432 ** Description      operation completed.
1433 **
1434 ** Returns          None.
1435 **
1436 *******************************************************************************/
1437 void  bta_gattc_ignore_op_cmpl(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1438 {
1439     UNUSED(p_clcb);
1440
1441     /* receive op complete when discovery is started, ignore the response,
1442         and wait for discovery finish and resent */
1443     APPL_TRACE_DEBUG("bta_gattc_ignore_op_cmpl op = %d", p_data->hdr.layer_specific);
1444
1445 }
1446 /*******************************************************************************
1447 **
1448 ** Function         bta_gattc_search
1449 **
1450 ** Description      start a search in the local server cache
1451 **
1452 ** Returns          None.
1453 **
1454 *******************************************************************************/
1455 void bta_gattc_search(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1456 {
1457     tBTA_GATT_STATUS    status = GATT_INTERNAL_ERROR;
1458     tBTA_GATTC cb_data;
1459     APPL_TRACE_DEBUG("bta_gattc_search conn_id=%d", p_clcb->bta_conn_id);
1460     if (p_clcb->p_srcb && p_clcb->p_srcb->p_srvc_cache) {
1461         status = BTA_GATT_OK;
1462         /* search the local cache of a server device */
1463         bta_gattc_search_service(p_clcb, p_data->api_search.p_srvc_uuid);
1464     }
1465     cb_data.search_cmpl.status  = status;
1466     cb_data.search_cmpl.conn_id = p_clcb->bta_conn_id;
1467
1468     /* end of search or no server cache available */
1469     ( *p_clcb->p_rcb->p_cback)(BTA_GATTC_SEARCH_CMPL_EVT,  &cb_data);
1470 }
1471 /*******************************************************************************
1472 **
1473 ** Function         bta_gattc_q_cmd
1474 **
1475 ** Description      enqueue a command into control block, usually because discovery
1476 **                  operation is busy.
1477 **
1478 ** Returns          None.
1479 **
1480 *******************************************************************************/
1481 void bta_gattc_q_cmd(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1482 {
1483     bta_gattc_enqueue(p_clcb, p_data);
1484 }
1485 /*******************************************************************************
1486 **
1487 ** Function         bta_gattc_pop_command_to_send
1488 **
1489 ** Description      dequeue a command into control block.
1490 **                  Check if there has command pending in the command queue or not,
1491 **                  if there has command pending in the command queue, sent it to the state machine to decision
1492 **                  should be sent it to the remote device or not.
1493 **
1494 ** Returns          None.
1495 **
1496 *******************************************************************************/
1497 static void bta_gattc_pop_command_to_send(tBTA_GATTC_CLCB *p_clcb)
1498 {
1499     if (!list_is_empty(p_clcb->p_cmd_list)) {
1500         list_node_t *node = list_begin(p_clcb->p_cmd_list);
1501         tBTA_GATTC_DATA *p_data = (tBTA_GATTC_DATA *)list_node(node);
1502         if (p_data != NULL) {
1503             /* execute pending operation of link block still present */
1504             if (l2cu_find_lcb_by_bd_addr(p_clcb->p_srcb->server_bda, BT_TRANSPORT_LE) != NULL) {
1505                 // The data to be sent to the gattc state machine for processing
1506                 if(bta_gattc_sm_execute(p_clcb, p_data->hdr.event, p_data)) {
1507                     list_remove(p_clcb->p_cmd_list, (void *)p_data);
1508                 }
1509
1510                 if (p_clcb->is_full) {
1511                     tBTA_GATTC cb_data = {0};
1512                     p_clcb->is_full = FALSE;
1513                     cb_data.status = GATT_SUCCESS;
1514                     cb_data.queue_full.conn_id = p_clcb->bta_conn_id;
1515                     cb_data.queue_full.is_full = FALSE;
1516                     if (p_clcb->p_rcb->p_cback != NULL) {
1517                         ( *p_clcb->p_rcb->p_cback)(BTA_GATTC_QUEUE_FULL_EVT, (tBTA_GATTC *)&cb_data);
1518                     }
1519                 }
1520             }
1521         }
1522     }
1523 }
1524 /*******************************************************************************
1525 **
1526 ** Function         bta_gattc_free_command_data
1527 **
1528 ** Description      free the command data into control block.
1529 **
1530 ** Returns          None.
1531 **
1532 *******************************************************************************/
1533 void bta_gattc_free_command_data(tBTA_GATTC_CLCB *p_clcb)
1534 {
1535     assert(p_clcb->p_cmd_list);
1536     //Check the list is empty or not.
1537     if (!list_is_empty(p_clcb->p_cmd_list)) {
1538         /* Traversal the command queue, check the p_q_cmd is point to the queue data or not, if the p_q_cmd point to the
1539            command queue,should remove it from the list */
1540         for (list_node_t *node = list_begin(p_clcb->p_cmd_list); node != list_end(p_clcb->p_cmd_list);
1541              node = list_next(node)) {
1542             tBTA_GATTC_DATA *p_data = (tBTA_GATTC_DATA *)list_node(node);
1543             if (p_data == p_clcb->p_q_cmd) {
1544                 list_remove(p_clcb->p_cmd_list, (void *)p_data);
1545                 p_clcb->p_q_cmd = NULL;
1546                 return;
1547             }
1548         }
1549
1550         osi_free(p_clcb->p_q_cmd);
1551         p_clcb->p_q_cmd = NULL;
1552     } else {
1553         osi_free(p_clcb->p_q_cmd);
1554         p_clcb->p_q_cmd = NULL;
1555     }
1556 }
1557
1558 /*******************************************************************************
1559 **
1560 ** Function         bta_gattc_fail
1561 **
1562 ** Description      report API call failure back to apps
1563 **
1564 ** Returns          None.
1565 **
1566 *******************************************************************************/
1567 void bta_gattc_fail(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1568 {
1569     UNUSED(p_data);
1570
1571     if (p_clcb->status == BTA_GATT_OK) {
1572         APPL_TRACE_ERROR("operation not supported at current state [%d]", p_clcb->state);
1573     }
1574 }
1575
1576 /*******************************************************************************
1577 **
1578 ** Function         bta_gattc_deregister_cmpl
1579 **
1580 ** Description      De-Register a GATT client application with BTA completed.
1581 **
1582 ** Returns          void
1583 **
1584 *******************************************************************************/
1585 static void bta_gattc_deregister_cmpl(tBTA_GATTC_RCB *p_clreg)
1586 {
1587     tBTA_GATTC_CB       *p_cb = &bta_gattc_cb;
1588     tBTA_GATTC_IF       client_if = p_clreg->client_if;
1589     tBTA_GATTC          cb_data;
1590     tBTA_GATTC_CBACK    *p_cback = p_clreg->p_cback;
1591
1592     memset(&cb_data, 0, sizeof(tBTA_GATTC));
1593
1594     GATT_Deregister(p_clreg->client_if);
1595     memset(p_clreg, 0, sizeof(tBTA_GATTC_RCB));
1596
1597     cb_data.reg_oper.client_if = client_if;
1598     cb_data.reg_oper.status    = BTA_GATT_OK;
1599
1600     if (p_cback)
1601         /* callback with de-register event */
1602     {
1603         (*p_cback)(BTA_GATTC_DEREG_EVT,  (tBTA_GATTC *)&cb_data);
1604     }
1605
1606     if (bta_gattc_num_reg_app() == 0 && p_cb->state == BTA_GATTC_STATE_DISABLING) {
1607         p_cb->state = BTA_GATTC_STATE_DISABLED;
1608     }
1609 }
1610
1611 /*******************************************************************************
1612 **
1613 ** Function         bta_gattc_conn_cback
1614 **
1615 ** Description      callback functions to GATT client stack.
1616 **
1617 ** Returns          void
1618 **
1619 *******************************************************************************/
1620 static void bta_gattc_conn_cback(tGATT_IF gattc_if, BD_ADDR bda, UINT16 conn_id,
1621                                  BOOLEAN connected, tGATT_DISCONN_REASON reason,
1622                                  tBT_TRANSPORT transport)
1623 {
1624     tBTA_GATTC_DATA *p_buf;
1625
1626     if (reason != 0) {
1627         APPL_TRACE_WARNING("%s() - cif=%d connected=%d conn_id=%d reason=0x%04x",
1628                            __FUNCTION__, gattc_if, connected, conn_id, reason);
1629     }
1630
1631     bt_bdaddr_t bdaddr;
1632     bdcpy(bdaddr.address, bda);
1633
1634     if ((p_buf = (tBTA_GATTC_DATA *) osi_malloc(sizeof(tBTA_GATTC_DATA))) != NULL) {
1635         memset(p_buf, 0, sizeof(tBTA_GATTC_DATA));
1636
1637         p_buf->int_conn.hdr.event            = connected ? BTA_GATTC_INT_CONN_EVT :
1638                                                BTA_GATTC_INT_DISCONN_EVT;
1639         p_buf->int_conn.hdr.layer_specific   = conn_id;
1640         p_buf->int_conn.client_if            = gattc_if;
1641         p_buf->int_conn.role                 = L2CA_GetBleConnRole(bda);
1642         p_buf->int_conn.reason               = reason;
1643         p_buf->int_conn.transport            = transport;
1644         bdcpy(p_buf->int_conn.remote_bda, bda);
1645
1646         bta_sys_sendmsg(p_buf);
1647     }
1648 }
1649
1650 /*******************************************************************************
1651 **
1652 ** Function         bta_gattc_enc_cmpl_cback
1653 **
1654 ** Description      encryption complete callback function to GATT client stack.
1655 **
1656 ** Returns          void
1657 **
1658 *******************************************************************************/
1659 static void bta_gattc_enc_cmpl_cback(tGATT_IF gattc_if, BD_ADDR bda)
1660 {
1661     tBTA_GATTC_DATA *p_buf;
1662     tBTA_GATTC_CLCB *p_clcb = NULL;
1663
1664     if ((p_clcb = bta_gattc_find_clcb_by_cif(gattc_if, bda, BTA_GATT_TRANSPORT_LE)) == NULL) {
1665         return;
1666     }
1667
1668 #if (defined BTA_HH_LE_INCLUDED && BTA_HH_LE_INCLUDED == TRUE)
1669     /* filter this event just for BTA HH LE GATT client,
1670        In the future, if we want to enable encryption complete event
1671        for all GATT clients, we can remove this code */
1672     if (!bta_hh_le_is_hh_gatt_if(gattc_if)) {
1673         return;
1674     }
1675 #endif
1676
1677     APPL_TRACE_DEBUG("bta_gattc_enc_cmpl_cback: cif = %d", gattc_if);
1678
1679     if ((p_buf = (tBTA_GATTC_DATA *) osi_calloc(sizeof(tBTA_GATTC_DATA))) != NULL) {
1680         memset(p_buf, 0, sizeof(tBTA_GATTC_DATA));
1681
1682         p_buf->enc_cmpl.hdr.event            = BTA_GATTC_ENC_CMPL_EVT;
1683         p_buf->enc_cmpl.hdr.layer_specific   = p_clcb->bta_conn_id;
1684         p_buf->enc_cmpl.client_if            = gattc_if;
1685         bdcpy(p_buf->enc_cmpl.remote_bda, bda);
1686
1687         bta_sys_sendmsg(p_buf);
1688     }
1689 }
1690
1691 /*******************************************************************************
1692 **
1693 ** Function         bta_gattc_process_api_refresh
1694 **
1695 ** Description      process refresh API to delete cache and start a new discovery
1696 **                  if currently connected.
1697 **
1698 ** Returns          None.
1699 **
1700 *******************************************************************************/
1701 void bta_gattc_process_api_refresh(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA *p_msg)
1702 {
1703     tBTA_GATTC_SERV *p_srvc_cb = bta_gattc_find_srvr_cache(p_msg->api_conn.remote_bda);
1704     tBTA_GATTC_CLCB      *p_clcb = &bta_gattc_cb.clcb[0];
1705     BOOLEAN         found = FALSE;
1706     UINT8           i;
1707     UNUSED(p_cb);
1708
1709     if (p_srvc_cb != NULL) {
1710         /* try to find a CLCB */
1711         if (p_srvc_cb->connected && p_srvc_cb->num_clcb != 0) {
1712             for (i = 0; i < BTA_GATTC_CLCB_MAX; i ++, p_clcb ++) {
1713                 if (p_clcb->in_use && p_clcb->p_srcb == p_srvc_cb) {
1714                     found = TRUE;
1715                     break;
1716                 }
1717             }
1718             if (found) {
1719                 bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_DISCOVER_EVT, NULL);
1720                 bta_gattc_cache_reset(p_msg->api_conn.remote_bda);
1721                 return;
1722             }
1723         }
1724         /* in all other cases, mark it and delete the cache */
1725         if (p_srvc_cb->p_srvc_cache != NULL) {
1726             list_free(p_srvc_cb->p_srvc_cache);
1727             p_srvc_cb->p_srvc_cache = NULL;
1728         }
1729     }
1730     /* used to reset cache in application */
1731     bta_gattc_cache_reset(p_msg->api_conn.remote_bda);
1732 }
1733
1734 void bta_gattc_process_api_cache_assoc(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA *p_msg)
1735 {
1736     tBTA_GATTC gattc_cb = {0};
1737     gattc_cb.set_assoc.client_if = p_msg->api_assoc.client_if;
1738     BOOLEAN state = FALSE;
1739     tBTA_GATTC_CLCB *p_assoc_clcb = bta_gattc_find_clcb_by_cif(p_msg->api_assoc.client_if, 
1740                                                              p_msg->api_assoc.assoc_addr, BTA_TRANSPORT_LE);
1741     tBTA_GATTC_RCB *p_clrcb = bta_gattc_cl_get_regcb(p_msg->api_assoc.client_if);
1742     if (p_assoc_clcb != NULL) {
1743         if (p_assoc_clcb->state == BTA_GATTC_CONN_ST || p_assoc_clcb->state == BTA_GATTC_DISCOVER_ST) {
1744             gattc_cb.set_assoc.status = BTA_GATT_BUSY;
1745             if (p_clrcb != NULL) {
1746                 (*p_clrcb->p_cback)(BTA_GATTC_ASSOC_EVT, &gattc_cb);
1747                 return;
1748             }
1749         }
1750     }
1751
1752     if (p_msg->api_assoc.is_assoc) {
1753         if ((state = bta_gattc_co_cache_append_assoc_addr(p_msg->api_assoc.src_addr, p_msg->api_assoc.assoc_addr)) == TRUE) {
1754             gattc_cb.set_assoc.status = BTA_GATT_OK;
1755
1756         } else {
1757             gattc_cb.set_assoc.status = BTA_GATT_ERROR;
1758             if (p_clrcb != NULL) {
1759                 (*p_clrcb->p_cback)(BTA_GATTC_ASSOC_EVT, &gattc_cb);
1760                 return;
1761             }
1762         }
1763     } else {
1764         if (( state = bta_gattc_co_cache_remove_assoc_addr(p_msg->api_assoc.src_addr, p_msg->api_assoc.assoc_addr)) == TRUE) {
1765             gattc_cb.set_assoc.status = BTA_GATT_OK;
1766         } else {
1767             gattc_cb.set_assoc.status = BTA_GATT_ERROR;
1768             if (p_clrcb != NULL) {
1769                 (*p_clrcb->p_cback)(BTA_GATTC_ASSOC_EVT, &gattc_cb);
1770                 return;
1771             }
1772         }
1773     }
1774
1775     if (p_clrcb != NULL) {
1776         (*p_clrcb->p_cback)(BTA_GATTC_ASSOC_EVT, &gattc_cb);
1777     }
1778
1779     return;
1780  
1781 }
1782 void bta_gattc_process_api_cache_get_addr_list(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA *p_msg)
1783 {
1784     tBTA_GATTC gattc_cb = {0};
1785     tBTA_GATTC_RCB *p_clrcb = bta_gattc_cl_get_regcb(p_msg->api_get_addr.client_if);
1786     UINT8 num_addr = bta_gattc_co_get_addr_num();
1787     gattc_cb.get_addr_list.client_if = p_msg->api_get_addr.client_if;
1788
1789     if (num_addr != 0) {
1790         gattc_cb.get_addr_list.num_addr = num_addr;
1791         gattc_cb.get_addr_list.bda_list = (BD_ADDR *)osi_malloc(sizeof(BD_ADDR)*num_addr);
1792         if (gattc_cb.get_addr_list.bda_list != NULL) {
1793             bta_gattc_co_get_addr_list(gattc_cb.get_addr_list.bda_list);
1794             gattc_cb.get_addr_list.status = BTA_GATT_OK;
1795         } else {
1796             gattc_cb.get_addr_list.status = BTA_GATT_ERROR;
1797         }
1798     } else {
1799         gattc_cb.get_addr_list.status = BTA_GATT_NOT_FOUND;
1800     }
1801
1802     if (p_clrcb != NULL) {
1803         (* p_clrcb->p_cback)(BTA_GATTC_GET_ADDR_LIST_EVT, &gattc_cb);
1804     }
1805
1806     //release the address list buffer after used.
1807     if (gattc_cb.get_addr_list.bda_list != NULL) {
1808         osi_free((void *)gattc_cb.get_addr_list.bda_list);
1809     }
1810
1811 }
1812 /*******************************************************************************
1813 **
1814 ** Function         bta_gattc_process_srvc_chg_ind
1815 **
1816 ** Description      process service change indication.
1817 **
1818 ** Returns          None.
1819 **
1820 *******************************************************************************/
1821 BOOLEAN bta_gattc_process_srvc_chg_ind(UINT16 conn_id,
1822                                        tBTA_GATTC_RCB      *p_clrcb,
1823                                        tBTA_GATTC_SERV     *p_srcb,
1824                                        tBTA_GATTC_CLCB      *p_clcb,
1825                                        tBTA_GATTC_NOTIFY    *p_notify,
1826                                        tGATT_VALUE *att_value)
1827 {
1828     tBT_UUID        gattp_uuid, srvc_chg_uuid;
1829     BOOLEAN         processed = FALSE;
1830     UINT8           i;
1831
1832     gattp_uuid.len = 2;
1833     gattp_uuid.uu.uuid16 = UUID_SERVCLASS_GATT_SERVER;
1834
1835     srvc_chg_uuid.len = 2;
1836     srvc_chg_uuid.uu.uuid16 = GATT_UUID_GATT_SRV_CHGD;
1837
1838     const tBTA_GATTC_CHARACTERISTIC *p_char = bta_gattc_get_characteristic_srcb(p_srcb, p_notify->handle);
1839     if (p_char && bta_gattc_uuid_compare(&p_char->service->uuid, &gattp_uuid, TRUE) &&
1840         bta_gattc_uuid_compare(&p_char->uuid, &srvc_chg_uuid, TRUE)) {
1841         if (att_value->len != BTA_GATTC_SERVICE_CHANGED_LEN) {
1842             APPL_TRACE_ERROR("%s: received malformed service changed indication, skipping", __func__);
1843             return FALSE;
1844         }
1845
1846         UINT8 *p = att_value->value;
1847         UINT16 s_handle = ((UINT16)(*(p    )) + (((UINT16)(*(p + 1))) << 8));
1848         UINT16 e_handle = ((UINT16)(*(p + 2)) + (((UINT16)(*(p + 3))) << 8));
1849
1850         APPL_TRACE_DEBUG("%s: service changed s_handle:0x%04x e_handle:0x%04x",
1851                          __func__, s_handle, e_handle);
1852
1853         processed = TRUE;
1854         /* mark service handle change pending */
1855         p_srcb->srvc_hdl_chg = TRUE;
1856         /* clear up all notification/indication registration */
1857         bta_gattc_clear_notif_registration(p_srcb, conn_id, s_handle, e_handle);
1858         /* service change indication all received, do discovery update */
1859         if ( ++ p_srcb->update_count == bta_gattc_num_reg_app()) {
1860             /* not an opened connection; or connection busy */
1861             /* search for first available clcb and start discovery */
1862             if (p_clcb == NULL || (p_clcb && p_clcb->p_q_cmd != NULL)) {
1863                 for (i = 0 ; i < BTA_GATTC_CLCB_MAX; i ++) {
1864                     if (bta_gattc_cb.clcb[i].in_use &&
1865                             bta_gattc_cb.clcb[i].p_srcb == p_srcb &&
1866                             bta_gattc_cb.clcb[i].p_q_cmd == NULL) {
1867                         p_clcb = &bta_gattc_cb.clcb[i];
1868                         break;
1869                     }
1870                 }
1871             }
1872             /* send confirmation here if this is an indication, it should always be */
1873             GATTC_SendHandleValueConfirm(conn_id, att_value->handle);
1874
1875             /* if connection available, refresh cache by doing discovery now */
1876             if (p_clcb != NULL) {
1877                 bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_DISCOVER_EVT, NULL);
1878             }
1879         }
1880         /* notify applicationf or service change */
1881         if (p_clrcb->p_cback != NULL) {
1882             tBTA_GATTC_SERVICE_CHANGE srvc_chg= {0};
1883             memcpy(srvc_chg.remote_bda, p_srcb->server_bda, sizeof(BD_ADDR));
1884             srvc_chg.conn_id = conn_id;
1885             (* p_clrcb->p_cback)(BTA_GATTC_SRVC_CHG_EVT, (tBTA_GATTC *)&srvc_chg);
1886         }
1887
1888     }
1889
1890     return processed;
1891
1892 }
1893 /*******************************************************************************
1894 **
1895 ** Function         bta_gattc_proc_other_indication
1896 **
1897 ** Description      process all non-service change indication/notification.
1898 **
1899 ** Returns          None.
1900 **
1901 *******************************************************************************/
1902 void bta_gattc_proc_other_indication(tBTA_GATTC_CLCB *p_clcb, UINT8 op,
1903                                      tGATT_CL_COMPLETE *p_data,
1904                                      tBTA_GATTC_NOTIFY *p_notify)
1905 {
1906     APPL_TRACE_DEBUG("bta_gattc_proc_other_indication check p_data->att_value.handle=%d p_data->handle=%d",
1907                        p_data->att_value.handle, p_data->handle);
1908     APPL_TRACE_DEBUG("is_notify %d", p_notify->is_notify);
1909
1910     p_notify->is_notify = (op == GATTC_OPTYPE_INDICATION) ? FALSE : TRUE;
1911     p_notify->len = p_data->att_value.len;
1912     bdcpy(p_notify->bda, p_clcb->bda);
1913     memcpy(p_notify->value, p_data->att_value.value, p_data->att_value.len);
1914     p_notify->conn_id = p_clcb->bta_conn_id;
1915
1916     if (p_clcb->p_rcb->p_cback) {
1917         (*p_clcb->p_rcb->p_cback)(BTA_GATTC_NOTIF_EVT,  (tBTA_GATTC *)p_notify);
1918     }
1919
1920 }
1921 /*******************************************************************************
1922 **
1923 ** Function         bta_gattc_process_indicate
1924 **
1925 ** Description      process indication/notification.
1926 **
1927 ** Returns          None.
1928 **
1929 *******************************************************************************/
1930 void bta_gattc_process_indicate(UINT16 conn_id, tGATTC_OPTYPE op, tGATT_CL_COMPLETE *p_data)
1931 {
1932     UINT16              handle = p_data->att_value.handle;
1933     tBTA_GATTC_CLCB     *p_clcb ;
1934     tBTA_GATTC_RCB      *p_clrcb = NULL;
1935     tBTA_GATTC_SERV     *p_srcb = NULL;
1936     tBTA_GATTC_NOTIFY   notify;
1937     BD_ADDR             remote_bda;
1938     tBTA_GATTC_IF       gatt_if;
1939     tBTA_TRANSPORT transport;
1940
1941     if (!GATT_GetConnectionInfor(conn_id, &gatt_if, remote_bda, &transport)) {
1942         APPL_TRACE_ERROR("%s indication/notif for unknown app", __func__);
1943         if (op == GATTC_OPTYPE_INDICATION) {
1944             GATTC_SendHandleValueConfirm(conn_id, handle);
1945         }
1946         return;
1947     }
1948
1949     if ((p_clrcb = bta_gattc_cl_get_regcb(gatt_if)) == NULL) {
1950         APPL_TRACE_ERROR("%s indication/notif for unregistered app", __func__);
1951         if (op == GATTC_OPTYPE_INDICATION) {
1952             GATTC_SendHandleValueConfirm(conn_id, handle);
1953         }
1954         return;
1955     }
1956
1957     if ((p_srcb = bta_gattc_find_srcb(remote_bda)) == NULL) {
1958         APPL_TRACE_ERROR("%s indication/notif for unknown device, ignore", __func__);
1959         if (op == GATTC_OPTYPE_INDICATION) {
1960             GATTC_SendHandleValueConfirm(conn_id, handle);
1961         }
1962         return;
1963     }
1964
1965     p_clcb = bta_gattc_find_clcb_by_conn_id(conn_id);
1966
1967     notify.handle = handle;
1968     /* if non-service change indication/notification, forward to application */
1969     if (!bta_gattc_process_srvc_chg_ind(conn_id, p_clrcb, p_srcb, p_clcb, &notify, &p_data->att_value)) {
1970         /* if app registered for the notification */
1971         if (bta_gattc_check_notif_registry(p_clrcb, p_srcb, &notify)) {
1972             /* connection not open yet */
1973             if (p_clcb == NULL) {
1974                 p_clcb = bta_gattc_clcb_alloc(gatt_if, remote_bda, transport);
1975
1976                 if (p_clcb == NULL) {
1977                     APPL_TRACE_ERROR("No resources");
1978                     return;
1979                 }
1980
1981                 p_clcb->bta_conn_id = conn_id;
1982                 p_clcb->transport   = transport;
1983
1984                 bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_CONN_EVT, NULL);
1985             }
1986
1987             if (p_clcb != NULL) {
1988                 bta_gattc_proc_other_indication(p_clcb, op, p_data, &notify);
1989             }
1990         } else if (op == GATTC_OPTYPE_INDICATION) {
1991             /* no one intersted and need ack? */
1992             APPL_TRACE_DEBUG("%s no one interested, ack now", __func__);
1993             GATTC_SendHandleValueConfirm(conn_id, handle);
1994         }
1995     }
1996 }
1997 /*******************************************************************************
1998 **
1999 ** Function         bta_gattc_cmpl_cback
2000 **
2001 ** Description      client operation complete callback register with BTE GATT.
2002 **
2003 ** Returns          None.
2004 **
2005 *******************************************************************************/
2006 static void  bta_gattc_cmpl_cback(UINT16 conn_id, tGATTC_OPTYPE op, tGATT_STATUS status,
2007                                   tGATT_CL_COMPLETE *p_data)
2008 {
2009     tBTA_GATTC_CLCB     *p_clcb;
2010     APPL_TRACE_DEBUG("bta_gattc_cmpl_cback: conn_id = %d op = %d status = %d",
2011                      conn_id, op, status);
2012
2013     /* notification and indication processed right away */
2014     if (op == GATTC_OPTYPE_NOTIFICATION || op == GATTC_OPTYPE_INDICATION) {
2015         bta_gattc_process_indicate(conn_id, op, p_data);
2016         return;
2017     }
2018     /* for all other operation, not expected if w/o connection */
2019     else if ((p_clcb = bta_gattc_find_clcb_by_conn_id(conn_id)) == NULL) {
2020         APPL_TRACE_ERROR("bta_gattc_cmpl_cback unknown conn_id =  %d, ignore data", conn_id);
2021         return;
2022     }
2023
2024     /* if over BR_EDR, inform PM for mode change */
2025     if (p_clcb->transport == BTA_TRANSPORT_BR_EDR) {
2026         bta_sys_busy(BTA_ID_GATTC, BTA_ALL_APP_ID, p_clcb->bda);
2027         bta_sys_idle(BTA_ID_GATTC, BTA_ALL_APP_ID, p_clcb->bda);
2028     }
2029
2030     bta_gattc_cmpl_sendmsg(conn_id, op, status, p_data);
2031 }
2032
2033 /*******************************************************************************
2034 **
2035 ** Function         bta_gattc_cmpl_sendmsg
2036 **
2037 ** Description      client operation complete send message
2038 **
2039 ** Returns          None.
2040 **
2041 *******************************************************************************/
2042 static void bta_gattc_cmpl_sendmsg(UINT16 conn_id, tGATTC_OPTYPE op,
2043                                    tBTA_GATT_STATUS status,
2044                                    tGATT_CL_COMPLETE *p_data)
2045 {
2046     const UINT16         len = sizeof(tBTA_GATTC_OP_CMPL) + sizeof(tGATT_CL_COMPLETE);
2047     tBTA_GATTC_OP_CMPL  *p_buf = (tBTA_GATTC_OP_CMPL *) osi_malloc(len);
2048
2049     if (p_buf != NULL) {
2050         memset(p_buf, 0, len);
2051         p_buf->hdr.event = BTA_GATTC_OP_CMPL_EVT;
2052         p_buf->hdr.layer_specific = conn_id;
2053         p_buf->status = status;
2054         p_buf->op_code = op;
2055
2056         if (p_data != NULL) {
2057             p_buf->p_cmpl = (tGATT_CL_COMPLETE *)(p_buf + 1);
2058             memcpy(p_buf->p_cmpl, p_data, sizeof(tGATT_CL_COMPLETE));
2059         }
2060
2061         bta_sys_sendmsg(p_buf);
2062     }
2063 }
2064
2065 /*******************************************************************************
2066 **
2067 ** Function         bta_gattc_cong_cback
2068 **
2069 ** Description      congestion callback for BTA GATT client.
2070 **
2071 ** Returns          void
2072 **
2073 ********************************************************************************/
2074 static void bta_gattc_cong_cback (UINT16 conn_id, BOOLEAN congested)
2075 {
2076     tBTA_GATTC_CLCB *p_clcb;
2077     tBTA_GATTC cb_data;
2078
2079     if ((p_clcb = bta_gattc_find_clcb_by_conn_id(conn_id)) != NULL) {
2080         if (p_clcb->p_rcb->p_cback) {
2081             cb_data.congest.conn_id = conn_id;
2082             cb_data.congest.congested = congested;
2083
2084             (*p_clcb->p_rcb->p_cback)(BTA_GATTC_CONGEST_EVT, &cb_data);
2085         }
2086     }
2087 }
2088
2089 /*******************************************************************************
2090 **
2091 ** Function         bta_gattc_req_cback
2092 **
2093 ** Description      GATT request command callback for BTA GATT client.
2094 **
2095 ** Returns          void
2096 **
2097 ********************************************************************************/
2098 static void bta_gattc_req_cback (UINT16 conn_id, UINT32 trans_id, tGATTS_REQ_TYPE type, tGATTS_DATA *p_data)
2099 {
2100     /* GATTC doesn't need to process the GATT request commands.
2101      * Add this callback here to avoid the warning "Call back not found for application"
2102      * printed in the function gatt_sr_send_req_callback
2103      * */
2104     UNUSED (conn_id);
2105     UNUSED (trans_id) ;
2106     UNUSED (type);
2107     UNUSED (p_data);
2108 }
2109
2110 #if BLE_INCLUDED == TRUE
2111 /*******************************************************************************
2112 **
2113 ** Function         bta_gattc_init_clcb_conn
2114 **
2115 ** Description      Initaite a BTA CLCB connection
2116 **
2117 ** Returns          void
2118 **
2119 ********************************************************************************/
2120 void bta_gattc_init_clcb_conn(UINT8 cif, BD_ADDR remote_bda)
2121 {
2122     tBTA_GATTC_CLCB     *p_clcb = NULL;
2123     tBTA_GATTC_DATA     gattc_data;
2124     UINT16              conn_id;
2125
2126     /* should always get the connection ID */
2127     if (GATT_GetConnIdIfConnected(cif, remote_bda, &conn_id, BTA_GATT_TRANSPORT_LE) == FALSE) {
2128         APPL_TRACE_ERROR("bta_gattc_init_clcb_conn ERROR: not a connected device");
2129         return;
2130     }
2131
2132     /* initaite a new connection here */
2133     if ((p_clcb = bta_gattc_clcb_alloc(cif, remote_bda, BTA_GATT_TRANSPORT_LE)) != NULL) {
2134         gattc_data.hdr.layer_specific = p_clcb->bta_conn_id = conn_id;
2135
2136         gattc_data.api_conn.client_if = cif;
2137         memcpy(gattc_data.api_conn.remote_bda, remote_bda, BD_ADDR_LEN);
2138         gattc_data.api_conn.is_direct = TRUE;
2139
2140         bta_gattc_sm_execute(p_clcb, BTA_GATTC_API_OPEN_EVT, &gattc_data);
2141     } else {
2142         APPL_TRACE_ERROR("No resources");
2143     }
2144 }
2145 /*******************************************************************************
2146 **
2147 ** Function         bta_gattc_process_listen_all
2148 **
2149 ** Description      process listen all, send open callback to application for all
2150 **                  connected slave LE link.
2151 **
2152 ** Returns          void
2153 **
2154 ********************************************************************************/
2155 void bta_gattc_process_listen_all(UINT8 cif)
2156 {
2157     UINT8               i_conn = 0;
2158     tBTA_GATTC_CONN     *p_conn = &bta_gattc_cb.conn_track[0];
2159
2160     for (i_conn = 0; i_conn < BTA_GATTC_CONN_MAX; i_conn++, p_conn ++) {
2161         if (p_conn->in_use ) {
2162             if (bta_gattc_find_clcb_by_cif(cif, p_conn->remote_bda, BTA_GATT_TRANSPORT_LE) == NULL) {
2163                 bta_gattc_init_clcb_conn(cif, p_conn->remote_bda);
2164             }
2165             /* else already connected */
2166         }
2167     }
2168 }
2169 /*******************************************************************************
2170 **
2171 ** Function         bta_gattc_listen
2172 **
2173 ** Description      Start or stop a listen for connection
2174 **
2175 ** Returns          void
2176 **
2177 ********************************************************************************/
2178 void bta_gattc_listen(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA *p_msg)
2179 {
2180     tBTA_GATTC_RCB      *p_clreg = bta_gattc_cl_get_regcb(p_msg->api_listen.client_if);
2181     tBTA_GATTC          cb_data;
2182     UNUSED(p_cb);
2183
2184     cb_data.reg_oper.status = BTA_GATT_ERROR;
2185     cb_data.reg_oper.client_if = p_msg->api_listen.client_if;
2186
2187     if (p_clreg == NULL) {
2188         APPL_TRACE_ERROR("bta_gattc_listen failed, unknown client_if: %d",
2189                          p_msg->api_listen.client_if);
2190         return;
2191     }
2192     /* mark bg conn record */
2193     if (bta_gattc_mark_bg_conn(p_msg->api_listen.client_if,
2194                                (BD_ADDR_PTR) p_msg->api_listen.remote_bda,
2195                                p_msg->api_listen.start,
2196                                TRUE)) {
2197         if (!GATT_Listen(p_msg->api_listen.client_if,
2198                          p_msg->api_listen.start,
2199                          p_msg->api_listen.remote_bda)) {
2200             APPL_TRACE_ERROR("Listen failure");
2201             (*p_clreg->p_cback)(BTA_GATTC_LISTEN_EVT, &cb_data);
2202         } else {
2203             cb_data.status = BTA_GATT_OK;
2204
2205             (*p_clreg->p_cback)(BTA_GATTC_LISTEN_EVT, &cb_data);
2206
2207             if (p_msg->api_listen.start) {
2208                 /* if listen to a specific target */
2209                 if (p_msg->api_listen.remote_bda != NULL) {
2210
2211                     /* if is a connected remote device */
2212                     if (L2CA_GetBleConnRole(p_msg->api_listen.remote_bda) == HCI_ROLE_SLAVE &&
2213                             bta_gattc_find_clcb_by_cif(p_msg->api_listen.client_if,
2214                                                        p_msg->api_listen.remote_bda,
2215                                                        BTA_GATT_TRANSPORT_LE) == NULL) {
2216
2217                         bta_gattc_init_clcb_conn(p_msg->api_listen.client_if,
2218                                                  p_msg->api_listen.remote_bda);
2219                     }
2220                 }
2221                 /* if listen to all */
2222                 else {
2223                     APPL_TRACE_DEBUG("Listen For All now");
2224                     /* go through all connected device and send
2225                     callback for all connected slave connection */
2226                     bta_gattc_process_listen_all(p_msg->api_listen.client_if);
2227                 }
2228             }
2229         }
2230     }
2231 }
2232
2233 /*******************************************************************************
2234 **
2235 ** Function         bta_gattc_broadcast
2236 **
2237 ** Description      Start or stop broadcasting
2238 **
2239 ** Returns          void
2240 **
2241 ********************************************************************************/
2242 void bta_gattc_broadcast(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA *p_msg)
2243 {
2244     tBTA_GATTC_RCB      *p_clreg = bta_gattc_cl_get_regcb(p_msg->api_listen.client_if);
2245     tBTA_GATTC          cb_data;
2246     UNUSED(p_cb);
2247
2248     cb_data.reg_oper.client_if = p_msg->api_listen.client_if;
2249     cb_data.reg_oper.status = BTM_BleBroadcast(p_msg->api_listen.start, NULL);
2250     //TODO need modify callback if used
2251     if (p_clreg && p_clreg->p_cback) {
2252         (*p_clreg->p_cback)(BTA_GATTC_LISTEN_EVT, &cb_data);
2253     }
2254 }
2255
2256 /*******************************************************************************
2257 **
2258 ** Function         bta_gattc_register_service_change_notify
2259 **
2260 ** Description      Find remote device's gatt service change characteristic ccc's handle and write 2 to this
2261 **                  this ccc.
2262 **
2263 ** Returns          Return result of service change ccc service discovery result
2264 **
2265 *******************************************************************************/
2266 tBTA_GATTC_FIND_SERVICE_CB bta_gattc_register_service_change_notify(UINT16 conn_id, BD_ADDR remote_bda)
2267 {
2268     tBTA_GATTC_SERV     *p_srcb = NULL;
2269     list_t              *p_cache = NULL;
2270     tBTA_GATTC_SERVICE *p_service = NULL;
2271     tBTA_GATTC_CHARACTERISTIC *p_char = NULL;
2272     tBTA_GATTC_DESCRIPTOR *p_desc = NULL;
2273     tBTA_GATTC_FIND_SERVICE_CB    result;
2274     BOOLEAN             gatt_cache_found = FALSE;
2275     BOOLEAN             gatt_service_found = FALSE;
2276     BOOLEAN             gatt_service_change_found = FALSE;
2277     BOOLEAN             gatt_ccc_found = FALSE;
2278
2279     tBT_UUID gatt_service_uuid = {LEN_UUID_16, {UUID_SERVCLASS_GATT_SERVER}};
2280     tBT_UUID gatt_service_change_uuid = {LEN_UUID_16, {GATT_UUID_GATT_SRV_CHGD}};
2281     tBT_UUID gatt_ccc_uuid = {LEN_UUID_16, {GATT_UUID_CHAR_CLIENT_CONFIG}};
2282
2283     p_srcb = bta_gattc_find_srcb(remote_bda);
2284     if ((p_srcb != NULL) && (p_srcb->p_srvc_cache != NULL)) {
2285         p_cache = p_srcb->p_srvc_cache;
2286         gatt_cache_found = TRUE;
2287     }
2288     else {
2289         result = SERVICE_CHANGE_CACHE_NOT_FOUND;
2290     }
2291     /* start to find gatt service */
2292     if (gatt_cache_found == TRUE) {
2293         for (list_node_t *sn = list_begin(p_cache);
2294              sn != list_end(p_cache); sn = list_next(sn)) {
2295             p_service = list_node(sn);
2296             if (bta_gattc_uuid_compare(&gatt_service_uuid, &p_service->uuid, TRUE)) {
2297                 gatt_service_found = TRUE;
2298                 break;
2299             }
2300         }
2301     }
2302     else {
2303         result = SERVICE_CHANGE_CACHE_NOT_FOUND;
2304     }
2305
2306     /* start to find gatt service change characteristic */
2307     if (gatt_service_found == TRUE) {
2308         if (p_service->characteristics) {
2309             for (list_node_t *cn = list_begin(p_service->characteristics);
2310                  cn != list_end(p_service->characteristics); cn = list_next(cn)) {
2311                 p_char = list_node(cn);
2312                 if (bta_gattc_uuid_compare(&gatt_service_change_uuid, &p_char->uuid, TRUE)) {
2313                     gatt_service_change_found = TRUE;
2314                     break;
2315                 }
2316             }
2317         }
2318     }
2319     else if (gatt_cache_found == TRUE) {
2320         /* Gatt service not found, start a timer to wait for service discovery */
2321         result = SERVICE_CHANGE_SERVICE_NOT_FOUND;
2322     }
2323     /* start to find gatt service change characteristic ccc */
2324     if (gatt_service_change_found == TRUE) {
2325         if (p_char->descriptors) {
2326             for (list_node_t *dn = list_begin(p_char->descriptors);
2327                  dn != list_end(p_char->descriptors); dn = list_next(dn)) {
2328                 p_desc = list_node(dn);
2329                 if (bta_gattc_uuid_compare(&gatt_ccc_uuid, &p_desc->uuid, TRUE)) {
2330                     gatt_ccc_found = TRUE;
2331                     break;
2332                 }
2333             }
2334         }
2335     }
2336     else if (gatt_service_found ==TRUE) {
2337         /* Gatt service found, but service change char not found,
2338          * Case1: remote device doesn't have service change char, we don't need to start a timer here to
2339          * wait for service discovery
2340          * Case2: remote device exist service change char, we have found gatt service, but have not found
2341          * service change char, we need to start a timer here*/
2342         result = SERVICE_CHANGE_CHAR_NOT_FOUND;
2343     }
2344
2345     if (gatt_ccc_found == TRUE){
2346         tBTA_GATTC_CONN *p_conn = bta_gattc_conn_find_alloc(remote_bda);
2347         if (p_conn) {
2348             p_conn->svc_change_descr_handle = p_desc->handle;
2349         }
2350         result = SERVICE_CHANGE_CCC_WRITTEN_SUCCESS;
2351         uint16_t indicate_value = GATT_CLT_CONFIG_INDICATION;
2352         tBTA_GATT_UNFMT indicate_v;
2353         indicate_v.len = 2;
2354         indicate_v.p_value = (uint8_t *)&indicate_value;
2355         BTA_GATTC_WriteCharDescr (conn_id, p_desc->handle, BTA_GATTC_TYPE_WRITE, &indicate_v, BTA_GATT_AUTH_REQ_NONE);
2356
2357     }
2358     else if (gatt_service_change_found == TRUE) {
2359         /* Gatt service char found, but service change char ccc not found,
2360          * Case1: remote device doesn't have service change char ccc, we don't need to start a timer here to
2361          * wait for service discovery
2362          * Case2: remote device exist service change char ccc, we have found gatt service change char, but have not found
2363          * service change char ccc, we need to start a timer here */
2364         result = SERVICE_CHANGE_CCC_NOT_FOUND;
2365     }
2366
2367     return result;
2368 }
2369
2370 #endif
2371 #endif  ///GATTC_INCLUDED == TRUE && BLE_INCLUDED == TRUE