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 action functions for the state
22 * machine.
23 *
24 ******************************************************************************/
25 #define LOG_TAG "bt_bta_gattc"
26
27 #include "bt_target.h"
28
29 #include "utl.h"
30 #include "gki.h"
31 #include "bta_sys.h"
32
33 #include "bta_gattc_int.h"
34 #include "l2c_api.h"
35
36 #if (defined BTA_HH_LE_INCLUDED && BTA_HH_LE_INCLUDED == TRUE)
37 #include "bta_hh_int.h"
38 #endif
39
40 #include "btif/include/btif_debug_conn.h"
41
42 #include <string.h>
43
44 #include "osi/include/log.h"
45
46 #if BTA_GATT_INCLUDED && BLE_INCLUDED == TRUE
47
48 /*****************************************************************************
49 ** Constants
50 *****************************************************************************/
51 static void bta_gattc_conn_cback(tGATT_IF gattc_if, BD_ADDR bda, UINT16 conn_id,
52 BOOLEAN connected, tGATT_DISCONN_REASON reason,
53 tBT_TRANSPORT transport);
54
55 static void bta_gattc_cmpl_cback(UINT16 conn_id, tGATTC_OPTYPE op, tGATT_STATUS status,
56 tGATT_CL_COMPLETE *p_data);
57 static void bta_gattc_cmpl_sendmsg(UINT16 conn_id, tGATTC_OPTYPE op,
58 tBTA_GATT_STATUS status,
59 tGATT_CL_COMPLETE *p_data);
60
61 static void bta_gattc_deregister_cmpl(tBTA_GATTC_RCB *p_clreg);
62 static void bta_gattc_enc_cmpl_cback(tGATT_IF gattc_if, BD_ADDR bda);
63 static void bta_gattc_cong_cback (UINT16 conn_id, BOOLEAN congested);
64
65 static tGATT_CBACK bta_gattc_cl_cback =
66 {
67 bta_gattc_conn_cback,
68 bta_gattc_cmpl_cback,
69 bta_gattc_disc_res_cback,
70 bta_gattc_disc_cmpl_cback,
71 NULL,
72 bta_gattc_enc_cmpl_cback,
73 bta_gattc_cong_cback
74 };
75
76 /* opcode(tGATTC_OPTYPE) order has to be comply with internal event order */
77 static UINT16 bta_gattc_opcode_to_int_evt[] =
78 {
79 BTA_GATTC_API_READ_EVT,
80 BTA_GATTC_API_WRITE_EVT,
81 BTA_GATTC_API_EXEC_EVT,
82 BTA_GATTC_API_CFG_MTU_EVT
83 };
84
85 #if (BT_TRACE_VERBOSE == TRUE)
86 static const char *bta_gattc_op_code_name[] =
87 {
88 "Unknown",
89 "Discovery",
90 "Read",
91 "Write",
92 "Exec",
93 "Config",
94 "Notification",
95 "Indication"
96 };
97 #endif
98 /*****************************************************************************
99 ** Action Functions
100 *****************************************************************************/
101
102 /*******************************************************************************
103 **
104 ** Function bta_gattc_enable
105 **
106 ** Description Enables GATTC module
107 **
108 **
109 ** Returns void
110 **
111 *******************************************************************************/
bta_gattc_enable(tBTA_GATTC_CB * p_cb)112 static void bta_gattc_enable(tBTA_GATTC_CB *p_cb)
113 {
114 APPL_TRACE_DEBUG("bta_gattc_enable");
115
116 if (p_cb->state == BTA_GATTC_STATE_DISABLED)
117 {
118 /* initialize control block */
119 memset(&bta_gattc_cb, 0, sizeof(tBTA_GATTC_CB));
120 p_cb->state = BTA_GATTC_STATE_ENABLED;
121 }
122 else
123 {
124 APPL_TRACE_DEBUG("GATTC is arelady enabled");
125 }
126 }
127
128
129 /*******************************************************************************
130 **
131 ** Function bta_gattc_disable
132 **
133 ** Description Disable GATTC module by cleaning up all active connections
134 ** and deregister all application.
135 **
136 ** Returns void
137 **
138 *******************************************************************************/
bta_gattc_disable(tBTA_GATTC_CB * p_cb)139 void bta_gattc_disable(tBTA_GATTC_CB *p_cb)
140 {
141 UINT8 i;
142
143 APPL_TRACE_DEBUG("bta_gattc_disable");
144
145 if (p_cb->state != BTA_GATTC_STATE_ENABLED)
146 {
147 APPL_TRACE_ERROR("not enabled or disable in pogress");
148 return;
149 }
150
151 for (i = 0; i <BTA_GATTC_CL_MAX; i ++)
152 {
153 if (p_cb->cl_rcb[i].in_use)
154 {
155 p_cb->state = BTA_GATTC_STATE_DISABLING;
156 /* don't deregister HH GATT IF */
157 /* HH GATT IF will be deregistered by bta_hh_le_deregister when disable HH */
158 #if (defined BTA_HH_LE_INCLUDED && BTA_HH_LE_INCLUDED == TRUE)
159 if (!bta_hh_le_is_hh_gatt_if(p_cb->cl_rcb[i].client_if)) {
160 #endif
161 bta_gattc_deregister(p_cb, &p_cb->cl_rcb[i]);
162 #if (defined BTA_HH_LE_INCLUDED && BTA_HH_LE_INCLUDED == TRUE)
163 }
164 #endif
165 }
166 }
167
168 /* no registered apps, indicate disable completed */
169 if (p_cb->state != BTA_GATTC_STATE_DISABLING)
170 {
171 p_cb->state = BTA_GATTC_STATE_DISABLED;
172 memset(p_cb, 0, sizeof(tBTA_GATTC_CB));
173 }
174 }
175
176 /*******************************************************************************
177 **
178 ** Function bta_gattc_register
179 **
180 ** Description Register a GATT client application with BTA.
181 **
182 ** Returns void
183 **
184 *******************************************************************************/
bta_gattc_register(tBTA_GATTC_CB * p_cb,tBTA_GATTC_DATA * p_data)185 void bta_gattc_register(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA *p_data)
186 {
187 tBTA_GATTC cb_data;
188 UINT8 i;
189 tBT_UUID *p_app_uuid = &p_data->api_reg.app_uuid;
190 tBTA_GATTC_INT_START_IF *p_buf;
191 tBTA_GATT_STATUS status = BTA_GATT_NO_RESOURCES;
192
193
194 APPL_TRACE_DEBUG("bta_gattc_register state %d",p_cb->state);
195 memset(&cb_data, 0, sizeof(cb_data));
196 cb_data.reg_oper.status = BTA_GATT_NO_RESOURCES;
197
198 /* check if GATTC module is already enabled . Else enable */
199 if (p_cb->state == BTA_GATTC_STATE_DISABLED)
200 {
201 bta_gattc_enable (p_cb);
202 }
203 /* todo need to check duplicate uuid */
204 for (i = 0; i < BTA_GATTC_CL_MAX; i ++)
205 {
206 if (!p_cb->cl_rcb[i].in_use)
207 {
208 if ((p_app_uuid == NULL) || (p_cb->cl_rcb[i].client_if = GATT_Register(p_app_uuid, &bta_gattc_cl_cback)) == 0)
209 {
210 APPL_TRACE_ERROR("Register with GATT stack failed.");
211 status = BTA_GATT_ERROR;
212 }
213 else
214 {
215 p_cb->cl_rcb[i].in_use = TRUE;
216 p_cb->cl_rcb[i].p_cback = p_data->api_reg.p_cback;
217 memcpy(&p_cb->cl_rcb[i].app_uuid, p_app_uuid, sizeof(tBT_UUID));
218
219 /* BTA use the same client interface as BTE GATT statck */
220 cb_data.reg_oper.client_if = p_cb->cl_rcb[i].client_if;
221
222 if ((p_buf = (tBTA_GATTC_INT_START_IF *) GKI_getbuf(sizeof(tBTA_GATTC_INT_START_IF))) != NULL)
223 {
224 p_buf->hdr.event = BTA_GATTC_INT_START_IF_EVT;
225 p_buf->client_if = p_cb->cl_rcb[i].client_if;
226
227 bta_sys_sendmsg(p_buf);
228 status = BTA_GATT_OK;
229 }
230 else
231 {
232 GATT_Deregister(p_cb->cl_rcb[i].client_if);
233
234 status = BTA_GATT_NO_RESOURCES;
235 memset( &p_cb->cl_rcb[i], 0 , sizeof(tBTA_GATTC_RCB));
236 }
237 break;
238 }
239 }
240 }
241
242 /* callback with register event */
243 if (p_data->api_reg.p_cback)
244 {
245 if (p_app_uuid != NULL)
246 memcpy(&(cb_data.reg_oper.app_uuid),p_app_uuid,sizeof(tBT_UUID));
247
248 cb_data.reg_oper.status = status;
249 (*p_data->api_reg.p_cback)(BTA_GATTC_REG_EVT, (tBTA_GATTC *)&cb_data);
250 }
251 }
252 /*******************************************************************************
253 **
254 ** Function bta_gattc_start_if
255 **
256 ** Description start an application interface.
257 **
258 ** Returns none.
259 **
260 *******************************************************************************/
bta_gattc_start_if(tBTA_GATTC_CB * p_cb,tBTA_GATTC_DATA * p_msg)261 void bta_gattc_start_if(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA *p_msg)
262 {
263 UNUSED(p_cb);
264
265 if (bta_gattc_cl_get_regcb(p_msg->int_start_if.client_if) !=NULL )
266 {
267 GATT_StartIf(p_msg->int_start_if.client_if);
268 }
269 else
270 {
271 APPL_TRACE_ERROR("Unable to start app.: Unknown interface =%d",p_msg->int_start_if.client_if );
272 }
273 }
274 /*******************************************************************************
275 **
276 ** Function bta_gattc_deregister
277 **
278 ** Description De-Register a GATT client application with BTA.
279 **
280 ** Returns void
281 **
282 *******************************************************************************/
bta_gattc_deregister(tBTA_GATTC_CB * p_cb,tBTA_GATTC_RCB * p_clreg)283 void bta_gattc_deregister(tBTA_GATTC_CB *p_cb, tBTA_GATTC_RCB *p_clreg)
284 {
285 UINT8 i;
286 BT_HDR buf;
287
288 if (p_clreg != NULL)
289 {
290 /* remove bg connection associated with this rcb */
291 for (i = 0; i < BTA_GATTC_KNOWN_SR_MAX; i ++)
292 {
293 if (p_cb->bg_track[i].in_use)
294 {
295 if (p_cb->bg_track[i].cif_mask & (1 <<(p_clreg->client_if - 1)))
296 {
297 bta_gattc_mark_bg_conn(p_clreg->client_if, p_cb->bg_track[i].remote_bda, FALSE, FALSE);
298 GATT_CancelConnect(p_clreg->client_if, p_cb->bg_track[i].remote_bda, FALSE);
299 }
300 if (p_cb->bg_track[i].cif_adv_mask & (1 <<(p_clreg->client_if - 1)))
301 {
302 bta_gattc_mark_bg_conn(p_clreg->client_if, p_cb->bg_track[i].remote_bda, FALSE, TRUE);
303 }
304 }
305 }
306
307 if (p_clreg->num_clcb > 0)
308 {
309 /* close all CLCB related to this app */
310 for (i= 0; i < BTA_GATTC_CLCB_MAX; i ++)
311 {
312 if (p_cb->clcb[i].in_use && (p_cb->clcb[i].p_rcb == p_clreg))
313 {
314 p_clreg->dereg_pending = TRUE;
315
316 buf.event = BTA_GATTC_API_CLOSE_EVT;
317 buf.layer_specific = p_cb->clcb[i].bta_conn_id;
318 bta_gattc_close(&p_cb->clcb[i], (tBTA_GATTC_DATA *)&buf) ;
319 }
320 }
321 }
322 else
323 bta_gattc_deregister_cmpl(p_clreg);
324 }
325 else
326 {
327 APPL_TRACE_ERROR("bta_gattc_deregister Deregister Failedm unknown client cif");
328 }
329 }
330 /*******************************************************************************
331 **
332 ** Function bta_gattc_process_api_open
333 **
334 ** Description process connect API request.
335 **
336 ** Returns void
337 **
338 *******************************************************************************/
bta_gattc_process_api_open(tBTA_GATTC_CB * p_cb,tBTA_GATTC_DATA * p_msg)339 void bta_gattc_process_api_open (tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA * p_msg)
340 {
341 UINT16 event = ((BT_HDR *)p_msg)->event;
342 tBTA_GATTC_CLCB *p_clcb = NULL;
343 tBTA_GATTC_RCB *p_clreg = bta_gattc_cl_get_regcb(p_msg->api_conn.client_if);
344 UNUSED(p_cb);
345
346 if (p_clreg != NULL)
347 {
348 if (p_msg->api_conn.is_direct)
349 {
350 if ((p_clcb = bta_gattc_find_alloc_clcb(p_msg->api_conn.client_if,
351 p_msg->api_conn.remote_bda,
352 p_msg->api_conn.transport)) != NULL)
353 {
354 bta_gattc_sm_execute(p_clcb, event, p_msg);
355 }
356 else
357 {
358 APPL_TRACE_ERROR("No resources to open a new connection.");
359
360 bta_gattc_send_open_cback(p_clreg,
361 BTA_GATT_NO_RESOURCES,
362 p_msg->api_conn.remote_bda,
363 BTA_GATT_INVALID_CONN_ID,
364 p_msg->api_conn.transport, 0);
365 }
366 }
367 else
368 {
369 bta_gattc_init_bk_conn(&p_msg->api_conn, p_clreg);
370 }
371 }
372 else
373 {
374 APPL_TRACE_ERROR("bta_gattc_process_api_open Failed, unknown client_if: %d",
375 p_msg->api_conn.client_if);
376 }
377 }
378 /*******************************************************************************
379 **
380 ** Function bta_gattc_process_api_open_cancel
381 **
382 ** Description process connect API request.
383 **
384 ** Returns void
385 **
386 *******************************************************************************/
bta_gattc_process_api_open_cancel(tBTA_GATTC_CB * p_cb,tBTA_GATTC_DATA * p_msg)387 void bta_gattc_process_api_open_cancel (tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA * p_msg)
388 {
389 UINT16 event = ((BT_HDR *)p_msg)->event;
390 tBTA_GATTC_CLCB *p_clcb = NULL;
391 tBTA_GATTC_RCB *p_clreg;
392 tBTA_GATTC cb_data;
393 UNUSED(p_cb);
394
395 if (p_msg->api_cancel_conn.is_direct)
396 {
397 if ((p_clcb = bta_gattc_find_clcb_by_cif(p_msg->api_cancel_conn.client_if,
398 p_msg->api_cancel_conn.remote_bda,
399 BTA_GATT_TRANSPORT_LE)) != NULL)
400 {
401 bta_gattc_sm_execute(p_clcb, event, p_msg);
402 }
403 else
404 {
405 APPL_TRACE_ERROR("No such connection need to be cancelled");
406
407 p_clreg = bta_gattc_cl_get_regcb(p_msg->api_cancel_conn.client_if);
408
409 if (p_clreg && p_clreg->p_cback)
410 {
411 cb_data.status = BTA_GATT_ERROR;
412 (*p_clreg->p_cback)(BTA_GATTC_CANCEL_OPEN_EVT, &cb_data);
413 }
414 }
415 }
416 else
417 {
418 bta_gattc_cancel_bk_conn(&p_msg->api_cancel_conn);
419
420 }
421 }
422
423 /*******************************************************************************
424 **
425 ** Function bta_gattc_process_enc_cmpl
426 **
427 ** Description process encryption complete message.
428 **
429 ** Returns void
430 **
431 *******************************************************************************/
bta_gattc_process_enc_cmpl(tBTA_GATTC_CB * p_cb,tBTA_GATTC_DATA * p_msg)432 void bta_gattc_process_enc_cmpl(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA *p_msg)
433 {
434 tBTA_GATTC_RCB *p_clreg;
435 tBTA_GATTC cb_data;
436 UNUSED(p_cb);
437
438 p_clreg = bta_gattc_cl_get_regcb(p_msg->enc_cmpl.client_if);
439
440 if (p_clreg && p_clreg->p_cback)
441 {
442 memset(&cb_data, 0, sizeof(tBTA_GATTC));
443
444 cb_data.enc_cmpl.client_if = p_msg->enc_cmpl.client_if;
445 bdcpy(cb_data.enc_cmpl.remote_bda, p_msg->enc_cmpl.remote_bda);
446
447 (*p_clreg->p_cback)(BTA_GATTC_ENC_CMPL_CB_EVT, &cb_data);
448 }
449 }
450
451 /*******************************************************************************
452 **
453 ** Function bta_gattc_cancel_open_error
454 **
455 ** Description
456 **
457 ** Returns void
458 **
459 *******************************************************************************/
bta_gattc_cancel_open_error(tBTA_GATTC_CLCB * p_clcb,tBTA_GATTC_DATA * p_data)460 void bta_gattc_cancel_open_error(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
461 {
462 tBTA_GATTC cb_data;
463 UNUSED(p_data);
464
465 cb_data.status=BTA_GATT_ERROR;
466
467 if ( p_clcb && p_clcb->p_rcb && p_clcb->p_rcb->p_cback )
468 (*p_clcb->p_rcb->p_cback)(BTA_GATTC_CANCEL_OPEN_EVT, &cb_data);
469 }
470
471 /*******************************************************************************
472 **
473 ** Function bta_gattc_open_error
474 **
475 ** Description
476 **
477 ** Returns void
478 **
479 *******************************************************************************/
bta_gattc_open_error(tBTA_GATTC_CLCB * p_clcb,tBTA_GATTC_DATA * p_data)480 void bta_gattc_open_error(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
481 {
482 UNUSED(p_data);
483
484 APPL_TRACE_ERROR("Connection already opened. wrong state");
485
486 bta_gattc_send_open_cback(p_clcb->p_rcb,
487 BTA_GATT_OK,
488 p_clcb->bda,
489 p_clcb->bta_conn_id,
490 p_clcb->transport,
491 0);
492 }
493 /*******************************************************************************
494 **
495 ** Function bta_gattc_open_fail
496 **
497 ** Description
498 **
499 ** Returns void
500 **
501 *******************************************************************************/
bta_gattc_open_fail(tBTA_GATTC_CLCB * p_clcb,tBTA_GATTC_DATA * p_data)502 void bta_gattc_open_fail(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
503 {
504 UNUSED(p_data);
505
506 bta_gattc_send_open_cback(p_clcb->p_rcb,
507 BTA_GATT_ERROR,
508 p_clcb->bda,
509 p_clcb->bta_conn_id,
510 p_clcb->transport,
511 0);
512 /* open failure, remove clcb */
513 bta_gattc_clcb_dealloc(p_clcb);
514 }
515
516 /*******************************************************************************
517 **
518 ** Function bta_gattc_open
519 **
520 ** Description Process API connection function.
521 **
522 ** Returns void
523 **
524 *******************************************************************************/
bta_gattc_open(tBTA_GATTC_CLCB * p_clcb,tBTA_GATTC_DATA * p_data)525 void bta_gattc_open(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
526 {
527 tBTA_GATTC_DATA gattc_data;
528
529 /* open/hold a connection */
530 if (!GATT_Connect(p_clcb->p_rcb->client_if, p_data->api_conn.remote_bda,
531 TRUE, p_data->api_conn.transport))
532 {
533 APPL_TRACE_ERROR("Connection open failure");
534
535 bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_OPEN_FAIL_EVT, p_data);
536 }
537 else
538 {
539 /* a connected remote device */
540 if (GATT_GetConnIdIfConnected(p_clcb->p_rcb->client_if,
541 p_data->api_conn.remote_bda,
542 &p_clcb->bta_conn_id,
543 p_data->api_conn.transport))
544 {
545 gattc_data.int_conn.hdr.layer_specific = p_clcb->bta_conn_id;
546
547 bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_CONN_EVT, &gattc_data);
548 }
549 /* else wait for the callback event */
550 }
551 }
552 /*******************************************************************************
553 **
554 ** Function bta_gattc_init_bk_conn
555 **
556 ** Description Process API Open for a background connection
557 **
558 ** Returns void
559 **
560 *******************************************************************************/
bta_gattc_init_bk_conn(tBTA_GATTC_API_OPEN * p_data,tBTA_GATTC_RCB * p_clreg)561 void bta_gattc_init_bk_conn(tBTA_GATTC_API_OPEN *p_data, tBTA_GATTC_RCB *p_clreg)
562 {
563 tBTA_GATT_STATUS status = BTA_GATT_NO_RESOURCES;
564 UINT16 conn_id;
565 tBTA_GATTC_CLCB *p_clcb;
566 tBTA_GATTC_DATA gattc_data;
567
568 if (bta_gattc_mark_bg_conn(p_data->client_if, p_data->remote_bda, TRUE, FALSE))
569 {
570 /* always call open to hold a connection */
571 if (!GATT_Connect(p_data->client_if, p_data->remote_bda, FALSE, p_data->transport))
572 {
573 uint8_t *bda = (uint8_t *)p_data->remote_bda;
574 status = BTA_GATT_ERROR;
575 APPL_TRACE_ERROR("%s unable to connect to remote bd_addr:%02x:%02x:%02x:%02x:%02x:%02x",
576 __func__, bda[0], bda[1], bda[2], bda[3], bda[4], bda[5]);
577
578 }
579 else
580 {
581 status = BTA_GATT_OK;
582
583 /* if is a connected remote device */
584 if (GATT_GetConnIdIfConnected(p_data->client_if,
585 p_data->remote_bda,
586 &conn_id,
587 p_data->transport))
588 {
589 if ((p_clcb = bta_gattc_find_alloc_clcb(p_data->client_if, p_data->remote_bda,
590 BTA_GATT_TRANSPORT_LE)) != NULL)
591 {
592 gattc_data.hdr.layer_specific = p_clcb->bta_conn_id = conn_id;
593
594 /* open connection */
595 bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_CONN_EVT, &gattc_data);
596 status = BTA_GATT_OK;
597 }
598 }
599 }
600 }
601
602 /* open failure, report OPEN_EVT */
603 if (status != BTA_GATT_OK)
604 {
605 bta_gattc_send_open_cback(p_clreg, status, p_data->remote_bda,
606 BTA_GATT_INVALID_CONN_ID, BTA_GATT_TRANSPORT_LE, 0);
607 }
608 }
609 /*******************************************************************************
610 **
611 ** Function bta_gattc_cancel_bk_conn
612 **
613 ** Description Process API Cancel Open for a background connection
614 **
615 ** Returns void
616 **
617 *******************************************************************************/
bta_gattc_cancel_bk_conn(tBTA_GATTC_API_CANCEL_OPEN * p_data)618 void bta_gattc_cancel_bk_conn(tBTA_GATTC_API_CANCEL_OPEN *p_data)
619 {
620 tBTA_GATTC_RCB *p_clreg;
621 tBTA_GATTC cb_data;
622 cb_data.status = BTA_GATT_ERROR;
623
624 /* remove the device from the bg connection mask */
625 if (bta_gattc_mark_bg_conn(p_data->client_if, p_data->remote_bda, FALSE, FALSE))
626 {
627 if (GATT_CancelConnect(p_data->client_if, p_data->remote_bda, FALSE))
628 {
629 cb_data.status = BTA_GATT_OK;
630 }
631 else
632 {
633 APPL_TRACE_ERROR("bta_gattc_cancel_bk_conn failed");
634 }
635 }
636 p_clreg = bta_gattc_cl_get_regcb(p_data->client_if);
637
638 if (p_clreg && p_clreg->p_cback)
639 {
640 (*p_clreg->p_cback)(BTA_GATTC_CANCEL_OPEN_EVT, &cb_data);
641 }
642
643 }
644 /*******************************************************************************
645 **
646 ** Function bta_gattc_int_cancel_open_ok
647 **
648 ** Description
649 **
650 ** Returns void
651 **
652 *******************************************************************************/
bta_gattc_cancel_open_ok(tBTA_GATTC_CLCB * p_clcb,tBTA_GATTC_DATA * p_data)653 void bta_gattc_cancel_open_ok(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
654 {
655 tBTA_GATTC cb_data;
656 UNUSED(p_data);
657
658 if ( p_clcb->p_rcb->p_cback )
659 {
660 cb_data.status = BTA_GATT_OK;
661 (*p_clcb->p_rcb->p_cback)(BTA_GATTC_CANCEL_OPEN_EVT, &cb_data);
662 }
663
664 bta_gattc_clcb_dealloc(p_clcb);
665 }
666 /*******************************************************************************
667 **
668 ** Function bta_gattc_cancel_open
669 **
670 ** Description
671 **
672 ** Returns void
673 **
674 *******************************************************************************/
bta_gattc_cancel_open(tBTA_GATTC_CLCB * p_clcb,tBTA_GATTC_DATA * p_data)675 void bta_gattc_cancel_open(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
676 {
677 tBTA_GATTC cb_data;
678
679 if (GATT_CancelConnect(p_clcb->p_rcb->client_if, p_data->api_cancel_conn.remote_bda, TRUE))
680 {
681 bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_CANCEL_OPEN_OK_EVT, p_data);
682 }
683 else
684 {
685 if ( p_clcb->p_rcb->p_cback )
686 {
687 cb_data.status = BTA_GATT_ERROR;
688 (*p_clcb->p_rcb->p_cback)(BTA_GATTC_CANCEL_OPEN_EVT, &cb_data);
689 }
690 }
691 }
692 /*******************************************************************************
693 **
694 ** Function bta_gattc_conn
695 **
696 ** Description receive connection callback from stack
697 **
698 ** Returns void
699 **
700 *******************************************************************************/
bta_gattc_conn(tBTA_GATTC_CLCB * p_clcb,tBTA_GATTC_DATA * p_data)701 void bta_gattc_conn(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
702 {
703 tBTA_GATTC_IF gatt_if;
704 APPL_TRACE_DEBUG("bta_gattc_conn server cache state=%d",p_clcb->p_srcb->state);
705
706 if (p_data != NULL)
707 {
708 APPL_TRACE_DEBUG("bta_gattc_conn conn_id=%d",p_data->hdr.layer_specific);
709 p_clcb->bta_conn_id = p_data->int_conn.hdr.layer_specific;
710
711 GATT_GetConnectionInfor(p_data->hdr.layer_specific,
712 &gatt_if, p_clcb->bda, &p_clcb->transport);
713 }
714
715 p_clcb->p_srcb->connected = TRUE;
716
717 if (p_clcb->p_srcb->mtu == 0)
718 p_clcb->p_srcb->mtu = GATT_DEF_BLE_MTU_SIZE;
719
720 /* start database cache if needed */
721 if (p_clcb->p_srcb->p_srvc_cache == NULL ||
722 p_clcb->p_srcb->state != BTA_GATTC_SERV_IDLE)
723 {
724 if (p_clcb->p_srcb->state == BTA_GATTC_SERV_IDLE)
725 {
726 p_clcb->p_srcb->state = BTA_GATTC_SERV_LOAD;
727 bta_gattc_sm_execute(p_clcb, BTA_GATTC_START_CACHE_EVT, NULL);
728 }
729 else /* cache is building */
730 p_clcb->state = BTA_GATTC_DISCOVER_ST;
731 }
732
733 else
734 {
735 /* a pending service handle change indication */
736 if (p_clcb->p_srcb->srvc_hdl_chg)
737 {
738 p_clcb->p_srcb->srvc_hdl_chg = FALSE;
739 /* start discovery */
740 bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_DISCOVER_EVT, NULL);
741 }
742 }
743
744 if (p_clcb->p_rcb)
745 {
746 /* there is no RM for GATT */
747 if (p_clcb->transport == BTA_TRANSPORT_BR_EDR)
748 bta_sys_conn_open(BTA_ID_GATTC, BTA_ALL_APP_ID, p_clcb->bda);
749
750 bta_gattc_send_open_cback(p_clcb->p_rcb,
751 BTA_GATT_OK,
752 p_clcb->bda,
753 p_clcb->bta_conn_id,
754 p_clcb->transport,
755 p_clcb->p_srcb->mtu);
756 }
757 }
758 /*******************************************************************************
759 **
760 ** Function bta_gattc_close_fail
761 **
762 ** Description close a connection.
763 **
764 ** Returns void
765 **
766 *******************************************************************************/
bta_gattc_close_fail(tBTA_GATTC_CLCB * p_clcb,tBTA_GATTC_DATA * p_data)767 void bta_gattc_close_fail(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
768 {
769 tBTA_GATTC cb_data;
770
771 if ( p_clcb->p_rcb->p_cback )
772 {
773 memset(&cb_data, 0, sizeof(tBTA_GATTC));
774 cb_data.close.client_if = p_clcb->p_rcb->client_if;
775 cb_data.close.conn_id = p_data->hdr.layer_specific;
776 bdcpy(cb_data.close.remote_bda, p_clcb->bda);
777 cb_data.close.status = BTA_GATT_ERROR;
778 cb_data.close.reason = BTA_GATT_CONN_NONE;
779
780
781 (*p_clcb->p_rcb->p_cback)(BTA_GATTC_CLOSE_EVT, &cb_data);
782 }
783 }
784 /*******************************************************************************
785 **
786 ** Function bta_gattc_api_close
787 **
788 ** Description close a GATTC connection.
789 **
790 ** Returns void
791 **
792 *******************************************************************************/
bta_gattc_close(tBTA_GATTC_CLCB * p_clcb,tBTA_GATTC_DATA * p_data)793 void bta_gattc_close(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
794 {
795 tBTA_GATTC_CBACK *p_cback = p_clcb->p_rcb->p_cback;
796 tBTA_GATTC_RCB *p_clreg = p_clcb->p_rcb;
797 tBTA_GATTC cb_data;
798
799 APPL_TRACE_DEBUG("bta_gattc_close conn_id=%d",p_clcb->bta_conn_id);
800
801 cb_data.close.client_if = p_clcb->p_rcb->client_if;
802 cb_data.close.conn_id = p_clcb->bta_conn_id;
803 cb_data.close.reason = p_clcb->reason;
804 cb_data.close.status = p_clcb->status;
805 bdcpy(cb_data.close.remote_bda, p_clcb->bda);
806
807 if (p_clcb->transport == BTA_TRANSPORT_BR_EDR)
808 bta_sys_conn_close( BTA_ID_GATTC ,BTA_ALL_APP_ID, p_clcb->bda);
809
810 bta_gattc_clcb_dealloc(p_clcb);
811
812 if (p_data->hdr.event == BTA_GATTC_API_CLOSE_EVT)
813 {
814 cb_data.close.status = GATT_Disconnect(p_data->hdr.layer_specific);
815 }
816 else if (p_data->hdr.event == BTA_GATTC_INT_DISCONN_EVT)
817 {
818 cb_data.close.status = p_data->int_conn.reason;
819 cb_data.close.reason = p_data->int_conn.reason;
820 }
821
822 if(p_cback)
823 (* p_cback)(BTA_GATTC_CLOSE_EVT, (tBTA_GATTC *)&cb_data);
824
825 if (p_clreg->num_clcb == 0 && p_clreg->dereg_pending)
826 {
827 bta_gattc_deregister_cmpl(p_clreg);
828 }
829 }
830 /*******************************************************************************
831 **
832 ** Function bta_gattc_reset_discover_st
833 **
834 ** Description when a SRCB finished discovery, tell all related clcb.
835 **
836 ** Returns None.
837 **
838 *******************************************************************************/
bta_gattc_reset_discover_st(tBTA_GATTC_SERV * p_srcb,tBTA_GATT_STATUS status)839 void bta_gattc_reset_discover_st(tBTA_GATTC_SERV *p_srcb, tBTA_GATT_STATUS status)
840 {
841 tBTA_GATTC_CB *p_cb = &bta_gattc_cb;
842 UINT8 i;
843
844 for (i = 0; i < BTA_GATTC_CLCB_MAX; i ++)
845 {
846 if (p_cb->clcb[i].p_srcb == p_srcb)
847 {
848 p_cb->clcb[i].status = status;
849 bta_gattc_sm_execute(&p_cb->clcb[i], BTA_GATTC_DISCOVER_CMPL_EVT, NULL);
850 }
851 }
852 }
853 /*******************************************************************************
854 **
855 ** Function bta_gattc_disc_close
856 **
857 ** Description close a GATTC connection while in discovery state.
858 **
859 ** Returns void
860 **
861 *******************************************************************************/
bta_gattc_disc_close(tBTA_GATTC_CLCB * p_clcb,tBTA_GATTC_DATA * p_data)862 void bta_gattc_disc_close(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
863 {
864 APPL_TRACE_DEBUG("%s: Discovery cancel conn_id=%d", __func__,
865 p_clcb->bta_conn_id);
866
867 if (p_clcb->disc_active)
868 bta_gattc_reset_discover_st(p_clcb->p_srcb, BTA_GATT_ERROR);
869 else
870 p_clcb->state = BTA_GATTC_CONN_ST;
871
872 // This function only gets called as the result of a BTA_GATTC_API_CLOSE_EVT
873 // while in the BTA_GATTC_DISCOVER_ST state. Once the state changes, the
874 // connection itself still needs to be closed to resolve the original event.
875 if (p_clcb->state == BTA_GATTC_CONN_ST)
876 {
877 APPL_TRACE_DEBUG("State is back to BTA_GATTC_CONN_ST. "
878 "Trigger connection close");
879 bta_gattc_close(p_clcb, p_data);
880 }
881 }
882 /*******************************************************************************
883 **
884 ** Function bta_gattc_set_discover_st
885 **
886 ** Description when a SRCB start discovery, tell all related clcb and set
887 ** the state.
888 **
889 ** Returns None.
890 **
891 *******************************************************************************/
bta_gattc_set_discover_st(tBTA_GATTC_SERV * p_srcb)892 void bta_gattc_set_discover_st(tBTA_GATTC_SERV *p_srcb)
893 {
894 tBTA_GATTC_CB *p_cb = &bta_gattc_cb;
895 UINT8 i;
896
897 #if BLE_INCLUDED == TRUE
898 L2CA_EnableUpdateBleConnParams(p_srcb->server_bda, FALSE);
899 #endif
900 for (i = 0; i < BTA_GATTC_CLCB_MAX; i ++)
901 {
902 if (p_cb->clcb[i].p_srcb == p_srcb)
903 {
904 p_cb->clcb[i].status = BTA_GATT_OK;
905 p_cb->clcb[i].state = BTA_GATTC_DISCOVER_ST;
906 }
907 }
908 }
909 /*******************************************************************************
910 **
911 ** Function bta_gattc_restart_discover
912 **
913 ** Description process service change in discovery state, mark up the auto
914 ** update flag and set status to be discovery cancel for current
915 ** discovery.
916 **
917 ** Returns None.
918 **
919 *******************************************************************************/
bta_gattc_restart_discover(tBTA_GATTC_CLCB * p_clcb,tBTA_GATTC_DATA * p_data)920 void bta_gattc_restart_discover(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
921 {
922 UNUSED(p_data);
923
924 p_clcb->status = BTA_GATT_CANCEL;
925 p_clcb->auto_update = BTA_GATTC_DISC_WAITING;
926 }
927
928 /*******************************************************************************
929 **
930 ** Function bta_gattc_cfg_mtu
931 **
932 ** Description Configure MTU size on the GATT connection.
933 **
934 ** Returns None.
935 **
936 *******************************************************************************/
bta_gattc_cfg_mtu(tBTA_GATTC_CLCB * p_clcb,tBTA_GATTC_DATA * p_data)937 void bta_gattc_cfg_mtu(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
938 {
939 tBTA_GATT_STATUS status;
940
941 if (bta_gattc_enqueue(p_clcb, p_data))
942 {
943 status = GATTC_ConfigureMTU (p_clcb->bta_conn_id, p_data->api_mtu.mtu);
944
945 /* if failed, return callback here */
946 if (status != GATT_SUCCESS && status != GATT_CMD_STARTED)
947 {
948 /* Dequeue the data, if it was enqueued */
949 if (p_clcb->p_q_cmd == p_data)
950 p_clcb->p_q_cmd = NULL;
951
952 bta_gattc_cmpl_sendmsg(p_clcb->bta_conn_id, GATTC_OPTYPE_CONFIG, status, NULL);
953 }
954 }
955 }
956 /*******************************************************************************
957 **
958 ** Function bta_gattc_start_discover
959 **
960 ** Description Start a discovery on server.
961 **
962 ** Returns None.
963 **
964 *******************************************************************************/
bta_gattc_start_discover(tBTA_GATTC_CLCB * p_clcb,tBTA_GATTC_DATA * p_data)965 void bta_gattc_start_discover(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
966 {
967 UNUSED(p_data);
968
969 APPL_TRACE_DEBUG("bta_gattc_start_discover conn_id=%d p_clcb->p_srcb->state = %d ",
970 p_clcb->bta_conn_id, p_clcb->p_srcb->state);
971
972 if (((p_clcb->p_q_cmd == NULL || p_clcb->auto_update == BTA_GATTC_REQ_WAITING) &&
973 p_clcb->p_srcb->state == BTA_GATTC_SERV_IDLE) ||
974 p_clcb->p_srcb->state == BTA_GATTC_SERV_DISC)
975 /* no pending operation, start discovery right away */
976 {
977 p_clcb->auto_update = BTA_GATTC_NO_SCHEDULE;
978
979 if (p_clcb->p_srcb != NULL)
980 {
981 /* clear the service change mask */
982 p_clcb->p_srcb->srvc_hdl_chg = FALSE;
983 p_clcb->p_srcb->update_count = 0;
984 p_clcb->p_srcb->state = BTA_GATTC_SERV_DISC_ACT;
985
986 if (p_clcb->transport == BTA_TRANSPORT_LE)
987 L2CA_EnableUpdateBleConnParams(p_clcb->p_srcb->server_bda, FALSE);
988
989 /* set all srcb related clcb into discovery ST */
990 bta_gattc_set_discover_st(p_clcb->p_srcb);
991
992 if ((p_clcb->status = bta_gattc_init_cache(p_clcb->p_srcb)) == BTA_GATT_OK)
993 {
994 p_clcb->status = bta_gattc_discover_pri_service(p_clcb->bta_conn_id,
995 p_clcb->p_srcb, GATT_DISC_SRVC_ALL);
996 }
997 if (p_clcb->status != BTA_GATT_OK)
998 {
999 APPL_TRACE_ERROR("discovery on server failed");
1000 bta_gattc_reset_discover_st(p_clcb->p_srcb, p_clcb->status);
1001 }
1002 else
1003 p_clcb->disc_active = TRUE;
1004 }
1005 else
1006 {
1007 APPL_TRACE_ERROR("unknown device, can not start discovery");
1008 }
1009 }
1010 /* pending operation, wait until it finishes */
1011 else
1012 {
1013 p_clcb->auto_update = BTA_GATTC_DISC_WAITING;
1014
1015 if (p_clcb->p_srcb->state == BTA_GATTC_SERV_IDLE)
1016 p_clcb->state = BTA_GATTC_CONN_ST; /* set clcb state */
1017 }
1018
1019 }
1020 /*******************************************************************************
1021 **
1022 ** Function bta_gattc_disc_cmpl
1023 **
1024 ** Description discovery on server is finished
1025 **
1026 ** Returns None.
1027 **
1028 *******************************************************************************/
bta_gattc_disc_cmpl(tBTA_GATTC_CLCB * p_clcb,tBTA_GATTC_DATA * p_data)1029 void bta_gattc_disc_cmpl(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1030 {
1031 tBTA_GATTC_DATA *p_q_cmd = p_clcb->p_q_cmd;
1032 UNUSED(p_data);
1033
1034 APPL_TRACE_DEBUG("bta_gattc_disc_cmpl conn_id=%d",p_clcb->bta_conn_id);
1035
1036 #if BLE_INCLUDED == TRUE
1037 if(p_clcb->transport == BTA_TRANSPORT_LE)
1038 L2CA_EnableUpdateBleConnParams(p_clcb->p_srcb->server_bda, TRUE);
1039 #endif
1040 p_clcb->p_srcb->state = BTA_GATTC_SERV_IDLE;
1041 p_clcb->disc_active = FALSE;
1042
1043 if (p_clcb->status != GATT_SUCCESS)
1044 {
1045 /* clean up cache */
1046 if(p_clcb->p_srcb && p_clcb->p_srcb->p_srvc_cache)
1047 {
1048 while (!GKI_queue_is_empty(&p_clcb->p_srcb->cache_buffer))
1049 {
1050 GKI_freebuf (GKI_dequeue (&p_clcb->p_srcb->cache_buffer));
1051 }
1052 p_clcb->p_srcb->p_srvc_cache = NULL;
1053 }
1054
1055 /* used to reset cache in application */
1056 bta_gattc_co_cache_reset(p_clcb->p_srcb->server_bda);
1057 }
1058 /* release pending attribute list buffer */
1059 utl_freebuf((void **)&p_clcb->p_srcb->p_srvc_list);
1060
1061 if (p_clcb->auto_update == BTA_GATTC_DISC_WAITING)
1062 {
1063 /* start discovery again */
1064 bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_DISCOVER_EVT, NULL);
1065 }
1066 /* get any queued command to proceed */
1067 else if (p_q_cmd != NULL)
1068 {
1069 p_clcb->p_q_cmd = NULL;
1070
1071 bta_gattc_sm_execute(p_clcb, p_q_cmd->hdr.event, p_q_cmd);
1072 /* if the command executed requeued the cmd, we don't
1073 * want to free the underlying buffer that's being
1074 * referenced by p_clcb->p_q_cmd
1075 */
1076 if (p_q_cmd != p_clcb->p_q_cmd) {
1077 utl_freebuf((void **)&p_q_cmd);
1078 }
1079 }
1080 }
1081 /*******************************************************************************
1082 **
1083 ** Function bta_gattc_read
1084 **
1085 ** Description Read an attribute
1086 **
1087 ** Returns None.
1088 **
1089 *******************************************************************************/
bta_gattc_read(tBTA_GATTC_CLCB * p_clcb,tBTA_GATTC_DATA * p_data)1090 void bta_gattc_read(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1091 {
1092 UINT16 handle = 0;
1093 tGATT_READ_PARAM read_param;
1094 tBTA_GATT_STATUS status;
1095
1096 memset (&read_param, 0 ,sizeof(tGATT_READ_PARAM));
1097
1098 if (bta_gattc_enqueue(p_clcb, p_data))
1099 {
1100 if ((handle = bta_gattc_id2handle(p_clcb->p_srcb,
1101 &p_data->api_read.srvc_id,
1102 &p_data->api_read.char_id,
1103 p_data->api_read.p_descr_type)) == 0)
1104 {
1105 status = BTA_GATT_ERROR;
1106 }
1107 else
1108 {
1109 read_param.by_handle.handle = handle;
1110 read_param.by_handle.auth_req = p_data->api_read.auth_req;
1111
1112 status = GATTC_Read(p_clcb->bta_conn_id, GATT_READ_BY_HANDLE, &read_param);
1113 }
1114
1115 /* read fail */
1116 if (status != BTA_GATT_OK)
1117 {
1118 /* Dequeue the data, if it was enqueued */
1119 if (p_clcb->p_q_cmd == p_data)
1120 p_clcb->p_q_cmd = NULL;
1121
1122 bta_gattc_cmpl_sendmsg(p_clcb->bta_conn_id, GATTC_OPTYPE_READ, status, NULL);
1123 }
1124 }
1125 }
1126 /*******************************************************************************
1127 **
1128 ** Function bta_gattc_read_multi
1129 **
1130 ** Description read multiple
1131 **
1132 ** Returns None.
1133 *********************************************************************************/
bta_gattc_read_multi(tBTA_GATTC_CLCB * p_clcb,tBTA_GATTC_DATA * p_data)1134 void bta_gattc_read_multi(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1135 {
1136 UINT16 i, handle;
1137 tBTA_GATT_STATUS status = BTA_GATT_OK;
1138 tGATT_READ_PARAM read_param;
1139 tBTA_GATTC_ATTR_ID *p_id;
1140
1141 if (bta_gattc_enqueue(p_clcb, p_data))
1142 {
1143 memset(&read_param, 0, sizeof(tGATT_READ_PARAM));
1144
1145 p_id = p_data->api_read_multi.p_id_list;
1146
1147 for (i = 0; i < p_data->api_read_multi.num_attr && p_id; i ++, p_id ++)
1148 {
1149 handle = 0;
1150
1151 if (p_id->id_type == BTA_GATT_TYPE_CHAR)
1152 {
1153 handle = bta_gattc_id2handle(p_clcb->p_srcb,
1154 &p_id->id_value.char_id.srvc_id,
1155 &p_id->id_value.char_id.char_id,
1156 NULL);
1157 }
1158 else if (p_id->id_type == BTA_GATT_TYPE_CHAR_DESCR)
1159 {
1160 handle = bta_gattc_id2handle(p_clcb->p_srcb,
1161 &p_id->id_value.char_descr_id.char_id.srvc_id,
1162 &p_id->id_value.char_descr_id.char_id.char_id,
1163 &p_id->id_value.char_descr_id.descr_id);
1164 }
1165 else
1166 {
1167 APPL_TRACE_ERROR("invalud ID type: %d", p_id->id_type);
1168 }
1169
1170 if (handle == 0)
1171 {
1172 status = BTA_GATT_ERROR;
1173 break;
1174 }
1175 }
1176 if (status == BTA_GATT_OK)
1177 {
1178 read_param.read_multiple.num_handles = p_data->api_read_multi.num_attr;
1179 read_param.read_multiple.auth_req = p_data->api_read_multi.auth_req;
1180
1181 status = GATTC_Read(p_clcb->bta_conn_id, GATT_READ_MULTIPLE, &read_param);
1182 }
1183
1184 /* read fail */
1185 if (status != BTA_GATT_OK)
1186 {
1187 /* Dequeue the data, if it was enqueued */
1188 if (p_clcb->p_q_cmd == p_data)
1189 p_clcb->p_q_cmd = NULL;
1190
1191 bta_gattc_cmpl_sendmsg(p_clcb->bta_conn_id, GATTC_OPTYPE_READ, status, NULL);
1192 }
1193 }
1194 }
1195 /*******************************************************************************
1196 **
1197 ** Function bta_gattc_write
1198 **
1199 ** Description Write an attribute
1200 **
1201 ** Returns None.
1202 **
1203 *******************************************************************************/
bta_gattc_write(tBTA_GATTC_CLCB * p_clcb,tBTA_GATTC_DATA * p_data)1204 void bta_gattc_write(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1205 {
1206 UINT16 handle = 0;
1207 tGATT_VALUE attr = {0};
1208 tBTA_GATT_STATUS status = BTA_GATT_OK;
1209
1210 if (bta_gattc_enqueue(p_clcb, p_data))
1211 {
1212 if ((handle = bta_gattc_id2handle(p_clcb->p_srcb,
1213 &p_data->api_write.srvc_id,
1214 &p_data->api_write.char_id,
1215 p_data->api_write.p_descr_type)) == 0)
1216 {
1217 status = BTA_GATT_ERROR;
1218 }
1219 else
1220 {
1221 attr.handle= handle;
1222 attr.offset = p_data->api_write.offset;
1223 attr.len = p_data->api_write.len;
1224 attr.auth_req = p_data->api_write.auth_req;
1225
1226 if (p_data->api_write.p_value)
1227 memcpy(attr.value, p_data->api_write.p_value, p_data->api_write.len);
1228
1229 status = GATTC_Write(p_clcb->bta_conn_id, p_data->api_write.write_type, &attr);
1230 }
1231
1232 /* write fail */
1233 if (status != BTA_GATT_OK)
1234 {
1235 /* Dequeue the data, if it was enqueued */
1236 if (p_clcb->p_q_cmd == p_data)
1237 p_clcb->p_q_cmd = NULL;
1238
1239 bta_gattc_cmpl_sendmsg(p_clcb->bta_conn_id, GATTC_OPTYPE_WRITE, status, NULL);
1240 }
1241 }
1242 }
1243 /*******************************************************************************
1244 **
1245 ** Function bta_gattc_execute
1246 **
1247 ** Description send execute write
1248 **
1249 ** Returns None.
1250 *********************************************************************************/
bta_gattc_execute(tBTA_GATTC_CLCB * p_clcb,tBTA_GATTC_DATA * p_data)1251 void bta_gattc_execute(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1252 {
1253 tBTA_GATT_STATUS status;
1254
1255 if (bta_gattc_enqueue(p_clcb, p_data))
1256 {
1257 status = GATTC_ExecuteWrite(p_clcb->bta_conn_id, p_data->api_exec.is_execute);
1258
1259 if (status != BTA_GATT_OK)
1260 {
1261 /* Dequeue the data, if it was enqueued */
1262 if (p_clcb->p_q_cmd == p_data)
1263 p_clcb->p_q_cmd = NULL;
1264
1265 bta_gattc_cmpl_sendmsg(p_clcb->bta_conn_id, GATTC_OPTYPE_EXE_WRITE, status, NULL);
1266 }
1267 }
1268 }
1269 /*******************************************************************************
1270 **
1271 ** Function bta_gattc_confirm
1272 **
1273 ** Description send handle value confirmation
1274 **
1275 ** Returns None.
1276 **
1277 *******************************************************************************/
bta_gattc_confirm(tBTA_GATTC_CLCB * p_clcb,tBTA_GATTC_DATA * p_data)1278 void bta_gattc_confirm(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1279 {
1280 UINT16 handle;
1281
1282 if ((handle = bta_gattc_id2handle(p_clcb->p_srcb,
1283 &p_data->api_confirm.srvc_id,
1284 &p_data->api_confirm.char_id,
1285 NULL)) == 0)
1286 {
1287 APPL_TRACE_ERROR("Can not map service/char ID into valid handle");
1288 }
1289 else
1290 {
1291 if (GATTC_SendHandleValueConfirm(p_data->api_confirm.hdr.layer_specific, handle)
1292 != GATT_SUCCESS)
1293 {
1294 APPL_TRACE_ERROR("bta_gattc_confirm to handle [0x%04x] failed", handle);
1295 }
1296 else
1297 {
1298 /* if over BR_EDR, inform PM for mode change */
1299 if (p_clcb->transport == BTA_TRANSPORT_BR_EDR)
1300 {
1301 bta_sys_busy(BTA_ID_GATTC, BTA_ALL_APP_ID, p_clcb->bda);
1302 bta_sys_idle(BTA_ID_GATTC, BTA_ALL_APP_ID, p_clcb->bda);
1303 }
1304 }
1305 }
1306 }
1307 /*******************************************************************************
1308 **
1309 ** Function bta_gattc_read_cmpl
1310 **
1311 ** Description read complete
1312 **
1313 ** Returns None.
1314 **
1315 *******************************************************************************/
bta_gattc_read_cmpl(tBTA_GATTC_CLCB * p_clcb,tBTA_GATTC_OP_CMPL * p_data)1316 void bta_gattc_read_cmpl(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_OP_CMPL *p_data)
1317 {
1318 UINT8 event;
1319 tBTA_GATTC cb_data;
1320 tBTA_GATT_READ_VAL read_value;
1321
1322 memset(&cb_data, 0, sizeof(tBTA_GATTC));
1323 memset(&read_value, 0, sizeof(tBTA_GATT_READ_VAL));
1324
1325 cb_data.read.status = p_data->status;
1326
1327 if (p_data->p_cmpl != NULL && p_data->status == BTA_GATT_OK)
1328 {
1329 if (bta_gattc_handle2id(p_clcb->p_srcb,
1330 p_data->p_cmpl->att_value.handle,
1331 &cb_data.read.srvc_id,
1332 &cb_data.read.char_id,
1333 &cb_data.read.descr_type) == FALSE)
1334 {
1335 cb_data.read.status = BTA_GATT_INTERNAL_ERROR;
1336 APPL_TRACE_ERROR("can not map to GATT ID. handle = 0x%04x",
1337 p_data->p_cmpl->att_value.handle);
1338 }
1339 else
1340 {
1341 cb_data.read.status = bta_gattc_pack_read_cb_data(p_clcb->p_srcb,
1342 &cb_data.read.descr_type.uuid,
1343 &p_data->p_cmpl->att_value,
1344 &read_value);
1345 cb_data.read.p_value = &read_value;
1346 }
1347 }
1348 else
1349 {
1350 cb_data.read.srvc_id = p_clcb->p_q_cmd->api_read.srvc_id;
1351 cb_data.read.char_id = p_clcb->p_q_cmd->api_read.char_id;
1352 if (p_clcb->p_q_cmd->api_read.p_descr_type)
1353 memcpy(&cb_data.read.descr_type, p_clcb->p_q_cmd->api_read.p_descr_type,
1354 sizeof(tBTA_GATT_ID));
1355 }
1356
1357 event = (p_clcb->p_q_cmd->api_read.p_descr_type == NULL) ?
1358 BTA_GATTC_READ_CHAR_EVT: BTA_GATTC_READ_DESCR_EVT;
1359 cb_data.read.conn_id = p_clcb->bta_conn_id;
1360
1361 utl_freebuf((void **)&p_clcb->p_q_cmd);
1362 /* read complete, callback */
1363 ( *p_clcb->p_rcb->p_cback)(event, (tBTA_GATTC *)&cb_data);
1364
1365 }
1366 /*******************************************************************************
1367 **
1368 ** Function bta_gattc_write_cmpl
1369 **
1370 ** Description write complete
1371 **
1372 ** Returns None.
1373 **
1374 *******************************************************************************/
bta_gattc_write_cmpl(tBTA_GATTC_CLCB * p_clcb,tBTA_GATTC_OP_CMPL * p_data)1375 void bta_gattc_write_cmpl(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_OP_CMPL *p_data)
1376 {
1377 tBTA_GATTC cb_data = {0};
1378 UINT8 event;
1379
1380 memset(&cb_data, 0, sizeof(tBTA_GATTC));
1381
1382 cb_data.write.status = p_data->status;
1383
1384 if (p_data->p_cmpl != NULL)
1385 {
1386 bta_gattc_handle2id(p_clcb->p_srcb, p_data->p_cmpl->att_value.handle,
1387 &cb_data.write.srvc_id, &cb_data.write.char_id,
1388 &cb_data.write.descr_type);
1389 }
1390 else
1391 {
1392 memcpy(&cb_data.write.srvc_id, &p_clcb->p_q_cmd->api_write.srvc_id,
1393 sizeof(tBTA_GATT_SRVC_ID));
1394 memcpy(&cb_data.write.char_id, &p_clcb->p_q_cmd->api_write.char_id,
1395 sizeof(tBTA_GATT_ID));
1396 if (p_clcb->p_q_cmd->api_write.p_descr_type)
1397 memcpy(&cb_data.write.descr_type, p_clcb->p_q_cmd->api_write.p_descr_type,
1398 sizeof(tBTA_GATT_ID));
1399 }
1400
1401 if (p_clcb->p_q_cmd->api_write.hdr.event == BTA_GATTC_API_WRITE_EVT &&
1402 p_clcb->p_q_cmd->api_write.write_type == BTA_GATTC_WRITE_PREPARE)
1403
1404 event = BTA_GATTC_PREP_WRITE_EVT;
1405
1406 else if (p_clcb->p_q_cmd->api_write.p_descr_type == NULL)
1407
1408 event = BTA_GATTC_WRITE_CHAR_EVT;
1409
1410 else
1411 event = BTA_GATTC_WRITE_DESCR_EVT;
1412
1413 utl_freebuf((void **)&p_clcb->p_q_cmd);
1414 cb_data.write.conn_id = p_clcb->bta_conn_id;
1415 /* write complete, callback */
1416 ( *p_clcb->p_rcb->p_cback)(event, (tBTA_GATTC *)&cb_data);
1417
1418 }
1419 /*******************************************************************************
1420 **
1421 ** Function bta_gattc_exec_cmpl
1422 **
1423 ** Description execute write complete
1424 **
1425 ** Returns None.
1426 **
1427 *******************************************************************************/
bta_gattc_exec_cmpl(tBTA_GATTC_CLCB * p_clcb,tBTA_GATTC_OP_CMPL * p_data)1428 void bta_gattc_exec_cmpl(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_OP_CMPL *p_data)
1429 {
1430 tBTA_GATTC cb_data;
1431
1432 utl_freebuf((void **)&p_clcb->p_q_cmd);
1433
1434 p_clcb->status = BTA_GATT_OK;
1435
1436 /* execute complete, callback */
1437 cb_data.exec_cmpl.conn_id = p_clcb->bta_conn_id;
1438 cb_data.exec_cmpl.status = p_data->status;
1439
1440 ( *p_clcb->p_rcb->p_cback)(BTA_GATTC_EXEC_EVT, &cb_data);
1441
1442 }
1443
1444 /*******************************************************************************
1445 **
1446 ** Function bta_gattc_cfg_mtu_cmpl
1447 **
1448 ** Description configure MTU operation complete
1449 **
1450 ** Returns None.
1451 **
1452 *******************************************************************************/
bta_gattc_cfg_mtu_cmpl(tBTA_GATTC_CLCB * p_clcb,tBTA_GATTC_OP_CMPL * p_data)1453 void bta_gattc_cfg_mtu_cmpl(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_OP_CMPL *p_data)
1454 {
1455 tBTA_GATTC cb_data;
1456
1457 utl_freebuf((void **)&p_clcb->p_q_cmd);
1458
1459
1460 if (p_data->p_cmpl && p_data->status == BTA_GATT_OK)
1461 p_clcb->p_srcb->mtu = p_data->p_cmpl->mtu;
1462
1463 /* configure MTU complete, callback */
1464 p_clcb->status = p_data->status;
1465 cb_data.cfg_mtu.conn_id = p_clcb->bta_conn_id;
1466 cb_data.cfg_mtu.status = p_data->status;
1467 cb_data.cfg_mtu.mtu = p_clcb->p_srcb->mtu;
1468
1469 (*p_clcb->p_rcb->p_cback) (BTA_GATTC_CFG_MTU_EVT, &cb_data);
1470
1471 }
1472 /*******************************************************************************
1473 **
1474 ** Function bta_gattc_op_cmpl
1475 **
1476 ** Description operation completed.
1477 **
1478 ** Returns None.
1479 **
1480 *******************************************************************************/
bta_gattc_op_cmpl(tBTA_GATTC_CLCB * p_clcb,tBTA_GATTC_DATA * p_data)1481 void bta_gattc_op_cmpl(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1482 {
1483 UINT8 op = (UINT8)p_data->op_cmpl.op_code;
1484 UINT8 mapped_op = 0;
1485
1486 APPL_TRACE_DEBUG("bta_gattc_op_cmpl op = %d", op);
1487
1488 if (op == GATTC_OPTYPE_INDICATION || op == GATTC_OPTYPE_NOTIFICATION)
1489 {
1490 APPL_TRACE_ERROR("unexpected operation, ignored");
1491 }
1492 else if (op >= GATTC_OPTYPE_READ)
1493 {
1494 if (p_clcb->p_q_cmd == NULL)
1495 {
1496 APPL_TRACE_ERROR("No pending command");
1497 return;
1498 }
1499 if (p_clcb->p_q_cmd->hdr.event != bta_gattc_opcode_to_int_evt[op - GATTC_OPTYPE_READ])
1500 {
1501 mapped_op = p_clcb->p_q_cmd->hdr.event - BTA_GATTC_API_READ_EVT + GATTC_OPTYPE_READ;
1502 if ( mapped_op > GATTC_OPTYPE_INDICATION) mapped_op = 0;
1503
1504 #if (BT_TRACE_VERBOSE == TRUE)
1505 APPL_TRACE_ERROR("expect op:(%s :0x%04x), receive unexpected operation (%s).",
1506 bta_gattc_op_code_name[mapped_op] , p_clcb->p_q_cmd->hdr.event,
1507 bta_gattc_op_code_name[op]);
1508 #else
1509 APPL_TRACE_ERROR("expect op:(%u :0x%04x), receive unexpected operation (%u).",
1510 mapped_op , p_clcb->p_q_cmd->hdr.event, op);
1511 #endif
1512 return;
1513 }
1514
1515 /* discard responses if service change indication is received before operation completed */
1516 if (p_clcb->auto_update == BTA_GATTC_DISC_WAITING && p_clcb->p_srcb->srvc_hdl_chg)
1517 {
1518 APPL_TRACE_DEBUG("Discard all responses when service change indication is received.");
1519 p_data->op_cmpl.status = GATT_ERROR;
1520 }
1521
1522 /* service handle change void the response, discard it */
1523 if (op == GATTC_OPTYPE_READ)
1524 bta_gattc_read_cmpl(p_clcb, &p_data->op_cmpl);
1525
1526 else if (op == GATTC_OPTYPE_WRITE)
1527 bta_gattc_write_cmpl(p_clcb, &p_data->op_cmpl);
1528
1529 else if (op == GATTC_OPTYPE_EXE_WRITE)
1530 bta_gattc_exec_cmpl(p_clcb, &p_data->op_cmpl);
1531
1532 else if (op == GATTC_OPTYPE_CONFIG)
1533 bta_gattc_cfg_mtu_cmpl(p_clcb, &p_data->op_cmpl);
1534
1535 if (p_clcb->auto_update == BTA_GATTC_DISC_WAITING)
1536 {
1537 p_clcb->auto_update = BTA_GATTC_REQ_WAITING;
1538 bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_DISCOVER_EVT, NULL);
1539 }
1540 }
1541 }
1542 /*******************************************************************************
1543 **
1544 ** Function bta_gattc_op_cmpl
1545 **
1546 ** Description operation completed.
1547 **
1548 ** Returns None.
1549 **
1550 *******************************************************************************/
bta_gattc_ignore_op_cmpl(tBTA_GATTC_CLCB * p_clcb,tBTA_GATTC_DATA * p_data)1551 void bta_gattc_ignore_op_cmpl(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1552 {
1553 UNUSED(p_clcb);
1554
1555 /* receive op complete when discovery is started, ignore the response,
1556 and wait for discovery finish and resent */
1557 APPL_TRACE_DEBUG("bta_gattc_ignore_op_cmpl op = %d", p_data->hdr.layer_specific);
1558
1559 }
1560 /*******************************************************************************
1561 **
1562 ** Function bta_gattc_search
1563 **
1564 ** Description start a search in the local server cache
1565 **
1566 ** Returns None.
1567 **
1568 *******************************************************************************/
bta_gattc_search(tBTA_GATTC_CLCB * p_clcb,tBTA_GATTC_DATA * p_data)1569 void bta_gattc_search(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1570 {
1571 tBTA_GATT_STATUS status = GATT_INTERNAL_ERROR;
1572 tBTA_GATTC cb_data;
1573 APPL_TRACE_DEBUG("bta_gattc_search conn_id=%d",p_clcb->bta_conn_id);
1574 if (p_clcb->p_srcb && p_clcb->p_srcb->p_srvc_cache)
1575 {
1576 status = BTA_GATT_OK;
1577 /* search the local cache of a server device */
1578 bta_gattc_search_service(p_clcb, p_data->api_search.p_srvc_uuid);
1579 }
1580 cb_data.search_cmpl.status = status;
1581 cb_data.search_cmpl.conn_id = p_clcb->bta_conn_id;
1582
1583 /* end of search or no server cache available */
1584 ( *p_clcb->p_rcb->p_cback)(BTA_GATTC_SEARCH_CMPL_EVT, &cb_data);
1585 }
1586 /*******************************************************************************
1587 **
1588 ** Function bta_gattc_q_cmd
1589 **
1590 ** Description enqueue a command into control block, usually because discovery
1591 ** operation is busy.
1592 **
1593 ** Returns None.
1594 **
1595 *******************************************************************************/
bta_gattc_q_cmd(tBTA_GATTC_CLCB * p_clcb,tBTA_GATTC_DATA * p_data)1596 void bta_gattc_q_cmd(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1597 {
1598 bta_gattc_enqueue(p_clcb, p_data);
1599 }
1600 /*******************************************************************************
1601 **
1602 ** Function bta_gattc_cache_open
1603 **
1604 ** Description open a NV cache for loading
1605 **
1606 ** Returns void
1607 **
1608 *******************************************************************************/
bta_gattc_cache_open(tBTA_GATTC_CLCB * p_clcb,tBTA_GATTC_DATA * p_data)1609 void bta_gattc_cache_open(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1610 {
1611 UNUSED(p_data);
1612
1613 bta_gattc_set_discover_st(p_clcb->p_srcb);
1614
1615 APPL_TRACE_DEBUG("bta_gattc_cache_open conn_id=%d",p_clcb->bta_conn_id);
1616 bta_gattc_co_cache_open(p_clcb->p_srcb->server_bda, BTA_GATTC_CI_CACHE_OPEN_EVT,
1617 p_clcb->bta_conn_id, FALSE);
1618 }
1619 /*******************************************************************************
1620 **
1621 ** Function bta_gattc_start_load
1622 **
1623 ** Description start cache loading by sending callout open cache
1624 **
1625 ** Returns None.
1626 **
1627 *******************************************************************************/
bta_gattc_ci_open(tBTA_GATTC_CLCB * p_clcb,tBTA_GATTC_DATA * p_data)1628 void bta_gattc_ci_open(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1629 {
1630 APPL_TRACE_DEBUG("bta_gattc_ci_open conn_id=%d server state=%d" ,
1631 p_clcb->bta_conn_id, p_clcb->p_srcb->state);
1632 if (p_clcb->p_srcb->state == BTA_GATTC_SERV_LOAD)
1633 {
1634 if (p_data->ci_open.status == BTA_GATT_OK)
1635 {
1636 p_clcb->p_srcb->attr_index = 0;
1637 bta_gattc_co_cache_load(p_clcb->p_srcb->server_bda,
1638 BTA_GATTC_CI_CACHE_LOAD_EVT,
1639 p_clcb->p_srcb->attr_index,
1640 p_clcb->bta_conn_id);
1641 }
1642 else
1643 {
1644 p_clcb->p_srcb->state = BTA_GATTC_SERV_DISC;
1645 /* cache open failure, start discovery */
1646 bta_gattc_start_discover(p_clcb, NULL);
1647 }
1648 }
1649 if (p_clcb->p_srcb->state == BTA_GATTC_SERV_SAVE)
1650 {
1651 if (p_data->ci_open.status == BTA_GATT_OK)
1652 {
1653 if (!bta_gattc_cache_save(p_clcb->p_srcb, p_clcb->bta_conn_id))
1654 {
1655 p_data->ci_open.status = BTA_GATT_ERROR;
1656 }
1657 }
1658 if (p_data->ci_open.status != BTA_GATT_OK)
1659 {
1660 p_clcb->p_srcb->attr_index = 0;
1661 bta_gattc_co_cache_close(p_clcb->p_srcb->server_bda, p_clcb->bta_conn_id);
1662 bta_gattc_reset_discover_st(p_clcb->p_srcb, p_clcb->status);
1663
1664 }
1665 }
1666 }
1667 /*******************************************************************************
1668 **
1669 ** Function bta_gattc_ci_load
1670 **
1671 ** Description cache loading received.
1672 **
1673 ** Returns None.
1674 **
1675 *******************************************************************************/
bta_gattc_ci_load(tBTA_GATTC_CLCB * p_clcb,tBTA_GATTC_DATA * p_data)1676 void bta_gattc_ci_load(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1677 {
1678
1679 APPL_TRACE_DEBUG("bta_gattc_ci_load conn_id=%d load status=%d",
1680 p_clcb->bta_conn_id, p_data->ci_load.status);
1681
1682 if (p_data->ci_load.status == BTA_GATT_OK ||
1683 p_data->ci_load.status == BTA_GATT_MORE)
1684 {
1685 if (p_data->ci_load.num_attr != 0)
1686 bta_gattc_rebuild_cache(p_clcb->p_srcb, p_data->ci_load.num_attr,
1687 p_data->ci_load.attr, p_clcb->p_srcb->attr_index);
1688
1689 if (p_data->ci_load.status == BTA_GATT_OK)
1690 {
1691 p_clcb->p_srcb->attr_index = 0;
1692 bta_gattc_reset_discover_st(p_clcb->p_srcb, BTA_GATT_OK);
1693 bta_gattc_co_cache_close(p_clcb->p_srcb->server_bda, 0);
1694 }
1695 else /* load more */
1696 {
1697 p_clcb->p_srcb->attr_index += p_data->ci_load.num_attr;
1698
1699 bta_gattc_co_cache_load(p_clcb->p_srcb->server_bda,
1700 BTA_GATTC_CI_CACHE_LOAD_EVT,
1701 p_clcb->p_srcb->attr_index,
1702 p_clcb->bta_conn_id);
1703 }
1704 }
1705 else
1706 {
1707 bta_gattc_co_cache_close(p_clcb->p_srcb->server_bda, 0);
1708 p_clcb->p_srcb->state = BTA_GATTC_SERV_DISC;
1709 p_clcb->p_srcb->attr_index = 0;
1710 /* cache load failure, start discovery */
1711 bta_gattc_start_discover(p_clcb, NULL);
1712 }
1713 }
1714 /*******************************************************************************
1715 **
1716 ** Function bta_gattc_ci_save
1717 **
1718 ** Description cache loading received.
1719 **
1720 ** Returns None.
1721 **
1722 *******************************************************************************/
bta_gattc_ci_save(tBTA_GATTC_CLCB * p_clcb,tBTA_GATTC_DATA * p_data)1723 void bta_gattc_ci_save(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1724 {
1725 UNUSED(p_data);
1726
1727 APPL_TRACE_DEBUG("bta_gattc_ci_save conn_id=%d " ,
1728 p_clcb->bta_conn_id );
1729
1730 if (!bta_gattc_cache_save(p_clcb->p_srcb, p_clcb->bta_conn_id))
1731 {
1732 p_clcb->p_srcb->attr_index = 0;
1733 bta_gattc_co_cache_close(p_clcb->p_srcb->server_bda, 0);
1734 bta_gattc_reset_discover_st(p_clcb->p_srcb, p_clcb->status);
1735 }
1736 }
1737 /*******************************************************************************
1738 **
1739 ** Function bta_gattc_fail
1740 **
1741 ** Description report API call failure back to apps
1742 **
1743 ** Returns None.
1744 **
1745 *******************************************************************************/
bta_gattc_fail(tBTA_GATTC_CLCB * p_clcb,tBTA_GATTC_DATA * p_data)1746 void bta_gattc_fail(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data)
1747 {
1748 UNUSED(p_data);
1749
1750 if (p_clcb->status == BTA_GATT_OK)
1751 {
1752 APPL_TRACE_ERROR("operation not supported at current state [%d]", p_clcb->state);
1753 }
1754 }
1755
1756 /*******************************************************************************
1757 **
1758 ** Function bta_gattc_deregister_cmpl
1759 **
1760 ** Description De-Register a GATT client application with BTA completed.
1761 **
1762 ** Returns void
1763 **
1764 *******************************************************************************/
bta_gattc_deregister_cmpl(tBTA_GATTC_RCB * p_clreg)1765 static void bta_gattc_deregister_cmpl(tBTA_GATTC_RCB *p_clreg)
1766 {
1767 tBTA_GATTC_CB *p_cb = &bta_gattc_cb;
1768 tBTA_GATTC_IF client_if = p_clreg->client_if;
1769 tBTA_GATTC cb_data;
1770 tBTA_GATTC_CBACK *p_cback = p_clreg->p_cback;
1771
1772 memset(&cb_data, 0, sizeof(tBTA_GATTC));
1773
1774 GATT_Deregister(p_clreg->client_if);
1775 memset(p_clreg, 0, sizeof(tBTA_GATTC_RCB));
1776
1777 cb_data.reg_oper.client_if = client_if;
1778 cb_data.reg_oper.status = BTA_GATT_OK;
1779
1780 if (p_cback)
1781 /* callback with de-register event */
1782 (*p_cback)(BTA_GATTC_DEREG_EVT, (tBTA_GATTC *)&cb_data);
1783
1784 if (bta_gattc_num_reg_app() == 0 && p_cb->state == BTA_GATTC_STATE_DISABLING)
1785 {
1786 p_cb->state = BTA_GATTC_STATE_DISABLED;
1787 }
1788 }
1789 /*******************************************************************************
1790 **
1791 ** Function bta_gattc_conn_cback
1792 **
1793 ** Description callback functions to GATT client stack.
1794 **
1795 ** Returns void
1796 **
1797 *******************************************************************************/
bta_gattc_conn_cback(tGATT_IF gattc_if,BD_ADDR bda,UINT16 conn_id,BOOLEAN connected,tGATT_DISCONN_REASON reason,tBT_TRANSPORT transport)1798 static void bta_gattc_conn_cback(tGATT_IF gattc_if, BD_ADDR bda, UINT16 conn_id,
1799 BOOLEAN connected, tGATT_DISCONN_REASON reason,
1800 tBT_TRANSPORT transport)
1801 {
1802 tBTA_GATTC_DATA *p_buf;
1803
1804 if (reason != 0)
1805 {
1806 APPL_TRACE_WARNING("%s() - cif=%d connected=%d conn_id=%d reason=0x%04x",
1807 __FUNCTION__, gattc_if, connected, conn_id, reason);
1808 }
1809
1810 bt_bdaddr_t bdaddr;
1811 bdcpy(bdaddr.address, bda);
1812 if (connected)
1813 btif_debug_conn_state(bdaddr, BTIF_DEBUG_CONNECTED, GATT_CONN_UNKNOWN);
1814 else
1815 btif_debug_conn_state(bdaddr, BTIF_DEBUG_DISCONNECTED, reason);
1816
1817 if ((p_buf = (tBTA_GATTC_DATA *) GKI_getbuf(sizeof(tBTA_GATTC_DATA))) != NULL)
1818 {
1819 memset(p_buf, 0, sizeof(tBTA_GATTC_DATA));
1820
1821 p_buf->int_conn.hdr.event = connected ? BTA_GATTC_INT_CONN_EVT:
1822 BTA_GATTC_INT_DISCONN_EVT;
1823 p_buf->int_conn.hdr.layer_specific = conn_id;
1824 p_buf->int_conn.client_if = gattc_if;
1825 p_buf->int_conn.role = L2CA_GetBleConnRole(bda);
1826 p_buf->int_conn.reason = reason;
1827 p_buf->int_conn.transport = transport;
1828 bdcpy(p_buf->int_conn.remote_bda, bda);
1829
1830 bta_sys_sendmsg(p_buf);
1831 }
1832 }
1833
1834 /*******************************************************************************
1835 **
1836 ** Function bta_gattc_enc_cmpl_cback
1837 **
1838 ** Description encryption complete callback function to GATT client stack.
1839 **
1840 ** Returns void
1841 **
1842 *******************************************************************************/
bta_gattc_enc_cmpl_cback(tGATT_IF gattc_if,BD_ADDR bda)1843 static void bta_gattc_enc_cmpl_cback(tGATT_IF gattc_if, BD_ADDR bda)
1844 {
1845 tBTA_GATTC_DATA *p_buf;
1846 tBTA_GATTC_CLCB *p_clcb = NULL;
1847
1848 if ((p_clcb = bta_gattc_find_clcb_by_cif(gattc_if, bda, BTA_GATT_TRANSPORT_LE)) == NULL)
1849 {
1850 return;
1851 }
1852
1853 #if (defined BTA_HH_LE_INCLUDED && BTA_HH_LE_INCLUDED == TRUE)
1854 /* filter this event just for BTA HH LE GATT client,
1855 In the future, if we want to enable encryption complete event
1856 for all GATT clients, we can remove this code */
1857 if (!bta_hh_le_is_hh_gatt_if(gattc_if))
1858 {
1859 return;
1860 }
1861 #endif
1862
1863 APPL_TRACE_DEBUG("bta_gattc_enc_cmpl_cback: cif = %d", gattc_if);
1864
1865 if ((p_buf = (tBTA_GATTC_DATA *) GKI_getbuf(sizeof(tBTA_GATTC_DATA))) != NULL)
1866 {
1867 memset(p_buf, 0, sizeof(tBTA_GATTC_DATA));
1868
1869 p_buf->enc_cmpl.hdr.event = BTA_GATTC_ENC_CMPL_EVT;
1870 p_buf->enc_cmpl.hdr.layer_specific = p_clcb->bta_conn_id;
1871 p_buf->enc_cmpl.client_if = gattc_if;
1872 bdcpy(p_buf->enc_cmpl.remote_bda, bda);
1873
1874 bta_sys_sendmsg(p_buf);
1875 }
1876 }
1877
1878 /*******************************************************************************
1879 **
1880 ** Function bta_gattc_process_api_refresh
1881 **
1882 ** Description process refresh API to delete cache and start a new discovery
1883 ** if currently connected.
1884 **
1885 ** Returns None.
1886 **
1887 *******************************************************************************/
bta_gattc_process_api_refresh(tBTA_GATTC_CB * p_cb,tBTA_GATTC_DATA * p_msg)1888 void bta_gattc_process_api_refresh(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA * p_msg)
1889 {
1890 tBTA_GATTC_SERV *p_srvc_cb = bta_gattc_find_srvr_cache(p_msg->api_conn.remote_bda);
1891 tBTA_GATTC_CLCB *p_clcb = &bta_gattc_cb.clcb[0];
1892 BOOLEAN found = FALSE;
1893 UINT8 i;
1894 UNUSED(p_cb);
1895
1896 if (p_srvc_cb != NULL)
1897 {
1898 /* try to find a CLCB */
1899 if (p_srvc_cb->connected && p_srvc_cb->num_clcb != 0)
1900 {
1901 for (i = 0; i < BTA_GATTC_CLCB_MAX; i ++, p_clcb ++)
1902 {
1903 if (p_clcb->in_use && p_clcb->p_srcb == p_srvc_cb)
1904 {
1905 found = TRUE;
1906 break;
1907 }
1908 }
1909 if (found)
1910 {
1911 bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_DISCOVER_EVT, NULL);
1912 return;
1913 }
1914 }
1915 /* in all other cases, mark it and delete the cache */
1916 if (p_srvc_cb->p_srvc_cache != NULL)
1917 {
1918 while (!GKI_queue_is_empty(&p_srvc_cb->cache_buffer))
1919 GKI_freebuf (GKI_dequeue (&p_srvc_cb->cache_buffer));
1920
1921 p_srvc_cb->p_srvc_cache = NULL;
1922 }
1923 }
1924 /* used to reset cache in application */
1925 bta_gattc_co_cache_reset(p_msg->api_conn.remote_bda);
1926
1927 }
1928 /*******************************************************************************
1929 **
1930 ** Function bta_gattc_process_srvc_chg_ind
1931 **
1932 ** Description process service change indication.
1933 **
1934 ** Returns None.
1935 **
1936 *******************************************************************************/
bta_gattc_process_srvc_chg_ind(UINT16 conn_id,tBTA_GATTC_RCB * p_clrcb,tBTA_GATTC_SERV * p_srcb,tBTA_GATTC_CLCB * p_clcb,tBTA_GATTC_NOTIFY * p_notify,UINT16 handle)1937 BOOLEAN bta_gattc_process_srvc_chg_ind(UINT16 conn_id,
1938 tBTA_GATTC_RCB *p_clrcb,
1939 tBTA_GATTC_SERV *p_srcb,
1940 tBTA_GATTC_CLCB *p_clcb,
1941 tBTA_GATTC_NOTIFY *p_notify,
1942 UINT16 handle)
1943 {
1944 tBT_UUID gattp_uuid, srvc_chg_uuid;
1945 BOOLEAN processed = FALSE;
1946 UINT8 i;
1947
1948 gattp_uuid.len = 2;
1949 gattp_uuid.uu.uuid16 = UUID_SERVCLASS_GATT_SERVER;
1950
1951 srvc_chg_uuid.len = 2;
1952 srvc_chg_uuid.uu.uuid16 = GATT_UUID_GATT_SRV_CHGD;
1953
1954 if (bta_gattc_uuid_compare(&p_notify->char_id.srvc_id.id.uuid, &gattp_uuid, TRUE) &&
1955 bta_gattc_uuid_compare(&p_notify->char_id.char_id.uuid, &srvc_chg_uuid, TRUE))
1956 {
1957 processed = TRUE;
1958 /* mark service handle change pending */
1959 p_srcb->srvc_hdl_chg = TRUE;
1960 /* clear up all notification/indication registration */
1961 bta_gattc_clear_notif_registration(conn_id);
1962 /* service change indication all received, do discovery update */
1963 if ( ++ p_srcb->update_count == bta_gattc_num_reg_app())
1964 {
1965 /* not an opened connection; or connection busy */
1966 /* search for first available clcb and start discovery */
1967 if (p_clcb == NULL || (p_clcb && p_clcb->p_q_cmd != NULL))
1968 {
1969 for (i = 0 ; i < BTA_GATTC_CLCB_MAX; i ++)
1970 {
1971 if (bta_gattc_cb.clcb[i].in_use &&
1972 bta_gattc_cb.clcb[i].p_srcb == p_srcb &&
1973 bta_gattc_cb.clcb[i].p_q_cmd == NULL)
1974 {
1975 p_clcb = &bta_gattc_cb.clcb[i];
1976 break;
1977 }
1978 }
1979 }
1980 /* send confirmation here if this is an indication, it should always be */
1981 GATTC_SendHandleValueConfirm(conn_id, handle);
1982
1983 /* if connection available, refresh cache by doing discovery now */
1984 if (p_clcb != NULL)
1985 bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_DISCOVER_EVT, NULL);
1986 }
1987 /* notify applicationf or service change */
1988 if (p_clrcb->p_cback != NULL)
1989 {
1990 (* p_clrcb->p_cback)(BTA_GATTC_SRVC_CHG_EVT, (tBTA_GATTC *)p_srcb->server_bda);
1991 }
1992
1993 }
1994
1995 return processed;
1996
1997 }
1998 /*******************************************************************************
1999 **
2000 ** Function bta_gattc_proc_other_indication
2001 **
2002 ** Description process all non-service change indication/notification.
2003 **
2004 ** Returns None.
2005 **
2006 *******************************************************************************/
bta_gattc_proc_other_indication(tBTA_GATTC_CLCB * p_clcb,UINT8 op,tGATT_CL_COMPLETE * p_data,tBTA_GATTC_NOTIFY * p_notify)2007 void bta_gattc_proc_other_indication(tBTA_GATTC_CLCB *p_clcb, UINT8 op,
2008 tGATT_CL_COMPLETE *p_data,
2009 tBTA_GATTC_NOTIFY *p_notify)
2010 {
2011 APPL_TRACE_DEBUG("bta_gattc_proc_other_indication check \
2012 p_data->att_value.handle=%d p_data->handle=%d",
2013 p_data->att_value.handle, p_data->handle);
2014 APPL_TRACE_DEBUG("is_notify", p_notify->is_notify);
2015
2016 p_notify->is_notify = (op == GATTC_OPTYPE_INDICATION) ? FALSE : TRUE;
2017 p_notify->len = p_data->att_value.len;
2018 bdcpy(p_notify->bda, p_clcb->bda);
2019 memcpy(p_notify->value, p_data->att_value.value, p_data->att_value.len);
2020 p_notify->conn_id = p_clcb->bta_conn_id;
2021
2022 if (p_clcb->p_rcb->p_cback)
2023 (*p_clcb->p_rcb->p_cback)(BTA_GATTC_NOTIF_EVT, (tBTA_GATTC *)p_notify);
2024
2025 }
2026 /*******************************************************************************
2027 **
2028 ** Function bta_gattc_process_indicate
2029 **
2030 ** Description process indication/notification.
2031 **
2032 ** Returns None.
2033 **
2034 *******************************************************************************/
bta_gattc_process_indicate(UINT16 conn_id,tGATTC_OPTYPE op,tGATT_CL_COMPLETE * p_data)2035 void bta_gattc_process_indicate(UINT16 conn_id, tGATTC_OPTYPE op, tGATT_CL_COMPLETE *p_data)
2036 {
2037 UINT16 handle = p_data->att_value.handle;
2038 tBTA_GATTC_CLCB *p_clcb ;
2039 tBTA_GATTC_RCB *p_clrcb = NULL;
2040 tBTA_GATTC_SERV *p_srcb = NULL;
2041 tBTA_GATTC_NOTIFY notify;
2042 BD_ADDR remote_bda;
2043 tBTA_GATTC_IF gatt_if;
2044 tBTA_TRANSPORT transport;
2045
2046 if (!GATT_GetConnectionInfor(conn_id, &gatt_if, remote_bda, &transport))
2047 {
2048 APPL_TRACE_ERROR("%s indication/notif for unknown app", __func__);
2049 if (op == GATTC_OPTYPE_INDICATION)
2050 GATTC_SendHandleValueConfirm(conn_id, handle);
2051 return;
2052 }
2053
2054 if ((p_clrcb = bta_gattc_cl_get_regcb(gatt_if)) == NULL)
2055 {
2056 APPL_TRACE_ERROR("%s indication/notif for unregistered app", __func__);
2057 if (op == GATTC_OPTYPE_INDICATION)
2058 GATTC_SendHandleValueConfirm(conn_id, handle);
2059 return;
2060 }
2061
2062 if ((p_srcb = bta_gattc_find_srcb(remote_bda)) == NULL)
2063 {
2064 APPL_TRACE_ERROR("%s indication/notif for unknown device, ignore", __func__);
2065 if (op == GATTC_OPTYPE_INDICATION)
2066 GATTC_SendHandleValueConfirm(conn_id, handle);
2067 return;
2068 }
2069
2070 p_clcb = bta_gattc_find_clcb_by_conn_id(conn_id);
2071
2072 if (bta_gattc_handle2id(p_srcb, handle,
2073 ¬ify.char_id.srvc_id,
2074 ¬ify.char_id.char_id,
2075 ¬ify.descr_type))
2076 {
2077 /* if non-service change indication/notification, forward to application */
2078 if (!bta_gattc_process_srvc_chg_ind(conn_id, p_clrcb, p_srcb, p_clcb, ¬ify, handle))
2079 {
2080 /* if app registered for the notification */
2081 if (bta_gattc_check_notif_registry(p_clrcb, p_srcb, ¬ify))
2082 {
2083 /* connection not open yet */
2084 if (p_clcb == NULL)
2085 {
2086 if ((p_clcb = bta_gattc_clcb_alloc(gatt_if, remote_bda, transport)) != NULL)
2087 {
2088 p_clcb->bta_conn_id = conn_id;
2089 p_clcb->transport = transport;
2090
2091 bta_gattc_sm_execute(p_clcb, BTA_GATTC_INT_CONN_EVT, NULL);
2092 }
2093 else
2094 {
2095 APPL_TRACE_ERROR("No resources");
2096 }
2097 }
2098
2099 if (p_clcb != NULL)
2100 bta_gattc_proc_other_indication(p_clcb, op, p_data, ¬ify);
2101 }
2102 /* no one intersted and need ack? */
2103 else if (op == GATTC_OPTYPE_INDICATION)
2104 {
2105 APPL_TRACE_DEBUG("%s no one interested, ack now", __func__);
2106 GATTC_SendHandleValueConfirm(conn_id, handle);
2107 }
2108 }
2109 }
2110 else
2111 {
2112 APPL_TRACE_ERROR("%s Indi/Notif for Unknown handle[0x%04x], can not find in local cache.",
2113 __func__, handle);
2114 if (op == GATTC_OPTYPE_INDICATION)
2115 GATTC_SendHandleValueConfirm(conn_id, handle);
2116 }
2117 }
2118 /*******************************************************************************
2119 **
2120 ** Function bta_gattc_cmpl_cback
2121 **
2122 ** Description client operation complete callback register with BTE GATT.
2123 **
2124 ** Returns None.
2125 **
2126 *******************************************************************************/
bta_gattc_cmpl_cback(UINT16 conn_id,tGATTC_OPTYPE op,tGATT_STATUS status,tGATT_CL_COMPLETE * p_data)2127 static void bta_gattc_cmpl_cback(UINT16 conn_id, tGATTC_OPTYPE op, tGATT_STATUS status,
2128 tGATT_CL_COMPLETE *p_data)
2129 {
2130 tBTA_GATTC_CLCB *p_clcb;
2131 APPL_TRACE_DEBUG("bta_gattc_cmpl_cback: conn_id = %d op = %d status = %d",
2132 conn_id, op, status);
2133
2134 /* notification and indication processed right away */
2135 if (op == GATTC_OPTYPE_NOTIFICATION || op == GATTC_OPTYPE_INDICATION)
2136 {
2137 bta_gattc_process_indicate(conn_id, op, p_data);
2138 return;
2139 }
2140 /* for all other operation, not expected if w/o connection */
2141 else if ((p_clcb = bta_gattc_find_clcb_by_conn_id(conn_id)) == NULL)
2142 {
2143 APPL_TRACE_ERROR("bta_gattc_cmpl_cback unknown conn_id = %d, ignore data", conn_id);
2144 return;
2145 }
2146
2147 /* if over BR_EDR, inform PM for mode change */
2148 if (p_clcb->transport == BTA_TRANSPORT_BR_EDR)
2149 {
2150 bta_sys_busy(BTA_ID_GATTC, BTA_ALL_APP_ID, p_clcb->bda);
2151 bta_sys_idle(BTA_ID_GATTC, BTA_ALL_APP_ID, p_clcb->bda);
2152 }
2153
2154 bta_gattc_cmpl_sendmsg(conn_id, op, status, p_data);
2155 }
2156
2157 /*******************************************************************************
2158 **
2159 ** Function bta_gattc_cmpl_sendmsg
2160 **
2161 ** Description client operation complete send message
2162 **
2163 ** Returns None.
2164 **
2165 *******************************************************************************/
bta_gattc_cmpl_sendmsg(UINT16 conn_id,tGATTC_OPTYPE op,tBTA_GATT_STATUS status,tGATT_CL_COMPLETE * p_data)2166 static void bta_gattc_cmpl_sendmsg(UINT16 conn_id, tGATTC_OPTYPE op,
2167 tBTA_GATT_STATUS status,
2168 tGATT_CL_COMPLETE *p_data)
2169 {
2170 const UINT16 len = sizeof(tBTA_GATTC_OP_CMPL) + sizeof(tGATT_CL_COMPLETE);
2171 tBTA_GATTC_OP_CMPL *p_buf = (tBTA_GATTC_OP_CMPL *) GKI_getbuf(len);
2172
2173 if (p_buf != NULL)
2174 {
2175 memset(p_buf, 0, len);
2176 p_buf->hdr.event = BTA_GATTC_OP_CMPL_EVT;
2177 p_buf->hdr.layer_specific = conn_id;
2178 p_buf->status = status;
2179 p_buf->op_code = op;
2180
2181 if (p_data != NULL)
2182 {
2183 p_buf->p_cmpl = (tGATT_CL_COMPLETE *)(p_buf + 1);
2184 memcpy(p_buf->p_cmpl, p_data, sizeof(tGATT_CL_COMPLETE));
2185 }
2186
2187 bta_sys_sendmsg(p_buf);
2188 }
2189 }
2190
2191 /*******************************************************************************
2192 **
2193 ** Function bta_gattc_cong_cback
2194 **
2195 ** Description congestion callback for BTA GATT client.
2196 **
2197 ** Returns void
2198 **
2199 ********************************************************************************/
bta_gattc_cong_cback(UINT16 conn_id,BOOLEAN congested)2200 static void bta_gattc_cong_cback (UINT16 conn_id, BOOLEAN congested)
2201 {
2202 tBTA_GATTC_CLCB *p_clcb;
2203 tBTA_GATTC cb_data;
2204
2205 if ((p_clcb = bta_gattc_find_clcb_by_conn_id(conn_id)) != NULL)
2206 {
2207 if (p_clcb->p_rcb->p_cback)
2208 {
2209 cb_data.congest.conn_id = conn_id;
2210 cb_data.congest.congested = congested;
2211
2212 (*p_clcb->p_rcb->p_cback)(BTA_GATTC_CONGEST_EVT, &cb_data);
2213 }
2214 }
2215 }
2216
2217 #if BLE_INCLUDED == TRUE
2218 /*******************************************************************************
2219 **
2220 ** Function bta_gattc_init_clcb_conn
2221 **
2222 ** Description Initaite a BTA CLCB connection
2223 **
2224 ** Returns void
2225 **
2226 ********************************************************************************/
bta_gattc_init_clcb_conn(UINT8 cif,BD_ADDR remote_bda)2227 void bta_gattc_init_clcb_conn(UINT8 cif, BD_ADDR remote_bda)
2228 {
2229 tBTA_GATTC_CLCB *p_clcb = NULL;
2230 tBTA_GATTC_DATA gattc_data;
2231 UINT16 conn_id;
2232
2233 /* should always get the connection ID */
2234 if (GATT_GetConnIdIfConnected(cif, remote_bda, &conn_id, BTA_GATT_TRANSPORT_LE) == FALSE)
2235 {
2236 APPL_TRACE_ERROR("bta_gattc_init_clcb_conn ERROR: not a connected device");
2237 return;
2238 }
2239
2240 /* initaite a new connection here */
2241 if ((p_clcb = bta_gattc_clcb_alloc(cif, remote_bda, BTA_GATT_TRANSPORT_LE)) != NULL)
2242 {
2243 gattc_data.hdr.layer_specific = p_clcb->bta_conn_id = conn_id;
2244
2245 gattc_data.api_conn.client_if = cif;
2246 memcpy(gattc_data.api_conn.remote_bda, remote_bda, BD_ADDR_LEN);
2247 gattc_data.api_conn.is_direct = TRUE;
2248
2249 bta_gattc_sm_execute(p_clcb, BTA_GATTC_API_OPEN_EVT, &gattc_data);
2250 }
2251 else
2252 {
2253 APPL_TRACE_ERROR("No resources");
2254 }
2255 }
2256 /*******************************************************************************
2257 **
2258 ** Function bta_gattc_process_listen_all
2259 **
2260 ** Description process listen all, send open callback to application for all
2261 ** connected slave LE link.
2262 **
2263 ** Returns void
2264 **
2265 ********************************************************************************/
bta_gattc_process_listen_all(UINT8 cif)2266 void bta_gattc_process_listen_all(UINT8 cif)
2267 {
2268 UINT8 i_conn = 0;
2269 tBTA_GATTC_CONN *p_conn = &bta_gattc_cb.conn_track[0];
2270
2271 for (i_conn = 0; i_conn < BTA_GATTC_CONN_MAX; i_conn++, p_conn ++)
2272 {
2273 if (p_conn->in_use )
2274 {
2275 if (bta_gattc_find_clcb_by_cif(cif, p_conn->remote_bda, BTA_GATT_TRANSPORT_LE) == NULL)
2276 {
2277 bta_gattc_init_clcb_conn(cif, p_conn->remote_bda);
2278 }
2279 /* else already connected */
2280 }
2281 }
2282 }
2283 /*******************************************************************************
2284 **
2285 ** Function bta_gattc_listen
2286 **
2287 ** Description Start or stop a listen for connection
2288 **
2289 ** Returns void
2290 **
2291 ********************************************************************************/
bta_gattc_listen(tBTA_GATTC_CB * p_cb,tBTA_GATTC_DATA * p_msg)2292 void bta_gattc_listen(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA * p_msg)
2293 {
2294 tBTA_GATTC_RCB *p_clreg = bta_gattc_cl_get_regcb(p_msg->api_listen.client_if);
2295 tBTA_GATTC cb_data;
2296 UNUSED(p_cb);
2297
2298 cb_data.reg_oper.status = BTA_GATT_ERROR;
2299 cb_data.reg_oper.client_if = p_msg->api_listen.client_if;
2300
2301 if (p_clreg == NULL)
2302 {
2303 APPL_TRACE_ERROR("bta_gattc_listen failed, unknown client_if: %d",
2304 p_msg->api_listen.client_if);
2305 return;
2306 }
2307 /* mark bg conn record */
2308 if (bta_gattc_mark_bg_conn(p_msg->api_listen.client_if,
2309 (BD_ADDR_PTR) p_msg->api_listen.remote_bda,
2310 p_msg->api_listen.start,
2311 TRUE))
2312 {
2313 if (!GATT_Listen(p_msg->api_listen.client_if,
2314 p_msg->api_listen.start,
2315 p_msg->api_listen.remote_bda))
2316 {
2317 APPL_TRACE_ERROR("Listen failure");
2318 (*p_clreg->p_cback)(BTA_GATTC_LISTEN_EVT, &cb_data);
2319 }
2320 else
2321 {
2322 cb_data.status = BTA_GATT_OK;
2323
2324 (*p_clreg->p_cback)(BTA_GATTC_LISTEN_EVT, &cb_data);
2325
2326 if (p_msg->api_listen.start)
2327 {
2328 /* if listen to a specific target */
2329 if (p_msg->api_listen.remote_bda != NULL)
2330 {
2331
2332 /* if is a connected remote device */
2333 if (L2CA_GetBleConnRole(p_msg->api_listen.remote_bda) == HCI_ROLE_SLAVE &&
2334 bta_gattc_find_clcb_by_cif(p_msg->api_listen.client_if,
2335 p_msg->api_listen.remote_bda,
2336 BTA_GATT_TRANSPORT_LE) == NULL)
2337 {
2338
2339 bta_gattc_init_clcb_conn(p_msg->api_listen.client_if,
2340 p_msg->api_listen.remote_bda);
2341 }
2342 }
2343 /* if listen to all */
2344 else
2345 {
2346 LOG_DEBUG("Listen For All now");
2347 /* go through all connected device and send
2348 callback for all connected slave connection */
2349 bta_gattc_process_listen_all(p_msg->api_listen.client_if);
2350 }
2351 }
2352 }
2353 }
2354 }
2355
2356 /*******************************************************************************
2357 **
2358 ** Function bta_gattc_broadcast
2359 **
2360 ** Description Start or stop broadcasting
2361 **
2362 ** Returns void
2363 **
2364 ********************************************************************************/
bta_gattc_broadcast(tBTA_GATTC_CB * p_cb,tBTA_GATTC_DATA * p_msg)2365 void bta_gattc_broadcast(tBTA_GATTC_CB *p_cb, tBTA_GATTC_DATA * p_msg)
2366 {
2367 tBTA_GATTC_RCB *p_clreg = bta_gattc_cl_get_regcb(p_msg->api_listen.client_if);
2368 tBTA_GATTC cb_data;
2369 UNUSED(p_cb);
2370
2371 cb_data.reg_oper.client_if = p_msg->api_listen.client_if;
2372 cb_data.reg_oper.status = BTM_BleBroadcast(p_msg->api_listen.start);
2373
2374 if (p_clreg && p_clreg->p_cback)
2375 (*p_clreg->p_cback)(BTA_GATTC_LISTEN_EVT, &cb_data);
2376 }
2377 #endif
2378 #endif
2379