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