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