summaryrefslogtreecommitdiff
path: root/cesar/bsu/test/utest/src/schedules.c
blob: 6bf6926f899cf6198f01bb14d86a4ebb3da6567b (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
/* Cesar project {{{
 *
 * Copyright (C) 2010 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    bsu/test/utest/src/schedules.c
 * \brief   BSU schedule tests.
 * \ingroup bsu
 */
#include "common/std.h"
#include "lib/test.h"
#include "bsu/bsu.h"
#include "mac/common/timings.h"
#include "bsu/test/utest/tests.h"
#include "bsu/inc/bsu.h"
#include "bsu/inc/context.h"
#include "bsu/inc/interface.h"
#include <string.h>

void
bsu_schedules_merge (bsu_t *ctx, bsu_beacon_t *beacon,
                     ca_schedule_t *schedules, uint nb);

void
bsu_ca_schedules (bsu_t *ctx, bsu_avln_t *avln);

void
bsu_timer_event_process (void *ud);

void
test_case_ca_schedules (test_t test)
{
    bsu_test_t t;
    bsu_avln_t avln;
    bsu_test_init (&t);
    test_case_begin (test, "CA Schedules");
    test_begin (test, "Merge function")
    {
        uint i, j;
        ca_schedule_t scheds_cmp [3];
        scheds_cmp[0].allocations[0].end_offset_tck = MAC_ATU_TO_TCK (500);
        scheds_cmp[0].allocations[0].glid = MAC_LID_SPC_BEACON_LISTEN;
        scheds_cmp[0].allocations[1].end_offset_tck = MAC_ATU_TO_TCK (700);
        scheds_cmp[0].allocations[1].glid = MAC_GLID_MIN;
        scheds_cmp[0].allocations[2].end_offset_tck = MAC_ATU_TO_TCK (1000);
        scheds_cmp[0].allocations[2].glid = MAC_GLID_MIN;
        scheds_cmp[0].allocations[3].end_offset_tck = MAC_ATU_TO_TCK (1600);
        scheds_cmp[0].allocations[3].glid = MAC_LID_SPC_HOLE;
        scheds_cmp[0].allocations[4].end_offset_tck = MAC_ATU_TO_TCK (2500);
        scheds_cmp[0].allocations[4].glid = MAC_GLID_MIN;
        scheds_cmp[0].allocations[5].end_offset_tck = MAC_ATU_TO_TCK (3907);
        scheds_cmp[0].allocations[5].glid = MAC_GLID_MIN;
        /* next */
        scheds_cmp[1].allocations[0].end_offset_tck = MAC_ATU_TO_TCK (500);
        scheds_cmp[1].allocations[0].glid = MAC_LID_SPC_BEACON_LISTEN;
        scheds_cmp[1].allocations[1].end_offset_tck = MAC_ATU_TO_TCK (700);
        scheds_cmp[1].allocations[1].glid = MAC_GLID_MIN;
        scheds_cmp[1].allocations[2].end_offset_tck = MAC_ATU_TO_TCK (1000);
        scheds_cmp[1].allocations[2].glid = MAC_GLID_MIN;
        scheds_cmp[1].allocations[3].end_offset_tck = MAC_ATU_TO_TCK (1600);
        scheds_cmp[1].allocations[3].glid = MAC_LID_SPC_HOLE;
        scheds_cmp[1].allocations[4].end_offset_tck = MAC_ATU_TO_TCK (2500);
        scheds_cmp[1].allocations[4].glid = MAC_GLID_MIN;
        scheds_cmp[1].allocations[5].end_offset_tck = MAC_ATU_TO_TCK (3907);
        scheds_cmp[1].allocations[5].glid = MAC_GLID_MIN;
        /* next */
        scheds_cmp[2].allocations[0].end_offset_tck = MAC_ATU_TO_TCK (50);
        scheds_cmp[2].allocations[0].glid = MAC_LID_SPC_BEACON_LISTEN;
        scheds_cmp[2].allocations[1].end_offset_tck = MAC_ATU_TO_TCK (500);
        scheds_cmp[2].allocations[1].glid = MAC_GLID_MIN;
        scheds_cmp[2].allocations[2].end_offset_tck = MAC_ATU_TO_TCK (700);
        scheds_cmp[2].allocations[2].glid = MAC_GLID_MIN;
        scheds_cmp[2].allocations[3].end_offset_tck = MAC_ATU_TO_TCK (1600);
        scheds_cmp[2].allocations[3].glid = MAC_GLID_MIN;
        scheds_cmp[2].allocations[4].end_offset_tck = MAC_ATU_TO_TCK (2500);
        scheds_cmp[2].allocations[4].glid = MAC_GLID_MIN;
        scheds_cmp[2].allocations[5].end_offset_tck = MAC_ATU_TO_TCK (3907);
        scheds_cmp[2].allocations[5].glid = MAC_GLID_MIN;
        ca_schedule_t scheds;
        bsu_test_avln_create (&t, &avln);
        for (i = 0; i < COUNT(scheds_cmp); i++)
        {
            bsu_schedules_merge (t.bsu, &avln.beacon, &scheds, i);
            for (j = 0; j < scheds.allocations_nb; j++)
            {
                test_fail_unless (scheds.allocations[j].end_offset_tck
                              == scheds_cmp[i].allocations[j].end_offset_tck);
                test_fail_unless (scheds.allocations[j].glid
                                  == scheds_cmp[i].allocations[j].glid);
            }
        }
    }
    test_end;
    test_begin (test, "Ca schedules")
    {
        uint i, index;
        u32 bpsd[BSU_ACLF_BPSD_NB-1];
        ca_schedule_t *sched;
        t.bsu->ca_index = 0;
        index = t.bsu->ca_index;
        bsu_test_avln_create (&t, &avln);
        bsu_ca_schedules (t.bsu, t.bsu->sta_avln);
        bsu_aclf_beacon_period_start_date (t.bsu->aclf, bpsd, COUNT (bpsd));
        for (i = 0; i < t.ca.nb_beacon_periods; i++)
        {
            sched = ca_alloc_get_schedule (t.bsu->ca, i + index);
            test_fail_unless (bpsd[i] == t.ca.beacon_periods[i].start_date);
            test_fail_unless (i + index
                              == t.ca.beacon_periods[i].schedule_index);
        }
    }
    test_end;
    bsu_test_uninit (&t);
}

void
test_case_bsu_schedules_countdowns_init (bsu_t *bsu, ca_schedule_t *cas)
{
    bsu->sta_avln->beacon.bmis.change_snid.present = true;
    bsu->sta_avln->beacon.bmis.change_snid.snidccd = 3;
    bsu->sta_avln->beacon.bmis.change_snid.new_snid = 3;
    bsu->sta_avln->beacon.bmis.eks.present = true;
    bsu->sta_avln->beacon.bmis.eks.kccd = 3;
    bsu->sta_avln->beacon.bmis.eks.kbc = BSU_BEACON_EKS_KBC_NEK;
    bsu->sta_avln->beacon.bmis.eks.new_eks = 4;
    bsu->sta_avln->beacon.bmis.change_hm.present = true;
    bsu->sta_avln->beacon.bmis.change_hm.hmccd = 3;
    bsu->sta_avln->beacon.bmis.change_hm.newhm = 1;
    bsu->nek_switch = 0;
    cas[0].snid = bsu->sta_avln->snid;
    cas[0].coexistence_mode = bsu->sta_avln->beacon.vf.hm;
    cas[0].nek_switch = bsu->nek_switch;
    cas[1].snid = bsu->sta_avln->snid;
    cas[1].coexistence_mode = bsu->sta_avln->beacon.vf.hm;
    cas[1].nek_switch = bsu->nek_switch;
    cas[2].snid = bsu->sta_avln->beacon.bmis.change_snid.new_snid;
    cas[2].coexistence_mode = bsu->sta_avln->beacon.bmis.change_hm.newhm;
    cas[2].nek_switch = !bsu->nek_switch;
}

void
test_case_bsu_schedules_countdowns (test_t t)
{
    test_suite_begin (t, "Countdowns");
    test_begin (t, "SNID/EKS/HM change")
    {
        uint i, j;
        bsu_beacon_t beacon;
        ca_schedule_t *casched;
        bsu_test_t ctx;
        pbproc_tx_beacon_params_t bparams;
        pbproc_rx_beacon_params_t bparamsrx;
        bsu_test_init (&ctx);
        bsu_activate (true);
        bsu_test_create_beacon (&ctx, &beacon);
        pb_beacon_t *pbbeacon = bsu_beacon_write (
            &beacon, BSU_BEACON_TYPE_CENTRAL, &ctx.mac_config, &bparams);
        ctx.mac_config.tei = 0xA;
        bparamsrx.snid = 0;
        pbbeacon->phy_pb.pb_rx.pb_measurement.crc_error = false;
        bsu_beacon_recv (ctx.bsu, pbbeacon, &bparamsrx);
        /* Clear memory. */
        blk_release_desc ((blk_t*) pbbeacon);
        ctx.ul.beacon = INVALID_PTR;
        ctx.ul.bparams = NULL;
        /* Continue the test. */
        bsu_track_avln (beacon.vf.nid, 0x0, ctx.mac_config.tei);
        ctx.bsu->is_sta = BSU_UPDATE_STA_TYPE_STA;
        for (i = 0; i < BSU_ACLF_BPSD_NB; i++)
            ctx.bsu->aclf->bpsd[i] = i * BSU_ACLF_BP_50HZ_TCK;
        ca_schedule_t cas[3];
        test_case_bsu_schedules_countdowns_init (ctx.bsu, cas);
        beacon.bmis.change_hm = ctx.bsu->sta_avln->beacon.bmis.change_hm;
        beacon.bmis.change_hm.hmccd--;
        beacon.bmis.change_snid = ctx.bsu->sta_avln->beacon.bmis.change_snid;
        beacon.bmis.change_snid.snidccd--;
        beacon.bmis.eks = ctx.bsu->sta_avln->beacon.bmis.eks;
        beacon.bmis.eks.kccd--;
        for (i = 0; i < COUNT (cas); i++)
        {
            ctx.bsu->ca_index = 0;
            /* Prepare expected results. */
            for (i = 0; i < CA_SCHEDULE_NB; i++)
                memset (ca_alloc_get_schedule (INVALID_PTR, i), 0xff,
                        sizeof (ca_schedule_t));
            /* station does not receive the last beacon. */
            ctx.bsu->sta_avln->beacon.beacon_period_start_date =
                phy_date () - BSU_ACLF_BP_50HZ_TCK;
            if (i != 1)
            {
                pb_beacon_t *b = bsu_beacon_write (
                    &beacon, BSU_BEACON_TYPE_CENTRAL, &ctx.mac_config, &bparams);
                ((pb_t *) b)->phy_pb.pb_rx.pb_measurement.crc_error = false;
                bparamsrx.preamble_date = bparamsrx.preamble_sysdate = 0;
                bparamsrx.bts = 0;
                /* On beacon reception only schedules are read and CA is
                 * programmed with the current data. */
                bsu_beacon_recv (ctx.bsu, b, &bparamsrx);
            }
            /* On timer event the snid change, NEK and hybrid mode are changed
             * in the lower layers. */
            bsu_timer_event_process (ctx.bsu);
            for (j = 0; j < COUNT (cas); j++)
            {
                casched = ca_alloc_get_schedule (INVALID_PTR, j);
                test_fail_unless (casched->snid == cas[j].snid);
                test_fail_unless (casched->nek_switch == cas[j].nek_switch);
                test_fail_unless (casched->coexistence_mode ==
                                  cas[j].coexistence_mode);
            }
            cas[0] = cas[1];
            cas[1] = cas[2];
            if (i != 1)
            {
                /* Release beacon. */
                blk_release_desc ((blk_t*) ctx.ul.beacon);
                ctx.ul.beacon = INVALID_PTR;
            }
            beacon.bmis.change_snid.snidccd --;
            beacon.bmis.change_snid.present =
                beacon.bmis.change_snid.snidccd != 0;
            if (beacon.bmis.change_snid.present == false)
                bparamsrx.snid = beacon.bmis.change_snid.new_snid;
            beacon.bmis.eks.kccd--;
            beacon.bmis.eks.present = beacon.bmis.eks.kccd != 0;
            beacon.bmis.change_hm.hmccd--;
            beacon.bmis.change_hm.present = beacon.bmis.change_hm.hmccd != 0;
            if (beacon.bmis.change_hm.present == false)
                beacon.vf.hm = beacon.bmis.change_hm.newhm;
        }
        bsu_test_uninit (&ctx);
    }
    test_end;
}

void
test_case_ca_schedules_intellon (test_t test)
{
    test_case_begin (test, "Intellon central beacons");
    test_begin (
        test, "Persistent schedule valid for the current beacon period")
    {
        bsu_test_t t;
        bsu_test_init (&t);
        bsu_beacon_t beacon;
        pb_beacon_t *pbbeacon;
        bsu_test_create_beacon (&t, &beacon);
        beacon.bmis.nps.ns = 0;
        beacon.bmis.ps.nb = 1;
        beacon.bmis.ps.ps[0].ns = 1;
        beacon.bmis.ps.ps[0].pscd = 0;
        beacon.bmis.ps.ps[0].cscd = 0;
        beacon.bmis.ps.ps[0].sais[0].stpf = true;
        beacon.bmis.ps.ps[0].sais[0].start_time_atu = 52;
        beacon.bmis.ps.ps[0].sais[0].end_time_atu = 3907;
        beacon.bmis.ps.ps[0].sais[0].glid = MAC_LID_SHARED_CSMA & 0x7f;
        uint i;
        for (i = 0; i < COUNT (t.bsu->aclf->bpsd); i++)
            t.bsu->aclf->bpsd[i] = i * BSU_ACLF_BP_50HZ_TCK;
        t.bsu->aclf->beacon_period = BSU_ACLF_BP_50HZ_TCK;
        *((uint*) &t.bsu->aclf->bp) = BSU_ACLF_BP_50HZ_TCK;
        pbproc_tx_beacon_params_t btx;
        pbproc_rx_beacon_params_t brx;
        memset (&brx, 0, sizeof (pbproc_rx_beacon_params_t));
        pbbeacon = bsu_beacon_write (
            &beacon, BSU_BEACON_TYPE_CENTRAL, &t.mac_config, &btx);
        pbbeacon->phy_pb.pb_rx.pb_measurement.crc_error = false;
        /* The first time bsu receives a beacon of an AVLN. */
        brx.bts = BSU_ACLF_BP_50HZ_TCK / 2;
        bsu_beacon_process (t.bsu, pbbeacon, &brx);
        /* Control plane request to track this AVLN. */
        bsu_track_avln (beacon.vf.nid, brx.snid, 0);
        t.bsu->is_sta = BSU_UPDATE_STA_TYPE_STA;
        /* BSU receives the next beacon. */
        bsu_beacon_process (t.bsu, pbbeacon, &brx);
        /* Schedules which should be present. */
        test_fail_unless (t.bsu->sta_avln->beacon.bmis.ps.nb == 1);
        test_fail_unless (t.bsu->sta_avln->beacon.bmis.ps.ps[0].pscd == 0);
        test_fail_unless (t.bsu->sta_avln->beacon.bmis.ps.ps[0].cscd == 7);
        test_fail_unless (
            t.bsu->sta_avln->beacon.bmis.ps.ps[0].sais[0].glid ==
            (MAC_LID_SHARED_CSMA & 0x7f));
        test_fail_unless (
            t.bsu->sta_avln->beacon.bmis.ps.ps[0].sais[0].end_time_atu ==
            3907);
        test_fail_unless (
            t.bsu->sta_avln->beacon.bmis.ps.ps[0].sais[0].stpf == false);
        u32 bpsd[4];
        bsu_aclf_beacon_period_start_date (t.bsu->aclf, bpsd, COUNT (bpsd));
        for (i = 0; i < COUNT (bpsd); i++)
            test_fail_unless (brx.bts + i * BSU_ACLF_BP_50HZ_TCK == bpsd[i] +
                              t.bsu->sta_avln->sync.ntb_offset_tck);
        blk_release_desc ((blk_t*) pbbeacon);
        bsu_test_uninit (&t);
    }
    test_end;
}

void
test_case_ca_schedules_special_allocations__test (
    test_t test, char *text, bsu_update_sta_type_t type)
{
    bsu_test_t t;
    bsu_avln_t avln;
    bsu_test_init (&t);
    test_begin (test, text)
    {
        uint i;
        ca_schedule_t sched_cmp;
        sched_cmp.allocations[0].end_offset_tck = MAC_ATU_TO_TCK (500);
        if (type == BSU_UPDATE_STA_TYPE_STA)
            sched_cmp.allocations[0].glid = MAC_LID_SPC_BEACON_LISTEN;
        else
            sched_cmp.allocations[0].glid = MAC_LID_SPC_CENTRAL;
        sched_cmp.allocations[1].end_offset_tck = MAC_ATU_TO_TCK (3907);
        sched_cmp.allocations[1].glid = MAC_LID_SHARED_CSMA;
        t.bsu->is_sta = type;
        memset (&avln, 0, sizeof (bsu_avln_t));
        avln.beacon.bmis.nps.ns = 1;
        avln.beacon.bmis.nps.sais[0].stpf = true;
        avln.beacon.bmis.nps.sais[0].start_time_atu = 500;
        avln.beacon.bmis.nps.sais[0].end_time_atu = 3907;
        avln.beacon.bmis.nps.sais[0].glid = MAC_LID_SHARED_CSMA & 0x7f;
        ca_schedule_t sched;
        bsu_schedules_merge (t.bsu, &avln.beacon, &sched, 0);
        for (i = 0; i < sched.allocations_nb; i++)
        {
            test_fail_unless (sched.allocations[i].end_offset_tck ==
                              sched_cmp.allocations[i].end_offset_tck);
            test_fail_unless (sched.allocations[i].glid ==
                              sched_cmp.allocations[i].glid);
        }
    }
    test_end;
    bsu_test_uninit (&t);
}

void
test_case_ca_schedules_special_allocations (test_t t)
{
    test_case_ca_schedules_special_allocations__test (
        t, "Region beacon acts as STA", BSU_UPDATE_STA_TYPE_STA);
    test_case_ca_schedules_special_allocations__test (
        t, "Region beacon acts as CCO", BSU_UPDATE_STA_TYPE_CCO);
}

void
test_suite_bsu_schedule (test_t t)
{
    test_suite_begin (t, "BSU schedules");
    test_case_ca_schedules (t);
    test_case_bsu_schedules_countdowns (t);
    test_case_ca_schedules_intellon (t);
    test_case_ca_schedules_special_allocations (t);
}