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