OSDN Git Service

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