summaryrefslogtreecommitdiff
path: root/cesar/mac/ca/test/ca_eoc/src/test_access.c
blob: d7c8a59db263aea012bce23218ad11457f89de14 (plain)
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
1001
1002
/* Cesar project {{{
 *
 * Copyright (C) 2007 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    src/test_access.c
 * \brief   Test access code.
 * \ingroup test
 */
#include "common/std.h"

#include "mac/ca/inc/context.h"
#include "mac/ca/inc/alloc.h"
#include "mac/common/timings.h"

#include "hal/phy/phy.h"
#include "inc/phy_stub.h"

#include "lib/test.h"

#include <string.h>
#include <stdarg.h>

#define NB_ITER 100000
#define NB_PEER 5
#define NB_GLID 20
#define RANDOM_START 0

#define PEER_MIN 10
#define MAX_SYMB 3

void
access_random_schedule (lib_rnd_t *rnd, ca_schedule_t *sched, uint length_tck)
{
    uint i;
    const uint nb = lib_rnd_uniform (rnd, 10/*CA_SCHEDULE_SIZE*/ - 3) + 3;
    /* Coexistence mode and encryption. */
    sched->coexistence_mode = MAC_COEXISTENCE_SHARED_CSMA_HYBRID_MODE;
    sched->snid = 5;
    sched->nek_switch = 0;
    /* Random allocations. */
    uint zoffset_tck = 0;
    uint avr_alloc_tck = length_tck/nb;
    uint low_alloc_tck = avr_alloc_tck - (avr_alloc_tck>>2); /* 3/4 */
    uint dif_alloc_tck = (avr_alloc_tck>>2); /* 1/4 */
    sched->allocations_nb = nb;
    for (i = 0; i < nb - 1; i++)
    {
        sched->allocations[i].end_offset_tck =
            lib_rnd_uniform (rnd, dif_alloc_tck)
            + zoffset_tck + low_alloc_tck;
        zoffset_tck = sched->allocations[i].end_offset_tck;
    }
    sched->allocations[nb - 1].end_offset_tck = length_tck;
    /* Random GLID. */
    static const struct
    {
        u8 glid_min;
        u8 glid_max;
        uint prob;
    } glid_prob[] = {
        { MAC_LID_SPC_HOLE, MAC_LID_SPC_HOLE, 8 },
        { MAC_GLID_MIN, MAC_GLID_MAX, 8 + 8 },
        { MAC_LID_CFPI, MAC_LID_CFPI, 8 + 8 + 1 },
        { MAC_LID_SHARED_CSMA, MAC_LID_SHARED_CSMA, 8 + 8 + 1 + 2 + 8 },
        { MAC_LID_LOCAL_CSMA, MAC_LID_LOCAL_CSMA, 8 + 8 + 1 + 2 + 8 + 8 },
    };
    sched->allocations[0].glid = MAC_LID_SPC_CENTRAL;
    for (i = 1; i < nb; i++)
    {
        uint r = lib_rnd_uniform (rnd, glid_prob[COUNT (glid_prob) - 1].prob);
        uint j;
        DICHOTOMY_SEARCH (0, COUNT (glid_prob), j, r < glid_prob[j].prob);
        dbg_assert (j < COUNT (glid_prob));
        sched->allocations[i].glid = glid_prob[j].glid_min
            + lib_rnd_uniform (rnd, glid_prob[j].glid_max
                               - glid_prob[j].glid_min + 1);
    }
}

/**
 * Get allocation end date without AIFS.
 * \param  ca  CA context
 * \param  date  date in allocation
 * \return  allocation end date
 */
static uint
access_allocation_end_date (ca_t *ca, uint date)
{
    dbg_assert (ca);
    ca_beacon_period_t *bp = ca_alloc_find_beacon_period (ca, date);
    dbg_assert (bp);
    ca_schedule_t *sched = &ca->schedules[bp->schedule_index];
    uint alloc_i = ca_alloc_find (sched, date - bp->start_date);
    dbg_assert (alloc_i < sched->allocations_nb);
    ca_allocation_t *alloc = &sched->allocations[alloc_i];
    return bp->start_date + alloc->end_offset_tck;
}

/**
 * Compute AIFS at given date.
 * \param  ca  CA context
 * \param  date  current date
 * \return  AIFS in tck
 */
static uint
access_aifs_tck (ca_t *ca, uint date)
{
    dbg_assert (ca);
    ca_beacon_period_t *bp = ca_alloc_find_beacon_period (ca, date);
    dbg_assert (bp);
    ca_schedule_t *sched = &ca->schedules[bp->schedule_index];
    uint alloc_i = ca_alloc_find (sched, date - bp->start_date);
    dbg_assert (alloc_i < sched->allocations_nb);
    ca_allocation_t *alloc = &sched->allocations[alloc_i];
    uint aifs_tck = MAC_LID_IS_BEACON (alloc->glid)
        ? MAC_B2BIFS_TCK : MAC_AIFS_TCK;
    if (alloc_i == sched->allocations_nb - 1
        && MAC_LID_IS_BEACON (ca->schedules[bp[1].schedule_index]
                              .allocations[0].glid)
        && MAC_B2BIFS_TCK > MAC_AIFS_TCK)
        aifs_tck = MAC_B2BIFS_TCK;
    return aifs_tck;
}

void
access_check_vcs_restart (test_t t, ca_t *ca, u32 date, uint duration_tck,
                          bool eifs, const ca_alloc_param_t *ap)
{
    test_within (t);
    dbg_assert (ca);
    ca_beacon_period_t *bp = ca_alloc_find_beacon_period (ca, date);
    dbg_assert (bp);
    ca_schedule_t *sched = &ca->schedules[bp->schedule_index];
    dbg_assert (sched->coexistence_mode < MAC_COEXISTENCE_NB);
    dbg_assert (sched->snid < 16);
    dbg_assert (sched->nek_switch < 2);
    dbg_assert (sched->allocations_nb);
    uint alloc_i = ca_alloc_find (sched, date - bp->start_date);
    dbg_assert (alloc_i < sched->allocations_nb);
    ca_allocation_t *alloc = &sched->allocations[alloc_i];
    ca_access_param_t *a = &ca->access.param;
    uint aifs_tck = access_aifs_tck (ca, date);
    uint alloc_end_date = bp->start_date + alloc->end_offset_tck - aifs_tck;
    if (ap)
    {
        /* Check current allocation parameters. */
        test_fail_unless (ap->coexistence_mode == sched->coexistence_mode);
        test_fail_unless (ap->snid == sched->snid);
        test_fail_unless (ap->nek_switch == sched->nek_switch);
        test_fail_unless (ap->end_date == alloc_end_date);
    }
    if (!ca->access.unusable)
    {
        /* Usable, the access should be prepared. */
        test_fail_unless (lesseq_mod2p32 (date, a->access_date));
        uint slot = CA_ALLOC_IS_CSMA (alloc->glid)
            ? (eifs ? 0 : 2) + ca->backoff.bc
            : 0;
        bool prp = CA_ALLOC_IS_CSMA (alloc->glid) && !eifs;
        test_fail_unless (a->access_date == date + duration_tck
                          + slot * MAC_SLOT_TCK
                          /* TODO: remove this: */
                          + (alloc->glid == MAC_LID_CFPI
                             ? MAC_US_TO_TCK (40) : 0));
        test_fail_unless (a->cw_start_date == date + duration_tck
                          + (prp ? 2 * MAC_SLOT_TCK : 0));
        test_fail_unless (a->aifs
                          || (a->duration_tck == alloc_end_date
                              - a->access_date));
        test_fail_unless (a->prp == prp);
        test_fail_unless (a->cfp == !CA_ALLOC_IS_CSMA (alloc->glid));
    }
    if (!a->aifs)
    {
        /* ACCESS scheduled. */
        test_fail_unless (lesseq_mod2p32 (a->access_date, alloc_end_date));
        test_fail_unless (ca->access.timer_date == a->access_date
                          - ca->anticipation_tck);
    }
    else
    {
        /* AIFS scheduled. */
        test_fail_unless (ca->access.timer_date == alloc_end_date);
    }
}

void
access_check_vcs_restart_eifs (test_t t, ca_t *ca, u32 date,
                               const ca_alloc_param_t *ap)
{
    const ca_alloc_param_t *cap = &ca->current_allocation_param;
    uint eifs_tck = ((cap->coexistence_mode
                      == MAC_COEXISTENCE_HYBRID_DELIMITERS_MODE)
                     || !cap->hybrid) ? MAC_EIFS_AV_TCK : MAC_EIFS_10_TCK;
    access_check_vcs_restart (t, ca, date, eifs_tck, true, ap);
}

void
access_check_defer (test_t t, ca_t *ca)
{
    test_within (t);
    dbg_assert (ca);
    test_fail_unless (ca->access.param.aifs);
    test_fail_unless (ca->access.timer_date !=
                      ca->current_allocation_param.end_date
                      - access_aifs_tck (ca, ca->access.timer_date));
}

void
access_check_aifs (test_t t, ca_t *ca, u32 date,
                   const ca_alloc_param_t *ap)
{
    dbg_assert_ptr (ap);
    access_check_vcs_restart (t, ca, access_allocation_end_date (ca, date), 0,
                              false, ap);
}

void
access_basic_test_case (test_t t)
{
    uint i, j;
    lib_rnd_t rnd[1];
    phy_t *phy;
    mac_config_t config;
    mac_store_t *store;
    ca_t *ca;
    test_case_begin (t, "basic");
    /* Initialise. */
    lib_rnd_init (rnd, 1234);
    phy = phy_init (NULL, NULL, NULL, NULL, NULL, NULL, NULL);
#if RANDOM_START
    phy->date = lib_rnd32 (rnd);
#else
    phy->date = MAC_MS_TO_TCK (500 / 50);
#endif
    mac_config_init (&config);
    config.tei = 1;
    store = mac_store_init ();
    ca = ca_init (phy, &config, store, 0);
    /* Characteristics for null slots are determined as-is:
     *  - for index < NB_GLID: i = index
     *    - lid = i != 0 ? i + MAC_GLID_MIN : MAC_LID_SPC_CENTRAL
     *    - cap = i % 4
     *    - tei = i % NB_PEER + PEER_MIN
     *    - bcast = false
     *    - cfp = true
     *  - for index >= NB_GLID: i = index - NB_GLID
     *    - lid = i % MAC_PLID_NB + MAC_PLID_MIN
     *    - cap = lid - MAC_PLID_MIN
     *    - tei = i >= MAC_PLID_NB * NB_PEER
     *          ? MAC_TEI_BCAST
     *          : i / MAC_PLID_NB + PEER_MIN
     *    - bcast = i >= MAC_PLID_NB * NB_PEER
     *    - cfp = false
     */
    mfs_tx_t *mfses[NB_GLID + MAC_PLID_NB * NB_PEER + 1];
    uint mfses_used = 0;
    memset (mfses, 0, sizeof (mfses));
    /* Now the big test.
     * Create random schedules and random MFS for the STA with TEI = [2,3]. */
    test_begin (t, "random test")
    {
        const int beacon_period_length_tck = MAC_MS_TO_TCK (1000 / 10);
        ca_beacon_period_t bps[2];
        /* Initialise first beacon period. */
        access_random_schedule (rnd, ca_alloc_get_schedule (ca, 3),
                                beacon_period_length_tck);
        bps[1].start_date = phy->date - beacon_period_length_tck / 2;
        bps[1].schedule_index = 3;
        for (i = 0; i < NB_ITER; i++)
        {
            /* Make new schedule. */
            access_random_schedule (rnd, ca_alloc_get_schedule (ca, i % 4),
                                    beacon_period_length_tck);
            /* Make new beacon period. */
            bps[0] = bps[1];
            bps[1].start_date = bps[0].start_date + beacon_period_length_tck;
            bps[1].schedule_index = i % 4;
            /* Update beacon period. */
            phy->date += CA_ACCESS_AIFS_ANTICIP_TCK;
            ca_alloc_update_beacon_periods (ca, bps, COUNT (bps));
            phy->date -= CA_ACCESS_AIFS_ANTICIP_TCK;
            /* First schedule. */
            if (i == 0)
            {
                const ca_alloc_param_t *ap =
                    ca_access_activate (ca, phy->date);
                access_check_vcs_restart_eifs (t, ca, phy->date, ap);
            }
            while (less_mod2p32 (phy->date, bps[1].start_date))
            {
                if (phy->date == ca->access.timer_date
                    || lib_rnd_flip_coin (rnd, LIB_RND_RATIO (0.5)))
                {
                    /* Next ACCESS. */
                    phy->date = ca->access.timer_date;
                    if (!ca->access.param.aifs)
                    {
                        mfs_tx_t *mfs = ca_access_get_mfs (ca);
                        int seg_sent;
                        uint fl_tck;
                        if (mfs)
                        {
                            /* Timings are completely approximated. */
                            seg_sent = mfs->seg_nb
                                ? lib_rnd_uniform (rnd, mfs->seg_nb) + 1 : 0;
                            fl_tck = MAC_PREAMBLE_TCK + MAC_FC_AV_TCK
                                + MAC_DX567_TCK * seg_sent;
                        }
                        if (!mfs
                            || mfs->seg_nb == 0
                            || fl_tck > ca->access.param.duration_tck)
                        {
                            if (mfs && mfs->seg_nb == 0
                                && ca->access.param.cfp)
                            {
                                test_fail_unless (!mfs->common.mme);
                                uint lid = mfs->common.lid;
                                test_fail_unless (lid >= MAC_LLID_MIN
                                                  && lid <= MAC_LLID_MAX);
                                /* Check this is the smallest LLID. */
                                while (lid-- > MAC_LLID_MIN)
                                    test_fail_if (
                                        mac_store_mfs_get_sta_tx_data_locked (
                                            store, lid, mfs->common.tei));
                            }
                            ca_access_done (ca);
                            ca_access_defer (ca, phy->date);
                            access_check_defer (t, ca);
                        }
                        else
                        {
                            ca_access_vcs_restart_eifs (ca, phy->date);
                            access_check_vcs_restart_eifs (t, ca, phy->date,
                                                           NULL);
                            if (seg_sent)
                            {
                                mfs->seg_nb -= seg_sent;
                                ca_access_done (ca);
                                ca_access_vcs_restart (ca,
                                                       phy->date + fl_tck);
                                access_check_vcs_restart (t, ca, phy->date,
                                                          fl_tck, false,
                                                          NULL);
                            }
                            else
                                ca_access_done (ca);
                        }
                    }
                    else
                    {
                        const ca_alloc_param_t *ap = ca_access_aifs (ca);
                        access_check_aifs (t, ca, phy->date, ap);
                    }
                }
                else
                {
                    /* Random event. */
                    dbg_assert (less_mod2p32 (phy->date,
                                              ca->access.timer_date));
                    u32 next_date = phy->date + lib_rnd_uniform (
                        rnd, ca->access.timer_date - phy->date);
                    /* TODO: For the moment, avoid bad cases where date is in
                     * AIFS. */
                    u32 danger_date = bps[1].start_date + MAC_US_TO_TCK (100);
                    if (!less_mod2p32 (next_date, danger_date))
                        next_date = danger_date;
                    phy->date = next_date;
                    if (mfses_used == 0
                        || lib_rnd_flip_coin (rnd, LIB_RND_RATIO (0.2)))
                    {
                        j = lib_rnd_uniform (rnd, COUNT (mfses));
                        if (!mfses[j])
                        {
                            /* Create a new MFS. */
                            uint lid, cap, tei;
                            bool bcast, cfp;
                            if (j < NB_GLID)
                            {
                                lid = j != 0 ? j + MAC_GLID_MIN
                                    : MAC_LID_SPC_CENTRAL;
                                cap = j % 4;
                                tei = j % NB_PEER + PEER_MIN;
                                bcast = false;
                                cfp = true;
                            }
                            else
                            {
                                uint sj = j - NB_GLID;
                                bcast = sj >= MAC_PLID_NB * NB_PEER;
                                cap = bcast ? 0: sj % MAC_PLID_NB;
                                lid = cap +
                                        (bcast ? MAC_PLID_MIN : MAC_LLID_MIN);
                                tei = bcast ? MAC_TEI_BCAST
                                    : sj / MAC_PLID_NB + PEER_MIN;
                                cfp = true;
                            }
                            bool added;
                            mfs_tx_t *mfs = mac_store_mfs_add_tx
                                (store, bcast, false, lid, tei, &added);
                            mfs->cfp = cfp;
                            dbg_assert (added);
                            mfs->seg_nb = lib_rnd_uniform (rnd, 100);
                            mfses[j] = mfs;
                            mfses_used++;
                            ca_mfs_add (ca, mfs);
                        }
                        else
                        {
                            /* Remove an MFS. */
                            mfs_tx_t *mfs = mfses[j];
                            ca_mfs_remove (ca, mfs);
                            mfs->seg_nb = 0;
                            mac_store_mfs_remove (store,
                                                  PARENT_OF (mfs_t, tx, mfs));
                            blk_release (mfs);
                            mfses[j] = NULL;
                            mfses_used--;
                        }
                    }
                    else
                    {
                        /* Modify an MFS. */
                        do {
                            j = lib_rnd_uniform (rnd, COUNT (mfses));
                        } while (!mfses[j]);
                        mfs_tx_t *mfs = mfses[j];
                        if (mfs->ca_state != CA_MFS_STATE_HELD)
                        {
                            mfs->seg_nb = lib_rnd_uniform (rnd, 100);
                            ca_mfs_update (ca, mfs);
                        }
                    }
                }
            }
        }
        ca_access_deactivate (ca);
    } test_end;
    /* Uninitialise. */
    for (i = 0; i < COUNT (mfses); i++)
    {
        mfs_tx_t *mfs = mfses[i];
        if (mfs)
        {
            ca_mfs_remove (ca, mfs);
            mac_store_mfs_remove (store, PARENT_OF (mfs_t, tx, mfs));
            blk_release (mfs);
        }
    }
    for (i = 0; i < NB_PEER; i++)
    {
        bool ok = mac_store_sta_remove (store, PEER_MIN + i);
        dbg_assert (ok);
    }
    ca_uninit (ca);
    phy_uninit (phy);
    mac_store_uninit (store);
}

/**
 * Test TDMA poll.
 * \param  t  test context
 * \param  sta_mask  mask of present stations
 * \param  rx_mask  mask of activated stations
 * \param  tx_mask  mask of MFS with content
 * \param  ...  list of polled STA TEI (uint), stop with 0
 */
void
access_tdma_poll_test (test_t t, u64 sta_mask, u64 rx_mask, u64 tx_mask, ...)
{
    test_within (t);
    int i;
    phy_t *phy;
    mac_config_t config;
    mac_store_t *store;
    ca_t *ca;
    /* Initialise contexts. */
    phy = phy_init (NULL, NULL, NULL, NULL, NULL, NULL, NULL);
    phy->date = 0;
    mac_config_init (&config);
    config.tei = 1;
    store = mac_store_init ();
    ca = ca_init (phy, &config, store, 0);
    /* Prepare schedule. */
    ca_schedule_t *sched = ca_alloc_get_schedule (ca, 0);
    sched->coexistence_mode = MAC_COEXISTENCE_AV_ONLY_MODE;
    sched->snid = 5;
    sched->nek_switch = 0;
    sched->allocations_nb = 1;
    sched->allocations[0].end_offset_tck = 1000000;
    sched->allocations[0].glid = MAC_LID_CFPI;
    /* Prepare beacon period. */
    ca_beacon_period_t bps[1] = { { .start_date = 0, .schedule_index = 0 } };
    ca_alloc_update_beacon_periods (ca, bps, COUNT (bps));
    /* Prepare STA. */
    bool added;
    u64 bit;
    for (i = 0, bit = 1; i < 64; i++, bit <<= 1)
    {
        bool bcast = i == 0;
        uint tei = bcast ? MAC_TEI_BCAST : i;
        uint lid = bcast ? MAC_PLID_MIN : MAC_LLID_MIN;
        if (sta_mask & bit)
        {
            mfs_tx_t *mfs = mac_store_mfs_add_tx (
                store, bcast, false, lid, tei, &added);
            dbg_assert (added);
            if (tx_mask & bit)
                mfs->seg_nb = 1;
            ca_mfs_add (ca, mfs);
            blk_release (mfs);
            if (rx_mask & bit)
            {
                sta_t *sta = mac_store_sta_get (store, i);
                sta->sppb = 1;
                ca_sta_update (ca, sta);
                blk_release (sta);
            }
        }
    }
    /* Compute schedule and check result. */
    ca_access_activate (ca, 0);
    ca_access_reprogram (ca, 0, 25000, MAC_TEI_UNASSOCIATED);
    va_list ap;
    uint expected_tei;
    va_start (ap, tx_mask);
    for (i = 0; (expected_tei = va_arg (ap, uint)); i++)
    {
        mfs_tx_t *the_mfs;
        the_mfs = ca_access_get_mfs (ca);
        test_fail_unless (
            the_mfs->common.tei == expected_tei,
            "unexpected tei at iteration %d: got %d, expected %d", i,
            the_mfs->common.tei, expected_tei);
        ca_access_done (ca);
    }
    va_end (ap);
    /* Remove MFS and STA. */
    for (i = 0, bit = 1; i < 64; i++, bit <<= 1)
    {
        bool bcast = i == 0;
        uint tei = bcast ? MAC_TEI_BCAST : i;
        uint lid = bcast ? MAC_PLID_MIN : MAC_LLID_MIN;
        if (sta_mask & bit)
        {
            mfs_t *mfs = mac_store_mfs_get (
                store, true, bcast, false, lid, tei);
            mac_store_mfs_remove (store, mfs);
            if (!bcast)
                dbg_check (mac_store_sta_remove (store, tei));
            blk_release (mfs);
        }
    }
    /* Cleanup. */
    ca_access_deactivate (ca);
    ca_uninit (ca);
    phy_uninit (phy);
    mac_store_uninit (store);
    dbg_assert (blk_check_memory ());
}

void
access_tdma_poll_test_case (test_t t)
{
    test_case_begin (t, "tdma poll");
    test_begin (t, "no sta")
    {
        access_tdma_poll_test (t, 1, 0, 0, MAC_TEI_BCAST, 0);
    } test_end;
    test_begin (t, "no data")
    {
        access_tdma_poll_test (t, 0x330ull, 0x300ull, 0ull,
                               8, 9, /* RX poll */
                               4, 5, 8, 9, /* Empty poll. */
                               4, 5, /* Empty poll... */
                               0);
    } test_end;
    test_begin (t, "data")
    {
        access_tdma_poll_test (t, 0x331ull, 0x000ull, 0x31ull,
                               4, 5, MAC_TEI_BCAST, /* TX poll. */
                               4, 5, MAC_TEI_BCAST, /* Again... */
                               0);
    } test_end;
    test_begin (t, "data rx tx")
    {
        access_tdma_poll_test (t, 0x11111ull, 0x01010ull, 0x10101ull,
                               4, 8, 12, 16, MAC_TEI_BCAST, /* RX & TX poll. */
                               8, 16, MAC_TEI_BCAST, /* TX poll only. */
                               0);
    } test_end;
}

/** TDMA test result, which MFS should be selected. */
enum access_tdma_polled_test_result_t
{
    ACCESS_TDMA_TEST_BCAST_MME,
    ACCESS_TDMA_TEST_BCAST_DATA,
    ACCESS_TDMA_TEST_MME,
    ACCESS_TDMA_TEST_DATA0,
    ACCESS_TDMA_TEST_DATA1,
    ACCESS_TDMA_TEST_DATA2,
    ACCESS_TDMA_TEST_DATA3,
    ACCESS_TDMA_TEST_NULL,
};

/** TDMA test MFS configuration. */
struct access_tdma_polled_test_mfs_t
{
    /** MFS exists. */
    bool present;
    /** Number of segments. */
    int seg_nb;
};

/** TDMA test configuration. */
struct access_tdma_polled_test_t
{
    /** Result of the test. */
    enum access_tdma_polled_test_result_t result;
    /** Schedule TEI. */
    uint tei;
    /** Repeat scheduling to test deficit. */
    uint repeat;
    /** MFS test configuration. */
    struct access_tdma_polled_test_mfs_t mfs[ACCESS_TDMA_TEST_NULL];
};

void
access_tdma_polled_test (test_t t,
                         const struct access_tdma_polled_test_t *conf)
{
    test_within (t);
    int i;
    const uint sta_tei = 5;
    phy_t *phy;
    mac_config_t config;
    mac_store_t *store;
    ca_t *ca;
    mfs_tx_t *mfs[ACCESS_TDMA_TEST_NULL];
    /* Check configuration. */
    dbg_assert (conf->tei == MAC_TEI_BCAST || conf->tei == sta_tei);
    /* Initialise contexts. */
    phy = phy_init (NULL, NULL, NULL, NULL, NULL, NULL, NULL);
    phy->date = 0;
    mac_config_init (&config);
    config.tei = 1;
    store = mac_store_init ();
    ca = ca_init (phy, &config, store, 0);
    /* Prepare schedule. */
    ca_schedule_t *sched = ca_alloc_get_schedule (ca, 0);
    sched->coexistence_mode = MAC_COEXISTENCE_AV_ONLY_MODE;
    sched->snid = 5;
    sched->nek_switch = 0;
    sched->allocations_nb = 1;
    sched->allocations[0].end_offset_tck = 1000000;
    sched->allocations[0].glid = MAC_LID_CFPI;
    /* Prepare beacon period. */
    ca_beacon_period_t bps[1] = { { .start_date = 0, .schedule_index = 0 } };
    ca_alloc_update_beacon_periods (ca, bps, COUNT (bps));
    /* Prepare MFS. */
    struct {
        bool bcast;
        bool mme;
        uint lid;
        uint tei;
    } mfs_param[ACCESS_TDMA_TEST_NULL] = {
        { true, true, MAC_LID_NONE, MAC_TEI_BCAST },
        { true, false, MAC_PLID_MIN, MAC_TEI_BCAST },
        { false, true, MAC_LID_NONE, sta_tei },
        { false, false, MAC_LLID_MIN + 0, sta_tei },
        { false, false, MAC_LLID_MIN + 1, sta_tei },
        { false, false, MAC_LLID_MIN + 2, sta_tei },
        { false, false, MAC_LLID_MIN + 3, sta_tei },
    };
    bool sta_added = false;
    bool added;
    for (i = 0; i < ACCESS_TDMA_TEST_NULL; i++)
    {
        if (conf->mfs[i].present)
        {
            mfs[i] = mac_store_mfs_add_tx (
                store, mfs_param[i].bcast, mfs_param[i].mme, mfs_param[i].lid,
                mfs_param[i].tei, &added);
            if (mfs_param[i].tei != MAC_TEI_BCAST)
                sta_added = true;
            dbg_assert (added);
            mfs[i]->seg_nb = conf->mfs[i].seg_nb;
            ca_mfs_add (ca, mfs[i]);
        }
        else
            mfs[i] = 0;
    }
    /* Compute schedule and check result. */
    ca_access_activate (ca, 0);
    ca_access_reprogram (ca, 0, 25000, conf->tei);
    int repeat = conf->repeat;
    mfs_tx_t *the_mfs;
    the_mfs = ca_access_get_mfs (ca);
    while (repeat--)
    {
        ca_access_done (ca);
        the_mfs = ca_access_get_mfs (ca);
    }
    if (the_mfs)
    {
        test_fail_unless (conf->result != ACCESS_TDMA_TEST_NULL);
        test_fail_unless (the_mfs == mfs[conf->result]);
    }
    else
        test_fail_unless (conf->result == ACCESS_TDMA_TEST_NULL);
    ca_access_done (ca);
    /* Remove MFS. */
    for (i = 0; i < ACCESS_TDMA_TEST_NULL; i++)
    {
        if (mfs[i])
        {
            ca_mfs_remove (ca, mfs[i]);
            mac_store_mfs_remove (store, PARENT_OF (mfs_t, tx, mfs[i]));
            blk_release (mfs[i]);
        }
    }
    if (sta_added)
    {
        bool ok = mac_store_sta_remove (store, sta_tei);
        dbg_assert (ok);
    }
    /* Cleanup. */
    ca_access_deactivate (ca);
    ca_uninit (ca);
    phy_uninit (phy);
    mac_store_uninit (store);
    dbg_assert (blk_check_memory ());
}

void
access_tdma_polled_test_case (test_t t)
{
    test_case_begin (t, "tdma polled");
    test_begin (t, "bcast null")
    {
        access_tdma_polled_test (t, &(struct access_tdma_polled_test_t) {
            .result = ACCESS_TDMA_TEST_NULL,
            .tei = MAC_TEI_BCAST,
            .mfs = {
                { .present = false },
                { .present = false },
                { .present = false },
                { .present = false },
            },
        });
    } test_end;
    test_begin (t, "bcast no seg")
    {
        access_tdma_polled_test (t, &(struct access_tdma_polled_test_t) {
            .result = ACCESS_TDMA_TEST_BCAST_DATA,
            .tei = MAC_TEI_BCAST,
            .mfs = {
                { .present = true },
                { .present = true },
                { .present = false },
                { .present = false },
            },
        });
    } test_end;
    test_begin (t, "bcast mme")
    {
        access_tdma_polled_test (t, &(struct access_tdma_polled_test_t) {
            .result = ACCESS_TDMA_TEST_BCAST_MME,
            .tei = MAC_TEI_BCAST,
            .mfs = {
                { .present = true, .seg_nb = 1 },
                { .present = true, .seg_nb = 0 },
                { .present = false },
                { .present = false },
            },
        });
        access_tdma_polled_test (t, &(struct access_tdma_polled_test_t) {
            .result = ACCESS_TDMA_TEST_BCAST_MME,
            .tei = MAC_TEI_BCAST,
            .mfs = {
                { .present = true, .seg_nb = 1 },
                { .present = true, .seg_nb = 1 },
                { .present = false },
                { .present = false },
            },
        });
        access_tdma_polled_test (t, &(struct access_tdma_polled_test_t) {
            .result = ACCESS_TDMA_TEST_BCAST_MME,
            .tei = 5,
            .mfs = {
                { .present = true, .seg_nb = 1 },
                { .present = false, .seg_nb = 1 },
                { .present = true, .seg_nb = 1 },
                { .present = true, .seg_nb = 1 },
            },
        });
    } test_end;
    test_begin (t, "bcast data")
    {
        access_tdma_polled_test (t, &(struct access_tdma_polled_test_t) {
            .result = ACCESS_TDMA_TEST_BCAST_DATA,
            .tei = MAC_TEI_BCAST,
            .mfs = {
                { .present = true, .seg_nb = 0 },
                { .present = true, .seg_nb = 1 },
                { .present = false },
                { .present = false },
            },
        });
    } test_end;
    test_begin (t, "null")
    {
        access_tdma_polled_test (t, &(struct access_tdma_polled_test_t) {
            .result = ACCESS_TDMA_TEST_NULL,
            .tei = 5,
            .mfs = {
                { .present = false },
                { .present = false },
                { .present = false },
                { .present = false },
            },
        });
    } test_end;
    test_begin (t, "no seg")
    {
        access_tdma_polled_test (t, &(struct access_tdma_polled_test_t) {
            .result = ACCESS_TDMA_TEST_BCAST_MME,
            .tei = 5,
            .mfs = {
                { .present = true, .seg_nb = 1 },
                { .present = false },
                { .present = true },
                { .present = true },
            },
        });
        access_tdma_polled_test (t, &(struct access_tdma_polled_test_t) {
            .result = ACCESS_TDMA_TEST_DATA0,
            .tei = 5,
            .mfs = {
                { .present = true },
                { .present = false },
                { .present = true },
                { .present = true },
            },
        });
    } test_end;
    test_begin (t, "mme")
    {
        access_tdma_polled_test (t, &(struct access_tdma_polled_test_t) {
            .result = ACCESS_TDMA_TEST_MME,
            .tei = 5,
            .mfs = {
                { .present = true },
                { .present = false },
                { .present = true, .seg_nb = 1 },
                { .present = true },
            },
        });
        access_tdma_polled_test (t, &(struct access_tdma_polled_test_t) {
            .result = ACCESS_TDMA_TEST_MME,
            .tei = 5,
            .mfs = {
                { .present = true },
                { .present = false },
                { .present = true, .seg_nb = 1 },
                { .present = true, .seg_nb = 1 },
            },
        });
    } test_end;
    test_begin (t, "data")
    {
        access_tdma_polled_test (t, &(struct access_tdma_polled_test_t) {
            .result = ACCESS_TDMA_TEST_DATA0,
            .tei = 5,
            .mfs = {
                { .present = true },
                { .present = false },
                { .present = true },
                { .present = true, .seg_nb = 1 },
            },
        });
    } test_end;
    test_begin (t, "data hi prio")
    {
        access_tdma_polled_test (t, &(struct access_tdma_polled_test_t) {
            .result = ACCESS_TDMA_TEST_DATA2, .tei = 5,
            .mfs = {
                { .present = true },
                { .present = false },
                { .present = true },
                { .present = true, .seg_nb = 1 },
                { .present = true },
                { .present = true, .seg_nb = 1 },
                { .present = true },
            },
        });
    } test_end;
    test_begin (t, "data low prio")
    {
        access_tdma_polled_test (t, &(struct access_tdma_polled_test_t) {
            .result = ACCESS_TDMA_TEST_DATA0,
            .tei = 5,
            .mfs = {
                { .present = true },
                { .present = false },
                { .present = true },
                { .present = true, .seg_nb = 1 },
                { .present = true },
                { .present = true },
                { .present = true },
            },
        });
    } test_end;
    test_begin (t, "data hi defer")
    {
        access_tdma_polled_test (t, &(struct access_tdma_polled_test_t) {
            .result = ACCESS_TDMA_TEST_DATA0,
            .tei = 5,
            .mfs = {
                { .present = true },
                { .present = false },
                { .present = true },
                { .present = true, .seg_nb = 2 },
                { .present = true },
                { .present = true, .seg_nb = 1 },
                { .present = true },
            },
        });
        access_tdma_polled_test (t, &(struct access_tdma_polled_test_t) {
            .result = ACCESS_TDMA_TEST_DATA1,
            .tei = 5,
            .mfs = {
                { .present = true },
                { .present = false },
                { .present = true },
                { .present = true, .seg_nb = 2 },
                { .present = true, .seg_nb = 100 },
                { .present = true, .seg_nb = 1 },
                { .present = true },
            },
        });
    } test_end;
    test_begin (t, "data dwrr")
    {
        access_tdma_polled_test (t, &(struct access_tdma_polled_test_t) {
            .result = ACCESS_TDMA_TEST_DATA0,
            .tei = 5,
            .mfs = {
                { .present = true },
                { .present = false },
                { .present = true },
                { .present = true, .seg_nb = 100 },
                { .present = true },
                { .present = true, .seg_nb = 10 },
                { .present = true },
            },
        });
        access_tdma_polled_test (t, &(struct access_tdma_polled_test_t) {
            .result = ACCESS_TDMA_TEST_DATA0,
            .tei = 5,
            .mfs = {
                { .present = true },
                { .present = false },
                { .present = true },
                { .present = true, .seg_nb = 100 },
                { .present = true },
                { .present = true, .seg_nb = 10 },
                { .present = true },
            },
            .repeat = 2,
        });
        access_tdma_polled_test (t, &(struct access_tdma_polled_test_t) {
            .result = ACCESS_TDMA_TEST_DATA2,
            .tei = 5,
            .mfs = {
                { .present = true },
                { .present = false },
                { .present = true },
                { .present = true, .seg_nb = 100 },
                { .present = true },
                { .present = true, .seg_nb = 10 },
                { .present = true },
            },
            .repeat = 5,
        });
    } test_end;
}

void
access_test_suite (test_t t)
{
    test_suite_begin (t, "access");
    access_basic_test_case (t);
    access_tdma_poll_test_case (t);
    access_tdma_polled_test_case (t);
}