btif_gatt_client.cc revision ee96a3c60fca590d38025925c072d264e06493c4
1ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta/******************************************************************************
2ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta *
3c4e2596a112a0dd63ac681b1226548c0f95e828cSatya Calloji *  Copyright (C) 2009-2014 Broadcom Corporation
4ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta *
5ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta *  Licensed under the Apache License, Version 2.0 (the "License");
6ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta *  you may not use this file except in compliance with the License.
7ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta *  You may obtain a copy of the License at:
8ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta *
9ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta *  http://www.apache.org/licenses/LICENSE-2.0
10ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta *
11ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta *  Unless required by applicable law or agreed to in writing, software
12ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta *  distributed under the License is distributed on an "AS IS" BASIS,
13ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta *  See the License for the specific language governing permissions and
15ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta *  limitations under the License.
16ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta *
17ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta ******************************************************************************/
18ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta
19ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta/*******************************************************************************
20ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta *
21ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta *  Filename:      btif_gatt_client.c
22ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta *
23ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta *  Description:   GATT client implementation
24ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta *
25ee96a3c60fca590d38025925c072d264e06493c4Myles Watson ******************************************************************************/
26ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta
2749120dc867c7818511b5afec461dfc97d17eef58Marie Janssen#define LOG_TAG "bt_btif_gattc"
2849120dc867c7818511b5afec461dfc97d17eef58Marie Janssen
29140645f6b8d87464c08f082bd14740eecab23f71Jakub Pawlowski#include <base/at_exit.h>
30140645f6b8d87464c08f082bd14740eecab23f71Jakub Pawlowski#include <base/bind.h>
31140645f6b8d87464c08f082bd14740eecab23f71Jakub Pawlowski#include <base/threading/thread.h>
3249a86709488e5cfd5e23759da18bf9613e15b04dMarie Janssen#include <errno.h>
33ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta#include <hardware/bluetooth.h>
34ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta#include <stdlib.h>
35ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta#include <string.h>
366ed1548be2295fc1bec14f10cd0fade4f63daa6aSatya Calloji#include "device/include/controller.h"
376ed1548be2295fc1bec14f10cd0fade4f63daa6aSatya Calloji
3895b74f252f534ec757aab1fc08e086e02b2cfe8dSharvil Nanavati#include "btcore/include/bdaddr.h"
39ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta#include "btif_common.h"
40ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta#include "btif_util.h"
41ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta
426975b4d711142b885af479721cada448952c6b41Andre Eisenbach#include <hardware/bt_gatt.h>
43db554581079863974af8e1289646f5deea6fc044Marie Janssen
44ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta#include "bta_api.h"
45ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta#include "bta_gatt_api.h"
46ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta#include "btif_config.h"
47db554581079863974af8e1289646f5deea6fc044Marie Janssen#include "btif_dm.h"
48ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta#include "btif_gatt.h"
49ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta#include "btif_gatt_util.h"
50ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta#include "btif_storage.h"
5144802768c447ab480d4227b3a852a97d923b816dSharvil Nanavati#include "osi/include/log.h"
52b203d478f4835f5e35217874a1787e1914328f7cAndre Eisenbach#include "vendor_api.h"
53b203d478f4835f5e35217874a1787e1914328f7cAndre Eisenbach
54140645f6b8d87464c08f082bd14740eecab23f71Jakub Pawlowskiusing base::Bind;
55140645f6b8d87464c08f082bd14740eecab23f71Jakub Pawlowskiusing base::Owned;
5626ecec06be48502eba1c24a6b1fdbb66eafa8032Jakub Pawlowskiusing std::vector;
57140645f6b8d87464c08f082bd14740eecab23f71Jakub Pawlowski
586bd442f543972b072ef2cbbcf2f7c91202de1045Myles Watsonextern bt_status_t do_in_jni_thread(const base::Closure& task);
5944be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowskiextern bt_status_t btif_gattc_test_command_impl(int command,
606bd442f543972b072ef2cbbcf2f7c91202de1045Myles Watson                                                btgatt_test_params_t* params);
616bd442f543972b072ef2cbbcf2f7c91202de1045Myles Watsonextern const btgatt_callbacks_t* bt_gatt_callbacks;
62140645f6b8d87464c08f082bd14740eecab23f71Jakub Pawlowski
63ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta/*******************************************************************************
646bd442f543972b072ef2cbbcf2f7c91202de1045Myles Watson *  Constants & Macros
65ee96a3c60fca590d38025925c072d264e06493c4Myles Watson ******************************************************************************/
66ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta
6740cde56d28ab9751ba76bded0b7ac5974c750a97Myles Watson#define CLI_CBACK_IN_JNI(P_CBACK, ...)                                         \
6840cde56d28ab9751ba76bded0b7ac5974c750a97Myles Watson  do {                                                                         \
6940cde56d28ab9751ba76bded0b7ac5974c750a97Myles Watson    if (bt_gatt_callbacks && bt_gatt_callbacks->client->P_CBACK) {             \
7040cde56d28ab9751ba76bded0b7ac5974c750a97Myles Watson      BTIF_TRACE_API("HAL bt_gatt_callbacks->client->%s", #P_CBACK);           \
7140cde56d28ab9751ba76bded0b7ac5974c750a97Myles Watson      do_in_jni_thread(Bind(bt_gatt_callbacks->client->P_CBACK, __VA_ARGS__)); \
7240cde56d28ab9751ba76bded0b7ac5974c750a97Myles Watson    } else {                                                                   \
7340cde56d28ab9751ba76bded0b7ac5974c750a97Myles Watson      ASSERTC(0, "Callback is NULL", 0);                                       \
7440cde56d28ab9751ba76bded0b7ac5974c750a97Myles Watson    }                                                                          \
7540cde56d28ab9751ba76bded0b7ac5974c750a97Myles Watson  } while (0)
76da5b59b5bf9cb34574c16ba784c1484055fcb1e3Jakub Pawlowski
7740cde56d28ab9751ba76bded0b7ac5974c750a97Myles Watson#define CHECK_BTGATT_INIT()                                      \
7840cde56d28ab9751ba76bded0b7ac5974c750a97Myles Watson  do {                                                           \
7940cde56d28ab9751ba76bded0b7ac5974c750a97Myles Watson    if (bt_gatt_callbacks == NULL) {                             \
8040cde56d28ab9751ba76bded0b7ac5974c750a97Myles Watson      LOG_WARN(LOG_TAG, "%s: BTGATT not initialized", __func__); \
8140cde56d28ab9751ba76bded0b7ac5974c750a97Myles Watson      return BT_STATUS_NOT_READY;                                \
8240cde56d28ab9751ba76bded0b7ac5974c750a97Myles Watson    } else {                                                     \
8340cde56d28ab9751ba76bded0b7ac5974c750a97Myles Watson      LOG_VERBOSE(LOG_TAG, "%s", __func__);                      \
8440cde56d28ab9751ba76bded0b7ac5974c750a97Myles Watson    }                                                            \
8540cde56d28ab9751ba76bded0b7ac5974c750a97Myles Watson  } while (0)
86ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta
876bd442f543972b072ef2cbbcf2f7c91202de1045Myles Watson#define BLE_RESOLVE_ADDR_MSB                                                   \
886bd442f543972b072ef2cbbcf2f7c91202de1045Myles Watson  0x40                             /* bit7, bit6 is 01 to be resolvable random \
8944be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski                                      */
9044be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski#define BLE_RESOLVE_ADDR_MASK 0xc0 /* bit 6, and bit7 */
9144be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski#define BTM_BLE_IS_RESOLVE_BDA(x) \
9263b0519c3f0d1c0cc523e0e9553c60fc68651accChih-Hung Hsieh  (((x)[0] & BLE_RESOLVE_ADDR_MASK) == BLE_RESOLVE_ADDR_MSB)
93ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta
9444be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowskinamespace {
95ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta
9644be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowskiuint8_t rssi_request_client_if;
97ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta
986bd442f543972b072ef2cbbcf2f7c91202de1045Myles Watsonvoid btif_gattc_upstreams_evt(uint16_t event, char* p_param) {
99b7f64bc45dec7f7fec74ceb04874f322b9434bbfMarie Janssen  LOG_VERBOSE(LOG_TAG, "%s: Event %d", __func__, event);
100ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta
1016bd442f543972b072ef2cbbcf2f7c91202de1045Myles Watson  tBTA_GATTC* p_data = (tBTA_GATTC*)p_param;
10244be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski  switch (event) {
10344be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski    case BTA_GATTC_REG_EVT: {
10444be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski      bt_uuid_t app_uuid;
10544be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski      bta_to_btif_uuid(&app_uuid, &p_data->reg_oper.app_uuid);
10644be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski      HAL_CBACK(bt_gatt_callbacks, client->register_client_cb,
10744be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski                p_data->reg_oper.status, p_data->reg_oper.client_if, &app_uuid);
10844be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski      break;
10944be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski    }
110ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta
11144be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski    case BTA_GATTC_DEREG_EVT:
11244be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski      break;
113ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta
11444be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski    case BTA_GATTC_EXEC_EVT: {
11544be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski      HAL_CBACK(bt_gatt_callbacks, client->execute_write_cb,
11644be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski                p_data->exec_cmpl.conn_id, p_data->exec_cmpl.status);
11744be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski      break;
11844be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski    }
119ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta
12044be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski    case BTA_GATTC_SEARCH_CMPL_EVT: {
12144be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski      HAL_CBACK(bt_gatt_callbacks, client->search_complete_cb,
12244be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski                p_data->search_cmpl.conn_id, p_data->search_cmpl.status);
12344be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski      break;
12444be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski    }
125ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta
12644be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski    case BTA_GATTC_NOTIF_EVT: {
12744be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski      btgatt_notify_params_t data;
128ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta
12944be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski      bdcpy(data.bda.address, p_data->notify.bda);
13044be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski      memcpy(data.value, p_data->notify.value, p_data->notify.len);
131ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta
13244be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski      data.handle = p_data->notify.handle;
13344be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski      data.is_notify = p_data->notify.is_notify;
13444be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski      data.len = p_data->notify.len;
135ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta
13644be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski      HAL_CBACK(bt_gatt_callbacks, client->notify_cb, p_data->notify.conn_id,
13744be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski                &data);
138ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta
139b7f64bc45dec7f7fec74ceb04874f322b9434bbfMarie Janssen      if (p_data->notify.is_notify == false)
14044be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski        BTA_GATTC_SendIndConfirm(p_data->notify.conn_id, p_data->notify.handle);
1419dc998503590163068845827d951fc2f05a17bdaJakub Pawlowski
14244be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski      break;
14344be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski    }
144ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta
14544be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski    case BTA_GATTC_OPEN_EVT: {
14644be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski      bt_bdaddr_t bda;
14744be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski      bdcpy(bda.address, p_data->open.remote_bda);
148ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta
14944be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski      HAL_CBACK(bt_gatt_callbacks, client->open_cb, p_data->open.conn_id,
15044be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski                p_data->open.status, p_data->open.client_if, &bda);
1516975b4d711142b885af479721cada448952c6b41Andre Eisenbach
15244be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski      if (GATT_DEF_BLE_MTU_SIZE != p_data->open.mtu && p_data->open.mtu) {
15344be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski        HAL_CBACK(bt_gatt_callbacks, client->configure_mtu_cb,
15444be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski                  p_data->open.conn_id, p_data->open.status, p_data->open.mtu);
15544be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski      }
1569426d530e9bbc5d3ffae55515388d49185c61325Andre Eisenbach
15744be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski      if (p_data->open.status == BTA_GATT_OK)
15844be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski        btif_gatt_check_encrypted_link(p_data->open.remote_bda,
15944be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski                                       p_data->open.transport);
16044be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski      break;
16144be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski    }
162ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta
16344be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski    case BTA_GATTC_CLOSE_EVT: {
16444be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski      bt_bdaddr_t bda;
16544be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski      bdcpy(bda.address, p_data->close.remote_bda);
16644be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski      HAL_CBACK(bt_gatt_callbacks, client->close_cb, p_data->close.conn_id,
16744be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski                p_data->status, p_data->close.client_if, &bda);
16844be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski      break;
16944be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski    }
170ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta
17144be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski    case BTA_GATTC_ACL_EVT:
17244be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski      LOG_DEBUG(LOG_TAG, "BTA_GATTC_ACL_EVT: status = %d", p_data->status);
17344be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski      /* Ignore for now */
17444be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski      break;
1759426d530e9bbc5d3ffae55515388d49185c61325Andre Eisenbach
17644be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski    case BTA_GATTC_CANCEL_OPEN_EVT:
17744be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski      break;
17817b04bd498405f2bb109a85562ebbdcb6bb06e95Andre Eisenbach
17944be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski    case BTA_GATTC_CFG_MTU_EVT: {
18044be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski      HAL_CBACK(bt_gatt_callbacks, client->configure_mtu_cb,
18144be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski                p_data->cfg_mtu.conn_id, p_data->cfg_mtu.status,
18244be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski                p_data->cfg_mtu.mtu);
18344be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski      break;
184ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta    }
1858444d1e649864c28b0c124bc2d16f7c153f01f14Andre Eisenbach
18644be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski    case BTA_GATTC_CONGEST_EVT:
18744be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski      HAL_CBACK(bt_gatt_callbacks, client->congestion_cb,
18844be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski                p_data->congest.conn_id, p_data->congest.congested);
18944be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski      break;
19044be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski
19144be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski    default:
192b7f64bc45dec7f7fec74ceb04874f322b9434bbfMarie Janssen      LOG_ERROR(LOG_TAG, "%s: Unhandled event (%d)!", __func__, event);
19344be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski      break;
19444be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski  }
195ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta}
196ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta
1976bd442f543972b072ef2cbbcf2f7c91202de1045Myles Watsonvoid bta_gattc_cback(tBTA_GATTC_EVT event, tBTA_GATTC* p_data) {
19824ea892320ca85c7f6cdd77343da50818fe29002Jakub Pawlowski  bt_status_t status =
19924ea892320ca85c7f6cdd77343da50818fe29002Jakub Pawlowski      btif_transfer_context(btif_gattc_upstreams_evt, (uint16_t)event,
2006bd442f543972b072ef2cbbcf2f7c91202de1045Myles Watson                            (char*)p_data, sizeof(tBTA_GATTC), NULL);
20144be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski  ASSERTC(status == BT_STATUS_SUCCESS, "Context transfer failed!", status);
202ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta}
203ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta
2046bd442f543972b072ef2cbbcf2f7c91202de1045Myles Watsonvoid btm_read_rssi_cb(tBTM_RSSI_RESULTS* p_result) {
2056bd442f543972b072ef2cbbcf2f7c91202de1045Myles Watson  if (!p_result) return;
2067294d958dcb610d7828f06c3a1cde933127694d3Jakub Pawlowski
2076bd442f543972b072ef2cbbcf2f7c91202de1045Myles Watson  bt_bdaddr_t* addr = new bt_bdaddr_t;
208c171ac407cfc6d30a11af6b12ead39688eb897f7Jakub Pawlowski  bdcpy(addr->address, p_result->rem_bda);
209c171ac407cfc6d30a11af6b12ead39688eb897f7Jakub Pawlowski  CLI_CBACK_IN_JNI(read_remote_rssi_cb, rssi_request_client_if,
210c171ac407cfc6d30a11af6b12ead39688eb897f7Jakub Pawlowski                   base::Owned(addr), p_result->rssi, p_result->status);
211ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta}
212ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta
213ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta/*******************************************************************************
2146bd442f543972b072ef2cbbcf2f7c91202de1045Myles Watson *  Client API Functions
215ee96a3c60fca590d38025925c072d264e06493c4Myles Watson ******************************************************************************/
216ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta
21744be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowskivoid btif_gattc_register_app_impl(tBT_UUID uuid) {
218c8b5185a5d4c4c9c15f3ec5df4acd37c1d46f1f1Jakub Pawlowski  BTA_GATTC_AppRegister(&uuid, bta_gattc_cback);
219ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta}
220ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta
2216bd442f543972b072ef2cbbcf2f7c91202de1045Myles Watsonbt_status_t btif_gattc_register_app(bt_uuid_t* uuid) {
222c8b5185a5d4c4c9c15f3ec5df4acd37c1d46f1f1Jakub Pawlowski  CHECK_BTGATT_INIT();
223c8b5185a5d4c4c9c15f3ec5df4acd37c1d46f1f1Jakub Pawlowski
224c8b5185a5d4c4c9c15f3ec5df4acd37c1d46f1f1Jakub Pawlowski  tBT_UUID bt_uuid;
225c8b5185a5d4c4c9c15f3ec5df4acd37c1d46f1f1Jakub Pawlowski  btif_to_bta_uuid(&bt_uuid, uuid);
226c8b5185a5d4c4c9c15f3ec5df4acd37c1d46f1f1Jakub Pawlowski  return do_in_jni_thread(Bind(&btif_gattc_register_app_impl, bt_uuid));
227c8b5185a5d4c4c9c15f3ec5df4acd37c1d46f1f1Jakub Pawlowski}
228c8b5185a5d4c4c9c15f3ec5df4acd37c1d46f1f1Jakub Pawlowski
22944be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowskivoid btif_gattc_unregister_app_impl(int client_if) {
230c8b5185a5d4c4c9c15f3ec5df4acd37c1d46f1f1Jakub Pawlowski  BTA_GATTC_AppDeregister(client_if);
231c8b5185a5d4c4c9c15f3ec5df4acd37c1d46f1f1Jakub Pawlowski}
232c8b5185a5d4c4c9c15f3ec5df4acd37c1d46f1f1Jakub Pawlowski
23344be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowskibt_status_t btif_gattc_unregister_app(int client_if) {
234c8b5185a5d4c4c9c15f3ec5df4acd37c1d46f1f1Jakub Pawlowski  CHECK_BTGATT_INIT();
235c8b5185a5d4c4c9c15f3ec5df4acd37c1d46f1f1Jakub Pawlowski  return do_in_jni_thread(Bind(&btif_gattc_unregister_app_impl, client_if));
236ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta}
237ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta
23844be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowskivoid btif_gattc_open_impl(int client_if, BD_ADDR address, bool is_direct,
23944be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski                          int transport_p) {
24026e51cb18509e7e04755b4fa4250c60f105ab9d2Jakub Pawlowski  // Ensure device is in inquiry database
24126e51cb18509e7e04755b4fa4250c60f105ab9d2Jakub Pawlowski  int addr_type = 0;
24226e51cb18509e7e04755b4fa4250c60f105ab9d2Jakub Pawlowski  int device_type = 0;
24326e51cb18509e7e04755b4fa4250c60f105ab9d2Jakub Pawlowski  tBTA_GATT_TRANSPORT transport = (tBTA_GATT_TRANSPORT)BTA_GATT_TRANSPORT_LE;
24426e51cb18509e7e04755b4fa4250c60f105ab9d2Jakub Pawlowski
24526e51cb18509e7e04755b4fa4250c60f105ab9d2Jakub Pawlowski  if (btif_get_address_type(address, &addr_type) &&
24626e51cb18509e7e04755b4fa4250c60f105ab9d2Jakub Pawlowski      btif_get_device_type(address, &device_type) &&
24726e51cb18509e7e04755b4fa4250c60f105ab9d2Jakub Pawlowski      device_type != BT_DEVICE_TYPE_BREDR) {
24826e51cb18509e7e04755b4fa4250c60f105ab9d2Jakub Pawlowski    BTA_DmAddBleDevice(address, addr_type, device_type);
24926e51cb18509e7e04755b4fa4250c60f105ab9d2Jakub Pawlowski  }
25026e51cb18509e7e04755b4fa4250c60f105ab9d2Jakub Pawlowski
25126e51cb18509e7e04755b4fa4250c60f105ab9d2Jakub Pawlowski  // Check for background connections
25226e51cb18509e7e04755b4fa4250c60f105ab9d2Jakub Pawlowski  if (!is_direct) {
25326e51cb18509e7e04755b4fa4250c60f105ab9d2Jakub Pawlowski    // Check for privacy 1.0 and 1.1 controller and do not start background
25426e51cb18509e7e04755b4fa4250c60f105ab9d2Jakub Pawlowski    // connection if RPA offloading is not supported, since it will not
25526e51cb18509e7e04755b4fa4250c60f105ab9d2Jakub Pawlowski    // connect after change of random address
25626e51cb18509e7e04755b4fa4250c60f105ab9d2Jakub Pawlowski    if (!controller_get_interface()->supports_ble_privacy() &&
25744be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski        (addr_type == BLE_ADDR_RANDOM) && BTM_BLE_IS_RESOLVE_BDA(address)) {
25826e51cb18509e7e04755b4fa4250c60f105ab9d2Jakub Pawlowski      tBTM_BLE_VSC_CB vnd_capabilities;
25926e51cb18509e7e04755b4fa4250c60f105ab9d2Jakub Pawlowski      BTM_BleGetVendorCapabilities(&vnd_capabilities);
26026e51cb18509e7e04755b4fa4250c60f105ab9d2Jakub Pawlowski      if (!vnd_capabilities.rpa_offloading) {
26126e51cb18509e7e04755b4fa4250c60f105ab9d2Jakub Pawlowski        HAL_CBACK(bt_gatt_callbacks, client->open_cb, 0, BT_STATUS_UNSUPPORTED,
2626bd442f543972b072ef2cbbcf2f7c91202de1045Myles Watson                  client_if, (bt_bdaddr_t*)&address);
26326e51cb18509e7e04755b4fa4250c60f105ab9d2Jakub Pawlowski        return;
26426e51cb18509e7e04755b4fa4250c60f105ab9d2Jakub Pawlowski      }
26526e51cb18509e7e04755b4fa4250c60f105ab9d2Jakub Pawlowski    }
26626e51cb18509e7e04755b4fa4250c60f105ab9d2Jakub Pawlowski    BTA_DmBleSetBgConnType(BTM_BLE_CONN_AUTO, NULL);
26726e51cb18509e7e04755b4fa4250c60f105ab9d2Jakub Pawlowski  }
26826e51cb18509e7e04755b4fa4250c60f105ab9d2Jakub Pawlowski
26926e51cb18509e7e04755b4fa4250c60f105ab9d2Jakub Pawlowski  // Determine transport
27026e51cb18509e7e04755b4fa4250c60f105ab9d2Jakub Pawlowski  if (transport_p != GATT_TRANSPORT_AUTO) {
27126e51cb18509e7e04755b4fa4250c60f105ab9d2Jakub Pawlowski    transport = transport_p;
27226e51cb18509e7e04755b4fa4250c60f105ab9d2Jakub Pawlowski  } else {
27326e51cb18509e7e04755b4fa4250c60f105ab9d2Jakub Pawlowski    switch (device_type) {
27426e51cb18509e7e04755b4fa4250c60f105ab9d2Jakub Pawlowski      case BT_DEVICE_TYPE_BREDR:
27526e51cb18509e7e04755b4fa4250c60f105ab9d2Jakub Pawlowski        transport = BTA_GATT_TRANSPORT_BR_EDR;
27626e51cb18509e7e04755b4fa4250c60f105ab9d2Jakub Pawlowski        break;
27726e51cb18509e7e04755b4fa4250c60f105ab9d2Jakub Pawlowski
27826e51cb18509e7e04755b4fa4250c60f105ab9d2Jakub Pawlowski      case BT_DEVICE_TYPE_BLE:
27926e51cb18509e7e04755b4fa4250c60f105ab9d2Jakub Pawlowski        transport = BTA_GATT_TRANSPORT_LE;
28026e51cb18509e7e04755b4fa4250c60f105ab9d2Jakub Pawlowski        break;
28126e51cb18509e7e04755b4fa4250c60f105ab9d2Jakub Pawlowski
28226e51cb18509e7e04755b4fa4250c60f105ab9d2Jakub Pawlowski      case BT_DEVICE_TYPE_DUMO:
28326e51cb18509e7e04755b4fa4250c60f105ab9d2Jakub Pawlowski        if (transport == GATT_TRANSPORT_LE)
28426e51cb18509e7e04755b4fa4250c60f105ab9d2Jakub Pawlowski          transport = BTA_GATT_TRANSPORT_LE;
28526e51cb18509e7e04755b4fa4250c60f105ab9d2Jakub Pawlowski        else
28626e51cb18509e7e04755b4fa4250c60f105ab9d2Jakub Pawlowski          transport = BTA_GATT_TRANSPORT_BR_EDR;
28726e51cb18509e7e04755b4fa4250c60f105ab9d2Jakub Pawlowski        break;
28826e51cb18509e7e04755b4fa4250c60f105ab9d2Jakub Pawlowski    }
28926e51cb18509e7e04755b4fa4250c60f105ab9d2Jakub Pawlowski  }
29026e51cb18509e7e04755b4fa4250c60f105ab9d2Jakub Pawlowski
29126e51cb18509e7e04755b4fa4250c60f105ab9d2Jakub Pawlowski  // Connect!
29226e51cb18509e7e04755b4fa4250c60f105ab9d2Jakub Pawlowski  BTIF_TRACE_DEBUG("%s Transport=%d, device type=%d", __func__, transport,
29326e51cb18509e7e04755b4fa4250c60f105ab9d2Jakub Pawlowski                   device_type);
29426e51cb18509e7e04755b4fa4250c60f105ab9d2Jakub Pawlowski  BTA_GATTC_Open(client_if, address, is_direct, transport);
29526e51cb18509e7e04755b4fa4250c60f105ab9d2Jakub Pawlowski}
29626e51cb18509e7e04755b4fa4250c60f105ab9d2Jakub Pawlowski
2976bd442f543972b072ef2cbbcf2f7c91202de1045Myles Watsonbt_status_t btif_gattc_open(int client_if, const bt_bdaddr_t* bd_addr,
29844be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski                            bool is_direct, int transport) {
29926e51cb18509e7e04755b4fa4250c60f105ab9d2Jakub Pawlowski  CHECK_BTGATT_INIT();
30026e51cb18509e7e04755b4fa4250c60f105ab9d2Jakub Pawlowski  // Closure will own this value and free it.
3016bd442f543972b072ef2cbbcf2f7c91202de1045Myles Watson  uint8_t* address = new BD_ADDR;
30226e51cb18509e7e04755b4fa4250c60f105ab9d2Jakub Pawlowski  bdcpy(address, bd_addr->address);
30344be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski  return do_in_jni_thread(Bind(&btif_gattc_open_impl, client_if,
30444be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski                               base::Owned(address), is_direct, transport));
305ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta}
306ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta
30744be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowskivoid btif_gattc_close_impl(int client_if, BD_ADDR address, int conn_id) {
308b454953bb6620cbafba86351b91d58b47dda107dJakub Pawlowski  // Disconnect established connections
309b454953bb6620cbafba86351b91d58b47dda107dJakub Pawlowski  if (conn_id != 0)
310b454953bb6620cbafba86351b91d58b47dda107dJakub Pawlowski    BTA_GATTC_Close(conn_id);
311b454953bb6620cbafba86351b91d58b47dda107dJakub Pawlowski  else
312b7f64bc45dec7f7fec74ceb04874f322b9434bbfMarie Janssen    BTA_GATTC_CancelOpen(client_if, address, true);
313b454953bb6620cbafba86351b91d58b47dda107dJakub Pawlowski
314b454953bb6620cbafba86351b91d58b47dda107dJakub Pawlowski  // Cancel pending background connections (remove from whitelist)
315b7f64bc45dec7f7fec74ceb04874f322b9434bbfMarie Janssen  BTA_GATTC_CancelOpen(client_if, address, false);
316b454953bb6620cbafba86351b91d58b47dda107dJakub Pawlowski}
317b454953bb6620cbafba86351b91d58b47dda107dJakub Pawlowski
3186bd442f543972b072ef2cbbcf2f7c91202de1045Myles Watsonbt_status_t btif_gattc_close(int client_if, const bt_bdaddr_t* bd_addr,
31944be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski                             int conn_id) {
320b454953bb6620cbafba86351b91d58b47dda107dJakub Pawlowski  CHECK_BTGATT_INIT();
321b454953bb6620cbafba86351b91d58b47dda107dJakub Pawlowski  // Closure will own this value and free it.
3226bd442f543972b072ef2cbbcf2f7c91202de1045Myles Watson  uint8_t* address = new BD_ADDR;
323b454953bb6620cbafba86351b91d58b47dda107dJakub Pawlowski  bdcpy(address, bd_addr->address);
324b454953bb6620cbafba86351b91d58b47dda107dJakub Pawlowski  return do_in_jni_thread(
325b454953bb6620cbafba86351b91d58b47dda107dJakub Pawlowski      Bind(&btif_gattc_close_impl, client_if, base::Owned(address), conn_id));
326ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta}
327ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta
3286bd442f543972b072ef2cbbcf2f7c91202de1045Myles Watsonbt_status_t btif_gattc_refresh(int client_if, const bt_bdaddr_t* bd_addr) {
329140645f6b8d87464c08f082bd14740eecab23f71Jakub Pawlowski  CHECK_BTGATT_INIT();
330140645f6b8d87464c08f082bd14740eecab23f71Jakub Pawlowski  // Closure will own this value and free it.
3316bd442f543972b072ef2cbbcf2f7c91202de1045Myles Watson  uint8_t* address = new BD_ADDR;
332140645f6b8d87464c08f082bd14740eecab23f71Jakub Pawlowski  bdcpy(address, bd_addr->address);
333140645f6b8d87464c08f082bd14740eecab23f71Jakub Pawlowski  return do_in_jni_thread(Bind(&BTA_GATTC_Refresh, base::Owned(address)));
334ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta}
335ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta
3366bd442f543972b072ef2cbbcf2f7c91202de1045Myles Watsonbt_status_t btif_gattc_search_service(int conn_id, bt_uuid_t* filter_uuid) {
337d05e50e7df462c928041bf565821d77fb493bc28Jakub Pawlowski  CHECK_BTGATT_INIT();
338d05e50e7df462c928041bf565821d77fb493bc28Jakub Pawlowski
339d05e50e7df462c928041bf565821d77fb493bc28Jakub Pawlowski  if (filter_uuid) {
3406bd442f543972b072ef2cbbcf2f7c91202de1045Myles Watson    tBT_UUID* uuid = new tBT_UUID;
341d05e50e7df462c928041bf565821d77fb493bc28Jakub Pawlowski    btif_to_bta_uuid(uuid, filter_uuid);
342d05e50e7df462c928041bf565821d77fb493bc28Jakub Pawlowski    return do_in_jni_thread(
343d05e50e7df462c928041bf565821d77fb493bc28Jakub Pawlowski        Bind(&BTA_GATTC_ServiceSearchRequest, conn_id, base::Owned(uuid)));
344d05e50e7df462c928041bf565821d77fb493bc28Jakub Pawlowski  } else {
345d05e50e7df462c928041bf565821d77fb493bc28Jakub Pawlowski    return do_in_jni_thread(
346d05e50e7df462c928041bf565821d77fb493bc28Jakub Pawlowski        Bind(&BTA_GATTC_ServiceSearchRequest, conn_id, nullptr));
347d05e50e7df462c928041bf565821d77fb493bc28Jakub Pawlowski  }
348ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta}
349ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta
35061a98132c39d3181a0ea23988ac36fbfef8194b4Jakub Pawlowskivoid btif_gattc_get_gatt_db_impl(int conn_id) {
3516bd442f543972b072ef2cbbcf2f7c91202de1045Myles Watson  btgatt_db_element_t* db = NULL;
35261a98132c39d3181a0ea23988ac36fbfef8194b4Jakub Pawlowski  int count = 0;
35361a98132c39d3181a0ea23988ac36fbfef8194b4Jakub Pawlowski  BTA_GATTC_GetGattDb(conn_id, 0x0000, 0xFFFF, &db, &count);
354533fdbf46fb623aefbe4d0107b5cccd41aec097aJakub Pawlowski
35561a98132c39d3181a0ea23988ac36fbfef8194b4Jakub Pawlowski  HAL_CBACK(bt_gatt_callbacks, client->get_gatt_db_cb, conn_id, db, count);
35661a98132c39d3181a0ea23988ac36fbfef8194b4Jakub Pawlowski  osi_free(db);
35761a98132c39d3181a0ea23988ac36fbfef8194b4Jakub Pawlowski}
35861a98132c39d3181a0ea23988ac36fbfef8194b4Jakub Pawlowski
35944be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowskibt_status_t btif_gattc_get_gatt_db(int conn_id) {
36061a98132c39d3181a0ea23988ac36fbfef8194b4Jakub Pawlowski  CHECK_BTGATT_INIT();
36161a98132c39d3181a0ea23988ac36fbfef8194b4Jakub Pawlowski  return do_in_jni_thread(Bind(&btif_gattc_get_gatt_db_impl, conn_id));
362533fdbf46fb623aefbe4d0107b5cccd41aec097aJakub Pawlowski}
363533fdbf46fb623aefbe4d0107b5cccd41aec097aJakub Pawlowski
36424ea892320ca85c7f6cdd77343da50818fe29002Jakub Pawlowskivoid read_char_cb(uint16_t conn_id, tGATT_STATUS status, uint16_t handle,
3656bd442f543972b072ef2cbbcf2f7c91202de1045Myles Watson                  uint16_t len, uint8_t* value, void* data) {
3666bd442f543972b072ef2cbbcf2f7c91202de1045Myles Watson  btgatt_read_params_t* params = new btgatt_read_params_t;
36724ea892320ca85c7f6cdd77343da50818fe29002Jakub Pawlowski  params->value_type = 0x00 /* GATTC_READ_VALUE_TYPE_VALUE */;
36824ea892320ca85c7f6cdd77343da50818fe29002Jakub Pawlowski  params->status = status;
36924ea892320ca85c7f6cdd77343da50818fe29002Jakub Pawlowski  params->handle = handle;
37024ea892320ca85c7f6cdd77343da50818fe29002Jakub Pawlowski  params->value.len = len;
37124ea892320ca85c7f6cdd77343da50818fe29002Jakub Pawlowski  assert(len <= BTGATT_MAX_ATTR_LEN);
37224ea892320ca85c7f6cdd77343da50818fe29002Jakub Pawlowski  if (len > 0) memcpy(params->value.value, value, len);
37324ea892320ca85c7f6cdd77343da50818fe29002Jakub Pawlowski
37424ea892320ca85c7f6cdd77343da50818fe29002Jakub Pawlowski  CLI_CBACK_IN_JNI(read_characteristic_cb, conn_id, status,
37524ea892320ca85c7f6cdd77343da50818fe29002Jakub Pawlowski                   base::Owned(params));
37624ea892320ca85c7f6cdd77343da50818fe29002Jakub Pawlowski}
37724ea892320ca85c7f6cdd77343da50818fe29002Jakub Pawlowski
37844be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowskibt_status_t btif_gattc_read_char(int conn_id, uint16_t handle, int auth_req) {
37961a112e488a7686e5cc9680434c42895ef9db878Jakub Pawlowski  CHECK_BTGATT_INIT();
38024ea892320ca85c7f6cdd77343da50818fe29002Jakub Pawlowski  return do_in_jni_thread(Bind(&BTA_GATTC_ReadCharacteristic, conn_id, handle,
38124ea892320ca85c7f6cdd77343da50818fe29002Jakub Pawlowski                               auth_req, read_char_cb, nullptr));
38224ea892320ca85c7f6cdd77343da50818fe29002Jakub Pawlowski}
38324ea892320ca85c7f6cdd77343da50818fe29002Jakub Pawlowski
38424ea892320ca85c7f6cdd77343da50818fe29002Jakub Pawlowskivoid read_desc_cb(uint16_t conn_id, tGATT_STATUS status, uint16_t handle,
3856bd442f543972b072ef2cbbcf2f7c91202de1045Myles Watson                  uint16_t len, uint8_t* value, void* data) {
3866bd442f543972b072ef2cbbcf2f7c91202de1045Myles Watson  btgatt_read_params_t* params = new btgatt_read_params_t;
38724ea892320ca85c7f6cdd77343da50818fe29002Jakub Pawlowski  params->value_type = 0x00 /* GATTC_READ_VALUE_TYPE_VALUE */;
38824ea892320ca85c7f6cdd77343da50818fe29002Jakub Pawlowski  params->status = status;
38924ea892320ca85c7f6cdd77343da50818fe29002Jakub Pawlowski  params->handle = handle;
39024ea892320ca85c7f6cdd77343da50818fe29002Jakub Pawlowski  params->value.len = len;
39124ea892320ca85c7f6cdd77343da50818fe29002Jakub Pawlowski  assert(len <= BTGATT_MAX_ATTR_LEN);
39224ea892320ca85c7f6cdd77343da50818fe29002Jakub Pawlowski  if (len > 0) memcpy(params->value.value, value, len);
39324ea892320ca85c7f6cdd77343da50818fe29002Jakub Pawlowski
39424ea892320ca85c7f6cdd77343da50818fe29002Jakub Pawlowski  CLI_CBACK_IN_JNI(read_descriptor_cb, conn_id, status, base::Owned(params));
395ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta}
396ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta
39744be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowskibt_status_t btif_gattc_read_char_descr(int conn_id, uint16_t handle,
39844be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski                                       int auth_req) {
39961a112e488a7686e5cc9680434c42895ef9db878Jakub Pawlowski  CHECK_BTGATT_INIT();
40024ea892320ca85c7f6cdd77343da50818fe29002Jakub Pawlowski  return do_in_jni_thread(Bind(&BTA_GATTC_ReadCharDescr, conn_id, handle,
40124ea892320ca85c7f6cdd77343da50818fe29002Jakub Pawlowski                               auth_req, read_desc_cb, nullptr));
40224ea892320ca85c7f6cdd77343da50818fe29002Jakub Pawlowski}
40324ea892320ca85c7f6cdd77343da50818fe29002Jakub Pawlowski
40424ea892320ca85c7f6cdd77343da50818fe29002Jakub Pawlowskivoid write_char_cb(uint16_t conn_id, tGATT_STATUS status, uint16_t handle,
4056bd442f543972b072ef2cbbcf2f7c91202de1045Myles Watson                   void* data) {
40624ea892320ca85c7f6cdd77343da50818fe29002Jakub Pawlowski  CLI_CBACK_IN_JNI(write_characteristic_cb, conn_id, status, handle);
407ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta}
408ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta
40944be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowskibt_status_t btif_gattc_write_char(int conn_id, uint16_t handle, int write_type,
410ec79baaaed83529dae14d492ea2f73aedee9e4d0Jakub Pawlowski                                  int auth_req, vector<uint8_t> value) {
41139a33612889a562da5c919f14df24707ba3e6007Jakub Pawlowski  CHECK_BTGATT_INIT();
41239a33612889a562da5c919f14df24707ba3e6007Jakub Pawlowski
4136bd442f543972b072ef2cbbcf2f7c91202de1045Myles Watson  if (value.size() > BTGATT_MAX_ATTR_LEN) value.resize(BTGATT_MAX_ATTR_LEN);
41439a33612889a562da5c919f14df24707ba3e6007Jakub Pawlowski
41539a33612889a562da5c919f14df24707ba3e6007Jakub Pawlowski  return do_in_jni_thread(Bind(&BTA_GATTC_WriteCharValue, conn_id, handle,
41624ea892320ca85c7f6cdd77343da50818fe29002Jakub Pawlowski                               write_type, std::move(value), auth_req,
41724ea892320ca85c7f6cdd77343da50818fe29002Jakub Pawlowski                               write_char_cb, nullptr));
41824ea892320ca85c7f6cdd77343da50818fe29002Jakub Pawlowski}
41924ea892320ca85c7f6cdd77343da50818fe29002Jakub Pawlowski
42024ea892320ca85c7f6cdd77343da50818fe29002Jakub Pawlowskivoid write_descr_cb(uint16_t conn_id, tGATT_STATUS status, uint16_t handle,
4216bd442f543972b072ef2cbbcf2f7c91202de1045Myles Watson                    void* data) {
42224ea892320ca85c7f6cdd77343da50818fe29002Jakub Pawlowski  CLI_CBACK_IN_JNI(write_descriptor_cb, conn_id, status, handle);
423ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta}
424ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta
42544be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowskibt_status_t btif_gattc_write_char_descr(int conn_id, uint16_t handle,
426bb920a65b959fbb593403587f89bc3fc7e831732Jakub Pawlowski                                        int auth_req, vector<uint8_t> value) {
427ec79baaaed83529dae14d492ea2f73aedee9e4d0Jakub Pawlowski  CHECK_BTGATT_INIT();
428ec79baaaed83529dae14d492ea2f73aedee9e4d0Jakub Pawlowski
4296bd442f543972b072ef2cbbcf2f7c91202de1045Myles Watson  if (value.size() > BTGATT_MAX_ATTR_LEN) value.resize(BTGATT_MAX_ATTR_LEN);
43039a33612889a562da5c919f14df24707ba3e6007Jakub Pawlowski
43139a33612889a562da5c919f14df24707ba3e6007Jakub Pawlowski  return do_in_jni_thread(Bind(&BTA_GATTC_WriteCharDescr, conn_id, handle,
4326bd442f543972b072ef2cbbcf2f7c91202de1045Myles Watson                               std::move(value), auth_req, write_descr_cb,
4336bd442f543972b072ef2cbbcf2f7c91202de1045Myles Watson                               nullptr));
434ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta}
435ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta
43644be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowskibt_status_t btif_gattc_execute_write(int conn_id, int execute) {
437a684a52231bf0422d991fb16e4dcfc88e4584680Jakub Pawlowski  CHECK_BTGATT_INIT();
438a684a52231bf0422d991fb16e4dcfc88e4584680Jakub Pawlowski  return do_in_jni_thread(
439a684a52231bf0422d991fb16e4dcfc88e4584680Jakub Pawlowski      Bind(&BTA_GATTC_ExecuteWrite, conn_id, (uint8_t)execute));
440ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta}
441ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta
44244be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowskivoid btif_gattc_reg_for_notification_impl(tBTA_GATTC_IF client_if,
443b7f64bc45dec7f7fec74ceb04874f322b9434bbfMarie Janssen                                          const BD_ADDR bda, uint16_t handle) {
444ca9d889000b087ddb9bedaa716176be986be158fJakub Pawlowski  tBTA_GATT_STATUS status = BTA_GATTC_RegisterForNotifications(
4456bd442f543972b072ef2cbbcf2f7c91202de1045Myles Watson      client_if, const_cast<uint8_t*>(bda), handle);
446ca9d889000b087ddb9bedaa716176be986be158fJakub Pawlowski
44744be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski  // TODO(jpawlowski): conn_id is currently unused
448ca9d889000b087ddb9bedaa716176be986be158fJakub Pawlowski  HAL_CBACK(bt_gatt_callbacks, client->register_for_notification_cb,
449ca9d889000b087ddb9bedaa716176be986be158fJakub Pawlowski            /* conn_id */ 0, 1, status, handle);
450ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta}
451ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta
45244be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowskibt_status_t btif_gattc_reg_for_notification(int client_if,
4536bd442f543972b072ef2cbbcf2f7c91202de1045Myles Watson                                            const bt_bdaddr_t* bd_addr,
45444be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski                                            uint16_t handle) {
455ca9d889000b087ddb9bedaa716176be986be158fJakub Pawlowski  CHECK_BTGATT_INIT();
456ca9d889000b087ddb9bedaa716176be986be158fJakub Pawlowski
4576bd442f543972b072ef2cbbcf2f7c91202de1045Myles Watson  uint8_t* address = new BD_ADDR;
458ca9d889000b087ddb9bedaa716176be986be158fJakub Pawlowski  bdcpy(address, bd_addr->address);
459ca9d889000b087ddb9bedaa716176be986be158fJakub Pawlowski  return do_in_jni_thread(
460ca9d889000b087ddb9bedaa716176be986be158fJakub Pawlowski      Bind(base::IgnoreResult(&btif_gattc_reg_for_notification_impl), client_if,
461ca9d889000b087ddb9bedaa716176be986be158fJakub Pawlowski           base::Owned(address), handle));
462ca9d889000b087ddb9bedaa716176be986be158fJakub Pawlowski}
463ca9d889000b087ddb9bedaa716176be986be158fJakub Pawlowski
46444be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowskivoid btif_gattc_dereg_for_notification_impl(tBTA_GATTC_IF client_if,
4656bd442f543972b072ef2cbbcf2f7c91202de1045Myles Watson                                            const BD_ADDR bda,
4666bd442f543972b072ef2cbbcf2f7c91202de1045Myles Watson                                            uint16_t handle) {
467ca9d889000b087ddb9bedaa716176be986be158fJakub Pawlowski  tBTA_GATT_STATUS status = BTA_GATTC_DeregisterForNotifications(
4686bd442f543972b072ef2cbbcf2f7c91202de1045Myles Watson      client_if, const_cast<uint8_t*>(bda), handle);
469ca9d889000b087ddb9bedaa716176be986be158fJakub Pawlowski
47044be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski  // TODO(jpawlowski): conn_id is currently unused
471ca9d889000b087ddb9bedaa716176be986be158fJakub Pawlowski  HAL_CBACK(bt_gatt_callbacks, client->register_for_notification_cb,
472ca9d889000b087ddb9bedaa716176be986be158fJakub Pawlowski            /* conn_id */ 0, 0, status, handle);
473ca9d889000b087ddb9bedaa716176be986be158fJakub Pawlowski}
474ca9d889000b087ddb9bedaa716176be986be158fJakub Pawlowski
47544be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowskibt_status_t btif_gattc_dereg_for_notification(int client_if,
4766bd442f543972b072ef2cbbcf2f7c91202de1045Myles Watson                                              const bt_bdaddr_t* bd_addr,
47744be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski                                              uint16_t handle) {
478ca9d889000b087ddb9bedaa716176be986be158fJakub Pawlowski  CHECK_BTGATT_INIT();
479ca9d889000b087ddb9bedaa716176be986be158fJakub Pawlowski
4806bd442f543972b072ef2cbbcf2f7c91202de1045Myles Watson  uint8_t* address = new BD_ADDR;
481ca9d889000b087ddb9bedaa716176be986be158fJakub Pawlowski  bdcpy(address, bd_addr->address);
482ca9d889000b087ddb9bedaa716176be986be158fJakub Pawlowski  return do_in_jni_thread(
483ca9d889000b087ddb9bedaa716176be986be158fJakub Pawlowski      Bind(base::IgnoreResult(&btif_gattc_dereg_for_notification_impl),
484ca9d889000b087ddb9bedaa716176be986be158fJakub Pawlowski           client_if, base::Owned(address), handle));
485ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta}
486ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta
48744be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowskibt_status_t btif_gattc_read_remote_rssi(int client_if,
4886bd442f543972b072ef2cbbcf2f7c91202de1045Myles Watson                                        const bt_bdaddr_t* bd_addr) {
489c9fe4b6f62c95b195fa99c4fbb75863e547929bfJakub Pawlowski  CHECK_BTGATT_INIT();
490c9fe4b6f62c95b195fa99c4fbb75863e547929bfJakub Pawlowski  rssi_request_client_if = client_if;
491c9fe4b6f62c95b195fa99c4fbb75863e547929bfJakub Pawlowski  // Closure will own this value and free it.
4926bd442f543972b072ef2cbbcf2f7c91202de1045Myles Watson  uint8_t* address = new BD_ADDR;
493c9fe4b6f62c95b195fa99c4fbb75863e547929bfJakub Pawlowski  bdcpy(address, bd_addr->address);
494c9fe4b6f62c95b195fa99c4fbb75863e547929bfJakub Pawlowski  return do_in_jni_thread(Bind(base::IgnoreResult(&BTM_ReadRSSI),
495c9fe4b6f62c95b195fa99c4fbb75863e547929bfJakub Pawlowski                               base::Owned(address),
4966bd442f543972b072ef2cbbcf2f7c91202de1045Myles Watson                               (tBTM_CMPL_CB*)btm_read_rssi_cb));
497ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta}
498ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta
49944be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowskibt_status_t btif_gattc_configure_mtu(int conn_id, int mtu) {
500c9fe4b6f62c95b195fa99c4fbb75863e547929bfJakub Pawlowski  CHECK_BTGATT_INIT();
501c9fe4b6f62c95b195fa99c4fbb75863e547929bfJakub Pawlowski  return do_in_jni_thread(
502c9fe4b6f62c95b195fa99c4fbb75863e547929bfJakub Pawlowski      Bind(base::IgnoreResult(&BTA_GATTC_ConfigureMTU), conn_id, mtu));
5039426d530e9bbc5d3ffae55515388d49185c61325Andre Eisenbach}
5049426d530e9bbc5d3ffae55515388d49185c61325Andre Eisenbach
5053021c5a5aac9760e8c4885cbf9c220d1b04dbdb3Jakub Pawlowskivoid btif_gattc_conn_parameter_update_impl(bt_bdaddr_t addr, int min_interval,
5067cb29a38027f9cb24fc30eefb389f848cfc17082Jakub Pawlowski                                           int max_interval, int latency,
5077cb29a38027f9cb24fc30eefb389f848cfc17082Jakub Pawlowski                                           int timeout) {
5083021c5a5aac9760e8c4885cbf9c220d1b04dbdb3Jakub Pawlowski  if (BTA_DmGetConnectionState(addr.address))
5096bd442f543972b072ef2cbbcf2f7c91202de1045Myles Watson    BTA_DmBleUpdateConnectionParams(addr.address, min_interval, max_interval,
5106bd442f543972b072ef2cbbcf2f7c91202de1045Myles Watson                                    latency, timeout);
5117cb29a38027f9cb24fc30eefb389f848cfc17082Jakub Pawlowski  else
5126bd442f543972b072ef2cbbcf2f7c91202de1045Myles Watson    BTA_DmSetBlePrefConnParams(addr.address, min_interval, max_interval,
5136bd442f543972b072ef2cbbcf2f7c91202de1045Myles Watson                               latency, timeout);
5147cb29a38027f9cb24fc30eefb389f848cfc17082Jakub Pawlowski}
5157cb29a38027f9cb24fc30eefb389f848cfc17082Jakub Pawlowski
5166bd442f543972b072ef2cbbcf2f7c91202de1045Myles Watsonbt_status_t btif_gattc_conn_parameter_update(const bt_bdaddr_t* bd_addr,
51744be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski                                             int min_interval, int max_interval,
51844be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski                                             int latency, int timeout) {
5197cb29a38027f9cb24fc30eefb389f848cfc17082Jakub Pawlowski  CHECK_BTGATT_INIT();
5207cb29a38027f9cb24fc30eefb389f848cfc17082Jakub Pawlowski  return do_in_jni_thread(
5216bd442f543972b072ef2cbbcf2f7c91202de1045Myles Watson      Bind(base::IgnoreResult(&btif_gattc_conn_parameter_update_impl), *bd_addr,
5226bd442f543972b072ef2cbbcf2f7c91202de1045Myles Watson           min_interval, max_interval, latency, timeout));
52352a2cd609897c04bb6440e14ef2ec8871143d028Andre Eisenbach}
52452a2cd609897c04bb6440e14ef2ec8871143d028Andre Eisenbach
5256bd442f543972b072ef2cbbcf2f7c91202de1045Myles Watsonint btif_gattc_get_device_type(const bt_bdaddr_t* bd_addr) {
52644be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski  int device_type = 0;
52744be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski  char bd_addr_str[18] = {0};
528ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta
52944be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski  bdaddr_to_string(bd_addr, bd_addr_str, sizeof(bd_addr_str));
53044be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski  if (btif_config_get_int(bd_addr_str, "DevType", &device_type))
53144be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski    return device_type;
53244be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski  return 0;
533ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta}
534ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta
5356bd442f543972b072ef2cbbcf2f7c91202de1045Myles Watsonbt_status_t btif_gattc_test_command(int command, btgatt_test_params_t* params) {
53644be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski  return btif_gattc_test_command_impl(command, params);
537ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta}
538ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta
53944be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski}  // namespace
54044be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski
541ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Battaconst btgatt_client_interface_t btgattClientInterface = {
542ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta    btif_gattc_register_app,
543ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta    btif_gattc_unregister_app,
544ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta    btif_gattc_open,
545ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta    btif_gattc_close,
546ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta    btif_gattc_refresh,
547ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta    btif_gattc_search_service,
548ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta    btif_gattc_read_char,
549ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta    btif_gattc_write_char,
550ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta    btif_gattc_read_char_descr,
551ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta    btif_gattc_write_char_descr,
552ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta    btif_gattc_execute_write,
553ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta    btif_gattc_reg_for_notification,
554ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta    btif_gattc_dereg_for_notification,
555ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta    btif_gattc_read_remote_rssi,
556ead3cde4bac0c3e32cd31f149093f004eef8ceebGanesh Ganapathi Batta    btif_gattc_get_device_type,
5579426d530e9bbc5d3ffae55515388d49185c61325Andre Eisenbach    btif_gattc_configure_mtu,
55852a2cd609897c04bb6440e14ef2ec8871143d028Andre Eisenbach    btif_gattc_conn_parameter_update,
559533fdbf46fb623aefbe4d0107b5cccd41aec097aJakub Pawlowski    btif_gattc_test_command,
56044be0690a1d15d1ffc97e4c7560d8aee5bb86062Jakub Pawlowski    btif_gattc_get_gatt_db};
561