OSDN Git Service

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