blob: 75168532b3437f46ca52b7691606bbc3c1b00af9 [file] [log] [blame]
The Android Open Source Project5738f832012-12-12 16:00:35 -08001/******************************************************************************
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>
Chris Manton83e2c342014-09-29 21:37:44 -070027#include "bt_types.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080028#include "bt_target.h"
29#include "avct_api.h"
30#include "avct_int.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*******************************************************************************/
42tAVCT_CCB *avct_ccb_alloc(tAVCT_CC *p_cc)
43{
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 {
49 if (!p_ccb->allocated)
50 {
51 p_ccb->allocated = AVCT_ALOC_LCB;
52 memcpy(&p_ccb->cc, p_cc, sizeof(tAVCT_CC));
Sharvil Nanavati158084e2014-05-04 09:53:44 -070053 AVCT_TRACE_DEBUG("avct_ccb_alloc %d", i);
The Android Open Source Project5738f832012-12-12 16:00:35 -080054 break;
55 }
56 }
57
58 if (i == AVCT_NUM_CONN)
59 {
60 /* out of ccbs */
61 p_ccb = NULL;
Sharvil Nanavati158084e2014-05-04 09:53:44 -070062 AVCT_TRACE_WARNING("Out of ccbs");
The Android Open Source Project5738f832012-12-12 16:00:35 -080063 }
64 return p_ccb;
65}
66
67/*******************************************************************************
68**
69** Function avct_ccb_dealloc
70**
71** Description Deallocate a connection control block and call application
72** callback.
73**
74**
75** Returns void.
76**
77*******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -070078void avct_ccb_dealloc(tAVCT_CCB *p_ccb, uint8_t event, uint16_t result, BD_ADDR bd_addr)
The Android Open Source Project5738f832012-12-12 16:00:35 -080079{
80 tAVCT_CTRL_CBACK *p_cback = p_ccb->cc.p_ctrl_cback;
81
Sharvil Nanavati158084e2014-05-04 09:53:44 -070082 AVCT_TRACE_DEBUG("avct_ccb_dealloc %d", avct_ccb_to_idx(p_ccb));
Avish Shaha408eb72016-06-22 06:47:20 +053083
The Android Open Source Project5738f832012-12-12 16:00:35 -080084 if(p_ccb->p_bcb == NULL)
Avish Shaha408eb72016-06-22 06:47:20 +053085 {
The Android Open Source Project5738f832012-12-12 16:00:35 -080086 memset(p_ccb, 0, sizeof(tAVCT_CCB));
Avish Shaha408eb72016-06-22 06:47:20 +053087 }
The Android Open Source Project5738f832012-12-12 16:00:35 -080088 else
89 {
90 /* control channel is down, but the browsing channel is still connected 0 disconnect it now */
91 avct_bcb_event(p_ccb->p_bcb, AVCT_LCB_UL_UNBIND_EVT, (tAVCT_LCB_EVT *) &p_ccb);
92 p_ccb->p_lcb = NULL;
93 }
The Android Open Source Project5738f832012-12-12 16:00:35 -080094
95 if (event != AVCT_NO_EVT)
96 {
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*******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -0700111uint8_t avct_ccb_to_idx(tAVCT_CCB *p_ccb)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800112{
113 /* use array arithmetic to determine index */
Marie Janssend19e0782016-07-15 12:48:27 -0700114 return (uint8_t) (p_ccb - avct_cb.ccb);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800115}
116
117/*******************************************************************************
118**
119** Function avct_ccb_by_idx
120**
121** Description Return ccb pointer based on ccb index (or handle).
122**
123**
124** Returns pointer to the ccb, or NULL if none found.
125**
126*******************************************************************************/
Marie Janssend19e0782016-07-15 12:48:27 -0700127tAVCT_CCB *avct_ccb_by_idx(uint8_t idx)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800128{
129 tAVCT_CCB *p_ccb;
130
131 /* verify index */
132 if (idx < AVCT_NUM_CONN)
133 {
134 p_ccb = &avct_cb.ccb[idx];
135
136 /* verify ccb is allocated */
137 if (!p_ccb->allocated)
138 {
139 p_ccb = NULL;
Sharvil Nanavati158084e2014-05-04 09:53:44 -0700140 AVCT_TRACE_WARNING("ccb %d not allocated", idx);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800141 }
142 }
143 else
144 {
145 p_ccb = NULL;
Sharvil Nanavati158084e2014-05-04 09:53:44 -0700146 AVCT_TRACE_WARNING("No ccb for idx %d", idx);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800147 }
148 return p_ccb;
149}