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 main functions and state machine.
22  *
23  ******************************************************************************/
24 
25 #include "common/bt_target.h"
26 
27 #if (GATTC_INCLUDED == TRUE && BLE_INCLUDED == TRUE)
28 
29 #include <string.h>
30 
31 #include "bta_gattc_int.h"
32 #include "osi/allocator.h"
33 
34 
35 /*****************************************************************************
36 ** Constants and types
37 *****************************************************************************/
38 
39 
40 /* state machine action enumeration list */
41 enum {
42     BTA_GATTC_OPEN,
43     BTA_GATTC_OPEN_FAIL,
44     BTA_GATTC_OPEN_ERROR,
45     BTA_GATTC_CANCEL_OPEN,
46     BTA_GATTC_CANCEL_OPEN_OK,
47     BTA_GATTC_CANCEL_OPEN_ERROR,
48     BTA_GATTC_CONN,
49     BTA_GATTC_START_DISCOVER,
50     BTA_GATTC_DISC_CMPL,
51 
52     BTA_GATTC_Q_CMD,
53     BTA_GATTC_CLOSE,
54     BTA_GATTC_CLOSE_FAIL,
55     BTA_GATTC_READ,
56     BTA_GATTC_WRITE,
57 
58     BTA_GATTC_OP_CMPL,
59     BTA_GATTC_SEARCH,
60     BTA_GATTC_FAIL,
61     BTA_GATTC_CONFIRM,
62     BTA_GATTC_EXEC,
63     BTA_GATTC_READ_MULTI,
64     BTA_GATTC_IGNORE_OP_CMPL,
65     BTA_GATTC_DISC_CLOSE,
66     BTA_GATTC_RESTART_DISCOVER,
67     BTA_GATTC_CFG_MTU,
68     BTA_GATTC_READ_BY_TYPE,
69     BTA_GATTC_READ_MULTI_VAR,
70 
71     BTA_GATTC_IGNORE
72 };
73 /* type for action functions */
74 typedef void (*tBTA_GATTC_ACTION)(tBTA_GATTC_CLCB *p_clcb, tBTA_GATTC_DATA *p_data);
75 
76 /* action function list */
77 const tBTA_GATTC_ACTION bta_gattc_action[] = {
78     bta_gattc_open,
79     bta_gattc_open_fail,
80     bta_gattc_open_error,
81     bta_gattc_cancel_open,
82     bta_gattc_cancel_open_ok,
83     bta_gattc_cancel_open_error,
84     bta_gattc_conn,
85     bta_gattc_start_discover,
86     bta_gattc_disc_cmpl,
87 
88     bta_gattc_q_cmd,
89     bta_gattc_close,
90     bta_gattc_close_fail,
91     bta_gattc_read,
92     bta_gattc_write,
93 
94     bta_gattc_op_cmpl,
95     bta_gattc_search,
96     bta_gattc_fail,
97     bta_gattc_confirm,
98     bta_gattc_execute,
99     bta_gattc_read_multi,
100     bta_gattc_ignore_op_cmpl,
101     bta_gattc_disc_close,
102     bta_gattc_restart_discover,
103     bta_gattc_cfg_mtu,
104     bta_gattc_read_by_type,
105     bta_gattc_read_multi_var,
106 };
107 
108 
109 /* state table information */
110 #define BTA_GATTC_ACTIONS           1       /* number of actions */
111 #define BTA_GATTC_NEXT_STATE          1       /* position of next state */
112 #define BTA_GATTC_NUM_COLS            2       /* number of columns in state tables */
113 
114 /* state table for idle state */
115 static const UINT8 bta_gattc_st_idle[][BTA_GATTC_NUM_COLS] = {
116     /* Event                            Action 1                  Next state */
117     /* BTA_GATTC_API_OPEN_EVT           */   {BTA_GATTC_OPEN,              BTA_GATTC_W4_CONN_ST},
118     /* BTA_GATTC_INT_OPEN_FAIL_EVT      */   {BTA_GATTC_IGNORE,            BTA_GATTC_IDLE_ST},
119     /* BTA_GATTC_API_CANCEL_OPEN_EVT    */   {BTA_GATTC_IGNORE,            BTA_GATTC_IDLE_ST},
120     /* BTA_GATTC_INT_CANCEL_OPEN_OK_EVT */   {BTA_GATTC_IGNORE,            BTA_GATTC_IDLE_ST},
121 
122     /* BTA_GATTC_API_READ_EVT           */   {BTA_GATTC_FAIL,              BTA_GATTC_IDLE_ST},
123     /* BTA_GATTC_API_WRITE_EVT          */   {BTA_GATTC_FAIL,              BTA_GATTC_IDLE_ST},
124     /* BTA_GATTC_API_EXEC_EVT           */   {BTA_GATTC_FAIL,              BTA_GATTC_IDLE_ST},
125     /* BTA_GATTC_API_CFG_MTU_EVT        */   {BTA_GATTC_IGNORE,            BTA_GATTC_IDLE_ST},
126 
127     /* BTA_GATTC_API_CLOSE_EVT          */   {BTA_GATTC_CLOSE_FAIL,        BTA_GATTC_IDLE_ST},
128 
129     /* BTA_GATTC_API_SEARCH_EVT         */   {BTA_GATTC_FAIL,              BTA_GATTC_IDLE_ST},
130     /* BTA_GATTC_API_CONFIRM_EVT        */   {BTA_GATTC_FAIL,              BTA_GATTC_IDLE_ST},
131     /* BTA_GATTC_API_READ_MULTI_EVT     */   {BTA_GATTC_FAIL,              BTA_GATTC_IDLE_ST},
132     /* BTA_GATTC_API_REFRESH_EVT        */   {BTA_GATTC_IGNORE,            BTA_GATTC_IDLE_ST},
133     /* BTA_GATTC_API_CACHE_CLEAN_EVT    */   {BTA_GATTC_IGNORE,            BTA_GATTC_IDLE_ST},
134 
135     /* BTA_GATTC_INT_CONN_EVT           */   {BTA_GATTC_CONN,              BTA_GATTC_CONN_ST},
136     /* BTA_GATTC_INT_DISCOVER_EVT       */   {BTA_GATTC_IGNORE,            BTA_GATTC_IDLE_ST},
137     /* BTA_GATTC_DISCOVER_CMPL_EVT      */   {BTA_GATTC_IGNORE,            BTA_GATTC_IDLE_ST},
138     /* BTA_GATTC_OP_CMPL_EVT            */   {BTA_GATTC_IGNORE,            BTA_GATTC_IDLE_ST},
139     /* BTA_GATTC_INT_DISCONN_EVT       */    {BTA_GATTC_IGNORE,            BTA_GATTC_IDLE_ST},
140 
141     /* BTA_GATTC_API_READ_BY_TYPE_EVT   */   {BTA_GATTC_FAIL,              BTA_GATTC_IDLE_ST},
142     /* BTA_GATTC_API_READ_MULTI_VAR_EVT */   {BTA_GATTC_FAIL,              BTA_GATTC_IDLE_ST},
143 };
144 
145 /* state table for wait for open state */
146 static const UINT8 bta_gattc_st_w4_conn[][BTA_GATTC_NUM_COLS] = {
147     /* Event                            Action 1                             Next state */
148     /* BTA_GATTC_API_OPEN_EVT           */   {BTA_GATTC_OPEN,              BTA_GATTC_W4_CONN_ST},
149     /* BTA_GATTC_INT_OPEN_FAIL_EVT      */   {BTA_GATTC_OPEN_FAIL,         BTA_GATTC_IDLE_ST},
150     /* BTA_GATTC_API_CANCEL_OPEN_EVT    */   {BTA_GATTC_CANCEL_OPEN,       BTA_GATTC_W4_CONN_ST},
151     /* BTA_GATTC_INT_CANCEL_OPEN_OK_EVT */   {BTA_GATTC_CANCEL_OPEN_OK,    BTA_GATTC_IDLE_ST},
152 
153     /* BTA_GATTC_API_READ_EVT           */   {BTA_GATTC_FAIL,               BTA_GATTC_W4_CONN_ST},
154     /* BTA_GATTC_API_WRITE_EVT          */   {BTA_GATTC_FAIL,               BTA_GATTC_W4_CONN_ST},
155     /* BTA_GATTC_API_EXEC_EVT           */   {BTA_GATTC_FAIL,               BTA_GATTC_W4_CONN_ST},
156     /* BTA_GATTC_API_CFG_MTU_EVT        */   {BTA_GATTC_IGNORE,             BTA_GATTC_W4_CONN_ST},
157 
158     /* BTA_GATTC_API_CLOSE_EVT          */   {BTA_GATTC_CANCEL_OPEN,         BTA_GATTC_W4_CONN_ST},
159 
160     /* BTA_GATTC_API_SEARCH_EVT         */   {BTA_GATTC_FAIL,               BTA_GATTC_W4_CONN_ST},
161     /* BTA_GATTC_API_CONFIRM_EVT        */   {BTA_GATTC_FAIL,               BTA_GATTC_W4_CONN_ST},
162     /* BTA_GATTC_API_READ_MULTI_EVT     */   {BTA_GATTC_FAIL,               BTA_GATTC_W4_CONN_ST},
163     /* BTA_GATTC_API_REFRESH_EVT        */   {BTA_GATTC_IGNORE,             BTA_GATTC_W4_CONN_ST},
164     /* BTA_GATTC_API_CACHE_CLEAN_EVT    */   {BTA_GATTC_IGNORE,             BTA_GATTC_W4_CONN_ST},
165 
166     /* BTA_GATTC_INT_CONN_EVT           */   {BTA_GATTC_CONN,               BTA_GATTC_CONN_ST},
167     /* BTA_GATTC_INT_DISCOVER_EVT       */   {BTA_GATTC_IGNORE,             BTA_GATTC_W4_CONN_ST},
168     /* BTA_GATTC_DISCOVER_CMPL_EVT       */  {BTA_GATTC_IGNORE,             BTA_GATTC_W4_CONN_ST},
169     /* BTA_GATTC_OP_CMPL_EVT            */   {BTA_GATTC_IGNORE,             BTA_GATTC_W4_CONN_ST},
170     /* BTA_GATTC_INT_DISCONN_EVT      */     {BTA_GATTC_OPEN_FAIL,          BTA_GATTC_IDLE_ST},
171 
172     /* BTA_GATTC_API_READ_BY_TYPE_EVT   */   {BTA_GATTC_FAIL,               BTA_GATTC_W4_CONN_ST},
173     /* BTA_GATTC_API_READ_MULTI_VAR_EVT */   {BTA_GATTC_FAIL,               BTA_GATTC_W4_CONN_ST},
174 };
175 
176 /* state table for open state */
177 static const UINT8 bta_gattc_st_connected[][BTA_GATTC_NUM_COLS] = {
178     /* Event                            Action 1                            Next state */
179     /* BTA_GATTC_API_OPEN_EVT           */   {BTA_GATTC_OPEN,               BTA_GATTC_CONN_ST},
180     /* BTA_GATTC_INT_OPEN_FAIL_EVT      */   {BTA_GATTC_IGNORE,             BTA_GATTC_CONN_ST},
181     /* BTA_GATTC_API_CANCEL_OPEN_EVT    */   {BTA_GATTC_CANCEL_OPEN_ERROR, BTA_GATTC_CONN_ST},
182     /* BTA_GATTC_INT_CANCEL_OPEN_OK_EVT */   {BTA_GATTC_IGNORE,            BTA_GATTC_CONN_ST},
183 
184     /* BTA_GATTC_API_READ_EVT           */   {BTA_GATTC_READ,               BTA_GATTC_CONN_ST},
185     /* BTA_GATTC_API_WRITE_EVT          */   {BTA_GATTC_WRITE,              BTA_GATTC_CONN_ST},
186     /* BTA_GATTC_API_EXEC_EVT           */   {BTA_GATTC_EXEC,               BTA_GATTC_CONN_ST},
187     /* BTA_GATTC_API_CFG_MTU_EVT        */   {BTA_GATTC_CFG_MTU,            BTA_GATTC_CONN_ST},
188 
189     /* BTA_GATTC_API_CLOSE_EVT          */   {BTA_GATTC_CLOSE,              BTA_GATTC_IDLE_ST},
190 
191     /* BTA_GATTC_API_SEARCH_EVT         */   {BTA_GATTC_SEARCH,             BTA_GATTC_CONN_ST},
192     /* BTA_GATTC_API_CONFIRM_EVT        */   {BTA_GATTC_CONFIRM,            BTA_GATTC_CONN_ST},
193     /* BTA_GATTC_API_READ_MULTI_EVT     */   {BTA_GATTC_READ_MULTI,         BTA_GATTC_CONN_ST},
194     /* BTA_GATTC_API_REFRESH_EVT        */   {BTA_GATTC_IGNORE,             BTA_GATTC_CONN_ST},
195     /* BTA_GATTC_API_CACHE_CLEAN_EVT    */   {BTA_GATTC_IGNORE,             BTA_GATTC_CONN_ST},
196 
197     /* BTA_GATTC_INT_CONN_EVT           */   {BTA_GATTC_CONN,               BTA_GATTC_CONN_ST},
198     /* BTA_GATTC_INT_DISCOVER_EVT       */   {BTA_GATTC_START_DISCOVER,     BTA_GATTC_DISCOVER_ST},
199     /* BTA_GATTC_DISCOVER_CMPL_EVT       */  {BTA_GATTC_IGNORE,             BTA_GATTC_CONN_ST},
200     /* BTA_GATTC_OP_CMPL_EVT            */   {BTA_GATTC_OP_CMPL,            BTA_GATTC_CONN_ST},
201 
202     /* BTA_GATTC_INT_DISCONN_EVT        */   {BTA_GATTC_CLOSE,              BTA_GATTC_IDLE_ST},
203 
204     /* BTA_GATTC_API_READ_BY_TYPE_EVT   */   {BTA_GATTC_READ_BY_TYPE,       BTA_GATTC_CONN_ST},
205     /* BTA_GATTC_API_READ_MULTI_VAR_EVT */   {BTA_GATTC_READ_MULTI_VAR,     BTA_GATTC_CONN_ST},
206 };
207 
208 /* state table for discover state */
209 static const UINT8 bta_gattc_st_discover[][BTA_GATTC_NUM_COLS] = {
210     /* Event                            Action 1                            Next state */
211     /* BTA_GATTC_API_OPEN_EVT           */   {BTA_GATTC_OPEN,               BTA_GATTC_DISCOVER_ST},
212     /* BTA_GATTC_INT_OPEN_FAIL_EVT      */   {BTA_GATTC_IGNORE,             BTA_GATTC_DISCOVER_ST},
213     /* BTA_GATTC_API_CANCEL_OPEN_EVT    */   {BTA_GATTC_CANCEL_OPEN_ERROR,  BTA_GATTC_DISCOVER_ST},
214     /* BTA_GATTC_INT_CANCEL_OPEN_OK_EVT */   {BTA_GATTC_FAIL,               BTA_GATTC_DISCOVER_ST},
215 
216     /* BTA_GATTC_API_READ_EVT           */   {BTA_GATTC_Q_CMD,              BTA_GATTC_DISCOVER_ST},
217     /* BTA_GATTC_API_WRITE_EVT          */   {BTA_GATTC_Q_CMD,              BTA_GATTC_DISCOVER_ST},
218     /* BTA_GATTC_API_EXEC_EVT           */   {BTA_GATTC_Q_CMD,              BTA_GATTC_DISCOVER_ST},
219     /* BTA_GATTC_API_CFG_MTU_EVT        */   {BTA_GATTC_Q_CMD,              BTA_GATTC_DISCOVER_ST},
220 
221     /* BTA_GATTC_API_CLOSE_EVT          */   {BTA_GATTC_DISC_CLOSE,         BTA_GATTC_DISCOVER_ST},
222 
223     /* BTA_GATTC_API_SEARCH_EVT         */   {BTA_GATTC_Q_CMD,              BTA_GATTC_DISCOVER_ST},
224     /* BTA_GATTC_API_CONFIRM_EVT        */   {BTA_GATTC_CONFIRM,            BTA_GATTC_DISCOVER_ST},
225     /* BTA_GATTC_API_READ_MULTI_EVT     */   {BTA_GATTC_Q_CMD,              BTA_GATTC_DISCOVER_ST},
226     /* BTA_GATTC_API_REFRESH_EVT        */   {BTA_GATTC_IGNORE,             BTA_GATTC_DISCOVER_ST},
227     /* BTA_GATTC_API_CACHE_CLEAN_EVT    */   {BTA_GATTC_IGNORE,             BTA_GATTC_DISCOVER_ST},
228 
229     /* BTA_GATTC_INT_CONN_EVT           */   {BTA_GATTC_CONN,               BTA_GATTC_DISCOVER_ST},
230     /* BTA_GATTC_INT_DISCOVER_EVT       */   {BTA_GATTC_RESTART_DISCOVER,   BTA_GATTC_DISCOVER_ST},
231     /* BTA_GATTC_DISCOVER_CMPL_EVT      */   {BTA_GATTC_DISC_CMPL,          BTA_GATTC_CONN_ST},
232     /* BTA_GATTC_OP_CMPL_EVT            */   {BTA_GATTC_IGNORE_OP_CMPL,     BTA_GATTC_DISCOVER_ST},
233     /* BTA_GATTC_INT_DISCONN_EVT        */   {BTA_GATTC_CLOSE,              BTA_GATTC_IDLE_ST},
234 
235     /* BTA_GATTC_API_READ_BY_TYPE_EVT   */   {BTA_GATTC_Q_CMD,              BTA_GATTC_DISCOVER_ST},
236     /* BTA_GATTC_API_READ_MULTI_VAR_EVT */   {BTA_GATTC_Q_CMD,              BTA_GATTC_DISCOVER_ST},
237 };
238 
239 /* type for state table */
240 typedef const UINT8 (*tBTA_GATTC_ST_TBL)[BTA_GATTC_NUM_COLS];
241 
242 /* state table */
243 const tBTA_GATTC_ST_TBL bta_gattc_st_tbl[] = {
244     bta_gattc_st_idle,
245     bta_gattc_st_w4_conn,
246     bta_gattc_st_connected,
247     bta_gattc_st_discover
248 };
249 
250 /*****************************************************************************
251 ** Global data
252 *****************************************************************************/
253 
254 /* GATTC control block */
255 #if BTA_DYNAMIC_MEMORY == FALSE
256 tBTA_GATTC_CB  bta_gattc_cb;
257 #else
258 tBTA_GATTC_CB  *bta_gattc_cb_ptr;
259 #endif
260 
261 #if BTA_GATT_DEBUG == TRUE
262 static char *gattc_evt_code(tBTA_GATTC_INT_EVT evt_code);
263 static char *gattc_state_code(tBTA_GATTC_STATE state_code);
264 #endif
265 
266 /*******************************************************************************
267 **
268 ** Function         bta_gattc_sm_execute
269 **
270 ** Description      State machine event handling function for GATTC
271 **
272 **
273 ** Returns          BOOLEAN  : TRUE if queued client request buffer can be immediately released
274 **                                        else FALSE
275 **
276 *******************************************************************************/
bta_gattc_sm_execute(tBTA_GATTC_CLCB * p_clcb,UINT16 event,tBTA_GATTC_DATA * p_data)277 BOOLEAN bta_gattc_sm_execute(tBTA_GATTC_CLCB *p_clcb, UINT16 event, tBTA_GATTC_DATA *p_data)
278 {
279     tBTA_GATTC_ST_TBL     state_table;
280     UINT8               action;
281     int                 i;
282     BOOLEAN             rt = TRUE;
283 #if BTA_GATT_DEBUG == TRUE
284     tBTA_GATTC_STATE in_state = p_clcb->state;
285     UINT16         in_event = event;
286     APPL_TRACE_DEBUG("bta_gattc_sm_execute: State 0x%02x [%s], Event 0x%x[%s]", in_state,
287                      gattc_state_code(in_state),
288                      in_event,
289                      gattc_evt_code(in_event));
290 #endif
291 
292 
293     /* look up the state table for the current state */
294     state_table = bta_gattc_st_tbl[p_clcb->state];
295 
296     event &= 0x00FF;
297 
298     /* set next state */
299     p_clcb->state = state_table[event][BTA_GATTC_NEXT_STATE];
300 
301     /* execute action functions */
302     for (i = 0; i < BTA_GATTC_ACTIONS; i++) {
303         if ((action = state_table[event][i]) != BTA_GATTC_IGNORE) {
304             (*bta_gattc_action[action])(p_clcb, p_data);
305             if (p_clcb->p_q_cmd == p_data) {
306                 /* buffer is queued, don't free in the bta dispatcher.
307                  * we free it ourselves when a completion event is received.
308                  */
309                 rt = FALSE;
310             }
311         } else {
312             break;
313         }
314     }
315 
316 #if BTA_GATT_DEBUG == TRUE
317     if (in_state != p_clcb->state) {
318         APPL_TRACE_DEBUG("GATTC State Change: [%s] -> [%s] after Event [%s]",
319                          gattc_state_code(in_state),
320                          gattc_state_code(p_clcb->state),
321                          gattc_evt_code(in_event));
322     }
323 #endif
324     return rt;
325 }
326 
327 /*******************************************************************************
328 **
329 ** Function         bta_gattc_hdl_event
330 **
331 ** Description      GATT client main event handling function.
332 **
333 **
334 ** Returns          BOOLEAN
335 **
336 *******************************************************************************/
bta_gattc_hdl_event(BT_HDR * p_msg)337 BOOLEAN bta_gattc_hdl_event(BT_HDR *p_msg)
338 {
339     tBTA_GATTC_CB *p_cb = &bta_gattc_cb;
340     tBTA_GATTC_CLCB *p_clcb = NULL;
341     tBTA_GATTC_RCB      *p_clreg;
342     BOOLEAN             rt = TRUE;
343 #if BTA_GATT_DEBUG == TRUE
344     APPL_TRACE_DEBUG("bta_gattc_hdl_event: Event [%s]\n", gattc_evt_code(p_msg->event));
345 #endif
346     switch (p_msg->event) {
347     case BTA_GATTC_API_DISABLE_EVT:
348         bta_gattc_disable(p_cb);
349         break;
350 
351     case BTA_GATTC_API_REG_EVT:
352         bta_gattc_register(p_cb, (tBTA_GATTC_DATA *) p_msg);
353         break;
354 
355     case BTA_GATTC_INT_START_IF_EVT:
356         bta_gattc_start_if(p_cb, (tBTA_GATTC_DATA *) p_msg);
357         break;
358 
359     case BTA_GATTC_API_DEREG_EVT:
360         p_clreg = bta_gattc_cl_get_regcb(((tBTA_GATTC_DATA *)p_msg)->api_dereg.client_if);
361         bta_gattc_deregister(p_cb, p_clreg);
362         break;
363 
364     case BTA_GATTC_API_OPEN_EVT:
365         bta_gattc_process_api_open(p_cb, (tBTA_GATTC_DATA *) p_msg);
366         break;
367 
368     case BTA_GATTC_API_CANCEL_OPEN_EVT:
369         bta_gattc_process_api_open_cancel(p_cb, (tBTA_GATTC_DATA *) p_msg);
370         break;
371 
372     case BTA_GATTC_API_REFRESH_EVT:
373         bta_gattc_process_api_refresh(p_cb, (tBTA_GATTC_DATA *) p_msg);
374         break;
375     case BTA_GATTC_API_CACHE_ASSOC_EVT:
376         bta_gattc_process_api_cache_assoc(p_cb, (tBTA_GATTC_DATA *)p_msg);
377         break;
378     case BTA_GATTC_API_CACHE_GET_ADDR_LIST_EVT:
379         bta_gattc_process_api_cache_get_addr_list(p_cb, (tBTA_GATTC_DATA *)p_msg);
380         break;
381     case BTA_GATTC_API_CACHE_CLEAN_EVT:
382         bta_gattc_process_api_cache_clean(p_cb, (tBTA_GATTC_DATA *) p_msg);
383         break;
384 #if BLE_INCLUDED == TRUE
385     case BTA_GATTC_API_LISTEN_EVT:
386         bta_gattc_listen(p_cb, (tBTA_GATTC_DATA *) p_msg);
387         break;
388     case BTA_GATTC_API_BROADCAST_EVT:
389         bta_gattc_broadcast(p_cb, (tBTA_GATTC_DATA *) p_msg);
390         break;
391 #endif
392 
393     case BTA_GATTC_ENC_CMPL_EVT:
394         bta_gattc_process_enc_cmpl(p_cb, (tBTA_GATTC_DATA *) p_msg);
395         break;
396 
397     default:
398         if (p_msg->event == BTA_GATTC_INT_CONN_EVT) {
399             p_clcb = bta_gattc_find_int_conn_clcb((tBTA_GATTC_DATA *) p_msg);
400             p_clreg = bta_gattc_cl_get_regcb(((tBTA_GATTC_DATA *)p_msg)->int_conn.client_if);
401             if (p_clreg != NULL){
402                 bta_gattc_conncback(p_clreg, (tBTA_GATTC_DATA *) p_msg);
403             }
404 
405         } else if (p_msg->event == BTA_GATTC_INT_DISCONN_EVT) {
406             p_clreg = bta_gattc_cl_get_regcb(((tBTA_GATTC_DATA *)p_msg)->int_conn.client_if);
407             if (p_clreg != NULL){
408                 bta_gattc_disconncback(p_clreg, (tBTA_GATTC_DATA *) p_msg);
409             }
410             p_clcb = bta_gattc_find_int_disconn_clcb((tBTA_GATTC_DATA *) p_msg);
411         } else {
412             p_clcb = bta_gattc_find_clcb_by_conn_id(p_msg->layer_specific);
413         }
414 
415         if (p_clcb != NULL) {
416             rt = bta_gattc_sm_execute(p_clcb, p_msg->event, (tBTA_GATTC_DATA *) p_msg);
417         } else {
418             APPL_TRACE_DEBUG("Ignore unknown conn ID: %d\n", p_msg->layer_specific);
419         }
420 
421         break;
422     }
423 
424 
425     return rt;
426 }
427 
428 
429 /*****************************************************************************
430 **  Debug Functions
431 *****************************************************************************/
432 #if BTA_GATT_DEBUG == TRUE
433 
434 /*******************************************************************************
435 **
436 ** Function         gattc_evt_code
437 **
438 ** Description
439 **
440 ** Returns          void
441 **
442 *******************************************************************************/
gattc_evt_code(tBTA_GATTC_INT_EVT evt_code)443 static char *gattc_evt_code(tBTA_GATTC_INT_EVT evt_code)
444 {
445     switch (evt_code) {
446     case BTA_GATTC_API_OPEN_EVT:
447         return "BTA_GATTC_API_OPEN_EVT";
448     case BTA_GATTC_INT_OPEN_FAIL_EVT:
449         return "BTA_GATTC_INT_OPEN_FAIL_EVT";
450     case BTA_GATTC_API_CANCEL_OPEN_EVT:
451         return "BTA_GATTC_API_CANCEL_OPEN_EVT";
452     case BTA_GATTC_INT_CANCEL_OPEN_OK_EVT:
453         return "BTA_GATTC_INT_CANCEL_OPEN_OK_EVT";
454     case BTA_GATTC_API_READ_EVT:
455         return "BTA_GATTC_API_READ_EVT";
456     case BTA_GATTC_API_WRITE_EVT:
457         return "BTA_GATTC_API_WRITE_EVT";
458     case BTA_GATTC_API_EXEC_EVT:
459         return "BTA_GATTC_API_EXEC_EVT";
460     case BTA_GATTC_API_CLOSE_EVT:
461         return "BTA_GATTC_API_CLOSE_EVT";
462     case BTA_GATTC_API_SEARCH_EVT:
463         return "BTA_GATTC_API_SEARCH_EVT";
464     case BTA_GATTC_API_CONFIRM_EVT:
465         return "BTA_GATTC_API_CONFIRM_EVT";
466     case BTA_GATTC_API_READ_MULTI_EVT:
467         return "BTA_GATTC_API_READ_MULTI_EVT";
468     case BTA_GATTC_INT_CONN_EVT:
469         return "BTA_GATTC_INT_CONN_EVT";
470     case BTA_GATTC_INT_DISCOVER_EVT:
471         return "BTA_GATTC_INT_DISCOVER_EVT";
472     case BTA_GATTC_DISCOVER_CMPL_EVT:
473         return "BTA_GATTC_DISCOVER_CMPL_EVT";
474     case BTA_GATTC_OP_CMPL_EVT:
475         return "BTA_GATTC_OP_CMPL_EVT";
476     case BTA_GATTC_INT_DISCONN_EVT:
477         return "BTA_GATTC_INT_DISCONN_EVT";
478     case BTA_GATTC_INT_START_IF_EVT:
479         return "BTA_GATTC_INT_START_IF_EVT";
480     case BTA_GATTC_API_REG_EVT:
481         return "BTA_GATTC_API_REG_EVT";
482     case BTA_GATTC_API_DEREG_EVT:
483         return "BTA_GATTC_API_DEREG_EVT";
484     case BTA_GATTC_API_REFRESH_EVT:
485         return "BTA_GATTC_API_REFRESH_EVT";
486     case BTA_GATTC_API_CACHE_CLEAN_EVT:
487         return "BTA_GATTC_API_CACHE_CLEAN_EVT";
488     case BTA_GATTC_API_LISTEN_EVT:
489         return "BTA_GATTC_API_LISTEN_EVT";
490     case BTA_GATTC_API_DISABLE_EVT:
491         return "BTA_GATTC_API_DISABLE_EVT";
492     case BTA_GATTC_API_CFG_MTU_EVT:
493         return "BTA_GATTC_API_CFG_MTU_EVT";
494     case BTA_GATTC_API_READ_BY_TYPE_EVT:
495         return "BTA_GATTC_API_READ_BY_TYPE_EVT";
496     case BTA_GATTC_API_READ_MULTI_VAR_EVT:
497         return "BTA_GATTC_API_READ_MULTI_VAR_EVT";
498     default:
499         return "unknown GATTC event code";
500     }
501 }
502 
503 /*******************************************************************************
504 **
505 ** Function         gattc_state_code
506 **
507 ** Description
508 **
509 ** Returns          void
510 **
511 *******************************************************************************/
gattc_state_code(tBTA_GATTC_STATE state_code)512 static char *gattc_state_code(tBTA_GATTC_STATE state_code)
513 {
514     switch (state_code) {
515     case BTA_GATTC_IDLE_ST:
516         return "GATTC_IDLE_ST";
517     case BTA_GATTC_W4_CONN_ST:
518         return "GATTC_W4_CONN_ST";
519     case BTA_GATTC_CONN_ST:
520         return "GATTC_CONN_ST";
521     case BTA_GATTC_DISCOVER_ST:
522         return "GATTC_DISCOVER_ST";
523     default:
524         return "unknown GATTC state code";
525     }
526 }
527 
528 #endif  /* Debug Functions */
529 
bta_gattc_deinit(void)530 void bta_gattc_deinit(void)
531 {
532 #if BTA_DYNAMIC_MEMORY
533     memset(bta_gattc_cb_ptr, 0, sizeof(tBTA_GATTC_CB));
534     FREE_AND_RESET(bta_gattc_cb_ptr);
535 #endif /* #if BTA_DYNAMIC_MEMORY */
536 }
537 
bta_gattc_cl_rcb_active_count(void)538 uint8_t bta_gattc_cl_rcb_active_count(void)
539 {
540     uint8_t count = 0;
541     uint8_t dm_gattc_uuid[16];
542 
543     // When SDP is included, Bluedroid stack will register the DM GATTC application
544     memset(dm_gattc_uuid, 0x87, 16);
545 
546     for (uint8_t i = 0; i < BTA_GATTC_CL_MAX; i ++) {
547         if (bta_gattc_cb.cl_rcb[i].in_use &&
548             memcmp(bta_gattc_cb.cl_rcb[i].app_uuid.uu.uuid128, dm_gattc_uuid, 16)) {
549             count++;
550         }
551     }
552 
553     return count;
554 }
555 #endif /* GATTC_INCLUDED == TRUE && BLE_INCLUDED == TRUE */
556