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 ******************************************************************************/
27 #include "bt_target.h"
34 /* Configuration flags. */
35 #define AVCT_L2C_CFG_IND_DONE (1<<0)
36 #define AVCT_L2C_CFG_CFM_DONE (1<<1)
38 /* callback function declarations */
39 void avct_l2c_connect_ind_cback(BD_ADDR bd_addr, UINT16 lcid, UINT16 psm, UINT8 id);
40 void avct_l2c_connect_cfm_cback(UINT16 lcid, UINT16 result);
41 void avct_l2c_config_cfm_cback(UINT16 lcid, tL2CAP_CFG_INFO *p_cfg);
42 void avct_l2c_config_ind_cback(UINT16 lcid, tL2CAP_CFG_INFO *p_cfg);
43 void avct_l2c_disconnect_ind_cback(UINT16 lcid, BOOLEAN ack_needed);
44 void avct_l2c_disconnect_cfm_cback(UINT16 lcid, UINT16 result);
45 void avct_l2c_congestion_ind_cback(UINT16 lcid, BOOLEAN is_congested);
46 void avct_l2c_data_ind_cback(UINT16 lcid, BT_HDR *p_buf);
48 /* L2CAP callback function structure */
49 const tL2CAP_APPL_INFO avct_l2c_appl = {
50 avct_l2c_connect_ind_cback,
51 avct_l2c_connect_cfm_cback,
53 avct_l2c_config_ind_cback,
54 avct_l2c_config_cfm_cback,
55 avct_l2c_disconnect_ind_cback,
56 avct_l2c_disconnect_cfm_cback,
58 avct_l2c_data_ind_cback,
59 avct_l2c_congestion_ind_cback,
60 NULL /* tL2CA_TX_COMPLETE_CB */
63 /*******************************************************************************
65 ** Function avct_l2c_is_passive
67 ** Description check is the CCB associated with the given LCB was created
70 ** Returns TRUE, if the given LCB is created as AVCT_PASSIVE
72 *******************************************************************************/
73 static BOOLEAN avct_l2c_is_passive (tAVCT_LCB *p_lcb)
75 BOOLEAN is_passive = FALSE;
76 tAVCT_CCB *p_ccb = &avct_cb.ccb[0];
79 for (i = 0; i < AVCT_NUM_CONN; i++, p_ccb++)
81 if (p_ccb->allocated && (p_ccb->p_lcb == p_lcb))
83 AVCT_TRACE_DEBUG("avct_l2c_is_ct control:x%x", p_ccb->cc.control);
84 if (p_ccb->cc.control & AVCT_PASSIVE)
94 /*******************************************************************************
96 ** Function avct_l2c_connect_ind_cback
98 ** Description This is the L2CAP connect indication callback function.
103 *******************************************************************************/
104 void avct_l2c_connect_ind_cback(BD_ADDR bd_addr, UINT16 lcid, UINT16 psm, UINT8 id)
107 UINT16 result = L2CAP_CONN_OK;
111 /* do we already have a channel for this peer? */
112 if ((p_lcb = avct_lcb_by_bd(bd_addr)) == NULL)
114 /* no, allocate lcb */
115 if ((p_lcb = avct_lcb_alloc(bd_addr)) == NULL)
117 /* no ccb available, reject L2CAP connection */
118 result = L2CAP_CONN_NO_RESOURCES;
121 /* else we already have a channel for this peer */
124 if (!avct_l2c_is_passive (p_lcb) || (p_lcb->ch_state == AVCT_CH_OPEN))
126 /* this LCB included CT role - reject */
127 result = L2CAP_CONN_NO_RESOURCES;
131 /* TG role only - accept the connection from CT. move the channel ID to the conflict list */
132 p_lcb->conflict_lcid = p_lcb->ch_lcid;
133 AVCT_TRACE_DEBUG("avct_l2c_connect_ind_cback conflict_lcid:0x%x", p_lcb->conflict_lcid);
139 AVCT_TRACE_DEBUG("avct_l2c_connect_ind_cback: 0x%x, res: %d, ch_state: %d",
140 lcid, result, p_lcb->ch_state);
142 /* Send L2CAP connect rsp */
143 L2CA_ConnectRsp(bd_addr, id, lcid, result, 0);
145 /* if result ok, proceed with connection */
146 if (result == L2CAP_CONN_OK)
149 p_lcb->ch_lcid = lcid;
151 /* transition to configuration state */
152 p_lcb->ch_state = AVCT_CH_CFG;
154 /* Send L2CAP config req */
155 memset(&cfg, 0, sizeof(tL2CAP_CFG_INFO));
156 cfg.mtu_present = TRUE;
157 cfg.mtu = avct_cb.mtu;
158 L2CA_ConfigReq(lcid, &cfg);
159 AVCT_TRACE_DEBUG("avct_l2c snd Cfg Req");
162 #if (BT_USE_TRACES == TRUE)
164 AVCT_TRACE_DEBUG("ch_state cni: %d ", p_lcb->ch_state);
168 /*******************************************************************************
170 ** Function avct_l2c_connect_cfm_cback
172 ** Description This is the L2CAP connect confirm callback function.
177 *******************************************************************************/
178 void avct_l2c_connect_cfm_cback(UINT16 lcid, UINT16 result)
183 /* look up lcb for this channel */
184 if ((p_lcb = avct_lcb_by_lcid(lcid)) != NULL)
186 AVCT_TRACE_DEBUG("avct_l2c_connect_cfm_cback lcid:0x%x result: %d ch_state: %d, conflict_lcid:0x%x",
187 lcid, result, p_lcb->ch_state, p_lcb->conflict_lcid);
188 /* if in correct state */
189 if (p_lcb->ch_state == AVCT_CH_CONN)
191 /* if result successful */
192 if (result == L2CAP_CONN_OK)
194 /* set channel state */
195 p_lcb->ch_state = AVCT_CH_CFG;
197 /* Send L2CAP config req */
198 memset(&cfg, 0, sizeof(tL2CAP_CFG_INFO));
199 cfg.mtu_present = TRUE;
200 cfg.mtu = avct_cb.mtu;
201 L2CA_ConfigReq(lcid, &cfg);
202 AVCT_TRACE_DEBUG("avct_l2c snd Cfg Req");
207 AVCT_TRACE_DEBUG("avct_l2c_connect_cfm_cback conflict_lcid:0x%x", p_lcb->conflict_lcid);
208 if (p_lcb->conflict_lcid == lcid)
209 p_lcb->conflict_lcid = 0;
211 avct_lcb_event(p_lcb, AVCT_LCB_LL_CLOSE_EVT, (tAVCT_LCB_EVT *) &result);
214 else if (p_lcb->conflict_lcid == lcid)
216 /* we must be in AVCT_CH_CFG state for the ch_lcid channel */
217 AVCT_TRACE_DEBUG("avct_l2c_connect_cfm_cback ch_state: %d, conflict_lcid:0x%x", p_lcb->ch_state, p_lcb->conflict_lcid);
218 if (result == L2CAP_CONN_OK)
220 /* just in case the peer also accepts our connection - Send L2CAP disconnect req */
221 L2CA_DisconnectReq(lcid);
223 p_lcb->conflict_lcid = 0;
225 AVCT_TRACE_DEBUG("ch_state cnc: %d ", p_lcb->ch_state);
229 /*******************************************************************************
231 ** Function avct_l2c_config_cfm_cback
233 ** Description This is the L2CAP config confirm callback function.
238 *******************************************************************************/
239 void avct_l2c_config_cfm_cback(UINT16 lcid, tL2CAP_CFG_INFO *p_cfg)
243 /* look up lcb for this channel */
244 if ((p_lcb = avct_lcb_by_lcid(lcid)) != NULL)
246 AVCT_TRACE_DEBUG("avct_l2c_config_cfm_cback: 0x%x, ch_state: %d, res: %d",
247 lcid, p_lcb->ch_state, p_cfg->result);
248 /* if in correct state */
249 if (p_lcb->ch_state == AVCT_CH_CFG)
251 /* if result successful */
252 if (p_cfg->result == L2CAP_CFG_OK)
255 p_lcb->ch_flags |= AVCT_L2C_CFG_CFM_DONE;
257 /* if configuration complete */
258 if (p_lcb->ch_flags & AVCT_L2C_CFG_IND_DONE)
260 p_lcb->ch_state = AVCT_CH_OPEN;
261 avct_lcb_event(p_lcb, AVCT_LCB_LL_OPEN_EVT, NULL);
267 AVCT_TRACE_DEBUG("ERROR avct_l2c_config_cfm_cback L2CA_DisconnectReq %d ", p_lcb->ch_state);
268 /* store result value */
269 p_lcb->ch_result = p_cfg->result;
271 /* Send L2CAP disconnect req */
272 L2CA_DisconnectReq(lcid);
275 AVCT_TRACE_DEBUG("ch_state cfc: %d ", p_lcb->ch_state);
279 /*******************************************************************************
281 ** Function avct_l2c_config_ind_cback
283 ** Description This is the L2CAP config indication callback function.
288 *******************************************************************************/
289 void avct_l2c_config_ind_cback(UINT16 lcid, tL2CAP_CFG_INFO *p_cfg)
293 /* look up lcb for this channel */
294 if ((p_lcb = avct_lcb_by_lcid(lcid)) != NULL)
296 AVCT_TRACE_DEBUG("avct_l2c_config_ind_cback: 0x%x, ch_state: %d", lcid, p_lcb->ch_state);
297 /* store the mtu in tbl */
298 if (p_cfg->mtu_present)
300 p_lcb->peer_mtu = p_cfg->mtu;
304 p_lcb->peer_mtu = L2CAP_DEFAULT_MTU;
307 /* send L2CAP configure response */
308 memset(p_cfg, 0, sizeof(tL2CAP_CFG_INFO));
309 p_cfg->result = L2CAP_CFG_OK;
310 L2CA_ConfigRsp(lcid, p_cfg);
312 /* if first config ind */
313 if ((p_lcb->ch_flags & AVCT_L2C_CFG_IND_DONE) == 0)
316 p_lcb->ch_flags |= AVCT_L2C_CFG_IND_DONE;
318 /* if configuration complete */
319 if (p_lcb->ch_flags & AVCT_L2C_CFG_CFM_DONE)
321 p_lcb->ch_state = AVCT_CH_OPEN;
322 avct_lcb_event(p_lcb, AVCT_LCB_LL_OPEN_EVT, NULL);
325 AVCT_TRACE_DEBUG("ch_state cfi: %d ", p_lcb->ch_state);
329 /*******************************************************************************
331 ** Function avct_l2c_disconnect_ind_cback
333 ** Description This is the L2CAP disconnect indication callback function.
338 *******************************************************************************/
339 void avct_l2c_disconnect_ind_cback(UINT16 lcid, BOOLEAN ack_needed)
342 UINT16 result = AVCT_RESULT_FAIL;
344 /* look up lcb for this channel */
345 if ((p_lcb = avct_lcb_by_lcid(lcid)) != NULL)
347 AVCT_TRACE_DEBUG("avct_l2c_disconnect_ind_cback: 0x%x, ch_state: %d", lcid, p_lcb->ch_state);
350 /* send L2CAP disconnect response */
351 L2CA_DisconnectRsp(lcid);
354 avct_lcb_event(p_lcb, AVCT_LCB_LL_CLOSE_EVT, (tAVCT_LCB_EVT *) &result);
355 AVCT_TRACE_DEBUG("ch_state di: %d ", p_lcb->ch_state);
359 /*******************************************************************************
361 ** Function avct_l2c_disconnect_cfm_cback
363 ** Description This is the L2CAP disconnect confirm callback function.
368 *******************************************************************************/
369 void avct_l2c_disconnect_cfm_cback(UINT16 lcid, UINT16 result)
374 /* look up lcb for this channel */
375 if ((p_lcb = avct_lcb_by_lcid(lcid)) != NULL)
377 AVCT_TRACE_DEBUG("avct_l2c_disconnect_cfm_cback: 0x%x, ch_state: %d, res: %d",
378 lcid, p_lcb->ch_state, result);
379 /* result value may be previously stored */
380 res = (p_lcb->ch_result != 0) ? p_lcb->ch_result : result;
381 p_lcb->ch_result = 0;
383 avct_lcb_event(p_lcb, AVCT_LCB_LL_CLOSE_EVT, (tAVCT_LCB_EVT *) &res);
384 AVCT_TRACE_DEBUG("ch_state dc: %d ", p_lcb->ch_state);
388 /*******************************************************************************
390 ** Function avct_l2c_congestion_ind_cback
392 ** Description This is the L2CAP congestion indication callback function.
397 *******************************************************************************/
398 void avct_l2c_congestion_ind_cback(UINT16 lcid, BOOLEAN is_congested)
402 AVCT_TRACE_DEBUG("avct_l2c_congestion_ind_cback: 0x%x", lcid);
403 /* look up lcb for this channel */
404 if ((p_lcb = avct_lcb_by_lcid(lcid)) != NULL)
406 avct_lcb_event(p_lcb, AVCT_LCB_LL_CONG_EVT, (tAVCT_LCB_EVT *) &is_congested);
410 /*******************************************************************************
412 ** Function avct_l2c_data_ind_cback
414 ** Description This is the L2CAP data indication callback function.
419 *******************************************************************************/
420 void avct_l2c_data_ind_cback(UINT16 lcid, BT_HDR *p_buf)
424 AVCT_TRACE_DEBUG("avct_l2c_data_ind_cback: 0x%x", lcid);
425 /* look up lcb for this channel */
426 if ((p_lcb = avct_lcb_by_lcid(lcid)) != NULL)
428 avct_lcb_event(p_lcb, AVCT_LCB_LL_MSG_EVT, (tAVCT_LCB_EVT *) &p_buf);
430 else /* prevent buffer leak */
432 AVCT_TRACE_WARNING("ERROR -> avct_l2c_data_ind_cback drop buffer");