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 address
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 extern void bta_gattc_deregister_cmpl(tBTA_GATTC_RCB *p_clreg);
bta_gattc_clcb_dealloc_by_conn_id(UINT16 conn_id)326 void bta_gattc_clcb_dealloc_by_conn_id(UINT16 conn_id)
327 {
328 tBTA_GATTC_CLCB *p_clcb = bta_gattc_find_clcb_by_conn_id(conn_id);
329
330 if (p_clcb) {
331 tBTA_GATTC_RCB *p_clreg = p_clcb->p_rcb;
332 bta_gattc_clcb_dealloc(p_clcb);
333 // there is a workaround: if there is no connect, we will reset it.
334 if (p_clreg && p_clreg->num_clcb == 0 && p_clreg->dereg_pending) {
335 bta_gattc_deregister_cmpl(p_clreg);
336 }
337 }
338 }
339
340 /*******************************************************************************
341 **
342 ** Function bta_gattc_find_srcb
343 **
344 ** Description find server cache by remote bd address currently in use
345 **
346 ** Returns pointer to the server cache.
347 **
348 *******************************************************************************/
bta_gattc_find_srcb(BD_ADDR bda)349 tBTA_GATTC_SERV *bta_gattc_find_srcb(BD_ADDR bda)
350 {
351 tBTA_GATTC_SERV *p_srcb = &bta_gattc_cb.known_server[0];
352 UINT8 i;
353
354 for (i = 0; i < BTA_GATTC_KNOWN_SR_MAX; i ++, p_srcb ++) {
355 if (p_srcb->in_use && bdcmp(p_srcb->server_bda, bda) == 0) {
356 return p_srcb;
357 }
358 }
359 return NULL;
360 }
361
362 /*******************************************************************************
363 **
364 ** Function bta_gattc_find_srvr_cache
365 **
366 ** Description find server cache by remote bd address
367 **
368 ** Returns pointer to the server cache.
369 **
370 *******************************************************************************/
bta_gattc_find_srvr_cache(BD_ADDR bda)371 tBTA_GATTC_SERV *bta_gattc_find_srvr_cache(BD_ADDR bda)
372 {
373 tBTA_GATTC_SERV *p_srcb = &bta_gattc_cb.known_server[0];
374 UINT8 i;
375
376 for (i = 0; i < BTA_GATTC_KNOWN_SR_MAX; i ++, p_srcb ++) {
377 if (bdcmp(p_srcb->server_bda, bda) == 0) {
378 return p_srcb;
379 }
380 }
381 return NULL;
382 }
383 /*******************************************************************************
384 **
385 ** Function bta_gattc_find_scb_by_cid
386 **
387 ** Description find server control block by connection ID
388 **
389 ** Returns pointer to the server cache.
390 **
391 *******************************************************************************/
bta_gattc_find_scb_by_cid(UINT16 conn_id)392 tBTA_GATTC_SERV *bta_gattc_find_scb_by_cid (UINT16 conn_id)
393 {
394 tBTA_GATTC_CLCB *p_clcb = bta_gattc_find_clcb_by_conn_id(conn_id);
395
396 if (p_clcb) {
397 return p_clcb->p_srcb;
398 } else {
399 return NULL;
400 }
401 }
402 /*******************************************************************************
403 **
404 ** Function bta_gattc_srcb_alloc
405 **
406 ** Description allocate server cache control block
407 **
408 ** Returns pointer to the server cache.
409 **
410 *******************************************************************************/
bta_gattc_srcb_alloc(BD_ADDR bda)411 tBTA_GATTC_SERV *bta_gattc_srcb_alloc(BD_ADDR bda)
412 {
413 tBTA_GATTC_SERV *p_tcb = &bta_gattc_cb.known_server[0],
414 *p_recycle = NULL;
415 BOOLEAN found = FALSE;
416 UINT8 i;
417
418 for (i = 0; i < BTA_GATTC_KNOWN_SR_MAX; i ++, p_tcb ++) {
419 if (!p_tcb->in_use) {
420 found = TRUE;
421 break;
422 } else if (!p_tcb->connected) {
423 p_recycle = p_tcb;
424 }
425 }
426
427 /* if not found, try to recycle one known device */
428 if (!found && !p_recycle) {
429 p_tcb = NULL;
430 }
431 else if (!found && p_recycle) {
432 p_tcb = p_recycle;
433 }
434
435 if (p_tcb != NULL)
436 {
437 if (p_tcb->p_srvc_cache != NULL) {
438 list_free(p_tcb->p_srvc_cache);
439 p_tcb->p_srvc_cache = NULL;
440 }
441 osi_free(p_tcb->p_srvc_list);
442 p_tcb->p_srvc_list = NULL;
443 //osi_free_and_reset((void **)&p_tcb->p_srvc_list);
444 memset(p_tcb, 0 , sizeof(tBTA_GATTC_SERV));
445
446 p_tcb->in_use = TRUE;
447 bdcpy(p_tcb->server_bda, bda);
448 }
449 return p_tcb;
450 }
451
bta_gattc_has_prepare_command_in_queue(tBTA_GATTC_CLCB * p_clcb)452 static BOOLEAN bta_gattc_has_prepare_command_in_queue(tBTA_GATTC_CLCB *p_clcb)
453 {
454 assert(p_clcb != NULL);
455
456 for(list_node_t *sn = list_begin(p_clcb->p_cmd_list);
457 sn != list_end(p_clcb->p_cmd_list); sn = list_next(sn)) {
458
459 tBTA_GATTC_DATA *cmd_data = (tBTA_GATTC_DATA *)list_node(sn);
460 if (cmd_data != NULL && ((cmd_data->hdr.event == BTA_GATTC_API_WRITE_EVT &&
461 cmd_data->api_write.write_type == BTA_GATTC_WRITE_PREPARE) ||
462 cmd_data->hdr.event == BTA_GATTC_API_EXEC_EVT)) {
463 return TRUE;
464 }
465 }
466
467 return FALSE;
468 }
469 /*******************************************************************************
470 **
471 ** Function bta_gattc_enqueue
472 **
473 ** Description enqueue a client request in clcb.
474 **
475 ** Returns success or failure.
476 **
477 *******************************************************************************/
bta_gattc_enqueue(tBTA_GATTC_CLCB * p_clcb,tBTA_GATTC_DATA * p_data)478 BOOLEAN bta_gattc_enqueue(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
479 {
480 tBTA_GATTC cb_data = {0};
481
482 if (p_clcb->p_q_cmd == NULL) {
483 p_clcb->p_q_cmd = p_data;
484 return TRUE;
485 } else if ((p_data->hdr.event == BTA_GATTC_API_WRITE_EVT &&
486 p_data->api_write.write_type == BTA_GATTC_WRITE_PREPARE) &&
487 ((p_clcb->p_q_cmd->hdr.event == BTA_GATTC_API_WRITE_EVT &&
488 p_clcb->p_q_cmd->api_write.write_type == BTA_GATTC_WRITE_PREPARE) ||
489 bta_gattc_has_prepare_command_in_queue(p_clcb))) {
490 APPL_TRACE_DEBUG("%s(), prepare offset = %d", __func__, p_data->api_write.offset);
491 cb_data.write.status = BTA_GATT_CONGESTED;
492 cb_data.write.handle = p_data->api_write.handle;
493 cb_data.write.conn_id = p_clcb->bta_conn_id;
494 cb_data.write.offset = p_data->api_write.offset;
495 /* write complete, callback */
496 if (p_clcb->p_rcb->p_cback != NULL) {
497 ( *p_clcb->p_rcb->p_cback)(BTA_GATTC_PREP_WRITE_EVT, (tBTA_GATTC *)&cb_data);
498 }
499 return FALSE;
500 }
501 else if (p_clcb->p_cmd_list) {
502 UINT16 len = 0;
503 tBTA_GATTC_DATA *cmd_data = NULL;
504
505 if (list_length(p_clcb->p_cmd_list) >= GATTC_COMMAND_QUEUE_SIZE_MAX) {
506
507 APPL_TRACE_ERROR("%s(), the gattc command queue is full.", __func__);
508 cb_data.status = GATT_BUSY;
509 cb_data.queue_full.conn_id = p_clcb->bta_conn_id;
510 cb_data.queue_full.is_full = TRUE;
511 p_clcb->is_full = TRUE;
512 if (p_clcb->p_rcb->p_cback != NULL) {
513 ( *p_clcb->p_rcb->p_cback)(BTA_GATTC_QUEUE_FULL_EVT, (tBTA_GATTC *)&cb_data);
514 }
515 return FALSE;
516 }
517
518 if (p_data->hdr.event == BTA_GATTC_API_WRITE_EVT) {
519 len = p_data->api_write.len;
520 if ((cmd_data = (tBTA_GATTC_DATA *)osi_malloc(sizeof(tBTA_GATTC_DATA) + len)) != NULL) {
521 memset(cmd_data, 0, sizeof(tBTA_GATTC_DATA) + len);
522 memcpy(cmd_data, p_data, sizeof(tBTA_GATTC_DATA));
523 cmd_data->api_write.p_value = (UINT8 *)(cmd_data + 1);
524 memcpy(cmd_data->api_write.p_value, p_data->api_write.p_value, len);
525 } else {
526 APPL_TRACE_ERROR("%s(), line = %d, alloc fail, no memory.", __func__, __LINE__);
527 return FALSE;
528 }
529 } else {
530 if ((cmd_data = (tBTA_GATTC_DATA *)osi_malloc(sizeof(tBTA_GATTC_DATA))) != NULL) {
531 memset(cmd_data, 0, sizeof(tBTA_GATTC_DATA));
532 memcpy(cmd_data, p_data, sizeof(tBTA_GATTC_DATA));
533 } else {
534 APPL_TRACE_ERROR("%s(), line = %d, alloc fail, no memory.", __func__, __LINE__);
535 return FALSE;
536 }
537 }
538
539 //store the command to the command list.
540 list_append(p_clcb->p_cmd_list, (void *)cmd_data);
541 return FALSE;
542 }
543
544 return FALSE;
545 }
546
547 /*******************************************************************************
548 **
549 ** Function bta_gattc_check_notif_registry
550 **
551 ** Description check if the service notificaition has been registered.
552 **
553 ** Returns
554 **
555 *******************************************************************************/
bta_gattc_check_notif_registry(tBTA_GATTC_RCB * p_clreg,tBTA_GATTC_SERV * p_srcb,tBTA_GATTC_NOTIFY * p_notify)556 BOOLEAN bta_gattc_check_notif_registry(tBTA_GATTC_RCB *p_clreg, tBTA_GATTC_SERV *p_srcb,
557 tBTA_GATTC_NOTIFY *p_notify)
558 {
559 UINT8 i;
560
561 for (i = 0 ; i < BTA_GATTC_NOTIF_REG_MAX; i ++)
562 {
563 if (p_clreg->notif_reg[i].in_use &&
564 bdcmp(p_clreg->notif_reg[i].remote_bda, p_srcb->server_bda) == 0 &&
565 p_clreg->notif_reg[i].handle == p_notify->handle)
566 {
567 APPL_TRACE_DEBUG("Notification registered!");
568 return TRUE;
569 }
570 }
571 return FALSE;
572
573 }
574 /*******************************************************************************
575 **
576 ** Function bta_gattc_clear_notif_registration
577 **
578 ** Description Clear up the notification registration information by BD_ADDR.
579 ** Where handle is between start_handle and end_handle, and
580 ** start_handle and end_handle are boundaries of service
581 ** containing characteristic.
582 **
583 ** Returns None.
584 **
585 *******************************************************************************/
bta_gattc_clear_notif_registration(tBTA_GATTC_SERV * p_srcb,UINT16 conn_id,UINT16 start_handle,UINT16 end_handle)586 void bta_gattc_clear_notif_registration(tBTA_GATTC_SERV *p_srcb, UINT16 conn_id,
587 UINT16 start_handle, UINT16 end_handle)
588 {
589 BD_ADDR remote_bda;
590 tBTA_GATTC_IF gatt_if;
591 tBTA_GATTC_RCB *p_clrcb ;
592 UINT8 i;
593 tGATT_TRANSPORT transport;
594 UINT16 handle = 0;
595
596 if (GATT_GetConnectionInfor(conn_id, &gatt_if, remote_bda, &transport)) {
597 if ((p_clrcb = bta_gattc_cl_get_regcb(gatt_if)) != NULL) {
598 for (i = 0 ; i < BTA_GATTC_NOTIF_REG_MAX; i ++) {
599 if (p_clrcb->notif_reg[i].in_use &&
600 !bdcmp(p_clrcb->notif_reg[i].remote_bda, remote_bda))
601 {
602 /* It's enough to get service or characteristic handle, as
603 * clear boundaries are always around service.
604 */
605 handle = p_clrcb->notif_reg[i].handle;
606 if (handle >= start_handle && handle <= end_handle) {
607 memset(&p_clrcb->notif_reg[i], 0, sizeof(tBTA_GATTC_NOTIF_REG));
608 }
609 }
610 }
611 }
612 } else {
613 APPL_TRACE_ERROR("can not clear indication/notif registration for unknown app");
614 }
615 return;
616 }
617
618 /*******************************************************************************
619 **
620 ** Function bta_gattc_clear_notif_registration_by_bda
621 **
622 ** Description Clear up the notification registration information by BD_ADDR.
623 **
624 **
625 ** Returns None.
626 **
627 *******************************************************************************/
bta_gattc_clear_notif_registration_by_bda(tBTA_GATTC_RCB * p_clrcb,BD_ADDR remote_bda)628 void bta_gattc_clear_notif_registration_by_bda(tBTA_GATTC_RCB *p_clrcb, BD_ADDR remote_bda)
629 {
630 if(p_clrcb == NULL) {
631 return;
632 }
633 for (uint8_t i = 0 ; i < BTA_GATTC_NOTIF_REG_MAX; i ++) {
634 if (p_clrcb->notif_reg[i].in_use &&
635 !bdcmp(p_clrcb->notif_reg[i].remote_bda, remote_bda))
636 {
637 memset(&p_clrcb->notif_reg[i], 0, sizeof(tBTA_GATTC_NOTIF_REG));
638 }
639 }
640 }
641
642 /*******************************************************************************
643 **
644 ** Function bta_gattc_mark_bg_conn
645 **
646 ** Description mark background connection status when a bg connection is initiated
647 ** or terminated.
648 **
649 ** Returns TRUE if success; FALSE otherwise.
650 **
651 *******************************************************************************/
bta_gattc_mark_bg_conn(tBTA_GATTC_IF client_if,BD_ADDR_PTR remote_bda_ptr,BOOLEAN add,BOOLEAN is_listen)652 BOOLEAN bta_gattc_mark_bg_conn (tBTA_GATTC_IF client_if, BD_ADDR_PTR remote_bda_ptr,
653 BOOLEAN add, BOOLEAN is_listen)
654 {
655 tBTA_GATTC_BG_TCK *p_bg_tck = &bta_gattc_cb.bg_track[0];
656 UINT8 i = 0;
657 tBTA_GATTC_CIF_MASK *p_cif_mask;
658
659 for (i = 0; i < BTA_GATTC_KNOWN_SR_MAX; i ++, p_bg_tck ++) {
660 if (p_bg_tck->in_use &&
661 ((remote_bda_ptr != NULL && bdcmp(p_bg_tck->remote_bda, remote_bda_ptr) == 0) ||
662 (remote_bda_ptr == NULL && bdcmp(p_bg_tck->remote_bda, dummy_bda) == 0))) {
663 p_cif_mask = is_listen ? &p_bg_tck->cif_adv_mask : &p_bg_tck->cif_mask;
664
665 if (add)
666 /* mask on the cif bit */
667 {
668 *p_cif_mask |= (1 << (client_if - 1));
669 } else {
670 if (client_if != 0) {
671 *p_cif_mask &= (~(1 << (client_if - 1)));
672 } else {
673 *p_cif_mask = 0;
674 }
675 }
676 /* no BG connection for this device, make it available */
677 if (p_bg_tck->cif_mask == 0 && p_bg_tck->cif_adv_mask == 0) {
678 memset(p_bg_tck, 0, sizeof(tBTA_GATTC_BG_TCK));
679 }
680 return TRUE;
681 }
682 }
683 if (!add) {
684 if (remote_bda_ptr) {
685 #if (!CONFIG_BT_STACK_NO_LOG)
686 char bdstr[18] = {0};
687 #endif
688 APPL_TRACE_WARNING("%s unable to find the bg connection mask for: %s", __func__,
689 bdaddr_to_string((bt_bdaddr_t *)remote_bda_ptr, bdstr, sizeof(bdstr)));
690 }
691 return FALSE;
692 } else { /* adding a new device mask */
693 for (i = 0, p_bg_tck = &bta_gattc_cb.bg_track[0];
694 i < BTA_GATTC_KNOWN_SR_MAX; i ++, p_bg_tck ++) {
695 if (!p_bg_tck->in_use) {
696 p_bg_tck->in_use = TRUE;
697 if (remote_bda_ptr) {
698 bdcpy(p_bg_tck->remote_bda, remote_bda_ptr);
699 } else {
700 bdcpy(p_bg_tck->remote_bda, dummy_bda);
701 }
702
703 p_cif_mask = is_listen ? &p_bg_tck->cif_adv_mask : &p_bg_tck->cif_mask;
704
705 *p_cif_mask = (1 << (client_if - 1));
706 return TRUE;
707 }
708 }
709 APPL_TRACE_ERROR("no available space to mark the bg connection status");
710 return FALSE;
711 }
712 }
713 /*******************************************************************************
714 **
715 ** Function bta_gattc_check_bg_conn
716 **
717 ** Description check if this is a background connection background connection.
718 **
719 ** Returns TRUE if success; FALSE otherwise.
720 **
721 *******************************************************************************/
bta_gattc_check_bg_conn(tBTA_GATTC_IF client_if,BD_ADDR remote_bda,UINT8 role)722 BOOLEAN bta_gattc_check_bg_conn (tBTA_GATTC_IF client_if, BD_ADDR remote_bda, UINT8 role)
723 {
724 tBTA_GATTC_BG_TCK *p_bg_tck = &bta_gattc_cb.bg_track[0];
725 UINT8 i = 0;
726 BOOLEAN is_bg_conn = FALSE;
727
728 for (i = 0; i < BTA_GATTC_KNOWN_SR_MAX && !is_bg_conn; i ++, p_bg_tck ++) {
729 if (p_bg_tck->in_use &&
730 (bdcmp(p_bg_tck->remote_bda, remote_bda) == 0 ||
731 bdcmp(p_bg_tck->remote_bda, dummy_bda) == 0)) {
732 if (((p_bg_tck->cif_mask & (1 << (client_if - 1))) != 0) &&
733 role == HCI_ROLE_MASTER) {
734 is_bg_conn = TRUE;
735 }
736
737 if (((p_bg_tck->cif_adv_mask & (1 << (client_if - 1))) != 0) &&
738 role == HCI_ROLE_SLAVE) {
739 is_bg_conn = TRUE;
740 }
741 }
742 }
743 return is_bg_conn;
744 }
745 /*******************************************************************************
746 **
747 ** Function bta_gattc_send_open_cback
748 **
749 ** Description send open callback
750 **
751 ** Returns
752 **
753 *******************************************************************************/
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)754 void bta_gattc_send_open_cback( tBTA_GATTC_RCB *p_clreg, tBTA_GATT_STATUS status,
755 BD_ADDR remote_bda, UINT16 conn_id,
756 tBTA_TRANSPORT transport, UINT16 mtu)
757 {
758
759 tBTA_GATTC cb_data;
760
761 if (p_clreg->p_cback) {
762 memset(&cb_data, 0, sizeof(tBTA_GATTC));
763
764 cb_data.open.status = status;
765 cb_data.open.client_if = p_clreg->client_if;
766 cb_data.open.conn_id = conn_id;
767 cb_data.open.mtu = mtu;
768 cb_data.open.transport = transport;
769 bdcpy(cb_data.open.remote_bda, remote_bda);
770
771 (*p_clreg->p_cback)(BTA_GATTC_OPEN_EVT, &cb_data);
772 }
773 }
774
775 /*******************************************************************************
776 **
777 ** Function bta_gattc_send_connect_cback
778 **
779 ** Description send connect callback
780 **
781 ** Returns
782 **
783 *******************************************************************************/
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,UINT8 ble_addr_type,UINT16 conn_handle)784 void bta_gattc_send_connect_cback( tBTA_GATTC_RCB *p_clreg, BD_ADDR remote_bda, UINT16 conn_id,
785 tBTA_GATT_CONN_PARAMS conn_params, UINT8 link_role, UINT8 ble_addr_type, UINT16 conn_handle)
786 {
787 tBTA_GATTC cb_data;
788
789 if (p_clreg->p_cback) {
790 memset(&cb_data, 0, sizeof(tBTA_GATTC));
791
792 cb_data.connect.client_if = p_clreg->client_if;
793 cb_data.connect.conn_id = conn_id;
794 cb_data.connect.link_role = link_role;
795 cb_data.connect.conn_params.interval = conn_params.interval;
796 cb_data.connect.conn_params.latency = conn_params.latency;
797 cb_data.connect.conn_params.timeout = conn_params.timeout;
798 bdcpy(cb_data.connect.remote_bda, remote_bda);
799 cb_data.connect.ble_addr_type = ble_addr_type;
800 cb_data.connect.conn_handle = conn_handle;
801
802 (*p_clreg->p_cback)(BTA_GATTC_CONNECT_EVT, &cb_data);
803 }
804 }
805
806 /*******************************************************************************
807 **
808 ** Function bta_gattc_send_disconnect_cback
809 **
810 ** Description send disconnect callback
811 **
812 ** Returns
813 **
814 *******************************************************************************/
bta_gattc_send_disconnect_cback(tBTA_GATTC_RCB * p_clreg,tGATT_DISCONN_REASON reason,BD_ADDR remote_bda,UINT16 conn_id)815 void bta_gattc_send_disconnect_cback( tBTA_GATTC_RCB *p_clreg, tGATT_DISCONN_REASON reason,
816 BD_ADDR remote_bda, UINT16 conn_id)
817 {
818 tBTA_GATTC cb_data;
819
820 if (p_clreg->p_cback) {
821 memset(&cb_data, 0, sizeof(tBTA_GATTC));
822
823 cb_data.disconnect.reason = reason;
824 cb_data.disconnect.client_if = p_clreg->client_if;
825 cb_data.disconnect.conn_id = conn_id;
826 bdcpy(cb_data.disconnect.remote_bda, remote_bda);
827
828 (*p_clreg->p_cback)(BTA_GATTC_DISCONNECT_EVT, &cb_data);
829 }
830 }
831 /*******************************************************************************
832 **
833 ** Function bta_gattc_conn_alloc
834 **
835 ** Description allocate connection tracking spot
836 **
837 ** Returns pointer to the clcb
838 **
839 *******************************************************************************/
bta_gattc_conn_alloc(BD_ADDR remote_bda)840 tBTA_GATTC_CONN *bta_gattc_conn_alloc(BD_ADDR remote_bda)
841 {
842 UINT8 i_conn = 0;
843 tBTA_GATTC_CONN *p_conn = &bta_gattc_cb.conn_track[0];
844
845 for (i_conn = 0; i_conn < BTA_GATTC_CONN_MAX; i_conn++, p_conn ++) {
846 if (!p_conn->in_use) {
847 #if BTA_GATT_DEBUG == TRUE
848 APPL_TRACE_DEBUG("bta_gattc_conn_alloc: found conn_track[%d] available", i_conn);
849 #endif
850 p_conn->in_use = TRUE;
851 bdcpy(p_conn->remote_bda, remote_bda);
852 return p_conn;
853 }
854 }
855 return NULL;
856 }
857
858 /*******************************************************************************
859 **
860 ** Function bta_gattc_conn_find
861 **
862 ** Description allocate connection tracking spot
863 **
864 ** Returns pointer to the clcb
865 **
866 *******************************************************************************/
bta_gattc_conn_find(BD_ADDR remote_bda)867 tBTA_GATTC_CONN *bta_gattc_conn_find(BD_ADDR remote_bda)
868 {
869 UINT8 i_conn = 0;
870 tBTA_GATTC_CONN *p_conn = &bta_gattc_cb.conn_track[0];
871
872 for (i_conn = 0; i_conn < BTA_GATTC_CONN_MAX; i_conn++, p_conn ++) {
873 if (p_conn->in_use && bdcmp(remote_bda, p_conn->remote_bda) == 0) {
874 #if BTA_GATT_DEBUG == TRUE
875 APPL_TRACE_DEBUG("bta_gattc_conn_find: found conn_track[%d] matched", i_conn);
876 #endif
877 return p_conn;
878 }
879 }
880 return NULL;
881 }
882
883
884 /*******************************************************************************
885 **
886 ** Function bta_gattc_conn_find_alloc
887 **
888 ** Description find or allocate connection tracking spot
889 **
890 ** Returns pointer to the clcb
891 **
892 *******************************************************************************/
bta_gattc_conn_find_alloc(BD_ADDR remote_bda)893 tBTA_GATTC_CONN *bta_gattc_conn_find_alloc(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 = bta_gattc_conn_alloc(remote_bda);
899 }
900 return p_conn;
901 }
902
903 /*******************************************************************************
904 **
905 ** Function bta_gattc_conn_dealloc
906 **
907 ** Description de-allocate connection tracking spot
908 **
909 ** Returns pointer to the clcb
910 **
911 *******************************************************************************/
bta_gattc_conn_dealloc(BD_ADDR remote_bda)912 BOOLEAN bta_gattc_conn_dealloc(BD_ADDR remote_bda)
913 {
914 tBTA_GATTC_CONN *p_conn = bta_gattc_conn_find (remote_bda);
915
916 if (p_conn != NULL) {
917 p_conn->in_use = FALSE;
918 memset(p_conn->remote_bda, 0, BD_ADDR_LEN);
919 return TRUE;
920 }
921 return FALSE;
922 }
923
924 /*******************************************************************************
925 **
926 ** Function bta_gattc_find_int_conn_clcb
927 **
928 ** Description try to locate a clcb when an internal connection event arrives.
929 **
930 ** Returns pointer to the clcb
931 **
932 *******************************************************************************/
bta_gattc_find_int_conn_clcb(tBTA_GATTC_DATA * p_msg)933 tBTA_GATTC_CLCB *bta_gattc_find_int_conn_clcb(tBTA_GATTC_DATA *p_msg)
934 {
935 tBTA_GATTC_CLCB *p_clcb = NULL;
936
937 if (p_msg->int_conn.role == HCI_ROLE_SLAVE) {
938 bta_gattc_conn_find_alloc(p_msg->int_conn.remote_bda);
939 }
940
941 /* try to locate a logic channel */
942 if ((p_clcb = bta_gattc_find_clcb_by_cif(p_msg->int_conn.client_if,
943 p_msg->int_conn.remote_bda,
944 p_msg->int_conn.transport)) == NULL) {
945 /* for a background connection or listening connection */
946 if (/*p_msg->int_conn.role == HCI_ROLE_SLAVE || */
947 bta_gattc_check_bg_conn(p_msg->int_conn.client_if,
948 p_msg->int_conn.remote_bda,
949 p_msg->int_conn.role)) {
950 /* allocate a new channel */
951 p_clcb = bta_gattc_clcb_alloc(p_msg->int_conn.client_if,
952 p_msg->int_conn.remote_bda,
953 p_msg->int_conn.transport);
954 }
955 }
956 return p_clcb;
957 }
958
959 /*******************************************************************************
960 **
961 ** Function bta_gattc_find_int_disconn_clcb
962 **
963 ** Description try to locate a clcb when an internal disconnect callback arrives.
964 **
965 ** Returns pointer to the clcb
966 **
967 *******************************************************************************/
bta_gattc_find_int_disconn_clcb(tBTA_GATTC_DATA * p_msg)968 tBTA_GATTC_CLCB *bta_gattc_find_int_disconn_clcb(tBTA_GATTC_DATA *p_msg)
969 {
970 tBTA_GATTC_CLCB *p_clcb = NULL;
971
972 bta_gattc_conn_dealloc(p_msg->int_conn.remote_bda);
973 if ((p_clcb = bta_gattc_find_clcb_by_conn_id(p_msg->int_conn.hdr.layer_specific)) == NULL) {
974 /* connection attempt failed, send connection callback event */
975 p_clcb = bta_gattc_find_clcb_by_cif(p_msg->int_conn.client_if,
976 p_msg->int_conn.remote_bda,
977 p_msg->int_conn.transport);
978 }
979 if (p_clcb == NULL) {
980 APPL_TRACE_DEBUG(" disconnection ID: [%d] not used by BTA",
981 p_msg->int_conn.hdr.layer_specific);
982 }
983 return p_clcb;
984 }
985
bta_to_btif_uuid(bt_uuid_t * p_dest,tBT_UUID * p_src)986 void bta_to_btif_uuid(bt_uuid_t *p_dest, tBT_UUID *p_src)
987 {
988 int i = 0;
989
990 if (p_src->len == LEN_UUID_16 || p_src->len == LEN_UUID_32)
991 {
992 for(i=0; i != 16; ++i) {
993 p_dest->uu[i] = base_uuid[i];
994 }
995 }
996
997 switch (p_src->len)
998 {
999 case 0:
1000 break;
1001
1002 case LEN_UUID_16:
1003 p_dest->uu[12] = p_src->uu.uuid16 & 0xff;
1004 p_dest->uu[13] = (p_src->uu.uuid16 >> 8) & 0xff;
1005 break;
1006
1007 case LEN_UUID_32:
1008 p_dest->uu[12] = p_src->uu.uuid16 & 0xff;
1009 p_dest->uu[13] = (p_src->uu.uuid16 >> 8) & 0xff;
1010 p_dest->uu[14] = (p_src->uu.uuid32 >> 16) & 0xff;
1011 p_dest->uu[15] = (p_src->uu.uuid32 >> 24) & 0xff;
1012 break;
1013
1014 case LEN_UUID_128:
1015 for(i=0; i != 16; ++i)
1016 p_dest->uu[i] = p_src->uu.uuid128[i];
1017 break;
1018
1019 default:
1020 APPL_TRACE_ERROR("%s: Unknown UUID length %d!", __FUNCTION__, p_src->len);
1021 break;
1022 }
1023 }
1024
1025
1026 #endif /* BTA_GATT_INCLUDED */
1027