OSDN Git Service

DO NOT MERGE ANYWHERE Do not update sco_state when no matching peer_addr is found...
[android-x86/system-bt.git] / bta / gatt / bta_gattc_utils.c
1 /******************************************************************************
2  *
3  *  Copyright (C) 2003-2012 Broadcom Corporation
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18
19 /******************************************************************************
20  *
21  *  This file contains the GATT client utility function.
22  *
23  ******************************************************************************/
24
25 #define LOG_TAG "bt_bta_gattc"
26
27 #include "bt_target.h"
28
29 #if defined(BTA_GATT_INCLUDED) && (BTA_GATT_INCLUDED == TRUE)
30
31 #include <string.h>
32
33 #include "bta_gattc_int.h"
34 #include "bta_sys.h"
35 #include "btcore/include/bdaddr.h"
36 #include "btif/include/btif_util.h"
37 #include "bt_common.h"
38 #include "l2c_api.h"
39 #include "utl.h"
40
41 /*****************************************************************************
42 **  Constants
43 *****************************************************************************/
44
45 static const UINT8  base_uuid[LEN_UUID_128] = {0xFB, 0x34, 0x9B, 0x5F, 0x80, 0x00, 0x00, 0x80,
46     0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
47
48 static const BD_ADDR dummy_bda = {0,0,0,0,0,0};
49
50 /*******************************************************************************
51 **
52 ** Function         bta_gatt_convert_uuid16_to_uuid128
53 **
54 ** Description      Convert a 16 bits UUID to be an standard 128 bits one.
55 **
56 ** Returns          TRUE if two uuid match; FALSE otherwise.
57 **
58 *******************************************************************************/
59 void bta_gatt_convert_uuid16_to_uuid128(UINT8 uuid_128[LEN_UUID_128], UINT16 uuid_16)
60 {
61     UINT8   *p = &uuid_128[LEN_UUID_128 - 4];
62
63     memcpy (uuid_128, base_uuid, LEN_UUID_128);
64
65     UINT16_TO_STREAM(p, uuid_16);
66 }
67 /*******************************************************************************
68 **
69 ** Function         bta_gattc_uuid_compare
70 **
71 ** Description      Compare two UUID to see if they are the same.
72 **
73 ** Returns          TRUE if two uuid match; FALSE otherwise.
74 **
75 *******************************************************************************/
76 BOOLEAN bta_gattc_uuid_compare (const tBT_UUID *p_src, const tBT_UUID *p_tar, BOOLEAN is_precise)
77 {
78     UINT8  su[LEN_UUID_128], tu[LEN_UUID_128];
79     const UINT8  *ps, *pt;
80
81     /* any of the UUID is unspecified */
82     if (p_src == 0 || p_tar == 0)
83     {
84         if (is_precise)
85             return FALSE;
86         else
87             return TRUE;
88     }
89
90     /* If both are 16-bit, we can do a simple compare */
91     if (p_src->len == 2 && p_tar->len == 2)
92     {
93         return p_src->uu.uuid16 == p_tar->uu.uuid16;
94     }
95
96     /* One or both of the UUIDs is 128-bit */
97     if (p_src->len == LEN_UUID_16)
98     {
99         /* convert a 16 bits UUID to 128 bits value */
100         bta_gatt_convert_uuid16_to_uuid128(su, p_src->uu.uuid16);
101         ps = su;
102     }
103     else
104         ps = p_src->uu.uuid128;
105
106     if (p_tar->len == LEN_UUID_16)
107     {
108         /* convert a 16 bits UUID to 128 bits value */
109         bta_gatt_convert_uuid16_to_uuid128(tu, p_tar->uu.uuid16);
110         pt = tu;
111     }
112     else
113         pt = p_tar->uu.uuid128;
114
115     return(memcmp(ps, pt, LEN_UUID_128) == 0);
116 }
117
118 /*******************************************************************************
119 **
120 ** Function         bta_gattc_cl_get_regcb
121 **
122 ** Description      get registration control block by client interface.
123 **
124 ** Returns          pointer to the regcb
125 **
126 *******************************************************************************/
127 tBTA_GATTC_RCB * bta_gattc_cl_get_regcb(UINT8 client_if)
128 {
129     UINT8   i = 0;
130     tBTA_GATTC_RCB  *p_clrcb = &bta_gattc_cb.cl_rcb[0];
131
132     for (i = 0; i < BTA_GATTC_CL_MAX; i ++, p_clrcb ++)
133     {
134         if (p_clrcb->in_use &&
135             p_clrcb->client_if == client_if)
136             return p_clrcb;
137     }
138     return NULL;
139 }
140 /*******************************************************************************
141 **
142 ** Function         bta_gattc_num_reg_app
143 **
144 ** Description      find the number of registered application.
145 **
146 ** Returns          pointer to the regcb
147 **
148 *******************************************************************************/
149 UINT8 bta_gattc_num_reg_app(void)
150 {
151     UINT8   i = 0, j = 0;
152
153     for (i = 0; i < BTA_GATTC_CL_MAX; i ++)
154     {
155         if (bta_gattc_cb.cl_rcb[i].in_use)
156             j ++;
157     }
158     return j;
159 }
160 /*******************************************************************************
161 **
162 ** Function         bta_gattc_find_clcb_by_cif
163 **
164 ** Description      get clcb by client interface and remote bd adddress
165 **
166 ** Returns          pointer to the clcb
167 **
168 *******************************************************************************/
169 tBTA_GATTC_CLCB * bta_gattc_find_clcb_by_cif (UINT8 client_if, BD_ADDR remote_bda,
170                                               tBTA_TRANSPORT transport)
171 {
172     tBTA_GATTC_CLCB *p_clcb = &bta_gattc_cb.clcb[0];
173     UINT8   i;
174
175     for (i = 0; i < BTA_GATTC_CLCB_MAX; i ++, p_clcb ++)
176     {
177         if (p_clcb->in_use &&
178             p_clcb->p_rcb->client_if == client_if &&
179             p_clcb->transport == transport &&
180             bdcmp(p_clcb->bda, remote_bda) == 0)
181             return p_clcb;
182     }
183     return NULL;
184 }
185 /*******************************************************************************
186 **
187 ** Function         bta_gattc_find_clcb_by_conn_id
188 **
189 ** Description      get clcb by connection ID
190 **
191 ** Returns          pointer to the clcb
192 **
193 *******************************************************************************/
194 tBTA_GATTC_CLCB * bta_gattc_find_clcb_by_conn_id (UINT16 conn_id)
195 {
196     tBTA_GATTC_CLCB *p_clcb = &bta_gattc_cb.clcb[0];
197     UINT8 i;
198
199     for (i = 0; i < BTA_GATTC_CLCB_MAX; i ++, p_clcb ++)
200     {
201         if (p_clcb->in_use &&
202             p_clcb->bta_conn_id == conn_id)
203             return p_clcb;
204     }
205     return NULL;
206 }
207
208 /*******************************************************************************
209 **
210 ** Function         bta_gattc_clcb_alloc
211 **
212 ** Description      allocate CLCB
213 **
214 ** Returns          pointer to the clcb
215 **
216 *******************************************************************************/
217 tBTA_GATTC_CLCB * bta_gattc_clcb_alloc(tBTA_GATTC_IF client_if, BD_ADDR remote_bda,
218                                        tBTA_TRANSPORT transport)
219 {
220     UINT8               i_clcb = 0;
221     tBTA_GATTC_CLCB     *p_clcb = NULL;
222
223     for (i_clcb = 0; i_clcb < BTA_GATTC_CLCB_MAX; i_clcb++)
224     {
225         if (!bta_gattc_cb.clcb[i_clcb].in_use)
226         {
227 #if BTA_GATT_DEBUG == TRUE
228             APPL_TRACE_DEBUG("bta_gattc_clcb_alloc: found clcb[%d] available",i_clcb);
229 #endif
230             p_clcb                  = &bta_gattc_cb.clcb[i_clcb];
231             p_clcb->in_use          = TRUE;
232             p_clcb->status          = BTA_GATT_OK;
233             p_clcb->transport       = transport;
234             bdcpy(p_clcb->bda, remote_bda);
235
236             p_clcb->p_rcb = bta_gattc_cl_get_regcb(client_if);
237
238             if ((p_clcb->p_srcb = bta_gattc_find_srcb(remote_bda)) == NULL)
239                 p_clcb->p_srcb      = bta_gattc_srcb_alloc(remote_bda);
240
241             if (p_clcb->p_rcb != NULL && p_clcb->p_srcb != NULL)
242             {
243                 p_clcb->p_srcb->num_clcb ++;
244                 p_clcb->p_rcb->num_clcb ++;
245             }
246             else
247             {
248                 /* release this clcb if clcb or srcb allocation failed */
249                 p_clcb->in_use = FALSE;
250                 p_clcb = NULL;
251             }
252             break;
253         }
254     }
255     return p_clcb;
256 }
257 /*******************************************************************************
258 **
259 ** Function         bta_gattc_find_alloc_clcb
260 **
261 ** Description      find or allocate CLCB if not found.
262 **
263 ** Returns          pointer to the clcb
264 **
265 *******************************************************************************/
266 tBTA_GATTC_CLCB *bta_gattc_find_alloc_clcb(tBTA_GATTC_IF client_if, BD_ADDR remote_bda,
267                                            tBTA_TRANSPORT transport)
268 {
269     tBTA_GATTC_CLCB *p_clcb ;
270
271     if ((p_clcb = bta_gattc_find_clcb_by_cif(client_if, remote_bda, transport)) == NULL)
272     {
273         p_clcb = bta_gattc_clcb_alloc(client_if, remote_bda, transport);
274     }
275     return p_clcb;
276 }
277
278 /*******************************************************************************
279 **
280 ** Function         bta_gattc_clcb_dealloc
281 **
282 ** Description      Deallocte a clcb
283 **
284 ** Returns          pointer to the clcb
285 **
286 *******************************************************************************/
287 void bta_gattc_clcb_dealloc(tBTA_GATTC_CLCB *p_clcb)
288 {
289     tBTA_GATTC_SERV     *p_srcb = NULL;
290
291     if (p_clcb)
292     {
293         p_srcb = p_clcb->p_srcb;
294         if (p_srcb->num_clcb)
295             p_srcb->num_clcb --;
296
297         if (p_clcb->p_rcb->num_clcb)
298             p_clcb->p_rcb->num_clcb --;
299
300         /* if the srcb is no longer needed, reset the state */
301         if ( p_srcb->num_clcb == 0)
302         {
303             p_srcb->connected = FALSE;
304             p_srcb->state = BTA_GATTC_SERV_IDLE;
305             p_srcb->mtu = 0;
306         }
307
308         osi_free_and_reset((void **)&p_clcb->p_q_cmd);
309         memset(p_clcb, 0, sizeof(tBTA_GATTC_CLCB));
310     } else {
311         APPL_TRACE_ERROR("bta_gattc_clcb_dealloc p_clcb=NULL");
312     }
313 }
314
315 /*******************************************************************************
316 **
317 ** Function         bta_gattc_find_srcb
318 **
319 ** Description      find server cache by remote bd address currently in use
320 **
321 ** Returns          pointer to the server cache.
322 **
323 *******************************************************************************/
324 tBTA_GATTC_SERV * bta_gattc_find_srcb(BD_ADDR bda)
325 {
326     tBTA_GATTC_SERV *p_srcb = &bta_gattc_cb.known_server[0];
327     UINT8   i;
328
329     for (i = 0; i < BTA_GATTC_KNOWN_SR_MAX; i ++, p_srcb ++)
330     {
331         if (p_srcb->in_use && bdcmp(p_srcb->server_bda, bda) == 0)
332             return p_srcb;
333     }
334     return NULL;
335 }
336
337 /*******************************************************************************
338 **
339 ** Function         bta_gattc_find_srvr_cache
340 **
341 ** Description      find server cache by remote bd address
342 **
343 ** Returns          pointer to the server cache.
344 **
345 *******************************************************************************/
346 tBTA_GATTC_SERV * bta_gattc_find_srvr_cache(BD_ADDR bda)
347 {
348     tBTA_GATTC_SERV *p_srcb = &bta_gattc_cb.known_server[0];
349     UINT8   i;
350
351     for (i = 0; i < BTA_GATTC_KNOWN_SR_MAX; i ++, p_srcb ++)
352     {
353         if (bdcmp(p_srcb->server_bda, bda) == 0)
354             return p_srcb;
355     }
356     return NULL;
357 }
358 /*******************************************************************************
359 **
360 ** Function         bta_gattc_find_scb_by_cid
361 **
362 ** Description      find server control block by connection ID
363 **
364 ** Returns          pointer to the server cache.
365 **
366 *******************************************************************************/
367 tBTA_GATTC_SERV * bta_gattc_find_scb_by_cid (UINT16 conn_id)
368 {
369     tBTA_GATTC_CLCB *p_clcb = bta_gattc_find_clcb_by_conn_id(conn_id);
370
371     if (p_clcb)
372         return p_clcb->p_srcb;
373     else
374         return NULL;
375 }
376 /*******************************************************************************
377 **
378 ** Function         bta_gattc_srcb_alloc
379 **
380 ** Description      allocate server cache control block
381 **
382 ** Returns          pointer to the server cache.
383 **
384 *******************************************************************************/
385 tBTA_GATTC_SERV * bta_gattc_srcb_alloc(BD_ADDR bda)
386 {
387     tBTA_GATTC_SERV *p_tcb = &bta_gattc_cb.known_server[0],
388                              *p_recycle = NULL;
389     BOOLEAN         found = FALSE;
390     UINT8           i;
391
392     for (i = 0; i < BTA_GATTC_KNOWN_SR_MAX; i ++, p_tcb ++)
393     {
394         if (!p_tcb->in_use)
395         {
396             found = TRUE;
397             break;
398         }
399         else if (!p_tcb->connected)
400         {
401             p_recycle = p_tcb;
402         }
403     }
404
405     /* if not found, try to recycle one known device */
406     if (!found && !p_recycle)
407         p_tcb = NULL;
408     else if (!found && p_recycle)
409         p_tcb = p_recycle;
410
411     if (p_tcb != NULL)
412     {
413         if (p_tcb->p_srvc_cache != NULL)
414             list_free(p_tcb->p_srvc_cache);
415
416         osi_free_and_reset((void **)&p_tcb->p_srvc_list);
417         memset(p_tcb, 0 , sizeof(tBTA_GATTC_SERV));
418
419         p_tcb->in_use = TRUE;
420         bdcpy(p_tcb->server_bda, bda);
421     }
422     return p_tcb;
423 }
424 /*******************************************************************************
425 **
426 ** Function         bta_gattc_enqueue
427 **
428 ** Description      enqueue a client request in clcb.
429 **
430 ** Returns          success or failure.
431 **
432 *******************************************************************************/
433 BOOLEAN bta_gattc_enqueue(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
434 {
435
436     if (p_clcb->p_q_cmd == NULL)
437     {
438         p_clcb->p_q_cmd = p_data;
439         return TRUE;
440     }
441
442     APPL_TRACE_ERROR ("%s: already has a pending command!!", __func__);
443     /* skip the callback now. ----- need to send callback ? */
444     return FALSE;
445 }
446
447 /*******************************************************************************
448 **
449 ** Function         bta_gattc_check_notif_registry
450 **
451 ** Description      check if the service notificaition has been registered.
452 **
453 ** Returns
454 **
455 *******************************************************************************/
456 BOOLEAN bta_gattc_check_notif_registry(tBTA_GATTC_RCB  *p_clreg, tBTA_GATTC_SERV *p_srcb,
457                                        tBTA_GATTC_NOTIFY  *p_notify)
458 {
459     UINT8           i;
460
461     for (i = 0 ; i < BTA_GATTC_NOTIF_REG_MAX; i ++)
462     {
463         if (p_clreg->notif_reg[i].in_use &&
464             bdcmp(p_clreg->notif_reg[i].remote_bda, p_srcb->server_bda) == 0 &&
465             p_clreg->notif_reg[i].handle == p_notify->handle)
466         {
467             APPL_TRACE_DEBUG("Notification registered!");
468             return TRUE;
469         }
470     }
471     return FALSE;
472
473 }
474 /*******************************************************************************
475 **
476 ** Function         bta_gattc_clear_notif_registration
477 **
478 ** Description      Clear up the notification registration information by BD_ADDR.
479 **                  Where handle is between start_handle and end_handle, and
480 **                  start_handle and end_handle are boundaries of service
481 **                  containing characteristic.
482 **
483 ** Returns          None.
484 **
485 *******************************************************************************/
486 void bta_gattc_clear_notif_registration(tBTA_GATTC_SERV *p_srcb, UINT16 conn_id,
487                                         UINT16 start_handle, UINT16 end_handle)
488 {
489     BD_ADDR             remote_bda;
490     tBTA_GATTC_IF       gatt_if;
491     tBTA_GATTC_RCB      *p_clrcb ;
492     UINT8       i;
493     tGATT_TRANSPORT     transport;
494     UINT16              handle;
495
496     if (GATT_GetConnectionInfor(conn_id, &gatt_if, remote_bda, &transport)) {
497         if ((p_clrcb = bta_gattc_cl_get_regcb(gatt_if)) != NULL) {
498             for (i = 0 ; i < BTA_GATTC_NOTIF_REG_MAX; i ++) {
499                 if (p_clrcb->notif_reg[i].in_use &&
500                     !bdcmp(p_clrcb->notif_reg[i].remote_bda, remote_bda))
501
502                     /* It's enough to get service or characteristic handle, as
503                      * clear boundaries are always around service.
504                      */
505                     handle = p_clrcb->notif_reg[i].handle;
506                     if (handle >= start_handle && handle <= end_handle)
507                         memset(&p_clrcb->notif_reg[i], 0, sizeof(tBTA_GATTC_NOTIF_REG));
508             }
509         }
510     } else {
511         APPL_TRACE_ERROR("can not clear indication/notif registration for unknown app");
512     }
513     return;
514 }
515
516 /*******************************************************************************
517 **
518 ** Function         bta_gattc_mark_bg_conn
519 **
520 ** Description      mark background connection status when a bg connection is initiated
521 **                  or terminated.
522 **
523 ** Returns          TRUE if success; FALSE otherwise.
524 **
525 *******************************************************************************/
526 BOOLEAN bta_gattc_mark_bg_conn (tBTA_GATTC_IF client_if,  BD_ADDR_PTR remote_bda_ptr,
527                                 BOOLEAN add, BOOLEAN is_listen)
528 {
529     tBTA_GATTC_BG_TCK   *p_bg_tck = &bta_gattc_cb.bg_track[0];
530     UINT8   i = 0;
531     tBTA_GATTC_CIF_MASK  *p_cif_mask;
532
533     for (i = 0; i < BTA_GATTC_KNOWN_SR_MAX; i ++, p_bg_tck ++)
534     {
535         if (p_bg_tck->in_use &&
536             ((remote_bda_ptr != NULL && bdcmp(p_bg_tck->remote_bda, remote_bda_ptr) == 0) ||
537             (remote_bda_ptr == NULL && bdcmp(p_bg_tck->remote_bda, dummy_bda) == 0)))
538         {
539              p_cif_mask = is_listen ? &p_bg_tck->cif_adv_mask : &p_bg_tck->cif_mask;
540
541             if (add)
542                 /* mask on the cif bit */
543                 *p_cif_mask |= (1 <<(client_if - 1));
544             else
545             {
546                 if (client_if != 0)
547                     *p_cif_mask &= (~(1 <<(client_if - 1)));
548                 else
549                     *p_cif_mask = 0;
550             }
551             /* no BG connection for this device, make it available */
552             if (p_bg_tck->cif_mask == 0 && p_bg_tck->cif_adv_mask == 0)
553             {
554                 memset(p_bg_tck, 0, sizeof(tBTA_GATTC_BG_TCK));
555             }
556             return TRUE;
557         }
558     }
559     if (!add)
560     {
561         if (remote_bda_ptr)
562         {
563             bdstr_t bdstr = {0};
564             APPL_TRACE_ERROR("%s unable to find the bg connection mask for: %s", __func__,
565                 bdaddr_to_string((bt_bdaddr_t *)remote_bda_ptr, bdstr, sizeof(bdstr)));
566         }
567         return FALSE;
568     }
569     else /* adding a new device mask */
570     {
571         for (i = 0, p_bg_tck = &bta_gattc_cb.bg_track[0];
572              i < BTA_GATTC_KNOWN_SR_MAX; i ++, p_bg_tck ++)
573         {
574             if (!p_bg_tck->in_use)
575             {
576                 p_bg_tck->in_use = TRUE;
577                 if (remote_bda_ptr)
578                     bdcpy(p_bg_tck->remote_bda, remote_bda_ptr);
579                 else
580                     bdcpy(p_bg_tck->remote_bda, dummy_bda);
581
582                 p_cif_mask = is_listen ? &p_bg_tck->cif_adv_mask : &p_bg_tck->cif_mask;
583
584                 *p_cif_mask = (1 <<(client_if - 1));
585                 return TRUE;
586             }
587         }
588         APPL_TRACE_ERROR("no available space to mark the bg connection status");
589         return FALSE;
590     }
591 }
592 /*******************************************************************************
593 **
594 ** Function         bta_gattc_check_bg_conn
595 **
596 ** Description      check if this is a background connection background connection.
597 **
598 ** Returns          TRUE if success; FALSE otherwise.
599 **
600 *******************************************************************************/
601 BOOLEAN bta_gattc_check_bg_conn (tBTA_GATTC_IF client_if,  BD_ADDR remote_bda, UINT8 role)
602 {
603     tBTA_GATTC_BG_TCK   *p_bg_tck = &bta_gattc_cb.bg_track[0];
604     UINT8       i = 0;
605     BOOLEAN     is_bg_conn = FALSE;
606
607     for (i = 0; i < BTA_GATTC_KNOWN_SR_MAX && !is_bg_conn; i ++, p_bg_tck ++)
608     {
609         if (p_bg_tck->in_use &&
610             (bdcmp(p_bg_tck->remote_bda, remote_bda) == 0 ||
611              bdcmp(p_bg_tck->remote_bda, dummy_bda) == 0))
612         {
613             if (((p_bg_tck->cif_mask &(1 <<(client_if - 1))) != 0) &&
614                 role == HCI_ROLE_MASTER)
615                 is_bg_conn = TRUE;
616
617             if (((p_bg_tck->cif_adv_mask &(1 <<(client_if - 1))) != 0) &&
618                 role == HCI_ROLE_SLAVE)
619                 is_bg_conn = TRUE;
620         }
621     }
622     return is_bg_conn;
623 }
624 /*******************************************************************************
625 **
626 ** Function         bta_gattc_send_open_cback
627 **
628 ** Description      send open callback
629 **
630 ** Returns
631 **
632 *******************************************************************************/
633 void bta_gattc_send_open_cback( tBTA_GATTC_RCB *p_clreg, tBTA_GATT_STATUS status,
634                                 BD_ADDR remote_bda, UINT16 conn_id,
635                                 tBTA_TRANSPORT transport, UINT16 mtu)
636 {
637     tBTA_GATTC      cb_data;
638
639     if (p_clreg->p_cback)
640     {
641         memset(&cb_data, 0, sizeof(tBTA_GATTC));
642
643         cb_data.open.status = status;
644         cb_data.open.client_if = p_clreg->client_if;
645         cb_data.open.conn_id = conn_id;
646         cb_data.open.mtu = mtu;
647         cb_data.open.transport = transport;
648         bdcpy(cb_data.open.remote_bda, remote_bda);
649
650         (*p_clreg->p_cback)(BTA_GATTC_OPEN_EVT, &cb_data);
651     }
652 }
653 /*******************************************************************************
654 **
655 ** Function         bta_gattc_conn_alloc
656 **
657 ** Description      allocate connection tracking spot
658 **
659 ** Returns          pointer to the clcb
660 **
661 *******************************************************************************/
662 tBTA_GATTC_CONN * bta_gattc_conn_alloc(BD_ADDR remote_bda)
663 {
664     UINT8               i_conn = 0;
665     tBTA_GATTC_CONN     *p_conn = &bta_gattc_cb.conn_track[0];
666
667     for (i_conn = 0; i_conn < BTA_GATTC_CONN_MAX; i_conn++, p_conn ++)
668     {
669         if (!p_conn->in_use)
670         {
671 #if BTA_GATT_DEBUG == TRUE
672             APPL_TRACE_DEBUG("bta_gattc_conn_alloc: found conn_track[%d] available",i_conn);
673 #endif
674             p_conn->in_use          = TRUE;
675             bdcpy(p_conn->remote_bda, remote_bda);
676             return p_conn;
677         }
678     }
679     return NULL;
680 }
681
682 /*******************************************************************************
683 **
684 ** Function         bta_gattc_conn_find
685 **
686 ** Description      allocate connection tracking spot
687 **
688 ** Returns          pointer to the clcb
689 **
690 *******************************************************************************/
691 tBTA_GATTC_CONN * bta_gattc_conn_find(BD_ADDR remote_bda)
692 {
693     UINT8               i_conn = 0;
694     tBTA_GATTC_CONN     *p_conn = &bta_gattc_cb.conn_track[0];
695
696     for (i_conn = 0; i_conn < BTA_GATTC_CONN_MAX; i_conn++, p_conn ++)
697     {
698         if (p_conn->in_use && bdcmp(remote_bda, p_conn->remote_bda) == 0)
699         {
700 #if BTA_GATT_DEBUG == TRUE
701             APPL_TRACE_DEBUG("bta_gattc_conn_find: found conn_track[%d] matched",i_conn);
702 #endif
703             return p_conn;
704         }
705     }
706     return NULL;
707 }
708
709 /*******************************************************************************
710 **
711 ** Function         bta_gattc_conn_find_alloc
712 **
713 ** Description      find or allocate connection tracking spot
714 **
715 ** Returns          pointer to the clcb
716 **
717 *******************************************************************************/
718 tBTA_GATTC_CONN * bta_gattc_conn_find_alloc(BD_ADDR remote_bda)
719 {
720     tBTA_GATTC_CONN     *p_conn = bta_gattc_conn_find (remote_bda);
721
722     if (p_conn == NULL)
723     {
724         p_conn = bta_gattc_conn_alloc(remote_bda);
725     }
726     return p_conn;
727 }
728
729 /*******************************************************************************
730 **
731 ** Function         bta_gattc_conn_dealloc
732 **
733 ** Description      de-allocate connection tracking spot
734 **
735 ** Returns          pointer to the clcb
736 **
737 *******************************************************************************/
738 BOOLEAN bta_gattc_conn_dealloc(BD_ADDR remote_bda)
739 {
740     tBTA_GATTC_CONN     *p_conn = bta_gattc_conn_find (remote_bda);
741
742     if (p_conn != NULL)
743     {
744         p_conn->in_use = FALSE;
745         memset(p_conn->remote_bda, 0, BD_ADDR_LEN);
746         return TRUE;
747     }
748     return FALSE;
749 }
750
751 /*******************************************************************************
752 **
753 ** Function         bta_gattc_find_int_conn_clcb
754 **
755 ** Description      try to locate a clcb when an internal connecion event arrives.
756 **
757 ** Returns          pointer to the clcb
758 **
759 *******************************************************************************/
760 tBTA_GATTC_CLCB * bta_gattc_find_int_conn_clcb(tBTA_GATTC_DATA *p_msg)
761 {
762     tBTA_GATTC_CLCB *p_clcb = NULL;
763
764     if (p_msg->int_conn.role == HCI_ROLE_SLAVE)
765         bta_gattc_conn_find_alloc(p_msg->int_conn.remote_bda);
766
767     /* try to locate a logic channel */
768     if ((p_clcb = bta_gattc_find_clcb_by_cif(p_msg->int_conn.client_if,
769                                              p_msg->int_conn.remote_bda,
770                                              p_msg->int_conn.transport)) == NULL)
771     {
772         /* for a background connection or listening connection */
773         if (/*p_msg->int_conn.role == HCI_ROLE_SLAVE ||  */
774             bta_gattc_check_bg_conn(p_msg->int_conn.client_if,
775                                     p_msg->int_conn.remote_bda,
776                                     p_msg->int_conn.role))
777         {
778             /* allocate a new channel */
779             p_clcb = bta_gattc_clcb_alloc(p_msg->int_conn.client_if,
780                                           p_msg->int_conn.remote_bda,
781                                           p_msg->int_conn.transport);
782         }
783     }
784     return p_clcb;
785 }
786
787 /*******************************************************************************
788 **
789 ** Function         bta_gattc_find_int_disconn_clcb
790 **
791 ** Description      try to locate a clcb when an internal disconnect callback arrives.
792 **
793 ** Returns          pointer to the clcb
794 **
795 *******************************************************************************/
796 tBTA_GATTC_CLCB * bta_gattc_find_int_disconn_clcb(tBTA_GATTC_DATA *p_msg)
797 {
798     tBTA_GATTC_CLCB         *p_clcb = NULL;
799
800     bta_gattc_conn_dealloc(p_msg->int_conn.remote_bda);
801     if ((p_clcb = bta_gattc_find_clcb_by_conn_id(p_msg->int_conn.hdr.layer_specific)) == NULL)
802     {
803         /* connection attempt failed, send connection callback event */
804         p_clcb = bta_gattc_find_clcb_by_cif(p_msg->int_conn.client_if,
805                                             p_msg->int_conn.remote_bda,
806                                             p_msg->int_conn.transport);
807     }
808     if (p_clcb == NULL)
809     {
810         APPL_TRACE_DEBUG(" disconnection ID: [%d] not used by BTA",
811             p_msg->int_conn.hdr.layer_specific);
812     }
813     return p_clcb;
814 }
815
816 #endif /* BTA_GATT_INCLUDED */