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