blob: 4ca28ebeec8b0b6c929012b15395d14568838309 [file] [log] [blame]
The Android Open Source Project5738f832012-12-12 16:00:35 -08001/******************************************************************************
2 *
3 * Copyright (C) 1999-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 file contains the main L2CAP entry points
22 *
23 ******************************************************************************/
24
25#include "bt_target.h"
26#include <stdlib.h>
27#include <string.h>
28#include <stdio.h>
29
Zach Johnson30e58062014-09-26 21:14:34 -070030#include "controller.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080031#include "gki.h"
32#include "hcimsgs.h"
33#include "l2cdefs.h"
34#include "l2c_int.h"
35#include "l2c_api.h"
36#include "btu.h"
37#include "btm_int.h"
38
39/********************************************************************************/
40/* L O C A L F U N C T I O N P R O T O T Y P E S */
41/********************************************************************************/
42static void process_l2cap_cmd (tL2C_LCB *p_lcb, UINT8 *p, UINT16 pkt_len);
43
44/********************************************************************************/
45/* G L O B A L L 2 C A P D A T A */
46/********************************************************************************/
47#if L2C_DYNAMIC_MEMORY == FALSE
48tL2C_CB l2cb;
49#endif
50
51/* Temporary - until l2cap implements group management */
52#if (TCS_BCST_SETUP_INCLUDED == TRUE && TCS_INCLUDED == TRUE)
53extern void tcs_proc_bcst_msg( BD_ADDR addr, BT_HDR *p_msg ) ;
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -080054#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -080055/*******************************************************************************
56**
57** Function l2c_bcst_msg
58**
59** Description
60**
61** Returns void
62**
63*******************************************************************************/
64void l2c_bcst_msg( BT_HDR *p_buf, UINT16 psm )
65{
66 UINT8 *p;
67
68 /* Ensure we have enough space in the buffer for the L2CAP and HCI headers */
69 if (p_buf->offset < L2CAP_BCST_MIN_OFFSET)
70 {
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -070071 L2CAP_TRACE_ERROR ("L2CAP - cannot send buffer, offset: %d", p_buf->offset);
The Android Open Source Project5738f832012-12-12 16:00:35 -080072 GKI_freebuf (p_buf);
73 return;
74 }
75
76 /* Step back some bytes to add the headers */
77 p_buf->offset -= (HCI_DATA_PREAMBLE_SIZE + L2CAP_PKT_OVERHEAD + L2CAP_BCST_OVERHEAD);
78 p_buf->len += L2CAP_PKT_OVERHEAD + L2CAP_BCST_OVERHEAD;
79
80 /* Set the pointer to the beginning of the data */
81 p = (UINT8 *)(p_buf + 1) + p_buf->offset;
82
83 /* First, the HCI transport header */
84 UINT16_TO_STREAM (p, 0x0050 | (L2CAP_PKT_START << 12) | (2 << 14));
85
Zach Johnson30e58062014-09-26 21:14:34 -070086 uint16_t acl_data_size = controller_get_interface()->get_acl_data_size_classic();
The Android Open Source Project5738f832012-12-12 16:00:35 -080087 /* The HCI transport will segment the buffers. */
Zach Johnson30e58062014-09-26 21:14:34 -070088 if (p_buf->len > acl_data_size)
The Android Open Source Project5738f832012-12-12 16:00:35 -080089 {
Zach Johnson30e58062014-09-26 21:14:34 -070090 UINT16_TO_STREAM (p, acl_data_size);
The Android Open Source Project5738f832012-12-12 16:00:35 -080091 }
92 else
93 {
94 UINT16_TO_STREAM (p, p_buf->len);
95 }
96
97 /* Now the L2CAP header */
98 UINT16_TO_STREAM (p, p_buf->len - L2CAP_PKT_OVERHEAD);
99 UINT16_TO_STREAM (p, L2CAP_CONNECTIONLESS_CID);
100 UINT16_TO_STREAM (p, psm);
101
102 p_buf->len += HCI_DATA_PREAMBLE_SIZE;
103
Zach Johnson30e58062014-09-26 21:14:34 -0700104 if (p_buf->len <= controller_get_interface()->get_acl_packet_size_classic())
The Android Open Source Project5738f832012-12-12 16:00:35 -0800105 {
Chris Mantonf857d642014-09-26 13:31:41 -0700106 bte_main_hci_send(p_buf, BT_EVT_TO_LM_HCI_ACL);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800107 }
108}
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800109
The Android Open Source Project5738f832012-12-12 16:00:35 -0800110
111/*******************************************************************************
112**
113** Function l2c_rcv_acl_data
114**
115** Description This function is called from the HCI Interface when an ACL
116** data packet is received.
117**
118** Returns void
119**
120*******************************************************************************/
121void l2c_rcv_acl_data (BT_HDR *p_msg)
122{
123 UINT8 *p = (UINT8 *)(p_msg + 1) + p_msg->offset;
124 UINT16 handle, hci_len;
125 UINT8 pkt_type;
126 tL2C_LCB *p_lcb;
127 tL2C_CCB *p_ccb = NULL;
128 UINT16 l2cap_len, rcv_cid, psm;
129
130 /* Extract the handle */
131 STREAM_TO_UINT16 (handle, p);
132 pkt_type = HCID_GET_EVENT (handle);
133 handle = HCID_GET_HANDLE (handle);
134
135 /* Since the HCI Transport is putting segmented packets back together, we */
136 /* should never get a valid packet with the type set to "continuation" */
137 if (pkt_type != L2CAP_PKT_CONTINUE)
138 {
139 /* Find the LCB based on the handle */
140 if ((p_lcb = l2cu_find_lcb_by_handle (handle)) == NULL)
141 {
142 UINT8 cmd_code;
143
144 /* There is a slight possibility (specifically with USB) that we get an */
145 /* L2CAP connection request before we get the HCI connection complete. */
146 /* So for these types of messages, hold them for up to 2 seconds. */
147 STREAM_TO_UINT16 (hci_len, p);
148 STREAM_TO_UINT16 (l2cap_len, p);
149 STREAM_TO_UINT16 (rcv_cid, p);
150 STREAM_TO_UINT8 (cmd_code, p);
151
152 if ((p_msg->layer_specific == 0) && (rcv_cid == L2CAP_SIGNALLING_CID)
153 && (cmd_code == L2CAP_CMD_INFO_REQ || cmd_code == L2CAP_CMD_CONN_REQ))
154 {
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700155 L2CAP_TRACE_WARNING ("L2CAP - holding ACL for unknown handle:%d ls:%d cid:%d opcode:%d cur count:%d",
The Android Open Source Project5738f832012-12-12 16:00:35 -0800156 handle, p_msg->layer_specific, rcv_cid, cmd_code,
Chris Mantonfe7216c2014-05-06 10:35:42 -0700157 GKI_queue_length(&l2cb.rcv_hold_q));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800158 p_msg->layer_specific = 2;
159 GKI_enqueue (&l2cb.rcv_hold_q, p_msg);
160
Chris Mantonfe7216c2014-05-06 10:35:42 -0700161 if (GKI_queue_length(&l2cb.rcv_hold_q) == 1)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800162 btu_start_timer (&l2cb.rcv_hold_tle, BTU_TTYPE_L2CAP_HOLD, BT_1SEC_TIMEOUT);
163
164 return;
165 }
166 else
167 {
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700168 L2CAP_TRACE_ERROR ("L2CAP - rcvd ACL for unknown handle:%d ls:%d cid:%d opcode:%d cur count:%d",
Chris Mantonfe7216c2014-05-06 10:35:42 -0700169 handle, p_msg->layer_specific, rcv_cid, cmd_code, GKI_queue_length(&l2cb.rcv_hold_q));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800170 }
171 GKI_freebuf (p_msg);
172 return;
173 }
174 }
175 else
176 {
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700177 L2CAP_TRACE_WARNING ("L2CAP - expected pkt start or complete, got: %d", pkt_type);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800178 GKI_freebuf (p_msg);
179 return;
180 }
181
182 /* Extract the length and update the buffer header */
183 STREAM_TO_UINT16 (hci_len, p);
184 p_msg->offset += 4;
185
The Android Open Source Project5738f832012-12-12 16:00:35 -0800186 /* Extract the length and CID */
187 STREAM_TO_UINT16 (l2cap_len, p);
188 STREAM_TO_UINT16 (rcv_cid, p);
189
190 /* Find the CCB for this CID */
191 if (rcv_cid >= L2CAP_BASE_APPL_CID)
192 {
193 if ((p_ccb = l2cu_find_ccb_by_cid (p_lcb, rcv_cid)) == NULL)
194 {
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700195 L2CAP_TRACE_WARNING ("L2CAP - unknown CID: 0x%04x", rcv_cid);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800196 GKI_freebuf (p_msg);
197 return;
198 }
199 }
200
201 if (hci_len >= L2CAP_PKT_OVERHEAD) /* Must receive at least the L2CAP length and CID.*/
202 {
203 p_msg->len = hci_len - L2CAP_PKT_OVERHEAD;
204 p_msg->offset += L2CAP_PKT_OVERHEAD;
205 }
206 else
207 {
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700208 L2CAP_TRACE_WARNING ("L2CAP - got incorrect hci header" );
The Android Open Source Project5738f832012-12-12 16:00:35 -0800209 GKI_freebuf (p_msg);
210 return;
211 }
212
213 if (l2cap_len != p_msg->len)
214 {
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700215 L2CAP_TRACE_WARNING ("L2CAP - bad length in pkt. Exp: %d Act: %d",
The Android Open Source Project5738f832012-12-12 16:00:35 -0800216 l2cap_len, p_msg->len);
217
218 GKI_freebuf (p_msg);
219 return;
220 }
221
222 /* Send the data through the channel state machine */
223 if (rcv_cid == L2CAP_SIGNALLING_CID)
224 {
225 process_l2cap_cmd (p_lcb, p, l2cap_len);
226 GKI_freebuf (p_msg);
227 }
228 else if (rcv_cid == L2CAP_CONNECTIONLESS_CID)
229 {
230 /* process_connectionless_data (p_lcb); */
231 STREAM_TO_UINT16 (psm, p);
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700232 L2CAP_TRACE_DEBUG( "GOT CONNECTIONLESS DATA PSM:%d", psm ) ;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800233#if (TCS_BCST_SETUP_INCLUDED == TRUE && TCS_INCLUDED == TRUE)
234 if (psm == TCS_PSM_INTERCOM || psm == TCS_PSM_CORDLESS)
235 {
236 p_msg->offset += L2CAP_BCST_OVERHEAD;
237 p_msg->len -= L2CAP_BCST_OVERHEAD;
238 tcs_proc_bcst_msg( p_lcb->remote_bd_addr, p_msg ) ;
239 GKI_freebuf (p_msg);
240 }
241 else
242#endif
243
244#if (L2CAP_UCD_INCLUDED == TRUE)
245 /* if it is not broadcast, check UCD registration */
246 if ( l2c_ucd_check_rx_pkts( p_lcb, p_msg ) )
247 {
248 /* nothing to do */
249 }
250 else
251#endif
252 GKI_freebuf (p_msg);
253 }
254#if (BLE_INCLUDED == TRUE)
255 else if (rcv_cid == L2CAP_BLE_SIGNALLING_CID)
256 {
257 l2cble_process_sig_cmd (p_lcb, p, l2cap_len);
258 GKI_freebuf (p_msg);
259 }
260#endif
261#if (L2CAP_NUM_FIXED_CHNLS > 0)
262 else if ((rcv_cid >= L2CAP_FIRST_FIXED_CHNL) && (rcv_cid <= L2CAP_LAST_FIXED_CHNL) &&
263 (l2cb.fixed_reg[rcv_cid - L2CAP_FIRST_FIXED_CHNL].pL2CA_FixedData_Cb != NULL) )
264 {
265 /* If no CCB for this channel, allocate one */
Chaojing Sunc5cda812014-11-07 16:42:46 -0800266 if (p_lcb &&
267 /* discard fixed channel data when link is disconnecting */
268 (p_lcb->link_state != LST_DISCONNECTING) &&
269 l2cu_initialize_fixed_ccb (p_lcb, rcv_cid,
Prerepa Viswanadham7ae25152014-09-10 17:08:11 -0700270 &l2cb.fixed_reg[rcv_cid - L2CAP_FIRST_FIXED_CHNL].fixed_chnl_opts))
The Android Open Source Project5738f832012-12-12 16:00:35 -0800271 {
Prerepa Viswanadham7ae25152014-09-10 17:08:11 -0700272#if(defined BLE_INCLUDED && (BLE_INCLUDED == TRUE))
273 l2cble_notify_le_connection(p_lcb->remote_bd_addr);
274#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -0800275 p_ccb = p_lcb->p_fixed_ccbs[rcv_cid - L2CAP_FIRST_FIXED_CHNL];
276
277 if (p_ccb->peer_cfg.fcr.mode != L2CAP_FCR_BASIC_MODE)
278 l2c_fcr_proc_pdu (p_ccb, p_msg);
279 else
280 (*l2cb.fixed_reg[rcv_cid - L2CAP_FIRST_FIXED_CHNL].pL2CA_FixedData_Cb)(p_lcb->remote_bd_addr, p_msg);
281 }
282 else
283 GKI_freebuf (p_msg);
284 }
285#endif
286
287 else
288 {
289 if (p_ccb == NULL)
290 GKI_freebuf (p_msg);
291 else
292 {
293 /* Basic mode packets go straight to the state machine */
294 if (p_ccb->peer_cfg.fcr.mode == L2CAP_FCR_BASIC_MODE)
295 l2c_csm_execute (p_ccb, L2CEVT_L2CAP_DATA, p_msg);
296 else
297 {
298 /* eRTM or streaming mode, so we need to validate states first */
299 if ((p_ccb->chnl_state == CST_OPEN) || (p_ccb->chnl_state == CST_CONFIG))
300 l2c_fcr_proc_pdu (p_ccb, p_msg);
301 else
302 GKI_freebuf (p_msg);
303 }
304 }
305 }
306}
307
308/*******************************************************************************
309**
310** Function process_l2cap_cmd
311**
312** Description This function is called when a packet is received on the
313** L2CAP signalling CID
314**
315** Returns void
316**
317*******************************************************************************/
318static void process_l2cap_cmd (tL2C_LCB *p_lcb, UINT8 *p, UINT16 pkt_len)
319{
320 UINT8 *p_pkt_end, *p_next_cmd, *p_cfg_end, *p_cfg_start;
321 UINT8 cmd_code, cfg_code, cfg_len, id;
322 tL2C_CONN_INFO con_info;
323 tL2CAP_CFG_INFO cfg_info;
324 UINT16 rej_reason, rej_mtu, lcid, rcid, info_type;
325 tL2C_CCB *p_ccb;
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700326 tL2C_RCB *p_rcb, *p_rcb2;
327 BOOLEAN cfg_rej, pkt_size_rej = FALSE;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800328 UINT16 cfg_rej_len, cmd_len;
329 UINT16 result;
330 tL2C_CONN_INFO ci;
331
332#if (defined BLE_INCLUDED && BLE_INCLUDED == TRUE)
333 /* if l2cap command received in CID 1 on top of an LE link, ignore this command */
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700334 if (p_lcb->transport == BT_TRANSPORT_LE)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800335 return;
336#endif
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700337
338 /* Reject the packet if it exceeds the default Signalling Channel MTU */
339 if (pkt_len > L2CAP_DEFAULT_MTU)
340 {
341 /* Core Spec requires a single response to the first command found in a multi-command
342 ** L2cap packet. If only responses in the packet, then it will be ignored.
343 ** Here we simply mark the bad packet and decide which cmd ID to reject later
344 */
345 pkt_size_rej = TRUE;
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700346 L2CAP_TRACE_ERROR ("L2CAP SIG MTU Pkt Len Exceeded (672) -> pkt_len: %d", pkt_len);
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700347 }
348
The Android Open Source Project5738f832012-12-12 16:00:35 -0800349 p_next_cmd = p;
350 p_pkt_end = p + pkt_len;
351
352 memset (&cfg_info, 0, sizeof(cfg_info));
353
354 /* An L2CAP packet may contain multiple commands */
355 while (TRUE)
356 {
357 /* Smallest command is 4 bytes */
358 if ((p = p_next_cmd) > (p_pkt_end - 4))
359 break;
360
361 STREAM_TO_UINT8 (cmd_code, p);
362 STREAM_TO_UINT8 (id, p);
363 STREAM_TO_UINT16 (cmd_len, p);
364
365 /* Check command length does not exceed packet length */
366 if ((p_next_cmd = p + cmd_len) > p_pkt_end)
367 {
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700368 L2CAP_TRACE_WARNING ("Command len bad pkt_len: %d cmd_len: %d code: %d",
The Android Open Source Project5738f832012-12-12 16:00:35 -0800369 pkt_len, cmd_len, cmd_code);
370 break;
371 }
372
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700373 L2CAP_TRACE_DEBUG ("cmd_code: %d, id:%d, cmd_len:%d", cmd_code, id, cmd_len);
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700374
375 /* Bad L2CAP packet length, look or cmd to reject */
376 if (pkt_size_rej)
377 {
378 /* If command found rejected it and we're done, otherwise keep looking */
379 if (l2c_is_cmd_rejected(cmd_code, id, p_lcb))
380 return;
381 else
382 continue; /* Look for next cmd/response in current packet */
383 }
384
The Android Open Source Project5738f832012-12-12 16:00:35 -0800385 switch (cmd_code)
386 {
387 case L2CAP_CMD_REJECT:
388 STREAM_TO_UINT16 (rej_reason, p);
389 if (rej_reason == L2CAP_CMD_REJ_MTU_EXCEEDED)
390 {
391 STREAM_TO_UINT16 (rej_mtu, p);
392 /* What to do with the MTU reject ? We have negotiated an MTU. For now */
393 /* we will ignore it and let a higher protocol timeout take care of it */
394
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700395 L2CAP_TRACE_WARNING ("L2CAP - MTU rej Handle: %d MTU: %d", p_lcb->handle, rej_mtu);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800396 }
397 if (rej_reason == L2CAP_CMD_REJ_INVALID_CID)
398 {
399 STREAM_TO_UINT16 (rcid, p);
400 STREAM_TO_UINT16 (lcid, p);
401
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700402 L2CAP_TRACE_WARNING ("L2CAP - rej with CID invalid, LCID: 0x%04x RCID: 0x%04x", lcid, rcid);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800403
404 /* Remote CID invalid. Treat as a disconnect */
405 if (((p_ccb = l2cu_find_ccb_by_cid (p_lcb, lcid)) != NULL)
406 && (p_ccb->remote_cid == rcid))
407 {
408 /* Fake link disconnect - no reply is generated */
409 l2c_csm_execute (p_ccb, L2CEVT_LP_DISCONNECT_IND, NULL);
410 }
411 }
412
413 /* SonyEricsson Info request Bug workaround (Continue connection) */
414 else if (rej_reason == L2CAP_CMD_REJ_NOT_UNDERSTOOD && p_lcb->w4_info_rsp)
415 {
416 btu_stop_timer (&p_lcb->info_timer_entry);
417
418 p_lcb->w4_info_rsp = FALSE;
419 ci.status = HCI_SUCCESS;
420 memcpy (ci.bd_addr, p_lcb->remote_bd_addr, sizeof(BD_ADDR));
421
422 /* For all channels, send the event through their FSMs */
423 for (p_ccb = p_lcb->ccb_queue.p_first_ccb; p_ccb; p_ccb = p_ccb->p_next_ccb)
424 {
425 l2c_csm_execute (p_ccb, L2CEVT_L2CAP_INFO_RSP, &ci);
426 }
427 }
428 break;
429
430 case L2CAP_CMD_CONN_REQ:
431 STREAM_TO_UINT16 (con_info.psm, p);
432 STREAM_TO_UINT16 (rcid, p);
433 if ((p_rcb = l2cu_find_rcb_by_psm (con_info.psm)) == NULL)
434 {
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700435 L2CAP_TRACE_WARNING ("L2CAP - rcvd conn req for unknown PSM: %d", con_info.psm);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800436 l2cu_reject_connection (p_lcb, rcid, id, L2CAP_CONN_NO_PSM);
437 break;
438 }
439 else
440 {
441 if (!p_rcb->api.pL2CA_ConnectInd_Cb)
442 {
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700443 L2CAP_TRACE_WARNING ("L2CAP - rcvd conn req for outgoing-only connection PSM: %d", con_info.psm);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800444 l2cu_reject_connection (p_lcb, rcid, id, L2CAP_CONN_NO_PSM);
445 break;
446 }
447 }
448 if ((p_ccb = l2cu_allocate_ccb (p_lcb, 0)) == NULL)
449 {
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700450 L2CAP_TRACE_ERROR ("L2CAP - unable to allocate CCB");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800451 l2cu_reject_connection (p_lcb, rcid, id, L2CAP_CONN_NO_RESOURCES);
452 break;
453 }
454 p_ccb->remote_id = id;
455 p_ccb->p_rcb = p_rcb;
456 p_ccb->remote_cid = rcid;
457
458 l2c_csm_execute(p_ccb, L2CEVT_L2CAP_CONNECT_REQ, &con_info);
459 break;
460
461 case L2CAP_CMD_CONN_RSP:
462 STREAM_TO_UINT16 (con_info.remote_cid, p);
463 STREAM_TO_UINT16 (lcid, p);
464 STREAM_TO_UINT16 (con_info.l2cap_result, p);
465 STREAM_TO_UINT16 (con_info.l2cap_status, p);
466
467 if ((p_ccb = l2cu_find_ccb_by_cid (p_lcb, lcid)) == NULL)
468 {
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700469 L2CAP_TRACE_WARNING ("L2CAP - no CCB for conn rsp, LCID: %d RCID: %d",
The Android Open Source Project5738f832012-12-12 16:00:35 -0800470 lcid, con_info.remote_cid);
471 break;
472 }
473 if (p_ccb->local_id != id)
474 {
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700475 L2CAP_TRACE_WARNING ("L2CAP - con rsp - bad ID. Exp: %d Got: %d",
The Android Open Source Project5738f832012-12-12 16:00:35 -0800476 p_ccb->local_id, id);
477 break;
478 }
479
480 if (con_info.l2cap_result == L2CAP_CONN_OK)
481 l2c_csm_execute(p_ccb, L2CEVT_L2CAP_CONNECT_RSP, &con_info);
482 else if (con_info.l2cap_result == L2CAP_CONN_PENDING)
483 l2c_csm_execute(p_ccb, L2CEVT_L2CAP_CONNECT_RSP_PND, &con_info);
484 else
485 l2c_csm_execute(p_ccb, L2CEVT_L2CAP_CONNECT_RSP_NEG, &con_info);
486
487 break;
488
489 case L2CAP_CMD_CONFIG_REQ:
490 p_cfg_end = p + cmd_len;
491 cfg_rej = FALSE;
492 cfg_rej_len = 0;
493
494 STREAM_TO_UINT16 (lcid, p);
495 STREAM_TO_UINT16 (cfg_info.flags, p);
496
497 p_cfg_start = p;
498
499 cfg_info.flush_to_present = cfg_info.mtu_present = cfg_info.qos_present =
500 cfg_info.fcr_present = cfg_info.fcs_present = FALSE;
501
502 while (p < p_cfg_end)
503 {
504 STREAM_TO_UINT8 (cfg_code, p);
505 STREAM_TO_UINT8 (cfg_len, p);
506
507 switch (cfg_code & 0x7F)
508 {
509 case L2CAP_CFG_TYPE_MTU:
510 cfg_info.mtu_present = TRUE;
511 STREAM_TO_UINT16 (cfg_info.mtu, p);
512 break;
513
514 case L2CAP_CFG_TYPE_FLUSH_TOUT:
515 cfg_info.flush_to_present = TRUE;
516 STREAM_TO_UINT16 (cfg_info.flush_to, p);
517 break;
518
519 case L2CAP_CFG_TYPE_QOS:
520 cfg_info.qos_present = TRUE;
521 STREAM_TO_UINT8 (cfg_info.qos.qos_flags, p);
522 STREAM_TO_UINT8 (cfg_info.qos.service_type, p);
523 STREAM_TO_UINT32 (cfg_info.qos.token_rate, p);
524 STREAM_TO_UINT32 (cfg_info.qos.token_bucket_size, p);
525 STREAM_TO_UINT32 (cfg_info.qos.peak_bandwidth, p);
526 STREAM_TO_UINT32 (cfg_info.qos.latency, p);
527 STREAM_TO_UINT32 (cfg_info.qos.delay_variation, p);
528 break;
529
530 case L2CAP_CFG_TYPE_FCR:
531 cfg_info.fcr_present = TRUE;
532 STREAM_TO_UINT8 (cfg_info.fcr.mode, p);
533 STREAM_TO_UINT8 (cfg_info.fcr.tx_win_sz, p);
534 STREAM_TO_UINT8 (cfg_info.fcr.max_transmit, p);
535 STREAM_TO_UINT16 (cfg_info.fcr.rtrans_tout, p);
536 STREAM_TO_UINT16 (cfg_info.fcr.mon_tout, p);
537 STREAM_TO_UINT16 (cfg_info.fcr.mps, p);
538 break;
539
540 case L2CAP_CFG_TYPE_FCS:
541 cfg_info.fcs_present = TRUE;
542 STREAM_TO_UINT8 (cfg_info.fcs, p);
543 break;
544
545 case L2CAP_CFG_TYPE_EXT_FLOW:
546 cfg_info.ext_flow_spec_present = TRUE;
547 STREAM_TO_UINT8 (cfg_info.ext_flow_spec.id, p);
548 STREAM_TO_UINT8 (cfg_info.ext_flow_spec.stype, p);
549 STREAM_TO_UINT16 (cfg_info.ext_flow_spec.max_sdu_size, p);
550 STREAM_TO_UINT32 (cfg_info.ext_flow_spec.sdu_inter_time, p);
551 STREAM_TO_UINT32 (cfg_info.ext_flow_spec.access_latency, p);
552 STREAM_TO_UINT32 (cfg_info.ext_flow_spec.flush_timeout, p);
553 break;
554
555 default:
556 /* sanity check option length */
557 if ((cfg_len + L2CAP_CFG_OPTION_OVERHEAD) <= cmd_len)
558 {
559 p += cfg_len;
560 if ((cfg_code & 0x80) == 0)
561 {
562 cfg_rej_len += cfg_len + L2CAP_CFG_OPTION_OVERHEAD;
563 cfg_rej = TRUE;
564 }
565 }
566 /* bad length; force loop exit */
567 else
568 {
569 p = p_cfg_end;
570 cfg_rej = TRUE;
571 }
572 break;
573 }
574 }
575
576 if ((p_ccb = l2cu_find_ccb_by_cid (p_lcb, lcid)) != NULL)
577 {
578 p_ccb->remote_id = id;
579 if (cfg_rej)
580 {
581 l2cu_send_peer_config_rej (p_ccb, p_cfg_start, (UINT16) (cmd_len - L2CAP_CONFIG_REQ_LEN), cfg_rej_len);
582 }
583 else
584 {
585 l2c_csm_execute (p_ccb, L2CEVT_L2CAP_CONFIG_REQ, &cfg_info);
586 }
587 }
588 else
589 {
590 /* updated spec says send command reject on invalid cid */
591 l2cu_send_peer_cmd_reject (p_lcb, L2CAP_CMD_REJ_INVALID_CID, id, 0, 0);
592 }
593 break;
594
595 case L2CAP_CMD_CONFIG_RSP:
596 p_cfg_end = p + cmd_len;
597 STREAM_TO_UINT16 (lcid, p);
598 STREAM_TO_UINT16 (cfg_info.flags, p);
599 STREAM_TO_UINT16 (cfg_info.result, p);
600
601 cfg_info.flush_to_present = cfg_info.mtu_present = cfg_info.qos_present =
602 cfg_info.fcr_present = cfg_info.fcs_present = FALSE;
603
604 while (p < p_cfg_end)
605 {
606 STREAM_TO_UINT8 (cfg_code, p);
607 STREAM_TO_UINT8 (cfg_len, p);
608
609 switch (cfg_code & 0x7F)
610 {
611 case L2CAP_CFG_TYPE_MTU:
612 cfg_info.mtu_present = TRUE;
613 STREAM_TO_UINT16 (cfg_info.mtu, p);
614 break;
615
616 case L2CAP_CFG_TYPE_FLUSH_TOUT:
617 cfg_info.flush_to_present = TRUE;
618 STREAM_TO_UINT16 (cfg_info.flush_to, p);
619 break;
620
621 case L2CAP_CFG_TYPE_QOS:
622 cfg_info.qos_present = TRUE;
623 STREAM_TO_UINT8 (cfg_info.qos.qos_flags, p);
624 STREAM_TO_UINT8 (cfg_info.qos.service_type, p);
625 STREAM_TO_UINT32 (cfg_info.qos.token_rate, p);
626 STREAM_TO_UINT32 (cfg_info.qos.token_bucket_size, p);
627 STREAM_TO_UINT32 (cfg_info.qos.peak_bandwidth, p);
628 STREAM_TO_UINT32 (cfg_info.qos.latency, p);
629 STREAM_TO_UINT32 (cfg_info.qos.delay_variation, p);
630 break;
631
632 case L2CAP_CFG_TYPE_FCR:
633 cfg_info.fcr_present = TRUE;
634 STREAM_TO_UINT8 (cfg_info.fcr.mode, p);
635 STREAM_TO_UINT8 (cfg_info.fcr.tx_win_sz, p);
636 STREAM_TO_UINT8 (cfg_info.fcr.max_transmit, p);
637 STREAM_TO_UINT16 (cfg_info.fcr.rtrans_tout, p);
638 STREAM_TO_UINT16 (cfg_info.fcr.mon_tout, p);
639 STREAM_TO_UINT16 (cfg_info.fcr.mps, p);
640 break;
641
642 case L2CAP_CFG_TYPE_FCS:
643 cfg_info.fcs_present = TRUE;
644 STREAM_TO_UINT8 (cfg_info.fcs, p);
645 break;
646
647 case L2CAP_CFG_TYPE_EXT_FLOW:
648 cfg_info.ext_flow_spec_present = TRUE;
649 STREAM_TO_UINT8 (cfg_info.ext_flow_spec.id, p);
650 STREAM_TO_UINT8 (cfg_info.ext_flow_spec.stype, p);
651 STREAM_TO_UINT16 (cfg_info.ext_flow_spec.max_sdu_size, p);
652 STREAM_TO_UINT32 (cfg_info.ext_flow_spec.sdu_inter_time, p);
653 STREAM_TO_UINT32 (cfg_info.ext_flow_spec.access_latency, p);
654 STREAM_TO_UINT32 (cfg_info.ext_flow_spec.flush_timeout, p);
655 break;
656 }
657 }
658
659 if ((p_ccb = l2cu_find_ccb_by_cid (p_lcb, lcid)) != NULL)
660 {
661 if (p_ccb->local_id != id)
662 {
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700663 L2CAP_TRACE_WARNING ("L2CAP - cfg rsp - bad ID. Exp: %d Got: %d",
The Android Open Source Project5738f832012-12-12 16:00:35 -0800664 p_ccb->local_id, id);
665 break;
666 }
667 if ( (cfg_info.result == L2CAP_CFG_OK) || (cfg_info.result == L2CAP_CFG_PENDING) )
668 l2c_csm_execute (p_ccb, L2CEVT_L2CAP_CONFIG_RSP, &cfg_info);
669 else
670 l2c_csm_execute (p_ccb, L2CEVT_L2CAP_CONFIG_RSP_NEG, &cfg_info);
671 }
672 else
673 {
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700674 L2CAP_TRACE_WARNING ("L2CAP - rcvd cfg rsp for unknown CID: 0x%04x", lcid);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800675 }
676 break;
677
678
679 case L2CAP_CMD_DISC_REQ:
680 STREAM_TO_UINT16 (lcid, p);
681 STREAM_TO_UINT16 (rcid, p);
682
683 if ((p_ccb = l2cu_find_ccb_by_cid (p_lcb, lcid)) != NULL)
684 {
685 if (p_ccb->remote_cid == rcid)
686 {
687 p_ccb->remote_id = id;
688 l2c_csm_execute (p_ccb, L2CEVT_L2CAP_DISCONNECT_REQ, &con_info);
689 }
690 }
691 else
692 l2cu_send_peer_disc_rsp (p_lcb, id, lcid, rcid);
693
694 break;
695
696 case L2CAP_CMD_DISC_RSP:
697 STREAM_TO_UINT16 (rcid, p);
698 STREAM_TO_UINT16 (lcid, p);
699
700 if ((p_ccb = l2cu_find_ccb_by_cid (p_lcb, lcid)) != NULL)
701 {
702 if ((p_ccb->remote_cid == rcid) && (p_ccb->local_id == id))
703 {
704 l2c_csm_execute (p_ccb, L2CEVT_L2CAP_DISCONNECT_RSP, &con_info);
705 }
706 }
707 break;
708
709 case L2CAP_CMD_ECHO_REQ:
The Android Open Source Project5738f832012-12-12 16:00:35 -0800710 l2cu_send_peer_echo_rsp (p_lcb, id, NULL, 0);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800711 break;
712
713 case L2CAP_CMD_ECHO_RSP:
The Android Open Source Project5738f832012-12-12 16:00:35 -0800714 if (p_lcb->p_echo_rsp_cb)
715 {
716 tL2CA_ECHO_RSP_CB *p_cb = p_lcb->p_echo_rsp_cb;
717
718 /* Zero out the callback in case app immediately calls us again */
719 p_lcb->p_echo_rsp_cb = NULL;
720
721 (*p_cb) (L2CAP_PING_RESULT_OK);
722 }
723 break;
724
725 case L2CAP_CMD_INFO_REQ:
726 STREAM_TO_UINT16 (info_type, p);
727 l2cu_send_peer_info_rsp (p_lcb, id, info_type);
728 break;
729
730 case L2CAP_CMD_INFO_RSP:
731 /* Stop the link connect timer if sent before L2CAP connection is up */
732 if (p_lcb->w4_info_rsp)
733 {
734 btu_stop_timer (&p_lcb->info_timer_entry);
735 p_lcb->w4_info_rsp = FALSE;
736 }
737
738 STREAM_TO_UINT16 (info_type, p);
739 STREAM_TO_UINT16 (result, p);
740
741 p_lcb->info_rx_bits |= (1 << info_type);
742
743 if ( (info_type == L2CAP_EXTENDED_FEATURES_INFO_TYPE)
744 && (result == L2CAP_INFO_RESP_RESULT_SUCCESS) )
745 {
746 STREAM_TO_UINT32( p_lcb->peer_ext_fea, p );
747
748#if (L2CAP_NUM_FIXED_CHNLS > 0)
749 if (p_lcb->peer_ext_fea & L2CAP_EXTFEA_FIXED_CHNLS)
750 {
751 l2cu_send_peer_info_req (p_lcb, L2CAP_FIXED_CHANNELS_INFO_TYPE);
752 break;
753 }
754 else
755 {
756 l2cu_process_fixed_chnl_resp (p_lcb);
757 }
758#endif
759 }
760
761
762#if (L2CAP_NUM_FIXED_CHNLS > 0)
763 if (info_type == L2CAP_FIXED_CHANNELS_INFO_TYPE)
764 {
765 if (result == L2CAP_INFO_RESP_RESULT_SUCCESS)
766 {
767 memcpy (p_lcb->peer_chnl_mask, p, L2CAP_FIXED_CHNL_ARRAY_SIZE);
768 }
769
770 l2cu_process_fixed_chnl_resp (p_lcb);
771 }
772#endif
773#if (L2CAP_UCD_INCLUDED == TRUE)
774 else if (info_type == L2CAP_CONNLESS_MTU_INFO_TYPE)
775 {
776 if (result == L2CAP_INFO_RESP_RESULT_SUCCESS)
777 {
778 STREAM_TO_UINT16 (p_lcb->ucd_mtu, p);
779 }
780 }
781#endif
782
783 ci.status = HCI_SUCCESS;
784 memcpy (ci.bd_addr, p_lcb->remote_bd_addr, sizeof(BD_ADDR));
785 for (p_ccb = p_lcb->ccb_queue.p_first_ccb; p_ccb; p_ccb = p_ccb->p_next_ccb)
786 {
787 l2c_csm_execute (p_ccb, L2CEVT_L2CAP_INFO_RSP, &ci);
788 }
789 break;
790
791 default:
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700792 L2CAP_TRACE_WARNING ("L2CAP - bad cmd code: %d", cmd_code);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800793 l2cu_send_peer_cmd_reject (p_lcb, L2CAP_CMD_REJ_NOT_UNDERSTOOD, id, 0, 0);
794 return;
795 }
796 }
797}
798
799/*******************************************************************************
800**
801** Function l2c_process_held_packets
802**
803** Description This function processes any L2CAP packets that arrived before
804** the HCI connection complete arrived. It is a work around for
805** badly behaved controllers.
806**
807** Returns void
808**
809*******************************************************************************/
810void l2c_process_held_packets (BOOLEAN timed_out)
811{
812 BT_HDR *p_buf, *p_buf1;
813 BUFFER_Q *p_rcv_hold_q = &l2cb.rcv_hold_q;
814
Chris Mantonfe7216c2014-05-06 10:35:42 -0700815 if (GKI_queue_is_empty(p_rcv_hold_q))
The Android Open Source Project5738f832012-12-12 16:00:35 -0800816 return;
817
818 if (!timed_out)
819 {
820 btu_stop_timer(&l2cb.rcv_hold_tle);
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700821 L2CAP_TRACE_WARNING("L2CAP HOLD CONTINUE");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800822 }
823 else
824 {
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700825 L2CAP_TRACE_WARNING("L2CAP HOLD TIMEOUT");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800826 }
827
828 /* Update the timeouts in the hold queue */
829 for (p_buf = (BT_HDR *)GKI_getfirst (p_rcv_hold_q); p_buf; p_buf = p_buf1)
830 {
831 p_buf1 = (BT_HDR *)GKI_getnext (p_buf);
832 if (!timed_out || (!p_buf->layer_specific) || (--p_buf->layer_specific == 0))
833 {
834 GKI_remove_from_queue (p_rcv_hold_q, p_buf);
835 p_buf->layer_specific = 0xFFFF;
836 l2c_rcv_acl_data (p_buf);
837 }
838 }
839
840 /* If anyone still in the queue, restart the timeout */
Chris Mantonfe7216c2014-05-06 10:35:42 -0700841 if (!GKI_queue_is_empty(p_rcv_hold_q))
The Android Open Source Project5738f832012-12-12 16:00:35 -0800842 btu_start_timer (&l2cb.rcv_hold_tle, BTU_TTYPE_L2CAP_HOLD, BT_1SEC_TIMEOUT);
843}
844
845
846/*******************************************************************************
847**
848** Function l2c_init
849**
850** Description This function is called once at startup to initialize
851** all the L2CAP structures
852**
853** Returns void
854**
855*******************************************************************************/
856void l2c_init (void)
857{
858 INT16 xx;
859
860 memset (&l2cb, 0, sizeof (tL2C_CB));
861 /* the psm is increased by 2 before being used */
862 l2cb.dyn_psm = 0xFFF;
863
864 /* Put all the channel control blocks on the free queue */
865 for (xx = 0; xx < MAX_L2CAP_CHANNELS - 1; xx++)
866 {
867 l2cb.ccb_pool[xx].p_next_ccb = &l2cb.ccb_pool[xx + 1];
868 }
869
870#if (L2CAP_NON_FLUSHABLE_PB_INCLUDED == TRUE)
871 /* it will be set to L2CAP_PKT_START_NON_FLUSHABLE if controller supports */
872 l2cb.non_flushable_pbf = L2CAP_PKT_START << L2CAP_PKT_TYPE_SHIFT;
873#endif
874
875
876 l2cb.p_free_ccb_first = &l2cb.ccb_pool[0];
877 l2cb.p_free_ccb_last = &l2cb.ccb_pool[MAX_L2CAP_CHANNELS - 1];
878
879#ifdef L2CAP_DESIRED_LINK_ROLE
880 l2cb.desire_role = L2CAP_DESIRED_LINK_ROLE;
881#else
882 l2cb.desire_role = HCI_ROLE_SLAVE;
883#endif
884
885 /* Set the default idle timeout */
886 l2cb.idle_timeout = L2CAP_LINK_INACTIVITY_TOUT;
887
888#if defined(L2CAP_INITIAL_TRACE_LEVEL)
889 l2cb.l2cap_trace_level = L2CAP_INITIAL_TRACE_LEVEL;
890#else
891 l2cb.l2cap_trace_level = BT_TRACE_LEVEL_NONE; /* No traces */
892#endif
893
894#if L2CAP_CONFORMANCE_TESTING == TRUE
895 /* Conformance testing needs a dynamic response */
896 l2cb.test_info_resp = L2CAP_EXTFEA_SUPPORTED_MASK;
897#endif
898
899 /* Number of ACL buffers to use for high priority channel */
900#if (defined(L2CAP_HIGH_PRI_CHAN_QUOTA_IS_CONFIGURABLE) && (L2CAP_HIGH_PRI_CHAN_QUOTA_IS_CONFIGURABLE == TRUE))
901 l2cb.high_pri_min_xmit_quota = L2CAP_HIGH_PRI_MIN_XMIT_QUOTA;
902#endif
903
904}
905
906/*******************************************************************************
907**
908** Function l2c_process_timeout
909**
910** Description This function is called when an L2CAP-related timeout occurs
911**
912** Returns void
913**
914*******************************************************************************/
915void l2c_process_timeout (TIMER_LIST_ENT *p_tle)
916{
917 /* What type of timeout ? */
918 switch (p_tle->event)
919 {
920 case BTU_TTYPE_L2CAP_LINK:
921 l2c_link_timeout ((tL2C_LCB *)p_tle->param);
922 break;
923
924 case BTU_TTYPE_L2CAP_CHNL:
925 l2c_csm_execute (((tL2C_CCB *)p_tle->param), L2CEVT_TIMEOUT, NULL);
926 break;
927
928 case BTU_TTYPE_L2CAP_FCR_ACK:
929 l2c_csm_execute (((tL2C_CCB *)p_tle->param), L2CEVT_ACK_TIMEOUT, NULL);
930 break;
931
932 case BTU_TTYPE_L2CAP_HOLD:
933 /* Update the timeouts in the hold queue */
934 l2c_process_held_packets(TRUE);
935 break;
936
937 case BTU_TTYPE_L2CAP_INFO:
938 l2c_info_timeout((tL2C_LCB *)p_tle->param);
939 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800940 }
941}
942
943/*******************************************************************************
944**
945** Function l2c_data_write
946**
947** Description API functions call this function to write data.
948**
949** Returns L2CAP_DW_SUCCESS, if data accepted, else FALSE
950** L2CAP_DW_CONGESTED, if data accepted and the channel is congested
951** L2CAP_DW_FAILED, if error
952**
953*******************************************************************************/
954UINT8 l2c_data_write (UINT16 cid, BT_HDR *p_data, UINT16 flags)
955{
956 tL2C_CCB *p_ccb;
957
958 /* Find the channel control block. We don't know the link it is on. */
959 if ((p_ccb = l2cu_find_ccb_by_cid (NULL, cid)) == NULL)
960 {
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700961 L2CAP_TRACE_WARNING ("L2CAP - no CCB for L2CA_DataWrite, CID: %d", cid);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800962 GKI_freebuf (p_data);
963 return (L2CAP_DW_FAILED);
964 }
965
966#ifndef TESTER /* Tester may send any amount of data. otherwise sending message
967 bigger than mtu size of peer is a violation of protocol */
968 if (p_data->len > p_ccb->peer_cfg.mtu)
969 {
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700970 L2CAP_TRACE_WARNING ("L2CAP - CID: 0x%04x cannot send message bigger than peer's mtu size", cid);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800971 GKI_freebuf (p_data);
972 return (L2CAP_DW_FAILED);
973 }
974#endif
975
976 /* channel based, packet based flushable or non-flushable */
977 p_data->layer_specific = flags;
978
979 /* If already congested, do not accept any more packets */
980 if (p_ccb->cong_sent)
981 {
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700982 L2CAP_TRACE_ERROR ("L2CAP - CID: 0x%04x cannot send, already congested xmit_hold_q.count: %u buff_quota: %u",
Chris Mantonfe7216c2014-05-06 10:35:42 -0700983 p_ccb->local_cid, GKI_queue_length(&p_ccb->xmit_hold_q), p_ccb->buff_quota);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800984
985 GKI_freebuf (p_data);
986 return (L2CAP_DW_FAILED);
987 }
988
989 l2c_csm_execute (p_ccb, L2CEVT_L2CA_DATA_WRITE, p_data);
990
991 if (p_ccb->cong_sent)
992 return (L2CAP_DW_CONGESTED);
993
994 return (L2CAP_DW_SUCCESS);
995}
996