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