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 #define LOG_TAG "avctp"
27 
28 #include <bluetooth/log.h>
29 #include <string.h>
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("avct_ccb_alloc {}", 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,
81                       const RawAddress* bd_addr) {
82   tAVCT_CTRL_CBACK* p_cback = p_ccb->cc.p_ctrl_cback;
83 
84   log::verbose("avct_ccb_dealloc {}", avct_ccb_to_idx(p_ccb));
85 
86   if (p_ccb->p_bcb == NULL) {
87     memset(p_ccb, 0, sizeof(tAVCT_CCB));
88   } else {
89     /* control channel is down, but the browsing channel is still connected 0
90      * disconnect it now */
91     avct_bcb_event(p_ccb->p_bcb, AVCT_LCB_UL_UNBIND_EVT,
92                    (tAVCT_LCB_EVT*)&p_ccb);
93     p_ccb->p_lcb = NULL;
94   }
95 
96   if (event != AVCT_NO_EVT) {
97     (*p_cback)(avct_ccb_to_idx(p_ccb), event, result, bd_addr);
98   }
99 }
100 
101 /*******************************************************************************
102  *
103  * Function         avct_ccb_to_idx
104  *
105  * Description      Given a pointer to an ccb, return its index.
106  *
107  *
108  * Returns          Index of ccb.
109  *
110  ******************************************************************************/
avct_ccb_to_idx(tAVCT_CCB * p_ccb)111 uint8_t avct_ccb_to_idx(tAVCT_CCB* p_ccb) {
112   /* use array arithmetic to determine index */
113   return (uint8_t)(p_ccb - avct_cb.ccb);
114 }
115 
116 /*******************************************************************************
117  *
118  * Function         avct_ccb_by_idx
119  *
120  * Description      Return ccb pointer based on ccb index (or handle).
121  *
122  *
123  * Returns          pointer to the ccb, or NULL if none found.
124  *
125  ******************************************************************************/
avct_ccb_by_idx(uint8_t idx)126 tAVCT_CCB* avct_ccb_by_idx(uint8_t idx) {
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       log::warn("ccb {} not allocated", idx);
137     }
138   } else {
139     p_ccb = NULL;
140     log::warn("No ccb for idx {}", idx);
141   }
142   return p_ccb;
143 }
144