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