summaryrefslogtreecommitdiff
path: root/cesar/ce/rx/test/src/test_rx.c
blob: df6008b185c7f33238a98413b8f3466757c1e547 (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
/* Cesar project {{{
 *
 * Copyright (C) 2009 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    ce/rx/test/src/test_rx.c
 * \brief   Test the CE in RX.
 * \ingroup test
 *
 */
#include "common/std.h"
#include "mac/common/store.h"
#include "ce/rx/rx.h"
#include "ce/rx/measure.h"
#include "ce/rx/inc/measure.h"
#include "ce/rx/inc/rx.h"
#include "ce/rx/bitloading/fsm/fsm.h"
#include "ce/rx/bitloading/inc/intervals.h"
#include "ce_rx_bl_fsm_defs.h"

#include "lib/test.h"
#include "lib/stats.h"
#include "lib/blk.h"
#include "lib/mbox.h"

#include <cyg/kernel/kapi.h>
#include <cyg/hal/hal_arch.h>

u8 thread_stack[CYGNUM_HAL_STACK_SIZE_TYPICAL];
cyg_handle_t thread_handler;
cyg_thread thread;
test_t test;
static int mbox_size = 0;
static ce_rx_measure_mbox_t *test_measure = NULL;

/**
 * Structure with all required tests context.
 */
typedef struct test_ce_rx
{
    sar_t *s;
    pbproc_t *p;
    mac_store_t *ms;
    mac_config_t mc;
    bsu_aclf_t ba;
} test_ce_rx_t;

/**
 * Common function to initialize lower layer.
 * \param  t  test suite context
 * \param  tcr  test ce rx context
 */
static void
test_ce_rx_ll_init (test_t t, test_ce_rx_t *tcr)
{
    tcr->p = (pbproc_t *) t;
    tcr->ms = mac_store_init ();
    tcr->s = sar_init (tcr->ms, NULL, NULL, NULL, 0x1);
    mac_config_init (&tcr->mc);
}

/**
 * Common function to un-initialize lower layer.
 * \param  tcr  test ce rx context
 */
static void
test_ce_rx_ll_uninit (test_ce_rx_t *tcr)
{
    mac_store_uninit (tcr->ms);
    sar_uninit (tcr->s);
}

/* Stub bitloadling. */
void
ce_rx_bl_fsm_handle_event (ce_rx_t *ce_rx, sta_t *sta,
                           ce_rx_bl_fsm_event_type_t e,
                           ce_rx_bitloading_fsm_event_param_t data)
{
    mbox_size--;
    /* Copy measure for analysis in test. */
    test_measure = data.measure;
    /* Suspend CE thread otherwise measure will be directly processed and can
     * not be used any more.
     * This is the normal behavior because SAR has higher priority than
     * CE. */
    cyg_thread_suspend (ce_rx->thread_handler);
    /* Restore to sane value. */
    test_measure = NULL;
}

/* Stub */
ce_rx_bl_fsm_event_type_t
ce_rx_bl_fsm_measure_to_event (sta_t *sta,
                               ce_rx_bitloading_fsm_event_param_t data)
{
    dbg_assert (sta);

    /* Prepare event types.
     * We create a array of two dimensions:
     * First dimension: is measure for intervals or global ?
     * Second dimension: Data, Sound complete or not.*/
    int has_interval, type;
    static const ce_rx_bl_fsm_event_type_t event_tab[2][3] =
    {
        {
            CE_RX_BL_FSM_EVENT_TYPE_data,
            CE_RX_BL_FSM_EVENT_TYPE_sound,
            CE_RX_BL_FSM_EVENT_TYPE_sound_completed
        },
        {
            CE_RX_BL_FSM_EVENT_TYPE_interval_data,
            CE_RX_BL_FSM_EVENT_TYPE_interval_sound,
            CE_RX_BL_FSM_EVENT_TYPE_interval_sound_completed
        }
    };

    /* Get measure. */
    ce_rx_measure_mbox_t *measure = data.measure;
    dbg_assert (measure);

    /* Get interval information. */
    int interval;
    s16 start_date_atu, end_date_atu;
    /* Global by default. */
    has_interval = CE_RX_FSM_MEASURE_TO_EVENT_TYPE_GLOBAL;
    measure->fsm_id = CE_RX_MEASURE_NO_INTERVAL;
    ce_rx_bl_intervals_measure_to_date (&measure->rx_params,
                                        &start_date_atu,
                                        &end_date_atu);
    interval = ce_rx_bl_intervals_measure_to_interval (sta->rx_tonemaps,
                                                       start_date_atu,
                                                       end_date_atu);
    if (interval != CE_RX_MEASURE_NO_INTERVAL)
    {
        /* We are on an interval. */
        has_interval = CE_RX_FSM_MEASURE_TO_EVENT_TYPE_INTERVAL;
        measure->fsm_id = interval % mac_interval_fsm_count_;
    }

    /* Is this measure a Sound ? */
    if (measure->rx_params.sound)
    {
        if (measure->rx_params.sound_complete)
            type = CE_RX_FSM_MEASURE_TO_EVENT_TYPE_SOUND_COMPLETED;
        else
            type = CE_RX_FSM_MEASURE_TO_EVENT_TYPE_SOUND;
    }
    /* This is a data. */
    else
        type = CE_RX_FSM_MEASURE_TO_EVENT_TYPE_DATA;

    return event_tab[has_interval][type];
}

void
test_rx_ce_suite (test_t t)
{
    test_ce_rx_t tcr;

    test_ce_rx_ll_init (t, &tcr);

    test_begin (t, "launch ce_rx thread")
    {
        ce_rx_t *ce_rx = ce_rx_init (tcr.ms, tcr.s, tcr.p, &tcr.mc, &tcr.ba);
        test_fail_if (mbox_peek (&ce_rx->measure_mbox) != 0);
        ce_rx_uninit (ce_rx);
    } test_end;

    test_ce_rx_ll_uninit (&tcr);
}

void
test_ce_rx_measure_suite (test_t t)
{
    pbproc_rx_params_t rx_params;
    rx_params.tei = 1;
    rx_params.multi_net_bcast = false;
    rx_params.preamble_ntb = 0;
    rx_params.beacon_period_start_ntb = 42;
    test_ce_rx_t tcr;

    test_ce_rx_ll_init (t, &tcr);

    ce_rx_t *ce_rx = ce_rx_init (tcr.ms, tcr.s, tcr.p, &tcr.mc, &tcr.ba);

    test_begin (t, "no RX params, no channel data & no PB stats")
    {
        bool catch = false;
        mbox_size = 1;
        dbg_fatal_try_begin
        {
            ce_rx_measure_sar_cb (ce_rx, NULL, 0, NULL, 0, 0, 0);
        }
        dbg_fatal_try_catch_void ()
        {
            catch = true;
        }
        dbg_fatal_try_end;
        test_fail_if (!catch);
        test_fail_if (mbox_size != 1);
        /* Resume CE thread. */
        cyg_thread_resume (ce_rx->thread_handler);
        test_fail_if (mbox_peek (&ce_rx->measure_mbox) != 0);
    } test_end;

    test_begin (t, "some RX params, but no channel data & no PB stats")
    {
        bool catch = false;
        mbox_size = 1;
        dbg_fatal_try_begin
        {
            ce_rx_measure_sar_cb (ce_rx, &rx_params, 0, NULL, 0, 0, 0);
        }
        dbg_fatal_try_catch_void ()
        {
            catch = true;
        }
        dbg_fatal_try_end;
        test_fail_if (!catch);
        test_fail_if (mbox_size != 1);
        /* Resume CE thread. */
        cyg_thread_resume (ce_rx->thread_handler);
        test_fail_if (mbox_peek (&ce_rx->measure_mbox) != 0);
    } test_end;

    test_begin (t, "some RX params & some channel data & no PB stats")
    {
        pb_t *pb = (pb_t *) blk_alloc_desc ();
        mbox_size = 1;
        ce_rx_measure_sar_cb (ce_rx, &rx_params, 0, pb, 1, 0, 0);
        test_fail_if (test_measure == NULL);
        test_fail_if (test_measure->chan_data_count != 1);
        test_fail_if (test_measure->chan_data != (phy_chandata_t *) pb);
        test_fail_if (test_measure->total_pb_count != 0);
        test_fail_if (test_measure->false_pb_count != 0);
        test_fail_if (test_measure->ber_sum != 0);
        test_fail_if (mbox_size != 0);
        /* Resume CE thread. */
        cyg_thread_resume (ce_rx->thread_handler);
        test_fail_if (mbox_peek (&ce_rx->measure_mbox) != 0);
    } test_end;

    test_begin (t, "some RX params & some channel data & PB stats")
    {
        pb_t *pb = (pb_t *) blk_alloc_desc ();
        mbox_size = 1;
        ce_rx_measure_sar_cb (ce_rx, &rx_params, 42, pb, 1, 24, 4242);
        test_fail_if (mbox_size != 0);
        test_fail_if (test_measure == NULL);
        test_fail_if (test_measure->chan_data_count != 1);
        test_fail_if (test_measure->chan_data != (phy_chandata_t *) pb);
        test_fail_if (test_measure->total_pb_count != 42);
        test_fail_if (test_measure->false_pb_count != 24);
        test_fail_if (test_measure->ber_sum != 4242);
        /* Resume CE thread. */
        cyg_thread_resume (ce_rx->thread_handler);
        test_fail_if (mbox_peek (&ce_rx->measure_mbox) != 0);
    } test_end;

    test_begin (t, "handle too much measure")
    {
        /* Stop CE thread to store many measure. */
        cyg_thread_suspend (ce_rx->thread_handler);
        uint i;
        for (i = 0; i < CE_RX_MEASURE_MBOX_MAX_SIZE; i++)
        {
            pb_t *pb = (pb_t *) blk_alloc_desc ();
            ce_rx_measure_sar_cb (ce_rx, &rx_params, 42, pb, 1, 24, 4242);
        }
        test_fail_if (mbox_peek (&ce_rx->measure_mbox) !=
                      CE_RX_MEASURE_MBOX_MAX_SIZE);

        /* Try to add a measurement without channel data. */
        ce_rx_measure_sar_cb (ce_rx, &rx_params, 42, NULL, 0, 24, 4242);
        test_fail_if (mbox_peek (&ce_rx->measure_mbox) !=
                      CE_RX_MEASURE_MBOX_MAX_SIZE);

        /* Try to add a channel data measurement. */
        pb_t *pb = (pb_t *) blk_alloc_desc ();
        ce_rx_measure_sar_cb (ce_rx, &rx_params, 42, pb, 1, 24, 4242);
        test_fail_if (mbox_peek (&ce_rx->measure_mbox) !=
                      CE_RX_MEASURE_MBOX_MAX_SIZE + 1);

        /* Clean. */
        for (i = 0; i <= CE_RX_MEASURE_MBOX_MAX_SIZE + 1; i++)
            cyg_thread_resume (ce_rx->thread_handler);
        test_fail_if (mbox_peek (&ce_rx->measure_mbox) != 0);

    } test_end;

    test_begin (t, "measure from unassociated station")
    {
        pb_t *pb = (pb_t *) blk_alloc_desc ();
        mbox_size = 0;
        rx_params.tei = MAC_TEI_UNASSOCIATED;
        rx_params.multi_net_bcast = false;
        ce_rx_measure_sar_cb (ce_rx, &rx_params, 0, pb, 1, 0, 0);
        test_fail_if (mbox_size != 0);
        /* Resume CE thread. */
        cyg_thread_resume (ce_rx->thread_handler);
        test_fail_if (mbox_peek (&ce_rx->measure_mbox) != 0);
        /* Clean. */
        blk_release_desc ((blk_t *) pb);
    } test_end;

    test_begin (t, "measure from multi-unicast")
    {
        pb_t *pb = (pb_t *) blk_alloc_desc ();
        mbox_size = 0;
        rx_params.tei = MAC_TEI_STA_MIN;
        rx_params.multi_net_bcast = true;
        ce_rx_measure_sar_cb (ce_rx, &rx_params, 0, pb, 1, 0, 0);
        test_fail_if (mbox_size != 0);
        /* Resume CE thread. */
        cyg_thread_resume (ce_rx->thread_handler);
        test_fail_if (mbox_peek (&ce_rx->measure_mbox) != 0);
        /* Clean. */
        blk_release_desc ((blk_t *) pb);
    } test_end;

    /* Clean. */
    dbg_assert (mac_store_sta_remove (tcr.ms, 1) == true);
    ce_rx_uninit (ce_rx);
    test_ce_rx_ll_uninit (&tcr);
}

void
test_ce_rx_get_snr (test_t t)
{
    const uint tei = 1;
    test_ce_rx_t tcr;
    u16 ber = 0x42;

    test_ce_rx_ll_init (t, &tcr);
    ce_rx_t *ce_rx = ce_rx_init (tcr.ms, tcr.s, tcr.p, &tcr.mc, &tcr.ba);

    mac_store_sta_add (tcr.ms, tei);
    sta_t *sta = mac_store_sta_get (tcr.ms, tei);
    dbg_assert (sta);
    sta->ce_rx_bt.noise_nrj = INVALID_PTR;

    test_case_begin (t, "get SNR");

    test_begin (t, "get SNR, no station")
    {
        test_fail_if (ce_rx_get_nsr (ce_rx, tei + 1, 0, 0, &ber) != NULL);
        test_fail_if (ber != 0x42);
    } test_end;

    test_begin (t, "get SNR, bit loading not ready")
    {
        sta->ce_rx_bt.fsm = 0;
        test_fail_if (ce_rx_get_nsr (ce_rx, tei, 0, 0, &ber) != NULL);
        test_fail_if (ber != 0x42);
    } test_end;

    test_begin (t, "get SNR, bit loading initial done")
    {
        sta->ce_rx_bt.fsm = CE_RX_BL_FSM_STATE_TRACKING;
        test_fail_if (ce_rx_get_nsr (ce_rx, tei, 0, 0, &ber) != INVALID_PTR);
        test_fail_if (ber != 0);
    } test_end;

    test_begin (t, "get SNR, intervals version")
    {
        sta->rx_tonemaps->intervals->version = 0x42;
        test_fail_if (ce_rx_get_nsr (ce_rx, tei, 0, 0, &ber) != NULL);
        test_fail_if (ce_rx_get_nsr (ce_rx, tei, 0, 0x42, &ber) !=
                      INVALID_PTR);
    } test_end;

    /* Clean. */
    blk_release (sta);
    dbg_assert (mac_store_sta_remove (tcr.ms, tei) == true);
    ce_rx_uninit (ce_rx);
    test_ce_rx_ll_uninit (&tcr);
}

void
test_ce_rx_stats (test_t t)
{
    test_case_begin (t, "stats");

    test_ce_rx_t tcr;

    test_ce_rx_ll_init (t, &tcr);

    test_begin (t, "init")
    {
        ce_rx_t *ce_rx = ce_rx_init (tcr.ms, tcr.s, tcr.p, &tcr.mc, &tcr.ba);
        test_fail_if (ce_rx->stats.resend_tm != 0);

        ce_rx->stats.resend_tm = 0x42;
        ce_rx_stats_init (ce_rx);
        test_fail_if (ce_rx->stats.resend_tm != 0);

        ce_rx_uninit (ce_rx);
    } test_end;

    test_ce_rx_ll_uninit (&tcr);
}

void
test_rx_ce_thread (cyg_addrword_t data)
{
    lib_stats_init ();

    test_rx_ce_suite (test);

    test_ce_rx_measure_suite (test);

    test_ce_rx_get_snr (test);

    test_ce_rx_stats (test);

    lib_stats_uninit ();

    test_begin (test, "memory")
    {
        test_fail_unless (blk_check_memory ());
    } test_end;

    test_result (test);

    HAL_PLATFORM_EXIT (test_nb_failed (test) == 0 ? 0 : 1);
}

int
main (int argc, char **argv)
{
    test_init (test, argc, argv);

    /* Create the ECos thread. */
    cyg_thread_create (CE_RX_THREAD_PRIORITY + 5, &test_rx_ce_thread,
                       (cyg_addrword_t) NULL, "Test CE RX",
                       thread_stack, sizeof (thread_stack),
                       &thread_handler, &thread);
    cyg_thread_resume (thread_handler);

    return 1;
}