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