1 /******************************************************************************
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 <stdlib.h>
26 #include <string.h>
27 //#include <stdio.h>
28
29 #include "device/controller.h"
30 //#include "btcore/include/counter.h"
31 #include "common/bt_target.h"
32 #include "btm_int.h"
33 #include "stack/btu.h"
34 #include "stack/hcimsgs.h"
35 #include "stack/l2c_api.h"
36 #include "l2c_int.h"
37 #include "stack/l2cdefs.h"
38 //#include "osi/include/log.h"
39
40 /********************************************************************************/
41 /* L O C A L F U N C T I O N P R O T O T Y P E S */
42 /********************************************************************************/
43 #if (CLASSIC_BT_INCLUDED == TRUE)
44 static void process_l2cap_cmd (tL2C_LCB *p_lcb, UINT8 *p, UINT16 pkt_len);
45 #endif ///CLASSIC_BT_INCLUDED == TRUE
46 /********************************************************************************/
47 /* G L O B A L L 2 C A P D A T A */
48 /********************************************************************************/
49 #if L2C_DYNAMIC_MEMORY == FALSE
50 tL2C_CB l2cb;
51 #else
52 tL2C_CB *l2c_cb_ptr;
53 #endif
54
55 #if 0 //Unused
56 /*******************************************************************************
57 **
58 ** Function l2c_bcst_msg
59 **
60 ** Description
61 **
62 ** Returns void
63 **
64 *******************************************************************************/
65 void l2c_bcst_msg( BT_HDR *p_buf, UINT16 psm )
66 {
67 UINT8 *p;
68
69 /* Ensure we have enough space in the buffer for the L2CAP and HCI headers */
70 if (p_buf->offset < L2CAP_BCST_MIN_OFFSET) {
71 L2CAP_TRACE_ERROR ("L2CAP - cannot send buffer, offset: %d", p_buf->offset);
72 osi_free (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
86 uint16_t acl_data_size = controller_get_interface()->get_acl_data_size_classic();
87 /* The HCI transport will segment the buffers. */
88 if (p_buf->len > acl_data_size) {
89 UINT16_TO_STREAM (p, acl_data_size);
90 } else {
91 UINT16_TO_STREAM (p, p_buf->len);
92 }
93
94 /* Now the L2CAP header */
95 UINT16_TO_STREAM (p, p_buf->len - L2CAP_PKT_OVERHEAD);
96 UINT16_TO_STREAM (p, L2CAP_CONNECTIONLESS_CID);
97 UINT16_TO_STREAM (p, psm);
98
99 p_buf->len += HCI_DATA_PREAMBLE_SIZE;
100
101 if (p_buf->len <= controller_get_interface()->get_acl_packet_size_classic()) {
102 //counter_add("l2cap.ch2.tx.bytes", p_buf->len);
103 //counter_add("l2cap.ch2.tx.pkts", 1);
104
105 bte_main_hci_send(p_buf, BT_EVT_TO_LM_HCI_ACL);
106 }
107 }
108 #endif
109
110 /*******************************************************************************
111 **
112 ** Function l2c_rcv_acl_data
113 **
114 ** Description This function is called from the HCI Interface when an ACL
115 ** data packet is received.
116 **
117 ** Returns void
118 **
119 *******************************************************************************/
l2c_rcv_acl_data(BT_HDR * p_msg)120 void l2c_rcv_acl_data (BT_HDR *p_msg)
121 {
122 UINT8 *p = (UINT8 *)(p_msg + 1) + p_msg->offset;
123 UINT16 handle, hci_len;
124 UINT8 pkt_type;
125 tL2C_LCB *p_lcb;
126 tL2C_CCB *p_ccb = NULL;
127 UINT16 l2cap_len, rcv_cid;
128 #if (!CONFIG_BT_STACK_NO_LOG)
129 UINT16 psm;
130 #endif
131 UINT16 credit;
132
133 /* Extract the handle */
134 STREAM_TO_UINT16 (handle, p);
135 pkt_type = HCID_GET_EVENT (handle);
136 handle = HCID_GET_HANDLE (handle);
137
138 /* Since the HCI Transport is putting segmented packets back together, we */
139 /* should never get a valid packet with the type set to "continuation" */
140 if (pkt_type != L2CAP_PKT_CONTINUE) {
141 /* Find the LCB based on the handle */
142 if ((p_lcb = l2cu_find_lcb_by_handle (handle)) == NULL) {
143 UINT8 cmd_code;
144
145 /* There is a slight possibility (specifically with USB) that we get an */
146 /* L2CAP connection request before we get the HCI connection complete. */
147 /* So for these types of messages, hold them for up to 2 seconds. */
148 STREAM_TO_UINT16 (hci_len, p);
149 STREAM_TO_UINT16 (l2cap_len, p);
150 STREAM_TO_UINT16 (rcv_cid, p);
151 STREAM_TO_UINT8 (cmd_code, p);
152
153 if ((p_msg->layer_specific == 0) && (rcv_cid == L2CAP_SIGNALLING_CID)
154 && (cmd_code == L2CAP_CMD_INFO_REQ || cmd_code == L2CAP_CMD_CONN_REQ)) {
155 L2CAP_TRACE_WARNING ("L2CAP - holding ACL for unknown handle:%d ls:%d"
156 " cid:%d opcode:%d cur count:%d", handle, p_msg->layer_specific,
157 rcv_cid, cmd_code, list_length(l2cb.rcv_pending_q));
158 p_msg->layer_specific = 2;
159 list_append(l2cb.rcv_pending_q, p_msg);
160
161 if (list_length(l2cb.rcv_pending_q) == 1) {
162 btu_start_timer (&l2cb.rcv_hold_tle, BTU_TTYPE_L2CAP_HOLD, BT_1SEC_TIMEOUT);
163 }
164
165 return;
166 } else {
167 L2CAP_TRACE_ERROR ("L2CAP - rcvd ACL for unknown handle:%d ls:%d cid:%d"
168 " opcode:%d cur count:%d", handle, p_msg->layer_specific, rcv_cid,
169 cmd_code, list_length(l2cb.rcv_pending_q));
170 }
171 osi_free (p_msg);
172 return;
173 }
174 } else {
175 L2CAP_TRACE_WARNING ("L2CAP - expected pkt start or complete, got: %d", pkt_type);
176 osi_free (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
184 /* Extract the length and CID */
185 STREAM_TO_UINT16 (l2cap_len, p);
186 STREAM_TO_UINT16 (rcv_cid, p);
187
188 #if BLE_INCLUDED == TRUE
189 /* for BLE channel, always notify connection when ACL data received on the link */
190 if (p_lcb && p_lcb->transport == BT_TRANSPORT_LE && p_lcb->link_state != LST_DISCONNECTING)
191 /* only process fixed channel data as channel open indication when link is not in disconnecting mode */
192 {
193 l2cble_notify_le_connection(p_lcb->remote_bd_addr);
194 }
195 #endif
196 L2CAP_TRACE_DEBUG ("L2CAP - rcv_cid CID: 0x%04x\n", rcv_cid);
197 /* Find the CCB for this CID */
198 if (rcv_cid >= L2CAP_BASE_APPL_CID) {
199 if ((p_ccb = l2cu_find_ccb_by_cid (p_lcb, rcv_cid)) == NULL) {
200 L2CAP_TRACE_WARNING ("L2CAP - unknown CID: 0x%04x", rcv_cid);
201 osi_free (p_msg);
202 return;
203 }
204 }
205
206 if (hci_len >= L2CAP_PKT_OVERHEAD) { /* Must receive at least the L2CAP length and CID.*/
207 p_msg->len = hci_len - L2CAP_PKT_OVERHEAD;
208 p_msg->offset += L2CAP_PKT_OVERHEAD;
209 } else {
210 L2CAP_TRACE_WARNING ("L2CAP - got incorrect hci header" );
211 osi_free (p_msg);
212 return;
213 }
214
215 if (l2cap_len != p_msg->len) {
216 L2CAP_TRACE_WARNING ("L2CAP - bad length in pkt. Exp: %d Act: %d",
217 l2cap_len, p_msg->len);
218
219 osi_free (p_msg);
220 return;
221 }
222
223 /* Send the data through the channel state machine */
224 if (rcv_cid == L2CAP_SIGNALLING_CID) {
225 //counter_add("l2cap.sig.rx.bytes", l2cap_len);
226 //counter_add("l2cap.sig.rx.pkts", 1);
227 #if (CLASSIC_BT_INCLUDED == TRUE)
228 process_l2cap_cmd (p_lcb, p, l2cap_len);
229 #endif ///CLASSIC_BT_INCLUDED == TRUE
230 osi_free (p_msg);
231 } else if (rcv_cid == L2CAP_CONNECTIONLESS_CID) {
232 //counter_add("l2cap.ch2.rx.bytes", l2cap_len);
233 //counter_add("l2cap.ch2.rx.pkts", 1);
234 /* process_connectionless_data (p_lcb); */
235 #if !CONFIG_BT_STACK_NO_LOG
236 STREAM_TO_UINT16 (psm, p);
237 #endif
238 L2CAP_TRACE_DEBUG( "GOT CONNECTIONLESS DATA PSM:%d", psm ) ;
239
240 #if (L2CAP_UCD_INCLUDED == TRUE)
241 /* if it is not broadcast, check UCD registration */
242 if ( l2c_ucd_check_rx_pkts( p_lcb, p_msg ) ) {
243 /* nothing to do */
244 } else
245 #endif
246 {
247 osi_free (p_msg);
248 }
249 }
250 #if (BLE_INCLUDED == TRUE)
251 else if (rcv_cid == L2CAP_BLE_SIGNALLING_CID) {
252 //counter_add("l2cap.ble.rx.bytes", l2cap_len);
253 //counter_add("l2cap.ble.rx.pkts", 1);
254 l2cble_process_sig_cmd (p_lcb, p, l2cap_len);
255 osi_free (p_msg);
256 }
257 #endif
258 #if (L2CAP_NUM_FIXED_CHNLS > 0)
259 else if ((rcv_cid >= L2CAP_FIRST_FIXED_CHNL) && (rcv_cid <= L2CAP_LAST_FIXED_CHNL) &&
260 (l2cb.fixed_reg[rcv_cid - L2CAP_FIRST_FIXED_CHNL].pL2CA_FixedData_Cb != NULL) ) {
261 //counter_add("l2cap.fix.rx.bytes", l2cap_len);
262 //counter_add("l2cap.fix.rx.pkts", 1);
263 /* If no CCB for this channel, allocate one */
264 if (p_lcb &&
265 /* only process fixed channel data when link is open or wait for data indication */
266 (p_lcb->link_state != LST_DISCONNECTING) &&
267 l2cu_initialize_fixed_ccb (p_lcb, rcv_cid, &l2cb.fixed_reg[rcv_cid - L2CAP_FIRST_FIXED_CHNL].fixed_chnl_opts)) {
268 p_ccb = p_lcb->p_fixed_ccbs[rcv_cid - L2CAP_FIRST_FIXED_CHNL];
269
270 if (p_ccb->peer_cfg.fcr.mode != L2CAP_FCR_BASIC_MODE) {
271 #if (CLASSIC_BT_INCLUDED == TRUE)
272 l2c_fcr_proc_pdu (p_ccb, p_msg);
273 #endif ///CLASSIC_BT_INCLUDED == TRUE
274 } else {
275 (*l2cb.fixed_reg[rcv_cid - L2CAP_FIRST_FIXED_CHNL].pL2CA_FixedData_Cb)
276 (rcv_cid, p_lcb->remote_bd_addr, p_msg);
277 }
278 } else {
279 osi_free (p_msg);
280 }
281 }
282 #endif
283
284 else {
285 //counter_add("l2cap.dyn.rx.bytes", l2cap_len);
286 //counter_add("l2cap.dyn.rx.pkts", 1);
287 if (p_ccb == NULL) {
288 osi_free (p_msg);
289 } else {
290 if (p_lcb->transport == BT_TRANSPORT_LE) {
291 // Got a pkt, valid send out credits to the peer device
292 credit = L2CAP_LE_DEFAULT_CREDIT;
293 L2CAP_TRACE_DEBUG("%s Credits received %d",__func__, credit);
294 if((p_ccb->peer_conn_cfg.credits + credit) > L2CAP_LE_MAX_CREDIT) {
295 /* we have received credits more than max coc credits,
296 * so disconnecting the Le Coc Channel
297 */
298 #if (BLE_INCLUDED == TRUE)
299 l2cble_send_peer_disc_req (p_ccb);
300 #endif ///BLE_INCLUDED == TRUE
301 } else {
302 p_ccb->peer_conn_cfg.credits += credit;
303 l2c_link_check_send_pkts (p_ccb->p_lcb, NULL, NULL);
304 }
305 }
306 /* Basic mode packets go straight to the state machine */
307 if (p_ccb->peer_cfg.fcr.mode == L2CAP_FCR_BASIC_MODE) {
308 #if (CLASSIC_BT_INCLUDED == TRUE)
309 l2c_csm_execute (p_ccb, L2CEVT_L2CAP_DATA, p_msg);
310 #endif ///CLASSIC_BT_INCLUDED == TRUE
311 } else {
312 /* eRTM or streaming mode, so we need to validate states first */
313 if ((p_ccb->chnl_state == CST_OPEN) || (p_ccb->chnl_state == CST_CONFIG)) {
314 #if (CLASSIC_BT_INCLUDED == TRUE)
315 l2c_fcr_proc_pdu (p_ccb, p_msg);
316 #endif ///CLASSIC_BT_INCLUDED == TRUE
317 } else {
318 osi_free (p_msg);
319 }
320 }
321 }
322 }
323 }
324
325 /*******************************************************************************
326 **
327 ** Function process_l2cap_cmd
328 **
329 ** Description This function is called when a packet is received on the
330 ** L2CAP signalling CID
331 **
332 ** Returns void
333 **
334 *******************************************************************************/
335 #if (CLASSIC_BT_INCLUDED == TRUE)
process_l2cap_cmd(tL2C_LCB * p_lcb,UINT8 * p,UINT16 pkt_len)336 static void process_l2cap_cmd (tL2C_LCB *p_lcb, UINT8 *p, UINT16 pkt_len)
337 {
338 UINT8 *p_pkt_end, *p_next_cmd, *p_cfg_end, *p_cfg_start;
339 UINT8 cmd_code, cfg_code, cfg_len, id;
340 tL2C_CONN_INFO con_info;
341 tL2CAP_CFG_INFO cfg_info;
342 UINT16 rej_reason, rej_mtu, lcid, rcid, info_type;
343 tL2C_CCB *p_ccb;
344 tL2C_RCB *p_rcb;
345 BOOLEAN cfg_rej, pkt_size_rej = FALSE;
346 UINT16 cfg_rej_len, cmd_len;
347 UINT16 result;
348 tL2C_CONN_INFO ci;
349
350 #if (defined BLE_INCLUDED && BLE_INCLUDED == TRUE)
351 /* if l2cap command received in CID 1 on top of an LE link, ignore this command */
352 if (p_lcb->transport == BT_TRANSPORT_LE) {
353 return;
354 }
355 #endif
356
357 /* Reject the packet if it exceeds the default Signalling Channel MTU */
358 if (pkt_len > L2CAP_DEFAULT_MTU) {
359 /* Core Spec requires a single response to the first command found in a multi-command
360 ** L2cap packet. If only responses in the packet, then it will be ignored.
361 ** Here we simply mark the bad packet and decide which cmd ID to reject later
362 */
363 pkt_size_rej = TRUE;
364 L2CAP_TRACE_ERROR ("L2CAP SIG MTU Pkt Len Exceeded (672) -> pkt_len: %d", pkt_len);
365 }
366
367 p_next_cmd = p;
368 p_pkt_end = p + pkt_len;
369
370 memset (&cfg_info, 0, sizeof(cfg_info));
371
372 /* An L2CAP packet may contain multiple commands */
373 while (TRUE) {
374 /* Smallest command is 4 bytes */
375 if ((p = p_next_cmd) > (p_pkt_end - 4)) {
376 break;
377 }
378
379 STREAM_TO_UINT8 (cmd_code, p);
380 STREAM_TO_UINT8 (id, p);
381 STREAM_TO_UINT16 (cmd_len, p);
382
383 /* Check command length does not exceed packet length */
384 if ((p_next_cmd = p + cmd_len) > p_pkt_end) {
385 L2CAP_TRACE_WARNING ("Command len bad pkt_len: %d cmd_len: %d code: %d",
386 pkt_len, cmd_len, cmd_code);
387 break;
388 }
389
390 L2CAP_TRACE_DEBUG ("cmd_code: %d, id:%d, cmd_len:%d", cmd_code, id, cmd_len);
391
392 /* Bad L2CAP packet length, look or cmd to reject */
393 if (pkt_size_rej) {
394 /* If command found rejected it and we're done, otherwise keep looking */
395 if (l2c_is_cmd_rejected(cmd_code, id, p_lcb)) {
396 return;
397 } else {
398 continue; /* Look for next cmd/response in current packet */
399 }
400 }
401
402 switch (cmd_code) {
403 case L2CAP_CMD_REJECT:
404 STREAM_TO_UINT16 (rej_reason, p);
405 if (rej_reason == L2CAP_CMD_REJ_MTU_EXCEEDED) {
406 STREAM_TO_UINT16 (rej_mtu, p);
407 /* What to do with the MTU reject ? We have negotiated an MTU. For now */
408 /* we will ignore it and let a higher protocol timeout take care of it */
409 L2CAP_TRACE_WARNING ("L2CAP - MTU rej Handle: %d MTU: %d", p_lcb->handle, rej_mtu);
410 }
411 if (rej_reason == L2CAP_CMD_REJ_INVALID_CID) {
412 STREAM_TO_UINT16 (rcid, p);
413 STREAM_TO_UINT16 (lcid, p);
414
415 L2CAP_TRACE_WARNING ("L2CAP - rej with CID invalid, LCID: 0x%04x RCID: 0x%04x", lcid, rcid);
416
417 /* Remote CID invalid. Treat as a disconnect */
418 if (((p_ccb = l2cu_find_ccb_by_cid (p_lcb, lcid)) != NULL)
419 && (p_ccb->remote_cid == rcid)) {
420 /* Fake link disconnect - no reply is generated */
421 l2c_csm_execute (p_ccb, L2CEVT_LP_DISCONNECT_IND, NULL);
422 }
423 }
424
425 /* SonyEricsson Info request Bug workaround (Continue connection) */
426 else if (rej_reason == L2CAP_CMD_REJ_NOT_UNDERSTOOD && p_lcb->w4_info_rsp) {
427 btu_stop_timer (&p_lcb->info_timer_entry);
428
429 p_lcb->w4_info_rsp = FALSE;
430 ci.status = HCI_SUCCESS;
431 memcpy (ci.bd_addr, p_lcb->remote_bd_addr, sizeof(BD_ADDR));
432
433 /* For all channels, send the event through their FSMs */
434 for (p_ccb = p_lcb->ccb_queue.p_first_ccb; p_ccb; p_ccb = p_ccb->p_next_ccb) {
435 l2c_csm_execute (p_ccb, L2CEVT_L2CAP_INFO_RSP, &ci);
436 }
437 }
438 break;
439
440 case L2CAP_CMD_CONN_REQ:
441 STREAM_TO_UINT16 (con_info.psm, p);
442 STREAM_TO_UINT16 (rcid, p);
443 if ((p_rcb = l2cu_find_rcb_by_psm (con_info.psm)) == NULL) {
444 L2CAP_TRACE_WARNING ("L2CAP - rcvd conn req for unknown PSM: %d", con_info.psm);
445 l2cu_reject_connection (p_lcb, rcid, id, L2CAP_CONN_NO_PSM);
446 break;
447 } else {
448 if (!p_rcb->api.pL2CA_ConnectInd_Cb) {
449 L2CAP_TRACE_WARNING ("L2CAP - rcvd conn req for outgoing-only connection PSM: %d", con_info.psm);
450 l2cu_reject_connection (p_lcb, rcid, id, L2CAP_CONN_NO_PSM);
451 break;
452 }
453 }
454 if ((p_ccb = l2cu_allocate_ccb (p_lcb, 0)) == NULL) {
455 L2CAP_TRACE_ERROR ("L2CAP - unable to allocate CCB");
456 l2cu_reject_connection (p_lcb, rcid, id, L2CAP_CONN_NO_RESOURCES);
457 break;
458 }
459 p_ccb->remote_id = id;
460 p_ccb->p_rcb = p_rcb;
461 p_ccb->remote_cid = rcid;
462
463 l2c_csm_execute(p_ccb, L2CEVT_L2CAP_CONNECT_REQ, &con_info);
464 break;
465
466 case L2CAP_CMD_CONN_RSP:
467 STREAM_TO_UINT16 (con_info.remote_cid, p);
468 STREAM_TO_UINT16 (lcid, p);
469 STREAM_TO_UINT16 (con_info.l2cap_result, p);
470 STREAM_TO_UINT16 (con_info.l2cap_status, p);
471
472 if ((p_ccb = l2cu_find_ccb_by_cid (p_lcb, lcid)) == NULL) {
473 L2CAP_TRACE_WARNING ("L2CAP - no CCB for conn rsp, LCID: %d RCID: %d",
474 lcid, con_info.remote_cid);
475 break;
476 }
477 if (p_ccb->local_id != id) {
478 L2CAP_TRACE_WARNING ("L2CAP - con rsp - bad ID. Exp: %d Got: %d",
479 p_ccb->local_id, id);
480 break;
481 }
482
483 if (con_info.l2cap_result == L2CAP_CONN_OK) {
484 l2c_csm_execute(p_ccb, L2CEVT_L2CAP_CONNECT_RSP, &con_info);
485 } else if (con_info.l2cap_result == L2CAP_CONN_PENDING) {
486 l2c_csm_execute(p_ccb, L2CEVT_L2CAP_CONNECT_RSP_PND, &con_info);
487 } else {
488 l2c_csm_execute(p_ccb, L2CEVT_L2CAP_CONNECT_RSP_NEG, &con_info);
489 }
490
491 break;
492
493 case L2CAP_CMD_CONFIG_REQ:
494 p_cfg_end = p + cmd_len;
495 cfg_rej = FALSE;
496 cfg_rej_len = 0;
497
498 STREAM_TO_UINT16 (lcid, p);
499 STREAM_TO_UINT16 (cfg_info.flags, p);
500
501 p_cfg_start = p;
502
503 cfg_info.flush_to_present = cfg_info.mtu_present = cfg_info.qos_present =
504 cfg_info.fcr_present = cfg_info.fcs_present = FALSE;
505
506 while (p < p_cfg_end) {
507 STREAM_TO_UINT8 (cfg_code, p);
508 STREAM_TO_UINT8 (cfg_len, p);
509
510 switch (cfg_code & 0x7F) {
511 case L2CAP_CFG_TYPE_MTU:
512 cfg_info.mtu_present = TRUE;
513 STREAM_TO_UINT16 (cfg_info.mtu, p);
514 break;
515
516 case L2CAP_CFG_TYPE_FLUSH_TOUT:
517 cfg_info.flush_to_present = TRUE;
518 STREAM_TO_UINT16 (cfg_info.flush_to, p);
519 break;
520
521 case L2CAP_CFG_TYPE_QOS:
522 cfg_info.qos_present = TRUE;
523 STREAM_TO_UINT8 (cfg_info.qos.qos_flags, p);
524 STREAM_TO_UINT8 (cfg_info.qos.service_type, p);
525 STREAM_TO_UINT32 (cfg_info.qos.token_rate, p);
526 STREAM_TO_UINT32 (cfg_info.qos.token_bucket_size, p);
527 STREAM_TO_UINT32 (cfg_info.qos.peak_bandwidth, p);
528 STREAM_TO_UINT32 (cfg_info.qos.latency, p);
529 STREAM_TO_UINT32 (cfg_info.qos.delay_variation, p);
530 break;
531
532 case L2CAP_CFG_TYPE_FCR:
533 cfg_info.fcr_present = TRUE;
534 STREAM_TO_UINT8 (cfg_info.fcr.mode, p);
535 STREAM_TO_UINT8 (cfg_info.fcr.tx_win_sz, p);
536 STREAM_TO_UINT8 (cfg_info.fcr.max_transmit, p);
537 STREAM_TO_UINT16 (cfg_info.fcr.rtrans_tout, p);
538 STREAM_TO_UINT16 (cfg_info.fcr.mon_tout, p);
539 STREAM_TO_UINT16 (cfg_info.fcr.mps, p);
540 break;
541
542 case L2CAP_CFG_TYPE_FCS:
543 cfg_info.fcs_present = TRUE;
544 STREAM_TO_UINT8 (cfg_info.fcs, p);
545 break;
546
547 case L2CAP_CFG_TYPE_EXT_FLOW:
548 cfg_info.ext_flow_spec_present = TRUE;
549 STREAM_TO_UINT8 (cfg_info.ext_flow_spec.id, p);
550 STREAM_TO_UINT8 (cfg_info.ext_flow_spec.stype, p);
551 STREAM_TO_UINT16 (cfg_info.ext_flow_spec.max_sdu_size, p);
552 STREAM_TO_UINT32 (cfg_info.ext_flow_spec.sdu_inter_time, p);
553 STREAM_TO_UINT32 (cfg_info.ext_flow_spec.access_latency, p);
554 STREAM_TO_UINT32 (cfg_info.ext_flow_spec.flush_timeout, p);
555 break;
556
557 default:
558 /* sanity check option length */
559 if ((cfg_len + L2CAP_CFG_OPTION_OVERHEAD) <= cmd_len) {
560 p += cfg_len;
561 if ((cfg_code & 0x80) == 0) {
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 p = p_cfg_end;
569 cfg_rej = TRUE;
570 }
571 break;
572 }
573 }
574
575 if ((p_ccb = l2cu_find_ccb_by_cid (p_lcb, lcid)) != NULL) {
576 p_ccb->remote_id = id;
577 if (cfg_rej) {
578 l2cu_send_peer_config_rej (p_ccb, p_cfg_start, (UINT16) (cmd_len - L2CAP_CONFIG_REQ_LEN), cfg_rej_len);
579 } else {
580 l2c_csm_execute (p_ccb, L2CEVT_L2CAP_CONFIG_REQ, &cfg_info);
581 }
582 } else {
583 /* updated spec says send command reject on invalid cid */
584 l2cu_send_peer_cmd_reject (p_lcb, L2CAP_CMD_REJ_INVALID_CID, id, 0, 0);
585 }
586 break;
587
588 case L2CAP_CMD_CONFIG_RSP:
589 p_cfg_end = p + cmd_len;
590 STREAM_TO_UINT16 (lcid, p);
591 STREAM_TO_UINT16 (cfg_info.flags, p);
592 STREAM_TO_UINT16 (cfg_info.result, p);
593
594 cfg_info.flush_to_present = cfg_info.mtu_present = cfg_info.qos_present =
595 cfg_info.fcr_present = cfg_info.fcs_present = FALSE;
596
597 while (p < p_cfg_end) {
598 STREAM_TO_UINT8 (cfg_code, p);
599 STREAM_TO_UINT8 (cfg_len, p);
600
601 switch (cfg_code & 0x7F) {
602 case L2CAP_CFG_TYPE_MTU:
603 cfg_info.mtu_present = TRUE;
604 STREAM_TO_UINT16 (cfg_info.mtu, p);
605 break;
606
607 case L2CAP_CFG_TYPE_FLUSH_TOUT:
608 cfg_info.flush_to_present = TRUE;
609 STREAM_TO_UINT16 (cfg_info.flush_to, p);
610 break;
611
612 case L2CAP_CFG_TYPE_QOS:
613 cfg_info.qos_present = TRUE;
614 STREAM_TO_UINT8 (cfg_info.qos.qos_flags, p);
615 STREAM_TO_UINT8 (cfg_info.qos.service_type, p);
616 STREAM_TO_UINT32 (cfg_info.qos.token_rate, p);
617 STREAM_TO_UINT32 (cfg_info.qos.token_bucket_size, p);
618 STREAM_TO_UINT32 (cfg_info.qos.peak_bandwidth, p);
619 STREAM_TO_UINT32 (cfg_info.qos.latency, p);
620 STREAM_TO_UINT32 (cfg_info.qos.delay_variation, p);
621 break;
622
623 case L2CAP_CFG_TYPE_FCR:
624 cfg_info.fcr_present = TRUE;
625 STREAM_TO_UINT8 (cfg_info.fcr.mode, p);
626 STREAM_TO_UINT8 (cfg_info.fcr.tx_win_sz, p);
627 STREAM_TO_UINT8 (cfg_info.fcr.max_transmit, p);
628 STREAM_TO_UINT16 (cfg_info.fcr.rtrans_tout, p);
629 STREAM_TO_UINT16 (cfg_info.fcr.mon_tout, p);
630 STREAM_TO_UINT16 (cfg_info.fcr.mps, p);
631 break;
632
633 case L2CAP_CFG_TYPE_FCS:
634 cfg_info.fcs_present = TRUE;
635 STREAM_TO_UINT8 (cfg_info.fcs, p);
636 break;
637
638 case L2CAP_CFG_TYPE_EXT_FLOW:
639 cfg_info.ext_flow_spec_present = TRUE;
640 STREAM_TO_UINT8 (cfg_info.ext_flow_spec.id, p);
641 STREAM_TO_UINT8 (cfg_info.ext_flow_spec.stype, p);
642 STREAM_TO_UINT16 (cfg_info.ext_flow_spec.max_sdu_size, p);
643 STREAM_TO_UINT32 (cfg_info.ext_flow_spec.sdu_inter_time, p);
644 STREAM_TO_UINT32 (cfg_info.ext_flow_spec.access_latency, p);
645 STREAM_TO_UINT32 (cfg_info.ext_flow_spec.flush_timeout, p);
646 break;
647 }
648 }
649
650 if ((p_ccb = l2cu_find_ccb_by_cid (p_lcb, lcid)) != NULL) {
651 if (p_ccb->local_id != id) {
652 L2CAP_TRACE_WARNING ("L2CAP - cfg rsp - bad ID. Exp: %d Got: %d",
653 p_ccb->local_id, id);
654 break;
655 }
656 if ( (cfg_info.result == L2CAP_CFG_OK) || (cfg_info.result == L2CAP_CFG_PENDING) ) {
657 l2c_csm_execute (p_ccb, L2CEVT_L2CAP_CONFIG_RSP, &cfg_info);
658 } else {
659 l2c_csm_execute (p_ccb, L2CEVT_L2CAP_CONFIG_RSP_NEG, &cfg_info);
660 }
661 } else {
662 L2CAP_TRACE_WARNING ("L2CAP - rcvd cfg rsp for unknown CID: 0x%04x", lcid);
663 }
664 break;
665
666
667 case L2CAP_CMD_DISC_REQ:
668 STREAM_TO_UINT16 (lcid, p);
669 STREAM_TO_UINT16 (rcid, p);
670
671 if ((p_ccb = l2cu_find_ccb_by_cid (p_lcb, lcid)) != NULL) {
672 if (p_ccb->remote_cid == rcid) {
673 p_ccb->remote_id = id;
674 l2c_csm_execute (p_ccb, L2CEVT_L2CAP_DISCONNECT_REQ, &con_info);
675 }
676 } else {
677 l2cu_send_peer_disc_rsp (p_lcb, id, lcid, rcid);
678 }
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 if ((p_ccb->remote_cid == rcid) && (p_ccb->local_id == id)) {
688 l2c_csm_execute (p_ccb, L2CEVT_L2CAP_DISCONNECT_RSP, &con_info);
689 }
690 }
691 break;
692
693 case L2CAP_CMD_ECHO_REQ:
694 l2cu_send_peer_echo_rsp (p_lcb, id, NULL, 0);
695 break;
696
697 case L2CAP_CMD_ECHO_RSP:
698 if (p_lcb->p_echo_rsp_cb) {
699 tL2CA_ECHO_RSP_CB *p_cb = p_lcb->p_echo_rsp_cb;
700
701 /* Zero out the callback in case app immediately calls us again */
702 p_lcb->p_echo_rsp_cb = NULL;
703
704 (*p_cb) (L2CAP_PING_RESULT_OK);
705 }
706 break;
707
708 case L2CAP_CMD_INFO_REQ:
709 STREAM_TO_UINT16 (info_type, p);
710 l2cu_send_peer_info_rsp (p_lcb, id, info_type);
711 break;
712
713 case L2CAP_CMD_INFO_RSP:
714 /* Stop the link connect timer if sent before L2CAP connection is up */
715 if (p_lcb->w4_info_rsp) {
716 btu_stop_timer (&p_lcb->info_timer_entry);
717 p_lcb->w4_info_rsp = FALSE;
718 }
719
720 STREAM_TO_UINT16 (info_type, p);
721 STREAM_TO_UINT16 (result, p);
722
723 p_lcb->info_rx_bits |= (1 << info_type);
724
725 if ( (info_type == L2CAP_EXTENDED_FEATURES_INFO_TYPE)
726 && (result == L2CAP_INFO_RESP_RESULT_SUCCESS) ) {
727 STREAM_TO_UINT32( p_lcb->peer_ext_fea, p );
728
729 #if (L2CAP_NUM_FIXED_CHNLS > 0)
730 if (p_lcb->peer_ext_fea & L2CAP_EXTFEA_FIXED_CHNLS) {
731 l2cu_send_peer_info_req (p_lcb, L2CAP_FIXED_CHANNELS_INFO_TYPE);
732 break;
733 } else {
734 l2cu_process_fixed_chnl_resp (p_lcb);
735 }
736 #endif
737 }
738
739
740 #if (L2CAP_NUM_FIXED_CHNLS > 0)
741 if (info_type == L2CAP_FIXED_CHANNELS_INFO_TYPE) {
742 if (result == L2CAP_INFO_RESP_RESULT_SUCCESS) {
743 memcpy (p_lcb->peer_chnl_mask, p, L2CAP_FIXED_CHNL_ARRAY_SIZE);
744 }
745
746 l2cu_process_fixed_chnl_resp (p_lcb);
747 }
748 #endif
749 #if (L2CAP_UCD_INCLUDED == TRUE)
750 else if (info_type == L2CAP_CONNLESS_MTU_INFO_TYPE) {
751 if (result == L2CAP_INFO_RESP_RESULT_SUCCESS) {
752 STREAM_TO_UINT16 (p_lcb->ucd_mtu, p);
753 }
754 }
755 #endif
756
757 ci.status = HCI_SUCCESS;
758 memcpy (ci.bd_addr, p_lcb->remote_bd_addr, sizeof(BD_ADDR));
759 for (p_ccb = p_lcb->ccb_queue.p_first_ccb; p_ccb; p_ccb = p_ccb->p_next_ccb) {
760 l2c_csm_execute (p_ccb, L2CEVT_L2CAP_INFO_RSP, &ci);
761 }
762 break;
763
764 default:
765 L2CAP_TRACE_WARNING ("L2CAP - bad cmd code: %d", cmd_code);
766 l2cu_send_peer_cmd_reject (p_lcb, L2CAP_CMD_REJ_NOT_UNDERSTOOD, id, 0, 0);
767 return;
768 }
769 }
770
771 UNUSED(rej_mtu);
772 }
773 #endif ///CLASSIC_BT_INCLUDED == TRUE
774
775
776 /*******************************************************************************
777 **
778 ** Function l2c_process_held_packets
779 **
780 ** Description This function processes any L2CAP packets that arrived before
781 ** the HCI connection complete arrived. It is a work around for
782 ** badly behaved controllers.
783 **
784 ** Returns void
785 **
786 *******************************************************************************/
l2c_process_held_packets(BOOLEAN timed_out)787 void l2c_process_held_packets(BOOLEAN timed_out)
788 {
789 if (list_is_empty(l2cb.rcv_pending_q)) {
790 return;
791 }
792
793 if (!timed_out) {
794 btu_stop_timer(&l2cb.rcv_hold_tle);
795 L2CAP_TRACE_WARNING("L2CAP HOLD CONTINUE");
796 } else {
797 L2CAP_TRACE_WARNING("L2CAP HOLD TIMEOUT");
798 }
799
800 for (const list_node_t *node = list_begin(l2cb.rcv_pending_q);
801 node != list_end(l2cb.rcv_pending_q);) {
802 BT_HDR *p_buf = list_node(node);
803 node = list_next(node);
804 if (!timed_out || (!p_buf->layer_specific) || (--p_buf->layer_specific == 0)) {
805 list_remove(l2cb.rcv_pending_q, p_buf);
806 p_buf->layer_specific = 0xFFFF;
807 l2c_rcv_acl_data(p_buf);
808 }
809 }
810
811 /* If anyone still in the queue, restart the timeout */
812 if (!list_is_empty(l2cb.rcv_pending_q)) {
813 btu_start_timer (&l2cb.rcv_hold_tle, BTU_TTYPE_L2CAP_HOLD, BT_1SEC_TIMEOUT);
814 }
815 }
816
817
818 /*******************************************************************************
819 **
820 ** Function l2c_init
821 **
822 ** Description This function is called once at startup to initialize
823 ** all the L2CAP structures
824 **
825 ** Returns void
826 **
827 *******************************************************************************/
l2c_init(void)828 void l2c_init (void)
829 {
830 #if L2C_DYNAMIC_MEMORY
831 l2c_cb_ptr = (tL2C_CB *)osi_malloc(sizeof(tL2C_CB));
832 #endif /* #if L2C_DYNAMIC_MEMORY */
833 memset (&l2cb, 0, sizeof (tL2C_CB));
834 /* the psm is increased by 2 before being used */
835 l2cb.dyn_psm = 0xFFF;
836
837 l2cb.p_ccb_pool = list_new(osi_free_func);
838 if (l2cb.p_ccb_pool == NULL) {
839 L2CAP_TRACE_ERROR("%s unable to allocate memory for L2CAP channel control block", __func__);
840 }
841 l2cb.p_lcb_pool = list_new(osi_free_func);
842 if (l2cb.p_lcb_pool == NULL) {
843 L2CAP_TRACE_ERROR("%s unable to allocate memory for L2CAP Link control block", __func__);
844 }
845
846 #if (L2CAP_NON_FLUSHABLE_PB_INCLUDED == TRUE)
847 /* it will be set to L2CAP_PKT_START_NON_FLUSHABLE if controller supports */
848 l2cb.non_flushable_pbf = L2CAP_PKT_START << L2CAP_PKT_TYPE_SHIFT;
849 #endif
850
851
852
853 #ifdef L2CAP_DESIRED_LINK_ROLE
854 l2cb.desire_role = L2CAP_DESIRED_LINK_ROLE;
855 #else
856 l2cb.desire_role = HCI_ROLE_SLAVE;
857 #endif
858
859 /* Set the default idle timeout */
860 l2cb.idle_timeout = L2CAP_LINK_INACTIVITY_TOUT;
861
862 #if defined(L2CAP_INITIAL_TRACE_LEVEL)
863 l2cb.l2cap_trace_level = L2CAP_INITIAL_TRACE_LEVEL;
864 #else
865 l2cb.l2cap_trace_level = BT_TRACE_LEVEL_NONE; /* No traces */
866 #endif
867
868 #if L2CAP_CONFORMANCE_TESTING == TRUE
869 /* Conformance testing needs a dynamic response */
870 l2cb.test_info_resp = L2CAP_EXTFEA_SUPPORTED_MASK;
871 #endif
872
873 /* Number of ACL buffers to use for high priority channel */
874 #if (defined(L2CAP_HIGH_PRI_CHAN_QUOTA_IS_CONFIGURABLE) && (L2CAP_HIGH_PRI_CHAN_QUOTA_IS_CONFIGURABLE == TRUE))
875 l2cb.high_pri_min_xmit_quota = L2CAP_HIGH_PRI_MIN_XMIT_QUOTA;
876 #endif
877
878 #if BLE_INCLUDED == TRUE
879 l2cb.l2c_ble_fixed_chnls_mask =
880 L2CAP_FIXED_CHNL_ATT_BIT | L2CAP_FIXED_CHNL_BLE_SIG_BIT | L2CAP_FIXED_CHNL_SMP_BIT;
881 #endif
882
883 l2cb.rcv_pending_q = list_new(NULL);
884 if (l2cb.rcv_pending_q == NULL) {
885 L2CAP_TRACE_ERROR("%s unable to allocate memory for link layer control block", __func__);
886 }
887 #if BLE_INCLUDED == TRUE
888 l2ble_update_att_acl_pkt_num(L2CA_BUFF_INI, NULL);
889 #endif
890 }
l2c_free_p_lcb_pool(void)891 void l2c_free_p_lcb_pool(void)
892 {
893 list_node_t *p_node = NULL;
894 tL2C_LCB *p_lcb = NULL;
895 for (p_node = list_begin(l2cb.p_lcb_pool); p_node; p_node = list_next(p_node)) {
896 p_lcb = list_node(p_node);
897 if (p_lcb) {
898 l2cu_release_lcb (p_lcb);
899 }
900 }
901
902 list_free(l2cb.p_lcb_pool);
903 }
904
l2c_free_p_ccb_pool(void)905 void l2c_free_p_ccb_pool(void)
906 {
907 list_node_t *p_node = NULL;
908 tL2C_CCB *p_ccb = NULL;
909 for (p_node = list_begin(l2cb.p_ccb_pool); p_node; p_node = list_next(p_node)) {
910 p_ccb = list_node(p_node);
911 if (p_ccb) {
912 l2cu_release_ccb (p_ccb);
913 }
914 }
915
916 list_free(l2cb.p_ccb_pool);
917 }
918
l2c_free(void)919 void l2c_free(void)
920 {
921 list_free(l2cb.rcv_pending_q);
922 l2cb.rcv_pending_q = NULL;
923 l2c_free_p_lcb_pool();
924 l2c_free_p_ccb_pool();
925 #if L2C_DYNAMIC_MEMORY
926 FREE_AND_RESET(l2c_cb_ptr);
927 #endif
928 #if BLE_INCLUDED == TRUE
929 l2ble_update_att_acl_pkt_num(L2CA_BUFF_DEINIT, NULL);
930 #endif
931 }
932
933 /*******************************************************************************
934 **
935 ** Function l2c_process_timeout
936 **
937 ** Description This function is called when an L2CAP-related timeout occurs
938 **
939 ** Returns void
940 **
941 *******************************************************************************/
l2c_process_timeout(TIMER_LIST_ENT * p_tle)942 void l2c_process_timeout (TIMER_LIST_ENT *p_tle)
943 {
944 /* What type of timeout ? */
945 switch (p_tle->event) {
946 case BTU_TTYPE_L2CAP_LINK:
947 l2c_link_timeout ((tL2C_LCB *)p_tle->param);
948 break;
949 #if (CLASSIC_BT_INCLUDED == TRUE)
950 case BTU_TTYPE_L2CAP_CHNL:
951 l2c_csm_execute (((tL2C_CCB *)p_tle->param), L2CEVT_TIMEOUT, NULL);
952 break;
953
954 case BTU_TTYPE_L2CAP_FCR_ACK:
955 l2c_csm_execute (((tL2C_CCB *)p_tle->param), L2CEVT_ACK_TIMEOUT, NULL);
956 break;
957 #endif ///CLASSIC_BT_INCLUDED == TRUE
958 case BTU_TTYPE_L2CAP_HOLD:
959 /* Update the timeouts in the hold queue */
960 l2c_process_held_packets(TRUE);
961 break;
962
963 case BTU_TTYPE_L2CAP_INFO:
964 l2c_info_timeout((tL2C_LCB *)p_tle->param);
965 break;
966 case BTU_TTYPE_L2CAP_UPDA_CONN_PARAMS: {
967 #if (BLE_INCLUDED == TRUE)
968 UINT8 status = HCI_ERR_HOST_TIMEOUT;
969 tL2C_LCB *p_lcb = (tL2C_LCB *)p_tle->param;
970 if (p_lcb){
971 p_lcb->conn_update_mask &= ~L2C_BLE_UPDATE_PENDING;
972 p_lcb->conn_update_mask &= ~L2C_BLE_UPDATE_PARAM_FULL;
973 l2c_send_update_conn_params_cb(p_lcb, status);
974 }
975 #endif ///BLE_INCLUDED == TRUE
976 break;
977 }
978 }
979 }
980
981 /*******************************************************************************
982 **
983 ** Function l2c_data_write
984 **
985 ** Description API functions call this function to write data.
986 **
987 ** Returns L2CAP_DW_SUCCESS, if data accepted, else FALSE
988 ** L2CAP_DW_CONGESTED, if data accepted and the channel is congested
989 ** L2CAP_DW_FAILED, if error
990 **
991 *******************************************************************************/
992 #if (CLASSIC_BT_INCLUDED == TRUE)
l2c_data_write(UINT16 cid,BT_HDR * p_data,UINT16 flags)993 UINT8 l2c_data_write (UINT16 cid, BT_HDR *p_data, UINT16 flags)
994 {
995 tL2C_CCB *p_ccb;
996
997 /* Find the channel control block. We don't know the link it is on. */
998 if ((p_ccb = l2cu_find_ccb_by_cid (NULL, cid)) == NULL) {
999 L2CAP_TRACE_WARNING ("L2CAP - no CCB for L2CA_DataWrite, CID: %d", cid);
1000 osi_free (p_data);
1001 return (L2CAP_DW_FAILED);
1002 }
1003
1004 #ifndef TESTER /* Tester may send any amount of data. otherwise sending message
1005 bigger than mtu size of peer is a violation of protocol */
1006 if (p_data->len > p_ccb->peer_cfg.mtu) {
1007 L2CAP_TRACE_WARNING ("L2CAP - CID: 0x%04x cannot send message bigger than peer's mtu size", cid);
1008 osi_free (p_data);
1009 return (L2CAP_DW_FAILED);
1010 }
1011 #endif
1012
1013 /* channel based, packet based flushable or non-flushable */
1014 p_data->layer_specific = flags;
1015
1016 /* If already congested, do not accept any more packets */
1017 if (p_ccb->cong_sent) {
1018 L2CAP_TRACE_DEBUG ("L2CAP - CID: 0x%04x cannot send, already congested xmit_hold_q.count: %u buff_quota: %u",
1019 p_ccb->local_cid,
1020 fixed_queue_length(p_ccb->xmit_hold_q),
1021 p_ccb->buff_quota);
1022
1023 osi_free (p_data);
1024 return (L2CAP_DW_FAILED);
1025 }
1026
1027 //counter_add("l2cap.dyn.tx.bytes", p_data->len);
1028 //counter_add("l2cap.dyn.tx.pkts", 1);
1029
1030 l2c_csm_execute (p_ccb, L2CEVT_L2CA_DATA_WRITE, p_data);
1031
1032 if (p_ccb->cong_sent) {
1033 return (L2CAP_DW_CONGESTED);
1034 }
1035 return (L2CAP_DW_SUCCESS);
1036 }
1037 #endif ///CLASSIC_BT_INCLUDED == TRUE
1038