1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
79
80
81
82
83
84
85
86
87
88
89
90
91
92
97
98
99
100
101
102
103
104
105
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
186
187
188
193
194
195
196
201
202
203
204
205
206
207
208
209
210
211
218
219
220
227
228
236
237
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
267
271
272
273
280
281
282
283
284
285
286
287
288
289
303
304
305
306
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
378
379
380
388
389
390
391
392
393
394
395
396
397
400
401
402
403
404
405
406
411
412
413
414
415
423
424
425
426
427
433
434
435
436
437
438
439
440
441
445
446
447
448
449
450
451
452
453
454
455
456
459
460
461
462
463
464
465
476
481
482
483
484
485
486
487
492
493
494
495
496
497
498
499
500
505
510
511
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
544
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
575
576
577
578
579
580
581
582
583
584
585
600
601
602
603
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
635
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
666
667
668
669
670
671
672
673
674
675
676
677
678
692
693
694
695
696
697
698
699
707
708
709
710
721
725
726
727
728
729
730
731
732
733
734
735
736
741
742
743
747
748
749
750
751
752
753
754
766
767
768
769
777
778
784
785
786
787
790
791
792
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
820
821
827
828
829
838
839
840
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
869
870
873
874
875
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
899
900
901
902
903
904
905
906
907
909
910
911
912
915
916
917
918
919
920
921
922
923
929
930
931
932
933
934
935
936
937
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1031
1032
1034
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1063
1064
1065
/* ... */
/* ... */
#include <stdlib.h>
#include <string.h>
#include "device/controller.h"
#include "common/bt_target.h"
#include "btm_int.h"
#include "stack/btu.h"
#include "stack/hcimsgs.h"
#include "stack/l2c_api.h"
#include "l2c_int.h"
#include "stack/l2cdefs.h"10 includes
#if (CLASSIC_BT_INCLUDED == TRUE)
static void process_l2cap_cmd (tL2C_LCB *p_lcb, UINT8 *p, UINT16 pkt_len);
#endif ...
#if L2C_DYNAMIC_MEMORY == FALSE
tL2C_CB l2cb;
#else
tL2C_CB *l2c_cb_ptr;
#endif
#if BT_CLASSIC_BQB_INCLUDED
static BOOLEAN s_l2cap_bqb_bad_cmd_len_rej_flag = FALSE;
#endif
#if 0
/* ... */
void l2c_bcst_msg( BT_HDR *p_buf, UINT16 psm )
{
UINT8 *p;
if (p_buf->offset < L2CAP_BCST_MIN_OFFSET) {
L2CAP_TRACE_ERROR ("L2CAP - cannot send buffer, offset: %d", p_buf->offset);
osi_free (p_buf);
return;
}{...}
p_buf->offset -= (HCI_DATA_PREAMBLE_SIZE + L2CAP_PKT_OVERHEAD + L2CAP_BCST_OVERHEAD);
p_buf->len += L2CAP_PKT_OVERHEAD + L2CAP_BCST_OVERHEAD;
p = (UINT8 *)(p_buf + 1) + p_buf->offset;
UINT16_TO_STREAM (p, 0x0050 | (L2CAP_PKT_START << 12) | (2 << 14));
uint16_t acl_data_size = controller_get_interface()->get_acl_data_size_classic();
if (p_buf->len > acl_data_size) {
UINT16_TO_STREAM (p, acl_data_size);
}{...} else {
UINT16_TO_STREAM (p, p_buf->len);
}{...}
UINT16_TO_STREAM (p, p_buf->len - L2CAP_PKT_OVERHEAD);
UINT16_TO_STREAM (p, L2CAP_CONNECTIONLESS_CID);
UINT16_TO_STREAM (p, psm);
p_buf->len += HCI_DATA_PREAMBLE_SIZE;
if (p_buf->len <= controller_get_interface()->get_acl_packet_size_classic()) {
bte_main_hci_send(p_buf, BT_EVT_TO_LM_HCI_ACL);
}{...}
}{...}
/* ... */#endif
/* ... */
#if BT_CLASSIC_BQB_INCLUDED
void l2cap_bqb_bad_cmd_len_rej_ctrl(BOOLEAN enable)
{
s_l2cap_bqb_bad_cmd_len_rej_flag = enable;
}{...}
/* ... */#endif
/* ... */
void l2c_rcv_acl_data (BT_HDR *p_msg)
{
UINT8 *p = (UINT8 *)(p_msg + 1) + p_msg->offset;
UINT16 handle, hci_len;
UINT8 pkt_type;
tL2C_LCB *p_lcb;
tL2C_CCB *p_ccb = NULL;
UINT16 l2cap_len, rcv_cid;
#if (!CONFIG_BT_STACK_NO_LOG)
UINT16 psm;
#endif
UINT16 credit;
STREAM_TO_UINT16 (handle, p);
pkt_type = HCID_GET_EVENT (handle);
handle = HCID_GET_HANDLE (handle);
if (pkt_type != L2CAP_PKT_CONTINUE) {
if ((p_lcb = l2cu_find_lcb_by_handle (handle)) == NULL) {
UINT8 cmd_code;
STREAM_TO_UINT16 (hci_len, p);
STREAM_TO_UINT16 (l2cap_len, p);
STREAM_TO_UINT16 (rcv_cid, p);
STREAM_TO_UINT8 (cmd_code, p);
if ((p_msg->layer_specific == 0) && (rcv_cid == L2CAP_SIGNALLING_CID)
&& (cmd_code == L2CAP_CMD_INFO_REQ || cmd_code == L2CAP_CMD_CONN_REQ)) {
L2CAP_TRACE_WARNING ("L2CAP - holding ACL for unknown handle:%d ls:%d"
" cid:%d opcode:%d cur count:%d", handle, p_msg->layer_specific,
rcv_cid, cmd_code, list_length(l2cb.rcv_pending_q));
p_msg->layer_specific = 2;
list_append(l2cb.rcv_pending_q, p_msg);
if (list_length(l2cb.rcv_pending_q) == 1) {
btu_start_timer (&l2cb.rcv_hold_tle, BTU_TTYPE_L2CAP_HOLD, BT_1SEC_TIMEOUT);
}{...}
return;
}{...} else {
L2CAP_TRACE_ERROR ("L2CAP - rcvd ACL for unknown handle:%d ls:%d cid:%d"
" opcode:%d cur count:%d", handle, p_msg->layer_specific, rcv_cid,
cmd_code, list_length(l2cb.rcv_pending_q));
}{...}
osi_free (p_msg);
return;
}{...}
}{...} else {
L2CAP_TRACE_WARNING ("L2CAP - expected pkt start or complete, got: %d", pkt_type);
osi_free (p_msg);
return;
}{...}
STREAM_TO_UINT16 (hci_len, p);
p_msg->offset += 4;
STREAM_TO_UINT16 (l2cap_len, p);
STREAM_TO_UINT16 (rcv_cid, p);
#if BLE_INCLUDED == TRUE
if (p_lcb && p_lcb->transport == BT_TRANSPORT_LE && p_lcb->link_state != LST_DISCONNECTING)
{
l2cble_notify_le_connection(p_lcb->remote_bd_addr);
}{...}
/* ... */#endif
L2CAP_TRACE_DEBUG ("L2CAP - rcv_cid CID: 0x%04x\n", rcv_cid);
if (rcv_cid >= L2CAP_BASE_APPL_CID) {
if ((p_ccb = l2cu_find_ccb_by_cid (p_lcb, rcv_cid)) == NULL) {
L2CAP_TRACE_WARNING ("L2CAP - unknown CID: 0x%04x", rcv_cid);
osi_free (p_msg);
return;
}{...}
}{...}
if (hci_len >= L2CAP_PKT_OVERHEAD) {
p_msg->len = hci_len - L2CAP_PKT_OVERHEAD;
p_msg->offset += L2CAP_PKT_OVERHEAD;
}{...} else {
L2CAP_TRACE_WARNING ("L2CAP - got incorrect hci header" );
osi_free (p_msg);
return;
}{...}
if (l2cap_len != p_msg->len) {
L2CAP_TRACE_WARNING ("L2CAP - bad length in pkt. Exp: %d Act: %d",
l2cap_len, p_msg->len);
osi_free (p_msg);
return;
}{...}
if (rcv_cid == L2CAP_SIGNALLING_CID) {
#if (CLASSIC_BT_INCLUDED == TRUE)
process_l2cap_cmd (p_lcb, p, l2cap_len);
#endif
osi_free (p_msg);
}{...} else if (rcv_cid == L2CAP_CONNECTIONLESS_CID) {
#if !CONFIG_BT_STACK_NO_LOG
STREAM_TO_UINT16 (psm, p);
#endif
L2CAP_TRACE_DEBUG( "GOT CONNECTIONLESS DATA PSM:%d", psm ) ;
#if (L2CAP_UCD_INCLUDED == TRUE)
if ( l2c_ucd_check_rx_pkts( p_lcb, p_msg ) ) {
}{...} else
#endif
{
osi_free (p_msg);
}{...}
}{...}
#if (BLE_INCLUDED == TRUE)
else if (rcv_cid == L2CAP_BLE_SIGNALLING_CID) {
l2cble_process_sig_cmd (p_lcb, p, l2cap_len);
osi_free (p_msg);
}{...}
#endif/* ... */
#if (L2CAP_NUM_FIXED_CHNLS > 0)
else if ((rcv_cid >= L2CAP_FIRST_FIXED_CHNL) && (rcv_cid <= L2CAP_LAST_FIXED_CHNL) &&
(l2cb.fixed_reg[rcv_cid - L2CAP_FIRST_FIXED_CHNL].pL2CA_FixedData_Cb != NULL) ) {
if (p_lcb &&
(p_lcb->link_state != LST_DISCONNECTING) &&
l2cu_initialize_fixed_ccb (p_lcb, rcv_cid, &l2cb.fixed_reg[rcv_cid - L2CAP_FIRST_FIXED_CHNL].fixed_chnl_opts)) {
p_ccb = p_lcb->p_fixed_ccbs[rcv_cid - L2CAP_FIRST_FIXED_CHNL];
if (p_ccb->peer_cfg.fcr.mode != L2CAP_FCR_BASIC_MODE) {
#if (CLASSIC_BT_INCLUDED == TRUE)
l2c_fcr_proc_pdu (p_ccb, p_msg);
#endif
}{...} else {
(*l2cb.fixed_reg[rcv_cid - L2CAP_FIRST_FIXED_CHNL].pL2CA_FixedData_Cb)
(rcv_cid, p_lcb->remote_bd_addr, p_msg);
}{...}
}{...} else {
osi_free (p_msg);
}{...}
}{...}
#endif/* ... */
else {
if (p_ccb == NULL) {
osi_free (p_msg);
}{...} else {
if (p_lcb->transport == BT_TRANSPORT_LE) {
credit = L2CAP_LE_DEFAULT_CREDIT;
L2CAP_TRACE_DEBUG("%s Credits received %d",__func__, credit);
if((p_ccb->peer_conn_cfg.credits + credit) > L2CAP_LE_MAX_CREDIT) {
/* ... */
#if (BLE_INCLUDED == TRUE)
l2cble_send_peer_disc_req (p_ccb);
#endif
}{...} else {
p_ccb->peer_conn_cfg.credits += credit;
l2c_link_check_send_pkts (p_ccb->p_lcb, NULL, NULL);
}{...}
}{...}
if (p_ccb->peer_cfg.fcr.mode == L2CAP_FCR_BASIC_MODE) {
#if (CLASSIC_BT_INCLUDED == TRUE)
l2c_csm_execute (p_ccb, L2CEVT_L2CAP_DATA, p_msg);
#endif
}{...} else {
if ((p_ccb->chnl_state == CST_OPEN) || (p_ccb->chnl_state == CST_CONFIG)) {
#if (CLASSIC_BT_INCLUDED == TRUE)
l2c_fcr_proc_pdu (p_ccb, p_msg);
#endif
}{...} else {
osi_free (p_msg);
}{...}
}{...}
}{...}
}{...}
}{ ... }
/* ... */
#if (CLASSIC_BT_INCLUDED == TRUE)
static void process_l2cap_cmd (tL2C_LCB *p_lcb, UINT8 *p, UINT16 pkt_len)
{
UINT8 *p_pkt_end, *p_next_cmd, *p_cfg_end, *p_cfg_start;
UINT8 cmd_code, cfg_code, cfg_len, id;
tL2C_CONN_INFO con_info;
tL2CAP_CFG_INFO cfg_info;
UINT16 rej_reason, rej_mtu, lcid, rcid, info_type;
tL2C_CCB *p_ccb;
tL2C_RCB *p_rcb;
BOOLEAN cfg_rej, pkt_size_rej = FALSE;
UINT16 cfg_rej_len, cmd_len;
UINT16 result;
tL2C_CONN_INFO ci;
#if (defined BLE_INCLUDED && BLE_INCLUDED == TRUE)
if (p_lcb->transport == BT_TRANSPORT_LE) {
return;
}{...}
/* ... */#endif
if (pkt_len > L2CAP_DEFAULT_MTU) {
/* ... */
pkt_size_rej = TRUE;
L2CAP_TRACE_ERROR ("L2CAP SIG MTU Pkt Len Exceeded (672) -> pkt_len: %d", pkt_len);
}{...}
p_next_cmd = p;
p_pkt_end = p + pkt_len;
memset (&cfg_info, 0, sizeof(cfg_info));
while (TRUE) {
if ((p = p_next_cmd) > (p_pkt_end - 4)) {
break;
}{...}
STREAM_TO_UINT8 (cmd_code, p);
STREAM_TO_UINT8 (id, p);
STREAM_TO_UINT16 (cmd_len, p);
if ((p_next_cmd = p + cmd_len) > p_pkt_end) {
L2CAP_TRACE_WARNING ("Command len bad pkt_len: %d cmd_len: %d code: %d",
pkt_len, cmd_len, cmd_code);
break;
}{...}
L2CAP_TRACE_DEBUG ("cmd_code: %d, id:%d, cmd_len:%d", cmd_code, id, cmd_len);
if (pkt_size_rej) {
if (l2c_is_cmd_rejected(cmd_code, id, p_lcb)) {
return;
}{...} else {
continue;
}{...}
}{...}
switch (cmd_code) {
case L2CAP_CMD_REJECT:
STREAM_TO_UINT16 (rej_reason, p);
if (rej_reason == L2CAP_CMD_REJ_MTU_EXCEEDED) {
STREAM_TO_UINT16 (rej_mtu, p);
L2CAP_TRACE_WARNING ("L2CAP - MTU rej Handle: %d MTU: %d", p_lcb->handle, rej_mtu);
}{...}
if (rej_reason == L2CAP_CMD_REJ_INVALID_CID) {
STREAM_TO_UINT16 (rcid, p);
STREAM_TO_UINT16 (lcid, p);
L2CAP_TRACE_WARNING ("L2CAP - rej with CID invalid, LCID: 0x%04x RCID: 0x%04x", lcid, rcid);
if (((p_ccb = l2cu_find_ccb_by_cid (p_lcb, lcid)) != NULL)
&& (p_ccb->remote_cid == rcid)) {
l2c_csm_execute (p_ccb, L2CEVT_LP_DISCONNECT_IND, NULL);
}{...}
}{...}
else if (rej_reason == L2CAP_CMD_REJ_NOT_UNDERSTOOD && p_lcb->w4_info_rsp) {
btu_stop_timer (&p_lcb->info_timer_entry);
p_lcb->w4_info_rsp = FALSE;
ci.status = HCI_SUCCESS;
memcpy (ci.bd_addr, p_lcb->remote_bd_addr, sizeof(BD_ADDR));
for (p_ccb = p_lcb->ccb_queue.p_first_ccb; p_ccb; p_ccb = p_ccb->p_next_ccb) {
l2c_csm_execute (p_ccb, L2CEVT_L2CAP_INFO_RSP, &ci);
}{...}
}{...}
break;
...
case L2CAP_CMD_CONN_REQ:
STREAM_TO_UINT16 (con_info.psm, p);
STREAM_TO_UINT16 (rcid, p);
if ((p_rcb = l2cu_find_rcb_by_psm (con_info.psm)) == NULL) {
L2CAP_TRACE_WARNING ("L2CAP - rcvd conn req for unknown PSM: %d", con_info.psm);
l2cu_reject_connection (p_lcb, rcid, id, L2CAP_CONN_NO_PSM);
break;
}{...} else {
if (!p_rcb->api.pL2CA_ConnectInd_Cb) {
L2CAP_TRACE_WARNING ("L2CAP - rcvd conn req for outgoing-only connection PSM: %d", con_info.psm);
l2cu_reject_connection (p_lcb, rcid, id, L2CAP_CONN_NO_PSM);
break;
}{...}
}{...}
if ((p_ccb = l2cu_allocate_ccb (p_lcb, 0)) == NULL) {
L2CAP_TRACE_ERROR ("L2CAP - unable to allocate CCB");
l2cu_reject_connection (p_lcb, rcid, id, L2CAP_CONN_NO_RESOURCES);
break;
}{...}
p_ccb->remote_id = id;
p_ccb->p_rcb = p_rcb;
p_ccb->remote_cid = rcid;
l2c_csm_execute(p_ccb, L2CEVT_L2CAP_CONNECT_REQ, &con_info);
#if BT_CLASSIC_BQB_INCLUDED
if (s_l2cap_bqb_bad_cmd_len_rej_flag) {
l2cu_send_peer_cmd_reject (p_lcb, L2CAP_CMD_REJ_NOT_UNDERSTOOD, id, 0, 0);
}{...}
/* ... */#endif
break;
...
case L2CAP_CMD_CONN_RSP:
STREAM_TO_UINT16 (con_info.remote_cid, p);
STREAM_TO_UINT16 (lcid, p);
STREAM_TO_UINT16 (con_info.l2cap_result, p);
STREAM_TO_UINT16 (con_info.l2cap_status, p);
if ((p_ccb = l2cu_find_ccb_by_cid (p_lcb, lcid)) == NULL) {
L2CAP_TRACE_WARNING ("L2CAP - no CCB for conn rsp, LCID: %d RCID: %d",
lcid, con_info.remote_cid);
break;
}{...}
if (p_ccb->local_id != id) {
L2CAP_TRACE_WARNING ("L2CAP - con rsp - bad ID. Exp: %d Got: %d",
p_ccb->local_id, id);
break;
}{...}
if (con_info.l2cap_result == L2CAP_CONN_OK) {
l2c_csm_execute(p_ccb, L2CEVT_L2CAP_CONNECT_RSP, &con_info);
}{...} else if (con_info.l2cap_result == L2CAP_CONN_PENDING) {
l2c_csm_execute(p_ccb, L2CEVT_L2CAP_CONNECT_RSP_PND, &con_info);
}{...} else {
l2c_csm_execute(p_ccb, L2CEVT_L2CAP_CONNECT_RSP_NEG, &con_info);
}{...}
break;
...
case L2CAP_CMD_CONFIG_REQ:
p_cfg_end = p + cmd_len;
cfg_rej = FALSE;
cfg_rej_len = 0;
STREAM_TO_UINT16 (lcid, p);
STREAM_TO_UINT16 (cfg_info.flags, p);
p_cfg_start = p;
cfg_info.flush_to_present = cfg_info.mtu_present = cfg_info.qos_present =
cfg_info.fcr_present = cfg_info.fcs_present = FALSE;
while (p < p_cfg_end) {
STREAM_TO_UINT8 (cfg_code, p);
STREAM_TO_UINT8 (cfg_len, p);
switch (cfg_code & 0x7F) {
case L2CAP_CFG_TYPE_MTU:
cfg_info.mtu_present = TRUE;
STREAM_TO_UINT16 (cfg_info.mtu, p);
break;
...
case L2CAP_CFG_TYPE_FLUSH_TOUT:
cfg_info.flush_to_present = TRUE;
STREAM_TO_UINT16 (cfg_info.flush_to, p);
break;
...
case L2CAP_CFG_TYPE_QOS:
cfg_info.qos_present = TRUE;
STREAM_TO_UINT8 (cfg_info.qos.qos_flags, p);
STREAM_TO_UINT8 (cfg_info.qos.service_type, p);
STREAM_TO_UINT32 (cfg_info.qos.token_rate, p);
STREAM_TO_UINT32 (cfg_info.qos.token_bucket_size, p);
STREAM_TO_UINT32 (cfg_info.qos.peak_bandwidth, p);
STREAM_TO_UINT32 (cfg_info.qos.latency, p);
STREAM_TO_UINT32 (cfg_info.qos.delay_variation, p);
break;
...
case L2CAP_CFG_TYPE_FCR:
cfg_info.fcr_present = TRUE;
STREAM_TO_UINT8 (cfg_info.fcr.mode, p);
STREAM_TO_UINT8 (cfg_info.fcr.tx_win_sz, p);
STREAM_TO_UINT8 (cfg_info.fcr.max_transmit, p);
STREAM_TO_UINT16 (cfg_info.fcr.rtrans_tout, p);
STREAM_TO_UINT16 (cfg_info.fcr.mon_tout, p);
STREAM_TO_UINT16 (cfg_info.fcr.mps, p);
break;
...
case L2CAP_CFG_TYPE_FCS:
cfg_info.fcs_present = TRUE;
STREAM_TO_UINT8 (cfg_info.fcs, p);
break;
...
case L2CAP_CFG_TYPE_EXT_FLOW:
cfg_info.ext_flow_spec_present = TRUE;
STREAM_TO_UINT8 (cfg_info.ext_flow_spec.id, p);
STREAM_TO_UINT8 (cfg_info.ext_flow_spec.stype, p);
STREAM_TO_UINT16 (cfg_info.ext_flow_spec.max_sdu_size, p);
STREAM_TO_UINT32 (cfg_info.ext_flow_spec.sdu_inter_time, p);
STREAM_TO_UINT32 (cfg_info.ext_flow_spec.access_latency, p);
STREAM_TO_UINT32 (cfg_info.ext_flow_spec.flush_timeout, p);
break;
...
default:
if ((cfg_len + L2CAP_CFG_OPTION_OVERHEAD) <= cmd_len) {
p += cfg_len;
if ((cfg_code & 0x80) == 0) {
cfg_rej_len += cfg_len + L2CAP_CFG_OPTION_OVERHEAD;
cfg_rej = TRUE;
}{...}
}{...}
else {
p = p_cfg_end;
cfg_rej = TRUE;
}{...}
break;...
}{...}
}{...}
if ((p_ccb = l2cu_find_ccb_by_cid (p_lcb, lcid)) != NULL) {
p_ccb->remote_id = id;
if (cfg_rej) {
l2cu_send_peer_config_rej (p_ccb, p_cfg_start, (UINT16) (cmd_len - L2CAP_CONFIG_REQ_LEN), cfg_rej_len);
}{...} else {
l2c_csm_execute (p_ccb, L2CEVT_L2CAP_CONFIG_REQ, &cfg_info);
}{...}
}{...} else {
l2cu_send_peer_cmd_reject (p_lcb, L2CAP_CMD_REJ_INVALID_CID, id, 0, 0);
}{...}
break;
...
case L2CAP_CMD_CONFIG_RSP:
p_cfg_end = p + cmd_len;
STREAM_TO_UINT16 (lcid, p);
STREAM_TO_UINT16 (cfg_info.flags, p);
STREAM_TO_UINT16 (cfg_info.result, p);
cfg_info.flush_to_present = cfg_info.mtu_present = cfg_info.qos_present =
cfg_info.fcr_present = cfg_info.fcs_present = FALSE;
while (p < p_cfg_end) {
STREAM_TO_UINT8 (cfg_code, p);
STREAM_TO_UINT8 (cfg_len, p);
switch (cfg_code & 0x7F) {
case L2CAP_CFG_TYPE_MTU:
cfg_info.mtu_present = TRUE;
STREAM_TO_UINT16 (cfg_info.mtu, p);
break;
...
case L2CAP_CFG_TYPE_FLUSH_TOUT:
cfg_info.flush_to_present = TRUE;
STREAM_TO_UINT16 (cfg_info.flush_to, p);
break;
...
case L2CAP_CFG_TYPE_QOS:
cfg_info.qos_present = TRUE;
STREAM_TO_UINT8 (cfg_info.qos.qos_flags, p);
STREAM_TO_UINT8 (cfg_info.qos.service_type, p);
STREAM_TO_UINT32 (cfg_info.qos.token_rate, p);
STREAM_TO_UINT32 (cfg_info.qos.token_bucket_size, p);
STREAM_TO_UINT32 (cfg_info.qos.peak_bandwidth, p);
STREAM_TO_UINT32 (cfg_info.qos.latency, p);
STREAM_TO_UINT32 (cfg_info.qos.delay_variation, p);
break;
...
case L2CAP_CFG_TYPE_FCR:
cfg_info.fcr_present = TRUE;
STREAM_TO_UINT8 (cfg_info.fcr.mode, p);
STREAM_TO_UINT8 (cfg_info.fcr.tx_win_sz, p);
STREAM_TO_UINT8 (cfg_info.fcr.max_transmit, p);
STREAM_TO_UINT16 (cfg_info.fcr.rtrans_tout, p);
STREAM_TO_UINT16 (cfg_info.fcr.mon_tout, p);
STREAM_TO_UINT16 (cfg_info.fcr.mps, p);
break;
...
case L2CAP_CFG_TYPE_FCS:
cfg_info.fcs_present = TRUE;
STREAM_TO_UINT8 (cfg_info.fcs, p);
break;
...
case L2CAP_CFG_TYPE_EXT_FLOW:
cfg_info.ext_flow_spec_present = TRUE;
STREAM_TO_UINT8 (cfg_info.ext_flow_spec.id, p);
STREAM_TO_UINT8 (cfg_info.ext_flow_spec.stype, p);
STREAM_TO_UINT16 (cfg_info.ext_flow_spec.max_sdu_size, p);
STREAM_TO_UINT32 (cfg_info.ext_flow_spec.sdu_inter_time, p);
STREAM_TO_UINT32 (cfg_info.ext_flow_spec.access_latency, p);
STREAM_TO_UINT32 (cfg_info.ext_flow_spec.flush_timeout, p);
break;...
}{...}
}{...}
if ((p_ccb = l2cu_find_ccb_by_cid (p_lcb, lcid)) != NULL) {
if (p_ccb->local_id != id) {
L2CAP_TRACE_WARNING ("L2CAP - cfg rsp - bad ID. Exp: %d Got: %d",
p_ccb->local_id, id);
break;
}{...}
if ( (cfg_info.result == L2CAP_CFG_OK) || (cfg_info.result == L2CAP_CFG_PENDING) ) {
l2c_csm_execute (p_ccb, L2CEVT_L2CAP_CONFIG_RSP, &cfg_info);
}{...} else {
l2c_csm_execute (p_ccb, L2CEVT_L2CAP_CONFIG_RSP_NEG, &cfg_info);
}{...}
}{...} else {
L2CAP_TRACE_WARNING ("L2CAP - rcvd cfg rsp for unknown CID: 0x%04x", lcid);
}{...}
break;
...
case L2CAP_CMD_DISC_REQ:
STREAM_TO_UINT16 (lcid, p);
STREAM_TO_UINT16 (rcid, p);
if ((p_ccb = l2cu_find_ccb_by_cid (p_lcb, lcid)) != NULL) {
if (p_ccb->remote_cid == rcid) {
p_ccb->remote_id = id;
l2c_csm_execute (p_ccb, L2CEVT_L2CAP_DISCONNECT_REQ, &con_info);
}{...}
}{...} else {
l2cu_send_peer_disc_rsp (p_lcb, id, lcid, rcid);
}{...}
break;
...
case L2CAP_CMD_DISC_RSP:
STREAM_TO_UINT16 (rcid, p);
STREAM_TO_UINT16 (lcid, p);
if ((p_ccb = l2cu_find_ccb_by_cid (p_lcb, lcid)) != NULL) {
if ((p_ccb->remote_cid == rcid) && (p_ccb->local_id == id)) {
l2c_csm_execute (p_ccb, L2CEVT_L2CAP_DISCONNECT_RSP, &con_info);
}{...}
}{...}
break;
...
case L2CAP_CMD_ECHO_REQ:
l2cu_send_peer_echo_rsp (p_lcb, id, NULL, 0);
break;
...
case L2CAP_CMD_ECHO_RSP:
if (p_lcb->p_echo_rsp_cb) {
tL2CA_ECHO_RSP_CB *p_cb = p_lcb->p_echo_rsp_cb;
p_lcb->p_echo_rsp_cb = NULL;
(*p_cb) (L2CAP_PING_RESULT_OK);
}{...}
break;
...
case L2CAP_CMD_INFO_REQ:
STREAM_TO_UINT16 (info_type, p);
l2cu_send_peer_info_rsp (p_lcb, id, info_type);
break;
...
case L2CAP_CMD_INFO_RSP:
if (p_lcb->w4_info_rsp) {
btu_stop_timer (&p_lcb->info_timer_entry);
p_lcb->w4_info_rsp = FALSE;
}{...}
STREAM_TO_UINT16 (info_type, p);
STREAM_TO_UINT16 (result, p);
p_lcb->info_rx_bits |= (1 << info_type);
if ( (info_type == L2CAP_EXTENDED_FEATURES_INFO_TYPE)
&& (result == L2CAP_INFO_RESP_RESULT_SUCCESS) ) {
STREAM_TO_UINT32( p_lcb->peer_ext_fea, p );
#if (L2CAP_NUM_FIXED_CHNLS > 0)
if (p_lcb->peer_ext_fea & L2CAP_EXTFEA_FIXED_CHNLS) {
l2cu_send_peer_info_req (p_lcb, L2CAP_FIXED_CHANNELS_INFO_TYPE);
break;
}{...} else {
l2cu_process_fixed_chnl_resp (p_lcb);
}{...}
/* ... */#endif
}{...}
#if (L2CAP_NUM_FIXED_CHNLS > 0)
if (info_type == L2CAP_FIXED_CHANNELS_INFO_TYPE) {
if (result == L2CAP_INFO_RESP_RESULT_SUCCESS) {
memcpy (p_lcb->peer_chnl_mask, p, L2CAP_FIXED_CHNL_ARRAY_SIZE);
}{...}
l2cu_process_fixed_chnl_resp (p_lcb);
}{...}
/* ... */#endif
#if (L2CAP_UCD_INCLUDED == TRUE)
else if (info_type == L2CAP_CONNLESS_MTU_INFO_TYPE) {
if (result == L2CAP_INFO_RESP_RESULT_SUCCESS) {
STREAM_TO_UINT16 (p_lcb->ucd_mtu, p);
}{...}
}{...}
/* ... */#endif
ci.status = HCI_SUCCESS;
memcpy (ci.bd_addr, p_lcb->remote_bd_addr, sizeof(BD_ADDR));
for (p_ccb = p_lcb->ccb_queue.p_first_ccb; p_ccb; p_ccb = p_ccb->p_next_ccb) {
l2c_csm_execute (p_ccb, L2CEVT_L2CAP_INFO_RSP, &ci);
}{...}
break;
...
default:
L2CAP_TRACE_WARNING ("L2CAP - bad cmd code: %d", cmd_code);
l2cu_send_peer_cmd_reject (p_lcb, L2CAP_CMD_REJ_NOT_UNDERSTOOD, id, 0, 0);
return;...
}{...}
}{...}
UNUSED(rej_mtu);
}{...}
/* ... */#endif
/* ... */
void l2c_process_held_packets(BOOLEAN timed_out)
{
if (list_is_empty(l2cb.rcv_pending_q)) {
return;
}{...}
if (!timed_out) {
btu_stop_timer(&l2cb.rcv_hold_tle);
L2CAP_TRACE_WARNING("L2CAP HOLD CONTINUE");
}{...} else {
L2CAP_TRACE_WARNING("L2CAP HOLD TIMEOUT");
}{...}
for (const list_node_t *node = list_begin(l2cb.rcv_pending_q);
node != list_end(l2cb.rcv_pending_q);) {
BT_HDR *p_buf = list_node(node);
node = list_next(node);
if (!timed_out || (!p_buf->layer_specific) || (--p_buf->layer_specific == 0)) {
list_remove(l2cb.rcv_pending_q, p_buf);
p_buf->layer_specific = 0xFFFF;
l2c_rcv_acl_data(p_buf);
}{...}
}{...}
if (!list_is_empty(l2cb.rcv_pending_q)) {
btu_start_timer (&l2cb.rcv_hold_tle, BTU_TTYPE_L2CAP_HOLD, BT_1SEC_TIMEOUT);
}{...}
}{ ... }
/* ... */
void l2c_init (void)
{
#if L2C_DYNAMIC_MEMORY
l2c_cb_ptr = (tL2C_CB *)osi_malloc(sizeof(tL2C_CB));
#endif
memset (&l2cb, 0, sizeof (tL2C_CB));
l2cb.dyn_psm = 0xFFF;
l2cb.p_ccb_pool = list_new(osi_free_func);
if (l2cb.p_ccb_pool == NULL) {
L2CAP_TRACE_ERROR("%s unable to allocate memory for L2CAP channel control block", __func__);
}{...}
l2cb.p_lcb_pool = list_new(osi_free_func);
if (l2cb.p_lcb_pool == NULL) {
L2CAP_TRACE_ERROR("%s unable to allocate memory for L2CAP Link control block", __func__);
}{...}
#if (L2CAP_NON_FLUSHABLE_PB_INCLUDED == TRUE)
l2cb.non_flushable_pbf = L2CAP_PKT_START << L2CAP_PKT_TYPE_SHIFT;/* ... */
#endif
#ifdef L2CAP_DESIRED_LINK_ROLE
l2cb.desire_role = L2CAP_DESIRED_LINK_ROLE;
#else
l2cb.desire_role = HCI_ROLE_SLAVE;
#endif
l2cb.idle_timeout = L2CAP_LINK_INACTIVITY_TOUT;
#if defined(L2CAP_INITIAL_TRACE_LEVEL)
l2cb.l2cap_trace_level = L2CAP_INITIAL_TRACE_LEVEL;
#else
l2cb.l2cap_trace_level = BT_TRACE_LEVEL_NONE;
#endif
#if L2CAP_CONFORMANCE_TESTING == TRUE
l2cb.test_info_resp = L2CAP_EXTFEA_SUPPORTED_MASK;/* ... */
#endif
#if (defined(L2CAP_HIGH_PRI_CHAN_QUOTA_IS_CONFIGURABLE) && (L2CAP_HIGH_PRI_CHAN_QUOTA_IS_CONFIGURABLE == TRUE))
l2cb.high_pri_min_xmit_quota = L2CAP_HIGH_PRI_MIN_XMIT_QUOTA;
#endif
#if BLE_INCLUDED == TRUE
l2cb.l2c_ble_fixed_chnls_mask =
L2CAP_FIXED_CHNL_ATT_BIT | L2CAP_FIXED_CHNL_BLE_SIG_BIT | L2CAP_FIXED_CHNL_SMP_BIT;/* ... */
#endif
l2cb.rcv_pending_q = list_new(NULL);
if (l2cb.rcv_pending_q == NULL) {
L2CAP_TRACE_ERROR("%s unable to allocate memory for link layer control block", __func__);
}{...}
#if BLE_INCLUDED == TRUE
l2ble_update_att_acl_pkt_num(L2CA_BUFF_INI, NULL);
#endif
}{ ... }
void l2c_free_p_lcb_pool(void)
{
list_node_t *p_node = NULL;
tL2C_LCB *p_lcb = NULL;
for (p_node = list_begin(l2cb.p_lcb_pool); p_node; p_node = list_next(p_node)) {
p_lcb = list_node(p_node);
if (p_lcb) {
l2cu_release_lcb (p_lcb);
}{...}
}{...}
list_free(l2cb.p_lcb_pool);
}{ ... }
void l2c_free_p_ccb_pool(void)
{
list_node_t *p_node = NULL;
tL2C_CCB *p_ccb = NULL;
for (p_node = list_begin(l2cb.p_ccb_pool); p_node; p_node = list_next(p_node)) {
p_ccb = list_node(p_node);
if (p_ccb) {
l2cu_release_ccb (p_ccb);
}{...}
}{...}
list_free(l2cb.p_ccb_pool);
}{ ... }
void l2c_free(void)
{
list_free(l2cb.rcv_pending_q);
l2cb.rcv_pending_q = NULL;
l2c_free_p_lcb_pool();
l2c_free_p_ccb_pool();
#if L2C_DYNAMIC_MEMORY
FREE_AND_RESET(l2c_cb_ptr);
#endif
#if BLE_INCLUDED == TRUE
l2ble_update_att_acl_pkt_num(L2CA_BUFF_DEINIT, NULL);
#endif
}{ ... }
/* ... */
void l2c_process_timeout (TIMER_LIST_ENT *p_tle)
{
switch (p_tle->event) {
case BTU_TTYPE_L2CAP_LINK:
l2c_link_timeout ((tL2C_LCB *)p_tle->param);
break;
#if (CLASSIC_BT_INCLUDED == TRUE)...
case BTU_TTYPE_L2CAP_CHNL:
l2c_csm_execute (((tL2C_CCB *)p_tle->param), L2CEVT_TIMEOUT, NULL);
break;
...
case BTU_TTYPE_L2CAP_FCR_ACK:
l2c_csm_execute (((tL2C_CCB *)p_tle->param), L2CEVT_ACK_TIMEOUT, NULL);
break;/* ... */
#endif
case BTU_TTYPE_L2CAP_HOLD:
l2c_process_held_packets(TRUE);
break;
...
case BTU_TTYPE_L2CAP_INFO:
l2c_info_timeout((tL2C_LCB *)p_tle->param);
break;...
case BTU_TTYPE_L2CAP_UPDA_CONN_PARAMS: {
#if (BLE_INCLUDED == TRUE)
UINT8 status = HCI_ERR_HOST_TIMEOUT;
tL2C_LCB *p_lcb = (tL2C_LCB *)p_tle->param;
if (p_lcb){
p_lcb->conn_update_mask &= ~L2C_BLE_UPDATE_PENDING;
p_lcb->conn_update_mask &= ~L2C_BLE_UPDATE_PARAM_FULL;
l2c_send_update_conn_params_cb(p_lcb, status);
}{...}
/* ... */#endif
break;
}{...}
... }{...}
}{ ... }
/* ... */
#if (CLASSIC_BT_INCLUDED == TRUE)
UINT8 l2c_data_write (UINT16 cid, BT_HDR *p_data, UINT16 flags)
{
tL2C_CCB *p_ccb;
if ((p_ccb = l2cu_find_ccb_by_cid (NULL, cid)) == NULL) {
L2CAP_TRACE_WARNING ("L2CAP - no CCB for L2CA_DataWrite, CID: %d", cid);
osi_free (p_data);
return (L2CAP_DW_FAILED);
}{...}
#ifndef TESTER
/* ... */
if (p_data->len > p_ccb->peer_cfg.mtu) {
L2CAP_TRACE_WARNING ("L2CAP - CID: 0x%04x cannot send message bigger than peer's mtu size", cid);
osi_free (p_data);
return (L2CAP_DW_FAILED);
}{...}
#endif
p_data->layer_specific = flags;
if (p_ccb->cong_sent) {
L2CAP_TRACE_DEBUG ("L2CAP - CID: 0x%04x cannot send, already congested xmit_hold_q.count: %u buff_quota: %u",
p_ccb->local_cid,
fixed_queue_length(p_ccb->xmit_hold_q),
p_ccb->buff_quota);
osi_free (p_data);
return (L2CAP_DW_FAILED);
}{...}
l2c_csm_execute (p_ccb, L2CEVT_L2CA_DATA_WRITE, p_data);
if (p_ccb->cong_sent) {
return (L2CAP_DW_CONGESTED);
}{...}
return (L2CAP_DW_SUCCESS);
}{...}
/* ... */#endif...