1/****************************************************************************** 2 * 3 * Copyright (C) 2003-2012 Broadcom Corporation 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at: 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 ******************************************************************************/ 18 19/****************************************************************************** 20 * 21 * This file contains the GATT client action functions for the state 22 * machine. 23 * 24 ******************************************************************************/ 25#define LOG_TAG "bt_bta_gattc" 26 27#include "bt_target.h" 28 29#include "utl.h" 30#include "gki.h" 31#include "bta_sys.h" 32 33#include "bta_gattc_int.h" 34#include "l2c_api.h" 35 36#if (defined BTA_HH_LE_INCLUDED && BTA_HH_LE_INCLUDED == TRUE) 37#include "bta_hh_int.h" 38#endif 39 40#include "btif/include/btif_debug_conn.h" 41 42#include <string.h> 43 44#include "osi/include/log.h" 45 46#if BTA_GATT_INCLUDED && BLE_INCLUDED == TRUE 47 48/***************************************************************************** 49** Constants 50*****************************************************************************/ 51static void bta_gattc_conn_cback(tGATT_IF gattc_if, BD_ADDR bda, UINT16 conn_id, 52 BOOLEAN connected, tGATT_DISCONN_REASON reason, 53 tBT_TRANSPORT transport); 54 55static void bta_gattc_cmpl_cback(UINT16 conn_id, tGATTC_OPTYPE op, tGATT_STATUS status, 56 tGATT_CL_COMPLETE *p_data); 57 58static void bta_gattc_deregister_cmpl(tBTA_GATTC_RCB *p_clreg); 59static void bta_gattc_enc_cmpl_cback(tGATT_IF gattc_if, BD_ADDR bda); 60static void bta_gattc_cong_cback (UINT16 conn_id, BOOLEAN congested); 61 62static tGATT_CBACK bta_gattc_cl_cback = 63{ 64 bta_gattc_conn_cback, 65 bta_gattc_cmpl_cback, 66 bta_gattc_disc_res_cback, 67 bta_gattc_disc_cmpl_cback, 68 NULL, 69 bta_gattc_enc_cmpl_cback, 70 bta_gattc_cong_cback 71}; 72 73/* opcode(tGATTC_OPTYPE) order has to be comply with internal event order */ 74static UINT16 bta_gattc_opcode_to_int_evt[] = 75{ 76 BTA_GATTC_API_READ_EVT, 77 BTA_GATTC_API_WRITE_EVT, 78 BTA_GATTC_API_EXEC_EVT, 79 BTA_GATTC_API_CFG_MTU_EVT 80}; 81 82#if (BT_TRACE_VERBOSE == TRUE) 83static const char *bta_gattc_op_code_name[] = 84{ 85 "Unknown", 86 "Discovery", 87 "Read", 88 "Write", 89 "Exec", 90 "Config", 91 "Notification", 92 "Indication" 93}; 94#endif 95/***************************************************************************** 96** Action Functions 97*****************************************************************************/ 98 99/******************************************************************************* 100** 101** Function bta_gattc_enable 102** 103** Description Enables GATTC module 104** 105** 106** Returns void 107** 108*******************************************************************************/ 109static void bta_gattc_enable(tBTA_GATTC_CB *p_cb) 110{ 111 APPL_TRACE_DEBUG("bta_gattc_enable"); 112 113 if (p_cb->state == BTA_GATTC_STATE_DISABLED) 114 { 115 /* initialize control block */ 116 memset(&bta_gattc_cb, 0, sizeof(tBTA_GATTC_CB)); 117 p_cb->state = BTA_GATTC_STATE_ENABLED; 118 } 119 else 120 { 121 APPL_TRACE_DEBUG("GATTC is arelady enabled"); 122 } 123} 124 125 126/******************************************************************************* 127** 128** Function bta_gattc_disable 129** 130** Description Disable GATTC module by cleaning up all active connections 131** and deregister all application. 132** 133** Returns void 134** 135*******************************************************************************/ 136void bta_gattc_disable(tBTA_GATTC_CB *p_cb) 137{ 138 UINT8 i; 139 140 APPL_TRACE_DEBUG("bta_gattc_disable"); 141 142 if (p_cb->state != BTA_GATTC_STATE_ENABLED) 143 { 144 APPL_TRACE_ERROR("not enabled or disable in pogress"); 145 return; 146 } 147 148 for (i = 0; i <BTA_GATTC_CL_MAX; i ++) 149 { 150 if (p_cb->cl_rcb[i].in_use) 151 { 152 p_cb->state = BTA_GATTC_STATE_DISABLING; 153 /* don't deregister HH GATT IF */ 154 /* HH GATT IF will be deregistered by bta_hh_le_deregister when disable HH */ 155#if (defined BTA_HH_LE_INCLUDED && BTA_HH_LE_INCLUDED == TRUE) 156 if (!bta_hh_le_is_hh_gatt_if(p_cb->cl_rcb[i].client_if)) { 157#endif 158 bta_gattc_deregister(p_cb, &p_cb->cl_rcb[i]); 159#if (defined BTA_HH_LE_INCLUDED && BTA_HH_LE_INCLUDED == TRUE) 160 } 161#endif 162 } 163 } 164 165 /* no registered apps, indicate disable completed */ 166 if (p_cb->state != BTA_GATTC_STATE_DISABLING) 167 { 168 p_cb->state = BTA_GATTC_STATE_DISABLED; 169 memset(p_cb, 0, sizeof(tBTA_GATTC_CB)); 170 } 171} 172 173/******************************************************************************* 174** 175** Function bta_gattc_register 176** 177** Description Register a GATT client application with BTA. 178** 179** Returns void 180** 181*******************************************************************************/ 182void bta_gattc_register(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA *p_data) 183{ 184 tBTA_GATTC cb_data; 185 UINT8 i; 186 tBT_UUID *p_app_uuid = &p_data->api_reg.app_uuid; 187 tBTA_GATTC_INT_START_IF *p_buf; 188 tBTA_GATT_STATUS status = BTA_GATT_NO_RESOURCES; 189 190 191 APPL_TRACE_DEBUG("bta_gattc_register state %d",p_cb->state); 192 memset(&cb_data, 0, sizeof(cb_data)); 193 cb_data.reg_oper.status = BTA_GATT_NO_RESOURCES; 194 195 /* check if GATTC module is already enabled . Else enable */ 196 if (p_cb->state == BTA_GATTC_STATE_DISABLED) 197 { 198 bta_gattc_enable (p_cb); 199 } 200 /* todo need to check duplicate uuid */ 201 for (i = 0; i < BTA_GATTC_CL_MAX; i ++) 202 { 203 if (!p_cb->cl_rcb[i].in_use) 204 { 205 if ((p_app_uuid == NULL) || (p_cb->cl_rcb[i].client_if = GATT_Register(p_app_uuid, &bta_gattc_cl_cback)) == 0) 206 { 207 APPL_TRACE_ERROR("Register with GATT stack failed."); 208 status = BTA_GATT_ERROR; 209 } 210 else 211 { 212 p_cb->cl_rcb[i].in_use = TRUE; 213 p_cb->cl_rcb[i].p_cback = p_data->api_reg.p_cback; 214 memcpy(&p_cb->cl_rcb[i].app_uuid, p_app_uuid, sizeof(tBT_UUID)); 215 216 /* BTA use the same client interface as BTE GATT statck */ 217 cb_data.reg_oper.client_if = p_cb->cl_rcb[i].client_if; 218 219 if ((p_buf = (tBTA_GATTC_INT_START_IF *) GKI_getbuf(sizeof(tBTA_GATTC_INT_START_IF))) != NULL) 220 { 221 p_buf->hdr.event = BTA_GATTC_INT_START_IF_EVT; 222 p_buf->client_if = p_cb->cl_rcb[i].client_if; 223 224 bta_sys_sendmsg(p_buf); 225 status = BTA_GATT_OK; 226 } 227 else 228 { 229 GATT_Deregister(p_cb->cl_rcb[i].client_if); 230 231 status = BTA_GATT_NO_RESOURCES; 232 memset( &p_cb->cl_rcb[i], 0 , sizeof(tBTA_GATTC_RCB)); 233 } 234 break; 235 } 236 } 237 } 238 239 /* callback with register event */ 240 if (p_data->api_reg.p_cback) 241 { 242 if (p_app_uuid != NULL) 243 memcpy(&(cb_data.reg_oper.app_uuid),p_app_uuid,sizeof(tBT_UUID)); 244 245 cb_data.reg_oper.status = status; 246 (*p_data->api_reg.p_cback)(BTA_GATTC_REG_EVT, (tBTA_GATTC *)&cb_data); 247 } 248} 249/******************************************************************************* 250** 251** Function bta_gattc_start_if 252** 253** Description start an application interface. 254** 255** Returns none. 256** 257*******************************************************************************/ 258void bta_gattc_start_if(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA *p_msg) 259{ 260 UNUSED(p_cb); 261 262 if (bta_gattc_cl_get_regcb(p_msg->int_start_if.client_if) !=NULL ) 263 { 264 GATT_StartIf(p_msg->int_start_if.client_if); 265 } 266 else 267 { 268 APPL_TRACE_ERROR("Unable to start app.: Unknown interface =%d",p_msg->int_start_if.client_if ); 269 } 270} 271/******************************************************************************* 272** 273** Function bta_gattc_deregister 274** 275** Description De-Register a GATT client application with BTA. 276** 277** Returns void 278** 279*******************************************************************************/ 280void bta_gattc_deregister(tBTA_GATTC_CB *p_cb, tBTA_GATTC_RCB *p_clreg) 281{ 282 UINT8 i; 283 BT_HDR buf; 284 285 if (p_clreg != NULL) 286 { 287 /* remove bg connection associated with this rcb */ 288 for (i = 0; i < BTA_GATTC_KNOWN_SR_MAX; i ++) 289 { 290 if (p_cb->bg_track[i].in_use) 291 { 292 if (p_cb->bg_track[i].cif_mask & (1 <<(p_clreg->client_if - 1))) 293 { 294 bta_gattc_mark_bg_conn(p_clreg->client_if, p_cb->bg_track[i].remote_bda, FALSE, FALSE); 295 GATT_CancelConnect(p_clreg->client_if, p_cb->bg_track[i].remote_bda, FALSE); 296 } 297 if (p_cb->bg_track[i].cif_adv_mask & (1 <<(p_clreg->client_if - 1))) 298 { 299 bta_gattc_mark_bg_conn(p_clreg->client_if, p_cb->bg_track[i].remote_bda, FALSE, TRUE); 300 } 301 } 302 } 303 304 if (p_clreg->num_clcb > 0) 305 { 306 /* close all CLCB related to this app */ 307 for (i= 0; i < BTA_GATTC_CLCB_MAX; i ++) 308 { 309 if (p_cb->clcb[i].in_use && (p_cb->clcb[i].p_rcb == p_clreg)) 310 { 311 p_clreg->dereg_pending = TRUE; 312 313 buf.event = BTA_GATTC_API_CLOSE_EVT; 314 buf.layer_specific = p_cb->clcb[i].bta_conn_id; 315 bta_gattc_close(&p_cb->clcb[i], (tBTA_GATTC_DATA *)&buf) ; 316 } 317 } 318 } 319 else 320 bta_gattc_deregister_cmpl(p_clreg); 321 } 322 else 323 { 324 APPL_TRACE_ERROR("bta_gattc_deregister Deregister Failedm unknown client cif"); 325 } 326} 327/******************************************************************************* 328** 329** Function bta_gattc_process_api_open 330** 331** Description process connect API request. 332** 333** Returns void 334** 335*******************************************************************************/ 336void bta_gattc_process_api_open (tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA * p_msg) 337{ 338 UINT16 event = ((BT_HDR *)p_msg)->event; 339 tBTA_GATTC_CLCB *p_clcb = NULL; 340 tBTA_GATTC_RCB *p_clreg = bta_gattc_cl_get_regcb(p_msg->api_conn.client_if); 341 UNUSED(p_cb); 342 343 if (p_clreg != NULL) 344 { 345 if (p_msg->api_conn.is_direct) 346 { 347 if ((p_clcb = bta_gattc_find_alloc_clcb(p_msg->api_conn.client_if, 348 p_msg->api_conn.remote_bda, 349 p_msg->api_conn.transport)) != NULL) 350 { 351 bta_gattc_sm_execute(p_clcb, event, p_msg); 352 } 353 else 354 { 355 APPL_TRACE_ERROR("No resources to open a new connection."); 356 357 bta_gattc_send_open_cback(p_clreg, 358 BTA_GATT_NO_RESOURCES, 359 p_msg->api_conn.remote_bda, 360 BTA_GATT_INVALID_CONN_ID, 361 p_msg->api_conn.transport, 0); 362 } 363 } 364 else 365 { 366 bta_gattc_init_bk_conn(&p_msg->api_conn, p_clreg); 367 } 368 } 369 else 370 { 371 APPL_TRACE_ERROR("bta_gattc_process_api_open Failed, unknown client_if: %d", 372 p_msg->api_conn.client_if); 373 } 374} 375/******************************************************************************* 376** 377** Function bta_gattc_process_api_open_cancel 378** 379** Description process connect API request. 380** 381** Returns void 382** 383*******************************************************************************/ 384void bta_gattc_process_api_open_cancel (tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA * p_msg) 385{ 386 UINT16 event = ((BT_HDR *)p_msg)->event; 387 tBTA_GATTC_CLCB *p_clcb = NULL; 388 tBTA_GATTC_RCB *p_clreg; 389 tBTA_GATTC cb_data; 390 UNUSED(p_cb); 391 392 if (p_msg->api_cancel_conn.is_direct) 393 { 394 if ((p_clcb = bta_gattc_find_clcb_by_cif(p_msg->api_cancel_conn.client_if, 395 p_msg->api_cancel_conn.remote_bda, 396 BTA_GATT_TRANSPORT_LE)) != NULL) 397 { 398 bta_gattc_sm_execute(p_clcb, event, p_msg); 399 } 400 else 401 { 402 APPL_TRACE_ERROR("No such connection need to be cancelled"); 403 404 p_clreg = bta_gattc_cl_get_regcb(p_msg->api_cancel_conn.client_if); 405 406 if (p_clreg && p_clreg->p_cback) 407 { 408 cb_data.status = BTA_GATT_ERROR; 409 (*p_clreg->p_cback)(BTA_GATTC_CANCEL_OPEN_EVT, &cb_data); 410 } 411 } 412 } 413 else 414 { 415 bta_gattc_cancel_bk_conn(&p_msg->api_cancel_conn); 416 417 } 418} 419 420/******************************************************************************* 421** 422** Function bta_gattc_process_enc_cmpl 423** 424** Description process encryption complete message. 425** 426** Returns void 427** 428*******************************************************************************/ 429void bta_gattc_process_enc_cmpl(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA *p_msg) 430{ 431 tBTA_GATTC_RCB *p_clreg; 432 tBTA_GATTC cb_data; 433 UNUSED(p_cb); 434 435 p_clreg = bta_gattc_cl_get_regcb(p_msg->enc_cmpl.client_if); 436 437 if (p_clreg && p_clreg->p_cback) 438 { 439 memset(&cb_data, 0, sizeof(tBTA_GATTC)); 440 441 cb_data.enc_cmpl.client_if = p_msg->enc_cmpl.client_if; 442 bdcpy(cb_data.enc_cmpl.remote_bda, p_msg->enc_cmpl.remote_bda); 443 444 (*p_clreg->p_cback)(BTA_GATTC_ENC_CMPL_CB_EVT, &cb_data); 445 } 446} 447 448/******************************************************************************* 449** 450** Function bta_gattc_cancel_open_error 451** 452** Description 453** 454** Returns void 455** 456*******************************************************************************/ 457void bta_gattc_cancel_open_error(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data) 458{ 459 tBTA_GATTC cb_data; 460 UNUSED(p_data); 461 462 cb_data.status=BTA_GATT_ERROR; 463 464 if ( p_clcb && p_clcb->p_rcb && p_clcb->p_rcb->p_cback ) 465 (*p_clcb->p_rcb->p_cback)(BTA_GATTC_CANCEL_OPEN_EVT, &cb_data); 466} 467 468/******************************************************************************* 469** 470** Function bta_gattc_open_error 471** 472** Description 473** 474** Returns void 475** 476*******************************************************************************/ 477void bta_gattc_open_error(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data) 478{ 479 UNUSED(p_data); 480 481 APPL_TRACE_ERROR("Connection already opened. wrong state"); 482 483 bta_gattc_send_open_cback(p_clcb->p_rcb, 484 BTA_GATT_OK, 485 p_clcb->bda, 486 p_clcb->bta_conn_id, 487 p_clcb->transport, 488 0); 489} 490/******************************************************************************* 491** 492** Function bta_gattc_open_fail 493** 494** Description 495** 496** Returns void 497** 498*******************************************************************************/ 499void bta_gattc_open_fail(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data) 500{ 501 UNUSED(p_data); 502 503 bta_gattc_send_open_cback(p_clcb->p_rcb, 504 BTA_GATT_ERROR, 505 p_clcb->bda, 506 p_clcb->bta_conn_id, 507 p_clcb->transport, 508 0); 509 /* open failure, remove clcb */ 510 bta_gattc_clcb_dealloc(p_clcb); 511} 512 513/******************************************************************************* 514** 515** Function bta_gattc_open 516** 517** Description Process API connection function. 518** 519** Returns void 520** 521*******************************************************************************/ 522void bta_gattc_open(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data) 523{ 524 tBTA_GATTC_DATA gattc_data; 525 526 /* open/hold a connection */ 527 if (!GATT_Connect(p_clcb->p_rcb->client_if, p_data->api_conn.remote_bda, 528 TRUE, p_data->api_conn.transport)) 529 { 530 APPL_TRACE_ERROR("Connection open failure"); 531 532 bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_OPEN_FAIL_EVT, p_data); 533 } 534 else 535 { 536 /* a connected remote device */ 537 if (GATT_GetConnIdIfConnected(p_clcb->p_rcb->client_if, 538 p_data->api_conn.remote_bda, 539 &p_clcb->bta_conn_id, 540 p_data->api_conn.transport)) 541 { 542 gattc_data.int_conn.hdr.layer_specific = p_clcb->bta_conn_id; 543 544 bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_CONN_EVT, &gattc_data); 545 } 546 /* else wait for the callback event */ 547 } 548} 549/******************************************************************************* 550** 551** Function bta_gattc_init_bk_conn 552** 553** Description Process API Open for a background connection 554** 555** Returns void 556** 557*******************************************************************************/ 558void bta_gattc_init_bk_conn(tBTA_GATTC_API_OPEN *p_data, tBTA_GATTC_RCB *p_clreg) 559{ 560 tBTA_GATT_STATUS status = BTA_GATT_NO_RESOURCES; 561 UINT16 conn_id; 562 tBTA_GATTC_CLCB *p_clcb; 563 tBTA_GATTC_DATA gattc_data; 564 565 if (bta_gattc_mark_bg_conn(p_data->client_if, p_data->remote_bda, TRUE, FALSE)) 566 { 567 /* always call open to hold a connection */ 568 if (!GATT_Connect(p_data->client_if, p_data->remote_bda, FALSE, p_data->transport)) 569 { 570 uint8_t *bda = (uint8_t *)p_data->remote_bda; 571 status = BTA_GATT_ERROR; 572 APPL_TRACE_ERROR("%s unable to connect to remote bd_addr:%02x:%02x:%02x:%02x:%02x:%02x", 573 __func__, bda[0], bda[1], bda[2], bda[3], bda[4], bda[5]); 574 575 } 576 else 577 { 578 status = BTA_GATT_OK; 579 580 /* if is a connected remote device */ 581 if (GATT_GetConnIdIfConnected(p_data->client_if, 582 p_data->remote_bda, 583 &conn_id, 584 p_data->transport)) 585 { 586 if ((p_clcb = bta_gattc_find_alloc_clcb(p_data->client_if, p_data->remote_bda, 587 BTA_GATT_TRANSPORT_LE)) != NULL) 588 { 589 gattc_data.hdr.layer_specific = p_clcb->bta_conn_id = conn_id; 590 591 /* open connection */ 592 bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_CONN_EVT, &gattc_data); 593 status = BTA_GATT_OK; 594 } 595 } 596 } 597 } 598 599 /* open failure, report OPEN_EVT */ 600 if (status != BTA_GATT_OK) 601 { 602 bta_gattc_send_open_cback(p_clreg, status, p_data->remote_bda, 603 BTA_GATT_INVALID_CONN_ID, BTA_GATT_TRANSPORT_LE, 0); 604 } 605} 606/******************************************************************************* 607** 608** Function bta_gattc_cancel_bk_conn 609** 610** Description Process API Cancel Open for a background connection 611** 612** Returns void 613** 614*******************************************************************************/ 615void bta_gattc_cancel_bk_conn(tBTA_GATTC_API_CANCEL_OPEN *p_data) 616{ 617 tBTA_GATTC_RCB *p_clreg; 618 tBTA_GATTC cb_data; 619 cb_data.status = BTA_GATT_ERROR; 620 621 /* remove the device from the bg connection mask */ 622 if (bta_gattc_mark_bg_conn(p_data->client_if, p_data->remote_bda, FALSE, FALSE)) 623 { 624 if (GATT_CancelConnect(p_data->client_if, p_data->remote_bda, FALSE)) 625 { 626 cb_data.status = BTA_GATT_OK; 627 } 628 else 629 { 630 APPL_TRACE_ERROR("bta_gattc_cancel_bk_conn failed"); 631 } 632 } 633 p_clreg = bta_gattc_cl_get_regcb(p_data->client_if); 634 635 if (p_clreg && p_clreg->p_cback) 636 { 637 (*p_clreg->p_cback)(BTA_GATTC_CANCEL_OPEN_EVT, &cb_data); 638 } 639 640} 641/******************************************************************************* 642** 643** Function bta_gattc_int_cancel_open_ok 644** 645** Description 646** 647** Returns void 648** 649*******************************************************************************/ 650void bta_gattc_cancel_open_ok(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data) 651{ 652 tBTA_GATTC cb_data; 653 UNUSED(p_data); 654 655 if ( p_clcb->p_rcb->p_cback ) 656 { 657 cb_data.status = BTA_GATT_OK; 658 (*p_clcb->p_rcb->p_cback)(BTA_GATTC_CANCEL_OPEN_EVT, &cb_data); 659 } 660 661 bta_gattc_clcb_dealloc(p_clcb); 662} 663/******************************************************************************* 664** 665** Function bta_gattc_cancel_open 666** 667** Description 668** 669** Returns void 670** 671*******************************************************************************/ 672void bta_gattc_cancel_open(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data) 673{ 674 tBTA_GATTC cb_data; 675 676 if (GATT_CancelConnect(p_clcb->p_rcb->client_if, p_data->api_cancel_conn.remote_bda, TRUE)) 677 { 678 bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_CANCEL_OPEN_OK_EVT, p_data); 679 } 680 else 681 { 682 if ( p_clcb->p_rcb->p_cback ) 683 { 684 cb_data.status = BTA_GATT_ERROR; 685 (*p_clcb->p_rcb->p_cback)(BTA_GATTC_CANCEL_OPEN_EVT, &cb_data); 686 } 687 } 688} 689/******************************************************************************* 690** 691** Function bta_gattc_conn 692** 693** Description receive connection callback from stack 694** 695** Returns void 696** 697*******************************************************************************/ 698void bta_gattc_conn(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data) 699{ 700 tBTA_GATTC_IF gatt_if; 701 APPL_TRACE_DEBUG("bta_gattc_conn server cache state=%d",p_clcb->p_srcb->state); 702 703 if (p_data != NULL) 704 { 705 APPL_TRACE_DEBUG("bta_gattc_conn conn_id=%d",p_data->hdr.layer_specific); 706 p_clcb->bta_conn_id = p_data->int_conn.hdr.layer_specific; 707 708 GATT_GetConnectionInfor(p_data->hdr.layer_specific, 709 &gatt_if, p_clcb->bda, &p_clcb->transport); 710 } 711 712 p_clcb->p_srcb->connected = TRUE; 713 714 if (p_clcb->p_srcb->mtu == 0) 715 p_clcb->p_srcb->mtu = GATT_DEF_BLE_MTU_SIZE; 716 717 /* start database cache if needed */ 718 if (p_clcb->p_srcb->p_srvc_cache == NULL || 719 p_clcb->p_srcb->state != BTA_GATTC_SERV_IDLE) 720 { 721 if (p_clcb->p_srcb->state == BTA_GATTC_SERV_IDLE) 722 { 723 p_clcb->p_srcb->state = BTA_GATTC_SERV_LOAD; 724 bta_gattc_sm_execute(p_clcb, BTA_GATTC_START_CACHE_EVT, NULL); 725 } 726 else /* cache is building */ 727 p_clcb->state = BTA_GATTC_DISCOVER_ST; 728 } 729 730 else 731 { 732 /* a pending service handle change indication */ 733 if (p_clcb->p_srcb->srvc_hdl_chg) 734 { 735 p_clcb->p_srcb->srvc_hdl_chg = FALSE; 736 /* start discovery */ 737 bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_DISCOVER_EVT, NULL); 738 } 739 } 740 741 if (p_clcb->p_rcb) 742 { 743 /* there is no RM for GATT */ 744 if (p_clcb->transport == BTA_TRANSPORT_BR_EDR) 745 bta_sys_conn_open(BTA_ID_GATTC, BTA_ALL_APP_ID, p_clcb->bda); 746 747 bta_gattc_send_open_cback(p_clcb->p_rcb, 748 BTA_GATT_OK, 749 p_clcb->bda, 750 p_clcb->bta_conn_id, 751 p_clcb->transport, 752 p_clcb->p_srcb->mtu); 753 } 754 } 755/******************************************************************************* 756** 757** Function bta_gattc_close_fail 758** 759** Description close a connection. 760** 761** Returns void 762** 763*******************************************************************************/ 764void bta_gattc_close_fail(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data) 765{ 766 tBTA_GATTC cb_data; 767 768 if ( p_clcb->p_rcb->p_cback ) 769 { 770 memset(&cb_data, 0, sizeof(tBTA_GATTC)); 771 cb_data.close.client_if = p_clcb->p_rcb->client_if; 772 cb_data.close.conn_id = p_data->hdr.layer_specific; 773 bdcpy(cb_data.close.remote_bda, p_clcb->bda); 774 cb_data.close.status = BTA_GATT_ERROR; 775 cb_data.close.reason = BTA_GATT_CONN_NONE; 776 777 778 (*p_clcb->p_rcb->p_cback)(BTA_GATTC_CLOSE_EVT, &cb_data); 779 } 780} 781/******************************************************************************* 782** 783** Function bta_gattc_api_close 784** 785** Description close a GATTC connection. 786** 787** Returns void 788** 789*******************************************************************************/ 790void bta_gattc_close(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data) 791{ 792 tBTA_GATTC_CBACK *p_cback = p_clcb->p_rcb->p_cback; 793 tBTA_GATTC_RCB *p_clreg = p_clcb->p_rcb; 794 tBTA_GATTC cb_data; 795 796 APPL_TRACE_DEBUG("bta_gattc_close conn_id=%d",p_clcb->bta_conn_id); 797 798 cb_data.close.client_if = p_clcb->p_rcb->client_if; 799 cb_data.close.conn_id = p_clcb->bta_conn_id; 800 cb_data.close.reason = p_clcb->reason; 801 cb_data.close.status = p_clcb->status; 802 bdcpy(cb_data.close.remote_bda, p_clcb->bda); 803 804 if (p_clcb->transport == BTA_TRANSPORT_BR_EDR) 805 bta_sys_conn_close( BTA_ID_GATTC ,BTA_ALL_APP_ID, p_clcb->bda); 806 807 bta_gattc_clcb_dealloc(p_clcb); 808 809 if (p_data->hdr.event == BTA_GATTC_API_CLOSE_EVT) 810 { 811 cb_data.close.status = GATT_Disconnect(p_data->hdr.layer_specific); 812 } 813 else if (p_data->hdr.event == BTA_GATTC_INT_DISCONN_EVT) 814 { 815 cb_data.close.status = p_data->int_conn.reason; 816 cb_data.close.reason = p_data->int_conn.reason; 817 } 818 819 if(p_cback) 820 (* p_cback)(BTA_GATTC_CLOSE_EVT, (tBTA_GATTC *)&cb_data); 821 822 if (p_clreg->num_clcb == 0 && p_clreg->dereg_pending) 823 { 824 bta_gattc_deregister_cmpl(p_clreg); 825 } 826} 827/******************************************************************************* 828** 829** Function bta_gattc_reset_discover_st 830** 831** Description when a SRCB finished discovery, tell all related clcb. 832** 833** Returns None. 834** 835*******************************************************************************/ 836void bta_gattc_reset_discover_st(tBTA_GATTC_SERV *p_srcb, tBTA_GATT_STATUS status) 837{ 838 tBTA_GATTC_CB *p_cb = &bta_gattc_cb; 839 UINT8 i; 840 841 for (i = 0; i < BTA_GATTC_CLCB_MAX; i ++) 842 { 843 if (p_cb->clcb[i].p_srcb == p_srcb) 844 { 845 p_cb->clcb[i].status = status; 846 bta_gattc_sm_execute(&p_cb->clcb[i], BTA_GATTC_DISCOVER_CMPL_EVT, NULL); 847 } 848 } 849} 850/******************************************************************************* 851** 852** Function bta_gattc_disc_close 853** 854** Description close a GATTC connection while in discovery state. 855** 856** Returns void 857** 858*******************************************************************************/ 859void bta_gattc_disc_close(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data) 860{ 861 APPL_TRACE_DEBUG("%s: Discovery cancel conn_id=%d", __func__, 862 p_clcb->bta_conn_id); 863 864 if (p_clcb->disc_active) 865 bta_gattc_reset_discover_st(p_clcb->p_srcb, BTA_GATT_ERROR); 866 else 867 p_clcb->state = BTA_GATTC_CONN_ST; 868 869 // This function only gets called as the result of a BTA_GATTC_API_CLOSE_EVT 870 // while in the BTA_GATTC_DISCOVER_ST state. Once the state changes, the 871 // connection itself still needs to be closed to resolve the original event. 872 if (p_clcb->state == BTA_GATTC_CONN_ST) 873 { 874 APPL_TRACE_DEBUG("State is back to BTA_GATTC_CONN_ST. " 875 "Trigger connection close"); 876 bta_gattc_close(p_clcb, p_data); 877 } 878} 879/******************************************************************************* 880** 881** Function bta_gattc_set_discover_st 882** 883** Description when a SRCB start discovery, tell all related clcb and set 884** the state. 885** 886** Returns None. 887** 888*******************************************************************************/ 889void bta_gattc_set_discover_st(tBTA_GATTC_SERV *p_srcb) 890{ 891 tBTA_GATTC_CB *p_cb = &bta_gattc_cb; 892 UINT8 i; 893 894#if BLE_INCLUDED == TRUE 895 L2CA_EnableUpdateBleConnParams(p_srcb->server_bda, FALSE); 896#endif 897 for (i = 0; i < BTA_GATTC_CLCB_MAX; i ++) 898 { 899 if (p_cb->clcb[i].p_srcb == p_srcb) 900 { 901 p_cb->clcb[i].status = BTA_GATT_OK; 902 p_cb->clcb[i].state = BTA_GATTC_DISCOVER_ST; 903 } 904 } 905} 906/******************************************************************************* 907** 908** Function bta_gattc_restart_discover 909** 910** Description process service change in discovery state, mark up the auto 911** update flag and set status to be discovery cancel for current 912** discovery. 913** 914** Returns None. 915** 916*******************************************************************************/ 917void bta_gattc_restart_discover(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data) 918{ 919 UNUSED(p_data); 920 921 p_clcb->status = BTA_GATT_CANCEL; 922 p_clcb->auto_update = BTA_GATTC_DISC_WAITING; 923} 924 925/******************************************************************************* 926** 927** Function bta_gattc_cfg_mtu 928** 929** Description Configure MTU size on the GATT connection. 930** 931** Returns None. 932** 933*******************************************************************************/ 934void bta_gattc_cfg_mtu(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data) 935{ 936 tBTA_GATTC_OP_CMPL op_cmpl; 937 tBTA_GATT_STATUS status; 938 939 if (bta_gattc_enqueue(p_clcb, p_data)) 940 { 941 status = GATTC_ConfigureMTU (p_clcb->bta_conn_id, p_data->api_mtu.mtu); 942 943 /* if failed, return callback here */ 944 if (status != GATT_SUCCESS && status != GATT_CMD_STARTED) 945 { 946 memset(&op_cmpl, 0, sizeof(tBTA_GATTC_OP_CMPL)); 947 948 op_cmpl.status = status; 949 op_cmpl.op_code = GATTC_OPTYPE_CONFIG; 950 op_cmpl.p_cmpl = NULL; 951 952 bta_gattc_sm_execute(p_clcb, BTA_GATTC_OP_CMPL_EVT, (tBTA_GATTC_DATA *)&op_cmpl); 953 } 954 } 955} 956/******************************************************************************* 957** 958** Function bta_gattc_start_discover 959** 960** Description Start a discovery on server. 961** 962** Returns None. 963** 964*******************************************************************************/ 965void bta_gattc_start_discover(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data) 966{ 967 UNUSED(p_data); 968 969 APPL_TRACE_DEBUG("bta_gattc_start_discover conn_id=%d p_clcb->p_srcb->state = %d ", 970 p_clcb->bta_conn_id, p_clcb->p_srcb->state); 971 972 if (((p_clcb->p_q_cmd == NULL || p_clcb->auto_update == BTA_GATTC_REQ_WAITING) && 973 p_clcb->p_srcb->state == BTA_GATTC_SERV_IDLE) || 974 p_clcb->p_srcb->state == BTA_GATTC_SERV_DISC) 975 /* no pending operation, start discovery right away */ 976 { 977 p_clcb->auto_update = BTA_GATTC_NO_SCHEDULE; 978 979 if (p_clcb->p_srcb != NULL) 980 { 981 /* clear the service change mask */ 982 p_clcb->p_srcb->srvc_hdl_chg = FALSE; 983 p_clcb->p_srcb->update_count = 0; 984 p_clcb->p_srcb->state = BTA_GATTC_SERV_DISC_ACT; 985 986 if (p_clcb->transport == BTA_TRANSPORT_LE) 987 L2CA_EnableUpdateBleConnParams(p_clcb->p_srcb->server_bda, FALSE); 988 989 /* set all srcb related clcb into discovery ST */ 990 bta_gattc_set_discover_st(p_clcb->p_srcb); 991 992 if ((p_clcb->status = bta_gattc_init_cache(p_clcb->p_srcb)) == BTA_GATT_OK) 993 { 994 p_clcb->status = bta_gattc_discover_pri_service(p_clcb->bta_conn_id, 995 p_clcb->p_srcb, GATT_DISC_SRVC_ALL); 996 } 997 if (p_clcb->status != BTA_GATT_OK) 998 { 999 APPL_TRACE_ERROR("discovery on server failed"); 1000 bta_gattc_reset_discover_st(p_clcb->p_srcb, p_clcb->status); 1001 } 1002 else 1003 p_clcb->disc_active = TRUE; 1004 } 1005 else 1006 { 1007 APPL_TRACE_ERROR("unknown device, can not start discovery"); 1008 } 1009 } 1010 /* pending operation, wait until it finishes */ 1011 else 1012 { 1013 p_clcb->auto_update = BTA_GATTC_DISC_WAITING; 1014 1015 if (p_clcb->p_srcb->state == BTA_GATTC_SERV_IDLE) 1016 p_clcb->state = BTA_GATTC_CONN_ST; /* set clcb state */ 1017 } 1018 1019} 1020/******************************************************************************* 1021** 1022** Function bta_gattc_disc_cmpl 1023** 1024** Description discovery on server is finished 1025** 1026** Returns None. 1027** 1028*******************************************************************************/ 1029void bta_gattc_disc_cmpl(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data) 1030{ 1031 tBTA_GATTC_DATA *p_q_cmd = p_clcb->p_q_cmd; 1032 UNUSED(p_data); 1033 1034 APPL_TRACE_DEBUG("bta_gattc_disc_cmpl conn_id=%d",p_clcb->bta_conn_id); 1035 1036#if BLE_INCLUDED == TRUE 1037 if(p_clcb->transport == BTA_TRANSPORT_LE) 1038 L2CA_EnableUpdateBleConnParams(p_clcb->p_srcb->server_bda, TRUE); 1039#endif 1040 p_clcb->p_srcb->state = BTA_GATTC_SERV_IDLE; 1041 p_clcb->disc_active = FALSE; 1042 1043 if (p_clcb->status != GATT_SUCCESS) 1044 { 1045 /* clean up cache */ 1046 if(p_clcb->p_srcb && p_clcb->p_srcb->p_srvc_cache) 1047 { 1048 while (!GKI_queue_is_empty(&p_clcb->p_srcb->cache_buffer)) 1049 { 1050 GKI_freebuf (GKI_dequeue (&p_clcb->p_srcb->cache_buffer)); 1051 } 1052 p_clcb->p_srcb->p_srvc_cache = NULL; 1053 } 1054 1055 /* used to reset cache in application */ 1056 bta_gattc_co_cache_reset(p_clcb->p_srcb->server_bda); 1057 } 1058 /* release pending attribute list buffer */ 1059 utl_freebuf((void **)&p_clcb->p_srcb->p_srvc_list); 1060 1061 if (p_clcb->auto_update == BTA_GATTC_DISC_WAITING) 1062 { 1063 /* start discovery again */ 1064 bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_DISCOVER_EVT, NULL); 1065 } 1066 /* get any queued command to proceed */ 1067 else if (p_q_cmd != NULL) 1068 { 1069 p_clcb->p_q_cmd = NULL; 1070 1071 bta_gattc_sm_execute(p_clcb, p_q_cmd->hdr.event, p_q_cmd); 1072 /* if the command executed requeued the cmd, we don't 1073 * want to free the underlying buffer that's being 1074 * referenced by p_clcb->p_q_cmd 1075 */ 1076 if (p_q_cmd != p_clcb->p_q_cmd) { 1077 utl_freebuf((void **)&p_q_cmd); 1078 } 1079 } 1080} 1081/******************************************************************************* 1082** 1083** Function bta_gattc_read 1084** 1085** Description Read an attribute 1086** 1087** Returns None. 1088** 1089*******************************************************************************/ 1090void bta_gattc_read(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data) 1091{ 1092 UINT16 handle = 0; 1093 tGATT_READ_PARAM read_param; 1094 tBTA_GATTC_OP_CMPL op_cmpl; 1095 1096 memset (&read_param, 0 ,sizeof(tGATT_READ_PARAM)); 1097 memset (&op_cmpl, 0 ,sizeof(tBTA_GATTC_OP_CMPL)); 1098 1099 if (bta_gattc_enqueue(p_clcb, p_data)) 1100 { 1101 if ((handle = bta_gattc_id2handle(p_clcb->p_srcb, 1102 &p_data->api_read.srvc_id, 1103 &p_data->api_read.char_id, 1104 p_data->api_read.p_descr_type)) == 0) 1105 { 1106 op_cmpl.status = BTA_GATT_ERROR; 1107 } 1108 else 1109 { 1110 read_param.by_handle.handle = handle; 1111 read_param.by_handle.auth_req = p_data->api_read.auth_req; 1112 1113 op_cmpl.status = GATTC_Read(p_clcb->bta_conn_id, GATT_READ_BY_HANDLE, &read_param); 1114 } 1115 1116 /* read fail */ 1117 if (op_cmpl.status != BTA_GATT_OK) 1118 { 1119 op_cmpl.op_code = GATTC_OPTYPE_READ; 1120 op_cmpl.p_cmpl = NULL; 1121 1122 bta_gattc_sm_execute(p_clcb, BTA_GATTC_OP_CMPL_EVT, (tBTA_GATTC_DATA *)&op_cmpl); 1123 } 1124 } 1125} 1126/******************************************************************************* 1127** 1128** Function bta_gattc_read_multi 1129** 1130** Description read multiple 1131** 1132** Returns None. 1133*********************************************************************************/ 1134void bta_gattc_read_multi(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data) 1135{ 1136 UINT16 i, handle; 1137 tBTA_GATT_STATUS status = BTA_GATT_OK; 1138 tGATT_READ_PARAM read_param; 1139 tBTA_GATTC_OP_CMPL op_cmpl; 1140 tBTA_GATTC_ATTR_ID *p_id; 1141 1142 if (bta_gattc_enqueue(p_clcb, p_data)) 1143 { 1144 memset(&read_param, 0, sizeof(tGATT_READ_PARAM)); 1145 1146 p_id = p_data->api_read_multi.p_id_list; 1147 1148 for (i = 0; i < p_data->api_read_multi.num_attr && p_id; i ++, p_id ++) 1149 { 1150 handle = 0; 1151 1152 if (p_id->id_type == BTA_GATT_TYPE_CHAR) 1153 { 1154 handle = bta_gattc_id2handle(p_clcb->p_srcb, 1155 &p_id->id_value.char_id.srvc_id, 1156 &p_id->id_value.char_id.char_id, 1157 NULL); 1158 } 1159 else if (p_id->id_type == BTA_GATT_TYPE_CHAR_DESCR) 1160 { 1161 handle = bta_gattc_id2handle(p_clcb->p_srcb, 1162 &p_id->id_value.char_descr_id.char_id.srvc_id, 1163 &p_id->id_value.char_descr_id.char_id.char_id, 1164 &p_id->id_value.char_descr_id.descr_id); 1165 } 1166 else 1167 { 1168 APPL_TRACE_ERROR("invalud ID type: %d", p_id->id_type); 1169 } 1170 1171 if (handle == 0) 1172 { 1173 status = BTA_GATT_ERROR; 1174 break; 1175 } 1176 } 1177 if (status == BTA_GATT_OK) 1178 { 1179 read_param.read_multiple.num_handles = p_data->api_read_multi.num_attr; 1180 read_param.read_multiple.auth_req = p_data->api_read_multi.auth_req; 1181 1182 status = GATTC_Read(p_clcb->bta_conn_id, GATT_READ_MULTIPLE, &read_param); 1183 } 1184 1185 /* read fail */ 1186 if (status != BTA_GATT_OK) 1187 { 1188 memset(&op_cmpl, 0, sizeof(tBTA_GATTC_OP_CMPL)); 1189 1190 op_cmpl.status = status; 1191 op_cmpl.op_code = GATTC_OPTYPE_READ; 1192 op_cmpl.p_cmpl = NULL; 1193 1194 bta_gattc_sm_execute(p_clcb, BTA_GATTC_OP_CMPL_EVT, (tBTA_GATTC_DATA *)&op_cmpl); 1195 } 1196 } 1197} 1198/******************************************************************************* 1199** 1200** Function bta_gattc_write 1201** 1202** Description Write an attribute 1203** 1204** Returns None. 1205** 1206*******************************************************************************/ 1207void bta_gattc_write(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data) 1208{ 1209 UINT16 handle = 0; 1210 tGATT_VALUE attr = {0}; 1211 tBTA_GATTC_OP_CMPL op_cmpl; 1212 tBTA_GATT_STATUS status = BTA_GATT_OK; 1213 1214 if (bta_gattc_enqueue(p_clcb, p_data)) 1215 { 1216 if ((handle = bta_gattc_id2handle(p_clcb->p_srcb, 1217 &p_data->api_write.srvc_id, 1218 &p_data->api_write.char_id, 1219 p_data->api_write.p_descr_type)) == 0) 1220 { 1221 status = BTA_GATT_ERROR; 1222 } 1223 else 1224 { 1225 attr.handle= handle; 1226 attr.offset = p_data->api_write.offset; 1227 attr.len = p_data->api_write.len; 1228 attr.auth_req = p_data->api_write.auth_req; 1229 1230 if (p_data->api_write.p_value) 1231 memcpy(attr.value, p_data->api_write.p_value, p_data->api_write.len); 1232 1233 status = GATTC_Write(p_clcb->bta_conn_id, p_data->api_write.write_type, &attr); 1234 } 1235 1236 /* write fail */ 1237 if (status != BTA_GATT_OK) 1238 { 1239 memset(&op_cmpl, 0, sizeof(tBTA_GATTC_OP_CMPL)); 1240 1241 op_cmpl.status = status; 1242 op_cmpl.op_code = GATTC_OPTYPE_WRITE; 1243 op_cmpl.p_cmpl = NULL; 1244 1245 bta_gattc_sm_execute(p_clcb, BTA_GATTC_OP_CMPL_EVT, (tBTA_GATTC_DATA *)&op_cmpl); 1246 } 1247 } 1248} 1249/******************************************************************************* 1250** 1251** Function bta_gattc_execute 1252** 1253** Description send execute write 1254** 1255** Returns None. 1256*********************************************************************************/ 1257void bta_gattc_execute(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data) 1258{ 1259 tBTA_GATTC_OP_CMPL op_cmpl; 1260 tBTA_GATT_STATUS status; 1261 1262 if (bta_gattc_enqueue(p_clcb, p_data)) 1263 { 1264 status = GATTC_ExecuteWrite(p_clcb->bta_conn_id, p_data->api_exec.is_execute); 1265 1266 if (status != BTA_GATT_OK) 1267 { 1268 memset(&op_cmpl, 0, sizeof(tBTA_GATTC_OP_CMPL)); 1269 1270 op_cmpl.status = status; 1271 op_cmpl.op_code = GATTC_OPTYPE_EXE_WRITE; 1272 op_cmpl.p_cmpl = NULL; 1273 1274 bta_gattc_sm_execute(p_clcb, BTA_GATTC_OP_CMPL_EVT, (tBTA_GATTC_DATA *)&op_cmpl); 1275 } 1276 } 1277} 1278/******************************************************************************* 1279** 1280** Function bta_gattc_confirm 1281** 1282** Description send handle value confirmation 1283** 1284** Returns None. 1285** 1286*******************************************************************************/ 1287void bta_gattc_confirm(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data) 1288{ 1289 UINT16 handle; 1290 1291 if ((handle = bta_gattc_id2handle(p_clcb->p_srcb, 1292 &p_data->api_confirm.srvc_id, 1293 &p_data->api_confirm.char_id, 1294 NULL)) == 0) 1295 { 1296 APPL_TRACE_ERROR("Can not map service/char ID into valid handle"); 1297 } 1298 else 1299 { 1300 if (GATTC_SendHandleValueConfirm(p_data->api_confirm.hdr.layer_specific, handle) 1301 != GATT_SUCCESS) 1302 { 1303 APPL_TRACE_ERROR("bta_gattc_confirm to handle [0x%04x] failed", handle); 1304 } 1305 else 1306 { 1307 /* if over BR_EDR, inform PM for mode change */ 1308 if (p_clcb->transport == BTA_TRANSPORT_BR_EDR) 1309 { 1310 bta_sys_busy(BTA_ID_GATTC, BTA_ALL_APP_ID, p_clcb->bda); 1311 bta_sys_idle(BTA_ID_GATTC, BTA_ALL_APP_ID, p_clcb->bda); 1312 } 1313 } 1314 } 1315} 1316/******************************************************************************* 1317** 1318** Function bta_gattc_read_cmpl 1319** 1320** Description read complete 1321** 1322** Returns None. 1323** 1324*******************************************************************************/ 1325void bta_gattc_read_cmpl(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_OP_CMPL *p_data) 1326{ 1327 UINT8 event; 1328 tBTA_GATTC cb_data; 1329 tBTA_GATT_READ_VAL read_value; 1330 1331 memset(&cb_data, 0, sizeof(tBTA_GATTC)); 1332 memset(&read_value, 0, sizeof(tBTA_GATT_READ_VAL)); 1333 1334 cb_data.read.status = p_data->status; 1335 1336 if (p_data->p_cmpl != NULL && p_data->status == BTA_GATT_OK) 1337 { 1338 if (bta_gattc_handle2id(p_clcb->p_srcb, 1339 p_data->p_cmpl->att_value.handle, 1340 &cb_data.read.srvc_id, 1341 &cb_data.read.char_id, 1342 &cb_data.read.descr_type) == FALSE) 1343 { 1344 cb_data.read.status = BTA_GATT_INTERNAL_ERROR; 1345 APPL_TRACE_ERROR("can not map to GATT ID. handle = 0x%04x", 1346 p_data->p_cmpl->att_value.handle); 1347 } 1348 else 1349 { 1350 cb_data.read.status = bta_gattc_pack_read_cb_data(p_clcb->p_srcb, 1351 &cb_data.read.descr_type.uuid, 1352 &p_data->p_cmpl->att_value, 1353 &read_value); 1354 cb_data.read.p_value = &read_value; 1355 } 1356 } 1357 else 1358 { 1359 cb_data.read.srvc_id = p_clcb->p_q_cmd->api_read.srvc_id; 1360 cb_data.read.char_id = p_clcb->p_q_cmd->api_read.char_id; 1361 if (p_clcb->p_q_cmd->api_read.p_descr_type) 1362 memcpy(&cb_data.read.descr_type, p_clcb->p_q_cmd->api_read.p_descr_type, 1363 sizeof(tBTA_GATT_ID)); 1364 } 1365 1366 event = (p_clcb->p_q_cmd->api_read.p_descr_type == NULL) ? 1367 BTA_GATTC_READ_CHAR_EVT: BTA_GATTC_READ_DESCR_EVT; 1368 cb_data.read.conn_id = p_clcb->bta_conn_id; 1369 1370 utl_freebuf((void **)&p_clcb->p_q_cmd); 1371 /* read complete, callback */ 1372 ( *p_clcb->p_rcb->p_cback)(event, (tBTA_GATTC *)&cb_data); 1373 1374} 1375/******************************************************************************* 1376** 1377** Function bta_gattc_write_cmpl 1378** 1379** Description write complete 1380** 1381** Returns None. 1382** 1383*******************************************************************************/ 1384void bta_gattc_write_cmpl(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_OP_CMPL *p_data) 1385{ 1386 tBTA_GATTC cb_data = {0}; 1387 UINT8 event; 1388 1389 memset(&cb_data, 0, sizeof(tBTA_GATTC)); 1390 1391 cb_data.write.status = p_data->status; 1392 1393 if (p_data->p_cmpl != NULL) 1394 { 1395 bta_gattc_handle2id(p_clcb->p_srcb, p_data->p_cmpl->att_value.handle, 1396 &cb_data.write.srvc_id, &cb_data.write.char_id, 1397 &cb_data.write.descr_type); 1398 } 1399 else 1400 { 1401 memcpy(&cb_data.write.srvc_id, &p_clcb->p_q_cmd->api_write.srvc_id, 1402 sizeof(tBTA_GATT_SRVC_ID)); 1403 memcpy(&cb_data.write.char_id, &p_clcb->p_q_cmd->api_write.char_id, 1404 sizeof(tBTA_GATT_ID)); 1405 if (p_clcb->p_q_cmd->api_write.p_descr_type) 1406 memcpy(&cb_data.write.descr_type, p_clcb->p_q_cmd->api_write.p_descr_type, 1407 sizeof(tBTA_GATT_ID)); 1408 } 1409 1410 if (p_clcb->p_q_cmd->api_write.hdr.event == BTA_GATTC_API_WRITE_EVT && 1411 p_clcb->p_q_cmd->api_write.write_type == BTA_GATTC_WRITE_PREPARE) 1412 1413 event = BTA_GATTC_PREP_WRITE_EVT; 1414 1415 else if (p_clcb->p_q_cmd->api_write.p_descr_type == NULL) 1416 1417 event = BTA_GATTC_WRITE_CHAR_EVT; 1418 1419 else 1420 event = BTA_GATTC_WRITE_DESCR_EVT; 1421 1422 utl_freebuf((void **)&p_clcb->p_q_cmd); 1423 cb_data.write.conn_id = p_clcb->bta_conn_id; 1424 /* write complete, callback */ 1425 ( *p_clcb->p_rcb->p_cback)(event, (tBTA_GATTC *)&cb_data); 1426 1427} 1428/******************************************************************************* 1429** 1430** Function bta_gattc_exec_cmpl 1431** 1432** Description execute write complete 1433** 1434** Returns None. 1435** 1436*******************************************************************************/ 1437void bta_gattc_exec_cmpl(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_OP_CMPL *p_data) 1438{ 1439 tBTA_GATTC cb_data; 1440 1441 utl_freebuf((void **)&p_clcb->p_q_cmd); 1442 1443 p_clcb->status = BTA_GATT_OK; 1444 1445 /* execute complete, callback */ 1446 cb_data.exec_cmpl.conn_id = p_clcb->bta_conn_id; 1447 cb_data.exec_cmpl.status = p_data->status; 1448 1449 ( *p_clcb->p_rcb->p_cback)(BTA_GATTC_EXEC_EVT, &cb_data); 1450 1451} 1452 1453/******************************************************************************* 1454** 1455** Function bta_gattc_cfg_mtu_cmpl 1456** 1457** Description configure MTU operation complete 1458** 1459** Returns None. 1460** 1461*******************************************************************************/ 1462void bta_gattc_cfg_mtu_cmpl(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_OP_CMPL *p_data) 1463{ 1464 tBTA_GATTC cb_data; 1465 1466 utl_freebuf((void **)&p_clcb->p_q_cmd); 1467 1468 1469 if (p_data->p_cmpl && p_data->status == BTA_GATT_OK) 1470 p_clcb->p_srcb->mtu = p_data->p_cmpl->mtu; 1471 1472 /* configure MTU complete, callback */ 1473 p_clcb->status = p_data->status; 1474 cb_data.cfg_mtu.conn_id = p_clcb->bta_conn_id; 1475 cb_data.cfg_mtu.status = p_data->status; 1476 cb_data.cfg_mtu.mtu = p_clcb->p_srcb->mtu; 1477 1478 (*p_clcb->p_rcb->p_cback) (BTA_GATTC_CFG_MTU_EVT, &cb_data); 1479 1480} 1481/******************************************************************************* 1482** 1483** Function bta_gattc_op_cmpl 1484** 1485** Description operation completed. 1486** 1487** Returns None. 1488** 1489*******************************************************************************/ 1490void bta_gattc_op_cmpl(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data) 1491{ 1492 UINT8 op = (UINT8)p_data->op_cmpl.op_code; 1493 UINT8 mapped_op = 0; 1494 1495 APPL_TRACE_DEBUG("bta_gattc_op_cmpl op = %d", op); 1496 1497 if (op == GATTC_OPTYPE_INDICATION || op == GATTC_OPTYPE_NOTIFICATION) 1498 { 1499 APPL_TRACE_ERROR("unexpected operation, ignored"); 1500 } 1501 else if (op >= GATTC_OPTYPE_READ) 1502 { 1503 if (p_clcb->p_q_cmd == NULL) 1504 { 1505 APPL_TRACE_ERROR("No pending command"); 1506 return; 1507 } 1508 if (p_clcb->p_q_cmd->hdr.event != bta_gattc_opcode_to_int_evt[op - GATTC_OPTYPE_READ]) 1509 { 1510 mapped_op = p_clcb->p_q_cmd->hdr.event - BTA_GATTC_API_READ_EVT + GATTC_OPTYPE_READ; 1511 if ( mapped_op > GATTC_OPTYPE_INDICATION) mapped_op = 0; 1512 1513#if (BT_TRACE_VERBOSE == TRUE) 1514 APPL_TRACE_ERROR("expect op:(%s :0x%04x), receive unexpected operation (%s).", 1515 bta_gattc_op_code_name[mapped_op] , p_clcb->p_q_cmd->hdr.event, 1516 bta_gattc_op_code_name[op]); 1517#else 1518 APPL_TRACE_ERROR("expect op:(%u :0x%04x), receive unexpected operation (%u).", 1519 mapped_op , p_clcb->p_q_cmd->hdr.event, op); 1520#endif 1521 return; 1522 } 1523 1524 /* discard responses if service change indication is received before operation completed */ 1525 if (p_clcb->auto_update == BTA_GATTC_DISC_WAITING && p_clcb->p_srcb->srvc_hdl_chg) 1526 { 1527 APPL_TRACE_DEBUG("Discard all responses when service change indication is received."); 1528 p_data->op_cmpl.status = GATT_ERROR; 1529 } 1530 1531 /* service handle change void the response, discard it */ 1532 if (op == GATTC_OPTYPE_READ) 1533 bta_gattc_read_cmpl(p_clcb, &p_data->op_cmpl); 1534 1535 else if (op == GATTC_OPTYPE_WRITE) 1536 bta_gattc_write_cmpl(p_clcb, &p_data->op_cmpl); 1537 1538 else if (op == GATTC_OPTYPE_EXE_WRITE) 1539 bta_gattc_exec_cmpl(p_clcb, &p_data->op_cmpl); 1540 1541 else if (op == GATTC_OPTYPE_CONFIG) 1542 bta_gattc_cfg_mtu_cmpl(p_clcb, &p_data->op_cmpl); 1543 1544 if (p_clcb->auto_update == BTA_GATTC_DISC_WAITING) 1545 { 1546 p_clcb->auto_update = BTA_GATTC_REQ_WAITING; 1547 bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_DISCOVER_EVT, NULL); 1548 } 1549 } 1550} 1551/******************************************************************************* 1552** 1553** Function bta_gattc_op_cmpl 1554** 1555** Description operation completed. 1556** 1557** Returns None. 1558** 1559*******************************************************************************/ 1560void bta_gattc_ignore_op_cmpl(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data) 1561{ 1562 UNUSED(p_clcb); 1563 1564 /* receive op complete when discovery is started, ignore the response, 1565 and wait for discovery finish and resent */ 1566 APPL_TRACE_DEBUG("bta_gattc_ignore_op_cmpl op = %d", p_data->hdr.layer_specific); 1567 1568} 1569/******************************************************************************* 1570** 1571** Function bta_gattc_search 1572** 1573** Description start a search in the local server cache 1574** 1575** Returns None. 1576** 1577*******************************************************************************/ 1578void bta_gattc_search(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data) 1579{ 1580 tBTA_GATT_STATUS status = GATT_INTERNAL_ERROR; 1581 tBTA_GATTC cb_data; 1582 APPL_TRACE_DEBUG("bta_gattc_search conn_id=%d",p_clcb->bta_conn_id); 1583 if (p_clcb->p_srcb && p_clcb->p_srcb->p_srvc_cache) 1584 { 1585 status = BTA_GATT_OK; 1586 /* search the local cache of a server device */ 1587 bta_gattc_search_service(p_clcb, p_data->api_search.p_srvc_uuid); 1588 } 1589 cb_data.search_cmpl.status = status; 1590 cb_data.search_cmpl.conn_id = p_clcb->bta_conn_id; 1591 1592 /* end of search or no server cache available */ 1593 ( *p_clcb->p_rcb->p_cback)(BTA_GATTC_SEARCH_CMPL_EVT, &cb_data); 1594} 1595/******************************************************************************* 1596** 1597** Function bta_gattc_q_cmd 1598** 1599** Description enqueue a command into control block, usually because discovery 1600** operation is busy. 1601** 1602** Returns None. 1603** 1604*******************************************************************************/ 1605void bta_gattc_q_cmd(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data) 1606{ 1607 bta_gattc_enqueue(p_clcb, p_data); 1608} 1609/******************************************************************************* 1610** 1611** Function bta_gattc_cache_open 1612** 1613** Description open a NV cache for loading 1614** 1615** Returns void 1616** 1617*******************************************************************************/ 1618void bta_gattc_cache_open(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data) 1619{ 1620 UNUSED(p_data); 1621 1622 bta_gattc_set_discover_st(p_clcb->p_srcb); 1623 1624 APPL_TRACE_DEBUG("bta_gattc_cache_open conn_id=%d",p_clcb->bta_conn_id); 1625 bta_gattc_co_cache_open(p_clcb->p_srcb->server_bda, BTA_GATTC_CI_CACHE_OPEN_EVT, 1626 p_clcb->bta_conn_id, FALSE); 1627} 1628/******************************************************************************* 1629** 1630** Function bta_gattc_start_load 1631** 1632** Description start cache loading by sending callout open cache 1633** 1634** Returns None. 1635** 1636*******************************************************************************/ 1637void bta_gattc_ci_open(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data) 1638{ 1639 APPL_TRACE_DEBUG("bta_gattc_ci_open conn_id=%d server state=%d" , 1640 p_clcb->bta_conn_id, p_clcb->p_srcb->state); 1641 if (p_clcb->p_srcb->state == BTA_GATTC_SERV_LOAD) 1642 { 1643 if (p_data->ci_open.status == BTA_GATT_OK) 1644 { 1645 p_clcb->p_srcb->attr_index = 0; 1646 bta_gattc_co_cache_load(p_clcb->p_srcb->server_bda, 1647 BTA_GATTC_CI_CACHE_LOAD_EVT, 1648 p_clcb->p_srcb->attr_index, 1649 p_clcb->bta_conn_id); 1650 } 1651 else 1652 { 1653 p_clcb->p_srcb->state = BTA_GATTC_SERV_DISC; 1654 /* cache open failure, start discovery */ 1655 bta_gattc_start_discover(p_clcb, NULL); 1656 } 1657 } 1658 if (p_clcb->p_srcb->state == BTA_GATTC_SERV_SAVE) 1659 { 1660 if (p_data->ci_open.status == BTA_GATT_OK) 1661 { 1662 if (!bta_gattc_cache_save(p_clcb->p_srcb, p_clcb->bta_conn_id)) 1663 { 1664 p_data->ci_open.status = BTA_GATT_ERROR; 1665 } 1666 } 1667 if (p_data->ci_open.status != BTA_GATT_OK) 1668 { 1669 p_clcb->p_srcb->attr_index = 0; 1670 bta_gattc_co_cache_close(p_clcb->p_srcb->server_bda, p_clcb->bta_conn_id); 1671 bta_gattc_reset_discover_st(p_clcb->p_srcb, p_clcb->status); 1672 1673 } 1674 } 1675} 1676/******************************************************************************* 1677** 1678** Function bta_gattc_ci_load 1679** 1680** Description cache loading received. 1681** 1682** Returns None. 1683** 1684*******************************************************************************/ 1685void bta_gattc_ci_load(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data) 1686{ 1687 1688 APPL_TRACE_DEBUG("bta_gattc_ci_load conn_id=%d load status=%d", 1689 p_clcb->bta_conn_id, p_data->ci_load.status); 1690 1691 if (p_data->ci_load.status == BTA_GATT_OK || 1692 p_data->ci_load.status == BTA_GATT_MORE) 1693 { 1694 if (p_data->ci_load.num_attr != 0) 1695 bta_gattc_rebuild_cache(p_clcb->p_srcb, p_data->ci_load.num_attr, 1696 p_data->ci_load.attr, p_clcb->p_srcb->attr_index); 1697 1698 if (p_data->ci_load.status == BTA_GATT_OK) 1699 { 1700 p_clcb->p_srcb->attr_index = 0; 1701 bta_gattc_reset_discover_st(p_clcb->p_srcb, BTA_GATT_OK); 1702 bta_gattc_co_cache_close(p_clcb->p_srcb->server_bda, 0); 1703 } 1704 else /* load more */ 1705 { 1706 p_clcb->p_srcb->attr_index += p_data->ci_load.num_attr; 1707 1708 bta_gattc_co_cache_load(p_clcb->p_srcb->server_bda, 1709 BTA_GATTC_CI_CACHE_LOAD_EVT, 1710 p_clcb->p_srcb->attr_index, 1711 p_clcb->bta_conn_id); 1712 } 1713 } 1714 else 1715 { 1716 bta_gattc_co_cache_close(p_clcb->p_srcb->server_bda, 0); 1717 p_clcb->p_srcb->state = BTA_GATTC_SERV_DISC; 1718 p_clcb->p_srcb->attr_index = 0; 1719 /* cache load failure, start discovery */ 1720 bta_gattc_start_discover(p_clcb, NULL); 1721 } 1722} 1723/******************************************************************************* 1724** 1725** Function bta_gattc_ci_save 1726** 1727** Description cache loading received. 1728** 1729** Returns None. 1730** 1731*******************************************************************************/ 1732void bta_gattc_ci_save(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data) 1733{ 1734 UNUSED(p_data); 1735 1736 APPL_TRACE_DEBUG("bta_gattc_ci_save conn_id=%d " , 1737 p_clcb->bta_conn_id ); 1738 1739 if (!bta_gattc_cache_save(p_clcb->p_srcb, p_clcb->bta_conn_id)) 1740 { 1741 p_clcb->p_srcb->attr_index = 0; 1742 bta_gattc_co_cache_close(p_clcb->p_srcb->server_bda, 0); 1743 bta_gattc_reset_discover_st(p_clcb->p_srcb, p_clcb->status); 1744 } 1745} 1746/******************************************************************************* 1747** 1748** Function bta_gattc_fail 1749** 1750** Description report API call failure back to apps 1751** 1752** Returns None. 1753** 1754*******************************************************************************/ 1755void bta_gattc_fail(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data) 1756{ 1757 UNUSED(p_data); 1758 1759 if (p_clcb->status == BTA_GATT_OK) 1760 { 1761 APPL_TRACE_ERROR("operation not supported at current state [%d]", p_clcb->state); 1762 } 1763} 1764 1765/******************************************************************************* 1766** 1767** Function bta_gattc_deregister_cmpl 1768** 1769** Description De-Register a GATT client application with BTA completed. 1770** 1771** Returns void 1772** 1773*******************************************************************************/ 1774static void bta_gattc_deregister_cmpl(tBTA_GATTC_RCB *p_clreg) 1775{ 1776 tBTA_GATTC_CB *p_cb = &bta_gattc_cb; 1777 tBTA_GATTC_IF client_if = p_clreg->client_if; 1778 tBTA_GATTC cb_data; 1779 tBTA_GATTC_CBACK *p_cback = p_clreg->p_cback; 1780 1781 memset(&cb_data, 0, sizeof(tBTA_GATTC)); 1782 1783 GATT_Deregister(p_clreg->client_if); 1784 memset(p_clreg, 0, sizeof(tBTA_GATTC_RCB)); 1785 1786 cb_data.reg_oper.client_if = client_if; 1787 cb_data.reg_oper.status = BTA_GATT_OK; 1788 1789 if (p_cback) 1790 /* callback with de-register event */ 1791 (*p_cback)(BTA_GATTC_DEREG_EVT, (tBTA_GATTC *)&cb_data); 1792 1793 if (bta_gattc_num_reg_app() == 0 && p_cb->state == BTA_GATTC_STATE_DISABLING) 1794 { 1795 p_cb->state = BTA_GATTC_STATE_DISABLED; 1796 } 1797} 1798/******************************************************************************* 1799** 1800** Function bta_gattc_conn_cback 1801** bta_gattc_cmpl_cback 1802** 1803** Description callback functions to GATT client stack. 1804** 1805** Returns void 1806** 1807*******************************************************************************/ 1808static void bta_gattc_conn_cback(tGATT_IF gattc_if, BD_ADDR bda, UINT16 conn_id, 1809 BOOLEAN connected, tGATT_DISCONN_REASON reason, 1810 tBT_TRANSPORT transport) 1811{ 1812 tBTA_GATTC_DATA *p_buf; 1813 1814 if (reason != 0) 1815 { 1816 APPL_TRACE_WARNING("%s() - cif=%d connected=%d conn_id=%d reason=0x%04x", 1817 __FUNCTION__, gattc_if, connected, conn_id, reason); 1818 } 1819 1820 bt_bdaddr_t bdaddr; 1821 bdcpy(bdaddr.address, bda); 1822 if (connected) 1823 btif_debug_conn_state(bdaddr, BTIF_DEBUG_CONNECTED, GATT_CONN_UNKNOWN); 1824 else 1825 btif_debug_conn_state(bdaddr, BTIF_DEBUG_DISCONNECTED, reason); 1826 1827 if ((p_buf = (tBTA_GATTC_DATA *) GKI_getbuf(sizeof(tBTA_GATTC_DATA))) != NULL) 1828 { 1829 memset(p_buf, 0, sizeof(tBTA_GATTC_DATA)); 1830 1831 p_buf->int_conn.hdr.event = connected ? BTA_GATTC_INT_CONN_EVT: 1832 BTA_GATTC_INT_DISCONN_EVT; 1833 p_buf->int_conn.hdr.layer_specific = conn_id; 1834 p_buf->int_conn.client_if = gattc_if; 1835 p_buf->int_conn.role = L2CA_GetBleConnRole(bda); 1836 p_buf->int_conn.reason = reason; 1837 p_buf->int_conn.transport = transport; 1838 bdcpy(p_buf->int_conn.remote_bda, bda); 1839 1840 bta_sys_sendmsg(p_buf); 1841 } 1842} 1843 1844/******************************************************************************* 1845** 1846** Function bta_gattc_enc_cmpl_cback 1847** 1848** Description encryption complete callback function to GATT client stack. 1849** 1850** Returns void 1851** 1852*******************************************************************************/ 1853static void bta_gattc_enc_cmpl_cback(tGATT_IF gattc_if, BD_ADDR bda) 1854{ 1855 tBTA_GATTC_DATA *p_buf; 1856 tBTA_GATTC_CLCB *p_clcb = NULL; 1857 1858 if ((p_clcb = bta_gattc_find_clcb_by_cif(gattc_if, bda, BTA_GATT_TRANSPORT_LE)) == NULL) 1859 { 1860 return; 1861 } 1862 1863#if (defined BTA_HH_LE_INCLUDED && BTA_HH_LE_INCLUDED == TRUE) 1864 /* filter this event just for BTA HH LE GATT client, 1865 In the future, if we want to enable encryption complete event 1866 for all GATT clients, we can remove this code */ 1867 if (!bta_hh_le_is_hh_gatt_if(gattc_if)) 1868 { 1869 return; 1870 } 1871#endif 1872 1873 APPL_TRACE_DEBUG("bta_gattc_enc_cmpl_cback: cif = %d", gattc_if); 1874 1875 if ((p_buf = (tBTA_GATTC_DATA *) GKI_getbuf(sizeof(tBTA_GATTC_DATA))) != NULL) 1876 { 1877 memset(p_buf, 0, sizeof(tBTA_GATTC_DATA)); 1878 1879 p_buf->enc_cmpl.hdr.event = BTA_GATTC_ENC_CMPL_EVT; 1880 p_buf->enc_cmpl.hdr.layer_specific = p_clcb->bta_conn_id; 1881 p_buf->enc_cmpl.client_if = gattc_if; 1882 bdcpy(p_buf->enc_cmpl.remote_bda, bda); 1883 1884 bta_sys_sendmsg(p_buf); 1885 } 1886} 1887 1888/******************************************************************************* 1889** 1890** Function bta_gattc_process_api_refresh 1891** 1892** Description process refresh API to delete cache and start a new discovery 1893** if currently connected. 1894** 1895** Returns None. 1896** 1897*******************************************************************************/ 1898void bta_gattc_process_api_refresh(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA * p_msg) 1899{ 1900 tBTA_GATTC_SERV *p_srvc_cb = bta_gattc_find_srvr_cache(p_msg->api_conn.remote_bda); 1901 tBTA_GATTC_CLCB *p_clcb = &bta_gattc_cb.clcb[0]; 1902 BOOLEAN found = FALSE; 1903 UINT8 i; 1904 UNUSED(p_cb); 1905 1906 if (p_srvc_cb != NULL) 1907 { 1908 /* try to find a CLCB */ 1909 if (p_srvc_cb->connected && p_srvc_cb->num_clcb != 0) 1910 { 1911 for (i = 0; i < BTA_GATTC_CLCB_MAX; i ++, p_clcb ++) 1912 { 1913 if (p_clcb->in_use && p_clcb->p_srcb == p_srvc_cb) 1914 { 1915 found = TRUE; 1916 break; 1917 } 1918 } 1919 if (found) 1920 { 1921 bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_DISCOVER_EVT, NULL); 1922 return; 1923 } 1924 } 1925 /* in all other cases, mark it and delete the cache */ 1926 if (p_srvc_cb->p_srvc_cache != NULL) 1927 { 1928 while (!GKI_queue_is_empty(&p_srvc_cb->cache_buffer)) 1929 GKI_freebuf (GKI_dequeue (&p_srvc_cb->cache_buffer)); 1930 1931 p_srvc_cb->p_srvc_cache = NULL; 1932 } 1933 } 1934 /* used to reset cache in application */ 1935 bta_gattc_co_cache_reset(p_msg->api_conn.remote_bda); 1936 1937} 1938/******************************************************************************* 1939** 1940** Function bta_gattc_process_srvc_chg_ind 1941** 1942** Description process service change indication. 1943** 1944** Returns None. 1945** 1946*******************************************************************************/ 1947BOOLEAN bta_gattc_process_srvc_chg_ind(UINT16 conn_id, 1948 tBTA_GATTC_RCB *p_clrcb, 1949 tBTA_GATTC_SERV *p_srcb, 1950 tBTA_GATTC_CLCB *p_clcb, 1951 tBTA_GATTC_NOTIFY *p_notify, 1952 UINT16 handle) 1953{ 1954 tBT_UUID gattp_uuid, srvc_chg_uuid; 1955 BOOLEAN processed = FALSE; 1956 UINT8 i; 1957 1958 gattp_uuid.len = 2; 1959 gattp_uuid.uu.uuid16 = UUID_SERVCLASS_GATT_SERVER; 1960 1961 srvc_chg_uuid.len = 2; 1962 srvc_chg_uuid.uu.uuid16 = GATT_UUID_GATT_SRV_CHGD; 1963 1964 if (bta_gattc_uuid_compare(&p_notify->char_id.srvc_id.id.uuid, &gattp_uuid, TRUE) && 1965 bta_gattc_uuid_compare(&p_notify->char_id.char_id.uuid, &srvc_chg_uuid, TRUE)) 1966 { 1967 processed = TRUE; 1968 /* mark service handle change pending */ 1969 p_srcb->srvc_hdl_chg = TRUE; 1970 /* clear up all notification/indication registration */ 1971 bta_gattc_clear_notif_registration(conn_id); 1972 /* service change indication all received, do discovery update */ 1973 if ( ++ p_srcb->update_count == bta_gattc_num_reg_app()) 1974 { 1975 /* not an opened connection; or connection busy */ 1976 /* search for first available clcb and start discovery */ 1977 if (p_clcb == NULL || (p_clcb && p_clcb->p_q_cmd != NULL)) 1978 { 1979 for (i = 0 ; i < BTA_GATTC_CLCB_MAX; i ++) 1980 { 1981 if (bta_gattc_cb.clcb[i].in_use && 1982 bta_gattc_cb.clcb[i].p_srcb == p_srcb && 1983 bta_gattc_cb.clcb[i].p_q_cmd == NULL) 1984 { 1985 p_clcb = &bta_gattc_cb.clcb[i]; 1986 break; 1987 } 1988 } 1989 } 1990 /* send confirmation here if this is an indication, it should always be */ 1991 GATTC_SendHandleValueConfirm(conn_id, handle); 1992 1993 /* if connection available, refresh cache by doing discovery now */ 1994 if (p_clcb != NULL) 1995 bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_DISCOVER_EVT, NULL); 1996 } 1997 /* notify applicationf or service change */ 1998 if (p_clrcb->p_cback != NULL) 1999 { 2000 (* p_clrcb->p_cback)(BTA_GATTC_SRVC_CHG_EVT, (tBTA_GATTC *)p_srcb->server_bda); 2001 } 2002 2003 } 2004 2005 return processed; 2006 2007} 2008/******************************************************************************* 2009** 2010** Function bta_gattc_proc_other_indication 2011** 2012** Description process all non-service change indication/notification. 2013** 2014** Returns None. 2015** 2016*******************************************************************************/ 2017void bta_gattc_proc_other_indication(tBTA_GATTC_CLCB *p_clcb, UINT8 op, 2018 tGATT_CL_COMPLETE *p_data, 2019 tBTA_GATTC_NOTIFY *p_notify) 2020{ 2021 APPL_TRACE_DEBUG("bta_gattc_proc_other_indication check \ 2022 p_data->att_value.handle=%d p_data->handle=%d", 2023 p_data->att_value.handle, p_data->handle); 2024 APPL_TRACE_DEBUG("is_notify", p_notify->is_notify); 2025 2026 p_notify->is_notify = (op == GATTC_OPTYPE_INDICATION) ? FALSE : TRUE; 2027 p_notify->len = p_data->att_value.len; 2028 bdcpy(p_notify->bda, p_clcb->bda); 2029 memcpy(p_notify->value, p_data->att_value.value, p_data->att_value.len); 2030 p_notify->conn_id = p_clcb->bta_conn_id; 2031 2032 if (p_clcb->p_rcb->p_cback) 2033 (*p_clcb->p_rcb->p_cback)(BTA_GATTC_NOTIF_EVT, (tBTA_GATTC *)p_notify); 2034 2035} 2036/******************************************************************************* 2037** 2038** Function bta_gattc_process_indicate 2039** 2040** Description process indication/notification. 2041** 2042** Returns None. 2043** 2044*******************************************************************************/ 2045void bta_gattc_process_indicate(UINT16 conn_id, tGATTC_OPTYPE op, tGATT_CL_COMPLETE *p_data) 2046{ 2047 UINT16 handle = p_data->att_value.handle; 2048 tBTA_GATTC_CLCB *p_clcb ; 2049 tBTA_GATTC_RCB *p_clrcb = NULL; 2050 tBTA_GATTC_SERV *p_srcb = NULL; 2051 tBTA_GATTC_NOTIFY notify; 2052 BD_ADDR remote_bda; 2053 tBTA_GATTC_IF gatt_if; 2054 tBTA_TRANSPORT transport; 2055 2056 if (!GATT_GetConnectionInfor(conn_id, &gatt_if, remote_bda, &transport)) 2057 { 2058 APPL_TRACE_ERROR("%s indication/notif for unknown app", __func__); 2059 if (op == GATTC_OPTYPE_INDICATION) 2060 GATTC_SendHandleValueConfirm(conn_id, handle); 2061 return; 2062 } 2063 2064 if ((p_clrcb = bta_gattc_cl_get_regcb(gatt_if)) == NULL) 2065 { 2066 APPL_TRACE_ERROR("%s indication/notif for unregistered app", __func__); 2067 if (op == GATTC_OPTYPE_INDICATION) 2068 GATTC_SendHandleValueConfirm(conn_id, handle); 2069 return; 2070 } 2071 2072 if ((p_srcb = bta_gattc_find_srcb(remote_bda)) == NULL) 2073 { 2074 APPL_TRACE_ERROR("%s indication/notif for unknown device, ignore", __func__); 2075 if (op == GATTC_OPTYPE_INDICATION) 2076 GATTC_SendHandleValueConfirm(conn_id, handle); 2077 return; 2078 } 2079 2080 p_clcb = bta_gattc_find_clcb_by_conn_id(conn_id); 2081 2082 if (bta_gattc_handle2id(p_srcb, handle, 2083 ¬ify.char_id.srvc_id, 2084 ¬ify.char_id.char_id, 2085 ¬ify.descr_type)) 2086 { 2087 /* if non-service change indication/notification, forward to application */ 2088 if (!bta_gattc_process_srvc_chg_ind(conn_id, p_clrcb, p_srcb, p_clcb, ¬ify, handle)) 2089 { 2090 /* if app registered for the notification */ 2091 if (bta_gattc_check_notif_registry(p_clrcb, p_srcb, ¬ify)) 2092 { 2093 /* connection not open yet */ 2094 if (p_clcb == NULL) 2095 { 2096 if ((p_clcb = bta_gattc_clcb_alloc(gatt_if, remote_bda, transport)) != NULL) 2097 { 2098 p_clcb->bta_conn_id = conn_id; 2099 p_clcb->transport = transport; 2100 2101 bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_CONN_EVT, NULL); 2102 } 2103 else 2104 { 2105 APPL_TRACE_ERROR("No resources"); 2106 } 2107 } 2108 2109 if (p_clcb != NULL) 2110 bta_gattc_proc_other_indication(p_clcb, op, p_data, ¬ify); 2111 } 2112 /* no one intersted and need ack? */ 2113 else if (op == GATTC_OPTYPE_INDICATION) 2114 { 2115 APPL_TRACE_DEBUG("%s no one interested, ack now", __func__); 2116 GATTC_SendHandleValueConfirm(conn_id, handle); 2117 } 2118 } 2119 } 2120 else 2121 { 2122 APPL_TRACE_ERROR("%s Indi/Notif for Unknown handle[0x%04x], can not find in local cache.", 2123 __func__, handle); 2124 if (op == GATTC_OPTYPE_INDICATION) 2125 GATTC_SendHandleValueConfirm(conn_id, handle); 2126 } 2127} 2128/******************************************************************************* 2129** 2130** Function bta_gattc_cmpl_cback 2131** 2132** Description client operation complete callback register with BTE GATT. 2133** 2134** Returns None. 2135** 2136*******************************************************************************/ 2137static void bta_gattc_cmpl_cback(UINT16 conn_id, tGATTC_OPTYPE op, tGATT_STATUS status, 2138 tGATT_CL_COMPLETE *p_data) 2139{ 2140 tBTA_GATTC_CLCB *p_clcb ; 2141 tBTA_GATTC_OP_CMPL *p_buf; 2142 UINT16 len = sizeof(tBTA_GATTC_OP_CMPL) + sizeof(tGATT_CL_COMPLETE); 2143 2144 APPL_TRACE_DEBUG("bta_gattc_cmpl_cback: conn_id = %d op = %d status = %d", 2145 conn_id, op, status); 2146 2147 /* notification and indication processed right away */ 2148 if (op == GATTC_OPTYPE_NOTIFICATION || op == GATTC_OPTYPE_INDICATION) 2149 { 2150 bta_gattc_process_indicate(conn_id, op, p_data); 2151 return; 2152 } 2153 /* for all other operation, not expected if w/o connection */ 2154 else if ((p_clcb = bta_gattc_find_clcb_by_conn_id(conn_id)) == NULL) 2155 { 2156 APPL_TRACE_ERROR("bta_gattc_cmpl_cback unknown conn_id = %d, ignore data", conn_id); 2157 return; 2158 } 2159 2160 /* if over BR_EDR, inform PM for mode change */ 2161 if (p_clcb->transport == BTA_TRANSPORT_BR_EDR) 2162 { 2163 bta_sys_busy(BTA_ID_GATTC, BTA_ALL_APP_ID, p_clcb->bda); 2164 bta_sys_idle(BTA_ID_GATTC, BTA_ALL_APP_ID, p_clcb->bda); 2165 } 2166 2167 if ((p_buf = (tBTA_GATTC_OP_CMPL *) GKI_getbuf(len)) != NULL) 2168 { 2169 memset(p_buf, 0, len); 2170 p_buf->hdr.event = BTA_GATTC_OP_CMPL_EVT; 2171 p_buf->hdr.layer_specific = conn_id; 2172 p_buf->status = status; 2173 p_buf->op_code = op; 2174 2175 if (p_data != NULL) 2176 { 2177 p_buf->p_cmpl = (tGATT_CL_COMPLETE *)(p_buf + 1); 2178 memcpy(p_buf->p_cmpl, p_data, sizeof(tGATT_CL_COMPLETE)); 2179 } 2180 2181 bta_sys_sendmsg(p_buf); 2182 } 2183 2184 return; 2185} 2186 2187/******************************************************************************* 2188** 2189** Function bta_gattc_cong_cback 2190** 2191** Description congestion callback for BTA GATT client. 2192** 2193** Returns void 2194** 2195********************************************************************************/ 2196static void bta_gattc_cong_cback (UINT16 conn_id, BOOLEAN congested) 2197{ 2198 tBTA_GATTC_CLCB *p_clcb; 2199 tBTA_GATTC cb_data; 2200 2201 if ((p_clcb = bta_gattc_find_clcb_by_conn_id(conn_id)) != NULL) 2202 { 2203 if (p_clcb->p_rcb->p_cback) 2204 { 2205 cb_data.congest.conn_id = conn_id; 2206 cb_data.congest.congested = congested; 2207 2208 (*p_clcb->p_rcb->p_cback)(BTA_GATTC_CONGEST_EVT, &cb_data); 2209 } 2210 } 2211} 2212 2213#if BLE_INCLUDED == TRUE 2214/******************************************************************************* 2215** 2216** Function bta_gattc_init_clcb_conn 2217** 2218** Description Initaite a BTA CLCB connection 2219** 2220** Returns void 2221** 2222********************************************************************************/ 2223void bta_gattc_init_clcb_conn(UINT8 cif, BD_ADDR remote_bda) 2224{ 2225 tBTA_GATTC_CLCB *p_clcb = NULL; 2226 tBTA_GATTC_DATA gattc_data; 2227 UINT16 conn_id; 2228 2229 /* should always get the connection ID */ 2230 if (GATT_GetConnIdIfConnected(cif, remote_bda, &conn_id, BTA_GATT_TRANSPORT_LE) == FALSE) 2231 { 2232 APPL_TRACE_ERROR("bta_gattc_init_clcb_conn ERROR: not a connected device"); 2233 return; 2234 } 2235 2236 /* initaite a new connection here */ 2237 if ((p_clcb = bta_gattc_clcb_alloc(cif, remote_bda, BTA_GATT_TRANSPORT_LE)) != NULL) 2238 { 2239 gattc_data.hdr.layer_specific = p_clcb->bta_conn_id = conn_id; 2240 2241 gattc_data.api_conn.client_if = cif; 2242 memcpy(gattc_data.api_conn.remote_bda, remote_bda, BD_ADDR_LEN); 2243 gattc_data.api_conn.is_direct = TRUE; 2244 2245 bta_gattc_sm_execute(p_clcb, BTA_GATTC_API_OPEN_EVT, &gattc_data); 2246 } 2247 else 2248 { 2249 APPL_TRACE_ERROR("No resources"); 2250 } 2251} 2252/******************************************************************************* 2253** 2254** Function bta_gattc_process_listen_all 2255** 2256** Description process listen all, send open callback to application for all 2257** connected slave LE link. 2258** 2259** Returns void 2260** 2261********************************************************************************/ 2262void bta_gattc_process_listen_all(UINT8 cif) 2263{ 2264 UINT8 i_conn = 0; 2265 tBTA_GATTC_CONN *p_conn = &bta_gattc_cb.conn_track[0]; 2266 2267 for (i_conn = 0; i_conn < BTA_GATTC_CONN_MAX; i_conn++, p_conn ++) 2268 { 2269 if (p_conn->in_use ) 2270 { 2271 if (bta_gattc_find_clcb_by_cif(cif, p_conn->remote_bda, BTA_GATT_TRANSPORT_LE) == NULL) 2272 { 2273 bta_gattc_init_clcb_conn(cif, p_conn->remote_bda); 2274 } 2275 /* else already connected */ 2276 } 2277 } 2278} 2279/******************************************************************************* 2280** 2281** Function bta_gattc_listen 2282** 2283** Description Start or stop a listen for connection 2284** 2285** Returns void 2286** 2287********************************************************************************/ 2288void bta_gattc_listen(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA * p_msg) 2289{ 2290 tBTA_GATTC_RCB *p_clreg = bta_gattc_cl_get_regcb(p_msg->api_listen.client_if); 2291 tBTA_GATTC cb_data; 2292 UNUSED(p_cb); 2293 2294 cb_data.reg_oper.status = BTA_GATT_ERROR; 2295 cb_data.reg_oper.client_if = p_msg->api_listen.client_if; 2296 2297 if (p_clreg == NULL) 2298 { 2299 APPL_TRACE_ERROR("bta_gattc_listen failed, unknown client_if: %d", 2300 p_msg->api_listen.client_if); 2301 return; 2302 } 2303 /* mark bg conn record */ 2304 if (bta_gattc_mark_bg_conn(p_msg->api_listen.client_if, 2305 (BD_ADDR_PTR) p_msg->api_listen.remote_bda, 2306 p_msg->api_listen.start, 2307 TRUE)) 2308 { 2309 if (!GATT_Listen(p_msg->api_listen.client_if, 2310 p_msg->api_listen.start, 2311 p_msg->api_listen.remote_bda)) 2312 { 2313 APPL_TRACE_ERROR("Listen failure"); 2314 (*p_clreg->p_cback)(BTA_GATTC_LISTEN_EVT, &cb_data); 2315 } 2316 else 2317 { 2318 cb_data.status = BTA_GATT_OK; 2319 2320 (*p_clreg->p_cback)(BTA_GATTC_LISTEN_EVT, &cb_data); 2321 2322 if (p_msg->api_listen.start) 2323 { 2324 /* if listen to a specific target */ 2325 if (p_msg->api_listen.remote_bda != NULL) 2326 { 2327 2328 /* if is a connected remote device */ 2329 if (L2CA_GetBleConnRole(p_msg->api_listen.remote_bda) == HCI_ROLE_SLAVE && 2330 bta_gattc_find_clcb_by_cif(p_msg->api_listen.client_if, 2331 p_msg->api_listen.remote_bda, 2332 BTA_GATT_TRANSPORT_LE) == NULL) 2333 { 2334 2335 bta_gattc_init_clcb_conn(p_msg->api_listen.client_if, 2336 p_msg->api_listen.remote_bda); 2337 } 2338 } 2339 /* if listen to all */ 2340 else 2341 { 2342 LOG_DEBUG("Listen For All now"); 2343 /* go through all connected device and send 2344 callback for all connected slave connection */ 2345 bta_gattc_process_listen_all(p_msg->api_listen.client_if); 2346 } 2347 } 2348 } 2349 } 2350} 2351 2352/******************************************************************************* 2353** 2354** Function bta_gattc_broadcast 2355** 2356** Description Start or stop broadcasting 2357** 2358** Returns void 2359** 2360********************************************************************************/ 2361void bta_gattc_broadcast(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA * p_msg) 2362{ 2363 tBTA_GATTC_RCB *p_clreg = bta_gattc_cl_get_regcb(p_msg->api_listen.client_if); 2364 tBTA_GATTC cb_data; 2365 UNUSED(p_cb); 2366 2367 cb_data.reg_oper.client_if = p_msg->api_listen.client_if; 2368 cb_data.reg_oper.status = BTM_BleBroadcast(p_msg->api_listen.start); 2369 2370 if (p_clreg && p_clreg->p_cback) 2371 (*p_clreg->p_cback)(BTA_GATTC_LISTEN_EVT, &cb_data); 2372} 2373#endif 2374#endif 2375