1 /******************************************************************************
2  *
3  *  Copyright (C) 2003-2012 Broadcom Corporation
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18 
19 /******************************************************************************
20  *
21  *  This file contains the GATT client utility function.
22  *
23  ******************************************************************************/
24 
25 #include "common/bt_target.h"
26 
27 #if defined(GATTC_INCLUDED) && (GATTC_INCLUDED == TRUE)
28 
29 #include <string.h>
30 
31 #include "device/bdaddr.h"
32 // #include "btif/include/btif_util.h"
33 #include "bta/utl.h"
34 #include "bta/bta_sys.h"
35 #include "bta_gattc_int.h"
36 #include "stack/l2c_api.h"
37 #include "osi/allocator.h"
38 
39 /*****************************************************************************
40 **  Constants
41 *****************************************************************************/
42 
43 
44 static const UINT8  base_uuid[LEN_UUID_128] = {0xFB, 0x34, 0x9B, 0x5F, 0x80, 0x00, 0x00, 0x80,
45                                                0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
46                                               };
47 
48 static const BD_ADDR dummy_bda = {0, 0, 0, 0, 0, 0};
49 
50 #define GATTC_COMMAND_QUEUE_SIZE_MAX    30
51 
52 /*******************************************************************************
53 **
54 ** Function         bta_gatt_convert_uuid16_to_uuid128
55 **
56 ** Description      Convert a 16 bits UUID to be an standard 128 bits one.
57 **
58 ** Returns          TRUE if two uuid match; FALSE otherwise.
59 **
60 *******************************************************************************/
bta_gatt_convert_uuid16_to_uuid128(UINT8 uuid_128[LEN_UUID_128],UINT16 uuid_16)61 void bta_gatt_convert_uuid16_to_uuid128(UINT8 uuid_128[LEN_UUID_128], UINT16 uuid_16)
62 {
63     UINT8   *p = &uuid_128[LEN_UUID_128 - 4];
64 
65     memcpy (uuid_128, base_uuid, LEN_UUID_128);
66 
67     UINT16_TO_STREAM(p, uuid_16);
68 }
69 /*******************************************************************************
70 **
71 ** Function         bta_gattc_uuid_compare
72 **
73 ** Description      Compare two UUID to see if they are the same.
74 **
75 ** Returns          TRUE if two uuid match; FALSE otherwise.
76 **
77 *******************************************************************************/
bta_gattc_uuid_compare(const tBT_UUID * p_src,const tBT_UUID * p_tar,BOOLEAN is_precise)78 BOOLEAN bta_gattc_uuid_compare (const tBT_UUID *p_src, const tBT_UUID *p_tar, BOOLEAN is_precise)
79 {
80     UINT8  su[LEN_UUID_128], tu[LEN_UUID_128];
81     const UINT8  *ps, *pt;
82 
83     /* any of the UUID is unspecified */
84     if (p_src == 0 || p_tar == 0) {
85         if (is_precise) {
86             return FALSE;
87         } else {
88             return TRUE;
89         }
90     }
91 
92     /* If both are 16-bit, we can do a simple compare */
93     if (p_src->len == 2 && p_tar->len == 2) {
94         return p_src->uu.uuid16 == p_tar->uu.uuid16;
95     }
96 
97     /* One or both of the UUIDs is 128-bit */
98     if (p_src->len == LEN_UUID_16) {
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     } else {
103         ps = p_src->uu.uuid128;
104     }
105 
106     if (p_tar->len == LEN_UUID_16) {
107         /* convert a 16 bits UUID to 128 bits value */
108         bta_gatt_convert_uuid16_to_uuid128(tu, p_tar->uu.uuid16);
109         pt = tu;
110     } else {
111         pt = p_tar->uu.uuid128;
112     }
113 
114     return (memcmp(ps, pt, LEN_UUID_128) == 0);
115 }
116 
117 /*******************************************************************************
118 **
119 ** Function         bta_gattc_cl_get_regcb
120 **
121 ** Description      get registration control block by client interface.
122 **
123 ** Returns          pointer to the regcb
124 **
125 *******************************************************************************/
bta_gattc_cl_get_regcb(UINT8 client_if)126 tBTA_GATTC_RCB *bta_gattc_cl_get_regcb(UINT8 client_if)
127 {
128     UINT8   i = 0;
129     tBTA_GATTC_RCB  *p_clrcb = &bta_gattc_cb.cl_rcb[0];
130 
131     for (i = 0; i < BTA_GATTC_CL_MAX; i ++, p_clrcb ++) {
132         if (p_clrcb->in_use &&
133                 p_clrcb->client_if == client_if) {
134             return p_clrcb;
135         }
136     }
137     return NULL;
138 }
139 /*******************************************************************************
140 **
141 ** Function         bta_gattc_num_reg_app
142 **
143 ** Description      find the number of registered application.
144 **
145 ** Returns          pointer to the regcb
146 **
147 *******************************************************************************/
bta_gattc_num_reg_app(void)148 UINT8 bta_gattc_num_reg_app(void)
149 {
150     UINT8   i = 0, j = 0;
151 
152     for (i = 0; i < BTA_GATTC_CL_MAX; i ++) {
153         if (bta_gattc_cb.cl_rcb[i].in_use) {
154             j ++;
155         }
156     }
157     return j;
158 }
159 /*******************************************************************************
160 **
161 ** Function         bta_gattc_find_clcb_by_cif
162 **
163 ** Description      get clcb by client interface and remote bd adddress
164 **
165 ** Returns          pointer to the clcb
166 **
167 *******************************************************************************/
bta_gattc_find_clcb_by_cif(UINT8 client_if,BD_ADDR remote_bda,tBTA_TRANSPORT transport)168 tBTA_GATTC_CLCB *bta_gattc_find_clcb_by_cif (UINT8 client_if, BD_ADDR remote_bda,
169         tBTA_TRANSPORT transport)
170 {
171     tBTA_GATTC_CLCB *p_clcb = &bta_gattc_cb.clcb[0];
172     UINT8   i;
173 
174     for (i = 0; i < BTA_GATTC_CLCB_MAX; i ++, p_clcb ++) {
175         if (p_clcb->in_use &&
176                 p_clcb->p_rcb->client_if == client_if &&
177                 p_clcb->transport == transport &&
178                 bdcmp(p_clcb->bda, remote_bda) == 0) {
179             return p_clcb;
180         }
181     }
182     return NULL;
183 }
184 /*******************************************************************************
185 **
186 ** Function         bta_gattc_find_clcb_by_conn_id
187 **
188 ** Description      get clcb by connection ID
189 **
190 ** Returns          pointer to the clcb
191 **
192 *******************************************************************************/
bta_gattc_find_clcb_by_conn_id(UINT16 conn_id)193 tBTA_GATTC_CLCB *bta_gattc_find_clcb_by_conn_id (UINT16 conn_id)
194 {
195     tBTA_GATTC_CLCB *p_clcb = &bta_gattc_cb.clcb[0];
196     UINT8 i;
197 
198     for (i = 0; i < BTA_GATTC_CLCB_MAX; i ++, p_clcb ++) {
199         if (p_clcb->in_use &&
200                 p_clcb->bta_conn_id == conn_id) {
201             return p_clcb;
202         }
203     }
204     return NULL;
205 }
206 
207 /*******************************************************************************
208 **
209 ** Function         bta_gattc_clcb_alloc
210 **
211 ** Description      allocate CLCB
212 **
213 ** Returns          pointer to the clcb
214 **
215 *******************************************************************************/
bta_gattc_clcb_alloc(tBTA_GATTC_IF client_if,BD_ADDR remote_bda,tBTA_TRANSPORT transport)216 tBTA_GATTC_CLCB *bta_gattc_clcb_alloc(tBTA_GATTC_IF client_if, BD_ADDR remote_bda,
217                                       tBTA_TRANSPORT transport)
218 {
219     UINT8               i_clcb = 0;
220     tBTA_GATTC_CLCB     *p_clcb = NULL;
221 
222     for (i_clcb = 0; i_clcb < BTA_GATTC_CLCB_MAX; i_clcb++) {
223         if (!bta_gattc_cb.clcb[i_clcb].in_use) {
224 #if BTA_GATT_DEBUG == TRUE
225             APPL_TRACE_DEBUG("bta_gattc_clcb_alloc: found clcb[%d] available", i_clcb);
226 #endif
227             p_clcb                  = &bta_gattc_cb.clcb[i_clcb];
228             p_clcb->in_use          = TRUE;
229             p_clcb->status          = BTA_GATT_OK;
230             p_clcb->transport       = transport;
231             bdcpy(p_clcb->bda, remote_bda);
232             p_clcb->searched_service_source = BTA_GATTC_SERVICE_INFO_FROM_UNKNOWN;
233             p_clcb->p_rcb = bta_gattc_cl_get_regcb(client_if);
234             if (p_clcb->p_cmd_list == NULL) {
235                 p_clcb->p_cmd_list = list_new(osi_free_func);
236             }
237             if ((p_clcb->p_srcb = bta_gattc_find_srcb(remote_bda)) == NULL) {
238                 p_clcb->p_srcb      = bta_gattc_srcb_alloc(remote_bda);
239             }
240 
241             if (p_clcb->p_rcb != NULL && p_clcb->p_srcb != NULL) {
242                 p_clcb->p_srcb->num_clcb ++;
243                 p_clcb->p_rcb->num_clcb ++;
244             } else {
245                 /* release this clcb if clcb or srcb allocation failed */
246                 p_clcb->in_use = FALSE;
247                 p_clcb = NULL;
248             }
249             break;
250         }
251     }
252     return p_clcb;
253 }
254 /*******************************************************************************
255 **
256 ** Function         bta_gattc_find_alloc_clcb
257 **
258 ** Description      find or allocate CLCB if not found.
259 **
260 ** Returns          pointer to the clcb
261 **
262 *******************************************************************************/
bta_gattc_find_alloc_clcb(tBTA_GATTC_IF client_if,BD_ADDR remote_bda,tBTA_TRANSPORT transport)263 tBTA_GATTC_CLCB *bta_gattc_find_alloc_clcb(tBTA_GATTC_IF client_if, BD_ADDR remote_bda,
264         tBTA_TRANSPORT transport)
265 {
266     tBTA_GATTC_CLCB *p_clcb ;
267 
268     if ((p_clcb = bta_gattc_find_clcb_by_cif(client_if, remote_bda, transport)) == NULL) {
269         p_clcb = bta_gattc_clcb_alloc(client_if, remote_bda, transport);
270     }
271     return p_clcb;
272 }
273 
274 /*******************************************************************************
275 **
276 ** Function         bta_gattc_clcb_dealloc
277 **
278 ** Description      Deallocte a clcb
279 **
280 ** Returns          pointer to the clcb
281 **
282 *******************************************************************************/
bta_gattc_clcb_dealloc(tBTA_GATTC_CLCB * p_clcb)283 void bta_gattc_clcb_dealloc(tBTA_GATTC_CLCB *p_clcb)
284 {
285     tBTA_GATTC_SERV     *p_srcb = NULL;
286 
287     if (p_clcb) {
288         p_srcb = p_clcb->p_srcb;
289         if (p_srcb->num_clcb) {
290             p_srcb->num_clcb --;
291         }
292 
293         if (p_clcb->p_rcb->num_clcb) {
294             p_clcb->p_rcb->num_clcb --;
295         }
296 
297         /* if the srcb is no longer needed, reset the state */
298         if ( p_srcb->num_clcb == 0) {
299             p_srcb->connected = FALSE;
300             p_srcb->state = BTA_GATTC_SERV_IDLE;
301             p_srcb->mtu = 0;
302 
303             /* clean up cache */
304             if (p_srcb->p_srvc_cache) {
305                 list_free(p_srcb->p_srvc_cache);
306                 p_srcb->p_srvc_cache = NULL;
307             }
308         }
309 
310         if ( p_clcb->p_q_cmd != NULL && !list_contains(p_clcb->p_cmd_list, p_clcb->p_q_cmd)){
311             osi_free(p_clcb->p_q_cmd);
312             p_clcb->p_q_cmd = NULL;
313         }
314         // don't forget to clear the command queue before dealloc the clcb.
315         list_clear(p_clcb->p_cmd_list);
316         osi_free((void *)p_clcb->p_cmd_list);
317         p_clcb->p_cmd_list = NULL;
318         //osi_free_and_reset((void **)&p_clcb->p_q_cmd);
319         memset(p_clcb, 0, sizeof(tBTA_GATTC_CLCB));
320     } else {
321         APPL_TRACE_ERROR("bta_gattc_clcb_dealloc p_clcb=NULL");
322     }
323 }
324 
325 /*******************************************************************************
326 **
327 ** Function         bta_gattc_find_srcb
328 **
329 ** Description      find server cache by remote bd address currently in use
330 **
331 ** Returns          pointer to the server cache.
332 **
333 *******************************************************************************/
bta_gattc_find_srcb(BD_ADDR bda)334 tBTA_GATTC_SERV *bta_gattc_find_srcb(BD_ADDR bda)
335 {
336     tBTA_GATTC_SERV *p_srcb = &bta_gattc_cb.known_server[0];
337     UINT8   i;
338 
339     for (i = 0; i < BTA_GATTC_KNOWN_SR_MAX; i ++, p_srcb ++) {
340         if (p_srcb->in_use && bdcmp(p_srcb->server_bda, bda) == 0) {
341             return p_srcb;
342         }
343     }
344     return NULL;
345 }
346 
347 /*******************************************************************************
348 **
349 ** Function         bta_gattc_find_srvr_cache
350 **
351 ** Description      find server cache by remote bd address
352 **
353 ** Returns          pointer to the server cache.
354 **
355 *******************************************************************************/
bta_gattc_find_srvr_cache(BD_ADDR bda)356 tBTA_GATTC_SERV *bta_gattc_find_srvr_cache(BD_ADDR bda)
357 {
358     tBTA_GATTC_SERV *p_srcb = &bta_gattc_cb.known_server[0];
359     UINT8   i;
360 
361     for (i = 0; i < BTA_GATTC_KNOWN_SR_MAX; i ++, p_srcb ++) {
362         if (bdcmp(p_srcb->server_bda, bda) == 0) {
363             return p_srcb;
364         }
365     }
366     return NULL;
367 }
368 /*******************************************************************************
369 **
370 ** Function         bta_gattc_find_scb_by_cid
371 **
372 ** Description      find server control block by connection ID
373 **
374 ** Returns          pointer to the server cache.
375 **
376 *******************************************************************************/
bta_gattc_find_scb_by_cid(UINT16 conn_id)377 tBTA_GATTC_SERV *bta_gattc_find_scb_by_cid (UINT16 conn_id)
378 {
379     tBTA_GATTC_CLCB *p_clcb = bta_gattc_find_clcb_by_conn_id(conn_id);
380 
381     if (p_clcb) {
382         return p_clcb->p_srcb;
383     } else {
384         return NULL;
385     }
386 }
387 /*******************************************************************************
388 **
389 ** Function         bta_gattc_srcb_alloc
390 **
391 ** Description      allocate server cache control block
392 **
393 ** Returns          pointer to the server cache.
394 **
395 *******************************************************************************/
bta_gattc_srcb_alloc(BD_ADDR bda)396 tBTA_GATTC_SERV *bta_gattc_srcb_alloc(BD_ADDR bda)
397 {
398     tBTA_GATTC_SERV *p_tcb = &bta_gattc_cb.known_server[0],
399                      *p_recycle = NULL;
400     BOOLEAN         found = FALSE;
401     UINT8           i;
402 
403     for (i = 0; i < BTA_GATTC_KNOWN_SR_MAX; i ++, p_tcb ++) {
404         if (!p_tcb->in_use) {
405             found = TRUE;
406             break;
407         } else if (!p_tcb->connected) {
408             p_recycle = p_tcb;
409         }
410     }
411 
412     /* if not found, try to recycle one known device */
413     if (!found && !p_recycle) {
414         p_tcb = NULL;
415 	}
416     else if (!found && p_recycle) {
417         p_tcb = p_recycle;
418 	}
419 
420     if (p_tcb != NULL)
421     {
422         if (p_tcb->p_srvc_cache != NULL) {
423             list_free(p_tcb->p_srvc_cache);
424         }
425         osi_free(p_tcb->p_srvc_list);
426         p_tcb->p_srvc_list = NULL;
427         //osi_free_and_reset((void **)&p_tcb->p_srvc_list);
428         memset(p_tcb, 0 , sizeof(tBTA_GATTC_SERV));
429 
430         p_tcb->in_use = TRUE;
431         bdcpy(p_tcb->server_bda, bda);
432     }
433     return p_tcb;
434 }
435 
bta_gattc_has_prepare_command_in_queue(tBTA_GATTC_CLCB * p_clcb)436 static BOOLEAN bta_gattc_has_prepare_command_in_queue(tBTA_GATTC_CLCB *p_clcb)
437 {
438     assert(p_clcb != NULL);
439 
440     for(list_node_t *sn = list_begin(p_clcb->p_cmd_list);
441         sn != list_end(p_clcb->p_cmd_list); sn = list_next(sn)) {
442 
443         tBTA_GATTC_DATA *cmd_data = (tBTA_GATTC_DATA *)list_node(sn);
444         if (cmd_data != NULL && ((cmd_data->hdr.event == BTA_GATTC_API_WRITE_EVT &&
445             cmd_data->api_write.write_type == BTA_GATTC_WRITE_PREPARE) ||
446             cmd_data->hdr.event == BTA_GATTC_API_EXEC_EVT)) {
447             return TRUE;
448         }
449     }
450 
451     return FALSE;
452 }
453 /*******************************************************************************
454 **
455 ** Function         bta_gattc_enqueue
456 **
457 ** Description      enqueue a client request in clcb.
458 **
459 ** Returns          success or failure.
460 **
461 *******************************************************************************/
bta_gattc_enqueue(tBTA_GATTC_CLCB * p_clcb,tBTA_GATTC_DATA * p_data)462 BOOLEAN bta_gattc_enqueue(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
463 {
464     tBTA_GATTC cb_data = {0};
465 
466     if (p_clcb->p_q_cmd == NULL) {
467         p_clcb->p_q_cmd = p_data;
468         return TRUE;
469     } else if ((p_data->hdr.event == BTA_GATTC_API_WRITE_EVT &&
470                p_data->api_write.write_type == BTA_GATTC_WRITE_PREPARE) &&
471                ((p_clcb->p_q_cmd->hdr.event == BTA_GATTC_API_WRITE_EVT &&
472                p_clcb->p_q_cmd->api_write.write_type == BTA_GATTC_WRITE_PREPARE) ||
473                bta_gattc_has_prepare_command_in_queue(p_clcb))) {
474         APPL_TRACE_DEBUG("%s(), prepare offset = %d", __func__, p_data->api_write.offset);
475         cb_data.write.status = BTA_GATT_CONGESTED;
476         cb_data.write.handle = p_data->api_write.handle;
477         cb_data.write.conn_id = p_clcb->bta_conn_id;
478         cb_data.write.offset = p_data->api_write.offset;
479         /* write complete, callback */
480         if (p_clcb->p_rcb->p_cback != NULL) {
481             ( *p_clcb->p_rcb->p_cback)(BTA_GATTC_PREP_WRITE_EVT, (tBTA_GATTC *)&cb_data);
482         }
483         return FALSE;
484     }
485     else if (p_clcb->p_cmd_list) {
486         UINT16 len = 0;
487         tBTA_GATTC_DATA *cmd_data = NULL;
488 
489         if (list_length(p_clcb->p_cmd_list) >= GATTC_COMMAND_QUEUE_SIZE_MAX) {
490 
491             APPL_TRACE_ERROR("%s(), the gattc command queue is full.", __func__);
492             cb_data.status = GATT_BUSY;
493             cb_data.queue_full.conn_id = p_clcb->bta_conn_id;
494             cb_data.queue_full.is_full = TRUE;
495             p_clcb->is_full = TRUE;
496             if (p_clcb->p_rcb->p_cback != NULL) {
497                 ( *p_clcb->p_rcb->p_cback)(BTA_GATTC_QUEUE_FULL_EVT, (tBTA_GATTC *)&cb_data);
498             }
499             return FALSE;
500         }
501 
502         if (p_data->hdr.event == BTA_GATTC_API_WRITE_EVT) {
503             len = p_data->api_write.len;
504             if ((cmd_data = (tBTA_GATTC_DATA *)osi_malloc(sizeof(tBTA_GATTC_DATA) + len)) != NULL) {
505                 memset(cmd_data, 0, sizeof(tBTA_GATTC_DATA) + len);
506 			    memcpy(cmd_data, p_data, sizeof(tBTA_GATTC_DATA));
507                 cmd_data->api_write.p_value = (UINT8 *)(cmd_data + 1);
508 			    memcpy(cmd_data->api_write.p_value, p_data->api_write.p_value, len);
509             } else {
510                 APPL_TRACE_ERROR("%s(), line = %d, alloc fail, no memery.", __func__, __LINE__);
511                 return FALSE;
512             }
513         } else {
514             if ((cmd_data = (tBTA_GATTC_DATA *)osi_malloc(sizeof(tBTA_GATTC_DATA))) != NULL) {
515                 memset(cmd_data, 0, sizeof(tBTA_GATTC_DATA));
516                 memcpy(cmd_data, p_data, sizeof(tBTA_GATTC_DATA));
517             } else {
518                 APPL_TRACE_ERROR("%s(), line = %d, alloc fail, no memery.", __func__, __LINE__);
519                 return FALSE;
520             }
521         }
522 
523         //store the command to the command list.
524         list_append(p_clcb->p_cmd_list, (void *)cmd_data);
525         return FALSE;
526     }
527 
528     return FALSE;
529 }
530 
531 /*******************************************************************************
532 **
533 ** Function         bta_gattc_check_notif_registry
534 **
535 ** Description      check if the service notificaition has been registered.
536 **
537 ** Returns
538 **
539 *******************************************************************************/
bta_gattc_check_notif_registry(tBTA_GATTC_RCB * p_clreg,tBTA_GATTC_SERV * p_srcb,tBTA_GATTC_NOTIFY * p_notify)540 BOOLEAN bta_gattc_check_notif_registry(tBTA_GATTC_RCB  *p_clreg, tBTA_GATTC_SERV *p_srcb,
541                                        tBTA_GATTC_NOTIFY  *p_notify)
542 {
543     UINT8           i;
544 
545     for (i = 0 ; i < BTA_GATTC_NOTIF_REG_MAX; i ++)
546     {
547         if (p_clreg->notif_reg[i].in_use &&
548             bdcmp(p_clreg->notif_reg[i].remote_bda, p_srcb->server_bda) == 0 &&
549             p_clreg->notif_reg[i].handle == p_notify->handle)
550         {
551             APPL_TRACE_DEBUG("Notification registered!");
552             return TRUE;
553         }
554     }
555     return FALSE;
556 
557 }
558 /*******************************************************************************
559 **
560 ** Function         bta_gattc_clear_notif_registration
561 **
562 ** Description      Clear up the notification registration information by BD_ADDR.
563 **                  Where handle is between start_handle and end_handle, and
564 **                  start_handle and end_handle are boundaries of service
565 **                  containing characteristic.
566 **
567 ** Returns          None.
568 **
569 *******************************************************************************/
bta_gattc_clear_notif_registration(tBTA_GATTC_SERV * p_srcb,UINT16 conn_id,UINT16 start_handle,UINT16 end_handle)570 void bta_gattc_clear_notif_registration(tBTA_GATTC_SERV *p_srcb, UINT16 conn_id,
571                                         UINT16 start_handle, UINT16 end_handle)
572 {
573     BD_ADDR             remote_bda;
574     tBTA_GATTC_IF       gatt_if;
575     tBTA_GATTC_RCB      *p_clrcb ;
576     UINT8       i;
577     tGATT_TRANSPORT     transport;
578     UINT16              handle = 0;
579 
580     if (GATT_GetConnectionInfor(conn_id, &gatt_if, remote_bda, &transport)) {
581         if ((p_clrcb = bta_gattc_cl_get_regcb(gatt_if)) != NULL) {
582             for (i = 0 ; i < BTA_GATTC_NOTIF_REG_MAX; i ++) {
583                 if (p_clrcb->notif_reg[i].in_use &&
584                     !bdcmp(p_clrcb->notif_reg[i].remote_bda, remote_bda))
585                 {
586                     /* It's enough to get service or characteristic handle, as
587                      * clear boundaries are always around service.
588                      */
589                     handle = p_clrcb->notif_reg[i].handle;
590                     if (handle >= start_handle && handle <= end_handle) {
591                         memset(&p_clrcb->notif_reg[i], 0, sizeof(tBTA_GATTC_NOTIF_REG));
592                     }
593                 }
594             }
595         }
596     } else {
597         APPL_TRACE_ERROR("can not clear indication/notif registration for unknown app");
598     }
599     return;
600 }
601 
602 /*******************************************************************************
603 **
604 ** Function         bta_gattc_clear_notif_registration_by_bda
605 **
606 ** Description      Clear up the notification registration information by BD_ADDR.
607 **
608 **
609 ** Returns          None.
610 **
611 *******************************************************************************/
bta_gattc_clear_notif_registration_by_bda(tBTA_GATTC_RCB * p_clrcb,BD_ADDR remote_bda)612 void bta_gattc_clear_notif_registration_by_bda(tBTA_GATTC_RCB *p_clrcb, BD_ADDR remote_bda)
613 {
614     if(p_clrcb == NULL) {
615         return;
616     }
617     for (uint8_t i = 0 ; i < BTA_GATTC_NOTIF_REG_MAX; i ++) {
618         if (p_clrcb->notif_reg[i].in_use &&
619             !bdcmp(p_clrcb->notif_reg[i].remote_bda, remote_bda))
620         {
621             memset(&p_clrcb->notif_reg[i], 0, sizeof(tBTA_GATTC_NOTIF_REG));
622         }
623     }
624 }
625 
626 /*******************************************************************************
627 **
628 ** Function         bta_gattc_mark_bg_conn
629 **
630 ** Description      mark background connection status when a bg connection is initiated
631 **                  or terminated.
632 **
633 ** Returns          TRUE if success; FALSE otherwise.
634 **
635 *******************************************************************************/
bta_gattc_mark_bg_conn(tBTA_GATTC_IF client_if,BD_ADDR_PTR remote_bda_ptr,BOOLEAN add,BOOLEAN is_listen)636 BOOLEAN bta_gattc_mark_bg_conn (tBTA_GATTC_IF client_if,  BD_ADDR_PTR remote_bda_ptr,
637                                 BOOLEAN add, BOOLEAN is_listen)
638 {
639     tBTA_GATTC_BG_TCK   *p_bg_tck = &bta_gattc_cb.bg_track[0];
640     UINT8   i = 0;
641     tBTA_GATTC_CIF_MASK  *p_cif_mask;
642 
643     for (i = 0; i < BTA_GATTC_KNOWN_SR_MAX; i ++, p_bg_tck ++) {
644         if (p_bg_tck->in_use &&
645                 ((remote_bda_ptr != NULL && bdcmp(p_bg_tck->remote_bda, remote_bda_ptr) == 0) ||
646                  (remote_bda_ptr == NULL && bdcmp(p_bg_tck->remote_bda, dummy_bda) == 0))) {
647             p_cif_mask = is_listen ? &p_bg_tck->cif_adv_mask : &p_bg_tck->cif_mask;
648 
649             if (add)
650                 /* mask on the cif bit */
651             {
652                 *p_cif_mask |= (1 << (client_if - 1));
653             } else {
654                 if (client_if != 0) {
655                     *p_cif_mask &= (~(1 << (client_if - 1)));
656                 } else {
657                     *p_cif_mask = 0;
658                 }
659             }
660             /* no BG connection for this device, make it available */
661             if (p_bg_tck->cif_mask == 0 && p_bg_tck->cif_adv_mask == 0) {
662                 memset(p_bg_tck, 0, sizeof(tBTA_GATTC_BG_TCK));
663             }
664             return TRUE;
665         }
666     }
667     if (!add) {
668         if (remote_bda_ptr) {
669 #if (!CONFIG_BT_STACK_NO_LOG)
670             char bdstr[18] = {0};
671 #endif
672             APPL_TRACE_ERROR("%s unable to find the bg connection mask for: %s", __func__,
673                              bdaddr_to_string((bt_bdaddr_t *)remote_bda_ptr, bdstr, sizeof(bdstr)));
674         }
675         return FALSE;
676     } else { /* adding a new device mask */
677         for (i = 0, p_bg_tck = &bta_gattc_cb.bg_track[0];
678                 i < BTA_GATTC_KNOWN_SR_MAX; i ++, p_bg_tck ++) {
679             if (!p_bg_tck->in_use) {
680                 p_bg_tck->in_use = TRUE;
681                 if (remote_bda_ptr) {
682                     bdcpy(p_bg_tck->remote_bda, remote_bda_ptr);
683                 } else {
684                     bdcpy(p_bg_tck->remote_bda, dummy_bda);
685                 }
686 
687                 p_cif_mask = is_listen ? &p_bg_tck->cif_adv_mask : &p_bg_tck->cif_mask;
688 
689                 *p_cif_mask = (1 << (client_if - 1));
690                 return TRUE;
691             }
692         }
693         APPL_TRACE_ERROR("no available space to mark the bg connection status");
694         return FALSE;
695     }
696 }
697 /*******************************************************************************
698 **
699 ** Function         bta_gattc_check_bg_conn
700 **
701 ** Description      check if this is a background connection background connection.
702 **
703 ** Returns          TRUE if success; FALSE otherwise.
704 **
705 *******************************************************************************/
bta_gattc_check_bg_conn(tBTA_GATTC_IF client_if,BD_ADDR remote_bda,UINT8 role)706 BOOLEAN bta_gattc_check_bg_conn (tBTA_GATTC_IF client_if,  BD_ADDR remote_bda, UINT8 role)
707 {
708     tBTA_GATTC_BG_TCK   *p_bg_tck = &bta_gattc_cb.bg_track[0];
709     UINT8       i = 0;
710     BOOLEAN     is_bg_conn = FALSE;
711 
712     for (i = 0; i < BTA_GATTC_KNOWN_SR_MAX && !is_bg_conn; i ++, p_bg_tck ++) {
713         if (p_bg_tck->in_use &&
714                 (bdcmp(p_bg_tck->remote_bda, remote_bda) == 0 ||
715                  bdcmp(p_bg_tck->remote_bda, dummy_bda) == 0)) {
716             if (((p_bg_tck->cif_mask & (1 << (client_if - 1))) != 0) &&
717                     role == HCI_ROLE_MASTER) {
718                 is_bg_conn = TRUE;
719             }
720 
721             if (((p_bg_tck->cif_adv_mask & (1 << (client_if - 1))) != 0) &&
722                     role == HCI_ROLE_SLAVE) {
723                 is_bg_conn = TRUE;
724             }
725         }
726     }
727     return is_bg_conn;
728 }
729 /*******************************************************************************
730 **
731 ** Function         bta_gattc_send_open_cback
732 **
733 ** Description      send open callback
734 **
735 ** Returns
736 **
737 *******************************************************************************/
bta_gattc_send_open_cback(tBTA_GATTC_RCB * p_clreg,tBTA_GATT_STATUS status,BD_ADDR remote_bda,UINT16 conn_id,tBTA_TRANSPORT transport,UINT16 mtu)738 void bta_gattc_send_open_cback( tBTA_GATTC_RCB *p_clreg, tBTA_GATT_STATUS status,
739                                 BD_ADDR remote_bda, UINT16 conn_id,
740                                 tBTA_TRANSPORT transport, UINT16 mtu)
741 {
742 
743     tBTA_GATTC      cb_data;
744 
745     if (p_clreg->p_cback) {
746         memset(&cb_data, 0, sizeof(tBTA_GATTC));
747 
748         cb_data.open.status = status;
749         cb_data.open.client_if = p_clreg->client_if;
750         cb_data.open.conn_id = conn_id;
751         cb_data.open.mtu = mtu;
752         cb_data.open.transport = transport;
753         bdcpy(cb_data.open.remote_bda, remote_bda);
754 
755         (*p_clreg->p_cback)(BTA_GATTC_OPEN_EVT, &cb_data);
756     }
757 }
758 
759 /*******************************************************************************
760 **
761 ** Function         bta_gattc_send_connect_cback
762 **
763 ** Description      send connect callback
764 **
765 ** Returns
766 **
767 *******************************************************************************/
bta_gattc_send_connect_cback(tBTA_GATTC_RCB * p_clreg,BD_ADDR remote_bda,UINT16 conn_id,tBTA_GATT_CONN_PARAMS conn_params,UINT8 link_role)768 void bta_gattc_send_connect_cback( tBTA_GATTC_RCB *p_clreg, BD_ADDR remote_bda, UINT16 conn_id, tBTA_GATT_CONN_PARAMS conn_params, UINT8 link_role)
769 {
770     tBTA_GATTC      cb_data;
771 
772     if (p_clreg->p_cback) {
773         memset(&cb_data, 0, sizeof(tBTA_GATTC));
774 
775         cb_data.connect.client_if = p_clreg->client_if;
776         cb_data.connect.conn_id = conn_id;
777         cb_data.connect.link_role = link_role;
778         cb_data.connect.conn_params.interval = conn_params.interval;
779         cb_data.connect.conn_params.latency = conn_params.latency;
780         cb_data.connect.conn_params.timeout = conn_params.timeout;
781         bdcpy(cb_data.connect.remote_bda, remote_bda);
782 
783         (*p_clreg->p_cback)(BTA_GATTC_CONNECT_EVT, &cb_data);
784     }
785 }
786 
787 /*******************************************************************************
788 **
789 ** Function         bta_gattc_send_disconnect_cback
790 **
791 ** Description      send disconnect callback
792 **
793 ** Returns
794 **
795 *******************************************************************************/
bta_gattc_send_disconnect_cback(tBTA_GATTC_RCB * p_clreg,tGATT_DISCONN_REASON reason,BD_ADDR remote_bda,UINT16 conn_id)796 void bta_gattc_send_disconnect_cback( tBTA_GATTC_RCB *p_clreg, tGATT_DISCONN_REASON reason,
797                                 BD_ADDR remote_bda, UINT16 conn_id)
798 {
799     tBTA_GATTC      cb_data;
800 
801     if (p_clreg->p_cback) {
802         memset(&cb_data, 0, sizeof(tBTA_GATTC));
803 
804         cb_data.disconnect.reason = reason;
805         cb_data.disconnect.client_if = p_clreg->client_if;
806         cb_data.disconnect.conn_id = conn_id;
807         bdcpy(cb_data.disconnect.remote_bda, remote_bda);
808 
809         (*p_clreg->p_cback)(BTA_GATTC_DISCONNECT_EVT, &cb_data);
810     }
811 }
812 /*******************************************************************************
813 **
814 ** Function         bta_gattc_conn_alloc
815 **
816 ** Description      allocate connection tracking spot
817 **
818 ** Returns          pointer to the clcb
819 **
820 *******************************************************************************/
bta_gattc_conn_alloc(BD_ADDR remote_bda)821 tBTA_GATTC_CONN *bta_gattc_conn_alloc(BD_ADDR remote_bda)
822 {
823     UINT8               i_conn = 0;
824     tBTA_GATTC_CONN     *p_conn = &bta_gattc_cb.conn_track[0];
825 
826     for (i_conn = 0; i_conn < BTA_GATTC_CONN_MAX; i_conn++, p_conn ++) {
827         if (!p_conn->in_use) {
828 #if BTA_GATT_DEBUG == TRUE
829             APPL_TRACE_DEBUG("bta_gattc_conn_alloc: found conn_track[%d] available", i_conn);
830 #endif
831             p_conn->in_use          = TRUE;
832             bdcpy(p_conn->remote_bda, remote_bda);
833             return p_conn;
834         }
835     }
836     return NULL;
837 }
838 
839 /*******************************************************************************
840 **
841 ** Function         bta_gattc_conn_find
842 **
843 ** Description      allocate connection tracking spot
844 **
845 ** Returns          pointer to the clcb
846 **
847 *******************************************************************************/
bta_gattc_conn_find(BD_ADDR remote_bda)848 tBTA_GATTC_CONN *bta_gattc_conn_find(BD_ADDR remote_bda)
849 {
850     UINT8               i_conn = 0;
851     tBTA_GATTC_CONN     *p_conn = &bta_gattc_cb.conn_track[0];
852 
853     for (i_conn = 0; i_conn < BTA_GATTC_CONN_MAX; i_conn++, p_conn ++) {
854         if (p_conn->in_use && bdcmp(remote_bda, p_conn->remote_bda) == 0) {
855 #if BTA_GATT_DEBUG == TRUE
856             APPL_TRACE_DEBUG("bta_gattc_conn_find: found conn_track[%d] matched", i_conn);
857 #endif
858             return p_conn;
859         }
860     }
861     return NULL;
862 }
863 
864 
865 /*******************************************************************************
866 **
867 ** Function         bta_gattc_conn_find_alloc
868 **
869 ** Description      find or allocate connection tracking spot
870 **
871 ** Returns          pointer to the clcb
872 **
873 *******************************************************************************/
bta_gattc_conn_find_alloc(BD_ADDR remote_bda)874 tBTA_GATTC_CONN *bta_gattc_conn_find_alloc(BD_ADDR remote_bda)
875 {
876     tBTA_GATTC_CONN     *p_conn = bta_gattc_conn_find (remote_bda);
877 
878     if (p_conn == NULL) {
879         p_conn = bta_gattc_conn_alloc(remote_bda);
880     }
881     return p_conn;
882 }
883 
884 /*******************************************************************************
885 **
886 ** Function         bta_gattc_conn_dealloc
887 **
888 ** Description      de-allocate connection tracking spot
889 **
890 ** Returns          pointer to the clcb
891 **
892 *******************************************************************************/
bta_gattc_conn_dealloc(BD_ADDR remote_bda)893 BOOLEAN bta_gattc_conn_dealloc(BD_ADDR remote_bda)
894 {
895     tBTA_GATTC_CONN     *p_conn = bta_gattc_conn_find (remote_bda);
896 
897     if (p_conn != NULL) {
898         p_conn->in_use = FALSE;
899         memset(p_conn->remote_bda, 0, BD_ADDR_LEN);
900         return TRUE;
901     }
902     return FALSE;
903 }
904 
905 /*******************************************************************************
906 **
907 ** Function         bta_gattc_find_int_conn_clcb
908 **
909 ** Description      try to locate a clcb when an internal connecion event arrives.
910 **
911 ** Returns          pointer to the clcb
912 **
913 *******************************************************************************/
bta_gattc_find_int_conn_clcb(tBTA_GATTC_DATA * p_msg)914 tBTA_GATTC_CLCB *bta_gattc_find_int_conn_clcb(tBTA_GATTC_DATA *p_msg)
915 {
916     tBTA_GATTC_CLCB *p_clcb = NULL;
917 
918     if (p_msg->int_conn.role == HCI_ROLE_SLAVE) {
919         bta_gattc_conn_find_alloc(p_msg->int_conn.remote_bda);
920     }
921 
922     /* try to locate a logic channel */
923     if ((p_clcb = bta_gattc_find_clcb_by_cif(p_msg->int_conn.client_if,
924                   p_msg->int_conn.remote_bda,
925                   p_msg->int_conn.transport)) == NULL) {
926         /* for a background connection or listening connection */
927         if (/*p_msg->int_conn.role == HCI_ROLE_SLAVE ||  */
928             bta_gattc_check_bg_conn(p_msg->int_conn.client_if,
929                                     p_msg->int_conn.remote_bda,
930                                     p_msg->int_conn.role)) {
931             /* allocate a new channel */
932             p_clcb = bta_gattc_clcb_alloc(p_msg->int_conn.client_if,
933                                           p_msg->int_conn.remote_bda,
934                                           p_msg->int_conn.transport);
935         }
936     }
937     return p_clcb;
938 }
939 
940 /*******************************************************************************
941 **
942 ** Function         bta_gattc_find_int_disconn_clcb
943 **
944 ** Description      try to locate a clcb when an internal disconnect callback arrives.
945 **
946 ** Returns          pointer to the clcb
947 **
948 *******************************************************************************/
bta_gattc_find_int_disconn_clcb(tBTA_GATTC_DATA * p_msg)949 tBTA_GATTC_CLCB *bta_gattc_find_int_disconn_clcb(tBTA_GATTC_DATA *p_msg)
950 {
951     tBTA_GATTC_CLCB         *p_clcb = NULL;
952 
953     bta_gattc_conn_dealloc(p_msg->int_conn.remote_bda);
954     if ((p_clcb = bta_gattc_find_clcb_by_conn_id(p_msg->int_conn.hdr.layer_specific)) == NULL) {
955         /* connection attempt failed, send connection callback event */
956         p_clcb = bta_gattc_find_clcb_by_cif(p_msg->int_conn.client_if,
957                                             p_msg->int_conn.remote_bda,
958                                             p_msg->int_conn.transport);
959     }
960     if (p_clcb == NULL) {
961         APPL_TRACE_DEBUG(" disconnection ID: [%d] not used by BTA",
962                          p_msg->int_conn.hdr.layer_specific);
963     }
964     return p_clcb;
965 }
966 
bta_to_btif_uuid(bt_uuid_t * p_dest,tBT_UUID * p_src)967 void bta_to_btif_uuid(bt_uuid_t *p_dest, tBT_UUID *p_src)
968 {
969     int i = 0;
970 
971     if (p_src->len == LEN_UUID_16 || p_src->len == LEN_UUID_32)
972     {
973         for(i=0; i != 16; ++i) {
974             p_dest->uu[i] = base_uuid[i];
975         }
976     }
977 
978     switch (p_src->len)
979     {
980         case 0:
981             break;
982 
983         case LEN_UUID_16:
984             p_dest->uu[12] = p_src->uu.uuid16 & 0xff;
985             p_dest->uu[13] = (p_src->uu.uuid16 >> 8) & 0xff;
986             break;
987 
988         case LEN_UUID_32:
989             p_dest->uu[12] = p_src->uu.uuid16 & 0xff;
990             p_dest->uu[13] = (p_src->uu.uuid16 >> 8) & 0xff;
991             p_dest->uu[14] = (p_src->uu.uuid32 >> 16) & 0xff;
992             p_dest->uu[15] = (p_src->uu.uuid32 >> 24) & 0xff;
993             break;
994 
995         case LEN_UUID_128:
996             for(i=0; i != 16; ++i)
997                 p_dest->uu[i] = p_src->uu.uuid128[i];
998             break;
999 
1000         default:
1001             APPL_TRACE_ERROR("%s: Unknown UUID length %d!", __FUNCTION__, p_src->len);
1002             break;
1003     }
1004 }
1005 
1006 
1007 #endif /* BTA_GATT_INCLUDED */
1008