Home | History | Annotate | Download | only in gatt
      1 /******************************************************************************
      2  *
      3  *  Copyright (C) 2010-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 is the implementation of the API for GATT module of BTA.
     22  *
     23  ******************************************************************************/
     24 
     25 #include "bt_target.h"
     26 
     27 #if defined(BTA_GATT_INCLUDED) && (BTA_GATT_INCLUDED == TRUE)
     28 
     29 #include <string.h>
     30 #include "gki.h"
     31 #include "bta_sys.h"
     32 #include "bta_gatt_api.h"
     33 #include "bta_gattc_int.h"
     34 
     35 /*****************************************************************************
     36 **  Constants
     37 *****************************************************************************/
     38 
     39 static const tBTA_SYS_REG bta_gattc_reg =
     40 {
     41     bta_gattc_hdl_event,
     42     BTA_GATTC_Disable
     43 };
     44 
     45 
     46 /*******************************************************************************
     47 **
     48 ** Function         BTA_GATTC_Disable
     49 **
     50 ** Description      This function is called to disable GATTC module
     51 **
     52 ** Parameters       None.
     53 **
     54 ** Returns          None
     55 **
     56 *******************************************************************************/
     57 void BTA_GATTC_Disable(void)
     58 {
     59     BT_HDR  *p_buf;
     60 
     61     if (bta_sys_is_register(BTA_ID_GATTC) == FALSE)
     62     {
     63         APPL_TRACE_WARNING("GATTC Module not enabled/already disabled");
     64         return;
     65     }
     66     if ((p_buf = (BT_HDR *) GKI_getbuf(sizeof(BT_HDR))) != NULL)
     67     {
     68         p_buf->event = BTA_GATTC_API_DISABLE_EVT;
     69         bta_sys_sendmsg(p_buf);
     70     }
     71     bta_sys_deregister(BTA_ID_GATTC);
     72 
     73 }
     74 
     75 /*******************************************************************************
     76 **
     77 ** Function         BTA_GATTC_AppRegister
     78 **
     79 ** Description      This function is called to register application callbacks
     80 **                    with BTA GATTC module.
     81 **
     82 ** Parameters       p_app_uuid - applicaiton UUID
     83 **                  p_client_cb - pointer to the application callback function.
     84 **
     85 ** Returns          None
     86 **
     87 *******************************************************************************/
     88 void BTA_GATTC_AppRegister(tBT_UUID *p_app_uuid, tBTA_GATTC_CBACK *p_client_cb)
     89 {
     90     tBTA_GATTC_API_REG  *p_buf;
     91 
     92     if (bta_sys_is_register(BTA_ID_GATTC) == FALSE)
     93     {
     94         bta_sys_register(BTA_ID_GATTC, &bta_gattc_reg);
     95     }
     96 
     97     if ((p_buf = (tBTA_GATTC_API_REG *) GKI_getbuf(sizeof(tBTA_GATTC_API_REG))) != NULL)
     98     {
     99         p_buf->hdr.event    = BTA_GATTC_API_REG_EVT;
    100         if (p_app_uuid != NULL)
    101             memcpy(&p_buf->app_uuid, p_app_uuid, sizeof(tBT_UUID));
    102         p_buf->p_cback      = p_client_cb;
    103 
    104         bta_sys_sendmsg(p_buf);
    105     }
    106     return;
    107 }
    108 
    109 /*******************************************************************************
    110 **
    111 ** Function         BTA_GATTC_AppDeregister
    112 **
    113 ** Description      This function is called to deregister an application
    114 **                  from BTA GATTC module.
    115 **
    116 ** Parameters       client_if - client interface identifier.
    117 **
    118 ** Returns          None
    119 **
    120 *******************************************************************************/
    121 void BTA_GATTC_AppDeregister(tBTA_GATTC_IF client_if)
    122 {
    123     tBTA_GATTC_API_DEREG  *p_buf;
    124 
    125     if ((p_buf = (tBTA_GATTC_API_DEREG *) GKI_getbuf(sizeof(tBTA_GATTC_API_DEREG))) != NULL)
    126     {
    127         p_buf->hdr.event = BTA_GATTC_API_DEREG_EVT;
    128         p_buf->client_if = client_if;
    129         bta_sys_sendmsg(p_buf);
    130     }
    131     return;
    132 }
    133 
    134 /*******************************************************************************
    135 **
    136 ** Function         BTA_GATTC_Open
    137 **
    138 ** Description      Open a direct connection or add a background auto connection
    139 **                  bd address
    140 **
    141 ** Parameters       client_if: server interface.
    142 **                  remote_bda: remote device BD address.
    143 **                  is_direct: direct connection or background auto connection
    144 **                  transport: Transport to be used for GATT connection (BREDR/LE)
    145 **
    146 ** Returns          void
    147 **
    148 *******************************************************************************/
    149 void BTA_GATTC_Open(tBTA_GATTC_IF client_if, BD_ADDR remote_bda,
    150                     BOOLEAN is_direct, tBTA_GATT_TRANSPORT transport)
    151 {
    152     tBTA_GATTC_API_OPEN  *p_buf;
    153 
    154     if ((p_buf = (tBTA_GATTC_API_OPEN *) GKI_getbuf(sizeof(tBTA_GATTC_API_OPEN))) != NULL)
    155     {
    156         p_buf->hdr.event = BTA_GATTC_API_OPEN_EVT;
    157 
    158         p_buf->client_if = client_if;
    159         p_buf->is_direct = is_direct;
    160         p_buf->transport = transport;
    161         memcpy(p_buf->remote_bda, remote_bda, BD_ADDR_LEN);
    162 
    163 
    164         bta_sys_sendmsg(p_buf);
    165     }
    166     return;
    167 }
    168 
    169 /*******************************************************************************
    170 **
    171 ** Function         BTA_GATTC_CancelOpen
    172 **
    173 ** Description      Cancel a direct open connection or remove a background auto connection
    174 **                  bd address
    175 **
    176 ** Parameters       client_if: server interface.
    177 **                  remote_bda: remote device BD address.
    178 **                  is_direct: direct connection or background auto connection
    179 **
    180 ** Returns          void
    181 **
    182 *******************************************************************************/
    183 void BTA_GATTC_CancelOpen(tBTA_GATTC_IF client_if, BD_ADDR remote_bda, BOOLEAN is_direct)
    184 {
    185     tBTA_GATTC_API_CANCEL_OPEN  *p_buf;
    186 
    187     if ((p_buf = (tBTA_GATTC_API_CANCEL_OPEN *) GKI_getbuf(sizeof(tBTA_GATTC_API_CANCEL_OPEN))) != NULL)
    188     {
    189         p_buf->hdr.event = BTA_GATTC_API_CANCEL_OPEN_EVT;
    190 
    191         p_buf->client_if = client_if;
    192         p_buf->is_direct = is_direct;
    193         memcpy(p_buf->remote_bda, remote_bda, BD_ADDR_LEN);
    194 
    195         bta_sys_sendmsg(p_buf);
    196     }
    197     return;
    198 }
    199 
    200 /*******************************************************************************
    201 **
    202 ** Function         BTA_GATTC_Close
    203 **
    204 ** Description      Close a connection to a GATT server.
    205 **
    206 ** Parameters       conn_id: connectino ID to be closed.
    207 **
    208 ** Returns          void
    209 **
    210 *******************************************************************************/
    211 void BTA_GATTC_Close(UINT16 conn_id)
    212 {
    213     BT_HDR  *p_buf;
    214 
    215     if ((p_buf = (BT_HDR *) GKI_getbuf(sizeof(BT_HDR))) != NULL)
    216     {
    217         p_buf->event = BTA_GATTC_API_CLOSE_EVT;
    218 
    219         p_buf->layer_specific = conn_id;
    220 
    221         bta_sys_sendmsg(p_buf);
    222     }
    223     return;
    224 
    225 }
    226 /*******************************************************************************
    227 **
    228 ** Function         BTA_GATTC_ConfigureMTU
    229 **
    230 ** Description      Configure the MTU size in the GATT channel. This can be done
    231 **                  only once per connection.
    232 **
    233 ** Parameters       conn_id: connection ID.
    234 **                  mtu: desired MTU size to use.
    235 **
    236 ** Returns          void
    237 **
    238 *******************************************************************************/
    239 void BTA_GATTC_ConfigureMTU (UINT16 conn_id, UINT16 mtu)
    240 {
    241     tBTA_GATTC_API_CFG_MTU  *p_buf;
    242 
    243     if ((p_buf = (tBTA_GATTC_API_CFG_MTU *) GKI_getbuf(sizeof(tBTA_GATTC_API_CFG_MTU))) != NULL)
    244     {
    245         p_buf->hdr.event = BTA_GATTC_API_CFG_MTU_EVT;
    246         p_buf->hdr.layer_specific = conn_id;
    247 
    248         p_buf->mtu = mtu;
    249 
    250         bta_sys_sendmsg(p_buf);
    251     }
    252     return;
    253 }
    254 /*******************************************************************************
    255 **
    256 ** Function         BTA_GATTC_ServiceSearchRequest
    257 **
    258 ** Description      This function is called to request a GATT service discovery
    259 **                    on a GATT server. This function report service search result
    260 **                  by a callback event, and followed by a service search complete
    261 **                  event.
    262 **
    263 ** Parameters       conn_id: connection ID.
    264 **                  p_srvc_uuid: a UUID of the service application is interested in.
    265 **                              If Null, discover for all services.
    266 **
    267 ** Returns          None
    268 **
    269 *******************************************************************************/
    270 void BTA_GATTC_ServiceSearchRequest (UINT16 conn_id, tBT_UUID *p_srvc_uuid)
    271 {
    272     tBTA_GATTC_API_SEARCH  *p_buf;
    273     UINT16  len = sizeof(tBTA_GATTC_API_SEARCH) + sizeof(tBT_UUID);
    274 
    275     if ((p_buf = (tBTA_GATTC_API_SEARCH *) GKI_getbuf(len)) != NULL)
    276     {
    277         memset(p_buf, 0, len);
    278 
    279         p_buf->hdr.event = BTA_GATTC_API_SEARCH_EVT;
    280         p_buf->hdr.layer_specific = conn_id;
    281 
    282         if (p_srvc_uuid)
    283         {
    284             p_buf->p_srvc_uuid = (tBT_UUID *)(p_buf + 1);
    285             memcpy(p_buf->p_srvc_uuid, p_srvc_uuid, sizeof(tBT_UUID));
    286         }
    287         else
    288             p_buf->p_srvc_uuid = NULL;
    289 
    290         bta_sys_sendmsg(p_buf);
    291     }
    292     return;
    293 }
    294 
    295 
    296 /*******************************************************************************
    297 **
    298 ** Function         BTA_GATTC_GetFirstChar
    299 **
    300 ** Description      This function is called to find the first characteristic of the
    301 **                  service on the given server.
    302 **
    303 ** Parameters       conn_id: connection ID which identify the server.
    304 **                  p_srvc_id: the service ID of which the characteristic is belonged to.
    305 **                  p_char_uuid_cond: Characteristic UUID, if NULL find the first available
    306 **                               characteristic.
    307 **                  p_char_result: output parameter which will store the GATT
    308 **                                  characteristic ID.
    309 **                  p_property: output parameter to carry the characteristic property.
    310 **
    311 ** Returns          returns status.
    312 **
    313 *******************************************************************************/
    314 tBTA_GATT_STATUS  BTA_GATTC_GetFirstChar (UINT16 conn_id, tBTA_GATT_SRVC_ID *p_srvc_id,
    315                                           tBT_UUID *p_char_uuid_cond,
    316                                           tBTA_GATTC_CHAR_ID *p_char_result,
    317                                           tBTA_GATT_CHAR_PROP *p_property)
    318 {
    319     tBTA_GATT_STATUS    status;
    320 
    321     if (!p_srvc_id || !p_char_result)
    322         return BTA_GATT_ILLEGAL_PARAMETER;
    323 
    324     if ((status = bta_gattc_query_cache(conn_id, BTA_GATTC_ATTR_TYPE_CHAR, p_srvc_id, NULL,
    325                                         p_char_uuid_cond, &p_char_result->char_id, (void *)p_property))
    326         == BTA_GATT_OK)
    327     {
    328         memcpy(&p_char_result->srvc_id, p_srvc_id, sizeof(tBTA_GATT_SRVC_ID));
    329     }
    330 
    331     return status;
    332 
    333 }
    334 /*******************************************************************************
    335 **
    336 ** Function         BTA_GATTC_GetNextChar
    337 **
    338 ** Description      This function is called to find the next characteristic of the
    339 **                  service on the given server.
    340 **
    341 ** Parameters       conn_id: connection ID which identify the server.
    342 **                  p_start_char_id: start the characteristic search from the next record
    343 **                           after the one identified by char_id.
    344 **                  p_char_uuid_cond: Characteristic UUID, if NULL find the first available
    345 **                               characteristic.
    346 **                  p_char_result: output parameter which will store the GATT
    347 **                                  characteristic ID.
    348 **                  p_property: output parameter to carry the characteristic property.
    349 **
    350 ** Returns          returns status.
    351 **
    352 *******************************************************************************/
    353 tBTA_GATT_STATUS  BTA_GATTC_GetNextChar (UINT16 conn_id,
    354                                          tBTA_GATTC_CHAR_ID *p_start_char_id,
    355                                          tBT_UUID           *p_char_uuid_cond,
    356                                          tBTA_GATTC_CHAR_ID *p_char_result,
    357                                          tBTA_GATT_CHAR_PROP    *p_property)
    358 {
    359     tBTA_GATT_STATUS    status;
    360 
    361     if (!p_start_char_id || !p_char_result)
    362         return BTA_GATT_ILLEGAL_PARAMETER;
    363 
    364     if ((status = bta_gattc_query_cache(conn_id, BTA_GATTC_ATTR_TYPE_CHAR,
    365                                         &p_start_char_id->srvc_id,
    366                                         &p_start_char_id->char_id,
    367                                         p_char_uuid_cond,
    368                                         &p_char_result->char_id,
    369                                         (void *) p_property))
    370         == BTA_GATT_OK)
    371     {
    372         memcpy(&p_char_result->srvc_id, &p_start_char_id->srvc_id, sizeof(tBTA_GATT_SRVC_ID));
    373     }
    374 
    375     return status;
    376 }
    377 
    378 /*******************************************************************************
    379 **
    380 ** Function         BTA_GATTC_GetFirstCharDescr
    381 **
    382 ** Description      This function is called to find the first characteristic descriptor of the
    383 **                  characteristic on the given server.
    384 **
    385 ** Parameters       conn_id: connection ID which identify the server.
    386 **                  p_char_id: the characteristic ID of which the descriptor is belonged to.
    387 **                  p_descr_uuid_cond: Characteristic Descr UUID, if NULL find the first available
    388 **                               characteristic.
    389 **                  p_descr_result: output parameter which will store the GATT
    390 **                                  characteristic descriptor ID.
    391 **
    392 ** Returns          returns status.
    393 **
    394 *******************************************************************************/
    395 tBTA_GATT_STATUS  BTA_GATTC_GetFirstCharDescr (UINT16 conn_id, tBTA_GATTC_CHAR_ID *p_char_id,
    396                                                 tBT_UUID *p_descr_uuid_cond,
    397                                                 tBTA_GATTC_CHAR_DESCR_ID *p_descr_result)
    398 {
    399     tBTA_GATT_STATUS    status;
    400 
    401     if (!p_char_id || !p_descr_result)
    402         return BTA_GATT_ILLEGAL_PARAMETER;
    403 
    404     memset(p_descr_result, 0, sizeof(tBTA_GATTC_CHAR_DESCR_ID));
    405 
    406     if ((status = bta_gattc_query_cache(conn_id,
    407                                         BTA_GATTC_ATTR_TYPE_CHAR_DESCR,
    408                                         &p_char_id->srvc_id,
    409                                         &p_char_id->char_id,
    410                                         p_descr_uuid_cond,
    411                                         &p_descr_result->char_id.char_id,
    412                                         NULL))
    413         == BTA_GATT_OK)
    414     {
    415         memcpy(&p_descr_result->descr_id, &p_descr_result->char_id.char_id, sizeof(tBTA_GATT_ID));
    416         memcpy(&p_descr_result->char_id, p_char_id, sizeof(tBTA_GATTC_CHAR_ID));
    417     }
    418     return status;
    419 
    420 }
    421 /*******************************************************************************
    422 **
    423 ** Function         BTA_GATTC_GetNextCharDescr
    424 **
    425 ** Description      This function is called to find the next characteristic descriptor
    426 **                  of the characterisctic.
    427 **
    428 ** Parameters       conn_id: connection ID which identify the server.
    429 **                  p_start_descr_id: start the descriptor search from the next record
    430 **                           after the one identified by p_start_descr_id.
    431 **                  p_descr_uuid_cond: Characteristic descriptor UUID, if NULL find
    432 **                               the first available characteristic descriptor.
    433 **                  p_descr_result: output parameter which will store the GATT
    434 **                                  characteristic descriptor ID.
    435 **
    436 ** Returns          returns status.
    437 **
    438 *******************************************************************************/
    439 tBTA_GATT_STATUS  BTA_GATTC_GetNextCharDescr (UINT16 conn_id,
    440                                              tBTA_GATTC_CHAR_DESCR_ID *p_start_descr_id,
    441                                              tBT_UUID           *p_descr_uuid_cond,
    442                                              tBTA_GATTC_CHAR_DESCR_ID *p_descr_result)
    443 {
    444     tBTA_GATT_STATUS    status;
    445 
    446     if (!p_start_descr_id || !p_descr_result)
    447         return BTA_GATT_ILLEGAL_PARAMETER;
    448 
    449     memset(p_descr_result, 0, sizeof(tBTA_GATTC_CHAR_DESCR_ID));
    450 
    451     if ((status = bta_gattc_query_cache(conn_id, BTA_GATTC_ATTR_TYPE_CHAR_DESCR,
    452                                         &p_start_descr_id->char_id.srvc_id,
    453                                         &p_start_descr_id->char_id.char_id,
    454                                         p_descr_uuid_cond,
    455                                         &p_descr_result->char_id.char_id,
    456                                         (void *)&p_start_descr_id->descr_id))
    457         == BTA_GATT_OK)
    458     {
    459         memcpy(&p_descr_result->descr_id, &p_descr_result->char_id.char_id, sizeof(tBTA_GATT_ID));
    460         memcpy(&p_descr_result->char_id, p_start_descr_id, sizeof(tBTA_GATTC_CHAR_ID));
    461     }
    462 
    463     return status;
    464 }
    465 
    466 
    467 /*******************************************************************************
    468 **
    469 ** Function         BTA_GATTC_GetFirstIncludedService
    470 **
    471 ** Description      This function is called to find the first included service of the
    472 **                  service on the given server.
    473 **
    474 ** Parameters       conn_id: connection ID which identify the server.
    475 **                  p_srvc_id: the service ID of which the characteristic is belonged to.
    476 **                  p_uuid_cond: Characteristic UUID, if NULL find the first available
    477 **                               characteristic.
    478 **                  p_result: output parameter which will store the GATT ID
    479 **                              of the included service found.
    480 **
    481 ** Returns          returns status.
    482 **
    483 *******************************************************************************/
    484 tBTA_GATT_STATUS  BTA_GATTC_GetFirstIncludedService(UINT16 conn_id, tBTA_GATT_SRVC_ID *p_srvc_id,
    485                                                     tBT_UUID *p_uuid_cond, tBTA_GATTC_INCL_SVC_ID *p_result)
    486 {
    487     tBTA_GATT_STATUS    status;
    488 
    489     if (!p_srvc_id || !p_result)
    490         return BTA_GATT_ILLEGAL_PARAMETER;
    491 
    492     if ((status = bta_gattc_query_cache(conn_id,
    493                                         BTA_GATTC_ATTR_TYPE_INCL_SRVC,
    494                                         p_srvc_id,
    495                                         NULL,
    496                                         p_uuid_cond,
    497                                         &p_result->incl_svc_id.id,
    498                                         (void *)&p_result->incl_svc_id.is_primary))
    499         == BTA_GATT_OK)
    500     {
    501         memcpy(&p_result->srvc_id, p_srvc_id, sizeof(tBTA_GATT_SRVC_ID));
    502     }
    503 
    504     return status;
    505 }
    506 /*******************************************************************************
    507 **
    508 ** Function         BTA_GATTC_GetNextIncludedService
    509 **
    510 ** Description      This function is called to find the next included service of the
    511 **                  service on the given server.
    512 **
    513 ** Parameters       conn_id: connection ID which identify the server.
    514 **                  p_start_id: start the search from the next record
    515 **                                  after the one identified by p_start_id.
    516 **                  p_uuid_cond: Included service UUID, if NULL find the first available
    517 **                               included service.
    518 **                  p_result: output parameter which will store the GATT ID
    519 **                              of the included service found.
    520 **
    521 ** Returns          returns status.
    522 **
    523 *******************************************************************************/
    524 tBTA_GATT_STATUS  BTA_GATTC_GetNextIncludedService(UINT16 conn_id,
    525                                                    tBTA_GATTC_INCL_SVC_ID *p_start_id,
    526                                                    tBT_UUID               *p_uuid_cond,
    527                                                    tBTA_GATTC_INCL_SVC_ID *p_result)
    528 {
    529     tBTA_GATT_STATUS    status;
    530 
    531     if (!p_start_id || !p_result)
    532         return BTA_GATT_ILLEGAL_PARAMETER;
    533 
    534     if ((status = bta_gattc_query_cache(conn_id,
    535                                         BTA_GATTC_ATTR_TYPE_INCL_SRVC,
    536                                         &p_start_id->srvc_id,
    537                                         &p_start_id->incl_svc_id.id,
    538                                         p_uuid_cond,
    539                                         &p_result->incl_svc_id.id,
    540                                         (void *)&p_result->incl_svc_id.is_primary))
    541         == BTA_GATT_OK)
    542     {
    543         memcpy(&p_result->srvc_id, &p_start_id->srvc_id, sizeof(tBTA_GATT_SRVC_ID));
    544     }
    545 
    546     return status;
    547 }
    548 
    549 /*******************************************************************************
    550 **
    551 ** Function         BTA_GATTC_ReadCharacteristic
    552 **
    553 ** Description      This function is called to read a service's characteristics of
    554 **                    the given characteritisc ID.
    555 **
    556 ** Parameters       conn_id - connectino ID.
    557 **                    p_char_id - characteritic ID to read.
    558 **
    559 ** Returns          None
    560 **
    561 *******************************************************************************/
    562 void BTA_GATTC_ReadCharacteristic(UINT16 conn_id, tBTA_GATTC_CHAR_ID *p_char_id,
    563                                   tBTA_GATT_AUTH_REQ auth_req)
    564 {
    565     tBTA_GATTC_API_READ  *p_buf;
    566 
    567     if ((p_buf = (tBTA_GATTC_API_READ *) GKI_getbuf(sizeof(tBTA_GATTC_API_READ))) != NULL)
    568     {
    569         memset(p_buf, 0, sizeof(tBTA_GATTC_API_READ));
    570 
    571         p_buf->hdr.event = BTA_GATTC_API_READ_EVT;
    572         p_buf->hdr.layer_specific = conn_id;
    573         p_buf->auth_req = auth_req;
    574 
    575         memcpy(&p_buf->srvc_id, &p_char_id->srvc_id, sizeof(tBTA_GATT_SRVC_ID));
    576         memcpy(&p_buf->char_id, &p_char_id->char_id, sizeof(tBTA_GATT_ID));
    577         p_buf->p_descr_type = NULL;
    578 
    579         bta_sys_sendmsg(p_buf);
    580     }
    581     return;
    582 }
    583 
    584 /*******************************************************************************
    585 **
    586 ** Function         BTA_GATTC_ReadCharDescr
    587 **
    588 ** Description      This function is called to read a characteristics descriptor.
    589 **
    590 ** Parameters       conn_id - connection ID.
    591 **                    p_char_descr_id - characteritic descriptor ID to read.
    592 **
    593 ** Returns          None
    594 **
    595 *******************************************************************************/
    596 void BTA_GATTC_ReadCharDescr (UINT16 conn_id,
    597                               tBTA_GATTC_CHAR_DESCR_ID  *p_descr_id,
    598                               tBTA_GATT_AUTH_REQ auth_req)
    599 {
    600     tBTA_GATTC_API_READ  *p_buf;
    601     UINT16  len = (UINT16)(sizeof(tBTA_GATT_ID) + sizeof(tBTA_GATTC_API_READ));
    602 
    603     if ((p_buf = (tBTA_GATTC_API_READ *) GKI_getbuf(len)) != NULL)
    604     {
    605         memset(p_buf, 0, sizeof(tBTA_GATTC_API_READ));
    606 
    607         p_buf->hdr.event = BTA_GATTC_API_READ_EVT;
    608         p_buf->hdr.layer_specific = conn_id;
    609         p_buf->auth_req = auth_req;
    610 
    611         memcpy(&p_buf->srvc_id, &p_descr_id->char_id.srvc_id, sizeof(tBTA_GATT_SRVC_ID));
    612         memcpy(&p_buf->char_id, &p_descr_id->char_id.char_id, sizeof(tBTA_GATT_ID));
    613         p_buf->p_descr_type  = (tBTA_GATT_ID *)(p_buf + 1);
    614 
    615         memcpy(p_buf->p_descr_type, &p_descr_id->descr_id, sizeof(tBTA_GATT_ID));
    616 
    617         bta_sys_sendmsg(p_buf);
    618     }
    619     return;
    620 
    621 }
    622 /*******************************************************************************
    623 **
    624 ** Function         BTA_GATTC_ReadMultiple
    625 **
    626 ** Description      This function is called to read multiple characteristic or
    627 **                  characteristic descriptors.
    628 **
    629 ** Parameters       conn_id - connectino ID.
    630 **                    p_read_multi - pointer to the read multiple parameter.
    631 **
    632 ** Returns          None
    633 **
    634 *******************************************************************************/
    635 void BTA_GATTC_ReadMultiple(UINT16 conn_id, tBTA_GATTC_MULTI *p_read_multi,
    636                             tBTA_GATT_AUTH_REQ auth_req)
    637 {
    638     tBTA_GATTC_API_READ_MULTI  *p_buf;
    639     tBTA_GATTC_ATTR_ID          *p_value;
    640     UINT16      len = (UINT16)(sizeof(tBTA_GATTC_API_READ_MULTI) +
    641                                p_read_multi->num_attr * sizeof(tBTA_GATTC_ATTR_ID));
    642     UINT8       i;
    643 
    644     if ((p_buf = (tBTA_GATTC_API_READ_MULTI *) GKI_getbuf(len)) != NULL)
    645     {
    646         memset(p_buf, 0, len);
    647 
    648         p_buf->hdr.event = BTA_GATTC_API_READ_MULTI_EVT;
    649         p_buf->hdr.layer_specific = conn_id;
    650         p_buf->auth_req = auth_req;
    651 
    652         p_buf->num_attr = p_read_multi->num_attr;
    653 
    654         if (p_buf->num_attr > 0)
    655         {
    656             p_buf->p_id_list = p_value = (tBTA_GATTC_ATTR_ID *)(p_buf + 1);
    657 
    658             for (i = 0; i < p_buf->num_attr; i ++, p_value ++)
    659             {
    660                 memcpy(p_value, &p_read_multi->id_list[i], sizeof(tBTA_GATTC_ATTR_ID));
    661             }
    662         }
    663         bta_sys_sendmsg(p_buf);
    664     }
    665     return;
    666 }
    667 
    668 
    669 /*******************************************************************************
    670 **
    671 ** Function         BTA_GATTC_WriteCharValue
    672 **
    673 ** Description      This function is called to write characteristic value.
    674 **
    675 ** Parameters       conn_id - connection ID.
    676 **                    p_char_id - characteristic ID to write.
    677 **                    write_type - type of write.
    678 **                  len: length of the data to be written.
    679 **                  p_value - the value to be written.
    680 **
    681 ** Returns          None
    682 **
    683 *******************************************************************************/
    684 void BTA_GATTC_WriteCharValue ( UINT16 conn_id,
    685                                 tBTA_GATTC_CHAR_ID *p_char_id,
    686                                 tBTA_GATTC_WRITE_TYPE  write_type,
    687                                 UINT16 len,
    688                                 UINT8 *p_value,
    689                                 tBTA_GATT_AUTH_REQ auth_req)
    690 {
    691     tBTA_GATTC_API_WRITE  *p_buf;
    692 
    693     if ((p_buf = (tBTA_GATTC_API_WRITE *) GKI_getbuf((UINT16)(sizeof(tBTA_GATTC_API_WRITE) + len))) != NULL)
    694     {
    695         memset(p_buf, 0, sizeof(tBTA_GATTC_API_WRITE) + len);
    696 
    697         p_buf->hdr.event = BTA_GATTC_API_WRITE_EVT;
    698         p_buf->hdr.layer_specific = conn_id;
    699         p_buf->auth_req = auth_req;
    700 
    701         memcpy(&p_buf->srvc_id, &p_char_id->srvc_id, sizeof(tBTA_GATT_SRVC_ID));
    702         memcpy(&p_buf->char_id, &p_char_id->char_id, sizeof(tBTA_GATT_ID));
    703 
    704         p_buf->write_type = write_type;
    705         p_buf->len = len;
    706 
    707         if (p_value && len > 0)
    708         {
    709             p_buf->p_value = (UINT8 *)(p_buf + 1);
    710             memcpy(p_buf->p_value, p_value, len);
    711         }
    712 
    713         bta_sys_sendmsg(p_buf);
    714     }
    715     return;
    716 }
    717 /*******************************************************************************
    718 **
    719 ** Function         BTA_GATTC_WriteCharDescr
    720 **
    721 ** Description      This function is called to write characteristic descriptor value.
    722 **
    723 ** Parameters       conn_id - connection ID
    724 **                    p_char_descr_id - characteristic descriptor ID to write.
    725 **                  write_type - write type.
    726 **                  p_value - the value to be written.
    727 **
    728 ** Returns          None
    729 **
    730 *******************************************************************************/
    731 void BTA_GATTC_WriteCharDescr (UINT16 conn_id,
    732                                tBTA_GATTC_CHAR_DESCR_ID *p_char_descr_id,
    733                                tBTA_GATTC_WRITE_TYPE  write_type,
    734                                tBTA_GATT_UNFMT      *p_data,
    735                                tBTA_GATT_AUTH_REQ auth_req)
    736 {
    737     tBTA_GATTC_API_WRITE  *p_buf;
    738     UINT16  len = sizeof(tBTA_GATTC_API_WRITE) + sizeof(tBTA_GATT_ID);
    739 
    740     if (p_data != NULL)
    741         len += p_data->len;
    742 
    743     if ((p_buf = (tBTA_GATTC_API_WRITE *) GKI_getbuf(len)) != NULL)
    744     {
    745         memset(p_buf, 0, len);
    746 
    747         p_buf->hdr.event = BTA_GATTC_API_WRITE_EVT;
    748         p_buf->hdr.layer_specific = conn_id;
    749         p_buf->auth_req = auth_req;
    750 
    751         memcpy(&p_buf->srvc_id, &p_char_descr_id->char_id.srvc_id, sizeof(tBTA_GATT_SRVC_ID));
    752         memcpy(&p_buf->char_id, &p_char_descr_id->char_id.char_id, sizeof(tBTA_GATT_ID));
    753         p_buf->p_descr_type = (tBTA_GATT_ID *)(p_buf + 1);
    754         memcpy(p_buf->p_descr_type, &p_char_descr_id->descr_id, sizeof(tBTA_GATT_ID));
    755         p_buf->write_type = write_type;
    756 
    757         if (p_data && p_data->len != 0)
    758         {
    759             p_buf->p_value  = (UINT8 *)(p_buf->p_descr_type + 1);
    760             p_buf->len      = p_data->len;
    761             /* pack the descr data */
    762             memcpy(p_buf->p_value, p_data->p_value, p_data->len);
    763         }
    764 
    765         bta_sys_sendmsg(p_buf);
    766     }
    767     return;
    768 
    769 }
    770 /*******************************************************************************
    771 **
    772 ** Function         BTA_GATTC_PrepareWrite
    773 **
    774 ** Description      This function is called to prepare write a characteristic value.
    775 **
    776 ** Parameters       conn_id - connection ID.
    777 **                    p_char_id - GATT characteritic ID of the service.
    778 **                  offset - offset of the write value.
    779 **                  len: length of the data to be written.
    780 **                  p_value - the value to be written.
    781 **
    782 ** Returns          None
    783 **
    784 *******************************************************************************/
    785 void BTA_GATTC_PrepareWrite  (UINT16 conn_id, tBTA_GATTC_CHAR_ID *p_char_id,
    786                               UINT16 offset, UINT16 len, UINT8 *p_value,
    787                               tBTA_GATT_AUTH_REQ auth_req)
    788 {
    789     tBTA_GATTC_API_WRITE  *p_buf;
    790 
    791     if ((p_buf = (tBTA_GATTC_API_WRITE *) GKI_getbuf((UINT16)(sizeof(tBTA_GATTC_API_WRITE) + len))) != NULL)
    792     {
    793         memset(p_buf, 0, sizeof(tBTA_GATTC_API_WRITE) + len);
    794 
    795         p_buf->hdr.event = BTA_GATTC_API_WRITE_EVT;
    796         p_buf->hdr.layer_specific = conn_id;
    797         p_buf->auth_req = auth_req;
    798 
    799         memcpy(&p_buf->srvc_id, &p_char_id->srvc_id, sizeof(tBTA_GATT_SRVC_ID));
    800         memcpy(&p_buf->char_id, &p_char_id->char_id, sizeof(tBTA_GATT_ID));
    801 
    802         p_buf->write_type = BTA_GATTC_WRITE_PREPARE;
    803         p_buf->offset   = offset;
    804         p_buf->len = len;
    805 
    806         if (p_value && len > 0)
    807         {
    808             p_buf->p_value = (UINT8 *)(p_buf + 1);
    809             memcpy(p_buf->p_value, p_value, len);
    810         }
    811 
    812         bta_sys_sendmsg(p_buf);
    813     }
    814     return;
    815 
    816 }
    817 /*******************************************************************************
    818 **
    819 ** Function         BTA_GATTC_ExecuteWrite
    820 **
    821 ** Description      This function is called to execute write a prepare write sequence.
    822 **
    823 ** Parameters       conn_id - connection ID.
    824 **                    is_execute - execute or cancel.
    825 **
    826 ** Returns          None
    827 **
    828 *******************************************************************************/
    829 void BTA_GATTC_ExecuteWrite  (UINT16 conn_id, BOOLEAN is_execute)
    830 {
    831     tBTA_GATTC_API_EXEC  *p_buf;
    832 
    833     if ((p_buf = (tBTA_GATTC_API_EXEC *) GKI_getbuf((UINT16)sizeof(tBTA_GATTC_API_EXEC))) != NULL)
    834     {
    835         memset(p_buf, 0, sizeof(tBTA_GATTC_API_EXEC));
    836 
    837         p_buf->hdr.event = BTA_GATTC_API_EXEC_EVT;
    838         p_buf->hdr.layer_specific = conn_id;
    839 
    840         p_buf->is_execute = is_execute;
    841 
    842         bta_sys_sendmsg(p_buf);
    843     }
    844     return;
    845 
    846 }
    847 /*******************************************************************************
    848 **
    849 ** Function         BTA_GATTC_SendIndConfirm
    850 **
    851 ** Description      This function is called to send handle value confirmation.
    852 **
    853 ** Parameters       conn_id - connection ID.
    854 **                    p_char_id - characteristic ID to confirm.
    855 **
    856 ** Returns          None
    857 **
    858 *******************************************************************************/
    859 void BTA_GATTC_SendIndConfirm (UINT16 conn_id, tBTA_GATTC_CHAR_ID *p_char_id)
    860 {
    861     tBTA_GATTC_API_CONFIRM  *p_buf;
    862 
    863     APPL_TRACE_API("BTA_GATTC_SendIndConfirm conn_id=%d service uuid1=0x%x char uuid=0x%x",
    864                     conn_id, p_char_id->srvc_id.id.uuid.uu.uuid16, p_char_id->char_id.uuid.uu.uuid16);
    865 
    866     if ((p_buf = (tBTA_GATTC_API_CONFIRM *) GKI_getbuf(sizeof(tBTA_GATTC_API_CONFIRM))) != NULL)
    867     {
    868         memset(p_buf, 0, sizeof(tBTA_GATTC_API_CONFIRM));
    869 
    870         p_buf->hdr.event = BTA_GATTC_API_CONFIRM_EVT;
    871         p_buf->hdr.layer_specific = conn_id;
    872 
    873         memcpy(&p_buf->srvc_id, &p_char_id->srvc_id, sizeof(tBTA_GATT_SRVC_ID));
    874         memcpy(&p_buf->char_id, &p_char_id->char_id, sizeof(tBTA_GATT_ID));
    875 
    876         bta_sys_sendmsg(p_buf);
    877     }
    878     return;
    879 
    880 }
    881 
    882 /*******************************************************************************
    883 **
    884 ** Function         BTA_GATTC_RegisterForNotifications
    885 **
    886 ** Description      This function is called to register for notification of a service.
    887 **
    888 ** Parameters       client_if - client interface.
    889 **                  bda - target GATT server.
    890 **                  p_char_id - pointer to GATT characteristic ID.
    891 **
    892 ** Returns          OK if registration succeed, otherwise failed.
    893 **
    894 *******************************************************************************/
    895 tBTA_GATT_STATUS BTA_GATTC_RegisterForNotifications (tBTA_GATTC_IF client_if,
    896                                                      BD_ADDR bda,
    897                                                      tBTA_GATTC_CHAR_ID *p_char_id)
    898 {
    899     tBTA_GATTC_RCB      *p_clreg;
    900     tBTA_GATT_STATUS    status = BTA_GATT_ILLEGAL_PARAMETER;
    901     UINT8               i;
    902 
    903     if (!p_char_id)
    904     {
    905         APPL_TRACE_ERROR("deregistration failed, unknow char id");
    906         return status;
    907     }
    908 
    909     if ((p_clreg = bta_gattc_cl_get_regcb(client_if)) != NULL)
    910     {
    911         for (i = 0; i < BTA_GATTC_NOTIF_REG_MAX; i ++)
    912         {
    913             if ( p_clreg->notif_reg[i].in_use &&
    914                  !memcmp(p_clreg->notif_reg[i].remote_bda, bda, BD_ADDR_LEN) &&
    915                   bta_gattc_charid_compare(&p_clreg->notif_reg[i].char_id, p_char_id))
    916             {
    917                 APPL_TRACE_WARNING("notification already registered");
    918                 status = BTA_GATT_OK;
    919                 break;
    920             }
    921         }
    922         if (status != BTA_GATT_OK)
    923         {
    924             for (i = 0; i < BTA_GATTC_NOTIF_REG_MAX; i ++)
    925             {
    926                 if (!p_clreg->notif_reg[i].in_use)
    927                 {
    928                     memset((void *)&p_clreg->notif_reg[i], 0, sizeof(tBTA_GATTC_NOTIF_REG));
    929 
    930                     p_clreg->notif_reg[i].in_use = TRUE;
    931                     memcpy(p_clreg->notif_reg[i].remote_bda, bda, BD_ADDR_LEN);
    932 
    933                     p_clreg->notif_reg[i].char_id.srvc_id.is_primary = p_char_id->srvc_id.is_primary;
    934                     bta_gattc_cpygattid(&p_clreg->notif_reg[i].char_id.srvc_id.id, &p_char_id->srvc_id.id);
    935                     bta_gattc_cpygattid(&p_clreg->notif_reg[i].char_id.char_id, &p_char_id->char_id);
    936 
    937                     status = BTA_GATT_OK;
    938                     break;
    939                 }
    940             }
    941             if (i == BTA_GATTC_NOTIF_REG_MAX)
    942             {
    943                 status = BTA_GATT_NO_RESOURCES;
    944                 APPL_TRACE_ERROR("Max Notification Reached, registration failed.");
    945             }
    946         }
    947     }
    948     else
    949     {
    950         APPL_TRACE_ERROR("Client_if: %d Not Registered", client_if);
    951     }
    952 
    953     return status;
    954 }
    955 
    956 /*******************************************************************************
    957 **
    958 ** Function         BTA_GATTC_DeregisterForNotifications
    959 **
    960 ** Description      This function is called to de-register for notification of a service.
    961 **
    962 ** Parameters       client_if - client interface.
    963 **                  bda - target GATT server.
    964 **                  p_char_id - pointer to GATT characteristic ID.
    965 **
    966 ** Returns          OK if deregistration succeed, otherwise failed.
    967 **
    968 *******************************************************************************/
    969 tBTA_GATT_STATUS BTA_GATTC_DeregisterForNotifications (tBTA_GATTC_IF client_if,
    970                                                        BD_ADDR bda,
    971                                                        tBTA_GATTC_CHAR_ID *p_char_id)
    972 {
    973     tBTA_GATTC_RCB      *p_clreg;
    974     tBTA_GATT_STATUS    status = BTA_GATT_ILLEGAL_PARAMETER;
    975     UINT8               i;
    976 
    977     if (!p_char_id)
    978     {
    979         APPL_TRACE_ERROR("deregistration failed, unknow char id");
    980         return status;
    981     }
    982 
    983     if ((p_clreg = bta_gattc_cl_get_regcb(client_if)) != NULL)
    984     {
    985         for (i = 0; i < BTA_GATTC_NOTIF_REG_MAX; i ++)
    986         {
    987             if (p_clreg->notif_reg[i].in_use &&
    988                 !memcmp(p_clreg->notif_reg[i].remote_bda, bda, BD_ADDR_LEN) &&
    989                 bta_gattc_charid_compare(&p_clreg->notif_reg[i].char_id, p_char_id))
    990             {
    991                 APPL_TRACE_DEBUG("Deregistered.");
    992                 memset(&p_clreg->notif_reg[i], 0, sizeof(tBTA_GATTC_NOTIF_REG));
    993                 status = BTA_GATT_OK;
    994                 break;
    995             }
    996         }
    997         if (i == BTA_GATTC_NOTIF_REG_MAX)
    998         {
    999             status = BTA_GATT_ERROR;
   1000 
   1001             APPL_TRACE_ERROR("registration not found");
   1002         }
   1003     }
   1004     else
   1005     {
   1006         APPL_TRACE_ERROR("Client_if: %d Not Registered", client_if);
   1007     }
   1008 
   1009     return status;
   1010 }
   1011 
   1012 /*******************************************************************************
   1013 **
   1014 ** Function         BTA_GATTC_Refresh
   1015 **
   1016 ** Description      Refresh the server cache of the remote device
   1017 **
   1018 ** Parameters       remote_bda: remote device BD address.
   1019 **
   1020 ** Returns          void
   1021 **
   1022 *******************************************************************************/
   1023 void BTA_GATTC_Refresh(BD_ADDR remote_bda)
   1024 {
   1025     tBTA_GATTC_API_OPEN  *p_buf;
   1026 
   1027     if ((p_buf = (tBTA_GATTC_API_OPEN *) GKI_getbuf(sizeof(tBTA_GATTC_API_OPEN))) != NULL)
   1028     {
   1029         p_buf->hdr.event = BTA_GATTC_API_REFRESH_EVT;
   1030 
   1031         memcpy(p_buf->remote_bda, remote_bda, BD_ADDR_LEN);
   1032 
   1033 
   1034         bta_sys_sendmsg(p_buf);
   1035     }
   1036     return;
   1037 }
   1038 
   1039 /*******************************************************************************
   1040 **
   1041 ** Function         BTA_GATTC_Listen
   1042 **
   1043 ** Description      Start advertisement to listen for connection request for a GATT
   1044 **                  client application.
   1045 **
   1046 ** Parameters       client_if: server interface.
   1047 **                  start: to start or stop listening for connection
   1048 **                  remote_bda: remote device BD address, if listen to all device
   1049 **                              use NULL.
   1050 **
   1051 ** Returns          void
   1052 **
   1053 *******************************************************************************/
   1054 void BTA_GATTC_Listen(tBTA_GATTC_IF client_if, BOOLEAN start, BD_ADDR_PTR target_bda)
   1055 {
   1056     tBTA_GATTC_API_LISTEN  *p_buf;
   1057 
   1058     if ((p_buf = (tBTA_GATTC_API_LISTEN *) GKI_getbuf((UINT16)(sizeof(tBTA_GATTC_API_LISTEN) + BD_ADDR_LEN))) != NULL)
   1059     {
   1060         p_buf->hdr.event = BTA_GATTC_API_LISTEN_EVT;
   1061 
   1062         p_buf->client_if = client_if;
   1063         p_buf->start = start;
   1064         if (target_bda)
   1065         {
   1066             p_buf->remote_bda = (UINT8*)(p_buf + 1);
   1067             memcpy(p_buf->remote_bda, target_bda, BD_ADDR_LEN);
   1068         }
   1069         else
   1070             p_buf->remote_bda = NULL;
   1071 
   1072         bta_sys_sendmsg(p_buf);
   1073     }
   1074     return;
   1075 }
   1076 
   1077 /*******************************************************************************
   1078 **
   1079 ** Function         BTA_GATTC_Broadcast
   1080 **
   1081 ** Description      Start broadcasting (non-connectable advertisements)
   1082 **
   1083 ** Parameters       client_if: client interface.
   1084 **                  start: to start or stop listening for connection
   1085 **
   1086 ** Returns          void
   1087 **
   1088 *******************************************************************************/
   1089 void BTA_GATTC_Broadcast(tBTA_GATTC_IF client_if, BOOLEAN start)
   1090 {
   1091     tBTA_GATTC_API_LISTEN  *p_buf;
   1092 
   1093     if ((p_buf = (tBTA_GATTC_API_LISTEN *) GKI_getbuf((UINT16)(sizeof(tBTA_GATTC_API_LISTEN) + BD_ADDR_LEN))) != NULL)
   1094     {
   1095         p_buf->hdr.event = BTA_GATTC_API_BROADCAST_EVT;
   1096         p_buf->client_if = client_if;
   1097         p_buf->start = start;
   1098         bta_sys_sendmsg(p_buf);
   1099     }
   1100     return;
   1101 }
   1102 
   1103 #endif /* BTA_GATT_INCLUDED */
   1104 
   1105