blob: caef05f2efe6eb0992e14a18ffb4bdd0916eaa15 [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
The Android Open Source Project5738f832012-12-12 16:00:35 -080051/*******************************************************************************
52**
53** Function l2c_bcst_msg
54**
55** Description
56**
57** Returns void
58**
59*******************************************************************************/
60void l2c_bcst_msg( BT_HDR *p_buf, UINT16 psm )
61{
62 UINT8 *p;
63
64 /* Ensure we have enough space in the buffer for the L2CAP and HCI headers */
65 if (p_buf->offset < L2CAP_BCST_MIN_OFFSET)
66 {
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -070067 L2CAP_TRACE_ERROR ("L2CAP - cannot send buffer, offset: %d", p_buf->offset);
The Android Open Source Project5738f832012-12-12 16:00:35 -080068 GKI_freebuf (p_buf);
69 return;
70 }
71
72 /* Step back some bytes to add the headers */
73 p_buf->offset -= (HCI_DATA_PREAMBLE_SIZE + L2CAP_PKT_OVERHEAD + L2CAP_BCST_OVERHEAD);
74 p_buf->len += L2CAP_PKT_OVERHEAD + L2CAP_BCST_OVERHEAD;
75
76 /* Set the pointer to the beginning of the data */
77 p = (UINT8 *)(p_buf + 1) + p_buf->offset;
78
79 /* First, the HCI transport header */
80 UINT16_TO_STREAM (p, 0x0050 | (L2CAP_PKT_START << 12) | (2 << 14));
81
Zach Johnson30e58062014-09-26 21:14:34 -070082 uint16_t acl_data_size = controller_get_interface()->get_acl_data_size_classic();
The Android Open Source Project5738f832012-12-12 16:00:35 -080083 /* The HCI transport will segment the buffers. */
Zach Johnson30e58062014-09-26 21:14:34 -070084 if (p_buf->len > acl_data_size)
The Android Open Source Project5738f832012-12-12 16:00:35 -080085 {
Zach Johnson30e58062014-09-26 21:14:34 -070086 UINT16_TO_STREAM (p, acl_data_size);
The Android Open Source Project5738f832012-12-12 16:00:35 -080087 }
88 else
89 {
90 UINT16_TO_STREAM (p, p_buf->len);
91 }
92
93 /* Now the L2CAP header */
94 UINT16_TO_STREAM (p, p_buf->len - L2CAP_PKT_OVERHEAD);
95 UINT16_TO_STREAM (p, L2CAP_CONNECTIONLESS_CID);
96 UINT16_TO_STREAM (p, psm);
97
98 p_buf->len += HCI_DATA_PREAMBLE_SIZE;
99
Zach Johnson30e58062014-09-26 21:14:34 -0700100 if (p_buf->len <= controller_get_interface()->get_acl_packet_size_classic())
The Android Open Source Project5738f832012-12-12 16:00:35 -0800101 {
Chris Mantonf857d642014-09-26 13:31:41 -0700102 bte_main_hci_send(p_buf, BT_EVT_TO_LM_HCI_ACL);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800103 }
104}
Ganesh Ganapathi Battaead3cde2013-02-05 15:22:31 -0800105
The Android Open Source Project5738f832012-12-12 16:00:35 -0800106
107/*******************************************************************************
108**
109** Function l2c_rcv_acl_data
110**
111** Description This function is called from the HCI Interface when an ACL
112** data packet is received.
113**
114** Returns void
115**
116*******************************************************************************/
117void l2c_rcv_acl_data (BT_HDR *p_msg)
118{
119 UINT8 *p = (UINT8 *)(p_msg + 1) + p_msg->offset;
120 UINT16 handle, hci_len;
121 UINT8 pkt_type;
122 tL2C_LCB *p_lcb;
123 tL2C_CCB *p_ccb = NULL;
124 UINT16 l2cap_len, rcv_cid, psm;
125
126 /* Extract the handle */
127 STREAM_TO_UINT16 (handle, p);
128 pkt_type = HCID_GET_EVENT (handle);
129 handle = HCID_GET_HANDLE (handle);
130
131 /* Since the HCI Transport is putting segmented packets back together, we */
132 /* should never get a valid packet with the type set to "continuation" */
133 if (pkt_type != L2CAP_PKT_CONTINUE)
134 {
135 /* Find the LCB based on the handle */
136 if ((p_lcb = l2cu_find_lcb_by_handle (handle)) == NULL)
137 {
138 UINT8 cmd_code;
139
140 /* There is a slight possibility (specifically with USB) that we get an */
141 /* L2CAP connection request before we get the HCI connection complete. */
142 /* So for these types of messages, hold them for up to 2 seconds. */
143 STREAM_TO_UINT16 (hci_len, p);
144 STREAM_TO_UINT16 (l2cap_len, p);
145 STREAM_TO_UINT16 (rcv_cid, p);
146 STREAM_TO_UINT8 (cmd_code, p);
147
148 if ((p_msg->layer_specific == 0) && (rcv_cid == L2CAP_SIGNALLING_CID)
149 && (cmd_code == L2CAP_CMD_INFO_REQ || cmd_code == L2CAP_CMD_CONN_REQ))
150 {
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700151 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 -0800152 handle, p_msg->layer_specific, rcv_cid, cmd_code,
Chris Mantonfe7216c2014-05-06 10:35:42 -0700153 GKI_queue_length(&l2cb.rcv_hold_q));
The Android Open Source Project5738f832012-12-12 16:00:35 -0800154 p_msg->layer_specific = 2;
155 GKI_enqueue (&l2cb.rcv_hold_q, p_msg);
156
Chris Mantonfe7216c2014-05-06 10:35:42 -0700157 if (GKI_queue_length(&l2cb.rcv_hold_q) == 1)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800158 btu_start_timer (&l2cb.rcv_hold_tle, BTU_TTYPE_L2CAP_HOLD, BT_1SEC_TIMEOUT);
159
160 return;
161 }
162 else
163 {
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700164 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 -0700165 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 -0800166 }
167 GKI_freebuf (p_msg);
168 return;
169 }
170 }
171 else
172 {
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700173 L2CAP_TRACE_WARNING ("L2CAP - expected pkt start or complete, got: %d", pkt_type);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800174 GKI_freebuf (p_msg);
175 return;
176 }
177
178 /* Extract the length and update the buffer header */
179 STREAM_TO_UINT16 (hci_len, p);
180 p_msg->offset += 4;
181
The Android Open Source Project5738f832012-12-12 16:00:35 -0800182 /* Extract the length and CID */
183 STREAM_TO_UINT16 (l2cap_len, p);
184 STREAM_TO_UINT16 (rcv_cid, p);
185
186 /* Find the CCB for this CID */
187 if (rcv_cid >= L2CAP_BASE_APPL_CID)
188 {
189 if ((p_ccb = l2cu_find_ccb_by_cid (p_lcb, rcv_cid)) == NULL)
190 {
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700191 L2CAP_TRACE_WARNING ("L2CAP - unknown CID: 0x%04x", rcv_cid);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800192 GKI_freebuf (p_msg);
193 return;
194 }
195 }
196
197 if (hci_len >= L2CAP_PKT_OVERHEAD) /* Must receive at least the L2CAP length and CID.*/
198 {
199 p_msg->len = hci_len - L2CAP_PKT_OVERHEAD;
200 p_msg->offset += L2CAP_PKT_OVERHEAD;
201 }
202 else
203 {
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700204 L2CAP_TRACE_WARNING ("L2CAP - got incorrect hci header" );
The Android Open Source Project5738f832012-12-12 16:00:35 -0800205 GKI_freebuf (p_msg);
206 return;
207 }
208
209 if (l2cap_len != p_msg->len)
210 {
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700211 L2CAP_TRACE_WARNING ("L2CAP - bad length in pkt. Exp: %d Act: %d",
The Android Open Source Project5738f832012-12-12 16:00:35 -0800212 l2cap_len, p_msg->len);
213
214 GKI_freebuf (p_msg);
215 return;
216 }
217
218 /* Send the data through the channel state machine */
219 if (rcv_cid == L2CAP_SIGNALLING_CID)
220 {
221 process_l2cap_cmd (p_lcb, p, l2cap_len);
222 GKI_freebuf (p_msg);
223 }
224 else if (rcv_cid == L2CAP_CONNECTIONLESS_CID)
225 {
226 /* process_connectionless_data (p_lcb); */
227 STREAM_TO_UINT16 (psm, p);
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700228 L2CAP_TRACE_DEBUG( "GOT CONNECTIONLESS DATA PSM:%d", psm ) ;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800229
230#if (L2CAP_UCD_INCLUDED == TRUE)
231 /* if it is not broadcast, check UCD registration */
232 if ( l2c_ucd_check_rx_pkts( p_lcb, p_msg ) )
233 {
234 /* nothing to do */
235 }
236 else
237#endif
238 GKI_freebuf (p_msg);
239 }
240#if (BLE_INCLUDED == TRUE)
241 else if (rcv_cid == L2CAP_BLE_SIGNALLING_CID)
242 {
243 l2cble_process_sig_cmd (p_lcb, p, l2cap_len);
244 GKI_freebuf (p_msg);
245 }
246#endif
247#if (L2CAP_NUM_FIXED_CHNLS > 0)
248 else if ((rcv_cid >= L2CAP_FIRST_FIXED_CHNL) && (rcv_cid <= L2CAP_LAST_FIXED_CHNL) &&
249 (l2cb.fixed_reg[rcv_cid - L2CAP_FIRST_FIXED_CHNL].pL2CA_FixedData_Cb != NULL) )
250 {
251 /* If no CCB for this channel, allocate one */
Chaojing Sunc5cda812014-11-07 16:42:46 -0800252 if (p_lcb &&
253 /* discard fixed channel data when link is disconnecting */
254 (p_lcb->link_state != LST_DISCONNECTING) &&
255 l2cu_initialize_fixed_ccb (p_lcb, rcv_cid,
Prerepa Viswanadham7ae25152014-09-10 17:08:11 -0700256 &l2cb.fixed_reg[rcv_cid - L2CAP_FIRST_FIXED_CHNL].fixed_chnl_opts))
The Android Open Source Project5738f832012-12-12 16:00:35 -0800257 {
Prerepa Viswanadham7ae25152014-09-10 17:08:11 -0700258#if(defined BLE_INCLUDED && (BLE_INCLUDED == TRUE))
259 l2cble_notify_le_connection(p_lcb->remote_bd_addr);
260#endif
The Android Open Source Project5738f832012-12-12 16:00:35 -0800261 p_ccb = p_lcb->p_fixed_ccbs[rcv_cid - L2CAP_FIRST_FIXED_CHNL];
262
263 if (p_ccb->peer_cfg.fcr.mode != L2CAP_FCR_BASIC_MODE)
264 l2c_fcr_proc_pdu (p_ccb, p_msg);
265 else
266 (*l2cb.fixed_reg[rcv_cid - L2CAP_FIRST_FIXED_CHNL].pL2CA_FixedData_Cb)(p_lcb->remote_bd_addr, p_msg);
267 }
268 else
269 GKI_freebuf (p_msg);
270 }
271#endif
272
273 else
274 {
275 if (p_ccb == NULL)
276 GKI_freebuf (p_msg);
277 else
278 {
279 /* Basic mode packets go straight to the state machine */
280 if (p_ccb->peer_cfg.fcr.mode == L2CAP_FCR_BASIC_MODE)
281 l2c_csm_execute (p_ccb, L2CEVT_L2CAP_DATA, p_msg);
282 else
283 {
284 /* eRTM or streaming mode, so we need to validate states first */
285 if ((p_ccb->chnl_state == CST_OPEN) || (p_ccb->chnl_state == CST_CONFIG))
286 l2c_fcr_proc_pdu (p_ccb, p_msg);
287 else
288 GKI_freebuf (p_msg);
289 }
290 }
291 }
292}
293
294/*******************************************************************************
295**
296** Function process_l2cap_cmd
297**
298** Description This function is called when a packet is received on the
299** L2CAP signalling CID
300**
301** Returns void
302**
303*******************************************************************************/
304static void process_l2cap_cmd (tL2C_LCB *p_lcb, UINT8 *p, UINT16 pkt_len)
305{
306 UINT8 *p_pkt_end, *p_next_cmd, *p_cfg_end, *p_cfg_start;
307 UINT8 cmd_code, cfg_code, cfg_len, id;
308 tL2C_CONN_INFO con_info;
309 tL2CAP_CFG_INFO cfg_info;
310 UINT16 rej_reason, rej_mtu, lcid, rcid, info_type;
311 tL2C_CCB *p_ccb;
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700312 tL2C_RCB *p_rcb, *p_rcb2;
313 BOOLEAN cfg_rej, pkt_size_rej = FALSE;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800314 UINT16 cfg_rej_len, cmd_len;
315 UINT16 result;
316 tL2C_CONN_INFO ci;
317
318#if (defined BLE_INCLUDED && BLE_INCLUDED == TRUE)
319 /* if l2cap command received in CID 1 on top of an LE link, ignore this command */
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700320 if (p_lcb->transport == BT_TRANSPORT_LE)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800321 return;
322#endif
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700323
324 /* Reject the packet if it exceeds the default Signalling Channel MTU */
325 if (pkt_len > L2CAP_DEFAULT_MTU)
326 {
327 /* Core Spec requires a single response to the first command found in a multi-command
328 ** L2cap packet. If only responses in the packet, then it will be ignored.
329 ** Here we simply mark the bad packet and decide which cmd ID to reject later
330 */
331 pkt_size_rej = TRUE;
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700332 L2CAP_TRACE_ERROR ("L2CAP SIG MTU Pkt Len Exceeded (672) -> pkt_len: %d", pkt_len);
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700333 }
334
The Android Open Source Project5738f832012-12-12 16:00:35 -0800335 p_next_cmd = p;
336 p_pkt_end = p + pkt_len;
337
338 memset (&cfg_info, 0, sizeof(cfg_info));
339
340 /* An L2CAP packet may contain multiple commands */
341 while (TRUE)
342 {
343 /* Smallest command is 4 bytes */
344 if ((p = p_next_cmd) > (p_pkt_end - 4))
345 break;
346
347 STREAM_TO_UINT8 (cmd_code, p);
348 STREAM_TO_UINT8 (id, p);
349 STREAM_TO_UINT16 (cmd_len, p);
350
351 /* Check command length does not exceed packet length */
352 if ((p_next_cmd = p + cmd_len) > p_pkt_end)
353 {
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700354 L2CAP_TRACE_WARNING ("Command len bad pkt_len: %d cmd_len: %d code: %d",
The Android Open Source Project5738f832012-12-12 16:00:35 -0800355 pkt_len, cmd_len, cmd_code);
356 break;
357 }
358
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700359 L2CAP_TRACE_DEBUG ("cmd_code: %d, id:%d, cmd_len:%d", cmd_code, id, cmd_len);
Ganesh Ganapathi Batta8fe58872014-04-16 16:50:09 -0700360
361 /* Bad L2CAP packet length, look or cmd to reject */
362 if (pkt_size_rej)
363 {
364 /* If command found rejected it and we're done, otherwise keep looking */
365 if (l2c_is_cmd_rejected(cmd_code, id, p_lcb))
366 return;
367 else
368 continue; /* Look for next cmd/response in current packet */
369 }
370
The Android Open Source Project5738f832012-12-12 16:00:35 -0800371 switch (cmd_code)
372 {
373 case L2CAP_CMD_REJECT:
374 STREAM_TO_UINT16 (rej_reason, p);
375 if (rej_reason == L2CAP_CMD_REJ_MTU_EXCEEDED)
376 {
377 STREAM_TO_UINT16 (rej_mtu, p);
378 /* What to do with the MTU reject ? We have negotiated an MTU. For now */
379 /* we will ignore it and let a higher protocol timeout take care of it */
380
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700381 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 -0800382 }
383 if (rej_reason == L2CAP_CMD_REJ_INVALID_CID)
384 {
385 STREAM_TO_UINT16 (rcid, p);
386 STREAM_TO_UINT16 (lcid, p);
387
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700388 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 -0800389
390 /* Remote CID invalid. Treat as a disconnect */
391 if (((p_ccb = l2cu_find_ccb_by_cid (p_lcb, lcid)) != NULL)
392 && (p_ccb->remote_cid == rcid))
393 {
394 /* Fake link disconnect - no reply is generated */
395 l2c_csm_execute (p_ccb, L2CEVT_LP_DISCONNECT_IND, NULL);
396 }
397 }
398
399 /* SonyEricsson Info request Bug workaround (Continue connection) */
400 else if (rej_reason == L2CAP_CMD_REJ_NOT_UNDERSTOOD && p_lcb->w4_info_rsp)
401 {
402 btu_stop_timer (&p_lcb->info_timer_entry);
403
404 p_lcb->w4_info_rsp = FALSE;
405 ci.status = HCI_SUCCESS;
406 memcpy (ci.bd_addr, p_lcb->remote_bd_addr, sizeof(BD_ADDR));
407
408 /* For all channels, send the event through their FSMs */
409 for (p_ccb = p_lcb->ccb_queue.p_first_ccb; p_ccb; p_ccb = p_ccb->p_next_ccb)
410 {
411 l2c_csm_execute (p_ccb, L2CEVT_L2CAP_INFO_RSP, &ci);
412 }
413 }
414 break;
415
416 case L2CAP_CMD_CONN_REQ:
417 STREAM_TO_UINT16 (con_info.psm, p);
418 STREAM_TO_UINT16 (rcid, p);
419 if ((p_rcb = l2cu_find_rcb_by_psm (con_info.psm)) == NULL)
420 {
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700421 L2CAP_TRACE_WARNING ("L2CAP - rcvd conn req for unknown PSM: %d", con_info.psm);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800422 l2cu_reject_connection (p_lcb, rcid, id, L2CAP_CONN_NO_PSM);
423 break;
424 }
425 else
426 {
427 if (!p_rcb->api.pL2CA_ConnectInd_Cb)
428 {
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700429 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 -0800430 l2cu_reject_connection (p_lcb, rcid, id, L2CAP_CONN_NO_PSM);
431 break;
432 }
433 }
434 if ((p_ccb = l2cu_allocate_ccb (p_lcb, 0)) == NULL)
435 {
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700436 L2CAP_TRACE_ERROR ("L2CAP - unable to allocate CCB");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800437 l2cu_reject_connection (p_lcb, rcid, id, L2CAP_CONN_NO_RESOURCES);
438 break;
439 }
440 p_ccb->remote_id = id;
441 p_ccb->p_rcb = p_rcb;
442 p_ccb->remote_cid = rcid;
443
444 l2c_csm_execute(p_ccb, L2CEVT_L2CAP_CONNECT_REQ, &con_info);
445 break;
446
447 case L2CAP_CMD_CONN_RSP:
448 STREAM_TO_UINT16 (con_info.remote_cid, p);
449 STREAM_TO_UINT16 (lcid, p);
450 STREAM_TO_UINT16 (con_info.l2cap_result, p);
451 STREAM_TO_UINT16 (con_info.l2cap_status, p);
452
453 if ((p_ccb = l2cu_find_ccb_by_cid (p_lcb, lcid)) == NULL)
454 {
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700455 L2CAP_TRACE_WARNING ("L2CAP - no CCB for conn rsp, LCID: %d RCID: %d",
The Android Open Source Project5738f832012-12-12 16:00:35 -0800456 lcid, con_info.remote_cid);
457 break;
458 }
459 if (p_ccb->local_id != id)
460 {
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700461 L2CAP_TRACE_WARNING ("L2CAP - con rsp - bad ID. Exp: %d Got: %d",
The Android Open Source Project5738f832012-12-12 16:00:35 -0800462 p_ccb->local_id, id);
463 break;
464 }
465
466 if (con_info.l2cap_result == L2CAP_CONN_OK)
467 l2c_csm_execute(p_ccb, L2CEVT_L2CAP_CONNECT_RSP, &con_info);
468 else if (con_info.l2cap_result == L2CAP_CONN_PENDING)
469 l2c_csm_execute(p_ccb, L2CEVT_L2CAP_CONNECT_RSP_PND, &con_info);
470 else
471 l2c_csm_execute(p_ccb, L2CEVT_L2CAP_CONNECT_RSP_NEG, &con_info);
472
473 break;
474
475 case L2CAP_CMD_CONFIG_REQ:
476 p_cfg_end = p + cmd_len;
477 cfg_rej = FALSE;
478 cfg_rej_len = 0;
479
480 STREAM_TO_UINT16 (lcid, p);
481 STREAM_TO_UINT16 (cfg_info.flags, p);
482
483 p_cfg_start = p;
484
485 cfg_info.flush_to_present = cfg_info.mtu_present = cfg_info.qos_present =
486 cfg_info.fcr_present = cfg_info.fcs_present = FALSE;
487
488 while (p < p_cfg_end)
489 {
490 STREAM_TO_UINT8 (cfg_code, p);
491 STREAM_TO_UINT8 (cfg_len, p);
492
493 switch (cfg_code & 0x7F)
494 {
495 case L2CAP_CFG_TYPE_MTU:
496 cfg_info.mtu_present = TRUE;
497 STREAM_TO_UINT16 (cfg_info.mtu, p);
498 break;
499
500 case L2CAP_CFG_TYPE_FLUSH_TOUT:
501 cfg_info.flush_to_present = TRUE;
502 STREAM_TO_UINT16 (cfg_info.flush_to, p);
503 break;
504
505 case L2CAP_CFG_TYPE_QOS:
506 cfg_info.qos_present = TRUE;
507 STREAM_TO_UINT8 (cfg_info.qos.qos_flags, p);
508 STREAM_TO_UINT8 (cfg_info.qos.service_type, p);
509 STREAM_TO_UINT32 (cfg_info.qos.token_rate, p);
510 STREAM_TO_UINT32 (cfg_info.qos.token_bucket_size, p);
511 STREAM_TO_UINT32 (cfg_info.qos.peak_bandwidth, p);
512 STREAM_TO_UINT32 (cfg_info.qos.latency, p);
513 STREAM_TO_UINT32 (cfg_info.qos.delay_variation, p);
514 break;
515
516 case L2CAP_CFG_TYPE_FCR:
517 cfg_info.fcr_present = TRUE;
518 STREAM_TO_UINT8 (cfg_info.fcr.mode, p);
519 STREAM_TO_UINT8 (cfg_info.fcr.tx_win_sz, p);
520 STREAM_TO_UINT8 (cfg_info.fcr.max_transmit, p);
521 STREAM_TO_UINT16 (cfg_info.fcr.rtrans_tout, p);
522 STREAM_TO_UINT16 (cfg_info.fcr.mon_tout, p);
523 STREAM_TO_UINT16 (cfg_info.fcr.mps, p);
524 break;
525
526 case L2CAP_CFG_TYPE_FCS:
527 cfg_info.fcs_present = TRUE;
528 STREAM_TO_UINT8 (cfg_info.fcs, p);
529 break;
530
531 case L2CAP_CFG_TYPE_EXT_FLOW:
532 cfg_info.ext_flow_spec_present = TRUE;
533 STREAM_TO_UINT8 (cfg_info.ext_flow_spec.id, p);
534 STREAM_TO_UINT8 (cfg_info.ext_flow_spec.stype, p);
535 STREAM_TO_UINT16 (cfg_info.ext_flow_spec.max_sdu_size, p);
536 STREAM_TO_UINT32 (cfg_info.ext_flow_spec.sdu_inter_time, p);
537 STREAM_TO_UINT32 (cfg_info.ext_flow_spec.access_latency, p);
538 STREAM_TO_UINT32 (cfg_info.ext_flow_spec.flush_timeout, p);
539 break;
540
541 default:
542 /* sanity check option length */
543 if ((cfg_len + L2CAP_CFG_OPTION_OVERHEAD) <= cmd_len)
544 {
545 p += cfg_len;
546 if ((cfg_code & 0x80) == 0)
547 {
548 cfg_rej_len += cfg_len + L2CAP_CFG_OPTION_OVERHEAD;
549 cfg_rej = TRUE;
550 }
551 }
552 /* bad length; force loop exit */
553 else
554 {
555 p = p_cfg_end;
556 cfg_rej = TRUE;
557 }
558 break;
559 }
560 }
561
562 if ((p_ccb = l2cu_find_ccb_by_cid (p_lcb, lcid)) != NULL)
563 {
564 p_ccb->remote_id = id;
565 if (cfg_rej)
566 {
567 l2cu_send_peer_config_rej (p_ccb, p_cfg_start, (UINT16) (cmd_len - L2CAP_CONFIG_REQ_LEN), cfg_rej_len);
568 }
569 else
570 {
571 l2c_csm_execute (p_ccb, L2CEVT_L2CAP_CONFIG_REQ, &cfg_info);
572 }
573 }
574 else
575 {
576 /* updated spec says send command reject on invalid cid */
577 l2cu_send_peer_cmd_reject (p_lcb, L2CAP_CMD_REJ_INVALID_CID, id, 0, 0);
578 }
579 break;
580
581 case L2CAP_CMD_CONFIG_RSP:
582 p_cfg_end = p + cmd_len;
583 STREAM_TO_UINT16 (lcid, p);
584 STREAM_TO_UINT16 (cfg_info.flags, p);
585 STREAM_TO_UINT16 (cfg_info.result, p);
586
587 cfg_info.flush_to_present = cfg_info.mtu_present = cfg_info.qos_present =
588 cfg_info.fcr_present = cfg_info.fcs_present = FALSE;
589
590 while (p < p_cfg_end)
591 {
592 STREAM_TO_UINT8 (cfg_code, p);
593 STREAM_TO_UINT8 (cfg_len, p);
594
595 switch (cfg_code & 0x7F)
596 {
597 case L2CAP_CFG_TYPE_MTU:
598 cfg_info.mtu_present = TRUE;
599 STREAM_TO_UINT16 (cfg_info.mtu, p);
600 break;
601
602 case L2CAP_CFG_TYPE_FLUSH_TOUT:
603 cfg_info.flush_to_present = TRUE;
604 STREAM_TO_UINT16 (cfg_info.flush_to, p);
605 break;
606
607 case L2CAP_CFG_TYPE_QOS:
608 cfg_info.qos_present = TRUE;
609 STREAM_TO_UINT8 (cfg_info.qos.qos_flags, p);
610 STREAM_TO_UINT8 (cfg_info.qos.service_type, p);
611 STREAM_TO_UINT32 (cfg_info.qos.token_rate, p);
612 STREAM_TO_UINT32 (cfg_info.qos.token_bucket_size, p);
613 STREAM_TO_UINT32 (cfg_info.qos.peak_bandwidth, p);
614 STREAM_TO_UINT32 (cfg_info.qos.latency, p);
615 STREAM_TO_UINT32 (cfg_info.qos.delay_variation, p);
616 break;
617
618 case L2CAP_CFG_TYPE_FCR:
619 cfg_info.fcr_present = TRUE;
620 STREAM_TO_UINT8 (cfg_info.fcr.mode, p);
621 STREAM_TO_UINT8 (cfg_info.fcr.tx_win_sz, p);
622 STREAM_TO_UINT8 (cfg_info.fcr.max_transmit, p);
623 STREAM_TO_UINT16 (cfg_info.fcr.rtrans_tout, p);
624 STREAM_TO_UINT16 (cfg_info.fcr.mon_tout, p);
625 STREAM_TO_UINT16 (cfg_info.fcr.mps, p);
626 break;
627
628 case L2CAP_CFG_TYPE_FCS:
629 cfg_info.fcs_present = TRUE;
630 STREAM_TO_UINT8 (cfg_info.fcs, p);
631 break;
632
633 case L2CAP_CFG_TYPE_EXT_FLOW:
634 cfg_info.ext_flow_spec_present = TRUE;
635 STREAM_TO_UINT8 (cfg_info.ext_flow_spec.id, p);
636 STREAM_TO_UINT8 (cfg_info.ext_flow_spec.stype, p);
637 STREAM_TO_UINT16 (cfg_info.ext_flow_spec.max_sdu_size, p);
638 STREAM_TO_UINT32 (cfg_info.ext_flow_spec.sdu_inter_time, p);
639 STREAM_TO_UINT32 (cfg_info.ext_flow_spec.access_latency, p);
640 STREAM_TO_UINT32 (cfg_info.ext_flow_spec.flush_timeout, p);
641 break;
642 }
643 }
644
645 if ((p_ccb = l2cu_find_ccb_by_cid (p_lcb, lcid)) != NULL)
646 {
647 if (p_ccb->local_id != id)
648 {
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700649 L2CAP_TRACE_WARNING ("L2CAP - cfg rsp - bad ID. Exp: %d Got: %d",
The Android Open Source Project5738f832012-12-12 16:00:35 -0800650 p_ccb->local_id, id);
651 break;
652 }
653 if ( (cfg_info.result == L2CAP_CFG_OK) || (cfg_info.result == L2CAP_CFG_PENDING) )
654 l2c_csm_execute (p_ccb, L2CEVT_L2CAP_CONFIG_RSP, &cfg_info);
655 else
656 l2c_csm_execute (p_ccb, L2CEVT_L2CAP_CONFIG_RSP_NEG, &cfg_info);
657 }
658 else
659 {
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700660 L2CAP_TRACE_WARNING ("L2CAP - rcvd cfg rsp for unknown CID: 0x%04x", lcid);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800661 }
662 break;
663
664
665 case L2CAP_CMD_DISC_REQ:
666 STREAM_TO_UINT16 (lcid, p);
667 STREAM_TO_UINT16 (rcid, p);
668
669 if ((p_ccb = l2cu_find_ccb_by_cid (p_lcb, lcid)) != NULL)
670 {
671 if (p_ccb->remote_cid == rcid)
672 {
673 p_ccb->remote_id = id;
674 l2c_csm_execute (p_ccb, L2CEVT_L2CAP_DISCONNECT_REQ, &con_info);
675 }
676 }
677 else
678 l2cu_send_peer_disc_rsp (p_lcb, id, lcid, rcid);
679
680 break;
681
682 case L2CAP_CMD_DISC_RSP:
683 STREAM_TO_UINT16 (rcid, p);
684 STREAM_TO_UINT16 (lcid, p);
685
686 if ((p_ccb = l2cu_find_ccb_by_cid (p_lcb, lcid)) != NULL)
687 {
688 if ((p_ccb->remote_cid == rcid) && (p_ccb->local_id == id))
689 {
690 l2c_csm_execute (p_ccb, L2CEVT_L2CAP_DISCONNECT_RSP, &con_info);
691 }
692 }
693 break;
694
695 case L2CAP_CMD_ECHO_REQ:
The Android Open Source Project5738f832012-12-12 16:00:35 -0800696 l2cu_send_peer_echo_rsp (p_lcb, id, NULL, 0);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800697 break;
698
699 case L2CAP_CMD_ECHO_RSP:
The Android Open Source Project5738f832012-12-12 16:00:35 -0800700 if (p_lcb->p_echo_rsp_cb)
701 {
702 tL2CA_ECHO_RSP_CB *p_cb = p_lcb->p_echo_rsp_cb;
703
704 /* Zero out the callback in case app immediately calls us again */
705 p_lcb->p_echo_rsp_cb = NULL;
706
707 (*p_cb) (L2CAP_PING_RESULT_OK);
708 }
709 break;
710
711 case L2CAP_CMD_INFO_REQ:
712 STREAM_TO_UINT16 (info_type, p);
713 l2cu_send_peer_info_rsp (p_lcb, id, info_type);
714 break;
715
716 case L2CAP_CMD_INFO_RSP:
717 /* Stop the link connect timer if sent before L2CAP connection is up */
718 if (p_lcb->w4_info_rsp)
719 {
720 btu_stop_timer (&p_lcb->info_timer_entry);
721 p_lcb->w4_info_rsp = FALSE;
722 }
723
724 STREAM_TO_UINT16 (info_type, p);
725 STREAM_TO_UINT16 (result, p);
726
727 p_lcb->info_rx_bits |= (1 << info_type);
728
729 if ( (info_type == L2CAP_EXTENDED_FEATURES_INFO_TYPE)
730 && (result == L2CAP_INFO_RESP_RESULT_SUCCESS) )
731 {
732 STREAM_TO_UINT32( p_lcb->peer_ext_fea, p );
733
734#if (L2CAP_NUM_FIXED_CHNLS > 0)
735 if (p_lcb->peer_ext_fea & L2CAP_EXTFEA_FIXED_CHNLS)
736 {
737 l2cu_send_peer_info_req (p_lcb, L2CAP_FIXED_CHANNELS_INFO_TYPE);
738 break;
739 }
740 else
741 {
742 l2cu_process_fixed_chnl_resp (p_lcb);
743 }
744#endif
745 }
746
747
748#if (L2CAP_NUM_FIXED_CHNLS > 0)
749 if (info_type == L2CAP_FIXED_CHANNELS_INFO_TYPE)
750 {
751 if (result == L2CAP_INFO_RESP_RESULT_SUCCESS)
752 {
753 memcpy (p_lcb->peer_chnl_mask, p, L2CAP_FIXED_CHNL_ARRAY_SIZE);
754 }
755
756 l2cu_process_fixed_chnl_resp (p_lcb);
757 }
758#endif
759#if (L2CAP_UCD_INCLUDED == TRUE)
760 else if (info_type == L2CAP_CONNLESS_MTU_INFO_TYPE)
761 {
762 if (result == L2CAP_INFO_RESP_RESULT_SUCCESS)
763 {
764 STREAM_TO_UINT16 (p_lcb->ucd_mtu, p);
765 }
766 }
767#endif
768
769 ci.status = HCI_SUCCESS;
770 memcpy (ci.bd_addr, p_lcb->remote_bd_addr, sizeof(BD_ADDR));
771 for (p_ccb = p_lcb->ccb_queue.p_first_ccb; p_ccb; p_ccb = p_ccb->p_next_ccb)
772 {
773 l2c_csm_execute (p_ccb, L2CEVT_L2CAP_INFO_RSP, &ci);
774 }
775 break;
776
777 default:
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700778 L2CAP_TRACE_WARNING ("L2CAP - bad cmd code: %d", cmd_code);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800779 l2cu_send_peer_cmd_reject (p_lcb, L2CAP_CMD_REJ_NOT_UNDERSTOOD, id, 0, 0);
780 return;
781 }
782 }
783}
784
785/*******************************************************************************
786**
787** Function l2c_process_held_packets
788**
789** Description This function processes any L2CAP packets that arrived before
790** the HCI connection complete arrived. It is a work around for
791** badly behaved controllers.
792**
793** Returns void
794**
795*******************************************************************************/
796void l2c_process_held_packets (BOOLEAN timed_out)
797{
798 BT_HDR *p_buf, *p_buf1;
799 BUFFER_Q *p_rcv_hold_q = &l2cb.rcv_hold_q;
800
Chris Mantonfe7216c2014-05-06 10:35:42 -0700801 if (GKI_queue_is_empty(p_rcv_hold_q))
The Android Open Source Project5738f832012-12-12 16:00:35 -0800802 return;
803
804 if (!timed_out)
805 {
806 btu_stop_timer(&l2cb.rcv_hold_tle);
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700807 L2CAP_TRACE_WARNING("L2CAP HOLD CONTINUE");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800808 }
809 else
810 {
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700811 L2CAP_TRACE_WARNING("L2CAP HOLD TIMEOUT");
The Android Open Source Project5738f832012-12-12 16:00:35 -0800812 }
813
814 /* Update the timeouts in the hold queue */
815 for (p_buf = (BT_HDR *)GKI_getfirst (p_rcv_hold_q); p_buf; p_buf = p_buf1)
816 {
817 p_buf1 = (BT_HDR *)GKI_getnext (p_buf);
818 if (!timed_out || (!p_buf->layer_specific) || (--p_buf->layer_specific == 0))
819 {
820 GKI_remove_from_queue (p_rcv_hold_q, p_buf);
821 p_buf->layer_specific = 0xFFFF;
822 l2c_rcv_acl_data (p_buf);
823 }
824 }
825
826 /* If anyone still in the queue, restart the timeout */
Chris Mantonfe7216c2014-05-06 10:35:42 -0700827 if (!GKI_queue_is_empty(p_rcv_hold_q))
The Android Open Source Project5738f832012-12-12 16:00:35 -0800828 btu_start_timer (&l2cb.rcv_hold_tle, BTU_TTYPE_L2CAP_HOLD, BT_1SEC_TIMEOUT);
829}
830
831
832/*******************************************************************************
833**
834** Function l2c_init
835**
836** Description This function is called once at startup to initialize
837** all the L2CAP structures
838**
839** Returns void
840**
841*******************************************************************************/
842void l2c_init (void)
843{
844 INT16 xx;
845
846 memset (&l2cb, 0, sizeof (tL2C_CB));
847 /* the psm is increased by 2 before being used */
848 l2cb.dyn_psm = 0xFFF;
849
850 /* Put all the channel control blocks on the free queue */
851 for (xx = 0; xx < MAX_L2CAP_CHANNELS - 1; xx++)
852 {
853 l2cb.ccb_pool[xx].p_next_ccb = &l2cb.ccb_pool[xx + 1];
854 }
855
856#if (L2CAP_NON_FLUSHABLE_PB_INCLUDED == TRUE)
857 /* it will be set to L2CAP_PKT_START_NON_FLUSHABLE if controller supports */
858 l2cb.non_flushable_pbf = L2CAP_PKT_START << L2CAP_PKT_TYPE_SHIFT;
859#endif
860
861
862 l2cb.p_free_ccb_first = &l2cb.ccb_pool[0];
863 l2cb.p_free_ccb_last = &l2cb.ccb_pool[MAX_L2CAP_CHANNELS - 1];
864
865#ifdef L2CAP_DESIRED_LINK_ROLE
866 l2cb.desire_role = L2CAP_DESIRED_LINK_ROLE;
867#else
868 l2cb.desire_role = HCI_ROLE_SLAVE;
869#endif
870
871 /* Set the default idle timeout */
872 l2cb.idle_timeout = L2CAP_LINK_INACTIVITY_TOUT;
873
874#if defined(L2CAP_INITIAL_TRACE_LEVEL)
875 l2cb.l2cap_trace_level = L2CAP_INITIAL_TRACE_LEVEL;
876#else
877 l2cb.l2cap_trace_level = BT_TRACE_LEVEL_NONE; /* No traces */
878#endif
879
880#if L2CAP_CONFORMANCE_TESTING == TRUE
881 /* Conformance testing needs a dynamic response */
882 l2cb.test_info_resp = L2CAP_EXTFEA_SUPPORTED_MASK;
883#endif
884
885 /* Number of ACL buffers to use for high priority channel */
886#if (defined(L2CAP_HIGH_PRI_CHAN_QUOTA_IS_CONFIGURABLE) && (L2CAP_HIGH_PRI_CHAN_QUOTA_IS_CONFIGURABLE == TRUE))
887 l2cb.high_pri_min_xmit_quota = L2CAP_HIGH_PRI_MIN_XMIT_QUOTA;
888#endif
889
890}
891
892/*******************************************************************************
893**
894** Function l2c_process_timeout
895**
896** Description This function is called when an L2CAP-related timeout occurs
897**
898** Returns void
899**
900*******************************************************************************/
901void l2c_process_timeout (TIMER_LIST_ENT *p_tle)
902{
903 /* What type of timeout ? */
904 switch (p_tle->event)
905 {
906 case BTU_TTYPE_L2CAP_LINK:
907 l2c_link_timeout ((tL2C_LCB *)p_tle->param);
908 break;
909
910 case BTU_TTYPE_L2CAP_CHNL:
911 l2c_csm_execute (((tL2C_CCB *)p_tle->param), L2CEVT_TIMEOUT, NULL);
912 break;
913
914 case BTU_TTYPE_L2CAP_FCR_ACK:
915 l2c_csm_execute (((tL2C_CCB *)p_tle->param), L2CEVT_ACK_TIMEOUT, NULL);
916 break;
917
918 case BTU_TTYPE_L2CAP_HOLD:
919 /* Update the timeouts in the hold queue */
920 l2c_process_held_packets(TRUE);
921 break;
922
923 case BTU_TTYPE_L2CAP_INFO:
924 l2c_info_timeout((tL2C_LCB *)p_tle->param);
925 break;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800926 }
927}
928
929/*******************************************************************************
930**
931** Function l2c_data_write
932**
933** Description API functions call this function to write data.
934**
935** Returns L2CAP_DW_SUCCESS, if data accepted, else FALSE
936** L2CAP_DW_CONGESTED, if data accepted and the channel is congested
937** L2CAP_DW_FAILED, if error
938**
939*******************************************************************************/
940UINT8 l2c_data_write (UINT16 cid, BT_HDR *p_data, UINT16 flags)
941{
942 tL2C_CCB *p_ccb;
943
944 /* Find the channel control block. We don't know the link it is on. */
945 if ((p_ccb = l2cu_find_ccb_by_cid (NULL, cid)) == NULL)
946 {
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700947 L2CAP_TRACE_WARNING ("L2CAP - no CCB for L2CA_DataWrite, CID: %d", cid);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800948 GKI_freebuf (p_data);
949 return (L2CAP_DW_FAILED);
950 }
951
952#ifndef TESTER /* Tester may send any amount of data. otherwise sending message
953 bigger than mtu size of peer is a violation of protocol */
954 if (p_data->len > p_ccb->peer_cfg.mtu)
955 {
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700956 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 -0800957 GKI_freebuf (p_data);
958 return (L2CAP_DW_FAILED);
959 }
960#endif
961
962 /* channel based, packet based flushable or non-flushable */
963 p_data->layer_specific = flags;
964
965 /* If already congested, do not accept any more packets */
966 if (p_ccb->cong_sent)
967 {
Sharvil Nanavatia51c9d92014-05-04 01:08:21 -0700968 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 -0700969 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 -0800970
971 GKI_freebuf (p_data);
972 return (L2CAP_DW_FAILED);
973 }
974
975 l2c_csm_execute (p_ccb, L2CEVT_L2CA_DATA_WRITE, p_data);
976
977 if (p_ccb->cong_sent)
978 return (L2CAP_DW_CONGESTED);
979
980 return (L2CAP_DW_SUCCESS);
981}
982