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 Server action functions for the state
     22  *  machine.
     23  *
     24  ******************************************************************************/
     25 
     26 #include "bt_target.h"
     27 
     28 #include <string.h>
     29 #include "bt_common.h"
     30 #include "bta_gatts_co.h"
     31 #include "bta_gatts_int.h"
     32 #include "bta_sys.h"
     33 #include "btif/include/btif_debug_conn.h"
     34 #include "btm_ble_api.h"
     35 #include "osi/include/osi.h"
     36 #include "utl.h"
     37 
     38 static void bta_gatts_nv_save_cback(bool is_saved,
     39                                     tGATTS_HNDL_RANGE* p_hndl_range);
     40 static bool bta_gatts_nv_srv_chg_cback(tGATTS_SRV_CHG_CMD cmd,
     41                                        tGATTS_SRV_CHG_REQ* p_req,
     42                                        tGATTS_SRV_CHG_RSP* p_rsp);
     43 
     44 static void bta_gatts_conn_cback(tGATT_IF gatt_if, BD_ADDR bda,
     45                                  uint16_t conn_id, bool connected,
     46                                  tGATT_DISCONN_REASON reason,
     47                                  tGATT_TRANSPORT transport);
     48 static void bta_gatts_send_request_cback(uint16_t conn_id, uint32_t trans_id,
     49                                          tGATTS_REQ_TYPE req_type,
     50                                          tGATTS_DATA* p_data);
     51 static void bta_gatts_cong_cback(uint16_t conn_id, bool congested);
     52 static void bta_gatts_phy_update_cback(tGATT_IF gatt_if, uint16_t conn_id,
     53                                        uint8_t tx_phy, uint8_t rx_phy,
     54                                        uint8_t status);
     55 static void bta_gatts_conn_update_cback(tGATT_IF gatt_if, uint16_t conn_id,
     56                                         uint16_t interval, uint16_t latency,
     57                                         uint16_t timeout, uint8_t status);
     58 
     59 static tGATT_CBACK bta_gatts_cback = {bta_gatts_conn_cback,
     60                                       NULL,
     61                                       NULL,
     62                                       NULL,
     63                                       bta_gatts_send_request_cback,
     64                                       NULL,
     65                                       bta_gatts_cong_cback,
     66                                       bta_gatts_phy_update_cback,
     67                                       bta_gatts_conn_update_cback};
     68 
     69 tGATT_APPL_INFO bta_gatts_nv_cback = {bta_gatts_nv_save_cback,
     70                                       bta_gatts_nv_srv_chg_cback};
     71 
     72 /*******************************************************************************
     73  *
     74  * Function         bta_gatts_nv_save_cback
     75  *
     76  * Description      NV save callback function.
     77  *
     78  * Parameter        is_add: true is to add a handle range; otherwise is to
     79  *                          delete.
     80  * Returns          none.
     81  *
     82  ******************************************************************************/
     83 static void bta_gatts_nv_save_cback(bool is_add,
     84                                     tGATTS_HNDL_RANGE* p_hndl_range) {
     85   bta_gatts_co_update_handle_range(is_add,
     86                                    (tBTA_GATTS_HNDL_RANGE*)p_hndl_range);
     87 }
     88 
     89 /*******************************************************************************
     90  *
     91  * Function         bta_gatts_nv_srv_chg_cback
     92  *
     93  * Description      NV save callback function.
     94  *
     95  * Parameter        is_add: true is to add a handle range; otherwise is to
     96  *                          delete.
     97  * Returns          none.
     98  *
     99  ******************************************************************************/
    100 static bool bta_gatts_nv_srv_chg_cback(tGATTS_SRV_CHG_CMD cmd,
    101                                        tGATTS_SRV_CHG_REQ* p_req,
    102                                        tGATTS_SRV_CHG_RSP* p_rsp) {
    103   return bta_gatts_co_srv_chg((tBTA_GATTS_SRV_CHG_CMD)cmd,
    104                               (tBTA_GATTS_SRV_CHG_REQ*)p_req,
    105                               (tBTA_GATTS_SRV_CHG_RSP*)p_rsp);
    106 }
    107 
    108 /*******************************************************************************
    109  *
    110  * Function         bta_gatts_enable
    111  *
    112  * Description      enable BTA GATTS module.
    113  *
    114  * Returns          none.
    115  *
    116  ******************************************************************************/
    117 void bta_gatts_enable(tBTA_GATTS_CB* p_cb) {
    118   uint8_t index = 0;
    119   tBTA_GATTS_HNDL_RANGE handle_range;
    120 
    121   if (p_cb->enabled) {
    122     APPL_TRACE_DEBUG("GATTS already enabled.");
    123   } else {
    124     memset(p_cb, 0, sizeof(tBTA_GATTS_CB));
    125 
    126     p_cb->enabled = true;
    127 
    128     while (bta_gatts_co_load_handle_range(index, &handle_range)) {
    129       GATTS_AddHandleRange((tGATTS_HNDL_RANGE*)&handle_range);
    130       memset(&handle_range, 0, sizeof(tGATTS_HNDL_RANGE));
    131       index++;
    132     }
    133 
    134     APPL_TRACE_DEBUG("bta_gatts_enable: num of handle range added=%d", index);
    135 
    136     if (!GATTS_NVRegister(&bta_gatts_nv_cback)) {
    137       APPL_TRACE_ERROR("BTA GATTS NV register failed.");
    138     }
    139   }
    140 }
    141 
    142 /*******************************************************************************
    143  *
    144  * Function         bta_gatts_api_disable
    145  *
    146  * Description      disable BTA GATTS module.
    147  *
    148  * Returns          none.
    149  *
    150  ******************************************************************************/
    151 void bta_gatts_api_disable(tBTA_GATTS_CB* p_cb) {
    152   uint8_t i;
    153 
    154   if (p_cb->enabled) {
    155     for (i = 0; i < BTA_GATTS_MAX_APP_NUM; i++) {
    156       if (p_cb->rcb[i].in_use) {
    157         GATT_Deregister(p_cb->rcb[i].gatt_if);
    158       }
    159     }
    160     memset(p_cb, 0, sizeof(tBTA_GATTS_CB));
    161   } else {
    162     APPL_TRACE_ERROR("GATTS not enabled");
    163   }
    164 }
    165 
    166 /*******************************************************************************
    167  *
    168  * Function         bta_gatts_register
    169  *
    170  * Description      register an application.
    171  *
    172  * Returns          none.
    173  *
    174  ******************************************************************************/
    175 void bta_gatts_register(tBTA_GATTS_CB* p_cb, tBTA_GATTS_DATA* p_msg) {
    176   tBTA_GATTS cb_data;
    177   tBTA_GATT_STATUS status = BTA_GATT_OK;
    178   uint8_t i, first_unuse = 0xff;
    179 
    180   if (p_cb->enabled == false) {
    181     bta_gatts_enable(p_cb);
    182   }
    183 
    184   for (i = 0; i < BTA_GATTS_MAX_APP_NUM; i++) {
    185     if (p_cb->rcb[i].in_use) {
    186       if (bta_gatts_uuid_compare(p_cb->rcb[i].app_uuid,
    187                                  p_msg->api_reg.app_uuid)) {
    188         APPL_TRACE_ERROR("application already registered.");
    189         status = BTA_GATT_DUP_REG;
    190         break;
    191       }
    192     }
    193   }
    194 
    195   if (status == BTA_GATT_OK) {
    196     for (i = 0; i < BTA_GATTS_MAX_APP_NUM; i++) {
    197       if (first_unuse == 0xff && !p_cb->rcb[i].in_use) {
    198         first_unuse = i;
    199         break;
    200       }
    201     }
    202 
    203     cb_data.reg_oper.server_if = BTA_GATTS_INVALID_IF;
    204     memcpy(&cb_data.reg_oper.uuid, &p_msg->api_reg.app_uuid, sizeof(tBT_UUID));
    205     if (first_unuse != 0xff) {
    206       APPL_TRACE_ERROR("register application first_unuse rcb_idx = %d",
    207                        first_unuse);
    208 
    209       p_cb->rcb[first_unuse].in_use = true;
    210       p_cb->rcb[first_unuse].p_cback = p_msg->api_reg.p_cback;
    211       memcpy(&p_cb->rcb[first_unuse].app_uuid, &p_msg->api_reg.app_uuid,
    212              sizeof(tBT_UUID));
    213       cb_data.reg_oper.server_if = p_cb->rcb[first_unuse].gatt_if =
    214           GATT_Register(&p_msg->api_reg.app_uuid, &bta_gatts_cback);
    215       if (!p_cb->rcb[first_unuse].gatt_if) {
    216         status = BTA_GATT_NO_RESOURCES;
    217       } else {
    218         tBTA_GATTS_INT_START_IF* p_buf = (tBTA_GATTS_INT_START_IF*)osi_malloc(
    219             sizeof(tBTA_GATTS_INT_START_IF));
    220         p_buf->hdr.event = BTA_GATTS_INT_START_IF_EVT;
    221         p_buf->server_if = p_cb->rcb[first_unuse].gatt_if;
    222 
    223         bta_sys_sendmsg(p_buf);
    224       }
    225     } else {
    226       status = BTA_GATT_NO_RESOURCES;
    227     }
    228   }
    229   cb_data.reg_oper.status = status;
    230   if (p_msg->api_reg.p_cback)
    231     (*p_msg->api_reg.p_cback)(BTA_GATTS_REG_EVT, &cb_data);
    232 }
    233 
    234 /*******************************************************************************
    235  *
    236  * Function         bta_gatts_start_if
    237  *
    238  * Description      start an application interface.
    239  *
    240  * Returns          none.
    241  *
    242  ******************************************************************************/
    243 void bta_gatts_start_if(UNUSED_ATTR tBTA_GATTS_CB* p_cb,
    244                         tBTA_GATTS_DATA* p_msg) {
    245   if (bta_gatts_find_app_rcb_by_app_if(p_msg->int_start_if.server_if)) {
    246     GATT_StartIf(p_msg->int_start_if.server_if);
    247   } else {
    248     APPL_TRACE_ERROR("Unable to start app.: Unknown interface =%d",
    249                      p_msg->int_start_if.server_if);
    250   }
    251 }
    252 /*******************************************************************************
    253  *
    254  * Function         bta_gatts_deregister
    255  *
    256  * Description      deregister an application.
    257  *
    258  * Returns          none.
    259  *
    260  ******************************************************************************/
    261 void bta_gatts_deregister(tBTA_GATTS_CB* p_cb, tBTA_GATTS_DATA* p_msg) {
    262   tBTA_GATT_STATUS status = BTA_GATT_ERROR;
    263   tBTA_GATTS_CBACK* p_cback = NULL;
    264   uint8_t i;
    265   tBTA_GATTS cb_data;
    266 
    267   cb_data.reg_oper.server_if = p_msg->api_dereg.server_if;
    268   cb_data.reg_oper.status = status;
    269 
    270   for (i = 0; i < BTA_GATTS_MAX_APP_NUM; i++) {
    271     if (p_cb->rcb[i].in_use &&
    272         p_cb->rcb[i].gatt_if == p_msg->api_dereg.server_if) {
    273       p_cback = p_cb->rcb[i].p_cback;
    274       status = BTA_GATT_OK;
    275 
    276       /* deregister the app */
    277       GATT_Deregister(p_cb->rcb[i].gatt_if);
    278 
    279       /* reset cb */
    280       memset(&p_cb->rcb[i], 0, sizeof(tBTA_GATTS_RCB));
    281       cb_data.reg_oper.status = status;
    282       break;
    283     }
    284   }
    285 
    286   if (p_cback) {
    287     (*p_cback)(BTA_GATTS_DEREG_EVT, &cb_data);
    288   } else {
    289     APPL_TRACE_ERROR("application not registered.");
    290   }
    291 }
    292 
    293 /*******************************************************************************
    294  *
    295  * Function         bta_gatts_delete_service
    296  *
    297  * Description      action function to delete a service.
    298  *
    299  * Returns          none.
    300  *
    301  ******************************************************************************/
    302 void bta_gatts_delete_service(tBTA_GATTS_SRVC_CB* p_srvc_cb,
    303                               tBTA_GATTS_DATA* p_msg) {
    304   tBTA_GATTS_RCB* p_rcb = &bta_gatts_cb.rcb[p_srvc_cb->rcb_idx];
    305   tBTA_GATTS cb_data;
    306 
    307   cb_data.srvc_oper.server_if = p_rcb->gatt_if;
    308   // cb_data.srvc_oper.service_id = p_msg->api_add_incl_srvc.hdr.layer_specific;
    309 
    310   if (GATTS_DeleteService(p_rcb->gatt_if, &p_srvc_cb->service_uuid,
    311                           p_srvc_cb->service_id)) {
    312     cb_data.srvc_oper.status = BTA_GATT_OK;
    313     memset(p_srvc_cb, 0, sizeof(tBTA_GATTS_SRVC_CB));
    314   } else {
    315     cb_data.srvc_oper.status = BTA_GATT_ERROR;
    316   }
    317 
    318   if (p_rcb->p_cback) (*p_rcb->p_cback)(BTA_GATTS_DELELTE_EVT, &cb_data);
    319 }
    320 
    321 /*******************************************************************************
    322  *
    323  * Function         bta_gatts_stop_service
    324  *
    325  * Description      action function to stop a service.
    326  *
    327  * Returns          none.
    328  *
    329  ******************************************************************************/
    330 void bta_gatts_stop_service(tBTA_GATTS_SRVC_CB* p_srvc_cb,
    331                             UNUSED_ATTR tBTA_GATTS_DATA* p_msg) {
    332   tBTA_GATTS_RCB* p_rcb = &bta_gatts_cb.rcb[p_srvc_cb->rcb_idx];
    333   tBTA_GATTS cb_data;
    334 
    335   GATTS_StopService(p_srvc_cb->service_id);
    336   cb_data.srvc_oper.server_if = p_rcb->gatt_if;
    337   cb_data.srvc_oper.service_id = p_srvc_cb->service_id;
    338   cb_data.srvc_oper.status = BTA_GATT_OK;
    339   APPL_TRACE_ERROR("bta_gatts_stop_service service_id= %d",
    340                    p_srvc_cb->service_id);
    341 
    342   if (p_rcb->p_cback) (*p_rcb->p_cback)(BTA_GATTS_STOP_EVT, &cb_data);
    343 }
    344 /*******************************************************************************
    345  *
    346  * Function         bta_gatts_send_rsp
    347  *
    348  * Description      GATTS send response.
    349  *
    350  * Returns          none.
    351  *
    352  ******************************************************************************/
    353 void bta_gatts_send_rsp(UNUSED_ATTR tBTA_GATTS_CB* p_cb,
    354                         tBTA_GATTS_DATA* p_msg) {
    355   if (GATTS_SendRsp(p_msg->api_rsp.hdr.layer_specific, p_msg->api_rsp.trans_id,
    356                     p_msg->api_rsp.status,
    357                     (tGATTS_RSP*)p_msg->api_rsp.p_rsp) != GATT_SUCCESS) {
    358     APPL_TRACE_ERROR("Sending response failed");
    359   }
    360 }
    361 /*******************************************************************************
    362  *
    363  * Function         bta_gatts_indicate_handle
    364  *
    365  * Description      GATTS send handle value indication or notification.
    366  *
    367  * Returns          none.
    368  *
    369  ******************************************************************************/
    370 void bta_gatts_indicate_handle(tBTA_GATTS_CB* p_cb, tBTA_GATTS_DATA* p_msg) {
    371   tBTA_GATTS_SRVC_CB* p_srvc_cb;
    372   tBTA_GATTS_RCB* p_rcb = NULL;
    373   tBTA_GATT_STATUS status = BTA_GATT_ILLEGAL_PARAMETER;
    374   tGATT_IF gatt_if;
    375   BD_ADDR remote_bda;
    376   tBTA_TRANSPORT transport;
    377   tBTA_GATTS cb_data;
    378 
    379   p_srvc_cb =
    380       bta_gatts_find_srvc_cb_by_attr_id(p_cb, p_msg->api_indicate.attr_id);
    381 
    382   if (p_srvc_cb) {
    383     if (GATT_GetConnectionInfor(p_msg->api_indicate.hdr.layer_specific,
    384                                 &gatt_if, remote_bda, &transport)) {
    385       p_rcb = bta_gatts_find_app_rcb_by_app_if(gatt_if);
    386 
    387       if (p_msg->api_indicate.need_confirm)
    388 
    389         status = GATTS_HandleValueIndication(
    390             p_msg->api_indicate.hdr.layer_specific, p_msg->api_indicate.attr_id,
    391             p_msg->api_indicate.len, p_msg->api_indicate.value);
    392       else
    393         status = GATTS_HandleValueNotification(
    394             p_msg->api_indicate.hdr.layer_specific, p_msg->api_indicate.attr_id,
    395             p_msg->api_indicate.len, p_msg->api_indicate.value);
    396 
    397       /* if over BR_EDR, inform PM for mode change */
    398       if (transport == BTA_TRANSPORT_BR_EDR) {
    399         bta_sys_busy(BTA_ID_GATTS, BTA_ALL_APP_ID, remote_bda);
    400         bta_sys_idle(BTA_ID_GATTS, BTA_ALL_APP_ID, remote_bda);
    401       }
    402     } else {
    403       APPL_TRACE_ERROR("Unknown connection ID: %d fail sending notification",
    404                        p_msg->api_indicate.hdr.layer_specific);
    405     }
    406 
    407     if ((status != GATT_SUCCESS || !p_msg->api_indicate.need_confirm) &&
    408         p_rcb && p_cb->rcb[p_srvc_cb->rcb_idx].p_cback) {
    409       cb_data.req_data.status = status;
    410       cb_data.req_data.conn_id = p_msg->api_indicate.hdr.layer_specific;
    411 
    412       (*p_rcb->p_cback)(BTA_GATTS_CONF_EVT, &cb_data);
    413     }
    414   } else {
    415     APPL_TRACE_ERROR("Not an registered servce attribute ID: 0x%04x",
    416                      p_msg->api_indicate.attr_id);
    417   }
    418 }
    419 
    420 /*******************************************************************************
    421  *
    422  * Function         bta_gatts_open
    423  *
    424  * Description
    425  *
    426  * Returns          none.
    427  *
    428  ******************************************************************************/
    429 void bta_gatts_open(UNUSED_ATTR tBTA_GATTS_CB* p_cb, tBTA_GATTS_DATA* p_msg) {
    430   tBTA_GATTS_RCB* p_rcb = NULL;
    431   tBTA_GATT_STATUS status = BTA_GATT_ERROR;
    432   uint16_t conn_id;
    433 
    434   p_rcb = bta_gatts_find_app_rcb_by_app_if(p_msg->api_open.server_if);
    435   if (p_rcb != NULL) {
    436     /* should always get the connection ID */
    437     if (GATT_Connect(p_rcb->gatt_if, p_msg->api_open.remote_bda,
    438                      p_msg->api_open.is_direct, p_msg->api_open.transport,
    439                      false)) {
    440       status = BTA_GATT_OK;
    441 
    442       if (GATT_GetConnIdIfConnected(p_rcb->gatt_if, p_msg->api_open.remote_bda,
    443                                     &conn_id, p_msg->api_open.transport)) {
    444         status = BTA_GATT_ALREADY_OPEN;
    445       }
    446     }
    447   } else {
    448     APPL_TRACE_ERROR("Inavlide server_if=%d", p_msg->api_open.server_if);
    449   }
    450 
    451   if (p_rcb && p_rcb->p_cback)
    452     (*p_rcb->p_cback)(BTA_GATTS_OPEN_EVT, (tBTA_GATTS*)&status);
    453 }
    454 /*******************************************************************************
    455  *
    456  * Function         bta_gatts_cancel_open
    457  *
    458  * Description
    459  *
    460  * Returns          none.
    461  *
    462  ******************************************************************************/
    463 void bta_gatts_cancel_open(UNUSED_ATTR tBTA_GATTS_CB* p_cb,
    464                            tBTA_GATTS_DATA* p_msg) {
    465   tBTA_GATTS_RCB* p_rcb;
    466   tBTA_GATT_STATUS status = BTA_GATT_ERROR;
    467 
    468   p_rcb = bta_gatts_find_app_rcb_by_app_if(p_msg->api_cancel_open.server_if);
    469   if (p_rcb != NULL) {
    470     if (!GATT_CancelConnect(p_rcb->gatt_if, p_msg->api_cancel_open.remote_bda,
    471                             p_msg->api_cancel_open.is_direct)) {
    472       APPL_TRACE_ERROR("bta_gatts_cancel_open failed for open request");
    473     } else {
    474       status = BTA_GATT_OK;
    475     }
    476   } else {
    477     APPL_TRACE_ERROR("Inavlide server_if=%d", p_msg->api_cancel_open.server_if);
    478   }
    479 
    480   if (p_rcb && p_rcb->p_cback)
    481     (*p_rcb->p_cback)(BTA_GATTS_CANCEL_OPEN_EVT, (tBTA_GATTS*)&status);
    482 }
    483 /*******************************************************************************
    484  *
    485  * Function         bta_gatts_close
    486  *
    487  * Description
    488  *
    489  * Returns          none.
    490  *
    491  ******************************************************************************/
    492 void bta_gatts_close(UNUSED_ATTR tBTA_GATTS_CB* p_cb, tBTA_GATTS_DATA* p_msg) {
    493   tBTA_GATTS_RCB* p_rcb;
    494   tBTA_GATT_STATUS status = BTA_GATT_ERROR;
    495   tGATT_IF gatt_if;
    496   BD_ADDR remote_bda;
    497   tBTA_GATT_TRANSPORT transport;
    498 
    499   if (GATT_GetConnectionInfor(p_msg->hdr.layer_specific, &gatt_if, remote_bda,
    500                               &transport)) {
    501     if (GATT_Disconnect(p_msg->hdr.layer_specific) != GATT_SUCCESS) {
    502       APPL_TRACE_ERROR("bta_gatts_close fail conn_id=%d",
    503                        p_msg->hdr.layer_specific);
    504     } else {
    505       status = BTA_GATT_OK;
    506     }
    507 
    508     p_rcb = bta_gatts_find_app_rcb_by_app_if(gatt_if);
    509 
    510     if (p_rcb && p_rcb->p_cback) {
    511       if (transport == BTA_TRANSPORT_BR_EDR)
    512         bta_sys_conn_close(BTA_ID_GATTS, BTA_ALL_APP_ID, remote_bda);
    513 
    514       (*p_rcb->p_cback)(BTA_GATTS_CLOSE_EVT, (tBTA_GATTS*)&status);
    515     }
    516   } else {
    517     APPL_TRACE_ERROR("Unknown connection ID: %d", p_msg->hdr.layer_specific);
    518   }
    519 }
    520 
    521 /*******************************************************************************
    522  *
    523  * Function         bta_gatts_request_cback
    524  *
    525  * Description      GATTS attribute request callback.
    526  *
    527  * Returns          none.
    528  *
    529  ******************************************************************************/
    530 static void bta_gatts_send_request_cback(uint16_t conn_id, uint32_t trans_id,
    531                                          tGATTS_REQ_TYPE req_type,
    532                                          tGATTS_DATA* p_data) {
    533   tBTA_GATTS cb_data;
    534   tBTA_GATTS_RCB* p_rcb;
    535   tGATT_IF gatt_if;
    536   tBTA_GATT_TRANSPORT transport;
    537 
    538   memset(&cb_data, 0, sizeof(tBTA_GATTS));
    539 
    540   if (GATT_GetConnectionInfor(conn_id, &gatt_if, cb_data.req_data.remote_bda,
    541                               &transport)) {
    542     p_rcb = bta_gatts_find_app_rcb_by_app_if(gatt_if);
    543 
    544     APPL_TRACE_DEBUG("%s: conn_id=%d trans_id=%d req_type=%d", __func__,
    545                      conn_id, trans_id, req_type);
    546 
    547     if (p_rcb && p_rcb->p_cback) {
    548       /* if over BR_EDR, inform PM for mode change */
    549       if (transport == BTA_TRANSPORT_BR_EDR) {
    550         bta_sys_busy(BTA_ID_GATTS, BTA_ALL_APP_ID, cb_data.req_data.remote_bda);
    551         bta_sys_idle(BTA_ID_GATTS, BTA_ALL_APP_ID, cb_data.req_data.remote_bda);
    552       }
    553 
    554       cb_data.req_data.conn_id = conn_id;
    555       cb_data.req_data.trans_id = trans_id;
    556       cb_data.req_data.p_data = (tBTA_GATTS_REQ_DATA*)p_data;
    557 
    558       (*p_rcb->p_cback)(req_type, &cb_data);
    559     } else {
    560       APPL_TRACE_ERROR("connection request on gatt_if[%d] is not interested",
    561                        gatt_if);
    562     }
    563   } else {
    564     APPL_TRACE_ERROR("request received on unknown connectino ID: %d", conn_id);
    565   }
    566 }
    567 
    568 /*******************************************************************************
    569  *
    570  * Function         bta_gatts_conn_cback
    571  *
    572  * Description      connection callback.
    573  *
    574  * Returns          none.
    575  *
    576  ******************************************************************************/
    577 static void bta_gatts_conn_cback(tGATT_IF gatt_if, BD_ADDR bda,
    578                                  uint16_t conn_id, bool connected,
    579                                  tGATT_DISCONN_REASON reason,
    580                                  tGATT_TRANSPORT transport) {
    581   tBTA_GATTS cb_data;
    582   uint8_t evt = connected ? BTA_GATTS_CONNECT_EVT : BTA_GATTS_DISCONNECT_EVT;
    583   tBTA_GATTS_RCB* p_reg;
    584 
    585   APPL_TRACE_DEBUG(
    586       "bta_gatts_conn_cback gatt_if=%d conn_id=%d connected=%d reason = 0x%04d",
    587       gatt_if, conn_id, connected, reason);
    588   APPL_TRACE_DEBUG("bta_gatts_conn_cback  bda :%02x-%02x-%02x-%02x-%02x-%02x ",
    589                    bda[0], bda[1], bda[2], bda[3], bda[4], bda[5]);
    590 
    591   bt_bdaddr_t bdaddr;
    592   bdcpy(bdaddr.address, bda);
    593   if (connected)
    594     btif_debug_conn_state(bdaddr, BTIF_DEBUG_CONNECTED, GATT_CONN_UNKNOWN);
    595   else
    596     btif_debug_conn_state(bdaddr, BTIF_DEBUG_DISCONNECTED, reason);
    597 
    598   p_reg = bta_gatts_find_app_rcb_by_app_if(gatt_if);
    599 
    600   if (p_reg && p_reg->p_cback) {
    601     /* there is no RM for GATT */
    602     if (transport == BTA_TRANSPORT_BR_EDR) {
    603       if (connected)
    604         bta_sys_conn_open(BTA_ID_GATTS, BTA_ALL_APP_ID, bda);
    605       else
    606         bta_sys_conn_close(BTA_ID_GATTS, BTA_ALL_APP_ID, bda);
    607     }
    608 
    609     cb_data.conn.conn_id = conn_id;
    610     cb_data.conn.server_if = gatt_if;
    611     cb_data.conn.reason = reason;
    612     cb_data.conn.transport = transport;
    613     memcpy(cb_data.conn.remote_bda, bda, BD_ADDR_LEN);
    614     (*p_reg->p_cback)(evt, &cb_data);
    615   } else {
    616     APPL_TRACE_ERROR("bta_gatts_conn_cback server_if=%d not found", gatt_if);
    617   }
    618 }
    619 
    620 static void bta_gatts_phy_update_cback(tGATT_IF gatt_if, uint16_t conn_id,
    621                                        uint8_t tx_phy, uint8_t rx_phy,
    622                                        uint8_t status) {
    623   tBTA_GATTS_RCB* p_reg = bta_gatts_find_app_rcb_by_app_if(gatt_if);
    624   if (!p_reg || !p_reg->p_cback) {
    625     APPL_TRACE_ERROR("%s: server_if=%d not found", __func__, gatt_if);
    626     return;
    627   }
    628 
    629   tBTA_GATTS cb_data;
    630   cb_data.phy_update.conn_id = conn_id;
    631   cb_data.phy_update.server_if = gatt_if;
    632   cb_data.phy_update.tx_phy = tx_phy;
    633   cb_data.phy_update.rx_phy = rx_phy;
    634   cb_data.phy_update.status = status;
    635   (*p_reg->p_cback)(BTA_GATTS_PHY_UPDATE_EVT, &cb_data);
    636 }
    637 
    638 static void bta_gatts_conn_update_cback(tGATT_IF gatt_if, uint16_t conn_id,
    639                                         uint16_t interval, uint16_t latency,
    640                                         uint16_t timeout, uint8_t status) {
    641   tBTA_GATTS_RCB* p_reg = bta_gatts_find_app_rcb_by_app_if(gatt_if);
    642   if (!p_reg || !p_reg->p_cback) {
    643     APPL_TRACE_ERROR("%s: server_if=%d not found", __func__, gatt_if);
    644     return;
    645   }
    646 
    647   tBTA_GATTS cb_data;
    648   cb_data.conn_update.conn_id = conn_id;
    649   cb_data.conn_update.server_if = gatt_if;
    650   cb_data.conn_update.interval = interval;
    651   cb_data.conn_update.latency = latency;
    652   cb_data.conn_update.timeout = timeout;
    653   cb_data.conn_update.status = status;
    654   (*p_reg->p_cback)(BTA_GATTS_CONN_UPDATE_EVT, &cb_data);
    655 }
    656 
    657 /*******************************************************************************
    658  *
    659  * Function         bta_gatts_cong_cback
    660  *
    661  * Description      congestion callback.
    662  *
    663  * Returns          none.
    664  *
    665  ******************************************************************************/
    666 static void bta_gatts_cong_cback(uint16_t conn_id, bool congested) {
    667   tBTA_GATTS_RCB* p_rcb;
    668   tGATT_IF gatt_if;
    669   tBTA_GATT_TRANSPORT transport;
    670   tBTA_GATTS cb_data;
    671 
    672   if (GATT_GetConnectionInfor(conn_id, &gatt_if, cb_data.req_data.remote_bda,
    673                               &transport)) {
    674     p_rcb = bta_gatts_find_app_rcb_by_app_if(gatt_if);
    675 
    676     if (p_rcb && p_rcb->p_cback) {
    677       cb_data.congest.conn_id = conn_id;
    678       cb_data.congest.congested = congested;
    679 
    680       (*p_rcb->p_cback)(BTA_GATTS_CONGEST_EVT, &cb_data);
    681     }
    682   }
    683 }
    684