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