Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 1 | /****************************************************************************** |
| 2 | * |
| 3 | * Copyright (C) 2009-2013 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 | |
Marie Janssen | 49a8670 | 2015-07-08 11:48:57 -0700 | [diff] [blame] | 19 | #define LOG_TAG "bt_btif_gatt" |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 20 | |
Marie Janssen | 49a8670 | 2015-07-08 11:48:57 -0700 | [diff] [blame] | 21 | #include <errno.h> |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 24 | #include <string.h> |
| 25 | |
Marie Janssen | db55458 | 2015-06-26 14:53:46 -0700 | [diff] [blame] | 26 | #include <hardware/bluetooth.h> |
| 27 | #include <hardware/bt_gatt.h> |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 28 | |
| 29 | #include "btif_common.h" |
| 30 | #include "btif_util.h" |
| 31 | |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 32 | #include "bta_gatt_api.h" |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 33 | #include "bte_appl.h" |
Marie Janssen | db55458 | 2015-06-26 14:53:46 -0700 | [diff] [blame] | 34 | #include "btif_dm.h" |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 35 | #include "btif_gatt.h" |
| 36 | #include "btif_gatt_util.h" |
Marie Janssen | db55458 | 2015-06-26 14:53:46 -0700 | [diff] [blame] | 37 | #include "btif_storage.h" |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 38 | #include "gatt_api.h" |
Sharvil Nanavati | 4480276 | 2014-12-23 23:08:58 -0800 | [diff] [blame] | 39 | #include "osi/include/log.h" |
Myles Watson | d7ffd64 | 2016-10-27 10:27:36 -0700 | [diff] [blame] | 40 | #include "osi/include/osi.h" |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 41 | |
| 42 | /******************************************************************************* |
| 43 | * Typedefs & Macros |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 44 | ******************************************************************************/ |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 45 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 46 | typedef struct { |
| 47 | tGATT_IF gatt_if; |
| 48 | uint16_t conn_id; |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 49 | } btif_test_cb_t; |
| 50 | |
| 51 | /******************************************************************************* |
| 52 | * Static variables |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 53 | ******************************************************************************/ |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 54 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 55 | static const char* disc_name[GATT_DISC_MAX] = {"Unknown", |
| 56 | "GATT_DISC_SRVC_ALL", |
| 57 | "GATT_DISC_SRVC_BY_UUID", |
| 58 | "GATT_DISC_INC_SRVC", |
| 59 | "GATT_DISC_CHAR", |
| 60 | "GATT_DISC_CHAR_DSCPT"}; |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 61 | |
| 62 | static btif_test_cb_t test_cb; |
| 63 | |
| 64 | /******************************************************************************* |
| 65 | * Callback functions |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 66 | ******************************************************************************/ |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 67 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 68 | static char* format_uuid(tBT_UUID bt_uuid, char* str_buf, size_t buf_size) { |
| 69 | if (bt_uuid.len == LEN_UUID_16) { |
| 70 | snprintf(str_buf, buf_size, "0x%04x", bt_uuid.uu.uuid16); |
| 71 | } else if (bt_uuid.len == LEN_UUID_128) { |
| 72 | int x = snprintf(str_buf, buf_size, "%02x%02x%02x%02x-%02x%02x-%02x%02x", |
| 73 | bt_uuid.uu.uuid128[15], bt_uuid.uu.uuid128[14], |
| 74 | bt_uuid.uu.uuid128[13], bt_uuid.uu.uuid128[12], |
| 75 | bt_uuid.uu.uuid128[11], bt_uuid.uu.uuid128[10], |
| 76 | bt_uuid.uu.uuid128[9], bt_uuid.uu.uuid128[8]); |
| 77 | snprintf(&str_buf[x], buf_size - x, "%02x%02x-%02x%02x%02x%02x%02x%02x", |
| 78 | bt_uuid.uu.uuid128[7], bt_uuid.uu.uuid128[6], |
| 79 | bt_uuid.uu.uuid128[5], bt_uuid.uu.uuid128[4], |
| 80 | bt_uuid.uu.uuid128[3], bt_uuid.uu.uuid128[2], |
| 81 | bt_uuid.uu.uuid128[1], bt_uuid.uu.uuid128[0]); |
| 82 | } else { |
| 83 | snprintf(str_buf, buf_size, "Unknown (len=%d)", bt_uuid.len); |
| 84 | } |
George Burgess IV | 80d7f60 | 2016-03-02 14:00:19 -0800 | [diff] [blame] | 85 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 86 | return str_buf; |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 87 | } |
| 88 | |
Myles Watson | 9008888 | 2016-11-15 16:33:22 -0800 | [diff] [blame] | 89 | static void btif_test_connect_cback(UNUSED_ATTR tGATT_IF gatt_if, |
| 90 | UNUSED_ATTR BD_ADDR bda, uint16_t conn_id, |
| 91 | bool connected, |
Myles Watson | d35a648 | 2016-10-27 08:52:16 -0700 | [diff] [blame] | 92 | UNUSED_ATTR tGATT_DISCONN_REASON reason, |
| 93 | UNUSED_ATTR tBT_TRANSPORT transport) { |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 94 | LOG_DEBUG(LOG_TAG, "%s: conn_id=%d, connected=%d", __func__, conn_id, |
| 95 | connected); |
| 96 | test_cb.conn_id = connected ? conn_id : 0; |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 97 | } |
| 98 | |
Marie Janssen | b7f64bc | 2016-06-22 12:52:19 -0700 | [diff] [blame] | 99 | static void btif_test_command_complete_cback(uint16_t conn_id, tGATTC_OPTYPE op, |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 100 | tGATT_STATUS status, |
| 101 | tGATT_CL_COMPLETE* p_data) { |
| 102 | LOG_DEBUG(LOG_TAG, "%s: op_code=0x%02x, conn_id=0x%x. status=0x%x", __func__, |
| 103 | op, conn_id, status); |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 104 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 105 | switch (op) { |
| 106 | case GATTC_OPTYPE_READ: |
| 107 | case GATTC_OPTYPE_WRITE: |
| 108 | case GATTC_OPTYPE_CONFIG: |
| 109 | case GATTC_OPTYPE_EXE_WRITE: |
| 110 | case GATTC_OPTYPE_NOTIFICATION: |
| 111 | break; |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 112 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 113 | case GATTC_OPTYPE_INDICATION: |
| 114 | GATTC_SendHandleValueConfirm(conn_id, p_data->handle); |
| 115 | break; |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 116 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 117 | default: |
| 118 | LOG_DEBUG(LOG_TAG, "%s: Unknown op_code (0x%02x)", __func__, op); |
| 119 | break; |
| 120 | } |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 121 | } |
| 122 | |
Myles Watson | d35a648 | 2016-10-27 08:52:16 -0700 | [diff] [blame] | 123 | static void btif_test_discovery_result_cback(UNUSED_ATTR uint16_t conn_id, |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 124 | tGATT_DISC_TYPE disc_type, |
| 125 | tGATT_DISC_RES* p_data) { |
| 126 | char str_buf[50]; |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 127 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 128 | LOG_DEBUG(LOG_TAG, "------ GATT Discovery result %-22s -------", |
| 129 | disc_name[disc_type]); |
| 130 | LOG_DEBUG(LOG_TAG, " Attribute handle: 0x%04x (%d)", p_data->handle, |
| 131 | p_data->handle); |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 132 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 133 | if (disc_type != GATT_DISC_CHAR_DSCPT) { |
| 134 | LOG_DEBUG(LOG_TAG, " Attribute type: %s", |
| 135 | format_uuid(p_data->type, str_buf, sizeof(str_buf))); |
| 136 | } |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 137 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 138 | switch (disc_type) { |
| 139 | case GATT_DISC_SRVC_ALL: |
| 140 | LOG_DEBUG(LOG_TAG, " Handle range: 0x%04x ~ 0x%04x (%d ~ %d)", |
| 141 | p_data->handle, p_data->value.group_value.e_handle, |
| 142 | p_data->handle, p_data->value.group_value.e_handle); |
| 143 | LOG_DEBUG(LOG_TAG, " Service UUID: %s", |
| 144 | format_uuid(p_data->value.group_value.service_type, str_buf, |
| 145 | sizeof(str_buf))); |
| 146 | break; |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 147 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 148 | case GATT_DISC_SRVC_BY_UUID: |
| 149 | LOG_DEBUG(LOG_TAG, " Handle range: 0x%04x ~ 0x%04x (%d ~ %d)", |
| 150 | p_data->handle, p_data->value.handle, p_data->handle, |
| 151 | p_data->value.handle); |
| 152 | break; |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 153 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 154 | case GATT_DISC_INC_SRVC: |
| 155 | LOG_DEBUG(LOG_TAG, " Handle range: 0x%04x ~ 0x%04x (%d ~ %d)", |
| 156 | p_data->value.incl_service.s_handle, |
| 157 | p_data->value.incl_service.e_handle, |
| 158 | p_data->value.incl_service.s_handle, |
| 159 | p_data->value.incl_service.e_handle); |
| 160 | LOG_DEBUG(LOG_TAG, " Service UUID: %s", |
| 161 | format_uuid(p_data->value.incl_service.service_type, str_buf, |
| 162 | sizeof(str_buf))); |
| 163 | break; |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 164 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 165 | case GATT_DISC_CHAR: |
| 166 | LOG_DEBUG(LOG_TAG, " Properties: 0x%02x", |
| 167 | p_data->value.dclr_value.char_prop); |
| 168 | LOG_DEBUG(LOG_TAG, " Characteristic UUID: %s", |
| 169 | format_uuid(p_data->value.dclr_value.char_uuid, str_buf, |
| 170 | sizeof(str_buf))); |
| 171 | break; |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 172 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 173 | case GATT_DISC_CHAR_DSCPT: |
| 174 | LOG_DEBUG(LOG_TAG, " Descriptor UUID: %s", |
| 175 | format_uuid(p_data->type, str_buf, sizeof(str_buf))); |
| 176 | break; |
| 177 | } |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 178 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 179 | LOG_DEBUG(LOG_TAG, |
| 180 | "-----------------------------------------------------------"); |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 181 | } |
| 182 | |
Myles Watson | 9008888 | 2016-11-15 16:33:22 -0800 | [diff] [blame] | 183 | static void btif_test_discovery_complete_cback( |
| 184 | UNUSED_ATTR uint16_t conn_id, UNUSED_ATTR tGATT_DISC_TYPE disc_type, |
| 185 | tGATT_STATUS status) { |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 186 | LOG_DEBUG(LOG_TAG, "%s: status=%d", __func__, status); |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 187 | } |
| 188 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 189 | static tGATT_CBACK btif_test_callbacks = {btif_test_connect_cback, |
| 190 | btif_test_command_complete_cback, |
| 191 | btif_test_discovery_result_cback, |
| 192 | btif_test_discovery_complete_cback, |
| 193 | NULL, |
| 194 | NULL, |
Jakub Pawlowski | eafd45d | 2017-03-22 19:00:47 -0700 | [diff] [blame] | 195 | NULL, |
Jakub Pawlowski | b5ba4fd | 2017-03-23 18:11:04 -0700 | [diff] [blame^] | 196 | NULL, |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 197 | NULL}; |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 198 | |
| 199 | /******************************************************************************* |
| 200 | * Implementation |
Myles Watson | ee96a3c | 2016-11-23 14:49:54 -0800 | [diff] [blame] | 201 | ******************************************************************************/ |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 202 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 203 | bt_status_t btif_gattc_test_command_impl(int command, |
| 204 | btgatt_test_params_t* params) { |
| 205 | switch (command) { |
| 206 | case 0x01: /* Enable */ |
| 207 | { |
| 208 | LOG_DEBUG(LOG_TAG, "%s: ENABLE - enable=%d", __func__, params->u1); |
| 209 | if (params->u1) { |
| 210 | tBT_UUID app_uuid = {LEN_UUID_128, {0xAE}}; |
| 211 | test_cb.gatt_if = GATT_Register(&app_uuid, &btif_test_callbacks); |
| 212 | GATT_StartIf(test_cb.gatt_if); |
| 213 | } else { |
| 214 | GATT_Deregister(test_cb.gatt_if); |
| 215 | test_cb.gatt_if = 0; |
| 216 | } |
| 217 | break; |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 218 | } |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame] | 219 | |
| 220 | case 0x02: /* Connect */ |
| 221 | { |
| 222 | LOG_DEBUG(LOG_TAG, |
| 223 | "%s: CONNECT - device=%02x:%02x:%02x:%02x:%02x:%02x " |
| 224 | "(dev_type=%d, addr_type=%d)", |
| 225 | __func__, params->bda1->address[0], params->bda1->address[1], |
| 226 | params->bda1->address[2], params->bda1->address[3], |
| 227 | params->bda1->address[4], params->bda1->address[5], params->u1, |
| 228 | params->u2); |
| 229 | |
| 230 | if (params->u1 == BT_DEVICE_TYPE_BLE) |
| 231 | BTM_SecAddBleDevice(params->bda1->address, NULL, BT_DEVICE_TYPE_BLE, |
| 232 | params->u2); |
| 233 | |
| 234 | if (!GATT_Connect(test_cb.gatt_if, params->bda1->address, true, |
| 235 | BT_TRANSPORT_LE, false)) { |
| 236 | LOG_ERROR(LOG_TAG, "%s: GATT_Connect failed!", __func__); |
| 237 | } |
| 238 | break; |
| 239 | } |
| 240 | |
| 241 | case 0x03: /* Disconnect */ |
| 242 | { |
| 243 | LOG_DEBUG(LOG_TAG, "%s: DISCONNECT - conn_id=%d", __func__, |
| 244 | test_cb.conn_id); |
| 245 | GATT_Disconnect(test_cb.conn_id); |
| 246 | break; |
| 247 | } |
| 248 | |
| 249 | case 0x04: /* Discover */ |
| 250 | { |
| 251 | char buf[50] = {0}; |
| 252 | tGATT_DISC_PARAM param; |
| 253 | memset(¶m, 0, sizeof(tGATT_DISC_PARAM)); |
| 254 | |
| 255 | if (params->u1 >= GATT_DISC_MAX) { |
| 256 | LOG_ERROR(LOG_TAG, "%s: DISCOVER - Invalid type (%d)!", __func__, |
| 257 | params->u1); |
| 258 | return (bt_status_t)0; |
| 259 | } |
| 260 | |
| 261 | param.s_handle = params->u2; |
| 262 | param.e_handle = params->u3; |
| 263 | btif_to_bta_uuid(¶m.service, params->uuid1); |
| 264 | |
| 265 | LOG_DEBUG(LOG_TAG, |
| 266 | "%s: DISCOVER (%s), conn_id=%d, uuid=%s, handles=0x%04x-0x%04x", |
| 267 | __func__, disc_name[params->u1], test_cb.conn_id, |
| 268 | format_uuid(param.service, buf, sizeof(buf)), params->u2, |
| 269 | params->u3); |
| 270 | GATTC_Discover(test_cb.conn_id, params->u1, ¶m); |
| 271 | break; |
| 272 | } |
| 273 | |
| 274 | case 0xF0: /* Pairing configuration */ |
| 275 | LOG_DEBUG(LOG_TAG, |
| 276 | "%s: Setting pairing config auth=%d, iocaps=%d, keys=%d/%d/%d", |
| 277 | __func__, params->u1, params->u2, params->u3, params->u4, |
| 278 | params->u5); |
| 279 | |
| 280 | bte_appl_cfg.ble_auth_req = params->u1; |
| 281 | bte_appl_cfg.ble_io_cap = params->u2; |
| 282 | bte_appl_cfg.ble_init_key = params->u3; |
| 283 | bte_appl_cfg.ble_resp_key = params->u4; |
| 284 | bte_appl_cfg.ble_max_key_size = params->u5; |
| 285 | break; |
| 286 | |
| 287 | default: |
| 288 | LOG_ERROR(LOG_TAG, "%s: UNKNOWN TEST COMMAND 0x%02x", __func__, command); |
| 289 | break; |
| 290 | } |
| 291 | return (bt_status_t)0; |
Ganesh Ganapathi Batta | ead3cde | 2013-02-05 15:22:31 -0800 | [diff] [blame] | 292 | } |