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 utility function. 22 * 23 ******************************************************************************/ 24 25 #define LOG_TAG "bt_bta_gattc" 26 27 #include "bt_target.h" 28 29 #include <string.h> 30 31 #include "bt_common.h" 32 #include "bta_gattc_int.h" 33 #include "bta_sys.h" 34 #include "btcore/include/bdaddr.h" 35 #include "l2c_api.h" 36 #include "utl.h" 37 38 /***************************************************************************** 39 * Constants 40 ****************************************************************************/ 41 42 static const uint8_t base_uuid[LEN_UUID_128] = { 43 0xFB, 0x34, 0x9B, 0x5F, 0x80, 0x00, 0x00, 0x80, 44 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; 45 46 static const BD_ADDR dummy_bda = {0, 0, 0, 0, 0, 0}; 47 48 /******************************************************************************* 49 * 50 * Function bta_gatt_convert_uuid16_to_uuid128 51 * 52 * Description Convert a 16 bits UUID to be an standard 128 bits one. 53 * 54 * Returns true if two uuid match; false otherwise. 55 * 56 ******************************************************************************/ 57 void bta_gatt_convert_uuid16_to_uuid128(uint8_t uuid_128[LEN_UUID_128], 58 uint16_t uuid_16) { 59 uint8_t* p = &uuid_128[LEN_UUID_128 - 4]; 60 61 memcpy(uuid_128, base_uuid, LEN_UUID_128); 62 63 UINT16_TO_STREAM(p, uuid_16); 64 } 65 /******************************************************************************* 66 * 67 * Function bta_gattc_uuid_compare 68 * 69 * Description Compare two UUID to see if they are the same. 70 * 71 * Returns true if two uuid match; false otherwise. 72 * 73 ******************************************************************************/ 74 bool bta_gattc_uuid_compare(const tBT_UUID* p_src, const tBT_UUID* p_tar, 75 bool is_precise) { 76 uint8_t su[LEN_UUID_128], tu[LEN_UUID_128]; 77 const uint8_t *ps, *pt; 78 79 /* any of the UUID is unspecified */ 80 if (p_src == 0 || p_tar == 0) { 81 if (is_precise) 82 return false; 83 else 84 return true; 85 } 86 87 /* If both are 16-bit, we can do a simple compare */ 88 if (p_src->len == 2 && p_tar->len == 2) { 89 return p_src->uu.uuid16 == p_tar->uu.uuid16; 90 } 91 92 /* One or both of the UUIDs is 128-bit */ 93 if (p_src->len == LEN_UUID_16) { 94 /* convert a 16 bits UUID to 128 bits value */ 95 bta_gatt_convert_uuid16_to_uuid128(su, p_src->uu.uuid16); 96 ps = su; 97 } else 98 ps = p_src->uu.uuid128; 99 100 if (p_tar->len == LEN_UUID_16) { 101 /* convert a 16 bits UUID to 128 bits value */ 102 bta_gatt_convert_uuid16_to_uuid128(tu, p_tar->uu.uuid16); 103 pt = tu; 104 } else 105 pt = p_tar->uu.uuid128; 106 107 return (memcmp(ps, pt, LEN_UUID_128) == 0); 108 } 109 110 /******************************************************************************* 111 * 112 * Function bta_gattc_cl_get_regcb 113 * 114 * Description get registration control block by client interface. 115 * 116 * Returns pointer to the regcb 117 * 118 ******************************************************************************/ 119 tBTA_GATTC_RCB* bta_gattc_cl_get_regcb(uint8_t client_if) { 120 uint8_t i = 0; 121 tBTA_GATTC_RCB* p_clrcb = &bta_gattc_cb.cl_rcb[0]; 122 123 for (i = 0; i < BTA_GATTC_CL_MAX; i++, p_clrcb++) { 124 if (p_clrcb->in_use && p_clrcb->client_if == client_if) return p_clrcb; 125 } 126 return NULL; 127 } 128 /******************************************************************************* 129 * 130 * Function bta_gattc_num_reg_app 131 * 132 * Description find the number of registered application. 133 * 134 * Returns pointer to the regcb 135 * 136 ******************************************************************************/ 137 uint8_t bta_gattc_num_reg_app(void) { 138 uint8_t i = 0, j = 0; 139 140 for (i = 0; i < BTA_GATTC_CL_MAX; i++) { 141 if (bta_gattc_cb.cl_rcb[i].in_use) j++; 142 } 143 return j; 144 } 145 /******************************************************************************* 146 * 147 * Function bta_gattc_find_clcb_by_cif 148 * 149 * Description get clcb by client interface and remote bd adddress 150 * 151 * Returns pointer to the clcb 152 * 153 ******************************************************************************/ 154 tBTA_GATTC_CLCB* bta_gattc_find_clcb_by_cif(uint8_t client_if, 155 BD_ADDR remote_bda, 156 tBTA_TRANSPORT transport) { 157 tBTA_GATTC_CLCB* p_clcb = &bta_gattc_cb.clcb[0]; 158 uint8_t i; 159 160 for (i = 0; i < BTA_GATTC_CLCB_MAX; i++, p_clcb++) { 161 if (p_clcb->in_use && p_clcb->p_rcb->client_if == client_if && 162 p_clcb->transport == transport && bdcmp(p_clcb->bda, remote_bda) == 0) 163 return p_clcb; 164 } 165 return NULL; 166 } 167 /******************************************************************************* 168 * 169 * Function bta_gattc_find_clcb_by_conn_id 170 * 171 * Description get clcb by connection ID 172 * 173 * Returns pointer to the clcb 174 * 175 ******************************************************************************/ 176 tBTA_GATTC_CLCB* bta_gattc_find_clcb_by_conn_id(uint16_t conn_id) { 177 tBTA_GATTC_CLCB* p_clcb = &bta_gattc_cb.clcb[0]; 178 uint8_t i; 179 180 for (i = 0; i < BTA_GATTC_CLCB_MAX; i++, p_clcb++) { 181 if (p_clcb->in_use && p_clcb->bta_conn_id == conn_id) return p_clcb; 182 } 183 return NULL; 184 } 185 186 /******************************************************************************* 187 * 188 * Function bta_gattc_clcb_alloc 189 * 190 * Description allocate CLCB 191 * 192 * Returns pointer to the clcb 193 * 194 ******************************************************************************/ 195 tBTA_GATTC_CLCB* bta_gattc_clcb_alloc(tBTA_GATTC_IF client_if, 196 BD_ADDR remote_bda, 197 tBTA_TRANSPORT transport) { 198 uint8_t i_clcb = 0; 199 tBTA_GATTC_CLCB* p_clcb = NULL; 200 201 for (i_clcb = 0; i_clcb < BTA_GATTC_CLCB_MAX; i_clcb++) { 202 if (!bta_gattc_cb.clcb[i_clcb].in_use) { 203 #if (BTA_GATT_DEBUG == TRUE) 204 APPL_TRACE_DEBUG("bta_gattc_clcb_alloc: found clcb[%d] available", 205 i_clcb); 206 #endif 207 p_clcb = &bta_gattc_cb.clcb[i_clcb]; 208 p_clcb->in_use = true; 209 p_clcb->status = BTA_GATT_OK; 210 p_clcb->transport = transport; 211 bdcpy(p_clcb->bda, remote_bda); 212 213 p_clcb->p_rcb = bta_gattc_cl_get_regcb(client_if); 214 215 p_clcb->p_srcb = bta_gattc_find_srcb(remote_bda); 216 if (p_clcb->p_srcb == NULL) 217 p_clcb->p_srcb = bta_gattc_srcb_alloc(remote_bda); 218 219 if (p_clcb->p_rcb != NULL && p_clcb->p_srcb != NULL) { 220 p_clcb->p_srcb->num_clcb++; 221 p_clcb->p_rcb->num_clcb++; 222 } else { 223 /* release this clcb if clcb or srcb allocation failed */ 224 p_clcb->in_use = false; 225 p_clcb = NULL; 226 } 227 break; 228 } 229 } 230 return p_clcb; 231 } 232 /******************************************************************************* 233 * 234 * Function bta_gattc_find_alloc_clcb 235 * 236 * Description find or allocate CLCB if not found. 237 * 238 * Returns pointer to the clcb 239 * 240 ******************************************************************************/ 241 tBTA_GATTC_CLCB* bta_gattc_find_alloc_clcb(tBTA_GATTC_IF client_if, 242 BD_ADDR remote_bda, 243 tBTA_TRANSPORT transport) { 244 tBTA_GATTC_CLCB* p_clcb; 245 246 p_clcb = bta_gattc_find_clcb_by_cif(client_if, remote_bda, transport); 247 if (p_clcb == NULL) { 248 p_clcb = bta_gattc_clcb_alloc(client_if, remote_bda, transport); 249 } 250 return p_clcb; 251 } 252 253 /******************************************************************************* 254 * 255 * Function bta_gattc_clcb_dealloc 256 * 257 * Description Deallocte a clcb 258 * 259 * Returns pointer to the clcb 260 * 261 ******************************************************************************/ 262 void bta_gattc_clcb_dealloc(tBTA_GATTC_CLCB* p_clcb) { 263 tBTA_GATTC_SERV* p_srcb = NULL; 264 265 if (p_clcb) { 266 p_srcb = p_clcb->p_srcb; 267 if (p_srcb->num_clcb) p_srcb->num_clcb--; 268 269 if (p_clcb->p_rcb->num_clcb) p_clcb->p_rcb->num_clcb--; 270 271 /* if the srcb is no longer needed, reset the state */ 272 if (p_srcb->num_clcb == 0) { 273 p_srcb->connected = false; 274 p_srcb->state = BTA_GATTC_SERV_IDLE; 275 p_srcb->mtu = 0; 276 277 /* clean up cache */ 278 if (p_srcb->p_srvc_cache) { 279 list_free(p_srcb->p_srvc_cache); 280 p_srcb->p_srvc_cache = NULL; 281 } 282 } 283 284 osi_free_and_reset((void**)&p_clcb->p_q_cmd); 285 memset(p_clcb, 0, sizeof(tBTA_GATTC_CLCB)); 286 } else { 287 APPL_TRACE_ERROR("bta_gattc_clcb_dealloc p_clcb=NULL"); 288 } 289 } 290 291 /******************************************************************************* 292 * 293 * Function bta_gattc_find_srcb 294 * 295 * Description find server cache by remote bd address currently in use 296 * 297 * Returns pointer to the server cache. 298 * 299 ******************************************************************************/ 300 tBTA_GATTC_SERV* bta_gattc_find_srcb(BD_ADDR bda) { 301 tBTA_GATTC_SERV* p_srcb = &bta_gattc_cb.known_server[0]; 302 uint8_t i; 303 304 for (i = 0; i < BTA_GATTC_KNOWN_SR_MAX; i++, p_srcb++) { 305 if (p_srcb->in_use && bdcmp(p_srcb->server_bda, bda) == 0) return p_srcb; 306 } 307 return NULL; 308 } 309 310 /******************************************************************************* 311 * 312 * Function bta_gattc_find_srvr_cache 313 * 314 * Description find server cache by remote bd address 315 * 316 * Returns pointer to the server cache. 317 * 318 ******************************************************************************/ 319 tBTA_GATTC_SERV* bta_gattc_find_srvr_cache(BD_ADDR bda) { 320 tBTA_GATTC_SERV* p_srcb = &bta_gattc_cb.known_server[0]; 321 uint8_t i; 322 323 for (i = 0; i < BTA_GATTC_KNOWN_SR_MAX; i++, p_srcb++) { 324 if (bdcmp(p_srcb->server_bda, bda) == 0) return p_srcb; 325 } 326 return NULL; 327 } 328 /******************************************************************************* 329 * 330 * Function bta_gattc_find_scb_by_cid 331 * 332 * Description find server control block by connection ID 333 * 334 * Returns pointer to the server cache. 335 * 336 ******************************************************************************/ 337 tBTA_GATTC_SERV* bta_gattc_find_scb_by_cid(uint16_t conn_id) { 338 tBTA_GATTC_CLCB* p_clcb = bta_gattc_find_clcb_by_conn_id(conn_id); 339 340 if (p_clcb) 341 return p_clcb->p_srcb; 342 else 343 return NULL; 344 } 345 /******************************************************************************* 346 * 347 * Function bta_gattc_srcb_alloc 348 * 349 * Description allocate server cache control block 350 * 351 * Returns pointer to the server cache. 352 * 353 ******************************************************************************/ 354 tBTA_GATTC_SERV* bta_gattc_srcb_alloc(BD_ADDR bda) { 355 tBTA_GATTC_SERV *p_tcb = &bta_gattc_cb.known_server[0], *p_recycle = NULL; 356 bool found = false; 357 uint8_t i; 358 359 for (i = 0; i < BTA_GATTC_KNOWN_SR_MAX; i++, p_tcb++) { 360 if (!p_tcb->in_use) { 361 found = true; 362 break; 363 } else if (!p_tcb->connected) { 364 p_recycle = p_tcb; 365 } 366 } 367 368 /* if not found, try to recycle one known device */ 369 if (!found && !p_recycle) 370 p_tcb = NULL; 371 else if (!found && p_recycle) 372 p_tcb = p_recycle; 373 374 if (p_tcb != NULL) { 375 if (p_tcb->p_srvc_cache != NULL) list_free(p_tcb->p_srvc_cache); 376 377 osi_free_and_reset((void**)&p_tcb->p_srvc_list); 378 memset(p_tcb, 0, sizeof(tBTA_GATTC_SERV)); 379 380 p_tcb->in_use = true; 381 bdcpy(p_tcb->server_bda, bda); 382 } 383 return p_tcb; 384 } 385 /******************************************************************************* 386 * 387 * Function bta_gattc_enqueue 388 * 389 * Description enqueue a client request in clcb. 390 * 391 * Returns success or failure. 392 * 393 ******************************************************************************/ 394 bool bta_gattc_enqueue(tBTA_GATTC_CLCB* p_clcb, tBTA_GATTC_DATA* p_data) { 395 if (p_clcb->p_q_cmd == NULL) { 396 p_clcb->p_q_cmd = p_data; 397 return true; 398 } 399 400 APPL_TRACE_ERROR("%s: already has a pending command!!", __func__); 401 /* skip the callback now. ----- need to send callback ? */ 402 return false; 403 } 404 405 /******************************************************************************* 406 * 407 * Function bta_gattc_check_notif_registry 408 * 409 * Description check if the service notificaition has been registered. 410 * 411 * Returns 412 * 413 ******************************************************************************/ 414 bool bta_gattc_check_notif_registry(tBTA_GATTC_RCB* p_clreg, 415 tBTA_GATTC_SERV* p_srcb, 416 tBTA_GATTC_NOTIFY* p_notify) { 417 uint8_t i; 418 419 for (i = 0; i < BTA_GATTC_NOTIF_REG_MAX; i++) { 420 if (p_clreg->notif_reg[i].in_use && 421 bdcmp(p_clreg->notif_reg[i].remote_bda, p_srcb->server_bda) == 0 && 422 p_clreg->notif_reg[i].handle == p_notify->handle) { 423 APPL_TRACE_DEBUG("Notification registered!"); 424 return true; 425 } 426 } 427 return false; 428 } 429 /******************************************************************************* 430 * 431 * Function bta_gattc_clear_notif_registration 432 * 433 * Description Clear up the notification registration information by 434 * BD_ADDR. 435 * Where handle is between start_handle and end_handle, and 436 * start_handle and end_handle are boundaries of service 437 * containing characteristic. 438 * 439 * Returns None. 440 * 441 ******************************************************************************/ 442 void bta_gattc_clear_notif_registration(tBTA_GATTC_SERV* p_srcb, 443 uint16_t conn_id, uint16_t start_handle, 444 uint16_t end_handle) { 445 BD_ADDR remote_bda; 446 tBTA_GATTC_IF gatt_if; 447 tBTA_GATTC_RCB* p_clrcb; 448 uint8_t i; 449 tGATT_TRANSPORT transport; 450 uint16_t handle; 451 452 if (GATT_GetConnectionInfor(conn_id, &gatt_if, remote_bda, &transport)) { 453 p_clrcb = bta_gattc_cl_get_regcb(gatt_if); 454 if (p_clrcb != NULL) { 455 for (i = 0; i < BTA_GATTC_NOTIF_REG_MAX; i++) { 456 if (p_clrcb->notif_reg[i].in_use && 457 !bdcmp(p_clrcb->notif_reg[i].remote_bda, remote_bda)) { 458 /* It's enough to get service or characteristic handle, as 459 * clear boundaries are always around service. 460 */ 461 handle = p_clrcb->notif_reg[i].handle; 462 if (handle >= start_handle && handle <= end_handle) 463 memset(&p_clrcb->notif_reg[i], 0, sizeof(tBTA_GATTC_NOTIF_REG)); 464 } 465 } 466 } 467 } else { 468 APPL_TRACE_ERROR( 469 "can not clear indication/notif registration for unknown app"); 470 } 471 return; 472 } 473 474 /******************************************************************************* 475 * 476 * Function bta_gattc_mark_bg_conn 477 * 478 * Description mark background connection status when a bg connection is 479 * initiated or terminated. 480 * 481 * Returns true if success; false otherwise. 482 * 483 ******************************************************************************/ 484 bool bta_gattc_mark_bg_conn(tBTA_GATTC_IF client_if, BD_ADDR_PTR remote_bda_ptr, 485 bool add) { 486 tBTA_GATTC_BG_TCK* p_bg_tck = &bta_gattc_cb.bg_track[0]; 487 uint8_t i = 0; 488 tBTA_GATTC_CIF_MASK* p_cif_mask; 489 490 for (i = 0; i < BTA_GATTC_KNOWN_SR_MAX; i++, p_bg_tck++) { 491 if (p_bg_tck->in_use && 492 ((remote_bda_ptr != NULL && 493 bdcmp(p_bg_tck->remote_bda, remote_bda_ptr) == 0) || 494 (remote_bda_ptr == NULL && 495 bdcmp(p_bg_tck->remote_bda, dummy_bda) == 0))) { 496 p_cif_mask = &p_bg_tck->cif_mask; 497 498 if (add) /* mask on the cif bit */ 499 *p_cif_mask |= (1 << (client_if - 1)); 500 else { 501 if (client_if != 0) 502 *p_cif_mask &= (~(1 << (client_if - 1))); 503 else 504 *p_cif_mask = 0; 505 } 506 /* no BG connection for this device, make it available */ 507 if (p_bg_tck->cif_mask == 0) { 508 memset(p_bg_tck, 0, sizeof(tBTA_GATTC_BG_TCK)); 509 } 510 return true; 511 } 512 } 513 if (!add) { 514 if (remote_bda_ptr) { 515 bdstr_t bdstr = {0}; 516 APPL_TRACE_ERROR( 517 "%s unable to find the bg connection mask for: %s", __func__, 518 bdaddr_to_string((bt_bdaddr_t*)remote_bda_ptr, bdstr, sizeof(bdstr))); 519 } 520 return false; 521 } else /* adding a new device mask */ 522 { 523 for (i = 0, p_bg_tck = &bta_gattc_cb.bg_track[0]; 524 i < BTA_GATTC_KNOWN_SR_MAX; i++, p_bg_tck++) { 525 if (!p_bg_tck->in_use) { 526 p_bg_tck->in_use = true; 527 if (remote_bda_ptr) 528 bdcpy(p_bg_tck->remote_bda, remote_bda_ptr); 529 else 530 bdcpy(p_bg_tck->remote_bda, dummy_bda); 531 532 p_cif_mask = &p_bg_tck->cif_mask; 533 534 *p_cif_mask = (1 << (client_if - 1)); 535 return true; 536 } 537 } 538 APPL_TRACE_ERROR("no available space to mark the bg connection status"); 539 return false; 540 } 541 } 542 /******************************************************************************* 543 * 544 * Function bta_gattc_check_bg_conn 545 * 546 * Description check if this is a background connection background 547 * connection. 548 * 549 * Returns true if success; false otherwise. 550 * 551 ******************************************************************************/ 552 bool bta_gattc_check_bg_conn(tBTA_GATTC_IF client_if, BD_ADDR remote_bda, 553 uint8_t role) { 554 tBTA_GATTC_BG_TCK* p_bg_tck = &bta_gattc_cb.bg_track[0]; 555 uint8_t i = 0; 556 bool is_bg_conn = false; 557 558 for (i = 0; i < BTA_GATTC_KNOWN_SR_MAX && !is_bg_conn; i++, p_bg_tck++) { 559 if (p_bg_tck->in_use && (bdcmp(p_bg_tck->remote_bda, remote_bda) == 0 || 560 bdcmp(p_bg_tck->remote_bda, dummy_bda) == 0)) { 561 if (((p_bg_tck->cif_mask & (1 << (client_if - 1))) != 0) && 562 role == HCI_ROLE_MASTER) 563 is_bg_conn = true; 564 } 565 } 566 return is_bg_conn; 567 } 568 /******************************************************************************* 569 * 570 * Function bta_gattc_send_open_cback 571 * 572 * Description send open callback 573 * 574 * Returns 575 * 576 ******************************************************************************/ 577 void bta_gattc_send_open_cback(tBTA_GATTC_RCB* p_clreg, tBTA_GATT_STATUS status, 578 BD_ADDR remote_bda, uint16_t conn_id, 579 tBTA_TRANSPORT transport, uint16_t mtu) { 580 tBTA_GATTC cb_data; 581 582 if (p_clreg->p_cback) { 583 memset(&cb_data, 0, sizeof(tBTA_GATTC)); 584 585 cb_data.open.status = status; 586 cb_data.open.client_if = p_clreg->client_if; 587 cb_data.open.conn_id = conn_id; 588 cb_data.open.mtu = mtu; 589 cb_data.open.transport = transport; 590 bdcpy(cb_data.open.remote_bda, remote_bda); 591 592 (*p_clreg->p_cback)(BTA_GATTC_OPEN_EVT, &cb_data); 593 } 594 } 595 /******************************************************************************* 596 * 597 * Function bta_gattc_conn_alloc 598 * 599 * Description allocate connection tracking spot 600 * 601 * Returns pointer to the clcb 602 * 603 ******************************************************************************/ 604 tBTA_GATTC_CONN* bta_gattc_conn_alloc(BD_ADDR remote_bda) { 605 uint8_t i_conn = 0; 606 tBTA_GATTC_CONN* p_conn = &bta_gattc_cb.conn_track[0]; 607 608 for (i_conn = 0; i_conn < BTA_GATTC_CONN_MAX; i_conn++, p_conn++) { 609 if (!p_conn->in_use) { 610 #if (BTA_GATT_DEBUG == TRUE) 611 APPL_TRACE_DEBUG("bta_gattc_conn_alloc: found conn_track[%d] available", 612 i_conn); 613 #endif 614 p_conn->in_use = true; 615 bdcpy(p_conn->remote_bda, remote_bda); 616 return p_conn; 617 } 618 } 619 return NULL; 620 } 621 622 /******************************************************************************* 623 * 624 * Function bta_gattc_conn_find 625 * 626 * Description allocate connection tracking spot 627 * 628 * Returns pointer to the clcb 629 * 630 ******************************************************************************/ 631 tBTA_GATTC_CONN* bta_gattc_conn_find(BD_ADDR remote_bda) { 632 uint8_t i_conn = 0; 633 tBTA_GATTC_CONN* p_conn = &bta_gattc_cb.conn_track[0]; 634 635 for (i_conn = 0; i_conn < BTA_GATTC_CONN_MAX; i_conn++, p_conn++) { 636 if (p_conn->in_use && bdcmp(remote_bda, p_conn->remote_bda) == 0) { 637 #if (BTA_GATT_DEBUG == TRUE) 638 APPL_TRACE_DEBUG("bta_gattc_conn_find: found conn_track[%d] matched", 639 i_conn); 640 #endif 641 return p_conn; 642 } 643 } 644 return NULL; 645 } 646 647 /******************************************************************************* 648 * 649 * Function bta_gattc_conn_find_alloc 650 * 651 * Description find or allocate connection tracking spot 652 * 653 * Returns pointer to the clcb 654 * 655 ******************************************************************************/ 656 tBTA_GATTC_CONN* bta_gattc_conn_find_alloc(BD_ADDR remote_bda) { 657 tBTA_GATTC_CONN* p_conn = bta_gattc_conn_find(remote_bda); 658 659 if (p_conn == NULL) { 660 p_conn = bta_gattc_conn_alloc(remote_bda); 661 } 662 return p_conn; 663 } 664 665 /******************************************************************************* 666 * 667 * Function bta_gattc_conn_dealloc 668 * 669 * Description de-allocate connection tracking spot 670 * 671 * Returns pointer to the clcb 672 * 673 ******************************************************************************/ 674 bool bta_gattc_conn_dealloc(BD_ADDR remote_bda) { 675 tBTA_GATTC_CONN* p_conn = bta_gattc_conn_find(remote_bda); 676 677 if (p_conn != NULL) { 678 p_conn->in_use = false; 679 memset(p_conn->remote_bda, 0, BD_ADDR_LEN); 680 return true; 681 } 682 return false; 683 } 684 685 /******************************************************************************* 686 * 687 * Function bta_gattc_find_int_conn_clcb 688 * 689 * Description try to locate a clcb when an internal connecion event 690 * arrives. 691 * 692 * Returns pointer to the clcb 693 * 694 ******************************************************************************/ 695 tBTA_GATTC_CLCB* bta_gattc_find_int_conn_clcb(tBTA_GATTC_DATA* p_msg) { 696 tBTA_GATTC_CLCB* p_clcb = NULL; 697 698 if (p_msg->int_conn.role == HCI_ROLE_SLAVE) 699 bta_gattc_conn_find_alloc(p_msg->int_conn.remote_bda); 700 701 /* try to locate a logic channel */ 702 p_clcb = bta_gattc_find_clcb_by_cif(p_msg->int_conn.client_if, 703 p_msg->int_conn.remote_bda, 704 p_msg->int_conn.transport); 705 if (p_clcb == NULL) { 706 /* for a background connection or listening connection */ 707 if (/*p_msg->int_conn.role == HCI_ROLE_SLAVE || */ 708 bta_gattc_check_bg_conn(p_msg->int_conn.client_if, 709 p_msg->int_conn.remote_bda, 710 p_msg->int_conn.role)) { 711 /* allocate a new channel */ 712 p_clcb = bta_gattc_clcb_alloc(p_msg->int_conn.client_if, 713 p_msg->int_conn.remote_bda, 714 p_msg->int_conn.transport); 715 } 716 } 717 return p_clcb; 718 } 719 720 /******************************************************************************* 721 * 722 * Function bta_gattc_find_int_disconn_clcb 723 * 724 * Description try to locate a clcb when an internal disconnect callback 725 * arrives. 726 * 727 * Returns pointer to the clcb 728 * 729 ******************************************************************************/ 730 tBTA_GATTC_CLCB* bta_gattc_find_int_disconn_clcb(tBTA_GATTC_DATA* p_msg) { 731 tBTA_GATTC_CLCB* p_clcb = NULL; 732 733 bta_gattc_conn_dealloc(p_msg->int_conn.remote_bda); 734 p_clcb = bta_gattc_find_clcb_by_conn_id(p_msg->int_conn.hdr.layer_specific); 735 if (p_clcb == NULL) { 736 /* connection attempt failed, send connection callback event */ 737 p_clcb = bta_gattc_find_clcb_by_cif(p_msg->int_conn.client_if, 738 p_msg->int_conn.remote_bda, 739 p_msg->int_conn.transport); 740 } 741 if (p_clcb == NULL) { 742 APPL_TRACE_DEBUG(" disconnection ID: [%d] not used by BTA", 743 p_msg->int_conn.hdr.layer_specific); 744 } 745 return p_clcb; 746 } 747