1 /******************************************************************************
3 * Copyright (C) 2003-2012 Broadcom Corporation
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 ******************************************************************************/
19 /******************************************************************************
21 * This AVCTP module interfaces to L2CAP
23 ******************************************************************************/
28 #include "bt_target.h"
33 #include "osi/include/osi.h"
35 /* Configuration flags. */
36 #define AVCT_L2C_CFG_IND_DONE (1 << 0)
37 #define AVCT_L2C_CFG_CFM_DONE (1 << 1)
39 /* callback function declarations */
40 void avct_l2c_connect_ind_cback(const RawAddress& bd_addr, uint16_t lcid,
41 uint16_t psm, uint8_t id);
42 void avct_l2c_connect_cfm_cback(uint16_t lcid, uint16_t result);
43 void avct_l2c_config_cfm_cback(uint16_t lcid, tL2CAP_CFG_INFO* p_cfg);
44 void avct_l2c_config_ind_cback(uint16_t lcid, tL2CAP_CFG_INFO* p_cfg);
45 void avct_l2c_disconnect_ind_cback(uint16_t lcid, bool ack_needed);
46 void avct_l2c_disconnect_cfm_cback(uint16_t lcid, uint16_t result);
47 void avct_l2c_congestion_ind_cback(uint16_t lcid, bool is_congested);
48 void avct_l2c_data_ind_cback(uint16_t lcid, BT_HDR* p_buf);
50 /* L2CAP callback function structure */
51 const tL2CAP_APPL_INFO avct_l2c_appl = {
52 avct_l2c_connect_ind_cback,
53 avct_l2c_connect_cfm_cback,
55 avct_l2c_config_ind_cback,
56 avct_l2c_config_cfm_cback,
57 avct_l2c_disconnect_ind_cback,
58 avct_l2c_disconnect_cfm_cback,
60 avct_l2c_data_ind_cback,
61 avct_l2c_congestion_ind_cback,
62 NULL /* tL2CA_TX_COMPLETE_CB */
65 /*******************************************************************************
67 * Function avct_l2c_is_passive
69 * Description check is the CCB associated with the given LCB was created
72 * Returns true, if the given LCB is created as AVCT_PASSIVE
74 ******************************************************************************/
75 static bool avct_l2c_is_passive(tAVCT_LCB* p_lcb) {
76 bool is_passive = false;
77 tAVCT_CCB* p_ccb = &avct_cb.ccb[0];
80 for (i = 0; i < AVCT_NUM_CONN; i++, p_ccb++) {
81 if (p_ccb->allocated && (p_ccb->p_lcb == p_lcb)) {
82 AVCT_TRACE_DEBUG("avct_l2c_is_ct control:x%x", p_ccb->cc.control);
83 if (p_ccb->cc.control & AVCT_PASSIVE) {
92 /*******************************************************************************
94 * Function avct_l2c_connect_ind_cback
96 * Description This is the L2CAP connect indication callback function.
101 ******************************************************************************/
102 void avct_l2c_connect_ind_cback(const RawAddress& bd_addr, uint16_t lcid,
103 UNUSED_ATTR uint16_t psm, uint8_t id) {
105 uint16_t result = L2CAP_CONN_OK;
108 /* do we already have a channel for this peer? */
109 p_lcb = avct_lcb_by_bd(bd_addr);
111 /* no, allocate lcb */
112 p_lcb = avct_lcb_alloc(bd_addr);
114 /* no ccb available, reject L2CAP connection */
115 result = L2CAP_CONN_NO_RESOURCES;
118 /* else we already have a channel for this peer */
120 if (!avct_l2c_is_passive(p_lcb) || (p_lcb->ch_state == AVCT_CH_OPEN)) {
121 /* this LCB included CT role - reject */
122 result = L2CAP_CONN_NO_RESOURCES;
124 /* TG role only - accept the connection from CT. move the channel ID to
125 * the conflict list */
126 p_lcb->conflict_lcid = p_lcb->ch_lcid;
127 AVCT_TRACE_DEBUG("avct_l2c_connect_ind_cback conflict_lcid:0x%x",
128 p_lcb->conflict_lcid);
133 AVCT_TRACE_DEBUG("avct_l2c_connect_ind_cback: 0x%x, res: %d, ch_state: %d",
134 lcid, result, p_lcb->ch_state);
136 /* Send L2CAP connect rsp */
137 L2CA_ConnectRsp(bd_addr, id, lcid, result, 0);
139 /* if result ok, proceed with connection */
140 if (result == L2CAP_CONN_OK) {
142 p_lcb->ch_lcid = lcid;
144 /* transition to configuration state */
145 p_lcb->ch_state = AVCT_CH_CFG;
147 /* Send L2CAP config req */
148 memset(&cfg, 0, sizeof(tL2CAP_CFG_INFO));
149 cfg.mtu_present = true;
150 cfg.mtu = avct_cb.mtu;
151 L2CA_ConfigReq(lcid, &cfg);
152 AVCT_TRACE_DEBUG("avct_l2c snd Cfg Req");
155 if (p_lcb) AVCT_TRACE_DEBUG("ch_state cni: %d ", p_lcb->ch_state);
158 /*******************************************************************************
160 * Function avct_l2c_connect_cfm_cback
162 * Description This is the L2CAP connect confirm callback function.
167 ******************************************************************************/
168 void avct_l2c_connect_cfm_cback(uint16_t lcid, uint16_t result) {
172 /* look up lcb for this channel */
173 p_lcb = avct_lcb_by_lcid(lcid);
176 "avct_l2c_connect_cfm_cback lcid:0x%x result: %d ch_state: %d, "
177 "conflict_lcid:0x%x",
178 lcid, result, p_lcb->ch_state, p_lcb->conflict_lcid);
179 /* if in correct state */
180 if (p_lcb->ch_state == AVCT_CH_CONN) {
181 /* if result successful */
182 if (result == L2CAP_CONN_OK) {
183 /* set channel state */
184 p_lcb->ch_state = AVCT_CH_CFG;
186 /* Send L2CAP config req */
187 memset(&cfg, 0, sizeof(tL2CAP_CFG_INFO));
188 cfg.mtu_present = true;
189 cfg.mtu = avct_cb.mtu;
190 L2CA_ConfigReq(lcid, &cfg);
191 AVCT_TRACE_DEBUG("avct_l2c snd Cfg Req");
195 AVCT_TRACE_DEBUG("avct_l2c_connect_cfm_cback conflict_lcid:0x%x",
196 p_lcb->conflict_lcid);
197 if (p_lcb->conflict_lcid == lcid)
198 p_lcb->conflict_lcid = 0;
200 avct_lcb_event(p_lcb, AVCT_LCB_LL_CLOSE_EVT, (tAVCT_LCB_EVT*)&result);
202 } else if (p_lcb->conflict_lcid == lcid) {
203 /* we must be in AVCT_CH_CFG state for the ch_lcid channel */
205 "avct_l2c_connect_cfm_cback ch_state: %d, conflict_lcid:0x%x",
206 p_lcb->ch_state, p_lcb->conflict_lcid);
207 if (result == L2CAP_CONN_OK) {
208 /* just in case the peer also accepts our connection - Send L2CAP
210 L2CA_DisconnectReq(lcid);
212 p_lcb->conflict_lcid = 0;
214 AVCT_TRACE_DEBUG("ch_state cnc: %d ", p_lcb->ch_state);
218 /*******************************************************************************
220 * Function avct_l2c_config_cfm_cback
222 * Description This is the L2CAP config confirm callback function.
227 ******************************************************************************/
228 void avct_l2c_config_cfm_cback(uint16_t lcid, tL2CAP_CFG_INFO* p_cfg) {
231 /* look up lcb for this channel */
232 p_lcb = avct_lcb_by_lcid(lcid);
234 AVCT_TRACE_DEBUG("avct_l2c_config_cfm_cback: 0x%x, ch_state: %d, res: %d",
235 lcid, p_lcb->ch_state, p_cfg->result);
236 /* if in correct state */
237 if (p_lcb->ch_state == AVCT_CH_CFG) {
238 /* if result successful */
239 if (p_cfg->result == L2CAP_CFG_OK) {
241 p_lcb->ch_flags |= AVCT_L2C_CFG_CFM_DONE;
243 /* if configuration complete */
244 if (p_lcb->ch_flags & AVCT_L2C_CFG_IND_DONE) {
245 p_lcb->ch_state = AVCT_CH_OPEN;
246 avct_lcb_event(p_lcb, AVCT_LCB_LL_OPEN_EVT, NULL);
252 "ERROR avct_l2c_config_cfm_cback L2CA_DisconnectReq %d ",
254 /* store result value */
255 p_lcb->ch_result = p_cfg->result;
257 /* Send L2CAP disconnect req */
258 L2CA_DisconnectReq(lcid);
261 AVCT_TRACE_DEBUG("ch_state cfc: %d ", p_lcb->ch_state);
265 /*******************************************************************************
267 * Function avct_l2c_config_ind_cback
269 * Description This is the L2CAP config indication callback function.
274 ******************************************************************************/
275 void avct_l2c_config_ind_cback(uint16_t lcid, tL2CAP_CFG_INFO* p_cfg) {
278 /* look up lcb for this channel */
279 p_lcb = avct_lcb_by_lcid(lcid);
281 AVCT_TRACE_DEBUG("avct_l2c_config_ind_cback: 0x%x, ch_state: %d", lcid,
283 /* store the mtu in tbl */
284 if (p_cfg->mtu_present) {
285 p_lcb->peer_mtu = p_cfg->mtu;
287 p_lcb->peer_mtu = L2CAP_DEFAULT_MTU;
290 /* send L2CAP configure response */
291 memset(p_cfg, 0, sizeof(tL2CAP_CFG_INFO));
292 p_cfg->result = L2CAP_CFG_OK;
293 L2CA_ConfigRsp(lcid, p_cfg);
295 /* if first config ind */
296 if ((p_lcb->ch_flags & AVCT_L2C_CFG_IND_DONE) == 0) {
298 p_lcb->ch_flags |= AVCT_L2C_CFG_IND_DONE;
300 /* if configuration complete */
301 if (p_lcb->ch_flags & AVCT_L2C_CFG_CFM_DONE) {
302 p_lcb->ch_state = AVCT_CH_OPEN;
303 avct_lcb_event(p_lcb, AVCT_LCB_LL_OPEN_EVT, NULL);
306 AVCT_TRACE_DEBUG("ch_state cfi: %d ", p_lcb->ch_state);
310 /*******************************************************************************
312 * Function avct_l2c_disconnect_ind_cback
314 * Description This is the L2CAP disconnect indication callback function.
319 ******************************************************************************/
320 void avct_l2c_disconnect_ind_cback(uint16_t lcid, bool ack_needed) {
322 uint16_t result = AVCT_RESULT_FAIL;
324 /* look up lcb for this channel */
325 p_lcb = avct_lcb_by_lcid(lcid);
327 AVCT_TRACE_DEBUG("avct_l2c_disconnect_ind_cback: 0x%x, ch_state: %d", lcid,
330 /* send L2CAP disconnect response */
331 L2CA_DisconnectRsp(lcid);
334 avct_lcb_event(p_lcb, AVCT_LCB_LL_CLOSE_EVT, (tAVCT_LCB_EVT*)&result);
335 AVCT_TRACE_DEBUG("ch_state di: %d ", p_lcb->ch_state);
339 /*******************************************************************************
341 * Function avct_l2c_disconnect_cfm_cback
343 * Description This is the L2CAP disconnect confirm callback function.
348 ******************************************************************************/
349 void avct_l2c_disconnect_cfm_cback(uint16_t lcid, uint16_t result) {
353 /* look up lcb for this channel */
354 p_lcb = avct_lcb_by_lcid(lcid);
357 "avct_l2c_disconnect_cfm_cback: 0x%x, ch_state: %d, res: %d", lcid,
358 p_lcb->ch_state, result);
359 /* result value may be previously stored */
360 res = (p_lcb->ch_result != 0) ? p_lcb->ch_result : result;
361 p_lcb->ch_result = 0;
363 avct_lcb_event(p_lcb, AVCT_LCB_LL_CLOSE_EVT, (tAVCT_LCB_EVT*)&res);
364 AVCT_TRACE_DEBUG("ch_state dc: %d ", p_lcb->ch_state);
368 /*******************************************************************************
370 * Function avct_l2c_congestion_ind_cback
372 * Description This is the L2CAP congestion indication callback function.
377 ******************************************************************************/
378 void avct_l2c_congestion_ind_cback(uint16_t lcid, bool is_congested) {
381 AVCT_TRACE_DEBUG("avct_l2c_congestion_ind_cback: 0x%x", lcid);
382 /* look up lcb for this channel */
383 p_lcb = avct_lcb_by_lcid(lcid);
385 avct_lcb_event(p_lcb, AVCT_LCB_LL_CONG_EVT, (tAVCT_LCB_EVT*)&is_congested);
389 /*******************************************************************************
391 * Function avct_l2c_data_ind_cback
393 * Description This is the L2CAP data indication callback function.
398 ******************************************************************************/
399 void avct_l2c_data_ind_cback(uint16_t lcid, BT_HDR* p_buf) {
402 AVCT_TRACE_DEBUG("avct_l2c_data_ind_cback: 0x%x", lcid);
403 /* look up lcb for this channel */
404 p_lcb = avct_lcb_by_lcid(lcid);
406 avct_lcb_event(p_lcb, AVCT_LCB_LL_MSG_EVT, (tAVCT_LCB_EVT*)&p_buf);
407 } else /* prevent buffer leak */
409 AVCT_TRACE_WARNING("ERROR -> avct_l2c_data_ind_cback drop buffer");