Home | History | Annotate | Download | only in hl
      1 /******************************************************************************
      2  *
      3  *  Copyright (C) 1998-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 HeaLth device profile main functions and state
     22  *  machine.
     23  *
     24  ******************************************************************************/
     25 #include <string.h>
     26 
     27 #include "bt_target.h"
     28 #if defined(HL_INCLUDED) && (HL_INCLUDED == TRUE)
     29 
     30 
     31 
     32 #include "bta_hl_api.h"
     33 #include "bta_hl_int.h"
     34 #include "gki.h"
     35 #include "utl.h"
     36 #include "bd.h"
     37 #include "l2c_api.h"
     38 #include "mca_defs.h"
     39 
     40 
     41 #if (BTA_HL_DEBUG == TRUE) && (BT_USE_TRACES == TRUE)
     42 static char *bta_hl_cch_state_code(tBTA_HL_CCH_STATE state_code);
     43 static char *bta_hl_dch_state_code(tBTA_HL_DCH_STATE state_code);
     44 #endif
     45 
     46 extern UINT16 L2CA_AllocateRandomPsm(void);
     47 extern UINT16 L2CA_AllocatePsm(void);
     48 /*****************************************************************************
     49 ** DCH State Table
     50 *****************************************************************************/
     51 /*****************************************************************************
     52 ** Constants and types
     53 *****************************************************************************/
     54 /* state machine action enumeration list for DCH */
     55 /* The order of this enumeration must be the same as bta_hl_dch_act_tbl[] */
     56 enum
     57 {
     58     BTA_HL_DCH_MCA_CREATE,
     59     BTA_HL_DCH_MCA_CREATE_CFM,
     60     BTA_HL_DCH_MCA_CREATE_IND,
     61     BTA_HL_DCH_MCA_OPEN_CFM,
     62     BTA_HL_DCH_MCA_OPEN_IND,
     63     BTA_HL_DCH_MCA_CLOSE,
     64     BTA_HL_DCH_MCA_CLOSE_CFM,
     65     BTA_HL_DCH_MCA_CLOSE_IND,
     66     BTA_HL_DCH_CLOSE_CMPL,
     67     BTA_HL_DCH_MCA_RCV_DATA,
     68 
     69     BTA_HL_DCH_SDP_INIT,
     70     BTA_HL_DCH_MCA_RECONNECT,
     71     BTA_HL_DCH_MCA_RECONNECT_IND,
     72     BTA_HL_DCH_MCA_RECONNECT_CFM,
     73     BTA_HL_DCH_CLOSE_ECHO_TEST,
     74     BTA_HL_DCH_CREATE_RSP,
     75     BTA_HL_DCH_MCA_ABORT,
     76     BTA_HL_DCH_MCA_ABORT_IND,
     77     BTA_HL_DCH_MCA_ABORT_CFM,
     78     BTA_HL_DCH_MCA_CONG_CHANGE,
     79 
     80     BTA_HL_DCH_SDP_FAIL,
     81     BTA_HL_DCH_SEND_DATA,
     82     BTA_HL_DCH_CI_GET_TX_DATA,
     83     BTA_HL_DCH_CI_PUT_RX_DATA,
     84     BTA_HL_DCH_CI_GET_ECHO_DATA,
     85     BTA_HL_DCH_ECHO_TEST,
     86     BTA_HL_DCH_CI_PUT_ECHO_DATA,
     87     BTA_HL_DCH_IGNORE
     88 };
     89 
     90 typedef void (*tBTA_HL_DCH_ACTION)(UINT8 app_idx, UINT8 mcl_idx, UINT8 mdl_idx, tBTA_HL_DATA *p_data);
     91 
     92 static const tBTA_HL_DCH_ACTION bta_hl_dch_action[] =
     93 {
     94     bta_hl_dch_mca_create,
     95     bta_hl_dch_mca_create_cfm,
     96     bta_hl_dch_mca_create_ind,
     97     bta_hl_dch_mca_open_cfm,
     98     bta_hl_dch_mca_open_ind,
     99     bta_hl_dch_mca_close,
    100     bta_hl_dch_mca_close_cfm,
    101     bta_hl_dch_mca_close_ind,
    102     bta_hl_dch_close_cmpl,
    103     bta_hl_dch_mca_rcv_data,
    104 
    105     bta_hl_dch_sdp_init,
    106     bta_hl_dch_mca_reconnect,
    107     bta_hl_dch_mca_reconnect_ind,
    108     bta_hl_dch_mca_reconnect_cfm,
    109     bta_hl_dch_close_echo_test,
    110     bta_hl_dch_create_rsp,
    111     bta_hl_dch_mca_abort,
    112     bta_hl_dch_mca_abort_ind,
    113     bta_hl_dch_mca_abort_cfm,
    114     bta_hl_dch_mca_cong_change,
    115 
    116     bta_hl_dch_sdp_fail,
    117     bta_hl_dch_send_data,
    118     bta_hl_dch_ci_get_tx_data,
    119     bta_hl_dch_ci_put_rx_data,
    120     bta_hl_dch_ci_get_echo_data,
    121     bta_hl_dch_echo_test,
    122     bta_hl_dch_ci_put_echo_data,
    123 };
    124 
    125 
    126 /* state table information */
    127 #define BTA_HL_DCH_ACTIONS             1       /* number of actions */
    128 #define BTA_HL_DCH_ACTION_COL          0       /* position of action */
    129 #define BTA_HL_DCH_NEXT_STATE          1       /* position of next state */
    130 #define BTA_HL_DCH_NUM_COLS            2       /* number of columns in state tables */
    131 
    132 /* state table for idle state */
    133 static const UINT8 bta_hl_dch_st_idle[][BTA_HL_DCH_NUM_COLS] =
    134 {
    135 /* Event                                Action 1                    Next state */
    136 /* BTA_HL_DCH_SDP_INIT_EVT   */     {BTA_HL_DCH_SDP_INIT,           BTA_HL_DCH_OPENING_ST},
    137 /* BTA_HL_DCH_OPEN_EVT       */     {BTA_HL_DCH_MCA_CREATE,         BTA_HL_DCH_OPENING_ST},
    138 /* BTA_HL_MCA_CREATE_IND_EVT */     {BTA_HL_DCH_MCA_CREATE_IND,     BTA_HL_DCH_OPENING_ST},
    139 /* BTA_HL_MCA_CREATE_CFM_EVT */     {BTA_HL_DCH_IGNORE,             BTA_HL_DCH_IDLE_ST},
    140 /* BTA_HL_MCA_OPEN_IND_EVT   */     {BTA_HL_DCH_IGNORE,             BTA_HL_DCH_IDLE_ST},
    141 
    142 /* BTA_HL_MCA_OPEN_CFM_EVT   */     {BTA_HL_DCH_IGNORE,             BTA_HL_DCH_IDLE_ST},
    143 /* BTA_HL_DCH_CLOSE_EVT      */     {BTA_HL_DCH_IGNORE,             BTA_HL_DCH_IDLE_ST},
    144 /* BTA_HL_MCA_CLOSE_IND_EVT  */     {BTA_HL_DCH_IGNORE,             BTA_HL_DCH_IDLE_ST},
    145 /* BTA_HL_MCA_CLOSE_CFM_EVT  */     {BTA_HL_DCH_IGNORE,             BTA_HL_DCH_IDLE_ST},
    146 /* BTA_HL_API_SEND_DATA_EVT  */     {BTA_HL_DCH_IGNORE,             BTA_HL_DCH_IDLE_ST},
    147 
    148 /* BTA_HL_MCA_RCV_DATA_EVT   */     {BTA_HL_DCH_IGNORE,             BTA_HL_DCH_IDLE_ST},
    149 /* BTA_HL_DCH_CLOSE_CMPL_EVT */     {BTA_HL_DCH_IGNORE,             BTA_HL_DCH_IDLE_ST},
    150 /* BTA_HL_DCH_RECONNECT_EVT  */     {BTA_HL_DCH_MCA_RECONNECT,      BTA_HL_DCH_OPENING_ST},
    151 /* BTA_HL_DCH_SDP_FAIL_EVT   */     {BTA_HL_DCH_IGNORE,             BTA_HL_DCH_IDLE_ST},
    152 /* BTA_HL_MCA_RECONNECT_IND_EVT*/   {BTA_HL_DCH_MCA_RECONNECT_IND,  BTA_HL_DCH_OPENING_ST},
    153 
    154 /* BTA_HL_MCA_RECONNECT_CFM_EVT*/   {BTA_HL_DCH_IGNORE,             BTA_HL_DCH_IDLE_ST},
    155 /* BTA_HL_DCH_CLOSE_ECHO_TEST_EVT*/ {BTA_HL_DCH_IGNORE,             BTA_HL_DCH_IDLE_ST},
    156 /* BTA_HL_API_DCH_CREATE_RSP_EVT */ {BTA_HL_DCH_IGNORE,             BTA_HL_DCH_IDLE_ST},
    157 /* BTA_HL_DCH_ABORT_EVT */          {BTA_HL_DCH_IGNORE,             BTA_HL_DCH_IDLE_ST},
    158 /* BTA_HL_MCA_ABORT_IND_EVT */      {BTA_HL_DCH_IGNORE,             BTA_HL_DCH_IDLE_ST},
    159 
    160 /* BTA_HL_MCA_ABORT_CFM_EVT */      {BTA_HL_DCH_IGNORE,             BTA_HL_DCH_IDLE_ST},
    161 /* BTA_HL_MCA_CONG_CHG_EVT */       {BTA_HL_DCH_IGNORE,             BTA_HL_DCH_IDLE_ST},
    162 /* BTA_HL_CI_GET_TX_DATA_EVT  */    {BTA_HL_DCH_IGNORE,             BTA_HL_DCH_IDLE_ST},
    163 /* BTA_HL_CI_PUT_RX_DATA_EVT  */    {BTA_HL_DCH_IGNORE,             BTA_HL_DCH_IDLE_ST},
    164 /* BTA_HL_CI_GET_ECHO_DATA_EVT  */  {BTA_HL_DCH_IGNORE,             BTA_HL_DCH_IDLE_ST},
    165 /* BTA_HL_DCH_ECHO_TEST_EVT  */     {BTA_HL_DCH_ECHO_TEST,          BTA_HL_DCH_OPENING_ST},
    166 /* BTA_HL_CI_PUT_ECHO_DATA_EVT  */  {BTA_HL_DCH_IGNORE,             BTA_HL_DCH_IDLE_ST}
    167 };
    168 
    169 /* state table for opening state */
    170 static const UINT8 bta_hl_dch_st_opening[][BTA_HL_DCH_NUM_COLS] =
    171 {
    172 /* Event                                Action 1                    Next state */
    173 /* BTA_HL_DCH_SDP_INIT_EVT   */   {BTA_HL_DCH_SDP_INIT,             BTA_HL_DCH_OPENING_ST},
    174 /* BTA_HL_DCH_OPEN_EVT       */   {BTA_HL_DCH_MCA_CREATE,           BTA_HL_DCH_OPENING_ST},
    175 /* BTA_HL_MCA_CREATE_IND_EVT */   {BTA_HL_DCH_IGNORE,               BTA_HL_DCH_OPENING_ST},
    176 /* BTA_HL_MCA_CREATE_CFM_EVT */   {BTA_HL_DCH_MCA_CREATE_CFM,       BTA_HL_DCH_OPENING_ST},
    177 /* BTA_HL_MCA_OPEN_IND_EVT   */   {BTA_HL_DCH_MCA_OPEN_IND,         BTA_HL_DCH_OPEN_ST},
    178 /* BTA_HL_MCA_OPEN_CFM_EVT   */   {BTA_HL_DCH_MCA_OPEN_CFM,         BTA_HL_DCH_OPEN_ST},
    179 /* BTA_HL_DCH_CLOSE_EVT      */   {BTA_HL_DCH_IGNORE,               BTA_HL_DCH_OPENING_ST},
    180 /* BTA_HL_MCA_CLOSE_IND_EVT  */   {BTA_HL_DCH_MCA_CLOSE_IND,        BTA_HL_DCH_CLOSING_ST},
    181 /* BTA_HL_MCA_CLOSE_CFM_EVT  */   {BTA_HL_DCH_MCA_CLOSE_CFM,        BTA_HL_DCH_CLOSING_ST},
    182 /* BTA_HL_API_SEND_DATA_EVT  */   {BTA_HL_DCH_IGNORE,               BTA_HL_DCH_OPEN_ST},
    183 
    184 /* BTA_HL_MCA_RCV_DATA_EVT   */   {BTA_HL_DCH_IGNORE,               BTA_HL_DCH_OPEN_ST},
    185 /* BTA_HL_DCH_CLOSE_CMPL_EVT */   {BTA_HL_DCH_CLOSE_CMPL,           BTA_HL_DCH_IDLE_ST},
    186 /* BTA_HL_DCH_RECONNECT_EVT  */   {BTA_HL_DCH_MCA_RECONNECT,        BTA_HL_DCH_OPENING_ST},
    187 /* BTA_HL_DCH_SDP_FAIL_EVT   */   {BTA_HL_DCH_SDP_FAIL,             BTA_HL_DCH_CLOSING_ST},
    188 /* BTA_HL_MCA_RECONNECT_IND_EVT*/ {BTA_HL_DCH_IGNORE,               BTA_HL_DCH_IDLE_ST},
    189 /* BTA_HL_MCA_RECONNECT_CFM_EVT*/ {BTA_HL_DCH_MCA_RECONNECT_CFM,    BTA_HL_DCH_OPENING_ST},
    190 /* BTA_HL_DCH_CLOSE_ECHO_TEST_EVT*/ {BTA_HL_DCH_IGNORE,             BTA_HL_DCH_OPENING_ST},
    191 /* BTA_HL_API_DCH_CREATE_RSP_EVT */ {BTA_HL_DCH_CREATE_RSP,         BTA_HL_DCH_OPENING_ST},
    192 /* BTA_HL_DCH_ABORT_EVT */          {BTA_HL_DCH_MCA_ABORT,          BTA_HL_DCH_OPENING_ST},
    193 /* BTA_HL_MCA_ABORT_IND_EVT */      {BTA_HL_DCH_MCA_ABORT_IND,      BTA_HL_DCH_OPENING_ST},
    194 
    195 /* BTA_HL_MCA_ABORT_CFM_EVT */      {BTA_HL_DCH_MCA_ABORT_CFM,      BTA_HL_DCH_OPENING_ST},
    196 /* BTA_HL_MCA_CONG_CHG_EVT */       {BTA_HL_DCH_IGNORE,             BTA_HL_DCH_OPENING_ST},
    197 /* BTA_HL_CI_GET_TX_DATA_EVT  */    {BTA_HL_DCH_IGNORE,             BTA_HL_DCH_OPENING_ST},
    198 /* BTA_HL_CI_PUT_RX_DATA_EVT  */    {BTA_HL_DCH_IGNORE,             BTA_HL_DCH_OPENING_ST},
    199 /* BTA_HL_CI_GET_ECHO_DATA_EVT  */  {BTA_HL_DCH_CI_GET_ECHO_DATA,   BTA_HL_DCH_OPENING_ST},
    200 /* BTA_HL_DCH_ECHO_TEST_EVT  */     {BTA_HL_DCH_ECHO_TEST,          BTA_HL_DCH_OPENING_ST},
    201 /* BTA_HL_CI_PUT_ECHO_DATA_EVT  */  {BTA_HL_DCH_IGNORE,             BTA_HL_DCH_OPENING_ST}
    202 };
    203 
    204 /* state table for open state */
    205 static const UINT8 bta_hl_dch_st_open[][BTA_HL_DCH_NUM_COLS] =
    206 {
    207 /* Event                                Action 1                  Next state */
    208 /* BTA_HL_DCH_SDP_INIT_EVT   */     {BTA_HL_DCH_IGNORE,           BTA_HL_DCH_OPEN_ST},
    209 /* BTA_HL_DCH_OPEN_EVT       */     {BTA_HL_DCH_IGNORE,           BTA_HL_DCH_OPEN_ST},
    210 /* BTA_HL_MCA_CREATE_IND_EVT */     {BTA_HL_DCH_IGNORE,           BTA_HL_DCH_OPEN_ST},
    211 /* BTA_HL_MCA_CREATE_CFM_EVT */     {BTA_HL_DCH_IGNORE,           BTA_HL_DCH_OPEN_ST},
    212 /* BTA_HL_MCA_OPEN_IND_EVT   */     {BTA_HL_DCH_IGNORE,           BTA_HL_DCH_OPEN_ST},
    213 /* BTA_HL_MCA_OPEN_CFM_EVT   */     {BTA_HL_DCH_IGNORE,           BTA_HL_DCH_OPEN_ST},
    214 /* BTA_HL_DCH_CLOSE_EVT      */     {BTA_HL_DCH_MCA_CLOSE,        BTA_HL_DCH_CLOSING_ST},
    215 /* BTA_HL_MCA_CLOSE_IND_EVT  */     {BTA_HL_DCH_MCA_CLOSE_IND,    BTA_HL_DCH_CLOSING_ST},
    216 /* BTA_HL_MCA_CLOSE_CFM_EVT  */     {BTA_HL_DCH_MCA_CLOSE_CFM,    BTA_HL_DCH_CLOSING_ST},
    217 /* BTA_HL_API_SEND_DATA_EVT  */     {BTA_HL_DCH_SEND_DATA,        BTA_HL_DCH_OPEN_ST},
    218 
    219 /* BTA_HL_MCA_RCV_DATA_EVT   */     {BTA_HL_DCH_MCA_RCV_DATA,     BTA_HL_DCH_OPEN_ST},
    220 /* BTA_HL_DCH_CLOSE_CMPL_EVT */     {BTA_HL_DCH_CLOSE_CMPL,       BTA_HL_DCH_IDLE_ST},
    221 /* BTA_HL_DCH_RECONNECT_EVT  */     {BTA_HL_DCH_IGNORE,           BTA_HL_DCH_OPEN_ST},
    222 /* BTA_HL_DCH_SDP_FAIL_EVT   */     {BTA_HL_DCH_IGNORE,           BTA_HL_DCH_OPEN_ST},
    223 /* BTA_HL_MCA_RECONNECT_IND_EVT*/   {BTA_HL_DCH_IGNORE,           BTA_HL_DCH_OPEN_ST},
    224 /* BTA_HL_MCA_RECONNECT_CFM_EVT*/   {BTA_HL_DCH_IGNORE,           BTA_HL_DCH_OPEN_ST},
    225 /* BTA_HL_DCH_CLOSE_ECHO_TEST_EVT*/ {BTA_HL_DCH_CLOSE_ECHO_TEST,  BTA_HL_DCH_CLOSING_ST},
    226 /* BTA_HL_API_DCH_CREATE_RSP_EVT */ {BTA_HL_DCH_IGNORE,           BTA_HL_DCH_OPEN_ST},
    227 /* BTA_HL_DCH_ABORT_EVT */          {BTA_HL_DCH_IGNORE,           BTA_HL_DCH_OPEN_ST},
    228 /* BTA_HL_MCA_ABORT_IND_EVT */      {BTA_HL_DCH_IGNORE,           BTA_HL_DCH_OPEN_ST},
    229 
    230 /* BTA_HL_DCH_ABORT_CFM_EVT */      {BTA_HL_DCH_IGNORE,           BTA_HL_DCH_OPEN_ST},
    231 /* BTA_HL_MCA_CONG_CHG_EVT */       {BTA_HL_DCH_MCA_CONG_CHANGE,  BTA_HL_DCH_OPEN_ST},
    232 /* BTA_HL_CI_GET_TX_DATA_EVT  */    {BTA_HL_DCH_CI_GET_TX_DATA,   BTA_HL_DCH_OPEN_ST},
    233 /* BTA_HL_CI_PUT_RX_DATA_EVT  */    {BTA_HL_DCH_CI_PUT_RX_DATA,   BTA_HL_DCH_OPEN_ST},
    234 /* BTA_HL_CI_GET_ECHO_DATA_EVT  */  {BTA_HL_DCH_CI_GET_ECHO_DATA, BTA_HL_DCH_OPEN_ST},
    235 /* BTA_HL_DCH_ECHO_TEST_EVT  */     {BTA_HL_DCH_IGNORE,           BTA_HL_DCH_OPEN_ST},
    236 /* BTA_HL_CI_PUT_ECHO_DATA_EVT  */  {BTA_HL_DCH_CI_PUT_ECHO_DATA, BTA_HL_DCH_OPEN_ST}
    237 };
    238 
    239 
    240 /* state table for closing state */
    241 static const UINT8 bta_hl_dch_st_closing[][BTA_HL_DCH_NUM_COLS] =
    242 {
    243 /* Event                                Action 1                  Next state */
    244 /* BTA_HL_DCH_SDP_INIT_EVT   */     {BTA_HL_DCH_IGNORE,           BTA_HL_DCH_CLOSING_ST},
    245 /* BTA_HL_DCH_OPEN_EVT       */     {BTA_HL_DCH_IGNORE,           BTA_HL_DCH_CLOSING_ST},
    246 /* BTA_HL_MCA_CREATE_IND_EVT */     {BTA_HL_DCH_IGNORE,           BTA_HL_DCH_CLOSING_ST},
    247 /* BTA_HL_MCA_CREATE_CFM_EVT */     {BTA_HL_DCH_IGNORE,           BTA_HL_DCH_CLOSING_ST},
    248 /* BTA_HL_MCA_OPEN_IND_EVT   */     {BTA_HL_DCH_IGNORE,           BTA_HL_DCH_CLOSING_ST},
    249 /* BTA_HL_MCA_OPEN_CFM_EVT   */     {BTA_HL_DCH_IGNORE,           BTA_HL_DCH_CLOSING_ST},
    250 /* BTA_HL_DCH_CLOSE_EVT      */     {BTA_HL_DCH_MCA_CLOSE,        BTA_HL_DCH_CLOSING_ST},
    251 /* BTA_HL_MCA_CLOSE_IND_EVT  */     {BTA_HL_DCH_IGNORE,           BTA_HL_DCH_CLOSING_ST},
    252 /* BTA_HL_MCA_CLOSE_CFM_EVT  */     {BTA_HL_DCH_MCA_CLOSE_CFM,    BTA_HL_DCH_CLOSING_ST},
    253 /* BTA_HL_API_SEND_DATA_EVT  */     {BTA_HL_DCH_IGNORE,           BTA_HL_DCH_CLOSING_ST},
    254 
    255 /* BTA_HL_MCA_RCV_DATA_EVT   */     {BTA_HL_DCH_IGNORE,           BTA_HL_DCH_CLOSING_ST},
    256 /* BTA_HL_DCH_CLOSE_CMPL_EVT */     {BTA_HL_DCH_CLOSE_CMPL,       BTA_HL_DCH_IDLE_ST},
    257 /* BTA_HL_DCH_RECONNECT_EVT  */     {BTA_HL_DCH_IGNORE,           BTA_HL_DCH_CLOSING_ST},
    258 /* BTA_HL_DCH_SDP_FAIL_EVT   */     {BTA_HL_DCH_IGNORE,           BTA_HL_DCH_CLOSING_ST},
    259 /* BTA_HL_MCA_RECONNECT_IND_EVT*/   {BTA_HL_DCH_IGNORE,           BTA_HL_DCH_CLOSING_ST},
    260 /* BTA_HL_MCA_RECONNECT_CFM_EVT*/   {BTA_HL_DCH_IGNORE,           BTA_HL_DCH_CLOSING_ST},
    261 /* BTA_HL_DCH_CLOSE_ECHO_TEST_EVT*/ {BTA_HL_DCH_IGNORE,           BTA_HL_DCH_CLOSING_ST},
    262 /* BTA_HL_API_DCH_CREATE_RSP_EVT */ {BTA_HL_DCH_IGNORE,           BTA_HL_DCH_CLOSING_ST},
    263 /* BTA_HL_DCH_ABORT_EVT */          {BTA_HL_DCH_IGNORE,           BTA_HL_DCH_CLOSING_ST},
    264 /* BTA_HL_MCA_ABORT_IND_EVT */      {BTA_HL_DCH_IGNORE,           BTA_HL_DCH_CLOSING_ST},
    265 
    266 /* BTA_HL_DCH_ABORT_CFM_EVT */      {BTA_HL_DCH_IGNORE,           BTA_HL_DCH_CLOSING_ST},
    267 /* BTA_HL_MCA_CONG_CHG_EVT */       {BTA_HL_DCH_IGNORE,           BTA_HL_DCH_CLOSING_ST},
    268 /* BTA_HL_CI_GET_TX_DATA_EVT  */    {BTA_HL_DCH_CI_GET_TX_DATA,   BTA_HL_DCH_CLOSING_ST},
    269 /* BTA_HL_CI_PUT_RX_DATA_EVT  */    {BTA_HL_DCH_CI_PUT_RX_DATA,   BTA_HL_DCH_CLOSING_ST},
    270 /* BTA_HL_CI_GET_ECHO_DATA_EVT  */  {BTA_HL_DCH_CI_GET_ECHO_DATA, BTA_HL_DCH_CLOSING_ST},
    271 /* BTA_HL_DCH_ECHO_TEST_EVT  */     {BTA_HL_DCH_IGNORE,           BTA_HL_DCH_CLOSING_ST},
    272 /* BTA_HL_CI_PUT_ECHO_DATA_EVT  */  {BTA_HL_DCH_CI_PUT_ECHO_DATA, BTA_HL_DCH_CLOSING_ST}
    273 };
    274 
    275 /* type for state table */
    276 typedef const UINT8 (*tBTA_HL_DCH_ST_TBL)[BTA_HL_DCH_NUM_COLS];
    277 
    278 /* state table */
    279 const tBTA_HL_DCH_ST_TBL bta_hl_dch_st_tbl[] =
    280 {
    281     bta_hl_dch_st_idle,
    282     bta_hl_dch_st_opening,
    283     bta_hl_dch_st_open,
    284     bta_hl_dch_st_closing
    285 };
    286 
    287 /*****************************************************************************
    288 ** CCH State Table
    289 *****************************************************************************/
    290 /*****************************************************************************
    291 ** Constants and types
    292 *****************************************************************************/
    293 /* state machine action enumeration list for CCH */
    294 enum
    295 {
    296     BTA_HL_CCH_SDP_INIT,
    297     BTA_HL_CCH_MCA_OPEN,
    298     BTA_HL_CCH_MCA_CLOSE,
    299     BTA_HL_CCH_CLOSE_CMPL,
    300     BTA_HL_CCH_MCA_CONNECT,
    301     BTA_HL_CCH_MCA_DISCONNECT,
    302     BTA_HL_CCH_MCA_RSP_TOUT,
    303     BTA_HL_CCH_MCA_DISC_OPEN,
    304     BTA_HL_CCH_IGNORE
    305 };
    306 
    307 /* type for action functions */
    308 typedef void (*tBTA_HL_CCH_ACTION)(UINT8 app_idx, UINT8 mcl_idx, tBTA_HL_DATA *p_data);
    309 
    310 /* action function list for MAS */
    311 const tBTA_HL_CCH_ACTION bta_hl_cch_action[] =
    312 {
    313     bta_hl_cch_sdp_init,
    314     bta_hl_cch_mca_open,
    315     bta_hl_cch_mca_close,
    316     bta_hl_cch_close_cmpl,
    317     bta_hl_cch_mca_connect,
    318     bta_hl_cch_mca_disconnect,
    319     bta_hl_cch_mca_rsp_tout,
    320     bta_hl_cch_mca_disc_open
    321 };
    322 
    323 
    324 /* state table information */
    325 #define BTA_HL_CCH_ACTIONS             1       /* number of actions */
    326 #define BTA_HL_CCH_NEXT_STATE          1       /* position of next state */
    327 #define BTA_HL_CCH_NUM_COLS            2       /* number of columns in state tables */
    328 
    329 
    330 /* state table for MAS idle state */
    331 static const UINT8 bta_hl_cch_st_idle[][BTA_HL_CCH_NUM_COLS] =
    332 {
    333 /* Event                          Action 1                  Next state */
    334 /* BTA_HL_CCH_OPEN_EVT           */ {BTA_HL_CCH_SDP_INIT,       BTA_HL_CCH_OPENING_ST},
    335 /* BTA_HL_CCH_SDP_OK_EVT         */ {BTA_HL_CCH_IGNORE,         BTA_HL_CCH_IDLE_ST},
    336 /* BTA_HL_CCH_SDP_FAIL_EVT       */ {BTA_HL_CCH_IGNORE,         BTA_HL_CCH_IDLE_ST},
    337 /* BTA_HL_MCA_CONNECT_IND_EVT    */ {BTA_HL_CCH_MCA_CONNECT,    BTA_HL_CCH_OPEN_ST},
    338 /* BTA_HL_MCA_DISCONNECT_IND_EVT */ {BTA_HL_CCH_IGNORE,         BTA_HL_CCH_IDLE_ST},
    339 /* BTA_HL_CCH_CLOSE_EVT          */ {BTA_HL_CCH_IGNORE,         BTA_HL_CCH_IDLE_ST},
    340 /* BTA_HL_CCH_CLOSE_CMPL_EVT     */ {BTA_HL_CCH_IGNORE,         BTA_HL_CCH_IDLE_ST},
    341 /* BTA_HL_CCH_CLOSE_CMPL_EVT     */ {BTA_HL_CCH_IGNORE,         BTA_HL_CCH_IDLE_ST}
    342 };
    343 
    344 /* state table for obex/rfcomm connection state */
    345 static const UINT8 bta_hl_cch_st_opening[][BTA_HL_CCH_NUM_COLS] =
    346 {
    347 /* Event                          Action 1               Next state */
    348 /* BTA_HL_CCH_OPEN_EVT           */ {BTA_HL_CCH_IGNORE,         BTA_HL_CCH_OPENING_ST},
    349 /* BTA_HL_CCH_SDP_OK_EVT         */ {BTA_HL_CCH_MCA_OPEN,       BTA_HL_CCH_OPENING_ST},
    350 /* BTA_HL_CCH_SDP_FAIL_EVT       */ {BTA_HL_CCH_CLOSE_CMPL,     BTA_HL_CCH_IDLE_ST},
    351 /* BTA_HL_MCA_CONNECT_IND_EVT    */ {BTA_HL_CCH_MCA_CONNECT,    BTA_HL_CCH_OPEN_ST},
    352 /* BTA_HL_MCA_DISCONNECT_IND_EVT */ {BTA_HL_CCH_MCA_DISCONNECT, BTA_HL_CCH_CLOSING_ST},
    353 /* BTA_HL_CCH_CLOSE_EVT          */ {BTA_HL_CCH_MCA_CLOSE,      BTA_HL_CCH_CLOSING_ST},
    354 /* BTA_HL_CCH_CLOSE_CMPL_EVT     */ {BTA_HL_CCH_CLOSE_CMPL,     BTA_HL_CCH_IDLE_ST},
    355 /* BTA_HL_MCA_RSP_TOUT_IND_EVT   */ {BTA_HL_CCH_MCA_RSP_TOUT,   BTA_HL_CCH_CLOSING_ST}
    356 };
    357 
    358 /* state table for open state */
    359 static const UINT8 bta_hl_cch_st_open[][BTA_HL_CCH_NUM_COLS] =
    360 {
    361 /* Event                          Action 1                  Next state */
    362 /* BTA_HL_CCH_OPEN_EVT           */ {BTA_HL_CCH_IGNORE,         BTA_HL_CCH_OPEN_ST},
    363 /* BTA_HL_CCH_SDP_OK_EVT         */ {BTA_HL_CCH_IGNORE,         BTA_HL_CCH_OPEN_ST},
    364 /* BTA_HL_CCH_SDP_FAIL_EVT       */ {BTA_HL_CCH_IGNORE,         BTA_HL_CCH_OPEN_ST},
    365 /* BTA_HL_MCA_CONNECT_IND_EVT    */ {BTA_HL_CCH_IGNORE,         BTA_HL_CCH_OPEN_ST},
    366 /* BTA_HL_MCA_DISCONNECT_IND_EVT */ {BTA_HL_CCH_MCA_DISCONNECT, BTA_HL_CCH_CLOSING_ST},
    367 /* BTA_HL_CCH_CLOSE_EVT          */ {BTA_HL_CCH_MCA_CLOSE,      BTA_HL_CCH_CLOSING_ST},
    368 /* BTA_HL_CCH_CLOSE_CMPL_EVT     */ {BTA_HL_CCH_IGNORE,         BTA_HL_CCH_OPEN_ST},
    369 /* BTA_HL_MCA_RSP_TOUT_IND_EVT   */ {BTA_HL_CCH_MCA_RSP_TOUT,   BTA_HL_CCH_CLOSING_ST}
    370 };
    371 
    372 /* state table for closing state */
    373 static const UINT8 bta_hl_cch_st_closing[][BTA_HL_CCH_NUM_COLS] =
    374 {
    375 /* Event                          Action 1                  Next state */
    376 /* BTA_HL_CCH_OPEN_EVT           */ {BTA_HL_CCH_IGNORE,         BTA_HL_CCH_CLOSING_ST},
    377 /* BTA_HL_CCH_SDP_OK_EVT         */ {BTA_HL_CCH_CLOSE_CMPL,     BTA_HL_CCH_IDLE_ST},
    378 /* BTA_HL_CCH_SDP_FAIL_EVT       */ {BTA_HL_CCH_CLOSE_CMPL,     BTA_HL_CCH_IDLE_ST},
    379 /* BTA_HL_MCA_CONNECT_IND_EVT    */ {BTA_HL_CCH_MCA_DISC_OPEN,  BTA_HL_CCH_CLOSING_ST},
    380 /* BTA_HL_MCA_DISCONNECT_IND_EVT */ {BTA_HL_CCH_MCA_DISCONNECT, BTA_HL_CCH_CLOSING_ST},
    381 /* BTA_HL_CCH_CLOSE_EVT          */ {BTA_HL_CCH_MCA_CLOSE,      BTA_HL_CCH_CLOSING_ST},
    382 /* BTA_HL_CCH_CLOSE_CMPL_EVT     */ {BTA_HL_CCH_CLOSE_CMPL,     BTA_HL_CCH_IDLE_ST},
    383 /* BTA_HL_MCA_RSP_TOUT_IND_EVT   */ {BTA_HL_CCH_IGNORE,         BTA_HL_CCH_CLOSING_ST}
    384 };
    385 
    386 /* type for state table CCH */
    387 typedef const UINT8 (*tBTA_HL_CCH_ST_TBL)[BTA_HL_CCH_NUM_COLS];
    388 
    389 /* MAS state table */
    390 const tBTA_HL_CCH_ST_TBL bta_hl_cch_st_tbl[] =
    391 {
    392     bta_hl_cch_st_idle,
    393     bta_hl_cch_st_opening,
    394     bta_hl_cch_st_open,
    395     bta_hl_cch_st_closing
    396 };
    397 
    398 
    399 /*****************************************************************************
    400 ** Global data
    401 *****************************************************************************/
    402 
    403 /* HL control block */
    404 #if BTA_DYNAMIC_MEMORY == FALSE
    405 tBTA_HL_CB  bta_hl_cb;
    406 #endif
    407 
    408 
    409 /*******************************************************************************
    410 **
    411 ** Function         bta_hl_cch_sm_execute
    412 **
    413 ** Description      State machine event handling function for CCH
    414 **
    415 ** Returns          void
    416 **
    417 *******************************************************************************/
    418 void bta_hl_cch_sm_execute(UINT8 app_idx, UINT8 mcl_idx,
    419                            UINT16 event, tBTA_HL_DATA *p_data)
    420 {
    421     tBTA_HL_CCH_ST_TBL  state_table;
    422     UINT8               action;
    423     int                 i;
    424     tBTA_HL_MCL_CB      *p_cb  = BTA_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
    425 
    426 #if (BTA_HL_DEBUG == TRUE) && (BT_USE_TRACES == TRUE)
    427     tBTA_HL_CCH_STATE in_state = p_cb->cch_state;
    428     UINT16             cur_evt = event;
    429     APPL_TRACE_DEBUG("HDP CCH Event Handler: State 0x%02x [%s], Event [%s]", in_state,
    430                       bta_hl_cch_state_code(in_state),
    431                       bta_hl_evt_code(cur_evt));
    432 #endif
    433 
    434     /* look up the state table for the current state */
    435     state_table = bta_hl_cch_st_tbl[p_cb->cch_state];
    436 
    437     event &= 0x00FF;
    438 
    439     /* set next state */
    440     p_cb->cch_state = state_table[event][BTA_HL_CCH_NEXT_STATE];
    441 
    442     for (i = 0; i < BTA_HL_CCH_ACTIONS; i++)
    443     {
    444         if ((action = state_table[event][i]) != BTA_HL_CCH_IGNORE)
    445         {
    446             (*bta_hl_cch_action[action])(app_idx, mcl_idx, p_data);
    447         }
    448         else
    449         {
    450             /* discard HDP data */
    451             bta_hl_discard_data(p_data->hdr.event, p_data);
    452             break;
    453         }
    454     }
    455 #if (BTA_HL_DEBUG == TRUE) && (BT_USE_TRACES == TRUE)
    456     if (in_state != p_cb->cch_state)
    457     {
    458         APPL_TRACE_DEBUG("HL CCH State Change: [%s] -> [%s] after [%s]",
    459                           bta_hl_cch_state_code(in_state),
    460                           bta_hl_cch_state_code(p_cb->cch_state),
    461                           bta_hl_evt_code(cur_evt));
    462     }
    463 #endif
    464 
    465 }
    466 
    467 /*******************************************************************************
    468 **
    469 ** Function         bta_hl_dch_sm_execute
    470 **
    471 ** Description      State machine event handling function for DCH
    472 **
    473 ** Returns          void
    474 **
    475 *******************************************************************************/
    476 void bta_hl_dch_sm_execute(UINT8 app_idx, UINT8 mcl_idx, UINT8 mdl_idx,
    477                            UINT16 event, tBTA_HL_DATA *p_data)
    478 {
    479     tBTA_HL_DCH_ST_TBL  state_table;
    480     UINT8               action;
    481     int                 i;
    482     tBTA_HL_MDL_CB      *p_cb  = BTA_HL_GET_MDL_CB_PTR(app_idx, mcl_idx, mdl_idx);
    483 
    484 #if (BTA_HL_DEBUG == TRUE) && (BT_USE_TRACES == TRUE)
    485     tBTA_HL_DCH_STATE in_state = p_cb->dch_state;
    486     UINT16             cur_evt = event;
    487     APPL_TRACE_DEBUG("HDP DCH Event Handler: State 0x%02x [%s], Event [%s]", in_state,
    488                       bta_hl_dch_state_code(in_state),
    489                       bta_hl_evt_code(cur_evt));
    490 #endif
    491 
    492     /* look up the state table for the current state */
    493     state_table = bta_hl_dch_st_tbl[p_cb->dch_state];
    494     event -= BTA_HL_DCH_EVT_MIN;
    495 
    496     /* set next state */
    497     p_cb->dch_state = state_table[event][BTA_HL_DCH_NEXT_STATE];
    498 
    499     for (i = 0; i < BTA_HL_DCH_ACTIONS; i++)
    500     {
    501         if ((action = state_table[event][i]) != BTA_HL_DCH_IGNORE)
    502         {
    503             (*bta_hl_dch_action[action])(app_idx, mcl_idx, mdl_idx, p_data);
    504         }
    505         else
    506         {
    507             /* discard mas data */
    508             bta_hl_discard_data(p_data->hdr.event, p_data);
    509             break;
    510         }
    511     }
    512 
    513 
    514 #if (BTA_HL_DEBUG == TRUE) && (BT_USE_TRACES == TRUE)
    515     if (in_state != p_cb->dch_state)
    516     {
    517         APPL_TRACE_DEBUG("HL DCH State Change: [%s] -> [%s] after [%s]",
    518                           bta_hl_dch_state_code(in_state),
    519                           bta_hl_dch_state_code(p_cb->dch_state),
    520                           bta_hl_evt_code(cur_evt));
    521     }
    522 #endif
    523 }
    524 /*******************************************************************************
    525 **
    526 ** Function         bta_hl_api_enable
    527 **
    528 ** Description      Process the API enable request to enable the HL subsystem
    529 **
    530 ** Returns          void
    531 **
    532 *******************************************************************************/
    533 static void bta_hl_api_enable(tBTA_HL_CB *p_cb, tBTA_HL_DATA *p_data)
    534 {
    535     tBTA_HL_CTRL    evt_data;
    536 
    537     /* If already enabled then reject this request */
    538     if (p_cb->enable)
    539     {
    540         APPL_TRACE_ERROR("HL is already enabled");
    541         evt_data.enable_cfm.status = BTA_HL_STATUS_FAIL;
    542         if (p_data->api_enable.p_cback)
    543             p_data->api_enable.p_cback(BTA_HL_CTRL_ENABLE_CFM_EVT, (tBTA_HL_CTRL *) &evt_data);
    544         return;
    545     }
    546 
    547     /* Done with checking. now perform the enable oepration*/
    548     /* initialize control block */
    549     memset(p_cb, 0, sizeof(tBTA_HL_CB));
    550     p_cb->enable = TRUE;
    551     p_cb->p_ctrl_cback = p_data->api_enable.p_cback;
    552     evt_data.enable_cfm.status = BTA_HL_STATUS_OK;
    553     if (p_data->api_enable.p_cback)
    554         p_data->api_enable.p_cback(BTA_HL_CTRL_ENABLE_CFM_EVT, (tBTA_HL_CTRL *) &evt_data);
    555 
    556 }
    557 /*******************************************************************************
    558 **
    559 ** Function         bta_hl_api_disable
    560 **
    561 ** Description      Process the API disable request to disable the HL subsystem
    562 **
    563 ** Returns          void
    564 **
    565 *******************************************************************************/
    566 static void bta_hl_api_disable(tBTA_HL_CB *p_cb, tBTA_HL_DATA *p_data)
    567 {
    568     tBTA_HL_CTRL    evt_data;
    569     tBTA_HL_STATUS  status = BTA_HL_STATUS_OK;
    570 
    571     if (p_cb->enable)
    572     {
    573         p_cb->disabling = TRUE;
    574         bta_hl_check_disable(p_data);
    575     }
    576     else
    577     {
    578         status = BTA_HL_STATUS_FAIL;
    579         evt_data.disable_cfm.status = status;
    580         if (p_cb->p_ctrl_cback) p_cb->p_ctrl_cback(BTA_HL_CTRL_DISABLE_CFM_EVT, (tBTA_HL_CTRL *) &evt_data);
    581     }
    582 
    583 
    584 #if BTA_HL_DEBUG == TRUE
    585     if (status != BTA_HL_STATUS_OK)
    586     {
    587         APPL_TRACE_DEBUG("bta_hl_api_disable status =%s", bta_hl_status_code(status));
    588     }
    589 #endif
    590 
    591 }
    592 
    593 /*******************************************************************************
    594 **
    595 ** Function         bta_hl_api_update
    596 **
    597 ** Description      Process the API registration request to register an HDP applciation
    598 **
    599 ** Returns          void
    600 **
    601 *******************************************************************************/
    602 static void bta_hl_api_update(tBTA_HL_CB *p_cb, tBTA_HL_DATA *p_data)
    603 {
    604     tBTA_HL         evt_data;
    605     tBTA_HL_APP_CB  *p_acb = BTA_HL_GET_APP_CB_PTR(0);
    606     tBTA_HL_STATUS  status = BTA_HL_STATUS_FAIL;
    607 
    608 
    609     APPL_TRACE_DEBUG("bta_hl_api_update");
    610     if (p_cb->enable)
    611     {
    612 
    613         status = bta_hl_app_update(p_data->api_update.app_id, p_data->api_update.is_register);
    614         if (!p_data->api_update.is_register)
    615         {
    616             APPL_TRACE_DEBUG("Deregister");
    617             memset(&evt_data, 0, sizeof(tBTA_HL));
    618             evt_data.dereg_cfm.status = status;
    619             evt_data.dereg_cfm.app_id = p_data->api_update.app_id;
    620             if (status == BTA_HL_STATUS_OK)
    621                 evt_data.dereg_cfm.app_handle = p_acb->app_handle;
    622             if (p_acb->p_cback)
    623             {
    624                 p_acb->p_cback(BTA_HL_DEREGISTER_CFM_EVT, (tBTA_HL *) &evt_data);
    625             }
    626             return ;
    627         }
    628 
    629     }
    630 
    631     if (status != BTA_HL_STATUS_OK)
    632     {
    633         if ((status != BTA_HL_STATUS_DUPLICATE_APP_ID) &&
    634             (status != BTA_HL_STATUS_NO_RESOURCE))
    635         {
    636             if (p_acb)
    637                 memset(p_acb, 0, sizeof(tBTA_HL_APP_CB));
    638         }
    639     }
    640 #if BTA_HL_DEBUG == TRUE
    641     if (status != BTA_HL_STATUS_OK)
    642     {
    643         APPL_TRACE_DEBUG("bta_hl_api_register status =%s", bta_hl_status_code(status));
    644     }
    645 #endif
    646 
    647     memset(&evt_data, 0, sizeof(tBTA_HL));
    648     evt_data.reg_cfm.status = status;
    649     evt_data.reg_cfm.app_id = p_data->api_update.app_id;
    650     if (status == BTA_HL_STATUS_OK)
    651         evt_data.reg_cfm.app_handle = p_acb->app_handle;
    652     if (p_data->api_reg.p_cback)
    653     {
    654         p_data->api_reg.p_cback(BTA_HL_REGISTER_CFM_EVT, (tBTA_HL *) &evt_data);
    655     }
    656 
    657     if (status == BTA_HL_STATUS_OK)
    658     {
    659         evt_data.sdp_info_ind.app_handle = p_acb->app_handle;
    660         evt_data.sdp_info_ind.ctrl_psm = p_acb->ctrl_psm;
    661         evt_data.sdp_info_ind.data_psm = p_acb->data_psm;
    662         evt_data.sdp_info_ind.data_x_spec = BTA_HL_SDP_IEEE_11073_20601;
    663         evt_data.sdp_info_ind.mcap_sup_procs = BTA_HL_MCAP_SUP_PROC_MASK ;
    664 
    665         if (p_data->api_reg.p_cback)
    666         {
    667             p_data->api_reg.p_cback(BTA_HL_SDP_INFO_IND_EVT, (tBTA_HL *) &evt_data);
    668         }
    669     }
    670 }
    671 
    672 /*******************************************************************************
    673 **
    674 ** Function         bta_hl_api_register
    675 **
    676 ** Description      Process the API registration request to register an HDP applciation
    677 **
    678 ** Returns          void
    679 **
    680 *******************************************************************************/
    681 static void bta_hl_api_register(tBTA_HL_CB *p_cb, tBTA_HL_DATA *p_data)
    682 {
    683     tBTA_HL         evt_data;
    684     UINT8           app_idx;
    685     tBTA_HL_APP_CB  *p_acb = NULL;
    686     tBTA_HL_STATUS  status = BTA_HL_STATUS_FAIL;
    687 
    688 
    689     if (p_cb->enable)
    690     {
    691         if (!bta_hl_is_a_duplicate_id(p_data->api_reg.app_id))
    692         {
    693             if (bta_hl_find_avail_app_idx(&app_idx))
    694             {
    695                 p_acb = BTA_HL_GET_APP_CB_PTR(app_idx);
    696                 p_acb->in_use = TRUE;
    697                 p_acb->app_id = p_data->api_reg.app_id;
    698                 p_acb->p_cback = p_data->api_reg.p_cback;
    699                 p_acb->sec_mask = p_data->api_reg.sec_mask;
    700                 p_acb->dev_type = p_data->api_reg.dev_type;
    701                 BCM_STRNCPY_S(p_acb->srv_name, sizeof(p_acb->srv_name), p_data->api_reg.srv_name, BTA_SERVICE_NAME_LEN);
    702                 BCM_STRNCPY_S(p_acb->srv_desp, sizeof(p_acb->srv_desp), p_data->api_reg.srv_desp, BTA_SERVICE_DESP_LEN);
    703                 BCM_STRNCPY_S(p_acb->provider_name, sizeof(p_acb->provider_name), p_data->api_reg.provider_name, BTA_PROVIDER_NAME_LEN);
    704                 bta_hl_cb.p_alloc_psm = L2CA_AllocatePSM;
    705                 p_acb->ctrl_psm = bta_hl_cb.p_alloc_psm();
    706                 p_acb->data_psm = bta_hl_cb.p_alloc_psm();
    707                 p_acb->p_mcap_cback = bta_hl_mcap_ctrl_cback;
    708                 status = bta_hl_app_registration(app_idx);
    709             }
    710             else
    711             {
    712                 status = BTA_HL_STATUS_NO_RESOURCE;
    713             }
    714         }
    715         else
    716         {
    717             status = BTA_HL_STATUS_DUPLICATE_APP_ID;
    718         }
    719     }
    720 
    721     if (status != BTA_HL_STATUS_OK)
    722     {
    723         if ((status != BTA_HL_STATUS_DUPLICATE_APP_ID) &&
    724             (status != BTA_HL_STATUS_NO_RESOURCE))
    725         {
    726             if (p_acb)
    727                 memset(p_acb, 0, sizeof(tBTA_HL_APP_CB));
    728         }
    729     }
    730 #if BTA_HL_DEBUG == TRUE
    731     if (status != BTA_HL_STATUS_OK)
    732     {
    733         APPL_TRACE_DEBUG("bta_hl_api_register status =%s", bta_hl_status_code(status));
    734     }
    735 #endif
    736 
    737     memset(&evt_data, 0, sizeof(tBTA_HL));
    738     evt_data.reg_cfm.status = status;
    739     evt_data.reg_cfm.app_id = p_data->api_reg.app_id;
    740     if (status == BTA_HL_STATUS_OK)
    741         evt_data.reg_cfm.app_handle = p_acb->app_handle;
    742     if (p_data->api_reg.p_cback)
    743     {
    744         p_data->api_reg.p_cback(BTA_HL_REGISTER_CFM_EVT, (tBTA_HL *) &evt_data);
    745     }
    746 
    747     if (status == BTA_HL_STATUS_OK)
    748     {
    749         evt_data.sdp_info_ind.app_handle = p_acb->app_handle;
    750         evt_data.sdp_info_ind.ctrl_psm = p_acb->ctrl_psm;
    751         evt_data.sdp_info_ind.data_psm = p_acb->data_psm;
    752         evt_data.sdp_info_ind.data_x_spec = BTA_HL_SDP_IEEE_11073_20601;
    753         evt_data.sdp_info_ind.mcap_sup_procs = BTA_HL_MCAP_SUP_PROC_MASK ;
    754 
    755         if (p_data->api_reg.p_cback)
    756         {
    757             p_data->api_reg.p_cback(BTA_HL_SDP_INFO_IND_EVT, (tBTA_HL *) &evt_data);
    758         }
    759     }
    760 }
    761 
    762 /*******************************************************************************
    763 **
    764 ** Function         bta_hl_api_deregister
    765 **
    766 ** Description      Process the API de-registration request
    767 **
    768 ** Returns          void
    769 **
    770 *******************************************************************************/
    771 static void bta_hl_api_deregister(tBTA_HL_CB *p_cb, tBTA_HL_DATA *p_data)
    772 {
    773     UINT8           app_idx;
    774     tBTA_HL_APP_CB  *p_acb;
    775     UNUSED(p_cb);
    776 
    777     if (bta_hl_find_app_idx_using_handle(p_data->api_dereg.app_handle, &app_idx))
    778     {
    779         p_acb = BTA_HL_GET_APP_CB_PTR(app_idx);
    780         p_acb->deregistering= TRUE;
    781         bta_hl_check_deregistration(app_idx,p_data);
    782     }
    783     else
    784     {
    785         APPL_TRACE_ERROR("Invalid app_handle=%d", p_data->api_dereg.app_handle);
    786     }
    787 }
    788 
    789 /*******************************************************************************
    790 **
    791 ** Function         bta_hl_api_cch_open
    792 **
    793 ** Description      Process the API CCH open request
    794 **
    795 ** Returns          void
    796 **
    797 *******************************************************************************/
    798 static void bta_hl_api_cch_open(tBTA_HL_CB *p_cb, tBTA_HL_DATA *p_data)
    799 {
    800     tBTA_HL         evt_data;
    801     tBTA_HL_STATUS  status = BTA_HL_STATUS_OK;
    802     UINT8           app_idx, mcl_idx;
    803     tBTA_HL_APP_CB  *p_acb;
    804     tBTA_HL_MCL_CB  *p_mcb;
    805     UNUSED(p_cb);
    806 
    807     if (bta_hl_find_app_idx_using_handle(p_data->api_cch_open.app_handle, &app_idx))
    808     {
    809 
    810         if (!bta_hl_find_mcl_idx(app_idx, p_data->api_cch_open.bd_addr, &mcl_idx))
    811         {
    812             if (bta_hl_find_avail_mcl_idx(app_idx, &mcl_idx))
    813             {
    814                 p_mcb = BTA_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
    815                 p_mcb->in_use = TRUE;
    816                 p_mcb->req_ctrl_psm = p_data->api_cch_open.ctrl_psm;
    817                 p_mcb->sec_mask = p_data->api_cch_open.sec_mask;
    818                 bdcpy(p_mcb->bd_addr, p_data->api_cch_open.bd_addr);
    819                 p_mcb->cch_oper = BTA_HL_CCH_OP_LOCAL_OPEN;
    820             }
    821             else
    822             {
    823                 status = BTA_HL_STATUS_NO_RESOURCE;
    824             }
    825         }
    826         else
    827         {
    828             /* Only one MCL per BD_ADDR */
    829             status = BTA_HL_STATUS_DUPLICATE_CCH_OPEN;
    830             APPL_TRACE_DEBUG("bta_hl_api_cch_open: CCH already open: status =%d",status)
    831             p_acb = BTA_HL_GET_APP_CB_PTR(app_idx);
    832             p_mcb = BTA_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
    833             if (p_acb->p_cback)
    834             {
    835                 bta_hl_build_cch_open_cfm(&evt_data, p_data->api_cch_open.app_id,
    836                                           p_data->api_cch_open.app_handle,
    837                                           p_mcb->mcl_handle,
    838                                           p_data->api_cch_open.bd_addr,
    839                                           status);
    840                 p_acb->p_cback(BTA_HL_CCH_OPEN_CFM_EVT,(tBTA_HL *) &evt_data );
    841             }
    842             else
    843             {
    844                 APPL_TRACE_ERROR("bta_hl_api_cch_open Null Callback");
    845             }
    846             return;
    847         }
    848     }
    849     else
    850     {
    851         status = BTA_HL_STATUS_INVALID_APP_HANDLE;
    852     }
    853 #if BTA_HL_DEBUG == TRUE
    854     if (status != BTA_HL_STATUS_OK)
    855     {
    856         APPL_TRACE_DEBUG("bta_hl_api_cch_open status =%s", bta_hl_status_code(status));
    857     }
    858 #endif
    859     switch (status)
    860     {
    861         case BTA_HL_STATUS_OK:
    862             bta_hl_cch_sm_execute(app_idx, mcl_idx, BTA_HL_CCH_OPEN_EVT, p_data);
    863             break;
    864         case BTA_HL_STATUS_NO_RESOURCE:
    865         case BTA_HL_STATUS_FAIL:
    866 
    867             p_acb = BTA_HL_GET_APP_CB_PTR(app_idx);
    868             if (p_acb->p_cback)
    869             {
    870                 bta_hl_build_cch_open_cfm(&evt_data, p_data->api_cch_open.app_id,
    871                                           p_data->api_cch_open.app_handle,
    872                                           0,
    873                                           p_data->api_cch_open.bd_addr,
    874                                           status);
    875                 p_acb->p_cback(BTA_HL_CCH_OPEN_CFM_EVT,(tBTA_HL *) &evt_data );
    876             }
    877             else
    878             {
    879                 APPL_TRACE_ERROR("bta_hl_api_cch_open Null Callback");
    880             }
    881             break;
    882         default:
    883             APPL_TRACE_ERROR("status code=%d", status);
    884             break;
    885     }
    886 }
    887 
    888 /*******************************************************************************
    889 **
    890 ** Function         bta_hl_api_cch_close
    891 **
    892 ** Description      Process the API CCH close request
    893 **
    894 ** Returns          void
    895 **
    896 *******************************************************************************/
    897 static void bta_hl_api_cch_close(tBTA_HL_CB *p_cb, tBTA_HL_DATA *p_data)
    898 {
    899     tBTA_HL         evt_data;
    900     tBTA_HL_STATUS  status = BTA_HL_STATUS_OK;
    901     UINT8           app_idx, mcl_idx;
    902     tBTA_HL_APP_CB  *p_acb;
    903     tBTA_HL_MCL_CB  *p_mcb;
    904     UNUSED(p_cb);
    905 
    906     if (bta_hl_find_mcl_idx_using_handle(p_data->api_cch_close.mcl_handle, &app_idx,  &mcl_idx))
    907     {
    908         p_mcb = BTA_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
    909         p_mcb->cch_oper = BTA_HL_CCH_OP_LOCAL_CLOSE;
    910     }
    911     else
    912     {
    913         status = BTA_HL_STATUS_INVALID_MCL_HANDLE;
    914     }
    915 #if BTA_HL_DEBUG == TRUE
    916     if (status != BTA_HL_STATUS_OK)
    917     {
    918         APPL_TRACE_DEBUG("bta_hl_api_cch_close status =%s", bta_hl_status_code(status));
    919     }
    920 #endif
    921     switch (status)
    922     {
    923         case BTA_HL_STATUS_OK:
    924             bta_hl_check_cch_close(app_idx, mcl_idx, p_data, TRUE);
    925             break;
    926 
    927         case BTA_HL_STATUS_INVALID_MCL_HANDLE:
    928             p_acb = BTA_HL_GET_APP_CB_PTR(app_idx);
    929             if (p_acb->p_cback)
    930             {
    931                 bta_hl_build_cch_close_cfm(&evt_data,
    932                                        p_acb->app_handle,
    933                                        p_data->api_cch_close.mcl_handle,
    934                                        status);
    935                 p_acb->p_cback(BTA_HL_CCH_CLOSE_CFM_EVT,(tBTA_HL *) &evt_data );
    936             }
    937             else
    938             {
    939                 APPL_TRACE_ERROR("bta_hl_api_cch_close Null Callback");
    940             }
    941             break;
    942 
    943         default:
    944             APPL_TRACE_ERROR("status code=%d", status);
    945             break;
    946 
    947     }
    948 
    949 }
    950 
    951 /*******************************************************************************
    952 **
    953 ** Function         bta_hl_api_dch_open
    954 **
    955 ** Description      Process the API DCH open request
    956 **
    957 ** Returns          void
    958 **
    959 *******************************************************************************/
    960 static void bta_hl_api_dch_open(tBTA_HL_CB *p_cb, tBTA_HL_DATA *p_data)
    961 {
    962     tBTA_HL                     evt_data;
    963     tBTA_HL_STATUS              status = BTA_HL_STATUS_OK;
    964     UINT8                       app_idx, mcl_idx, mdl_idx;
    965     tBTA_HL_APP_CB              *p_acb;
    966     tBTA_HL_MCL_CB              *p_mcb = NULL;
    967     tBTA_HL_MDL_CB              *p_dcb;
    968     tBTA_HL_MDEP_CFG            *p_mdep_cfg;
    969     UINT8                       mdep_cfg_idx;
    970     UNUSED(p_cb);
    971 
    972     if (bta_hl_find_mcl_idx_using_handle(p_data->api_dch_open.mcl_handle, &app_idx, &mcl_idx))
    973     {
    974         p_acb = BTA_HL_GET_APP_CB_PTR(app_idx);
    975         p_mcb = BTA_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
    976 
    977         APPL_TRACE_DEBUG("bta_hl_api_dch_open: app_ix=%d, mcl_idx=%d, cch_state=%d, mcl_handle=%d",app_idx,mcl_idx,p_mcb->cch_state,p_data->api_dch_open.mcl_handle);
    978         if (p_mcb->cch_state == BTA_HL_CCH_OPEN_ST)
    979         {
    980             if (bta_hl_find_avail_mdl_idx(app_idx, mcl_idx, &mdl_idx))
    981             {
    982                 p_dcb = BTA_HL_GET_MDL_CB_PTR(app_idx, mcl_idx, mdl_idx);
    983 
    984                 if (bta_hl_find_mdep_cfg_idx(app_idx, p_data->api_dch_open.local_mdep_id, &mdep_cfg_idx))
    985                 {
    986                     if ( mdep_cfg_idx &&
    987                          (p_acb->sup_feature.mdep[mdep_cfg_idx].mdep_cfg.mdep_role == BTA_HL_MDEP_ROLE_SINK))
    988                     {
    989                         p_data->api_dch_open.local_cfg = BTA_HL_DCH_CFG_NO_PREF;
    990                     }
    991 
    992                     if ((status = bta_hl_chk_local_cfg(app_idx,mcl_idx,mdep_cfg_idx,p_data->api_dch_open.local_cfg))
    993                         == BTA_HL_STATUS_OK)
    994                     {
    995 
    996                         if (p_data->api_dch_open.local_mdep_id != BTA_HL_ECHO_TEST_MDEP_ID)
    997                         {
    998                             if (bta_hl_set_ctrl_psm_for_dch(app_idx, mcl_idx, mdl_idx, p_data->api_dch_open.ctrl_psm))
    999                             {
   1000                                 p_mdep_cfg = BTA_HL_GET_MDEP_CFG_PTR(app_idx, mdep_cfg_idx);
   1001                                 p_dcb->in_use                   = TRUE;
   1002                                 p_dcb->dch_oper                 = BTA_HL_DCH_OP_LOCAL_OPEN;
   1003                                 p_dcb->sec_mask                 = p_data->api_dch_open.sec_mask;
   1004                                 p_dcb->local_mdep_id            = p_data->api_dch_open.local_mdep_id;
   1005                                 p_dcb->peer_mdep_id             = p_data->api_dch_open.peer_mdep_id;
   1006 
   1007                                 if (p_mdep_cfg->mdep_role == BTA_HL_MDEP_ROLE_SINK)
   1008                                 {
   1009                                     p_dcb->peer_mdep_role = BTA_HL_MDEP_ROLE_SOURCE;
   1010                                 }
   1011                                 else
   1012                                 {
   1013                                     p_dcb->peer_mdep_role = BTA_HL_MDEP_ROLE_SINK;
   1014                                 }
   1015 
   1016                                 p_dcb->local_mdep_cfg_idx   = mdep_cfg_idx;
   1017                                 p_dcb->local_cfg            = p_data->api_dch_open.local_cfg;
   1018 
   1019                                 bta_hl_find_rxtx_apdu_size(app_idx, mdep_cfg_idx,
   1020                                                            &p_dcb->max_rx_apdu_size,
   1021                                                            &p_dcb->max_tx_apdu_size);
   1022                                 p_dcb->mdl_id               = bta_hl_allocate_mdl_id(app_idx,mcl_idx,mdl_idx);
   1023                                 p_dcb->mdl_cfg_idx_included = FALSE;
   1024                             }
   1025                             else
   1026                             {
   1027                                 status =  BTA_HL_STATUS_INVALID_CTRL_PSM;
   1028                             }
   1029 
   1030                         }
   1031                         else
   1032                         {
   1033                             status =  BTA_HL_STATUS_INVALID_LOCAL_MDEP_ID;
   1034                         }
   1035                     }
   1036                 }
   1037                 else
   1038                 {
   1039                     status =  BTA_HL_STATUS_INVALID_LOCAL_MDEP_ID;
   1040                 }
   1041             }
   1042             else
   1043             {
   1044                 status = BTA_HL_STATUS_NO_RESOURCE;
   1045             }
   1046         }
   1047         else
   1048         {
   1049             status =  BTA_HL_STATUS_NO_CCH;
   1050         }
   1051     }
   1052     else
   1053     {
   1054         status = BTA_HL_STATUS_INVALID_MCL_HANDLE;
   1055     }
   1056 
   1057 #if BTA_HL_DEBUG == TRUE
   1058     if (status != BTA_HL_STATUS_OK)
   1059     {
   1060         APPL_TRACE_DEBUG("bta_hl_api_dch_open status =%s", bta_hl_status_code(status));
   1061     }
   1062 #endif
   1063     switch (status)
   1064     {
   1065         case BTA_HL_STATUS_OK:
   1066             if (p_mcb->sdp.num_recs)
   1067             {
   1068                 bta_hl_dch_sm_execute(app_idx, mcl_idx, mdl_idx, BTA_HL_DCH_OPEN_EVT, p_data);
   1069             }
   1070             else
   1071             {
   1072                 bta_hl_dch_sm_execute(app_idx, mcl_idx, mdl_idx, BTA_HL_DCH_SDP_INIT_EVT, p_data);
   1073             }
   1074             break;
   1075         case BTA_HL_STATUS_INVALID_DCH_CFG:
   1076         case BTA_HL_STATUS_NO_FIRST_RELIABLE:
   1077         case BTA_HL_STATUS_NO_CCH:
   1078         case BTA_HL_STATUS_NO_RESOURCE:
   1079         case BTA_HL_STATUS_FAIL:
   1080         case BTA_HL_STATUS_INVALID_LOCAL_MDEP_ID:
   1081         case BTA_HL_STATUS_INVALID_CTRL_PSM:
   1082             p_acb = BTA_HL_GET_APP_CB_PTR(app_idx);
   1083             if (p_acb->p_cback)
   1084             {
   1085                 bta_hl_build_dch_open_cfm(&evt_data,
   1086                                           p_acb->app_handle,
   1087                                           p_data->api_dch_open.mcl_handle,
   1088                                           BTA_HL_INVALID_MDL_HANDLE,
   1089                                           0,0,0,0,0, status);
   1090                 p_acb->p_cback(BTA_HL_DCH_OPEN_CFM_EVT,(tBTA_HL *) &evt_data );
   1091             }
   1092             else
   1093             {
   1094                 APPL_TRACE_ERROR("bta_hl_api_dch_open Null Callback");
   1095             }
   1096 
   1097             break;
   1098         default:
   1099             APPL_TRACE_ERROR("Status code=%d", status);
   1100             break;
   1101 
   1102     }
   1103 
   1104 }
   1105 /*******************************************************************************
   1106 **
   1107 ** Function         bta_hl_api_dch_close
   1108 **
   1109 ** Description      Process the API DCH close request
   1110 **
   1111 ** Returns          void
   1112 **
   1113 *******************************************************************************/
   1114 static void bta_hl_api_dch_close(tBTA_HL_CB *p_cb, tBTA_HL_DATA *p_data)
   1115 {
   1116     tBTA_HL         evt_data;
   1117     tBTA_HL_STATUS  status = BTA_HL_STATUS_OK;
   1118     UINT8           app_idx, mcl_idx, mdl_idx;
   1119     tBTA_HL_APP_CB  *p_acb;
   1120     tBTA_HL_MCL_CB  *p_mcb;
   1121     tBTA_HL_MDL_CB  *p_dcb;
   1122     UNUSED(p_cb);
   1123 
   1124     if (bta_hl_find_mdl_idx_using_handle(p_data->api_dch_close.mdl_handle, &app_idx, &mcl_idx, &mdl_idx ))
   1125     {
   1126         p_dcb = BTA_HL_GET_MDL_CB_PTR(app_idx, mcl_idx, mdl_idx);
   1127         if (p_dcb->dch_state != BTA_HL_DCH_OPEN_ST)
   1128         {
   1129             status =  BTA_HL_STATUS_FAIL;
   1130         }
   1131     }
   1132     else
   1133     {
   1134         status = BTA_HL_STATUS_INVALID_MDL_HANDLE;
   1135     }
   1136 
   1137 #if BTA_HL_DEBUG == TRUE
   1138     if (status != BTA_HL_STATUS_OK)
   1139     {
   1140         APPL_TRACE_DEBUG("bta_hl_api_dch_close status =%s", bta_hl_status_code(status));
   1141     }
   1142 #endif
   1143 
   1144     switch (status)
   1145     {
   1146         case BTA_HL_STATUS_OK:
   1147             bta_hl_dch_sm_execute(app_idx, mcl_idx, mdl_idx, BTA_HL_DCH_CLOSE_EVT, p_data);
   1148             break;
   1149         case BTA_HL_STATUS_FAIL:
   1150             p_acb = BTA_HL_GET_APP_CB_PTR(app_idx);
   1151             if (p_acb->p_cback)
   1152             {
   1153                 p_mcb = BTA_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
   1154                 bta_hl_build_dch_close_cfm(&evt_data,
   1155                                            p_acb->app_handle,
   1156                                            p_mcb->mcl_handle,
   1157                                            p_data->api_dch_close.mdl_handle,
   1158                                            status);
   1159 
   1160                 p_acb->p_cback(BTA_HL_DCH_CLOSE_CFM_EVT,(tBTA_HL *) &evt_data );
   1161             }
   1162             else
   1163             {
   1164                 APPL_TRACE_ERROR("bta_hl_api_dch_close Null Callback");
   1165             }
   1166             break;
   1167         default:
   1168             APPL_TRACE_ERROR("Status code=%d", status);
   1169             break;
   1170     }
   1171 }
   1172 
   1173 
   1174 /*******************************************************************************
   1175 **
   1176 ** Function         bta_hl_api_dch_reconnect
   1177 **
   1178 ** Description      Process the API DCH reconnect request
   1179 **
   1180 ** Returns          void
   1181 **
   1182 *******************************************************************************/
   1183 static void bta_hl_api_dch_reconnect(tBTA_HL_CB *p_cb, tBTA_HL_DATA *p_data)
   1184 {
   1185     tBTA_HL         evt_data;
   1186     tBTA_HL_STATUS  status = BTA_HL_STATUS_OK;
   1187     UINT8           app_idx, mcl_idx, mdl_idx;
   1188     tBTA_HL_APP_CB  *p_acb;
   1189     tBTA_HL_MCL_CB  *p_mcb = NULL;
   1190     tBTA_HL_MDL_CB  *p_dcb;
   1191     UINT8           mdep_cfg_idx;
   1192     UINT8           mdl_cfg_idx;
   1193     tBTA_HL_MDEP_CFG            *p_mdep_cfg;
   1194     UNUSED(p_cb);
   1195 
   1196     if (bta_hl_find_mcl_idx_using_handle(p_data->api_dch_reconnect.mcl_handle, &app_idx, &mcl_idx))
   1197     {
   1198         p_acb = BTA_HL_GET_APP_CB_PTR(app_idx);
   1199         p_mcb = BTA_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
   1200         if (p_mcb->cch_state == BTA_HL_CCH_OPEN_ST)
   1201         {
   1202             if (bta_hl_find_avail_mdl_idx(app_idx, mcl_idx, &mdl_idx))
   1203             {
   1204                 p_dcb = BTA_HL_GET_MDL_CB_PTR(app_idx, mcl_idx, mdl_idx);
   1205                 if (bta_hl_validate_reconnect_params(app_idx, mcl_idx,  &(p_data->api_dch_reconnect),
   1206                                                      &mdep_cfg_idx, &mdl_cfg_idx ))
   1207                 {
   1208                     if (!bta_hl_is_the_first_reliable_existed(app_idx, mcl_idx) &&
   1209                         (p_acb->mdl_cfg[mdl_cfg_idx].dch_mode != BTA_HL_DCH_MODE_RELIABLE))
   1210                     {
   1211                         status =  BTA_HL_STATUS_NO_FIRST_RELIABLE;
   1212                     }
   1213                     else
   1214                     {
   1215                         if (bta_hl_set_ctrl_psm_for_dch(app_idx, mcl_idx, mdl_idx, p_data->api_dch_open.ctrl_psm))
   1216                         {
   1217                             p_dcb->in_use                   = TRUE;
   1218                             p_dcb->dch_oper                 = BTA_HL_DCH_OP_LOCAL_RECONNECT;
   1219                             p_dcb->sec_mask                 = (BTA_SEC_AUTHENTICATE | BTA_SEC_ENCRYPT);
   1220                             p_dcb->local_mdep_id            = p_acb->mdl_cfg[mdl_cfg_idx].local_mdep_id;
   1221                             p_dcb->local_mdep_cfg_idx       = mdep_cfg_idx;
   1222                             p_dcb->local_cfg                = BTA_HL_DCH_CFG_UNKNOWN;
   1223                             p_dcb->mdl_id                   = p_data->api_dch_reconnect.mdl_id;
   1224                             p_dcb->mdl_cfg_idx_included     = TRUE;
   1225                             p_dcb->mdl_cfg_idx              = mdl_cfg_idx;
   1226                             p_dcb->dch_mode                 = p_acb->mdl_cfg[mdl_cfg_idx].dch_mode;
   1227 
   1228                            p_mdep_cfg = BTA_HL_GET_MDEP_CFG_PTR(app_idx, mdep_cfg_idx);
   1229 
   1230                             if (p_mdep_cfg->mdep_role == BTA_HL_MDEP_ROLE_SINK)
   1231                             {
   1232                                 p_dcb->peer_mdep_role = BTA_HL_MDEP_ROLE_SOURCE;
   1233                                   APPL_TRACE_DEBUG("peer mdep role = SOURCE ");
   1234 							}
   1235                             else
   1236                             {
   1237                                 p_dcb->peer_mdep_role = BTA_HL_MDEP_ROLE_SINK;
   1238                                 APPL_TRACE_DEBUG("peer mdep role = SINK ");
   1239                             }
   1240 
   1241                             bta_hl_find_rxtx_apdu_size(app_idx, mdep_cfg_idx,
   1242                                                        &p_dcb->max_rx_apdu_size,
   1243                                                        &p_dcb->max_tx_apdu_size);
   1244                         }
   1245                         else
   1246                         {
   1247                             status =  BTA_HL_STATUS_INVALID_CTRL_PSM;
   1248                         }
   1249                     }
   1250                 }
   1251                 else
   1252                 {
   1253                     status =  BTA_HL_STATUS_INVALID_RECONNECT_CFG;
   1254                 }
   1255             }
   1256             else
   1257             {
   1258                 status = BTA_HL_STATUS_NO_RESOURCE;
   1259             }
   1260         }
   1261         else
   1262         {
   1263             status =  BTA_HL_STATUS_NO_CCH;
   1264         }
   1265     }
   1266     else
   1267     {
   1268         status = BTA_HL_STATUS_INVALID_MCL_HANDLE;
   1269     }
   1270 
   1271 #if BTA_HL_DEBUG == TRUE
   1272     if (status != BTA_HL_STATUS_OK)
   1273     {
   1274         APPL_TRACE_DEBUG("bta_hl_api_dch_reconnect status=%s", bta_hl_status_code(status));
   1275     }
   1276 #endif
   1277 
   1278     switch (status)
   1279     {
   1280         case BTA_HL_STATUS_OK:
   1281             if (p_mcb->sdp.num_recs)
   1282             {
   1283                 bta_hl_dch_sm_execute(app_idx, mcl_idx, mdl_idx, BTA_HL_DCH_RECONNECT_EVT, p_data);
   1284             }
   1285             else
   1286             {
   1287                 bta_hl_dch_sm_execute(app_idx, mcl_idx, mdl_idx, BTA_HL_DCH_SDP_INIT_EVT, p_data);
   1288             }
   1289             break;
   1290         case BTA_HL_STATUS_INVALID_RECONNECT_CFG:
   1291         case BTA_HL_STATUS_NO_FIRST_RELIABLE:
   1292         case BTA_HL_STATUS_NO_CCH:
   1293         case BTA_HL_STATUS_NO_RESOURCE:
   1294             p_acb = BTA_HL_GET_APP_CB_PTR(app_idx);
   1295             if (p_acb->p_cback)
   1296             {
   1297                 bta_hl_build_dch_open_cfm(&evt_data,
   1298                                           p_acb->app_handle,
   1299                                           p_data->api_dch_reconnect.mcl_handle,
   1300                                           BTA_HL_INVALID_MDL_HANDLE,
   1301                                           0,p_data->api_dch_reconnect.mdl_id,0,0,0, status);
   1302                 p_acb->p_cback(BTA_HL_DCH_RECONNECT_CFM_EVT,(tBTA_HL *) &evt_data );
   1303             }
   1304             else
   1305             {
   1306                 APPL_TRACE_ERROR("bta_hl_api_dch_reconnect Null Callback");
   1307             }
   1308             break;
   1309         default:
   1310             APPL_TRACE_ERROR("Status code=%d", status);
   1311             break;
   1312     }
   1313 }
   1314 
   1315 /*******************************************************************************
   1316 **
   1317 ** Function         bta_hl_api_dch_echo_test
   1318 **
   1319 ** Description      Process the API Echo test request
   1320 **
   1321 ** Returns          void
   1322 **
   1323 *******************************************************************************/
   1324 static void bta_hl_api_dch_echo_test(tBTA_HL_CB *p_cb, tBTA_HL_DATA *p_data)
   1325 {
   1326     tBTA_HL             evt_data;
   1327     tBTA_HL_STATUS      status = BTA_HL_STATUS_OK;
   1328     UINT8               app_idx, mcl_idx, mdl_idx;
   1329     tBTA_HL_APP_CB      *p_acb;
   1330     tBTA_HL_MCL_CB      *p_mcb = NULL;
   1331     tBTA_HL_MDL_CB      *p_dcb;
   1332     tBTA_HL_ECHO_CFG    *p_echo_cfg;
   1333     UNUSED(p_cb);
   1334 
   1335     if (bta_hl_find_mcl_idx_using_handle(p_data->api_dch_echo_test.mcl_handle, &app_idx,  &mcl_idx))
   1336     {
   1337         p_acb = BTA_HL_GET_APP_CB_PTR(app_idx);
   1338         p_mcb = BTA_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
   1339         if (p_mcb->cch_state == BTA_HL_CCH_OPEN_ST)
   1340         {
   1341             if (!p_mcb->echo_test )
   1342             {
   1343                 if (bta_hl_find_avail_mdl_idx(app_idx, mcl_idx, &mdl_idx))
   1344                 {
   1345                     p_dcb = BTA_HL_GET_MDL_CB_PTR(app_idx, mcl_idx, mdl_idx);
   1346 
   1347                     if ((p_data->api_dch_echo_test.local_cfg == BTA_HL_DCH_CFG_RELIABLE) ||
   1348                         (p_data->api_dch_echo_test.local_cfg == BTA_HL_DCH_CFG_STREAMING))
   1349                     {
   1350                         if ((p_dcb->p_echo_tx_pkt = bta_hl_get_buf(p_data->api_dch_echo_test.pkt_size)) != NULL )
   1351                         {
   1352                             if (bta_hl_set_ctrl_psm_for_dch(app_idx, mcl_idx, mdl_idx, p_data->api_dch_open.ctrl_psm))
   1353                             {
   1354                                 p_dcb->in_use                   = TRUE;
   1355                                 p_dcb->dch_oper                 = BTA_HL_DCH_OP_LOCAL_OPEN;
   1356                                 p_dcb->sec_mask                 = (BTA_SEC_AUTHENTICATE | BTA_SEC_ENCRYPT);
   1357                                 p_dcb->local_mdep_cfg_idx       = BTA_HL_ECHO_TEST_MDEP_CFG_IDX;
   1358                                 p_dcb->local_cfg                = p_data->api_dch_echo_test.local_cfg;
   1359                                 p_dcb->local_mdep_id            = BTA_HL_ECHO_TEST_MDEP_ID;
   1360                                 p_dcb->peer_mdep_id             = BTA_HL_ECHO_TEST_MDEP_ID;
   1361                                 p_dcb->mdl_id                   = bta_hl_allocate_mdl_id(app_idx,mcl_idx,mdl_idx);
   1362                                 p_dcb->mdl_cfg_idx_included     = FALSE;
   1363                                 p_echo_cfg                      = BTA_HL_GET_ECHO_CFG_PTR(app_idx);
   1364                                 p_dcb->max_rx_apdu_size         = p_echo_cfg->max_rx_apdu_size;
   1365                                 p_dcb->max_tx_apdu_size         = p_echo_cfg->max_tx_apdu_size;
   1366                                 p_mcb->echo_test                = TRUE;
   1367                                 p_mcb->echo_mdl_idx             = mdl_idx;
   1368                             }
   1369                             else
   1370                             {
   1371                                 status =  BTA_HL_STATUS_INVALID_CTRL_PSM;
   1372                             }
   1373                         }
   1374                         else
   1375                         {
   1376                             status = BTA_HL_STATUS_NO_RESOURCE;
   1377                         }
   1378                     }
   1379                     else
   1380                     {
   1381                         status = BTA_HL_STATUS_INVALID_DCH_CFG;
   1382                     }
   1383                 }
   1384                 else
   1385                 {
   1386                     status = BTA_HL_STATUS_NO_RESOURCE;
   1387                 }
   1388             }
   1389             else
   1390             {
   1391                 status = BTA_HL_STATUS_ECHO_TEST_BUSY;
   1392             }
   1393         }
   1394         else
   1395         {
   1396             status =  BTA_HL_STATUS_NO_CCH;
   1397         }
   1398     }
   1399     else
   1400     {
   1401         status = BTA_HL_STATUS_NO_MCL;
   1402     }
   1403 
   1404 #if BTA_HL_DEBUG == TRUE
   1405     if (status != BTA_HL_STATUS_OK)
   1406     {
   1407         APPL_TRACE_DEBUG("bta_hl_api_dch_echo_test status=%s", bta_hl_status_code(status));
   1408     }
   1409 #endif
   1410 
   1411     switch (status)
   1412     {
   1413         case BTA_HL_STATUS_OK:
   1414             if (p_mcb->sdp.num_recs)
   1415             {
   1416                 bta_hl_dch_sm_execute(app_idx, mcl_idx, mdl_idx, BTA_HL_DCH_ECHO_TEST_EVT, p_data);
   1417             }
   1418             else
   1419             {
   1420                 bta_hl_dch_sm_execute(app_idx, mcl_idx, mdl_idx, BTA_HL_DCH_SDP_INIT_EVT, p_data);
   1421             }
   1422             break;
   1423         case BTA_HL_STATUS_ECHO_TEST_BUSY:
   1424         case BTA_HL_STATUS_NO_RESOURCE:
   1425         case BTA_HL_STATUS_INVALID_DCH_CFG:
   1426             p_acb = BTA_HL_GET_APP_CB_PTR(app_idx);
   1427             if (p_acb->p_cback)
   1428             {
   1429                 bta_hl_build_echo_test_cfm(&evt_data,
   1430                                            p_acb->app_handle,
   1431                                            p_mcb->mcl_handle,
   1432                                            status);
   1433                 p_acb->p_cback(BTA_HL_DCH_ECHO_TEST_CFM_EVT,(tBTA_HL *) &evt_data );
   1434             }
   1435             else
   1436             {
   1437                 APPL_TRACE_ERROR("bta_hl_api_dch_echo_test Null Callback");
   1438             }
   1439             break;
   1440 
   1441         default:
   1442             APPL_TRACE_ERROR("Status code=%s", status);
   1443             break;
   1444     }
   1445 }
   1446 
   1447 
   1448 /*******************************************************************************
   1449 **
   1450 ** Function         bta_hl_api_sdp_query
   1451 **
   1452 ** Description      Process the API SDP query request
   1453 **
   1454 ** Returns          void
   1455 **
   1456 *******************************************************************************/
   1457 static void bta_hl_api_sdp_query(tBTA_HL_CB *p_cb, tBTA_HL_DATA *p_data)
   1458 {
   1459     tBTA_HL         evt_data;
   1460     tBTA_HL_STATUS  status = BTA_HL_STATUS_OK;
   1461     UINT8           app_idx, mcl_idx;
   1462     tBTA_HL_APP_CB  *p_acb;
   1463     tBTA_HL_MCL_CB  *p_mcb;
   1464     UNUSED(p_cb);
   1465 
   1466     if (bta_hl_find_app_idx_using_handle(p_data->api_sdp_query.app_handle, &app_idx))
   1467     {
   1468         if (!bta_hl_find_mcl_idx(app_idx, p_data->api_sdp_query.bd_addr, &mcl_idx))
   1469         {
   1470             if (bta_hl_find_avail_mcl_idx(app_idx, &mcl_idx))
   1471             {
   1472                 p_mcb = BTA_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
   1473                 p_mcb->in_use = TRUE;
   1474                 bdcpy(p_mcb->bd_addr, p_data->api_sdp_query.bd_addr);
   1475                 APPL_TRACE_DEBUG("bta_hl_api_sdp_query p_mcb->app_id %d app_idx %d mcl_idx %d", p_mcb->app_id, app_idx, mcl_idx);
   1476                 p_mcb->app_id = p_data->api_sdp_query.app_id;
   1477                 p_mcb->sdp_oper  = BTA_HL_SDP_OP_SDP_QUERY_NEW ;
   1478             }
   1479             else
   1480             {
   1481                 status = BTA_HL_STATUS_NO_RESOURCE;
   1482             }
   1483         }
   1484         else
   1485         {
   1486             p_mcb = BTA_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
   1487             p_mcb->app_id = p_data->api_sdp_query.app_id;
   1488             if (p_mcb->sdp_oper != BTA_HL_SDP_OP_NONE)
   1489             {
   1490                 status = BTA_HL_STATUS_SDP_NO_RESOURCE;
   1491             }
   1492             else
   1493             {
   1494                 p_mcb->sdp_oper  = BTA_HL_SDP_OP_SDP_QUERY_CURRENT;
   1495             }
   1496         }
   1497     }
   1498     else
   1499     {
   1500         status = BTA_HL_STATUS_INVALID_APP_HANDLE;
   1501     }
   1502 
   1503     if (status == BTA_HL_STATUS_OK)
   1504     {
   1505         status = bta_hl_init_sdp( p_mcb->sdp_oper, app_idx, mcl_idx, 0xFF);
   1506         if ( (status != BTA_HL_STATUS_OK) &&
   1507              (p_mcb->sdp_oper == BTA_HL_SDP_OP_SDP_QUERY_NEW) )
   1508         {
   1509             memset(p_mcb, 0 ,sizeof(tBTA_HL_MCL_CB));
   1510         }
   1511     }
   1512 #if BTA_HL_DEBUG == TRUE
   1513     if (status != BTA_HL_STATUS_OK)
   1514     {
   1515         APPL_TRACE_DEBUG("bta_hl_api_sdp_query status=%s", bta_hl_status_code(status));
   1516     }
   1517 #endif
   1518     switch (status)
   1519     {
   1520         case BTA_HL_STATUS_NO_RESOURCE:
   1521         case BTA_HL_STATUS_FAIL:
   1522         case BTA_HL_STATUS_SDP_NO_RESOURCE:
   1523             p_acb = BTA_HL_GET_APP_CB_PTR(app_idx);
   1524             if (p_acb->p_cback)
   1525             {
   1526                 bta_hl_build_sdp_query_cfm(&evt_data,
   1527                                            p_data->api_sdp_query.app_id,
   1528                                            p_data->api_sdp_query.app_handle,
   1529                                            p_data->api_sdp_query.bd_addr,
   1530                                            NULL,
   1531                                            status);
   1532                 p_acb->p_cback(BTA_HL_SDP_QUERY_CFM_EVT,(tBTA_HL *) &evt_data );
   1533             }
   1534             else
   1535             {
   1536                 APPL_TRACE_ERROR("bta_hl_api_sdp_query Null Callback");
   1537             }
   1538             break;
   1539         case BTA_HL_STATUS_OK:
   1540             break;
   1541         default:
   1542             APPL_TRACE_ERROR("Status code=%d", status);
   1543             break;
   1544     }
   1545 }
   1546 
   1547 
   1548 
   1549 
   1550 /*******************************************************************************
   1551 **
   1552 ** Function         bta_hl_sdp_query_results
   1553 **
   1554 ** Description      Process the SDP query results
   1555 **
   1556 ** Returns          void
   1557 **
   1558 *******************************************************************************/
   1559 static void bta_hl_sdp_query_results(tBTA_HL_CB *p_cb, tBTA_HL_DATA *p_data)
   1560 {
   1561     tBTA_HL             evt_data;
   1562     tBTA_HL_STATUS      status = BTA_HL_STATUS_OK;
   1563     UINT8               app_idx = p_data->cch_sdp.app_idx;
   1564     UINT8               mcl_idx = p_data->cch_sdp.mcl_idx;
   1565     tBTA_HL_APP_CB      *p_acb = BTA_HL_GET_APP_CB_PTR( app_idx);
   1566     tBTA_HL_MCL_CB      *p_mcb =  BTA_HL_GET_MCL_CB_PTR( app_idx,  mcl_idx);
   1567     tBTA_HL_SDP         *p_sdp=NULL;
   1568     tBTA_HL_SDP_OPER    sdp_oper;
   1569     UINT16              event;
   1570     BOOLEAN             release_sdp_buf=FALSE;
   1571     UNUSED(p_cb);
   1572 
   1573     event = p_data->hdr.event;
   1574     sdp_oper = p_mcb->sdp_oper;
   1575 
   1576     if ( event == BTA_HL_SDP_QUERY_OK_EVT)
   1577     {
   1578         if ((p_sdp = (tBTA_HL_SDP *)GKI_getbuf((UINT16)(sizeof(tBTA_HL_SDP)))) != NULL)
   1579         {
   1580             memcpy(p_sdp, &p_mcb->sdp, sizeof(tBTA_HL_SDP));
   1581             release_sdp_buf = TRUE;
   1582         }
   1583         else
   1584         {
   1585             status = BTA_HL_STATUS_SDP_NO_RESOURCE;
   1586         }
   1587     }
   1588     else
   1589     {
   1590         status = BTA_HL_STATUS_SDP_FAIL;
   1591     }
   1592 
   1593 #if BTA_HL_DEBUG == TRUE
   1594     if (status != BTA_HL_STATUS_OK)
   1595     {
   1596         APPL_TRACE_DEBUG("bta_hl_sdp_query_results status=%s", bta_hl_status_code(status));
   1597     }
   1598 #endif
   1599 
   1600     APPL_TRACE_DEBUG("bta_hl_sdp_query_results p_mcb->app_id %d app_idx %d mcl_idx %d", p_mcb->app_id, app_idx, mcl_idx);
   1601     bta_hl_build_sdp_query_cfm(&evt_data,p_mcb->app_id, p_acb->app_handle,
   1602                                p_mcb->bd_addr,p_sdp,status);
   1603     p_acb->p_cback(BTA_HL_SDP_QUERY_CFM_EVT,(tBTA_HL *) &evt_data );
   1604 
   1605     if (release_sdp_buf)
   1606     {
   1607         utl_freebuf((void **) &p_sdp);
   1608     }
   1609 
   1610     if (p_data->cch_sdp.release_mcl_cb)
   1611     {
   1612         memset(p_mcb, 0 ,sizeof(tBTA_HL_MCL_CB));
   1613     }
   1614     else
   1615     {
   1616         if (p_mcb->close_pending)
   1617         {
   1618             bta_hl_check_cch_close(app_idx,mcl_idx,p_data, TRUE);
   1619         }
   1620 
   1621         if (!p_mcb->ctrl_psm)
   1622         {
   1623             /* this is a control channel acceptor do not store the sdp records*/
   1624             memset(&p_mcb->sdp, 0, sizeof(tBTA_HL_SDP));
   1625         }
   1626     }
   1627 }
   1628 
   1629 
   1630 /*******************************************************************************
   1631 **
   1632 ** Function         bta_hl_api_delete_mdl
   1633 **
   1634 ** Description      Process the API DELETE MDL request
   1635 **
   1636 ** Returns          void
   1637 **
   1638 *******************************************************************************/
   1639 static void bta_hl_api_delete_mdl(tBTA_HL_CB *p_cb, tBTA_HL_DATA *p_data)
   1640 {
   1641     tBTA_HL         evt_data;
   1642     tBTA_HL_STATUS  status = BTA_HL_STATUS_OK;
   1643     UINT8           app_idx, mcl_idx;
   1644     tBTA_HL_APP_CB  *p_acb;
   1645     tBTA_HL_MCL_CB  *p_mcb;
   1646     UNUSED(p_cb);
   1647 
   1648     if (bta_hl_find_mcl_idx_using_handle(p_data->api_delete_mdl.mcl_handle, &app_idx, &mcl_idx ))
   1649     {
   1650         if (bta_hl_is_mdl_value_valid(p_data->api_delete_mdl.mdl_id))
   1651         {
   1652             p_mcb = BTA_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
   1653             if (bta_hl_is_mdl_exsit_in_mcl(app_idx,
   1654                                            p_mcb->bd_addr,
   1655                                            p_data->api_delete_mdl.mdl_id))
   1656             {
   1657 
   1658 
   1659                 p_mcb->delete_mdl.mcl_handle =  p_data->api_delete_mdl.mcl_handle;
   1660                 p_mcb->delete_mdl.mdl_id = p_data->api_delete_mdl.mdl_id;
   1661                 p_mcb->delete_mdl.delete_req_pending = TRUE;
   1662 
   1663                 if (MCA_Delete((tMCA_CL) p_mcb->mcl_handle,
   1664                                p_data->api_delete_mdl.mdl_id)!= MCA_SUCCESS)
   1665                 {
   1666                     status = BTA_HL_STATUS_FAIL;
   1667                     memset(&p_mcb->delete_mdl, 0, sizeof(tBTA_HL_DELETE_MDL));
   1668                 }
   1669             }
   1670             else
   1671             {
   1672                 status = BTA_HL_STATUS_NO_MDL_ID_FOUND;
   1673             }
   1674         }
   1675         else
   1676         {
   1677             status = BTA_HL_STATUS_INVALID_MDL_ID;
   1678         }
   1679     }
   1680     else
   1681     {
   1682         status = BTA_HL_STATUS_INVALID_MCL_HANDLE;
   1683     }
   1684 
   1685 #if BTA_HL_DEBUG == TRUE
   1686     if (status != BTA_HL_STATUS_OK)
   1687     {
   1688         APPL_TRACE_DEBUG("bta_hl_api_delete_mdl status=%s", bta_hl_status_code(status));
   1689     }
   1690 #endif
   1691     switch (status)
   1692     {
   1693         case BTA_HL_STATUS_OK:
   1694             break;
   1695         case BTA_HL_STATUS_FAIL:
   1696         case BTA_HL_STATUS_NO_MDL_ID_FOUND:
   1697         case BTA_HL_STATUS_INVALID_MDL_ID:
   1698             p_acb = BTA_HL_GET_APP_CB_PTR(app_idx);
   1699             if (p_acb->p_cback)
   1700             {
   1701                 bta_hl_build_delete_mdl_cfm(&evt_data,
   1702                                             p_acb->app_handle,
   1703                                             p_data->api_delete_mdl.mcl_handle,
   1704                                             p_data->api_delete_mdl.mdl_id,
   1705                                             status);
   1706                 p_acb->p_cback(BTA_HL_DELETE_MDL_CFM_EVT,(tBTA_HL *) &evt_data );
   1707             }
   1708             else
   1709             {
   1710                 APPL_TRACE_ERROR("bta_hl_api_delete_mdl Null Callback");
   1711             }
   1712             break;
   1713         default:
   1714             APPL_TRACE_ERROR("status code =%d", status);
   1715             break;
   1716     }
   1717 }
   1718 
   1719 /*******************************************************************************
   1720 **
   1721 ** Function         bta_hl_mca_delete_mdl_cfm
   1722 **
   1723 ** Description      Process the DELETE MDL confirmation event
   1724 **
   1725 ** Returns          void
   1726 **
   1727 *******************************************************************************/
   1728 static void bta_hl_mca_delete_mdl_cfm(tBTA_HL_CB *p_cb, tBTA_HL_DATA *p_data)
   1729 {
   1730     tBTA_HL         evt_data;
   1731     tBTA_HL_STATUS  status = BTA_HL_STATUS_OK;
   1732     UINT8           app_idx, mcl_idx;
   1733     tMCA_RSP_EVT    *p_delete_cfm = &p_data->mca_evt.mca_data.delete_cfm;
   1734     tBTA_HL_MCL_CB  *p_mcb;
   1735     BOOLEAN         send_cfm_evt = TRUE;
   1736     tBTA_HL_APP_CB  *p_acb;
   1737     UNUSED(p_cb);
   1738 
   1739     if (bta_hl_find_mcl_idx_using_handle(p_data->mca_evt.mcl_handle, &app_idx, &mcl_idx))
   1740     {
   1741         p_mcb = BTA_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
   1742         if ( p_mcb->delete_mdl.delete_req_pending)
   1743         {
   1744             if (p_delete_cfm->rsp_code == MCA_RSP_SUCCESS   )
   1745             {
   1746 
   1747                 if (!bta_hl_delete_mdl_cfg(app_idx,
   1748                                            p_mcb->bd_addr ,
   1749                                            p_delete_cfm->mdl_id))
   1750                 {
   1751                     status = BTA_HL_STATUS_FAIL;
   1752                 }
   1753             }
   1754             else
   1755             {
   1756                 status = BTA_HL_STATUS_FAIL;
   1757             }
   1758 
   1759             memset(&p_mcb->delete_mdl, 0, sizeof(tBTA_HL_DELETE_MDL));
   1760         }
   1761         else
   1762         {
   1763             send_cfm_evt = FALSE;
   1764         }
   1765     }
   1766     else
   1767     {
   1768         send_cfm_evt = FALSE;
   1769     }
   1770 
   1771 #if BTA_HL_DEBUG == TRUE
   1772     if (status != BTA_HL_STATUS_OK)
   1773     {
   1774         APPL_TRACE_DEBUG("bta_hl_api_delete_mdl status=%s", bta_hl_status_code(status));
   1775     }
   1776 #endif
   1777 
   1778     if (send_cfm_evt)
   1779     {
   1780         p_acb = BTA_HL_GET_APP_CB_PTR(app_idx);
   1781         if (p_acb->p_cback)
   1782         {
   1783             bta_hl_build_delete_mdl_cfm(&evt_data,
   1784                                         p_acb->app_handle,
   1785                                         p_mcb->mcl_handle,
   1786                                         p_delete_cfm->mdl_id,
   1787                                         status);
   1788 
   1789             p_acb->p_cback(BTA_HL_DELETE_MDL_CFM_EVT,(tBTA_HL *) &evt_data );
   1790         }
   1791         else
   1792         {
   1793             APPL_TRACE_ERROR("bta_hl_mca_delete_mdl_cfm Null Callback");
   1794 
   1795         }
   1796     }
   1797 }
   1798 
   1799 /*******************************************************************************
   1800 **
   1801 ** Function         bta_hl_mca_delete_mdl_ind
   1802 **
   1803 ** Description      Process the DELETE MDL indication event
   1804 **
   1805 ** Returns          void
   1806 **
   1807 *******************************************************************************/
   1808 static void bta_hl_mca_delete_mdl_ind(tBTA_HL_CB *p_cb, tBTA_HL_DATA *p_data)
   1809 {
   1810     tBTA_HL         evt_data;
   1811     UINT8           app_idx, mcl_idx, mdl_idx;
   1812     tMCA_EVT_HDR    *p_delete_ind = &p_data->mca_evt.mca_data.delete_ind;
   1813     tBTA_HL_MCL_CB  *p_mcb;
   1814     tBTA_HL_MDL_CB  *p_dcb;
   1815     BOOLEAN         send_ind_evt = FALSE;
   1816     tBTA_HL_APP_CB  *p_acb;
   1817     UNUSED(p_cb);
   1818 
   1819     if (bta_hl_find_mcl_idx_using_handle(p_data->mca_evt.mcl_handle, &app_idx, &mcl_idx) )
   1820     {
   1821         p_mcb = BTA_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
   1822 
   1823         if (bta_hl_find_mdl_idx(app_idx, mcl_idx, p_delete_ind->mdl_id, &mdl_idx ))
   1824         {
   1825             p_dcb = BTA_HL_GET_MDL_CB_PTR(app_idx, mcl_idx, mdl_idx);
   1826             p_dcb->dch_oper = BTA_HL_DCH_OP_REMOTE_DELETE;
   1827         }
   1828         if (bta_hl_delete_mdl_cfg(app_idx,
   1829                                   p_mcb->bd_addr ,
   1830                                   p_delete_ind->mdl_id))
   1831         {
   1832             send_ind_evt = TRUE;
   1833         }
   1834     }
   1835 
   1836 #if BTA_HL_DEBUG == TRUE
   1837     if (!send_ind_evt)
   1838     {
   1839         APPL_TRACE_DEBUG("bta_hl_mca_delete_mdl_ind is_send_ind_evt =%d", send_ind_evt);
   1840     }
   1841 #endif
   1842 
   1843     if (send_ind_evt)
   1844     {
   1845         p_acb = BTA_HL_GET_APP_CB_PTR(app_idx);
   1846         if (p_acb->p_cback)
   1847         {
   1848             evt_data.delete_mdl_ind.mcl_handle = p_mcb->mcl_handle;
   1849             evt_data.delete_mdl_ind.app_handle = p_acb->app_handle;
   1850             evt_data.delete_mdl_ind.mdl_id = p_delete_ind->mdl_id;
   1851             p_acb->p_cback(BTA_HL_DELETE_MDL_IND_EVT,(tBTA_HL *) &evt_data );
   1852         }
   1853         else
   1854         {
   1855             APPL_TRACE_ERROR("bta_hl_mca_delete_mdl_ind Null Callback");
   1856         }
   1857     }
   1858 }
   1859 
   1860 
   1861 
   1862 /*******************************************************************************
   1863 **
   1864 ** Function         bta_hl_api_dch_abort
   1865 **
   1866 ** Description      Process the API DCH abort request
   1867 **
   1868 ** Returns          void
   1869 **
   1870 *******************************************************************************/
   1871 static void bta_hl_api_dch_abort(tBTA_HL_CB *p_cb, tBTA_HL_DATA *p_data)
   1872 {
   1873     tBTA_HL_STATUS  status = BTA_HL_STATUS_OK;
   1874     UINT8           app_idx, mcl_idx, mdl_idx;
   1875     tBTA_HL_APP_CB  *p_acb;
   1876     tBTA_HL_MCL_CB  *p_mcb;
   1877     tBTA_HL_MDL_CB  *p_dcb;
   1878     tBTA_HL         evt_data;
   1879     UNUSED(p_cb);
   1880 
   1881     if (bta_hl_find_mcl_idx_using_handle(p_data->api_dch_abort.mcl_handle, &app_idx, &mcl_idx ))
   1882     {
   1883 
   1884         if (!bta_hl_find_dch_setup_mdl_idx(app_idx, mcl_idx, &mdl_idx ))
   1885         {
   1886             status = BTA_HL_STATUS_NO_MDL_ID_FOUND;
   1887         }
   1888         else
   1889         {
   1890             p_dcb = BTA_HL_GET_MDL_CB_PTR(app_idx, mcl_idx, mdl_idx);
   1891             if (p_dcb->abort_oper)
   1892             {
   1893                 /* abort already in progress*/
   1894                 status = BTA_HL_STATUS_FAIL;
   1895             }
   1896             else
   1897             {
   1898                 p_dcb->abort_oper = BTA_HL_ABORT_LOCAL_MASK;
   1899             }
   1900         }
   1901     }
   1902     else
   1903     {
   1904         status = BTA_HL_STATUS_INVALID_MCL_HANDLE;
   1905     }
   1906 
   1907 
   1908 #if BTA_HL_DEBUG == TRUE
   1909     if (status != BTA_HL_STATUS_OK)
   1910     {
   1911         APPL_TRACE_DEBUG("bta_hl_api_dch_abort status=%s", bta_hl_status_code(status));
   1912     }
   1913 #endif
   1914     switch (status)
   1915     {
   1916         case BTA_HL_STATUS_OK:
   1917 
   1918             bta_hl_dch_sm_execute(app_idx, mcl_idx, mdl_idx, BTA_HL_DCH_ABORT_EVT, p_data);
   1919             break;
   1920         case BTA_HL_STATUS_NO_MDL_ID_FOUND:
   1921         case BTA_HL_STATUS_FAIL:
   1922 
   1923             p_acb = BTA_HL_GET_APP_CB_PTR(app_idx);
   1924             if (p_acb->p_cback)
   1925             {
   1926                 p_mcb = BTA_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
   1927                 bta_hl_build_abort_cfm(&evt_data,
   1928 
   1929 
   1930 
   1931                                        p_acb->app_handle,
   1932                                        p_mcb->mcl_handle,
   1933                                        BTA_HL_STATUS_FAIL);
   1934                 p_acb->p_cback(BTA_HL_DCH_ABORT_CFM_EVT,(tBTA_HL *) &evt_data);
   1935             }
   1936             else
   1937             {
   1938                 APPL_TRACE_ERROR("bta_hl_api_dch_abort Null Callback");
   1939             }
   1940             break;
   1941         default:
   1942             APPL_TRACE_ERROR("Status code=%d", status);
   1943             break;
   1944     }
   1945 }
   1946 
   1947 /*******************************************************************************
   1948 **
   1949 ** Function         bta_hl_hdl_event
   1950 **
   1951 ** Description      HL main event handling function.
   1952 **
   1953 ** Returns          void
   1954 **
   1955 *******************************************************************************/
   1956 BOOLEAN bta_hl_hdl_event(BT_HDR *p_msg)
   1957 {
   1958     UINT8 app_idx, mcl_idx, mdl_idx;
   1959     BOOLEAN success = TRUE;
   1960 
   1961 #if BTA_HL_DEBUG == TRUE
   1962     APPL_TRACE_DEBUG("BTA HL Event Handler: Event [%s]",
   1963                       bta_hl_evt_code(p_msg->event));
   1964 #endif
   1965 
   1966     switch (p_msg->event)
   1967     {
   1968         case BTA_HL_API_ENABLE_EVT:
   1969             bta_hl_api_enable(&bta_hl_cb, (tBTA_HL_DATA *) p_msg);
   1970             break;
   1971         case BTA_HL_API_DISABLE_EVT:
   1972             bta_hl_api_disable(&bta_hl_cb, (tBTA_HL_DATA *) p_msg);
   1973             break;
   1974         case BTA_HL_API_UPDATE_EVT:
   1975             bta_hl_api_update(&bta_hl_cb, (tBTA_HL_DATA *) p_msg);
   1976             break;
   1977         case BTA_HL_API_REGISTER_EVT:
   1978             bta_hl_api_register(&bta_hl_cb, (tBTA_HL_DATA *) p_msg);
   1979             break;
   1980         case BTA_HL_API_DEREGISTER_EVT:
   1981             bta_hl_api_deregister(&bta_hl_cb, (tBTA_HL_DATA *) p_msg);
   1982             break;
   1983         case BTA_HL_API_CCH_OPEN_EVT:
   1984             bta_hl_api_cch_open(&bta_hl_cb, (tBTA_HL_DATA *) p_msg);
   1985             break;
   1986         case BTA_HL_API_CCH_CLOSE_EVT:
   1987             bta_hl_api_cch_close(&bta_hl_cb, (tBTA_HL_DATA *) p_msg);
   1988             break;
   1989         case BTA_HL_API_DCH_OPEN_EVT:
   1990             bta_hl_api_dch_open(&bta_hl_cb, (tBTA_HL_DATA *) p_msg);
   1991             break;
   1992         case BTA_HL_API_DCH_CLOSE_EVT:
   1993             bta_hl_api_dch_close(&bta_hl_cb, (tBTA_HL_DATA *) p_msg);
   1994             break;
   1995         case BTA_HL_API_DELETE_MDL_EVT:
   1996             bta_hl_api_delete_mdl(&bta_hl_cb, (tBTA_HL_DATA *) p_msg);
   1997             break;
   1998         case BTA_HL_API_DCH_RECONNECT_EVT:
   1999             bta_hl_api_dch_reconnect(&bta_hl_cb, (tBTA_HL_DATA *) p_msg);
   2000             break;
   2001 
   2002         case BTA_HL_API_DCH_ECHO_TEST_EVT:
   2003             bta_hl_api_dch_echo_test(&bta_hl_cb, (tBTA_HL_DATA *) p_msg);
   2004             break;
   2005 
   2006         case BTA_HL_API_SDP_QUERY_EVT:
   2007             bta_hl_api_sdp_query(&bta_hl_cb, (tBTA_HL_DATA *) p_msg);
   2008             break;
   2009 
   2010         case BTA_HL_MCA_DELETE_CFM_EVT:
   2011             bta_hl_mca_delete_mdl_cfm(&bta_hl_cb, (tBTA_HL_DATA *) p_msg);
   2012             break;
   2013 
   2014         case BTA_HL_MCA_DELETE_IND_EVT:
   2015             bta_hl_mca_delete_mdl_ind(&bta_hl_cb, (tBTA_HL_DATA *) p_msg);
   2016             break;
   2017 
   2018         case BTA_HL_SDP_QUERY_OK_EVT:
   2019         case BTA_HL_SDP_QUERY_FAIL_EVT:
   2020             bta_hl_sdp_query_results(&bta_hl_cb, (tBTA_HL_DATA *) p_msg);
   2021             break;
   2022         case BTA_HL_API_DCH_ABORT_EVT:
   2023             bta_hl_api_dch_abort(&bta_hl_cb, (tBTA_HL_DATA *) p_msg);
   2024             break;
   2025 
   2026 
   2027         default:
   2028             if (p_msg->event < BTA_HL_DCH_EVT_MIN)
   2029             {
   2030                 if (bta_hl_find_cch_cb_indexes((tBTA_HL_DATA *) p_msg, &app_idx, &mcl_idx))
   2031                 {
   2032                     bta_hl_cch_sm_execute( app_idx,
   2033                                            mcl_idx,
   2034                                            p_msg->event, (tBTA_HL_DATA *) p_msg);
   2035                 }
   2036                 else
   2037                 {
   2038 #if BTA_HL_DEBUG == TRUE
   2039                     APPL_TRACE_ERROR("unable to find control block indexes for CCH: [event=%s]",
   2040                                       bta_hl_evt_code(p_msg->event));
   2041 #else
   2042                     APPL_TRACE_ERROR("unable to find control block indexes for CCH: [event=%d]", p_msg->event);
   2043 #endif
   2044                     success = FALSE;
   2045                 }
   2046             }
   2047             else
   2048             {
   2049                 if (bta_hl_find_dch_cb_indexes((tBTA_HL_DATA *) p_msg, &app_idx, &mcl_idx, &mdl_idx))
   2050                 {
   2051                     bta_hl_dch_sm_execute( app_idx,
   2052                                            mcl_idx,
   2053                                            mdl_idx,
   2054                                            p_msg->event, (tBTA_HL_DATA *) p_msg);
   2055                 }
   2056                 else
   2057                 {
   2058 
   2059 #if BTA_HL_DEBUG == TRUE
   2060                     APPL_TRACE_ERROR("unable to find control block indexes for DCH : [event=%s]",
   2061                                       bta_hl_evt_code(p_msg->event));
   2062 #else
   2063                     APPL_TRACE_ERROR("unable to find control block indexes for DCH: [event=%d]", p_msg->event);
   2064 #endif
   2065                     success = FALSE;
   2066                 }
   2067             }
   2068 
   2069             break;
   2070     }
   2071 
   2072     return(success);
   2073 }
   2074 
   2075 
   2076 /*****************************************************************************
   2077 **  Debug Functions
   2078 *****************************************************************************/
   2079 #if (BTA_HL_DEBUG == TRUE) && (BT_USE_TRACES == TRUE)
   2080 
   2081 /*******************************************************************************
   2082 **
   2083 ** Function         bta_hl_cch_state_code
   2084 **
   2085 ** Description      Map CCH state code to the corresponding state string
   2086 **
   2087 ** Returns          string pointer for the associated state name
   2088 **
   2089 *******************************************************************************/
   2090 static char *bta_hl_cch_state_code(tBTA_HL_CCH_STATE state_code)
   2091 {
   2092     switch (state_code)
   2093     {
   2094         case BTA_HL_CCH_IDLE_ST:
   2095             return "BTA_HL_CCH_IDLE_ST";
   2096         case BTA_HL_CCH_OPENING_ST:
   2097             return "BTA_HL_CCH_OPENING_ST";
   2098         case BTA_HL_CCH_OPEN_ST:
   2099             return "BTA_HL_CCH_OPEN_ST";
   2100         case BTA_HL_CCH_CLOSING_ST:
   2101             return "BTA_HL_CCH_CLOSING_ST";
   2102         default:
   2103             return "Unknown CCH state code";
   2104     }
   2105 }
   2106 
   2107 /*******************************************************************************
   2108 **
   2109 ** Function         bta_hl_dch_state_code
   2110 **
   2111 ** Description      Map DCH state code to the corresponding state string
   2112 **
   2113 ** Returns          string pointer for the associated state name
   2114 **
   2115 *******************************************************************************/
   2116 static char *bta_hl_dch_state_code(tBTA_HL_DCH_STATE state_code)
   2117 {
   2118     switch (state_code)
   2119     {
   2120         case BTA_HL_DCH_IDLE_ST:
   2121             return "BTA_HL_DCH_IDLE_ST";
   2122         case BTA_HL_DCH_OPENING_ST:
   2123             return "BTA_HL_DCH_OPENING_ST";
   2124         case BTA_HL_DCH_OPEN_ST:
   2125             return "BTA_HL_DCH_OPEN_ST";
   2126         case BTA_HL_DCH_CLOSING_ST:
   2127             return "BTA_HL_DCH_CLOSING_ST";
   2128         default:
   2129             return "Unknown DCH state code";
   2130     }
   2131 }
   2132 #endif  /* Debug Functions */
   2133 #endif /* HL_INCLUDED */
   2134