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