Home | History | Annotate | Download | only in hl
      1 /******************************************************************************
      2  *
      3  *  Copyright (C) 2003-2012 Broadcom Corporation
      4  *
      5  *  Licensed under the Apache License, Version 2.0 (the "License");
      6  *  you may not use this file except in compliance with the License.
      7  *  You may obtain a copy of the License at:
      8  *
      9  *  http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *  Unless required by applicable law or agreed to in writing, software
     12  *  distributed under the License is distributed on an "AS IS" BASIS,
     13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *  See the License for the specific language governing permissions and
     15  *  limitations under the License.
     16  *
     17  ******************************************************************************/
     18 
     19 /******************************************************************************
     20  *
     21  *  This file implements utility functions for the HeaLth device profile
     22  *  (HL).
     23  *
     24  ******************************************************************************/
     25 
     26 #include <stdio.h>
     27 #include <string.h>
     28 
     29 #include "bt_target.h"
     30 #if defined(HL_INCLUDED) && (HL_INCLUDED == TRUE)
     31 
     32 
     33 #include "gki.h"
     34 #include "utl.h"
     35 #include "bd.h"
     36 #include "bta_fs_api.h"
     37 #include "bta_hl_int.h"
     38 #include "bta_hl_co.h"
     39 #include "mca_defs.h"
     40 #include "mca_api.h"
     41 
     42 
     43 /*******************************************************************************
     44 **
     45 ** Function      bta_hl_set_ctrl_psm_for_dch
     46 **
     47 ** Description    This function set the control PSM for the DCH setup
     48 **
     49 ** Returns     BOOLEAN - TRUE - control PSM setting is successful
     50 *******************************************************************************/
     51 BOOLEAN bta_hl_set_ctrl_psm_for_dch(UINT8 app_idx, UINT8 mcl_idx,
     52                                     UINT8 mdl_idx, UINT16 ctrl_psm)
     53 {
     54     tBTA_HL_MCL_CB *p_mcb  = BTA_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
     55     BOOLEAN success = TRUE, update_ctrl_psm = FALSE;
     56 
     57     if (p_mcb->sdp.num_recs)
     58     {
     59         if (p_mcb->ctrl_psm != ctrl_psm)
     60         {
     61             /* can not use a different ctrl PSM than the current one*/
     62             success = FALSE;
     63         }
     64     }
     65     else
     66     {
     67         /* No SDP info control i.e. channel was opened by the peer */
     68         update_ctrl_psm = TRUE;
     69     }
     70 
     71     if (success && update_ctrl_psm)
     72     {
     73         p_mcb->ctrl_psm = ctrl_psm;
     74     }
     75 
     76 
     77 #if BTA_HL_DEBUG == TRUE
     78     if (!success)
     79     {
     80         APPL_TRACE_DEBUG4("bta_hl_set_ctrl_psm_for_dch num_recs=%d success=%d update_ctrl_psm=%d ctrl_psm=0x%x ",
     81                           p_mcb->sdp.num_recs, success, update_ctrl_psm, ctrl_psm );
     82     }
     83 #endif
     84 
     85     return success;
     86 }
     87 
     88 
     89 /*******************************************************************************
     90 **
     91 ** Function      bta_hl_find_sdp_idx_using_ctrl_psm
     92 **
     93 ** Description
     94 **
     95 ** Returns      UINT8 pool_id
     96 **
     97 *******************************************************************************/
     98 BOOLEAN bta_hl_find_sdp_idx_using_ctrl_psm(tBTA_HL_SDP *p_sdp,
     99                                            UINT16 ctrl_psm,
    100                                            UINT8 *p_sdp_idx)
    101 {
    102     BOOLEAN found=FALSE;
    103     tBTA_HL_SDP_REC     *p_rec;
    104     UINT8 i;
    105 
    106     if (ctrl_psm != 0)
    107     {
    108         for (i=0; i<p_sdp->num_recs; i++)
    109         {
    110             p_rec = &p_sdp->sdp_rec[i];
    111             if (p_rec->ctrl_psm == ctrl_psm)
    112             {
    113                 *p_sdp_idx = i;
    114                 found = TRUE;
    115                 break;
    116             }
    117         }
    118     }
    119     else
    120     {
    121         *p_sdp_idx = 0;
    122         found = TRUE;
    123     }
    124 
    125 #if BTA_HL_DEBUG == TRUE
    126     if (!found)
    127     {
    128         APPL_TRACE_DEBUG3("bta_hl_find_sdp_idx_using_ctrl_psm found=%d sdp_idx=%d ctrl_psm=0x%x ",
    129                           found, *p_sdp_idx, ctrl_psm );
    130     }
    131 #endif
    132     return found;
    133 }
    134 
    135 /*******************************************************************************
    136 **
    137 ** Function      bta_hl_set_user_tx_pool_id
    138 **
    139 ** Description  This function sets the user tx pool id
    140 **
    141 ** Returns      UINT8 pool_id
    142 **
    143 *******************************************************************************/
    144 
    145 UINT8 bta_hl_set_user_tx_pool_id(UINT16 max_tx_size)
    146 {
    147     UINT8 pool_id;
    148 
    149     if (max_tx_size > GKI_get_pool_bufsize (OBX_FCR_TX_POOL_ID))
    150     {
    151         pool_id = BTA_HL_LRG_DATA_POOL_ID;
    152     }
    153     else
    154     {
    155         pool_id = L2CAP_DEFAULT_ERM_POOL_ID;
    156     }
    157 
    158 #if BTA_HL_DEBUG == TRUE
    159     APPL_TRACE_DEBUG3("bta_hl_set_user_rx_pool_id pool_id=%d max_tx_size=%d default_ertm_pool_size=%d",
    160                       pool_id, max_tx_size, GKI_get_pool_bufsize (OBX_FCR_TX_POOL_ID));
    161 #endif
    162 
    163     return pool_id;
    164 }
    165 
    166 /*******************************************************************************
    167 **
    168 ** Function      bta_hl_set_user_rx_pool_id
    169 **
    170 ** Description  This function sets the user trx pool id
    171 **
    172 ** Returns      UINT8 pool_id
    173 **
    174 *******************************************************************************/
    175 
    176 UINT8 bta_hl_set_user_rx_pool_id(UINT16 mtu)
    177 {
    178     UINT8 pool_id;
    179 
    180     if (mtu > GKI_get_pool_bufsize (OBX_FCR_RX_POOL_ID))
    181     {
    182         pool_id = BTA_HL_LRG_DATA_POOL_ID;
    183     }
    184     else
    185     {
    186         pool_id = L2CAP_DEFAULT_ERM_POOL_ID;
    187     }
    188 
    189 #if BTA_HL_DEBUG == TRUE
    190     APPL_TRACE_DEBUG3("bta_hl_set_user_rx_pool_id pool_id=%d mtu=%d default_ertm_pool_size=%d",
    191                       pool_id, mtu, GKI_get_pool_bufsize (OBX_FCR_RX_POOL_ID));
    192 #endif
    193 
    194     return pool_id;
    195 }
    196 
    197 
    198 
    199 /*******************************************************************************
    200 **
    201 ** Function      bta_hl_set_tx_win_size
    202 **
    203 ** Description  This function sets the tx window size
    204 **
    205 ** Returns      UINT8 tx_win_size
    206 **
    207 *******************************************************************************/
    208 UINT8 bta_hl_set_tx_win_size(UINT16 mtu, UINT16 mps)
    209 {
    210     UINT8 tx_win_size;
    211 
    212     if (mtu <= mps)
    213     {
    214         tx_win_size =1;
    215     }
    216     else
    217     {
    218         if (mps > 0)
    219         {
    220             tx_win_size = (mtu/mps)+1;
    221         }
    222         else
    223         {
    224             APPL_TRACE_ERROR0("The MPS is zero");
    225             tx_win_size = 10;
    226         }
    227     }
    228 
    229 #if BTA_HL_DEBUG == TRUE
    230     APPL_TRACE_DEBUG3("bta_hl_set_tx_win_size win_size=%d mtu=%d mps=%d",
    231                       tx_win_size, mtu, mps);
    232 #endif
    233     return tx_win_size;
    234 }
    235 
    236 /*******************************************************************************
    237 **
    238 ** Function      bta_hl_set_mps
    239 **
    240 ** Description  This function sets the MPS
    241 **
    242 ** Returns      UINT16 MPS
    243 **
    244 *******************************************************************************/
    245 UINT16 bta_hl_set_mps(UINT16 mtu)
    246 {
    247     UINT16 mps;
    248     if (mtu > BTA_HL_L2C_MPS)
    249     {
    250         mps = BTA_HL_L2C_MPS;
    251     }
    252     else
    253     {
    254         mps = mtu;
    255     }
    256 #if BTA_HL_DEBUG == TRUE
    257     APPL_TRACE_DEBUG2("bta_hl_set_mps mps=%d mtu=%d",
    258                       mps, mtu);
    259 #endif
    260     return mps;
    261 }
    262 
    263 
    264 /*******************************************************************************
    265 **
    266 ** Function      bta_hl_clean_mdl_cb
    267 **
    268 ** Description  This function clean up the specified MDL control block
    269 **
    270 ** Returns      void
    271 **
    272 *******************************************************************************/
    273 void bta_hl_clean_mdl_cb(UINT8 app_idx, UINT8 mcl_idx, UINT8 mdl_idx)
    274 {
    275     tBTA_HL_MDL_CB      *p_dcb  = BTA_HL_GET_MDL_CB_PTR(app_idx, mcl_idx, mdl_idx);
    276 #if BTA_HL_DEBUG == TRUE
    277     APPL_TRACE_DEBUG3("bta_hl_clean_mdl_cb app_idx=%d mcl_idx=%d mdl_idx=%d",
    278                       app_idx, mcl_idx, mdl_idx);
    279 #endif
    280     utl_freebuf((void **) &p_dcb->p_tx_pkt);
    281     utl_freebuf((void **) &p_dcb->p_rx_pkt);
    282     utl_freebuf((void **) &p_dcb->p_echo_tx_pkt);
    283     utl_freebuf((void **) &p_dcb->p_echo_rx_pkt);
    284 
    285     memset((void *)p_dcb, 0 , sizeof(tBTA_HL_MDL_CB));
    286 }
    287 
    288 
    289 /*******************************************************************************
    290 **
    291 ** Function      bta_hl_get_buf
    292 **
    293 ** Description  This function allocate a buffer based on the specified data size
    294 **
    295 ** Returns      BT_HDR *.
    296 **
    297 *******************************************************************************/
    298 BT_HDR * bta_hl_get_buf(UINT16 data_size)
    299 {
    300     BT_HDR *p_new;
    301     UINT16 size = data_size + L2CAP_MIN_OFFSET + BT_HDR_SIZE;
    302 
    303     if (size < GKI_MAX_BUF_SIZE)
    304     {
    305         p_new = (BT_HDR *)GKI_getbuf(size);
    306     }
    307     else
    308     {
    309         p_new = (BT_HDR *) GKI_getpoolbuf(BTA_HL_LRG_DATA_POOL_ID);
    310     }
    311 
    312     if (p_new)
    313     {
    314         p_new->len = data_size;
    315         p_new->offset = L2CAP_MIN_OFFSET;
    316     }
    317 
    318     return p_new;
    319 }
    320 
    321 /*******************************************************************************
    322 **
    323 ** Function      bta_hl_find_service_in_db
    324 **
    325 ** Description  This function check the specified service class(es) can be find in
    326 **              the received SDP database
    327 **
    328 ** Returns      BOOLEAN TRUE - found
    329 **                      FALSE - not found
    330 **
    331 *******************************************************************************/
    332 BOOLEAN bta_hl_find_service_in_db( UINT8 app_idx, UINT8 mcl_idx,
    333                                    UINT16 service_uuid,
    334                                    tSDP_DISC_REC **pp_rec )
    335 {
    336     tBTA_HL_MCL_CB          *p_mcb = BTA_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
    337     BOOLEAN found = TRUE;
    338 
    339     switch (service_uuid)
    340     {
    341         case UUID_SERVCLASS_HDP_SINK:
    342         case UUID_SERVCLASS_HDP_SOURCE:
    343             if ((*pp_rec = SDP_FindServiceInDb(p_mcb->p_db, service_uuid,
    344                                                *pp_rec)) == NULL)
    345             {
    346                 found = FALSE;
    347             }
    348             break;
    349         default:
    350             if (((*pp_rec = bta_hl_find_sink_or_src_srv_class_in_db(p_mcb->p_db,
    351                                                                     *pp_rec)) == NULL))
    352             {
    353                 found = FALSE;
    354             }
    355             break;
    356     }
    357     return found;
    358 }
    359 
    360 /*******************************************************************************
    361 **
    362 ** Function      bta_hl_get_service_uuids
    363 **
    364 **
    365 ** Description  This function finds the service class(es) for both CCH and DCH oeprations
    366 **
    367 ** Returns      UINT16 - service_id
    368 **                       if service_uuid = 0xFFFF then it means service uuid
    369 **                       can be either Sink or Source
    370 **
    371 *******************************************************************************/
    372 UINT16 bta_hl_get_service_uuids(UINT8 sdp_oper, UINT8 app_idx, UINT8 mcl_idx,
    373                                 UINT8 mdl_idx )
    374 {
    375     tBTA_HL_MDL_CB          *p_dcb;
    376     UINT16                  service_uuid = 0xFFFF; /* both Sink and Source */
    377 
    378     switch (sdp_oper)
    379     {
    380 
    381         case BTA_HL_SDP_OP_DCH_OPEN_INIT:
    382         case BTA_HL_SDP_OP_DCH_RECONNECT_INIT:
    383             p_dcb = BTA_HL_GET_MDL_CB_PTR(app_idx, mcl_idx, mdl_idx);
    384             if (p_dcb->local_mdep_id != BTA_HL_ECHO_TEST_MDEP_ID)
    385             {
    386                 if (p_dcb->peer_mdep_role == BTA_HL_MDEP_ROLE_SINK)
    387                 {
    388                     service_uuid = UUID_SERVCLASS_HDP_SINK;
    389                 }
    390                 else
    391                 {
    392                     service_uuid = UUID_SERVCLASS_HDP_SOURCE;
    393                 }
    394             }
    395             break;
    396         case BTA_HL_SDP_OP_CCH_INIT:
    397         default:
    398             /* use default that is both Sink and Source */
    399             break;
    400     }
    401 #if BTA_HL_DEBUG == TRUE
    402     APPL_TRACE_DEBUG1("bta_hl_get_service_uuids service_uuid=0x%x",service_uuid );
    403 #endif
    404     return service_uuid;
    405 }
    406 
    407 /*******************************************************************************
    408 **
    409 ** Function      bta_hl_find_echo_cfg_rsp
    410 **
    411 **
    412 ** Description  This function finds the configuration response for the echo test
    413 **
    414 ** Returns      BOOLEAN - TRUE found
    415 **                        FALSE not found
    416 **
    417 *******************************************************************************/
    418 BOOLEAN bta_hl_find_echo_cfg_rsp(UINT8 app_idx, UINT8 mcl_idx, UINT8 mdep_idx, UINT8 cfg,
    419                                  UINT8 *p_cfg_rsp)
    420 {
    421     tBTA_HL_APP_CB      *p_acb = BTA_HL_GET_APP_CB_PTR(app_idx);
    422     tBTA_HL_MDEP        *p_mdep= &p_acb->sup_feature.mdep[mdep_idx];
    423     BOOLEAN             status =TRUE;
    424 
    425     if (p_mdep->mdep_id == BTA_HL_ECHO_TEST_MDEP_ID)
    426     {
    427         if ((cfg == BTA_HL_DCH_CFG_RELIABLE) || (cfg == BTA_HL_DCH_CFG_STREAMING))
    428         {
    429             *p_cfg_rsp = cfg;
    430         }
    431         else if (cfg == BTA_HL_DCH_CFG_NO_PREF )
    432         {
    433             *p_cfg_rsp = BTA_HL_DEFAULT_ECHO_TEST_SRC_DCH_CFG;
    434         }
    435         else
    436         {
    437             status = FALSE;
    438             APPL_TRACE_ERROR0("Inavlid echo cfg value");
    439         }
    440         return status;
    441     }
    442 
    443 #if BTA_HL_DEBUG == TRUE
    444     if (!status)
    445     {
    446         APPL_TRACE_DEBUG4("bta_hl_find_echo_cfg_rsp status=failed app_idx=%d mcl_idx=%d mdep_idx=%d cfg=%d",
    447                           app_idx, mcl_idx, mdep_idx, cfg);
    448     }
    449 #endif
    450 
    451     return status;
    452 }
    453 
    454 /*******************************************************************************
    455 **
    456 ** Function      bta_hl_validate_dch_cfg
    457 **
    458 ** Description  This function validate the DCH configuration
    459 **
    460 ** Returns      BOOLEAN - TRUE cfg is valid
    461 **                        FALSE not valid
    462 **
    463 *******************************************************************************/
    464 BOOLEAN bta_hl_validate_cfg(UINT8 app_idx, UINT8 mcl_idx, UINT8 mdl_idx,
    465                             UINT8 cfg)
    466 {
    467     tBTA_HL_MDL_CB      *p_dcb = BTA_HL_GET_MDL_CB_PTR(app_idx, mcl_idx, mdl_idx);
    468     BOOLEAN is_valid =FALSE;
    469 
    470 
    471     if (!bta_hl_is_the_first_reliable_existed(app_idx, mcl_idx) &&
    472         (cfg != BTA_HL_DCH_CFG_RELIABLE))
    473     {
    474         APPL_TRACE_ERROR0("the first DCH should be a reliable channel");
    475         return is_valid;
    476     }
    477 
    478     switch (p_dcb->local_cfg)
    479     {
    480         case BTA_HL_DCH_CFG_NO_PREF:
    481 
    482             if ((cfg == BTA_HL_DCH_CFG_RELIABLE) || (cfg == BTA_HL_DCH_CFG_STREAMING))
    483             {
    484                 is_valid = TRUE;
    485             }
    486             break;
    487         case BTA_HL_DCH_CFG_RELIABLE:
    488         case BTA_HL_DCH_CFG_STREAMING:
    489             if (p_dcb->local_cfg == cfg )
    490             {
    491                 is_valid = TRUE;
    492             }
    493             break;
    494         default:
    495             break;
    496     }
    497 
    498 #if BTA_HL_DEBUG == TRUE
    499     if (!is_valid)
    500     {
    501         APPL_TRACE_DEBUG2("bta_hl_validate_dch_open_cfg is_valid=%d, cfg=%d", is_valid, cfg );
    502     }
    503 #endif
    504     return is_valid;
    505 }
    506 
    507 /*******************************************************************************
    508 **
    509 ** Function       bta_hl_find_cch_cb_indexes
    510 **
    511 ** Description  This function finds the indexes needed for the CCH state machine
    512 **
    513 ** Returns      BOOLEAN - TRUE found
    514 **                        FALSE not found
    515 **
    516 *******************************************************************************/
    517 BOOLEAN bta_hl_find_cch_cb_indexes(tBTA_HL_DATA *p_msg,
    518                                    UINT8 *p_app_idx,
    519                                    UINT8  *p_mcl_idx)
    520 {
    521     BOOLEAN found = FALSE;
    522     tBTA_HL_MCL_CB      *p_mcb;
    523     UINT8               app_idx, mcl_idx;
    524 
    525     switch (p_msg->hdr.event)
    526     {
    527         case BTA_HL_CCH_SDP_OK_EVT:
    528         case BTA_HL_CCH_SDP_FAIL_EVT:
    529             app_idx = p_msg->cch_sdp.app_idx;
    530             mcl_idx = p_msg->cch_sdp.mcl_idx;
    531             found = TRUE;
    532             break;
    533 
    534         case BTA_HL_MCA_CONNECT_IND_EVT:
    535 
    536             if (bta_hl_find_app_idx_using_handle(p_msg->mca_evt.app_handle, &app_idx))
    537             {
    538                 if (bta_hl_find_mcl_idx(app_idx, p_msg->mca_evt.mca_data.connect_ind.bd_addr, &mcl_idx))
    539                 {
    540                     /* local initiated */
    541                     found = TRUE;
    542                 }
    543                 else if (!bta_hl_find_mcl_idx_using_handle(p_msg->mca_evt.mcl_handle, &app_idx, &mcl_idx)&&
    544                          bta_hl_find_avail_mcl_idx(app_idx, &mcl_idx))
    545                 {
    546                     /* remote initiated */
    547                     p_mcb = BTA_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
    548                     p_mcb->in_use = TRUE;
    549                     p_mcb->cch_oper = BTA_HL_CCH_OP_REMOTE_OPEN;
    550                     found = TRUE;
    551                 }
    552             }
    553             break;
    554 
    555         case BTA_HL_MCA_DISCONNECT_IND_EVT:
    556 
    557             if (bta_hl_find_mcl_idx_using_handle(p_msg->mca_evt.mcl_handle, &app_idx,  &mcl_idx))
    558             {
    559                 found = TRUE;
    560             }
    561             else if (bta_hl_find_app_idx_using_handle(p_msg->mca_evt.app_handle, &app_idx) &&
    562                      bta_hl_find_mcl_idx(app_idx, p_msg->mca_evt.mca_data.disconnect_ind.bd_addr, &mcl_idx))
    563             {
    564                 found = TRUE;
    565             }
    566 
    567             if (found)
    568             {
    569                 p_mcb = BTA_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
    570                 if (p_mcb->cch_oper != BTA_HL_CCH_OP_LOCAL_CLOSE)
    571                 {
    572                     p_mcb->cch_oper = BTA_HL_CCH_OP_REMOTE_CLOSE;
    573                 }
    574             }
    575             break;
    576 
    577         case BTA_HL_MCA_RSP_TOUT_IND_EVT:
    578 
    579             if (bta_hl_find_mcl_idx_using_handle(p_msg->mca_evt.mcl_handle, &app_idx,  &mcl_idx))
    580             {
    581                 found = TRUE;
    582             }
    583 
    584             if (found)
    585             {
    586                 p_mcb = BTA_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
    587                 if (p_mcb->cch_oper != BTA_HL_CCH_OP_REMOTE_CLOSE)
    588                 {
    589                     p_mcb->cch_oper = BTA_HL_CCH_OP_LOCAL_CLOSE;
    590                 }
    591             }
    592             break;
    593         default:
    594             break;
    595     }
    596 
    597 
    598     if (found)
    599     {
    600         *p_app_idx = app_idx;
    601         *p_mcl_idx = mcl_idx;
    602     }
    603 
    604 #if BTA_HL_DEBUG == TRUE
    605     if (!found)
    606     {
    607         APPL_TRACE_DEBUG4("bta_hl_find_cch_cb_indexes event=%s found=%d app_idx=%d mcl_idx=%d",
    608                           bta_hl_evt_code(p_msg->hdr.event), found, app_idx, mcl_idx);
    609     }
    610 #endif
    611 
    612     return found;
    613 }
    614 
    615 /*******************************************************************************
    616 **
    617 ** Function       bta_hl_find_dch_cb_indexes
    618 **
    619 ** Description  This function finds the indexes needed for the DCH state machine
    620 **
    621 ** Returns      BOOLEAN - TRUE found
    622 **                        FALSE not found
    623 **
    624 *******************************************************************************/
    625 BOOLEAN bta_hl_find_dch_cb_indexes(tBTA_HL_DATA *p_msg,
    626                                    UINT8 *p_app_idx,
    627                                    UINT8 *p_mcl_idx,
    628                                    UINT8 *p_mdl_idx)
    629 {
    630     BOOLEAN         found = FALSE;
    631     tBTA_HL_MCL_CB  *p_mcb;
    632     UINT8           app_idx, mcl_idx, mdl_idx;
    633 
    634     switch (p_msg->hdr.event)
    635     {
    636         case BTA_HL_MCA_CREATE_CFM_EVT:
    637             if (bta_hl_find_mcl_idx_using_handle(p_msg->mca_evt.mcl_handle, &app_idx, &mcl_idx) &&
    638                 bta_hl_find_mdl_idx( app_idx,  mcl_idx,  p_msg->mca_evt.mca_data.create_cfm.mdl_id, &mdl_idx))
    639             {
    640                 found = TRUE;
    641             }
    642             break;
    643 
    644         case BTA_HL_MCA_CREATE_IND_EVT:
    645         case BTA_HL_MCA_RECONNECT_IND_EVT:
    646             if (bta_hl_find_mcl_idx_using_handle(p_msg->mca_evt.mcl_handle, &app_idx, &mcl_idx) &&
    647                 bta_hl_find_avail_mdl_idx( app_idx,  mcl_idx, &mdl_idx))
    648             {
    649                 found = TRUE;
    650             }
    651             break;
    652 
    653         case BTA_HL_MCA_OPEN_CFM_EVT:
    654             if (bta_hl_find_mcl_idx_using_handle(p_msg->mca_evt.mcl_handle, &app_idx, &mcl_idx) &&
    655                 bta_hl_find_mdl_idx( app_idx,  mcl_idx,  p_msg->mca_evt.mca_data.open_cfm.mdl_id, &mdl_idx))
    656             {
    657                 found = TRUE;
    658             }
    659             break;
    660 
    661         case BTA_HL_MCA_OPEN_IND_EVT:
    662             if (bta_hl_find_mcl_idx_using_handle(p_msg->mca_evt.mcl_handle, &app_idx, &mcl_idx) &&
    663                 bta_hl_find_mdl_idx( app_idx,  mcl_idx,  p_msg->mca_evt.mca_data.open_ind.mdl_id, &mdl_idx))
    664             {
    665                 found = TRUE;
    666             }
    667             break;
    668 
    669         case BTA_HL_MCA_CLOSE_CFM_EVT:
    670 
    671             if (bta_hl_find_mdl_idx_using_handle((tBTA_HL_MDL_HANDLE)p_msg->mca_evt.mca_data.close_cfm.mdl,
    672                                                  &app_idx, &mcl_idx, &mdl_idx))
    673             {
    674                 found = TRUE;
    675             }
    676             break;
    677         case BTA_HL_MCA_CLOSE_IND_EVT:
    678 
    679             if (bta_hl_find_mdl_idx_using_handle((tBTA_HL_MDL_HANDLE)p_msg->mca_evt.mca_data.close_ind.mdl,
    680                                                  &app_idx, &mcl_idx, &mdl_idx))
    681             {
    682                 found = TRUE;
    683             }
    684             break;
    685         case BTA_HL_API_SEND_DATA_EVT:
    686 
    687             if (bta_hl_find_mdl_idx_using_handle(p_msg->api_send_data.mdl_handle,
    688                                                  &app_idx, &mcl_idx, &mdl_idx ))
    689             {
    690                 found = TRUE;
    691             }
    692 
    693             break;
    694 
    695         case BTA_HL_MCA_CONG_CHG_EVT:
    696 
    697             if (bta_hl_find_mdl_idx_using_handle((tBTA_HL_MDL_HANDLE)p_msg->mca_evt.mca_data.cong_chg.mdl,
    698                                                  &app_idx, &mcl_idx, &mdl_idx ))
    699             {
    700                 found = TRUE;
    701             }
    702 
    703             break;
    704 
    705         case BTA_HL_MCA_RCV_DATA_EVT:
    706             app_idx = p_msg->mca_rcv_data_evt.app_idx;
    707             mcl_idx = p_msg->mca_rcv_data_evt.mcl_idx;
    708             mdl_idx = p_msg->mca_rcv_data_evt.mdl_idx;
    709             found = TRUE;
    710             break;
    711         case BTA_HL_DCH_RECONNECT_EVT:
    712         case BTA_HL_DCH_OPEN_EVT:
    713         case BTA_HL_DCH_ECHO_TEST_EVT:
    714         case BTA_HL_DCH_SDP_FAIL_EVT:
    715             app_idx = p_msg->dch_sdp.app_idx;
    716             mcl_idx = p_msg->dch_sdp.mcl_idx;
    717             mdl_idx = p_msg->dch_sdp.mdl_idx;
    718             found = TRUE;
    719             break;
    720         case BTA_HL_MCA_RECONNECT_CFM_EVT:
    721             if (bta_hl_find_mcl_idx_using_handle(p_msg->mca_evt.mcl_handle, &app_idx, &mcl_idx) &&
    722                 bta_hl_find_mdl_idx( app_idx,  mcl_idx,  p_msg->mca_evt.mca_data.reconnect_cfm.mdl_id, &mdl_idx))
    723             {
    724                 found = TRUE;
    725             }
    726             break;
    727 
    728 
    729         case BTA_HL_API_DCH_CREATE_RSP_EVT:
    730             if (bta_hl_find_mcl_idx_using_handle(p_msg->api_dch_create_rsp.mcl_handle, &app_idx, &mcl_idx)&&
    731                 bta_hl_find_mdl_idx( app_idx,  mcl_idx,p_msg->api_dch_create_rsp.mdl_id, &mdl_idx))
    732             {
    733                 found = TRUE;
    734             }
    735             break;
    736         case BTA_HL_MCA_ABORT_IND_EVT:
    737             if (bta_hl_find_mcl_idx_using_handle(p_msg->mca_evt.mcl_handle, &app_idx, &mcl_idx) &&
    738                 bta_hl_find_mdl_idx( app_idx,  mcl_idx,p_msg->mca_evt.mca_data.abort_ind.mdl_id, &mdl_idx))
    739             {
    740                 found = TRUE;
    741             }
    742             break;
    743         case BTA_HL_MCA_ABORT_CFM_EVT:
    744             if (bta_hl_find_mcl_idx_using_handle(p_msg->mca_evt.mcl_handle, &app_idx,  &mcl_idx) &&
    745                 bta_hl_find_mdl_idx( app_idx,  mcl_idx,  p_msg->mca_evt.mca_data.abort_cfm.mdl_id, &mdl_idx))
    746             {
    747                 found = TRUE;
    748             }
    749             break;
    750         case BTA_HL_CI_GET_TX_DATA_EVT:
    751         case BTA_HL_CI_PUT_RX_DATA_EVT:
    752             if (bta_hl_find_mdl_idx_using_handle(p_msg->ci_get_put_data.mdl_handle, &app_idx, &mcl_idx, &mdl_idx))
    753             {
    754                 found = TRUE;
    755             }
    756             break;
    757         case BTA_HL_CI_GET_ECHO_DATA_EVT:
    758         case BTA_HL_CI_PUT_ECHO_DATA_EVT:
    759             if (bta_hl_find_mcl_idx_using_handle(p_msg->ci_get_put_echo_data.mcl_handle, &app_idx, &mcl_idx))
    760             {
    761                 p_mcb = BTA_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
    762                 mdl_idx = p_mcb->echo_mdl_idx;
    763                 found = TRUE;
    764             }
    765             break;
    766 
    767         default:
    768             break;
    769 
    770     }
    771 
    772     if (found)
    773     {
    774         *p_app_idx = app_idx;
    775         *p_mcl_idx = mcl_idx;
    776         *p_mdl_idx = mdl_idx;
    777     }
    778 #if BTA_HL_DEBUG == TRUE
    779     if (!found)
    780     {
    781         APPL_TRACE_DEBUG5("bta_hl_find_dch_cb_indexes event=%s found=%d app_idx=%d mcl_idx=%d mdl_idx=%d",
    782                           bta_hl_evt_code(p_msg->hdr.event), found, *p_app_idx, *p_mcl_idx, *p_mdl_idx  );
    783     }
    784 #endif
    785 
    786     return found;
    787 }
    788 
    789 /*******************************************************************************
    790 **
    791 ** Function      bta_hl_allocate_mdl_id
    792 **
    793 ** Description  This function allocates a MDL ID
    794 **
    795 ** Returns      UINT16 - MDL ID
    796 **
    797 *******************************************************************************/
    798 UINT16  bta_hl_allocate_mdl_id(UINT8 app_idx, UINT8 mcl_idx, UINT8 mdl_idx )
    799 {
    800     UINT16  mdl_id=0;
    801     tBTA_HL_MCL_CB      *p_mcb = BTA_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
    802     BOOLEAN duplicate_id;
    803     UINT8 i, mdl_cfg_idx;
    804 
    805     do
    806     {
    807         duplicate_id = FALSE;
    808         mdl_id = ((mdl_id+1) & 0xFEFF);
    809         /* check mdl_ids that are used for the current conenctions */
    810         for (i=0; i< BTA_HL_NUM_MDLS_PER_MCL; i++)
    811         {
    812             if (p_mcb->mdl[i].in_use &&
    813                 (i != mdl_idx) &&
    814                 (p_mcb->mdl[i].mdl_id == mdl_id) )
    815             {
    816                 duplicate_id = TRUE;
    817                 break;
    818             }
    819         }
    820 
    821         if (duplicate_id)
    822         {
    823             /* start from the beginning to get another MDL value*/
    824             continue;
    825         }
    826         else
    827         {
    828             /* check mdl_ids that are stored in the persistent memory */
    829             if (bta_hl_find_mdl_cfg_idx(app_idx,mcl_idx, mdl_id, &mdl_cfg_idx))
    830             {
    831                 duplicate_id = TRUE;
    832             }
    833             else
    834             {
    835                 /* found a new MDL value */
    836                 break;
    837             }
    838         }
    839 
    840     }while (TRUE);
    841 
    842 #if BTA_HL_DEBUG == TRUE
    843     APPL_TRACE_DEBUG1("bta_hl_allocate_mdl OK mdl_id=%d",  mdl_id);
    844 #endif
    845     return mdl_id;
    846 }
    847 /*******************************************************************************
    848 **
    849 ** Function      bta_hl_find_mdl_idx
    850 **
    851 ** Description  This function finds the MDL index based on mdl_id
    852 **
    853 ** Returns      BOOLEAN TRUE-found
    854 **
    855 *******************************************************************************/
    856 BOOLEAN bta_hl_find_mdl_idx(UINT8 app_idx, UINT8 mcl_idx, UINT16 mdl_id,
    857                             UINT8 *p_mdl_idx)
    858 {
    859     tBTA_HL_MCL_CB      *p_mcb  = BTA_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
    860     BOOLEAN found=FALSE;
    861     UINT8 i;
    862 
    863     for (i=0; i < BTA_HL_NUM_MDLS_PER_MCL ; i ++)
    864     {
    865         if (p_mcb->mdl[i].in_use  &&
    866             (mdl_id !=0) &&
    867             (p_mcb->mdl[i].mdl_id== mdl_id))
    868         {
    869             found = TRUE;
    870             *p_mdl_idx = i;
    871             break;
    872         }
    873     }
    874 
    875 #if BTA_HL_DEBUG == TRUE
    876     if (!found)
    877     {
    878         APPL_TRACE_DEBUG3("bta_hl_find_mdl_idx found=%d mdl_id=%d mdl_idx=%d ",
    879                           found, mdl_id, i);
    880     }
    881 #endif
    882 
    883     return found;
    884 }
    885 
    886 /*******************************************************************************
    887 **
    888 ** Function      bta_hl_find_an_active_mdl_idx
    889 **
    890 ** Description  This function finds an active MDL
    891 **
    892 ** Returns      BOOLEAN TRUE-found
    893 **
    894 *******************************************************************************/
    895 BOOLEAN bta_hl_find_an_active_mdl_idx(UINT8 app_idx, UINT8 mcl_idx,
    896                                       UINT8 *p_mdl_idx)
    897 {
    898     tBTA_HL_MCL_CB      *p_mcb  = BTA_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
    899     BOOLEAN found=FALSE;
    900     UINT8 i;
    901 
    902     for (i=0; i < BTA_HL_NUM_MDLS_PER_MCL ; i ++)
    903     {
    904         if (p_mcb->mdl[i].in_use  &&
    905             (p_mcb->mdl[i].dch_state == BTA_HL_DCH_OPEN_ST))
    906         {
    907             found = TRUE;
    908             *p_mdl_idx = i;
    909             break;
    910         }
    911     }
    912 
    913 #if BTA_HL_DEBUG == TRUE
    914     if (found)
    915     {
    916         APPL_TRACE_DEBUG4("bta_hl_find_an_opened_mdl_idx found=%d app_idx=%d mcl_idx=%d mdl_idx=%d",
    917                           found, app_idx, mcl_idx, i);
    918     }
    919 #endif
    920 
    921     return found;
    922 }
    923 
    924 /*******************************************************************************
    925 **
    926 ** Function      bta_hl_find_dch_setup_mdl_idx
    927 **
    928 ** Description  This function finds a MDL which in the DCH setup state
    929 **
    930 ** Returns      BOOLEAN TRUE-found
    931 **
    932 *******************************************************************************/
    933 BOOLEAN bta_hl_find_dch_setup_mdl_idx(UINT8 app_idx, UINT8 mcl_idx,
    934                                       UINT8 *p_mdl_idx)
    935 {
    936     tBTA_HL_MCL_CB      *p_mcb  = BTA_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
    937     BOOLEAN found=FALSE;
    938     UINT8 i;
    939 
    940     for (i=0; i < BTA_HL_NUM_MDLS_PER_MCL ; i ++)
    941     {
    942         if (p_mcb->mdl[i].in_use  &&
    943             (p_mcb->mdl[i].dch_state == BTA_HL_DCH_OPENING_ST))
    944         {
    945             found = TRUE;
    946             *p_mdl_idx = i;
    947             break;
    948         }
    949     }
    950 
    951 #if BTA_HL_DEBUG == TRUE
    952     if (found)
    953     {
    954         APPL_TRACE_DEBUG4("bta_hl_find_dch_setup_mdl_idx found=%d app_idx=%d mcl_idx=%d mdl_idx=%d",
    955                           found, app_idx, mcl_idx, i);
    956     }
    957 #endif
    958 
    959     return found;
    960 }
    961 
    962 /*******************************************************************************
    963 **
    964 ** Function      bta_hl_find_an_in_use_mcl_idx
    965 **
    966 ** Description  This function finds an in-use MCL control block index
    967 **
    968 ** Returns      BOOLEAN TRUE-found
    969 **
    970 *******************************************************************************/
    971 BOOLEAN bta_hl_find_an_in_use_mcl_idx(UINT8 app_idx,
    972                                       UINT8 *p_mcl_idx)
    973 {
    974     tBTA_HL_MCL_CB      *p_mcb;
    975     BOOLEAN found=FALSE;
    976     UINT8 i;
    977 
    978     for (i=0; i < BTA_HL_NUM_MCLS ; i ++)
    979     {
    980         p_mcb  = BTA_HL_GET_MCL_CB_PTR(app_idx, i);
    981         if (p_mcb->in_use  &&
    982             (p_mcb->cch_state != BTA_HL_CCH_IDLE_ST))
    983         {
    984             found = TRUE;
    985             *p_mcl_idx = i;
    986             break;
    987         }
    988     }
    989 
    990 #if BTA_HL_DEBUG == TRUE
    991     if (found)
    992     {
    993         APPL_TRACE_DEBUG3("bta_hl_find_an_in_use_mcl_idx found=%d app_idx=%d mcl_idx=%d ",
    994                           found, app_idx, i);
    995     }
    996 #endif
    997 
    998     return found;
    999 }
   1000 
   1001 
   1002 /*******************************************************************************
   1003 **
   1004 ** Function      bta_hl_find_an_in_use_app_idx
   1005 **
   1006 ** Description  This function finds an in-use application control block index
   1007 **
   1008 ** Returns      BOOLEAN TRUE-found
   1009 **
   1010 *******************************************************************************/
   1011 BOOLEAN bta_hl_find_an_in_use_app_idx(UINT8 *p_app_idx)
   1012 {
   1013     tBTA_HL_APP_CB      *p_acb ;
   1014     BOOLEAN found=FALSE;
   1015     UINT8 i;
   1016 
   1017     for (i=0; i < BTA_HL_NUM_APPS ; i ++)
   1018     {
   1019         p_acb  = BTA_HL_GET_APP_CB_PTR(i);
   1020         if (p_acb->in_use)
   1021         {
   1022             found = TRUE;
   1023             *p_app_idx = i;
   1024             break;
   1025         }
   1026     }
   1027 
   1028 #if BTA_HL_DEBUG == TRUE
   1029     if (found)
   1030     {
   1031         APPL_TRACE_DEBUG2("bta_hl_find_an_in_use_app_idx found=%d app_idx=%d ",
   1032                           found, i);
   1033     }
   1034 #endif
   1035 
   1036     return found;
   1037 }
   1038 /*******************************************************************************
   1039 **
   1040 ** Function      bta_hl_find_app_idx
   1041 **
   1042 ** Description  This function finds the application control block index based on
   1043 **              the application ID
   1044 **
   1045 ** Returns      BOOLEAN TRUE-found
   1046 **
   1047 *******************************************************************************/
   1048 BOOLEAN bta_hl_find_app_idx(UINT8 app_id, UINT8 *p_app_idx)
   1049 {
   1050     BOOLEAN found=FALSE;
   1051     UINT8 i;
   1052 
   1053     for (i=0; i < BTA_HL_NUM_APPS ; i ++)
   1054     {
   1055         if (bta_hl_cb.acb[i].in_use &&
   1056             (bta_hl_cb.acb[i].app_id == app_id))
   1057         {
   1058             found = TRUE;
   1059             *p_app_idx = i;
   1060             break;
   1061         }
   1062     }
   1063 
   1064 #if BTA_HL_DEBUG == TRUE
   1065     APPL_TRACE_DEBUG3("bta_hl_find_app_idx found=%d app_id=%d idx=%d ",
   1066                       found, app_id, i);
   1067 #endif
   1068 
   1069     return found;
   1070 }
   1071 
   1072 
   1073 /*******************************************************************************
   1074 **
   1075 ** Function      bta_hl_find_app_idx_using_handle
   1076 **
   1077 ** Description  This function finds the application control block index based on
   1078 **              the application handle
   1079 **
   1080 ** Returns      BOOLEAN TRUE-found
   1081 **
   1082 *******************************************************************************/
   1083 BOOLEAN bta_hl_find_app_idx_using_handle(tBTA_HL_APP_HANDLE app_handle,
   1084                                          UINT8 *p_app_idx)
   1085 {
   1086     BOOLEAN found=FALSE;
   1087     UINT8 i;
   1088 
   1089     for (i=0; i < BTA_HL_NUM_APPS ; i ++)
   1090     {
   1091         if (bta_hl_cb.acb[i].in_use &&
   1092             (bta_hl_cb.acb[i].app_handle == app_handle))
   1093         {
   1094             found = TRUE;
   1095             *p_app_idx = i;
   1096             break;
   1097         }
   1098     }
   1099 
   1100 #if BTA_HL_DEBUG == TRUE
   1101     if (!found)
   1102     {
   1103         APPL_TRACE_DEBUG3("bta_hl_find_app_idx_using_mca_handle status=%d handle=%d app_idx=%d ",
   1104                           found, app_handle , i);
   1105     }
   1106 #endif
   1107 
   1108     return found;
   1109 }
   1110 
   1111 
   1112 /*******************************************************************************
   1113 **
   1114 ** Function      bta_hl_find_mcl_idx_using_handle
   1115 **
   1116 ** Description  This function finds the MCL control block index based on
   1117 **              the MCL handle
   1118 **
   1119 ** Returns      BOOLEAN TRUE-found
   1120 **
   1121 *******************************************************************************/
   1122 BOOLEAN bta_hl_find_mcl_idx_using_handle( tBTA_HL_MCL_HANDLE mcl_handle,
   1123                                           UINT8 *p_app_idx, UINT8 *p_mcl_idx)
   1124 {
   1125     tBTA_HL_APP_CB  *p_acb;
   1126     BOOLEAN         found=FALSE;
   1127     UINT8 i,j;
   1128 
   1129     for (i=0; i<BTA_HL_NUM_APPS; i++)
   1130     {
   1131         p_acb = BTA_HL_GET_APP_CB_PTR(i);
   1132         if (p_acb->in_use)
   1133         {
   1134             for (j=0; j < BTA_HL_NUM_MCLS ; j++)
   1135             {
   1136                 if ( p_acb->mcb[j].mcl_handle == mcl_handle )
   1137                 {
   1138                     found = TRUE;
   1139                     *p_app_idx = i;
   1140                     *p_mcl_idx = j;
   1141                     break;
   1142                 }
   1143             }
   1144         }
   1145     }
   1146 
   1147 #if BTA_HL_DEBUG == TRUE
   1148     if (!found)
   1149     {
   1150         APPL_TRACE_DEBUG3("bta_hl_find_mcl_idx_using_handle found=%d app_idx=%d mcl_idx=%d",
   1151                           found, i, j);
   1152     }
   1153 #endif
   1154     return found;
   1155 }
   1156 
   1157 /*******************************************************************************
   1158 **
   1159 ** Function      bta_hl_find_mcl_idx
   1160 **
   1161 ** Description  This function finds the MCL control block index based on
   1162 **              the peer BD address
   1163 **
   1164 ** Returns      BOOLEAN TRUE-found
   1165 **
   1166 *******************************************************************************/
   1167 BOOLEAN bta_hl_find_mcl_idx(UINT8 app_idx, BD_ADDR p_bd_addr, UINT8 *p_mcl_idx)
   1168 {
   1169     BOOLEAN found=FALSE;
   1170     UINT8 i;
   1171     tBTA_HL_MCL_CB  *p_mcb;
   1172 
   1173     for (i=0; i < BTA_HL_NUM_MCLS ; i ++)
   1174     {
   1175         p_mcb = BTA_HL_GET_MCL_CB_PTR(app_idx, i);
   1176 
   1177         if (bta_hl_cb.acb[app_idx].mcb[i].in_use &&
   1178             (!memcmp (bta_hl_cb.acb[app_idx].mcb[i].bd_addr, p_bd_addr, BD_ADDR_LEN)))
   1179         {
   1180             found = TRUE;
   1181             *p_mcl_idx = i;
   1182             break;
   1183         }
   1184     }
   1185 
   1186 #if BTA_HL_DEBUG == TRUE
   1187     if (!found)
   1188     {
   1189         APPL_TRACE_DEBUG2("bta_hl_find_mcl_idx found=%d idx=%d",
   1190                           found, i);
   1191     }
   1192 #endif
   1193     return found;
   1194 }
   1195 
   1196 
   1197 
   1198 /*******************************************************************************
   1199 **
   1200 ** Function      bta_hl_find_mdl_idx_using_handle
   1201 **
   1202 ** Description  This function finds the MDL control block index based on
   1203 **              the MDL handle
   1204 **
   1205 ** Returns      BOOLEAN TRUE-found
   1206 **
   1207 *******************************************************************************/
   1208 BOOLEAN bta_hl_find_mdl_idx_using_handle(tBTA_HL_MDL_HANDLE mdl_handle,
   1209                                          UINT8 *p_app_idx,UINT8 *p_mcl_idx,
   1210                                          UINT8 *p_mdl_idx)
   1211 {
   1212     tBTA_HL_APP_CB      *p_acb;
   1213     tBTA_HL_MCL_CB      *p_mcb;
   1214     tBTA_HL_MDL_CB      *p_dcb;
   1215     BOOLEAN found=FALSE;
   1216     UINT8 i,j,k;
   1217 
   1218     for (i=0; i < BTA_HL_NUM_APPS ; i ++)
   1219     {
   1220         p_acb = BTA_HL_GET_APP_CB_PTR(i);
   1221         if (p_acb->in_use)
   1222         {
   1223             for (j=0; j< BTA_HL_NUM_MCLS; j++)
   1224             {
   1225                 p_mcb = BTA_HL_GET_MCL_CB_PTR(i,j);
   1226                 if (p_mcb->in_use)
   1227                 {
   1228                     for (k=0; k< BTA_HL_NUM_MDLS_PER_MCL; k++)
   1229                     {
   1230                         p_dcb = BTA_HL_GET_MDL_CB_PTR(i,j,k);
   1231                         if (p_dcb->in_use)
   1232                         {
   1233                             if (p_dcb->mdl_handle == mdl_handle)
   1234                             {
   1235                                 found = TRUE;
   1236                                 *p_app_idx = i;
   1237                                 *p_mcl_idx =j;
   1238                                 *p_mdl_idx = k;
   1239                                 break;
   1240                             }
   1241                         }
   1242                     }
   1243                 }
   1244             }
   1245         }
   1246     }
   1247 
   1248 
   1249 #if BTA_HL_DEBUG == TRUE
   1250     if (!found)
   1251     {
   1252         APPL_TRACE_DEBUG2("bta_hl_find_mdl_idx_using_handle found=%d mdl_handle=%d  ",
   1253                           found, mdl_handle);
   1254     }
   1255 #endif
   1256     return found;
   1257 }
   1258 /*******************************************************************************
   1259 **
   1260 ** Function      bta_hl_is_the_first_reliable_existed
   1261 **
   1262 ** Description  This function checks whether the first reliable DCH channel
   1263 **              has been setup on the MCL or not
   1264 **
   1265 ** Returns      BOOLEAN - TRUE exist
   1266 **                        FALSE does not exist
   1267 **
   1268 *******************************************************************************/
   1269 BOOLEAN bta_hl_is_the_first_reliable_existed(UINT8 app_idx, UINT8 mcl_idx )
   1270 {
   1271     tBTA_HL_MCL_CB      *p_mcb = BTA_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
   1272     BOOLEAN is_existed =FALSE;
   1273     UINT8 i ;
   1274 
   1275     for (i=0; i< BTA_HL_NUM_MDLS_PER_MCL; i++)
   1276     {
   1277         if (p_mcb->mdl[i].in_use && p_mcb->mdl[i].is_the_first_reliable)
   1278         {
   1279             is_existed = TRUE;
   1280             break;
   1281         }
   1282     }
   1283 
   1284 #if BTA_HL_DEBUG == TRUE
   1285     APPL_TRACE_DEBUG1("bta_hl_is_the_first_reliable_existed is_existed=%d  ",is_existed );
   1286 #endif
   1287     return is_existed;
   1288 }
   1289 
   1290 /*******************************************************************************
   1291 **
   1292 ** Function      bta_hl_find_non_active_mdl_cfg
   1293 **
   1294 ** Description  This function finds a valid MDL configiration index and this
   1295 **              MDL ID is not active
   1296 **
   1297 ** Returns      BOOLEAN - TRUE found
   1298 **                        FALSE not found
   1299 **
   1300 *******************************************************************************/
   1301 BOOLEAN  bta_hl_find_non_active_mdl_cfg(UINT8 app_idx, UINT8 start_mdl_cfg_idx,
   1302                                         UINT8 *p_mdl_cfg_idx)
   1303 {
   1304 
   1305     tBTA_HL_MCL_CB      *p_mcb;
   1306     tBTA_HL_MDL_CB      *p_dcb;
   1307     tBTA_HL_MDL_CFG     *p_mdl;
   1308     BOOLEAN             mdl_in_use;
   1309     BOOLEAN             found = FALSE;
   1310     UINT8               i,j,k;
   1311 
   1312     for (i = start_mdl_cfg_idx; i< BTA_HL_NUM_MDL_CFGS; i++)
   1313     {
   1314         mdl_in_use = FALSE;
   1315         p_mdl = BTA_HL_GET_MDL_CFG_PTR(app_idx, i);
   1316         for (j=0; j< BTA_HL_NUM_MCLS; j++)
   1317         {
   1318             p_mcb  = BTA_HL_GET_MCL_CB_PTR(app_idx, j);
   1319             if (p_mcb->in_use &&
   1320                 !memcmp(p_mdl->peer_bd_addr,p_mcb->bd_addr,BD_ADDR_LEN))
   1321             {
   1322 
   1323                 for (k=0; k<BTA_HL_NUM_MDLS_PER_MCL; k++)
   1324                 {
   1325                     p_dcb  = BTA_HL_GET_MDL_CB_PTR(app_idx, j, k);
   1326 
   1327                     if (p_dcb->in_use &&  p_mdl->mdl_id == p_dcb->mdl_id)
   1328                     {
   1329                         mdl_in_use = TRUE;
   1330                         break;
   1331                     }
   1332                 }
   1333             }
   1334 
   1335             if (mdl_in_use)
   1336             {
   1337                 break;
   1338             }
   1339         }
   1340 
   1341         if (!mdl_in_use)
   1342         {
   1343             *p_mdl_cfg_idx = i;
   1344             found = TRUE;
   1345             break;
   1346         }
   1347     }
   1348 
   1349     return found;
   1350 }
   1351 
   1352 /*******************************************************************************
   1353 **
   1354 ** Function      bta_hl_find_mdl_cfg_idx
   1355 **
   1356 ** Description  This function finds an available MDL configiration index
   1357 **
   1358 ** Returns      BOOLEAN - TRUE found
   1359 **                        FALSE not found
   1360 **
   1361 *******************************************************************************/
   1362 BOOLEAN  bta_hl_find_avail_mdl_cfg_idx(UINT8 app_idx, UINT8 mcl_idx,
   1363                                        UINT8 *p_mdl_cfg_idx)
   1364 {
   1365     tBTA_HL_MDL_CFG     *p_mdl, *p_mdl1, *p_mdl2;
   1366     UINT8               i;
   1367     BOOLEAN             found=FALSE;
   1368     UINT8               first_mdl_cfg_idx, second_mdl_cfg_idx, older_mdl_cfg_idx;
   1369     BOOLEAN             done;
   1370 
   1371 
   1372     for (i=0; i< BTA_HL_NUM_MDL_CFGS; i++)
   1373     {
   1374         p_mdl = BTA_HL_GET_MDL_CFG_PTR(app_idx, i);
   1375         if (!p_mdl->active  )
   1376         {
   1377             /* found an unused space to store mdl cfg*/
   1378             found=TRUE;
   1379             *p_mdl_cfg_idx =i;
   1380             break;
   1381         }
   1382     }
   1383 
   1384     if (!found)
   1385     {
   1386         /* all available mdl cfg spaces are in use so we need to find the mdl cfg which is
   1387         not currently in use and has the the oldest time stamp to remove*/
   1388 
   1389         found = TRUE;
   1390         if (bta_hl_find_non_active_mdl_cfg(app_idx,0, &first_mdl_cfg_idx))
   1391         {
   1392             if (bta_hl_find_non_active_mdl_cfg(app_idx,(UINT8) (first_mdl_cfg_idx+1), &second_mdl_cfg_idx))
   1393             {
   1394                 done = FALSE;
   1395                 while (!done)
   1396                 {
   1397                     p_mdl1 = BTA_HL_GET_MDL_CFG_PTR(app_idx, first_mdl_cfg_idx);
   1398                     p_mdl2 = BTA_HL_GET_MDL_CFG_PTR(app_idx, second_mdl_cfg_idx);
   1399 
   1400                     if (p_mdl1->time < p_mdl2->time)
   1401                     {
   1402                         older_mdl_cfg_idx =  first_mdl_cfg_idx;
   1403                     }
   1404                     else
   1405                     {
   1406                         older_mdl_cfg_idx =  second_mdl_cfg_idx;
   1407                     }
   1408 
   1409                     if (bta_hl_find_non_active_mdl_cfg(app_idx,(UINT8) (second_mdl_cfg_idx+1), &second_mdl_cfg_idx))
   1410                     {
   1411                         first_mdl_cfg_idx = older_mdl_cfg_idx;
   1412                     }
   1413                     else
   1414                     {
   1415                         done = TRUE;
   1416                     }
   1417                 }
   1418 
   1419                 *p_mdl_cfg_idx = older_mdl_cfg_idx;
   1420 
   1421             }
   1422             else
   1423             {
   1424                 *p_mdl_cfg_idx = first_mdl_cfg_idx;
   1425             }
   1426 
   1427         }
   1428         else
   1429         {
   1430             found = FALSE;
   1431         }
   1432     }
   1433 
   1434 #if BTA_HL_DEBUG == TRUE
   1435     if (!found)
   1436     {
   1437         APPL_TRACE_DEBUG2("bta_hl_find_avail_mdl_cfg_idx found=%d mdl_cfg_idx=%d ",found, *p_mdl_cfg_idx  );
   1438     }
   1439 #endif
   1440 
   1441     return found;
   1442 
   1443 
   1444 }
   1445 
   1446 /*******************************************************************************
   1447 **
   1448 ** Function      bta_hl_find_mdl_cfg_idx
   1449 **
   1450 ** Description  This function finds the MDL configuration index based on
   1451 **              the MDL ID
   1452 **
   1453 ** Returns      BOOLEAN - TRUE found
   1454 **                        FALSE not found
   1455 **
   1456 *******************************************************************************/
   1457 BOOLEAN  bta_hl_find_mdl_cfg_idx(UINT8 app_idx, UINT8 mcl_idx,
   1458                                  tBTA_HL_MDL_ID mdl_id, UINT8 *p_mdl_cfg_idx)
   1459 {
   1460     tBTA_HL_MCL_CB      *p_mcb = BTA_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
   1461     tBTA_HL_MDL_CFG     *p_mdl;
   1462     UINT8 i ;
   1463     BOOLEAN found=FALSE;
   1464     for (i=0; i< BTA_HL_NUM_MDL_CFGS; i++)
   1465     {
   1466         p_mdl = BTA_HL_GET_MDL_CFG_PTR(app_idx, i);
   1467         if (p_mdl->active &&
   1468             (!memcmp (p_mcb->bd_addr, p_mdl->peer_bd_addr, BD_ADDR_LEN))&&
   1469             (p_mdl->mdl_id == mdl_id))
   1470         {
   1471             found=TRUE;
   1472             *p_mdl_cfg_idx =i;
   1473             break;
   1474         }
   1475     }
   1476 
   1477 #if BTA_HL_DEBUG == TRUE
   1478     if (!found)
   1479     {
   1480         APPL_TRACE_DEBUG2("bta_hl_find_mdl_cfg_idx found=%d mdl_cfg_idx=%d ",found, i );
   1481     }
   1482 #endif
   1483 
   1484     return found;
   1485 
   1486 
   1487 }
   1488 
   1489 
   1490 /*******************************************************************************
   1491 **
   1492 ** Function      bta_hl_get_cur_time
   1493 **
   1494 ** Description  This function get the cuurent time value
   1495 **
   1496 ** Returns      BOOLEAN - TRUE found
   1497 **                        FALSE not found
   1498 **
   1499 *******************************************************************************/
   1500 BOOLEAN  bta_hl_get_cur_time(UINT8 app_idx, UINT8 *p_cur_time)
   1501 {
   1502     tBTA_HL_MDL_CFG     *p_mdl;
   1503     UINT8 i, j, time_latest, time;
   1504     BOOLEAN found=FALSE, result=TRUE;
   1505 
   1506     for (i=0; i< BTA_HL_NUM_MDL_CFGS; i++)
   1507     {
   1508         p_mdl = BTA_HL_GET_MDL_CFG_PTR(app_idx, i);
   1509         if (p_mdl->active)
   1510         {
   1511             found=TRUE;
   1512             time_latest = p_mdl->time;
   1513             for (j=(i+1); j< BTA_HL_NUM_MDL_CFGS; j++ )
   1514             {
   1515                 p_mdl = BTA_HL_GET_MDL_CFG_PTR(app_idx, j);
   1516                 if (p_mdl->active)
   1517                 {
   1518                     time = p_mdl->time;
   1519                     if (time > time_latest)
   1520                     {
   1521                         time_latest = time;
   1522                     }
   1523                 }
   1524             }
   1525             break;
   1526         }
   1527     }
   1528 
   1529 
   1530     if (found)
   1531     {
   1532         if (time_latest < BTA_HL_MAX_TIME)
   1533         {
   1534             *p_cur_time = time_latest+1;
   1535         }
   1536         else
   1537         {
   1538             /* need to wrap around */
   1539             result = FALSE;
   1540         }
   1541     }
   1542     else
   1543     {
   1544         *p_cur_time = BTA_HL_MIN_TIME;
   1545     }
   1546 
   1547 #if BTA_HL_DEBUG == TRUE
   1548     if (!result)
   1549     {
   1550         APPL_TRACE_DEBUG2("bta_hl_get_cur_time result=%s cur_time=%d",
   1551                           (result?"OK":"FAIL"), *p_cur_time);
   1552     }
   1553 #endif
   1554 
   1555     return result;
   1556 }
   1557 
   1558 /*******************************************************************************
   1559 **
   1560 ** Function      bta_hl_sort_cfg_time_idx
   1561 **
   1562 ** Description  This function sort the mdl configuration idx stored in array a
   1563 **              based on decending time value
   1564 **
   1565 ** Returns      BOOLEAN - TRUE found
   1566 **                        FALSE not found
   1567 **
   1568 *******************************************************************************/
   1569 void bta_hl_sort_cfg_time_idx(UINT8 app_idx, UINT8 *a, UINT8 n)
   1570 {
   1571     tBTA_HL_APP_CB      *p_acb = BTA_HL_GET_APP_CB_PTR(app_idx);
   1572     UINT8 temp_time, temp_idx;
   1573     INT16 i, j;
   1574     for (i = 1; i < n; ++i)
   1575     {
   1576         temp_idx = a[i];
   1577         temp_time = p_acb->mdl_cfg[temp_idx].time;
   1578         j = i - 1;
   1579         while ((j >= 0) && (temp_time < p_acb->mdl_cfg[a[j]].time))
   1580         {
   1581             a[j + 1] = a[j];
   1582             --j;
   1583         }
   1584         a[j + 1] = temp_idx;
   1585     }
   1586 }
   1587 
   1588 /*******************************************************************************
   1589 **
   1590 ** Function      bta_hl_compact_mdl_cfg_time
   1591 **
   1592 ** Description  This function finds the MDL configuration index based on
   1593 **              the MDL ID
   1594 **
   1595 ** Returns      BOOLEAN - TRUE found
   1596 **                        FALSE not found
   1597 **
   1598 *******************************************************************************/
   1599 void  bta_hl_compact_mdl_cfg_time(UINT8 app_idx)
   1600 {
   1601     tBTA_HL_APP_CB      *p_acb = BTA_HL_GET_APP_CB_PTR(app_idx);
   1602     tBTA_HL_MDL_CFG     *p_mdl;
   1603     UINT8 i, time_min, cnt=0;
   1604     UINT8   s_arr[BTA_HL_NUM_MDL_CFGS];
   1605 
   1606 
   1607     for (i=0; i< BTA_HL_NUM_MDL_CFGS; i++)
   1608     {
   1609         p_mdl = BTA_HL_GET_MDL_CFG_PTR(app_idx, i);
   1610         if (p_mdl->active )
   1611         {
   1612             s_arr[cnt]= i;
   1613             cnt++;
   1614         }
   1615     }
   1616 
   1617 
   1618 
   1619 #if BTA_HL_DEBUG == TRUE
   1620     APPL_TRACE_DEBUG1("bta_hl_compact_mdl_cfg_time cnt=%d ",cnt );
   1621 #endif
   1622 
   1623 
   1624     if (cnt)
   1625     {
   1626         bta_hl_sort_cfg_time_idx(app_idx, s_arr, cnt);
   1627         time_min = BTA_HL_MIN_TIME;
   1628         for (i=0;i<cnt; i++)
   1629         {
   1630             p_mdl = BTA_HL_GET_MDL_CFG_PTR(app_idx, s_arr[i]);
   1631             p_mdl->time = time_min + i;
   1632             bta_hl_co_save_mdl(p_acb->app_id, s_arr[i], p_mdl);
   1633         }
   1634     }
   1635 
   1636 
   1637 }
   1638 
   1639 
   1640 
   1641 /*******************************************************************************
   1642 **
   1643 ** Function      bta_hl_is_mdl_exsit_in_mcl
   1644 **
   1645 ** Description  This function checks whether the MDL ID
   1646 **              has already existed in teh MCL or not
   1647 **
   1648 ** Returns      BOOLEAN - TRUE exist
   1649 **                        FALSE does not exist
   1650 **
   1651 *******************************************************************************/
   1652 BOOLEAN  bta_hl_is_mdl_exsit_in_mcl(UINT8 app_idx, BD_ADDR bd_addr,
   1653                                     tBTA_HL_MDL_ID mdl_id)
   1654 {
   1655     tBTA_HL_MDL_CFG     *p_mdl;
   1656     BOOLEAN             found = FALSE;
   1657     UINT8               i;
   1658 
   1659     for (i = 0; i< BTA_HL_NUM_MDL_CFGS; i++)
   1660     {
   1661         p_mdl = BTA_HL_GET_MDL_CFG_PTR(app_idx, i);
   1662         if (p_mdl->active &&
   1663             !memcmp(p_mdl->peer_bd_addr, bd_addr,BD_ADDR_LEN))
   1664         {
   1665             if (mdl_id != BTA_HL_DELETE_ALL_MDL_IDS)
   1666             {
   1667                 if (p_mdl->mdl_id == mdl_id)
   1668                 {
   1669                     found = TRUE;
   1670                     break;
   1671                 }
   1672             }
   1673             else
   1674             {
   1675                 found = TRUE;
   1676                 break;
   1677             }
   1678         }
   1679     }
   1680 
   1681     return found;
   1682 }
   1683 
   1684 /*******************************************************************************
   1685 **
   1686 ** Function      bta_hl_delete_mdl_cfg
   1687 **
   1688 ** Description  This function delete the specified MDL ID
   1689 **
   1690 ** Returns      BOOLEAN - TRUE Success
   1691 **                        FALSE Failed
   1692 **
   1693 *******************************************************************************/
   1694 BOOLEAN  bta_hl_delete_mdl_cfg(UINT8 app_idx, BD_ADDR bd_addr,
   1695                                tBTA_HL_MDL_ID mdl_id)
   1696 {
   1697     tBTA_HL_MDL_CFG     *p_mdl;
   1698     BOOLEAN             success = FALSE;
   1699     UINT8               i;
   1700     tBTA_HL_APP_CB      *p_acb= BTA_HL_GET_APP_CB_PTR(app_idx);
   1701     UINT8               app_id = p_acb->app_id;
   1702 
   1703     for (i = 0; i< BTA_HL_NUM_MDL_CFGS; i++)
   1704     {
   1705         p_mdl = BTA_HL_GET_MDL_CFG_PTR(app_idx, i);
   1706         if (p_mdl->active &&
   1707             !memcmp(p_mdl->peer_bd_addr, bd_addr,BD_ADDR_LEN))
   1708         {
   1709             if (mdl_id != BTA_HL_DELETE_ALL_MDL_IDS)
   1710             {
   1711                 if (p_mdl->mdl_id == mdl_id)
   1712                 {
   1713                     bta_hl_co_delete_mdl(app_id, i);
   1714                     memset(p_mdl, 0, sizeof(tBTA_HL_MDL_CFG));
   1715                     success = TRUE;
   1716                     break;
   1717                 }
   1718             }
   1719             else
   1720             {
   1721                 bta_hl_co_delete_mdl(app_id, i);
   1722                 memset(p_mdl, 0, sizeof(tBTA_HL_MDL_CFG));
   1723                 success = TRUE;
   1724             }
   1725         }
   1726     }
   1727 
   1728     return success;
   1729 }
   1730 
   1731 
   1732 /*******************************************************************************
   1733 **
   1734 ** Function      bta_hl_is_mdl_value_valid
   1735 **
   1736 **
   1737 ** Description  This function checks the specified MDL ID is in valid range or not
   1738 **
   1739 ** Returns      BOOLEAN - TRUE Success
   1740 **                        FALSE Failed
   1741 **
   1742 ** note:   mdl_id range   0x0000 reserved,
   1743 **                        0x0001-oxFEFF dynamic range,
   1744 **                        0xFF00-0xFFFE reserved,
   1745 **                        0xFFFF indicates all MDLs (for delete operation only)
   1746 **
   1747 *******************************************************************************/
   1748 BOOLEAN  bta_hl_is_mdl_value_valid(tBTA_HL_MDL_ID mdl_id)
   1749 {
   1750     BOOLEAN             status = TRUE;
   1751 
   1752     if (mdl_id != BTA_HL_DELETE_ALL_MDL_IDS)
   1753     {
   1754         if (mdl_id != 0)
   1755         {
   1756             if (mdl_id > BTA_HL_MAX_MDL_VAL )
   1757             {
   1758                 status = FALSE;
   1759             }
   1760         }
   1761         else
   1762         {
   1763             status = FALSE;
   1764         }
   1765     }
   1766 
   1767     return status;
   1768 }
   1769 
   1770 /*******************************************************************************
   1771 **
   1772 ** Function      bta_hl_find_mdep_cfg_idx
   1773 **
   1774 ** Description  This function finds the MDEP configuration index based
   1775 **                on the local MDEP ID
   1776 **
   1777 ** Returns      BOOLEAN - TRUE found
   1778 **                        FALSE not found
   1779 **
   1780 *******************************************************************************/
   1781 BOOLEAN bta_hl_find_mdep_cfg_idx(UINT8 app_idx,  tBTA_HL_MDEP_ID local_mdep_id,
   1782                                  UINT8 *p_mdep_cfg_idx)
   1783 {
   1784     tBTA_HL_APP_CB      *p_acb = BTA_HL_GET_APP_CB_PTR(app_idx);
   1785     tBTA_HL_SUP_FEATURE     *p_sup_feature= &p_acb->sup_feature;
   1786     BOOLEAN found =FALSE;
   1787     UINT8 i;
   1788 
   1789     for (i=0; i< p_sup_feature->num_of_mdeps; i++)
   1790     {
   1791         if ( p_sup_feature->mdep[i].mdep_id == local_mdep_id)
   1792         {
   1793             found = TRUE;
   1794             *p_mdep_cfg_idx = i;
   1795             break;
   1796         }
   1797     }
   1798 
   1799 #if BTA_HL_DEBUG == TRUE
   1800     if (!found)
   1801     {
   1802         APPL_TRACE_DEBUG3("bta_hl_find_mdep_cfg_idx found=%d mdep_idx=%d local_mdep_id=%d ",
   1803                           found,i, local_mdep_id );
   1804     }
   1805 #endif
   1806     return found;
   1807 }
   1808 
   1809 
   1810 /*******************************************************************************
   1811 **
   1812 ** Function      bta_hl_find_rxtx_apdu_size
   1813 **
   1814 ** Description  This function finds the maximum APDU rx and tx sizes based on
   1815 **              the MDEP configuration data
   1816 **
   1817 ** Returns      void
   1818 **
   1819 *******************************************************************************/
   1820 void bta_hl_find_rxtx_apdu_size(UINT8 app_idx, UINT8 mdep_cfg_idx,
   1821                                 UINT16 *p_rx_apu_size,
   1822                                 UINT16 *p_tx_apu_size)
   1823 {
   1824     tBTA_HL_APP_CB      *p_acb = BTA_HL_GET_APP_CB_PTR(app_idx);
   1825     tBTA_HL_MDEP_CFG     *p_mdep_cfg;
   1826     UINT8 i;
   1827     UINT16 max_rx_apdu_size=0, max_tx_apdu_size=0;
   1828 
   1829     p_mdep_cfg = &p_acb->sup_feature.mdep[mdep_cfg_idx].mdep_cfg;
   1830 
   1831 
   1832     for (i=0; i< p_mdep_cfg->num_of_mdep_data_types ; i++)
   1833     {
   1834 
   1835         if (max_rx_apdu_size < p_mdep_cfg->data_cfg[i].max_rx_apdu_size)
   1836         {
   1837             max_rx_apdu_size = p_mdep_cfg->data_cfg[i].max_rx_apdu_size;
   1838         }
   1839 
   1840         if (max_tx_apdu_size < p_mdep_cfg->data_cfg[i].max_tx_apdu_size)
   1841         {
   1842             max_tx_apdu_size = p_mdep_cfg->data_cfg[i].max_tx_apdu_size;
   1843         }
   1844     }
   1845 
   1846 
   1847     *p_rx_apu_size = max_rx_apdu_size;
   1848     *p_tx_apu_size = max_tx_apdu_size;
   1849 
   1850 #if BTA_HL_DEBUG == TRUE
   1851     APPL_TRACE_DEBUG2("bta_hl_find_rxtx_apdu_size max_rx_apdu_size=%d max_tx_apdu_size=%d ",
   1852                       max_rx_apdu_size, max_tx_apdu_size );
   1853 #endif
   1854 
   1855 
   1856 }
   1857 
   1858 /*******************************************************************************
   1859 **
   1860 ** Function      bta_hl_validate_peer_cfg
   1861 **
   1862 ** Description  This function validates the peer DCH configuration
   1863 **
   1864 ** Returns      BOOLEAN - TRUE validation is successful
   1865 **                        FALSE validation failed
   1866 **
   1867 *******************************************************************************/
   1868 BOOLEAN bta_hl_validate_peer_cfg(UINT8 app_idx, UINT8 mcl_idx, UINT8 mdl_idx,
   1869                                  tBTA_HL_MDEP_ID peer_mdep_id,
   1870                                  tBTA_HL_MDEP_ROLE peer_mdep_role,
   1871                                  UINT8 sdp_idx)
   1872 {
   1873     tBTA_HL_MCL_CB      *p_mcb = BTA_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
   1874     tBTA_HL_MDL_CB      *p_dcb = BTA_HL_GET_MDL_CB_PTR(app_idx, mcl_idx, mdl_idx);
   1875     tBTA_HL_SDP_REC     *p_rec;
   1876     BOOLEAN peer_found =FALSE;
   1877     UINT8 i;
   1878 
   1879     APPL_TRACE_DEBUG1("bta_hl_validate_peer_cfg sdp_idx=%d", sdp_idx);
   1880 
   1881 
   1882     if (p_dcb->local_mdep_id == BTA_HL_ECHO_TEST_MDEP_ID)
   1883     {
   1884         return TRUE;
   1885     }
   1886 
   1887     p_rec = &p_mcb->sdp.sdp_rec[sdp_idx];
   1888     for (i=0; i< p_rec->num_mdeps; i++)
   1889     {
   1890         if ( (p_rec->mdep_cfg[i].mdep_id == peer_mdep_id) &&
   1891              (p_rec->mdep_cfg[i].mdep_role == peer_mdep_role))
   1892         {
   1893             peer_found = TRUE;
   1894 
   1895             break;
   1896         }
   1897     }
   1898 
   1899 
   1900 #if BTA_HL_DEBUG == TRUE
   1901     if (!peer_found)
   1902     {
   1903         APPL_TRACE_DEBUG1("bta_hl_validate_peer_cfg failed num_mdeps=%d",p_rec->num_mdeps);
   1904     }
   1905 #endif
   1906     return peer_found;
   1907 }
   1908 
   1909 
   1910 /*******************************************************************************
   1911 **
   1912 ** Function      bta_hl_chk_local_cfg
   1913 **
   1914 ** Description  This function check whether the local DCH configuration is OK or not
   1915 **
   1916 ** Returns      tBTA_HL_STATUS - OK - local DCH configuration is OK
   1917 **                               NO_FIRST_RELIABLE - the streaming DCH configuration
   1918 **                                                   is not OK and it needs to use
   1919 **                                                   reliable DCH configuration
   1920 **
   1921 *******************************************************************************/
   1922 tBTA_HL_STATUS bta_hl_chk_local_cfg(UINT8 app_idx, UINT8 mcl_idx,
   1923                                     UINT8 mdep_cfg_idx,
   1924                                     tBTA_HL_DCH_CFG local_cfg)
   1925 {
   1926     tBTA_HL_APP_CB      *p_acb = BTA_HL_GET_APP_CB_PTR(app_idx);
   1927     tBTA_HL_STATUS status = BTA_HL_STATUS_OK;
   1928 
   1929     if ( mdep_cfg_idx &&
   1930          (p_acb->sup_feature.mdep[mdep_cfg_idx].mdep_cfg.mdep_role == BTA_HL_MDEP_ROLE_SOURCE) &&
   1931          (!bta_hl_is_the_first_reliable_existed(app_idx, mcl_idx)) &&
   1932          (local_cfg != BTA_HL_DCH_CFG_RELIABLE))
   1933     {
   1934         status =  BTA_HL_STATUS_NO_FIRST_RELIABLE;
   1935         APPL_TRACE_ERROR0("BTA_HL_STATUS_INVALID_DCH_CFG");
   1936     }
   1937 
   1938     return status;
   1939 }
   1940 
   1941 
   1942 /*******************************************************************************
   1943 **
   1944 ** Function      bta_hl_validate_reconnect_params
   1945 **
   1946 ** Description  This function validates the reconnect parameters
   1947 **
   1948 ** Returns      BOOLEAN - TRUE validation is successful
   1949 **                        FALSE validation failed
   1950 *******************************************************************************/
   1951 BOOLEAN bta_hl_validate_reconnect_params(UINT8 app_idx, UINT8 mcl_idx,
   1952                                          tBTA_HL_API_DCH_RECONNECT *p_reconnect,
   1953                                          UINT8 *p_mdep_cfg_idx, UINT8 *p_mdl_cfg_idx)
   1954 {
   1955     tBTA_HL_APP_CB      *p_acb = BTA_HL_GET_APP_CB_PTR(app_idx);
   1956     tBTA_HL_SUP_FEATURE *p_sup_feature = &p_acb->sup_feature;
   1957     UINT8               num_mdeps;
   1958     UINT8               mdl_cfg_idx;
   1959     BOOLEAN local_mdep_id_found =FALSE;
   1960     BOOLEAN mdl_cfg_found =FALSE;
   1961     BOOLEAN            status=FALSE;
   1962     UINT8 i, in_use_mdl_idx;
   1963 
   1964 #if BTA_HL_DEBUG == TRUE
   1965     APPL_TRACE_DEBUG1("bta_hl_validate_reconnect_params  mdl_id=%d", p_reconnect->mdl_id);
   1966 #endif
   1967     if (bta_hl_find_mdl_cfg_idx(app_idx, mcl_idx, p_reconnect->mdl_id, &mdl_cfg_idx))
   1968     {
   1969         mdl_cfg_found = TRUE;
   1970     }
   1971 
   1972 #if BTA_HL_DEBUG == TRUE
   1973     if (!mdl_cfg_found)
   1974     {
   1975         APPL_TRACE_DEBUG0("mdl_cfg_found not found");
   1976     }
   1977 #endif
   1978 
   1979 
   1980     if (mdl_cfg_found)
   1981     {
   1982         num_mdeps = p_sup_feature->num_of_mdeps;
   1983         for (i=0; i< num_mdeps ; i++)
   1984         {
   1985             if ( p_sup_feature->mdep[i].mdep_id == p_acb->mdl_cfg[mdl_cfg_idx].local_mdep_id)
   1986             {
   1987                 local_mdep_id_found = TRUE;
   1988                 *p_mdep_cfg_idx =i;
   1989                 *p_mdl_cfg_idx = mdl_cfg_idx;
   1990                 break;
   1991             }
   1992         }
   1993     }
   1994 
   1995 #if BTA_HL_DEBUG == TRUE
   1996     if (!local_mdep_id_found)
   1997     {
   1998         APPL_TRACE_DEBUG0("local_mdep_id not found");
   1999     }
   2000 #endif
   2001 
   2002 
   2003     if (local_mdep_id_found)
   2004     {
   2005         if (!bta_hl_find_mdl_idx(app_idx,mcl_idx, p_reconnect->mdl_id, &in_use_mdl_idx))
   2006         {
   2007             status= TRUE;
   2008         }
   2009         else
   2010         {
   2011             APPL_TRACE_ERROR1("mdl_id=%d is curreltly in use",p_reconnect->mdl_id);
   2012         }
   2013     }
   2014 
   2015 #if BTA_HL_DEBUG == TRUE
   2016     if (!status)
   2017     {
   2018         APPL_TRACE_DEBUG3("Reconnect validation failed local_mdep_id found=%d mdl_cfg_idx found=%d in_use_mdl_idx=%d ",
   2019                           local_mdep_id_found,  mdl_cfg_found, in_use_mdl_idx);
   2020     }
   2021 #endif
   2022     return status;
   2023 }
   2024 
   2025 /*******************************************************************************
   2026 **
   2027 ** Function      bta_hl_find_avail_mcl_idx
   2028 **
   2029 ** Returns      BOOLEAN - TRUE found
   2030 **                        FALSE not found
   2031 **
   2032 *******************************************************************************/
   2033 BOOLEAN bta_hl_find_avail_mcl_idx(UINT8 app_idx, UINT8 *p_mcl_idx)
   2034 {
   2035     BOOLEAN found=FALSE;
   2036     UINT8 i;
   2037 
   2038     for (i=0; i < BTA_HL_NUM_MCLS ; i ++)
   2039     {
   2040         if (!bta_hl_cb.acb[app_idx].mcb[i].in_use)
   2041         {
   2042             found = TRUE;
   2043             *p_mcl_idx = i;
   2044             break;
   2045         }
   2046     }
   2047 
   2048 #if BTA_HL_DEBUG == TRUE
   2049     if (!found)
   2050     {
   2051         APPL_TRACE_DEBUG2("bta_hl_find_avail_mcl_idx found=%d idx=%d",
   2052                           found, i);
   2053     }
   2054 #endif
   2055     return found;
   2056 }
   2057 
   2058 
   2059 
   2060 /*******************************************************************************
   2061 **
   2062 ** Function      bta_hl_find_avail_mdl_idx
   2063 **
   2064 ** Description  This function finds an available MDL control block index
   2065 **
   2066 ** Returns      BOOLEAN - TRUE found
   2067 **                        FALSE not found
   2068 **
   2069 *******************************************************************************/
   2070 BOOLEAN bta_hl_find_avail_mdl_idx(UINT8 app_idx, UINT8 mcl_idx,
   2071                                   UINT8 *p_mdl_idx)
   2072 {
   2073     tBTA_HL_MCL_CB      *p_mcb = BTA_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
   2074     BOOLEAN found=FALSE;
   2075     UINT8 i;
   2076 
   2077     for (i=0; i < BTA_HL_NUM_MDLS_PER_MCL ; i ++)
   2078     {
   2079         if (!p_mcb->mdl[i].in_use)
   2080         {
   2081             memset((void *)&p_mcb->mdl[i],0, sizeof(tBTA_HL_MDL_CB));
   2082             found = TRUE;
   2083             *p_mdl_idx = i;
   2084             break;
   2085         }
   2086     }
   2087 
   2088 #if BTA_HL_DEBUG == TRUE
   2089     if (!found)
   2090     {
   2091         APPL_TRACE_DEBUG2("bta_hl_find_avail_mdl_idx found=%d idx=%d",
   2092                           found, i);
   2093     }
   2094 #endif
   2095     return found;
   2096 }
   2097 
   2098 /*******************************************************************************
   2099 **
   2100 ** Function      bta_hl_is_a_duplicate_id
   2101 **
   2102 ** Description  This function finds the application has been used or not
   2103 **
   2104 ** Returns      BOOLEAN - TRUE the app_id is a duplicate ID
   2105 **                        FALSE not a duplicate ID
   2106 *******************************************************************************/
   2107 BOOLEAN bta_hl_is_a_duplicate_id(UINT8 app_id)
   2108 {
   2109     BOOLEAN is_duplicate=FALSE;
   2110     UINT8 i;
   2111 
   2112     for (i=0; i < BTA_HL_NUM_APPS ; i ++)
   2113     {
   2114         if (bta_hl_cb.acb[i].in_use &&
   2115             (bta_hl_cb.acb[i].app_id == app_id))
   2116         {
   2117             is_duplicate = TRUE;
   2118 
   2119             break;
   2120         }
   2121     }
   2122 
   2123 #if BTA_HL_DEBUG == TRUE
   2124     if (is_duplicate)
   2125     {
   2126 
   2127         APPL_TRACE_DEBUG2("bta_hl_is_a_duplicate_id app_id=%d is_duplicate=%d",
   2128                           app_id, is_duplicate);
   2129     }
   2130 #endif
   2131 
   2132     return is_duplicate;
   2133 }
   2134 
   2135 
   2136 /*******************************************************************************
   2137 **
   2138 ** Function      bta_hl_find_avail_app_idx
   2139 **
   2140 ** Description  This function finds an available application control block index
   2141 **
   2142 ** Returns      BOOLEAN - TRUE found
   2143 **                        FALSE not found
   2144 **
   2145 *******************************************************************************/
   2146 BOOLEAN bta_hl_find_avail_app_idx(UINT8 *p_idx)
   2147 {
   2148     BOOLEAN found=FALSE;
   2149     UINT8 i;
   2150 
   2151     for (i=0; i < BTA_HL_NUM_APPS ; i ++)
   2152     {
   2153         if (!bta_hl_cb.acb[i].in_use)
   2154         {
   2155             found = TRUE;
   2156             *p_idx = i;
   2157             break;
   2158         }
   2159     }
   2160 
   2161 #if BTA_HL_DEBUG == TRUE
   2162     if (!found)
   2163     {
   2164         APPL_TRACE_DEBUG2("bta_hl_find_avail_app_idx found=%d app_idx=%d",
   2165                           found, i);
   2166     }
   2167 #endif
   2168     return found;
   2169 }
   2170 
   2171 /*******************************************************************************
   2172 **
   2173 ** Function      bta_hl_app_registration
   2174 **
   2175 ** Description  This function registers an HDP application MCAP and DP
   2176 **
   2177 ** Returns      tBTA_HL_STATUS -registration status
   2178 **
   2179 *******************************************************************************/
   2180 tBTA_HL_STATUS bta_hl_app_registration(UINT8 app_idx)
   2181 {
   2182     tBTA_HL_STATUS  status = BTA_HL_STATUS_OK;
   2183     tBTA_HL_APP_CB  *p_acb = BTA_HL_GET_APP_CB_PTR(app_idx);
   2184     tMCA_REG        reg;
   2185     tMCA_CS         mca_cs;
   2186     UINT8           i, num_of_mdeps;
   2187 
   2188 
   2189 #if BTA_HL_DEBUG == TRUE
   2190     APPL_TRACE_DEBUG1("bta_hl_app_registration app_idx=%d", app_idx);
   2191 #endif
   2192 
   2193     reg.ctrl_psm = p_acb->ctrl_psm;
   2194     reg.data_psm = p_acb->data_psm;
   2195     reg.sec_mask = p_acb->sec_mask;
   2196     reg.rsp_tout = BTA_HL_MCAP_RSP_TOUT;
   2197 
   2198     if ( (p_acb->app_handle = (tBTA_HL_APP_HANDLE) MCA_Register(&reg, bta_hl_mcap_ctrl_cback))!=0)
   2199     {
   2200         mca_cs.type = MCA_TDEP_ECHO;
   2201         mca_cs.max_mdl = BTA_HL_NUM_MDLS_PER_MDEP;
   2202         mca_cs.p_data_cback = bta_hl_mcap_data_cback;
   2203 
   2204         if (MCA_CreateDep((tMCA_HANDLE)p_acb->app_handle,
   2205                           &(p_acb->sup_feature.mdep[0].mdep_id),
   2206                           &mca_cs) == MCA_SUCCESS)
   2207         {
   2208             if (p_acb->sup_feature.mdep[0].mdep_id != BTA_HL_ECHO_TEST_MDEP_ID)
   2209             {
   2210                 status = BTA_HL_STATUS_MCAP_REG_FAIL;
   2211                 APPL_TRACE_ERROR1("BAD MDEP ID for echo test mdep_id=%d",
   2212                                   p_acb->sup_feature.mdep[0].mdep_id );
   2213             }
   2214         }
   2215         else
   2216         {
   2217             status = BTA_HL_STATUS_MCAP_REG_FAIL;
   2218             APPL_TRACE_ERROR0("MCA_CreateDep for echo test(mdep_id=0) failed");
   2219         }
   2220 
   2221 
   2222         if ((status == BTA_HL_STATUS_OK) &&
   2223             bta_hl_co_get_num_of_mdep(p_acb->app_id, &num_of_mdeps))
   2224         {
   2225             p_acb->sup_feature.num_of_mdeps = num_of_mdeps+1;
   2226 
   2227             for (i=1; i<p_acb->sup_feature.num_of_mdeps; i++)
   2228             {
   2229                 mca_cs.type = MCA_TDEP_DATA;
   2230                 mca_cs.max_mdl = BTA_HL_NUM_MDLS_PER_MDEP;
   2231                 mca_cs.p_data_cback = bta_hl_mcap_data_cback;
   2232 
   2233                 if (MCA_CreateDep((tMCA_HANDLE)p_acb->app_handle,
   2234                                   &(p_acb->sup_feature.mdep[i].mdep_id), &mca_cs) == MCA_SUCCESS)
   2235                 {
   2236                     if (bta_hl_co_get_mdep_config(p_acb->app_id,
   2237                                                   i,
   2238                                                   p_acb->sup_feature.mdep[i].mdep_id,
   2239                                                   &p_acb->sup_feature.mdep[i].mdep_cfg))
   2240                     {
   2241                         if (p_acb->sup_feature.mdep[i].mdep_cfg.mdep_role == BTA_HL_MDEP_ROLE_SOURCE)
   2242                         {
   2243                             p_acb->sup_feature.app_role_mask |= BTA_HL_MDEP_ROLE_MASK_SOURCE;
   2244                         }
   2245                         else if (p_acb->sup_feature.mdep[i].mdep_cfg.mdep_role == BTA_HL_MDEP_ROLE_SINK)
   2246                         {
   2247                             p_acb->sup_feature.app_role_mask |= BTA_HL_MDEP_ROLE_MASK_SINK;
   2248                         }
   2249                         else
   2250                         {
   2251                             status = BTA_HL_STATUS_MDEP_CO_FAIL;
   2252                             break;
   2253                         }
   2254                     }
   2255                     else
   2256                     {
   2257                         status = BTA_HL_STATUS_MDEP_CO_FAIL;
   2258                         break;
   2259                     }
   2260                 }
   2261                 else
   2262                 {
   2263                     status = BTA_HL_STATUS_MCAP_REG_FAIL;
   2264                     break;
   2265                 }
   2266             }
   2267 
   2268 
   2269 
   2270             if ((status == BTA_HL_STATUS_OK) &&
   2271                 (p_acb->sup_feature.app_role_mask == BTA_HL_MDEP_ROLE_MASK_SOURCE))
   2272             {
   2273                 /* this is a source only applciation */
   2274                 p_acb->sup_feature.advertize_source_sdp =
   2275                 bta_hl_co_advrtise_source_sdp(p_acb->app_id);
   2276             }
   2277 
   2278             if ((status == BTA_HL_STATUS_OK)&&
   2279                 (!bta_hl_co_get_echo_config(p_acb->app_id, &p_acb->sup_feature.echo_cfg)))
   2280             {
   2281                 status = BTA_HL_STATUS_ECHO_CO_FAIL;
   2282             }
   2283 
   2284             if ((status == BTA_HL_STATUS_OK)&&
   2285                 (!bta_hl_co_load_mdl_config(p_acb->app_id, BTA_HL_NUM_MDL_CFGS, &p_acb->mdl_cfg[0])))
   2286             {
   2287                 status = BTA_HL_STATUS_MDL_CFG_CO_FAIL;
   2288             }
   2289         }
   2290         else
   2291         {
   2292             status = BTA_HL_STATUS_MDEP_CO_FAIL;
   2293         }
   2294     }
   2295     else
   2296     {
   2297         status = BTA_HL_STATUS_MCAP_REG_FAIL;
   2298     }
   2299 
   2300     if (status == BTA_HL_STATUS_OK)
   2301     {
   2302         status = bta_hl_sdp_register(app_idx);
   2303     }
   2304 
   2305     return status;
   2306 }
   2307 
   2308 
   2309 /*******************************************************************************
   2310 **
   2311 ** Function         bta_hl_discard_data
   2312 **
   2313 ** Description  This function discard an HDP event
   2314 **
   2315 ** Returns     void
   2316 **
   2317 *******************************************************************************/
   2318 void bta_hl_discard_data(UINT16 event, tBTA_HL_DATA *p_data)
   2319 {
   2320 
   2321 #if BTA_HL_DEBUG == TRUE
   2322     APPL_TRACE_ERROR1("BTA HL Discard event=%s",bta_hl_evt_code(event));
   2323 
   2324 #endif
   2325 
   2326     switch (event)
   2327     {
   2328         case BTA_HL_API_SEND_DATA_EVT:
   2329             break;
   2330 
   2331         case BTA_HL_MCA_RCV_DATA_EVT:
   2332             utl_freebuf((void**)&p_data->mca_rcv_data_evt.p_pkt);
   2333             break;
   2334 
   2335         default:
   2336             /*Nothing to free*/
   2337             break;
   2338     }
   2339 }
   2340 
   2341 /*******************************************************************************
   2342 **
   2343 ** Function         bta_hl_save_mdl_cfg
   2344 **
   2345 ** Description    This function saves the MDL configuration
   2346 **
   2347 ** Returns     void
   2348 **
   2349 *******************************************************************************/
   2350 void bta_hl_save_mdl_cfg(UINT8 app_idx, UINT8 mcl_idx, UINT8 mdl_idx )
   2351 {
   2352     tBTA_HL_APP_CB      *p_acb  = BTA_HL_GET_APP_CB_PTR(app_idx);
   2353     tBTA_HL_MCL_CB      *p_mcb  = BTA_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
   2354     tBTA_HL_MDL_CB      *p_dcb  = BTA_HL_GET_MDL_CB_PTR(app_idx, mcl_idx, mdl_idx);
   2355     UINT8 mdl_cfg_idx;
   2356     tBTA_HL_MDL_ID mdl_id;
   2357     BOOLEAN      found=TRUE;
   2358     tBTA_HL_MDL_CFG mdl_cfg;
   2359     tBTA_HL_MDEP *p_mdep_cfg;
   2360     tBTA_HL_L2CAP_CFG_INFO l2cap_cfg;
   2361     UINT8 time_val;
   2362     mdl_id = p_dcb->mdl_id;
   2363     if (!bta_hl_find_mdl_cfg_idx(app_idx, mcl_idx, mdl_id, &mdl_cfg_idx))
   2364     {
   2365         if (!bta_hl_find_avail_mdl_cfg_idx(app_idx, mcl_idx, &mdl_cfg_idx))
   2366         {
   2367             APPL_TRACE_ERROR0("No space to save the MDL config");
   2368             found= FALSE; /*no space available*/
   2369         }
   2370     }
   2371 
   2372     if (found)
   2373     {
   2374         bta_hl_get_l2cap_cfg(p_dcb->mdl_handle, &l2cap_cfg);
   2375         if (!bta_hl_get_cur_time(app_idx, &time_val ))
   2376         {
   2377             bta_hl_compact_mdl_cfg_time(app_idx);
   2378             bta_hl_get_cur_time(app_idx, &time_val);
   2379         }
   2380         mdl_cfg.active = TRUE;
   2381         mdl_cfg.time = time_val;
   2382         mdl_cfg.mdl_id  = p_dcb->mdl_id;
   2383         mdl_cfg.dch_mode = p_dcb->dch_mode;
   2384         mdl_cfg.mtu = l2cap_cfg.mtu;
   2385         mdl_cfg.fcs = l2cap_cfg.fcs;
   2386 
   2387         bdcpy(mdl_cfg.peer_bd_addr, p_mcb->bd_addr);
   2388         mdl_cfg.local_mdep_id= p_dcb->local_mdep_id;
   2389         p_mdep_cfg = &p_acb->sup_feature.mdep[p_dcb->local_mdep_cfg_idx];
   2390         mdl_cfg.local_mdep_role= p_mdep_cfg->mdep_cfg.mdep_role;
   2391         memcpy(&p_acb->mdl_cfg[mdl_cfg_idx], &mdl_cfg, sizeof(tBTA_HL_MDL_CFG));
   2392         bta_hl_co_save_mdl(p_acb->app_id, mdl_cfg_idx, &mdl_cfg);
   2393     }
   2394 
   2395 #if BTA_HL_DEBUG == TRUE
   2396     if (found)
   2397     {
   2398         if (p_dcb->mtu != l2cap_cfg.mtu)
   2399         {
   2400             APPL_TRACE_WARNING2("MCAP and L2CAP peer mtu size out of sync from MCAP mtu=%d from l2cap mtu=%d",
   2401                                 p_dcb->mtu, l2cap_cfg.mtu);
   2402         }
   2403         APPL_TRACE_DEBUG1("bta_hl_save_mdl_cfg saved=%d", found);
   2404         APPL_TRACE_DEBUG4("Saved. L2cap cfg mdl_id=%d mtu=%d fcs=%d dch_mode=%d",
   2405                           mdl_cfg.mdl_id, mdl_cfg.mtu, mdl_cfg.fcs,  mdl_cfg.dch_mode);
   2406     }
   2407 #endif
   2408 
   2409 
   2410 
   2411 }
   2412 
   2413 /*******************************************************************************
   2414 **
   2415 ** Function      bta_hl_set_dch_chan_cfg
   2416 **
   2417 ** Description    This function setups the L2CAP DCH channel configuration
   2418 **
   2419 ** Returns     void
   2420 *******************************************************************************/
   2421 void bta_hl_set_dch_chan_cfg(UINT8 app_idx, UINT8 mcl_idx, UINT8 mdl_idx,tBTA_HL_DATA *p_data)
   2422 {
   2423     tBTA_HL_APP_CB *p_acb  = BTA_HL_GET_APP_CB_PTR(app_idx);
   2424     tBTA_HL_MDL_CB *p_dcb  = BTA_HL_GET_MDL_CB_PTR(app_idx, mcl_idx, mdl_idx);
   2425     UINT8 l2cap_mode = L2CAP_FCR_ERTM_MODE;
   2426     tBTA_HL_SUP_FEATURE *p_sup_feature= &p_acb->sup_feature;
   2427     UINT8 local_mdep_cfg_idx = p_dcb->local_mdep_cfg_idx;
   2428 
   2429     switch (p_dcb->dch_oper)
   2430     {
   2431         case BTA_HL_DCH_OP_LOCAL_RECONNECT:
   2432         case BTA_HL_DCH_OP_REMOTE_RECONNECT:
   2433             if (p_dcb->dch_mode  == BTA_HL_DCH_MODE_STREAMING)
   2434                 l2cap_mode = L2CAP_FCR_STREAM_MODE;
   2435             break;
   2436         case BTA_HL_DCH_OP_LOCAL_OPEN:
   2437             if (p_data->mca_evt.mca_data.create_cfm.cfg == BTA_HL_DCH_CFG_STREAMING)
   2438                 l2cap_mode = L2CAP_FCR_STREAM_MODE;
   2439             break;
   2440         case BTA_HL_DCH_OP_REMOTE_OPEN:
   2441             if (p_dcb->local_cfg == BTA_HL_DCH_CFG_STREAMING )
   2442                 l2cap_mode = L2CAP_FCR_STREAM_MODE;
   2443             break;
   2444         default:
   2445             APPL_TRACE_ERROR1("Invalid dch oper=%d for set dch chan cfg", p_dcb->dch_oper);
   2446             break;
   2447     }
   2448     p_dcb->chnl_cfg.fcr_opt.mode        = l2cap_mode;
   2449     p_dcb->chnl_cfg.fcr_opt.mps         = bta_hl_set_mps(p_dcb->max_rx_apdu_size);
   2450     p_dcb->chnl_cfg.fcr_opt.tx_win_sz   = bta_hl_set_tx_win_size(p_dcb->max_rx_apdu_size,
   2451                                                                  p_dcb->chnl_cfg.fcr_opt.mps);
   2452     p_dcb->chnl_cfg.fcr_opt.max_transmit= BTA_HL_L2C_MAX_TRANSMIT;
   2453     p_dcb->chnl_cfg.fcr_opt.rtrans_tout = BTA_HL_L2C_RTRANS_TOUT;
   2454     p_dcb->chnl_cfg.fcr_opt.mon_tout    = BTA_HL_L2C_MON_TOUT;
   2455 
   2456     p_dcb->chnl_cfg.user_rx_pool_id     = bta_hl_set_user_rx_pool_id(p_dcb->max_rx_apdu_size);
   2457     p_dcb->chnl_cfg.user_tx_pool_id     = bta_hl_set_user_tx_pool_id(p_dcb->max_tx_apdu_size);
   2458     p_dcb->chnl_cfg.fcr_rx_pool_id      = BTA_HL_L2C_FCR_RX_POOL_ID;
   2459     p_dcb->chnl_cfg.fcr_tx_pool_id      = BTA_HL_L2C_FCR_TX_POOL_ID;
   2460     p_dcb->chnl_cfg.data_mtu            = p_dcb->max_rx_apdu_size;
   2461 
   2462     p_dcb->chnl_cfg.fcs = BTA_HL_MCA_NO_FCS;
   2463     if (local_mdep_cfg_idx !=  BTA_HL_ECHO_TEST_MDEP_CFG_IDX)
   2464     {
   2465         if (p_sup_feature->mdep[local_mdep_cfg_idx].mdep_cfg.mdep_role ==
   2466             BTA_HL_MDEP_ROLE_SOURCE)
   2467         {
   2468             p_dcb->chnl_cfg.fcs = BTA_HL_DEFAULT_SOURCE_FCS;
   2469         }
   2470     }
   2471     else
   2472     {
   2473         p_dcb->chnl_cfg.fcs = BTA_HL_MCA_USE_FCS;
   2474     }
   2475 
   2476 #if BTA_HL_DEBUG == TRUE
   2477     APPL_TRACE_DEBUG1("L2CAP Params l2cap_mode[3-ERTM 4-STREAM]=%d", l2cap_mode);
   2478     APPL_TRACE_DEBUG2("Use FCS =%s mtu=%d", ((p_dcb->chnl_cfg.fcs & 1)?"YES":"NO"),
   2479                       p_dcb->chnl_cfg.data_mtu);
   2480     APPL_TRACE_DEBUG5("tx_win_sz=%d, max_transmit=%d, rtrans_tout=%d, mon_tout=%d, mps=%d",
   2481                       p_dcb->chnl_cfg.fcr_opt.tx_win_sz,
   2482                       p_dcb->chnl_cfg.fcr_opt.max_transmit,
   2483                       p_dcb->chnl_cfg.fcr_opt.rtrans_tout,
   2484                       p_dcb->chnl_cfg.fcr_opt.mon_tout,
   2485                       p_dcb->chnl_cfg.fcr_opt.mps);
   2486 
   2487     APPL_TRACE_DEBUG4("USER rx_pool_id=%d, tx_pool_id=%d, FCR rx_pool_id=%d, tx_pool_id=%d",
   2488                       p_dcb->chnl_cfg.user_rx_pool_id,
   2489                       p_dcb->chnl_cfg.user_tx_pool_id,
   2490                       p_dcb->chnl_cfg.fcr_rx_pool_id,
   2491                       p_dcb->chnl_cfg.fcr_tx_pool_id);
   2492 
   2493 #endif
   2494 
   2495 
   2496 
   2497 
   2498 
   2499 
   2500 
   2501 
   2502 }
   2503 
   2504 /*******************************************************************************
   2505 **
   2506 ** Function      bta_hl_get_l2cap_cfg
   2507 **
   2508 ** Description    This function get the current L2CAP channel configuration
   2509 **
   2510 ** Returns     BOOLEAN - TRUE - operation is successful
   2511 *******************************************************************************/
   2512 BOOLEAN bta_hl_get_l2cap_cfg(tBTA_HL_MDL_HANDLE mdl_hnd, tBTA_HL_L2CAP_CFG_INFO *p_cfg)
   2513 {
   2514     BOOLEAN success = FALSE;
   2515     UINT16 lcid;
   2516     tL2CAP_CFG_INFO *p_our_cfg;
   2517     tL2CAP_CH_CFG_BITS our_cfg_bits;
   2518     tL2CAP_CFG_INFO *p_peer_cfg;
   2519     tL2CAP_CH_CFG_BITS peer_cfg_bits;
   2520 
   2521     lcid = MCA_GetL2CapChannel((tMCA_DL) mdl_hnd);
   2522     if ( lcid &&
   2523          L2CA_GetCurrentConfig(lcid, &p_our_cfg, &our_cfg_bits, &p_peer_cfg,
   2524                                &peer_cfg_bits))
   2525     {
   2526         p_cfg->fcs = BTA_HL_MCA_NO_FCS;
   2527         if (our_cfg_bits & L2CAP_CH_CFG_MASK_FCS)
   2528         {
   2529             p_cfg->fcs |= p_our_cfg->fcs;
   2530         }
   2531         else
   2532         {
   2533             p_cfg->fcs = BTA_HL_MCA_USE_FCS;
   2534         }
   2535 
   2536         if (p_cfg->fcs != BTA_HL_MCA_USE_FCS )
   2537         {
   2538             if (peer_cfg_bits & L2CAP_CH_CFG_MASK_FCS)
   2539             {
   2540                 p_cfg->fcs |= p_peer_cfg->fcs;
   2541             }
   2542             else
   2543             {
   2544                 p_cfg->fcs = BTA_HL_MCA_USE_FCS;
   2545             }
   2546         }
   2547 
   2548         p_cfg->mtu =0;
   2549         if (peer_cfg_bits & L2CAP_CH_CFG_MASK_MTU)
   2550         {
   2551             p_cfg->mtu = p_peer_cfg->mtu;
   2552         }
   2553         else
   2554         {
   2555             p_cfg->mtu = L2CAP_DEFAULT_MTU;
   2556         }
   2557         success = TRUE;
   2558     }
   2559 
   2560 #if BTA_HL_DEBUG == TRUE
   2561     if (!success)
   2562     {
   2563         APPL_TRACE_DEBUG3("bta_hl_get_l2cap_cfg success=%d mdl=%d lcid=%d", success, mdl_hnd, lcid);
   2564         APPL_TRACE_DEBUG2("l2cap mtu=%d fcs=%d", p_cfg->mtu, p_cfg->fcs);
   2565     }
   2566 #endif
   2567 
   2568     return success;
   2569 }
   2570 
   2571 /*******************************************************************************
   2572 **
   2573 ** Function      bta_hl_validate_chan_cfg
   2574 **
   2575 ** Description    This function validates the L2CAP channel configuration
   2576 **
   2577 ** Returns     BOOLEAN - TRUE - validation is successful
   2578 *******************************************************************************/
   2579 BOOLEAN bta_hl_validate_chan_cfg(UINT8 app_idx, UINT8 mcl_idx, UINT8 mdl_idx)
   2580 {
   2581     tBTA_HL_APP_CB *p_acb  = BTA_HL_GET_APP_CB_PTR(app_idx);
   2582     tBTA_HL_MDL_CB *p_dcb  = BTA_HL_GET_MDL_CB_PTR(app_idx, mcl_idx, mdl_idx);
   2583     BOOLEAN success = FALSE;
   2584     UINT8 mdl_cfg_idx;
   2585     tBTA_HL_L2CAP_CFG_INFO l2cap_cfg;
   2586 
   2587 
   2588     if (bta_hl_get_l2cap_cfg(p_dcb->mdl_handle, &l2cap_cfg) &&
   2589         bta_hl_find_mdl_cfg_idx(app_idx, mcl_idx, p_dcb->mdl_id, &mdl_cfg_idx))
   2590     {
   2591         if ((p_acb->mdl_cfg[mdl_cfg_idx].mtu <= l2cap_cfg.mtu) &&
   2592             (p_acb->mdl_cfg[mdl_cfg_idx].fcs == l2cap_cfg.fcs) &&
   2593             (p_acb->mdl_cfg[mdl_cfg_idx].dch_mode == p_dcb->dch_mode))
   2594         {
   2595             success = TRUE;
   2596         }
   2597     }
   2598 
   2599 
   2600 #if BTA_HL_DEBUG == TRUE
   2601 
   2602     if (p_dcb->mtu != l2cap_cfg.mtu)
   2603     {
   2604         APPL_TRACE_WARNING2("MCAP and L2CAP peer mtu size out of sync from MCAP mtu=%d from l2cap mtu=%d",
   2605                             p_dcb->mtu, l2cap_cfg.mtu);
   2606     }
   2607 
   2608     if (!success)
   2609     {
   2610         APPL_TRACE_DEBUG4("bta_hl_validate_chan_cfg success=%d app_idx=%d mcl_idx=%d mdl_idx=%d",success, app_idx, mcl_idx, mdl_idx);
   2611         APPL_TRACE_DEBUG3("Cur. L2cap cfg mtu=%d fcs=%d dch_mode=%d", l2cap_cfg.mtu, l2cap_cfg.fcs, p_dcb->dch_mode);
   2612         APPL_TRACE_DEBUG3("From saved: L2cap cfg mtu=%d fcs=%d dch_mode=%d", p_acb->mdl_cfg[mdl_cfg_idx].mtu,
   2613                           p_acb->mdl_cfg[mdl_cfg_idx].fcs , p_acb->mdl_cfg[mdl_cfg_idx].dch_mode);
   2614     }
   2615 #endif
   2616 
   2617     return success;
   2618 }
   2619 
   2620 
   2621 /*******************************************************************************
   2622 **
   2623 ** Function      bta_hl_is_cong_on
   2624 **
   2625 ** Description    This function checks whether the congestion condition is on or not
   2626 **
   2627 ** Returns      BOOLEAN - TRUE DCH is congested
   2628 **                        FALSE not congested
   2629 **
   2630 *******************************************************************************/
   2631 BOOLEAN  bta_hl_is_cong_on(UINT8 app_id, BD_ADDR bd_addr, tBTA_HL_MDL_ID mdl_id)
   2632 
   2633 {
   2634     tBTA_HL_MDL_CB      *p_dcb;
   2635     UINT8   app_idx, mcl_idx, mdl_idx;
   2636     BOOLEAN cong_status = TRUE;
   2637 
   2638     if (bta_hl_find_app_idx(app_id, &app_idx))
   2639     {
   2640         if (bta_hl_find_mcl_idx(app_idx, bd_addr, &mcl_idx ))
   2641         {
   2642             if (bta_hl_find_mdl_idx(app_idx, mcl_idx, mdl_id, &mdl_idx ))
   2643             {
   2644                 p_dcb = BTA_HL_GET_MDL_CB_PTR(app_idx, mcl_idx, mdl_idx);
   2645                 cong_status  = p_dcb->cong;
   2646             }
   2647         }
   2648     }
   2649 
   2650     return cong_status;
   2651 }
   2652 
   2653 /*******************************************************************************
   2654 **
   2655 ** Function      bta_hl_check_cch_close
   2656 **
   2657 ** Description   This function checks whether there is a pending CCH close request
   2658 **               or not
   2659 **
   2660 ** Returns      void
   2661 *******************************************************************************/
   2662 void bta_hl_check_cch_close(UINT8 app_idx, UINT8 mcl_idx, tBTA_HL_DATA *p_data, BOOLEAN check_dch_setup )
   2663 {
   2664     tBTA_HL_MCL_CB      *p_mcb  = BTA_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
   2665     tBTA_HL_MDL_CB      *p_dcb;
   2666     UINT8               mdl_idx;
   2667 
   2668 #if (BTA_HL_DEBUG == TRUE)
   2669     APPL_TRACE_DEBUG1("bta_hl_check_cch_close cch_close_dch_oper=%d",p_mcb->cch_close_dch_oper );
   2670 #endif
   2671 
   2672     if (p_mcb->cch_oper == BTA_HL_CCH_OP_LOCAL_CLOSE)
   2673     {
   2674         if (check_dch_setup && bta_hl_find_dch_setup_mdl_idx(app_idx, mcl_idx, &mdl_idx))
   2675         {
   2676             p_dcb = BTA_HL_GET_MDL_CB_PTR(app_idx, mcl_idx, mdl_idx);
   2677             if (!p_mcb->rsp_tout)
   2678             {
   2679                 p_mcb->cch_close_dch_oper = BTA_HL_CCH_CLOSE_OP_DCH_ABORT;
   2680 
   2681                 if (!p_dcb->abort_oper)
   2682                 {
   2683                     p_dcb->abort_oper |= BTA_HL_ABORT_CCH_CLOSE_MASK;
   2684                     bta_hl_dch_sm_execute(app_idx, mcl_idx, mdl_idx, BTA_HL_DCH_ABORT_EVT, p_data);
   2685                 }
   2686             }
   2687             else
   2688             {
   2689                 p_mcb->cch_close_dch_oper = BTA_HL_CCH_CLOSE_OP_DCH_CLOSE;
   2690                 bta_hl_dch_sm_execute(app_idx, mcl_idx, mdl_idx, BTA_HL_DCH_CLOSE_CMPL_EVT, p_data);
   2691             }
   2692         }
   2693         else if (bta_hl_find_an_active_mdl_idx(app_idx, mcl_idx,&mdl_idx))
   2694         {
   2695             p_mcb->cch_close_dch_oper = BTA_HL_CCH_CLOSE_OP_DCH_CLOSE;
   2696             bta_hl_dch_sm_execute(app_idx, mcl_idx, mdl_idx, BTA_HL_DCH_CLOSE_EVT, p_data);
   2697         }
   2698         else
   2699         {
   2700             p_mcb->cch_close_dch_oper = BTA_HL_CCH_CLOSE_OP_DCH_NONE;
   2701             bta_hl_cch_sm_execute(app_idx, mcl_idx, BTA_HL_CCH_CLOSE_EVT, p_data);
   2702         }
   2703     }
   2704 }
   2705 
   2706 /*******************************************************************************
   2707 **
   2708 ** Function         bta_hl_clean_app
   2709 **
   2710 ** Description      Cleans up the HDP application resources and control block
   2711 **
   2712 ** Returns          void
   2713 **
   2714 *******************************************************************************/
   2715 void bta_hl_clean_app(UINT8 app_idx)
   2716 {
   2717     tBTA_HL_APP_CB         *p_acb   = BTA_HL_GET_APP_CB_PTR(app_idx);
   2718     int i, num_act_apps=0;
   2719 
   2720 #if BTA_HL_DEBUG == TRUE
   2721     APPL_TRACE_DEBUG0("bta_hl_clean_app");
   2722 #endif
   2723     MCA_Deregister((tMCA_HANDLE)p_acb->app_handle);
   2724 
   2725     if (p_acb->sdp_handle) SDP_DeleteRecord(p_acb->sdp_handle);
   2726 
   2727     memset((void *) p_acb, 0, sizeof(tBTA_HL_APP_CB));
   2728 
   2729     /* check any application is still active */
   2730     for (i=0; i < BTA_HL_NUM_APPS ; i ++)
   2731     {
   2732         p_acb = BTA_HL_GET_APP_CB_PTR(i);
   2733         if (p_acb->in_use) num_act_apps++;
   2734     }
   2735 
   2736     if (!num_act_apps)
   2737     {
   2738         bta_sys_remove_uuid(UUID_SERVCLASS_HDP_PROFILE);
   2739     }
   2740 }
   2741 
   2742 /*******************************************************************************
   2743 **
   2744 ** Function      bta_hl_check_deregistration
   2745 **
   2746 ** Description   This function checks whether there is a pending deregistration
   2747 **               request or not
   2748 **
   2749 ** Returns      void
   2750 *******************************************************************************/
   2751 void bta_hl_check_deregistration(UINT8 app_idx, tBTA_HL_DATA *p_data )
   2752 {
   2753     tBTA_HL_APP_CB      *p_acb  = BTA_HL_GET_APP_CB_PTR(app_idx);
   2754     tBTA_HL_MCL_CB      *p_mcb;
   2755     UINT8               mcl_idx;
   2756     tBTA_HL             evt_data;
   2757 
   2758 #if (BTA_HL_DEBUG == TRUE)
   2759     APPL_TRACE_DEBUG0("bta_hl_check_deregistration");
   2760 #endif
   2761 
   2762     if (p_acb->deregistering)
   2763     {
   2764         if (bta_hl_find_an_in_use_mcl_idx(app_idx, &mcl_idx))
   2765         {
   2766             p_mcb  = BTA_HL_GET_MCL_CB_PTR(app_idx, mcl_idx);
   2767             if (p_mcb->cch_oper != BTA_HL_CCH_OP_LOCAL_CLOSE)
   2768             {
   2769                 p_mcb->cch_oper = BTA_HL_CCH_OP_LOCAL_CLOSE;
   2770                 bta_hl_check_cch_close(app_idx,mcl_idx,p_data, TRUE);
   2771             }
   2772         }
   2773         else
   2774         {
   2775             /* all cchs are closed */
   2776             evt_data.dereg_cfm.app_handle = p_acb->app_handle;
   2777             evt_data.dereg_cfm.status = BTA_HL_STATUS_OK;
   2778             p_acb->p_cback(BTA_HL_DEREGISTER_CFM_EVT, (tBTA_HL *) &evt_data );
   2779             bta_hl_clean_app(app_idx);
   2780             bta_hl_check_disable(p_data);
   2781         }
   2782     }
   2783 }
   2784 
   2785 
   2786 /*******************************************************************************
   2787 **
   2788 ** Function      bta_hl_check_disable
   2789 **
   2790 ** Description   This function checks whether there is a pending disable
   2791 **               request or not
   2792 **
   2793 ** Returns      void
   2794 **
   2795 *******************************************************************************/
   2796 void bta_hl_check_disable(tBTA_HL_DATA *p_data )
   2797 {
   2798     tBTA_HL_CB          *p_cb= &bta_hl_cb;
   2799     tBTA_HL_APP_CB      *p_acb;
   2800     UINT8               app_idx;
   2801     tBTA_HL_CTRL        evt_data;
   2802 
   2803 #if (BTA_HL_DEBUG == TRUE)
   2804     APPL_TRACE_DEBUG0("bta_hl_check_disable");
   2805 #endif
   2806 
   2807     if (bta_hl_cb.disabling)
   2808     {
   2809         if (bta_hl_find_an_in_use_app_idx(&app_idx))
   2810         {
   2811             p_acb  = BTA_HL_GET_APP_CB_PTR(app_idx);
   2812             if (!p_acb->deregistering)
   2813             {
   2814                 p_acb->deregistering = TRUE;
   2815                 bta_hl_check_deregistration(app_idx, p_data);
   2816             }
   2817         }
   2818         else
   2819         {
   2820             /* all apps are deregistered */
   2821             bta_sys_deregister(BTA_ID_HL);
   2822             evt_data.disable_cfm.status = BTA_HL_STATUS_OK;
   2823             if (p_cb->p_ctrl_cback) p_cb->p_ctrl_cback(BTA_HL_CTRL_DISABLE_CFM_EVT, (tBTA_HL_CTRL *) &evt_data);
   2824             memset((void *) p_cb, 0, sizeof(tBTA_HL_CB));
   2825         }
   2826     }
   2827 }
   2828 
   2829 /*******************************************************************************
   2830 **
   2831 ** Function      bta_hl_build_abort_cfm
   2832 **
   2833 ** Description   This function builds the abort confirmation event data
   2834 **
   2835 ** Returns      None
   2836 **
   2837 *******************************************************************************/
   2838 void  bta_hl_build_abort_cfm(tBTA_HL *p_evt_data,
   2839                              tBTA_HL_APP_HANDLE app_handle,
   2840                              tBTA_HL_MCL_HANDLE mcl_handle,
   2841                              tBTA_HL_STATUS status)
   2842 {
   2843     p_evt_data->dch_abort_cfm.status = status;
   2844     p_evt_data->dch_abort_cfm.mcl_handle = mcl_handle;
   2845     p_evt_data->dch_abort_cfm.app_handle = app_handle;
   2846 }
   2847 
   2848 /*******************************************************************************
   2849 **
   2850 ** Function      bta_hl_build_abort_ind
   2851 **
   2852 ** Description   This function builds the abort indication event data
   2853 **
   2854 ** Returns      None
   2855 **
   2856 *******************************************************************************/
   2857 void  bta_hl_build_abort_ind(tBTA_HL *p_evt_data,
   2858                              tBTA_HL_APP_HANDLE app_handle,
   2859                              tBTA_HL_MCL_HANDLE mcl_handle)
   2860 {
   2861     p_evt_data->dch_abort_ind.mcl_handle = mcl_handle;
   2862     p_evt_data->dch_abort_ind.app_handle = app_handle;
   2863 }
   2864 /*******************************************************************************
   2865 **
   2866 ** Function      bta_hl_build_close_cfm
   2867 **
   2868 ** Description   This function builds the close confirmation event data
   2869 **
   2870 ** Returns      None
   2871 **
   2872 *******************************************************************************/
   2873 void  bta_hl_build_dch_close_cfm(tBTA_HL *p_evt_data,
   2874                                  tBTA_HL_APP_HANDLE app_handle,
   2875                                  tBTA_HL_MCL_HANDLE mcl_handle,
   2876                                  tBTA_HL_MDL_HANDLE mdl_handle,
   2877                                  tBTA_HL_STATUS status)
   2878 {
   2879     p_evt_data->dch_close_cfm.status = status;
   2880     p_evt_data->dch_close_cfm.mdl_handle = mdl_handle;
   2881     p_evt_data->dch_close_cfm.mcl_handle = mcl_handle;
   2882     p_evt_data->dch_close_cfm.app_handle = app_handle;
   2883 }
   2884 
   2885 /*******************************************************************************
   2886 **
   2887 ** Function      bta_hl_build_dch_close_ind
   2888 **
   2889 ** Description   This function builds the close indication event data
   2890 **
   2891 ** Returns      None
   2892 **
   2893 *******************************************************************************/
   2894 void  bta_hl_build_dch_close_ind(tBTA_HL *p_evt_data,
   2895                                  tBTA_HL_APP_HANDLE app_handle,
   2896                                  tBTA_HL_MCL_HANDLE mcl_handle,
   2897                                  tBTA_HL_MDL_HANDLE mdl_handle,
   2898                                  BOOLEAN intentional)
   2899 {
   2900     p_evt_data->dch_close_ind.mdl_handle = mdl_handle;
   2901     p_evt_data->dch_close_ind.mcl_handle = mcl_handle;
   2902     p_evt_data->dch_close_ind.app_handle = app_handle;
   2903     p_evt_data->dch_close_ind.intentional = intentional;
   2904 }
   2905 
   2906 /*******************************************************************************
   2907 **
   2908 ** Function      bta_hl_build_send_data_cfm
   2909 **
   2910 ** Description   This function builds the send data confirmation event data
   2911 **
   2912 ** Returns      None
   2913 **
   2914 *******************************************************************************/
   2915 void  bta_hl_build_send_data_cfm(tBTA_HL *p_evt_data,
   2916                                  tBTA_HL_APP_HANDLE app_handle,
   2917                                  tBTA_HL_MCL_HANDLE mcl_handle,
   2918                                  tBTA_HL_MDL_HANDLE mdl_handle,
   2919                                  tBTA_HL_STATUS status )
   2920 {
   2921 
   2922     p_evt_data->dch_send_data_cfm.mdl_handle = mdl_handle;
   2923     p_evt_data->dch_send_data_cfm.mcl_handle = mcl_handle;
   2924     p_evt_data->dch_send_data_cfm.app_handle = app_handle;
   2925     p_evt_data->dch_send_data_cfm.status = status;
   2926 }
   2927 
   2928 /*******************************************************************************
   2929 **
   2930 ** Function      bta_hl_build_rcv_data_ind
   2931 **
   2932 ** Description   This function builds the received data indication event data
   2933 **
   2934 ** Returns      None
   2935 **
   2936 *******************************************************************************/
   2937 void  bta_hl_build_rcv_data_ind(tBTA_HL *p_evt_data,
   2938                                 tBTA_HL_APP_HANDLE app_handle,
   2939                                 tBTA_HL_MCL_HANDLE mcl_handle,
   2940                                 tBTA_HL_MDL_HANDLE mdl_handle)
   2941 {
   2942     p_evt_data->dch_rcv_data_ind.mdl_handle = mdl_handle;
   2943     p_evt_data->dch_rcv_data_ind.mcl_handle = mcl_handle;
   2944     p_evt_data->dch_rcv_data_ind.app_handle = app_handle;
   2945 }
   2946 
   2947 
   2948 /*******************************************************************************
   2949 **
   2950 ** Function      bta_hl_build_cch_open_cfm
   2951 **
   2952 ** Description   This function builds the CCH open confirmation event data
   2953 **
   2954 ** Returns      None
   2955 **
   2956 *******************************************************************************/
   2957 void  bta_hl_build_cch_open_cfm(tBTA_HL *p_evt_data,
   2958                                 tBTA_HL_APP_HANDLE app_handle,
   2959                                 tBTA_HL_MCL_HANDLE mcl_handle,
   2960                                 BD_ADDR bd_addr,
   2961                                 tBTA_HL_STATUS status )
   2962 {
   2963 
   2964     p_evt_data->cch_open_cfm.app_handle = app_handle;
   2965     p_evt_data->cch_open_cfm.mcl_handle = mcl_handle;
   2966     bdcpy(p_evt_data->cch_open_cfm.bd_addr, bd_addr);
   2967     p_evt_data->cch_open_cfm.status = status;
   2968 }
   2969 
   2970 /*******************************************************************************
   2971 **
   2972 ** Function      bta_hl_build_cch_open_ind
   2973 **
   2974 ** Description   This function builds the CCH open indication event data
   2975 **
   2976 ** Returns      None
   2977 **
   2978 *******************************************************************************/
   2979 void  bta_hl_build_cch_open_ind(tBTA_HL *p_evt_data, tBTA_HL_APP_HANDLE app_handle,
   2980                                 tBTA_HL_MCL_HANDLE mcl_handle,
   2981                                 BD_ADDR bd_addr )
   2982 {
   2983 
   2984     p_evt_data->cch_open_ind.app_handle = app_handle;
   2985     p_evt_data->cch_open_ind.mcl_handle = mcl_handle;
   2986     bdcpy(p_evt_data->cch_open_ind.bd_addr, bd_addr);
   2987 }
   2988 
   2989 /*******************************************************************************
   2990 **
   2991 ** Function      bta_hl_build_cch_close_cfm
   2992 **
   2993 ** Description   This function builds the CCH close confirmation event data
   2994 **
   2995 ** Returns      None
   2996 **
   2997 *******************************************************************************/
   2998 void  bta_hl_build_cch_close_cfm(tBTA_HL *p_evt_data,
   2999                                  tBTA_HL_APP_HANDLE app_handle,
   3000                                  tBTA_HL_MCL_HANDLE mcl_handle,
   3001                                  tBTA_HL_STATUS status )
   3002 {
   3003     p_evt_data->cch_close_cfm.mcl_handle = mcl_handle;
   3004     p_evt_data->cch_close_cfm.app_handle = app_handle;
   3005     p_evt_data->cch_close_cfm.status = status;
   3006 }
   3007 
   3008 
   3009 /*******************************************************************************
   3010 **
   3011 ** Function      bta_hl_build_cch_close_ind
   3012 **
   3013 ** Description   This function builds the CCH colse indication event data
   3014 **
   3015 ** Returns      None
   3016 **
   3017 *******************************************************************************/
   3018 void  bta_hl_build_cch_close_ind(tBTA_HL *p_evt_data,
   3019                                  tBTA_HL_APP_HANDLE app_handle,
   3020                                  tBTA_HL_MCL_HANDLE mcl_handle,
   3021                                  BOOLEAN intentional)
   3022 {
   3023     p_evt_data->cch_close_ind.mcl_handle = mcl_handle;
   3024     p_evt_data->cch_close_ind.app_handle = app_handle;
   3025     p_evt_data->cch_close_ind.intentional = intentional;
   3026 }
   3027 
   3028 /*******************************************************************************
   3029 **
   3030 ** Function      bta_hl_build_dch_open_cfm
   3031 **
   3032 ** Description   This function builds the DCH open confirmation event data
   3033 **
   3034 ** Returns      None
   3035 **
   3036 *******************************************************************************/
   3037 void  bta_hl_build_dch_open_cfm(tBTA_HL *p_evt_data,
   3038                                 tBTA_HL_APP_HANDLE app_handle,
   3039                                 tBTA_HL_MCL_HANDLE mcl_handle,
   3040                                 tBTA_HL_MDL_HANDLE mdl_handle,
   3041                                 tBTA_HL_MDEP_ID local_mdep_id,
   3042                                 tBTA_HL_MDL_ID mdl_id,
   3043                                 tBTA_HL_DCH_MODE dch_mode,
   3044                                 BOOLEAN first_reliable,
   3045                                 UINT16 mtu,
   3046                                 tBTA_HL_STATUS status)
   3047 
   3048 {
   3049     p_evt_data->dch_open_cfm.mdl_handle = mdl_handle;
   3050     p_evt_data->dch_open_cfm.mcl_handle = mcl_handle;
   3051     p_evt_data->dch_open_cfm.app_handle = app_handle;
   3052     p_evt_data->dch_open_cfm.local_mdep_id = local_mdep_id;
   3053     p_evt_data->dch_open_cfm.mdl_id = mdl_id;
   3054     p_evt_data->dch_open_cfm.dch_mode = dch_mode;
   3055     p_evt_data->dch_open_cfm.first_reliable = first_reliable;
   3056     p_evt_data->dch_open_cfm.mtu = mtu;
   3057     p_evt_data->dch_open_cfm.status = status;
   3058 }
   3059 
   3060 
   3061 /*******************************************************************************
   3062 **
   3063 ** Function      bta_hl_build_sdp_query_cfm
   3064 **
   3065 ** Description   This function builds the SDP query indication event data
   3066 **
   3067 ** Returns      None
   3068 **
   3069 *******************************************************************************/
   3070 void  bta_hl_build_sdp_query_cfm(tBTA_HL *p_evt_data,
   3071                                  tBTA_HL_APP_HANDLE app_handle,
   3072                                  BD_ADDR bd_addr,
   3073                                  tBTA_HL_SDP *p_sdp,
   3074                                  tBTA_HL_STATUS status)
   3075 
   3076 {
   3077     p_evt_data->sdp_query_cfm.app_handle = app_handle;
   3078     bdcpy(p_evt_data->sdp_query_cfm.bd_addr, bd_addr);
   3079     p_evt_data->sdp_query_cfm.p_sdp = p_sdp;
   3080     p_evt_data->sdp_query_cfm.status = status;
   3081 }
   3082 
   3083 
   3084 /*******************************************************************************
   3085 **
   3086 ** Function      bta_hl_build_delete_mdl_cfm
   3087 **
   3088 ** Description   This function builds the delete MDL confirmation event data
   3089 **
   3090 ** Returns      None
   3091 **
   3092 *******************************************************************************/
   3093 void  bta_hl_build_delete_mdl_cfm(tBTA_HL *p_evt_data,
   3094                                   tBTA_HL_APP_HANDLE app_handle,
   3095                                   tBTA_HL_MCL_HANDLE mcl_handle,
   3096                                   tBTA_HL_MDL_ID mdl_id,
   3097                                   tBTA_HL_STATUS status)
   3098 
   3099 {
   3100     p_evt_data->delete_mdl_cfm.mcl_handle = mcl_handle;
   3101     p_evt_data->delete_mdl_cfm.app_handle = app_handle;
   3102     p_evt_data->delete_mdl_cfm.mdl_id = mdl_id;
   3103     p_evt_data->delete_mdl_cfm.status = status;
   3104 }
   3105 
   3106 /*******************************************************************************
   3107 **
   3108 ** Function      bta_hl_build_echo_test_cfm
   3109 **
   3110 ** Description   This function builds the echo test confirmation event data
   3111 **
   3112 ** Returns      None
   3113 **
   3114 *******************************************************************************/
   3115 void  bta_hl_build_echo_test_cfm(tBTA_HL *p_evt_data,
   3116                                  tBTA_HL_APP_HANDLE app_handle,
   3117                                  tBTA_HL_MCL_HANDLE mcl_handle,
   3118                                  tBTA_HL_STATUS status )
   3119 {
   3120     p_evt_data->echo_test_cfm.mcl_handle = mcl_handle;
   3121     p_evt_data->echo_test_cfm.app_handle = app_handle;
   3122     p_evt_data->echo_test_cfm.status = status;
   3123 }
   3124 
   3125 
   3126 /*****************************************************************************
   3127 **  Debug Functions
   3128 *****************************************************************************/
   3129 #if (BTA_HL_DEBUG == TRUE)
   3130 
   3131 /*******************************************************************************
   3132 **
   3133 ** Function         bta_hl_status_code
   3134 **
   3135 ** Description      get the status string pointer
   3136 **
   3137 ** Returns          char * - status string pointer
   3138 **
   3139 *******************************************************************************/
   3140 char *bta_hl_status_code(tBTA_HL_STATUS status)
   3141 {
   3142     switch (status)
   3143     {
   3144         case BTA_HL_STATUS_OK:
   3145             return "BTA_HL_STATUS_OK";
   3146         case BTA_HL_STATUS_FAIL:
   3147             return "BTA_HL_STATUS_FAIL";
   3148         case BTA_HL_STATUS_ABORTED:
   3149             return "BTA_HL_STATUS_ABORTED";
   3150         case BTA_HL_STATUS_NO_RESOURCE:
   3151             return "BTA_HL_STATUS_NO_RESOURCE";
   3152         case BTA_HL_STATUS_LAST_ITEM:
   3153             return "BTA_HL_STATUS_LAST_ITEM";
   3154         case BTA_HL_STATUS_DUPLICATE_APP_ID:
   3155             return "BTA_HL_STATUS_DUPLICATE_APP_ID";
   3156         case BTA_HL_STATUS_INVALID_APP_HANDLE:
   3157             return "BTA_HL_STATUS_INVALID_APP_HANDLE";
   3158         case BTA_HL_STATUS_INVALID_MCL_HANDLE:
   3159             return "BTA_HL_STATUS_INVALID_MCL_HANDLE";
   3160         case BTA_HL_STATUS_MCAP_REG_FAIL:
   3161             return "BTA_HL_STATUS_MCAP_REG_FAIL";
   3162         case BTA_HL_STATUS_MDEP_CO_FAIL:
   3163             return "BTA_HL_STATUS_MDEP_CO_FAIL";
   3164         case BTA_HL_STATUS_ECHO_CO_FAIL:
   3165             return "BTA_HL_STATUS_ECHO_CO_FAIL";
   3166         case BTA_HL_STATUS_MDL_CFG_CO_FAIL:
   3167             return "BTA_HL_STATUS_MDL_CFG_CO_FAIL";
   3168         case BTA_HL_STATUS_SDP_NO_RESOURCE:
   3169             return "BTA_HL_STATUS_SDP_NO_RESOURCE";
   3170         case BTA_HL_STATUS_SDP_FAIL:
   3171             return "BTA_HL_STATUS_SDP_FAIL";
   3172         case BTA_HL_STATUS_NO_CCH:
   3173             return "BTA_HL_STATUS_NO_CCH";
   3174         case BTA_HL_STATUS_NO_MCL:
   3175             return "BTA_HL_STATUS_NO_MCL";
   3176 
   3177         case BTA_HL_STATUS_NO_FIRST_RELIABLE:
   3178             return "BTA_HL_STATUS_NO_FIRST_RELIABLE";
   3179         case BTA_HL_STATUS_INVALID_DCH_CFG:
   3180             return "BTA_HL_STATUS_INVALID_DCH_CFG";
   3181         case BTA_HL_STATUS_INVALID_BD_ADDR:
   3182             return "BTA_HL_STATUS_INVALID_BD_ADDR";
   3183         case BTA_HL_STATUS_INVALID_RECONNECT_CFG:
   3184             return "BTA_HL_STATUS_INVALID_RECONNECT_CFG";
   3185         case BTA_HL_STATUS_ECHO_TEST_BUSY:
   3186             return "BTA_HL_STATUS_ECHO_TEST_BUSY";
   3187         case BTA_HL_STATUS_INVALID_LOCAL_MDEP_ID:
   3188             return "BTA_HL_STATUS_INVALID_LOCAL_MDEP_ID";
   3189         case BTA_HL_STATUS_INVALID_MDL_ID:
   3190             return "BTA_HL_STATUS_INVALID_MDL_ID";
   3191         case BTA_HL_STATUS_NO_MDL_ID_FOUND:
   3192             return "BTA_HL_STATUS_NO_MDL_ID_FOUND";
   3193         case BTA_HL_STATUS_DCH_BUSY:
   3194             return "BTA_HL_STATUS_DCH_BUSY";
   3195         default:
   3196             return "Unknown status code";
   3197     }
   3198 }
   3199 /*******************************************************************************
   3200 **
   3201 ** Function         bta_hl_evt_code
   3202 **
   3203 ** Description      Maps HL event code to the corresponding event string
   3204 **
   3205 ** Returns          string pointer for the associated event name
   3206 **
   3207 *******************************************************************************/
   3208 char *bta_hl_evt_code(tBTA_HL_INT_EVT evt_code)
   3209 {
   3210     switch (evt_code)
   3211     {
   3212         case BTA_HL_CCH_OPEN_EVT:
   3213             return "BTA_HL_CCH_OPEN_EVT";
   3214         case BTA_HL_CCH_SDP_OK_EVT:
   3215             return "BTA_HL_CCH_SDP_OK_EVT";
   3216         case BTA_HL_CCH_SDP_FAIL_EVT:
   3217             return "BTA_HL_CCH_SDP_FAIL_EVT";
   3218         case BTA_HL_MCA_CONNECT_IND_EVT:
   3219             return "BTA_HL_MCA_CONNECT_IND_EVT";
   3220         case BTA_HL_MCA_DISCONNECT_IND_EVT:
   3221             return "BTA_HL_MCA_DISCONNECT_IND_EVT";
   3222 
   3223         case BTA_HL_CCH_CLOSE_EVT:
   3224             return "BTA_HL_CCH_CLOSE_EVT";
   3225         case BTA_HL_CCH_CLOSE_CMPL_EVT:
   3226             return "BTA_HL_CCH_CLOSE_CMPL_EVT";
   3227         case BTA_HL_DCH_OPEN_EVT:
   3228             return "BTA_HL_DCH_OPEN_EVT";
   3229         case BTA_HL_MCA_CREATE_IND_EVT:
   3230             return "BTA_HL_MCA_CREATE_IND_EVT";
   3231         case BTA_HL_MCA_CREATE_CFM_EVT:
   3232             return "BTA_HL_MCA_CREATE_CFM_EVT";
   3233         case BTA_HL_MCA_OPEN_IND_EVT:
   3234             return "BTA_HL_MCA_OPEN_IND_EVT";
   3235         case BTA_HL_MCA_OPEN_CFM_EVT:
   3236             return "BTA_HL_MCA_OPEN_CFM_EVT";
   3237         case BTA_HL_DCH_CLOSE_EVT:
   3238             return "BTA_HL_DCH_CLOSE_EVT";
   3239         case BTA_HL_MCA_CLOSE_IND_EVT:
   3240             return "BTA_HL_MCA_CLOSE_IND_EVT";
   3241         case BTA_HL_MCA_CLOSE_CFM_EVT:
   3242             return "BTA_HL_MCA_CLOSE_CFM_EVT";
   3243         case BTA_HL_API_SEND_DATA_EVT:
   3244             return "BTA_HL_API_SEND_DATA_EVT";
   3245         case BTA_HL_MCA_RCV_DATA_EVT:
   3246             return "BTA_HL_MCA_RCV_DATA_EVT";
   3247         case BTA_HL_DCH_CLOSE_CMPL_EVT:
   3248             return "BTA_HL_DCH_CLOSE_CMPL_EVT";
   3249 
   3250         case BTA_HL_API_ENABLE_EVT:
   3251             return "BTA_HL_API_ENABLE_EVT";
   3252         case BTA_HL_API_DISABLE_EVT:
   3253             return "BTA_HL_API_DISABLE_EVT";
   3254         case BTA_HL_API_REGISTER_EVT:
   3255             return "BTA_HL_API_REGISTER_EVT";
   3256         case BTA_HL_API_DEREGISTER_EVT:
   3257             return "BTA_HL_API_DEREGISTER_EVT";
   3258 
   3259         case BTA_HL_API_CCH_OPEN_EVT:
   3260             return "BTA_HL_API_CCH_OPEN_EVT";
   3261 
   3262         case BTA_HL_API_CCH_CLOSE_EVT:
   3263             return "BTA_HL_API_CCH_CLOSE_EVT";
   3264         case BTA_HL_API_DCH_OPEN_EVT:
   3265             return "BTA_HL_API_DCH_OPEN_EVT";
   3266 
   3267         case BTA_HL_API_DCH_RECONNECT_EVT:
   3268             return "BTA_HL_API_DCH_RECONNECT_EVT";
   3269         case BTA_HL_API_DCH_CLOSE_EVT:
   3270             return "BTA_HL_API_DCH_CLOSE_EVT";
   3271         case BTA_HL_API_DELETE_MDL_EVT:
   3272             return "BTA_HL_API_DELETE_MDL_EVT";
   3273         case BTA_HL_API_DCH_ABORT_EVT:
   3274             return "BTA_HL_API_DCH_ABORT_EVT";
   3275 
   3276         case BTA_HL_DCH_RECONNECT_EVT:
   3277             return "BTA_HL_DCH_RECONNECT_EVT";
   3278         case BTA_HL_DCH_SDP_INIT_EVT:
   3279             return "BTA_HL_DCH_SDP_INIT_EVT";
   3280         case BTA_HL_DCH_SDP_FAIL_EVT:
   3281             return "BTA_HL_DCH_SDP_FAIL_EVT";
   3282         case BTA_HL_API_DCH_ECHO_TEST_EVT:
   3283             return "BTA_HL_API_DCH_ECHO_TEST_EVT";
   3284         case BTA_HL_DCH_CLOSE_ECHO_TEST_EVT:
   3285             return "BTA_HL_DCH_CLOSE_ECHO_TEST_EVT";
   3286         case BTA_HL_MCA_RECONNECT_IND_EVT:
   3287             return "BTA_HL_MCA_RECONNECT_IND_EVT";
   3288         case BTA_HL_MCA_RECONNECT_CFM_EVT:
   3289             return "BTA_HL_MCA_RECONNECT_CFM_EVT";
   3290         case BTA_HL_API_DCH_CREATE_RSP_EVT:
   3291             return "BTA_HL_API_DCH_CREATE_RSP_EVT";
   3292         case BTA_HL_DCH_ABORT_EVT:
   3293             return "BTA_HL_DCH_ABORT_EVT";
   3294         case BTA_HL_MCA_ABORT_IND_EVT:
   3295             return "BTA_HL_MCA_ABORT_IND_EVT";
   3296         case BTA_HL_MCA_ABORT_CFM_EVT:
   3297             return "BTA_HL_MCA_ABORT_CFM_EVT";
   3298         case BTA_HL_MCA_DELETE_IND_EVT:
   3299             return "BTA_HL_MCA_DELETE_IND_EVT";
   3300         case BTA_HL_MCA_DELETE_CFM_EVT:
   3301             return "BTA_HL_MCA_DELETE_CFM_EVT";
   3302         case BTA_HL_MCA_CONG_CHG_EVT:
   3303             return "BTA_HL_MCA_CONG_CHG_EVT";
   3304         case BTA_HL_CI_GET_TX_DATA_EVT:
   3305             return "BTA_HL_CI_GET_TX_DATA_EVT";
   3306         case BTA_HL_CI_PUT_RX_DATA_EVT:
   3307             return "BTA_HL_CI_PUT_RX_DATA_EVT";
   3308         case BTA_HL_CI_GET_ECHO_DATA_EVT:
   3309             return "BTA_HL_CI_GET_ECHO_DATA_EVT";
   3310         case BTA_HL_DCH_ECHO_TEST_EVT:
   3311             return "BTA_HL_DCH_ECHO_TEST_EVT";
   3312         case BTA_HL_CI_PUT_ECHO_DATA_EVT:
   3313             return "BTA_HL_CI_PUT_ECHO_DATA_EVT";
   3314         case BTA_HL_API_SDP_QUERY_EVT:
   3315             return "BTA_HL_API_SDP_QUERY_EVT";
   3316         case BTA_HL_SDP_QUERY_OK_EVT:
   3317             return "BTA_HL_SDP_QUERY_OK_EVT";
   3318         case BTA_HL_SDP_QUERY_FAIL_EVT:
   3319             return "BTA_HL_SDP_QUERY_FAIL_EVT";
   3320         case BTA_HL_MCA_RSP_TOUT_IND_EVT:
   3321             return "BTA_HL_MCA_RSP_TOUT_IND_EVT";
   3322 
   3323         default:
   3324             return "Unknown HL event code";
   3325     }
   3326 }
   3327 
   3328 #endif  /* Debug Functions */
   3329 #endif // HL_INCLUDED
   3330 
   3331 
   3332 
   3333 
   3334 
   3335 
   3336 
   3337 
   3338