OSDN Git Service

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