Home | History | Annotate | Download | only in gatt
      1 /******************************************************************************
      2  *
      3  *  Copyright 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 #include <string.h>
     28 
     29 #include <base/bind.h>
     30 #include <base/bind_helpers.h>
     31 #include <base/callback.h>
     32 #include "bt_common.h"
     33 #include "bta_closure_api.h"
     34 #include "bta_gatt_api.h"
     35 #include "bta_gattc_int.h"
     36 #include "bta_sys.h"
     37 #include "device/include/controller.h"
     38 
     39 using bluetooth::Uuid;
     40 
     41 /*****************************************************************************
     42  *  Constants
     43  ****************************************************************************/
     44 
     45 static const tBTA_SYS_REG bta_gattc_reg = {bta_gattc_hdl_event,
     46                                            BTA_GATTC_Disable};
     47 
     48 /*******************************************************************************
     49  *
     50  * Function         BTA_GATTC_Disable
     51  *
     52  * Description      This function is called to disable GATTC module
     53  *
     54  * Parameters       None.
     55  *
     56  * Returns          None
     57  *
     58  ******************************************************************************/
     59 void BTA_GATTC_Disable(void) {
     60   if (!bta_sys_is_register(BTA_ID_GATTC)) {
     61     LOG(WARNING) << "GATTC Module not enabled/already disabled";
     62     return;
     63   }
     64 
     65   do_in_bta_thread(FROM_HERE, base::Bind(&bta_gattc_disable));
     66   bta_sys_deregister(BTA_ID_GATTC);
     67 }
     68 
     69 /**
     70  * This function is called to register application callbacks with BTA GATTC
     71  * module. |client_cb| pointer to the application callback function.
     72  * |cb| one time callback when registration is finished
     73  */
     74 void BTA_GATTC_AppRegister(tBTA_GATTC_CBACK* p_client_cb,
     75                            BtaAppRegisterCallback cb) {
     76   if (!bta_sys_is_register(BTA_ID_GATTC))
     77     bta_sys_register(BTA_ID_GATTC, &bta_gattc_reg);
     78 
     79   do_in_bta_thread(FROM_HERE, base::Bind(&bta_gattc_register, Uuid::GetRandom(),
     80                                          p_client_cb, std::move(cb)));
     81 }
     82 
     83 static void app_deregister_impl(tGATT_IF client_if) {
     84   bta_gattc_deregister(bta_gattc_cl_get_regcb(client_if));
     85 }
     86 /*******************************************************************************
     87  *
     88  * Function         BTA_GATTC_AppDeregister
     89  *
     90  * Description      This function is called to deregister an application
     91  *                  from BTA GATTC module.
     92  *
     93  * Parameters       client_if - client interface identifier.
     94  *
     95  * Returns          None
     96  *
     97  ******************************************************************************/
     98 void BTA_GATTC_AppDeregister(tGATT_IF client_if) {
     99   do_in_bta_thread(FROM_HERE, base::Bind(&app_deregister_impl, client_if));
    100 }
    101 
    102 /*******************************************************************************
    103  *
    104  * Function         BTA_GATTC_Open
    105  *
    106  * Description      Open a direct connection or add a background auto connection
    107  *                  bd address
    108  *
    109  * Parameters       client_if: server interface.
    110  *                  remote_bda: remote device BD address.
    111  *                  is_direct: direct connection or background auto connection
    112  *                  transport: Transport to be used for GATT connection
    113  *                             (BREDR/LE)
    114  *                  initiating_phys: LE PHY to use, optional
    115  *                  opportunistic: wether the connection shall be opportunistic,
    116  *                                 and don't impact the disconnection timer
    117  *
    118  ******************************************************************************/
    119 void BTA_GATTC_Open(tGATT_IF client_if, const RawAddress& remote_bda,
    120                     bool is_direct, tGATT_TRANSPORT transport,
    121                     bool opportunistic) {
    122   uint8_t phy = controller_get_interface()->get_le_all_initiating_phys();
    123   BTA_GATTC_Open(client_if, remote_bda, is_direct, transport, opportunistic,
    124                  phy);
    125 }
    126 
    127 void BTA_GATTC_Open(tGATT_IF client_if, const RawAddress& remote_bda,
    128                     bool is_direct, tGATT_TRANSPORT transport,
    129                     bool opportunistic, uint8_t initiating_phys) {
    130   tBTA_GATTC_API_OPEN* p_buf =
    131       (tBTA_GATTC_API_OPEN*)osi_malloc(sizeof(tBTA_GATTC_API_OPEN));
    132 
    133   p_buf->hdr.event = BTA_GATTC_API_OPEN_EVT;
    134   p_buf->client_if = client_if;
    135   p_buf->is_direct = is_direct;
    136   p_buf->transport = transport;
    137   p_buf->initiating_phys = initiating_phys;
    138   p_buf->opportunistic = opportunistic;
    139   p_buf->remote_bda = remote_bda;
    140 
    141   bta_sys_sendmsg(p_buf);
    142 }
    143 
    144 /*******************************************************************************
    145  *
    146  * Function         BTA_GATTC_CancelOpen
    147  *
    148  * Description      Cancel a direct open connection or remove a background auto
    149  *                  connection
    150  *                  bd address
    151  *
    152  * Parameters       client_if: server interface.
    153  *                  remote_bda: remote device BD address.
    154  *                  is_direct: direct connection or background auto connection
    155  *
    156  * Returns          void
    157  *
    158  ******************************************************************************/
    159 void BTA_GATTC_CancelOpen(tGATT_IF client_if, const RawAddress& remote_bda,
    160                           bool is_direct) {
    161   tBTA_GATTC_API_CANCEL_OPEN* p_buf = (tBTA_GATTC_API_CANCEL_OPEN*)osi_malloc(
    162       sizeof(tBTA_GATTC_API_CANCEL_OPEN));
    163 
    164   p_buf->hdr.event = BTA_GATTC_API_CANCEL_OPEN_EVT;
    165   p_buf->client_if = client_if;
    166   p_buf->is_direct = is_direct;
    167   p_buf->remote_bda = remote_bda;
    168 
    169   bta_sys_sendmsg(p_buf);
    170 }
    171 
    172 /*******************************************************************************
    173  *
    174  * Function         BTA_GATTC_Close
    175  *
    176  * Description      Close a connection to a GATT server.
    177  *
    178  * Parameters       conn_id: connectino ID to be closed.
    179  *
    180  * Returns          void
    181  *
    182  ******************************************************************************/
    183 void BTA_GATTC_Close(uint16_t conn_id) {
    184   BT_HDR* p_buf = (BT_HDR*)osi_malloc(sizeof(BT_HDR));
    185 
    186   p_buf->event = BTA_GATTC_API_CLOSE_EVT;
    187   p_buf->layer_specific = conn_id;
    188 
    189   bta_sys_sendmsg(p_buf);
    190 }
    191 
    192 /*******************************************************************************
    193  *
    194  * Function         BTA_GATTC_ConfigureMTU
    195  *
    196  * Description      Configure the MTU size in the GATT channel. This can be done
    197  *                  only once per connection.
    198  *
    199  * Parameters       conn_id: connection ID.
    200  *                  mtu: desired MTU size to use.
    201  *
    202  * Returns          void
    203  *
    204  ******************************************************************************/
    205 void BTA_GATTC_ConfigureMTU(uint16_t conn_id, uint16_t mtu) {
    206   tBTA_GATTC_API_CFG_MTU* p_buf =
    207       (tBTA_GATTC_API_CFG_MTU*)osi_malloc(sizeof(tBTA_GATTC_API_CFG_MTU));
    208 
    209   p_buf->hdr.event = BTA_GATTC_API_CFG_MTU_EVT;
    210   p_buf->hdr.layer_specific = conn_id;
    211   p_buf->mtu = mtu;
    212 
    213   bta_sys_sendmsg(p_buf);
    214 }
    215 
    216 /*******************************************************************************
    217  *
    218  * Function         BTA_GATTC_ServiceSearchRequest
    219  *
    220  * Description      This function is called to request a GATT service discovery
    221  *                  on a GATT server. This function report service search
    222  *                  result by a callback event, and followed by a service search
    223  *                  complete event.
    224  *
    225  * Parameters       conn_id: connection ID.
    226  *                  p_srvc_uuid: a UUID of the service application is interested
    227  *                               in.
    228  *                              If Null, discover for all services.
    229  *
    230  * Returns          None
    231  *
    232  ******************************************************************************/
    233 void BTA_GATTC_ServiceSearchRequest(uint16_t conn_id, Uuid* p_srvc_uuid) {
    234   const size_t len = sizeof(tBTA_GATTC_API_SEARCH) + sizeof(Uuid);
    235   tBTA_GATTC_API_SEARCH* p_buf = (tBTA_GATTC_API_SEARCH*)osi_calloc(len);
    236 
    237   p_buf->hdr.event = BTA_GATTC_API_SEARCH_EVT;
    238   p_buf->hdr.layer_specific = conn_id;
    239   if (p_srvc_uuid) {
    240     p_buf->p_srvc_uuid = (Uuid*)(p_buf + 1);
    241     *p_buf->p_srvc_uuid = *p_srvc_uuid;
    242   } else {
    243     p_buf->p_srvc_uuid = NULL;
    244   }
    245 
    246   bta_sys_sendmsg(p_buf);
    247 }
    248 
    249 void BTA_GATTC_DiscoverServiceByUuid(uint16_t conn_id,
    250                                      const Uuid& p_srvc_uuid) {
    251   tGATT_DISC_PARAM* param = new tGATT_DISC_PARAM;
    252   param->s_handle = 0x0001;
    253   param->e_handle = 0xFFFF;
    254   param->service = p_srvc_uuid;
    255   do_in_bta_thread(FROM_HERE,
    256                    base::Bind(base::IgnoreResult(&GATTC_Discover), conn_id,
    257                               GATT_DISC_SRVC_BY_UUID, base::Owned(param)));
    258 }
    259 
    260 /*******************************************************************************
    261  *
    262  * Function         BTA_GATTC_GetServices
    263  *
    264  * Description      This function is called to find the services on the given
    265  *                  server.
    266  *
    267  * Parameters       conn_id: connection ID which identify the server.
    268  *
    269  * Returns          returns list of tBTA_GATTC_SERVICE or NULL.
    270  *
    271  ******************************************************************************/
    272 const std::vector<tBTA_GATTC_SERVICE>* BTA_GATTC_GetServices(uint16_t conn_id) {
    273   return bta_gattc_get_services(conn_id);
    274 }
    275 
    276 /*******************************************************************************
    277  *
    278  * Function         BTA_GATTC_GetCharacteristic
    279  *
    280  * Description      This function is called to find the characteristic on the
    281  *                  given server.
    282  *
    283  * Parameters       conn_id - connection ID which identify the server.
    284  *                  handle - characteristic handle
    285  *
    286  * Returns          returns pointer to tBTA_GATTC_CHARACTERISTIC or NULL.
    287  *
    288  ******************************************************************************/
    289 const tBTA_GATTC_CHARACTERISTIC* BTA_GATTC_GetCharacteristic(uint16_t conn_id,
    290                                                              uint16_t handle) {
    291   return bta_gattc_get_characteristic(conn_id, handle);
    292 }
    293 
    294 /*******************************************************************************
    295  *
    296  * Function         BTA_GATTC_GetDescriptor
    297  *
    298  * Description      This function is called to find the characteristic on the
    299  *                  given server.
    300  *
    301  * Parameters       conn_id - connection ID which identify the server.
    302  *                  handle - descriptor handle
    303  *
    304  * Returns          returns pointer to tBTA_GATTC_DESCRIPTOR or NULL.
    305  *
    306  ******************************************************************************/
    307 const tBTA_GATTC_DESCRIPTOR* BTA_GATTC_GetDescriptor(uint16_t conn_id,
    308                                                      uint16_t handle) {
    309   return bta_gattc_get_descriptor(conn_id, handle);
    310 }
    311 
    312 /* Return characteristic that owns descriptor with handle equal to |handle|, or
    313  * NULL */
    314 const tBTA_GATTC_CHARACTERISTIC* BTA_GATTC_GetOwningCharacteristic(
    315     uint16_t conn_id, uint16_t handle) {
    316   return bta_gattc_get_owning_characteristic(conn_id, handle);
    317 }
    318 
    319 /* Return service that owns descriptor or characteristic with handle equal to
    320  * |handle|, or NULL */
    321 const tBTA_GATTC_SERVICE* BTA_GATTC_GetOwningService(uint16_t conn_id,
    322                                                      uint16_t handle) {
    323   return bta_gattc_get_service_for_handle(conn_id, handle);
    324 }
    325 
    326 /*******************************************************************************
    327  *
    328  * Function         BTA_GATTC_GetGattDb
    329  *
    330  * Description      This function is called to get the GATT database.
    331  *
    332  * Parameters       conn_id: connection ID which identify the server.
    333  *                  db: output parameter which will contain the GATT database
    334  *                      copy. Caller is responsible for freeing it.
    335  *                  count: number of elements in database.
    336  *
    337  ******************************************************************************/
    338 void BTA_GATTC_GetGattDb(uint16_t conn_id, uint16_t start_handle,
    339                          uint16_t end_handle, btgatt_db_element_t** db,
    340                          int* count) {
    341   bta_gattc_get_gatt_db(conn_id, start_handle, end_handle, db, count);
    342 }
    343 
    344 /*******************************************************************************
    345  *
    346  * Function         BTA_GATTC_ReadCharacteristic
    347  *
    348  * Description      This function is called to read a characteristics value
    349  *
    350  * Parameters       conn_id - connection ID.
    351  *                  handle - characteritic handle to read.
    352  *
    353  * Returns          None
    354  *
    355  ******************************************************************************/
    356 void BTA_GATTC_ReadCharacteristic(uint16_t conn_id, uint16_t handle,
    357                                   tGATT_AUTH_REQ auth_req,
    358                                   GATT_READ_OP_CB callback, void* cb_data) {
    359   tBTA_GATTC_API_READ* p_buf =
    360       (tBTA_GATTC_API_READ*)osi_calloc(sizeof(tBTA_GATTC_API_READ));
    361 
    362   p_buf->hdr.event = BTA_GATTC_API_READ_EVT;
    363   p_buf->hdr.layer_specific = conn_id;
    364   p_buf->auth_req = auth_req;
    365   p_buf->handle = handle;
    366   p_buf->read_cb = callback;
    367   p_buf->read_cb_data = cb_data;
    368 
    369   bta_sys_sendmsg(p_buf);
    370 }
    371 
    372 /**
    373  * This function is called to read a value of characteristic with uuid equal to
    374  * |uuid|
    375  */
    376 void BTA_GATTC_ReadUsingCharUuid(uint16_t conn_id, const Uuid& uuid,
    377                                  uint16_t s_handle, uint16_t e_handle,
    378                                  tGATT_AUTH_REQ auth_req,
    379                                  GATT_READ_OP_CB callback, void* cb_data) {
    380   tBTA_GATTC_API_READ* p_buf =
    381       (tBTA_GATTC_API_READ*)osi_calloc(sizeof(tBTA_GATTC_API_READ));
    382 
    383   p_buf->hdr.event = BTA_GATTC_API_READ_EVT;
    384   p_buf->hdr.layer_specific = conn_id;
    385   p_buf->auth_req = auth_req;
    386   p_buf->handle = 0;
    387   p_buf->uuid = uuid;
    388   p_buf->s_handle = s_handle;
    389   p_buf->e_handle = e_handle;
    390   p_buf->read_cb = callback;
    391   p_buf->read_cb_data = cb_data;
    392 
    393   bta_sys_sendmsg(p_buf);
    394 }
    395 
    396 /*******************************************************************************
    397  *
    398  * Function         BTA_GATTC_ReadCharDescr
    399  *
    400  * Description      This function is called to read a descriptor value.
    401  *
    402  * Parameters       conn_id - connection ID.
    403  *                  handle - descriptor handle to read.
    404  *
    405  * Returns          None
    406  *
    407  ******************************************************************************/
    408 void BTA_GATTC_ReadCharDescr(uint16_t conn_id, uint16_t handle,
    409                              tGATT_AUTH_REQ auth_req, GATT_READ_OP_CB callback,
    410                              void* cb_data) {
    411   tBTA_GATTC_API_READ* p_buf =
    412       (tBTA_GATTC_API_READ*)osi_calloc(sizeof(tBTA_GATTC_API_READ));
    413 
    414   p_buf->hdr.event = BTA_GATTC_API_READ_EVT;
    415   p_buf->hdr.layer_specific = conn_id;
    416   p_buf->auth_req = auth_req;
    417   p_buf->handle = handle;
    418   p_buf->read_cb = callback;
    419   p_buf->read_cb_data = cb_data;
    420 
    421   bta_sys_sendmsg(p_buf);
    422 }
    423 
    424 /*******************************************************************************
    425  *
    426  * Function         BTA_GATTC_ReadMultiple
    427  *
    428  * Description      This function is called to read multiple characteristic or
    429  *                  characteristic descriptors.
    430  *
    431  * Parameters       conn_id - connectino ID.
    432  *                    p_read_multi - pointer to the read multiple parameter.
    433  *
    434  * Returns          None
    435  *
    436  ******************************************************************************/
    437 void BTA_GATTC_ReadMultiple(uint16_t conn_id, tBTA_GATTC_MULTI* p_read_multi,
    438                             tGATT_AUTH_REQ auth_req) {
    439   tBTA_GATTC_API_READ_MULTI* p_buf =
    440       (tBTA_GATTC_API_READ_MULTI*)osi_calloc(sizeof(tBTA_GATTC_API_READ_MULTI));
    441 
    442   p_buf->hdr.event = BTA_GATTC_API_READ_MULTI_EVT;
    443   p_buf->hdr.layer_specific = conn_id;
    444   p_buf->auth_req = auth_req;
    445   p_buf->num_attr = p_read_multi->num_attr;
    446 
    447   if (p_buf->num_attr > 0)
    448     memcpy(p_buf->handles, p_read_multi->handles,
    449            sizeof(uint16_t) * p_read_multi->num_attr);
    450 
    451   bta_sys_sendmsg(p_buf);
    452 }
    453 
    454 /*******************************************************************************
    455  *
    456  * Function         BTA_GATTC_WriteCharValue
    457  *
    458  * Description      This function is called to write characteristic value.
    459  *
    460  * Parameters       conn_id - connection ID.
    461  *                  handle - characteristic handle to write.
    462  *                  write_type - type of write.
    463  *                  value - the value to be written.
    464  *
    465  * Returns          None
    466  *
    467  ******************************************************************************/
    468 void BTA_GATTC_WriteCharValue(uint16_t conn_id, uint16_t handle,
    469                               tGATT_WRITE_TYPE write_type,
    470                               std::vector<uint8_t> value,
    471                               tGATT_AUTH_REQ auth_req,
    472                               GATT_WRITE_OP_CB callback, void* cb_data) {
    473   tBTA_GATTC_API_WRITE* p_buf = (tBTA_GATTC_API_WRITE*)osi_calloc(
    474       sizeof(tBTA_GATTC_API_WRITE) + value.size());
    475 
    476   p_buf->hdr.event = BTA_GATTC_API_WRITE_EVT;
    477   p_buf->hdr.layer_specific = conn_id;
    478   p_buf->auth_req = auth_req;
    479   p_buf->handle = handle;
    480   p_buf->write_type = write_type;
    481   p_buf->len = value.size();
    482   p_buf->write_cb = callback;
    483   p_buf->write_cb_data = cb_data;
    484 
    485   if (value.size() > 0) {
    486     p_buf->p_value = (uint8_t*)(p_buf + 1);
    487     memcpy(p_buf->p_value, value.data(), value.size());
    488   }
    489 
    490   bta_sys_sendmsg(p_buf);
    491 }
    492 
    493 /*******************************************************************************
    494  *
    495  * Function         BTA_GATTC_WriteCharDescr
    496  *
    497  * Description      This function is called to write descriptor value.
    498  *
    499  * Parameters       conn_id - connection ID
    500  *                  handle - descriptor hadle to write.
    501  *                  value - the value to be written.
    502  *
    503  * Returns          None
    504  *
    505  ******************************************************************************/
    506 void BTA_GATTC_WriteCharDescr(uint16_t conn_id, uint16_t handle,
    507                               std::vector<uint8_t> value,
    508                               tGATT_AUTH_REQ auth_req,
    509                               GATT_WRITE_OP_CB callback, void* cb_data) {
    510   tBTA_GATTC_API_WRITE* p_buf = (tBTA_GATTC_API_WRITE*)osi_calloc(
    511       sizeof(tBTA_GATTC_API_WRITE) + value.size());
    512 
    513   p_buf->hdr.event = BTA_GATTC_API_WRITE_EVT;
    514   p_buf->hdr.layer_specific = conn_id;
    515   p_buf->auth_req = auth_req;
    516   p_buf->handle = handle;
    517   p_buf->write_type = GATT_WRITE;
    518   p_buf->write_cb = callback;
    519   p_buf->write_cb_data = cb_data;
    520 
    521   if (value.size() != 0) {
    522     p_buf->p_value = (uint8_t*)(p_buf + 1);
    523     p_buf->len = value.size();
    524     memcpy(p_buf->p_value, value.data(), value.size());
    525   }
    526 
    527   bta_sys_sendmsg(p_buf);
    528 }
    529 
    530 /*******************************************************************************
    531  *
    532  * Function         BTA_GATTC_PrepareWrite
    533  *
    534  * Description      This function is called to prepare write a characteristic
    535  *                  value.
    536  *
    537  * Parameters       conn_id - connection ID.
    538  *                  p_char_id - GATT characteritic ID of the service.
    539  *                  offset - offset of the write value.
    540  *                  value - the value to be written.
    541  *
    542  * Returns          None
    543  *
    544  ******************************************************************************/
    545 void BTA_GATTC_PrepareWrite(uint16_t conn_id, uint16_t handle, uint16_t offset,
    546                             std::vector<uint8_t> value, tGATT_AUTH_REQ auth_req,
    547                             GATT_WRITE_OP_CB callback, void* cb_data) {
    548   tBTA_GATTC_API_WRITE* p_buf = (tBTA_GATTC_API_WRITE*)osi_calloc(
    549       sizeof(tBTA_GATTC_API_WRITE) + value.size());
    550 
    551   p_buf->hdr.event = BTA_GATTC_API_WRITE_EVT;
    552   p_buf->hdr.layer_specific = conn_id;
    553   p_buf->auth_req = auth_req;
    554   p_buf->handle = handle;
    555   p_buf->write_cb = callback;
    556   p_buf->write_cb_data = cb_data;
    557 
    558   p_buf->write_type = BTA_GATTC_WRITE_PREPARE;
    559   p_buf->offset = offset;
    560   p_buf->len = value.size();
    561 
    562   if (value.size() > 0) {
    563     p_buf->p_value = (uint8_t*)(p_buf + 1);
    564     memcpy(p_buf->p_value, value.data(), value.size());
    565   }
    566 
    567   bta_sys_sendmsg(p_buf);
    568 }
    569 
    570 /*******************************************************************************
    571  *
    572  * Function         BTA_GATTC_ExecuteWrite
    573  *
    574  * Description      This function is called to execute write a prepare write
    575  *                  sequence.
    576  *
    577  * Parameters       conn_id - connection ID.
    578  *                    is_execute - execute or cancel.
    579  *
    580  * Returns          None
    581  *
    582  ******************************************************************************/
    583 void BTA_GATTC_ExecuteWrite(uint16_t conn_id, bool is_execute) {
    584   tBTA_GATTC_API_EXEC* p_buf =
    585       (tBTA_GATTC_API_EXEC*)osi_calloc(sizeof(tBTA_GATTC_API_EXEC));
    586 
    587   p_buf->hdr.event = BTA_GATTC_API_EXEC_EVT;
    588   p_buf->hdr.layer_specific = conn_id;
    589   p_buf->is_execute = is_execute;
    590 
    591   bta_sys_sendmsg(p_buf);
    592 }
    593 
    594 /*******************************************************************************
    595  *
    596  * Function         BTA_GATTC_SendIndConfirm
    597  *
    598  * Description      This function is called to send handle value confirmation.
    599  *
    600  * Parameters       conn_id - connection ID.
    601  *                    p_char_id - characteristic ID to confirm.
    602  *
    603  * Returns          None
    604  *
    605  ******************************************************************************/
    606 void BTA_GATTC_SendIndConfirm(uint16_t conn_id, uint16_t handle) {
    607   tBTA_GATTC_API_CONFIRM* p_buf =
    608       (tBTA_GATTC_API_CONFIRM*)osi_calloc(sizeof(tBTA_GATTC_API_CONFIRM));
    609 
    610   VLOG(1) << __func__ << ": conn_id=" << +conn_id << " handle=0x" << std::hex
    611           << +handle;
    612 
    613   p_buf->hdr.event = BTA_GATTC_API_CONFIRM_EVT;
    614   p_buf->hdr.layer_specific = conn_id;
    615   p_buf->handle = handle;
    616 
    617   bta_sys_sendmsg(p_buf);
    618 }
    619 
    620 /*******************************************************************************
    621  *
    622  * Function         BTA_GATTC_RegisterForNotifications
    623  *
    624  * Description      This function is called to register for notification of a
    625  *                  service.
    626  *
    627  * Parameters       client_if - client interface.
    628  *                  bda - target GATT server.
    629  *                  handle - GATT characteristic handle.
    630  *
    631  * Returns          OK if registration succeed, otherwise failed.
    632  *
    633  ******************************************************************************/
    634 tGATT_STATUS BTA_GATTC_RegisterForNotifications(tGATT_IF client_if,
    635                                                 const RawAddress& bda,
    636                                                 uint16_t handle) {
    637   tBTA_GATTC_RCB* p_clreg;
    638   tGATT_STATUS status = GATT_ILLEGAL_PARAMETER;
    639   uint8_t i;
    640 
    641   if (!handle) {
    642     LOG(ERROR) << "deregistration failed, handle is 0";
    643     return status;
    644   }
    645 
    646   p_clreg = bta_gattc_cl_get_regcb(client_if);
    647   if (p_clreg != NULL) {
    648     for (i = 0; i < BTA_GATTC_NOTIF_REG_MAX; i++) {
    649       if (p_clreg->notif_reg[i].in_use &&
    650           p_clreg->notif_reg[i].remote_bda == bda &&
    651           p_clreg->notif_reg[i].handle == handle) {
    652         LOG(WARNING) << "notification already registered";
    653         status = GATT_SUCCESS;
    654         break;
    655       }
    656     }
    657     if (status != GATT_SUCCESS) {
    658       for (i = 0; i < BTA_GATTC_NOTIF_REG_MAX; i++) {
    659         if (!p_clreg->notif_reg[i].in_use) {
    660           memset((void*)&p_clreg->notif_reg[i], 0,
    661                  sizeof(tBTA_GATTC_NOTIF_REG));
    662 
    663           p_clreg->notif_reg[i].in_use = true;
    664           p_clreg->notif_reg[i].remote_bda = bda;
    665 
    666           p_clreg->notif_reg[i].handle = handle;
    667           status = GATT_SUCCESS;
    668           break;
    669         }
    670       }
    671       if (i == BTA_GATTC_NOTIF_REG_MAX) {
    672         status = GATT_NO_RESOURCES;
    673         LOG(ERROR) << "Max Notification Reached, registration failed.";
    674       }
    675     }
    676   } else {
    677     LOG(ERROR) << "client_if=" << +client_if << " Not Registered";
    678   }
    679 
    680   return status;
    681 }
    682 
    683 /*******************************************************************************
    684  *
    685  * Function         BTA_GATTC_DeregisterForNotifications
    686  *
    687  * Description      This function is called to de-register for notification of a
    688  *                  service.
    689  *
    690  * Parameters       client_if - client interface.
    691  *                  remote_bda - target GATT server.
    692  *                  handle - GATT characteristic handle.
    693  *
    694  * Returns          OK if deregistration succeed, otherwise failed.
    695  *
    696  ******************************************************************************/
    697 tGATT_STATUS BTA_GATTC_DeregisterForNotifications(tGATT_IF client_if,
    698                                                   const RawAddress& bda,
    699                                                   uint16_t handle) {
    700   if (!handle) {
    701     LOG(ERROR) << __func__ << ": deregistration failed, handle is 0";
    702     return GATT_ILLEGAL_PARAMETER;
    703   }
    704 
    705   tBTA_GATTC_RCB* p_clreg = bta_gattc_cl_get_regcb(client_if);
    706   if (p_clreg == NULL) {
    707     LOG(ERROR) << __func__ << " client_if=" << +client_if
    708                << " not registered bd_addr=" << bda;
    709     return GATT_ILLEGAL_PARAMETER;
    710   }
    711 
    712   for (int i = 0; i < BTA_GATTC_NOTIF_REG_MAX; i++) {
    713     if (p_clreg->notif_reg[i].in_use &&
    714         p_clreg->notif_reg[i].remote_bda == bda &&
    715         p_clreg->notif_reg[i].handle == handle) {
    716       VLOG(1) << __func__ << " deregistered bd_addr=" << bda;
    717       memset(&p_clreg->notif_reg[i], 0, sizeof(tBTA_GATTC_NOTIF_REG));
    718       return GATT_SUCCESS;
    719     }
    720   }
    721 
    722   LOG(ERROR) << __func__ << " registration not found bd_addr=" << bda;
    723   return GATT_ERROR;
    724 }
    725 
    726 /*******************************************************************************
    727  *
    728  * Function         BTA_GATTC_Refresh
    729  *
    730  * Description      Refresh the server cache of the remote device
    731  *
    732  * Parameters       remote_bda: remote device BD address.
    733  *
    734  * Returns          void
    735  *
    736  ******************************************************************************/
    737 void BTA_GATTC_Refresh(const RawAddress& remote_bda) {
    738   do_in_bta_thread(FROM_HERE,
    739                    base::Bind(&bta_gattc_process_api_refresh, remote_bda));
    740 }
    741