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 module contains functions which operate on the AVCTP connection
22  *  control block.
23  *
24  ******************************************************************************/
25 
26 #include <string.h>
27 #include "stack/bt_types.h"
28 #include "common/bt_target.h"
29 #include "stack/avct_api.h"
30 #include "avct_int.h"
31 
32 #if (defined(AVCT_INCLUDED) && AVCT_INCLUDED == TRUE)
33 
34 /*******************************************************************************
35 **
36 ** Function         avct_ccb_alloc
37 **
38 ** Description      Allocate a connection control block; copy parameters to ccb.
39 **
40 **
41 ** Returns          pointer to the ccb, or NULL if none could be allocated.
42 **
43 *******************************************************************************/
avct_ccb_alloc(tAVCT_CC * p_cc)44 tAVCT_CCB *avct_ccb_alloc(tAVCT_CC *p_cc)
45 {
46     tAVCT_CCB   *p_ccb = &avct_cb.ccb[0];
47     int         i;
48 
49     for (i = 0; i < AVCT_NUM_CONN; i++, p_ccb++) {
50         if (!p_ccb->allocated) {
51             p_ccb->allocated = AVCT_ALOC_LCB;
52             memcpy(&p_ccb->cc, p_cc, sizeof(tAVCT_CC));
53             AVCT_TRACE_DEBUG("avct_ccb_alloc %d", i);
54             break;
55         }
56     }
57 
58     if (i == AVCT_NUM_CONN) {
59         /* out of ccbs */
60         p_ccb = NULL;
61         AVCT_TRACE_WARNING("Out of ccbs");
62     }
63     return p_ccb;
64 }
65 
66 /*******************************************************************************
67 **
68 ** Function         avct_ccb_dealloc
69 **
70 ** Description      Deallocate a connection control block and call application
71 **                  callback.
72 **
73 **
74 ** Returns          void.
75 **
76 *******************************************************************************/
avct_ccb_dealloc(tAVCT_CCB * p_ccb,UINT8 event,UINT16 result,BD_ADDR bd_addr)77 void avct_ccb_dealloc(tAVCT_CCB *p_ccb, UINT8 event, UINT16 result, BD_ADDR bd_addr)
78 {
79     tAVCT_CTRL_CBACK    *p_cback = p_ccb->cc.p_ctrl_cback;
80 
81     AVCT_TRACE_DEBUG("avct_ccb_dealloc %d", avct_ccb_to_idx(p_ccb));
82 #if (AVCT_BROWSE_INCLUDED == TRUE)
83     if (p_ccb->p_bcb == NULL) {
84         memset(p_ccb, 0, sizeof(tAVCT_CCB));
85     } else {
86         /* control channel is down, but the browsing channel is still connected 0 disconnect it now */
87         avct_bcb_event(p_ccb->p_bcb, AVCT_LCB_UL_UNBIND_EVT, (tAVCT_LCB_EVT *) &p_ccb);
88         p_ccb->p_lcb = NULL;
89     }
90 #else
91     memset(p_ccb, 0, sizeof(tAVCT_CCB));
92 #endif
93 
94     if (event != AVCT_NO_EVT) {
95         (*p_cback)(avct_ccb_to_idx(p_ccb), event, result, bd_addr);
96     }
97 }
98 
99 /*******************************************************************************
100 **
101 ** Function         avct_ccb_to_idx
102 **
103 ** Description      Given a pointer to an ccb, return its index.
104 **
105 **
106 ** Returns          Index of ccb.
107 **
108 *******************************************************************************/
avct_ccb_to_idx(tAVCT_CCB * p_ccb)109 UINT8 avct_ccb_to_idx(tAVCT_CCB *p_ccb)
110 {
111     /* use array arithmetic to determine index */
112     return (UINT8) (p_ccb - avct_cb.ccb);
113 }
114 
115 /*******************************************************************************
116 **
117 ** Function         avct_ccb_by_idx
118 **
119 ** Description      Return ccb pointer based on ccb index (or handle).
120 **
121 **
122 ** Returns          pointer to the ccb, or NULL if none found.
123 **
124 *******************************************************************************/
avct_ccb_by_idx(UINT8 idx)125 tAVCT_CCB *avct_ccb_by_idx(UINT8 idx)
126 {
127     tAVCT_CCB   *p_ccb;
128 
129     /* verify index */
130     if (idx < AVCT_NUM_CONN) {
131         p_ccb = &avct_cb.ccb[idx];
132 
133         /* verify ccb is allocated */
134         if (!p_ccb->allocated) {
135             p_ccb = NULL;
136             AVCT_TRACE_WARNING("ccb %d not allocated", idx);
137         }
138     } else {
139         p_ccb = NULL;
140         AVCT_TRACE_WARNING("No ccb for idx %d", idx);
141     }
142     return p_ccb;
143 }
144 
145 #endif /* #if (defined(AVCT_INCLUDED) && AVCT_INCLUDED == TRUE) */
146