-
Notifications
You must be signed in to change notification settings - Fork 7.1k
/
l2c_fcr.c
2229 lines (1877 loc) · 88.3 KB
/
l2c_fcr.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
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
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/******************************************************************************
*
* Copyright (C) 2004-2012 Broadcom Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
/******************************************************************************
*
* This file contains the L2CAP 1.2 Flow Control and retransmissions
* functions
*
******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "common/bt_trace.h"
#include "stack/bt_types.h"
#include "stack/hcimsgs.h"
#include "stack/l2c_api.h"
#include "l2c_int.h"
#include "stack/l2cdefs.h"
#include "stack/btm_api.h"
#include "btm_int.h"
#include "stack/btu.h"
#include "osi/allocator.h"
#if (L2CAP_COC_INCLUDED == TRUE)
/* Flag passed to retransmit_i_frames() when all packets should be retransmitted */
#define L2C_FCR_RETX_ALL_PKTS 0xFF
#if BT_TRACE_VERBOSE == TRUE
static char *SAR_types[] = { "Unsegmented", "Start", "End", "Continuation" };
static char *SUP_types[] = { "RR", "REJ", "RNR", "SREJ" };
#endif
/* Look-up table for the CRC calculation */
static const unsigned short crctab[256] = {
0x0000, 0xc0c1, 0xc181, 0x0140, 0xc301, 0x03c0, 0x0280, 0xc241,
0xc601, 0x06c0, 0x0780, 0xc741, 0x0500, 0xc5c1, 0xc481, 0x0440,
0xcc01, 0x0cc0, 0x0d80, 0xcd41, 0x0f00, 0xcfc1, 0xce81, 0x0e40,
0x0a00, 0xcac1, 0xcb81, 0x0b40, 0xc901, 0x09c0, 0x0880, 0xc841,
0xd801, 0x18c0, 0x1980, 0xd941, 0x1b00, 0xdbc1, 0xda81, 0x1a40,
0x1e00, 0xdec1, 0xdf81, 0x1f40, 0xdd01, 0x1dc0, 0x1c80, 0xdc41,
0x1400, 0xd4c1, 0xd581, 0x1540, 0xd701, 0x17c0, 0x1680, 0xd641,
0xd201, 0x12c0, 0x1380, 0xd341, 0x1100, 0xd1c1, 0xd081, 0x1040,
0xf001, 0x30c0, 0x3180, 0xf141, 0x3300, 0xf3c1, 0xf281, 0x3240,
0x3600, 0xf6c1, 0xf781, 0x3740, 0xf501, 0x35c0, 0x3480, 0xf441,
0x3c00, 0xfcc1, 0xfd81, 0x3d40, 0xff01, 0x3fc0, 0x3e80, 0xfe41,
0xfa01, 0x3ac0, 0x3b80, 0xfb41, 0x3900, 0xf9c1, 0xf881, 0x3840,
0x2800, 0xe8c1, 0xe981, 0x2940, 0xeb01, 0x2bc0, 0x2a80, 0xea41,
0xee01, 0x2ec0, 0x2f80, 0xef41, 0x2d00, 0xedc1, 0xec81, 0x2c40,
0xe401, 0x24c0, 0x2580, 0xe541, 0x2700, 0xe7c1, 0xe681, 0x2640,
0x2200, 0xe2c1, 0xe381, 0x2340, 0xe101, 0x21c0, 0x2080, 0xe041,
0xa001, 0x60c0, 0x6180, 0xa141, 0x6300, 0xa3c1, 0xa281, 0x6240,
0x6600, 0xa6c1, 0xa781, 0x6740, 0xa501, 0x65c0, 0x6480, 0xa441,
0x6c00, 0xacc1, 0xad81, 0x6d40, 0xaf01, 0x6fc0, 0x6e80, 0xae41,
0xaa01, 0x6ac0, 0x6b80, 0xab41, 0x6900, 0xa9c1, 0xa881, 0x6840,
0x7800, 0xb8c1, 0xb981, 0x7940, 0xbb01, 0x7bc0, 0x7a80, 0xba41,
0xbe01, 0x7ec0, 0x7f80, 0xbf41, 0x7d00, 0xbdc1, 0xbc81, 0x7c40,
0xb401, 0x74c0, 0x7580, 0xb541, 0x7700, 0xb7c1, 0xb681, 0x7640,
0x7200, 0xb2c1, 0xb381, 0x7340, 0xb101, 0x71c0, 0x7080, 0xb041,
0x5000, 0x90c1, 0x9181, 0x5140, 0x9301, 0x53c0, 0x5280, 0x9241,
0x9601, 0x56c0, 0x5780, 0x9741, 0x5500, 0x95c1, 0x9481, 0x5440,
0x9c01, 0x5cc0, 0x5d80, 0x9d41, 0x5f00, 0x9fc1, 0x9e81, 0x5e40,
0x5a00, 0x9ac1, 0x9b81, 0x5b40, 0x9901, 0x59c0, 0x5880, 0x9841,
0x8801, 0x48c0, 0x4980, 0x8941, 0x4b00, 0x8bc1, 0x8a81, 0x4a40,
0x4e00, 0x8ec1, 0x8f81, 0x4f40, 0x8d01, 0x4dc0, 0x4c80, 0x8c41,
0x4400, 0x84c1, 0x8581, 0x4540, 0x8701, 0x47c0, 0x4680, 0x8641,
0x8201, 0x42c0, 0x4380, 0x8341, 0x4100, 0x81c1, 0x8081, 0x4040,
};
/*******************************************************************************
** Static local functions
*/
static BOOLEAN process_reqseq (tL2C_CCB *p_ccb, UINT16 ctrl_word);
static void process_s_frame (tL2C_CCB *p_ccb, BT_HDR *p_buf, UINT16 ctrl_word);
static void process_i_frame (tL2C_CCB *p_ccb, BT_HDR *p_buf, UINT16 ctrl_word, BOOLEAN delay_ack);
static BOOLEAN retransmit_i_frames (tL2C_CCB *p_ccb, UINT8 tx_seq);
static void prepare_I_frame (tL2C_CCB *p_ccb, BT_HDR *p_buf, BOOLEAN is_retransmission);
static void process_stream_frame (tL2C_CCB *p_ccb, BT_HDR *p_buf);
static BOOLEAN do_sar_reassembly (tL2C_CCB *p_ccb, BT_HDR *p_buf, UINT16 ctrl_word);
#if (L2CAP_ERTM_STATS == TRUE)
static void l2c_fcr_collect_ack_delay (tL2C_CCB *p_ccb, UINT8 num_bufs_acked);
#endif
/*******************************************************************************
**
** Function l2c_fcr_updcrc
**
** Description This function computes the CRC using the look-up table.
**
** Returns CRC
**
*******************************************************************************/
static unsigned short l2c_fcr_updcrc(unsigned short icrc, unsigned char *icp, int icnt)
{
register unsigned short crc = icrc;
register unsigned char *cp = icp;
register int cnt = icnt;
while (cnt--) {
crc = ((crc >> 8) & 0xff) ^ crctab[(crc & 0xff) ^ *cp++];
}
return (crc);
}
/*******************************************************************************
**
** Function l2c_fcr_tx_get_fcs
**
** Description This function computes the CRC for a frame to be TXed.
**
** Returns CRC
**
*******************************************************************************/
static UINT16 l2c_fcr_tx_get_fcs (BT_HDR *p_buf)
{
UINT8 *p = ((UINT8 *) (p_buf + 1)) + p_buf->offset;
return (l2c_fcr_updcrc (L2CAP_FCR_INIT_CRC, p, p_buf->len));
}
/*******************************************************************************
**
** Function l2c_fcr_rx_get_fcs
**
** Description This function computes the CRC for a received frame.
**
** Returns CRC
**
*******************************************************************************/
static UINT16 l2c_fcr_rx_get_fcs (BT_HDR *p_buf)
{
UINT8 *p = ((UINT8 *) (p_buf + 1)) + p_buf->offset;
/* offset points past the L2CAP header, but the CRC check includes it */
p -= L2CAP_PKT_OVERHEAD;
return (l2c_fcr_updcrc (L2CAP_FCR_INIT_CRC, p, p_buf->len + L2CAP_PKT_OVERHEAD));
}
/*******************************************************************************
**
** Function l2c_fcr_start_timer
**
** Description This function starts the (monitor or retransmission) timer.
**
** Returns -
**
*******************************************************************************/
void l2c_fcr_start_timer (tL2C_CCB *p_ccb)
{
assert(p_ccb != NULL);
UINT32 tout;
/* The timers which are in milliseconds */
if (p_ccb->fcrb.wait_ack) {
tout = (UINT32)p_ccb->our_cfg.fcr.mon_tout;
} else {
tout = (UINT32)p_ccb->our_cfg.fcr.rtrans_tout;
}
/* Only start a timer that was not started */
if (p_ccb->fcrb.mon_retrans_timer.in_use == 0) {
btu_start_quick_timer (&p_ccb->fcrb.mon_retrans_timer, BTU_TTYPE_L2CAP_CHNL, tout * QUICK_TIMER_TICKS_PER_SEC / 1000);
}
}
/*******************************************************************************
**
** Function l2c_fcr_stop_timer
**
** Description This function stops the (monitor or transmission) timer.
**
** Returns -
**
*******************************************************************************/
void l2c_fcr_stop_timer (tL2C_CCB *p_ccb)
{
assert(p_ccb != NULL);
if (p_ccb->fcrb.mon_retrans_timer.in_use) {
btu_stop_quick_timer (&p_ccb->fcrb.mon_retrans_timer);
}
}
/*******************************************************************************
**
** Function l2c_fcr_free_timer
**
** Description This function releases the (monitor or transmission) timer.
**
** Returns -
**
*******************************************************************************/
void l2c_fcr_free_timer (tL2C_CCB *p_ccb)
{
assert(p_ccb != NULL);
btu_free_quick_timer (&p_ccb->fcrb.mon_retrans_timer);
}
/*******************************************************************************
**
** Function l2c_fcr_cleanup
**
** Description This function cleans up the variable used for flow-control/retrans.
**
** Returns -
**
*******************************************************************************/
void l2c_fcr_cleanup (tL2C_CCB *p_ccb)
{
assert(p_ccb != NULL);
tL2C_FCRB *p_fcrb = &p_ccb->fcrb;
l2c_fcr_stop_timer (p_ccb);
if (p_fcrb->p_rx_sdu) {
osi_free(p_fcrb->p_rx_sdu);
p_fcrb->p_rx_sdu = NULL;
}
fixed_queue_free(p_fcrb->waiting_for_ack_q, osi_free_func);
p_fcrb->waiting_for_ack_q = NULL;
fixed_queue_free(p_fcrb->srej_rcv_hold_q, osi_free_func);
p_fcrb->srej_rcv_hold_q = NULL;
fixed_queue_free(p_fcrb->retrans_q, osi_free_func);
p_fcrb->retrans_q = NULL;
btu_free_quick_timer (&p_fcrb->ack_timer);
memset(&p_fcrb->ack_timer, 0, sizeof(TIMER_LIST_ENT));
btu_free_quick_timer (&p_ccb->fcrb.mon_retrans_timer);
memset(&p_fcrb->mon_retrans_timer, 0, sizeof(TIMER_LIST_ENT));
#if (L2CAP_ERTM_STATS == TRUE)
if ( (p_ccb->local_cid >= L2CAP_BASE_APPL_CID) && (p_ccb->peer_cfg.fcr.mode == L2CAP_FCR_ERTM_MODE) ) {
UINT32 dur = osi_time_get_os_boottime_ms() - p_ccb->fcrb.connect_tick_count;
char *p_str = (char *)osi_malloc(120);
UINT16 i;
UINT32 throughput_avg, ack_delay_avg, ack_q_count_avg;
BT_TRACE(TRACE_CTRL_GENERAL | TRACE_LAYER_GKI | TRACE_ORG_GKI , TRACE_TYPE_GENERIC,
"--- L2CAP ERTM Stats for CID: 0x%04x Duration: %08ums", p_ccb->local_cid, dur);
BT_TRACE(TRACE_CTRL_GENERAL | TRACE_LAYER_GKI | TRACE_ORG_GKI , TRACE_TYPE_GENERIC,
"Retransmissions:%08u Times Flow Controlled:%08u Retrans Touts:%08u Ack Touts:%08u",
p_ccb->fcrb.pkts_retransmitted, p_ccb->fcrb.xmit_window_closed, p_ccb->fcrb.retrans_touts, p_ccb->fcrb.xmit_ack_touts);
BT_TRACE(TRACE_CTRL_GENERAL | TRACE_LAYER_GKI | TRACE_ORG_GKI , TRACE_TYPE_GENERIC,
"Times there is less than 2 packets in controller when flow controlled:%08u", p_ccb->fcrb.controller_idle);
BT_TRACE(TRACE_CTRL_GENERAL | TRACE_LAYER_GKI | TRACE_ORG_GKI , TRACE_TYPE_GENERIC,
"max_held_acks:%08u, in_cfg.fcr.tx_win_sz:%08u", p_ccb->fcrb.max_held_acks, p_ccb->peer_cfg.fcr.tx_win_sz );
if (p_str) {
sprintf(p_str, "Sent Pkts:%08u Bytes:%10u(%06u/sec) RR:%08u REJ:%08u RNR:%08u SREJ:%08u",
p_ccb->fcrb.ertm_pkt_counts[0], p_ccb->fcrb.ertm_byte_counts[0],
(dur >= 10 ? (p_ccb->fcrb.ertm_byte_counts[0] * 100) / (dur / 10) : 0),
p_ccb->fcrb.s_frames_sent[0], p_ccb->fcrb.s_frames_sent[1], p_ccb->fcrb.s_frames_sent[2], p_ccb->fcrb.s_frames_sent[3]);
BT_TRACE(TRACE_CTRL_GENERAL | TRACE_LAYER_GKI | TRACE_ORG_GKI , TRACE_TYPE_GENERIC, "%s", p_str);
sprintf(p_str, "Rcvd Pkts:%08u Bytes:%10u(%06u/sec) RR:%08u REJ:%08u RNR:%08u SREJ:%08u",
p_ccb->fcrb.ertm_pkt_counts[1], p_ccb->fcrb.ertm_byte_counts[1],
(dur >= 10 ? (p_ccb->fcrb.ertm_byte_counts[1] * 100) / (dur / 10) : 0),
p_ccb->fcrb.s_frames_rcvd[0], p_ccb->fcrb.s_frames_rcvd[1], p_ccb->fcrb.s_frames_rcvd[2], p_ccb->fcrb.s_frames_rcvd[3]);
BT_TRACE(TRACE_CTRL_GENERAL | TRACE_LAYER_GKI | TRACE_ORG_GKI , TRACE_TYPE_GENERIC, "%s", p_str);
throughput_avg = 0;
ack_delay_avg = 0;
ack_q_count_avg = 0;
for (i = 0; i < L2CAP_ERTM_STATS_NUM_AVG; i++ ) {
if (i == p_ccb->fcrb.ack_delay_avg_index ) {
BT_TRACE(TRACE_CTRL_GENERAL | TRACE_LAYER_GKI | TRACE_ORG_GKI , TRACE_TYPE_GENERIC,
"[%02u] collecting data ...", i );
continue;
}
sprintf(p_str, "[%02u] throughput: %5u, ack_delay avg:%3u, min:%3u, max:%3u, ack_q_count avg:%3u, min:%3u, max:%3u",
i, p_ccb->fcrb.throughput[i],
p_ccb->fcrb.ack_delay_avg[i], p_ccb->fcrb.ack_delay_min[i], p_ccb->fcrb.ack_delay_max[i],
p_ccb->fcrb.ack_q_count_avg[i], p_ccb->fcrb.ack_q_count_min[i], p_ccb->fcrb.ack_q_count_max[i] );
BT_TRACE(TRACE_CTRL_GENERAL | TRACE_LAYER_GKI | TRACE_ORG_GKI , TRACE_TYPE_GENERIC, "%s", p_str);
throughput_avg += p_ccb->fcrb.throughput[i];
ack_delay_avg += p_ccb->fcrb.ack_delay_avg[i];
ack_q_count_avg += p_ccb->fcrb.ack_q_count_avg[i];
}
throughput_avg /= (L2CAP_ERTM_STATS_NUM_AVG - 1);
ack_delay_avg /= (L2CAP_ERTM_STATS_NUM_AVG - 1);
ack_q_count_avg /= (L2CAP_ERTM_STATS_NUM_AVG - 1);
BT_TRACE(TRACE_CTRL_GENERAL | TRACE_LAYER_GKI | TRACE_ORG_GKI , TRACE_TYPE_GENERIC,
"throughput_avg: %8u (kbytes/sec), ack_delay_avg: %8u ms, ack_q_count_avg: %8u",
throughput_avg, ack_delay_avg, ack_q_count_avg );
osi_free(p_str);
}
BT_TRACE(TRACE_CTRL_GENERAL | TRACE_LAYER_GKI | TRACE_ORG_GKI , TRACE_TYPE_GENERIC,
"---");
}
#endif
memset (p_fcrb, 0, sizeof (tL2C_FCRB));
}
/*******************************************************************************
**
** Function l2c_fcr_clone_buf
**
** Description This function allocates and copies requested part of a buffer
** at a new-offset.
**
** Returns pointer to new buffer
**
*******************************************************************************/
BT_HDR *l2c_fcr_clone_buf (BT_HDR *p_buf, UINT16 new_offset, UINT16 no_of_bytes)
{
assert(p_buf != NULL);
/*
* NOTE: We allocate extra L2CAP_FCS_LEN octets, in case we need to put
* the FCS (Frame Check Sequence) at the end of the buffer.
*/
uint16_t buf_size = no_of_bytes + sizeof(BT_HDR) + new_offset + L2CAP_FCS_LEN;
#if (L2CAP_ERTM_STATS == TRUE)
/*
* NOTE: If L2CAP_ERTM_STATS is enabled, we need 4 extra octets at the
* end for a timestamp at the end of an I-frame.
*/
buf_size += sizeof(uint32_t);
#endif
BT_HDR *p_buf2 = (BT_HDR *)osi_malloc(buf_size);
p_buf2->offset = new_offset;
p_buf2->len = no_of_bytes;
memcpy(((UINT8 *)(p_buf2 + 1)) + p_buf2->offset,
((UINT8 *)(p_buf + 1)) + p_buf->offset,
no_of_bytes);
return (p_buf2);
}
/*******************************************************************************
**
** Function l2c_fcr_is_flow_controlled
**
** Description This function checks if the CCB is flow controlled by peer.
**
** Returns The control word
**
*******************************************************************************/
BOOLEAN l2c_fcr_is_flow_controlled (tL2C_CCB *p_ccb)
{
assert(p_ccb != NULL);
if (p_ccb->peer_cfg.fcr.mode == L2CAP_FCR_ERTM_MODE) {
/* Check if remote side flowed us off or the transmit window is full */
if ( (p_ccb->fcrb.remote_busy == TRUE)
|| (fixed_queue_length(p_ccb->fcrb.waiting_for_ack_q) >= p_ccb->peer_cfg.fcr.tx_win_sz) ) {
#if (L2CAP_ERTM_STATS == TRUE)
if (!fixed_queue_is_empty(p_ccb->xmit_hold_q)) {
p_ccb->fcrb.xmit_window_closed++;
if ((p_ccb->p_lcb->sent_not_acked < 2) && (l2cb.controller_xmit_window > 0)) {
p_ccb->fcrb.controller_idle++;
}
}
#endif
return (TRUE);
}
}
return (FALSE);
}
/*******************************************************************************
**
** Function prepare_I_frame
**
** Description This function sets the FCR variables in an I-frame that is
** about to be sent to HCI for transmission. This may be the
** first time the I-frame is sent, or a retransmission
**
** Returns -
**
*******************************************************************************/
static void prepare_I_frame (tL2C_CCB *p_ccb, BT_HDR *p_buf, BOOLEAN is_retransmission)
{
assert(p_ccb != NULL);
assert(p_buf != NULL);
tL2C_FCRB *p_fcrb = &p_ccb->fcrb;
UINT8 *p;
UINT16 fcs;
UINT16 ctrl_word;
BOOLEAN set_f_bit = p_fcrb->send_f_rsp;
p_fcrb->send_f_rsp = FALSE;
if (is_retransmission) {
/* Get the old control word and clear out the old req_seq and F bits */
p = ((UINT8 *) (p_buf + 1)) + p_buf->offset + L2CAP_PKT_OVERHEAD;
STREAM_TO_UINT16 (ctrl_word, p);
ctrl_word &= ~(L2CAP_FCR_REQ_SEQ_BITS + L2CAP_FCR_F_BIT);
} else {
ctrl_word = p_buf->layer_specific & L2CAP_FCR_SEG_BITS; /* SAR bits */
ctrl_word |= (p_fcrb->next_tx_seq << L2CAP_FCR_TX_SEQ_BITS_SHIFT); /* Tx Seq */
p_fcrb->next_tx_seq = (p_fcrb->next_tx_seq + 1) & L2CAP_FCR_SEQ_MODULO;
}
/* Set the F-bit and reqseq only if using re-transmission mode */
if (p_ccb->peer_cfg.fcr.mode == L2CAP_FCR_ERTM_MODE) {
if (set_f_bit) {
ctrl_word |= L2CAP_FCR_F_BIT;
}
ctrl_word |= (p_fcrb->next_seq_expected) << L2CAP_FCR_REQ_SEQ_BITS_SHIFT;
p_fcrb->last_ack_sent = p_ccb->fcrb.next_seq_expected;
if (p_ccb->fcrb.ack_timer.in_use) {
btu_stop_quick_timer (&p_ccb->fcrb.ack_timer);
}
}
/* Set the control word */
p = ((UINT8 *) (p_buf + 1)) + p_buf->offset + L2CAP_PKT_OVERHEAD;
UINT16_TO_STREAM (p, ctrl_word);
/* Compute the FCS and add to the end of the buffer if not bypassed */
if (p_ccb->bypass_fcs != L2CAP_BYPASS_FCS) {
/* length field in l2cap header has to include FCS length */
p = ((UINT8 *) (p_buf + 1)) + p_buf->offset;
UINT16_TO_STREAM (p, p_buf->len + L2CAP_FCS_LEN - L2CAP_PKT_OVERHEAD);
/* Calculate the FCS */
fcs = l2c_fcr_tx_get_fcs(p_buf);
/* Point to the end of the buffer and put the FCS there */
p = ((UINT8 *) (p_buf + 1)) + p_buf->offset + p_buf->len;
UINT16_TO_STREAM (p, fcs);
p_buf->len += L2CAP_FCS_LEN;
}
#if BT_TRACE_VERBOSE == TRUE
if (is_retransmission) {
L2CAP_TRACE_EVENT ("L2CAP eRTM ReTx I-frame CID: 0x%04x Len: %u SAR: %s TxSeq: %u ReqSeq: %u F: %u",
p_ccb->local_cid, p_buf->len,
SAR_types[(ctrl_word & L2CAP_FCR_SAR_BITS) >> L2CAP_FCR_SAR_BITS_SHIFT],
(ctrl_word & L2CAP_FCR_TX_SEQ_BITS) >> L2CAP_FCR_TX_SEQ_BITS_SHIFT,
(ctrl_word & L2CAP_FCR_REQ_SEQ_BITS) >> L2CAP_FCR_REQ_SEQ_BITS_SHIFT,
(ctrl_word & L2CAP_FCR_F_BIT) >> L2CAP_FCR_F_BIT_SHIFT);
} else {
L2CAP_TRACE_EVENT ("L2CAP eRTM Tx I-frame CID: 0x%04x Len: %u SAR: %-12s TxSeq: %u ReqSeq: %u F: %u",
p_ccb->local_cid, p_buf->len,
SAR_types[(ctrl_word & L2CAP_FCR_SAR_BITS) >> L2CAP_FCR_SAR_BITS_SHIFT],
(ctrl_word & L2CAP_FCR_TX_SEQ_BITS) >> L2CAP_FCR_TX_SEQ_BITS_SHIFT,
(ctrl_word & L2CAP_FCR_REQ_SEQ_BITS) >> L2CAP_FCR_REQ_SEQ_BITS_SHIFT,
(ctrl_word & L2CAP_FCR_F_BIT) >> L2CAP_FCR_F_BIT_SHIFT);
}
#endif
/* Start the retransmission timer if not already running */
if (p_ccb->peer_cfg.fcr.mode == L2CAP_FCR_ERTM_MODE) {
l2c_fcr_start_timer (p_ccb);
}
}
/*******************************************************************************
**
** Function l2c_fcr_send_S_frame
**
** Description This function formats and sends an S-frame for transmission.
**
** Returns -
**
*******************************************************************************/
void l2c_fcr_send_S_frame (tL2C_CCB *p_ccb, UINT16 function_code, UINT16 pf_bit)
{
assert(p_ccb != NULL);
BT_HDR *p_buf;
UINT8 *p;
UINT16 ctrl_word;
UINT16 fcs;
if ((!p_ccb->in_use) || (p_ccb->chnl_state != CST_OPEN)) {
return;
}
#if (L2CAP_ERTM_STATS == TRUE)
p_ccb->fcrb.s_frames_sent[function_code]++;
#endif
if (pf_bit == L2CAP_FCR_P_BIT) {
p_ccb->fcrb.wait_ack = TRUE;
l2c_fcr_stop_timer (p_ccb); /* Restart the monitor timer */
l2c_fcr_start_timer (p_ccb);
}
/* Create the control word to use */
ctrl_word = (function_code << L2CAP_FCR_SUP_SHIFT) | L2CAP_FCR_S_FRAME_BIT;
ctrl_word |= (p_ccb->fcrb.next_seq_expected << L2CAP_FCR_REQ_SEQ_BITS_SHIFT);
ctrl_word |= pf_bit;
if ((p_buf = (BT_HDR *)osi_malloc(L2CAP_CMD_BUF_SIZE)) != NULL) {
p_buf->offset = HCI_DATA_PREAMBLE_SIZE;
p_buf->len = L2CAP_PKT_OVERHEAD + L2CAP_FCR_OVERHEAD;
/* Set the pointer to the beginning of the data */
p = (UINT8 *)(p_buf + 1) + p_buf->offset;
/* Put in the L2CAP header */
UINT16_TO_STREAM (p, L2CAP_FCR_OVERHEAD + L2CAP_FCS_LEN);
UINT16_TO_STREAM (p, p_ccb->remote_cid);
UINT16_TO_STREAM (p, ctrl_word);
/* Compute the FCS and add to the end of the buffer if not bypassed */
if (p_ccb->bypass_fcs != L2CAP_BYPASS_FCS) {
fcs = l2c_fcr_tx_get_fcs (p_buf);
UINT16_TO_STREAM (p, fcs);
p_buf->len += L2CAP_FCS_LEN;
} else { /* rewrite the length without FCS length */
p -= 6;
UINT16_TO_STREAM (p, L2CAP_FCR_OVERHEAD);
}
/* Now, the HCI transport header */
p_buf->layer_specific = L2CAP_NON_FLUSHABLE_PKT;
l2cu_set_acl_hci_header (p_buf, p_ccb);
#if BT_TRACE_VERBOSE == TRUE
if ((((ctrl_word & L2CAP_FCR_SUP_BITS) >> L2CAP_FCR_SUP_SHIFT) == 1)
|| (((ctrl_word & L2CAP_FCR_SUP_BITS) >> L2CAP_FCR_SUP_SHIFT) == 3)) {
L2CAP_TRACE_WARNING ("L2CAP eRTM Tx S-frame CID: 0x%04x ctrlword: 0x%04x Type: %s ReqSeq: %u P: %u F: %u",
p_ccb->local_cid, ctrl_word,
SUP_types[(ctrl_word & L2CAP_FCR_SUP_BITS) >> L2CAP_FCR_SUP_SHIFT],
(ctrl_word & L2CAP_FCR_REQ_SEQ_BITS) >> L2CAP_FCR_REQ_SEQ_BITS_SHIFT,
(ctrl_word & L2CAP_FCR_P_BIT) >> L2CAP_FCR_P_BIT_SHIFT,
(ctrl_word & L2CAP_FCR_F_BIT) >> L2CAP_FCR_F_BIT_SHIFT);
L2CAP_TRACE_WARNING (" Buf Len: %u", p_buf->len);
} else {
L2CAP_TRACE_EVENT ("L2CAP eRTM Tx S-frame CID: 0x%04x ctrlword: 0x%04x Type: %s ReqSeq: %u P: %u F: %u",
p_ccb->local_cid, ctrl_word,
SUP_types[(ctrl_word & L2CAP_FCR_SUP_BITS) >> L2CAP_FCR_SUP_SHIFT],
(ctrl_word & L2CAP_FCR_REQ_SEQ_BITS) >> L2CAP_FCR_REQ_SEQ_BITS_SHIFT,
(ctrl_word & L2CAP_FCR_P_BIT) >> L2CAP_FCR_P_BIT_SHIFT,
(ctrl_word & L2CAP_FCR_F_BIT) >> L2CAP_FCR_F_BIT_SHIFT);
L2CAP_TRACE_EVENT (" Buf Len: %u", p_buf->len);
}
#endif /* BT_TRACE_VERBOSE */
l2c_link_check_send_pkts (p_ccb->p_lcb, NULL, p_buf);
p_ccb->fcrb.last_ack_sent = p_ccb->fcrb.next_seq_expected;
if (p_ccb->fcrb.ack_timer.in_use) {
btu_stop_quick_timer (&p_ccb->fcrb.ack_timer);
}
} else {
L2CAP_TRACE_ERROR ("l2c_fcr_send_S_frame(No Resources) cid 0x%04x, Type: 0x%4x",
p_ccb->local_cid, function_code);
}
}
/*******************************************************************************
**
** Function l2c_fcr_proc_pdu
**
** Description This function is the entry point for processing of a
** received PDU when in flow control and/or retransmission modes.
**
** Returns -
**
*******************************************************************************/
void l2c_fcr_proc_pdu (tL2C_CCB *p_ccb, BT_HDR *p_buf)
{
assert(p_ccb != NULL);
assert(p_buf != NULL);
UINT8 *p;
UINT16 fcs;
UINT16 min_pdu_len;
UINT16 ctrl_word;
/* Check the length */
min_pdu_len = (p_ccb->bypass_fcs == L2CAP_BYPASS_FCS) ?
(UINT16)L2CAP_FCR_OVERHEAD : (UINT16)(L2CAP_FCS_LEN + L2CAP_FCR_OVERHEAD);
if (p_buf->len < min_pdu_len) {
L2CAP_TRACE_WARNING ("Rx L2CAP PDU: CID: 0x%04x Len too short: %u", p_ccb->local_cid, p_buf->len);
osi_free (p_buf);
return;
}
if (p_ccb->peer_cfg.fcr.mode == L2CAP_FCR_STREAM_MODE) {
process_stream_frame (p_ccb, p_buf);
return;
}
#if BT_TRACE_VERBOSE == TRUE
/* Get the control word */
p = ((UINT8 *)(p_buf + 1)) + p_buf->offset;
STREAM_TO_UINT16 (ctrl_word, p);
if (ctrl_word & L2CAP_FCR_S_FRAME_BIT) {
if ((((ctrl_word & L2CAP_FCR_SUP_BITS) >> L2CAP_FCR_SUP_SHIFT) == 1)
|| (((ctrl_word & L2CAP_FCR_SUP_BITS) >> L2CAP_FCR_SUP_SHIFT) == 3)) {
/* REJ or SREJ */
L2CAP_TRACE_WARNING ("L2CAP eRTM Rx S-frame: cid: 0x%04x Len: %u Type: %s ReqSeq: %u P: %u F: %u",
p_ccb->local_cid, p_buf->len,
SUP_types[(ctrl_word & L2CAP_FCR_SUP_BITS) >> L2CAP_FCR_SUP_SHIFT],
(ctrl_word & L2CAP_FCR_REQ_SEQ_BITS) >> L2CAP_FCR_REQ_SEQ_BITS_SHIFT,
(ctrl_word & L2CAP_FCR_P_BIT) >> L2CAP_FCR_P_BIT_SHIFT,
(ctrl_word & L2CAP_FCR_F_BIT) >> L2CAP_FCR_F_BIT_SHIFT);
} else {
L2CAP_TRACE_EVENT ("L2CAP eRTM Rx S-frame: cid: 0x%04x Len: %u Type: %s ReqSeq: %u P: %u F: %u",
p_ccb->local_cid, p_buf->len,
SUP_types[(ctrl_word & L2CAP_FCR_SUP_BITS) >> L2CAP_FCR_SUP_SHIFT],
(ctrl_word & L2CAP_FCR_REQ_SEQ_BITS) >> L2CAP_FCR_REQ_SEQ_BITS_SHIFT,
(ctrl_word & L2CAP_FCR_P_BIT) >> L2CAP_FCR_P_BIT_SHIFT,
(ctrl_word & L2CAP_FCR_F_BIT) >> L2CAP_FCR_F_BIT_SHIFT);
}
} else {
L2CAP_TRACE_EVENT ("L2CAP eRTM Rx I-frame: cid: 0x%04x Len: %u SAR: %-12s TxSeq: %u ReqSeq: %u F: %u",
p_ccb->local_cid, p_buf->len,
SAR_types[(ctrl_word & L2CAP_FCR_SAR_BITS) >> L2CAP_FCR_SAR_BITS_SHIFT],
(ctrl_word & L2CAP_FCR_TX_SEQ_BITS) >> L2CAP_FCR_TX_SEQ_BITS_SHIFT,
(ctrl_word & L2CAP_FCR_REQ_SEQ_BITS) >> L2CAP_FCR_REQ_SEQ_BITS_SHIFT,
(ctrl_word & L2CAP_FCR_F_BIT) >> L2CAP_FCR_F_BIT_SHIFT);
}
L2CAP_TRACE_EVENT (" eRTM Rx Nxt_tx_seq %u, Lst_rx_ack %u, Nxt_seq_exp %u, Lst_ack_snt %u, wt_q.cnt %u, tries %u",
p_ccb->fcrb.next_tx_seq, p_ccb->fcrb.last_rx_ack,
p_ccb->fcrb.next_seq_expected,
p_ccb->fcrb.last_ack_sent,
fixed_queue_length(p_ccb->fcrb.waiting_for_ack_q),
p_ccb->fcrb.num_tries);
#endif /* BT_TRACE_VERBOSE */
/* Verify FCS if using */
if (p_ccb->bypass_fcs != L2CAP_BYPASS_FCS) {
p = ((UINT8 *)(p_buf + 1)) + p_buf->offset + p_buf->len - L2CAP_FCS_LEN;
/* Extract and drop the FCS from the packet */
STREAM_TO_UINT16 (fcs, p);
p_buf->len -= L2CAP_FCS_LEN;
if (l2c_fcr_rx_get_fcs(p_buf) != fcs) {
L2CAP_TRACE_WARNING ("Rx L2CAP PDU: CID: 0x%04x BAD FCS", p_ccb->local_cid);
osi_free(p_buf);
return;
}
}
/* Get the control word */
p = ((UINT8 *)(p_buf + 1)) + p_buf->offset;
STREAM_TO_UINT16 (ctrl_word, p);
p_buf->len -= L2CAP_FCR_OVERHEAD;
p_buf->offset += L2CAP_FCR_OVERHEAD;
/* If we had a poll bit outstanding, check if we got a final response */
if (p_ccb->fcrb.wait_ack) {
/* If final bit not set, ignore the frame unless it is a polled S-frame */
if ( !(ctrl_word & L2CAP_FCR_F_BIT) ) {
if ( (ctrl_word & L2CAP_FCR_P_BIT) && (ctrl_word & L2CAP_FCR_S_FRAME_BIT) ) {
if (p_ccb->fcrb.srej_sent) {
l2c_fcr_send_S_frame (p_ccb, L2CAP_FCR_SUP_SREJ, L2CAP_FCR_F_BIT);
} else if (p_ccb->fcrb.local_busy) {
l2c_fcr_send_S_frame (p_ccb, L2CAP_FCR_SUP_RNR, L2CAP_FCR_F_BIT);
} else {
l2c_fcr_send_S_frame (p_ccb, L2CAP_FCR_SUP_RR, L2CAP_FCR_F_BIT);
}
/* Got a poll while in wait_ack state, so re-start our timer with 1-second */
/* This is a small optimization... the monitor timer is 12 secs, but we saw */
/* that if the other side sends us a poll when we are waiting for a final, */
/* then it speeds up recovery significantly if we poll him back soon after his poll. */
btu_start_quick_timer (&p_ccb->fcrb.mon_retrans_timer, BTU_TTYPE_L2CAP_CHNL, QUICK_TIMER_TICKS_PER_SEC);
}
osi_free (p_buf);
return;
}
p_ccb->fcrb.wait_ack = FALSE;
/* P and F are mutually exclusive */
if (ctrl_word & L2CAP_FCR_S_FRAME_BIT) {
ctrl_word &= ~L2CAP_FCR_P_BIT;
}
if (fixed_queue_is_empty(p_ccb->fcrb.waiting_for_ack_q)) {
p_ccb->fcrb.num_tries = 0;
}
l2c_fcr_stop_timer (p_ccb);
} else {
/* Otherwise, ensure the final bit is ignored */
ctrl_word &= ~L2CAP_FCR_F_BIT;
}
/* Process receive sequence number */
if (!process_reqseq (p_ccb, ctrl_word)) {
osi_free (p_buf);
return;
}
/* Process based on whether it is an S-frame or an I-frame */
if (ctrl_word & L2CAP_FCR_S_FRAME_BIT) {
process_s_frame (p_ccb, p_buf, ctrl_word);
} else {
process_i_frame (p_ccb, p_buf, ctrl_word, FALSE);
}
/* Return if the channel got disconnected by a bad packet or max retransmissions */
if ( (!p_ccb->in_use) || (p_ccb->chnl_state != CST_OPEN) ) {
return;
}
/* If we have some buffers held while doing SREJ, and SREJ has cleared, process them now */
if ( (!p_ccb->fcrb.local_busy) && (!p_ccb->fcrb.srej_sent) &&
(!fixed_queue_is_empty(p_ccb->fcrb.srej_rcv_hold_q))) {
fixed_queue_t *temp_q = p_ccb->fcrb.srej_rcv_hold_q;
p_ccb->fcrb.srej_rcv_hold_q = fixed_queue_new(QUEUE_SIZE_MAX);
while ((p_buf = (BT_HDR *)fixed_queue_dequeue(temp_q, 0)) != NULL) {
if (p_ccb->in_use && (p_ccb->chnl_state == CST_OPEN)) {
/* Get the control word */
p = ((UINT8 *)(p_buf + 1)) + p_buf->offset - L2CAP_FCR_OVERHEAD;
STREAM_TO_UINT16 (ctrl_word, p);
L2CAP_TRACE_DEBUG ("l2c_fcr_proc_pdu() CID: 0x%04x Process Buffer from SREJ_Hold_Q TxSeq: %u Expected_Seq: %u",
p_ccb->local_cid, (ctrl_word & L2CAP_FCR_TX_SEQ_BITS) >> L2CAP_FCR_TX_SEQ_BITS_SHIFT,
p_ccb->fcrb.next_seq_expected);
/* Process the SREJ held I-frame, but do not send an RR for each individual frame */
process_i_frame (p_ccb, p_buf, ctrl_word, TRUE);
} else {
osi_free (p_buf);
}
/* If more frames were lost during SREJ, send a REJ */
if (p_ccb->fcrb.rej_after_srej) {
p_ccb->fcrb.rej_after_srej = FALSE;
p_ccb->fcrb.rej_sent = TRUE;
l2c_fcr_send_S_frame (p_ccb, L2CAP_FCR_SUP_REJ, 0);
}
}
fixed_queue_free(temp_q, NULL);
/* Now, if needed, send one RR for the whole held queue */
if ( (!p_ccb->fcrb.local_busy) && (!p_ccb->fcrb.rej_sent) && (!p_ccb->fcrb.srej_sent)
&& (p_ccb->fcrb.next_seq_expected != p_ccb->fcrb.last_ack_sent) ) {
l2c_fcr_send_S_frame (p_ccb, L2CAP_FCR_SUP_RR, 0);
} else {
L2CAP_TRACE_DEBUG ("l2c_fcr_proc_pdu() not sending RR CID: 0x%04x local_busy:%d rej_sent:%d srej_sent:%d Expected_Seq:%u Last_Ack:%u",
p_ccb->local_cid, p_ccb->fcrb.local_busy, p_ccb->fcrb.rej_sent, p_ccb->fcrb.srej_sent, p_ccb->fcrb.next_seq_expected,
p_ccb->fcrb.last_ack_sent);
}
}
/* If a window has opened, check if we can send any more packets */
if ( (!fixed_queue_is_empty(p_ccb->fcrb.retrans_q) ||
!fixed_queue_is_empty(p_ccb->xmit_hold_q))
&& (p_ccb->fcrb.wait_ack == FALSE)
&& (l2c_fcr_is_flow_controlled (p_ccb) == FALSE) ) {
l2c_link_check_send_pkts (p_ccb->p_lcb, NULL, NULL);
}
}
/*******************************************************************************
**
** Function l2c_fcr_proc_tout
**
** Description Handle a timeout. We should be in error recovery state.
**
** Returns -
**
*******************************************************************************/
void l2c_fcr_proc_tout (tL2C_CCB *p_ccb)
{
assert(p_ccb != NULL);
L2CAP_TRACE_DEBUG ("l2c_fcr_proc_tout: CID: 0x%04x num_tries: %u (max: %u) wait_ack: %u ack_q_count: %u",
p_ccb->local_cid, p_ccb->fcrb.num_tries,
p_ccb->peer_cfg.fcr.max_transmit,
p_ccb->fcrb.wait_ack,
fixed_queue_length(p_ccb->fcrb.waiting_for_ack_q));
#if (L2CAP_ERTM_STATS == TRUE)
p_ccb->fcrb.retrans_touts++;
#endif
if ( (p_ccb->peer_cfg.fcr.max_transmit != 0) && (++p_ccb->fcrb.num_tries > p_ccb->peer_cfg.fcr.max_transmit) ) {
l2cu_disconnect_chnl (p_ccb);
} else {
if (!p_ccb->fcrb.srej_sent && !p_ccb->fcrb.rej_sent) {
if (p_ccb->fcrb.local_busy) {
l2c_fcr_send_S_frame (p_ccb, L2CAP_FCR_SUP_RNR, L2CAP_FCR_P_BIT);
} else {
l2c_fcr_send_S_frame (p_ccb, L2CAP_FCR_SUP_RR, L2CAP_FCR_P_BIT);
}
}
}
}
/*******************************************************************************
**
** Function l2c_fcr_proc_ack_tout
**
** Description Send RR/RNR if we have not acked I frame
**
** Returns -
**
*******************************************************************************/
void l2c_fcr_proc_ack_tout (tL2C_CCB *p_ccb)
{
assert(p_ccb != NULL);
L2CAP_TRACE_DEBUG ("l2c_fcr_proc_ack_tout: CID: 0x%04x State: %u Wack:%u Rq:%d Acked:%d", p_ccb->local_cid,
p_ccb->chnl_state, p_ccb->fcrb.wait_ack, p_ccb->fcrb.next_seq_expected, p_ccb->fcrb.last_ack_sent);
if ( (p_ccb->chnl_state == CST_OPEN) && (!p_ccb->fcrb.wait_ack)
&& (p_ccb->fcrb.last_ack_sent != p_ccb->fcrb.next_seq_expected) ) {
#if (L2CAP_ERTM_STATS == TRUE)
p_ccb->fcrb.xmit_ack_touts++;
#endif
if (p_ccb->fcrb.local_busy) {
l2c_fcr_send_S_frame (p_ccb, L2CAP_FCR_SUP_RNR, 0);
} else {
l2c_fcr_send_S_frame (p_ccb, L2CAP_FCR_SUP_RR, 0);
}
}
}
/*******************************************************************************
**
** Function process_reqseq
**
** Description Handle receive sequence number
**
** Returns -
**
*******************************************************************************/
static BOOLEAN process_reqseq (tL2C_CCB *p_ccb, UINT16 ctrl_word)
{
assert(p_ccb != NULL);
tL2C_FCRB *p_fcrb = &p_ccb->fcrb;
UINT8 req_seq, num_bufs_acked, xx;
UINT16 ls;
UINT16 full_sdus_xmitted;
/* Receive sequence number does not ack anything for SREJ with P-bit set to zero */
if ( (ctrl_word & L2CAP_FCR_S_FRAME_BIT)
&& ((ctrl_word & L2CAP_FCR_SUP_BITS) == (L2CAP_FCR_SUP_SREJ << L2CAP_FCR_SUP_SHIFT))
&& ((ctrl_word & L2CAP_FCR_P_BIT) == 0) ) {
/* If anything still waiting for ack, restart the timer if it was stopped */
if (!fixed_queue_is_empty(p_fcrb->waiting_for_ack_q)) {
l2c_fcr_start_timer(p_ccb);
}
return (TRUE);
}
/* Extract the receive sequence number from the control word */
req_seq = (ctrl_word & L2CAP_FCR_REQ_SEQ_BITS) >> L2CAP_FCR_REQ_SEQ_BITS_SHIFT;
num_bufs_acked = (req_seq - p_fcrb->last_rx_ack) & L2CAP_FCR_SEQ_MODULO;
/* Verify the request sequence is in range before proceeding */
if (num_bufs_acked > fixed_queue_length(p_fcrb->waiting_for_ack_q)) {
/* The channel is closed if ReqSeq is not in range */
L2CAP_TRACE_WARNING ("L2CAP eRTM Frame BAD Req_Seq - ctrl_word: 0x%04x req_seq 0x%02x last_rx_ack: 0x%02x QCount: %u",
ctrl_word, req_seq, p_fcrb->last_rx_ack,
fixed_queue_length(p_fcrb->waiting_for_ack_q));
l2cu_disconnect_chnl (p_ccb);
return (FALSE);
}
p_fcrb->last_rx_ack = req_seq;
/* Now we can release all acknowledged frames, and restart the retransmission timer if needed */
if (num_bufs_acked != 0) {
p_fcrb->num_tries = 0;
full_sdus_xmitted = 0;
#if (L2CAP_ERTM_STATS == TRUE)
l2c_fcr_collect_ack_delay (p_ccb, num_bufs_acked);
#endif
for (xx = 0; xx < num_bufs_acked; xx++) {
BT_HDR *p_tmp = (BT_HDR *)fixed_queue_dequeue(p_fcrb->waiting_for_ack_q, 0);
ls = p_tmp->layer_specific & L2CAP_FCR_SAR_BITS;
if ( (ls == L2CAP_FCR_UNSEG_SDU) || (ls == L2CAP_FCR_END_SDU) ) {
full_sdus_xmitted++;
}
osi_free(p_tmp);
if (p_ccb->cong_sent) {
l2cu_check_channel_congestion(p_ccb);
}
}
/* If we are still in a wait_ack state, do not mess with the timer */
if (!p_ccb->fcrb.wait_ack) {
l2c_fcr_stop_timer (p_ccb);
}
/* Check if we need to call the "packet_sent" callback */
if ( (p_ccb->p_rcb) && (p_ccb->p_rcb->api.pL2CA_TxComplete_Cb) && (full_sdus_xmitted) ) {
/* Special case for eRTM, if all packets sent, send 0xFFFF */
if (fixed_queue_is_empty(p_fcrb->waiting_for_ack_q) &&
fixed_queue_is_empty(p_ccb->xmit_hold_q)) {
full_sdus_xmitted = 0xFFFF;
}
(*p_ccb->p_rcb->api.pL2CA_TxComplete_Cb)(p_ccb->local_cid, full_sdus_xmitted);
}
}
/* If anything still waiting for ack, restart the timer if it was stopped */
if (!fixed_queue_is_empty(p_fcrb->waiting_for_ack_q)) {
l2c_fcr_start_timer (p_ccb);
}
return (TRUE);
}
/*******************************************************************************
**
** Function process_s_frame
**
** Description Process an S frame
**
** Returns -
**
*******************************************************************************/
static void process_s_frame (tL2C_CCB *p_ccb, BT_HDR *p_buf, UINT16 ctrl_word)
{
assert(p_ccb != NULL);
assert(p_buf != NULL);
tL2C_FCRB *p_fcrb = &p_ccb->fcrb;
UINT16 s_frame_type = (ctrl_word & L2CAP_FCR_SUP_BITS) >> L2CAP_FCR_SUP_SHIFT;
BOOLEAN remote_was_busy;
BOOLEAN all_ok = TRUE;
if (p_buf->len != 0) {
L2CAP_TRACE_WARNING ("Incorrect S-frame Length (%d)", p_buf->len);
}
L2CAP_TRACE_DEBUG ("process_s_frame ctrl_word 0x%04x fcrb_remote_busy:%d", ctrl_word, p_fcrb->remote_busy);
#if (L2CAP_ERTM_STATS == TRUE)
p_ccb->fcrb.s_frames_rcvd[s_frame_type]++;
#endif
if (ctrl_word & L2CAP_FCR_P_BIT) {
p_fcrb->rej_sent = FALSE; /* After checkpoint, we can send anoher REJ */
p_fcrb->send_f_rsp = TRUE; /* Set a flag in case an I-frame is pending */
}
switch (s_frame_type) {
case L2CAP_FCR_SUP_RR:
remote_was_busy = p_fcrb->remote_busy;
p_fcrb->remote_busy = FALSE;