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