Home | History | Annotate | Download | only in src
      1 /******************************************************************************
      2  *
      3  *  Copyright (C) 2009-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  *  Filename:      btif_hl.c
     22  *
     23  *  Description:   Health Device Profile Bluetooth Interface
     24  *
     25  *
     26  ***********************************************************************************/
     27 #define LOG_TAG "BTIF_HL"
     28 
     29 #include <stdio.h>
     30 #include <stdlib.h>
     31 #include <errno.h>
     32 #include <string.h>
     33 #include <sys/types.h>
     34 #include <sys/socket.h>
     35 #include <sys/un.h>
     36 #include <time.h>
     37 #include <fcntl.h>
     38 #include <unistd.h>
     39 #include <pthread.h>
     40 #include <signal.h>
     41 #include <ctype.h>
     42 #include <sys/select.h>
     43 #include <sys/poll.h>
     44 #include <cutils/sockets.h>
     45 #include <cutils/log.h>
     46 
     47 #include <hardware/bluetooth.h>
     48 #include <hardware/bt_hl.h>
     49 
     50 #include "btif_common.h"
     51 #include "btif_util.h"
     52 #include "gki.h"
     53 #include "bd.h"
     54 #include "bta_api.h"
     55 #include "bta_hl_api.h"
     56 #include "mca_api.h"
     57 #include "btif_hl.h"
     58 #include "btif_storage.h"
     59 #include "btu.h"
     60 
     61 #define MAX_DATATYPE_SUPPORTED 8
     62 
     63 extern int btif_hl_update_maxfd( int max_org_s);
     64 extern void btif_hl_select_monitor_callback( fd_set *p_cur_set, fd_set *p_org_set );
     65 extern void btif_hl_select_wakeup_callback( fd_set *p_org_set , int wakeup_signal );
     66 extern int btif_hl_update_maxfd( int max_org_s);
     67 extern void btif_hl_select_monitor_callback( fd_set *p_cur_set, fd_set *p_org_set );
     68 extern void btif_hl_select_wakeup_callback( fd_set *p_org_set , int wakeup_signal );
     69 extern void btif_hl_soc_thread_init(void);
     70 extern void btif_hl_release_mcl_sockets(UINT8 app_idx, UINT8 mcl_idx);
     71 extern BOOLEAN btif_hl_create_socket(UINT8 app_idx, UINT8 mcl_idx, UINT8 mdl_idx);
     72 extern void btif_hl_release_socket(UINT8 app_idx, UINT8 mcl_idx, UINT8 mdl_idx);
     73 
     74 btif_hl_cb_t btif_hl_cb;
     75 btif_hl_cb_t *p_btif_hl_cb = &btif_hl_cb;
     76 
     77 /************************************************************************************
     78 **  Static variables
     79 ************************************************************************************/
     80 static bthl_callbacks_t  bt_hl_callbacks_cb;
     81 static bthl_callbacks_t *bt_hl_callbacks=NULL;
     82 
     83 /* signal socketpair to wake up select loop */
     84 
     85 const int btif_hl_signal_select_wakeup = 1;
     86 const int btif_hl_signal_select_exit = 2;
     87 const int btif_hl_signal_select_close_connected = 3;
     88 
     89 static int listen_s = -1;
     90 static int connected_s = -1;
     91 static int select_thread_id = -1;
     92 static int signal_fds[2];
     93 static BUFFER_Q soc_queue;
     94 static int reg_counter;
     95 
     96 static inline int btif_hl_select_wakeup(void);
     97 static inline int btif_hl_select_exit(void);
     98 static inline int btif_hl_select_close_connected(void);
     99 static inline int btif_hl_close_select_thread(void);
    100 static UINT8 btif_hl_get_next_app_id(void);
    101 static int btif_hl_get_next_channel_id(UINT8 app_id);
    102 static void btif_hl_init_next_app_id(void);
    103 static void btif_hl_init_next_channel_id(void);
    104 static void btif_hl_ctrl_cback(tBTA_HL_CTRL_EVT event, tBTA_HL_CTRL *p_data);
    105 static void btif_hl_set_state(btif_hl_state_t state);
    106 static btif_hl_state_t btif_hl_get_state(void);
    107 static void btif_hl_cback(tBTA_HL_EVT event, tBTA_HL *p_data);
    108 static void btif_hl_proc_cb_evt(UINT16 event, char* p_param);
    109 
    110 #define CHECK_CALL_CBACK(P_CB, P_CBACK, ...)\
    111     if (P_CB && P_CB->P_CBACK) {            \
    112         P_CB->P_CBACK(__VA_ARGS__);         \
    113     }                                       \
    114     else {                                  \
    115         ASSERTC(0, "Callback is NULL", 0);  \
    116     }
    117 
    118 
    119 #define BTIF_HL_CALL_CBACK(P_CB, P_CBACK, ...)\
    120      if((p_btif_hl_cb->state != BTIF_HL_STATE_DISABLING) &&\
    121          (p_btif_hl_cb->state != BTIF_HL_STATE_DISABLED))  \
    122      {                                                     \
    123         if (P_CB && P_CB->P_CBACK) {                       \
    124             P_CB->P_CBACK(__VA_ARGS__);                    \
    125         }                                                  \
    126         else {                                             \
    127             ASSERTC(0, "Callback is NULL", 0);             \
    128         }                                                  \
    129     }
    130 
    131 
    132 #define CHECK_BTHL_INIT() if (bt_hl_callbacks == NULL)\
    133     {\
    134         BTIF_TRACE_WARNING1("BTHL: %s: BTHL not initialized", __FUNCTION__);\
    135         return BT_STATUS_NOT_READY;\
    136     }\
    137     else\
    138     {\
    139         BTIF_TRACE_EVENT1("BTHL: %s", __FUNCTION__);\
    140     }
    141 
    142 
    143 static const btif_hl_data_type_cfg_t data_type_table[] = {
    144     /* Data Specilization                   Ntx     Nrx (from Bluetooth SIG's HDP whitepaper)*/
    145     {BTIF_HL_DATA_TYPE_PULSE_OXIMETER,      9216,   256},
    146     {BTIF_HL_DATA_TYPE_BLOOD_PRESSURE_MON,  896,    224},
    147     {BTIF_HL_DATA_TYPE_BODY_THERMOMETER,    896,    224},
    148     {BTIF_HL_DATA_TYPE_BODY_WEIGHT_SCALE,   896,    224},
    149     {BTIF_HL_DATA_TYPE_GLUCOSE_METER,       896,    224},
    150     {BTIF_HL_DATA_TYPE_STEP_COUNTER,        6624,   224}
    151 };
    152 
    153 #define BTIF_HL_DATA_TABLE_SIZE  (sizeof(data_type_table) / sizeof(btif_hl_data_type_cfg_t))
    154 #define BTIF_HL_DEFAULT_SRC_TX_APDU_SIZE   10240 /* use this size if the data type is not
    155                                                     defined in the table; for future proof */
    156 #define BTIF_HL_DEFAULT_SRC_RX_APDU_SIZE   512  /* use this size if the data type is not
    157                                                    defined in the table; for future proof */
    158 
    159 #define BTIF_HL_ECHO_MAX_TX_RX_APDU_SIZE 1024
    160 
    161 /************************************************************************************
    162 **  Static utility functions
    163 ************************************************************************************/
    164 
    165 #define BTIF_IF_GET_NAME 16
    166 void btif_hl_display_calling_process_name(void)
    167 {
    168     char name[16];
    169     prctl(BTIF_IF_GET_NAME, name, 0, 0, 0);
    170     BTIF_TRACE_DEBUG1("Process name (%s)", name);
    171 }
    172 #define BTIF_TIMEOUT_CCH_NO_DCH_SECS   30
    173 /*******************************************************************************
    174 **
    175 ** Function      btif_hl_if_channel_setup_pending
    176 **
    177 ** Description   check whether channel id is in setup pending state or not
    178 **
    179 ** Returns      BOOLEAN
    180 **
    181 *******************************************************************************/
    182 BOOLEAN btif_hl_if_channel_setup_pending(int channel_id, UINT8 *p_app_idx, UINT8 *p_mcl_idx)
    183 {
    184     btif_hl_app_cb_t    *p_acb;
    185     btif_hl_mcl_cb_t    *p_mcb;
    186     UINT8 i, j;
    187     BOOLEAN found=FALSE;
    188     for (i=0; i < BTA_HL_NUM_APPS ; i ++)
    189     {
    190         p_acb  =BTIF_HL_GET_APP_CB_PTR(i);
    191         if (p_acb->in_use)
    192         {
    193             for (j=0; j< BTA_HL_NUM_MCLS; j++)
    194             {
    195                 p_mcb = BTIF_HL_GET_MCL_CB_PTR(i, j);
    196                 if (p_mcb->in_use &&
    197                     p_mcb->is_connected && p_mcb->pcb.channel_id == channel_id )
    198                 {
    199                     found = TRUE;
    200                     *p_app_idx = i;
    201                     *p_mcl_idx = j;
    202                     break;
    203                 }
    204             }
    205         }
    206         if (found)
    207             break;
    208     }
    209     BTIF_TRACE_DEBUG5("%s found=%d channel_id=0x%08x",
    210                       __FUNCTION__, found, channel_id, *p_app_idx, *p_mcl_idx);
    211     return found;
    212 
    213 }
    214 /*******************************************************************************
    215 **
    216 ** Function      btif_hl_num_dchs_in_use
    217 **
    218 ** Description find number of DCHs in use
    219 **
    220 ** Returns      UINT8
    221 *******************************************************************************/
    222 UINT8 btif_hl_num_dchs_in_use(UINT8 mcl_handle){
    223 
    224     btif_hl_app_cb_t    * p_acb;
    225     btif_hl_mcl_cb_t    *p_mcb;
    226     UINT8               i,j,x;
    227     UINT8               cnt=0;
    228 
    229     for (i=0; i<BTA_HL_NUM_APPS; i++)
    230     {
    231         BTIF_TRACE_DEBUG1("btif_hl_num_dchs:i = %d",i);
    232         p_acb =BTIF_HL_GET_APP_CB_PTR(i);
    233         if (p_acb && p_acb->in_use)
    234         {
    235             for (j=0; j < BTA_HL_NUM_MCLS ; j++)
    236             {
    237                 if(p_acb->mcb[j].in_use)
    238                     BTIF_TRACE_DEBUG3("btif_hl_num_dchs:mcb in use j=%d, mcl_handle=%d,mcb handle=%d",
    239                                         j,mcl_handle, p_acb->mcb[j].mcl_handle);
    240                 if (p_acb->mcb[j].in_use &&
    241                     (p_acb->mcb[j].mcl_handle == mcl_handle))
    242                 {
    243                     p_mcb = &p_acb->mcb[j];
    244                     BTIF_TRACE_DEBUG1("btif_hl_num_dchs: mcl handle found j =%d",j);
    245                     for (x=0; x < BTA_HL_NUM_MDLS_PER_MCL ; x ++)
    246                     {
    247                         if (p_mcb->mdl[x].in_use)
    248                         {
    249                             BTIF_TRACE_DEBUG1("btif_hl_num_dchs_in_use:found x =%d",x);
    250                             cnt++;
    251                         }
    252                     }
    253                 }
    254             }
    255         }
    256     }
    257 
    258     BTIF_TRACE_DEBUG2("%s dch in use count=%d", __FUNCTION__, cnt);
    259     return cnt;
    260 }
    261 /*******************************************************************************
    262 **
    263 ** Function      btif_hl_tmr_hdlr
    264 **
    265 ** Description   Process timer timeout
    266 **
    267 ** Returns      void
    268 *******************************************************************************/
    269 void btif_hl_tmr_hdlr(TIMER_LIST_ENT *tle)
    270 {
    271     btif_hl_mcl_cb_t    *p_mcb;
    272     UINT8               i,j;
    273     BTIF_TRACE_DEBUG2("%s timer_in_use=%d",  __FUNCTION__, tle->in_use );
    274 
    275     for (i=0; i < BTA_HL_NUM_APPS ; i ++)
    276     {
    277         for (j=0; j< BTA_HL_NUM_MCLS; j++)
    278         {
    279             p_mcb =BTIF_HL_GET_MCL_CB_PTR(i,j);
    280 
    281             if (p_mcb->cch_timer_active)
    282             {
    283                 BTIF_TRACE_DEBUG3("%app_idx=%d, mcl_idx=%d mcl-connected=%d",
    284                                   i, j,  p_mcb->is_connected);
    285                 p_mcb->cch_timer_active = FALSE;
    286                 if (p_mcb->is_connected)
    287                 {
    288                     BTIF_TRACE_DEBUG3("Idle timeout Close CCH app_idx=%d mcl_idx=%d mcl_handle=%d",
    289                                       i ,j, p_mcb->mcl_handle);
    290                     BTA_HlCchClose(p_mcb->mcl_handle);
    291                 }
    292                 else
    293                 {
    294                     BTIF_TRACE_DEBUG2("CCH idle timeout But CCH not connected app_idx=%d mcl_idx=%d ",i,j);
    295                 }
    296             }
    297         }
    298     }
    299 }
    300 /*******************************************************************************
    301 **
    302 ** Function      btif_hl_stop_cch_timer
    303 **
    304 ** Description  stop CCH timer
    305 **
    306 ** Returns      void
    307 *******************************************************************************/
    308 void btif_hl_stop_cch_timer(UINT8 app_idx, UINT8 mcl_idx)
    309 {
    310     btif_hl_mcl_cb_t    *p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
    311     BTIF_TRACE_DEBUG4("%s app_idx=%d, mcl_idx=%d timer_in_use=%d",
    312                       __FUNCTION__,app_idx, mcl_idx, p_mcb->cch_timer.in_use);
    313 
    314     p_mcb->cch_timer_active = FALSE;
    315     if (p_mcb->cch_timer.in_use)
    316     {
    317         BTIF_TRACE_DEBUG0("stop CCH timer ");
    318         btu_stop_timer(&p_mcb->cch_timer);
    319     }
    320 }
    321 /*******************************************************************************
    322 **
    323 ** Function      btif_hl_start_cch_timer
    324 **
    325 ** Description  start CCH timer
    326 **
    327 ** Returns      void
    328 *******************************************************************************/
    329 void btif_hl_start_cch_timer(UINT8 app_idx, UINT8 mcl_idx)
    330 {
    331     btif_hl_mcl_cb_t    *p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
    332     BTIF_TRACE_DEBUG5("%s app_idx=%d, mcl_idx=%d  timer_active=%d timer_in_use=%d",
    333                       __FUNCTION__,app_idx, mcl_idx,
    334                       p_mcb->cch_timer_active, p_mcb->cch_timer.in_use);
    335 
    336     p_mcb->cch_timer_active = TRUE;
    337     if (!p_mcb->cch_timer.in_use)
    338     {
    339         BTIF_TRACE_DEBUG0("Start CCH timer ");
    340         memset(&p_mcb->cch_timer, 0, sizeof(TIMER_LIST_ENT));
    341         p_mcb->cch_timer.param = (UINT32)btif_hl_tmr_hdlr;
    342         btu_start_timer(&p_mcb->cch_timer, BTU_TTYPE_USER_FUNC,
    343                         BTIF_TIMEOUT_CCH_NO_DCH_SECS);
    344     }
    345     else
    346     {
    347         BTIF_TRACE_DEBUG0("Restart CCH timer ");
    348         btu_stop_timer(&p_mcb->cch_timer);
    349         btu_start_timer(&p_mcb->cch_timer, BTU_TTYPE_USER_FUNC,
    350                         BTIF_TIMEOUT_CCH_NO_DCH_SECS);
    351     }
    352 
    353 }
    354 /*******************************************************************************
    355 **
    356 ** Function      btif_hl_find_mdl_idx
    357 **
    358 ** Description  Find the MDL index using MDL ID
    359 **
    360 ** Returns      BOOLEAN
    361 **
    362 *******************************************************************************/
    363 static BOOLEAN btif_hl_find_mdl_idx(UINT8 app_idx, UINT8 mcl_idx, UINT16 mdl_id,
    364                                     UINT8 *p_mdl_idx)
    365 {
    366     btif_hl_mcl_cb_t      *p_mcb  = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
    367     BOOLEAN found=FALSE;
    368     UINT8 i;
    369 
    370     for (i=0; i < BTA_HL_NUM_MDLS_PER_MCL ; i ++)
    371     {
    372         if (p_mcb->mdl[i].in_use  &&
    373             (mdl_id !=0) &&
    374             (p_mcb->mdl[i].mdl_id== mdl_id))
    375         {
    376             found = TRUE;
    377             *p_mdl_idx = i;
    378             break;
    379         }
    380     }
    381 
    382     BTIF_TRACE_DEBUG4("%s found=%d mdl_id=%d mdl_idx=%d ",
    383                       __FUNCTION__,found, mdl_id, i);
    384 
    385     return found;
    386 }
    387 
    388 /*******************************************************************************
    389 **
    390 ** Function      btif_hl_get_buf
    391 **
    392 ** Description   get buffer
    393 **
    394 ** Returns     void
    395 **
    396 *******************************************************************************/
    397 void * btif_hl_get_buf(UINT16 size)
    398 {
    399     void *p_new;
    400 
    401     BTIF_TRACE_DEBUG1("%s", __FUNCTION__);
    402     BTIF_TRACE_DEBUG2("ret size=%d GKI_MAX_BUF_SIZE=%d",size, 6000);
    403 
    404     if (size < 6000)
    405     {
    406         p_new = GKI_getbuf(size);
    407     }
    408     else
    409     {
    410         BTIF_TRACE_DEBUG0("btif_hl_get_buf use HL large data pool");
    411         p_new = GKI_getpoolbuf(4);
    412     }
    413 
    414     return p_new;
    415 }
    416 /*******************************************************************************
    417 **
    418 ** Function      btif_hl_free_buf
    419 **
    420 ** Description free buffer
    421 **
    422 ** Return void
    423 **
    424 *******************************************************************************/
    425 void btif_hl_free_buf(void **p)
    426 {
    427     if (*p != NULL)
    428     {
    429         BTIF_TRACE_DEBUG1("%s OK", __FUNCTION__ );
    430         GKI_freebuf(*p);
    431         *p = NULL;
    432     }
    433     else
    434         BTIF_TRACE_ERROR1("%s NULL pointer",__FUNCTION__ );
    435 }
    436 /*******************************************************************************
    437 **
    438 ** Function      btif_hl_is_the_first_reliable_existed
    439 **
    440 ** Description  This function checks whether the first reliable DCH channel
    441 **              has been setup on the MCL or not
    442 **
    443 ** Returns      BOOLEAN - TRUE exist
    444 **                        FALSE does not exist
    445 **
    446 *******************************************************************************/
    447 BOOLEAN btif_hl_is_the_first_reliable_existed(UINT8 app_idx, UINT8 mcl_idx )
    448 {
    449     btif_hl_mcl_cb_t          *p_mcb  =BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
    450     BOOLEAN is_existed =FALSE;
    451     UINT8 i ;
    452 
    453     for (i=0; i< BTA_HL_NUM_MDLS_PER_MCL; i++)
    454     {
    455         if (p_mcb->mdl[i].in_use && p_mcb->mdl[i].is_the_first_reliable)
    456         {
    457             is_existed = TRUE;
    458             break;
    459         }
    460     }
    461 
    462     BTIF_TRACE_DEBUG1("bta_hl_is_the_first_reliable_existed is_existed=%d  ",is_existed );
    463     return is_existed;
    464 }
    465 /*******************************************************************************
    466 **
    467 ** Function      btif_hl_clean_delete_mdl
    468 **
    469 ** Description   Cleanup the delete mdl control block
    470 **
    471 ** Returns     Nothing
    472 **
    473 *******************************************************************************/
    474 static void btif_hl_clean_delete_mdl(btif_hl_delete_mdl_t *p_cb)
    475 {
    476     BTIF_TRACE_DEBUG1("%s", __FUNCTION__ );
    477     memset(p_cb, 0 , sizeof(btif_hl_delete_mdl_t));
    478 }
    479 
    480 /*******************************************************************************
    481 **
    482 ** Function      btif_hl_clean_pcb
    483 **
    484 ** Description   Cleanup the pending chan control block
    485 **
    486 ** Returns     Nothing
    487 **
    488 *******************************************************************************/
    489 static void btif_hl_clean_pcb(btif_hl_pending_chan_cb_t *p_pcb)
    490 {
    491     BTIF_TRACE_DEBUG1("%s", __FUNCTION__ );
    492     memset(p_pcb, 0 , sizeof(btif_hl_pending_chan_cb_t));
    493 }
    494 
    495 
    496 /*******************************************************************************
    497 **
    498 ** Function      btif_hl_clean_mdl_cb
    499 **
    500 ** Description   Cleanup the MDL control block
    501 **
    502 ** Returns     Nothing
    503 **
    504 *******************************************************************************/
    505 static void btif_hl_clean_mdl_cb(btif_hl_mdl_cb_t *p_dcb)
    506 {
    507     BTIF_TRACE_DEBUG1("%s", __FUNCTION__ );
    508     btif_hl_free_buf((void **) &p_dcb->p_rx_pkt);
    509     btif_hl_free_buf((void **) &p_dcb->p_tx_pkt);
    510     memset(p_dcb, 0 , sizeof(btif_hl_mdl_cb_t));
    511 }
    512 
    513 
    514 /*******************************************************************************
    515 **
    516 ** Function      btif_hl_reset_mcb
    517 **
    518 ** Description   Reset MCL control block
    519 **
    520 ** Returns      BOOLEAN
    521 **
    522 *******************************************************************************/
    523 static void btif_hl_clean_mcl_cb(UINT8 app_idx, UINT8 mcl_idx)
    524 {
    525     btif_hl_mcl_cb_t     *p_mcb;
    526     BTIF_TRACE_DEBUG3("%s app_idx=%d, mcl_idx=%d", __FUNCTION__,app_idx, mcl_idx);
    527     p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
    528     memset(p_mcb, 0, sizeof(btif_hl_mcl_cb_t));
    529 }
    530 
    531 
    532 /*******************************************************************************
    533 **
    534 ** Function      btif_hl_find_sdp_idx_using_mdep_filter
    535 **
    536 ** Description  This function finds the SDP record index using MDEP filter parameters
    537 **
    538 ** Returns      BOOLEAN
    539 **
    540 *******************************************************************************/
    541 static void btif_hl_reset_mdep_filter(UINT8 app_idx)
    542 {
    543     btif_hl_app_cb_t          *p_acb  =BTIF_HL_GET_APP_CB_PTR(app_idx);
    544     p_acb->filter.num_elems = 0;
    545 }
    546 
    547 /*******************************************************************************
    548 **
    549 ** Function      btif_hl_find_sdp_idx_using_mdep_filter
    550 **
    551 ** Description  This function finds the SDP record index using MDEP filter parameters
    552 **
    553 ** Returns      BOOLEAN
    554 **
    555 *******************************************************************************/
    556 static BOOLEAN btif_hl_find_sdp_idx_using_mdep_filter(UINT8 app_idx, UINT8 mcl_idx, UINT8 *p_sdp_idx)
    557 {
    558     btif_hl_app_cb_t          *p_acb  =BTIF_HL_GET_APP_CB_PTR(app_idx);
    559     btif_hl_mcl_cb_t          *p_mcb  =BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
    560     UINT8                   i, j, num_recs,num_elems, num_mdeps, mdep_cnt, mdep_idx;
    561     tBTA_HL_MDEP_ROLE       peer_mdep_role;
    562     UINT16                  data_type;
    563     tBTA_HL_SDP_MDEP_CFG    *p_mdep;
    564     BOOLEAN                 found = FALSE;
    565     BOOLEAN                 elem_found;
    566 
    567     BTIF_TRACE_DEBUG0("btif_hl_find_sdp_idx_using_mdep_filter");
    568     num_recs = p_mcb->sdp.num_recs;
    569     num_elems = p_acb->filter.num_elems;
    570     if (!num_elems)
    571     {
    572         BTIF_TRACE_DEBUG0("btif_hl_find_sdp_idx_using_mdep_filter num_elem=0");
    573         *p_sdp_idx = 0;
    574         found = TRUE;
    575         return found;
    576     }
    577 
    578     for (i=0; i<num_recs; i++)
    579     {
    580         num_mdeps = p_mcb->sdp.sdp_rec[i].num_mdeps;
    581         for (j=0; j<num_elems; j++ )
    582         {
    583             data_type = p_acb->filter.elem[j].data_type;
    584             peer_mdep_role = p_acb->filter.elem[j].peer_mdep_role;
    585             elem_found = FALSE;
    586             mdep_cnt =0;
    587             mdep_idx=0;
    588             while (!elem_found && mdep_idx < num_mdeps )
    589             {
    590                 p_mdep = &(p_mcb->sdp.sdp_rec[i].mdep_cfg[mdep_idx]);
    591                 if ( (p_mdep->data_type == data_type) &&
    592                      (p_mdep->mdep_role == peer_mdep_role) )
    593                 {
    594                     elem_found = TRUE;
    595                 }
    596                 else
    597                 {
    598                     mdep_idx++;
    599                 }
    600             }
    601 
    602             if (!elem_found)
    603             {
    604                 found = FALSE;
    605                 break;
    606             }
    607             else
    608             {
    609                 found = TRUE;
    610             }
    611         }
    612 
    613         if (found)
    614         {
    615             BTIF_TRACE_DEBUG1("btif_hl_find_sdp_idx_using_mdep_filter found idx=%d",i);
    616             *p_sdp_idx = i;
    617             break;
    618         }
    619     }
    620 
    621     BTIF_TRACE_DEBUG3("%s found=%d sdp_idx=%d",__FUNCTION__ , found, *p_sdp_idx);
    622 
    623     btif_hl_reset_mdep_filter(app_idx);
    624 
    625     return found;
    626 }
    627 /*******************************************************************************
    628 **
    629 ** Function      btif_hl_is_reconnect_possible
    630 **
    631 ** Description  check reconnect is possible or not
    632 **
    633 ** Returns      BOOLEAN
    634 **
    635 *******************************************************************************/
    636 BOOLEAN btif_hl_is_reconnect_possible(UINT8 app_idx, UINT8 mcl_idx,  int mdep_cfg_idx,
    637                                       tBTA_HL_DCH_OPEN_PARAM *p_dch_open_api, tBTA_HL_MDL_ID *p_mdl_id)
    638 {
    639     btif_hl_app_cb_t     *p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx);
    640     btif_hl_mcl_cb_t     *p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
    641     tBTA_HL_DCH_CFG      local_cfg = p_dch_open_api->local_cfg;
    642     tBTA_HL_DCH_MODE     dch_mode = BTA_HL_DCH_MODE_RELIABLE;
    643     BOOLEAN              use_mdl_dch_mode=FALSE;
    644     btif_hl_mdl_cfg_t    *p_mdl;
    645     btif_hl_mdl_cfg_t    *p_mdl1;
    646     UINT8                i, j;
    647     BOOLEAN              is_reconnect_ok=FALSE;
    648     BOOLEAN              stream_mode_avail=FALSE;
    649     UINT16               data_type = p_acb->sup_feature.mdep[mdep_cfg_idx].mdep_cfg.data_cfg[0].data_type;
    650     tBTA_HL_MDEP_ID      peer_mdep_id = p_dch_open_api->peer_mdep_id;
    651     UINT8                mdl_idx;
    652 
    653 
    654     BTIF_TRACE_DEBUG4("%s app_idx=%d mcl_idx=%d mdep_cfg_idx=%d",
    655                       __FUNCTION__, app_idx, mcl_idx, mdep_cfg_idx  );
    656     switch (local_cfg)
    657     {
    658         case BTA_HL_DCH_CFG_NO_PREF:
    659             if (!btif_hl_is_the_first_reliable_existed(app_idx, mcl_idx))
    660             {
    661                 dch_mode = BTA_HL_DCH_MODE_RELIABLE;
    662             }
    663             else
    664             {
    665                 use_mdl_dch_mode = TRUE;
    666             }
    667             break;
    668         case BTA_HL_DCH_CFG_RELIABLE:
    669             dch_mode = BTA_HL_DCH_MODE_RELIABLE;
    670             break;
    671         case BTA_HL_DCH_CFG_STREAMING:
    672             dch_mode = BTA_HL_DCH_MODE_STREAMING;
    673             break;
    674         default:
    675             BTIF_TRACE_ERROR1("Invalid local_cfg=%d",local_cfg );
    676             return is_reconnect_ok;
    677             break;
    678 
    679     }
    680 
    681     BTIF_TRACE_DEBUG3("local_cfg=%d use_mdl_dch_mode=%d dch_mode=%d ",
    682                       local_cfg, use_mdl_dch_mode, dch_mode  );
    683 
    684     for (i=0, p_mdl=&p_acb->mdl_cfg[0] ; i< BTA_HL_NUM_MDL_CFGS; i++, p_mdl++ )
    685     {
    686         if (p_mdl->base.active &&
    687             p_mdl->extra.data_type ==data_type &&
    688             (p_mdl->extra.peer_mdep_id != BTA_HL_INVALID_MDEP_ID && p_mdl->extra.peer_mdep_id == peer_mdep_id) &&
    689             memcpy(p_mdl->base.peer_bd_addr, p_mcb->bd_addr,sizeof(BD_ADDR) ) &&
    690             p_mdl->base.mdl_id &&
    691             !btif_hl_find_mdl_idx(app_idx, mcl_idx,p_mdl->base.mdl_id, &mdl_idx))
    692         {
    693             BTIF_TRACE_DEBUG4("i=%d Matched active=%d   mdl_id =%d, mdl_dch_mode=%d",
    694                               i, p_mdl->base.active, p_mdl->base.mdl_id,p_mdl->base.dch_mode);
    695             if (!use_mdl_dch_mode)
    696             {
    697                 if (p_mdl->base.dch_mode == dch_mode)
    698                 {
    699                     is_reconnect_ok = TRUE;
    700                     *p_mdl_id = p_mdl->base.mdl_id;
    701                     BTIF_TRACE_DEBUG2("reconnect is possible dch_mode=%d mdl_id=%d", dch_mode, p_mdl->base.mdl_id );
    702                     break;
    703                 }
    704             }
    705             else
    706             {
    707                 is_reconnect_ok = TRUE;
    708                 for (j=i, p_mdl1=&p_acb->mdl_cfg[i]; j< BTA_HL_NUM_MDL_CFGS; j++, p_mdl1++)
    709                 {
    710                     if (p_mdl1->base.active &&
    711                         p_mdl1->extra.data_type == data_type &&
    712                         (p_mdl1->extra.peer_mdep_id != BTA_HL_INVALID_MDEP_ID && p_mdl1->extra.peer_mdep_id == peer_mdep_id) &&
    713                         memcpy(p_mdl1->base.peer_bd_addr, p_mcb->bd_addr,sizeof(BD_ADDR)) &&
    714                         p_mdl1->base.dch_mode == BTA_HL_DCH_MODE_STREAMING)
    715                     {
    716                         stream_mode_avail = TRUE;
    717                         BTIF_TRACE_DEBUG1("found streaming mode mdl index=%d", j);
    718                         break;
    719                     }
    720                 }
    721 
    722                 if (stream_mode_avail)
    723                 {
    724                     dch_mode = BTA_HL_DCH_MODE_STREAMING;
    725                     *p_mdl_id = p_mdl1->base.mdl_id;
    726                     BTIF_TRACE_DEBUG2("reconnect is ok index=%d dch_mode=streaming  mdl_id=%d", j, *p_mdl_id);
    727                     break;
    728                 }
    729                 else
    730                 {
    731                     dch_mode= p_mdl->base.dch_mode;
    732                     *p_mdl_id = p_mdl->base.mdl_id;
    733                     BTIF_TRACE_DEBUG3("reconnect is ok index=%d  dch_mode=%d mdl_id=%d", i,  p_mdl->base.dch_mode, *p_mdl_id);
    734                     break;
    735 
    736                 }
    737             }
    738 
    739         }
    740 
    741     }
    742 
    743     BTIF_TRACE_DEBUG3("is_reconnect_ok  dch_mode=%d mdl_id=%d",is_reconnect_ok, dch_mode, *p_mdl_id);
    744     return is_reconnect_ok;
    745 }
    746 
    747 /*******************************************************************************
    748 **
    749 ** Function      btif_hl_dch_open
    750 **
    751 ** Description   Process DCH open request using the DCH Open API parameters
    752 **
    753 ** Returns      BOOLEAN
    754 **
    755 *******************************************************************************/
    756 BOOLEAN btif_hl_dch_open(UINT8 app_id, BD_ADDR bd_addr,
    757                          tBTA_HL_DCH_OPEN_PARAM *p_dch_open_api,
    758                          int mdep_cfg_idx,
    759                          btif_hl_pend_dch_op_t op, int *channel_id){
    760     btif_hl_app_cb_t            *p_acb;
    761     btif_hl_mcl_cb_t            *p_mcb;
    762     btif_hl_pending_chan_cb_t   *p_pcb;
    763     UINT8                       app_idx, mcl_idx;
    764     BOOLEAN                     status = FALSE;
    765     tBTA_HL_MDL_ID              mdl_id;
    766     tBTA_HL_DCH_RECONNECT_PARAM reconnect_param;
    767 
    768     BTIF_TRACE_DEBUG2("%s app_id=%d ",
    769                       __FUNCTION__, app_id );
    770     BTIF_TRACE_DEBUG6("DB [%02x:%02x:%02x:%02x:%02x:%02x]",
    771                       bd_addr[0],  bd_addr[1],bd_addr[2],  bd_addr[3], bd_addr[4],  bd_addr[5]);
    772 
    773     if (btif_hl_find_app_idx(app_id, &app_idx))
    774     {
    775         if (btif_hl_find_mcl_idx(app_idx, bd_addr , &mcl_idx))
    776         {
    777             p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
    778 
    779             p_pcb = BTIF_HL_GET_PCB_PTR(app_idx, mcl_idx);
    780             if (!p_pcb->in_use)
    781             {
    782                 p_mcb->req_ctrl_psm = p_dch_open_api->ctrl_psm;
    783 
    784                 p_pcb->in_use = TRUE;
    785                 *channel_id       =
    786                 p_pcb->channel_id =  (int) btif_hl_get_next_channel_id(app_id);
    787                 p_pcb->cb_state = BTIF_HL_CHAN_CB_STATE_CONNECTING_PENDING;
    788                 p_pcb->mdep_cfg_idx = mdep_cfg_idx;
    789                 memcpy(p_pcb->bd_addr, bd_addr, sizeof(BD_ADDR));
    790                 p_pcb->op = op;
    791 
    792                 if (p_mcb->sdp.num_recs)
    793                 {
    794                     if (p_mcb->valid_sdp_idx)
    795                     {
    796                         p_dch_open_api->ctrl_psm  = p_mcb->ctrl_psm;
    797                     }
    798 
    799                     if (!btif_hl_is_reconnect_possible(app_idx, mcl_idx, mdep_cfg_idx, p_dch_open_api, &mdl_id ))
    800                     {
    801 
    802                         BTIF_TRACE_DEBUG0("Issue DCH open" );
    803                         BTA_HlDchOpen(p_mcb->mcl_handle, p_dch_open_api);
    804                     }
    805                     else
    806                     {
    807                         reconnect_param.ctrl_psm = p_mcb->ctrl_psm;
    808                         reconnect_param.mdl_id = mdl_id;;
    809                         BTIF_TRACE_DEBUG2("Issue Reconnect ctrl_psm=0x%x mdl_id=0x%x",reconnect_param.ctrl_psm, reconnect_param.mdl_id   );
    810                         BTA_HlDchReconnect(p_mcb->mcl_handle, &reconnect_param);
    811                     }
    812 
    813                     status = TRUE;
    814                 }
    815                 else
    816                 {
    817                     p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx);
    818                     p_mcb->cch_oper = BTIF_HL_CCH_OP_DCH_OPEN;
    819                     BTA_HlSdpQuery(app_id,p_acb->app_handle, bd_addr);
    820                     status = TRUE;
    821                 }
    822             }
    823         }
    824     }
    825 
    826     BTIF_TRACE_DEBUG1("status=%d ", status);
    827     return status;
    828 }
    829 /*******************************************************************************
    830 **
    831 ** Function      btif_hl_copy_bda
    832 **
    833 ** Description  copy bt_bdaddr_t to BD_ADDR format
    834 **
    835 ** Returns      void
    836 **
    837 *******************************************************************************/
    838 void btif_hl_copy_bda(bt_bdaddr_t *bd_addr, BD_ADDR  bda){
    839     UINT8 i;
    840     for (i=0; i<6; i++)
    841     {
    842         bd_addr->address[i] = bda[i] ;
    843     }
    844 }
    845 /*******************************************************************************
    846 **
    847 ** Function      btif_hl_copy_bda
    848 **
    849 ** Description  display bt_bdaddr_t
    850 **
    851 ** Returns      BOOLEAN
    852 **
    853 *******************************************************************************/
    854 void btif_hl_display_bt_bda(bt_bdaddr_t *bd_addr){
    855     BTIF_TRACE_DEBUG6("DB [%02x:%02x:%02x:%02x:%02x:%02x]",
    856                       bd_addr->address[0],   bd_addr->address[1], bd_addr->address[2],
    857                       bd_addr->address[3],  bd_addr->address[4],   bd_addr->address[5]);
    858 }
    859 
    860 /*******************************************************************************
    861 **
    862 ** Function         btif_hl_dch_abort
    863 **
    864 ** Description      Process DCH abort request
    865 **
    866 ** Returns          Nothing
    867 **
    868 *******************************************************************************/
    869 void  btif_hl_dch_abort(UINT8 app_idx, UINT8 mcl_idx){
    870     btif_hl_mcl_cb_t      *p_mcb;
    871 
    872     BTIF_TRACE_DEBUG3("%s app_idx=%d mcl_idx=%d",__FUNCTION__, app_idx, mcl_idx );
    873     p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
    874     if (p_mcb->is_connected)
    875     {
    876         BTA_HlDchAbort(p_mcb->mcl_handle);
    877     }
    878     else
    879     {
    880         p_mcb->pcb.abort_pending = TRUE;
    881     }
    882 
    883 }
    884 /*******************************************************************************
    885 **
    886 ** Function      btif_hl_cch_open
    887 **
    888 ** Description   Process CCH open request
    889 **
    890 ** Returns     Nothing
    891 **
    892 *******************************************************************************/
    893 BOOLEAN btif_hl_cch_open(UINT8 app_id, BD_ADDR bd_addr, UINT16 ctrl_psm,
    894                          int mdep_cfg_idx,
    895                          btif_hl_pend_dch_op_t op, int *channel_id){
    896 
    897     btif_hl_app_cb_t            *p_acb;
    898     btif_hl_mcl_cb_t            *p_mcb;
    899     btif_hl_pending_chan_cb_t   *p_pcb;
    900     UINT8                       app_idx, mcl_idx, chan_idx;
    901     BOOLEAN                     status = TRUE;
    902 
    903     BTIF_TRACE_DEBUG5("%s app_id=%d ctrl_psm=%d mdep_cfg_idx=%d op=%d",
    904                       __FUNCTION__, app_id, ctrl_psm, mdep_cfg_idx, op);
    905     BTIF_TRACE_DEBUG6("DB [%02x:%02x:%02x:%02x:%02x:%02x]",
    906                       bd_addr[0],  bd_addr[1],bd_addr[2],  bd_addr[3], bd_addr[4],  bd_addr[5]);
    907 
    908     if (btif_hl_find_app_idx(app_id, &app_idx))
    909     {
    910         p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx);
    911 
    912         if (!btif_hl_find_mcl_idx(app_idx, bd_addr, &mcl_idx))
    913         {
    914             if (btif_hl_find_avail_mcl_idx(app_idx, &mcl_idx))
    915             {
    916                 p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
    917                 memset(p_mcb,0, sizeof(btif_hl_mcl_cb_t));
    918                 p_mcb->in_use = TRUE;
    919                 bdcpy(p_mcb->bd_addr, bd_addr);
    920 
    921                 if (!ctrl_psm)
    922                 {
    923                     p_mcb->cch_oper = BTIF_HL_CCH_OP_MDEP_FILTERING;
    924                 }
    925                 else
    926                 {
    927                     p_mcb->cch_oper        = BTIF_HL_CCH_OP_MATCHED_CTRL_PSM;
    928                     p_mcb->req_ctrl_psm    = ctrl_psm;
    929                 }
    930 
    931                 p_pcb = BTIF_HL_GET_PCB_PTR(app_idx, mcl_idx);
    932                 p_pcb->in_use = TRUE;
    933                 p_pcb->mdep_cfg_idx = mdep_cfg_idx;
    934                 memcpy(p_pcb->bd_addr, bd_addr, sizeof(BD_ADDR));
    935                 p_pcb->op = op;
    936 
    937                 switch (op)
    938                 {
    939                     case BTIF_HL_PEND_DCH_OP_OPEN:
    940                         *channel_id       =
    941                         p_pcb->channel_id =  (int) btif_hl_get_next_channel_id(app_id);
    942                         p_pcb->cb_state = BTIF_HL_CHAN_CB_STATE_CONNECTING_PENDING;
    943                         break;
    944                     case BTIF_HL_PEND_DCH_OP_DELETE_MDL:
    945                         p_pcb->channel_id =  p_acb->delete_mdl.channel_id;
    946                         p_pcb->cb_state = BTIF_HL_CHAN_CB_STATE_DESTROYED_PENDING;
    947                         break;
    948                     default:
    949                         break;
    950                 }
    951                 BTA_HlSdpQuery(app_id,p_acb->app_handle, bd_addr);
    952             }
    953             else
    954             {
    955                 status = FALSE;
    956                 BTIF_TRACE_ERROR0("Open CCH request discarded- No mcl cb");
    957             }
    958         }
    959         else
    960         {
    961             status = FALSE;
    962             BTIF_TRACE_ERROR0("Open CCH request discarded- already in USE");
    963         }
    964     }
    965     else
    966     {
    967         status = FALSE;
    968         BTIF_TRACE_ERROR1("Invalid app_id=%d", app_id);
    969     }
    970 
    971     if (channel_id)
    972     {
    973         BTIF_TRACE_DEBUG2("status=%d channel_id=0x%08x", status, *channel_id);
    974     }
    975     else
    976     {
    977         BTIF_TRACE_DEBUG1("status=%d ", status);
    978     }
    979     return status;
    980 }
    981 
    982 
    983 /*******************************************************************************
    984 **
    985 ** Function      btif_hl_find_mdl_idx_using_handle
    986 **
    987 ** Description  Find the MDL index using channel id
    988 **
    989 ** Returns      BOOLEAN
    990 **
    991 *******************************************************************************/
    992 BOOLEAN btif_hl_find_mdl_cfg_idx_using_channel_id(int channel_id,
    993                                                   UINT8 *p_app_idx,
    994                                                   UINT8 *p_mdl_cfg_idx){
    995     btif_hl_app_cb_t      *p_acb;
    996     btif_hl_mdl_cfg_t     *p_mdl;
    997     BOOLEAN found=FALSE;
    998     UINT8 i,j;
    999     int mdl_cfg_channel_id;
   1000 
   1001     for (i=0; i < BTA_HL_NUM_APPS ; i ++)
   1002     {
   1003         p_acb =BTIF_HL_GET_APP_CB_PTR(i);
   1004         for (j=0; j< BTA_HL_NUM_MDL_CFGS; j++)
   1005         {
   1006             p_mdl =BTIF_HL_GET_MDL_CFG_PTR(i,j);
   1007             mdl_cfg_channel_id = *(BTIF_HL_GET_MDL_CFG_CHANNEL_ID_PTR(i,j));
   1008             if (p_acb->in_use &&
   1009                 p_mdl->base.active &&
   1010                 (mdl_cfg_channel_id == channel_id))
   1011             {
   1012                 found = TRUE;
   1013                 *p_app_idx = i;
   1014                 *p_mdl_cfg_idx =j;
   1015                 break;
   1016             }
   1017         }
   1018     }
   1019 
   1020     BTIF_TRACE_EVENT5("%s found=%d channel_id=0x%08x, app_idx=%d mdl_cfg_idx=%d  ",
   1021                       __FUNCTION__,found,channel_id, i,j );
   1022     return found;
   1023 }
   1024 /*******************************************************************************
   1025 **
   1026 ** Function      btif_hl_find_mdl_idx_using_handle
   1027 **
   1028 ** Description  Find the MDL index using channel id
   1029 **
   1030 ** Returns      BOOLEAN
   1031 **
   1032 *******************************************************************************/
   1033 BOOLEAN btif_hl_find_mdl_idx_using_channel_id(int channel_id,
   1034                                               UINT8 *p_app_idx,UINT8 *p_mcl_idx,
   1035                                               UINT8 *p_mdl_idx){
   1036     btif_hl_app_cb_t      *p_acb;
   1037     btif_hl_mcl_cb_t      *p_mcb;
   1038     btif_hl_mdl_cb_t      *p_dcb;
   1039     BOOLEAN found=FALSE;
   1040     UINT8 i,j,k;
   1041 
   1042     for (i=0; i < BTA_HL_NUM_APPS ; i ++)
   1043     {
   1044         p_acb =BTIF_HL_GET_APP_CB_PTR(i);
   1045         for (j=0; j< BTA_HL_NUM_MCLS; j++)
   1046         {
   1047             p_mcb =BTIF_HL_GET_MCL_CB_PTR(i,j);
   1048             for (k=0; k< BTA_HL_NUM_MDLS_PER_MCL; k++)
   1049             {
   1050                 p_dcb =BTIF_HL_GET_MDL_CB_PTR(i,j,k);
   1051                 if (p_acb->in_use &&
   1052                     p_mcb->in_use &&
   1053                     p_dcb->in_use &&
   1054                     (p_dcb->channel_id == channel_id))
   1055                 {
   1056                     found = TRUE;
   1057                     *p_app_idx = i;
   1058                     *p_mcl_idx =j;
   1059                     *p_mdl_idx = k;
   1060                     break;
   1061                 }
   1062             }
   1063         }
   1064     }
   1065     BTIF_TRACE_DEBUG5("%s found=%d app_idx=%d mcl_idx=%d mdl_idx=%d  ",
   1066                       __FUNCTION__,found,i,j,k );
   1067     return found;
   1068 }
   1069 
   1070 /*******************************************************************************
   1071 **
   1072 ** Function      btif_hl_find_channel_id_using_mdl_id
   1073 **
   1074 ** Description  Find channel id using mdl_id'
   1075 **
   1076 ** Returns      BOOLEAN
   1077 *********************************************************************************/
   1078 BOOLEAN btif_hl_find_channel_id_using_mdl_id(UINT8 app_idx, tBTA_HL_MDL_ID mdl_id,
   1079                                             int *p_channel_id){
   1080     btif_hl_app_cb_t      *p_acb;
   1081     btif_hl_mdl_cfg_t     *p_mdl;
   1082     BOOLEAN found=FALSE;
   1083     UINT8 j=0;
   1084     int mdl_cfg_channel_id;
   1085     p_acb =BTIF_HL_GET_APP_CB_PTR(app_idx);
   1086     if (p_acb && p_acb->in_use)
   1087         {
   1088             for (j=0; j< BTA_HL_NUM_MDL_CFGS; j++)
   1089                 {
   1090                     p_mdl =BTIF_HL_GET_MDL_CFG_PTR(app_idx,j);
   1091                     mdl_cfg_channel_id = *(BTIF_HL_GET_MDL_CFG_CHANNEL_ID_PTR(app_idx,j));
   1092                     if ( p_mdl->base.active && (p_mdl->base.mdl_id == mdl_id))
   1093                     {
   1094                             found = TRUE;
   1095                             *p_channel_id = mdl_cfg_channel_id;
   1096                             break;
   1097                     }
   1098                 }
   1099         }
   1100     BTIF_TRACE_EVENT6("%s found=%d channel_id=0x%08x, mdl_id=0x%x app_idx=%d mdl_cfg_idx=%d  ",
   1101                     __FUNCTION__,found,*p_channel_id,mdl_id, app_idx,j );
   1102     return found;
   1103 }
   1104 
   1105 
   1106 /*******************************************************************************
   1107 **
   1108 ** Function      btif_hl_find_mdl_idx_using_handle
   1109 **
   1110 ** Description  Find the MDL index using handle
   1111 **
   1112 ** Returns      BOOLEAN
   1113 **
   1114 *******************************************************************************/
   1115 BOOLEAN btif_hl_find_mdl_idx_using_handle(tBTA_HL_MDL_HANDLE mdl_handle,
   1116                                           UINT8 *p_app_idx,UINT8 *p_mcl_idx,
   1117                                           UINT8 *p_mdl_idx){
   1118     btif_hl_app_cb_t      *p_acb;
   1119     btif_hl_mcl_cb_t      *p_mcb;
   1120     btif_hl_mdl_cb_t      *p_dcb;
   1121     BOOLEAN found=FALSE;
   1122     UINT8 i,j,k;
   1123 
   1124     for (i=0; i < BTA_HL_NUM_APPS ; i ++)
   1125     {
   1126         p_acb =BTIF_HL_GET_APP_CB_PTR(i);
   1127         for (j=0; j< BTA_HL_NUM_MCLS; j++)
   1128         {
   1129             p_mcb =BTIF_HL_GET_MCL_CB_PTR(i,j);
   1130             for (k=0; k< BTA_HL_NUM_MDLS_PER_MCL; k++)
   1131             {
   1132                 p_dcb =BTIF_HL_GET_MDL_CB_PTR(i,j,k);
   1133                 if (p_acb->in_use &&
   1134                     p_mcb->in_use &&
   1135                     p_dcb->in_use &&
   1136                     (p_dcb->mdl_handle == mdl_handle))
   1137                 {
   1138                     found = TRUE;
   1139                     *p_app_idx = i;
   1140                     *p_mcl_idx =j;
   1141                     *p_mdl_idx = k;
   1142                     break;
   1143                 }
   1144             }
   1145         }
   1146     }
   1147 
   1148 
   1149     BTIF_TRACE_EVENT5("%s found=%d app_idx=%d mcl_idx=%d mdl_idx=%d  ",
   1150                       __FUNCTION__,found,i,j,k );
   1151     return found;
   1152 }
   1153 /*******************************************************************************
   1154 **
   1155 ** Function        btif_hl_find_peer_mdep_id
   1156 **
   1157 ** Description      Find the peer MDEP ID from the received SPD records
   1158 **
   1159 ** Returns          BOOLEAN
   1160 **
   1161 *******************************************************************************/
   1162 static BOOLEAN btif_hl_find_peer_mdep_id(UINT8 app_id, BD_ADDR bd_addr,
   1163                                          tBTA_HL_MDEP_ROLE local_mdep_role,
   1164                                          UINT16 data_type,
   1165                                          tBTA_HL_MDEP_ID *p_peer_mdep_id){
   1166     UINT8               app_idx, mcl_idx;
   1167     btif_hl_app_cb_t     *p_acb;
   1168     btif_hl_mcl_cb_t     *p_mcb;
   1169     tBTA_HL_SDP_REC     *p_rec;
   1170     UINT8               i, num_mdeps;
   1171     BOOLEAN             found = FALSE;
   1172     tBTA_HL_MDEP_ROLE   peer_mdep_role;
   1173 
   1174 
   1175     BTIF_TRACE_DEBUG4("%s app_id=%d local_mdep_role=%d, data_type=%d",
   1176                       __FUNCTION__, app_id, local_mdep_role, data_type);
   1177 
   1178     BTIF_TRACE_DEBUG6("DB [%02x:%02x:%02x:%02x:%02x:%02x]",
   1179                       bd_addr[0],  bd_addr[1],
   1180                       bd_addr[2],  bd_addr[3],
   1181                       bd_addr[4],  bd_addr[5]);
   1182 
   1183 
   1184     BTIF_TRACE_DEBUG1("local_mdep_role=%d", local_mdep_role);
   1185     BTIF_TRACE_DEBUG1("data_type=%d", data_type);
   1186 
   1187     if (local_mdep_role == BTA_HL_MDEP_ROLE_SINK)
   1188         peer_mdep_role = BTA_HL_MDEP_ROLE_SOURCE;
   1189     else
   1190         peer_mdep_role = BTA_HL_MDEP_ROLE_SINK;
   1191 
   1192     if (btif_hl_find_app_idx(app_id, &app_idx) )
   1193     {
   1194         p_acb  = BTIF_HL_GET_APP_CB_PTR(app_idx);
   1195         if (btif_hl_find_mcl_idx(app_idx, bd_addr, &mcl_idx))
   1196         {
   1197             p_mcb  =BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
   1198 
   1199             BTIF_TRACE_DEBUG2("app_idx=%d mcl_idx=%d",app_idx, mcl_idx);
   1200             BTIF_TRACE_DEBUG2("valid_spd_idx=%d sdp_idx=%d",p_mcb->valid_sdp_idx, p_mcb->sdp_idx);
   1201             if (p_mcb->valid_sdp_idx)
   1202             {
   1203                 p_rec = &p_mcb->sdp.sdp_rec[p_mcb->sdp_idx];
   1204                 num_mdeps = p_rec->num_mdeps;
   1205                 BTIF_TRACE_DEBUG1("num_mdeps=%d", num_mdeps);
   1206 
   1207                 for (i=0; i< num_mdeps; i++)
   1208                 {
   1209                     BTIF_TRACE_DEBUG2("p_rec->mdep_cfg[%d].mdep_role=%d",i, p_rec->mdep_cfg[i].mdep_role);
   1210                     BTIF_TRACE_DEBUG2("p_rec->mdep_cfg[%d].data_type =%d",i, p_rec->mdep_cfg[i].data_type );
   1211                     if ((p_rec->mdep_cfg[i].mdep_role == peer_mdep_role) &&
   1212                         (p_rec->mdep_cfg[i].data_type == data_type))
   1213                     {
   1214                         found = TRUE;
   1215                         *p_peer_mdep_id = p_rec->mdep_cfg[i].mdep_id;
   1216                         break;
   1217                     }
   1218                 }
   1219             }
   1220         }
   1221     }
   1222 
   1223     BTIF_TRACE_DEBUG2("found =%d  *p_peer_mdep_id=%d", found,  *p_peer_mdep_id);
   1224 
   1225     return found;
   1226 }
   1227 /*******************************************************************************
   1228 **
   1229 ** Function        btif_hl_find_local_mdep_id
   1230 **
   1231 ** Description      Find the local MDEP ID from the MDEP configuration
   1232 **
   1233 ** Returns          BOOLEAN
   1234 **
   1235 *******************************************************************************/
   1236 static BOOLEAN btif_hl_find_local_mdep_id(UINT8 app_id,
   1237                                           tBTA_HL_MDEP_ROLE local_mdep_role,
   1238                                           UINT16 mdep_data_type,
   1239                                           tBTA_HL_MDEP_ID *p_local_mdep_id){
   1240     UINT8 app_idx;
   1241     btif_hl_app_cb_t      *p_acb;
   1242     UINT8  i,j;
   1243     BOOLEAN found = FALSE;
   1244 
   1245     BTIF_TRACE_DEBUG1("%s", __FUNCTION__);
   1246 
   1247     if (btif_hl_find_app_idx(app_id, &app_idx) )
   1248     {
   1249         p_acb  =BTIF_HL_GET_APP_CB_PTR(app_idx);
   1250 
   1251         for (i=0; i< p_acb->sup_feature.num_of_mdeps; i++)
   1252         {
   1253             if (p_acb->sup_feature.mdep[i].mdep_cfg.mdep_role == local_mdep_role )
   1254             {
   1255                 for (j=0; j< p_acb->sup_feature.mdep[i].mdep_cfg.num_of_mdep_data_types; j++)
   1256                 {
   1257                     if ( p_acb->sup_feature.mdep[i].mdep_cfg.data_cfg[j].data_type == mdep_data_type)
   1258                     {
   1259                         found = TRUE;
   1260                         *p_local_mdep_id = p_acb->sup_feature.mdep[i].mdep_id;
   1261                         return found;
   1262                     }
   1263                 }
   1264             }
   1265         }
   1266 
   1267 
   1268     }
   1269     BTIF_TRACE_DEBUG2("found=%d local mdep id=%d", found, *p_local_mdep_id );
   1270     return found;
   1271 }
   1272 
   1273 /*******************************************************************************
   1274 **
   1275 ** Function      btif_hl_find_mdep_cfg_idx
   1276 **
   1277 ** Description  Find the MDEP configuration index using local MDEP_ID
   1278 **
   1279 ** Returns      BOOLEAN
   1280 **
   1281 *******************************************************************************/
   1282 static  BOOLEAN btif_hl_find_mdep_cfg_idx(UINT8 app_idx,  tBTA_HL_MDEP_ID local_mdep_id,
   1283                                           UINT8 *p_mdep_cfg_idx){
   1284     btif_hl_app_cb_t      *p_acb =BTIF_HL_GET_APP_CB_PTR(app_idx);
   1285     tBTA_HL_SUP_FEATURE     *p_sup_feature= &p_acb->sup_feature;
   1286     BOOLEAN found =FALSE;
   1287     UINT8 i;
   1288 
   1289     for (i=0; i< p_sup_feature->num_of_mdeps; i++)
   1290     {
   1291         BTIF_TRACE_DEBUG2("btif_hl_find_mdep_cfg_idx: mdep_id=%d app_idx = %d",
   1292                     p_sup_feature->mdep[i].mdep_id,app_idx);
   1293         if ( p_sup_feature->mdep[i].mdep_id == local_mdep_id)
   1294         {
   1295             found = TRUE;
   1296             *p_mdep_cfg_idx = i;
   1297             break;
   1298         }
   1299     }
   1300 
   1301     BTIF_TRACE_DEBUG5("%s found=%d mdep_idx=%d local_mdep_id=%d app_idx=%d ",
   1302                       __FUNCTION__, found,i, local_mdep_id,app_idx);
   1303     return found;
   1304 }
   1305 
   1306 
   1307 
   1308 /*******************************************************************************
   1309 **
   1310 ** Function      btif_hl_find_mcl_idx
   1311 **
   1312 ** Description  Find the MCL index using BD address
   1313 **
   1314 ** Returns      BOOLEAN
   1315 **
   1316 *******************************************************************************/
   1317 BOOLEAN btif_hl_find_mcl_idx(UINT8 app_idx, BD_ADDR p_bd_addr, UINT8 *p_mcl_idx){
   1318     BOOLEAN found=FALSE;
   1319     UINT8 i;
   1320     btif_hl_app_cb_t  *p_acb =BTIF_HL_GET_APP_CB_PTR(app_idx);
   1321     btif_hl_mcl_cb_t  *p_mcb;
   1322 
   1323     for (i=0; i < BTA_HL_NUM_MCLS ; i ++)
   1324     {
   1325         p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, i);
   1326         if (p_mcb->in_use &&
   1327             (!memcmp (p_mcb->bd_addr, p_bd_addr, BD_ADDR_LEN)))
   1328         {
   1329             found = TRUE;
   1330             *p_mcl_idx = i;
   1331             break;
   1332         }
   1333     }
   1334 
   1335 
   1336     BTIF_TRACE_DEBUG3("%s found=%d idx=%d",__FUNCTION__, found, i);
   1337     return found;
   1338 }
   1339 /*******************************************************************************
   1340 **
   1341 ** Function         btif_hl_init
   1342 **
   1343 ** Description      HL initialization function.
   1344 **
   1345 ** Returns          void
   1346 **
   1347 *******************************************************************************/
   1348 static void btif_hl_init(void){
   1349     BTIF_TRACE_DEBUG1("%s", __FUNCTION__);
   1350     memset(p_btif_hl_cb, 0, sizeof(btif_hl_cb_t));
   1351     btif_hl_init_next_app_id();
   1352     btif_hl_init_next_channel_id();
   1353 }
   1354 /*******************************************************************************
   1355 **
   1356 ** Function         btif_hl_disable
   1357 **
   1358 ** Description      Disable initialization function.
   1359 **
   1360 ** Returns          void
   1361 **
   1362 *******************************************************************************/
   1363 static void btif_hl_disable(void){
   1364     BTIF_TRACE_DEBUG1("%s", __FUNCTION__);
   1365 
   1366     if ((p_btif_hl_cb->state != BTIF_HL_STATE_DISABLING) &&
   1367         (p_btif_hl_cb->state != BTIF_HL_STATE_DISABLED))
   1368     {
   1369         btif_hl_set_state(BTIF_HL_STATE_DISABLING);
   1370         BTA_HlDisable();
   1371     }
   1372 }
   1373 /*******************************************************************************
   1374 **
   1375 ** Function      btif_hl_is_no_active_app
   1376 **
   1377 ** Description  Find whether or not  any APP is still in use
   1378 **
   1379 ** Returns      BOOLEAN
   1380 **
   1381 *******************************************************************************/
   1382 static BOOLEAN btif_hl_is_no_active_app(void){
   1383     BOOLEAN no_active_app = TRUE;
   1384     UINT8 i;
   1385 
   1386     for (i=0; i < BTA_HL_NUM_APPS ; i ++)
   1387     {
   1388         if (btif_hl_cb.acb[i].in_use)
   1389         {
   1390             no_active_app = FALSE;
   1391             break;
   1392         }
   1393     }
   1394 
   1395     BTIF_TRACE_DEBUG2("%s no_active_app=%d  ", __FUNCTION__, no_active_app );
   1396     return no_active_app;
   1397 }
   1398 
   1399 /*******************************************************************************
   1400 **
   1401 ** Function      btif_hl_free_app_idx
   1402 **
   1403 ** Description free an application control block
   1404 **
   1405 ** Returns      void
   1406 **
   1407 *******************************************************************************/
   1408 static void btif_hl_free_app_idx(UINT8 app_idx){
   1409 
   1410     if ((app_idx < BTA_HL_NUM_APPS) && btif_hl_cb.acb[app_idx].in_use )
   1411     {
   1412         btif_hl_cb.acb[app_idx].in_use = FALSE;
   1413         memset (&btif_hl_cb.acb[app_idx], 0, sizeof(btif_hl_app_cb_t));
   1414     }
   1415 }
   1416 /*******************************************************************************
   1417 **
   1418 ** Function      btif_hl_set_state
   1419 **
   1420 ** Description set HL state
   1421 **
   1422 ** Returns      void
   1423 **
   1424 *******************************************************************************/
   1425 static void btif_hl_set_state(btif_hl_state_t state){
   1426     BTIF_TRACE_DEBUG2("btif_hl_set_state:  %d ---> %d ", p_btif_hl_cb->state, state);
   1427     p_btif_hl_cb->state = state;
   1428 }
   1429 
   1430 /*******************************************************************************
   1431 **
   1432 ** Function      btif_hl_set_state
   1433 **
   1434 ** Description get HL state
   1435 **
   1436 ** Returns      btif_hl_state_t
   1437 **
   1438 *******************************************************************************/
   1439 
   1440 static btif_hl_state_t btif_hl_get_state(void){
   1441     BTIF_TRACE_DEBUG1("btif_hl_get_state:  %d   ", p_btif_hl_cb->state);
   1442     return p_btif_hl_cb->state;
   1443 }
   1444 
   1445 /*******************************************************************************
   1446 **
   1447 ** Function      btif_hl_find_data_type_idx
   1448 **
   1449 ** Description  Find the index in the data type table
   1450 **
   1451 ** Returns      BOOLEAN
   1452 **
   1453 *******************************************************************************/
   1454 static BOOLEAN  btif_hl_find_data_type_idx(UINT16 data_type, UINT8 *p_idx){
   1455     BOOLEAN found = FALSE;
   1456     UINT8 i;
   1457 
   1458     for (i=0; i< BTIF_HL_DATA_TABLE_SIZE; i++ )
   1459     {
   1460         if (data_type_table[i].data_type == data_type)
   1461         {
   1462             found = TRUE;
   1463             *p_idx= i;
   1464             break;
   1465         }
   1466     }
   1467 
   1468     BTIF_TRACE_DEBUG4("%s found=%d, data_type=0x%x idx=%d", __FUNCTION__, found, data_type, i);
   1469     return found;
   1470 }
   1471 
   1472 /*******************************************************************************
   1473 **
   1474 ** Function      btif_hl_get_max_tx_apdu_size
   1475 **
   1476 ** Description  Find the maximum TX APDU size for the specified data type and
   1477 **              MDEP role
   1478 **
   1479 ** Returns      UINT16
   1480 **
   1481 *******************************************************************************/
   1482 UINT16  btif_hl_get_max_tx_apdu_size(tBTA_HL_MDEP_ROLE mdep_role,
   1483                                      UINT16 data_type ){
   1484     UINT8 idx;
   1485     UINT16 max_tx_apdu_size =0;
   1486 
   1487     if (btif_hl_find_data_type_idx(data_type, &idx))
   1488     {
   1489         if (mdep_role == BTA_HL_MDEP_ROLE_SOURCE)
   1490         {
   1491             max_tx_apdu_size = data_type_table[idx].max_tx_apdu_size;
   1492         }
   1493         else
   1494         {
   1495             max_tx_apdu_size = data_type_table[idx].max_rx_apdu_size;
   1496         }
   1497     }
   1498     else
   1499     {
   1500         if (mdep_role == BTA_HL_MDEP_ROLE_SOURCE)
   1501         {
   1502             max_tx_apdu_size = BTIF_HL_DEFAULT_SRC_TX_APDU_SIZE;
   1503         }
   1504         else
   1505         {
   1506             max_tx_apdu_size = BTIF_HL_DEFAULT_SRC_RX_APDU_SIZE;
   1507         }
   1508 
   1509 
   1510     }
   1511 
   1512     BTIF_TRACE_DEBUG4("%s mdep_role=%d data_type=0x%4x size=%d",
   1513                       __FUNCTION__, mdep_role, data_type, max_tx_apdu_size);
   1514     return max_tx_apdu_size;
   1515 }
   1516 
   1517 
   1518 /*******************************************************************************
   1519 **
   1520 ** Function      btif_hl_get_max_rx_apdu_size
   1521 **
   1522 ** Description  Find the maximum RX APDU size for the specified data type and
   1523 **              MDEP role
   1524 **
   1525 ** Returns      UINT16
   1526 **
   1527 *******************************************************************************/
   1528 UINT16  btif_hl_get_max_rx_apdu_size(tBTA_HL_MDEP_ROLE mdep_role,
   1529                                      UINT16 data_type ){
   1530     UINT8  idx;
   1531     UINT16 max_rx_apdu_size =0;
   1532 
   1533     if (btif_hl_find_data_type_idx(data_type, &idx))
   1534     {
   1535         if (mdep_role == BTA_HL_MDEP_ROLE_SOURCE)
   1536         {
   1537             max_rx_apdu_size = data_type_table[idx].max_rx_apdu_size;
   1538         }
   1539         else
   1540         {
   1541             max_rx_apdu_size = data_type_table[idx].max_tx_apdu_size;
   1542         }
   1543     }
   1544     else
   1545     {
   1546         if (mdep_role == BTA_HL_MDEP_ROLE_SOURCE)
   1547         {
   1548             max_rx_apdu_size = BTIF_HL_DEFAULT_SRC_RX_APDU_SIZE;
   1549         }
   1550         else
   1551         {
   1552             max_rx_apdu_size = BTIF_HL_DEFAULT_SRC_TX_APDU_SIZE;
   1553         }
   1554     }
   1555 
   1556 
   1557     BTIF_TRACE_DEBUG4("%s mdep_role=%d data_type=0x%4x size=%d",
   1558                       __FUNCTION__, mdep_role, data_type, max_rx_apdu_size);
   1559 
   1560     return max_rx_apdu_size;
   1561 }
   1562 
   1563 /*******************************************************************************
   1564 **
   1565 ** Function      btif_hl_if_channel_setup_pending
   1566 **
   1567 ** Description
   1568 **
   1569 ** Returns      BOOLEAN
   1570 **
   1571 *******************************************************************************/
   1572 
   1573 static BOOLEAN btif_hl_get_bta_mdep_role(bthl_mdep_role_t mdep, tBTA_HL_MDEP_ROLE *p){
   1574     BOOLEAN status = TRUE;
   1575     switch (mdep)
   1576     {
   1577         case BTHL_MDEP_ROLE_SOURCE:
   1578             *p = BTA_HL_MDEP_ROLE_SOURCE;
   1579             break;
   1580         case BTHL_MDEP_ROLE_SINK:
   1581             *p = BTA_HL_MDEP_ROLE_SINK;
   1582             break;
   1583         default:
   1584             status = FALSE;
   1585             break;
   1586     }
   1587 
   1588     BTIF_TRACE_DEBUG4("%s status=%d bta_mdep_role=%d (%d:btif)",
   1589                       __FUNCTION__, status, *p, mdep);
   1590     return status;
   1591 }
   1592 /*******************************************************************************
   1593 **
   1594 ** Function btif_hl_get_bta_channel_type
   1595 **
   1596 ** Description convert bthl channel type to BTA DCH channel type
   1597 **
   1598 ** Returns BOOLEAN
   1599 **
   1600 *******************************************************************************/
   1601 
   1602 static BOOLEAN btif_hl_get_bta_channel_type(bthl_channel_type_t channel_type, tBTA_HL_DCH_CFG *p){
   1603     BOOLEAN status = TRUE;
   1604     switch (channel_type)
   1605     {
   1606         case BTHL_CHANNEL_TYPE_RELIABLE:
   1607             *p = BTA_HL_DCH_CFG_RELIABLE;
   1608             break;
   1609         case BTHL_CHANNEL_TYPE_STREAMING:
   1610             *p = BTA_HL_DCH_CFG_STREAMING;
   1611             break;
   1612         case BTHL_CHANNEL_TYPE_ANY:
   1613             *p = BTA_HL_DCH_CFG_NO_PREF;
   1614             break;
   1615         default:
   1616             status = FALSE;
   1617             break;
   1618     }
   1619     BTIF_TRACE_DEBUG3("%s status = %d BTA DCH CFG=%d (1-rel 2-strm",
   1620                       __FUNCTION__, status, *p);
   1621     return status;
   1622 }
   1623 /*******************************************************************************
   1624 **
   1625 ** Function btif_hl_get_next_app_id
   1626 **
   1627 ** Description get next applcation id
   1628 **
   1629 ** Returns UINT8
   1630 **
   1631 *******************************************************************************/
   1632 
   1633 static UINT8 btif_hl_get_next_app_id(){
   1634     UINT8 next_app_id = btif_hl_cb.next_app_id;
   1635 
   1636     btif_hl_cb.next_app_id++;
   1637     return next_app_id;
   1638 }
   1639 /*******************************************************************************
   1640 **
   1641 ** Function btif_hl_get_next_channel_id
   1642 **
   1643 ** Description get next channel id
   1644 **
   1645 ** Returns int
   1646 **
   1647 *******************************************************************************/
   1648 static int btif_hl_get_next_channel_id(UINT8 app_id){
   1649     UINT16 next_channel_id = btif_hl_cb.next_channel_id;
   1650     int channel_id;
   1651     btif_hl_cb.next_channel_id++;
   1652     channel_id = (app_id << 16) + next_channel_id;
   1653     BTIF_TRACE_DEBUG4("%s channel_id=0x%08x, app_id=0x%02x next_channel_id=0x%04x", __FUNCTION__,
   1654                       channel_id, app_id,  next_channel_id);
   1655     return channel_id;
   1656 }
   1657 /*******************************************************************************
   1658 **
   1659 ** Function btif_hl_get_app_id
   1660 **
   1661 ** Description get the applicaiton id associated with the channel id
   1662 **
   1663 ** Returns UINT8
   1664 **
   1665 *******************************************************************************/
   1666 
   1667 static UINT8 btif_hl_get_app_id(int channel_id){
   1668     UINT8 app_id =(UINT8) (channel_id >> 16);
   1669     BTIF_TRACE_DEBUG3("%s channel_id=0x%08x, app_id=0x%02x ", __FUNCTION__,channel_id, app_id);
   1670     return app_id;
   1671 }
   1672 /*******************************************************************************
   1673 **
   1674 ** Function btif_hl_init_next_app_id
   1675 **
   1676 ** Description initialize the application id
   1677 **
   1678 ** Returns void
   1679 **
   1680 *******************************************************************************/
   1681 static void btif_hl_init_next_app_id(void){
   1682     btif_hl_cb.next_app_id = 1;
   1683 }
   1684 /*******************************************************************************
   1685 **
   1686 ** Function btif_hl_init_next_channel_id
   1687 **
   1688 ** Description initialize the channel id
   1689 **
   1690 ** Returns void
   1691 **
   1692 *******************************************************************************/
   1693 static void btif_hl_init_next_channel_id(void){
   1694     btif_hl_cb.next_channel_id = 1;
   1695 }
   1696 
   1697 
   1698 /*******************************************************************************
   1699 **
   1700 ** Function      btif_hl_find_app_idx_using_handle
   1701 **
   1702 ** Description  Find the applicaiton index using handle
   1703 **
   1704 ** Returns      BOOLEAN
   1705 **
   1706 *******************************************************************************/
   1707 BOOLEAN btif_hl_find_app_idx_using_handle(tBTA_HL_APP_HANDLE app_handle,
   1708                                           UINT8 *p_app_idx){
   1709     BOOLEAN found=FALSE;
   1710     UINT8 i;
   1711 
   1712     for (i=0; i < BTA_HL_NUM_APPS ; i ++)
   1713     {
   1714         if (btif_hl_cb.acb[i].in_use &&
   1715             (btif_hl_cb.acb[i].app_handle == app_handle))
   1716         {
   1717             found = TRUE;
   1718             *p_app_idx = i;
   1719             break;
   1720         }
   1721     }
   1722 
   1723     BTIF_TRACE_EVENT4("%s status=%d handle=%d app_idx=%d ",
   1724                       __FUNCTION__, found, app_handle , i);
   1725 
   1726     return found;
   1727 }
   1728 
   1729 /*******************************************************************************
   1730 **
   1731 ** Function      btif_hl_find_app_idx_using_app_id
   1732 **
   1733 ** Description  Find the applicaiton index using app_id
   1734 **
   1735 ** Returns      BOOLEAN
   1736 **
   1737 *******************************************************************************/
   1738 BOOLEAN btif_hl_find_app_idx_using_app_id(UINT8 app_id,
   1739                                           UINT8 *p_app_idx){
   1740     BOOLEAN found=FALSE;
   1741     UINT8 i;
   1742 
   1743     for (i=0; i < BTA_HL_NUM_APPS ; i ++)
   1744     {
   1745         if (btif_hl_cb.acb[i].in_use &&
   1746             (btif_hl_cb.acb[i].app_id == app_id))
   1747         {
   1748             found = TRUE;
   1749             *p_app_idx = i;
   1750             break;
   1751         }
   1752     }
   1753 
   1754     BTIF_TRACE_EVENT4("%s found=%d app_id=%d app_idx=%d ",
   1755                       __FUNCTION__, found, app_id , i);
   1756 
   1757     return found;
   1758 }
   1759 
   1760 /*******************************************************************************
   1761 **
   1762 ** Function      btif_hl_find_mcl_idx_using_handle
   1763 **
   1764 ** Description  Find the MCL index using handle
   1765 **
   1766 ** Returns      BOOLEAN
   1767 **
   1768 *******************************************************************************/
   1769 BOOLEAN btif_hl_find_mcl_idx_using_handle( tBTA_HL_MCL_HANDLE mcl_handle,
   1770                                            UINT8 *p_app_idx, UINT8 *p_mcl_idx){
   1771     btif_hl_app_cb_t  *p_acb;
   1772     BOOLEAN         found=FALSE;
   1773     UINT8 i,j;
   1774 
   1775     for (i=0; i<BTA_HL_NUM_APPS; i++)
   1776     {
   1777         p_acb =BTIF_HL_GET_APP_CB_PTR(i);
   1778         for (j=0; j < BTA_HL_NUM_MCLS ; j++)
   1779         {
   1780             if (p_acb->mcb[j].in_use)
   1781                 BTIF_TRACE_DEBUG3("btif_hl_find_mcl_idx_using_handle:app_idx=%d,"
   1782                 "mcl_idx =%d mcl_handle=%d",i,j,p_acb->mcb[j].mcl_handle);
   1783             if (p_acb->mcb[j].in_use &&
   1784                 (p_acb->mcb[j].mcl_handle == mcl_handle))
   1785             {
   1786                 found = TRUE;
   1787                 *p_app_idx = i;
   1788                 *p_mcl_idx = j;
   1789                 break;
   1790             }
   1791         }
   1792     }
   1793     BTIF_TRACE_DEBUG4("%s found=%d app_idx=%d mcl_idx=%d",__FUNCTION__,
   1794                       found, i, j);
   1795     return found;
   1796 }
   1797 
   1798 /*******************************************************************************
   1799 **
   1800 ** Function      btif_hl_find_mdl_idx_using_mdl_id
   1801 **
   1802 ** Description  Find the mdl index using mdl_id
   1803 **
   1804 ** Returns      BOOLEAN
   1805 **
   1806 *******************************************************************************/
   1807 BOOLEAN btif_hl_find_mcl_idx_using_mdl_id( UINT8 mdl_id,UINT8 mcl_handle,
   1808                                            UINT8 *p_app_idx, UINT8 *p_mcl_idx){
   1809     btif_hl_app_cb_t  *p_acb;
   1810     btif_hl_mcl_cb_t  *p_mcb;
   1811     BOOLEAN         found=FALSE;
   1812     UINT8 i,j,x;
   1813 
   1814     for (i=0; i<BTA_HL_NUM_APPS; i++)
   1815     {
   1816         p_acb =BTIF_HL_GET_APP_CB_PTR(i);
   1817         for (j=0; j < BTA_HL_NUM_MCLS ; j++)
   1818         {
   1819             if (p_acb->mcb[j].in_use &&
   1820                 (p_acb->mcb[j].mcl_handle == mcl_handle))
   1821             {
   1822                     p_mcb = &p_acb->mcb[j];
   1823                     BTIF_TRACE_DEBUG1("btif_hl_find_mcl_idx_using_mdl_id: mcl handle found j =%d",j);
   1824                     for (x=0; x < BTA_HL_NUM_MDLS_PER_MCL ; x ++)
   1825                     {
   1826                         if (p_mcb->mdl[x].in_use && p_mcb->mdl[x].mdl_id == mdl_id)
   1827                         {
   1828                             BTIF_TRACE_DEBUG1("btif_hl_find_mcl_idx_using_mdl_id:found x =%d",x);
   1829                             found = TRUE;
   1830                             *p_app_idx = i;
   1831                             *p_mcl_idx = j;
   1832                             break;
   1833                         }
   1834                     }
   1835             }
   1836         }
   1837     }
   1838     BTIF_TRACE_DEBUG4("%s found=%d app_idx=%d mcl_idx=%d",__FUNCTION__,
   1839                       found, i, j);
   1840     return found;
   1841 }
   1842 
   1843 /*******************************************************************************
   1844 **
   1845 ** Function      btif_hl_find_mcl_idx_using_deleted_mdl_id
   1846 **
   1847 ** Description  Find the app index deleted_mdl_id
   1848 **
   1849 ** Returns      BOOLEAN
   1850 **
   1851 *******************************************************************************/
   1852 BOOLEAN btif_hl_find_app_idx_using_deleted_mdl_id( UINT8 mdl_id,
   1853                                            UINT8 *p_app_idx){
   1854     btif_hl_app_cb_t  *p_acb;
   1855     BOOLEAN         found=FALSE;
   1856     UINT8 i,j;
   1857 
   1858     for (i=0; i<BTA_HL_NUM_APPS; i++)
   1859     {
   1860         p_acb =BTIF_HL_GET_APP_CB_PTR(i);
   1861         if (p_acb->delete_mdl.active)
   1862                 BTIF_TRACE_DEBUG3("btif_hl_find_app_idx_using_deleted_mdl_id: app_idx=%d,"
   1863                     "mcl_idx =%d mcl_handle=%d",i,j,p_acb->mcb[j].mcl_handle);
   1864             if (p_acb->delete_mdl.active &&
   1865                 (p_acb->delete_mdl.mdl_id == mdl_id))
   1866             {
   1867                 found = TRUE;
   1868                 *p_app_idx = i;
   1869                 break;
   1870             }
   1871     }
   1872     BTIF_TRACE_DEBUG3("%s found=%d app_idx=%d",__FUNCTION__,
   1873                       found, i);
   1874     return found;
   1875 }
   1876 
   1877 /*******************************************************************************
   1878 **
   1879 ** Function      btif_hl_stop_timer_using_handle
   1880 **
   1881 ** Description  clean control channel cb using handle
   1882 **
   1883 ** Returns      void
   1884 **
   1885 *******************************************************************************/
   1886 static void btif_hl_stop_timer_using_handle( tBTA_HL_MCL_HANDLE mcl_handle){
   1887     btif_hl_app_cb_t  *p_acb;
   1888     BOOLEAN         found=FALSE;
   1889     UINT8 i,j;
   1890 
   1891     for (i=0; i<BTA_HL_NUM_APPS; i++)
   1892     {
   1893         p_acb =BTIF_HL_GET_APP_CB_PTR(i);
   1894         for (j=0; j < BTA_HL_NUM_MCLS ; j++)
   1895         {
   1896             if (p_acb->mcb[j].in_use &&
   1897                 (p_acb->mcb[j].mcl_handle == mcl_handle))
   1898             {
   1899                 btif_hl_stop_cch_timer(i, j);
   1900             }
   1901         }
   1902     }
   1903 }
   1904 
   1905 /*******************************************************************************
   1906 **
   1907 ** Function      btif_hl_find_mcl_idx_using_app_idx
   1908 **
   1909 ** Description  Find the MCL index using handle
   1910 **
   1911 ** Returns      BOOLEAN
   1912 **
   1913 *******************************************************************************/
   1914 BOOLEAN btif_hl_find_mcl_idx_using_app_idx( tBTA_HL_MCL_HANDLE mcl_handle,
   1915                                            UINT8 p_app_idx, UINT8 *p_mcl_idx){
   1916     btif_hl_app_cb_t  *p_acb;
   1917     BOOLEAN         found=FALSE;
   1918     UINT8 i,j;
   1919 
   1920     p_acb =BTIF_HL_GET_APP_CB_PTR(p_app_idx);
   1921     for (j=0; j < BTA_HL_NUM_MCLS ; j++)
   1922     {
   1923         if (p_acb->mcb[j].in_use &&
   1924             (p_acb->mcb[j].mcl_handle == mcl_handle))
   1925         {
   1926             found = TRUE;
   1927             *p_mcl_idx = j;
   1928             break;
   1929         }
   1930     }
   1931     BTIF_TRACE_DEBUG3("%s found=%dmcl_idx=%d",__FUNCTION__,
   1932                       found, j);
   1933     return found;
   1934 }
   1935 
   1936 /*******************************************************************************
   1937 **
   1938 ** Function      btif_hl_clean_mdls_using_app_idx
   1939 **
   1940 ** Description  clean dch cpntrol bloack using app_idx
   1941 **
   1942 ** Returns      void
   1943 **
   1944 *******************************************************************************/
   1945 void btif_hl_clean_mdls_using_app_idx( UINT8 app_idx){
   1946     btif_hl_app_cb_t  *p_acb;
   1947     btif_hl_mcl_cb_t  *p_mcb;
   1948     btif_hl_mdl_cb_t  *p_dcb;
   1949     UINT8 i,j,x,y;
   1950     bt_bdaddr_t     bd_addr;
   1951 
   1952         p_acb =BTIF_HL_GET_APP_CB_PTR(app_idx);
   1953         for (j=0; j < BTA_HL_NUM_MCLS ; j++)
   1954         {
   1955             if (p_acb->mcb[j].in_use)
   1956             {
   1957                     p_mcb = &p_acb->mcb[j];
   1958                     BTIF_TRACE_DEBUG1("btif_hl_find_mcl_idx_using_mdl_id: mcl handle found j =%d",j);
   1959                     for (x=0; x < BTA_HL_NUM_MDLS_PER_MCL ; x ++)
   1960                     {
   1961                         if (p_mcb->mdl[x].in_use)
   1962                         {
   1963                             p_dcb = BTIF_HL_GET_MDL_CB_PTR(app_idx, j,x);
   1964                             btif_hl_release_socket(app_idx,j,x);
   1965                             for (y=0; y<6; y++)
   1966                             {
   1967                                 bd_addr.address[y] = p_mcb->bd_addr[y];
   1968                             }
   1969                             BTIF_HL_CALL_CBACK(bt_hl_callbacks, channel_state_cb,  p_acb->app_id,
   1970                                                &bd_addr, p_dcb->local_mdep_cfg_idx,
   1971                                                p_dcb->channel_id, BTHL_CONN_STATE_DISCONNECTED, 0 );
   1972                             btif_hl_clean_mdl_cb(p_dcb);
   1973                             if (!btif_hl_num_dchs_in_use(p_mcb->mcl_handle))
   1974                                     BTA_HlCchClose(p_mcb->mcl_handle);
   1975                             BTIF_TRACE_DEBUG1("remote DCH close success mdl_idx=%d", x);
   1976                         }
   1977                     }
   1978             }
   1979         }
   1980 }
   1981 
   1982 /*******************************************************************************
   1983 **
   1984 ** Function      btif_hl_find_app_idx
   1985 **
   1986 ** Description  Find the application index using application ID
   1987 **
   1988 ** Returns      BOOLEAN
   1989 **
   1990 *******************************************************************************/
   1991 BOOLEAN btif_hl_find_app_idx(UINT8 app_id, UINT8 *p_app_idx){
   1992     BOOLEAN found=FALSE;
   1993     UINT8 i;
   1994 
   1995     for (i=0; i < BTA_HL_NUM_APPS ; i ++)
   1996     {
   1997 
   1998         if (btif_hl_cb.acb[i].in_use &&
   1999             (btif_hl_cb.acb[i].app_id == app_id))
   2000         {
   2001             found = TRUE;
   2002             *p_app_idx = i;
   2003             break;
   2004         }
   2005     }
   2006     BTIF_TRACE_DEBUG3("%s found=%d app_idx=%d", __FUNCTION__, found, i );
   2007 
   2008     return found;
   2009 }
   2010 
   2011 /*******************************************************************************
   2012 **
   2013 ** Function      btif_hl_find_app_idx
   2014 **
   2015 ** Description  Find the application index using application ID
   2016 **
   2017 ** Returns      BOOLEAN
   2018 **
   2019 *******************************************************************************/
   2020 BOOLEAN btif_hl_find_app_idx_using_mdepId(UINT8 mdep_id, UINT8 *p_app_idx){
   2021     BOOLEAN found=FALSE;
   2022     UINT8 i;
   2023 
   2024     for (i=0; i < BTA_HL_NUM_APPS ; i ++)
   2025     {
   2026         BTIF_TRACE_DEBUG1("btif_hl_find_app_idx_using_mdepId: MDEP-ID = %d",
   2027                 btif_hl_cb.acb[i].sup_feature.mdep[0].mdep_id);
   2028         if (btif_hl_cb.acb[i].in_use &&
   2029             (btif_hl_cb.acb[i].sup_feature.mdep[0].mdep_id == mdep_id))
   2030         {
   2031             found = TRUE;
   2032             *p_app_idx = i;
   2033             break;
   2034         }
   2035     }
   2036     BTIF_TRACE_DEBUG3("%s found=%d app_idx=%d", __FUNCTION__, found, i );
   2037 
   2038     return found;
   2039 }
   2040 
   2041 /*******************************************************************************
   2042 **
   2043 ** Function      btif_hl_find_avail_mdl_idx
   2044 **
   2045 ** Description  Find a not in-use MDL index
   2046 **
   2047 ** Returns      BOOLEAN
   2048 **
   2049 *******************************************************************************/
   2050 BOOLEAN btif_hl_find_avail_mdl_idx(UINT8 app_idx, UINT8 mcl_idx,
   2051                                    UINT8 *p_mdl_idx){
   2052     btif_hl_mcl_cb_t      *p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
   2053     BOOLEAN found=FALSE;
   2054     UINT8 i;
   2055 
   2056     for (i=0; i < BTA_HL_NUM_MDLS_PER_MCL ; i ++)
   2057     {
   2058         if (!p_mcb->mdl[i].in_use)
   2059         {
   2060             btif_hl_clean_mdl_cb(&p_mcb->mdl[i]);
   2061             found = TRUE;
   2062             *p_mdl_idx = i;
   2063             break;
   2064         }
   2065     }
   2066 
   2067     BTIF_TRACE_DEBUG3("%s found=%d idx=%d",__FUNCTION__, found, i);
   2068     return found;
   2069 }
   2070 
   2071 /*******************************************************************************
   2072 **
   2073 ** Function      btif_hl_find_avail_mcl_idx
   2074 **
   2075 ** Description  Find a not in-use MDL index
   2076 **
   2077 ** Returns      BOOLEAN
   2078 **
   2079 *******************************************************************************/
   2080 BOOLEAN btif_hl_find_avail_mcl_idx(UINT8 app_idx, UINT8 *p_mcl_idx){
   2081     BOOLEAN found=FALSE;
   2082     UINT8 i;
   2083 
   2084     for (i=0; i < BTA_HL_NUM_MCLS ; i ++)
   2085     {
   2086         if (!btif_hl_cb.acb[app_idx].mcb[i].in_use)
   2087         {
   2088             found = TRUE;
   2089             *p_mcl_idx = i;
   2090             break;
   2091         }
   2092     }
   2093     BTIF_TRACE_DEBUG3("%s found=%d mcl_idx=%d", __FUNCTION__, found, i);
   2094     return found;
   2095 }
   2096 
   2097 /*******************************************************************************
   2098 **
   2099 ** Function      btif_hl_find_avail_app_idx
   2100 **
   2101 ** Description  Find a not in-use APP index
   2102 **
   2103 ** Returns      BOOLEAN
   2104 **
   2105 *******************************************************************************/
   2106 static BOOLEAN btif_hl_find_avail_app_idx(UINT8 *p_idx){
   2107     BOOLEAN found = FALSE;
   2108     UINT8 i;
   2109 
   2110     for (i=0; i < BTA_HL_NUM_APPS ; i ++)
   2111     {
   2112         if (!btif_hl_cb.acb[i].in_use)
   2113         {
   2114             found = TRUE;
   2115             *p_idx = i;
   2116             break;
   2117         }
   2118     }
   2119 
   2120     BTIF_TRACE_DEBUG3("%s found=%d app_idx=%d", __FUNCTION__, found, i);
   2121     return found;
   2122 }
   2123 
   2124 
   2125 /*******************************************************************************
   2126 **
   2127 ** Function         btif_hl_proc_dereg_cfm
   2128 **
   2129 ** Description      Process the de-registration confirmation
   2130 **
   2131 ** Returns          Nothing
   2132 **
   2133 *******************************************************************************/
   2134 static void btif_hl_proc_dereg_cfm(tBTA_HL *p_data)
   2135 
   2136 {
   2137     btif_hl_app_cb_t        *p_acb;
   2138     UINT8                   app_idx;
   2139     int                     app_id = 0;
   2140     bthl_app_reg_state_t    state = BTHL_APP_REG_STATE_DEREG_SUCCESS;
   2141     bt_status_t             status            = BT_STATUS_SUCCESS;
   2142 
   2143     BTIF_TRACE_DEBUG3("%s de-reg status=%d app_handle=%d", __FUNCTION__,
   2144                 p_data->dereg_cfm.status, p_data->dereg_cfm.app_handle);
   2145 
   2146     if (btif_hl_find_app_idx_using_app_id(p_data->dereg_cfm.app_id, &app_idx))
   2147     {
   2148         p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx);
   2149         app_id = (int) p_acb->app_id;
   2150         if (p_data->dereg_cfm.status == BTA_HL_STATUS_OK)
   2151         {
   2152             btif_hl_clean_mdls_using_app_idx(app_idx);
   2153             memset(p_acb, 0,sizeof(btif_hl_app_cb_t));
   2154         }
   2155         else
   2156             state = BTHL_APP_REG_STATE_DEREG_FAILED;
   2157 
   2158         BTIF_TRACE_DEBUG2("call reg state callback app_id=%d state=%d", app_id, state);
   2159         BTIF_HL_CALL_CBACK(bt_hl_callbacks, app_reg_state_cb, app_id, state );
   2160 
   2161         if (btif_hl_is_no_active_app())
   2162         {
   2163             btif_hl_disable();
   2164         }
   2165     }
   2166 }
   2167 
   2168 /*******************************************************************************
   2169 **
   2170 ** Function         btif_hl_proc_reg_cfm
   2171 **
   2172 ** Description      Process the registration confirmation
   2173 **
   2174 ** Returns          Nothing
   2175 **
   2176 *******************************************************************************/
   2177 static void btif_hl_proc_reg_cfm(tBTA_HL *p_data){
   2178     btif_hl_app_cb_t       *p_acb;
   2179     UINT8                  app_idx;
   2180     bthl_app_reg_state_t   state = BTHL_APP_REG_STATE_REG_SUCCESS;
   2181     bt_status_t            bt_status;
   2182 
   2183     BTIF_TRACE_DEBUG3("%s reg status=%d app_handle=%d", __FUNCTION__, p_data->reg_cfm.status, p_data->reg_cfm.app_handle);
   2184 
   2185     if (btif_hl_find_app_idx(p_data->reg_cfm.app_id, &app_idx))
   2186     {
   2187         p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx);
   2188         if (p_data->reg_cfm.status == BTA_HL_STATUS_OK)
   2189         {
   2190             p_acb->app_handle = p_data->reg_cfm.app_handle;
   2191         }
   2192         else
   2193         {
   2194             btif_hl_free_app_idx(app_idx);
   2195             reg_counter--;
   2196             state = BTHL_APP_REG_STATE_REG_FAILED;
   2197         }
   2198 
   2199         BTIF_TRACE_DEBUG3("%s call reg state callback app_id=%d reg state=%d", __FUNCTION__,  p_data->reg_cfm.app_id, state);
   2200         BTIF_HL_CALL_CBACK(bt_hl_callbacks, app_reg_state_cb, ((int) p_data->reg_cfm.app_id), state );
   2201     }
   2202 }
   2203 
   2204 /*******************************************************************************
   2205 **
   2206 ** Function         btif_hl_proc_sdp_info_ind
   2207 **
   2208 ** Description      Process the SDP info indication
   2209 **
   2210 ** Returns          Nothing
   2211 **
   2212 *******************************************************************************/
   2213 static void btif_hl_proc_sdp_info_ind(tBTA_HL *p_data)
   2214 
   2215 {
   2216     btif_hl_app_cb_t         *p_acb;
   2217     UINT8                   app_idx;
   2218 
   2219     BTIF_TRACE_DEBUG1("%s", __FUNCTION__);
   2220     if (btif_hl_find_app_idx_using_handle(p_data->sdp_info_ind.app_handle, &app_idx))
   2221     {
   2222         p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx);
   2223         memcpy(&p_acb->sdp_info_ind, &p_data->sdp_info_ind, sizeof(tBTA_HL_SDP_INFO_IND));
   2224     }
   2225 }
   2226 /*******************************************************************************
   2227 **
   2228 ** Function btif_hl_set_chan_cb_state
   2229 **
   2230 ** Description set the channel callback state
   2231 **
   2232 ** Returns void
   2233 **
   2234 *******************************************************************************/
   2235 void btif_hl_set_chan_cb_state(UINT8 app_idx, UINT8 mcl_idx, btif_hl_chan_cb_state_t state){
   2236     btif_hl_pending_chan_cb_t   *p_pcb = BTIF_HL_GET_PCB_PTR(app_idx, mcl_idx);
   2237     btif_hl_chan_cb_state_t cur_state = p_pcb->cb_state;
   2238 
   2239     if (cur_state != state)
   2240     {
   2241         p_pcb->cb_state = state;
   2242         BTIF_TRACE_DEBUG3("%s state %d--->%d",__FUNCTION__, cur_state, state);
   2243     }
   2244 
   2245 
   2246 }
   2247 /*******************************************************************************
   2248 **
   2249 ** Function btif_hl_send_destroyed_cb
   2250 **
   2251 ** Description send the channel destroyed callback
   2252 **
   2253 ** Returns void
   2254 **
   2255 *******************************************************************************/
   2256 void btif_hl_send_destroyed_cb(btif_hl_app_cb_t        *p_acb ){
   2257     bt_bdaddr_t     bd_addr;
   2258     int             app_id = (int) btif_hl_get_app_id(p_acb->delete_mdl.channel_id);
   2259 
   2260     btif_hl_copy_bda(&bd_addr, p_acb->delete_mdl.bd_addr);
   2261     BTIF_TRACE_DEBUG1("%s",__FUNCTION__);
   2262     BTIF_TRACE_DEBUG4("call channel state callback channel_id=0x%08x mdep_cfg_idx=%d, state=%d fd=%d",p_acb->delete_mdl.channel_id,
   2263                       p_acb->delete_mdl.mdep_cfg_idx, BTHL_CONN_STATE_DESTROYED, 0);
   2264     btif_hl_display_bt_bda(&bd_addr);
   2265 
   2266     BTIF_HL_CALL_CBACK(bt_hl_callbacks, channel_state_cb,  app_id,
   2267                        &bd_addr, p_acb->delete_mdl.mdep_cfg_idx,
   2268                        p_acb->delete_mdl.channel_id, BTHL_CONN_STATE_DESTROYED, 0 );
   2269 }
   2270 /*******************************************************************************
   2271 **
   2272 ** Function btif_hl_send_disconnecting_cb
   2273 **
   2274 ** Description send a channel disconnecting callback
   2275 **
   2276 ** Returns void
   2277 **
   2278 *******************************************************************************/
   2279 void btif_hl_send_disconnecting_cb(UINT8 app_idx, UINT8 mcl_idx, UINT8 mdl_idx){
   2280     btif_hl_mdl_cb_t        *p_dcb = BTIF_HL_GET_MDL_CB_PTR( app_idx,  mcl_idx, mdl_idx);
   2281     btif_hl_soc_cb_t        *p_scb = p_dcb->p_scb;
   2282     bt_bdaddr_t             bd_addr;
   2283     int                     app_id = (int) btif_hl_get_app_id(p_scb->channel_id);
   2284 
   2285     btif_hl_copy_bda(&bd_addr, p_scb->bd_addr);
   2286 
   2287     BTIF_TRACE_DEBUG1("%s",__FUNCTION__);
   2288     BTIF_TRACE_DEBUG4("call channel state callback  channel_id=0x%08x mdep_cfg_idx=%d, state=%d fd=%d",p_scb->channel_id,
   2289                       p_scb->mdep_cfg_idx, BTHL_CONN_STATE_DISCONNECTING, p_scb->socket_id[0]);
   2290     btif_hl_display_bt_bda(&bd_addr);
   2291     BTIF_HL_CALL_CBACK(bt_hl_callbacks, channel_state_cb,  app_id,
   2292                        &bd_addr, p_scb->mdep_cfg_idx,
   2293                        p_scb->channel_id, BTHL_CONN_STATE_DISCONNECTING, p_scb->socket_id[0] );
   2294 }
   2295 /*******************************************************************************
   2296 **
   2297 ** Function btif_hl_send_setup_connecting_cb
   2298 **
   2299 ** Description send a channel connecting callback
   2300 **
   2301 ** Returns void
   2302 **
   2303 *******************************************************************************/
   2304 void btif_hl_send_setup_connecting_cb(UINT8 app_idx, UINT8 mcl_idx){
   2305     btif_hl_pending_chan_cb_t   *p_pcb = BTIF_HL_GET_PCB_PTR(app_idx, mcl_idx);
   2306     bt_bdaddr_t                 bd_addr;
   2307     int                         app_id = (int) btif_hl_get_app_id(p_pcb->channel_id);
   2308 
   2309     btif_hl_copy_bda(&bd_addr, p_pcb->bd_addr);
   2310 
   2311     if (p_pcb->in_use && p_pcb->cb_state == BTIF_HL_CHAN_CB_STATE_CONNECTING_PENDING)
   2312     {
   2313         BTIF_TRACE_DEBUG1("%s",__FUNCTION__);
   2314         BTIF_TRACE_DEBUG4("call channel state callback  channel_id=0x%08x mdep_cfg_idx=%d state=%d fd=%d",p_pcb->channel_id,
   2315                           p_pcb->mdep_cfg_idx, BTHL_CONN_STATE_CONNECTING, 0);
   2316         btif_hl_display_bt_bda(&bd_addr);
   2317 
   2318         BTIF_HL_CALL_CBACK(bt_hl_callbacks, channel_state_cb, app_id,
   2319                            &bd_addr, p_pcb->mdep_cfg_idx,
   2320                            p_pcb->channel_id, BTHL_CONN_STATE_CONNECTING, 0 );
   2321         btif_hl_set_chan_cb_state(app_idx, mcl_idx, BTIF_HL_CHAN_CB_STATE_CONNECTED_PENDING);
   2322     }
   2323 }
   2324 /*******************************************************************************
   2325 **
   2326 ** Function btif_hl_send_setup_disconnected_cb
   2327 **
   2328 ** Description send a channel disconnected callback
   2329 **
   2330 ** Returns void
   2331 **
   2332 *******************************************************************************/
   2333 void btif_hl_send_setup_disconnected_cb(UINT8 app_idx, UINT8 mcl_idx){
   2334     btif_hl_pending_chan_cb_t   *p_pcb = BTIF_HL_GET_PCB_PTR(app_idx, mcl_idx);
   2335     bt_bdaddr_t                 bd_addr;
   2336     int                         app_id = (int) btif_hl_get_app_id(p_pcb->channel_id);
   2337 
   2338     btif_hl_copy_bda(&bd_addr, p_pcb->bd_addr);
   2339 
   2340     BTIF_TRACE_DEBUG2("%s p_pcb->in_use=%d",__FUNCTION__, p_pcb->in_use);
   2341     if (p_pcb->in_use)
   2342     {
   2343         BTIF_TRACE_DEBUG1("%p_pcb->cb_state=%d",p_pcb->cb_state);
   2344         if (p_pcb->cb_state == BTIF_HL_CHAN_CB_STATE_CONNECTING_PENDING)
   2345         {
   2346             BTIF_TRACE_DEBUG4("call channel state callback  channel_id=0x%08x mdep_cfg_idx=%d state=%d fd=%d",p_pcb->channel_id,
   2347                               p_pcb->mdep_cfg_idx, BTHL_CONN_STATE_CONNECTING, 0);
   2348             btif_hl_display_bt_bda(&bd_addr);
   2349             BTIF_HL_CALL_CBACK(bt_hl_callbacks, channel_state_cb, app_id,
   2350                                &bd_addr, p_pcb->mdep_cfg_idx,
   2351                                p_pcb->channel_id, BTHL_CONN_STATE_CONNECTING, 0 );
   2352 
   2353             BTIF_TRACE_DEBUG4("call channel state callback  channel_id=0x%08x mdep_cfg_idx=%d state=%d fd=%d",p_pcb->channel_id,
   2354                               p_pcb->mdep_cfg_idx, BTHL_CONN_STATE_DISCONNECTED, 0);
   2355             btif_hl_display_bt_bda(&bd_addr);
   2356             BTIF_HL_CALL_CBACK(bt_hl_callbacks, channel_state_cb, app_id,
   2357                                &bd_addr, p_pcb->mdep_cfg_idx,
   2358                                p_pcb->channel_id, BTHL_CONN_STATE_DISCONNECTED, 0 );
   2359         }
   2360         else if (p_pcb->cb_state == BTIF_HL_CHAN_CB_STATE_CONNECTED_PENDING)
   2361         {
   2362             BTIF_TRACE_DEBUG4("call channel state callback  channel_id=0x%08x mdep_cfg_idx=%d state=%d fd=%d",p_pcb->channel_id,
   2363                               p_pcb->mdep_cfg_idx, BTHL_CONN_STATE_DISCONNECTED, 0);
   2364             btif_hl_display_bt_bda(&bd_addr);
   2365             BTIF_HL_CALL_CBACK(bt_hl_callbacks, channel_state_cb,  app_id,
   2366                                &bd_addr, p_pcb->mdep_cfg_idx,
   2367                                p_pcb->channel_id, BTHL_CONN_STATE_DISCONNECTED, 0 );
   2368         }
   2369         btif_hl_clean_pcb(p_pcb);
   2370     }
   2371 }
   2372 /*******************************************************************************
   2373 **
   2374 ** Function         btif_hl_proc_sdp_query_cfm
   2375 **
   2376 ** Description      Process the SDP query confirmation
   2377 **
   2378 ** Returns          Nothing
   2379 **
   2380 *******************************************************************************/
   2381 static BOOLEAN btif_hl_proc_sdp_query_cfm(tBTA_HL *p_data){
   2382     btif_hl_app_cb_t                *p_acb;
   2383     btif_hl_mcl_cb_t                *p_mcb;
   2384     tBTA_HL_SDP                     *p_sdp;
   2385     tBTA_HL_CCH_OPEN_PARAM          open_param;
   2386     UINT8                           app_idx, mcl_idx, sdp_idx = 0;
   2387     UINT8                           num_recs, i, num_mdeps, j;
   2388     btif_hl_cch_op_t                old_cch_oper;
   2389     BOOLEAN                         status =FALSE;
   2390     btif_hl_pending_chan_cb_t     *p_pcb;
   2391 
   2392     BTIF_TRACE_DEBUG1("%s", __FUNCTION__);
   2393 
   2394     p_sdp = p_data->sdp_query_cfm.p_sdp;
   2395     num_recs = p_sdp->num_recs;
   2396 
   2397     BTIF_TRACE_DEBUG1("num of SDP records=%d",num_recs);
   2398     for (i=0; i<num_recs; i++)
   2399     {
   2400         BTIF_TRACE_DEBUG3("rec_idx=%d ctrl_psm=0x%x data_psm=0x%x",
   2401                           (i+1),p_sdp->sdp_rec[i].ctrl_psm, p_sdp->sdp_rec[i].data_psm);
   2402         BTIF_TRACE_DEBUG1("MCAP supported procedures=0x%x",p_sdp->sdp_rec[i].mcap_sup_proc);
   2403         num_mdeps = p_sdp->sdp_rec[i].num_mdeps;
   2404         BTIF_TRACE_DEBUG1("num of mdeps =%d",num_mdeps);
   2405         for (j=0; j< num_mdeps; j++)
   2406         {
   2407             BTIF_TRACE_DEBUG4("mdep_idx=%d mdep_id=0x%x data_type=0x%x mdep_role=0x%x",
   2408                               (j+1),
   2409                               p_sdp->sdp_rec[i].mdep_cfg[j].mdep_id,
   2410                               p_sdp->sdp_rec[i].mdep_cfg[j].data_type,
   2411                               p_sdp->sdp_rec[i].mdep_cfg[j].mdep_role );
   2412         }
   2413     }
   2414 
   2415         if (btif_hl_find_app_idx_using_app_id(p_data->sdp_query_cfm.app_id, &app_idx))
   2416         {
   2417             p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx);
   2418 
   2419             if (btif_hl_find_mcl_idx(app_idx, p_data->sdp_query_cfm.bd_addr, &mcl_idx))
   2420             {
   2421                 p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
   2422                 if (p_mcb->cch_oper != BTIF_HL_CCH_OP_NONE)
   2423                 {
   2424                     memcpy(&p_mcb->sdp, p_sdp, sizeof(tBTA_HL_SDP));
   2425                     old_cch_oper = p_mcb->cch_oper;
   2426                     p_mcb->cch_oper = BTIF_HL_CCH_OP_NONE;
   2427 
   2428                     switch (old_cch_oper)
   2429                     {
   2430                         case BTIF_HL_CCH_OP_MDEP_FILTERING:
   2431                             status = btif_hl_find_sdp_idx_using_mdep_filter(app_idx,
   2432                                                                     mcl_idx, &sdp_idx);
   2433                             break;
   2434                         default:
   2435                             break;
   2436                     }
   2437 
   2438                     if (status)
   2439                     {
   2440                         p_mcb->sdp_idx       = sdp_idx;
   2441                         p_mcb->valid_sdp_idx = TRUE;
   2442                         p_mcb->ctrl_psm      = p_mcb->sdp.sdp_rec[sdp_idx].ctrl_psm;
   2443 
   2444                         switch (old_cch_oper)
   2445                         {
   2446                             case BTIF_HL_CCH_OP_MDEP_FILTERING:
   2447                                 p_pcb = BTIF_HL_GET_PCB_PTR(app_idx, mcl_idx);
   2448                                 if (p_pcb->in_use)
   2449                                 {
   2450                                     if (!p_pcb->abort_pending)
   2451                                     {
   2452                                         switch (p_pcb->op)
   2453                                         {
   2454                                             case BTIF_HL_PEND_DCH_OP_OPEN:
   2455                                                 btif_hl_send_setup_connecting_cb(app_idx, mcl_idx);
   2456                                                 break;
   2457                                             case BTIF_HL_PEND_DCH_OP_DELETE_MDL:
   2458                                             default:
   2459                                                 break;
   2460                                         }
   2461                                         open_param.ctrl_psm = p_mcb->ctrl_psm;
   2462                                         bdcpy(open_param.bd_addr, p_mcb->bd_addr);
   2463                                         open_param.sec_mask =
   2464                                                 (BTA_SEC_AUTHENTICATE | BTA_SEC_ENCRYPT);
   2465                                         BTA_HlCchOpen(p_acb->app_id,p_acb->app_handle, &open_param);
   2466                                     }
   2467                                     else
   2468                                     {
   2469                                         BTIF_TRACE_DEBUG0("channel abort pending");
   2470                                     }
   2471                                 }
   2472                                 break;
   2473 
   2474                             case BTIF_HL_CCH_OP_DCH_OPEN:
   2475                                 status = btif_hl_proc_pending_op(app_idx,mcl_idx);
   2476                                 break;
   2477 
   2478                             default:
   2479                                 BTIF_TRACE_ERROR1("Invalid CCH oper %d", old_cch_oper);
   2480                                 break;
   2481                         }
   2482                     }
   2483                     else
   2484                     {
   2485                         BTIF_TRACE_ERROR0("Can not find SDP idx discard CCH Open request");
   2486                     }
   2487                 }
   2488             }
   2489         }
   2490     return status;
   2491 }
   2492 
   2493 
   2494 /*******************************************************************************
   2495 **
   2496 ** Function         btif_hl_proc_cch_open_ind
   2497 **
   2498 ** Description      Process the CCH open indication
   2499 **
   2500 ** Returns          Nothing
   2501 **
   2502 *******************************************************************************/
   2503 static void btif_hl_proc_cch_open_ind(tBTA_HL *p_data)
   2504 
   2505 {
   2506     btif_hl_mcl_cb_t         *p_mcb;
   2507     UINT8                   app_idx, mcl_idx;
   2508     int                     i;
   2509 
   2510     BTIF_TRACE_DEBUG1("%s", __FUNCTION__);
   2511     for(i=0; i<BTA_HL_NUM_APPS; i++)
   2512     {
   2513         if (btif_hl_cb.acb[i].in_use)
   2514         {
   2515             if (!btif_hl_find_mcl_idx(i, p_data->cch_open_ind.bd_addr, &mcl_idx))
   2516             {
   2517                 if (btif_hl_find_avail_mcl_idx(i, &mcl_idx))
   2518                 {
   2519                     p_mcb = BTIF_HL_GET_MCL_CB_PTR(i, mcl_idx);
   2520                     memset(p_mcb, 0, sizeof(btif_hl_mcl_cb_t));
   2521                     p_mcb->in_use = TRUE;
   2522                     p_mcb->is_connected = TRUE;
   2523                     p_mcb->mcl_handle = p_data->cch_open_ind.mcl_handle;
   2524                     bdcpy(p_mcb->bd_addr, p_data->cch_open_ind.bd_addr);
   2525                     btif_hl_start_cch_timer(i, mcl_idx);
   2526                 }
   2527             }
   2528             else
   2529             {
   2530                 BTIF_TRACE_ERROR0("The MCL already exist for cch_open_ind");
   2531             }
   2532         }
   2533     }
   2534 }
   2535 
   2536 /*******************************************************************************
   2537 **
   2538 ** Function         btif_hl_proc_pending_op
   2539 **
   2540 ** Description      Process the pending dch operation.
   2541 **
   2542 ** Returns          Nothing
   2543 **
   2544 *******************************************************************************/
   2545 BOOLEAN btif_hl_proc_pending_op(UINT8 app_idx, UINT8 mcl_idx)
   2546 
   2547 {
   2548 
   2549     btif_hl_app_cb_t            *p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx);
   2550     btif_hl_mcl_cb_t            *p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
   2551     btif_hl_pending_chan_cb_t   *p_pcb;
   2552     BOOLEAN                     status = FALSE;
   2553     tBTA_HL_DCH_OPEN_PARAM      dch_open;
   2554     tBTA_HL_MDL_ID              mdl_id;
   2555     tBTA_HL_DCH_RECONNECT_PARAM reconnect_param;
   2556 
   2557     p_pcb = BTIF_HL_GET_PCB_PTR(app_idx, mcl_idx);
   2558     if (p_pcb->in_use)
   2559     {
   2560         switch (p_pcb->op)
   2561         {
   2562             case BTIF_HL_PEND_DCH_OP_OPEN:
   2563                 if (!p_pcb->abort_pending)
   2564                 {
   2565                     BTIF_TRACE_DEBUG0("op BTIF_HL_PEND_DCH_OP_OPEN");
   2566                     dch_open.ctrl_psm = p_mcb->ctrl_psm;
   2567                     dch_open.local_mdep_id = p_acb->sup_feature.mdep[p_pcb->mdep_cfg_idx].mdep_id;
   2568                     if (btif_hl_find_peer_mdep_id(p_acb->app_id, p_mcb->bd_addr,
   2569                                                   p_acb->sup_feature.mdep[p_pcb->mdep_cfg_idx].mdep_cfg.mdep_role,
   2570                                                   p_acb->sup_feature.mdep[p_pcb->mdep_cfg_idx].mdep_cfg.data_cfg[0].data_type, &dch_open.peer_mdep_id ))
   2571                     {
   2572                         dch_open.local_cfg = p_acb->channel_type[p_pcb->mdep_cfg_idx];
   2573                         if ((p_acb->sup_feature.mdep[p_pcb->mdep_cfg_idx].mdep_cfg.mdep_role == BTA_HL_MDEP_ROLE_SOURCE)
   2574                             && !btif_hl_is_the_first_reliable_existed(app_idx, mcl_idx))
   2575                         {
   2576                             dch_open.local_cfg = BTA_HL_DCH_CFG_RELIABLE;
   2577                         }
   2578                         dch_open.sec_mask = (BTA_SEC_AUTHENTICATE | BTA_SEC_ENCRYPT);
   2579                         BTIF_TRACE_DEBUG1("dch_open.local_cfg=%d  ", dch_open.local_cfg);
   2580                         btif_hl_send_setup_connecting_cb(app_idx,mcl_idx);
   2581 
   2582                         if (!btif_hl_is_reconnect_possible(app_idx, mcl_idx, p_pcb->mdep_cfg_idx, &dch_open, &mdl_id ))
   2583                         {
   2584                             BTIF_TRACE_DEBUG1("Issue DCH open, mcl_handle=%d",p_mcb->mcl_handle);
   2585                             BTA_HlDchOpen(p_mcb->mcl_handle, &dch_open);
   2586                         }
   2587                         else
   2588                         {
   2589                             reconnect_param.ctrl_psm = p_mcb->ctrl_psm;
   2590                             reconnect_param.mdl_id = mdl_id;;
   2591                             BTIF_TRACE_DEBUG2("Issue Reconnect ctrl_psm=0x%x mdl_id=0x%x",reconnect_param.ctrl_psm, reconnect_param.mdl_id);
   2592                             BTA_HlDchReconnect(p_mcb->mcl_handle, &reconnect_param);
   2593                         }
   2594                         status = TRUE;
   2595                     }
   2596                 }
   2597                 else
   2598                 {
   2599                     btif_hl_send_setup_disconnected_cb(app_idx, mcl_idx);
   2600                     status = TRUE;
   2601                 }
   2602                 break;
   2603             case BTIF_HL_PEND_DCH_OP_DELETE_MDL:
   2604                 BTA_HlDeleteMdl(p_mcb->mcl_handle, p_acb->delete_mdl.mdl_id);
   2605                 status = TRUE;
   2606                 break;
   2607 
   2608             default:
   2609                 break;
   2610         }
   2611     }
   2612     return status;
   2613 }
   2614 
   2615 /*******************************************************************************
   2616 **
   2617 ** Function         btif_hl_proc_cch_open_cfm
   2618 **
   2619 ** Description      Process the CCH open confirmation
   2620 **
   2621 ** Returns          Nothing
   2622 **
   2623 *******************************************************************************/
   2624 static BOOLEAN btif_hl_proc_cch_open_cfm(tBTA_HL *p_data)
   2625 
   2626 {
   2627     btif_hl_app_cb_t         *p_acb;
   2628     btif_hl_mcl_cb_t         *p_mcb;
   2629     UINT8                    app_idx, mcl_idx;
   2630     BOOLEAN                  status = FALSE;
   2631     tBTA_HL_DCH_OPEN_PARAM   dch_open;
   2632 
   2633 
   2634     BTIF_TRACE_DEBUG1("%s", __FUNCTION__);
   2635 
   2636     if (btif_hl_find_app_idx_using_app_id(p_data->cch_open_cfm.app_id, &app_idx))
   2637     {
   2638         BTIF_TRACE_DEBUG1("app_idx=%d", app_idx);
   2639         if (btif_hl_find_mcl_idx(app_idx, p_data->cch_open_cfm.bd_addr, &mcl_idx))
   2640         {
   2641             p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx);
   2642 
   2643             p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
   2644             BTIF_TRACE_DEBUG2("mcl_idx=%d, mcl_handle=%d", mcl_idx,p_data->cch_open_cfm.mcl_handle);
   2645             p_mcb->mcl_handle = p_data->cch_open_cfm.mcl_handle;
   2646             p_mcb->is_connected = TRUE;
   2647             status = btif_hl_proc_pending_op(app_idx, mcl_idx);
   2648             if (status)
   2649                 btif_hl_start_cch_timer(app_idx, mcl_idx);
   2650         }
   2651     }
   2652 
   2653     return status;
   2654 }
   2655 
   2656 /*******************************************************************************
   2657 **
   2658 ** Function      btif_hl_clean_mcb_using_handle
   2659 **
   2660 ** Description  clean control channel cb using handle
   2661 **
   2662 ** Returns      void
   2663 **
   2664 *******************************************************************************/
   2665 static void btif_hl_clean_mcb_using_handle( tBTA_HL_MCL_HANDLE mcl_handle){
   2666     btif_hl_app_cb_t  *p_acb;
   2667     UINT8 i,j;
   2668 
   2669     for (i=0; i<BTA_HL_NUM_APPS; i++)
   2670     {
   2671         p_acb =BTIF_HL_GET_APP_CB_PTR(i);
   2672         for (j=0; j < BTA_HL_NUM_MCLS ; j++)
   2673         {
   2674             if (p_acb->mcb[j].in_use)
   2675                 BTIF_TRACE_DEBUG3("btif_hl_find_mcl_idx_using_handle: app_idx=%d,"
   2676                     "mcl_idx =%d mcl_handle=%d",i,j,p_acb->mcb[j].mcl_handle);
   2677             if (p_acb->mcb[j].in_use &&
   2678                 (p_acb->mcb[j].mcl_handle == mcl_handle))
   2679             {
   2680                 btif_hl_stop_cch_timer(i, j);
   2681                 btif_hl_release_mcl_sockets(i, j);
   2682                 btif_hl_send_setup_disconnected_cb(i, j);
   2683                 btif_hl_clean_mcl_cb(i, j);
   2684             }
   2685         }
   2686     }
   2687 }
   2688 
   2689 /*******************************************************************************
   2690 **
   2691 ** Function         btif_hl_proc_cch_close_ind
   2692 **
   2693 ** Description      Process the CCH close indication
   2694 **
   2695 ** Returns          Nothing
   2696 **
   2697 *******************************************************************************/
   2698 static void btif_hl_proc_cch_close_ind(tBTA_HL *p_data)
   2699 
   2700 {
   2701     UINT8                   app_idx, mcl_idx;
   2702     BTIF_TRACE_DEBUG1("%s", __FUNCTION__);
   2703 
   2704     btif_hl_clean_mcb_using_handle(p_data->cch_close_ind.mcl_handle);
   2705 }
   2706 
   2707 
   2708 /*******************************************************************************
   2709 **
   2710 ** Function         btif_hl_proc_cch_close_cfm
   2711 **
   2712 ** Description      Process the CCH close confirmation
   2713 **
   2714 ** Returns          Nothing
   2715 **
   2716 *******************************************************************************/
   2717 static void btif_hl_proc_cch_close_cfm(tBTA_HL *p_data)
   2718 {
   2719     UINT8                   app_idx, mcl_idx;
   2720     BTIF_TRACE_DEBUG1("%s", __FUNCTION__);
   2721 
   2722     btif_hl_clean_mcb_using_handle(p_data->cch_close_ind.mcl_handle);
   2723 }
   2724 
   2725 /*******************************************************************************
   2726 **
   2727 ** Function         btif_hl_proc_create_ind
   2728 **
   2729 ** Description      Process the MDL create indication
   2730 **
   2731 ** Returns          Nothing
   2732 **
   2733 *******************************************************************************/
   2734 static void btif_hl_proc_create_ind(tBTA_HL *p_data){
   2735     btif_hl_app_cb_t         *p_acb;
   2736     btif_hl_mcl_cb_t         *p_mcb;
   2737     tBTA_HL_MDEP            *p_mdep;
   2738     UINT8                   app_idx, orig_app_idx, mcl_idx, mdep_cfg_idx;
   2739     BOOLEAN                 first_reliable_exist;
   2740     BOOLEAN                 success = TRUE;
   2741     tBTA_HL_DCH_CFG         rsp_cfg = BTA_HL_DCH_CFG_UNKNOWN;
   2742     tBTA_HL_DCH_CREATE_RSP  rsp_code = BTA_HL_DCH_CREATE_RSP_CFG_REJ;
   2743     tBTA_HL_DCH_CREATE_RSP_PARAM create_rsp_param;
   2744 
   2745     BTIF_TRACE_DEBUG1("%s", __FUNCTION__);
   2746 
   2747 // Find the correct app_idx based on the mdep_id;
   2748     btif_hl_find_app_idx_using_mdepId(p_data->dch_create_ind.local_mdep_id,&orig_app_idx);
   2749     if (btif_hl_find_mcl_idx(orig_app_idx, p_data->dch_create_ind.bd_addr, &mcl_idx))
   2750     {
   2751         p_acb =BTIF_HL_GET_APP_CB_PTR(orig_app_idx);
   2752         p_mcb =BTIF_HL_GET_MCL_CB_PTR(orig_app_idx, mcl_idx);
   2753 
   2754         if (btif_hl_find_mdep_cfg_idx(orig_app_idx, p_data->dch_create_ind.local_mdep_id, &mdep_cfg_idx))
   2755         {
   2756             p_mdep = &(p_acb->sup_feature.mdep[mdep_cfg_idx]);
   2757             first_reliable_exist = btif_hl_is_the_first_reliable_existed(orig_app_idx, mcl_idx);
   2758             switch (p_mdep->mdep_cfg.mdep_role)
   2759             {
   2760                 case BTA_HL_MDEP_ROLE_SOURCE:
   2761                     if (p_data->dch_create_ind.cfg == BTA_HL_DCH_CFG_NO_PREF)
   2762                     {
   2763                         if (first_reliable_exist)
   2764                         {
   2765                             rsp_cfg = p_acb->channel_type[mdep_cfg_idx];
   2766                         }
   2767                         else
   2768                         {
   2769                             rsp_cfg = BTA_HL_DCH_CFG_RELIABLE;
   2770                         }
   2771                         rsp_code = BTA_HL_DCH_CREATE_RSP_SUCCESS;
   2772                     }
   2773 
   2774                     break;
   2775                 case BTA_HL_MDEP_ROLE_SINK:
   2776 
   2777                     BTIF_TRACE_DEBUG0("btif_hl_proc_create_ind:BTA_HL_MDEP_ROLE_SINK");
   2778                     if ((p_data->dch_create_ind.cfg  == BTA_HL_DCH_CFG_RELIABLE) ||
   2779                         (first_reliable_exist && (p_data->dch_create_ind.cfg  == BTA_HL_DCH_CFG_STREAMING)))
   2780                     {
   2781                         rsp_code = BTA_HL_DCH_CREATE_RSP_SUCCESS;
   2782                         rsp_cfg = p_data->dch_create_ind.cfg;
   2783                         BTIF_TRACE_DEBUG1("btif_hl_proc_create_ind:BTA_HL_MDEP_ROLE_SINK cfg = %d",rsp_cfg);
   2784                     }
   2785                     break;
   2786                 default:
   2787                     break;
   2788             }
   2789         }
   2790     }
   2791     else
   2792     {
   2793         success = FALSE;
   2794     }
   2795 
   2796     if (success)
   2797     {
   2798         BTIF_TRACE_DEBUG2("create response rsp_code=%d rsp_cfg=%d", rsp_code, rsp_cfg );
   2799         create_rsp_param.local_mdep_id = p_data->dch_create_ind.local_mdep_id;
   2800         create_rsp_param.mdl_id = p_data->dch_create_ind.mdl_id;
   2801         create_rsp_param.rsp_code = rsp_code;
   2802         create_rsp_param.cfg_rsp = rsp_cfg;
   2803         BTA_HlDchCreateRsp(p_mcb->mcl_handle, &create_rsp_param);
   2804     }
   2805 }
   2806 
   2807 /*******************************************************************************
   2808 **
   2809 ** Function         btif_hl_proc_dch_open_ind
   2810 **
   2811 ** Description      Process the DCH open indication
   2812 **
   2813 ** Returns          Nothing
   2814 **
   2815 *******************************************************************************/
   2816 static void btif_hl_proc_dch_open_ind(tBTA_HL *p_data)
   2817 
   2818 {
   2819     btif_hl_app_cb_t         *p_acb;
   2820     btif_hl_mcl_cb_t         *p_mcb;
   2821     btif_hl_mdl_cb_t         *p_dcb;
   2822     UINT8                    app_idx,orig_app_idx, mcl_idx, mdl_idx, mdep_cfg_idx;
   2823     UINT8                    dc_cfg;
   2824     BOOLEAN close_dch = FALSE;
   2825 
   2826     BTIF_TRACE_DEBUG1("%s", __FUNCTION__);
   2827 
   2828     // Find the correct app_idx based on the mdep_id;
   2829     btif_hl_find_app_idx_using_mdepId(p_data->dch_open_ind.local_mdep_id,&orig_app_idx);
   2830 
   2831     if (btif_hl_find_mcl_idx_using_app_idx(p_data->dch_open_ind.mcl_handle, orig_app_idx, &mcl_idx ))
   2832     {
   2833         p_acb =BTIF_HL_GET_APP_CB_PTR(orig_app_idx);
   2834         p_mcb =BTIF_HL_GET_MCL_CB_PTR(orig_app_idx, mcl_idx);
   2835 
   2836         if (btif_hl_find_avail_mdl_idx(orig_app_idx, mcl_idx, &mdl_idx))
   2837         {
   2838             p_dcb = BTIF_HL_GET_MDL_CB_PTR(orig_app_idx, mcl_idx, mdl_idx);
   2839 
   2840             if (btif_hl_find_mdep_cfg_idx(orig_app_idx, p_data->dch_open_ind.local_mdep_id, &mdep_cfg_idx))
   2841             {
   2842                 p_dcb->in_use               = TRUE;
   2843                 p_dcb->mdl_handle           =  p_data->dch_open_ind.mdl_handle;
   2844                 p_dcb->local_mdep_cfg_idx   = mdep_cfg_idx;
   2845                 p_dcb->local_mdep_id        = p_data->dch_open_ind.local_mdep_id;
   2846                 p_dcb->mdl_id               = p_data->dch_open_ind.mdl_id;
   2847                 p_dcb->dch_mode             = p_data->dch_open_ind.dch_mode;
   2848                 p_dcb->dch_mode             = p_data->dch_open_ind.dch_mode;
   2849                 p_dcb->is_the_first_reliable = p_data->dch_open_ind.first_reliable;
   2850                 p_dcb->mtu                  = p_data->dch_open_ind.mtu;
   2851 
   2852                 if(btif_hl_find_channel_id_using_mdl_id(orig_app_idx,p_dcb->mdl_id , &p_dcb->channel_id))
   2853                 {
   2854                     BTIF_TRACE_DEBUG4(" app_idx=%d mcl_idx=%d mdl_idx=%d channel_id=%d",
   2855                                         app_idx, mcl_idx, mdl_idx, p_dcb->channel_id  );
   2856                     if (!btif_hl_create_socket(orig_app_idx, mcl_idx, mdl_idx))
   2857                     {
   2858                         BTIF_TRACE_ERROR0("Unable to create socket");
   2859                         close_dch = TRUE;
   2860                     }
   2861                 }
   2862                 else
   2863                 {
   2864                     BTIF_TRACE_ERROR1("Unable find channel id for mdl_id=0x%x", p_dcb->mdl_id  );
   2865                     close_dch = TRUE;
   2866                 }
   2867             }
   2868             else
   2869             {
   2870                 BTIF_TRACE_ERROR1("INVALID_LOCAL_MDEP_ID mdep_id=%d",p_data->dch_open_cfm.local_mdep_id);
   2871                 close_dch = TRUE;
   2872             }
   2873 
   2874             if (close_dch)
   2875                 btif_hl_clean_mdl_cb(p_dcb);
   2876         }
   2877         else
   2878             close_dch = TRUE;
   2879     }
   2880     else
   2881         close_dch = TRUE;
   2882 
   2883     if (close_dch)
   2884         BTA_HlDchClose(p_data->dch_open_cfm.mdl_handle);
   2885 }
   2886 
   2887 /*******************************************************************************
   2888 **
   2889 ** Function         btif_hl_proc_dch_open_cfm
   2890 **
   2891 ** Description      Process the DCH close confirmation
   2892 **
   2893 ** Returns          Nothing
   2894 **
   2895 *******************************************************************************/
   2896 static BOOLEAN btif_hl_proc_dch_open_cfm(tBTA_HL *p_data)
   2897 
   2898 {
   2899     btif_hl_app_cb_t            *p_acb;
   2900     btif_hl_mcl_cb_t            *p_mcb;
   2901     btif_hl_mdl_cb_t            *p_dcb;
   2902     btif_hl_pending_chan_cb_t   *p_pcb;
   2903     UINT8                    app_idx, mcl_idx, mdl_idx, mdep_cfg_idx;
   2904     BOOLEAN                  status = FALSE;
   2905     BOOLEAN                  close_dch = FALSE;
   2906 
   2907     BTIF_TRACE_DEBUG1("%s", __FUNCTION__);
   2908 
   2909     // Find the correct app_idx based on the mdep_id;
   2910     btif_hl_find_app_idx_using_mdepId(p_data->dch_open_cfm.local_mdep_id,&app_idx);
   2911 
   2912     if (btif_hl_find_mcl_idx_using_app_idx(p_data->dch_open_cfm.mcl_handle, app_idx, &mcl_idx ))
   2913     {
   2914         p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx);
   2915         p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
   2916         p_pcb = BTIF_HL_GET_PCB_PTR(app_idx, mcl_idx);
   2917 
   2918         if (btif_hl_find_avail_mdl_idx(app_idx, mcl_idx, &mdl_idx))
   2919         {
   2920             p_dcb = BTIF_HL_GET_MDL_CB_PTR(app_idx, mcl_idx, mdl_idx);
   2921 
   2922             if (btif_hl_find_mdep_cfg_idx(app_idx, p_data->dch_open_cfm.local_mdep_id, &mdep_cfg_idx))
   2923             {
   2924                 p_dcb->in_use               = TRUE;
   2925                 p_dcb->mdl_handle           = p_data->dch_open_cfm.mdl_handle;
   2926                 p_dcb->local_mdep_cfg_idx   = mdep_cfg_idx;
   2927                 p_dcb->local_mdep_id        = p_data->dch_open_cfm.local_mdep_id;
   2928                 p_dcb->mdl_id               = p_data->dch_open_cfm.mdl_id;
   2929                 p_dcb->dch_mode             = p_data->dch_open_cfm.dch_mode;
   2930                 p_dcb->is_the_first_reliable= p_data->dch_open_cfm.first_reliable;
   2931                 p_dcb->mtu                  = p_data->dch_open_cfm.mtu;
   2932                 p_dcb->channel_id           = p_pcb->channel_id;
   2933 
   2934                 BTIF_TRACE_DEBUG3("app_idx=%d mcl_idx=%d mdl_idx=%d",  app_idx, mcl_idx, mdl_idx  );
   2935                 btif_hl_send_setup_connecting_cb(app_idx, mcl_idx);
   2936                 if (btif_hl_create_socket(app_idx, mcl_idx, mdl_idx))
   2937                 {
   2938                     status = TRUE;
   2939                     BTIF_TRACE_DEBUG4("app_idx=%d mcl_idx=%d mdl_idx=%d p_dcb->channel_id=0x%08x",
   2940                                       app_idx, mcl_idx, mdl_idx, p_dcb->channel_id);
   2941                     btif_hl_clean_pcb(p_pcb);
   2942                 }
   2943                 else
   2944                 {
   2945                     BTIF_TRACE_ERROR0("Unable to create socket");
   2946                     close_dch = TRUE;
   2947                 }
   2948             }
   2949             else
   2950             {
   2951                 BTIF_TRACE_ERROR1("INVALID_LOCAL_MDEP_ID mdep_id=%d",p_data->dch_open_cfm.local_mdep_id);
   2952                 close_dch = TRUE;
   2953             }
   2954 
   2955             if (close_dch)
   2956             {
   2957                 btif_hl_clean_mdl_cb(p_dcb);
   2958                 BTA_HlDchClose(p_data->dch_open_cfm.mdl_handle);
   2959             }
   2960         }
   2961     }
   2962 
   2963     return status;
   2964 }
   2965 /*******************************************************************************
   2966 **
   2967 ** Function         btif_hl_proc_dch_reconnect_cfm
   2968 **
   2969 ** Description      Process the DCH reconnect indication
   2970 **
   2971 ** Returns          Nothing
   2972 **
   2973 *******************************************************************************/
   2974 static BOOLEAN btif_hl_proc_dch_reconnect_cfm(tBTA_HL *p_data)
   2975 {
   2976     btif_hl_app_cb_t            *p_acb;
   2977     btif_hl_mcl_cb_t            *p_mcb;
   2978     btif_hl_mdl_cb_t            *p_dcb;
   2979     btif_hl_pending_chan_cb_t   *p_pcb;
   2980     UINT8                    app_idx, mcl_idx, mdl_idx, mdep_cfg_idx;
   2981     BOOLEAN                  status = FALSE;
   2982     BOOLEAN                  close_dch = FALSE;
   2983 
   2984     BTIF_TRACE_DEBUG1("%s", __FUNCTION__);
   2985 
   2986     btif_hl_find_app_idx_using_mdepId(p_data->dch_reconnect_cfm.local_mdep_id,&app_idx);
   2987 
   2988     if (btif_hl_find_mcl_idx_using_app_idx(p_data->dch_reconnect_cfm.mcl_handle, app_idx, &mcl_idx ))
   2989     {
   2990         p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx);
   2991         p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
   2992         p_pcb = BTIF_HL_GET_PCB_PTR(app_idx, mcl_idx);
   2993 
   2994         if (btif_hl_find_avail_mdl_idx(app_idx, mcl_idx, &mdl_idx))
   2995         {
   2996             p_dcb = BTIF_HL_GET_MDL_CB_PTR(app_idx, mcl_idx, mdl_idx);
   2997 
   2998             if (btif_hl_find_mdep_cfg_idx(app_idx, p_data->dch_reconnect_cfm.local_mdep_id, &mdep_cfg_idx))
   2999             {
   3000                 p_dcb->in_use               = TRUE;
   3001                 p_dcb->mdl_handle           = p_data->dch_reconnect_cfm.mdl_handle;
   3002                 p_dcb->local_mdep_cfg_idx   = mdep_cfg_idx;
   3003                 p_dcb->local_mdep_id        = p_data->dch_reconnect_cfm.local_mdep_id;
   3004                 p_dcb->mdl_id               = p_data->dch_reconnect_cfm.mdl_id;
   3005                 p_dcb->dch_mode             = p_data->dch_reconnect_cfm.dch_mode;
   3006                 p_dcb->is_the_first_reliable= p_data->dch_reconnect_cfm.first_reliable;
   3007                 p_dcb->mtu                  = p_data->dch_reconnect_cfm.mtu;
   3008                 p_dcb->channel_id           = p_pcb->channel_id;
   3009 
   3010                 BTIF_TRACE_DEBUG3("app_idx=%d mcl_idx=%d mdl_idx=%d",  app_idx, mcl_idx, mdl_idx  );
   3011                 btif_hl_send_setup_connecting_cb(app_idx, mcl_idx);
   3012                 if (btif_hl_create_socket(app_idx, mcl_idx, mdl_idx))
   3013                 {
   3014                     status = TRUE;
   3015                     BTIF_TRACE_DEBUG4("app_idx=%d mcl_idx=%d mdl_idx=%d p_dcb->channel_id=0x%08x",
   3016                                       app_idx, mcl_idx, mdl_idx, p_dcb->channel_id);
   3017                     btif_hl_clean_pcb(p_pcb);
   3018                 }
   3019                 else
   3020                 {
   3021                     BTIF_TRACE_ERROR0("Unable to create socket");
   3022                     close_dch = TRUE;
   3023                 }
   3024             }
   3025             else
   3026             {
   3027                 BTIF_TRACE_ERROR1("INVALID_LOCAL_MDEP_ID mdep_id=%d",p_data->dch_open_cfm.local_mdep_id);
   3028                 close_dch = TRUE;
   3029             }
   3030 
   3031             if (close_dch)
   3032             {
   3033                 btif_hl_clean_mdl_cb(p_dcb);
   3034                 BTA_HlDchClose(p_data->dch_reconnect_cfm.mdl_handle);
   3035             }
   3036         }
   3037     }
   3038 
   3039     return status;
   3040 
   3041 }
   3042 /*******************************************************************************
   3043 **
   3044 ** Function         btif_hl_proc_dch_reconnect_ind
   3045 **
   3046 ** Description      Process the DCH reconnect indication
   3047 **
   3048 ** Returns          Nothing
   3049 **
   3050 *******************************************************************************/
   3051 static void btif_hl_proc_dch_reconnect_ind(tBTA_HL *p_data)
   3052 
   3053 {
   3054     btif_hl_app_cb_t        *p_acb;
   3055     btif_hl_mcl_cb_t        *p_mcb;
   3056     btif_hl_mdl_cb_t        *p_dcb;
   3057     UINT8                   app_idx, mcl_idx, mdl_idx, mdep_cfg_idx, dc_cfg;
   3058     BOOLEAN                 close_dch = FALSE;
   3059 
   3060     BTIF_TRACE_DEBUG1("%s", __FUNCTION__);
   3061 
   3062     // Find the correct app_idx based on the mdep_id;
   3063     btif_hl_find_app_idx_using_mdepId(p_data->dch_reconnect_ind.local_mdep_id,&app_idx);
   3064 
   3065     if (btif_hl_find_mcl_idx_using_app_idx(p_data->dch_reconnect_ind.mcl_handle, app_idx, &mcl_idx ))
   3066     {
   3067         p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx);
   3068         BTIF_TRACE_DEBUG2("btif_hl_proc_dch_reconnect_ind: app_idx = %d, mcl_idx = %d",
   3069                                 app_idx, mcl_idx);
   3070         p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
   3071 
   3072         if (btif_hl_find_avail_mdl_idx(app_idx, mcl_idx, &mdl_idx))
   3073         {
   3074             p_dcb =BTIF_HL_GET_MDL_CB_PTR(app_idx, mcl_idx, mdl_idx);
   3075 
   3076             if (btif_hl_find_mdep_cfg_idx(app_idx, p_data->dch_reconnect_ind.local_mdep_id, &mdep_cfg_idx))
   3077             {
   3078                 p_dcb->in_use               = TRUE;
   3079                 p_dcb->mdl_handle           = p_data->dch_reconnect_ind.mdl_handle;
   3080                 p_dcb->local_mdep_cfg_idx   = mdep_cfg_idx;
   3081                 p_dcb->local_mdep_id        = p_data->dch_reconnect_ind.local_mdep_id;
   3082                 p_dcb->mdl_id               = p_data->dch_reconnect_ind.mdl_id;
   3083                 p_dcb->dch_mode             = p_data->dch_reconnect_ind.dch_mode;
   3084                 p_dcb->dch_mode             = p_data->dch_reconnect_ind.dch_mode;
   3085                 p_dcb->is_the_first_reliable= p_data->dch_reconnect_ind.first_reliable;
   3086                 p_dcb->mtu                  = p_data->dch_reconnect_ind.mtu;
   3087                 p_dcb->channel_id           = btif_hl_get_next_channel_id(p_acb->app_id);
   3088 
   3089                 BTIF_TRACE_DEBUG4(" app_idx=%d mcl_idx=%d mdl_idx=%d channel_id=%d",
   3090                                   app_idx, mcl_idx, mdl_idx, p_dcb->channel_id  );
   3091                 if (!btif_hl_create_socket(app_idx, mcl_idx, mdl_idx))
   3092                 {
   3093                     BTIF_TRACE_ERROR0("Unable to create socket");
   3094                     close_dch = TRUE;
   3095                 }
   3096             }
   3097             else
   3098             {
   3099                 BTIF_TRACE_ERROR1("INVALID_LOCAL_MDEP_ID mdep_id=%d",p_data->dch_open_cfm.local_mdep_id);
   3100                 close_dch = TRUE;
   3101             }
   3102 
   3103             if (close_dch)
   3104                 btif_hl_clean_mdl_cb(p_dcb);
   3105         }
   3106         else
   3107             close_dch = TRUE;
   3108     }
   3109     else
   3110         close_dch = TRUE;
   3111 
   3112     if (close_dch)
   3113         BTA_HlDchClose(p_data->dch_reconnect_ind.mdl_handle);
   3114 
   3115 }
   3116 
   3117 /*******************************************************************************
   3118 **
   3119 ** Function         btif_hl_proc_dch_close_ind
   3120 **
   3121 ** Description      Process the DCH close indication
   3122 **
   3123 ** Returns          Nothing
   3124 **
   3125 *******************************************************************************/
   3126 static void btif_hl_proc_dch_close_ind(tBTA_HL *p_data)
   3127 
   3128 {
   3129     btif_hl_mdl_cb_t         *p_dcb;
   3130     btif_hl_mcl_cb_t         *p_mcb;
   3131     UINT8                   app_idx, mcl_idx, mdl_idx;
   3132 
   3133     BTIF_TRACE_DEBUG1("%s", __FUNCTION__);
   3134     if (btif_hl_find_mdl_idx_using_handle(p_data->dch_close_ind.mdl_handle,
   3135                                           &app_idx, &mcl_idx, &mdl_idx ))
   3136     {
   3137         p_dcb = BTIF_HL_GET_MDL_CB_PTR(app_idx, mcl_idx, mdl_idx);
   3138         btif_hl_release_socket(app_idx,mcl_idx, mdl_idx);
   3139         btif_hl_send_setup_disconnected_cb(app_idx, mcl_idx);
   3140         p_mcb =  BTIF_HL_GET_MCL_CB_PTR(app_idx,mcl_idx);
   3141         btif_hl_clean_mdl_cb(p_dcb);
   3142         if (!btif_hl_num_dchs_in_use(p_mcb->mcl_handle))
   3143             btif_hl_start_cch_timer(app_idx, mcl_idx);
   3144         BTIF_TRACE_DEBUG1("remote DCH close success mdl_idx=%d", mdl_idx);
   3145     }
   3146 }
   3147 
   3148 /*******************************************************************************
   3149 **
   3150 ** Function         btif_hl_proc_dch_close_cfm
   3151 **
   3152 ** Description      Process the DCH reconnect confirmation
   3153 **
   3154 ** Returns          Nothing
   3155 **
   3156 *******************************************************************************/
   3157 static void btif_hl_proc_dch_close_cfm(tBTA_HL *p_data)
   3158 
   3159 {
   3160     btif_hl_mdl_cb_t         *p_dcb;
   3161     btif_hl_mcl_cb_t         *p_mcb;
   3162     UINT8                   app_idx, mcl_idx, mdl_idx;
   3163 
   3164     BTIF_TRACE_DEBUG1("%s", __FUNCTION__);
   3165     if (btif_hl_find_mdl_idx_using_handle(p_data->dch_close_cfm.mdl_handle,
   3166                                           &app_idx, &mcl_idx, &mdl_idx ))
   3167     {
   3168         p_dcb = BTIF_HL_GET_MDL_CB_PTR(app_idx, mcl_idx, mdl_idx);
   3169         btif_hl_release_socket(app_idx,mcl_idx,mdl_idx);
   3170         btif_hl_clean_mdl_cb(p_dcb);
   3171         p_mcb =  BTIF_HL_GET_MCL_CB_PTR(app_idx,mcl_idx);
   3172         if (!btif_hl_num_dchs_in_use(p_mcb->mcl_handle))
   3173             btif_hl_start_cch_timer(app_idx, mcl_idx);
   3174         BTIF_TRACE_DEBUG1(" local DCH close success mdl_idx=%d", mdl_idx);
   3175     }
   3176 }
   3177 
   3178 
   3179 /*******************************************************************************
   3180 **
   3181 ** Function         btif_hl_proc_abort_ind
   3182 **
   3183 ** Description      Process the abort indicaiton
   3184 **
   3185 ** Returns          Nothing
   3186 **
   3187 *******************************************************************************/
   3188 static void btif_hl_proc_abort_ind(tBTA_HL_MCL_HANDLE mcl_handle){
   3189 
   3190     UINT8                   app_idx,mcl_idx;
   3191     BTIF_TRACE_DEBUG1("%s", __FUNCTION__ );
   3192     btif_hl_app_cb_t  *p_acb;
   3193     UINT8 i,j;
   3194 
   3195     for (i=0; i<BTA_HL_NUM_APPS; i++)
   3196     {
   3197         p_acb =BTIF_HL_GET_APP_CB_PTR(i);
   3198         for (j=0; j < BTA_HL_NUM_MCLS ; j++)
   3199         {
   3200             if (p_acb->mcb[j].in_use)
   3201                 BTIF_TRACE_DEBUG3("btif_hl_find_mcl_idx_using_handle: app_idx=%d,mcl_idx =%d mcl_handle=%d",i,j,p_acb->mcb[j].mcl_handle);
   3202             if (p_acb->mcb[j].in_use &&
   3203                 (p_acb->mcb[j].mcl_handle == mcl_handle))
   3204             {
   3205                 btif_hl_stop_cch_timer(i, j);
   3206                 btif_hl_send_setup_disconnected_cb(i, j);
   3207                 btif_hl_clean_mcl_cb(i, j);
   3208             }
   3209         }
   3210     }
   3211 }
   3212 
   3213 /*******************************************************************************
   3214 **
   3215 ** Function         btif_hl_proc_abort_cfm
   3216 **
   3217 ** Description      Process the abort confirmation
   3218 **
   3219 ** Returns          Nothing
   3220 **
   3221 *******************************************************************************/
   3222 static void btif_hl_proc_abort_cfm(tBTA_HL_MCL_HANDLE mcl_handle){
   3223     UINT8                   app_idx,mcl_idx;
   3224 
   3225     BTIF_TRACE_DEBUG1("%s", __FUNCTION__ );
   3226     btif_hl_app_cb_t  *p_acb;
   3227     UINT8 i,j;
   3228 
   3229     for (i=0; i<BTA_HL_NUM_APPS; i++)
   3230     {
   3231         p_acb =BTIF_HL_GET_APP_CB_PTR(i);
   3232         for (j=0; j < BTA_HL_NUM_MCLS ; j++)
   3233         {
   3234             if (p_acb->mcb[j].in_use)
   3235                 BTIF_TRACE_DEBUG3("btif_hl_find_mcl_idx_using_handle: app_idx=%d,mcl_idx =%d mcl_handle=%d",i,j,p_acb->mcb[j].mcl_handle);
   3236             if (p_acb->mcb[j].in_use &&
   3237                 (p_acb->mcb[j].mcl_handle == mcl_handle))
   3238             {
   3239                 btif_hl_stop_cch_timer(i, j);
   3240                 btif_hl_send_setup_disconnected_cb(i, j);
   3241                 btif_hl_clean_mcl_cb(i, j);
   3242             }
   3243         }
   3244     }
   3245 
   3246 }
   3247 
   3248 /*******************************************************************************
   3249 **
   3250 ** Function         btif_hl_proc_send_data_cfm
   3251 **
   3252 ** Description      Process the send data confirmation
   3253 **
   3254 ** Returns          Nothing
   3255 **
   3256 *******************************************************************************/
   3257 static void btif_hl_proc_send_data_cfm(tBTA_HL_MDL_HANDLE mdl_handle,
   3258                                        tBTA_HL_STATUS status){
   3259     UINT8                   app_idx,mcl_idx, mdl_idx;
   3260     btif_hl_mdl_cb_t         *p_dcb;
   3261 
   3262     BTIF_TRACE_DEBUG1("%s", __FUNCTION__);
   3263     if (btif_hl_find_mdl_idx_using_handle(mdl_handle,
   3264                                           &app_idx, &mcl_idx, &mdl_idx ))
   3265     {
   3266         p_dcb =BTIF_HL_GET_MDL_CB_PTR(app_idx, mcl_idx, mdl_idx);
   3267         btif_hl_free_buf((void **) &p_dcb->p_tx_pkt);
   3268         BTIF_TRACE_DEBUG1("send success free p_tx_pkt tx_size=%d", p_dcb->tx_size);
   3269         p_dcb->tx_size = 0;
   3270     }
   3271 }
   3272 
   3273 /*******************************************************************************
   3274 **
   3275 ** Function         btif_hl_proc_dch_cong_ind
   3276 **
   3277 ** Description      Process the DCH congestion change indication
   3278 **
   3279 ** Returns          Nothing
   3280 **
   3281 *******************************************************************************/
   3282 static void btif_hl_proc_dch_cong_ind(tBTA_HL *p_data)
   3283 
   3284 {
   3285     btif_hl_mdl_cb_t         *p_dcb;
   3286     UINT8                   app_idx, mcl_idx, mdl_idx;
   3287 
   3288     BTIF_TRACE_DEBUG0("btif_hl_proc_dch_cong_ind");
   3289 
   3290 
   3291     if (btif_hl_find_mdl_idx_using_handle(p_data->dch_cong_ind.mdl_handle, &app_idx, &mcl_idx, &mdl_idx))
   3292     {
   3293         p_dcb =BTIF_HL_GET_MDL_CB_PTR(app_idx, mcl_idx, mdl_idx);
   3294         p_dcb->cong = p_data->dch_cong_ind.cong;
   3295     }
   3296 }
   3297 
   3298 /*******************************************************************************
   3299 **
   3300 ** Function         btif_hl_proc_cb_evt
   3301 **
   3302 ** Description      Process registration request
   3303 **
   3304 ** Returns          void
   3305 **
   3306 *******************************************************************************/
   3307 static void btif_hl_proc_reg_request(UINT8 app_idx, UINT8  app_id,
   3308                                      tBTA_HL_REG_PARAM *p_reg_param,
   3309                                      tBTA_HL_CBACK *p_cback){
   3310     bt_status_t status= BT_STATUS_SUCCESS;
   3311     UINT8 i;
   3312     btif_hl_app_data_t *p_data;
   3313     BTIF_TRACE_DEBUG3("%s app_idx=%d app_id=%d", __FUNCTION__, app_idx, app_id);
   3314 
   3315     if(reg_counter >1)
   3316     {
   3317         BTIF_TRACE_DEBUG0("btif_hl_proc_reg_request: calling uPDATE");
   3318         BTA_HlUpdate(app_id, p_reg_param,TRUE, btif_hl_cback);
   3319     }
   3320     else
   3321         BTA_HlRegister(app_id, p_reg_param, btif_hl_cback);
   3322 }
   3323 
   3324 
   3325 /*******************************************************************************
   3326 **
   3327 ** Function         btif_hl_proc_cb_evt
   3328 **
   3329 ** Description      Process HL callback events
   3330 **
   3331 ** Returns          void
   3332 **
   3333 *******************************************************************************/
   3334 static void btif_hl_proc_cb_evt(UINT16 event, char* p_param){
   3335 
   3336     btif_hl_evt_cb_t                *p_data = (btif_hl_evt_cb_t *)p_param;
   3337     bt_bdaddr_t                     bd_addr;
   3338     bthl_channel_state_t            state=BTHL_CONN_STATE_DISCONNECTED;
   3339     BOOLEAN                         send_chan_cb=TRUE;
   3340     tBTA_HL_REG_PARAM               reg_param;
   3341     btif_hl_app_cb_t                *p_acb;
   3342     bthl_app_reg_state_t            reg_state = BTHL_APP_REG_STATE_REG_FAILED;
   3343     int                             app_id;
   3344     UINT8                           preg_idx;
   3345     bt_status_t                     bt_status;
   3346 
   3347     BTIF_TRACE_DEBUG2("%s event %d", __FUNCTION__, event);
   3348     btif_hl_display_calling_process_name();
   3349 
   3350     switch (event)
   3351     {
   3352         case BTIF_HL_SEND_CONNECTED_CB:
   3353         case BTIF_HL_SEND_DISCONNECTED_CB:
   3354             if (p_data->chan_cb.cb_state == BTIF_HL_CHAN_CB_STATE_CONNECTED_PENDING)
   3355                 state = BTHL_CONN_STATE_CONNECTED;
   3356             else if (p_data->chan_cb.cb_state == BTIF_HL_CHAN_CB_STATE_DISCONNECTED_PENDING)
   3357                 state = BTHL_CONN_STATE_DISCONNECTED;
   3358             else
   3359                 send_chan_cb = FALSE;
   3360 
   3361             if (send_chan_cb)
   3362             {
   3363                 btif_hl_copy_bda(&bd_addr, p_data->chan_cb.bd_addr);
   3364                 BTIF_TRACE_DEBUG4("state callbk: ch_id=0x%08x cb_state=%d state=%d  fd=%d",
   3365                                   p_data->chan_cb.channel_id,
   3366                                   p_data->chan_cb.cb_state,
   3367                                   state,  p_data->chan_cb.fd);
   3368                 btif_hl_display_bt_bda(&bd_addr);
   3369                 BTIF_HL_CALL_CBACK(bt_hl_callbacks, channel_state_cb,  p_data->chan_cb.app_id,
   3370                                    &bd_addr, p_data->chan_cb.mdep_cfg_index,
   3371                                    p_data->chan_cb.channel_id, state, p_data->chan_cb.fd );
   3372             }
   3373 
   3374             break;
   3375         case BTIF_HL_REG_APP:
   3376             p_acb  = BTIF_HL_GET_APP_CB_PTR(p_data->reg.app_idx);
   3377             app_id = (int) p_acb->app_id;
   3378             BTIF_TRACE_DEBUG2("Rcv BTIF_HL_REG_APP app_idx=%d reg_pending=%d", p_data->reg.app_idx, p_acb->reg_pending);
   3379             if (btif_hl_get_state() == BTIF_HL_STATE_ENABLED && p_acb->reg_pending)
   3380             {
   3381                 BTIF_TRACE_DEBUG1("Rcv BTIF_HL_REG_APP reg_counter=%d",reg_counter);
   3382                 p_acb->reg_pending = FALSE;
   3383                 reg_param.dev_type = p_acb->dev_type;
   3384                 reg_param.sec_mask = BTA_SEC_AUTHENTICATE | BTA_SEC_ENCRYPT;
   3385                 reg_param.p_srv_name = p_acb->srv_name;
   3386                 reg_param.p_srv_desp = p_acb->srv_desp;
   3387                 reg_param.p_provider_name = p_acb->provider_name;
   3388                 btif_hl_proc_reg_request (p_data->reg.app_idx, p_acb->app_id, &reg_param, btif_hl_cback);
   3389             }
   3390             else
   3391             {
   3392                 BTIF_TRACE_DEBUG2("reg request is processed state=%d reg_pending=%d", btif_hl_get_state(), p_acb->reg_pending);
   3393             }
   3394 
   3395             break;
   3396 
   3397         case BTIF_HL_UNREG_APP:
   3398             BTIF_TRACE_DEBUG1("Rcv BTIF_HL_UNREG_APP app_idx=%d", p_data->unreg.app_idx );
   3399             p_acb = BTIF_HL_GET_APP_CB_PTR(p_data->unreg.app_idx);
   3400             if (btif_hl_get_state() == BTIF_HL_STATE_ENABLED)
   3401             {
   3402                 if(reg_counter >= 1)
   3403                     BTA_HlUpdate(p_acb->app_id,NULL,FALSE,NULL);
   3404                 else
   3405                     BTA_HlDeregister(p_acb->app_id, p_acb->app_handle);
   3406             }
   3407             break;
   3408         case BTIF_HL_UPDATE_MDL:
   3409             BTIF_TRACE_DEBUG1("Rcv BTIF_HL_UPDATE_MDL app_idx=%d", p_data->update_mdl.app_idx );
   3410             p_acb = BTIF_HL_GET_APP_CB_PTR(p_data->update_mdl.app_idx);
   3411             break;
   3412 
   3413         default:
   3414             BTIF_TRACE_ERROR1("Unknown event %d", event);
   3415             break;
   3416     }
   3417 }
   3418 
   3419 /*******************************************************************************
   3420 **
   3421 ** Function         btif_hl_upstreams_evt
   3422 **
   3423 ** Description      Process HL events
   3424 **
   3425 ** Returns          void
   3426 **
   3427 *******************************************************************************/
   3428 static void btif_hl_upstreams_evt(UINT16 event, char* p_param){
   3429     tBTA_HL *p_data = (tBTA_HL *)p_param;
   3430     UINT8                 app_idx, mcl_idx;
   3431     btif_hl_app_cb_t      *p_acb;
   3432     btif_hl_mcl_cb_t      *p_mcb = NULL;
   3433     BD_ADDR               bd_addr;
   3434     btif_hl_pend_dch_op_t  pending_op;
   3435     BOOLEAN status;
   3436 
   3437     BTIF_TRACE_DEBUG2("%s event %d", __FUNCTION__, event);
   3438     btif_hl_display_calling_process_name();
   3439     switch (event)
   3440     {
   3441         case BTA_HL_REGISTER_CFM_EVT:
   3442             BTIF_TRACE_DEBUG0("Rcv BTA_HL_REGISTER_CFM_EVT");
   3443             BTIF_TRACE_DEBUG3("app_id=%d app_handle=%d status=%d ",
   3444                               p_data->reg_cfm.app_id,
   3445                               p_data->reg_cfm.app_handle,
   3446                               p_data->reg_cfm.status );
   3447 
   3448             btif_hl_proc_reg_cfm(p_data);
   3449             break;
   3450         case BTA_HL_SDP_INFO_IND_EVT:
   3451             BTIF_TRACE_DEBUG0("Rcv BTA_HL_SDP_INFO_IND_EVT");
   3452             BTIF_TRACE_DEBUG5("app_handle=%d ctrl_psm=0x%04x data_psm=0x%04x x_spec=%d mcap_sup_procs=0x%02x",
   3453                               p_data->sdp_info_ind.app_handle,
   3454                               p_data->sdp_info_ind.ctrl_psm,
   3455                               p_data->sdp_info_ind.data_psm,
   3456                               p_data->sdp_info_ind.data_x_spec,
   3457                               p_data->sdp_info_ind.mcap_sup_procs);
   3458             //btif_hl_proc_sdp_info_ind(p_data);
   3459             break;
   3460 
   3461         case BTA_HL_DEREGISTER_CFM_EVT:
   3462             BTIF_TRACE_DEBUG0("Rcv BTA_HL_DEREGISTER_CFM_EVT");
   3463             BTIF_TRACE_DEBUG2("app_handle=%d status=%d ",
   3464                               p_data->dereg_cfm.app_handle,
   3465                               p_data->dereg_cfm.status );
   3466             btif_hl_proc_dereg_cfm(p_data);
   3467             break;
   3468 
   3469         case BTA_HL_SDP_QUERY_CFM_EVT:
   3470             BTIF_TRACE_DEBUG0("Rcv BTA_HL_SDP_QUERY_CFM_EVT");
   3471             BTIF_TRACE_DEBUG3("app_handle=%d app_id =%d,status =%d",
   3472                               p_data->sdp_query_cfm.app_handle,p_data->sdp_query_cfm.app_id,
   3473                               p_data->sdp_query_cfm.status);
   3474 
   3475             BTIF_TRACE_DEBUG6("DB [%02x] [%02x] [%02x] [%02x] [%02x] [%02x]",
   3476                               p_data->sdp_query_cfm.bd_addr[0], p_data->sdp_query_cfm.bd_addr[1],
   3477                               p_data->sdp_query_cfm.bd_addr[2], p_data->sdp_query_cfm.bd_addr[3],
   3478                               p_data->sdp_query_cfm.bd_addr[4], p_data->sdp_query_cfm.bd_addr[5]);
   3479 
   3480             if (p_data->sdp_query_cfm.status == BTA_HL_STATUS_OK)
   3481                 status = btif_hl_proc_sdp_query_cfm(p_data);
   3482             else
   3483                 status = FALSE;
   3484 
   3485             if (!status)
   3486             {
   3487                 BTIF_TRACE_DEBUG1("BTA_HL_SDP_QUERY_CFM_EVT Status = %d",
   3488                                                         p_data->sdp_query_cfm.status);
   3489                 if (btif_hl_find_app_idx_using_app_id(p_data->sdp_query_cfm.app_id, &app_idx))
   3490                 {
   3491                     p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx);
   3492                     if (btif_hl_find_mcl_idx(app_idx, p_data->sdp_query_cfm.bd_addr, &mcl_idx))
   3493                     {
   3494                         p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
   3495                         if ( (p_mcb->cch_oper =  BTIF_HL_CCH_OP_MDEP_FILTERING) ||
   3496                              (p_mcb->cch_oper == BTIF_HL_CCH_OP_DCH_OPEN) )
   3497                         {
   3498                             pending_op = p_mcb->pcb.op;
   3499                             switch (pending_op)
   3500                             {
   3501                                 case BTIF_HL_PEND_DCH_OP_OPEN:
   3502                                     btif_hl_send_setup_disconnected_cb(app_idx, mcl_idx);
   3503                                     break;
   3504                                 case BTIF_HL_PEND_DCH_OP_RECONNECT:
   3505                                 case BTIF_HL_PEND_DCH_OP_DELETE_MDL:
   3506                                 default:
   3507                                     break;
   3508                             }
   3509                             if (!p_mcb->is_connected)
   3510                                 btif_hl_clean_mcl_cb(app_idx, mcl_idx);
   3511                         }
   3512                     }
   3513                 }
   3514             }
   3515 
   3516             break;
   3517 
   3518 
   3519         case BTA_HL_CCH_OPEN_CFM_EVT:
   3520             BTIF_TRACE_DEBUG0("Rcv BTA_HL_CCH_OPEN_CFM_EVT");
   3521             BTIF_TRACE_DEBUG4("app_id=%d,app_handle=%d mcl_handle=%d status =%d",
   3522                               p_data->cch_open_cfm.app_id,
   3523                               p_data->cch_open_cfm.app_handle,
   3524                               p_data->cch_open_cfm.mcl_handle,
   3525                               p_data->cch_open_cfm.status);
   3526             BTIF_TRACE_DEBUG6("DB [%02x] [%02x] [%02x] [%02x] [%02x] [%02x]",
   3527                               p_data->cch_open_cfm.bd_addr[0], p_data->cch_open_cfm.bd_addr[1],
   3528                               p_data->cch_open_cfm.bd_addr[2], p_data->cch_open_cfm.bd_addr[3],
   3529                               p_data->cch_open_cfm.bd_addr[4], p_data->cch_open_cfm.bd_addr[5]);
   3530 
   3531             if (p_data->cch_open_cfm.status == BTA_HL_STATUS_OK ||
   3532                         p_data->cch_open_cfm.status == BTA_HL_STATUS_DUPLICATE_CCH_OPEN)
   3533             {
   3534                 status = btif_hl_proc_cch_open_cfm(p_data);
   3535             }
   3536             else
   3537             {
   3538                 status = FALSE;
   3539             }
   3540 
   3541             if (!status)
   3542             {
   3543                 if (btif_hl_find_app_idx_using_app_id(p_data->cch_open_cfm.app_id, &app_idx))
   3544                 {
   3545                     p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx);
   3546                     if (btif_hl_find_mcl_idx(app_idx, p_data->cch_open_cfm.bd_addr, &mcl_idx))
   3547                     {
   3548                         p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
   3549                         pending_op = p_mcb->pcb.op;
   3550                         switch (pending_op)
   3551                         {
   3552                             case BTIF_HL_PEND_DCH_OP_OPEN:
   3553                                 btif_hl_send_setup_disconnected_cb(app_idx, mcl_idx);
   3554                                 break;
   3555                             case BTIF_HL_PEND_DCH_OP_RECONNECT:
   3556                             case BTIF_HL_PEND_DCH_OP_DELETE_MDL:
   3557                             default:
   3558                                 break;
   3559                         }
   3560                         btif_hl_clean_mcl_cb(app_idx, mcl_idx);
   3561                     }
   3562                 }
   3563             }
   3564             break;
   3565 
   3566         case BTA_HL_DCH_OPEN_CFM_EVT:
   3567             BTIF_TRACE_DEBUG0("Rcv BTA_HL_DCH_OPEN_CFM_EVT");
   3568             BTIF_TRACE_DEBUG3("mcl_handle=%d mdl_handle=0x%x status=%d ",
   3569                               p_data->dch_open_cfm.mcl_handle,
   3570                               p_data->dch_open_cfm.mdl_handle,
   3571                               p_data->dch_open_cfm.status);
   3572             BTIF_TRACE_DEBUG5("first_reliable =%d dch_mode=%d local_mdep_id=%d mdl_id=%d mtu=%d",
   3573                               p_data->dch_open_cfm.first_reliable,
   3574                               p_data->dch_open_cfm.dch_mode,
   3575                               p_data->dch_open_cfm.local_mdep_id,
   3576                               p_data->dch_open_cfm.mdl_id,
   3577                               p_data->dch_open_cfm.mtu);
   3578             if (p_data->dch_open_cfm.status == BTA_HL_STATUS_OK)
   3579             {
   3580                 status = btif_hl_proc_dch_open_cfm(p_data);
   3581             }
   3582             else
   3583             {
   3584                 status = FALSE;
   3585             }
   3586 
   3587             if (!status)
   3588             {
   3589                 if (btif_hl_find_mcl_idx_using_handle(p_data->dch_open_cfm.mcl_handle,&app_idx, &mcl_idx))
   3590                 {
   3591                     p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
   3592                     pending_op = p_mcb->pcb.op;
   3593                     switch (pending_op)
   3594                     {
   3595                         case BTIF_HL_PEND_DCH_OP_OPEN:
   3596                             btif_hl_send_setup_disconnected_cb(app_idx, mcl_idx);
   3597                             break;
   3598                         case BTIF_HL_PEND_DCH_OP_RECONNECT:
   3599                         case BTIF_HL_PEND_DCH_OP_DELETE_MDL:
   3600                         default:
   3601                             break;
   3602                     }
   3603                 }
   3604             }
   3605             break;
   3606 
   3607 
   3608         case BTA_HL_CCH_OPEN_IND_EVT:
   3609             BTIF_TRACE_DEBUG0("Rcv BTA_HL_CCH_OPEN_IND_EVT");
   3610             BTIF_TRACE_DEBUG2("app_handle=%d mcl_handle=%d",
   3611                               p_data->cch_open_ind.app_handle,
   3612                               p_data->cch_open_ind.mcl_handle);
   3613             BTIF_TRACE_DEBUG6("DB [%02x] [%02x] [%02x] [%02x] [%02x] [%02x]",
   3614                               p_data->cch_open_ind.bd_addr[0], p_data->cch_open_ind.bd_addr[1],
   3615                               p_data->cch_open_ind.bd_addr[2], p_data->cch_open_ind.bd_addr[3],
   3616                               p_data->cch_open_ind.bd_addr[4], p_data->cch_open_ind.bd_addr[5]);
   3617 
   3618             btif_hl_proc_cch_open_ind(p_data);
   3619             break;
   3620 
   3621         case BTA_HL_DCH_CREATE_IND_EVT:
   3622             BTIF_TRACE_DEBUG0("Rcv BTA_HL_DCH_CREATE_IND_EVT");
   3623             BTIF_TRACE_DEBUG1("mcl_handle=%d",
   3624                               p_data->dch_create_ind.mcl_handle );
   3625             BTIF_TRACE_DEBUG3("local_mdep_id =%d mdl_id=%d cfg=%d",
   3626                               p_data->dch_create_ind.local_mdep_id,
   3627                               p_data->dch_create_ind.mdl_id,
   3628                               p_data->dch_create_ind.cfg);
   3629             btif_hl_proc_create_ind(p_data);
   3630             break;
   3631 
   3632         case BTA_HL_DCH_OPEN_IND_EVT:
   3633             BTIF_TRACE_DEBUG0("Rcv BTA_HL_DCH_OPEN_IND_EVT");
   3634             BTIF_TRACE_DEBUG2("mcl_handle=%d mdl_handle=0x%x",
   3635                               p_data->dch_open_ind.mcl_handle,
   3636                               p_data->dch_open_ind.mdl_handle );
   3637             BTIF_TRACE_DEBUG5("first_reliable =%d dch_mode=%d local_mdep_id=%d mdl_id=%d mtu=%d",
   3638                               p_data->dch_open_ind.first_reliable,
   3639                               p_data->dch_open_ind.dch_mode,
   3640                               p_data->dch_open_ind.local_mdep_id,
   3641                               p_data->dch_open_ind.mdl_id,
   3642                               p_data->dch_open_ind.mtu);
   3643 
   3644             btif_hl_proc_dch_open_ind(p_data);
   3645             break;
   3646 
   3647         case BTA_HL_DELETE_MDL_IND_EVT:
   3648             BTIF_TRACE_DEBUG0("Rcv BTA_HL_DELETE_MDL_IND_EVT");
   3649             BTIF_TRACE_DEBUG2("mcl_handle=%d mdl_id=0x%x",
   3650                               p_data->delete_mdl_ind.mcl_handle,
   3651                               p_data->delete_mdl_ind.mdl_id);
   3652             break;
   3653 
   3654         case BTA_HL_DELETE_MDL_CFM_EVT:
   3655             BTIF_TRACE_DEBUG0("Rcv BTA_HL_DELETE_MDL_CFM_EVT");
   3656             BTIF_TRACE_DEBUG3("mcl_handle=%d mdl_id=0x%x status=%d",
   3657                               p_data->delete_mdl_cfm.mcl_handle,
   3658                               p_data->delete_mdl_cfm.mdl_id,
   3659                               p_data->delete_mdl_cfm.status);
   3660 
   3661             if (btif_hl_find_app_idx_using_deleted_mdl_id( p_data->delete_mdl_cfm.mdl_id,
   3662                                     &app_idx))
   3663             {
   3664                 p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx);
   3665                 btif_hl_send_destroyed_cb(p_acb);
   3666                 btif_hl_clean_delete_mdl(&p_acb->delete_mdl);
   3667             }
   3668             break;
   3669 
   3670         case BTA_HL_DCH_RECONNECT_CFM_EVT:
   3671             BTIF_TRACE_DEBUG0("Rcv BTA_HL_DCH_RECONNECT_CFM_EVT");
   3672             BTIF_TRACE_DEBUG3("mcl_handle=%d mdl_handle=%d status=%d   ",
   3673                               p_data->dch_reconnect_cfm.mcl_handle,
   3674                               p_data->dch_reconnect_cfm.mdl_handle,
   3675                               p_data->dch_reconnect_cfm.status);
   3676             BTIF_TRACE_DEBUG4("first_reliable =%d dch_mode=%d mdl_id=%d mtu=%d",
   3677                               p_data->dch_reconnect_cfm.first_reliable,
   3678                               p_data->dch_reconnect_cfm.dch_mode,
   3679                               p_data->dch_reconnect_cfm.mdl_id,
   3680                               p_data->dch_reconnect_cfm.mtu);
   3681 
   3682 
   3683             if (p_data->dch_reconnect_cfm.status == BTA_HL_STATUS_OK)
   3684             {
   3685                 status = btif_hl_proc_dch_reconnect_cfm(p_data);
   3686             }
   3687             else
   3688             {
   3689                 status = FALSE;
   3690             }
   3691 
   3692             if (!status)
   3693             {
   3694                 if (btif_hl_find_mcl_idx_using_handle(p_data->dch_open_cfm.mcl_handle,&app_idx, &mcl_idx))
   3695                 {
   3696                     p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
   3697                     pending_op = p_mcb->pcb.op;
   3698                     switch (pending_op)
   3699                     {
   3700                         case BTIF_HL_PEND_DCH_OP_OPEN:
   3701                             btif_hl_send_setup_disconnected_cb(app_idx, mcl_idx);
   3702                             break;
   3703                         case BTIF_HL_PEND_DCH_OP_RECONNECT:
   3704                         case BTIF_HL_PEND_DCH_OP_DELETE_MDL:
   3705                         default:
   3706                             break;
   3707                     }
   3708                 }
   3709             }
   3710 
   3711             break;
   3712 
   3713         case BTA_HL_CCH_CLOSE_CFM_EVT:
   3714             BTIF_TRACE_DEBUG0("Rcv BTA_HL_CCH_CLOSE_CFM_EVT");
   3715             BTIF_TRACE_DEBUG2("mcl_handle=%d status =%d",
   3716                               p_data->cch_close_cfm.mcl_handle,
   3717                               p_data->cch_close_cfm.status);
   3718             if (p_data->cch_close_cfm.status == BTA_HL_STATUS_OK)
   3719             {
   3720                 btif_hl_proc_cch_close_cfm(p_data);
   3721             }
   3722             break;
   3723 
   3724         case BTA_HL_CCH_CLOSE_IND_EVT:
   3725             BTIF_TRACE_DEBUG0("Rcv BTA_HL_CCH_CLOSE_IND_EVT");
   3726             BTIF_TRACE_DEBUG2("mcl_handle =%d intentional_close=%s",
   3727                               p_data->cch_close_ind.mcl_handle,
   3728                               (p_data->cch_close_ind.intentional?"Yes":"No"));
   3729 
   3730             btif_hl_proc_cch_close_ind(p_data);
   3731             break;
   3732 
   3733         case BTA_HL_DCH_CLOSE_IND_EVT:
   3734             BTIF_TRACE_DEBUG0("Rcv BTA_HL_DCH_CLOSE_IND_EVT");
   3735             BTIF_TRACE_DEBUG2("mdl_handle=%d intentional_close=%s",
   3736                               p_data->dch_close_ind.mdl_handle,
   3737                               (p_data->dch_close_ind.intentional?"Yes":"No") );
   3738 
   3739             btif_hl_proc_dch_close_ind(p_data);
   3740             break;
   3741 
   3742         case BTA_HL_DCH_CLOSE_CFM_EVT:
   3743             BTIF_TRACE_DEBUG0("Rcv BTA_HL_DCH_CLOSE_CFM_EVT");
   3744             BTIF_TRACE_DEBUG2("mdl_handle=%d status=%d ",
   3745                               p_data->dch_close_cfm.mdl_handle,
   3746                               p_data->dch_close_cfm.status);
   3747 
   3748             if (p_data->dch_close_cfm.status == BTA_HL_STATUS_OK)
   3749             {
   3750                 btif_hl_proc_dch_close_cfm(p_data);
   3751             }
   3752             break;
   3753 
   3754         case BTA_HL_DCH_ECHO_TEST_CFM_EVT:
   3755             BTIF_TRACE_DEBUG0("Rcv BTA_HL_DCH_ECHO_TEST_CFM_EVT");
   3756             BTIF_TRACE_DEBUG2("mcl_handle=%d    status=%d",
   3757                               p_data->echo_test_cfm.mcl_handle,
   3758                               p_data->echo_test_cfm.status );
   3759             /* not supported */
   3760             break;
   3761 
   3762 
   3763         case BTA_HL_DCH_RECONNECT_IND_EVT:
   3764             BTIF_TRACE_DEBUG0("Rcv BTA_HL_DCH_RECONNECT_IND_EVT");
   3765 
   3766             BTIF_TRACE_DEBUG2("mcl_handle=%d mdl_handle=5d",
   3767                               p_data->dch_reconnect_ind.mcl_handle,
   3768                               p_data->dch_reconnect_ind.mdl_handle );
   3769             BTIF_TRACE_DEBUG4("first_reliable =%d dch_mode=%d mdl_id=%d mtu=%d",
   3770                               p_data->dch_reconnect_ind.first_reliable,
   3771                               p_data->dch_reconnect_ind.dch_mode,
   3772                               p_data->dch_reconnect_ind.mdl_id,
   3773                               p_data->dch_reconnect_ind.mtu);
   3774 
   3775             btif_hl_proc_dch_reconnect_ind(p_data);
   3776             break;
   3777 
   3778         case BTA_HL_CONG_CHG_IND_EVT:
   3779             BTIF_TRACE_DEBUG0("Rcv BTA_HL_CONG_CHG_IND_EVT");
   3780             BTIF_TRACE_DEBUG2("mdl_handle=%d cong =%d",
   3781                               p_data->dch_cong_ind.mdl_handle,
   3782                               p_data->dch_cong_ind.cong);
   3783             btif_hl_proc_dch_cong_ind(p_data);
   3784             break;
   3785 
   3786         case BTA_HL_DCH_ABORT_IND_EVT:
   3787             BTIF_TRACE_DEBUG0("Rcv BTA_HL_DCH_ABORT_IND_EVT");
   3788             BTIF_TRACE_DEBUG1("mcl_handle=%d",
   3789                               p_data->dch_abort_ind.mcl_handle );
   3790             btif_hl_proc_abort_ind(p_data->dch_abort_ind.mcl_handle);
   3791             break;
   3792         case BTA_HL_DCH_ABORT_CFM_EVT:
   3793             BTIF_TRACE_DEBUG0("Rcv BTA_HL_DCH_ABORT_CFM_EVT");
   3794             BTIF_TRACE_DEBUG2("mcl_handle=%d status =%d",
   3795                               p_data->dch_abort_cfm.mcl_handle,
   3796                               p_data->dch_abort_cfm.status);
   3797             if (p_data->dch_abort_cfm.status == BTA_HL_STATUS_OK)
   3798             {
   3799                 btif_hl_proc_abort_cfm(p_data->dch_abort_ind.mcl_handle);
   3800             }
   3801             break;
   3802 
   3803         case BTA_HL_DCH_SEND_DATA_CFM_EVT:
   3804             BTIF_TRACE_DEBUG0("Rcv BTA_HL_DCH_SEND_DATA_CFM_EVT");
   3805             BTIF_TRACE_DEBUG2("mdl_handle=0x%x status =%d",
   3806                               p_data->dch_send_data_cfm.mdl_handle,
   3807                               p_data->dch_send_data_cfm.status);
   3808             btif_hl_proc_send_data_cfm(p_data->dch_send_data_cfm.mdl_handle,
   3809                                        p_data->dch_send_data_cfm.status);
   3810             break;
   3811 
   3812         case BTA_HL_DCH_RCV_DATA_IND_EVT:
   3813             BTIF_TRACE_DEBUG0("Rcv BTA_HL_DCH_RCV_DATA_IND_EVT");
   3814             BTIF_TRACE_DEBUG1("mdl_handle=0x%x ",
   3815                               p_data->dch_rcv_data_ind.mdl_handle);
   3816             /* do nothing here */
   3817             break;
   3818 
   3819         default:
   3820             BTIF_TRACE_DEBUG1("Unknown Event (0x%02x)...", event);
   3821             break;
   3822     }
   3823 }
   3824 
   3825 /*******************************************************************************
   3826 **
   3827 ** Function         btif_hl_cback
   3828 **
   3829 ** Description      Callback function for HL events
   3830 **
   3831 ** Returns          void
   3832 **
   3833 *******************************************************************************/
   3834 static void btif_hl_cback(tBTA_HL_EVT event, tBTA_HL *p_data){
   3835     bt_status_t status;
   3836     int param_len = 0;
   3837     BTIF_TRACE_DEBUG2("%s event %d", __FUNCTION__, event);
   3838     btif_hl_display_calling_process_name();
   3839     switch (event)
   3840     {
   3841         case BTA_HL_REGISTER_CFM_EVT:
   3842             param_len = sizeof(tBTA_HL_REGISTER_CFM);
   3843             break;
   3844         case BTA_HL_SDP_INFO_IND_EVT:
   3845             param_len = sizeof(tBTA_HL_SDP_INFO_IND);
   3846             break;
   3847         case BTA_HL_DEREGISTER_CFM_EVT:
   3848             param_len = sizeof(tBTA_HL_DEREGISTER_CFM);
   3849             break;
   3850         case BTA_HL_SDP_QUERY_CFM_EVT:
   3851             param_len = sizeof(tBTA_HL_SDP_QUERY_CFM);
   3852             break;
   3853         case BTA_HL_CCH_OPEN_CFM_EVT:
   3854             param_len = sizeof(tBTA_HL_CCH_OPEN_CFM);
   3855             break;
   3856         case BTA_HL_DCH_OPEN_CFM_EVT:
   3857             param_len = sizeof(tBTA_HL_DCH_OPEN_CFM);
   3858             break;
   3859         case BTA_HL_CCH_OPEN_IND_EVT:
   3860             param_len = sizeof(tBTA_HL_CCH_OPEN_IND);
   3861             break;
   3862         case BTA_HL_DCH_CREATE_IND_EVT:
   3863             param_len = sizeof(tBTA_HL_DCH_CREATE_IND);
   3864             break;
   3865         case BTA_HL_DCH_OPEN_IND_EVT:
   3866             param_len = sizeof(tBTA_HL_DCH_OPEN_IND);
   3867             break;
   3868         case BTA_HL_DELETE_MDL_IND_EVT:
   3869             param_len = sizeof(tBTA_HL_MDL_IND);
   3870             break;
   3871         case BTA_HL_DELETE_MDL_CFM_EVT:
   3872             param_len = sizeof(tBTA_HL_MDL_CFM);
   3873             break;
   3874         case BTA_HL_DCH_RECONNECT_CFM_EVT:
   3875             param_len = sizeof(tBTA_HL_DCH_OPEN_CFM);
   3876             break;
   3877         case BTA_HL_CCH_CLOSE_CFM_EVT:
   3878             param_len = sizeof(tBTA_HL_MCL_CFM);
   3879             break;
   3880         case BTA_HL_CCH_CLOSE_IND_EVT:
   3881             param_len = sizeof(tBTA_HL_CCH_CLOSE_IND);
   3882             break;
   3883         case BTA_HL_DCH_CLOSE_IND_EVT:
   3884             param_len = sizeof(tBTA_HL_DCH_CLOSE_IND);
   3885             break;
   3886         case BTA_HL_DCH_CLOSE_CFM_EVT:
   3887             param_len = sizeof(tBTA_HL_MDL_CFM);
   3888             break;
   3889         case BTA_HL_DCH_ECHO_TEST_CFM_EVT:
   3890             param_len = sizeof(tBTA_HL_MCL_CFM);
   3891             break;
   3892         case BTA_HL_DCH_RECONNECT_IND_EVT:
   3893             param_len = sizeof(tBTA_HL_DCH_OPEN_IND);
   3894             break;
   3895         case BTA_HL_CONG_CHG_IND_EVT:
   3896             param_len = sizeof(tBTA_HL_DCH_CONG_IND);
   3897             break;
   3898         case BTA_HL_DCH_ABORT_IND_EVT:
   3899             param_len = sizeof(tBTA_HL_MCL_IND);
   3900             break;
   3901         case BTA_HL_DCH_ABORT_CFM_EVT:
   3902             param_len = sizeof(tBTA_HL_MCL_CFM);
   3903             break;
   3904         case BTA_HL_DCH_SEND_DATA_CFM_EVT:
   3905             param_len = sizeof(tBTA_HL_MDL_CFM);
   3906             break;
   3907         case BTA_HL_DCH_RCV_DATA_IND_EVT:
   3908             param_len = sizeof(tBTA_HL_MDL_IND);
   3909             break;
   3910         default:
   3911             param_len = sizeof(tBTA_HL_MDL_IND);
   3912             break;
   3913     }
   3914     status = btif_transfer_context(btif_hl_upstreams_evt, (uint16_t)event, (void*)p_data, param_len, NULL);
   3915 
   3916     /* catch any failed context transfers */
   3917     ASSERTC(status == BT_STATUS_SUCCESS, "context transfer failed", status);
   3918 }
   3919 
   3920 /*******************************************************************************
   3921 **
   3922 ** Function         btif_hl_upstreams_ctrl_evt
   3923 **
   3924 ** Description      Callback function for HL control events in the BTIF task context
   3925 **
   3926 ** Returns          void
   3927 **
   3928 *******************************************************************************/
   3929 static void btif_hl_upstreams_ctrl_evt(UINT16 event, char* p_param){
   3930     tBTA_HL_CTRL *p_data = (tBTA_HL_CTRL *) p_param;
   3931     UINT8               i;
   3932     tBTA_HL_REG_PARAM   reg_param;
   3933     btif_hl_app_cb_t    *p_acb;
   3934 
   3935     BTIF_TRACE_DEBUG2("%s event %d", __FUNCTION__, event);
   3936     btif_hl_display_calling_process_name();
   3937 
   3938     switch ( event )
   3939     {
   3940         case BTA_HL_CTRL_ENABLE_CFM_EVT:
   3941             BTIF_TRACE_DEBUG0("Rcv BTA_HL_CTRL_ENABLE_CFM_EVT");
   3942             BTIF_TRACE_DEBUG1("status=%d", p_data->enable_cfm.status);
   3943 
   3944             if (p_data->enable_cfm.status == BTA_HL_STATUS_OK)
   3945             {
   3946                 btif_hl_set_state(BTIF_HL_STATE_ENABLED);
   3947 
   3948 
   3949                 for (i=0; i < BTA_HL_NUM_APPS ; i ++)
   3950                 {
   3951                     p_acb = BTIF_HL_GET_APP_CB_PTR(i);
   3952                     if (p_acb->in_use && p_acb->reg_pending)
   3953                     {
   3954                         p_acb->reg_pending = FALSE;
   3955                         reg_param.dev_type = p_acb->dev_type;
   3956                         reg_param.sec_mask = BTA_SEC_AUTHENTICATE | BTA_SEC_ENCRYPT;
   3957                         reg_param.p_srv_name = p_acb->srv_name;
   3958                         reg_param.p_srv_desp = p_acb->srv_desp;
   3959                         reg_param.p_provider_name = p_acb->provider_name;
   3960 
   3961                         BTIF_TRACE_DEBUG1("Register pending app_id=%d", p_acb->app_id);
   3962                         btif_hl_proc_reg_request (i, p_acb->app_id, &reg_param, btif_hl_cback);
   3963                     }
   3964                 }
   3965             }
   3966 
   3967             break;
   3968         case BTA_HL_CTRL_DISABLE_CFM_EVT:
   3969             BTIF_TRACE_DEBUG0("Rcv BTA_HL_CTRL_DISABLE_CFM_EVT");
   3970             BTIF_TRACE_DEBUG1("status=%d",
   3971                               p_data->disable_cfm.status);
   3972 
   3973             if (p_data->disable_cfm.status == BTA_HL_STATUS_OK)
   3974             {
   3975                 memset(p_btif_hl_cb, 0, sizeof(btif_hl_cb_t));
   3976                 btif_hl_set_state(BTIF_HL_STATE_DISABLED);
   3977             }
   3978 
   3979             break;
   3980         default:
   3981             break;
   3982     }
   3983 }
   3984 
   3985 /*******************************************************************************
   3986 **
   3987 ** Function         btif_hl_ctrl_cback
   3988 **
   3989 ** Description      Callback function for HL control events
   3990 **
   3991 ** Returns          void
   3992 **
   3993 *******************************************************************************/
   3994 static void btif_hl_ctrl_cback(tBTA_HL_CTRL_EVT event, tBTA_HL_CTRL *p_data){
   3995     bt_status_t status;
   3996     int param_len = 0;
   3997 
   3998     BTIF_TRACE_DEBUG2("%s event %d", __FUNCTION__, event);
   3999     btif_hl_display_calling_process_name();
   4000 
   4001     switch ( event )
   4002     {
   4003         case BTA_HL_CTRL_ENABLE_CFM_EVT:
   4004         case BTA_HL_CTRL_DISABLE_CFM_EVT:
   4005             param_len = sizeof(tBTA_HL_CTRL_ENABLE_DISABLE);
   4006             break;
   4007         default:
   4008             break;
   4009     }
   4010 
   4011     status = btif_transfer_context(btif_hl_upstreams_ctrl_evt, (uint16_t)event, (void*)p_data, param_len, NULL);
   4012     ASSERTC(status == BT_STATUS_SUCCESS, "context transfer failed", status);
   4013 }
   4014 /*******************************************************************************
   4015 **
   4016 ** Function         connect_channel
   4017 **
   4018 ** Description     connect a data channel
   4019 **
   4020 ** Returns         bt_status_t
   4021 **
   4022 *******************************************************************************/
   4023 static bt_status_t connect_channel(int app_id, bt_bdaddr_t *bd_addr, int mdep_cfg_index, int *channel_id){
   4024     UINT8                   app_idx, mcl_idx;
   4025     btif_hl_app_cb_t        *p_acb = NULL;
   4026     btif_hl_pending_chan_cb_t   *p_pcb = NULL;
   4027     btif_hl_mcl_cb_t        *p_mcb=NULL;
   4028     bt_status_t             status = BT_STATUS_SUCCESS;
   4029     tBTA_HL_DCH_OPEN_PARAM  dch_open;
   4030     BD_ADDR                 bda;
   4031     UINT8 i;
   4032 
   4033     CHECK_BTHL_INIT();
   4034     BTIF_TRACE_EVENT1("%s", __FUNCTION__);
   4035     btif_hl_display_calling_process_name();
   4036 
   4037 
   4038     for (i=0; i<6; i++)
   4039     {
   4040         bda[i] = (UINT8) bd_addr->address[i];
   4041     }
   4042     if (btif_hl_find_app_idx(((UINT8)app_id), &app_idx))
   4043     {
   4044         p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx);
   4045         if (btif_hl_find_mcl_idx(app_idx, bda , &mcl_idx))
   4046         {
   4047             p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
   4048             if (p_mcb->is_connected)
   4049             {
   4050                 dch_open.ctrl_psm = p_mcb->ctrl_psm;
   4051                 dch_open.local_mdep_id = p_acb->sup_feature.mdep[mdep_cfg_index].mdep_id;
   4052                 BTIF_TRACE_DEBUG4("connect_channel: app_idx =%d, mdep_cfg_indx =%d, mdep_id =%d app_id= %d", app_idx,
   4053                                                 mdep_cfg_index, dch_open.local_mdep_id, app_id);
   4054                 if (btif_hl_find_peer_mdep_id(p_acb->app_id, p_mcb->bd_addr,
   4055                                               p_acb->sup_feature.mdep[mdep_cfg_index].mdep_cfg.mdep_role,
   4056                                               p_acb->sup_feature.mdep[mdep_cfg_index].mdep_cfg.data_cfg[0].data_type, &dch_open.peer_mdep_id ))
   4057                 {
   4058                     dch_open.local_cfg = p_acb->channel_type[mdep_cfg_index];
   4059                     if ((p_acb->sup_feature.mdep[mdep_cfg_index].mdep_cfg.mdep_role == BTA_HL_MDEP_ROLE_SOURCE)
   4060                         && !btif_hl_is_the_first_reliable_existed(app_idx,mcl_idx))
   4061                     {
   4062                         dch_open.local_cfg = BTA_HL_DCH_CFG_RELIABLE;
   4063                     }
   4064                     dch_open.sec_mask = (BTA_SEC_AUTHENTICATE | BTA_SEC_ENCRYPT);
   4065 
   4066                     if( !btif_hl_dch_open(p_acb->app_id, bda, &dch_open,
   4067                                               mdep_cfg_index, BTIF_HL_PEND_DCH_OP_OPEN, channel_id ))
   4068                     {
   4069                         status = BT_STATUS_FAIL;
   4070                         BTIF_TRACE_EVENT1("%s loc0 status = BT_STATUS_FAIL", __FUNCTION__);
   4071                     }
   4072                 }
   4073                 else
   4074                 {
   4075                     p_mcb->cch_oper = BTIF_HL_CCH_OP_MDEP_FILTERING;
   4076 
   4077                     p_pcb = BTIF_HL_GET_PCB_PTR(app_idx, mcl_idx);
   4078                     p_pcb->in_use = TRUE;
   4079                     p_pcb->mdep_cfg_idx = mdep_cfg_index;
   4080                     memcpy(p_pcb->bd_addr, bda, sizeof(BD_ADDR));
   4081                     p_pcb->op = BTIF_HL_PEND_DCH_OP_OPEN;
   4082                     BTA_HlSdpQuery(app_id,p_acb->app_handle, bda);
   4083                 }
   4084             }
   4085             else
   4086             {
   4087                 status = BT_STATUS_FAIL;
   4088             }
   4089         }
   4090         else
   4091         {
   4092             p_acb->filter.num_elems =1;
   4093             p_acb->filter.elem[0].data_type = p_acb->sup_feature.mdep[mdep_cfg_index].mdep_cfg.data_cfg[mdep_cfg_index].data_type;
   4094             if (p_acb->sup_feature.mdep[mdep_cfg_index].mdep_cfg.mdep_role == BTA_HL_MDEP_ROLE_SINK)
   4095                 p_acb->filter.elem[0].peer_mdep_role = BTA_HL_MDEP_ROLE_SOURCE;
   4096             else
   4097                 p_acb->filter.elem[0].peer_mdep_role = BTA_HL_MDEP_ROLE_SINK;
   4098 
   4099             if ( !btif_hl_cch_open(p_acb->app_id, bda, 0, mdep_cfg_index,
   4100                                    BTIF_HL_PEND_DCH_OP_OPEN,
   4101                                    channel_id))
   4102             {
   4103                 status = BT_STATUS_FAIL;
   4104             }
   4105         }
   4106     }
   4107     else
   4108     {
   4109         status = BT_STATUS_FAIL;
   4110     }
   4111 
   4112     BTIF_TRACE_DEBUG3("%s status=%d channel_id=0x%08x", __FUNCTION__, status, *channel_id);
   4113 
   4114     return status;
   4115 }
   4116 /*******************************************************************************
   4117 **
   4118 ** Function         destroy_channel
   4119 **
   4120 ** Description      destroy a data channel
   4121 **
   4122 ** Returns         bt_status_t
   4123 **
   4124 *******************************************************************************/
   4125 static bt_status_t destroy_channel(int channel_id){
   4126     UINT8 app_idx, mcl_idx, mdl_idx, mdl_cfg_idx, app_id, mdep_cfg_idx;
   4127     bt_status_t status = BT_STATUS_SUCCESS;
   4128     btif_hl_mdl_cfg_t     *p_mdl;
   4129     btif_hl_mcl_cb_t     *p_mcb;
   4130     btif_hl_mdl_cb_t     *p_dcb;
   4131     btif_hl_app_cb_t     *p_acb;
   4132 
   4133     CHECK_BTHL_INIT();
   4134     BTIF_TRACE_EVENT2("%s channel_id=0x%08x", __FUNCTION__, channel_id);
   4135     btif_hl_display_calling_process_name();
   4136 
   4137 
   4138     if (btif_hl_if_channel_setup_pending(channel_id, &app_idx, &mcl_idx))
   4139     {
   4140         btif_hl_dch_abort(app_idx, mcl_idx);
   4141     }
   4142     else
   4143     {
   4144         if (btif_hl_find_mdl_cfg_idx_using_channel_id(channel_id, &app_idx, &mdl_cfg_idx))
   4145  //       if(btif_hl_find_mdl_idx_using_channel_id(channel_id, &app_idx,&mcl_idx, &mdl_idx))
   4146         {
   4147             p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx);
   4148             if (!p_acb->delete_mdl.active)
   4149             {
   4150                 p_mdl =BTIF_HL_GET_MDL_CFG_PTR(app_idx, mdl_cfg_idx);
   4151                 p_acb->delete_mdl.active = TRUE;
   4152                 p_acb->delete_mdl.mdl_id = p_mdl->base.mdl_id;
   4153                 p_acb->delete_mdl.channel_id = channel_id;
   4154                 p_acb->delete_mdl.mdep_cfg_idx = p_mdl->extra.mdep_cfg_idx;
   4155                 memcpy(p_acb->delete_mdl.bd_addr, p_mdl->base.peer_bd_addr,sizeof(BD_ADDR));
   4156 
   4157                 if (btif_hl_find_mcl_idx(app_idx, p_mdl->base.peer_bd_addr, &mcl_idx))
   4158                 {
   4159                     p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
   4160                     if (p_mcb->is_connected)
   4161                     {
   4162                         BTIF_TRACE_DEBUG1("calling BTA_HlDeleteMdl mdl_id=%d",p_acb->delete_mdl.mdl_id );
   4163                         BTA_HlDeleteMdl(p_mcb->mcl_handle, p_acb->delete_mdl.mdl_id);
   4164                     }
   4165                     else
   4166                     {
   4167                         status = BT_STATUS_FAIL;
   4168                     }
   4169                 }
   4170                 else
   4171                 {
   4172                     BTIF_TRACE_DEBUG0("btif_hl_delete_mdl calling btif_hl_cch_open"  );
   4173                     mdep_cfg_idx = p_mdl->extra.mdep_cfg_idx;
   4174                     p_acb->filter.num_elems =1;
   4175                     p_acb->filter.elem[0].data_type = p_acb->sup_feature.mdep[mdep_cfg_idx].mdep_cfg.data_cfg[mdep_cfg_idx].data_type;
   4176                     if (p_acb->sup_feature.mdep[mdep_cfg_idx].mdep_cfg.mdep_role == BTA_HL_MDEP_ROLE_SINK)
   4177                         p_acb->filter.elem[0].peer_mdep_role = BTA_HL_MDEP_ROLE_SOURCE;
   4178                     else
   4179                         p_acb->filter.elem[0].peer_mdep_role = BTA_HL_MDEP_ROLE_SINK;
   4180                     if (btif_hl_cch_open(p_acb->app_id, p_acb->delete_mdl.bd_addr, 0,
   4181                                          mdep_cfg_idx,
   4182                                          BTIF_HL_PEND_DCH_OP_DELETE_MDL, NULL))
   4183                     {
   4184                         status = BT_STATUS_FAIL;
   4185                     }
   4186                 }
   4187 
   4188                 if (  status == BT_STATUS_FAIL)
   4189                 {
   4190                     /* fail for now  */
   4191                     btif_hl_clean_delete_mdl(&p_acb->delete_mdl);
   4192                 }
   4193             }
   4194             else
   4195             {
   4196                 status = BT_STATUS_BUSY;
   4197             }
   4198         }
   4199         else
   4200         {
   4201             status = BT_STATUS_FAIL;
   4202         }
   4203 
   4204     }
   4205     return status;
   4206 }
   4207 /*******************************************************************************
   4208 **
   4209 ** Function         unregister_application
   4210 **
   4211 ** Description     unregister an HDP application
   4212 **
   4213 ** Returns         bt_status_t
   4214 **
   4215 *******************************************************************************/
   4216 static bt_status_t unregister_application(int app_id){
   4217     btif_hl_app_cb_t    *p_acb;
   4218     UINT8               app_idx;
   4219     int                 len;
   4220     bt_status_t         status = BT_STATUS_SUCCESS;
   4221     btif_hl_evt_cb_t    evt_param;
   4222 
   4223     CHECK_BTHL_INIT();
   4224     BTIF_TRACE_EVENT2("%s app_id=%d", __FUNCTION__, app_id);
   4225     btif_hl_display_calling_process_name();
   4226 
   4227     if (btif_hl_find_app_idx(((UINT8)app_id), &app_idx))
   4228     {
   4229         evt_param.unreg.app_idx = app_idx;
   4230         p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx);
   4231         reg_counter --;
   4232         len = sizeof(btif_hl_unreg_t);
   4233         status = btif_transfer_context (btif_hl_proc_cb_evt, BTIF_HL_UNREG_APP,
   4234                                         (char*) &evt_param, len, NULL);
   4235         ASSERTC(status == BT_STATUS_SUCCESS, "context transfer failed", status);
   4236     }
   4237     else
   4238     {
   4239         status  = BT_STATUS_FAIL;
   4240     }
   4241 
   4242     BTIF_TRACE_DEBUG1("de-reg return status=%d", status);
   4243     return status;
   4244 }
   4245 /*******************************************************************************
   4246 **
   4247 ** Function         register_application
   4248 **
   4249 ** Description     register an HDP application
   4250 **
   4251 ** Returns         bt_status_t
   4252 **
   4253 *******************************************************************************/
   4254 static bt_status_t register_application(bthl_reg_param_t *p_reg_param, int *app_id){
   4255     btif_hl_app_cb_t            *p_acb;
   4256     tBTA_HL_SUP_FEATURE         *p_sup;
   4257     tBTA_HL_MDEP_CFG            *p_cfg;
   4258     tBTA_HL_MDEP_DATA_TYPE_CFG  *p_data;
   4259     UINT8                       app_idx=0, i=0, pending_reg_idx=0;
   4260     bthl_mdep_cfg_t             *p_mdep_cfg;
   4261     bt_status_t                 status = BT_STATUS_SUCCESS;
   4262     btif_hl_evt_cb_t            evt_param;
   4263     int                         len;
   4264 
   4265     CHECK_BTHL_INIT();
   4266     BTIF_TRACE_EVENT1("%s", __FUNCTION__);
   4267     btif_hl_display_calling_process_name();
   4268 
   4269     if (btif_hl_get_state() == BTIF_HL_STATE_DISABLED)
   4270     {
   4271         btif_hl_init();
   4272         btif_hl_set_state(BTIF_HL_STATE_ENABLING);
   4273         BTA_HlEnable(btif_hl_ctrl_cback);
   4274     }
   4275 
   4276     if (!btif_hl_find_avail_app_idx(&app_idx))
   4277     {
   4278         BTIF_TRACE_ERROR0("Unable to allocate a new application control block");
   4279         return BT_STATUS_FAIL;
   4280     }
   4281 
   4282     p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx);
   4283     p_acb->in_use = TRUE;
   4284 
   4285 
   4286     p_acb->app_id = btif_hl_get_next_app_id();
   4287 
   4288     if (p_reg_param->application_name != NULL )
   4289         strncpy(p_acb->application_name, p_reg_param->application_name, BTIF_HL_APPLICATION_NAME_LEN);
   4290 
   4291     if (p_reg_param->provider_name != NULL )
   4292         strncpy(p_acb->provider_name, p_reg_param->provider_name, BTA_PROVIDER_NAME_LEN);
   4293 
   4294     if (p_reg_param->srv_name != NULL )
   4295         strncpy(p_acb->srv_name, p_reg_param->srv_name, BTA_SERVICE_NAME_LEN);
   4296 
   4297     if (p_reg_param->srv_desp != NULL )
   4298         strncpy(p_acb->srv_desp, p_reg_param->srv_desp, BTA_SERVICE_DESP_LEN);
   4299 
   4300     p_sup = &p_acb->sup_feature;
   4301     p_sup->advertize_source_sdp = TRUE;
   4302     p_sup->echo_cfg.max_rx_apdu_size = BTIF_HL_ECHO_MAX_TX_RX_APDU_SIZE;
   4303     p_sup->echo_cfg.max_tx_apdu_size = BTIF_HL_ECHO_MAX_TX_RX_APDU_SIZE;
   4304     p_sup->num_of_mdeps = p_reg_param->number_of_mdeps;
   4305 
   4306     for (i=0, p_mdep_cfg = p_reg_param->mdep_cfg ; i<  p_sup->num_of_mdeps; i++, p_mdep_cfg++  )
   4307     {
   4308         p_cfg = &p_sup->mdep[i].mdep_cfg;
   4309         p_cfg->num_of_mdep_data_types = 1;
   4310         p_data  = &p_cfg->data_cfg[0];
   4311 
   4312         if ( !btif_hl_get_bta_mdep_role(p_mdep_cfg->mdep_role, &(p_cfg->mdep_role)))
   4313         {
   4314             BTIF_TRACE_ERROR1("Invalid mdep_role=%d", p_mdep_cfg->mdep_role);
   4315             status = BT_STATUS_FAIL;
   4316             break;
   4317         }
   4318         else
   4319         {
   4320             if (p_cfg->mdep_role == BTA_HL_MDEP_ROLE_SINK )
   4321                 p_sup->app_role_mask |= BTA_HL_MDEP_ROLE_MASK_SINK;
   4322             else
   4323                 p_sup->app_role_mask |=  BTA_HL_MDEP_ROLE_MASK_SOURCE;
   4324 
   4325             if ( (p_sup->app_role_mask & BTA_HL_MDEP_ROLE_MASK_SINK) &&
   4326                  (p_sup->app_role_mask & BTA_HL_MDEP_ROLE_MASK_SINK) )
   4327             {
   4328                 p_acb->dev_type = BTA_HL_DEVICE_TYPE_DUAL;
   4329             }
   4330             else if ( p_sup->app_role_mask & BTA_HL_MDEP_ROLE_MASK_SINK )
   4331                 p_acb->dev_type = BTA_HL_DEVICE_TYPE_SINK;
   4332             else
   4333 
   4334                 p_acb->dev_type = BTA_HL_DEVICE_TYPE_SOURCE;
   4335 
   4336             p_data->data_type = (UINT16) p_mdep_cfg->data_type;
   4337             p_data->max_rx_apdu_size = btif_hl_get_max_rx_apdu_size(p_cfg->mdep_role, p_data->data_type);
   4338             p_data->max_tx_apdu_size = btif_hl_get_max_tx_apdu_size(p_cfg->mdep_role, p_data->data_type);
   4339 
   4340             if (p_mdep_cfg->mdep_description != NULL )
   4341                 strncpy(p_data->desp, p_mdep_cfg->mdep_description, BTA_SERVICE_DESP_LEN);
   4342 
   4343             if ( !btif_hl_get_bta_channel_type(p_mdep_cfg->channel_type, &(p_acb->channel_type[i])))
   4344             {
   4345                 BTIF_TRACE_ERROR1("Invalid channel_type=%d", p_mdep_cfg->channel_type);
   4346                 status = BT_STATUS_FAIL;
   4347                 break;
   4348             }
   4349         }
   4350     }
   4351 
   4352     if (status == BT_STATUS_SUCCESS)
   4353     {
   4354         *app_id = (int) p_acb->app_id;
   4355         evt_param.reg.app_idx = app_idx;
   4356         len = sizeof(btif_hl_reg_t);
   4357         p_acb->reg_pending = TRUE;
   4358         reg_counter++;
   4359         BTIF_TRACE_DEBUG2("calling btif_transfer_context status=%d app_id=%d", status, *app_id);
   4360         status = btif_transfer_context (btif_hl_proc_cb_evt, BTIF_HL_REG_APP,
   4361                                         (char*) &evt_param, len, NULL);
   4362         ASSERTC(status == BT_STATUS_SUCCESS, "context transfer failed", status);
   4363 
   4364     }
   4365     else
   4366     {
   4367         btif_hl_free_app_idx(app_idx);
   4368     }
   4369 
   4370     BTIF_TRACE_DEBUG2("register_application status=%d app_id=%d", status, *app_id);
   4371     return status;
   4372 }
   4373 
   4374 /*******************************************************************************
   4375 **
   4376 ** Function      btif_hl_save_mdl_cfg
   4377 **
   4378 ** Description  Save the MDL configuration
   4379 **
   4380 ** Returns      BOOLEAN
   4381 **
   4382 *******************************************************************************/
   4383 BOOLEAN  btif_hl_save_mdl_cfg(UINT8 mdep_id, UINT8 item_idx,
   4384                               tBTA_HL_MDL_CFG *p_mdl_cfg){
   4385     btif_hl_mdl_cfg_t   *p_mdl=NULL;
   4386     BOOLEAN             success = FALSE;
   4387     btif_hl_app_cb_t    *p_acb;
   4388     btif_hl_mcl_cb_t    *p_mcb;
   4389     UINT8               app_idx, mcl_idx, mdl_idx, len;
   4390     bt_status_t         bt_status;
   4391     btif_hl_evt_cb_t    evt_param;
   4392     int                 *p_channel_id;
   4393 
   4394     BTIF_TRACE_DEBUG6("%s mdep_id=%d item_idx=%d, local_mdep_id=%d mdl_id=0x%x dch_mode=%d",
   4395                       __FUNCTION__, mdep_id, item_idx, p_mdl_cfg->local_mdep_id,
   4396                       p_mdl_cfg->mdl_id, p_mdl_cfg->dch_mode );
   4397 
   4398     if(btif_hl_find_app_idx_using_mdepId(mdep_id,&app_idx))
   4399     {
   4400         p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx);
   4401         p_mdl = BTIF_HL_GET_MDL_CFG_PTR(app_idx, item_idx);
   4402         p_channel_id = BTIF_HL_GET_MDL_CFG_CHANNEL_ID_PTR(app_idx, item_idx);
   4403         if (p_mdl)
   4404         {
   4405             memcpy(&p_mdl->base, p_mdl_cfg, sizeof(tBTA_HL_MDL_CFG));
   4406             if (btif_hl_find_mcl_idx(app_idx, p_mdl->base.peer_bd_addr , &mcl_idx))
   4407             {
   4408                 p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
   4409                 if (p_mcb->pcb.in_use)
   4410                     *p_channel_id = p_mcb->pcb.channel_id;
   4411                 else
   4412                     *p_channel_id = btif_hl_get_next_channel_id(p_acb->app_id);
   4413                 p_mdl->extra.mdep_cfg_idx = p_mcb->pcb.mdep_cfg_idx;
   4414                 p_mdl->extra.data_type = p_acb->sup_feature.mdep[p_mcb->pcb.mdep_cfg_idx].mdep_cfg.data_cfg[0].data_type;
   4415 
   4416                 if (!btif_hl_find_peer_mdep_id(p_acb->app_id, p_mcb->bd_addr,
   4417                                                p_acb->sup_feature.mdep[p_mcb->pcb.mdep_cfg_idx].mdep_cfg.mdep_role,
   4418                                                p_acb->sup_feature.mdep[p_mcb->pcb.mdep_cfg_idx].mdep_cfg.data_cfg[0].data_type,
   4419                                                &p_mdl->extra.peer_mdep_id))
   4420                 {
   4421                     p_mdl->extra.peer_mdep_id = BTA_HL_INVALID_MDEP_ID;
   4422                 }
   4423                 BTIF_TRACE_DEBUG4("%s app_idx=%d item_idx=%d mld_id=0x%x",
   4424                                   __FUNCTION__, app_idx, item_idx, p_mdl->base.mdl_id);
   4425                 evt_param.update_mdl.app_idx = app_idx;
   4426                 len = sizeof(btif_hl_update_mdl_t);
   4427                 BTIF_TRACE_DEBUG1("send BTIF_HL_UPDATE_MDL event app_idx=%d  ",app_idx);
   4428                 if ((bt_status = btif_transfer_context (btif_hl_proc_cb_evt, BTIF_HL_UPDATE_MDL,
   4429                                                         (char*) &evt_param, len, NULL)) == BT_STATUS_SUCCESS)
   4430                 {
   4431                     success = TRUE;
   4432                 }
   4433                 ASSERTC(bt_status == BT_STATUS_SUCCESS, "context transfer failed", bt_status);
   4434             }
   4435         }
   4436     }
   4437     BTIF_TRACE_DEBUG2("%s success=%d  ",__FUNCTION__, success );
   4438 
   4439     return success;
   4440 }
   4441 
   4442 /*******************************************************************************
   4443 **
   4444 ** Function      btif_hl_delete_mdl_cfg
   4445 **
   4446 ** Description  Delete the MDL configuration
   4447 **
   4448 ** Returns      BOOLEAN
   4449 **
   4450 *******************************************************************************/
   4451 BOOLEAN  btif_hl_delete_mdl_cfg(UINT8 mdep_id, UINT8 item_idx){
   4452     btif_hl_mdl_cfg_t     *p_mdl=NULL;
   4453     BOOLEAN             success = FALSE;
   4454     btif_hl_app_cb_t      *p_acb;
   4455     UINT8               app_idx, len;
   4456     bt_status_t         bt_status;
   4457     btif_hl_evt_cb_t    evt_param;
   4458 
   4459     if(btif_hl_find_app_idx_using_mdepId(mdep_id,&app_idx))
   4460     {
   4461 
   4462         p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx);
   4463 
   4464         p_mdl = BTIF_HL_GET_MDL_CFG_PTR(app_idx, item_idx);
   4465         if (p_mdl)
   4466         {
   4467             memset(p_mdl, 0, sizeof(btif_hl_mdl_cfg_t));
   4468             evt_param.update_mdl.app_idx = app_idx;
   4469             len = sizeof(btif_hl_update_mdl_t);
   4470             BTIF_TRACE_DEBUG1("send BTIF_HL_UPDATE_MDL event app_idx=%d  ",app_idx);
   4471             if ((bt_status = btif_transfer_context (btif_hl_proc_cb_evt, BTIF_HL_UPDATE_MDL,
   4472                                                     (char*) &evt_param, len, NULL)) == BT_STATUS_SUCCESS)
   4473             {
   4474                 success = TRUE;
   4475             }
   4476             ASSERTC(bt_status == BT_STATUS_SUCCESS, "context transfer failed", bt_status);
   4477         }
   4478     }
   4479 
   4480     BTIF_TRACE_DEBUG2("%s success=%d  ",__FUNCTION__, success );
   4481     return success;
   4482 }
   4483 
   4484 /*******************************************************************************
   4485 **
   4486 ** Function         get_device_datatype
   4487 **
   4488 ** Description      Start SDP on remote device and look for Remote HDP Data type and role
   4489 **
   4490 ** Returns         bt_status_t
   4491 **
   4492 *******************************************************************************/
   4493 static bt_status_t get_device_datatype(int app_id, bt_bdaddr_t *bd_addr){
   4494     btif_hl_app_cb_t    *p_acb;
   4495     UINT8               app_idx;
   4496     bt_status_t         status = BT_STATUS_SUCCESS;
   4497     BD_ADDR             bda;
   4498     UINT8               i;
   4499 
   4500     CHECK_BTHL_INIT();
   4501     BTIF_TRACE_EVENT2("%s app_id=%d", __FUNCTION__, app_id);
   4502     btif_hl_display_calling_process_name();
   4503 
   4504     for (i=0; i<6; i++)
   4505     {
   4506         bda[i] = (UINT8) bd_addr->address[i];
   4507     }
   4508 
   4509     if (btif_hl_find_app_idx(((UINT8)app_id), &app_idx))
   4510     {
   4511         p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx);
   4512         BTA_HlSdpQuery(app_id,p_acb->app_handle,bda);
   4513     }
   4514     else
   4515     {
   4516         status  = BT_STATUS_FAIL;
   4517     }
   4518 
   4519     BTIF_TRACE_DEBUG1("de-reg return status=%d", status);
   4520     return status;
   4521 }
   4522 
   4523 /*******************************************************************************
   4524 **
   4525 ** Function         init
   4526 **
   4527 ** Description     initializes the hl interface
   4528 **
   4529 ** Returns         bt_status_t
   4530 **
   4531 *******************************************************************************/
   4532 static bt_status_t init( bthl_callbacks_t* callbacks ){
   4533     bt_status_t status = BT_STATUS_SUCCESS;
   4534 
   4535     BTIF_TRACE_EVENT1("%s", __FUNCTION__);
   4536     btif_hl_display_calling_process_name();
   4537     bt_hl_callbacks_cb = *callbacks;
   4538     bt_hl_callbacks = &bt_hl_callbacks_cb;
   4539     btif_hl_soc_thread_init();
   4540     reg_counter = 0;
   4541     return status;
   4542 }
   4543 /*******************************************************************************
   4544 **
   4545 ** Function         cleanup
   4546 **
   4547 ** Description      Closes the HL interface
   4548 **
   4549 ** Returns          void
   4550 **
   4551 *******************************************************************************/
   4552 static void  cleanup( void ){
   4553     BTIF_TRACE_EVENT1("%s", __FUNCTION__);
   4554     btif_hl_display_calling_process_name();
   4555     if (bt_hl_callbacks)
   4556     {
   4557         btif_disable_service(BTA_HDP_SERVICE_ID);
   4558         bt_hl_callbacks = NULL;
   4559         reg_counter = 0;
   4560     }
   4561 
   4562     btif_hl_disable();
   4563     btif_hl_close_select_thread();
   4564 }
   4565 
   4566 static const bthl_interface_t bthlInterface = {
   4567     sizeof(bthl_interface_t),
   4568     init,
   4569     register_application,
   4570     unregister_application,
   4571     connect_channel,
   4572     destroy_channel,
   4573     cleanup,
   4574 };
   4575 
   4576 
   4577 /*******************************************************************************
   4578 **
   4579 ** Function         btif_hl_get_interface
   4580 **
   4581 ** Description      Get the hl callback interface
   4582 **
   4583 ** Returns          bthf_interface_t
   4584 **
   4585 *******************************************************************************/
   4586 const bthl_interface_t *btif_hl_get_interface(){
   4587     BTIF_TRACE_EVENT1("%s", __FUNCTION__);
   4588     return &bthlInterface;
   4589 }
   4590 
   4591 /*******************************************************************************
   4592 **
   4593 ** Function btif_hl_update_maxfd
   4594 **
   4595 ** Description Update the max fd if the input fd is greater than the current max fd
   4596 **
   4597 ** Returns int
   4598 **
   4599 *******************************************************************************/
   4600 int btif_hl_update_maxfd( int max_org_s){
   4601     btif_hl_soc_cb_t      *p_scb = NULL;
   4602     int maxfd=0;
   4603 
   4604     BTIF_TRACE_DEBUG1("btif_hl_update_maxfd max_org_s= %d", max_org_s);
   4605 
   4606     maxfd = max_org_s;
   4607     if (!GKI_queue_is_empty(&soc_queue))
   4608     {
   4609         p_scb = (btif_hl_soc_cb_t *)GKI_getfirst((void *)&soc_queue);
   4610         if (maxfd < p_scb->max_s)
   4611         {
   4612             maxfd = p_scb->max_s;
   4613             BTIF_TRACE_DEBUG1("btif_hl_update_maxfd 1 maxfd=%d", maxfd);
   4614         }
   4615         while (p_scb != NULL)
   4616         {
   4617             if (maxfd < p_scb->max_s)
   4618             {
   4619                 maxfd = p_scb->max_s;
   4620                 BTIF_TRACE_DEBUG1("btif_hl_update_maxfd 2 maxfd=%d", maxfd);
   4621             }
   4622             p_scb = (btif_hl_soc_cb_t *)GKI_getnext((void *)p_scb );
   4623         }
   4624     }
   4625 
   4626     BTIF_TRACE_DEBUG1("btif_hl_update_maxfd final *p_max_s=%d", maxfd);
   4627     return maxfd;
   4628 }
   4629 /*******************************************************************************
   4630 **
   4631 ** Function btif_hl_get_socket_state
   4632 **
   4633 ** Description get socket state
   4634 **
   4635 ** Returns btif_hl_soc_state_t
   4636 **
   4637 *******************************************************************************/
   4638 btif_hl_soc_state_t btif_hl_get_socket_state(btif_hl_soc_cb_t *p_scb){
   4639     BTIF_TRACE_DEBUG1("btif_hl_get_socket_state state=%d", p_scb->state);
   4640     return p_scb->state;
   4641 }
   4642 /*******************************************************************************
   4643 **
   4644 ** Function btif_hl_set_socket_state
   4645 **
   4646 ** Description set socket state
   4647 **
   4648 ** Returns void
   4649 **
   4650 *******************************************************************************/
   4651 void btif_hl_set_socket_state(btif_hl_soc_cb_t *p_scb, btif_hl_soc_state_t new_state){
   4652     BTIF_TRACE_DEBUG2("btif_hl_set_socket_state %d---->%d", p_scb->state, new_state);
   4653     p_scb->state = new_state;
   4654 }
   4655 /*******************************************************************************
   4656 **
   4657 ** Function btif_hl_release_mcl_sockets
   4658 **
   4659 ** Description Release all sockets on the MCL
   4660 **
   4661 ** Returns void
   4662 **
   4663 *******************************************************************************/
   4664 void btif_hl_release_mcl_sockets(UINT8 app_idx, UINT8 mcl_idx){
   4665     btif_hl_soc_cb_t    *p_scb = NULL;
   4666     UINT8               i;
   4667     btif_hl_mdl_cb_t    *p_dcb;
   4668     BOOLEAN             found= FALSE;
   4669     BTIF_TRACE_DEBUG1("%s", __FUNCTION__);
   4670     for (i=0; i < BTA_HL_NUM_MDLS_PER_MCL ; i ++)
   4671     {
   4672         p_dcb = BTIF_HL_GET_MDL_CB_PTR(app_idx, mcl_idx, i);
   4673         if (p_dcb && p_dcb->in_use && p_dcb->p_scb)
   4674         {
   4675             BTIF_TRACE_DEBUG3("found socket for app_idx=%d mcl_id=%d, mdl_idx=%d", app_idx, mcl_idx, i);
   4676             btif_hl_set_socket_state (p_dcb->p_scb, BTIF_HL_SOC_STATE_W4_REL);
   4677             p_dcb->p_scb = NULL;
   4678             found = TRUE;
   4679         }
   4680     }
   4681     if (found)
   4682         btif_hl_select_close_connected();
   4683 }
   4684 /*******************************************************************************
   4685 **
   4686 ** Function btif_hl_release_socket
   4687 **
   4688 ** Description release a specified socket
   4689 **
   4690 ** Returns void
   4691 **
   4692 *******************************************************************************/
   4693 void btif_hl_release_socket(UINT8 app_idx, UINT8 mcl_idx, UINT8 mdl_idx){
   4694     btif_hl_soc_cb_t       *p_scb = NULL;
   4695     btif_hl_mdl_cb_t      *p_dcb = BTIF_HL_GET_MDL_CB_PTR(app_idx, mcl_idx, mdl_idx);
   4696 
   4697     BTIF_TRACE_DEBUG1("%s", __FUNCTION__);
   4698     BTIF_TRACE_DEBUG3("app_idx=%d mcl_idx=%d mdl_idx=%d",  app_idx, mcl_idx, mdl_idx  );
   4699 
   4700     if (p_dcb && p_dcb->p_scb)
   4701     {
   4702         p_scb = p_dcb->p_scb;
   4703         btif_hl_set_socket_state(p_scb,  BTIF_HL_SOC_STATE_W4_REL);
   4704         p_dcb->p_scb = NULL;
   4705         btif_hl_select_close_connected();
   4706     }
   4707 }
   4708 /*******************************************************************************
   4709 **
   4710 ** Function btif_hl_create_socket
   4711 **
   4712 ** Description create a socket
   4713 **
   4714 ** Returns BOOLEAN
   4715 **
   4716 *******************************************************************************/
   4717 BOOLEAN btif_hl_create_socket(UINT8 app_idx, UINT8 mcl_idx, UINT8 mdl_idx){
   4718     btif_hl_mcl_cb_t      *p_mcb = BTIF_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
   4719     btif_hl_mdl_cb_t      *p_dcb = BTIF_HL_GET_MDL_CB_PTR(app_idx, mcl_idx, mdl_idx);
   4720     btif_hl_soc_cb_t      *p_scb = NULL;
   4721     UINT8                 soc_idx;
   4722     BOOLEAN               status = FALSE;
   4723 
   4724     BTIF_TRACE_DEBUG1("%s", __FUNCTION__);
   4725 
   4726     if (p_dcb && ((p_scb = (btif_hl_soc_cb_t *)GKI_getbuf((UINT16)sizeof(btif_hl_soc_cb_t)))!=NULL))
   4727     {
   4728         if (socketpair(AF_UNIX, SOCK_STREAM, 0, p_scb->socket_id) >= 0)
   4729         {
   4730             BTIF_TRACE_DEBUG2("socket id[0]=%d id[1]=%d",p_scb->socket_id[0], p_scb->socket_id[1] );
   4731             p_dcb->p_scb = p_scb;
   4732             p_scb->app_idx = app_idx;
   4733             p_scb->mcl_idx = mcl_idx;
   4734             p_scb->mdl_idx = mdl_idx;
   4735             p_scb->channel_id = p_dcb->channel_id;
   4736             p_scb->mdep_cfg_idx = p_dcb->local_mdep_cfg_idx;
   4737             memcpy(p_scb->bd_addr, p_mcb->bd_addr,sizeof(BD_ADDR));
   4738             btif_hl_set_socket_state(p_scb,  BTIF_HL_SOC_STATE_W4_ADD);
   4739             p_scb->max_s = p_scb->socket_id[1];
   4740             GKI_enqueue(&soc_queue, (void *) p_scb);
   4741             btif_hl_select_wakeup();
   4742             status = TRUE;
   4743         }
   4744         else
   4745         {
   4746 
   4747             btif_hl_free_buf((void **)&p_scb);
   4748         }
   4749     }
   4750 
   4751     BTIF_TRACE_DEBUG2("%s status=%d", __FUNCTION__, status);
   4752     return status;
   4753 }
   4754 /*******************************************************************************
   4755 **
   4756 ** Function btif_hl_add_socket_to_set
   4757 **
   4758 ** Description Add a socket
   4759 **
   4760 ** Returns void
   4761 **
   4762 *******************************************************************************/
   4763 void btif_hl_add_socket_to_set( fd_set *p_org_set){
   4764     btif_hl_soc_cb_t                *p_scb = NULL;
   4765     btif_hl_mdl_cb_t                *p_dcb = NULL;
   4766     btif_hl_mcl_cb_t                *p_mcb = NULL;
   4767     btif_hl_app_cb_t                *p_acb = NULL;
   4768     btif_hl_evt_cb_t                evt_param;
   4769     bt_status_t                     status;
   4770     int                             len;
   4771 
   4772     BTIF_TRACE_DEBUG1("entering %s",__FUNCTION__);
   4773 
   4774     if (!GKI_queue_is_empty(&soc_queue))
   4775     {
   4776         p_scb = (btif_hl_soc_cb_t *)GKI_getfirst((void *)&soc_queue);
   4777         BTIF_TRACE_DEBUG1("btif_hl_add_socket_to_set first p_scb=0x%x", p_scb);
   4778         while (p_scb != NULL)
   4779         {
   4780             if (btif_hl_get_socket_state(p_scb) == BTIF_HL_SOC_STATE_W4_ADD)
   4781             {
   4782                 btif_hl_set_socket_state(p_scb,   BTIF_HL_SOC_STATE_W4_READ);
   4783                 FD_SET(p_scb->socket_id[1], p_org_set);
   4784                 BTIF_TRACE_DEBUG2("found and set socket_id=%d is_set=%d", p_scb->socket_id[1], FD_ISSET(p_scb->socket_id[1], p_org_set));
   4785                 p_mcb = BTIF_HL_GET_MCL_CB_PTR(p_scb->app_idx, p_scb->mcl_idx);
   4786                 p_dcb = BTIF_HL_GET_MDL_CB_PTR(p_scb->app_idx, p_scb->mcl_idx, p_scb->mdl_idx);
   4787                 p_acb = BTIF_HL_GET_APP_CB_PTR(p_scb->app_idx);
   4788                 if (p_mcb && p_dcb)
   4789                 {
   4790                     btif_hl_stop_timer_using_handle(p_mcb->mcl_handle);
   4791                     evt_param.chan_cb.app_id = p_acb->app_id;
   4792                     memcpy(evt_param.chan_cb.bd_addr, p_mcb->bd_addr, sizeof(BD_ADDR));
   4793                     evt_param.chan_cb.channel_id = p_dcb->channel_id;
   4794                     evt_param.chan_cb.fd = p_scb->socket_id[0];
   4795                     evt_param.chan_cb.mdep_cfg_index = (int ) p_dcb->local_mdep_cfg_idx;
   4796                     evt_param.chan_cb.cb_state = BTIF_HL_CHAN_CB_STATE_CONNECTED_PENDING;
   4797                     len = sizeof(btif_hl_send_chan_state_cb_t);
   4798                     status = btif_transfer_context (btif_hl_proc_cb_evt, BTIF_HL_SEND_CONNECTED_CB,
   4799                                                     (char*) &evt_param, len, NULL);
   4800                     ASSERTC(status == BT_STATUS_SUCCESS, "context transfer failed", status);
   4801                 }
   4802             }
   4803             p_scb = (btif_hl_soc_cb_t *)GKI_getnext((void *)p_scb );
   4804             BTIF_TRACE_DEBUG1("next p_scb=0x%x", p_scb);
   4805         }
   4806     }
   4807 
   4808     BTIF_TRACE_DEBUG1("leaving %s",__FUNCTION__);
   4809 }
   4810 /*******************************************************************************
   4811 **
   4812 ** Function btif_hl_close_socket
   4813 **
   4814 ** Description close a socket
   4815 **
   4816 ** Returns void
   4817 **
   4818 *******************************************************************************/
   4819 void btif_hl_close_socket( fd_set *p_org_set){
   4820     btif_hl_soc_cb_t                *p_scb = NULL;
   4821     BOOLEAN                         element_removed = FALSE;
   4822     btif_hl_mdl_cb_t                *p_dcb = NULL ;
   4823     btif_hl_app_cb_t                *p_acb = NULL ;
   4824     btif_hl_evt_cb_t                evt_param;
   4825     int                             len;
   4826     int                             app_idx;
   4827     bt_status_t                     status;
   4828 
   4829     BTIF_TRACE_DEBUG1("entering %s",__FUNCTION__);
   4830     if (!GKI_queue_is_empty(&soc_queue))
   4831     {
   4832         p_scb = (btif_hl_soc_cb_t *)GKI_getfirst((void *)&soc_queue);
   4833         while (p_scb != NULL)
   4834         {
   4835             if (btif_hl_get_socket_state(p_scb) == BTIF_HL_SOC_STATE_W4_REL)
   4836             {
   4837                 BTIF_TRACE_DEBUG3("app_idx=%d mcl_id=%d, mdl_idx=%d",
   4838                                   p_scb->app_idx, p_scb->mcl_idx, p_scb->mdl_idx);
   4839                 btif_hl_set_socket_state(p_scb,   BTIF_HL_SOC_STATE_IDLE);
   4840                 if (p_scb->socket_id[1] != -1)
   4841                 {
   4842                     FD_CLR(p_scb->socket_id[1] , p_org_set);
   4843                     shutdown(p_scb->socket_id[1], SHUT_RDWR);
   4844                     close(p_scb->socket_id[1]);
   4845 
   4846                     evt_param.chan_cb.app_id = (int) btif_hl_get_app_id(p_scb->channel_id);
   4847                     memcpy(evt_param.chan_cb.bd_addr, p_scb->bd_addr, sizeof(BD_ADDR));
   4848                     evt_param.chan_cb.channel_id = p_scb->channel_id;
   4849                     evt_param.chan_cb.fd = p_scb->socket_id[0];
   4850                     evt_param.chan_cb.mdep_cfg_index = (int ) p_scb->mdep_cfg_idx;
   4851                     evt_param.chan_cb.cb_state = BTIF_HL_CHAN_CB_STATE_DISCONNECTED_PENDING;
   4852                     len = sizeof(btif_hl_send_chan_state_cb_t);
   4853                     status = btif_transfer_context (btif_hl_proc_cb_evt, BTIF_HL_SEND_DISCONNECTED_CB,
   4854                                                     (char*) &evt_param, len, NULL);
   4855                     ASSERTC(status == BT_STATUS_SUCCESS, "context transfer failed", status);
   4856 
   4857 
   4858                 }
   4859             }
   4860             p_scb = (btif_hl_soc_cb_t *)GKI_getnext((void *)p_scb );
   4861             BTIF_TRACE_DEBUG1("while loop next p_scb=0x%x", p_scb);
   4862         }
   4863 
   4864         p_scb = (btif_hl_soc_cb_t *)GKI_getfirst((void *)&soc_queue);
   4865         while (p_scb != NULL)
   4866         {
   4867             if (btif_hl_get_socket_state(p_scb) == BTIF_HL_SOC_STATE_IDLE)
   4868             {
   4869                 p_dcb = BTIF_HL_GET_MDL_CB_PTR(p_scb->app_idx, p_scb->mcl_idx, p_scb->mdl_idx);
   4870                 BTIF_TRACE_DEBUG4("idle socket app_idx=%d mcl_id=%d, mdl_idx=%d p_dcb->in_use=%d",
   4871                                   p_scb->app_idx, p_scb->mcl_idx, p_scb->mdl_idx, p_dcb->in_use);
   4872                 GKI_remove_from_queue((void *)&soc_queue, p_scb);
   4873                 btif_hl_free_buf((void **)&p_scb);
   4874                 p_dcb->p_scb = NULL;
   4875                 element_removed = TRUE;
   4876             }
   4877             BTIF_TRACE_DEBUG2("element_removed=%d p_scb=0x%x", element_removed, p_scb);
   4878             if (element_removed)
   4879             {
   4880                 element_removed = FALSE;
   4881                 p_scb = (btif_hl_soc_cb_t *)GKI_getfirst((void *)&soc_queue);
   4882             }
   4883             else
   4884                 p_scb = (btif_hl_soc_cb_t *)GKI_getnext((void *)p_scb );
   4885 
   4886             BTIF_TRACE_DEBUG1("while loop p_scb=0x%x", p_scb);
   4887         }
   4888     }
   4889     BTIF_TRACE_DEBUG1("leaving %s",__FUNCTION__);
   4890 }
   4891 /*******************************************************************************
   4892 **
   4893 ** Function btif_hl_select_wakeup_callback
   4894 **
   4895 ** Description Select wakup callback to add or close a socket
   4896 **
   4897 ** Returns void
   4898 **
   4899 *******************************************************************************/
   4900 
   4901 void btif_hl_select_wakeup_callback( fd_set *p_org_set ,  int wakeup_signal){
   4902     BTIF_TRACE_DEBUG2("entering %s wakeup_signal=0x%04x",__FUNCTION__, wakeup_signal);
   4903 
   4904     if (wakeup_signal == btif_hl_signal_select_wakeup )
   4905     {
   4906         btif_hl_add_socket_to_set(p_org_set);
   4907     }
   4908     else if (wakeup_signal == btif_hl_signal_select_close_connected)
   4909     {
   4910         btif_hl_close_socket(p_org_set);
   4911     }
   4912     BTIF_TRACE_DEBUG1("leaving %s",__FUNCTION__);
   4913 }
   4914 
   4915 /*******************************************************************************
   4916 **
   4917 ** Function btif_hl_select_monitor_callback
   4918 **
   4919 ** Description Select monitor callback to check pending socket actions
   4920 **
   4921 ** Returns void
   4922 **
   4923 *******************************************************************************/
   4924 void btif_hl_select_monitor_callback( fd_set *p_cur_set , fd_set *p_org_set){
   4925     btif_hl_soc_cb_t      *p_scb = NULL;
   4926     btif_hl_mdl_cb_t      *p_dcb = NULL;
   4927     int r;
   4928 
   4929     BTIF_TRACE_DEBUG1("entering %s",__FUNCTION__);
   4930 
   4931     if (!GKI_queue_is_empty(&soc_queue))
   4932     {
   4933         p_scb = (btif_hl_soc_cb_t *)GKI_getfirst((void *)&soc_queue);
   4934         BTIF_TRACE_DEBUG0(" GKI queue is not empty ");
   4935         while (p_scb != NULL)
   4936         {
   4937             if (btif_hl_get_socket_state(p_scb) == BTIF_HL_SOC_STATE_W4_READ)
   4938             {
   4939                 if (FD_ISSET(p_scb->socket_id[1], p_cur_set))
   4940                 {
   4941                     BTIF_TRACE_DEBUG0("read data");
   4942                     BTIF_TRACE_DEBUG0("state= BTIF_HL_SOC_STATE_W4_READ");
   4943                     p_dcb = BTIF_HL_GET_MDL_CB_PTR(p_scb->app_idx, p_scb->mcl_idx, p_scb->mdl_idx);
   4944                     if (p_dcb->p_tx_pkt)
   4945                     {
   4946                         BTIF_TRACE_ERROR1("Rcv new pkt but the last pkt is still not been sent tx_size=%d", p_dcb->tx_size);
   4947                         btif_hl_free_buf((void **) &p_dcb->p_tx_pkt);
   4948                     }
   4949                     p_dcb->p_tx_pkt =  btif_hl_get_buf (p_dcb->mtu);
   4950                     if (p_dcb )
   4951                     {
   4952                         //do
   4953                         // {
   4954                         //     r = recv(p_scb->socket_id[1], p_dcb->p_tx_pkt, p_dcb->mtu , MSG_DONTWAIT));
   4955                         // } while (r == SOCKET_ERROR && errno == EINTR);
   4956 
   4957                         if ((r = (int)recv(p_scb->socket_id[1], p_dcb->p_tx_pkt, p_dcb->mtu , MSG_DONTWAIT)) > 0)
   4958                         {
   4959                             BTIF_TRACE_DEBUG1("btif_hl_select_monitor_callback send data r =%d", r);
   4960                             p_dcb->tx_size = r;
   4961                             BTIF_TRACE_DEBUG1("btif_hl_select_monitor_callback send data tx_size=%d", p_dcb->tx_size );
   4962                             BTA_HlSendData(p_dcb->mdl_handle, p_dcb->tx_size  );
   4963                         }
   4964 
   4965                         if (r <= 0 )
   4966                         {
   4967                             BTIF_TRACE_DEBUG1("btif_hl_select_monitor_callback  receive failed r=%d",r);
   4968                             BTA_HlDchClose(p_dcb->mdl_handle );
   4969                         }
   4970                     }
   4971                 }
   4972             }
   4973             p_scb = (btif_hl_soc_cb_t *)GKI_getnext((void *)p_scb );
   4974         }
   4975     }
   4976     else
   4977     {
   4978         BTIF_TRACE_DEBUG0("btif_hl_select_monitor_queue is empty");
   4979     }
   4980     BTIF_TRACE_DEBUG1("leaving %s",__FUNCTION__);
   4981 }
   4982 /*******************************************************************************
   4983 **
   4984 ** Function btif_hl_select_wakeup_init
   4985 **
   4986 ** Description select loop wakup init
   4987 **
   4988 ** Returns int
   4989 **
   4990 *******************************************************************************/
   4991 static inline int btif_hl_select_wakeup_init(fd_set* set){
   4992     BTIF_TRACE_DEBUG0("btif_hl_select_wakeup_init");
   4993     if (socketpair(AF_UNIX, SOCK_STREAM, 0, signal_fds) < 0)
   4994     {
   4995         BTIF_TRACE_ERROR1("socketpair failed: %s", strerror(errno));
   4996         return -1;
   4997     }
   4998 
   4999     BTIF_TRACE_DEBUG2("btif_hl_select_wakeup_init signal_fds[0]=%d signal_fds[1]=%d",signal_fds[0], signal_fds[1] );
   5000     FD_SET(signal_fds[0], set);
   5001 
   5002     return signal_fds[0];
   5003 }
   5004 
   5005 /*******************************************************************************
   5006 **
   5007 ** Function btif_hl_select_wakeup
   5008 **
   5009 ** Description send a signal to wakupo the select loop
   5010 **
   5011 ** Returns int
   5012 **
   5013 *******************************************************************************/
   5014 static inline int btif_hl_select_wakeup(void){
   5015     char sig_on = btif_hl_signal_select_wakeup;
   5016     BTIF_TRACE_DEBUG0("btif_hl_select_wakeup");
   5017     return send(signal_fds[1], &sig_on, sizeof(sig_on), 0);
   5018 }
   5019 
   5020 /*******************************************************************************
   5021 **
   5022 ** Function btif_hl_select_close_connected
   5023 **
   5024 ** Description send a signal to close a socket
   5025 **
   5026 ** Returns int
   5027 **
   5028 *******************************************************************************/
   5029 static inline int btif_hl_select_close_connected(void){
   5030     char sig_on = btif_hl_signal_select_close_connected;
   5031     BTIF_TRACE_DEBUG0("btif_hl_select_close_connected");
   5032     return send(signal_fds[1], &sig_on, sizeof(sig_on), 0);
   5033 }
   5034 
   5035 /*******************************************************************************
   5036 **
   5037 ** Function btif_hl_close_select_thread
   5038 **
   5039 ** Description send signal to close the thread and then close all signal FDs
   5040 **
   5041 ** Returns int
   5042 **
   5043 *******************************************************************************/
   5044 static inline int btif_hl_close_select_thread(void)
   5045 {
   5046     int result = 0;
   5047     char sig_on = btif_hl_signal_select_exit;
   5048     BTIF_TRACE_DEBUG0("btif_hl_signal_select_exit");
   5049     result = send(signal_fds[1], &sig_on, sizeof(sig_on), 0);
   5050     /* Wait for the select_thread_id to exit */
   5051     if (select_thread_id != -1) {
   5052         pthread_join(select_thread_id, NULL);
   5053         select_thread_id = -1;
   5054     }
   5055    /* Cleanup signal sockets */
   5056     if(signal_fds[0] != -1)
   5057     {
   5058         close(signal_fds[0]);
   5059         signal_fds[0] = -1;
   5060     }
   5061     if(signal_fds[1] != -1)
   5062     {
   5063         close(signal_fds[1]);
   5064         signal_fds[1] = -1;
   5065     }
   5066     return result;
   5067 }
   5068 
   5069 /*******************************************************************************
   5070 **
   5071 ** Function btif_hl_select_wake_reset
   5072 **
   5073 ** Description clear the received signal for the select loop
   5074 **
   5075 ** Returns int
   5076 **
   5077 *******************************************************************************/
   5078 static inline int btif_hl_select_wake_reset(void){
   5079     char sig_recv = 0;
   5080 
   5081     BTIF_TRACE_DEBUG0("btif_hl_select_wake_reset");
   5082     recv(signal_fds[0], &sig_recv, sizeof(sig_recv), MSG_WAITALL);
   5083     return(int)sig_recv;
   5084 }
   5085 /*******************************************************************************
   5086 **
   5087 ** Function btif_hl_select_wake_signaled
   5088 **
   5089 ** Description check whether a fd is set or not
   5090 **
   5091 ** Returns int
   5092 **
   5093 *******************************************************************************/
   5094 static inline int btif_hl_select_wake_signaled(fd_set* set){
   5095     BTIF_TRACE_DEBUG0("btif_hl_select_wake_signaled");
   5096     return FD_ISSET(signal_fds[0], set);
   5097 }
   5098 /*******************************************************************************
   5099 **
   5100 ** Function btif_hl_thread_cleanup
   5101 **
   5102 ** Description  shut down and clean up the select loop
   5103 **
   5104 ** Returns void
   5105 **
   5106 *******************************************************************************/
   5107 static void btif_hl_thread_cleanup(){
   5108     if (listen_s != -1)
   5109         close(listen_s);
   5110     if (connected_s != -1)
   5111     {
   5112         shutdown(connected_s, SHUT_RDWR);
   5113         close(connected_s);
   5114     }
   5115     listen_s = connected_s = -1;
   5116     BTIF_TRACE_DEBUG0("hl thread cleanup");
   5117 }
   5118 /*******************************************************************************
   5119 **
   5120 ** Function btif_hl_select_thread
   5121 **
   5122 ** Description the select loop
   5123 **
   5124 ** Returns void
   5125 **
   5126 *******************************************************************************/
   5127 static void *btif_hl_select_thread(void *arg){
   5128     fd_set org_set, curr_set;
   5129     int r, max_curr_s, max_org_s;
   5130 
   5131     BTIF_TRACE_DEBUG0("entered btif_hl_select_thread");
   5132     FD_ZERO(&org_set);
   5133     max_org_s = btif_hl_select_wakeup_init(&org_set);
   5134     BTIF_TRACE_DEBUG1("max_s=%d ", max_org_s);
   5135 
   5136     for (;;)
   5137     {
   5138         r = 0;
   5139         BTIF_TRACE_DEBUG0("set curr_set = org_set ");
   5140         curr_set = org_set;
   5141         max_curr_s = max_org_s;
   5142         int ret = select((max_curr_s + 1), &curr_set, NULL, NULL, NULL);
   5143         BTIF_TRACE_DEBUG1("select unblocked ret=%d", ret);
   5144         if (ret == -1)
   5145         {
   5146             BTIF_TRACE_DEBUG0("select() ret -1, exit the thread");
   5147             btif_hl_thread_cleanup();
   5148             select_thread_id = -1;
   5149             return 0;
   5150         }
   5151         else if (ret)
   5152         {
   5153             BTIF_TRACE_DEBUG1("btif_hl_select_wake_signaled, signal ret=%d", ret);
   5154             if (btif_hl_select_wake_signaled(&curr_set))
   5155             {
   5156                 r = btif_hl_select_wake_reset();
   5157                 BTIF_TRACE_DEBUG1("btif_hl_select_wake_signaled, signal:%d", r);
   5158                 if (r == btif_hl_signal_select_wakeup || r == btif_hl_signal_select_close_connected )
   5159                 {
   5160                     btif_hl_select_wakeup_callback(&org_set, r);
   5161                 }
   5162                 else if( r == btif_hl_signal_select_exit)
   5163                 {
   5164                     btif_hl_thread_cleanup();
   5165                     BTIF_TRACE_DEBUG0("Exit hl_select_thread for btif_hl_signal_select_exit");
   5166                     return 0;
   5167                 }
   5168             }
   5169 
   5170             btif_hl_select_monitor_callback(&curr_set, &org_set);
   5171             max_org_s = btif_hl_update_maxfd(max_org_s);
   5172         }
   5173         else
   5174             BTIF_TRACE_DEBUG1("no data, select ret: %d\n", ret);
   5175     }
   5176     BTIF_TRACE_DEBUG0("leaving hl_select_thread");
   5177     return 0;
   5178 }
   5179 
   5180 /*******************************************************************************
   5181 **
   5182 ** Function create_thread
   5183 **
   5184 ** Description creat a select loop
   5185 **
   5186 ** Returns pthread_t
   5187 **
   5188 *******************************************************************************/
   5189 static inline pthread_t create_thread(void *(*start_routine)(void *), void * arg){
   5190     BTIF_TRACE_DEBUG0("create_thread: entered");
   5191     pthread_attr_t thread_attr;
   5192 
   5193     pthread_attr_init(&thread_attr);
   5194     pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_JOINABLE);
   5195     pthread_t thread_id = -1;
   5196     if ( pthread_create(&thread_id, &thread_attr, start_routine, arg)!=0 )
   5197     {
   5198         BTIF_TRACE_ERROR1("pthread_create : %s", strerror(errno));
   5199         return -1;
   5200     }
   5201     BTIF_TRACE_DEBUG0("create_thread: thread created successfully");
   5202     return thread_id;
   5203 }
   5204 
   5205 /*******************************************************************************
   5206 **
   5207 ** Function         btif_hl_soc_thread_init
   5208 **
   5209 ** Description      HL select loop init function.
   5210 **
   5211 ** Returns          void
   5212 **
   5213 *******************************************************************************/
   5214 void btif_hl_soc_thread_init(void){
   5215     BTIF_TRACE_DEBUG1("%s", __FUNCTION__);
   5216     GKI_init_q(&soc_queue);
   5217     select_thread_id = create_thread(btif_hl_select_thread, NULL);
   5218 }
   5219 /*******************************************************************************
   5220 **
   5221 ** Function btif_hl_load_mdl_config
   5222 **
   5223 ** Description load the MDL configuation from the application control block
   5224 **
   5225 ** Returns BOOLEAN
   5226 **
   5227 *******************************************************************************/
   5228 BOOLEAN btif_hl_load_mdl_config (UINT8 app_id, UINT8 buffer_size,
   5229                                  tBTA_HL_MDL_CFG *p_mdl_buf ){
   5230     UINT8 app_idx;
   5231     BOOLEAN result = FALSE;
   5232     btif_hl_app_cb_t          *p_acb;
   5233     tBTA_HL_MDL_CFG *p;
   5234     int i;
   5235     BTIF_TRACE_DEBUG1("%s", __FUNCTION__);
   5236 
   5237     if (btif_hl_find_app_idx(app_id, &app_idx))
   5238     {
   5239         p_acb  = BTIF_HL_GET_APP_CB_PTR(app_idx);
   5240         for (i=0, p=p_mdl_buf; i<buffer_size; i++, p++)
   5241         {
   5242             memcpy(p, &p_acb->mdl_cfg[i].base, sizeof(tBTA_HL_MDL_CFG));
   5243         }
   5244         result = TRUE;
   5245     }
   5246 
   5247     BTIF_TRACE_DEBUG1("result=%d", result);
   5248     return result;
   5249 }
   5250