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 && p_data) 432 { 433 UINT16 len; 434 switch (p_data->hdr.event) 435 { 436 case BTA_GATTC_API_SEARCH_EVT: 437 { 438 if (p_data->api_search.p_srvc_uuid) 439 { 440 len = sizeof(tBTA_GATTC_API_SEARCH) + sizeof(tBT_UUID); 441 } 442 else 443 { 444 len = sizeof(tBTA_GATTC_API_SEARCH); 445 } 446 p_clcb->p_q_cmd = (tBTA_GATTC_DATA *)GKI_getbuf(len); 447 if (p_clcb->p_q_cmd == NULL) 448 { 449 APPL_TRACE_ERROR0("allocate buffer failed for p_q_cmd"); 450 return FALSE; 451 } 452 memcpy(p_clcb->p_q_cmd, p_data, sizeof(tBTA_GATTC_API_SEARCH)); 453 if (p_data->api_search.p_srvc_uuid) 454 { 455 tBTA_GATTC_API_SEARCH *p_buf; 456 p_buf = &(p_clcb->p_q_cmd->api_search); 457 p_buf->p_srvc_uuid = (tBT_UUID *)(p_buf + 1); 458 memcpy(p_buf->p_srvc_uuid, p_data->api_search.p_srvc_uuid, 459 sizeof(tBT_UUID)); 460 } 461 break; 462 } 463 case BTA_GATTC_API_READ_EVT: 464 { 465 if (p_data->api_read.p_descr_type) 466 { 467 len = sizeof(tBTA_GATT_ID) + sizeof(tBTA_GATTC_API_READ); 468 } 469 else 470 { 471 len = sizeof(tBTA_GATTC_API_READ); 472 } 473 p_clcb->p_q_cmd = (tBTA_GATTC_DATA *)GKI_getbuf(len); 474 if (p_clcb->p_q_cmd == NULL) 475 { 476 APPL_TRACE_ERROR0("allocate buffer failed for p_q_cmd"); 477 return FALSE; 478 } 479 memcpy(p_clcb->p_q_cmd, p_data, sizeof(tBTA_GATTC_API_READ)); 480 if (p_data->api_read.p_descr_type) 481 { 482 tBTA_GATTC_API_READ *p_buf; 483 p_buf = &(p_clcb->p_q_cmd->api_read); 484 p_buf->p_descr_type = (tBTA_GATT_ID *)(p_buf + 1); 485 memcpy(p_buf->p_descr_type, p_data->api_read.p_descr_type, 486 sizeof(tBTA_GATT_ID)); 487 } 488 break; 489 } 490 case BTA_GATTC_API_WRITE_EVT: 491 { 492 tBTA_GATTC_API_WRITE *p_buf; 493 len = sizeof(tBTA_GATTC_API_WRITE) + p_data->api_write.len; 494 if (p_data->api_write.p_descr_type) 495 { 496 len += sizeof(tBTA_GATT_ID); 497 } 498 p_clcb->p_q_cmd = (tBTA_GATTC_DATA *)GKI_getbuf(len); 499 if (p_clcb->p_q_cmd == NULL) 500 { 501 APPL_TRACE_ERROR0("allocate buffer failed for p_q_cmd"); 502 return FALSE; 503 } 504 memcpy(p_clcb->p_q_cmd, p_data, sizeof(tBTA_GATTC_API_WRITE)); 505 p_buf = &(p_clcb->p_q_cmd->api_write); 506 if (p_data->api_write.p_descr_type) 507 { 508 p_buf->p_descr_type = (tBTA_GATT_ID *)(p_buf + 1); 509 memcpy(p_buf->p_descr_type, p_data->api_write.p_descr_type, 510 sizeof(tBTA_GATT_ID)); 511 if (p_buf->len && p_buf->p_value) 512 { 513 p_buf->p_value = (UINT8 *)(p_buf->p_descr_type + 1); 514 memcpy(p_buf->p_value, p_data->api_write.p_value, 515 p_data->api_write.len); 516 } 517 } 518 else if (p_buf->len && p_buf->p_value) 519 { 520 p_buf->p_value = (UINT8 *)(p_buf + 1); 521 memcpy(p_buf->p_value, p_data->api_write.p_value, 522 p_data->api_write.len); 523 } 524 break; 525 } 526 case BTA_GATTC_API_EXEC_EVT: 527 { 528 len = sizeof(tBTA_GATTC_API_EXEC); 529 p_clcb->p_q_cmd = (tBTA_GATTC_DATA *)GKI_getbuf(len); 530 if (p_clcb->p_q_cmd == NULL) 531 { 532 APPL_TRACE_ERROR0("allocate buffer failed for p_q_cmd"); 533 return FALSE; 534 } 535 memcpy(p_clcb->p_q_cmd, p_data, len); 536 break; 537 } 538 case BTA_GATTC_API_READ_MULTI_EVT: 539 { 540 len = sizeof(tBTA_GATTC_API_READ_MULTI) + 541 p_data->api_read_multi.num_attr * sizeof(tBTA_GATTC_ATTR_ID); 542 p_clcb->p_q_cmd = (tBTA_GATTC_DATA *)GKI_getbuf(len); 543 if (p_clcb->p_q_cmd == NULL) 544 { 545 APPL_TRACE_ERROR0("allocate buffer failed for p_q_cmd"); 546 return FALSE; 547 } 548 memcpy(p_clcb->p_q_cmd, p_data, sizeof(tBTA_GATTC_API_READ_MULTI)); 549 if (p_data->api_read_multi.num_attr && 550 p_data->api_read_multi.p_id_list) 551 { 552 tBTA_GATTC_API_READ_MULTI *p_buf; 553 p_buf = &(p_clcb->p_q_cmd->api_read_multi); 554 p_buf->p_id_list = (tBTA_GATTC_ATTR_ID *)(p_buf + 1); 555 memcpy(p_buf->p_id_list, p_data->api_read_multi.p_id_list, 556 p_data->api_read_multi.num_attr * sizeof(tBTA_GATTC_ATTR_ID)); 557 } 558 break; 559 } 560 default: 561 APPL_TRACE_ERROR1("queue unsupported command %d", p_data->hdr.event); 562 return FALSE; 563 } 564 565 in_q = TRUE; 566 } 567 else if (p_clcb->p_q_cmd) 568 { 569 APPL_TRACE_ERROR0("already has a pending command!!"); 570 /* skip the callback now. ----- need to send callback ? */ 571 } 572 else 573 { 574 APPL_TRACE_ERROR0("queue a null command"); 575 } 576 577 return in_q; 578 } 579 /******************************************************************************* 580 ** 581 ** Function bta_gattc_pack_attr_uuid 582 ** 583 ** Description pack UUID into a stream. 584 ** 585 ** Returns 586 ** 587 *******************************************************************************/ 588 void bta_gattc_pack_attr_uuid(tBTA_GATTC_CACHE_ATTR *p_attr, tBT_UUID *p_uuid) 589 { 590 UINT8 *pp = (UINT8 *)p_attr->p_uuid; 591 592 memset(p_uuid, 0, sizeof(tBT_UUID)); 593 594 p_uuid->len = p_attr->uuid_len; 595 596 if (p_attr->uuid_len == LEN_UUID_16) 597 { 598 STREAM_TO_UINT16(p_uuid->uu.uuid16, pp); 599 } 600 else 601 { 602 memcpy(p_uuid->uu.uuid128, pp, LEN_UUID_128); 603 } 604 605 return; 606 } 607 /******************************************************************************* 608 ** 609 ** Function bta_gattc_cpygattid 610 ** 611 ** Description copy two tBTA_GATT_ID value 612 ** 613 ** Returns 614 ** 615 *******************************************************************************/ 616 void bta_gattc_cpygattid(tBTA_GATT_ID *p_des, tBTA_GATT_ID *p_src) 617 { 618 memset ((void *)p_des, 0, sizeof(tBTA_GATT_ID)); 619 620 p_des->inst_id = p_src->inst_id; 621 622 p_des->uuid.len = p_src->uuid.len; 623 624 if (p_des->uuid.len == LEN_UUID_16) 625 { 626 p_des->uuid.uu.uuid16 = p_src->uuid.uu.uuid16; 627 } 628 else if (p_des->uuid.len == LEN_UUID_128) 629 { 630 memcpy(p_des->uuid.uu.uuid128, p_src->uuid.uu.uuid128, LEN_UUID_128); 631 } 632 } 633 /******************************************************************************* 634 ** 635 ** Function bta_gattc_gattid_compare 636 ** 637 ** Description compare two tBTA_GATT_ID type of pointer 638 ** 639 ** Returns 640 ** 641 *******************************************************************************/ 642 BOOLEAN bta_gattc_gattid_compare(tBTA_GATT_ID *p_src, tBTA_GATT_ID *p_tar) 643 { 644 if (p_src->inst_id == p_tar->inst_id && 645 bta_gattc_uuid_compare (&p_src->uuid, &p_tar->uuid, TRUE )) 646 return TRUE; 647 else 648 return FALSE; 649 650 } 651 /******************************************************************************* 652 ** 653 ** Function bta_gattc_srvcid_compare 654 ** 655 ** Description compare two tBTA_GATT_SRVC_ID type of pointer 656 ** 657 ** Returns 658 ** 659 *******************************************************************************/ 660 BOOLEAN bta_gattc_srvcid_compare(tBTA_GATT_SRVC_ID *p_src, tBTA_GATT_SRVC_ID *p_tar) 661 { 662 if (p_src->is_primary == p_tar->is_primary && 663 bta_gattc_gattid_compare (&p_src->id, &p_tar->id)) 664 return TRUE; 665 else 666 return FALSE; 667 } 668 /******************************************************************************* 669 ** 670 ** Function bta_gattc_charid_compare 671 ** 672 ** Description compare two tBTA_GATTC_CHAR_ID type of pointer 673 ** 674 ** Returns 675 ** 676 *******************************************************************************/ 677 BOOLEAN bta_gattc_charid_compare(tBTA_GATTC_CHAR_ID *p_src, tBTA_GATTC_CHAR_ID *p_tar) 678 { 679 if (bta_gattc_gattid_compare (&p_src->char_id, &p_tar->char_id) && 680 bta_gattc_srvcid_compare (&p_src->srvc_id, &p_tar->srvc_id)) 681 return TRUE; 682 else 683 return FALSE; 684 } 685 686 /******************************************************************************* 687 ** 688 ** Function bta_gattc_check_notif_registry 689 ** 690 ** Description check if the service notificaition has been registered. 691 ** 692 ** Returns 693 ** 694 *******************************************************************************/ 695 BOOLEAN bta_gattc_check_notif_registry(tBTA_GATTC_RCB *p_clreg, tBTA_GATTC_SERV *p_srcb, 696 tBTA_GATTC_NOTIFY *p_notify) 697 { 698 UINT8 i; 699 700 for (i = 0 ; i < BTA_GATTC_NOTIF_REG_MAX; i ++) 701 { 702 if (p_clreg->notif_reg[i].in_use && 703 bdcmp(p_clreg->notif_reg[i].remote_bda, p_srcb->server_bda) == 0 && 704 bta_gattc_charid_compare (&p_clreg->notif_reg[i].char_id, &p_notify->char_id)) 705 { 706 APPL_TRACE_DEBUG0("Notification registered!"); 707 return TRUE; 708 } 709 } 710 return FALSE; 711 712 } 713 /******************************************************************************* 714 ** 715 ** Function bta_gattc_clear_notif_registration 716 ** 717 ** Description clear up the notification registration information by BD_ADDR. 718 ** 719 ** Returns None. 720 ** 721 *******************************************************************************/ 722 void bta_gattc_clear_notif_registration(UINT16 conn_id) 723 { 724 BD_ADDR remote_bda; 725 tBTA_GATTC_IF gatt_if; 726 tBTA_GATTC_RCB *p_clrcb ; 727 UINT8 i; 728 729 if (GATT_GetConnectionInfor(conn_id, &gatt_if, remote_bda)) 730 { 731 if ((p_clrcb = bta_gattc_cl_get_regcb(gatt_if)) != NULL) 732 { 733 for (i = 0 ; i < BTA_GATTC_NOTIF_REG_MAX; i ++) 734 { 735 if (p_clrcb->notif_reg[i].in_use && !bdcmp(p_clrcb->notif_reg[i].remote_bda, remote_bda)) 736 memset(&p_clrcb->notif_reg[i], 0, sizeof(tBTA_GATTC_NOTIF_REG)); 737 } 738 } 739 } 740 else 741 { 742 APPL_TRACE_ERROR0("can not clear indication/notif registration for unknown app"); 743 } 744 return; 745 } 746 747 /******************************************************************************* 748 ** 749 ** Function bta_gattc_pack_cb_data 750 ** 751 ** Description pack the data from read response into callback data structure. 752 ** 753 ** Returns 754 ** 755 *******************************************************************************/ 756 tBTA_GATT_STATUS bta_gattc_pack_read_cb_data(tBTA_GATTC_SERV *p_srcb, 757 tBT_UUID *p_descr_uuid, 758 tGATT_VALUE *p_attr, 759 tBTA_GATT_READ_VAL *p_value) 760 { 761 UINT8 i = 0, *pp = p_attr->value; 762 tBT_UUID uuid = {LEN_UUID_16, {GATT_UUID_CHAR_AGG_FORMAT}}; 763 UINT16 handle; 764 tBTA_GATT_STATUS status = BTA_GATT_OK; 765 766 /* GATT_UUID_CHAR_AGG_FORMAT */ 767 if (bta_gattc_uuid_compare (&uuid, p_descr_uuid, TRUE)) 768 { 769 while (p_attr->len >= 2 && i < BTA_GATTC_MULTI_MAX) 770 { 771 STREAM_TO_UINT16(handle, pp); 772 773 if (bta_gattc_handle2id(p_srcb, 774 handle, 775 &p_value->aggre_value.pre_format[i].char_id.srvc_id, 776 &p_value->aggre_value.pre_format[i].char_id.char_id, 777 &p_value->aggre_value.pre_format[i].descr_id) == FALSE) 778 { 779 status = BTA_GATT_INTERNAL_ERROR; 780 APPL_TRACE_ERROR1("can not map to GATT ID. handle = 0x%04x", handle); 781 break; 782 } 783 i ++; 784 p_attr->len -= 2; 785 } 786 p_value->aggre_value.num_pres_fmt = i; 787 } 788 else 789 { 790 /* all others, take as raw format */ 791 p_value->unformat.len = p_attr->len; 792 p_value->unformat.p_value = p_attr->value; 793 } 794 return status; 795 } 796 /******************************************************************************* 797 ** 798 ** Function bta_gattc_mark_bg_conn 799 ** 800 ** Description mark background connection status when a bg connection is initiated 801 ** or terminated. 802 ** 803 ** Returns TRUE if success; FALSE otherwise. 804 ** 805 *******************************************************************************/ 806 BOOLEAN bta_gattc_mark_bg_conn (tBTA_GATTC_IF client_if, BD_ADDR_PTR remote_bda_ptr, 807 BOOLEAN add, BOOLEAN is_listen) 808 { 809 tBTA_GATTC_BG_TCK *p_bg_tck = &bta_gattc_cb.bg_track[0]; 810 UINT8 i = 0; 811 tBTA_GATTC_CIF_MASK *p_cif_mask; 812 813 for (i = 0; i < BTA_GATTC_KNOWN_SR_MAX; i ++, p_bg_tck ++) 814 { 815 if (p_bg_tck->in_use && 816 ((remote_bda_ptr != NULL && bdcmp(p_bg_tck->remote_bda, remote_bda_ptr) == 0) || 817 (remote_bda_ptr == NULL && bdcmp(p_bg_tck->remote_bda, dummy_bda) == 0))) 818 { 819 p_cif_mask = is_listen ? &p_bg_tck->cif_adv_mask : &p_bg_tck->cif_mask; 820 821 if (add) 822 /* mask on the cif bit */ 823 *p_cif_mask |= (1 <<(client_if - 1)); 824 else 825 { 826 if (client_if != 0) 827 *p_cif_mask &= (~(1 <<(client_if - 1))); 828 else 829 *p_cif_mask = 0; 830 } 831 /* no BG connection for this device, make it available */ 832 if (p_bg_tck->cif_mask == 0 && p_bg_tck->cif_adv_mask == 0) 833 { 834 memset(p_bg_tck, 0, sizeof(tBTA_GATTC_BG_TCK)); 835 } 836 return TRUE; 837 } 838 } 839 if (!add) 840 { 841 APPL_TRACE_ERROR0("Do not find the bg connection mask for the remote device"); 842 return FALSE; 843 } 844 else /* adding a new device mask */ 845 { 846 for (i = 0, p_bg_tck = &bta_gattc_cb.bg_track[0]; 847 i < BTA_GATTC_KNOWN_SR_MAX; i ++, p_bg_tck ++) 848 { 849 if (!p_bg_tck->in_use) 850 { 851 p_bg_tck->in_use = TRUE; 852 if (remote_bda_ptr) 853 bdcpy(p_bg_tck->remote_bda, remote_bda_ptr); 854 else 855 bdcpy(p_bg_tck->remote_bda, dummy_bda); 856 857 p_cif_mask = is_listen ? &p_bg_tck->cif_adv_mask : &p_bg_tck->cif_mask; 858 859 *p_cif_mask = (1 <<(client_if - 1)); 860 return TRUE; 861 } 862 } 863 APPL_TRACE_ERROR0("no available space to mark the bg connection status"); 864 return FALSE; 865 } 866 } 867 /******************************************************************************* 868 ** 869 ** Function bta_gattc_check_bg_conn 870 ** 871 ** Description check if this is a background connection background connection. 872 ** 873 ** Returns TRUE if success; FALSE otherwise. 874 ** 875 *******************************************************************************/ 876 BOOLEAN bta_gattc_check_bg_conn (tBTA_GATTC_IF client_if, BD_ADDR remote_bda, UINT8 role) 877 { 878 tBTA_GATTC_BG_TCK *p_bg_tck = &bta_gattc_cb.bg_track[0]; 879 UINT8 i = 0; 880 BOOLEAN is_bg_conn = FALSE; 881 882 for (i = 0; i < BTA_GATTC_KNOWN_SR_MAX && !is_bg_conn; i ++, p_bg_tck ++) 883 { 884 if (p_bg_tck->in_use && 885 (bdcmp(p_bg_tck->remote_bda, remote_bda) == 0 || 886 bdcmp(p_bg_tck->remote_bda, dummy_bda) == 0)) 887 { 888 if (((p_bg_tck->cif_mask &(1 <<(client_if - 1))) != 0) && 889 role == HCI_ROLE_MASTER) 890 is_bg_conn = TRUE; 891 892 if (((p_bg_tck->cif_adv_mask &(1 <<(client_if - 1))) != 0) && 893 role == HCI_ROLE_SLAVE) 894 is_bg_conn = TRUE; 895 } 896 } 897 return is_bg_conn; 898 } 899 /******************************************************************************* 900 ** 901 ** Function bta_gattc_send_open_cback 902 ** 903 ** Description send open callback 904 ** 905 ** Returns 906 ** 907 *******************************************************************************/ 908 void bta_gattc_send_open_cback( tBTA_GATTC_RCB *p_clreg, tBTA_GATT_STATUS status, 909 BD_ADDR remote_bda, UINT16 conn_id) 910 { 911 tBTA_GATTC cb_data; 912 913 if (p_clreg->p_cback) 914 { 915 memset(&cb_data, 0, sizeof(tBTA_GATTC)); 916 917 cb_data.open.status = status; 918 cb_data.open.client_if = p_clreg->client_if; 919 cb_data.open.conn_id = conn_id; 920 bdcpy(cb_data.open.remote_bda, remote_bda); 921 922 (*p_clreg->p_cback)(BTA_GATTC_OPEN_EVT, &cb_data); 923 } 924 } 925 /******************************************************************************* 926 ** 927 ** Function bta_gattc_conn_alloc 928 ** 929 ** Description allocate connection tracking spot 930 ** 931 ** Returns pointer to the clcb 932 ** 933 *******************************************************************************/ 934 tBTA_GATTC_CONN * bta_gattc_conn_alloc(BD_ADDR remote_bda) 935 { 936 UINT8 i_conn = 0; 937 tBTA_GATTC_CONN *p_conn = &bta_gattc_cb.conn_track[0]; 938 939 for (i_conn = 0; i_conn < BTA_GATTC_CONN_MAX; i_conn++, p_conn ++) 940 { 941 if (!p_conn->in_use) 942 { 943 #if BTA_GATT_DEBUG == TRUE 944 APPL_TRACE_DEBUG1("bta_gattc_conn_alloc: found conn_track[%d] available",i_conn); 945 #endif 946 p_conn->in_use = TRUE; 947 bdcpy(p_conn->remote_bda, remote_bda); 948 return p_conn; 949 } 950 } 951 return NULL; 952 } 953 954 /******************************************************************************* 955 ** 956 ** Function bta_gattc_conn_find 957 ** 958 ** Description allocate connection tracking spot 959 ** 960 ** Returns pointer to the clcb 961 ** 962 *******************************************************************************/ 963 tBTA_GATTC_CONN * bta_gattc_conn_find(BD_ADDR remote_bda) 964 { 965 UINT8 i_conn = 0; 966 tBTA_GATTC_CONN *p_conn = &bta_gattc_cb.conn_track[0]; 967 968 for (i_conn = 0; i_conn < BTA_GATTC_CONN_MAX; i_conn++, p_conn ++) 969 { 970 if (p_conn->in_use && bdcmp(remote_bda, p_conn->remote_bda) == 0) 971 { 972 #if BTA_GATT_DEBUG == TRUE 973 APPL_TRACE_DEBUG1("bta_gattc_conn_find: found conn_track[%d] matched",i_conn); 974 #endif 975 return p_conn; 976 } 977 } 978 return NULL; 979 } 980 981 982 /******************************************************************************* 983 ** 984 ** Function bta_gattc_conn_find_alloc 985 ** 986 ** Description find or allocate connection tracking spot 987 ** 988 ** Returns pointer to the clcb 989 ** 990 *******************************************************************************/ 991 tBTA_GATTC_CONN * bta_gattc_conn_find_alloc(BD_ADDR remote_bda) 992 { 993 tBTA_GATTC_CONN *p_conn = bta_gattc_conn_find (remote_bda); 994 995 if (p_conn == NULL) 996 { 997 p_conn = bta_gattc_conn_alloc(remote_bda); 998 } 999 return p_conn; 1000 } 1001 1002 /******************************************************************************* 1003 ** 1004 ** Function bta_gattc_conn_dealloc 1005 ** 1006 ** Description de-allocate connection tracking spot 1007 ** 1008 ** Returns pointer to the clcb 1009 ** 1010 *******************************************************************************/ 1011 BOOLEAN bta_gattc_conn_dealloc(BD_ADDR remote_bda) 1012 { 1013 tBTA_GATTC_CONN *p_conn = bta_gattc_conn_find (remote_bda); 1014 1015 if (p_conn != NULL) 1016 { 1017 p_conn->in_use = FALSE; 1018 memset(p_conn->remote_bda, 0, BD_ADDR_LEN); 1019 return TRUE; 1020 } 1021 return FALSE; 1022 } 1023 1024 /******************************************************************************* 1025 ** 1026 ** Function bta_gattc_find_int_conn_clcb 1027 ** 1028 ** Description try to locate a clcb when an internal connecion event arrives. 1029 ** 1030 ** Returns pointer to the clcb 1031 ** 1032 *******************************************************************************/ 1033 tBTA_GATTC_CLCB * bta_gattc_find_int_conn_clcb(tBTA_GATTC_DATA *p_msg) 1034 { 1035 tBTA_GATTC_CLCB *p_clcb = NULL; 1036 1037 if (p_msg->int_conn.role == HCI_ROLE_SLAVE) 1038 bta_gattc_conn_find_alloc(p_msg->int_conn.remote_bda); 1039 1040 /* try to locate a logic channel */ 1041 if ((p_clcb = bta_gattc_find_clcb_by_cif(p_msg->int_conn.client_if, 1042 p_msg->int_conn.remote_bda)) == NULL) 1043 { 1044 /* for a background connection or listening connection */ 1045 if (p_msg->int_conn.role == HCI_ROLE_SLAVE || 1046 bta_gattc_check_bg_conn(p_msg->int_conn.client_if, 1047 p_msg->int_conn.remote_bda, 1048 p_msg->int_conn.role)) 1049 { 1050 /* allocate a new channel */ 1051 p_clcb = bta_gattc_clcb_alloc(p_msg->int_conn.client_if, p_msg->int_conn.remote_bda); 1052 } 1053 } 1054 return p_clcb; 1055 } 1056 1057 /******************************************************************************* 1058 ** 1059 ** Function bta_gattc_find_int_disconn_clcb 1060 ** 1061 ** Description try to locate a clcb when an internal disconnect callback arrives. 1062 ** 1063 ** Returns pointer to the clcb 1064 ** 1065 *******************************************************************************/ 1066 tBTA_GATTC_CLCB * bta_gattc_find_int_disconn_clcb(tBTA_GATTC_DATA *p_msg) 1067 { 1068 tBTA_GATTC_CLCB *p_clcb = NULL; 1069 tGATT_DISCONN_REASON reason = p_msg->int_conn.reason; 1070 1071 bta_gattc_conn_dealloc(p_msg->int_conn.remote_bda); 1072 /* connection attempt timeout, send connection callback event */ 1073 if (reason == GATT_CONN_CANCEL || reason == GATT_CONN_L2C_FAILURE) 1074 { 1075 p_clcb = bta_gattc_find_clcb_by_cif(p_msg->int_conn.client_if, 1076 p_msg->int_conn.remote_bda); 1077 } 1078 else if ((p_clcb = bta_gattc_find_clcb_by_conn_id(p_msg->int_conn.hdr.layer_specific)) == NULL) 1079 { 1080 APPL_TRACE_DEBUG1("disconnection ID: [%d] not used by BTA", 1081 p_msg->int_conn.hdr.layer_specific); 1082 } 1083 return p_clcb; 1084 } 1085 1086 #endif /* BTA_GATT_INCLUDED */ 1087