btif_gatt_client.c revision e4020c28c8a9dc56f927b03a8ca3a17d9401bc1e
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 19 20/******************************************************************************* 21 * 22 * Filename: btif_gatt_client.c 23 * 24 * Description: GATT client implementation 25 * 26 *******************************************************************************/ 27 28#include <hardware/bluetooth.h> 29#include <hardware/bt_gatt.h> 30#include <stdio.h> 31#include <stdlib.h> 32#include <errno.h> 33#include <string.h> 34 35#define LOG_TAG "BtGatt.btif" 36 37#include "btif_common.h" 38#include "btif_util.h" 39 40#if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE)) 41 42#include "gki.h" 43#include "bta_api.h" 44#include "bta_gatt_api.h" 45#include "bd.h" 46#include "btif_storage.h" 47#include "btif_config.h" 48 49#include "btif_gatt.h" 50#include "btif_gatt_util.h" 51#include "btif_dm.h" 52#include "btif_storage.h" 53 54/******************************************************************************* 55** Constants & Macros 56********************************************************************************/ 57 58#define CHECK_BTGATT_INIT() if (bt_gatt_callbacks == NULL)\ 59 {\ 60 ALOGW("%s: BTGATT not initialized", __FUNCTION__);\ 61 return BT_STATUS_NOT_READY;\ 62 } else {\ 63 ALOGD("%s", __FUNCTION__);\ 64 } 65 66 67typedef enum { 68 BTIF_GATTC_REGISTER_APP = 1000, 69 BTIF_GATTC_UNREGISTER_APP, 70 BTIF_GATTC_SCAN_START, 71 BTIF_GATTC_SCAN_STOP, 72 BTIF_GATTC_OPEN, 73 BTIF_GATTC_CLOSE, 74 BTIF_GATTC_SEARCH_SERVICE, 75 BTIF_GATTC_GET_FIRST_CHAR, 76 BTIF_GATTC_GET_NEXT_CHAR, 77 BTIF_GATTC_GET_FIRST_CHAR_DESCR, 78 BTIF_GATTC_GET_NEXT_CHAR_DESCR, 79 BTIF_GATTC_GET_FIRST_INCL_SERVICE, 80 BTIF_GATTC_GET_NEXT_INCL_SERVICE, 81 BTIF_GATTC_READ_CHAR, 82 BTIF_GATTC_READ_CHAR_DESCR, 83 BTIF_GATTC_WRITE_CHAR, 84 BTIF_GATTC_WRITE_CHAR_DESCR, 85 BTIF_GATTC_EXECUTE_WRITE, 86 BTIF_GATTC_REG_FOR_NOTIFICATION, 87 BTIF_GATTC_DEREG_FOR_NOTIFICATION, 88 BTIF_GATTC_REFRESH, 89 BTIF_GATTC_READ_RSSI 90} btif_gattc_event_t; 91 92#define BTIF_GATT_MAX_OBSERVED_DEV 40 93 94#define BTIF_GATT_OBSERVE_EVT 0x1000 95#define BTIF_GATTC_RSSI_EVT 0x1001 96 97/******************************************************************************* 98** Local type definitions 99********************************************************************************/ 100 101typedef struct 102{ 103 uint8_t value[BTGATT_MAX_ATTR_LEN]; 104 bt_bdaddr_t bd_addr; 105 btgatt_srvc_id_t srvc_id; 106 btgatt_srvc_id_t incl_srvc_id; 107 btgatt_char_id_t char_id; 108 bt_uuid_t uuid; 109 uint16_t conn_id; 110 uint16_t len; 111 uint8_t client_if; 112 uint8_t action; 113 uint8_t is_direct; 114 uint8_t search_all; 115 uint8_t auth_req; 116 uint8_t write_type; 117 uint8_t status; 118 uint8_t addr_type; 119 int8_t rssi; 120 tBT_DEVICE_TYPE device_type; 121} __attribute__((packed)) btif_gattc_cb_t; 122 123typedef struct 124{ 125 bt_bdaddr_t bd_addr; 126 BOOLEAN in_use; 127}__attribute__((packed)) btif_gattc_dev_t; 128 129typedef struct 130{ 131 btif_gattc_dev_t remote_dev[BTIF_GATT_MAX_OBSERVED_DEV]; 132 uint8_t addr_type; 133 uint8_t next_storage_idx; 134}__attribute__((packed)) btif_gattc_dev_cb_t; 135 136/******************************************************************************* 137** Static variables 138********************************************************************************/ 139 140extern const btgatt_callbacks_t *bt_gatt_callbacks; 141static btif_gattc_dev_cb_t btif_gattc_dev_cb; 142static btif_gattc_dev_cb_t *p_dev_cb = &btif_gattc_dev_cb; 143static uint8_t rssi_request_client_if; 144 145/******************************************************************************* 146** Static functions 147********************************************************************************/ 148 149static void btapp_gattc_req_data(UINT16 event, char *p_dest, char *p_src) 150{ 151 tBTA_GATTC *p_dest_data = (tBTA_GATTC*)p_dest; 152 tBTA_GATTC *p_src_data = (tBTA_GATTC*)p_src; 153 154 if (!p_src_data || !p_dest_data) 155 return; 156 157 // Copy basic structure first 158 memcpy(p_dest_data, p_src_data, sizeof(tBTA_GATTC)); 159 160 // Allocate buffer for request data if necessary 161 switch (event) 162 { 163 case BTA_GATTC_READ_CHAR_EVT: 164 case BTA_GATTC_READ_DESCR_EVT: 165 166 if (p_src_data->read.p_value != NULL) 167 { 168 p_dest_data->read.p_value = GKI_getbuf(sizeof(tBTA_GATT_READ_VAL)); 169 170 if (p_dest_data->read.p_value != NULL) 171 { 172 memcpy(p_dest_data->read.p_value, p_src_data->read.p_value, 173 sizeof(tBTA_GATT_READ_VAL)); 174 175 // Allocate buffer for att value if necessary 176 if (get_uuid16(&p_src_data->read.descr_type) != GATT_UUID_CHAR_AGG_FORMAT 177 && p_src_data->read.p_value->unformat.p_value != NULL) 178 { 179 p_dest_data->read.p_value->unformat.p_value = 180 GKI_getbuf(p_src_data->read.p_value->unformat.len); 181 if (p_dest_data->read.p_value->unformat.p_value != NULL) 182 { 183 memcpy(p_dest_data->read.p_value->unformat.p_value, 184 p_src_data->read.p_value->unformat.p_value, 185 p_src_data->read.p_value->unformat.len); 186 } 187 } 188 } 189 } 190 else 191 { 192 BTIF_TRACE_WARNING2("%s :Src read.p_value ptr is NULL for event 0x%x", 193 __FUNCTION__, event); 194 p_dest_data->read.p_value = NULL; 195 196 } 197 break; 198 199 default: 200 break; 201 } 202} 203 204static void btapp_gattc_free_req_data(UINT16 event, tBTA_GATTC *p_data) 205{ 206 switch (event) 207 { 208 case BTA_GATTC_READ_CHAR_EVT: 209 case BTA_GATTC_READ_DESCR_EVT: 210 if (p_data != NULL && p_data->read.p_value != NULL) 211 { 212 if (get_uuid16 (&p_data->read.descr_type) != GATT_UUID_CHAR_AGG_FORMAT 213 && p_data->read.p_value->unformat.p_value != NULL) 214 { 215 GKI_freebuf(p_data->read.p_value->unformat.p_value); 216 } 217 GKI_freebuf(p_data->read.p_value); 218 } 219 break; 220 221 default: 222 break; 223 } 224} 225 226static void btif_gattc_init_dev_cb(void) 227{ 228 memset(p_dev_cb, 0, sizeof(btif_gattc_dev_cb_t)); 229} 230 231static void btif_gattc_add_remote_bdaddr (BD_ADDR p_bda, uint8_t addr_type) 232{ 233 BOOLEAN found=FALSE; 234 uint8_t i; 235 for (i = 0; i < BTIF_GATT_MAX_OBSERVED_DEV; i++) 236 { 237 if (!p_dev_cb->remote_dev[i].in_use ) 238 { 239 memcpy(p_dev_cb->remote_dev[i].bd_addr.address, p_bda, BD_ADDR_LEN); 240 p_dev_cb->addr_type = addr_type; 241 p_dev_cb->remote_dev[i].in_use = TRUE; 242 ALOGD("%s device added idx=%d", __FUNCTION__, i ); 243 break; 244 } 245 } 246 247 if ( i == BTIF_GATT_MAX_OBSERVED_DEV) 248 { 249 i= p_dev_cb->next_storage_idx; 250 memcpy(p_dev_cb->remote_dev[i].bd_addr.address, p_bda, BD_ADDR_LEN); 251 p_dev_cb->addr_type = addr_type; 252 p_dev_cb->remote_dev[i].in_use = TRUE; 253 ALOGD("%s device overwrite idx=%d", __FUNCTION__, i ); 254 p_dev_cb->next_storage_idx++; 255 if(p_dev_cb->next_storage_idx >= BTIF_GATT_MAX_OBSERVED_DEV) 256 p_dev_cb->next_storage_idx = 0; 257 } 258} 259 260static BOOLEAN btif_gattc_find_bdaddr (BD_ADDR p_bda) 261{ 262 uint8_t i; 263 for (i = 0; i < BTIF_GATT_MAX_OBSERVED_DEV; i++) 264 { 265 if (p_dev_cb->remote_dev[i].in_use && 266 !memcmp(p_dev_cb->remote_dev[i].bd_addr.address, p_bda, BD_ADDR_LEN)) 267 { 268 return TRUE; 269 } 270 } 271 return FALSE; 272} 273 274static void btif_gattc_update_properties ( btif_gattc_cb_t *p_btif_cb ) 275{ 276 uint8_t remote_name_len; 277 uint8_t *p_eir_remote_name=NULL; 278 bt_bdname_t bdname; 279 280 p_eir_remote_name = BTA_CheckEirData(p_btif_cb->value, 281 BTM_EIR_COMPLETE_LOCAL_NAME_TYPE, &remote_name_len); 282 283 if(p_eir_remote_name == NULL) 284 { 285 p_eir_remote_name = BTA_CheckEirData(p_btif_cb->value, 286 BT_EIR_SHORTENED_LOCAL_NAME_TYPE, &remote_name_len); 287 } 288 289 if(p_eir_remote_name) 290 { 291 memcpy(bdname.name, p_eir_remote_name, remote_name_len); 292 bdname.name[remote_name_len]='\0'; 293 294 ALOGD("%s BLE device name=%s len=%d dev_type=%d", __FUNCTION__, bdname.name, 295 remote_name_len, p_btif_cb->device_type ); 296 btif_dm_update_ble_remote_properties( p_btif_cb->bd_addr.address, bdname.name, 297 p_btif_cb->device_type); 298 } 299 300 btif_storage_set_remote_addr_type( &p_btif_cb->bd_addr, p_btif_cb->addr_type); 301} 302 303static void btif_gattc_upstreams_evt(uint16_t event, char* p_param) 304{ 305 ALOGD("%s: Event %d", __FUNCTION__, event); 306 307 tBTA_GATTC *p_data = (tBTA_GATTC*)p_param; 308 switch (event) 309 { 310 case BTA_GATTC_REG_EVT: 311 { 312 bt_uuid_t app_uuid; 313 bta_to_btif_uuid(&app_uuid, &p_data->reg_oper.app_uuid); 314 HAL_CBACK(bt_gatt_callbacks, client->register_client_cb 315 , p_data->reg_oper.status 316 , p_data->reg_oper.client_if 317 , &app_uuid 318 ); 319 break; 320 } 321 322 case BTA_GATTC_DEREG_EVT: 323 break; 324 325 case BTA_GATTC_READ_CHAR_EVT: 326 { 327 btgatt_read_params_t data; 328 set_read_value(&data, &p_data->read); 329 330 HAL_CBACK(bt_gatt_callbacks, client->read_characteristic_cb 331 , p_data->read.conn_id, p_data->read.status, &data); 332 break; 333 } 334 335 case BTA_GATTC_WRITE_CHAR_EVT: 336 case BTA_GATTC_PREP_WRITE_EVT: 337 { 338 btgatt_write_params_t data; 339 bta_to_btif_srvc_id(&data.srvc_id, &p_data->write.srvc_id); 340 bta_to_btif_char_id(&data.char_id, &p_data->write.char_id); 341 342 HAL_CBACK(bt_gatt_callbacks, client->write_characteristic_cb 343 , p_data->write.conn_id, p_data->write.status, &data 344 ); 345 break; 346 } 347 348 case BTA_GATTC_EXEC_EVT: 349 { 350 HAL_CBACK(bt_gatt_callbacks, client->execute_write_cb 351 , p_data->exec_cmpl.conn_id, p_data->exec_cmpl.status 352 ); 353 break; 354 } 355 356 case BTA_GATTC_SEARCH_CMPL_EVT: 357 { 358 HAL_CBACK(bt_gatt_callbacks, client->search_complete_cb 359 , p_data->search_cmpl.conn_id, p_data->search_cmpl.status); 360 break; 361 } 362 363 case BTA_GATTC_SEARCH_RES_EVT: 364 { 365 btgatt_srvc_id_t data; 366 bta_to_btif_srvc_id(&data, &(p_data->srvc_res.service_uuid)); 367 HAL_CBACK(bt_gatt_callbacks, client->search_result_cb 368 , p_data->srvc_res.conn_id, &data); 369 break; 370 } 371 372 case BTA_GATTC_READ_DESCR_EVT: 373 { 374 btgatt_read_params_t data; 375 set_read_value(&data, &p_data->read); 376 377 HAL_CBACK(bt_gatt_callbacks, client->read_descriptor_cb 378 , p_data->read.conn_id, p_data->read.status, &data); 379 break; 380 } 381 382 case BTA_GATTC_WRITE_DESCR_EVT: 383 { 384 btgatt_write_params_t data; 385 bta_to_btif_srvc_id(&data.srvc_id, &p_data->write.srvc_id); 386 bta_to_btif_char_id(&data.char_id, &p_data->write.char_id); 387 bta_to_btif_uuid(&data.descr_id, &p_data->write.descr_type); 388 389 HAL_CBACK(bt_gatt_callbacks, client->write_descriptor_cb 390 , p_data->write.conn_id, p_data->write.status, &data); 391 break; 392 } 393 394 case BTA_GATTC_NOTIF_EVT: 395 { 396 btgatt_notify_params_t data; 397 398 bdcpy(data.bda.address, p_data->notify.bda); 399 400 bta_to_btif_srvc_id(&data.srvc_id, &p_data->notify.char_id.srvc_id); 401 bta_to_btif_char_id(&data.char_id, &p_data->notify.char_id.char_id); 402 memcpy(data.value, p_data->notify.value, p_data->notify.len); 403 404 data.is_notify = p_data->notify.is_notify; 405 data.len = p_data->notify.len; 406 407 HAL_CBACK(bt_gatt_callbacks, client->notify_cb 408 , p_data->notify.conn_id, &data); 409 410 if (p_data->notify.is_notify == FALSE) 411 { 412 BTA_GATTC_SendIndConfirm(p_data->notify.conn_id, 413 &p_data->notify.char_id); 414 } 415 break; 416 } 417 418 case BTA_GATTC_OPEN_EVT: 419 { 420 bt_bdaddr_t bda; 421 bdcpy(bda.address, p_data->open.remote_bda); 422 423 if (p_data->open.status == BTA_GATT_OK) 424 btif_gatt_check_encrypted_link(p_data->open.remote_bda); 425 426 HAL_CBACK(bt_gatt_callbacks, client->open_cb, p_data->open.conn_id 427 , p_data->open.status, p_data->open.client_if, &bda); 428 break; 429 } 430 431 case BTA_GATTC_CLOSE_EVT: 432 { 433 bt_bdaddr_t bda; 434 bdcpy(bda.address, p_data->close.remote_bda); 435 HAL_CBACK(bt_gatt_callbacks, client->close_cb, p_data->close.conn_id 436 , p_data->status, p_data->close.client_if, &bda); 437 438 if(p_data->status == BTA_GATT_OK) 439 btif_gatt_remove_encrypted_link(p_data->close.remote_bda); 440 break; 441 } 442 443 case BTA_GATTC_ACL_EVT: 444 ALOGD("BTA_GATTC_ACL_EVT: status = %d", p_data->status); 445 /* Ignore for now */ 446 break; 447 448 case BTA_GATTC_CANCEL_OPEN_EVT: 449 break; 450 451 case BTIF_GATT_OBSERVE_EVT: 452 { 453 btif_gattc_cb_t *p_btif_cb = (btif_gattc_cb_t*)p_param; 454 if (!btif_gattc_find_bdaddr(p_btif_cb->bd_addr.address)) 455 { 456 btif_gattc_add_remote_bdaddr(p_btif_cb->bd_addr.address, p_btif_cb->addr_type); 457 btif_gattc_update_properties(p_btif_cb); 458 } 459 HAL_CBACK(bt_gatt_callbacks, client->scan_result_cb, 460 &p_btif_cb->bd_addr, p_btif_cb->rssi, p_btif_cb->value); 461 break; 462 } 463 464 case BTIF_GATTC_RSSI_EVT: 465 { 466 btif_gattc_cb_t *p_btif_cb = (btif_gattc_cb_t*)p_param; 467 HAL_CBACK(bt_gatt_callbacks, client->read_remote_rssi_cb, p_btif_cb->client_if, 468 &p_btif_cb->bd_addr, p_btif_cb->rssi, p_btif_cb->status); 469 break; 470 } 471 472 default: 473 ALOGE("%s: Unhandled event (%d)!", __FUNCTION__, event); 474 break; 475 } 476 477 btapp_gattc_free_req_data(event, p_data); 478} 479 480static void bte_gattc_cback(tBTA_GATTC_EVT event, tBTA_GATTC *p_data) 481{ 482 bt_status_t status = btif_transfer_context(btif_gattc_upstreams_evt, 483 (uint16_t) event, (void*)p_data, sizeof(tBTA_GATTC), btapp_gattc_req_data); 484 ASSERTC(status == BT_STATUS_SUCCESS, "Context transfer failed!", status); 485} 486 487static void bte_scan_results_cb (tBTA_DM_SEARCH_EVT event, tBTA_DM_SEARCH *p_data) 488{ 489 btif_gattc_cb_t btif_cb; 490 uint8_t len; 491 492 switch (event) 493 { 494 case BTA_DM_INQ_RES_EVT: 495 { 496 bdcpy(btif_cb.bd_addr.address, p_data->inq_res.bd_addr); 497 btif_cb.device_type = p_data->inq_res.device_type; 498 btif_cb.rssi = p_data->inq_res.rssi; 499 btif_cb.addr_type = p_data->inq_res.ble_addr_type; 500 if (p_data->inq_res.p_eir) 501 { 502 memcpy(btif_cb.value, p_data->inq_res.p_eir, 62); 503 if (BTA_CheckEirData(p_data->inq_res.p_eir, BTM_EIR_COMPLETE_LOCAL_NAME_TYPE, 504 &len)) 505 { 506 p_data->inq_res.remt_name_not_required = TRUE; 507 } 508 } 509 } 510 break; 511 512 case BTA_DM_INQ_CMPL_EVT: 513 { 514 BTIF_TRACE_DEBUG2("%s BLE observe complete. Num Resp %d", 515 __FUNCTION__,p_data->inq_cmpl.num_resps); 516 return; 517 } 518 519 default: 520 BTIF_TRACE_WARNING2("%s : Unknown event 0x%x", __FUNCTION__, event); 521 return; 522 } 523 btif_transfer_context(btif_gattc_upstreams_evt, BTIF_GATT_OBSERVE_EVT, 524 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL); 525} 526 527static void btm_read_rssi_cb (tBTM_RSSI_RESULTS *p_result) 528{ 529 btif_gattc_cb_t btif_cb; 530 531 bdcpy(btif_cb.bd_addr.address, p_result->rem_bda); 532 btif_cb.rssi = p_result->rssi; 533 btif_cb.status = p_result->status; 534 btif_cb.client_if = rssi_request_client_if; 535 btif_transfer_context(btif_gattc_upstreams_evt, BTIF_GATTC_RSSI_EVT, 536 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL); 537} 538 539 540static void btgattc_handle_event(uint16_t event, char* p_param) 541{ 542 tBTA_GATT_STATUS status; 543 tBT_UUID uuid; 544 tBTA_GATT_SRVC_ID srvc_id; 545 tGATT_CHAR_PROP out_char_prop; 546 tBTA_GATTC_CHAR_ID in_char_id; 547 tBTA_GATTC_CHAR_ID out_char_id; 548 tBTA_GATTC_CHAR_DESCR_ID in_char_descr_id; 549 tBTA_GATTC_CHAR_DESCR_ID out_char_descr_id; 550 tBTA_GATTC_INCL_SVC_ID in_incl_svc_id; 551 tBTA_GATTC_INCL_SVC_ID out_incl_svc_id; 552 tBTA_GATT_UNFMT descr_val; 553 554 btif_gattc_cb_t* p_cb = (btif_gattc_cb_t*)p_param; 555 if (!p_cb) return; 556 557 ALOGD("%s: Event %d", __FUNCTION__, event); 558 559 switch (event) 560 { 561 case BTIF_GATTC_REGISTER_APP: 562 btif_to_bta_uuid(&uuid, &p_cb->uuid); 563 BTA_GATTC_AppRegister(&uuid, bte_gattc_cback); 564 break; 565 566 case BTIF_GATTC_UNREGISTER_APP: 567 BTA_GATTC_AppDeregister(p_cb->client_if); 568 break; 569 570 case BTIF_GATTC_SCAN_START: 571 btif_gattc_init_dev_cb(); 572 BTA_DmBleObserve(TRUE, 0, bte_scan_results_cb); 573 break; 574 575 case BTIF_GATTC_SCAN_STOP: 576 BTA_DmBleObserve(FALSE, 0, 0); 577 break; 578 579 case BTIF_GATTC_OPEN: 580 if (!p_cb->is_direct) 581 BTA_DmBleSetBgConnType(BTM_BLE_CONN_AUTO, NULL); 582 583 BTA_GATTC_Open(p_cb->client_if, p_cb->bd_addr.address, p_cb->is_direct); 584 break; 585 586 case BTIF_GATTC_CLOSE: 587 // Disconnect establiched connections 588 if (p_cb->conn_id != 0) 589 BTA_GATTC_Close(p_cb->conn_id); 590 else 591 BTA_GATTC_CancelOpen(p_cb->client_if, p_cb->bd_addr.address, TRUE); 592 593 // Cancel pending background connections (remove from whitelist) 594 BTA_GATTC_CancelOpen(p_cb->client_if, p_cb->bd_addr.address, FALSE); 595 break; 596 597 case BTIF_GATTC_SEARCH_SERVICE: 598 { 599 if (p_cb->search_all) 600 { 601 BTA_GATTC_ServiceSearchRequest(p_cb->conn_id, NULL); 602 } else { 603 btif_to_bta_uuid(&uuid, &p_cb->uuid); 604 BTA_GATTC_ServiceSearchRequest(p_cb->conn_id, &uuid); 605 } 606 break; 607 } 608 609 case BTIF_GATTC_GET_FIRST_CHAR: 610 { 611 btgatt_char_id_t char_id; 612 btif_to_bta_srvc_id(&srvc_id, &p_cb->srvc_id); 613 status = BTA_GATTC_GetFirstChar(p_cb->conn_id, &srvc_id, NULL, 614 &out_char_id, &out_char_prop); 615 616 if (status == 0) 617 bta_to_btif_char_id(&char_id, &out_char_id.char_id); 618 619 HAL_CBACK(bt_gatt_callbacks, client->get_characteristic_cb, 620 p_cb->conn_id, status, &p_cb->srvc_id, 621 &char_id, out_char_prop); 622 break; 623 } 624 625 case BTIF_GATTC_GET_NEXT_CHAR: 626 { 627 btgatt_char_id_t char_id; 628 btif_to_bta_srvc_id(&in_char_id.srvc_id, &p_cb->srvc_id); 629 btif_to_bta_char_id(&in_char_id.char_id, &p_cb->char_id); 630 631 status = BTA_GATTC_GetNextChar(p_cb->conn_id, &in_char_id, NULL, 632 &out_char_id, &out_char_prop); 633 634 if (status == 0) 635 bta_to_btif_char_id(&char_id, &out_char_id.char_id); 636 637 HAL_CBACK(bt_gatt_callbacks, client->get_characteristic_cb, 638 p_cb->conn_id, status, &p_cb->srvc_id, 639 &char_id, out_char_prop); 640 break; 641 } 642 643 case BTIF_GATTC_GET_FIRST_CHAR_DESCR: 644 { 645 bt_uuid_t descr_id; 646 btif_to_bta_srvc_id(&in_char_id.srvc_id, &p_cb->srvc_id); 647 btif_to_bta_char_id(&in_char_id.char_id, &p_cb->char_id); 648 649 status = BTA_GATTC_GetFirstCharDescr(p_cb->conn_id, &in_char_id, NULL, 650 &out_char_descr_id); 651 652 if (status == 0) 653 bta_to_btif_uuid(&descr_id, &out_char_descr_id.descr_type); 654 655 HAL_CBACK(bt_gatt_callbacks, client->get_descriptor_cb, 656 p_cb->conn_id, status, &p_cb->srvc_id, 657 &p_cb->char_id, &descr_id); 658 break; 659 } 660 661 case BTIF_GATTC_GET_NEXT_CHAR_DESCR: 662 { 663 bt_uuid_t descr_id; 664 btif_to_bta_srvc_id(&in_char_descr_id.char_id.srvc_id, &p_cb->srvc_id); 665 btif_to_bta_char_id(&in_char_descr_id.char_id.char_id, &p_cb->char_id); 666 btif_to_bta_uuid(&in_char_descr_id.descr_type, &p_cb->uuid); 667 668 status = BTA_GATTC_GetNextCharDescr(p_cb->conn_id, &in_char_descr_id 669 , NULL, &out_char_descr_id); 670 671 if (status == 0) 672 bta_to_btif_uuid(&descr_id, &out_char_descr_id.descr_type); 673 674 HAL_CBACK(bt_gatt_callbacks, client->get_descriptor_cb, 675 p_cb->conn_id, status, &p_cb->srvc_id, 676 &p_cb->char_id, &descr_id); 677 break; 678 } 679 680 case BTIF_GATTC_GET_FIRST_INCL_SERVICE: 681 { 682 btgatt_srvc_id_t incl_srvc_id; 683 btif_to_bta_srvc_id(&srvc_id, &p_cb->srvc_id); 684 685 status = BTA_GATTC_GetFirstIncludedService(p_cb->conn_id, 686 &srvc_id, NULL, &out_incl_svc_id); 687 688 bta_to_btif_srvc_id(&incl_srvc_id, &out_incl_svc_id.incl_svc_id); 689 690 HAL_CBACK(bt_gatt_callbacks, client->get_included_service_cb, 691 p_cb->conn_id, status, &p_cb->srvc_id, 692 &incl_srvc_id); 693 break; 694 } 695 696 case BTIF_GATTC_GET_NEXT_INCL_SERVICE: 697 { 698 btgatt_srvc_id_t incl_srvc_id; 699 btif_to_bta_srvc_id(&in_incl_svc_id.srvc_id, &p_cb->srvc_id); 700 btif_to_bta_srvc_id(&in_incl_svc_id.incl_svc_id, &p_cb->incl_srvc_id); 701 702 status = BTA_GATTC_GetNextIncludedService(p_cb->conn_id, 703 &in_incl_svc_id, NULL, &out_incl_svc_id); 704 705 bta_to_btif_srvc_id(&incl_srvc_id, &out_incl_svc_id.incl_svc_id); 706 707 HAL_CBACK(bt_gatt_callbacks, client->get_included_service_cb, 708 p_cb->conn_id, status, &p_cb->srvc_id, 709 &incl_srvc_id); 710 break; 711 } 712 713 case BTIF_GATTC_READ_CHAR: 714 btif_to_bta_srvc_id(&in_char_id.srvc_id, &p_cb->srvc_id); 715 btif_to_bta_char_id(&in_char_id.char_id, &p_cb->char_id); 716 717 BTA_GATTC_ReadCharacteristic(p_cb->conn_id, &in_char_id, p_cb->auth_req); 718 break; 719 720 case BTIF_GATTC_READ_CHAR_DESCR: 721 btif_to_bta_srvc_id(&in_char_descr_id.char_id.srvc_id, &p_cb->srvc_id); 722 btif_to_bta_char_id(&in_char_descr_id.char_id.char_id, &p_cb->char_id); 723 btif_to_bta_uuid(&in_char_descr_id.descr_type, &p_cb->uuid); 724 725 BTA_GATTC_ReadCharDescr(p_cb->conn_id, &in_char_descr_id, p_cb->auth_req); 726 break; 727 728 case BTIF_GATTC_WRITE_CHAR: 729 btif_to_bta_srvc_id(&in_char_id.srvc_id, &p_cb->srvc_id); 730 btif_to_bta_char_id(&in_char_id.char_id, &p_cb->char_id); 731 732 BTA_GATTC_WriteCharValue(p_cb->conn_id, &in_char_id, 733 p_cb->write_type, 734 p_cb->len, 735 p_cb->value, 736 p_cb->auth_req); 737 break; 738 739 case BTIF_GATTC_WRITE_CHAR_DESCR: 740 btif_to_bta_srvc_id(&in_char_descr_id.char_id.srvc_id, &p_cb->srvc_id); 741 btif_to_bta_char_id(&in_char_descr_id.char_id.char_id, &p_cb->char_id); 742 btif_to_bta_uuid(&in_char_descr_id.descr_type, &p_cb->uuid); 743 744 descr_val.len = p_cb->len; 745 descr_val.p_value = p_cb->value; 746 747 BTA_GATTC_WriteCharDescr(p_cb->conn_id, &in_char_descr_id, 748 p_cb->write_type, &descr_val, 749 p_cb->auth_req); 750 break; 751 752 case BTIF_GATTC_EXECUTE_WRITE: 753 BTA_GATTC_ExecuteWrite(p_cb->conn_id, p_cb->action); 754 break; 755 756 case BTIF_GATTC_REG_FOR_NOTIFICATION: 757 btif_to_bta_srvc_id(&in_char_id.srvc_id, &p_cb->srvc_id); 758 btif_to_bta_char_id(&in_char_id.char_id, &p_cb->char_id); 759 760 status = BTA_GATTC_RegisterForNotifications(p_cb->client_if, 761 p_cb->bd_addr.address, &in_char_id); 762 763 HAL_CBACK(bt_gatt_callbacks, client->register_for_notification_cb, 764 p_cb->conn_id, 1, status, &p_cb->srvc_id, 765 &p_cb->char_id); 766 break; 767 768 case BTIF_GATTC_DEREG_FOR_NOTIFICATION: 769 btif_to_bta_srvc_id(&in_char_id.srvc_id, &p_cb->srvc_id); 770 btif_to_bta_char_id(&in_char_id.char_id, &p_cb->char_id); 771 772 status = BTA_GATTC_DeregisterForNotifications(p_cb->client_if, 773 p_cb->bd_addr.address, &in_char_id); 774 775 HAL_CBACK(bt_gatt_callbacks, client->register_for_notification_cb, 776 p_cb->conn_id, 0, status, &p_cb->srvc_id, 777 &p_cb->char_id); 778 break; 779 780 case BTIF_GATTC_REFRESH: 781 BTA_GATTC_Refresh(p_cb->bd_addr.address); 782 break; 783 784 case BTIF_GATTC_READ_RSSI: 785 rssi_request_client_if = p_cb->client_if; 786 BTM_ReadRSSI (p_cb->bd_addr.address, (tBTM_CMPL_CB *)btm_read_rssi_cb); 787 break; 788 789 default: 790 ALOGE("%s: Unknown event (%d)!", __FUNCTION__, event); 791 break; 792 } 793} 794 795/******************************************************************************* 796** Client API Functions 797********************************************************************************/ 798 799static bt_status_t btif_gattc_register_app(bt_uuid_t *uuid) 800{ 801 CHECK_BTGATT_INIT(); 802 btif_gattc_cb_t btif_cb; 803 memcpy(&btif_cb.uuid, uuid, sizeof(bt_uuid_t)); 804 return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_REGISTER_APP, 805 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL); 806} 807 808static bt_status_t btif_gattc_unregister_app(int client_if ) 809{ 810 CHECK_BTGATT_INIT(); 811 btif_gattc_cb_t btif_cb; 812 btif_cb.client_if = (uint8_t) client_if; 813 return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_UNREGISTER_APP, 814 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL); 815} 816 817static bt_status_t btif_gattc_scan( int client_if, bool start ) 818{ 819 CHECK_BTGATT_INIT(); 820 btif_gattc_cb_t btif_cb; 821 btif_cb.client_if = (uint8_t) client_if; 822 return btif_transfer_context(btgattc_handle_event, start ? BTIF_GATTC_SCAN_START : BTIF_GATTC_SCAN_STOP, 823 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL); 824} 825 826static bt_status_t btif_gattc_open(int client_if, const bt_bdaddr_t *bd_addr, bool is_direct ) 827{ 828 CHECK_BTGATT_INIT(); 829 btif_gattc_cb_t btif_cb; 830 btif_cb.client_if = (uint8_t) client_if; 831 btif_cb.is_direct = is_direct ? 1 : 0; 832 bdcpy(btif_cb.bd_addr.address, bd_addr->address); 833 return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_OPEN, 834 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL); 835} 836 837static bt_status_t btif_gattc_close( int client_if, const bt_bdaddr_t *bd_addr, int conn_id) 838{ 839 CHECK_BTGATT_INIT(); 840 btif_gattc_cb_t btif_cb; 841 btif_cb.client_if = (uint8_t) client_if; 842 btif_cb.conn_id = (uint16_t) conn_id; 843 bdcpy(btif_cb.bd_addr.address, bd_addr->address); 844 return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_CLOSE, 845 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL); 846} 847 848static bt_status_t btif_gattc_refresh( int client_if, const bt_bdaddr_t *bd_addr ) 849{ 850 CHECK_BTGATT_INIT(); 851 btif_gattc_cb_t btif_cb; 852 btif_cb.client_if = (uint8_t) client_if; 853 bdcpy(btif_cb.bd_addr.address, bd_addr->address); 854 return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_REFRESH, 855 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL); 856} 857 858static bt_status_t btif_gattc_search_service(int conn_id, bt_uuid_t *filter_uuid ) 859{ 860 CHECK_BTGATT_INIT(); 861 btif_gattc_cb_t btif_cb; 862 btif_cb.conn_id = (uint16_t) conn_id; 863 btif_cb.search_all = filter_uuid ? 0 : 1; 864 if (filter_uuid) 865 memcpy(&btif_cb.uuid, filter_uuid, sizeof(bt_uuid_t)); 866 return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_SEARCH_SERVICE, 867 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL); 868} 869 870static bt_status_t btif_gattc_get_characteristic( int conn_id 871 , btgatt_srvc_id_t *srvc_id, btgatt_char_id_t *start_char_id) 872{ 873 CHECK_BTGATT_INIT(); 874 btif_gattc_cb_t btif_cb; 875 btif_cb.conn_id = (uint16_t) conn_id; 876 memcpy(&btif_cb.srvc_id, srvc_id, sizeof(btgatt_srvc_id_t)); 877 if (start_char_id) 878 { 879 memcpy(&btif_cb.char_id, start_char_id, sizeof(btgatt_char_id_t)); 880 return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_GET_NEXT_CHAR, 881 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL); 882 } 883 return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_GET_FIRST_CHAR, 884 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL); 885} 886 887static bt_status_t btif_gattc_get_descriptor( int conn_id 888 , btgatt_srvc_id_t *srvc_id, btgatt_char_id_t *char_id 889 , bt_uuid_t *start_descr_id) 890{ 891 CHECK_BTGATT_INIT(); 892 btif_gattc_cb_t btif_cb; 893 btif_cb.conn_id = (uint16_t) conn_id; 894 memcpy(&btif_cb.srvc_id, srvc_id, sizeof(btgatt_srvc_id_t)); 895 memcpy(&btif_cb.char_id, char_id, sizeof(btgatt_char_id_t)); 896 if (start_descr_id) 897 { 898 memcpy(&btif_cb.uuid, start_descr_id, sizeof(bt_uuid_t)); 899 return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_GET_NEXT_CHAR_DESCR, 900 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL); 901 } 902 903 return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_GET_FIRST_CHAR_DESCR, 904 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL); 905} 906 907static bt_status_t btif_gattc_get_included_service(int conn_id, btgatt_srvc_id_t *srvc_id, 908 btgatt_srvc_id_t *start_incl_srvc_id) 909{ 910 CHECK_BTGATT_INIT(); 911 btif_gattc_cb_t btif_cb; 912 btif_cb.conn_id = (uint16_t) conn_id; 913 memcpy(&btif_cb.srvc_id, srvc_id, sizeof(btgatt_srvc_id_t)); 914 if (start_incl_srvc_id) 915 { 916 memcpy(&btif_cb.incl_srvc_id, start_incl_srvc_id, sizeof(btgatt_srvc_id_t)); 917 return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_GET_NEXT_INCL_SERVICE, 918 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL); 919 } 920 return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_GET_FIRST_INCL_SERVICE, 921 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL); 922} 923 924static bt_status_t btif_gattc_read_char(int conn_id, btgatt_srvc_id_t* srvc_id, 925 btgatt_char_id_t* char_id, int auth_req ) 926{ 927 CHECK_BTGATT_INIT(); 928 btif_gattc_cb_t btif_cb; 929 btif_cb.conn_id = (uint16_t) conn_id; 930 btif_cb.auth_req = (uint8_t) auth_req; 931 memcpy(&btif_cb.srvc_id, srvc_id, sizeof(btgatt_srvc_id_t)); 932 memcpy(&btif_cb.char_id, char_id, sizeof(btgatt_char_id_t)); 933 return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_READ_CHAR, 934 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL); 935} 936 937static bt_status_t btif_gattc_read_char_descr(int conn_id, btgatt_srvc_id_t* srvc_id, 938 btgatt_char_id_t* char_id, bt_uuid_t* descr_id, 939 int auth_req ) 940{ 941 CHECK_BTGATT_INIT(); 942 btif_gattc_cb_t btif_cb; 943 btif_cb.conn_id = (uint16_t) conn_id; 944 btif_cb.auth_req = (uint8_t) auth_req; 945 memcpy(&btif_cb.srvc_id, srvc_id, sizeof(btgatt_srvc_id_t)); 946 memcpy(&btif_cb.char_id, char_id, sizeof(btgatt_char_id_t)); 947 memcpy(&btif_cb.uuid, descr_id, sizeof(bt_uuid_t)); 948 return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_READ_CHAR_DESCR, 949 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL); 950} 951 952static bt_status_t btif_gattc_write_char(int conn_id, btgatt_srvc_id_t* srvc_id, 953 btgatt_char_id_t* char_id, int write_type, 954 int len, int auth_req, char* p_value) 955{ 956 CHECK_BTGATT_INIT(); 957 btif_gattc_cb_t btif_cb; 958 btif_cb.conn_id = (uint16_t) conn_id; 959 btif_cb.auth_req = (uint8_t) auth_req; 960 btif_cb.write_type = (uint8_t) write_type; 961 btif_cb.len = len > BTGATT_MAX_ATTR_LEN ? BTGATT_MAX_ATTR_LEN : len; 962 memcpy(&btif_cb.srvc_id, srvc_id, sizeof(btgatt_srvc_id_t)); 963 memcpy(&btif_cb.char_id, char_id, sizeof(btgatt_char_id_t)); 964 memcpy(btif_cb.value, p_value, btif_cb.len); 965 return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_WRITE_CHAR, 966 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL); 967} 968 969static bt_status_t btif_gattc_write_char_descr(int conn_id, btgatt_srvc_id_t* srvc_id, 970 btgatt_char_id_t* char_id, bt_uuid_t* descr_id, 971 int write_type, int len, int auth_req, 972 char* p_value) 973{ 974 CHECK_BTGATT_INIT(); 975 btif_gattc_cb_t btif_cb; 976 btif_cb.conn_id = (uint16_t) conn_id; 977 btif_cb.auth_req = (uint8_t) auth_req; 978 btif_cb.write_type = (uint8_t) write_type; 979 btif_cb.len = len > BTGATT_MAX_ATTR_LEN ? BTGATT_MAX_ATTR_LEN : len; 980 memcpy(&btif_cb.srvc_id, srvc_id, sizeof(btgatt_srvc_id_t)); 981 memcpy(&btif_cb.char_id, char_id, sizeof(btgatt_char_id_t)); 982 memcpy(&btif_cb.uuid, descr_id, sizeof(bt_uuid_t)); 983 memcpy(btif_cb.value, p_value, btif_cb.len); 984 return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_WRITE_CHAR_DESCR, 985 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL); 986} 987 988static bt_status_t btif_gattc_execute_write(int conn_id, int execute) 989{ 990 CHECK_BTGATT_INIT(); 991 btif_gattc_cb_t btif_cb; 992 btif_cb.conn_id = (uint16_t) conn_id; 993 btif_cb.action = (uint8_t) execute; 994 return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_EXECUTE_WRITE, 995 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL); 996} 997 998static bt_status_t btif_gattc_reg_for_notification(int client_if, const bt_bdaddr_t *bd_addr, 999 btgatt_srvc_id_t* srvc_id, 1000 btgatt_char_id_t* char_id) 1001{ 1002 CHECK_BTGATT_INIT(); 1003 btif_gattc_cb_t btif_cb; 1004 btif_cb.client_if = (uint8_t) client_if; 1005 bdcpy(btif_cb.bd_addr.address, bd_addr->address); 1006 memcpy(&btif_cb.srvc_id, srvc_id, sizeof(btgatt_srvc_id_t)); 1007 memcpy(&btif_cb.char_id, char_id, sizeof(btgatt_char_id_t)); 1008 return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_REG_FOR_NOTIFICATION, 1009 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL); 1010} 1011 1012static bt_status_t btif_gattc_dereg_for_notification(int client_if, const bt_bdaddr_t *bd_addr, 1013 btgatt_srvc_id_t* srvc_id, 1014 btgatt_char_id_t* char_id) 1015{ 1016 CHECK_BTGATT_INIT(); 1017 btif_gattc_cb_t btif_cb; 1018 btif_cb.client_if = (uint8_t) client_if; 1019 bdcpy(btif_cb.bd_addr.address, bd_addr->address); 1020 memcpy(&btif_cb.srvc_id, srvc_id, sizeof(btgatt_srvc_id_t)); 1021 memcpy(&btif_cb.char_id, char_id, sizeof(btgatt_char_id_t)); 1022 return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_DEREG_FOR_NOTIFICATION, 1023 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL); 1024} 1025 1026static bt_status_t btif_gattc_read_remote_rssi(int client_if, const bt_bdaddr_t *bd_addr) 1027{ 1028 CHECK_BTGATT_INIT(); 1029 btif_gattc_cb_t btif_cb; 1030 btif_cb.client_if = (uint8_t) client_if; 1031 bdcpy(btif_cb.bd_addr.address, bd_addr->address); 1032 return btif_transfer_context(btgattc_handle_event, BTIF_GATTC_READ_RSSI, 1033 (char*) &btif_cb, sizeof(btif_gattc_cb_t), NULL); 1034} 1035 1036static int btif_gattc_get_device_type( const bt_bdaddr_t *bd_addr ) 1037{ 1038 int device_type = 0; 1039 char bd_addr_str[18] = {0}; 1040 1041 bd2str(bd_addr, &bd_addr_str); 1042 if (btif_config_get_int("Remote", bd_addr_str, "DevType", &device_type)) 1043 return device_type; 1044 return 0; 1045} 1046 1047extern bt_status_t btif_gattc_test_command_impl(int command, btgatt_test_params_t* params); 1048 1049static bt_status_t btif_gattc_test_command(int command, btgatt_test_params_t* params) 1050{ 1051 return btif_gattc_test_command_impl(command, params); 1052} 1053 1054 1055const btgatt_client_interface_t btgattClientInterface = { 1056 btif_gattc_register_app, 1057 btif_gattc_unregister_app, 1058 btif_gattc_scan, 1059 btif_gattc_open, 1060 btif_gattc_close, 1061 btif_gattc_refresh, 1062 btif_gattc_search_service, 1063 btif_gattc_get_included_service, 1064 btif_gattc_get_characteristic, 1065 btif_gattc_get_descriptor, 1066 btif_gattc_read_char, 1067 btif_gattc_write_char, 1068 btif_gattc_read_char_descr, 1069 btif_gattc_write_char_descr, 1070 btif_gattc_execute_write, 1071 btif_gattc_reg_for_notification, 1072 btif_gattc_dereg_for_notification, 1073 btif_gattc_read_remote_rssi, 1074 btif_gattc_get_device_type, 1075 btif_gattc_test_command 1076}; 1077 1078#endif 1079