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