OSDN Git Service

Only initiate codec negotiation if feature is supported am: ecb3b8386f
[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 #include "bta_gattc_int.h"
32 #include "bta_sys.h"
33 #include "btif/include/btif_debug_conn.h"
34 #include "bt_common.h"
35 #include "l2c_api.h"
36 #include "osi/include/log.h"
37 #include "stack/l2cap/l2c_int.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 static void bta_gattc_cmpl_sendmsg(UINT16 conn_id, tGATTC_OPTYPE op,
56                                    tBTA_GATT_STATUS status,
57                                    tGATT_CL_COMPLETE *p_data);
58
59 static void bta_gattc_deregister_cmpl(tBTA_GATTC_RCB *p_clreg);
60 static void bta_gattc_enc_cmpl_cback(tGATT_IF gattc_if, BD_ADDR bda);
61 static void bta_gattc_cong_cback (UINT16 conn_id, BOOLEAN congested);
62
63 static tGATT_CBACK bta_gattc_cl_cback =
64 {
65     bta_gattc_conn_cback,
66     bta_gattc_cmpl_cback,
67     bta_gattc_disc_res_cback,
68     bta_gattc_disc_cmpl_cback,
69     NULL,
70     bta_gattc_enc_cmpl_cback,
71     bta_gattc_cong_cback
72 };
73
74 /* opcode(tGATTC_OPTYPE) order has to be comply with internal event order */
75 static UINT16 bta_gattc_opcode_to_int_evt[] =
76 {
77     BTA_GATTC_API_READ_EVT,
78     BTA_GATTC_API_WRITE_EVT,
79     BTA_GATTC_API_EXEC_EVT,
80     BTA_GATTC_API_CFG_MTU_EVT
81 };
82
83 #if (BT_TRACE_VERBOSE == TRUE)
84 static const char *bta_gattc_op_code_name[] =
85 {
86     "Unknown",
87     "Discovery",
88     "Read",
89     "Write",
90     "Exec",
91     "Config",
92     "Notification",
93     "Indication"
94 };
95 #endif
96 /*****************************************************************************
97 **  Action Functions
98 *****************************************************************************/
99
100 /*******************************************************************************
101 **
102 ** Function         bta_gattc_enable
103 **
104 ** Description      Enables GATTC module
105 **
106 **
107 ** Returns          void
108 **
109 *******************************************************************************/
110 static void bta_gattc_enable(tBTA_GATTC_CB *p_cb)
111 {
112     APPL_TRACE_DEBUG("bta_gattc_enable");
113
114     if (p_cb->state == BTA_GATTC_STATE_DISABLED)
115     {
116         /* initialize control block */
117         memset(&bta_gattc_cb, 0, sizeof(tBTA_GATTC_CB));
118         p_cb->state = BTA_GATTC_STATE_ENABLED;
119     }
120     else
121     {
122         APPL_TRACE_DEBUG("GATTC is arelady enabled");
123     }
124 }
125
126 /*******************************************************************************
127 **
128 ** Function         bta_gattc_disable
129 **
130 ** Description      Disable GATTC module by cleaning up all active connections
131 **                  and deregister all application.
132 **
133 ** Returns          void
134 **
135 *******************************************************************************/
136 void bta_gattc_disable(tBTA_GATTC_CB *p_cb)
137 {
138     UINT8           i;
139
140     APPL_TRACE_DEBUG("bta_gattc_disable");
141
142     if (p_cb->state != BTA_GATTC_STATE_ENABLED)
143     {
144         APPL_TRACE_ERROR("not enabled or disable in pogress");
145         return;
146     }
147
148     for (i = 0; i <BTA_GATTC_CL_MAX; i ++)
149     {
150         if (p_cb->cl_rcb[i].in_use)
151         {
152             p_cb->state = BTA_GATTC_STATE_DISABLING;
153             /* don't deregister HH GATT IF */
154             /* HH GATT IF will be deregistered by bta_hh_le_deregister when disable HH */
155 #if (defined BTA_HH_LE_INCLUDED && BTA_HH_LE_INCLUDED == TRUE)
156             if (!bta_hh_le_is_hh_gatt_if(p_cb->cl_rcb[i].client_if)) {
157 #endif
158                 bta_gattc_deregister(p_cb, &p_cb->cl_rcb[i]);
159 #if (defined BTA_HH_LE_INCLUDED && BTA_HH_LE_INCLUDED == TRUE)
160             }
161 #endif
162         }
163     }
164
165     /* no registered apps, indicate disable completed */
166     if (p_cb->state != BTA_GATTC_STATE_DISABLING)
167     {
168         p_cb->state = BTA_GATTC_STATE_DISABLED;
169         memset(p_cb, 0, sizeof(tBTA_GATTC_CB));
170     }
171 }
172
173 /*******************************************************************************
174 **
175 ** Function         bta_gattc_register
176 **
177 ** Description      Register a GATT client application with BTA.
178 **
179 ** Returns          void
180 **
181 *******************************************************************************/
182 void bta_gattc_register(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA *p_data)
183 {
184     tBTA_GATTC               cb_data;
185     UINT8                    i;
186     tBT_UUID                 *p_app_uuid = &p_data->api_reg.app_uuid;
187     tBTA_GATTC_INT_START_IF  *p_buf;
188     tBTA_GATT_STATUS         status = BTA_GATT_NO_RESOURCES;
189
190     APPL_TRACE_DEBUG("bta_gattc_register state %d",p_cb->state);
191     memset(&cb_data, 0, sizeof(cb_data));
192     cb_data.reg_oper.status = BTA_GATT_NO_RESOURCES;
193
194      /* check if  GATTC module is already enabled . Else enable */
195      if (p_cb->state == BTA_GATTC_STATE_DISABLED)
196      {
197          bta_gattc_enable (p_cb);
198      }
199     /* todo need to check duplicate uuid */
200     for (i = 0; i < BTA_GATTC_CL_MAX; i ++)
201     {
202         if (!p_cb->cl_rcb[i].in_use)
203         {
204             if ((p_app_uuid == NULL) || (p_cb->cl_rcb[i].client_if = GATT_Register(p_app_uuid, &bta_gattc_cl_cback)) == 0)
205             {
206                 APPL_TRACE_ERROR("Register with GATT stack failed.");
207                 status = BTA_GATT_ERROR;
208             }
209             else
210             {
211                 p_cb->cl_rcb[i].in_use = TRUE;
212                 p_cb->cl_rcb[i].p_cback = p_data->api_reg.p_cback;
213                 memcpy(&p_cb->cl_rcb[i].app_uuid, p_app_uuid, sizeof(tBT_UUID));
214
215                 /* BTA use the same client interface as BTE GATT statck */
216                 cb_data.reg_oper.client_if = p_cb->cl_rcb[i].client_if;
217
218                 if ((p_buf = (tBTA_GATTC_INT_START_IF *) osi_getbuf(sizeof(tBTA_GATTC_INT_START_IF))) != NULL)
219                 {
220                     p_buf->hdr.event    = BTA_GATTC_INT_START_IF_EVT;
221                     p_buf->client_if    = p_cb->cl_rcb[i].client_if;
222
223                     bta_sys_sendmsg(p_buf);
224                     status = BTA_GATT_OK;
225                 }
226                 else
227                 {
228                     GATT_Deregister(p_cb->cl_rcb[i].client_if);
229
230                     status = BTA_GATT_NO_RESOURCES;
231                     memset( &p_cb->cl_rcb[i], 0 , sizeof(tBTA_GATTC_RCB));
232                 }
233                 break;
234             }
235         }
236     }
237
238     /* callback with register event */
239     if (p_data->api_reg.p_cback)
240     {
241         if (p_app_uuid != NULL)
242             memcpy(&(cb_data.reg_oper.app_uuid),p_app_uuid,sizeof(tBT_UUID));
243
244         cb_data.reg_oper.status = status;
245         (*p_data->api_reg.p_cback)(BTA_GATTC_REG_EVT,  (tBTA_GATTC *)&cb_data);
246     }
247 }
248 /*******************************************************************************
249 **
250 ** Function         bta_gattc_start_if
251 **
252 ** Description      start an application interface.
253 **
254 ** Returns          none.
255 **
256 *******************************************************************************/
257 void bta_gattc_start_if(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA *p_msg)
258 {
259     UNUSED(p_cb);
260
261     if (bta_gattc_cl_get_regcb(p_msg->int_start_if.client_if) !=NULL )
262     {
263         GATT_StartIf(p_msg->int_start_if.client_if);
264     }
265     else
266     {
267         APPL_TRACE_ERROR("Unable to start app.: Unknown interface =%d",p_msg->int_start_if.client_if );
268     }
269 }
270 /*******************************************************************************
271 **
272 ** Function         bta_gattc_deregister
273 **
274 ** Description      De-Register a GATT client application with BTA.
275 **
276 ** Returns          void
277 **
278 *******************************************************************************/
279 void bta_gattc_deregister(tBTA_GATTC_CB *p_cb, tBTA_GATTC_RCB  *p_clreg)
280 {
281     UINT8               i;
282     BT_HDR              buf;
283
284     if (p_clreg != NULL)
285     {
286         /* remove bg connection associated with this rcb */
287         for (i = 0; i < BTA_GATTC_KNOWN_SR_MAX; i ++)
288         {
289             if (p_cb->bg_track[i].in_use)
290             {
291                 if (p_cb->bg_track[i].cif_mask & (1 <<(p_clreg->client_if - 1)))
292                 {
293                     bta_gattc_mark_bg_conn(p_clreg->client_if, p_cb->bg_track[i].remote_bda, FALSE, FALSE);
294                     GATT_CancelConnect(p_clreg->client_if, p_cb->bg_track[i].remote_bda, FALSE);
295                 }
296                 if (p_cb->bg_track[i].cif_adv_mask & (1 <<(p_clreg->client_if - 1)))
297                 {
298                     bta_gattc_mark_bg_conn(p_clreg->client_if, p_cb->bg_track[i].remote_bda, FALSE, TRUE);
299                 }
300             }
301         }
302
303         if (p_clreg->num_clcb > 0)
304         {
305             /* close all CLCB related to this app */
306             for (i= 0; i < BTA_GATTC_CLCB_MAX; i ++)
307             {
308                 if (p_cb->clcb[i].in_use && (p_cb->clcb[i].p_rcb == p_clreg))
309                 {
310                     p_clreg->dereg_pending = TRUE;
311
312                     buf.event = BTA_GATTC_API_CLOSE_EVT;
313                     buf.layer_specific = p_cb->clcb[i].bta_conn_id;
314                     bta_gattc_close(&p_cb->clcb[i], (tBTA_GATTC_DATA *)&buf)  ;
315                 }
316             }
317         }
318         else
319             bta_gattc_deregister_cmpl(p_clreg);
320     }
321     else
322     {
323         APPL_TRACE_ERROR("bta_gattc_deregister Deregister Failedm unknown client cif");
324     }
325 }
326 /*******************************************************************************
327 **
328 ** Function         bta_gattc_process_api_open
329 **
330 ** Description      process connect API request.
331 **
332 ** Returns          void
333 **
334 *******************************************************************************/
335 void bta_gattc_process_api_open (tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA * p_msg)
336 {
337     UINT16 event = ((BT_HDR *)p_msg)->event;
338     tBTA_GATTC_CLCB *p_clcb = NULL;
339     tBTA_GATTC_RCB *p_clreg = bta_gattc_cl_get_regcb(p_msg->api_conn.client_if);
340     UNUSED(p_cb);
341
342     if (p_clreg != NULL)
343     {
344         if (p_msg->api_conn.is_direct)
345         {
346             if ((p_clcb = bta_gattc_find_alloc_clcb(p_msg->api_conn.client_if,
347                                                     p_msg->api_conn.remote_bda,
348                                                     p_msg->api_conn.transport)) != NULL)
349             {
350                 bta_gattc_sm_execute(p_clcb, event, p_msg);
351             }
352             else
353             {
354                 APPL_TRACE_ERROR("No resources to open a new connection.");
355
356                 bta_gattc_send_open_cback(p_clreg,
357                                           BTA_GATT_NO_RESOURCES,
358                                           p_msg->api_conn.remote_bda,
359                                           BTA_GATT_INVALID_CONN_ID,
360                                           p_msg->api_conn.transport, 0);
361             }
362         }
363         else
364         {
365             bta_gattc_init_bk_conn(&p_msg->api_conn, p_clreg);
366         }
367     }
368     else
369     {
370         APPL_TRACE_ERROR("bta_gattc_process_api_open Failed, unknown client_if: %d",
371                         p_msg->api_conn.client_if);
372     }
373 }
374 /*******************************************************************************
375 **
376 ** Function         bta_gattc_process_api_open_cancel
377 **
378 ** Description      process connect API request.
379 **
380 ** Returns          void
381 **
382 *******************************************************************************/
383 void bta_gattc_process_api_open_cancel (tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA * p_msg)
384 {
385     UINT16 event = ((BT_HDR *)p_msg)->event;
386     tBTA_GATTC_CLCB *p_clcb = NULL;
387     tBTA_GATTC_RCB *p_clreg;
388     tBTA_GATTC cb_data;
389     UNUSED(p_cb);
390
391     if (p_msg->api_cancel_conn.is_direct)
392     {
393         if ((p_clcb = bta_gattc_find_clcb_by_cif(p_msg->api_cancel_conn.client_if,
394                                                  p_msg->api_cancel_conn.remote_bda,
395                                                  BTA_GATT_TRANSPORT_LE)) != NULL)
396         {
397             bta_gattc_sm_execute(p_clcb, event, p_msg);
398         }
399         else
400         {
401             APPL_TRACE_ERROR("No such connection need to be cancelled");
402
403             p_clreg = bta_gattc_cl_get_regcb(p_msg->api_cancel_conn.client_if);
404
405             if (p_clreg && p_clreg->p_cback)
406             {
407                 cb_data.status = BTA_GATT_ERROR;
408                 (*p_clreg->p_cback)(BTA_GATTC_CANCEL_OPEN_EVT, &cb_data);
409             }
410         }
411     }
412     else
413     {
414         bta_gattc_cancel_bk_conn(&p_msg->api_cancel_conn);
415
416     }
417 }
418
419 /*******************************************************************************
420 **
421 ** Function         bta_gattc_process_enc_cmpl
422 **
423 ** Description      process encryption complete message.
424 **
425 ** Returns          void
426 **
427 *******************************************************************************/
428 void bta_gattc_process_enc_cmpl(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA *p_msg)
429 {
430     tBTA_GATTC_RCB *p_clreg;
431     tBTA_GATTC cb_data;
432     UNUSED(p_cb);
433
434     p_clreg = bta_gattc_cl_get_regcb(p_msg->enc_cmpl.client_if);
435
436     if (p_clreg && p_clreg->p_cback)
437     {
438         memset(&cb_data, 0, sizeof(tBTA_GATTC));
439
440         cb_data.enc_cmpl.client_if = p_msg->enc_cmpl.client_if;
441         bdcpy(cb_data.enc_cmpl.remote_bda, p_msg->enc_cmpl.remote_bda);
442
443         (*p_clreg->p_cback)(BTA_GATTC_ENC_CMPL_CB_EVT, &cb_data);
444     }
445 }
446
447 /*******************************************************************************
448 **
449 ** Function         bta_gattc_cancel_open_error
450 **
451 ** Description
452 **
453 ** Returns          void
454 **
455 *******************************************************************************/
456 void bta_gattc_cancel_open_error(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
457 {
458     tBTA_GATTC cb_data;
459     UNUSED(p_data);
460
461     cb_data.status=BTA_GATT_ERROR;
462
463     if ( p_clcb && p_clcb->p_rcb && p_clcb->p_rcb->p_cback )
464         (*p_clcb->p_rcb->p_cback)(BTA_GATTC_CANCEL_OPEN_EVT, &cb_data);
465 }
466
467 /*******************************************************************************
468 **
469 ** Function         bta_gattc_open_error
470 **
471 ** Description
472 **
473 ** Returns          void
474 **
475 *******************************************************************************/
476 void bta_gattc_open_error(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
477 {
478     UNUSED(p_data);
479
480     APPL_TRACE_ERROR("Connection already opened. wrong state");
481
482     bta_gattc_send_open_cback(p_clcb->p_rcb,
483                               BTA_GATT_OK,
484                               p_clcb->bda,
485                               p_clcb->bta_conn_id,
486                               p_clcb->transport,
487                               0);
488 }
489 /*******************************************************************************
490 **
491 ** Function         bta_gattc_open_fail
492 **
493 ** Description
494 **
495 ** Returns          void
496 **
497 *******************************************************************************/
498 void bta_gattc_open_fail(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
499 {
500     UNUSED(p_data);
501
502     bta_gattc_send_open_cback(p_clcb->p_rcb,
503                               BTA_GATT_ERROR,
504                               p_clcb->bda,
505                               p_clcb->bta_conn_id,
506                               p_clcb->transport,
507                               0);
508     /* open failure, remove clcb */
509     bta_gattc_clcb_dealloc(p_clcb);
510 }
511
512 /*******************************************************************************
513 **
514 ** Function         bta_gattc_open
515 **
516 ** Description      Process API connection function.
517 **
518 ** Returns          void
519 **
520 *******************************************************************************/
521 void bta_gattc_open(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
522 {
523     tBTA_GATTC_DATA gattc_data;
524
525     /* open/hold a connection */
526     if (!GATT_Connect(p_clcb->p_rcb->client_if, p_data->api_conn.remote_bda,
527                       TRUE, p_data->api_conn.transport))
528     {
529         APPL_TRACE_ERROR("Connection open failure");
530
531         bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_OPEN_FAIL_EVT, p_data);
532     }
533     else
534     {
535         /* a connected remote device */
536         if (GATT_GetConnIdIfConnected(p_clcb->p_rcb->client_if,
537                                       p_data->api_conn.remote_bda,
538                                       &p_clcb->bta_conn_id,
539                                       p_data->api_conn.transport))
540         {
541             gattc_data.int_conn.hdr.layer_specific = p_clcb->bta_conn_id;
542
543             bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_CONN_EVT, &gattc_data);
544         }
545         /* else wait for the callback event */
546     }
547 }
548 /*******************************************************************************
549 **
550 ** Function         bta_gattc_init_bk_conn
551 **
552 ** Description      Process API Open for a background connection
553 **
554 ** Returns          void
555 **
556 *******************************************************************************/
557 void bta_gattc_init_bk_conn(tBTA_GATTC_API_OPEN *p_data, tBTA_GATTC_RCB *p_clreg)
558 {
559     tBTA_GATT_STATUS         status = BTA_GATT_NO_RESOURCES;
560     UINT16                   conn_id;
561     tBTA_GATTC_CLCB         *p_clcb;
562     tBTA_GATTC_DATA         gattc_data;
563
564     if (bta_gattc_mark_bg_conn(p_data->client_if, p_data->remote_bda, TRUE, FALSE))
565     {
566         /* always call open to hold a connection */
567         if (!GATT_Connect(p_data->client_if, p_data->remote_bda, FALSE, p_data->transport))
568         {
569             uint8_t *bda = (uint8_t *)p_data->remote_bda;
570             status = BTA_GATT_ERROR;
571             APPL_TRACE_ERROR("%s unable to connect to remote bd_addr:%02x:%02x:%02x:%02x:%02x:%02x",
572                 __func__, bda[0], bda[1], bda[2], bda[3], bda[4], bda[5]);
573
574         }
575         else
576         {
577             status = BTA_GATT_OK;
578
579             /* if is a connected remote device */
580             if (GATT_GetConnIdIfConnected(p_data->client_if,
581                                           p_data->remote_bda,
582                                           &conn_id,
583                                           p_data->transport))
584             {
585                 if ((p_clcb = bta_gattc_find_alloc_clcb(p_data->client_if, p_data->remote_bda,
586                     BTA_GATT_TRANSPORT_LE)) != NULL)
587                 {
588                     gattc_data.hdr.layer_specific = p_clcb->bta_conn_id = conn_id;
589
590                     /* open connection */
591                     bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_CONN_EVT, &gattc_data);
592                     status = BTA_GATT_OK;
593                 }
594             }
595         }
596     }
597
598     /* open failure, report OPEN_EVT */
599     if (status != BTA_GATT_OK)
600     {
601         bta_gattc_send_open_cback(p_clreg, status, p_data->remote_bda,
602         BTA_GATT_INVALID_CONN_ID, BTA_GATT_TRANSPORT_LE, 0);
603     }
604 }
605 /*******************************************************************************
606 **
607 ** Function         bta_gattc_cancel_bk_conn
608 **
609 ** Description      Process API Cancel Open for a background connection
610 **
611 ** Returns          void
612 **
613 *******************************************************************************/
614 void bta_gattc_cancel_bk_conn(tBTA_GATTC_API_CANCEL_OPEN *p_data)
615 {
616     tBTA_GATTC_RCB      *p_clreg;
617     tBTA_GATTC          cb_data;
618     cb_data.status = BTA_GATT_ERROR;
619
620     /* remove the device from the bg connection mask */
621     if (bta_gattc_mark_bg_conn(p_data->client_if, p_data->remote_bda, FALSE, FALSE))
622     {
623         if (GATT_CancelConnect(p_data->client_if, p_data->remote_bda, FALSE))
624         {
625             cb_data.status = BTA_GATT_OK;
626         }
627         else
628         {
629             APPL_TRACE_ERROR("bta_gattc_cancel_bk_conn failed");
630         }
631     }
632     p_clreg = bta_gattc_cl_get_regcb(p_data->client_if);
633
634     if (p_clreg && p_clreg->p_cback)
635     {
636         (*p_clreg->p_cback)(BTA_GATTC_CANCEL_OPEN_EVT, &cb_data);
637     }
638
639 }
640 /*******************************************************************************
641 **
642 ** Function         bta_gattc_int_cancel_open_ok
643 **
644 ** Description
645 **
646 ** Returns          void
647 **
648 *******************************************************************************/
649 void bta_gattc_cancel_open_ok(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
650 {
651     tBTA_GATTC          cb_data;
652     UNUSED(p_data);
653
654     if ( p_clcb->p_rcb->p_cback )
655     {
656         cb_data.status = BTA_GATT_OK;
657         (*p_clcb->p_rcb->p_cback)(BTA_GATTC_CANCEL_OPEN_EVT, &cb_data);
658     }
659
660     bta_gattc_clcb_dealloc(p_clcb);
661 }
662 /*******************************************************************************
663 **
664 ** Function         bta_gattc_cancel_open
665 **
666 ** Description
667 **
668 ** Returns          void
669 **
670 *******************************************************************************/
671 void bta_gattc_cancel_open(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
672 {
673     tBTA_GATTC          cb_data;
674
675     if (GATT_CancelConnect(p_clcb->p_rcb->client_if, p_data->api_cancel_conn.remote_bda, TRUE))
676     {
677         bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_CANCEL_OPEN_OK_EVT, p_data);
678     }
679     else
680     {
681         if ( p_clcb->p_rcb->p_cback )
682         {
683             cb_data.status = BTA_GATT_ERROR;
684             (*p_clcb->p_rcb->p_cback)(BTA_GATTC_CANCEL_OPEN_EVT, &cb_data);
685         }
686     }
687 }
688 /*******************************************************************************
689 **
690 ** Function         bta_gattc_conn
691 **
692 ** Description      receive connection callback from stack
693 **
694 ** Returns          void
695 **
696 *******************************************************************************/
697 void bta_gattc_conn(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
698 {
699     tBTA_GATTC_IF   gatt_if;
700     APPL_TRACE_DEBUG("bta_gattc_conn server cache state=%d",p_clcb->p_srcb->state);
701
702     if (p_data != NULL)
703     {
704         APPL_TRACE_DEBUG("bta_gattc_conn conn_id=%d",p_data->hdr.layer_specific);
705         p_clcb->bta_conn_id  = p_data->int_conn.hdr.layer_specific;
706
707         GATT_GetConnectionInfor(p_data->hdr.layer_specific,
708                                 &gatt_if, p_clcb->bda, &p_clcb->transport);
709     }
710
711         p_clcb->p_srcb->connected = TRUE;
712
713         if (p_clcb->p_srcb->mtu == 0)
714             p_clcb->p_srcb->mtu = GATT_DEF_BLE_MTU_SIZE;
715
716         /* start database cache if needed */
717         if (p_clcb->p_srcb->p_srvc_cache == NULL ||
718             p_clcb->p_srcb->state != BTA_GATTC_SERV_IDLE)
719         {
720             if (p_clcb->p_srcb->state == BTA_GATTC_SERV_IDLE)
721             {
722                 p_clcb->p_srcb->state = BTA_GATTC_SERV_LOAD;
723                 bta_gattc_sm_execute(p_clcb, BTA_GATTC_START_CACHE_EVT, NULL);
724             }
725             else /* cache is building */
726                 p_clcb->state = BTA_GATTC_DISCOVER_ST;
727         }
728
729         else
730         {
731             /* a pending service handle change indication */
732             if (p_clcb->p_srcb->srvc_hdl_chg)
733             {
734                 p_clcb->p_srcb->srvc_hdl_chg = FALSE;
735                 /* start discovery */
736                 bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_DISCOVER_EVT, NULL);
737             }
738         }
739
740         if (p_clcb->p_rcb)
741         {
742         /* there is no RM for GATT */
743         if (p_clcb->transport == BTA_TRANSPORT_BR_EDR)
744             bta_sys_conn_open(BTA_ID_GATTC, BTA_ALL_APP_ID, p_clcb->bda);
745
746         bta_gattc_send_open_cback(p_clcb->p_rcb,
747                                   BTA_GATT_OK,
748                                   p_clcb->bda,
749                                   p_clcb->bta_conn_id,
750                                   p_clcb->transport,
751                                   p_clcb->p_srcb->mtu);
752         }
753     }
754 /*******************************************************************************
755 **
756 ** Function         bta_gattc_close_fail
757 **
758 ** Description      close a  connection.
759 **
760 ** Returns          void
761 **
762 *******************************************************************************/
763 void bta_gattc_close_fail(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
764 {
765     tBTA_GATTC           cb_data;
766
767     if ( p_clcb->p_rcb->p_cback )
768     {
769         memset(&cb_data, 0, sizeof(tBTA_GATTC));
770         cb_data.close.client_if = p_clcb->p_rcb->client_if;
771         cb_data.close.conn_id   = p_data->hdr.layer_specific;
772         bdcpy(cb_data.close.remote_bda, p_clcb->bda);
773         cb_data.close.status    = BTA_GATT_ERROR;
774         cb_data.close.reason    = BTA_GATT_CONN_NONE;
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_GATT_STATUS    status;
935
936     if (bta_gattc_enqueue(p_clcb, p_data))
937     {
938         status = GATTC_ConfigureMTU (p_clcb->bta_conn_id, p_data->api_mtu.mtu);
939
940         /* if failed, return callback here */
941         if (status != GATT_SUCCESS && status != GATT_CMD_STARTED)
942         {
943             /* Dequeue the data, if it was enqueued */
944             if (p_clcb->p_q_cmd == p_data)
945                 p_clcb->p_q_cmd = NULL;
946
947             bta_gattc_cmpl_sendmsg(p_clcb->bta_conn_id, GATTC_OPTYPE_CONFIG, status, NULL);
948         }
949     }
950 }
951 /*******************************************************************************
952 **
953 ** Function         bta_gattc_start_discover
954 **
955 ** Description      Start a discovery on server.
956 **
957 ** Returns          None.
958 **
959 *******************************************************************************/
960 void bta_gattc_start_discover(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
961 {
962     UNUSED(p_data);
963
964     APPL_TRACE_DEBUG("bta_gattc_start_discover conn_id=%d p_clcb->p_srcb->state = %d ",
965         p_clcb->bta_conn_id, p_clcb->p_srcb->state);
966
967     if (((p_clcb->p_q_cmd == NULL || p_clcb->auto_update == BTA_GATTC_REQ_WAITING) &&
968         p_clcb->p_srcb->state == BTA_GATTC_SERV_IDLE) ||
969         p_clcb->p_srcb->state == BTA_GATTC_SERV_DISC)
970     /* no pending operation, start discovery right away */
971     {
972         p_clcb->auto_update = BTA_GATTC_NO_SCHEDULE;
973
974         if (p_clcb->p_srcb != NULL)
975         {
976             /* clear the service change mask */
977             p_clcb->p_srcb->srvc_hdl_chg = FALSE;
978             p_clcb->p_srcb->update_count = 0;
979             p_clcb->p_srcb->state = BTA_GATTC_SERV_DISC_ACT;
980
981             if (p_clcb->transport == BTA_TRANSPORT_LE)
982                 L2CA_EnableUpdateBleConnParams(p_clcb->p_srcb->server_bda, FALSE);
983
984             /* set all srcb related clcb into discovery ST */
985             bta_gattc_set_discover_st(p_clcb->p_srcb);
986
987             if ((p_clcb->status = bta_gattc_init_cache(p_clcb->p_srcb)) == BTA_GATT_OK)
988             {
989                 p_clcb->status = bta_gattc_discover_pri_service(p_clcb->bta_conn_id,
990                                                                p_clcb->p_srcb, GATT_DISC_SRVC_ALL);
991             }
992             if (p_clcb->status != BTA_GATT_OK)
993             {
994                 APPL_TRACE_ERROR("discovery on server failed");
995                 bta_gattc_reset_discover_st(p_clcb->p_srcb, p_clcb->status);
996             }
997             else
998                 p_clcb->disc_active = TRUE;
999         }
1000         else
1001         {
1002             APPL_TRACE_ERROR("unknown device, can not start discovery");
1003         }
1004     }
1005     /* pending operation, wait until it finishes */
1006     else
1007     {
1008         p_clcb->auto_update = BTA_GATTC_DISC_WAITING;
1009
1010         if (p_clcb->p_srcb->state == BTA_GATTC_SERV_IDLE)
1011             p_clcb->state = BTA_GATTC_CONN_ST; /* set clcb state */
1012     }
1013
1014 }
1015 /*******************************************************************************
1016 **
1017 ** Function         bta_gattc_disc_cmpl
1018 **
1019 ** Description      discovery on server is finished
1020 **
1021 ** Returns          None.
1022 **
1023 *******************************************************************************/
1024 void bta_gattc_disc_cmpl(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1025 {
1026     tBTA_GATTC_DATA *p_q_cmd = p_clcb->p_q_cmd;
1027     UNUSED(p_data);
1028
1029     APPL_TRACE_DEBUG("bta_gattc_disc_cmpl conn_id=%d",p_clcb->bta_conn_id);
1030
1031 #if BLE_INCLUDED == TRUE
1032     if(p_clcb->transport == BTA_TRANSPORT_LE)
1033         L2CA_EnableUpdateBleConnParams(p_clcb->p_srcb->server_bda, TRUE);
1034 #endif
1035     p_clcb->p_srcb->state = BTA_GATTC_SERV_IDLE;
1036     p_clcb->disc_active = FALSE;
1037
1038     if (p_clcb->status != GATT_SUCCESS)
1039     {
1040         /* clean up cache */
1041         if(p_clcb->p_srcb && p_clcb->p_srcb->p_srvc_cache)
1042         {
1043             while (! fixed_queue_is_empty(p_clcb->p_srcb->cache_buffer))
1044             {
1045                 osi_freebuf(fixed_queue_try_dequeue(p_clcb->p_srcb->cache_buffer));
1046             }
1047             p_clcb->p_srcb->p_srvc_cache = NULL;
1048         }
1049
1050         /* used to reset cache in application */
1051         bta_gattc_co_cache_reset(p_clcb->p_srcb->server_bda);
1052     }
1053     if (p_clcb->p_srcb && p_clcb->p_srcb->p_srvc_list) {
1054         /* release pending attribute list buffer */
1055         osi_freebuf_and_reset((void **)&p_clcb->p_srcb->p_srvc_list);
1056     }
1057
1058     if (p_clcb->auto_update == BTA_GATTC_DISC_WAITING)
1059     {
1060         /* start discovery again */
1061         bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_DISCOVER_EVT, NULL);
1062     }
1063     /* get any queued command to proceed */
1064     else if (p_q_cmd != NULL)
1065     {
1066         p_clcb->p_q_cmd = NULL;
1067         /* execute pending operation of link block still present */
1068         if (l2cu_find_lcb_by_bd_addr(p_clcb->p_srcb->server_bda, BT_TRANSPORT_LE) != NULL) {
1069             bta_gattc_sm_execute(p_clcb, p_q_cmd->hdr.event, p_q_cmd);
1070         }
1071         /* if the command executed requeued the cmd, we don't
1072          * want to free the underlying buffer that's being
1073          * referenced by p_clcb->p_q_cmd
1074          */
1075         if (p_q_cmd != p_clcb->p_q_cmd)
1076             osi_freebuf_and_reset((void **)&p_q_cmd);
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_GATT_STATUS    status;
1093
1094     memset (&read_param, 0 ,sizeof(tGATT_READ_PARAM));
1095
1096     if (bta_gattc_enqueue(p_clcb, p_data))
1097     {
1098         if ((handle = bta_gattc_id2handle(p_clcb->p_srcb,
1099                                           &p_data->api_read.srvc_id,
1100                                           &p_data->api_read.char_id,
1101                                           p_data->api_read.p_descr_type)) == 0)
1102         {
1103             status = BTA_GATT_ERROR;
1104         }
1105         else
1106         {
1107             read_param.by_handle.handle = handle;
1108             read_param.by_handle.auth_req = p_data->api_read.auth_req;
1109
1110             status = GATTC_Read(p_clcb->bta_conn_id, GATT_READ_BY_HANDLE, &read_param);
1111         }
1112
1113         /* read fail */
1114         if (status != BTA_GATT_OK)
1115         {
1116             /* Dequeue the data, if it was enqueued */
1117             if (p_clcb->p_q_cmd == p_data)
1118                 p_clcb->p_q_cmd = NULL;
1119
1120             bta_gattc_cmpl_sendmsg(p_clcb->bta_conn_id, GATTC_OPTYPE_READ, status, NULL);
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_ATTR_ID  *p_id;
1138
1139     if (bta_gattc_enqueue(p_clcb, p_data))
1140     {
1141         memset(&read_param, 0, sizeof(tGATT_READ_PARAM));
1142
1143         p_id = p_data->api_read_multi.p_id_list;
1144
1145         for (i = 0; i < p_data->api_read_multi.num_attr && p_id; i ++, p_id ++)
1146         {
1147             handle = 0;
1148
1149             if (p_id->id_type == BTA_GATT_TYPE_CHAR)
1150             {
1151                 handle = bta_gattc_id2handle(p_clcb->p_srcb,
1152                                      &p_id->id_value.char_id.srvc_id,
1153                                      &p_id->id_value.char_id.char_id,
1154                                      NULL);
1155             }
1156             else if (p_id->id_type == BTA_GATT_TYPE_CHAR_DESCR)
1157             {
1158                 handle = bta_gattc_id2handle(p_clcb->p_srcb,
1159                                      &p_id->id_value.char_descr_id.char_id.srvc_id,
1160                                      &p_id->id_value.char_descr_id.char_id.char_id,
1161                                      &p_id->id_value.char_descr_id.descr_id);
1162             }
1163             else
1164             {
1165                 APPL_TRACE_ERROR("invalud ID type: %d", p_id->id_type);
1166             }
1167
1168             if (handle == 0)
1169             {
1170                 status = BTA_GATT_ERROR;
1171                 break;
1172             }
1173         }
1174         if (status == BTA_GATT_OK)
1175         {
1176             read_param.read_multiple.num_handles = p_data->api_read_multi.num_attr;
1177             read_param.read_multiple.auth_req = p_data->api_read_multi.auth_req;
1178
1179             status = GATTC_Read(p_clcb->bta_conn_id, GATT_READ_MULTIPLE, &read_param);
1180         }
1181
1182         /* read fail */
1183         if (status != BTA_GATT_OK)
1184         {
1185             /* Dequeue the data, if it was enqueued */
1186             if (p_clcb->p_q_cmd == p_data)
1187                 p_clcb->p_q_cmd = NULL;
1188
1189             bta_gattc_cmpl_sendmsg(p_clcb->bta_conn_id, GATTC_OPTYPE_READ, status, NULL);
1190         }
1191     }
1192 }
1193 /*******************************************************************************
1194 **
1195 ** Function         bta_gattc_write
1196 **
1197 ** Description      Write an attribute
1198 **
1199 ** Returns          None.
1200 **
1201 *******************************************************************************/
1202 void bta_gattc_write(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1203 {
1204     UINT16              handle = 0;
1205     tGATT_VALUE         attr = {0};
1206     tBTA_GATT_STATUS    status = BTA_GATT_OK;
1207
1208     if (bta_gattc_enqueue(p_clcb, p_data))
1209     {
1210         if ((handle = bta_gattc_id2handle(p_clcb->p_srcb,
1211                                           &p_data->api_write.srvc_id,
1212                                           &p_data->api_write.char_id,
1213                                           p_data->api_write.p_descr_type)) == 0)
1214         {
1215             status = BTA_GATT_ERROR;
1216         }
1217         else
1218         {
1219             attr.handle= handle;
1220             attr.offset = p_data->api_write.offset;
1221             attr.len    = p_data->api_write.len;
1222             attr.auth_req = p_data->api_write.auth_req;
1223
1224             if (p_data->api_write.p_value)
1225                 memcpy(attr.value, p_data->api_write.p_value, p_data->api_write.len);
1226
1227             status = GATTC_Write(p_clcb->bta_conn_id, p_data->api_write.write_type, &attr);
1228         }
1229
1230         /* write fail */
1231         if (status != BTA_GATT_OK)
1232         {
1233             /* Dequeue the data, if it was enqueued */
1234             if (p_clcb->p_q_cmd == p_data)
1235                 p_clcb->p_q_cmd = NULL;
1236
1237             bta_gattc_cmpl_sendmsg(p_clcb->bta_conn_id, GATTC_OPTYPE_WRITE, status, NULL);
1238         }
1239     }
1240 }
1241 /*******************************************************************************
1242 **
1243 ** Function         bta_gattc_execute
1244 **
1245 ** Description      send execute write
1246 **
1247 ** Returns          None.
1248 *********************************************************************************/
1249 void bta_gattc_execute(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1250 {
1251     tBTA_GATT_STATUS    status;
1252
1253     if (bta_gattc_enqueue(p_clcb, p_data))
1254     {
1255         status = GATTC_ExecuteWrite(p_clcb->bta_conn_id, p_data->api_exec.is_execute);
1256
1257         if (status != BTA_GATT_OK)
1258         {
1259             /* Dequeue the data, if it was enqueued */
1260             if (p_clcb->p_q_cmd == p_data)
1261                 p_clcb->p_q_cmd = NULL;
1262
1263             bta_gattc_cmpl_sendmsg(p_clcb->bta_conn_id, GATTC_OPTYPE_EXE_WRITE, status, NULL);
1264         }
1265     }
1266 }
1267 /*******************************************************************************
1268 **
1269 ** Function         bta_gattc_confirm
1270 **
1271 ** Description      send handle value confirmation
1272 **
1273 ** Returns          None.
1274 **
1275 *******************************************************************************/
1276 void bta_gattc_confirm(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1277 {
1278     UINT16 handle;
1279
1280     if ((handle = bta_gattc_id2handle(p_clcb->p_srcb,
1281                                       &p_data->api_confirm.srvc_id,
1282                                       &p_data->api_confirm.char_id,
1283                                       NULL)) == 0)
1284     {
1285         APPL_TRACE_ERROR("Can not map service/char ID into valid handle");
1286     }
1287     else
1288     {
1289         if (GATTC_SendHandleValueConfirm(p_data->api_confirm.hdr.layer_specific, handle)
1290             != GATT_SUCCESS)
1291         {
1292             APPL_TRACE_ERROR("bta_gattc_confirm to handle [0x%04x] failed", handle);
1293         }
1294         else
1295         {
1296             /* if over BR_EDR, inform PM for mode change */
1297             if (p_clcb->transport == BTA_TRANSPORT_BR_EDR)
1298             {
1299                 bta_sys_busy(BTA_ID_GATTC, BTA_ALL_APP_ID, p_clcb->bda);
1300                 bta_sys_idle(BTA_ID_GATTC, BTA_ALL_APP_ID, p_clcb->bda);
1301             }
1302         }
1303     }
1304 }
1305 /*******************************************************************************
1306 **
1307 ** Function         bta_gattc_read_cmpl
1308 **
1309 ** Description      read complete
1310 **
1311 ** Returns          None.
1312 **
1313 *******************************************************************************/
1314 void bta_gattc_read_cmpl(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_OP_CMPL *p_data)
1315 {
1316     UINT8               event;
1317     tBTA_GATTC          cb_data;
1318     tBTA_GATT_READ_VAL  read_value;
1319
1320     memset(&cb_data, 0, sizeof(tBTA_GATTC));
1321     memset(&read_value, 0, sizeof(tBTA_GATT_READ_VAL));
1322
1323     cb_data.read.status     = p_data->status;
1324
1325     if (p_data->p_cmpl != NULL && p_data->status == BTA_GATT_OK)
1326     {
1327         if (bta_gattc_handle2id(p_clcb->p_srcb,
1328                                 p_data->p_cmpl->att_value.handle,
1329                                 &cb_data.read.srvc_id,
1330                                 &cb_data.read.char_id,
1331                                 &cb_data.read.descr_type) == FALSE)
1332         {
1333             cb_data.read.status = BTA_GATT_INTERNAL_ERROR;
1334             APPL_TRACE_ERROR("can not map to GATT ID. handle = 0x%04x",
1335                                 p_data->p_cmpl->att_value.handle);
1336         }
1337         else
1338         {
1339             cb_data.read.status = bta_gattc_pack_read_cb_data(p_clcb->p_srcb,
1340                                                               &cb_data.read.descr_type.uuid,
1341                                                               &p_data->p_cmpl->att_value,
1342                                                               &read_value);
1343             cb_data.read.p_value = &read_value;
1344         }
1345     }
1346     else
1347     {
1348         cb_data.read.srvc_id = p_clcb->p_q_cmd->api_read.srvc_id;
1349         cb_data.read.char_id = p_clcb->p_q_cmd->api_read.char_id;
1350         if (p_clcb->p_q_cmd->api_read.p_descr_type)
1351             memcpy(&cb_data.read.descr_type, p_clcb->p_q_cmd->api_read.p_descr_type,
1352                    sizeof(tBTA_GATT_ID));
1353     }
1354
1355     event = (p_clcb->p_q_cmd->api_read.p_descr_type == NULL) ?
1356                     BTA_GATTC_READ_CHAR_EVT: BTA_GATTC_READ_DESCR_EVT;
1357     cb_data.read.conn_id = p_clcb->bta_conn_id;
1358
1359     osi_freebuf_and_reset((void **)&p_clcb->p_q_cmd);
1360     /* read complete, callback */
1361     ( *p_clcb->p_rcb->p_cback)(event, (tBTA_GATTC *)&cb_data);
1362
1363 }
1364 /*******************************************************************************
1365 **
1366 ** Function         bta_gattc_write_cmpl
1367 **
1368 ** Description      write complete
1369 **
1370 ** Returns          None.
1371 **
1372 *******************************************************************************/
1373 void bta_gattc_write_cmpl(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_OP_CMPL *p_data)
1374 {
1375     tBTA_GATTC      cb_data = {0};
1376     UINT8          event;
1377
1378     memset(&cb_data, 0, sizeof(tBTA_GATTC));
1379
1380     cb_data.write.status     = p_data->status;
1381
1382     if (p_data->p_cmpl != NULL)
1383     {
1384         bta_gattc_handle2id(p_clcb->p_srcb, p_data->p_cmpl->att_value.handle,
1385                             &cb_data.write.srvc_id, &cb_data.write.char_id,
1386                             &cb_data.write.descr_type);
1387     }
1388     else
1389     {
1390         memcpy(&cb_data.write.srvc_id, &p_clcb->p_q_cmd->api_write.srvc_id,
1391                 sizeof(tBTA_GATT_SRVC_ID));
1392         memcpy(&cb_data.write.char_id, &p_clcb->p_q_cmd->api_write.char_id,
1393                 sizeof(tBTA_GATT_ID));
1394         if (p_clcb->p_q_cmd->api_write.p_descr_type)
1395             memcpy(&cb_data.write.descr_type, p_clcb->p_q_cmd->api_write.p_descr_type,
1396                    sizeof(tBTA_GATT_ID));
1397     }
1398
1399     if (p_clcb->p_q_cmd->api_write.hdr.event == BTA_GATTC_API_WRITE_EVT &&
1400         p_clcb->p_q_cmd->api_write.write_type == BTA_GATTC_WRITE_PREPARE)
1401
1402         event = BTA_GATTC_PREP_WRITE_EVT;
1403
1404     else if (p_clcb->p_q_cmd->api_write.p_descr_type == NULL)
1405
1406         event = BTA_GATTC_WRITE_CHAR_EVT;
1407
1408     else
1409         event = BTA_GATTC_WRITE_DESCR_EVT;
1410
1411     osi_freebuf_and_reset((void **)&p_clcb->p_q_cmd);
1412     cb_data.write.conn_id = p_clcb->bta_conn_id;
1413     /* write complete, callback */
1414     ( *p_clcb->p_rcb->p_cback)(event, (tBTA_GATTC *)&cb_data);
1415
1416 }
1417 /*******************************************************************************
1418 **
1419 ** Function         bta_gattc_exec_cmpl
1420 **
1421 ** Description      execute write complete
1422 **
1423 ** Returns          None.
1424 **
1425 *******************************************************************************/
1426 void bta_gattc_exec_cmpl(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_OP_CMPL *p_data)
1427 {
1428     tBTA_GATTC          cb_data;
1429
1430     osi_freebuf_and_reset((void **)&p_clcb->p_q_cmd);
1431     p_clcb->status      = BTA_GATT_OK;
1432
1433     /* execute complete, callback */
1434     cb_data.exec_cmpl.conn_id = p_clcb->bta_conn_id;
1435     cb_data.exec_cmpl.status = p_data->status;
1436
1437     ( *p_clcb->p_rcb->p_cback)(BTA_GATTC_EXEC_EVT,  &cb_data);
1438
1439 }
1440
1441 /*******************************************************************************
1442 **
1443 ** Function         bta_gattc_cfg_mtu_cmpl
1444 **
1445 ** Description      configure MTU operation complete
1446 **
1447 ** Returns          None.
1448 **
1449 *******************************************************************************/
1450 void bta_gattc_cfg_mtu_cmpl(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_OP_CMPL *p_data)
1451 {
1452     tBTA_GATTC          cb_data;
1453
1454     osi_freebuf_and_reset((void **)&p_clcb->p_q_cmd);
1455
1456     if (p_data->p_cmpl  &&  p_data->status == BTA_GATT_OK)
1457         p_clcb->p_srcb->mtu  = p_data->p_cmpl->mtu;
1458
1459     /* configure MTU complete, callback */
1460     p_clcb->status          = p_data->status;
1461     cb_data.cfg_mtu.conn_id = p_clcb->bta_conn_id;
1462     cb_data.cfg_mtu.status  = p_data->status;
1463     cb_data.cfg_mtu.mtu     = p_clcb->p_srcb->mtu;
1464
1465     (*p_clcb->p_rcb->p_cback) (BTA_GATTC_CFG_MTU_EVT,  &cb_data);
1466
1467 }
1468 /*******************************************************************************
1469 **
1470 ** Function         bta_gattc_op_cmpl
1471 **
1472 ** Description      operation completed.
1473 **
1474 ** Returns          None.
1475 **
1476 *******************************************************************************/
1477 void  bta_gattc_op_cmpl(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1478 {
1479     UINT8           op = (UINT8)p_data->op_cmpl.op_code;
1480     UINT8           mapped_op = 0;
1481
1482     APPL_TRACE_DEBUG("bta_gattc_op_cmpl op = %d", op);
1483
1484     if (op == GATTC_OPTYPE_INDICATION || op == GATTC_OPTYPE_NOTIFICATION)
1485     {
1486         APPL_TRACE_ERROR("unexpected operation, ignored");
1487     }
1488     else if (op >= GATTC_OPTYPE_READ)
1489     {
1490         if (p_clcb->p_q_cmd == NULL)
1491         {
1492             APPL_TRACE_ERROR("No pending command");
1493             return;
1494         }
1495         if (p_clcb->p_q_cmd->hdr.event != bta_gattc_opcode_to_int_evt[op - GATTC_OPTYPE_READ])
1496         {
1497             mapped_op = p_clcb->p_q_cmd->hdr.event - BTA_GATTC_API_READ_EVT + GATTC_OPTYPE_READ;
1498             if ( mapped_op > GATTC_OPTYPE_INDICATION)   mapped_op = 0;
1499
1500 #if (BT_TRACE_VERBOSE == TRUE)
1501             APPL_TRACE_ERROR("expect op:(%s :0x%04x), receive unexpected operation (%s).",
1502                                 bta_gattc_op_code_name[mapped_op] , p_clcb->p_q_cmd->hdr.event,
1503                                 bta_gattc_op_code_name[op]);
1504 #else
1505             APPL_TRACE_ERROR("expect op:(%u :0x%04x), receive unexpected operation (%u).",
1506                                 mapped_op , p_clcb->p_q_cmd->hdr.event, op);
1507 #endif
1508             return;
1509         }
1510
1511         /* discard responses if service change indication is received before operation completed */
1512         if (p_clcb->auto_update == BTA_GATTC_DISC_WAITING && p_clcb->p_srcb->srvc_hdl_chg)
1513         {
1514             APPL_TRACE_DEBUG("Discard all responses when service change indication is received.");
1515             p_data->op_cmpl.status = GATT_ERROR;
1516         }
1517
1518         /* service handle change void the response, discard it */
1519         if (op == GATTC_OPTYPE_READ)
1520             bta_gattc_read_cmpl(p_clcb, &p_data->op_cmpl);
1521
1522         else if (op == GATTC_OPTYPE_WRITE)
1523             bta_gattc_write_cmpl(p_clcb, &p_data->op_cmpl);
1524
1525         else if (op == GATTC_OPTYPE_EXE_WRITE)
1526             bta_gattc_exec_cmpl(p_clcb, &p_data->op_cmpl);
1527
1528         else if (op == GATTC_OPTYPE_CONFIG)
1529             bta_gattc_cfg_mtu_cmpl(p_clcb, &p_data->op_cmpl);
1530
1531         if (p_clcb->auto_update == BTA_GATTC_DISC_WAITING)
1532         {
1533             p_clcb->auto_update = BTA_GATTC_REQ_WAITING;
1534             bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_DISCOVER_EVT, NULL);
1535         }
1536     }
1537 }
1538 /*******************************************************************************
1539 **
1540 ** Function         bta_gattc_op_cmpl
1541 **
1542 ** Description      operation completed.
1543 **
1544 ** Returns          None.
1545 **
1546 *******************************************************************************/
1547 void  bta_gattc_ignore_op_cmpl(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1548 {
1549     UNUSED(p_clcb);
1550
1551     /* receive op complete when discovery is started, ignore the response,
1552         and wait for discovery finish and resent */
1553     APPL_TRACE_DEBUG("bta_gattc_ignore_op_cmpl op = %d", p_data->hdr.layer_specific);
1554
1555 }
1556 /*******************************************************************************
1557 **
1558 ** Function         bta_gattc_search
1559 **
1560 ** Description      start a search in the local server cache
1561 **
1562 ** Returns          None.
1563 **
1564 *******************************************************************************/
1565 void bta_gattc_search(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1566 {
1567     tBTA_GATT_STATUS    status = GATT_INTERNAL_ERROR;
1568     tBTA_GATTC cb_data;
1569     APPL_TRACE_DEBUG("bta_gattc_search conn_id=%d",p_clcb->bta_conn_id);
1570     if (p_clcb->p_srcb && p_clcb->p_srcb->p_srvc_cache)
1571     {
1572         status = BTA_GATT_OK;
1573         /* search the local cache of a server device */
1574         bta_gattc_search_service(p_clcb, p_data->api_search.p_srvc_uuid);
1575     }
1576     cb_data.search_cmpl.status  = status;
1577     cb_data.search_cmpl.conn_id = p_clcb->bta_conn_id;
1578
1579     /* end of search or no server cache available */
1580     ( *p_clcb->p_rcb->p_cback)(BTA_GATTC_SEARCH_CMPL_EVT,  &cb_data);
1581 }
1582 /*******************************************************************************
1583 **
1584 ** Function         bta_gattc_q_cmd
1585 **
1586 ** Description      enqueue a command into control block, usually because discovery
1587 **                  operation is busy.
1588 **
1589 ** Returns          None.
1590 **
1591 *******************************************************************************/
1592 void bta_gattc_q_cmd(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1593 {
1594     bta_gattc_enqueue(p_clcb, p_data);
1595 }
1596 /*******************************************************************************
1597 **
1598 ** Function         bta_gattc_cache_open
1599 **
1600 ** Description      open a NV cache for loading
1601 **
1602 ** Returns          void
1603 **
1604 *******************************************************************************/
1605 void bta_gattc_cache_open(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1606 {
1607     UNUSED(p_data);
1608
1609     bta_gattc_set_discover_st(p_clcb->p_srcb);
1610
1611     APPL_TRACE_DEBUG("bta_gattc_cache_open conn_id=%d",p_clcb->bta_conn_id);
1612     bta_gattc_co_cache_open(p_clcb->p_srcb->server_bda, BTA_GATTC_CI_CACHE_OPEN_EVT,
1613                             p_clcb->bta_conn_id, FALSE);
1614 }
1615 /*******************************************************************************
1616 **
1617 ** Function         bta_gattc_start_load
1618 **
1619 ** Description      start cache loading by sending callout open cache
1620 **
1621 ** Returns          None.
1622 **
1623 *******************************************************************************/
1624 void bta_gattc_ci_open(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1625 {
1626     APPL_TRACE_DEBUG("bta_gattc_ci_open conn_id=%d server state=%d" ,
1627                       p_clcb->bta_conn_id, p_clcb->p_srcb->state);
1628     if (p_clcb->p_srcb->state == BTA_GATTC_SERV_LOAD)
1629     {
1630         if (p_data->ci_open.status == BTA_GATT_OK)
1631         {
1632             p_clcb->p_srcb->attr_index = 0;
1633             bta_gattc_co_cache_load(p_clcb->p_srcb->server_bda,
1634                                     BTA_GATTC_CI_CACHE_LOAD_EVT,
1635                                     p_clcb->p_srcb->attr_index,
1636                                     p_clcb->bta_conn_id);
1637         }
1638         else
1639         {
1640             p_clcb->p_srcb->state = BTA_GATTC_SERV_DISC;
1641             /* cache open failure, start discovery */
1642             bta_gattc_start_discover(p_clcb, NULL);
1643         }
1644     }
1645     if (p_clcb->p_srcb->state == BTA_GATTC_SERV_SAVE)
1646     {
1647         if (p_data->ci_open.status == BTA_GATT_OK)
1648         {
1649             if (!bta_gattc_cache_save(p_clcb->p_srcb, p_clcb->bta_conn_id))
1650             {
1651                 p_data->ci_open.status = BTA_GATT_ERROR;
1652             }
1653         }
1654         if (p_data->ci_open.status != BTA_GATT_OK)
1655         {
1656             p_clcb->p_srcb->attr_index = 0;
1657             bta_gattc_co_cache_close(p_clcb->p_srcb->server_bda, p_clcb->bta_conn_id);
1658             bta_gattc_reset_discover_st(p_clcb->p_srcb, p_clcb->status);
1659
1660         }
1661     }
1662 }
1663 /*******************************************************************************
1664 **
1665 ** Function         bta_gattc_ci_load
1666 **
1667 ** Description      cache loading received.
1668 **
1669 ** Returns          None.
1670 **
1671 *******************************************************************************/
1672 void bta_gattc_ci_load(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1673 {
1674
1675     APPL_TRACE_DEBUG("bta_gattc_ci_load conn_id=%d load status=%d",
1676                       p_clcb->bta_conn_id, p_data->ci_load.status);
1677
1678     if (p_data->ci_load.status == BTA_GATT_OK ||
1679          p_data->ci_load.status == BTA_GATT_MORE)
1680     {
1681         if (p_data->ci_load.num_attr != 0)
1682             bta_gattc_rebuild_cache(p_clcb->p_srcb, p_data->ci_load.num_attr,
1683                                 p_data->ci_load.attr, p_clcb->p_srcb->attr_index);
1684
1685         if (p_data->ci_load.status == BTA_GATT_OK)
1686         {
1687             p_clcb->p_srcb->attr_index = 0;
1688             bta_gattc_reset_discover_st(p_clcb->p_srcb, BTA_GATT_OK);
1689             bta_gattc_co_cache_close(p_clcb->p_srcb->server_bda, 0);
1690         }
1691         else /* load more */
1692         {
1693             p_clcb->p_srcb->attr_index += p_data->ci_load.num_attr;
1694
1695             bta_gattc_co_cache_load(p_clcb->p_srcb->server_bda,
1696                                     BTA_GATTC_CI_CACHE_LOAD_EVT,
1697                                     p_clcb->p_srcb->attr_index,
1698                                     p_clcb->bta_conn_id);
1699         }
1700     }
1701     else
1702     {
1703         bta_gattc_co_cache_close(p_clcb->p_srcb->server_bda, 0);
1704         p_clcb->p_srcb->state = BTA_GATTC_SERV_DISC;
1705         p_clcb->p_srcb->attr_index = 0;
1706         /* cache load failure, start discovery */
1707         bta_gattc_start_discover(p_clcb, NULL);
1708     }
1709 }
1710 /*******************************************************************************
1711 **
1712 ** Function         bta_gattc_ci_save
1713 **
1714 ** Description      cache loading received.
1715 **
1716 ** Returns          None.
1717 **
1718 *******************************************************************************/
1719 void bta_gattc_ci_save(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1720 {
1721     UNUSED(p_data);
1722
1723     APPL_TRACE_DEBUG("bta_gattc_ci_save conn_id=%d  " ,
1724                       p_clcb->bta_conn_id   );
1725
1726     if (!bta_gattc_cache_save(p_clcb->p_srcb, p_clcb->bta_conn_id))
1727     {
1728         p_clcb->p_srcb->attr_index = 0;
1729         bta_gattc_co_cache_close(p_clcb->p_srcb->server_bda, 0);
1730         bta_gattc_reset_discover_st(p_clcb->p_srcb, p_clcb->status);
1731     }
1732 }
1733 /*******************************************************************************
1734 **
1735 ** Function         bta_gattc_fail
1736 **
1737 ** Description      report API call failure back to apps
1738 **
1739 ** Returns          None.
1740 **
1741 *******************************************************************************/
1742 void bta_gattc_fail(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1743 {
1744     UNUSED(p_data);
1745
1746     if (p_clcb->status == BTA_GATT_OK)
1747     {
1748         APPL_TRACE_ERROR("operation not supported at current state [%d]", p_clcb->state);
1749     }
1750 }
1751
1752 /*******************************************************************************
1753 **
1754 ** Function         bta_gattc_deregister_cmpl
1755 **
1756 ** Description      De-Register a GATT client application with BTA completed.
1757 **
1758 ** Returns          void
1759 **
1760 *******************************************************************************/
1761 static void bta_gattc_deregister_cmpl(tBTA_GATTC_RCB *p_clreg)
1762 {
1763     tBTA_GATTC_CB       *p_cb = &bta_gattc_cb;
1764     tBTA_GATTC_IF       client_if = p_clreg->client_if;
1765     tBTA_GATTC          cb_data;
1766     tBTA_GATTC_CBACK    *p_cback = p_clreg->p_cback;
1767
1768     memset(&cb_data, 0, sizeof(tBTA_GATTC));
1769
1770     GATT_Deregister(p_clreg->client_if);
1771     memset(p_clreg, 0, sizeof(tBTA_GATTC_RCB));
1772
1773     cb_data.reg_oper.client_if = client_if;
1774     cb_data.reg_oper.status    = BTA_GATT_OK;
1775
1776     if (p_cback)
1777         /* callback with de-register event */
1778         (*p_cback)(BTA_GATTC_DEREG_EVT,  (tBTA_GATTC *)&cb_data);
1779
1780     if (bta_gattc_num_reg_app() == 0 && p_cb->state == BTA_GATTC_STATE_DISABLING)
1781     {
1782         p_cb->state = BTA_GATTC_STATE_DISABLED;
1783     }
1784 }
1785 /*******************************************************************************
1786 **
1787 ** Function         bta_gattc_conn_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 *) osi_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 *) osi_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 (! fixed_queue_is_empty(p_srvc_cb->cache_buffer))
1915                 osi_freebuf(fixed_queue_try_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                                        tGATT_VALUE *att_value)
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         if (att_value->len != BTA_GATTC_SERVICE_CHANGED_LEN) {
1954             APPL_TRACE_ERROR("%s: received malformed service changed indication, skipping", __func__);
1955             return FALSE;
1956         }
1957
1958         UINT8 *p = att_value->value;
1959         UINT16 s_handle = ((UINT16)(*(p    )) + (((UINT16)(*(p + 1))) << 8));
1960         UINT16 e_handle = ((UINT16)(*(p + 2)) + (((UINT16)(*(p + 3))) << 8));
1961
1962         APPL_TRACE_ERROR("%s: service changed s_handle:0x%04x e_handle:0x%04x",
1963                          __func__, s_handle, e_handle);
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(p_srcb, conn_id, s_handle, e_handle);
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, att_value->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, &p_data->att_value))
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     APPL_TRACE_DEBUG("bta_gattc_cmpl_cback: conn_id = %d op = %d status = %d",
2140                       conn_id, op, status);
2141
2142     /* notification and indication processed right away */
2143     if (op == GATTC_OPTYPE_NOTIFICATION || op == GATTC_OPTYPE_INDICATION)
2144     {
2145         bta_gattc_process_indicate(conn_id, op, p_data);
2146         return;
2147     }
2148     /* for all other operation, not expected if w/o connection */
2149     else if ((p_clcb = bta_gattc_find_clcb_by_conn_id(conn_id)) == NULL)
2150     {
2151         APPL_TRACE_ERROR("bta_gattc_cmpl_cback unknown conn_id =  %d, ignore data", conn_id);
2152         return;
2153     }
2154
2155     /* if over BR_EDR, inform PM for mode change */
2156     if (p_clcb->transport == BTA_TRANSPORT_BR_EDR)
2157     {
2158         bta_sys_busy(BTA_ID_GATTC, BTA_ALL_APP_ID, p_clcb->bda);
2159         bta_sys_idle(BTA_ID_GATTC, BTA_ALL_APP_ID, p_clcb->bda);
2160     }
2161
2162     bta_gattc_cmpl_sendmsg(conn_id, op, status, p_data);
2163 }
2164
2165 /*******************************************************************************
2166 **
2167 ** Function         bta_gattc_cmpl_sendmsg
2168 **
2169 ** Description      client operation complete send message
2170 **
2171 ** Returns          None.
2172 **
2173 *******************************************************************************/
2174 static void bta_gattc_cmpl_sendmsg(UINT16 conn_id, tGATTC_OPTYPE op,
2175                                    tBTA_GATT_STATUS status,
2176                                    tGATT_CL_COMPLETE *p_data)
2177 {
2178     const UINT16         len = sizeof(tBTA_GATTC_OP_CMPL) + sizeof(tGATT_CL_COMPLETE);
2179     tBTA_GATTC_OP_CMPL  *p_buf = (tBTA_GATTC_OP_CMPL *) osi_getbuf(len);
2180
2181     if (p_buf != NULL)
2182     {
2183         memset(p_buf, 0, len);
2184         p_buf->hdr.event = BTA_GATTC_OP_CMPL_EVT;
2185         p_buf->hdr.layer_specific = conn_id;
2186         p_buf->status = status;
2187         p_buf->op_code = op;
2188
2189         if (p_data != NULL)
2190         {
2191             p_buf->p_cmpl = (tGATT_CL_COMPLETE *)(p_buf + 1);
2192             memcpy(p_buf->p_cmpl, p_data, sizeof(tGATT_CL_COMPLETE));
2193         }
2194
2195         bta_sys_sendmsg(p_buf);
2196     }
2197 }
2198
2199 /*******************************************************************************
2200 **
2201 ** Function         bta_gattc_cong_cback
2202 **
2203 ** Description      congestion callback for BTA GATT client.
2204 **
2205 ** Returns          void
2206 **
2207 ********************************************************************************/
2208 static void bta_gattc_cong_cback (UINT16 conn_id, BOOLEAN congested)
2209 {
2210     tBTA_GATTC_CLCB *p_clcb;
2211     tBTA_GATTC cb_data;
2212
2213     if ((p_clcb = bta_gattc_find_clcb_by_conn_id(conn_id)) != NULL)
2214     {
2215         if (p_clcb->p_rcb->p_cback)
2216         {
2217             cb_data.congest.conn_id = conn_id;
2218             cb_data.congest.congested = congested;
2219
2220             (*p_clcb->p_rcb->p_cback)(BTA_GATTC_CONGEST_EVT, &cb_data);
2221         }
2222     }
2223 }
2224
2225 #if BLE_INCLUDED == TRUE
2226 /*******************************************************************************
2227 **
2228 ** Function         bta_gattc_init_clcb_conn
2229 **
2230 ** Description      Initaite a BTA CLCB connection
2231 **
2232 ** Returns          void
2233 **
2234 ********************************************************************************/
2235 void bta_gattc_init_clcb_conn(UINT8 cif, BD_ADDR remote_bda)
2236 {
2237     tBTA_GATTC_CLCB     *p_clcb = NULL;
2238     tBTA_GATTC_DATA     gattc_data;
2239     UINT16              conn_id;
2240
2241     /* should always get the connection ID */
2242     if (GATT_GetConnIdIfConnected(cif, remote_bda, &conn_id, BTA_GATT_TRANSPORT_LE) == FALSE)
2243     {
2244         APPL_TRACE_ERROR("bta_gattc_init_clcb_conn ERROR: not a connected device");
2245         return;
2246     }
2247
2248     /* initaite a new connection here */
2249     if ((p_clcb = bta_gattc_clcb_alloc(cif, remote_bda, BTA_GATT_TRANSPORT_LE)) != NULL)
2250     {
2251         gattc_data.hdr.layer_specific = p_clcb->bta_conn_id = conn_id;
2252
2253         gattc_data.api_conn.client_if = cif;
2254         memcpy(gattc_data.api_conn.remote_bda, remote_bda, BD_ADDR_LEN);
2255         gattc_data.api_conn.is_direct = TRUE;
2256
2257         bta_gattc_sm_execute(p_clcb, BTA_GATTC_API_OPEN_EVT, &gattc_data);
2258     }
2259     else
2260     {
2261         APPL_TRACE_ERROR("No resources");
2262     }
2263 }
2264 /*******************************************************************************
2265 **
2266 ** Function         bta_gattc_process_listen_all
2267 **
2268 ** Description      process listen all, send open callback to application for all
2269 **                  connected slave LE link.
2270 **
2271 ** Returns          void
2272 **
2273 ********************************************************************************/
2274 void bta_gattc_process_listen_all(UINT8 cif)
2275 {
2276     UINT8               i_conn = 0;
2277     tBTA_GATTC_CONN     *p_conn = &bta_gattc_cb.conn_track[0];
2278
2279     for (i_conn = 0; i_conn < BTA_GATTC_CONN_MAX; i_conn++, p_conn ++)
2280     {
2281         if (p_conn->in_use )
2282         {
2283             if (bta_gattc_find_clcb_by_cif(cif, p_conn->remote_bda, BTA_GATT_TRANSPORT_LE) == NULL)
2284             {
2285                 bta_gattc_init_clcb_conn(cif, p_conn->remote_bda);
2286             }
2287             /* else already connected */
2288         }
2289     }
2290 }
2291 /*******************************************************************************
2292 **
2293 ** Function         bta_gattc_listen
2294 **
2295 ** Description      Start or stop a listen for connection
2296 **
2297 ** Returns          void
2298 **
2299 ********************************************************************************/
2300 void bta_gattc_listen(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA * p_msg)
2301 {
2302     tBTA_GATTC_RCB      *p_clreg = bta_gattc_cl_get_regcb(p_msg->api_listen.client_if);
2303     tBTA_GATTC          cb_data;
2304     UNUSED(p_cb);
2305
2306     cb_data.reg_oper.status = BTA_GATT_ERROR;
2307     cb_data.reg_oper.client_if = p_msg->api_listen.client_if;
2308
2309     if (p_clreg == NULL)
2310     {
2311         APPL_TRACE_ERROR("bta_gattc_listen failed, unknown client_if: %d",
2312                             p_msg->api_listen.client_if);
2313         return;
2314     }
2315     /* mark bg conn record */
2316     if (bta_gattc_mark_bg_conn(p_msg->api_listen.client_if,
2317                                (BD_ADDR_PTR) p_msg->api_listen.remote_bda,
2318                                p_msg->api_listen.start,
2319                                TRUE))
2320     {
2321         if (!GATT_Listen(p_msg->api_listen.client_if,
2322                          p_msg->api_listen.start,
2323                          p_msg->api_listen.remote_bda))
2324         {
2325             APPL_TRACE_ERROR("Listen failure");
2326             (*p_clreg->p_cback)(BTA_GATTC_LISTEN_EVT, &cb_data);
2327         }
2328         else
2329         {
2330             cb_data.status = BTA_GATT_OK;
2331
2332             (*p_clreg->p_cback)(BTA_GATTC_LISTEN_EVT, &cb_data);
2333
2334             if (p_msg->api_listen.start)
2335             {
2336                 /* if listen to a specific target */
2337                 if (p_msg->api_listen.remote_bda != NULL)
2338                 {
2339
2340                     /* if is a connected remote device */
2341                     if (L2CA_GetBleConnRole(p_msg->api_listen.remote_bda) == HCI_ROLE_SLAVE &&
2342                         bta_gattc_find_clcb_by_cif(p_msg->api_listen.client_if,
2343                                                    p_msg->api_listen.remote_bda,
2344                                                    BTA_GATT_TRANSPORT_LE) == NULL)
2345                     {
2346
2347                         bta_gattc_init_clcb_conn(p_msg->api_listen.client_if,
2348                                                 p_msg->api_listen.remote_bda);
2349                     }
2350                 }
2351                 /* if listen to all */
2352                 else
2353                 {
2354                     LOG_DEBUG(LOG_TAG, "Listen For All now");
2355                     /* go through all connected device and send
2356                     callback for all connected slave connection */
2357                     bta_gattc_process_listen_all(p_msg->api_listen.client_if);
2358                 }
2359             }
2360         }
2361     }
2362 }
2363
2364 /*******************************************************************************
2365 **
2366 ** Function         bta_gattc_broadcast
2367 **
2368 ** Description      Start or stop broadcasting
2369 **
2370 ** Returns          void
2371 **
2372 ********************************************************************************/
2373 void bta_gattc_broadcast(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA * p_msg)
2374 {
2375     tBTA_GATTC_RCB      *p_clreg = bta_gattc_cl_get_regcb(p_msg->api_listen.client_if);
2376     tBTA_GATTC          cb_data;
2377     UNUSED(p_cb);
2378
2379     cb_data.reg_oper.client_if = p_msg->api_listen.client_if;
2380     cb_data.reg_oper.status = BTM_BleBroadcast(p_msg->api_listen.start);
2381
2382     if (p_clreg && p_clreg->p_cback)
2383         (*p_clreg->p_cback)(BTA_GATTC_LISTEN_EVT, &cb_data);
2384 }
2385 #endif
2386 #endif