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