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 #include "bt_target.h" 26 27 #if defined(BTA_GATT_INCLUDED) && (BTA_GATT_INCLUDED == TRUE) 28 29 #include <string.h> 30 #include "utl.h" 31 #include "gki.h" 32 #include "bta_sys.h" 33 #include "bta_gattc_int.h" 34 #include "l2c_api.h" 35 #include "bd.h" 36 37 /***************************************************************************** 38 ** Constants 39 *****************************************************************************/ 40 41 42 static const UINT8 base_uuid[LEN_UUID_128] = {0xFB, 0x34, 0x9B, 0x5F, 0x80, 0x00, 0x00, 0x80, 43 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; 44 45 static const BD_ADDR dummy_bda = {0,0,0,0,0,0}; 46 47 /******************************************************************************* 48 ** 49 ** Function bta_gatt_convert_uuid16_to_uuid128 50 ** 51 ** Description Convert a 16 bits UUID to be an standard 128 bits one. 52 ** 53 ** Returns TRUE if two uuid match; FALSE otherwise. 54 ** 55 *******************************************************************************/ 56 void bta_gatt_convert_uuid16_to_uuid128(UINT8 uuid_128[LEN_UUID_128], UINT16 uuid_16) 57 { 58 UINT8 *p = &uuid_128[LEN_UUID_128 - 4]; 59 60 memcpy (uuid_128, base_uuid, LEN_UUID_128); 61 62 UINT16_TO_STREAM(p, uuid_16); 63 } 64 /******************************************************************************* 65 ** 66 ** Function bta_gattc_uuid_compare 67 ** 68 ** Description Compare two UUID to see if they are the same. 69 ** 70 ** Returns TRUE if two uuid match; FALSE otherwise. 71 ** 72 *******************************************************************************/ 73 BOOLEAN bta_gattc_uuid_compare (tBT_UUID *p_src, tBT_UUID *p_tar, BOOLEAN is_precise) 74 { 75 UINT8 su[LEN_UUID_128], tu[LEN_UUID_128]; 76 UINT8 *ps, *pt; 77 78 /* any of the UUID is unspecified */ 79 if (p_src == 0 || p_tar == 0) 80 { 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 { 90 return p_src->uu.uuid16 == p_tar->uu.uuid16; 91 } 92 93 /* One or both of the UUIDs is 128-bit */ 94 if (p_src->len == LEN_UUID_16) 95 { 96 /* convert a 16 bits UUID to 128 bits value */ 97 bta_gatt_convert_uuid16_to_uuid128(su, p_src->uu.uuid16); 98 ps = su; 99 } 100 else 101 ps = p_src->uu.uuid128; 102 103 if (p_tar->len == LEN_UUID_16) 104 { 105 /* convert a 16 bits UUID to 128 bits value */ 106 bta_gatt_convert_uuid16_to_uuid128(tu, p_tar->uu.uuid16); 107 pt = tu; 108 } 109 else 110 pt = p_tar->uu.uuid128; 111 112 return(memcmp(ps, pt, LEN_UUID_128) == 0); 113 } 114 115 /******************************************************************************* 116 ** 117 ** Function bta_gattc_cl_get_regcb 118 ** 119 ** Description get registration control block by client interface. 120 ** 121 ** Returns pointer to the regcb 122 ** 123 *******************************************************************************/ 124 tBTA_GATTC_RCB * bta_gattc_cl_get_regcb(UINT8 client_if) 125 { 126 UINT8 i = 0; 127 tBTA_GATTC_RCB *p_clrcb = &bta_gattc_cb.cl_rcb[0]; 128 129 for (i = 0; i < BTA_GATTC_CL_MAX; i ++, p_clrcb ++) 130 { 131 if (p_clrcb->in_use && 132 p_clrcb->client_if == client_if) 133 return p_clrcb; 134 } 135 return NULL; 136 } 137 /******************************************************************************* 138 ** 139 ** Function bta_gattc_num_reg_app 140 ** 141 ** Description find the number of registered application. 142 ** 143 ** Returns pointer to the regcb 144 ** 145 *******************************************************************************/ 146 UINT8 bta_gattc_num_reg_app(void) 147 { 148 UINT8 i = 0, j = 0; 149 150 for (i = 0; i < BTA_GATTC_CL_MAX; i ++) 151 { 152 if (bta_gattc_cb.cl_rcb[i].in_use) 153 j ++; 154 } 155 return j; 156 } 157 /******************************************************************************* 158 ** 159 ** Function bta_gattc_find_clcb_by_cif 160 ** 161 ** Description get clcb by client interface and remote bd adddress 162 ** 163 ** Returns pointer to the clcb 164 ** 165 *******************************************************************************/ 166 tBTA_GATTC_CLCB * bta_gattc_find_clcb_by_cif (UINT8 client_if, BD_ADDR remote_bda) 167 { 168 tBTA_GATTC_CLCB *p_clcb = &bta_gattc_cb.clcb[0]; 169 UINT8 i; 170 171 for (i = 0; i < BTA_GATTC_CLCB_MAX; i ++, p_clcb ++) 172 { 173 if (p_clcb->in_use && 174 p_clcb->p_rcb->client_if == client_if && 175 bdcmp(p_clcb->bda, remote_bda) == 0) 176 return p_clcb; 177 } 178 return NULL; 179 } 180 /******************************************************************************* 181 ** 182 ** Function bta_gattc_find_clcb_by_conn_id 183 ** 184 ** Description get clcb by connection ID 185 ** 186 ** Returns pointer to the clcb 187 ** 188 *******************************************************************************/ 189 tBTA_GATTC_CLCB * bta_gattc_find_clcb_by_conn_id (UINT16 conn_id) 190 { 191 tBTA_GATTC_CLCB *p_clcb = &bta_gattc_cb.clcb[0]; 192 UINT8 i; 193 194 for (i = 0; i < BTA_GATTC_CLCB_MAX; i ++, p_clcb ++) 195 { 196 if (p_clcb->in_use && 197 p_clcb->bta_conn_id == conn_id) 198 return p_clcb; 199 } 200 return NULL; 201 } 202 203 /******************************************************************************* 204 ** 205 ** Function bta_gattc_clcb_alloc 206 ** 207 ** Description allocate CLCB 208 ** 209 ** Returns pointer to the clcb 210 ** 211 *******************************************************************************/ 212 tBTA_GATTC_CLCB * bta_gattc_clcb_alloc(tBTA_GATTC_IF client_if, BD_ADDR remote_bda) 213 { 214 UINT8 i_clcb = 0; 215 tBTA_GATTC_CLCB *p_clcb = NULL; 216 217 for (i_clcb = 0; i_clcb < BTA_GATTC_CLCB_MAX; i_clcb++) 218 { 219 if (!bta_gattc_cb.clcb[i_clcb].in_use) 220 { 221 #if BTA_GATT_DEBUG == TRUE 222 APPL_TRACE_DEBUG1("bta_gattc_clcb_alloc: found clcb[%d] available",i_clcb); 223 #endif 224 p_clcb = &bta_gattc_cb.clcb[i_clcb]; 225 p_clcb->in_use = TRUE; 226 p_clcb->status = BTA_GATT_OK; 227 bdcpy(p_clcb->bda, remote_bda); 228 229 p_clcb->p_rcb = bta_gattc_cl_get_regcb(client_if); 230 231 if ((p_clcb->p_srcb = bta_gattc_find_srcb(remote_bda)) == NULL) 232 p_clcb->p_srcb = bta_gattc_srcb_alloc(remote_bda); 233 234 if (p_clcb->p_rcb != NULL && p_clcb->p_srcb != NULL) 235 { 236 p_clcb->p_srcb->num_clcb ++; 237 p_clcb->p_rcb->num_clcb ++; 238 } 239 else 240 { 241 /* release this clcb if clcb or srcb allocation failed */ 242 p_clcb->in_use = FALSE; 243 p_clcb = NULL; 244 } 245 break; 246 } 247 } 248 return p_clcb; 249 } 250 /******************************************************************************* 251 ** 252 ** Function bta_gattc_find_alloc_clcb 253 ** 254 ** Description find or allocate CLCB if not found. 255 ** 256 ** Returns pointer to the clcb 257 ** 258 *******************************************************************************/ 259 tBTA_GATTC_CLCB *bta_gattc_find_alloc_clcb(tBTA_GATTC_IF client_if, BD_ADDR remote_bda) 260 { 261 tBTA_GATTC_CLCB *p_clcb ; 262 263 if ((p_clcb = bta_gattc_find_clcb_by_cif(client_if, remote_bda)) == NULL) 264 { 265 p_clcb = bta_gattc_clcb_alloc(client_if, remote_bda); 266 } 267 return p_clcb; 268 } 269 270 /******************************************************************************* 271 ** 272 ** Function bta_gattc_clcb_dealloc 273 ** 274 ** Description Deallocte a clcb 275 ** 276 ** Returns pointer to the clcb 277 ** 278 *******************************************************************************/ 279 void bta_gattc_clcb_dealloc(tBTA_GATTC_CLCB *p_clcb) 280 { 281 tBTA_GATTC_SERV *p_srcb = NULL; 282 283 if (p_clcb) 284 { 285 p_srcb = p_clcb->p_srcb; 286 if (p_srcb->num_clcb) 287 p_srcb->num_clcb --; 288 289 if (p_clcb->p_rcb->num_clcb) 290 p_clcb->p_rcb->num_clcb --; 291 292 /* if the srcb is no longer needed, reset the state */ 293 if ( p_srcb->num_clcb == 0) 294 { 295 p_srcb->connected = FALSE; 296 p_srcb->state = BTA_GATTC_SERV_IDLE; 297 } 298 299 utl_freebuf((void **)&p_clcb->p_q_cmd); 300 301 memset(p_clcb, 0, sizeof(tBTA_GATTC_CLCB)); 302 } 303 else 304 { 305 APPL_TRACE_ERROR0("bta_gattc_clcb_dealloc p_clcb=NULL"); 306 } 307 } 308 309 /******************************************************************************* 310 ** 311 ** Function bta_gattc_find_srcb 312 ** 313 ** Description find server cache by remote bd address currently in use 314 ** 315 ** Returns pointer to the server cache. 316 ** 317 *******************************************************************************/ 318 tBTA_GATTC_SERV * bta_gattc_find_srcb(BD_ADDR bda) 319 { 320 tBTA_GATTC_SERV *p_srcb = &bta_gattc_cb.known_server[0]; 321 UINT8 i; 322 323 for (i = 0; i < BTA_GATTC_KNOWN_SR_MAX; i ++, p_srcb ++) 324 { 325 if (p_srcb->in_use && bdcmp(p_srcb->server_bda, bda) == 0) 326 return p_srcb; 327 } 328 return NULL; 329 } 330 331 /******************************************************************************* 332 ** 333 ** Function bta_gattc_find_srvr_cache 334 ** 335 ** Description find server cache by remote bd address 336 ** 337 ** Returns pointer to the server cache. 338 ** 339 *******************************************************************************/ 340 tBTA_GATTC_SERV * bta_gattc_find_srvr_cache(BD_ADDR bda) 341 { 342 tBTA_GATTC_SERV *p_srcb = &bta_gattc_cb.known_server[0]; 343 UINT8 i; 344 345 for (i = 0; i < BTA_GATTC_KNOWN_SR_MAX; i ++, p_srcb ++) 346 { 347 if (bdcmp(p_srcb->server_bda, bda) == 0) 348 return p_srcb; 349 } 350 return NULL; 351 } 352 /******************************************************************************* 353 ** 354 ** Function bta_gattc_find_scb_by_cid 355 ** 356 ** Description find server control block by connection ID 357 ** 358 ** Returns pointer to the server cache. 359 ** 360 *******************************************************************************/ 361 tBTA_GATTC_SERV * bta_gattc_find_scb_by_cid (UINT16 conn_id) 362 { 363 tBTA_GATTC_CLCB *p_clcb = bta_gattc_find_clcb_by_conn_id(conn_id); 364 365 if (p_clcb) 366 return p_clcb->p_srcb; 367 else 368 return NULL; 369 } 370 /******************************************************************************* 371 ** 372 ** Function bta_gattc_srcb_alloc 373 ** 374 ** Description allocate server cache control block 375 ** 376 ** Returns pointer to the server cache. 377 ** 378 *******************************************************************************/ 379 tBTA_GATTC_SERV * bta_gattc_srcb_alloc(BD_ADDR bda) 380 { 381 tBTA_GATTC_SERV *p_tcb = &bta_gattc_cb.known_server[0], 382 *p_recycle = NULL; 383 BOOLEAN found = FALSE; 384 UINT8 i; 385 386 for (i = 0; i < BTA_GATTC_KNOWN_SR_MAX; i ++, p_tcb ++) 387 { 388 if (!p_tcb->in_use) 389 { 390 found = TRUE; 391 break; 392 } 393 else if (!p_tcb->connected) 394 { 395 p_recycle = p_tcb; 396 } 397 } 398 399 /* if not found, try to recycle one known device */ 400 if (!found && !p_recycle) 401 p_tcb = NULL; 402 else if (!found && p_recycle) 403 p_tcb = p_recycle; 404 405 if (p_tcb != NULL) 406 { 407 while (p_tcb->cache_buffer.p_first) 408 GKI_freebuf (GKI_dequeue (&p_tcb->cache_buffer)); 409 410 utl_freebuf((void **)&p_tcb->p_srvc_list); 411 memset(p_tcb, 0 , sizeof(tBTA_GATTC_SERV)); 412 413 p_tcb->in_use = TRUE; 414 bdcpy(p_tcb->server_bda, bda); 415 } 416 return p_tcb; 417 } 418 /******************************************************************************* 419 ** 420 ** Function bta_gattc_enqueue 421 ** 422 ** Description enqueue a client request in clcb. 423 ** 424 ** Returns success or failure. 425 ** 426 *******************************************************************************/ 427 BOOLEAN bta_gattc_enqueue(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data) 428 { 429 BOOLEAN in_q = FALSE; 430 431 if (p_clcb->p_q_cmd == NULL) 432 { 433 p_clcb->p_q_cmd = (tBTA_GATTC_DATA *)GKI_getbuf(sizeof(tBTA_GATTC_DATA)); 434 435 if (p_data) 436 memcpy(p_clcb->p_q_cmd, p_data, sizeof(tBTA_GATTC_DATA)); 437 438 in_q = TRUE; 439 } 440 else 441 { 442 APPL_TRACE_ERROR0("already has a pending command!!"); 443 /* skip the callback now. ----- need to send callback ? */ 444 } 445 return in_q; 446 } 447 /******************************************************************************* 448 ** 449 ** Function bta_gattc_pack_attr_uuid 450 ** 451 ** Description pack UUID into a stream. 452 ** 453 ** Returns 454 ** 455 *******************************************************************************/ 456 void bta_gattc_pack_attr_uuid(tBTA_GATTC_CACHE_ATTR *p_attr, tBT_UUID *p_uuid) 457 { 458 UINT8 *pp = (UINT8 *)p_attr->p_uuid; 459 460 memset(p_uuid, 0, sizeof(tBT_UUID)); 461 462 p_uuid->len = p_attr->uuid_len; 463 464 if (p_attr->uuid_len == LEN_UUID_16) 465 { 466 STREAM_TO_UINT16(p_uuid->uu.uuid16, pp); 467 } 468 else 469 { 470 memcpy(p_uuid->uu.uuid128, pp, LEN_UUID_128); 471 } 472 473 return; 474 } 475 /******************************************************************************* 476 ** 477 ** Function bta_gattc_cpygattid 478 ** 479 ** Description copy two tBTA_GATT_ID value 480 ** 481 ** Returns 482 ** 483 *******************************************************************************/ 484 void bta_gattc_cpygattid(tBTA_GATT_ID *p_des, tBTA_GATT_ID *p_src) 485 { 486 memset ((void *)p_des, 0, sizeof(tBTA_GATT_ID)); 487 488 p_des->inst_id = p_src->inst_id; 489 490 p_des->uuid.len = p_src->uuid.len; 491 492 if (p_des->uuid.len == LEN_UUID_16) 493 { 494 p_des->uuid.uu.uuid16 = p_src->uuid.uu.uuid16; 495 } 496 else if (p_des->uuid.len == LEN_UUID_128) 497 { 498 memcpy(p_des->uuid.uu.uuid128, p_src->uuid.uu.uuid128, LEN_UUID_128); 499 } 500 } 501 /******************************************************************************* 502 ** 503 ** Function bta_gattc_gattid_compare 504 ** 505 ** Description compare two tBTA_GATT_ID type of pointer 506 ** 507 ** Returns 508 ** 509 *******************************************************************************/ 510 BOOLEAN bta_gattc_gattid_compare(tBTA_GATT_ID *p_src, tBTA_GATT_ID *p_tar) 511 { 512 if (p_src->inst_id == p_tar->inst_id && 513 bta_gattc_uuid_compare (&p_src->uuid, &p_tar->uuid, TRUE )) 514 return TRUE; 515 else 516 return FALSE; 517 518 } 519 /******************************************************************************* 520 ** 521 ** Function bta_gattc_srvcid_compare 522 ** 523 ** Description compare two tBTA_GATT_SRVC_ID type of pointer 524 ** 525 ** Returns 526 ** 527 *******************************************************************************/ 528 BOOLEAN bta_gattc_srvcid_compare(tBTA_GATT_SRVC_ID *p_src, tBTA_GATT_SRVC_ID *p_tar) 529 { 530 if (p_src->is_primary == p_tar->is_primary && 531 bta_gattc_gattid_compare (&p_src->id, &p_tar->id)) 532 return TRUE; 533 else 534 return FALSE; 535 } 536 /******************************************************************************* 537 ** 538 ** Function bta_gattc_charid_compare 539 ** 540 ** Description compare two tBTA_GATTC_CHAR_ID type of pointer 541 ** 542 ** Returns 543 ** 544 *******************************************************************************/ 545 BOOLEAN bta_gattc_charid_compare(tBTA_GATTC_CHAR_ID *p_src, tBTA_GATTC_CHAR_ID *p_tar) 546 { 547 if (bta_gattc_gattid_compare (&p_src->char_id, &p_tar->char_id) && 548 bta_gattc_srvcid_compare (&p_src->srvc_id, &p_tar->srvc_id)) 549 return TRUE; 550 else 551 return FALSE; 552 } 553 554 /******************************************************************************* 555 ** 556 ** Function bta_gattc_check_notif_registry 557 ** 558 ** Description check if the service notificaition has been registered. 559 ** 560 ** Returns 561 ** 562 *******************************************************************************/ 563 BOOLEAN bta_gattc_check_notif_registry(tBTA_GATTC_RCB *p_clreg, tBTA_GATTC_SERV *p_srcb, 564 tBTA_GATTC_NOTIFY *p_notify) 565 { 566 UINT8 i; 567 568 for (i = 0 ; i < BTA_GATTC_NOTIF_REG_MAX; i ++) 569 { 570 if (p_clreg->notif_reg[i].in_use && 571 bdcmp(p_clreg->notif_reg[i].remote_bda, p_srcb->server_bda) == 0 && 572 bta_gattc_charid_compare (&p_clreg->notif_reg[i].char_id, &p_notify->char_id)) 573 { 574 APPL_TRACE_DEBUG0("Notification registered!"); 575 return TRUE; 576 } 577 } 578 return FALSE; 579 580 } 581 /******************************************************************************* 582 ** 583 ** Function bta_gattc_clear_notif_registration 584 ** 585 ** Description clear up the notification registration information by BD_ADDR. 586 ** 587 ** Returns None. 588 ** 589 *******************************************************************************/ 590 void bta_gattc_clear_notif_registration(UINT16 conn_id) 591 { 592 BD_ADDR remote_bda; 593 tBTA_GATTC_IF gatt_if; 594 tBTA_GATTC_RCB *p_clrcb ; 595 UINT8 i; 596 597 if (GATT_GetConnectionInfor(conn_id, &gatt_if, remote_bda)) 598 { 599 if ((p_clrcb = bta_gattc_cl_get_regcb(gatt_if)) != NULL) 600 { 601 for (i = 0 ; i < BTA_GATTC_NOTIF_REG_MAX; i ++) 602 { 603 if (p_clrcb->notif_reg[i].in_use && !bdcmp(p_clrcb->notif_reg[i].remote_bda, remote_bda)) 604 memset(&p_clrcb->notif_reg[i], 0, sizeof(tBTA_GATTC_NOTIF_REG)); 605 } 606 } 607 } 608 else 609 { 610 APPL_TRACE_ERROR0("can not clear indication/notif registration for unknown app"); 611 } 612 return; 613 } 614 615 /******************************************************************************* 616 ** 617 ** Function bta_gattc_pack_cb_data 618 ** 619 ** Description pack the data from read response into callback data structure. 620 ** 621 ** Returns 622 ** 623 *******************************************************************************/ 624 tBTA_GATT_STATUS bta_gattc_pack_read_cb_data(tBTA_GATTC_SERV *p_srcb, 625 tBT_UUID *p_descr_uuid, 626 tGATT_VALUE *p_attr, 627 tBTA_GATT_READ_VAL *p_value) 628 { 629 UINT8 i = 0, *pp = p_attr->value; 630 tBT_UUID uuid = {LEN_UUID_16, {GATT_UUID_CHAR_AGG_FORMAT}}; 631 UINT16 handle; 632 tBTA_GATT_STATUS status = BTA_GATT_OK; 633 634 /* GATT_UUID_CHAR_AGG_FORMAT */ 635 if (bta_gattc_uuid_compare (&uuid, p_descr_uuid, TRUE)) 636 { 637 while (p_attr->len >= 2 && i < BTA_GATTC_MULTI_MAX) 638 { 639 STREAM_TO_UINT16(handle, pp); 640 641 if (bta_gattc_handle2id(p_srcb, 642 handle, 643 &p_value->aggre_value.pre_format[i].char_id.srvc_id, 644 &p_value->aggre_value.pre_format[i].char_id.char_id, 645 &p_value->aggre_value.pre_format[i].descr_id) == FALSE) 646 { 647 status = BTA_GATT_INTERNAL_ERROR; 648 APPL_TRACE_ERROR1("can not map to GATT ID. handle = 0x%04x", handle); 649 break; 650 } 651 i ++; 652 p_attr->len -= 2; 653 } 654 p_value->aggre_value.num_pres_fmt = i; 655 } 656 else 657 { 658 /* all others, take as raw format */ 659 p_value->unformat.len = p_attr->len; 660 p_value->unformat.p_value = p_attr->value; 661 } 662 return status; 663 } 664 /******************************************************************************* 665 ** 666 ** Function bta_gattc_mark_bg_conn 667 ** 668 ** Description mark background connection status when a bg connection is initiated 669 ** or terminated. 670 ** 671 ** Returns TRUE if success; FALSE otherwise. 672 ** 673 *******************************************************************************/ 674 BOOLEAN bta_gattc_mark_bg_conn (tBTA_GATTC_IF client_if, BD_ADDR_PTR remote_bda_ptr, 675 BOOLEAN add, BOOLEAN is_listen) 676 { 677 tBTA_GATTC_BG_TCK *p_bg_tck = &bta_gattc_cb.bg_track[0]; 678 UINT8 i = 0; 679 tBTA_GATTC_CIF_MASK *p_cif_mask; 680 681 for (i = 0; i < BTA_GATTC_KNOWN_SR_MAX; i ++, p_bg_tck ++) 682 { 683 if (p_bg_tck->in_use && 684 ((remote_bda_ptr != NULL && bdcmp(p_bg_tck->remote_bda, remote_bda_ptr) == 0) || 685 (remote_bda_ptr == NULL && bdcmp(p_bg_tck->remote_bda, dummy_bda) == 0))) 686 { 687 p_cif_mask = is_listen ? &p_bg_tck->cif_adv_mask : &p_bg_tck->cif_mask; 688 689 if (add) 690 /* mask on the cif bit */ 691 *p_cif_mask |= (1 <<(client_if - 1)); 692 else 693 { 694 if (client_if != 0) 695 *p_cif_mask &= (~(1 <<(client_if - 1))); 696 else 697 *p_cif_mask = 0; 698 } 699 /* no BG connection for this device, make it available */ 700 if (p_bg_tck->cif_mask == 0 && p_bg_tck->cif_adv_mask == 0) 701 { 702 memset(p_bg_tck, 0, sizeof(tBTA_GATTC_BG_TCK)); 703 } 704 return TRUE; 705 } 706 } 707 if (!add) 708 { 709 APPL_TRACE_ERROR0("Do not find the bg connection mask for the remote device"); 710 return FALSE; 711 } 712 else /* adding a new device mask */ 713 { 714 for (i = 0, p_bg_tck = &bta_gattc_cb.bg_track[0]; 715 i < BTA_GATTC_KNOWN_SR_MAX; i ++, p_bg_tck ++) 716 { 717 if (!p_bg_tck->in_use) 718 { 719 p_bg_tck->in_use = TRUE; 720 if (remote_bda_ptr) 721 bdcpy(p_bg_tck->remote_bda, remote_bda_ptr); 722 else 723 bdcpy(p_bg_tck->remote_bda, dummy_bda); 724 725 p_cif_mask = is_listen ? &p_bg_tck->cif_adv_mask : &p_bg_tck->cif_mask; 726 727 *p_cif_mask = (1 <<(client_if - 1)); 728 return TRUE; 729 } 730 } 731 APPL_TRACE_ERROR0("no available space to mark the bg connection status"); 732 return FALSE; 733 } 734 } 735 /******************************************************************************* 736 ** 737 ** Function bta_gattc_check_bg_conn 738 ** 739 ** Description check if this is a background connection background connection. 740 ** 741 ** Returns TRUE if success; FALSE otherwise. 742 ** 743 *******************************************************************************/ 744 BOOLEAN bta_gattc_check_bg_conn (tBTA_GATTC_IF client_if, BD_ADDR remote_bda, UINT8 role) 745 { 746 tBTA_GATTC_BG_TCK *p_bg_tck = &bta_gattc_cb.bg_track[0]; 747 UINT8 i = 0; 748 BOOLEAN is_bg_conn = FALSE; 749 750 for (i = 0; i < BTA_GATTC_KNOWN_SR_MAX && !is_bg_conn; i ++, p_bg_tck ++) 751 { 752 if (p_bg_tck->in_use && 753 (bdcmp(p_bg_tck->remote_bda, remote_bda) == 0 || 754 bdcmp(p_bg_tck->remote_bda, dummy_bda) == 0)) 755 { 756 if (((p_bg_tck->cif_mask &(1 <<(client_if - 1))) != 0) && 757 role == HCI_ROLE_MASTER) 758 is_bg_conn = TRUE; 759 760 if (((p_bg_tck->cif_adv_mask &(1 <<(client_if - 1))) != 0) && 761 role == HCI_ROLE_SLAVE) 762 is_bg_conn = TRUE; 763 } 764 } 765 return is_bg_conn; 766 } 767 /******************************************************************************* 768 ** 769 ** Function bta_gattc_send_open_cback 770 ** 771 ** Description send open callback 772 ** 773 ** Returns 774 ** 775 *******************************************************************************/ 776 void bta_gattc_send_open_cback( tBTA_GATTC_RCB *p_clreg, tBTA_GATT_STATUS status, 777 BD_ADDR remote_bda, UINT16 conn_id) 778 { 779 tBTA_GATTC cb_data; 780 781 if (p_clreg->p_cback) 782 { 783 memset(&cb_data, 0, sizeof(tBTA_GATTC)); 784 785 cb_data.open.status = status; 786 cb_data.open.client_if = p_clreg->client_if; 787 cb_data.open.conn_id = conn_id; 788 bdcpy(cb_data.open.remote_bda, remote_bda); 789 790 (*p_clreg->p_cback)(BTA_GATTC_OPEN_EVT, &cb_data); 791 } 792 } 793 /******************************************************************************* 794 ** 795 ** Function bta_gattc_conn_alloc 796 ** 797 ** Description allocate connection tracking spot 798 ** 799 ** Returns pointer to the clcb 800 ** 801 *******************************************************************************/ 802 tBTA_GATTC_CONN * bta_gattc_conn_alloc(BD_ADDR remote_bda) 803 { 804 UINT8 i_conn = 0; 805 tBTA_GATTC_CONN *p_conn = &bta_gattc_cb.conn_track[0]; 806 807 for (i_conn = 0; i_conn < BTA_GATTC_CONN_MAX; i_conn++, p_conn ++) 808 { 809 if (!p_conn->in_use) 810 { 811 #if BTA_GATT_DEBUG == TRUE 812 APPL_TRACE_DEBUG1("bta_gattc_conn_alloc: found conn_track[%d] available",i_conn); 813 #endif 814 p_conn->in_use = TRUE; 815 bdcpy(p_conn->remote_bda, remote_bda); 816 return p_conn; 817 } 818 } 819 return NULL; 820 } 821 822 /******************************************************************************* 823 ** 824 ** Function bta_gattc_conn_find 825 ** 826 ** Description allocate connection tracking spot 827 ** 828 ** Returns pointer to the clcb 829 ** 830 *******************************************************************************/ 831 tBTA_GATTC_CONN * bta_gattc_conn_find(BD_ADDR remote_bda) 832 { 833 UINT8 i_conn = 0; 834 tBTA_GATTC_CONN *p_conn = &bta_gattc_cb.conn_track[0]; 835 836 for (i_conn = 0; i_conn < BTA_GATTC_CONN_MAX; i_conn++, p_conn ++) 837 { 838 if (p_conn->in_use && bdcmp(remote_bda, p_conn->remote_bda) == 0) 839 { 840 #if BTA_GATT_DEBUG == TRUE 841 APPL_TRACE_DEBUG1("bta_gattc_conn_find: found conn_track[%d] matched",i_conn); 842 #endif 843 return p_conn; 844 } 845 } 846 return NULL; 847 } 848 849 850 /******************************************************************************* 851 ** 852 ** Function bta_gattc_conn_find_alloc 853 ** 854 ** Description find or allocate connection tracking spot 855 ** 856 ** Returns pointer to the clcb 857 ** 858 *******************************************************************************/ 859 tBTA_GATTC_CONN * bta_gattc_conn_find_alloc(BD_ADDR remote_bda) 860 { 861 tBTA_GATTC_CONN *p_conn = bta_gattc_conn_find (remote_bda); 862 863 if (p_conn == NULL) 864 { 865 p_conn = bta_gattc_conn_alloc(remote_bda); 866 } 867 return p_conn; 868 } 869 870 /******************************************************************************* 871 ** 872 ** Function bta_gattc_conn_dealloc 873 ** 874 ** Description de-allocate connection tracking spot 875 ** 876 ** Returns pointer to the clcb 877 ** 878 *******************************************************************************/ 879 BOOLEAN bta_gattc_conn_dealloc(BD_ADDR remote_bda) 880 { 881 tBTA_GATTC_CONN *p_conn = bta_gattc_conn_find (remote_bda); 882 883 if (p_conn != NULL) 884 { 885 p_conn->in_use = FALSE; 886 memset(p_conn->remote_bda, 0, BD_ADDR_LEN); 887 return TRUE; 888 } 889 return FALSE; 890 } 891 892 /******************************************************************************* 893 ** 894 ** Function bta_gattc_find_int_conn_clcb 895 ** 896 ** Description try to locate a clcb when an internal connecion event arrives. 897 ** 898 ** Returns pointer to the clcb 899 ** 900 *******************************************************************************/ 901 tBTA_GATTC_CLCB * bta_gattc_find_int_conn_clcb(tBTA_GATTC_DATA *p_msg) 902 { 903 tBTA_GATTC_CLCB *p_clcb = NULL; 904 905 if (p_msg->int_conn.role == HCI_ROLE_SLAVE) 906 bta_gattc_conn_find_alloc(p_msg->int_conn.remote_bda); 907 908 /* try to locate a logic channel */ 909 if ((p_clcb = bta_gattc_find_clcb_by_cif(p_msg->int_conn.client_if, 910 p_msg->int_conn.remote_bda)) == NULL) 911 { 912 /* for a background connection or listening connection */ 913 if (p_msg->int_conn.role == HCI_ROLE_SLAVE || 914 bta_gattc_check_bg_conn(p_msg->int_conn.client_if, 915 p_msg->int_conn.remote_bda, 916 p_msg->int_conn.role)) 917 { 918 /* allocate a new channel */ 919 p_clcb = bta_gattc_clcb_alloc(p_msg->int_conn.client_if, p_msg->int_conn.remote_bda); 920 } 921 } 922 return p_clcb; 923 } 924 925 /******************************************************************************* 926 ** 927 ** Function bta_gattc_find_int_disconn_clcb 928 ** 929 ** Description try to locate a clcb when an internal disconnect callback arrives. 930 ** 931 ** Returns pointer to the clcb 932 ** 933 *******************************************************************************/ 934 tBTA_GATTC_CLCB * bta_gattc_find_int_disconn_clcb(tBTA_GATTC_DATA *p_msg) 935 { 936 tBTA_GATTC_CLCB *p_clcb = NULL; 937 tGATT_DISCONN_REASON reason = p_msg->int_conn.reason; 938 939 bta_gattc_conn_dealloc(p_msg->int_conn.remote_bda); 940 /* connection attempt timeout, send connection callback event */ 941 if (reason == GATT_CONN_CANCEL || reason == GATT_CONN_L2C_FAILURE) 942 { 943 p_clcb = bta_gattc_find_clcb_by_cif(p_msg->int_conn.client_if, 944 p_msg->int_conn.remote_bda); 945 } 946 else if ((p_clcb = bta_gattc_find_clcb_by_conn_id(p_msg->int_conn.hdr.layer_specific)) == NULL) 947 { 948 APPL_TRACE_DEBUG1("disconnection ID: [%d] not used by BTA", 949 p_msg->int_conn.hdr.layer_specific); 950 } 951 return p_clcb; 952 } 953 954 #endif /* BTA_GATT_INCLUDED */ 955