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