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