bta_gattc_cache.cc revision 2647452211f7beaee19610fe79442828ab2b0ee6
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 discovery procedures and cache 22 * related functions. 23 * 24 ******************************************************************************/ 25 26#define LOG_TAG "bt_bta_gattc" 27 28#include "bt_target.h" 29 30#include <errno.h> 31#include <stdio.h> 32#include <string.h> 33#include <unistd.h> 34 35#include "bt_common.h" 36#include "bta_gattc_int.h" 37#include "bta_sys.h" 38#include "btm_api.h" 39#include "btm_ble_api.h" 40#include "btm_int.h" 41#include "osi/include/log.h" 42#include "osi/include/osi.h" 43#include "sdp_api.h" 44#include "sdpdefs.h" 45#include "utl.h" 46 47using bluetooth::Uuid; 48using base::StringPrintf; 49 50static void bta_gattc_cache_write(const RawAddress& server_bda, 51 uint16_t num_attr, tBTA_GATTC_NV_ATTR* attr); 52static void bta_gattc_char_dscpt_disc_cmpl(uint16_t conn_id, 53 tBTA_GATTC_SERV* p_srvc_cb); 54static tGATT_STATUS bta_gattc_sdp_service_disc(uint16_t conn_id, 55 tBTA_GATTC_SERV* p_server_cb); 56tBTA_GATTC_SERVICE* bta_gattc_find_matching_service( 57 std::list<tBTA_GATTC_SERVICE>& services, uint16_t handle); 58const tBTA_GATTC_DESCRIPTOR* bta_gattc_get_descriptor_srcb( 59 tBTA_GATTC_SERV* p_srcb, uint16_t handle); 60tBTA_GATTC_CHARACTERISTIC* bta_gattc_get_characteristic_srcb( 61 tBTA_GATTC_SERV* p_srcb, uint16_t handle); 62 63#define BTA_GATT_SDP_DB_SIZE 4096 64 65#define GATT_CACHE_PREFIX "/data/misc/bluetooth/gatt_cache_" 66#define GATT_CACHE_VERSION 2 67 68static void bta_gattc_generate_cache_file_name(char* buffer, size_t buffer_len, 69 const RawAddress& bda) { 70 snprintf(buffer, buffer_len, "%s%02x%02x%02x%02x%02x%02x", GATT_CACHE_PREFIX, 71 bda.address[0], bda.address[1], bda.address[2], bda.address[3], 72 bda.address[4], bda.address[5]); 73} 74 75/***************************************************************************** 76 * Constants and data types 77 ****************************************************************************/ 78 79typedef struct { 80 tSDP_DISCOVERY_DB* p_sdp_db; 81 uint16_t sdp_conn_id; 82} tBTA_GATTC_CB_DATA; 83 84#if (BTA_GATT_DEBUG == TRUE) 85static const char* bta_gattc_attr_type[] = { 86 "I", /* Included Service */ 87 "C", /* Characteristic */ 88 "D" /* Characteristic Descriptor */ 89}; 90/* utility functions */ 91 92/* debug function to display the server cache */ 93static void bta_gattc_display_cache_server( 94 const std::list<tBTA_GATTC_SERVICE>& cache) { 95 LOG(ERROR) << "<================Start Server Cache =============>"; 96 97 for (const tBTA_GATTC_SERVICE& service : cache) { 98 LOG(ERROR) << "Service: s_handle=" << loghex(service.s_handle) 99 << ", e_handle=" << loghex(service.e_handle) 100 << ", inst=" << loghex(service.handle) 101 << ", uuid=" << service.uuid; 102 103 if (service.characteristics.empty()) { 104 LOG(ERROR) << "\t No characteristics"; 105 continue; 106 } 107 108 for (const tBTA_GATTC_CHARACTERISTIC& c : service.characteristics) { 109 LOG(ERROR) << "\t Characteristic handle=" << loghex(c.handle) 110 << ", uuid=" << c.uuid << ", prop=" << loghex(c.properties); 111 112 if (c.descriptors.empty()) { 113 LOG(ERROR) << "\t\t No descriptors"; 114 continue; 115 } 116 117 for (const tBTA_GATTC_DESCRIPTOR& d : c.descriptors) { 118 LOG(ERROR) << "\t\t Descriptor handle=" << loghex(d.handle) 119 << ", uuid=" : << d.uuid; 120 } 121 } 122 } 123 124 LOG(ERROR) << "<================End Server Cache =============>"; 125 LOG(ERROR) << " "; 126} 127 128/******************************************************************************* 129 * 130 * Function bta_gattc_display_explore_record 131 * 132 * Description debug function to display the exploration list 133 * 134 * Returns none. 135 * 136 ******************************************************************************/ 137static void bta_gattc_display_explore_record(tBTA_GATTC_ATTR_REC* p_rec, 138 uint8_t num_rec) { 139 uint8_t i; 140 tBTA_GATTC_ATTR_REC* pp = p_rec; 141 142 LOG(ERROR) << "<================Start Explore Queue =============>"; 143 for (i = 0; i < num_rec; i++, pp++) { 144 LOG(ERROR) << StringPrintf( 145 "\t rec[%d] uuid[%s] s_handle[%d] e_handle[%d] is_primary[%d]", i + 1, 146 pp->uuid.ToString().c_str(), pp->s_handle, pp->e_handle, 147 pp->is_primary); 148 } 149 LOG(ERROR) << "<================ End Explore Queue =============>"; 150 LOG(ERROR) << " "; 151} 152#endif /* BTA_GATT_DEBUG == TRUE */ 153 154/******************************************************************************* 155 * 156 * Function bta_gattc_init_cache 157 * 158 * Description Initialize the database cache and discovery related 159 * resources. 160 * 161 * Returns status 162 * 163 ******************************************************************************/ 164tGATT_STATUS bta_gattc_init_cache(tBTA_GATTC_SERV* p_srvc_cb) { 165 p_srvc_cb->srvc_cache.clear(); 166 167 osi_free(p_srvc_cb->p_srvc_list); 168 p_srvc_cb->p_srvc_list = 169 (tBTA_GATTC_ATTR_REC*)osi_malloc(BTA_GATTC_ATTR_LIST_SIZE); 170 p_srvc_cb->total_srvc = 0; 171 p_srvc_cb->cur_srvc_idx = 0; 172 p_srvc_cb->cur_char_idx = 0; 173 p_srvc_cb->next_avail_idx = 0; 174 175 return GATT_SUCCESS; 176} 177 178/** Add a service into database cache */ 179static void bta_gattc_add_srvc_to_cache(tBTA_GATTC_SERV* p_srvc_cb, 180 uint16_t s_handle, uint16_t e_handle, 181 const Uuid& uuid, bool is_primary) { 182#if (BTA_GATT_DEBUG == TRUE) 183 VLOG(1) << "Add a service into Service"; 184#endif 185 186 p_srvc_cb->srvc_cache.emplace_back(tBTA_GATTC_SERVICE{ 187 .s_handle = s_handle, 188 .e_handle = e_handle, 189 .is_primary = is_primary, 190 .uuid = uuid, 191 .handle = s_handle, 192 }); 193} 194 195static void bta_gattc_add_char_to_cache(tBTA_GATTC_SERV* p_srvc_cb, 196 uint16_t attr_handle, 197 uint16_t value_handle, const Uuid& uuid, 198 uint8_t property) { 199#if (BTA_GATT_DEBUG == TRUE) 200 VLOG(1) << __func__ 201 << ": Add a characteristic into service. handle:" << +value_handle 202 << " uuid:" << uuid << " property=0x" << std::hex << +property; 203#endif 204 205 tBTA_GATTC_SERVICE* service = 206 bta_gattc_find_matching_service(p_srvc_cb->srvc_cache, attr_handle); 207 if (!service) { 208 LOG(ERROR) << "Illegal action to add char/descr/incl srvc for non-existing " 209 "service!"; 210 return; 211 } 212 213 /* TODO(jpawlowski): We should use attribute handle, not value handle to refer 214 to characteristic. 215 This is just a temporary workaround. 216 */ 217 if (service->e_handle < value_handle) service->e_handle = value_handle; 218 219 service->characteristics.emplace_back( 220 tBTA_GATTC_CHARACTERISTIC{.handle = value_handle, 221 .properties = property, 222 .uuid = uuid, 223 .service = service}); 224 return; 225} 226 227/* Add an attribute into database cache buffer */ 228static void bta_gattc_add_attr_to_cache(tBTA_GATTC_SERV* p_srvc_cb, 229 uint16_t handle, const Uuid& uuid, 230 uint8_t property, 231 uint16_t incl_srvc_s_handle, 232 tBTA_GATTC_ATTR_TYPE type) { 233#if (BTA_GATT_DEBUG == TRUE) 234 VLOG(1) << __func__ << ": Add a " << bta_gattc_attr_type[type] 235 << " into Service, handle=" << +handle << " uuid=" << uuid 236 << " property=0x" << std::hex << property << " type=" << +type; 237#endif 238 239 tBTA_GATTC_SERVICE* service = 240 bta_gattc_find_matching_service(p_srvc_cb->srvc_cache, handle); 241 if (!service) { 242 LOG(ERROR) << "Illegal action to add char/descr/incl srvc for non-existing " 243 "service!"; 244 return; 245 } 246 247 if (type == BTA_GATTC_ATTR_TYPE_INCL_SRVC) { 248 tBTA_GATTC_SERVICE* included_service = bta_gattc_find_matching_service( 249 p_srvc_cb->srvc_cache, incl_srvc_s_handle); 250 if (!included_service) { 251 LOG(ERROR) << __func__ 252 << ": Illegal action to add non-existing included service!"; 253 return; 254 } 255 256 service->included_svc.emplace_back(tBTA_GATTC_INCLUDED_SVC{ 257 .handle = handle, 258 .uuid = uuid, 259 .owning_service = service, 260 .included_service = included_service, 261 }); 262 } else if (type == BTA_GATTC_ATTR_TYPE_CHAR_DESCR) { 263 if (service->characteristics.empty()) { 264 LOG(ERROR) << __func__ 265 << ": Illegal action to add descriptor before adding a " 266 "characteristic!"; 267 return; 268 } 269 270 tBTA_GATTC_CHARACTERISTIC& char_node = service->characteristics.back(); 271 char_node.descriptors.emplace_back(tBTA_GATTC_DESCRIPTOR{ 272 .handle = handle, .uuid = uuid, .characteristic = &char_node, 273 }); 274 } 275} 276 277/******************************************************************************* 278 * 279 * Function bta_gattc_get_disc_range 280 * 281 * Description get discovery stating and ending handle range. 282 * 283 * Returns None. 284 * 285 ******************************************************************************/ 286void bta_gattc_get_disc_range(tBTA_GATTC_SERV* p_srvc_cb, uint16_t* p_s_hdl, 287 uint16_t* p_e_hdl, bool is_srvc) { 288 tBTA_GATTC_ATTR_REC* p_rec = NULL; 289 290 if (is_srvc) { 291 p_rec = p_srvc_cb->p_srvc_list + p_srvc_cb->cur_srvc_idx; 292 *p_s_hdl = p_rec->s_handle; 293 } else { 294 p_rec = p_srvc_cb->p_srvc_list + p_srvc_cb->cur_char_idx; 295 *p_s_hdl = p_rec->s_handle + 1; 296 } 297 298 *p_e_hdl = p_rec->e_handle; 299#if (BTA_GATT_DEBUG == TRUE) 300 VLOG(1) << StringPrintf("discover range [%d ~ %d]", p_rec->s_handle, 301 p_rec->e_handle); 302#endif 303 return; 304} 305/******************************************************************************* 306 * 307 * Function bta_gattc_discover_pri_service 308 * 309 * Description Start primary service discovery 310 * 311 * Returns status of the operation. 312 * 313 ******************************************************************************/ 314tGATT_STATUS bta_gattc_discover_pri_service(uint16_t conn_id, 315 tBTA_GATTC_SERV* p_server_cb, 316 uint8_t disc_type) { 317 tBTA_GATTC_CLCB* p_clcb = bta_gattc_find_clcb_by_conn_id(conn_id); 318 tGATT_STATUS status = GATT_ERROR; 319 320 if (p_clcb) { 321 if (p_clcb->transport == BTA_TRANSPORT_LE) 322 status = bta_gattc_discover_procedure(conn_id, p_server_cb, disc_type); 323 else 324 status = bta_gattc_sdp_service_disc(conn_id, p_server_cb); 325 } 326 327 return status; 328} 329/******************************************************************************* 330 * 331 * Function bta_gattc_discover_procedure 332 * 333 * Description Start a particular type of discovery procedure on server. 334 * 335 * Returns status of the operation. 336 * 337 ******************************************************************************/ 338tGATT_STATUS bta_gattc_discover_procedure(uint16_t conn_id, 339 tBTA_GATTC_SERV* p_server_cb, 340 uint8_t disc_type) { 341 tGATT_DISC_PARAM param; 342 bool is_service = true; 343 344 memset(¶m, 0, sizeof(tGATT_DISC_PARAM)); 345 346 if (disc_type == GATT_DISC_SRVC_ALL || disc_type == GATT_DISC_SRVC_BY_UUID) { 347 param.s_handle = 1; 348 param.e_handle = 0xFFFF; 349 } else { 350 if (disc_type == GATT_DISC_CHAR_DSCPT) is_service = false; 351 352 bta_gattc_get_disc_range(p_server_cb, ¶m.s_handle, ¶m.e_handle, 353 is_service); 354 355 if (param.s_handle > param.e_handle) { 356 return GATT_ERROR; 357 } 358 } 359 return GATTC_Discover(conn_id, disc_type, ¶m); 360} 361/******************************************************************************* 362 * 363 * Function bta_gattc_start_disc_include_srvc 364 * 365 * Description Start discovery for included service 366 * 367 * Returns status of the operation. 368 * 369 ******************************************************************************/ 370tGATT_STATUS bta_gattc_start_disc_include_srvc(uint16_t conn_id, 371 tBTA_GATTC_SERV* p_srvc_cb) { 372 return bta_gattc_discover_procedure(conn_id, p_srvc_cb, GATT_DISC_INC_SRVC); 373} 374/******************************************************************************* 375 * 376 * Function bta_gattc_start_disc_char 377 * 378 * Description Start discovery for characteristic 379 * 380 * Returns status of the operation. 381 * 382 ******************************************************************************/ 383tGATT_STATUS bta_gattc_start_disc_char(uint16_t conn_id, 384 tBTA_GATTC_SERV* p_srvc_cb) { 385 p_srvc_cb->total_char = 0; 386 387 return bta_gattc_discover_procedure(conn_id, p_srvc_cb, GATT_DISC_CHAR); 388} 389/******************************************************************************* 390 * 391 * Function bta_gattc_start_disc_char_dscp 392 * 393 * Description Start discovery for characteristic descriptor 394 * 395 * Returns none. 396 * 397 ******************************************************************************/ 398void bta_gattc_start_disc_char_dscp(uint16_t conn_id, 399 tBTA_GATTC_SERV* p_srvc_cb) { 400 VLOG(1) << "starting discover characteristics descriptor"; 401 402 if (bta_gattc_discover_procedure(conn_id, p_srvc_cb, GATT_DISC_CHAR_DSCPT) != 403 0) 404 bta_gattc_char_dscpt_disc_cmpl(conn_id, p_srvc_cb); 405} 406/******************************************************************************* 407 * 408 * Function bta_gattc_explore_srvc 409 * 410 * Description process the service discovery complete event 411 * 412 * Returns status 413 * 414 ******************************************************************************/ 415static void bta_gattc_explore_srvc(uint16_t conn_id, 416 tBTA_GATTC_SERV* p_srvc_cb) { 417 tBTA_GATTC_ATTR_REC* p_rec = p_srvc_cb->p_srvc_list + p_srvc_cb->cur_srvc_idx; 418 tBTA_GATTC_CLCB* p_clcb = bta_gattc_find_clcb_by_conn_id(conn_id); 419 420 VLOG(1) << "Start service discovery: srvc_idx:" << +p_srvc_cb->cur_srvc_idx; 421 422 p_srvc_cb->cur_char_idx = p_srvc_cb->next_avail_idx = p_srvc_cb->total_srvc; 423 424 if (p_clcb == NULL) { 425 LOG(ERROR) << "unknown connection ID"; 426 return; 427 } 428 /* start expore a service if there is service not been explored */ 429 if (p_srvc_cb->cur_srvc_idx < p_srvc_cb->total_srvc) { 430 /* add the first service into cache */ 431 bta_gattc_add_srvc_to_cache(p_srvc_cb, p_rec->s_handle, p_rec->e_handle, 432 p_rec->uuid, p_rec->is_primary); 433 434 /* start discovering included services */ 435 bta_gattc_start_disc_include_srvc(conn_id, p_srvc_cb); 436 return; 437 } 438 /* no service found at all, the end of server discovery*/ 439 LOG_WARN(LOG_TAG, "%s no more services found", __func__); 440 441#if (BTA_GATT_DEBUG == TRUE) 442 bta_gattc_display_cache_server(p_srvc_cb->srvc_cache); 443#endif 444 /* save cache to NV */ 445 p_clcb->p_srcb->state = BTA_GATTC_SERV_SAVE; 446 447 if (btm_sec_is_a_bonded_dev(p_srvc_cb->server_bda)) { 448 bta_gattc_cache_save(p_clcb->p_srcb, p_clcb->bta_conn_id); 449 } 450 451 bta_gattc_reset_discover_st(p_clcb->p_srcb, GATT_SUCCESS); 452} 453/******************************************************************************* 454 * 455 * Function bta_gattc_incl_srvc_disc_cmpl 456 * 457 * Description process the relationship discovery complete event 458 * 459 * Returns status 460 * 461 ******************************************************************************/ 462static void bta_gattc_incl_srvc_disc_cmpl(uint16_t conn_id, 463 tBTA_GATTC_SERV* p_srvc_cb) { 464 p_srvc_cb->cur_char_idx = p_srvc_cb->total_srvc; 465 466 /* start discoverying characteristic */ 467 bta_gattc_start_disc_char(conn_id, p_srvc_cb); 468} 469/******************************************************************************* 470 * 471 * Function bta_gattc_char_disc_cmpl 472 * 473 * Description process the characteristic discovery complete event 474 * 475 * Returns status 476 * 477 ******************************************************************************/ 478static void bta_gattc_char_disc_cmpl(uint16_t conn_id, 479 tBTA_GATTC_SERV* p_srvc_cb) { 480 tBTA_GATTC_ATTR_REC* p_rec = p_srvc_cb->p_srvc_list + p_srvc_cb->cur_char_idx; 481 482 /* if there are characteristic needs to be explored */ 483 if (p_srvc_cb->total_char > 0) { 484 /* add the first characteristic into cache */ 485 bta_gattc_add_char_to_cache(p_srvc_cb, p_rec->char_decl_handle, 486 p_rec->s_handle, p_rec->uuid, p_rec->property); 487 488 /* start discoverying characteristic descriptor , if failed, disc for next 489 * char*/ 490 bta_gattc_start_disc_char_dscp(conn_id, p_srvc_cb); 491 } else /* otherwise start with next service */ 492 { 493 p_srvc_cb->cur_srvc_idx++; 494 495 bta_gattc_explore_srvc(conn_id, p_srvc_cb); 496 } 497} 498/******************************************************************************* 499 * 500 * Function bta_gattc_char_dscpt_disc_cmpl 501 * 502 * Description process the char descriptor discovery complete event 503 * 504 * Returns status 505 * 506 ******************************************************************************/ 507static void bta_gattc_char_dscpt_disc_cmpl(uint16_t conn_id, 508 tBTA_GATTC_SERV* p_srvc_cb) { 509 tBTA_GATTC_ATTR_REC* p_rec = NULL; 510 511 if (--p_srvc_cb->total_char > 0) { 512 p_rec = p_srvc_cb->p_srvc_list + (++p_srvc_cb->cur_char_idx); 513 /* add the next characteristic into cache */ 514 bta_gattc_add_char_to_cache(p_srvc_cb, p_rec->char_decl_handle, 515 p_rec->s_handle, p_rec->uuid, p_rec->property); 516 517 /* start discoverying next characteristic for char descriptor */ 518 bta_gattc_start_disc_char_dscp(conn_id, p_srvc_cb); 519 } else 520 /* all characteristic has been explored, start with next service if any */ 521 { 522#if (BTA_GATT_DEBUG == TRUE) 523 LOG(ERROR) << "all char has been explored"; 524#endif 525 p_srvc_cb->cur_srvc_idx++; 526 bta_gattc_explore_srvc(conn_id, p_srvc_cb); 527 } 528} 529 530static bool bta_gattc_srvc_in_list(tBTA_GATTC_SERV* p_srvc_cb, 531 uint16_t s_handle, uint16_t e_handle, Uuid) { 532 tBTA_GATTC_ATTR_REC* p_rec = NULL; 533 uint8_t i; 534 bool exist_srvc = false; 535 536 if (!GATT_HANDLE_IS_VALID(s_handle) || !GATT_HANDLE_IS_VALID(e_handle)) { 537 LOG(ERROR) << "invalid included service s_handle=" << loghex(s_handle) 538 << ", e_handle=" << loghex(e_handle); 539 exist_srvc = true; 540 } else { 541 for (i = 0; i < p_srvc_cb->next_avail_idx; i++) { 542 p_rec = p_srvc_cb->p_srvc_list + i; 543 544 /* a new service should not have any overlap with other service handle 545 * range */ 546 if (p_rec->s_handle == s_handle || p_rec->e_handle == e_handle) { 547 exist_srvc = true; 548 break; 549 } 550 } 551 } 552 return exist_srvc; 553} 554/******************************************************************************* 555 * 556 * Function bta_gattc_add_srvc_to_list 557 * 558 * Description Add a service into explore pending list 559 * 560 * Returns status 561 * 562 ******************************************************************************/ 563static tGATT_STATUS bta_gattc_add_srvc_to_list(tBTA_GATTC_SERV* p_srvc_cb, 564 uint16_t s_handle, 565 uint16_t e_handle, 566 const Uuid& uuid, 567 bool is_primary) { 568 tBTA_GATTC_ATTR_REC* p_rec = NULL; 569 tGATT_STATUS status = GATT_SUCCESS; 570 571 if (p_srvc_cb->p_srvc_list && 572 p_srvc_cb->next_avail_idx < BTA_GATTC_MAX_CACHE_CHAR) { 573 p_rec = p_srvc_cb->p_srvc_list + p_srvc_cb->next_avail_idx; 574 575 VLOG(1) << __func__ << "handle:" << loghex(s_handle) 576 << " service type=" << uuid; 577 578 p_rec->s_handle = s_handle; 579 p_rec->e_handle = e_handle; 580 p_rec->is_primary = is_primary; 581 p_rec->uuid = uuid; 582 583 p_srvc_cb->total_srvc++; 584 p_srvc_cb->next_avail_idx++; 585 } else { /* allocate bigger buffer ?? */ 586 status = GATT_DB_FULL; 587 588 LOG(ERROR) << "service not added, no resources or wrong state"; 589 } 590 return status; 591} 592/******************************************************************************* 593 * 594 * Function bta_gattc_add_char_to_list 595 * 596 * Description Add a characteristic into explore pending list 597 * 598 * Returns status 599 * 600 ******************************************************************************/ 601static tGATT_STATUS bta_gattc_add_char_to_list(tBTA_GATTC_SERV* p_srvc_cb, 602 uint16_t decl_handle, 603 uint16_t value_handle, 604 const Uuid& uuid, 605 uint8_t property) { 606 tBTA_GATTC_ATTR_REC* p_rec = NULL; 607 tGATT_STATUS status = GATT_SUCCESS; 608 609 if (p_srvc_cb->p_srvc_list == NULL) { 610 LOG(ERROR) << "No service available, unexpected char discovery result"; 611 status = GATT_INTERNAL_ERROR; 612 } else if (p_srvc_cb->next_avail_idx < BTA_GATTC_MAX_CACHE_CHAR) { 613 p_rec = p_srvc_cb->p_srvc_list + p_srvc_cb->next_avail_idx; 614 615 p_srvc_cb->total_char++; 616 617 p_rec->s_handle = value_handle; 618 p_rec->char_decl_handle = decl_handle; 619 p_rec->property = property; 620 p_rec->e_handle = 621 (p_srvc_cb->p_srvc_list + p_srvc_cb->cur_srvc_idx)->e_handle; 622 p_rec->uuid = uuid; 623 624 /* update the endind handle of pervious characteristic if available */ 625 if (p_srvc_cb->total_char > 1) { 626 p_rec -= 1; 627 p_rec->e_handle = decl_handle - 1; 628 } 629 p_srvc_cb->next_avail_idx++; 630 } else { 631 LOG(ERROR) << "char not added, no resources"; 632 /* allocate bigger buffer ?? */ 633 status = GATT_DB_FULL; 634 } 635 return status; 636} 637 638/******************************************************************************* 639 * 640 * Function bta_gattc_sdp_callback 641 * 642 * Description Process the discovery result from sdp 643 * 644 * Returns void 645 * 646 ******************************************************************************/ 647void bta_gattc_sdp_callback(uint16_t sdp_status, void* user_data) { 648 tSDP_PROTOCOL_ELEM pe; 649 tBTA_GATTC_CB_DATA* cb_data = (tBTA_GATTC_CB_DATA*)user_data; 650 tBTA_GATTC_SERV* p_srvc_cb = bta_gattc_find_scb_by_cid(cb_data->sdp_conn_id); 651 652 if (((sdp_status == SDP_SUCCESS) || (sdp_status == SDP_DB_FULL)) && 653 p_srvc_cb != NULL) { 654 tSDP_DISC_REC* p_sdp_rec = NULL; 655 do { 656 /* find a service record, report it */ 657 p_sdp_rec = SDP_FindServiceInDb(cb_data->p_sdp_db, 0, p_sdp_rec); 658 if (p_sdp_rec) { 659 Uuid service_uuid; 660 if (SDP_FindServiceUUIDInRec(p_sdp_rec, &service_uuid)) { 661 if (SDP_FindProtocolListElemInRec(p_sdp_rec, UUID_PROTOCOL_ATT, 662 &pe)) { 663 uint16_t start_handle = (uint16_t)pe.params[0]; 664 uint16_t end_handle = (uint16_t)pe.params[1]; 665 666#if (BTA_GATT_DEBUG == TRUE) 667 VLOG(1) << "Found ATT service uuid=" << service_uuid 668 << ", s_handle=" << loghex(start_handle) 669 << ", e_handle=" << loghex(end_handle); 670#endif 671 672 if (GATT_HANDLE_IS_VALID(start_handle) && 673 GATT_HANDLE_IS_VALID(end_handle) && p_srvc_cb != NULL) { 674 /* discover services result, add services into a service list */ 675 bta_gattc_add_srvc_to_list(p_srvc_cb, start_handle, end_handle, 676 service_uuid, true); 677 } else { 678 LOG(ERROR) << "invalid start_handle=" << loghex(start_handle) 679 << ", end_handle=" << loghex(end_handle); 680 } 681 } 682 } 683 } 684 } while (p_sdp_rec); 685 } 686 687 if (p_srvc_cb != NULL) { 688 /* start discover primary service */ 689 bta_gattc_explore_srvc(cb_data->sdp_conn_id, p_srvc_cb); 690 } else { 691 LOG(ERROR) << "GATT service discovery is done on unknown connection"; 692 } 693 694 /* both were allocated in bta_gattc_sdp_service_disc */ 695 osi_free(cb_data->p_sdp_db); 696 osi_free(cb_data); 697} 698/******************************************************************************* 699 * 700 * Function bta_gattc_sdp_service_disc 701 * 702 * Description Start DSP Service Discovert 703 * 704 * Returns void 705 * 706 ******************************************************************************/ 707static tGATT_STATUS bta_gattc_sdp_service_disc(uint16_t conn_id, 708 tBTA_GATTC_SERV* p_server_cb) { 709 uint16_t num_attrs = 2; 710 uint16_t attr_list[2]; 711 712 /* 713 * On success, cb_data will be freed inside bta_gattc_sdp_callback, 714 * otherwise it will be freed within this function. 715 */ 716 tBTA_GATTC_CB_DATA* cb_data = 717 (tBTA_GATTC_CB_DATA*)osi_malloc(sizeof(tBTA_GATTC_CB_DATA)); 718 719 cb_data->p_sdp_db = (tSDP_DISCOVERY_DB*)osi_malloc(BTA_GATT_SDP_DB_SIZE); 720 attr_list[0] = ATTR_ID_SERVICE_CLASS_ID_LIST; 721 attr_list[1] = ATTR_ID_PROTOCOL_DESC_LIST; 722 723 Uuid uuid = Uuid::From16Bit(UUID_PROTOCOL_ATT); 724 SDP_InitDiscoveryDb(cb_data->p_sdp_db, BTA_GATT_SDP_DB_SIZE, 1, &uuid, 725 num_attrs, attr_list); 726 727 if (!SDP_ServiceSearchAttributeRequest2(p_server_cb->server_bda, 728 cb_data->p_sdp_db, 729 &bta_gattc_sdp_callback, cb_data)) { 730 osi_free(cb_data->p_sdp_db); 731 osi_free(cb_data); 732 return GATT_ERROR; 733 } 734 735 cb_data->sdp_conn_id = conn_id; 736 return GATT_SUCCESS; 737} 738/******************************************************************************* 739 * 740 * Function bta_gattc_disc_res_cback 741 * bta_gattc_disc_cmpl_cback 742 * 743 * Description callback functions to GATT client stack. 744 * 745 * Returns void 746 * 747 ******************************************************************************/ 748void bta_gattc_disc_res_cback(uint16_t conn_id, tGATT_DISC_TYPE disc_type, 749 tGATT_DISC_RES* p_data) { 750 tBTA_GATTC_SERV* p_srvc_cb = NULL; 751 bool pri_srvc; 752 tBTA_GATTC_CLCB* p_clcb = bta_gattc_find_clcb_by_conn_id(conn_id); 753 754 p_srvc_cb = bta_gattc_find_scb_by_cid(conn_id); 755 756 if (p_srvc_cb != NULL && p_clcb != NULL && 757 p_clcb->state == BTA_GATTC_DISCOVER_ST) { 758 switch (disc_type) { 759 case GATT_DISC_SRVC_ALL: 760 /* discover services result, add services into a service list */ 761 bta_gattc_add_srvc_to_list( 762 p_srvc_cb, p_data->handle, p_data->value.group_value.e_handle, 763 p_data->value.group_value.service_type, true); 764 765 break; 766 case GATT_DISC_SRVC_BY_UUID: 767 bta_gattc_add_srvc_to_list( 768 p_srvc_cb, p_data->handle, p_data->value.group_value.e_handle, 769 p_data->value.group_value.service_type, true); 770 break; 771 772 case GATT_DISC_INC_SRVC: 773 /* add included service into service list if it's secondary or it never 774 showed up 775 in the primary service search */ 776 pri_srvc = bta_gattc_srvc_in_list( 777 p_srvc_cb, p_data->value.incl_service.s_handle, 778 p_data->value.incl_service.e_handle, 779 p_data->value.incl_service.service_type); 780 781 if (!pri_srvc) 782 bta_gattc_add_srvc_to_list( 783 p_srvc_cb, p_data->value.incl_service.s_handle, 784 p_data->value.incl_service.e_handle, 785 p_data->value.incl_service.service_type, false); 786 /* add into database */ 787 bta_gattc_add_attr_to_cache( 788 p_srvc_cb, p_data->handle, p_data->value.incl_service.service_type, 789 pri_srvc, p_data->value.incl_service.s_handle, 790 BTA_GATTC_ATTR_TYPE_INCL_SRVC); 791 break; 792 793 case GATT_DISC_CHAR: 794 /* add char value into database */ 795 bta_gattc_add_char_to_list(p_srvc_cb, p_data->handle, 796 p_data->value.dclr_value.val_handle, 797 p_data->value.dclr_value.char_uuid, 798 p_data->value.dclr_value.char_prop); 799 break; 800 801 case GATT_DISC_CHAR_DSCPT: 802 bta_gattc_add_attr_to_cache(p_srvc_cb, p_data->handle, p_data->type, 0, 803 0 /* incl_srvc_handle */, 804 BTA_GATTC_ATTR_TYPE_CHAR_DESCR); 805 break; 806 } 807 } 808} 809 810void bta_gattc_disc_cmpl_cback(uint16_t conn_id, tGATT_DISC_TYPE disc_type, 811 tGATT_STATUS status) { 812 tBTA_GATTC_CLCB* p_clcb = bta_gattc_find_clcb_by_conn_id(conn_id); 813 814 if (p_clcb && (status != GATT_SUCCESS || p_clcb->status != GATT_SUCCESS)) { 815 if (status == GATT_SUCCESS) p_clcb->status = status; 816 bta_gattc_sm_execute(p_clcb, BTA_GATTC_DISCOVER_CMPL_EVT, NULL); 817 return; 818 } 819 820 tBTA_GATTC_SERV* p_srvc_cb = bta_gattc_find_scb_by_cid(conn_id); 821 if (!p_srvc_cb) return; 822 823 switch (disc_type) { 824 case GATT_DISC_SRVC_ALL: 825 case GATT_DISC_SRVC_BY_UUID: 826#if (BTA_GATT_DEBUG == TRUE) 827 bta_gattc_display_explore_record(p_srvc_cb->p_srvc_list, 828 p_srvc_cb->next_avail_idx); 829#endif 830 bta_gattc_explore_srvc(conn_id, p_srvc_cb); 831 break; 832 833 case GATT_DISC_INC_SRVC: 834 bta_gattc_incl_srvc_disc_cmpl(conn_id, p_srvc_cb); 835 break; 836 837 case GATT_DISC_CHAR: 838#if (BTA_GATT_DEBUG == TRUE) 839 bta_gattc_display_explore_record(p_srvc_cb->p_srvc_list, 840 p_srvc_cb->next_avail_idx); 841#endif 842 bta_gattc_char_disc_cmpl(conn_id, p_srvc_cb); 843 break; 844 845 case GATT_DISC_CHAR_DSCPT: 846 bta_gattc_char_dscpt_disc_cmpl(conn_id, p_srvc_cb); 847 break; 848 } 849} 850 851/******************************************************************************* 852 * 853 * Function bta_gattc_search_service 854 * 855 * Description search local cache for matching service record. 856 * 857 * Returns false if map can not be found. 858 * 859 ******************************************************************************/ 860void bta_gattc_search_service(tBTA_GATTC_CLCB* p_clcb, Uuid* p_uuid) { 861 for (const tBTA_GATTC_SERVICE& service : p_clcb->p_srcb->srvc_cache) { 862 if (p_uuid && *p_uuid != service.uuid) continue; 863 864#if (BTA_GATT_DEBUG == TRUE) 865 VLOG(1) << __func__ << "found service " << service.uuid 866 << ", inst:" << +service.handle << " handle:" << +service.s_handle; 867#endif 868 if (!p_clcb->p_rcb->p_cback) continue; 869 870 tBTA_GATTC cb_data; 871 memset(&cb_data, 0, sizeof(tBTA_GATTC)); 872 cb_data.srvc_res.conn_id = p_clcb->bta_conn_id; 873 cb_data.srvc_res.service_uuid.inst_id = service.handle; 874 cb_data.srvc_res.service_uuid.uuid = service.uuid; 875 876 (*p_clcb->p_rcb->p_cback)(BTA_GATTC_SEARCH_RES_EVT, &cb_data); 877 } 878} 879 880std::list<tBTA_GATTC_SERVICE>* bta_gattc_get_services_srcb( 881 tBTA_GATTC_SERV* p_srcb) { 882 if (!p_srcb || p_srcb->srvc_cache.empty()) return NULL; 883 884 return &p_srcb->srvc_cache; 885} 886 887std::list<tBTA_GATTC_SERVICE>* bta_gattc_get_services(uint16_t conn_id) { 888 tBTA_GATTC_CLCB* p_clcb = bta_gattc_find_clcb_by_conn_id(conn_id); 889 890 if (p_clcb == NULL) return NULL; 891 892 tBTA_GATTC_SERV* p_srcb = p_clcb->p_srcb; 893 894 return bta_gattc_get_services_srcb(p_srcb); 895} 896 897tBTA_GATTC_SERVICE* bta_gattc_find_matching_service( 898 std::list<tBTA_GATTC_SERVICE>& services, uint16_t handle) { 899 for (tBTA_GATTC_SERVICE& service : services) { 900 if (handle >= service.s_handle && handle <= service.e_handle) 901 return &service; 902 } 903 904 return NULL; 905} 906 907static tBTA_GATTC_SERVICE* bta_gattc_get_service_for_handle_srcb( 908 tBTA_GATTC_SERV* p_srcb, uint16_t handle) { 909 std::list<tBTA_GATTC_SERVICE>* services = bta_gattc_get_services_srcb(p_srcb); 910 if (services == NULL) return NULL; 911 return bta_gattc_find_matching_service(*services, handle); 912} 913 914const tBTA_GATTC_SERVICE* bta_gattc_get_service_for_handle(uint16_t conn_id, 915 uint16_t handle) { 916 std::list<tBTA_GATTC_SERVICE>* services = bta_gattc_get_services(conn_id); 917 if (services == NULL) return NULL; 918 919 return bta_gattc_find_matching_service(*services, handle); 920} 921 922tBTA_GATTC_CHARACTERISTIC* bta_gattc_get_characteristic_srcb( 923 tBTA_GATTC_SERV* p_srcb, uint16_t handle) { 924 tBTA_GATTC_SERVICE* service = 925 bta_gattc_get_service_for_handle_srcb(p_srcb, handle); 926 927 if (!service) return NULL; 928 929 for (tBTA_GATTC_CHARACTERISTIC& charac : service->characteristics) { 930 if (handle == charac.handle) return &charac; 931 } 932 933 return NULL; 934} 935 936tBTA_GATTC_CHARACTERISTIC* bta_gattc_get_characteristic(uint16_t conn_id, 937 uint16_t handle) { 938 tBTA_GATTC_CLCB* p_clcb = bta_gattc_find_clcb_by_conn_id(conn_id); 939 940 if (p_clcb == NULL) return NULL; 941 942 tBTA_GATTC_SERV* p_srcb = p_clcb->p_srcb; 943 return bta_gattc_get_characteristic_srcb(p_srcb, handle); 944} 945 946const tBTA_GATTC_DESCRIPTOR* bta_gattc_get_descriptor_srcb( 947 tBTA_GATTC_SERV* p_srcb, uint16_t handle) { 948 const tBTA_GATTC_SERVICE* service = 949 bta_gattc_get_service_for_handle_srcb(p_srcb, handle); 950 951 if (!service) { 952 return NULL; 953 } 954 955 for (const tBTA_GATTC_CHARACTERISTIC& charac : service->characteristics) { 956 for (const tBTA_GATTC_DESCRIPTOR& desc : charac.descriptors) { 957 if (handle == desc.handle) return &desc; 958 } 959 } 960 961 return NULL; 962} 963 964const tBTA_GATTC_DESCRIPTOR* bta_gattc_get_descriptor(uint16_t conn_id, 965 uint16_t handle) { 966 tBTA_GATTC_CLCB* p_clcb = bta_gattc_find_clcb_by_conn_id(conn_id); 967 968 if (p_clcb == NULL) return NULL; 969 970 tBTA_GATTC_SERV* p_srcb = p_clcb->p_srcb; 971 return bta_gattc_get_descriptor_srcb(p_srcb, handle); 972} 973 974/******************************************************************************* 975 * 976 * Function bta_gattc_fill_gatt_db_el 977 * 978 * Description fill a btgatt_db_element_t value 979 * 980 * Returns None. 981 * 982 ******************************************************************************/ 983void bta_gattc_fill_gatt_db_el(btgatt_db_element_t* p_attr, 984 bt_gatt_db_attribute_type_t type, 985 uint16_t att_handle, uint16_t s_handle, 986 uint16_t e_handle, uint16_t id, const Uuid& uuid, 987 uint8_t prop) { 988 p_attr->type = type; 989 p_attr->attribute_handle = att_handle; 990 p_attr->start_handle = s_handle; 991 p_attr->end_handle = e_handle; 992 p_attr->id = id; 993 p_attr->properties = prop; 994 995 // Permissions are not discoverable using the attribute protocol. 996 // Core 5.0, Part F, 3.2.5 Attribute Permissions 997 p_attr->permissions = 0; 998 p_attr->uuid = uuid; 999} 1000 1001/******************************************************************************* 1002 * Returns number of elements inside db from start_handle to end_handle 1003 ******************************************************************************/ 1004static size_t bta_gattc_get_db_size( 1005 const std::list<tBTA_GATTC_SERVICE>& services, uint16_t start_handle, 1006 uint16_t end_handle) { 1007 if (services.empty()) return 0; 1008 1009 size_t db_size = 0; 1010 1011 for (const tBTA_GATTC_SERVICE& service : services) { 1012 if (service.s_handle < start_handle) continue; 1013 1014 if (service.e_handle > end_handle) break; 1015 1016 db_size++; 1017 1018 for (const tBTA_GATTC_CHARACTERISTIC& charac : service.characteristics) { 1019 db_size++; 1020 1021 db_size += charac.descriptors.size(); 1022 } 1023 1024 db_size += service.included_svc.size(); 1025 } 1026 1027 return db_size; 1028} 1029 1030/******************************************************************************* 1031 * 1032 * Function bta_gattc_get_gatt_db_impl 1033 * 1034 * Description copy the server GATT database into db parameter. 1035 * 1036 * Parameters p_srvc_cb: server. 1037 * db: output parameter which will contain GATT database copy. 1038 * Caller is responsible for freeing it. 1039 * count: output parameter which will contain number of 1040 * elements in database. 1041 * 1042 * Returns None. 1043 * 1044 ******************************************************************************/ 1045static void bta_gattc_get_gatt_db_impl(tBTA_GATTC_SERV* p_srvc_cb, 1046 uint16_t start_handle, 1047 uint16_t end_handle, 1048 btgatt_db_element_t** db, int* count) { 1049 VLOG(1) << __func__ 1050 << StringPrintf(": start_handle 0x%04x, end_handle 0x%04x", 1051 start_handle, end_handle); 1052 1053 if (p_srvc_cb->srvc_cache.empty()) { 1054 *count = 0; 1055 *db = NULL; 1056 return; 1057 } 1058 1059 size_t db_size = 1060 bta_gattc_get_db_size(p_srvc_cb->srvc_cache, start_handle, end_handle); 1061 1062 void* buffer = osi_malloc(db_size * sizeof(btgatt_db_element_t)); 1063 btgatt_db_element_t* curr_db_attr = (btgatt_db_element_t*)buffer; 1064 1065 for (const tBTA_GATTC_SERVICE& service : p_srvc_cb->srvc_cache) { 1066 if (service.s_handle < start_handle) continue; 1067 1068 if (service.e_handle > end_handle) break; 1069 1070 bta_gattc_fill_gatt_db_el(curr_db_attr, 1071 service.is_primary ? BTGATT_DB_PRIMARY_SERVICE 1072 : BTGATT_DB_SECONDARY_SERVICE, 1073 0 /* att_handle */, service.s_handle, 1074 service.e_handle, service.s_handle, service.uuid, 1075 0 /* prop */); 1076 curr_db_attr++; 1077 1078 for (const tBTA_GATTC_CHARACTERISTIC& charac : service.characteristics) { 1079 bta_gattc_fill_gatt_db_el(curr_db_attr, BTGATT_DB_CHARACTERISTIC, 1080 charac.handle, 0 /* s_handle */, 1081 0 /* e_handle */, charac.handle, charac.uuid, 1082 charac.properties); 1083 curr_db_attr++; 1084 1085 for (const tBTA_GATTC_DESCRIPTOR& desc : charac.descriptors) { 1086 bta_gattc_fill_gatt_db_el( 1087 curr_db_attr, BTGATT_DB_DESCRIPTOR, desc.handle, 0 /* s_handle */, 1088 0 /* e_handle */, desc.handle, desc.uuid, 0 /* property */); 1089 curr_db_attr++; 1090 } 1091 } 1092 1093 for (const tBTA_GATTC_INCLUDED_SVC& p_isvc : service.included_svc) { 1094 bta_gattc_fill_gatt_db_el( 1095 curr_db_attr, BTGATT_DB_INCLUDED_SERVICE, p_isvc.handle, 1096 p_isvc.included_service ? p_isvc.included_service->s_handle : 0, 1097 0 /* e_handle */, p_isvc.handle, p_isvc.uuid, 0 /* property */); 1098 curr_db_attr++; 1099 } 1100 } 1101 1102 *db = (btgatt_db_element_t*)buffer; 1103 *count = db_size; 1104} 1105 1106/******************************************************************************* 1107 * 1108 * Function bta_gattc_get_gatt_db 1109 * 1110 * Description copy the server GATT database into db parameter. 1111 * 1112 * Parameters conn_id: connection ID which identify the server. 1113 * db: output parameter which will contain GATT database copy. 1114 * Caller is responsible for freeing it. 1115 * count: number of elements in database. 1116 * 1117 * Returns None. 1118 * 1119 ******************************************************************************/ 1120void bta_gattc_get_gatt_db(uint16_t conn_id, uint16_t start_handle, 1121 uint16_t end_handle, btgatt_db_element_t** db, 1122 int* count) { 1123 tBTA_GATTC_CLCB* p_clcb = bta_gattc_find_clcb_by_conn_id(conn_id); 1124 1125 LOG_DEBUG(LOG_TAG, "%s", __func__); 1126 if (p_clcb == NULL) { 1127 LOG(ERROR) << "Unknown conn_id=" << loghex(conn_id); 1128 return; 1129 } 1130 1131 if (p_clcb->state != BTA_GATTC_CONN_ST) { 1132 LOG(ERROR) << "server cache not available, CLCB state=" << +p_clcb->state; 1133 return; 1134 } 1135 1136 if (!p_clcb->p_srcb || 1137 p_clcb->p_srcb->p_srvc_list || /* no active discovery */ 1138 p_clcb->p_srcb->srvc_cache.empty()) { 1139 LOG(ERROR) << "No server cache available"; 1140 return; 1141 } 1142 1143 bta_gattc_get_gatt_db_impl(p_clcb->p_srcb, start_handle, end_handle, db, 1144 count); 1145} 1146 1147/******************************************************************************* 1148 * 1149 * Function bta_gattc_rebuild_cache 1150 * 1151 * Description rebuild server cache from NV cache. 1152 * 1153 * Parameters 1154 * 1155 * Returns None. 1156 * 1157 ******************************************************************************/ 1158void bta_gattc_rebuild_cache(tBTA_GATTC_SERV* p_srvc_cb, uint16_t num_attr, 1159 tBTA_GATTC_NV_ATTR* p_attr) { 1160 /* first attribute loading, initialize buffer */ 1161 LOG(ERROR) << __func__; 1162 1163 p_srvc_cb->srvc_cache.clear(); 1164 1165 while (num_attr > 0 && p_attr != NULL) { 1166 switch (p_attr->attr_type) { 1167 case BTA_GATTC_ATTR_TYPE_SRVC: 1168 bta_gattc_add_srvc_to_cache(p_srvc_cb, p_attr->s_handle, 1169 p_attr->e_handle, p_attr->uuid, 1170 p_attr->is_primary); 1171 break; 1172 1173 case BTA_GATTC_ATTR_TYPE_CHAR: 1174 // TODO(jpawlowski): store decl_handle properly. 1175 bta_gattc_add_char_to_cache(p_srvc_cb, p_attr->s_handle, 1176 p_attr->s_handle, p_attr->uuid, 1177 p_attr->prop); 1178 break; 1179 1180 case BTA_GATTC_ATTR_TYPE_CHAR_DESCR: 1181 case BTA_GATTC_ATTR_TYPE_INCL_SRVC: 1182 bta_gattc_add_attr_to_cache(p_srvc_cb, p_attr->s_handle, p_attr->uuid, 1183 p_attr->prop, p_attr->incl_srvc_handle, 1184 p_attr->attr_type); 1185 break; 1186 } 1187 p_attr++; 1188 num_attr--; 1189 } 1190} 1191 1192/******************************************************************************* 1193 * 1194 * Function bta_gattc_fill_nv_attr 1195 * 1196 * Description fill a NV attribute entry value 1197 * 1198 * Returns None. 1199 * 1200 ******************************************************************************/ 1201void bta_gattc_fill_nv_attr(tBTA_GATTC_NV_ATTR* p_attr, uint8_t type, 1202 uint16_t s_handle, uint16_t e_handle, Uuid uuid, 1203 uint8_t prop, uint16_t incl_srvc_handle, 1204 bool is_primary) { 1205 p_attr->s_handle = s_handle; 1206 p_attr->e_handle = e_handle; 1207 p_attr->attr_type = type; 1208 p_attr->is_primary = is_primary; 1209 p_attr->id = 0; 1210 p_attr->prop = prop; 1211 p_attr->incl_srvc_handle = incl_srvc_handle; 1212 p_attr->uuid = uuid; 1213} 1214 1215/******************************************************************************* 1216 * 1217 * Function bta_gattc_cache_save 1218 * 1219 * Description save the server cache into NV 1220 * 1221 * Returns None. 1222 * 1223 ******************************************************************************/ 1224void bta_gattc_cache_save(tBTA_GATTC_SERV* p_srvc_cb, uint16_t conn_id) { 1225 if (p_srvc_cb->srvc_cache.empty()) return; 1226 1227 int i = 0; 1228 size_t db_size = bta_gattc_get_db_size(p_srvc_cb->srvc_cache, 0x0000, 0xFFFF); 1229 tBTA_GATTC_NV_ATTR* nv_attr = 1230 (tBTA_GATTC_NV_ATTR*)osi_malloc(db_size * sizeof(tBTA_GATTC_NV_ATTR)); 1231 1232 for (const tBTA_GATTC_SERVICE& service : p_srvc_cb->srvc_cache) { 1233 bta_gattc_fill_nv_attr(&nv_attr[i++], BTA_GATTC_ATTR_TYPE_SRVC, 1234 service.s_handle, service.e_handle, service.uuid, 1235 0 /* properties */, 0 /* incl_srvc_handle */, 1236 service.is_primary); 1237 } 1238 1239 for (const tBTA_GATTC_SERVICE& service : p_srvc_cb->srvc_cache) { 1240 for (const tBTA_GATTC_CHARACTERISTIC& charac : service.characteristics) { 1241 bta_gattc_fill_nv_attr(&nv_attr[i++], BTA_GATTC_ATTR_TYPE_CHAR, 1242 charac.handle, 0, charac.uuid, charac.properties, 1243 0 /* incl_srvc_handle */, false); 1244 1245 for (const tBTA_GATTC_DESCRIPTOR& desc : charac.descriptors) { 1246 bta_gattc_fill_nv_attr(&nv_attr[i++], BTA_GATTC_ATTR_TYPE_CHAR_DESCR, 1247 desc.handle, 0, desc.uuid, 0 /* properties */, 1248 0 /* incl_srvc_handle */, false); 1249 } 1250 } 1251 1252 for (const tBTA_GATTC_INCLUDED_SVC& p_isvc : service.included_svc) { 1253 bta_gattc_fill_nv_attr(&nv_attr[i++], BTA_GATTC_ATTR_TYPE_INCL_SRVC, 1254 p_isvc.handle, 0, p_isvc.uuid, 0 /* properties */, 1255 p_isvc.included_service->s_handle, false); 1256 } 1257 } 1258 1259 bta_gattc_cache_write(p_srvc_cb->server_bda, db_size, nv_attr); 1260 osi_free(nv_attr); 1261} 1262 1263/******************************************************************************* 1264 * 1265 * Function bta_gattc_cache_load 1266 * 1267 * Description Load GATT cache from storage for server. 1268 * 1269 * Parameter p_clcb: pointer to server clcb, that will 1270 * be filled from storage 1271 * Returns true on success, false otherwise 1272 * 1273 ******************************************************************************/ 1274bool bta_gattc_cache_load(tBTA_GATTC_CLCB* p_clcb) { 1275 char fname[255] = {0}; 1276 bta_gattc_generate_cache_file_name(fname, sizeof(fname), 1277 p_clcb->p_srcb->server_bda); 1278 1279 FILE* fd = fopen(fname, "rb"); 1280 if (!fd) { 1281 LOG(ERROR) << __func__ << ": can't open GATT cache file " << fname 1282 << " for reading, error: " << strerror(errno); 1283 return false; 1284 } 1285 1286 uint16_t cache_ver = 0; 1287 tBTA_GATTC_NV_ATTR* attr = NULL; 1288 bool success = false; 1289 uint16_t num_attr = 0; 1290 1291 if (fread(&cache_ver, sizeof(uint16_t), 1, fd) != 1) { 1292 LOG(ERROR) << __func__ << ": can't read GATT cache version from: " << fname; 1293 goto done; 1294 } 1295 1296 if (cache_ver != GATT_CACHE_VERSION) { 1297 LOG(ERROR) << __func__ << ": wrong GATT cache version: " << fname; 1298 goto done; 1299 } 1300 1301 if (fread(&num_attr, sizeof(uint16_t), 1, fd) != 1) { 1302 LOG(ERROR) << __func__ 1303 << ": can't read number of GATT attributes: " << fname; 1304 goto done; 1305 } 1306 1307 if (num_attr > 0xFFFF) { 1308 LOG(ERROR) << __func__ << ": more than 0xFFFF GATT attributes: " << fname; 1309 goto done; 1310 } 1311 1312 attr = (tBTA_GATTC_NV_ATTR*)osi_malloc(sizeof(tBTA_GATTC_NV_ATTR) * num_attr); 1313 1314 if (fread(attr, sizeof(tBTA_GATTC_NV_ATTR), num_attr, fd) != num_attr) { 1315 LOG(ERROR) << __func__ << "s: can't read GATT attributes: " << fname; 1316 goto done; 1317 } 1318 1319 bta_gattc_rebuild_cache(p_clcb->p_srcb, num_attr, attr); 1320 1321 success = true; 1322 1323done: 1324 osi_free(attr); 1325 fclose(fd); 1326 return success; 1327} 1328 1329/******************************************************************************* 1330 * 1331 * Function bta_gattc_cache_write 1332 * 1333 * Description This callout function is executed by GATT when a server 1334 * cache is available to save. 1335 * 1336 * Parameter server_bda: server bd address of this cache belongs to 1337 * num_attr: number of attribute to be save. 1338 * attr: pointer to the list of attributes to save. 1339 * Returns 1340 * 1341 ******************************************************************************/ 1342static void bta_gattc_cache_write(const RawAddress& server_bda, 1343 uint16_t num_attr, tBTA_GATTC_NV_ATTR* attr) { 1344 char fname[255] = {0}; 1345 bta_gattc_generate_cache_file_name(fname, sizeof(fname), server_bda); 1346 1347 FILE* fd = fopen(fname, "wb"); 1348 if (!fd) { 1349 LOG(ERROR) << __func__ 1350 << ": can't open GATT cache file for writing: " << fname; 1351 return; 1352 } 1353 1354 uint16_t cache_ver = GATT_CACHE_VERSION; 1355 if (fwrite(&cache_ver, sizeof(uint16_t), 1, fd) != 1) { 1356 LOG(ERROR) << __func__ << ": can't write GATT cache version: " << fname; 1357 fclose(fd); 1358 return; 1359 } 1360 1361 if (fwrite(&num_attr, sizeof(uint16_t), 1, fd) != 1) { 1362 LOG(ERROR) << __func__ 1363 << ": can't write GATT cache attribute count: " << fname; 1364 fclose(fd); 1365 return; 1366 } 1367 1368 if (fwrite(attr, sizeof(tBTA_GATTC_NV_ATTR), num_attr, fd) != num_attr) { 1369 LOG(ERROR) << __func__ << ": can't write GATT cache attributes: " << fname; 1370 fclose(fd); 1371 return; 1372 } 1373 1374 fclose(fd); 1375} 1376 1377/******************************************************************************* 1378 * 1379 * Function bta_gattc_cache_reset 1380 * 1381 * Description This callout function is executed by GATTC to reset cache in 1382 * application 1383 * 1384 * Parameter server_bda: server bd address of this cache belongs to 1385 * 1386 * Returns void. 1387 * 1388 ******************************************************************************/ 1389void bta_gattc_cache_reset(const RawAddress& server_bda) { 1390 VLOG(1) << __func__; 1391 char fname[255] = {0}; 1392 bta_gattc_generate_cache_file_name(fname, sizeof(fname), server_bda); 1393 unlink(fname); 1394} 1395