OSDN Git Service

am f060365b: Add a SCO routing command to net_hci.
[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("Discovery cancel conn_id=%d",p_clcb->bta_conn_id);
860     if (p_clcb->disc_active)
861         bta_gattc_reset_discover_st(p_clcb->p_srcb, BTA_GATT_ERROR);
862     else
863         p_clcb->state = BTA_GATTC_CONN_ST;
864 }
865 /*******************************************************************************
866 **
867 ** Function         bta_gattc_set_discover_st
868 **
869 ** Description      when a SRCB start discovery, tell all related clcb and set
870 **                  the state.
871 **
872 ** Returns          None.
873 **
874 *******************************************************************************/
875 void bta_gattc_set_discover_st(tBTA_GATTC_SERV *p_srcb)
876 {
877     tBTA_GATTC_CB   *p_cb = &bta_gattc_cb;
878     UINT8   i;
879
880 #if BLE_INCLUDED == TRUE
881     L2CA_EnableUpdateBleConnParams(p_srcb->server_bda, FALSE);
882 #endif
883     for (i = 0; i < BTA_GATTC_CLCB_MAX; i ++)
884     {
885         if (p_cb->clcb[i].p_srcb == p_srcb)
886         {
887             p_cb->clcb[i].status = BTA_GATT_OK;
888             p_cb->clcb[i].state = BTA_GATTC_DISCOVER_ST;
889         }
890     }
891 }
892 /*******************************************************************************
893 **
894 ** Function         bta_gattc_restart_discover
895 **
896 ** Description      process service change in discovery state, mark up the auto
897 **                  update flag and set status to be discovery cancel for current
898 **                  discovery.
899 **
900 ** Returns          None.
901 **
902 *******************************************************************************/
903 void bta_gattc_restart_discover(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
904 {
905     UNUSED(p_data);
906
907     p_clcb->status      = BTA_GATT_CANCEL;
908     p_clcb->auto_update = BTA_GATTC_DISC_WAITING;
909 }
910
911 /*******************************************************************************
912 **
913 ** Function         bta_gattc_cfg_mtu
914 **
915 ** Description      Configure MTU size on the GATT connection.
916 **
917 ** Returns          None.
918 **
919 *******************************************************************************/
920 void bta_gattc_cfg_mtu(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
921 {
922     tBTA_GATTC_OP_CMPL  op_cmpl;
923     tBTA_GATT_STATUS    status;
924
925     if (bta_gattc_enqueue(p_clcb, p_data))
926     {
927         status = GATTC_ConfigureMTU (p_clcb->bta_conn_id, p_data->api_mtu.mtu);
928
929         /* if failed, return callback here */
930         if (status != GATT_SUCCESS && status != GATT_CMD_STARTED)
931         {
932             memset(&op_cmpl, 0, sizeof(tBTA_GATTC_OP_CMPL));
933
934             op_cmpl.status  = status;
935             op_cmpl.op_code = GATTC_OPTYPE_CONFIG;
936             op_cmpl.p_cmpl  = NULL;
937
938             bta_gattc_sm_execute(p_clcb, BTA_GATTC_OP_CMPL_EVT, (tBTA_GATTC_DATA *)&op_cmpl);
939         }
940     }
941 }
942 /*******************************************************************************
943 **
944 ** Function         bta_gattc_start_discover
945 **
946 ** Description      Start a discovery on server.
947 **
948 ** Returns          None.
949 **
950 *******************************************************************************/
951 void bta_gattc_start_discover(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
952 {
953     UNUSED(p_data);
954
955     APPL_TRACE_DEBUG("bta_gattc_start_discover conn_id=%d p_clcb->p_srcb->state = %d ",
956         p_clcb->bta_conn_id, p_clcb->p_srcb->state);
957
958     if (((p_clcb->p_q_cmd == NULL || p_clcb->auto_update == BTA_GATTC_REQ_WAITING) &&
959         p_clcb->p_srcb->state == BTA_GATTC_SERV_IDLE) ||
960         p_clcb->p_srcb->state == BTA_GATTC_SERV_DISC)
961     /* no pending operation, start discovery right away */
962     {
963         p_clcb->auto_update = BTA_GATTC_NO_SCHEDULE;
964
965         if (p_clcb->p_srcb != NULL)
966         {
967             /* clear the service change mask */
968             p_clcb->p_srcb->srvc_hdl_chg = FALSE;
969             p_clcb->p_srcb->update_count = 0;
970             p_clcb->p_srcb->state = BTA_GATTC_SERV_DISC_ACT;
971
972             if (p_clcb->transport == BTA_TRANSPORT_LE)
973                 L2CA_EnableUpdateBleConnParams(p_clcb->p_srcb->server_bda, FALSE);
974
975             /* set all srcb related clcb into discovery ST */
976             bta_gattc_set_discover_st(p_clcb->p_srcb);
977
978             if ((p_clcb->status = bta_gattc_init_cache(p_clcb->p_srcb)) == BTA_GATT_OK)
979             {
980                 p_clcb->status = bta_gattc_discover_pri_service(p_clcb->bta_conn_id,
981                                                                p_clcb->p_srcb, GATT_DISC_SRVC_ALL);
982             }
983             if (p_clcb->status != BTA_GATT_OK)
984             {
985                 APPL_TRACE_ERROR("discovery on server failed");
986                 bta_gattc_reset_discover_st(p_clcb->p_srcb, p_clcb->status);
987             }
988             else
989                 p_clcb->disc_active = TRUE;
990         }
991         else
992         {
993             APPL_TRACE_ERROR("unknown device, can not start discovery");
994         }
995     }
996     /* pending operation, wait until it finishes */
997     else
998     {
999         p_clcb->auto_update = BTA_GATTC_DISC_WAITING;
1000
1001         if (p_clcb->p_srcb->state == BTA_GATTC_SERV_IDLE)
1002             p_clcb->state = BTA_GATTC_CONN_ST; /* set clcb state */
1003     }
1004
1005 }
1006 /*******************************************************************************
1007 **
1008 ** Function         bta_gattc_disc_cmpl
1009 **
1010 ** Description      discovery on server is finished
1011 **
1012 ** Returns          None.
1013 **
1014 *******************************************************************************/
1015 void bta_gattc_disc_cmpl(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1016 {
1017     tBTA_GATTC_DATA *p_q_cmd = p_clcb->p_q_cmd;
1018     UNUSED(p_data);
1019
1020     APPL_TRACE_DEBUG("bta_gattc_disc_cmpl conn_id=%d",p_clcb->bta_conn_id);
1021
1022 #if BLE_INCLUDED == TRUE
1023     if(p_clcb->transport == BTA_TRANSPORT_LE)
1024         L2CA_EnableUpdateBleConnParams(p_clcb->p_srcb->server_bda, TRUE);
1025 #endif
1026     p_clcb->p_srcb->state = BTA_GATTC_SERV_IDLE;
1027     p_clcb->disc_active = FALSE;
1028
1029     if (p_clcb->status != GATT_SUCCESS)
1030     {
1031         /* clean up cache */
1032         if(p_clcb->p_srcb && p_clcb->p_srcb->p_srvc_cache)
1033         {
1034             while (!GKI_queue_is_empty(&p_clcb->p_srcb->cache_buffer))
1035             {
1036                 GKI_freebuf (GKI_dequeue (&p_clcb->p_srcb->cache_buffer));
1037             }
1038             p_clcb->p_srcb->p_srvc_cache = NULL;
1039         }
1040
1041         /* used to reset cache in application */
1042         bta_gattc_co_cache_reset(p_clcb->p_srcb->server_bda);
1043     }
1044     /* release pending attribute list buffer */
1045     utl_freebuf((void **)&p_clcb->p_srcb->p_srvc_list);
1046
1047     if (p_clcb->auto_update == BTA_GATTC_DISC_WAITING)
1048     {
1049         /* start discovery again */
1050         bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_DISCOVER_EVT, NULL);
1051     }
1052     /* get any queued command to proceed */
1053     else if (p_q_cmd != NULL)
1054     {
1055         p_clcb->p_q_cmd = NULL;
1056
1057         bta_gattc_sm_execute(p_clcb, p_q_cmd->hdr.event, p_q_cmd);
1058         /* if the command executed requeued the cmd, we don't
1059          * want to free the underlying buffer that's being
1060          * referenced by p_clcb->p_q_cmd
1061          */
1062         if (p_q_cmd != p_clcb->p_q_cmd) {
1063             utl_freebuf((void **)&p_q_cmd);
1064         }
1065     }
1066 }
1067 /*******************************************************************************
1068 **
1069 ** Function         bta_gattc_read
1070 **
1071 ** Description      Read an attribute
1072 **
1073 ** Returns          None.
1074 **
1075 *******************************************************************************/
1076 void bta_gattc_read(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1077 {
1078     UINT16 handle = 0;
1079     tGATT_READ_PARAM    read_param;
1080     tBTA_GATTC_OP_CMPL  op_cmpl;
1081
1082     memset (&read_param, 0 ,sizeof(tGATT_READ_PARAM));
1083     memset (&op_cmpl, 0 ,sizeof(tBTA_GATTC_OP_CMPL));
1084
1085     if (bta_gattc_enqueue(p_clcb, p_data))
1086     {
1087         if ((handle = bta_gattc_id2handle(p_clcb->p_srcb,
1088                                           &p_data->api_read.srvc_id,
1089                                           &p_data->api_read.char_id,
1090                                           p_data->api_read.p_descr_type)) == 0)
1091         {
1092             op_cmpl.status = BTA_GATT_ERROR;
1093         }
1094         else
1095         {
1096             read_param.by_handle.handle = handle;
1097             read_param.by_handle.auth_req = p_data->api_read.auth_req;
1098
1099             op_cmpl.status = GATTC_Read(p_clcb->bta_conn_id, GATT_READ_BY_HANDLE, &read_param);
1100         }
1101
1102         /* read fail */
1103         if (op_cmpl.status != BTA_GATT_OK)
1104         {
1105             op_cmpl.op_code = GATTC_OPTYPE_READ;
1106             op_cmpl.p_cmpl = NULL;
1107
1108             bta_gattc_sm_execute(p_clcb, BTA_GATTC_OP_CMPL_EVT, (tBTA_GATTC_DATA *)&op_cmpl);
1109         }
1110     }
1111 }
1112 /*******************************************************************************
1113 **
1114 ** Function         bta_gattc_read_multi
1115 **
1116 ** Description      read multiple
1117 **
1118 ** Returns          None.
1119 *********************************************************************************/
1120 void bta_gattc_read_multi(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1121 {
1122     UINT16              i, handle;
1123     tBTA_GATT_STATUS    status = BTA_GATT_OK;
1124     tGATT_READ_PARAM    read_param;
1125     tBTA_GATTC_OP_CMPL  op_cmpl;
1126     tBTA_GATTC_ATTR_ID  *p_id;
1127
1128     if (bta_gattc_enqueue(p_clcb, p_data))
1129     {
1130         memset(&read_param, 0, sizeof(tGATT_READ_PARAM));
1131
1132         p_id = p_data->api_read_multi.p_id_list;
1133
1134         for (i = 0; i < p_data->api_read_multi.num_attr && p_id; i ++, p_id ++)
1135         {
1136             handle = 0;
1137
1138             if (p_id->id_type == BTA_GATT_TYPE_CHAR)
1139             {
1140                 handle = bta_gattc_id2handle(p_clcb->p_srcb,
1141                                      &p_id->id_value.char_id.srvc_id,
1142                                      &p_id->id_value.char_id.char_id,
1143                                      NULL);
1144             }
1145             else if (p_id->id_type == BTA_GATT_TYPE_CHAR_DESCR)
1146             {
1147                 handle = bta_gattc_id2handle(p_clcb->p_srcb,
1148                                      &p_id->id_value.char_descr_id.char_id.srvc_id,
1149                                      &p_id->id_value.char_descr_id.char_id.char_id,
1150                                      &p_id->id_value.char_descr_id.descr_id);
1151             }
1152             else
1153             {
1154                 APPL_TRACE_ERROR("invalud ID type: %d", p_id->id_type);
1155             }
1156
1157             if (handle == 0)
1158             {
1159                 status = BTA_GATT_ERROR;
1160                 break;
1161             }
1162         }
1163         if (status == BTA_GATT_OK)
1164         {
1165             read_param.read_multiple.num_handles = p_data->api_read_multi.num_attr;
1166             read_param.read_multiple.auth_req = p_data->api_read_multi.auth_req;
1167
1168             status = GATTC_Read(p_clcb->bta_conn_id, GATT_READ_MULTIPLE, &read_param);
1169         }
1170
1171         /* read fail */
1172         if (status != BTA_GATT_OK)
1173         {
1174             memset(&op_cmpl, 0, sizeof(tBTA_GATTC_OP_CMPL));
1175
1176             op_cmpl.status  = status;
1177             op_cmpl.op_code = GATTC_OPTYPE_READ;
1178             op_cmpl.p_cmpl  = NULL;
1179
1180             bta_gattc_sm_execute(p_clcb, BTA_GATTC_OP_CMPL_EVT, (tBTA_GATTC_DATA *)&op_cmpl);
1181         }
1182     }
1183 }
1184 /*******************************************************************************
1185 **
1186 ** Function         bta_gattc_write
1187 **
1188 ** Description      Write an attribute
1189 **
1190 ** Returns          None.
1191 **
1192 *******************************************************************************/
1193 void bta_gattc_write(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1194 {
1195     UINT16              handle = 0;
1196     tGATT_VALUE         attr = {0};
1197     tBTA_GATTC_OP_CMPL  op_cmpl;
1198     tBTA_GATT_STATUS    status = BTA_GATT_OK;
1199
1200     if (bta_gattc_enqueue(p_clcb, p_data))
1201     {
1202         if ((handle = bta_gattc_id2handle(p_clcb->p_srcb,
1203                                           &p_data->api_write.srvc_id,
1204                                           &p_data->api_write.char_id,
1205                                           p_data->api_write.p_descr_type)) == 0)
1206         {
1207             status = BTA_GATT_ERROR;
1208         }
1209         else
1210         {
1211             attr.handle= handle;
1212             attr.offset = p_data->api_write.offset;
1213             attr.len    = p_data->api_write.len;
1214             attr.auth_req = p_data->api_write.auth_req;
1215
1216             if (p_data->api_write.p_value)
1217                 memcpy(attr.value, p_data->api_write.p_value, p_data->api_write.len);
1218
1219             status = GATTC_Write(p_clcb->bta_conn_id, p_data->api_write.write_type, &attr);
1220         }
1221
1222         /* write fail */
1223         if (status != BTA_GATT_OK)
1224         {
1225             memset(&op_cmpl, 0, sizeof(tBTA_GATTC_OP_CMPL));
1226
1227             op_cmpl.status  = status;
1228             op_cmpl.op_code = GATTC_OPTYPE_WRITE;
1229             op_cmpl.p_cmpl  = NULL;
1230
1231             bta_gattc_sm_execute(p_clcb, BTA_GATTC_OP_CMPL_EVT, (tBTA_GATTC_DATA *)&op_cmpl);
1232         }
1233     }
1234 }
1235 /*******************************************************************************
1236 **
1237 ** Function         bta_gattc_execute
1238 **
1239 ** Description      send execute write
1240 **
1241 ** Returns          None.
1242 *********************************************************************************/
1243 void bta_gattc_execute(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1244 {
1245     tBTA_GATTC_OP_CMPL  op_cmpl;
1246     tBTA_GATT_STATUS    status;
1247
1248     if (bta_gattc_enqueue(p_clcb, p_data))
1249     {
1250         status = GATTC_ExecuteWrite(p_clcb->bta_conn_id, p_data->api_exec.is_execute);
1251
1252         if (status != BTA_GATT_OK)
1253         {
1254             memset(&op_cmpl, 0, sizeof(tBTA_GATTC_OP_CMPL));
1255
1256             op_cmpl.status  = status;
1257             op_cmpl.op_code = GATTC_OPTYPE_EXE_WRITE;
1258             op_cmpl.p_cmpl  = NULL;
1259
1260             bta_gattc_sm_execute(p_clcb, BTA_GATTC_OP_CMPL_EVT, (tBTA_GATTC_DATA *)&op_cmpl);
1261         }
1262     }
1263 }
1264 /*******************************************************************************
1265 **
1266 ** Function         bta_gattc_confirm
1267 **
1268 ** Description      send handle value confirmation
1269 **
1270 ** Returns          None.
1271 **
1272 *******************************************************************************/
1273 void bta_gattc_confirm(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1274 {
1275     UINT16 handle;
1276
1277     if ((handle = bta_gattc_id2handle(p_clcb->p_srcb,
1278                                       &p_data->api_confirm.srvc_id,
1279                                       &p_data->api_confirm.char_id,
1280                                       NULL)) == 0)
1281     {
1282         APPL_TRACE_ERROR("Can not map service/char ID into valid handle");
1283     }
1284     else
1285     {
1286         if (GATTC_SendHandleValueConfirm(p_data->api_confirm.hdr.layer_specific, handle)
1287             != GATT_SUCCESS)
1288         {
1289             APPL_TRACE_ERROR("bta_gattc_confirm to handle [0x%04x] failed", handle);
1290         }
1291         else
1292         {
1293             /* if over BR_EDR, inform PM for mode change */
1294             if (p_clcb->transport == BTA_TRANSPORT_BR_EDR)
1295             {
1296                 bta_sys_busy(BTA_ID_GATTC, BTA_ALL_APP_ID, p_clcb->bda);
1297                 bta_sys_idle(BTA_ID_GATTC, BTA_ALL_APP_ID, p_clcb->bda);
1298             }
1299         }
1300     }
1301 }
1302 /*******************************************************************************
1303 **
1304 ** Function         bta_gattc_read_cmpl
1305 **
1306 ** Description      read complete
1307 **
1308 ** Returns          None.
1309 **
1310 *******************************************************************************/
1311 void bta_gattc_read_cmpl(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_OP_CMPL *p_data)
1312 {
1313     UINT8               event;
1314     tBTA_GATTC          cb_data;
1315     tBTA_GATT_READ_VAL  read_value;
1316
1317     memset(&cb_data, 0, sizeof(tBTA_GATTC));
1318     memset(&read_value, 0, sizeof(tBTA_GATT_READ_VAL));
1319
1320     cb_data.read.status     = p_data->status;
1321
1322     if (p_data->p_cmpl != NULL && p_data->status == BTA_GATT_OK)
1323     {
1324         if (bta_gattc_handle2id(p_clcb->p_srcb,
1325                                 p_data->p_cmpl->att_value.handle,
1326                                 &cb_data.read.srvc_id,
1327                                 &cb_data.read.char_id,
1328                                 &cb_data.read.descr_type) == FALSE)
1329         {
1330             cb_data.read.status = BTA_GATT_INTERNAL_ERROR;
1331             APPL_TRACE_ERROR("can not map to GATT ID. handle = 0x%04x",
1332                                 p_data->p_cmpl->att_value.handle);
1333         }
1334         else
1335         {
1336             cb_data.read.status = bta_gattc_pack_read_cb_data(p_clcb->p_srcb,
1337                                                               &cb_data.read.descr_type.uuid,
1338                                                               &p_data->p_cmpl->att_value,
1339                                                               &read_value);
1340             cb_data.read.p_value = &read_value;
1341         }
1342     }
1343     else
1344     {
1345         cb_data.read.srvc_id = p_clcb->p_q_cmd->api_read.srvc_id;
1346         cb_data.read.char_id = p_clcb->p_q_cmd->api_read.char_id;
1347         if (p_clcb->p_q_cmd->api_read.p_descr_type)
1348             memcpy(&cb_data.read.descr_type, p_clcb->p_q_cmd->api_read.p_descr_type,
1349                    sizeof(tBTA_GATT_ID));
1350     }
1351
1352     event = (p_clcb->p_q_cmd->api_read.p_descr_type == NULL) ?
1353                     BTA_GATTC_READ_CHAR_EVT: BTA_GATTC_READ_DESCR_EVT;
1354     cb_data.read.conn_id = p_clcb->bta_conn_id;
1355
1356     utl_freebuf((void **)&p_clcb->p_q_cmd);
1357     /* read complete, callback */
1358     ( *p_clcb->p_rcb->p_cback)(event, (tBTA_GATTC *)&cb_data);
1359
1360 }
1361 /*******************************************************************************
1362 **
1363 ** Function         bta_gattc_write_cmpl
1364 **
1365 ** Description      write complete
1366 **
1367 ** Returns          None.
1368 **
1369 *******************************************************************************/
1370 void bta_gattc_write_cmpl(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_OP_CMPL *p_data)
1371 {
1372     tBTA_GATTC      cb_data = {0};
1373     UINT8          event;
1374
1375     memset(&cb_data, 0, sizeof(tBTA_GATTC));
1376
1377     cb_data.write.status     = p_data->status;
1378
1379     if (p_data->p_cmpl != NULL)
1380     {
1381         bta_gattc_handle2id(p_clcb->p_srcb, p_data->p_cmpl->att_value.handle,
1382                             &cb_data.write.srvc_id, &cb_data.write.char_id,
1383                             &cb_data.write.descr_type);
1384     }
1385     else
1386     {
1387         memcpy(&cb_data.write.srvc_id, &p_clcb->p_q_cmd->api_write.srvc_id,
1388                 sizeof(tBTA_GATT_SRVC_ID));
1389         memcpy(&cb_data.write.char_id, &p_clcb->p_q_cmd->api_write.char_id,
1390                 sizeof(tBTA_GATT_ID));
1391         if (p_clcb->p_q_cmd->api_write.p_descr_type)
1392             memcpy(&cb_data.write.descr_type, p_clcb->p_q_cmd->api_write.p_descr_type,
1393                    sizeof(tBTA_GATT_ID));
1394     }
1395
1396     if (p_clcb->p_q_cmd->api_write.hdr.event == BTA_GATTC_API_WRITE_EVT &&
1397         p_clcb->p_q_cmd->api_write.write_type == BTA_GATTC_WRITE_PREPARE)
1398
1399         event = BTA_GATTC_PREP_WRITE_EVT;
1400
1401     else if (p_clcb->p_q_cmd->api_write.p_descr_type == NULL)
1402
1403         event = BTA_GATTC_WRITE_CHAR_EVT;
1404
1405     else
1406         event = BTA_GATTC_WRITE_DESCR_EVT;
1407
1408     utl_freebuf((void **)&p_clcb->p_q_cmd);
1409     cb_data.write.conn_id = p_clcb->bta_conn_id;
1410     /* write complete, callback */
1411     ( *p_clcb->p_rcb->p_cback)(event, (tBTA_GATTC *)&cb_data);
1412
1413 }
1414 /*******************************************************************************
1415 **
1416 ** Function         bta_gattc_exec_cmpl
1417 **
1418 ** Description      execute write complete
1419 **
1420 ** Returns          None.
1421 **
1422 *******************************************************************************/
1423 void bta_gattc_exec_cmpl(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_OP_CMPL *p_data)
1424 {
1425     tBTA_GATTC          cb_data;
1426
1427     utl_freebuf((void **)&p_clcb->p_q_cmd);
1428
1429     p_clcb->status      = BTA_GATT_OK;
1430
1431     /* execute complete, callback */
1432     cb_data.exec_cmpl.conn_id = p_clcb->bta_conn_id;
1433     cb_data.exec_cmpl.status = p_data->status;
1434
1435     ( *p_clcb->p_rcb->p_cback)(BTA_GATTC_EXEC_EVT,  &cb_data);
1436
1437 }
1438
1439 /*******************************************************************************
1440 **
1441 ** Function         bta_gattc_cfg_mtu_cmpl
1442 **
1443 ** Description      configure MTU operation complete
1444 **
1445 ** Returns          None.
1446 **
1447 *******************************************************************************/
1448 void bta_gattc_cfg_mtu_cmpl(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_OP_CMPL *p_data)
1449 {
1450     tBTA_GATTC          cb_data;
1451
1452     utl_freebuf((void **)&p_clcb->p_q_cmd);
1453
1454
1455     if (p_data->p_cmpl  &&  p_data->status == BTA_GATT_OK)
1456         p_clcb->p_srcb->mtu  = p_data->p_cmpl->mtu;
1457
1458     /* configure MTU complete, callback */
1459     p_clcb->status          = p_data->status;
1460     cb_data.cfg_mtu.conn_id = p_clcb->bta_conn_id;
1461     cb_data.cfg_mtu.status  = p_data->status;
1462     cb_data.cfg_mtu.mtu     = p_clcb->p_srcb->mtu;
1463
1464     (*p_clcb->p_rcb->p_cback) (BTA_GATTC_CFG_MTU_EVT,  &cb_data);
1465
1466 }
1467 /*******************************************************************************
1468 **
1469 ** Function         bta_gattc_op_cmpl
1470 **
1471 ** Description      operation completed.
1472 **
1473 ** Returns          None.
1474 **
1475 *******************************************************************************/
1476 void  bta_gattc_op_cmpl(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1477 {
1478     UINT8           op = (UINT8)p_data->op_cmpl.op_code;
1479     UINT8           mapped_op = 0;
1480
1481     APPL_TRACE_DEBUG("bta_gattc_op_cmpl op = %d", op);
1482
1483     if (op == GATTC_OPTYPE_INDICATION || op == GATTC_OPTYPE_NOTIFICATION)
1484     {
1485         APPL_TRACE_ERROR("unexpected operation, ignored");
1486     }
1487     else if (op >= GATTC_OPTYPE_READ)
1488     {
1489         if (p_clcb->p_q_cmd == NULL)
1490         {
1491             APPL_TRACE_ERROR("No pending command");
1492             return;
1493         }
1494         if (p_clcb->p_q_cmd->hdr.event != bta_gattc_opcode_to_int_evt[op - GATTC_OPTYPE_READ])
1495         {
1496             mapped_op = p_clcb->p_q_cmd->hdr.event - BTA_GATTC_API_READ_EVT + GATTC_OPTYPE_READ;
1497             if ( mapped_op > GATTC_OPTYPE_INDICATION)   mapped_op = 0;
1498
1499 #if (BT_TRACE_VERBOSE == TRUE)
1500             APPL_TRACE_ERROR("expect op:(%s :0x%04x), receive unexpected operation (%s).",
1501                                 bta_gattc_op_code_name[mapped_op] , p_clcb->p_q_cmd->hdr.event,
1502                                 bta_gattc_op_code_name[op]);
1503 #else
1504             APPL_TRACE_ERROR("expect op:(%u :0x%04x), receive unexpected operation (%u).",
1505                                 mapped_op , p_clcb->p_q_cmd->hdr.event, op);
1506 #endif
1507             return;
1508         }
1509
1510         /* discard responses if service change indication is received before operation completed */
1511         if (p_clcb->auto_update == BTA_GATTC_DISC_WAITING && p_clcb->p_srcb->srvc_hdl_chg)
1512         {
1513             APPL_TRACE_DEBUG("Discard all responses when service change indication is received.");
1514             p_data->op_cmpl.status = GATT_ERROR;
1515         }
1516
1517         /* service handle change void the response, discard it */
1518         if (op == GATTC_OPTYPE_READ)
1519             bta_gattc_read_cmpl(p_clcb, &p_data->op_cmpl);
1520
1521         else if (op == GATTC_OPTYPE_WRITE)
1522             bta_gattc_write_cmpl(p_clcb, &p_data->op_cmpl);
1523
1524         else if (op == GATTC_OPTYPE_EXE_WRITE)
1525             bta_gattc_exec_cmpl(p_clcb, &p_data->op_cmpl);
1526
1527         else if (op == GATTC_OPTYPE_CONFIG)
1528             bta_gattc_cfg_mtu_cmpl(p_clcb, &p_data->op_cmpl);
1529
1530         if (p_clcb->auto_update == BTA_GATTC_DISC_WAITING)
1531         {
1532             p_clcb->auto_update = BTA_GATTC_REQ_WAITING;
1533             bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_DISCOVER_EVT, NULL);
1534         }
1535     }
1536 }
1537 /*******************************************************************************
1538 **
1539 ** Function         bta_gattc_op_cmpl
1540 **
1541 ** Description      operation completed.
1542 **
1543 ** Returns          None.
1544 **
1545 *******************************************************************************/
1546 void  bta_gattc_ignore_op_cmpl(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1547 {
1548     UNUSED(p_clcb);
1549
1550     /* receive op complete when discovery is started, ignore the response,
1551         and wait for discovery finish and resent */
1552     APPL_TRACE_DEBUG("bta_gattc_ignore_op_cmpl op = %d", p_data->hdr.layer_specific);
1553
1554 }
1555 /*******************************************************************************
1556 **
1557 ** Function         bta_gattc_search
1558 **
1559 ** Description      start a search in the local server cache
1560 **
1561 ** Returns          None.
1562 **
1563 *******************************************************************************/
1564 void bta_gattc_search(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1565 {
1566     tBTA_GATT_STATUS    status = GATT_INTERNAL_ERROR;
1567     tBTA_GATTC cb_data;
1568     APPL_TRACE_DEBUG("bta_gattc_search conn_id=%d",p_clcb->bta_conn_id);
1569     if (p_clcb->p_srcb && p_clcb->p_srcb->p_srvc_cache)
1570     {
1571         status = BTA_GATT_OK;
1572         /* search the local cache of a server device */
1573         bta_gattc_search_service(p_clcb, p_data->api_search.p_srvc_uuid);
1574     }
1575     cb_data.search_cmpl.status  = status;
1576     cb_data.search_cmpl.conn_id = p_clcb->bta_conn_id;
1577
1578     /* end of search or no server cache available */
1579     ( *p_clcb->p_rcb->p_cback)(BTA_GATTC_SEARCH_CMPL_EVT,  &cb_data);
1580 }
1581 /*******************************************************************************
1582 **
1583 ** Function         bta_gattc_q_cmd
1584 **
1585 ** Description      enqueue a command into control block, usually because discovery
1586 **                  operation is busy.
1587 **
1588 ** Returns          None.
1589 **
1590 *******************************************************************************/
1591 void bta_gattc_q_cmd(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1592 {
1593     bta_gattc_enqueue(p_clcb, p_data);
1594 }
1595 /*******************************************************************************
1596 **
1597 ** Function         bta_gattc_cache_open
1598 **
1599 ** Description      open a NV cache for loading
1600 **
1601 ** Returns          void
1602 **
1603 *******************************************************************************/
1604 void bta_gattc_cache_open(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1605 {
1606     UNUSED(p_data);
1607
1608     bta_gattc_set_discover_st(p_clcb->p_srcb);
1609
1610     APPL_TRACE_DEBUG("bta_gattc_cache_open conn_id=%d",p_clcb->bta_conn_id);
1611     bta_gattc_co_cache_open(p_clcb->p_srcb->server_bda, BTA_GATTC_CI_CACHE_OPEN_EVT,
1612                             p_clcb->bta_conn_id, FALSE);
1613 }
1614 /*******************************************************************************
1615 **
1616 ** Function         bta_gattc_start_load
1617 **
1618 ** Description      start cache loading by sending callout open cache
1619 **
1620 ** Returns          None.
1621 **
1622 *******************************************************************************/
1623 void bta_gattc_ci_open(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1624 {
1625     APPL_TRACE_DEBUG("bta_gattc_ci_open conn_id=%d server state=%d" ,
1626                       p_clcb->bta_conn_id, p_clcb->p_srcb->state);
1627     if (p_clcb->p_srcb->state == BTA_GATTC_SERV_LOAD)
1628     {
1629         if (p_data->ci_open.status == BTA_GATT_OK)
1630         {
1631             p_clcb->p_srcb->attr_index = 0;
1632             bta_gattc_co_cache_load(p_clcb->p_srcb->server_bda,
1633                                     BTA_GATTC_CI_CACHE_LOAD_EVT,
1634                                     p_clcb->p_srcb->attr_index,
1635                                     p_clcb->bta_conn_id);
1636         }
1637         else
1638         {
1639             p_clcb->p_srcb->state = BTA_GATTC_SERV_DISC;
1640             /* cache open failure, start discovery */
1641             bta_gattc_start_discover(p_clcb, NULL);
1642         }
1643     }
1644     if (p_clcb->p_srcb->state == BTA_GATTC_SERV_SAVE)
1645     {
1646         if (p_data->ci_open.status == BTA_GATT_OK)
1647         {
1648             if (!bta_gattc_cache_save(p_clcb->p_srcb, p_clcb->bta_conn_id))
1649             {
1650                 p_data->ci_open.status = BTA_GATT_ERROR;
1651             }
1652         }
1653         if (p_data->ci_open.status != BTA_GATT_OK)
1654         {
1655             p_clcb->p_srcb->attr_index = 0;
1656             bta_gattc_co_cache_close(p_clcb->p_srcb->server_bda, p_clcb->bta_conn_id);
1657             bta_gattc_reset_discover_st(p_clcb->p_srcb, p_clcb->status);
1658
1659         }
1660     }
1661 }
1662 /*******************************************************************************
1663 **
1664 ** Function         bta_gattc_ci_load
1665 **
1666 ** Description      cache loading received.
1667 **
1668 ** Returns          None.
1669 **
1670 *******************************************************************************/
1671 void bta_gattc_ci_load(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1672 {
1673
1674     APPL_TRACE_DEBUG("bta_gattc_ci_load conn_id=%d load status=%d",
1675                       p_clcb->bta_conn_id, p_data->ci_load.status);
1676
1677     if (p_data->ci_load.status == BTA_GATT_OK ||
1678          p_data->ci_load.status == BTA_GATT_MORE)
1679     {
1680         if (p_data->ci_load.num_attr != 0)
1681             bta_gattc_rebuild_cache(p_clcb->p_srcb, p_data->ci_load.num_attr,
1682                                 p_data->ci_load.attr, p_clcb->p_srcb->attr_index);
1683
1684         if (p_data->ci_load.status == BTA_GATT_OK)
1685         {
1686             p_clcb->p_srcb->attr_index = 0;
1687             bta_gattc_reset_discover_st(p_clcb->p_srcb, BTA_GATT_OK);
1688             bta_gattc_co_cache_close(p_clcb->p_srcb->server_bda, 0);
1689         }
1690         else /* load more */
1691         {
1692             p_clcb->p_srcb->attr_index += p_data->ci_load.num_attr;
1693
1694             bta_gattc_co_cache_load(p_clcb->p_srcb->server_bda,
1695                                     BTA_GATTC_CI_CACHE_LOAD_EVT,
1696                                     p_clcb->p_srcb->attr_index,
1697                                     p_clcb->bta_conn_id);
1698         }
1699     }
1700     else
1701     {
1702         bta_gattc_co_cache_close(p_clcb->p_srcb->server_bda, 0);
1703         p_clcb->p_srcb->state = BTA_GATTC_SERV_DISC;
1704         p_clcb->p_srcb->attr_index = 0;
1705         /* cache load failure, start discovery */
1706         bta_gattc_start_discover(p_clcb, NULL);
1707     }
1708 }
1709 /*******************************************************************************
1710 **
1711 ** Function         bta_gattc_ci_save
1712 **
1713 ** Description      cache loading received.
1714 **
1715 ** Returns          None.
1716 **
1717 *******************************************************************************/
1718 void bta_gattc_ci_save(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1719 {
1720     UNUSED(p_data);
1721
1722     APPL_TRACE_DEBUG("bta_gattc_ci_save conn_id=%d  " ,
1723                       p_clcb->bta_conn_id   );
1724
1725     if (!bta_gattc_cache_save(p_clcb->p_srcb, p_clcb->bta_conn_id))
1726     {
1727         p_clcb->p_srcb->attr_index = 0;
1728         bta_gattc_co_cache_close(p_clcb->p_srcb->server_bda, 0);
1729         bta_gattc_reset_discover_st(p_clcb->p_srcb, p_clcb->status);
1730     }
1731 }
1732 /*******************************************************************************
1733 **
1734 ** Function         bta_gattc_fail
1735 **
1736 ** Description      report API call failure back to apps
1737 **
1738 ** Returns          None.
1739 **
1740 *******************************************************************************/
1741 void bta_gattc_fail(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1742 {
1743     UNUSED(p_data);
1744
1745     if (p_clcb->status == BTA_GATT_OK)
1746     {
1747         APPL_TRACE_ERROR("operation not supported at current state [%d]", p_clcb->state);
1748     }
1749 }
1750
1751 /*******************************************************************************
1752 **
1753 ** Function         bta_gattc_deregister_cmpl
1754 **
1755 ** Description      De-Register a GATT client application with BTA completed.
1756 **
1757 ** Returns          void
1758 **
1759 *******************************************************************************/
1760 static void bta_gattc_deregister_cmpl(tBTA_GATTC_RCB *p_clreg)
1761 {
1762     tBTA_GATTC_CB       *p_cb = &bta_gattc_cb;
1763     tBTA_GATTC_IF       client_if = p_clreg->client_if;
1764     tBTA_GATTC          cb_data;
1765     tBTA_GATTC_CBACK    *p_cback = p_clreg->p_cback;
1766
1767     memset(&cb_data, 0, sizeof(tBTA_GATTC));
1768
1769     GATT_Deregister(p_clreg->client_if);
1770     memset(p_clreg, 0, sizeof(tBTA_GATTC_RCB));
1771
1772     cb_data.reg_oper.client_if = client_if;
1773     cb_data.reg_oper.status    = BTA_GATT_OK;
1774
1775     if (p_cback)
1776         /* callback with de-register event */
1777         (*p_cback)(BTA_GATTC_DEREG_EVT,  (tBTA_GATTC *)&cb_data);
1778
1779     if (bta_gattc_num_reg_app() == 0 && p_cb->state == BTA_GATTC_STATE_DISABLING)
1780     {
1781         p_cb->state = BTA_GATTC_STATE_DISABLED;
1782     }
1783 }
1784 /*******************************************************************************
1785 **
1786 ** Function         bta_gattc_conn_cback
1787 **                  bta_gattc_cmpl_cback
1788 **
1789 ** Description      callback functions to GATT client stack.
1790 **
1791 ** Returns          void
1792 **
1793 *******************************************************************************/
1794 static void bta_gattc_conn_cback(tGATT_IF gattc_if, BD_ADDR bda, UINT16 conn_id,
1795                                  BOOLEAN connected, tGATT_DISCONN_REASON reason,
1796                                  tBT_TRANSPORT transport)
1797 {
1798     tBTA_GATTC_DATA *p_buf;
1799
1800     if (reason != 0)
1801     {
1802         APPL_TRACE_WARNING("%s() - cif=%d connected=%d conn_id=%d reason=0x%04x",
1803                       __FUNCTION__, gattc_if, connected, conn_id, reason);
1804     }
1805
1806     bt_bdaddr_t bdaddr;
1807     bdcpy(bdaddr.address, bda);
1808     if (connected)
1809         btif_debug_conn_state(bdaddr, BTIF_DEBUG_CONNECTED, GATT_CONN_UNKNOWN);
1810     else
1811         btif_debug_conn_state(bdaddr, BTIF_DEBUG_DISCONNECTED, reason);
1812
1813     if ((p_buf = (tBTA_GATTC_DATA *) GKI_getbuf(sizeof(tBTA_GATTC_DATA))) != NULL)
1814     {
1815         memset(p_buf, 0, sizeof(tBTA_GATTC_DATA));
1816
1817         p_buf->int_conn.hdr.event            = connected ? BTA_GATTC_INT_CONN_EVT:
1818                                                            BTA_GATTC_INT_DISCONN_EVT;
1819         p_buf->int_conn.hdr.layer_specific   = conn_id;
1820         p_buf->int_conn.client_if            = gattc_if;
1821         p_buf->int_conn.role                 = L2CA_GetBleConnRole(bda);
1822         p_buf->int_conn.reason               = reason;
1823         p_buf->int_conn.transport            = transport;
1824         bdcpy(p_buf->int_conn.remote_bda, bda);
1825
1826         bta_sys_sendmsg(p_buf);
1827     }
1828 }
1829
1830 /*******************************************************************************
1831 **
1832 ** Function         bta_gattc_enc_cmpl_cback
1833 **
1834 ** Description      encryption complete callback function to GATT client stack.
1835 **
1836 ** Returns          void
1837 **
1838 *******************************************************************************/
1839 static void bta_gattc_enc_cmpl_cback(tGATT_IF gattc_if, BD_ADDR bda)
1840 {
1841     tBTA_GATTC_DATA *p_buf;
1842     tBTA_GATTC_CLCB *p_clcb = NULL;
1843
1844     if ((p_clcb = bta_gattc_find_clcb_by_cif(gattc_if, bda, BTA_GATT_TRANSPORT_LE)) == NULL)
1845     {
1846         return;
1847     }
1848
1849 #if (defined BTA_HH_LE_INCLUDED && BTA_HH_LE_INCLUDED == TRUE)
1850     /* filter this event just for BTA HH LE GATT client,
1851        In the future, if we want to enable encryption complete event
1852        for all GATT clients, we can remove this code */
1853     if (!bta_hh_le_is_hh_gatt_if(gattc_if))
1854     {
1855         return;
1856     }
1857 #endif
1858
1859     APPL_TRACE_DEBUG("bta_gattc_enc_cmpl_cback: cif = %d", gattc_if);
1860
1861     if ((p_buf = (tBTA_GATTC_DATA *) GKI_getbuf(sizeof(tBTA_GATTC_DATA))) != NULL)
1862     {
1863         memset(p_buf, 0, sizeof(tBTA_GATTC_DATA));
1864
1865         p_buf->enc_cmpl.hdr.event            = BTA_GATTC_ENC_CMPL_EVT;
1866         p_buf->enc_cmpl.hdr.layer_specific   = p_clcb->bta_conn_id;
1867         p_buf->enc_cmpl.client_if            = gattc_if;
1868         bdcpy(p_buf->enc_cmpl.remote_bda, bda);
1869
1870         bta_sys_sendmsg(p_buf);
1871     }
1872 }
1873
1874 /*******************************************************************************
1875 **
1876 ** Function         bta_gattc_process_api_refresh
1877 **
1878 ** Description      process refresh API to delete cache and start a new discovery
1879 **                  if currently connected.
1880 **
1881 ** Returns          None.
1882 **
1883 *******************************************************************************/
1884 void bta_gattc_process_api_refresh(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA * p_msg)
1885 {
1886     tBTA_GATTC_SERV *p_srvc_cb = bta_gattc_find_srvr_cache(p_msg->api_conn.remote_bda);
1887     tBTA_GATTC_CLCB      *p_clcb = &bta_gattc_cb.clcb[0];
1888     BOOLEAN         found = FALSE;
1889     UINT8           i;
1890     UNUSED(p_cb);
1891
1892     if (p_srvc_cb != NULL)
1893     {
1894         /* try to find a CLCB */
1895         if (p_srvc_cb->connected && p_srvc_cb->num_clcb != 0)
1896         {
1897             for (i = 0; i < BTA_GATTC_CLCB_MAX; i ++, p_clcb ++)
1898             {
1899                 if (p_clcb->in_use && p_clcb->p_srcb == p_srvc_cb)
1900                 {
1901                     found = TRUE;
1902                     break;
1903                 }
1904             }
1905             if (found)
1906             {
1907                 bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_DISCOVER_EVT, NULL);
1908                 return;
1909             }
1910         }
1911         /* in all other cases, mark it and delete the cache */
1912         if (p_srvc_cb->p_srvc_cache != NULL)
1913         {
1914             while (!GKI_queue_is_empty(&p_srvc_cb->cache_buffer))
1915                 GKI_freebuf (GKI_dequeue (&p_srvc_cb->cache_buffer));
1916
1917             p_srvc_cb->p_srvc_cache = NULL;
1918         }
1919     }
1920     /* used to reset cache in application */
1921     bta_gattc_co_cache_reset(p_msg->api_conn.remote_bda);
1922
1923 }
1924 /*******************************************************************************
1925 **
1926 ** Function         bta_gattc_process_srvc_chg_ind
1927 **
1928 ** Description      process service change indication.
1929 **
1930 ** Returns          None.
1931 **
1932 *******************************************************************************/
1933 BOOLEAN bta_gattc_process_srvc_chg_ind(UINT16 conn_id,
1934                                        tBTA_GATTC_RCB      *p_clrcb,
1935                                        tBTA_GATTC_SERV     *p_srcb,
1936                                        tBTA_GATTC_CLCB      *p_clcb,
1937                                        tBTA_GATTC_NOTIFY    *p_notify,
1938                                        UINT16 handle)
1939 {
1940     tBT_UUID        gattp_uuid, srvc_chg_uuid;
1941     BOOLEAN         processed = FALSE;
1942     UINT8           i;
1943
1944     gattp_uuid.len = 2;
1945     gattp_uuid.uu.uuid16 = UUID_SERVCLASS_GATT_SERVER;
1946
1947     srvc_chg_uuid.len = 2;
1948     srvc_chg_uuid.uu.uuid16 = GATT_UUID_GATT_SRV_CHGD;
1949
1950     if (bta_gattc_uuid_compare(&p_notify->char_id.srvc_id.id.uuid, &gattp_uuid, TRUE) &&
1951         bta_gattc_uuid_compare(&p_notify->char_id.char_id.uuid, &srvc_chg_uuid, TRUE))
1952     {
1953         processed = TRUE;
1954         /* mark service handle change pending */
1955         p_srcb->srvc_hdl_chg = TRUE;
1956         /* clear up all notification/indication registration */
1957         bta_gattc_clear_notif_registration(conn_id);
1958         /* service change indication all received, do discovery update */
1959         if ( ++ p_srcb->update_count == bta_gattc_num_reg_app())
1960         {
1961             /* not an opened connection; or connection busy */
1962             /* search for first available clcb and start discovery */
1963             if (p_clcb == NULL || (p_clcb && p_clcb->p_q_cmd != NULL))
1964             {
1965                 for (i = 0 ; i < BTA_GATTC_CLCB_MAX; i ++)
1966                 {
1967                     if (bta_gattc_cb.clcb[i].in_use &&
1968                         bta_gattc_cb.clcb[i].p_srcb == p_srcb &&
1969                         bta_gattc_cb.clcb[i].p_q_cmd == NULL)
1970                     {
1971                         p_clcb = &bta_gattc_cb.clcb[i];
1972                         break;
1973                     }
1974                 }
1975             }
1976             /* send confirmation here if this is an indication, it should always be */
1977             GATTC_SendHandleValueConfirm(conn_id, handle);
1978
1979             /* if connection available, refresh cache by doing discovery now */
1980             if (p_clcb != NULL)
1981                 bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_DISCOVER_EVT, NULL);
1982         }
1983         /* notify applicationf or service change */
1984         if (p_clrcb->p_cback != NULL)
1985         {
1986            (* p_clrcb->p_cback)(BTA_GATTC_SRVC_CHG_EVT, (tBTA_GATTC *)p_srcb->server_bda);
1987         }
1988
1989     }
1990
1991     return processed;
1992
1993 }
1994 /*******************************************************************************
1995 **
1996 ** Function         bta_gattc_proc_other_indication
1997 **
1998 ** Description      process all non-service change indication/notification.
1999 **
2000 ** Returns          None.
2001 **
2002 *******************************************************************************/
2003 void bta_gattc_proc_other_indication(tBTA_GATTC_CLCB *p_clcb, UINT8 op,
2004                                      tGATT_CL_COMPLETE *p_data,
2005                                      tBTA_GATTC_NOTIFY *p_notify)
2006 {
2007     APPL_TRACE_DEBUG("bta_gattc_proc_other_indication check \
2008                        p_data->att_value.handle=%d p_data->handle=%d",
2009                        p_data->att_value.handle, p_data->handle);
2010     APPL_TRACE_DEBUG("is_notify", p_notify->is_notify);
2011
2012     p_notify->is_notify = (op == GATTC_OPTYPE_INDICATION) ? FALSE : TRUE;
2013     p_notify->len = p_data->att_value.len;
2014     bdcpy(p_notify->bda, p_clcb->bda);
2015     memcpy(p_notify->value, p_data->att_value.value, p_data->att_value.len);
2016     p_notify->conn_id = p_clcb->bta_conn_id;
2017
2018     if (p_clcb->p_rcb->p_cback)
2019         (*p_clcb->p_rcb->p_cback)(BTA_GATTC_NOTIF_EVT,  (tBTA_GATTC *)p_notify);
2020
2021 }
2022 /*******************************************************************************
2023 **
2024 ** Function         bta_gattc_process_indicate
2025 **
2026 ** Description      process indication/notification.
2027 **
2028 ** Returns          None.
2029 **
2030 *******************************************************************************/
2031 void bta_gattc_process_indicate(UINT16 conn_id, tGATTC_OPTYPE op, tGATT_CL_COMPLETE *p_data)
2032 {
2033     UINT16              handle = p_data->att_value.handle;
2034     tBTA_GATTC_CLCB     *p_clcb ;
2035     tBTA_GATTC_RCB      *p_clrcb = NULL;
2036     tBTA_GATTC_SERV     *p_srcb = NULL;
2037     tBTA_GATTC_NOTIFY   notify;
2038     BD_ADDR             remote_bda;
2039     tBTA_GATTC_IF       gatt_if;
2040     tBTA_TRANSPORT transport;
2041
2042     if (!GATT_GetConnectionInfor(conn_id, &gatt_if, remote_bda, &transport))
2043     {
2044         APPL_TRACE_ERROR("%s indication/notif for unknown app", __func__);
2045         if (op == GATTC_OPTYPE_INDICATION)
2046             GATTC_SendHandleValueConfirm(conn_id, handle);
2047         return;
2048     }
2049
2050     if ((p_clrcb = bta_gattc_cl_get_regcb(gatt_if)) == NULL)
2051     {
2052         APPL_TRACE_ERROR("%s indication/notif for unregistered app", __func__);
2053         if (op == GATTC_OPTYPE_INDICATION)
2054             GATTC_SendHandleValueConfirm(conn_id, handle);
2055         return;
2056     }
2057
2058     if ((p_srcb = bta_gattc_find_srcb(remote_bda)) == NULL)
2059     {
2060         APPL_TRACE_ERROR("%s indication/notif for unknown device, ignore", __func__);
2061         if (op == GATTC_OPTYPE_INDICATION)
2062             GATTC_SendHandleValueConfirm(conn_id, handle);
2063         return;
2064     }
2065
2066     p_clcb = bta_gattc_find_clcb_by_conn_id(conn_id);
2067
2068     if (bta_gattc_handle2id(p_srcb, handle,
2069                             &notify.char_id.srvc_id,
2070                             &notify.char_id.char_id,
2071                             &notify.descr_type))
2072     {
2073         /* if non-service change indication/notification, forward to application */
2074         if (!bta_gattc_process_srvc_chg_ind(conn_id, p_clrcb, p_srcb, p_clcb, &notify, handle))
2075         {
2076             /* if app registered for the notification */
2077             if (bta_gattc_check_notif_registry(p_clrcb, p_srcb, &notify))
2078             {
2079                 /* connection not open yet */
2080                 if (p_clcb == NULL)
2081                 {
2082                     if ((p_clcb = bta_gattc_clcb_alloc(gatt_if, remote_bda, transport)) != NULL)
2083                     {
2084                         p_clcb->bta_conn_id = conn_id;
2085                         p_clcb->transport   = transport;
2086
2087                         bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_CONN_EVT, NULL);
2088                     }
2089                     else
2090                     {
2091                         APPL_TRACE_ERROR("No resources");
2092                     }
2093                 }
2094
2095                 if (p_clcb != NULL)
2096                     bta_gattc_proc_other_indication(p_clcb, op, p_data, &notify);
2097             }
2098             /* no one intersted and need ack? */
2099             else if (op == GATTC_OPTYPE_INDICATION)
2100             {
2101                 APPL_TRACE_DEBUG("%s no one interested, ack now", __func__);
2102                 GATTC_SendHandleValueConfirm(conn_id, handle);
2103             }
2104         }
2105     }
2106     else
2107     {
2108         APPL_TRACE_ERROR("%s Indi/Notif for Unknown handle[0x%04x], can not find in local cache.",
2109                          __func__, handle);
2110         if (op == GATTC_OPTYPE_INDICATION)
2111             GATTC_SendHandleValueConfirm(conn_id, handle);
2112     }
2113 }
2114 /*******************************************************************************
2115 **
2116 ** Function         bta_gattc_cmpl_cback
2117 **
2118 ** Description      client operation complete callback register with BTE GATT.
2119 **
2120 ** Returns          None.
2121 **
2122 *******************************************************************************/
2123 static void  bta_gattc_cmpl_cback(UINT16 conn_id, tGATTC_OPTYPE op, tGATT_STATUS status,
2124                                   tGATT_CL_COMPLETE *p_data)
2125 {
2126     tBTA_GATTC_CLCB     *p_clcb ;
2127     tBTA_GATTC_OP_CMPL  *p_buf;
2128     UINT16              len = sizeof(tBTA_GATTC_OP_CMPL) + sizeof(tGATT_CL_COMPLETE);
2129
2130     APPL_TRACE_DEBUG("bta_gattc_cmpl_cback: conn_id = %d op = %d status = %d",
2131                       conn_id, op, status);
2132
2133     /* notification and indication processed right away */
2134     if (op == GATTC_OPTYPE_NOTIFICATION || op == GATTC_OPTYPE_INDICATION)
2135     {
2136         bta_gattc_process_indicate(conn_id, op, p_data);
2137         return;
2138     }
2139     /* for all other operation, not expected if w/o connection */
2140     else if ((p_clcb = bta_gattc_find_clcb_by_conn_id(conn_id)) == NULL)
2141     {
2142         APPL_TRACE_ERROR("bta_gattc_cmpl_cback unknown conn_id =  %d, ignore data", conn_id);
2143         return;
2144     }
2145
2146     /* if over BR_EDR, inform PM for mode change */
2147     if (p_clcb->transport == BTA_TRANSPORT_BR_EDR)
2148     {
2149         bta_sys_busy(BTA_ID_GATTC, BTA_ALL_APP_ID, p_clcb->bda);
2150         bta_sys_idle(BTA_ID_GATTC, BTA_ALL_APP_ID, p_clcb->bda);
2151     }
2152
2153     if ((p_buf = (tBTA_GATTC_OP_CMPL *) GKI_getbuf(len)) != NULL)
2154     {
2155         memset(p_buf, 0, len);
2156         p_buf->hdr.event = BTA_GATTC_OP_CMPL_EVT;
2157         p_buf->hdr.layer_specific = conn_id;
2158         p_buf->status = status;
2159         p_buf->op_code = op;
2160
2161         if (p_data != NULL)
2162         {
2163             p_buf->p_cmpl = (tGATT_CL_COMPLETE *)(p_buf + 1);
2164             memcpy(p_buf->p_cmpl, p_data, sizeof(tGATT_CL_COMPLETE));
2165         }
2166
2167         bta_sys_sendmsg(p_buf);
2168     }
2169
2170     return;
2171 }
2172
2173 /*******************************************************************************
2174 **
2175 ** Function         bta_gattc_cong_cback
2176 **
2177 ** Description      congestion callback for BTA GATT client.
2178 **
2179 ** Returns          void
2180 **
2181 ********************************************************************************/
2182 static void bta_gattc_cong_cback (UINT16 conn_id, BOOLEAN congested)
2183 {
2184     tBTA_GATTC_CLCB *p_clcb;
2185     tBTA_GATTC cb_data;
2186
2187     if ((p_clcb = bta_gattc_find_clcb_by_conn_id(conn_id)) != NULL)
2188     {
2189         if (p_clcb->p_rcb->p_cback)
2190         {
2191             cb_data.congest.conn_id = conn_id;
2192             cb_data.congest.congested = congested;
2193
2194             (*p_clcb->p_rcb->p_cback)(BTA_GATTC_CONGEST_EVT, &cb_data);
2195         }
2196     }
2197 }
2198
2199 #if BLE_INCLUDED == TRUE
2200 /*******************************************************************************
2201 **
2202 ** Function         bta_gattc_init_clcb_conn
2203 **
2204 ** Description      Initaite a BTA CLCB connection
2205 **
2206 ** Returns          void
2207 **
2208 ********************************************************************************/
2209 void bta_gattc_init_clcb_conn(UINT8 cif, BD_ADDR remote_bda)
2210 {
2211     tBTA_GATTC_CLCB     *p_clcb = NULL;
2212     tBTA_GATTC_DATA     gattc_data;
2213     UINT16              conn_id;
2214
2215     /* should always get the connection ID */
2216     if (GATT_GetConnIdIfConnected(cif, remote_bda, &conn_id, BTA_GATT_TRANSPORT_LE) == FALSE)
2217     {
2218         APPL_TRACE_ERROR("bta_gattc_init_clcb_conn ERROR: not a connected device");
2219         return;
2220     }
2221
2222     /* initaite a new connection here */
2223     if ((p_clcb = bta_gattc_clcb_alloc(cif, remote_bda, BTA_GATT_TRANSPORT_LE)) != NULL)
2224     {
2225         gattc_data.hdr.layer_specific = p_clcb->bta_conn_id = conn_id;
2226
2227         gattc_data.api_conn.client_if = cif;
2228         memcpy(gattc_data.api_conn.remote_bda, remote_bda, BD_ADDR_LEN);
2229         gattc_data.api_conn.is_direct = TRUE;
2230
2231         bta_gattc_sm_execute(p_clcb, BTA_GATTC_API_OPEN_EVT, &gattc_data);
2232     }
2233     else
2234     {
2235         APPL_TRACE_ERROR("No resources");
2236     }
2237 }
2238 /*******************************************************************************
2239 **
2240 ** Function         bta_gattc_process_listen_all
2241 **
2242 ** Description      process listen all, send open callback to application for all
2243 **                  connected slave LE link.
2244 **
2245 ** Returns          void
2246 **
2247 ********************************************************************************/
2248 void bta_gattc_process_listen_all(UINT8 cif)
2249 {
2250     UINT8               i_conn = 0;
2251     tBTA_GATTC_CONN     *p_conn = &bta_gattc_cb.conn_track[0];
2252
2253     for (i_conn = 0; i_conn < BTA_GATTC_CONN_MAX; i_conn++, p_conn ++)
2254     {
2255         if (p_conn->in_use )
2256         {
2257             if (bta_gattc_find_clcb_by_cif(cif, p_conn->remote_bda, BTA_GATT_TRANSPORT_LE) == NULL)
2258             {
2259                 bta_gattc_init_clcb_conn(cif, p_conn->remote_bda);
2260             }
2261             /* else already connected */
2262         }
2263     }
2264 }
2265 /*******************************************************************************
2266 **
2267 ** Function         bta_gattc_listen
2268 **
2269 ** Description      Start or stop a listen for connection
2270 **
2271 ** Returns          void
2272 **
2273 ********************************************************************************/
2274 void bta_gattc_listen(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA * p_msg)
2275 {
2276     tBTA_GATTC_RCB      *p_clreg = bta_gattc_cl_get_regcb(p_msg->api_listen.client_if);
2277     tBTA_GATTC          cb_data;
2278     UNUSED(p_cb);
2279
2280     cb_data.reg_oper.status = BTA_GATT_ERROR;
2281     cb_data.reg_oper.client_if = p_msg->api_listen.client_if;
2282
2283     if (p_clreg == NULL)
2284     {
2285         APPL_TRACE_ERROR("bta_gattc_listen failed, unknown client_if: %d",
2286                             p_msg->api_listen.client_if);
2287         return;
2288     }
2289     /* mark bg conn record */
2290     if (bta_gattc_mark_bg_conn(p_msg->api_listen.client_if,
2291                                (BD_ADDR_PTR) p_msg->api_listen.remote_bda,
2292                                p_msg->api_listen.start,
2293                                TRUE))
2294     {
2295         if (!GATT_Listen(p_msg->api_listen.client_if,
2296                          p_msg->api_listen.start,
2297                          p_msg->api_listen.remote_bda))
2298         {
2299             APPL_TRACE_ERROR("Listen failure");
2300             (*p_clreg->p_cback)(BTA_GATTC_LISTEN_EVT, &cb_data);
2301         }
2302         else
2303         {
2304             cb_data.status = BTA_GATT_OK;
2305
2306             (*p_clreg->p_cback)(BTA_GATTC_LISTEN_EVT, &cb_data);
2307
2308             if (p_msg->api_listen.start)
2309             {
2310                 /* if listen to a specific target */
2311                 if (p_msg->api_listen.remote_bda != NULL)
2312                 {
2313
2314                     /* if is a connected remote device */
2315                     if (L2CA_GetBleConnRole(p_msg->api_listen.remote_bda) == HCI_ROLE_SLAVE &&
2316                         bta_gattc_find_clcb_by_cif(p_msg->api_listen.client_if,
2317                                                    p_msg->api_listen.remote_bda,
2318                                                    BTA_GATT_TRANSPORT_LE) == NULL)
2319                     {
2320
2321                         bta_gattc_init_clcb_conn(p_msg->api_listen.client_if,
2322                                                 p_msg->api_listen.remote_bda);
2323                     }
2324                 }
2325                 /* if listen to all */
2326                 else
2327                 {
2328                     LOG_DEBUG(LOG_TAG, "Listen For All now");
2329                     /* go through all connected device and send
2330                     callback for all connected slave connection */
2331                     bta_gattc_process_listen_all(p_msg->api_listen.client_if);
2332                 }
2333             }
2334         }
2335     }
2336 }
2337
2338 /*******************************************************************************
2339 **
2340 ** Function         bta_gattc_broadcast
2341 **
2342 ** Description      Start or stop broadcasting
2343 **
2344 ** Returns          void
2345 **
2346 ********************************************************************************/
2347 void bta_gattc_broadcast(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA * p_msg)
2348 {
2349     tBTA_GATTC_RCB      *p_clreg = bta_gattc_cl_get_regcb(p_msg->api_listen.client_if);
2350     tBTA_GATTC          cb_data;
2351     UNUSED(p_cb);
2352
2353     cb_data.reg_oper.client_if = p_msg->api_listen.client_if;
2354     cb_data.reg_oper.status = BTM_BleBroadcast(p_msg->api_listen.start);
2355
2356     if (p_clreg && p_clreg->p_cback)
2357         (*p_clreg->p_cback)(BTA_GATTC_LISTEN_EVT, &cb_data);
2358 }
2359 #endif
2360 #endif