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