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