summaryrefslogtreecommitdiff
path: root/ce/test/rx/general/src/test_sar_integration.c
blob: e580928cea7a4ae86cc133c482ab37be5fb02ab2 (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
/* Cesar project {{{
 *
 * Copyright (C) 2007 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    ce/test/rx/sar_integration.c
 * \brief   Use sar callback for Channel estimation.
 * \ingroup ce
 *
 */
#include "common/std.h"
#include "cyg/hal/hal_intr.h"
#include "lib/test.h"
#include "mac/sar/sar.h"
#include "ce/inc/rx.h"
#include "mac/common/pb.h" // PB header initialisation
#include "ce/test/common/print_utils.h"
#include "cp/interf/interf.h"
//#include "mac/sar/inc/trace.h"
#include "mac/common/ntb.h"
#include "ce/inc/trace.h"
#include "hal/phy/phy.h"
#include "ce/inc/bitloading.h"

#define FRAME_PRIORITY 10
cyg_thread frame_thread;
cyg_handle_t frame_handle;
unsigned char frame_stack[CYGNUM_HAL_STACK_SIZE_TYPICAL];
#define RXCE_PRIORITY 14
cyg_thread rxce_thread;
cyg_handle_t rxce_handle;
unsigned char rxce_stack[CYGNUM_HAL_STACK_SIZE_TYPICAL];

test_t test;
mac_store_t *mac_store_ctx;
mac_config_t mac_config;
rxce_t *rxce_ctx;
phy_t *phy;

u32
phy_date (phy_t *phy)
{
    return (cyg_current_time());
}

void
pbproc_scf (void)
{
    ce_print("pbproc_scf()\n");
}

void
emul_reassembly (int pb_nb, uint date, pbproc_rx_params_t **rx_params,
                pb_t **f, pb_t **l, phy_chandata_t **noise)
{
    uint i, j;
    if (pb_nb > 0)
    {
        *f = (pb_t *) blk_alloc_desc_range (pb_nb,(blk_t **)l);
        (*l)->next = NULL;
        phy_pb_rx_t *p = (phy_pb_rx_t *) *f;
        for (i=0; i<(uint) pb_nb; i++)
        {
            p->pb_measurement.ber = i;
            ((pb_t *)p)->header.vpbf = 0;
            p = (phy_pb_rx_t *) p->blk.next;
        }
    }
    else
    {
        *f = *l = NULL;
    }

    phy_chandata_t *l_noise, *p_noise;
    phy_chandata_t *f_noise = (phy_chandata_t *)  blk_alloc_desc_range (mpdu_measure_chandata_blk_nb[PHY_CHANDATA_TYPE_NRJ], (blk_t **)&l_noise);
    p_noise = f_noise;
    phy_noise_energy_t *data;
    for (i=0; i<mpdu_measure_chandata_blk_nb[PHY_CHANDATA_TYPE_NRJ]; i++)
    {
        if (p_noise == l_noise)
        {
            p_noise->last = 1;
            p_noise->blk.next = NULL;
            dbg_assert (i == (mpdu_measure_chandata_blk_nb[PHY_CHANDATA_TYPE_NRJ]-1));
        }
        else
        {
            p_noise->last = 0;
        }
        p_noise->type = PHY_CHANDATA_TYPE_NRJ;
        data = (phy_noise_energy_t *) p_noise->blk.data;
        for (j=0; j<PHY_NOISE_NB_PER_BLK; j++) *data++ = (i+1) * j*3;
        p_noise = (phy_chandata_t *) p_noise->blk.next;
    }

    *noise = (phy_chandata_t *) blk_alloc_desc ();
    data = (phy_noise_energy_t *) (*noise)->blk.data;
    for (j=0; j<PHY_NOISE_NB_PER_BLK; j++)
    {
       *data++ = 1000;
    }
    (*noise)->type = PHY_CHANDATA_TYPE_NRJ_SYMBOL;
    (*noise)->last = 0;
    (*noise)->blk.next =(blk_t *) f_noise;

    *rx_params = blk_alloc();
    (*rx_params)->preamble_ntb = date;
    (*rx_params)->tei = 1;
    (*rx_params)->lid = 1;
}
void
run_one_reassembly (sar_t *sar_ctx, uint pb_nb)
{
    pbproc_rx_params_t *rx_params;
    pb_t *f, *l;
    phy_chandata_t *noise;

    emul_reassembly (pb_nb, 0x00, &rx_params, &f, &l, &noise);
    sar_mpdu_add (sar_ctx, NULL, NULL, rx_params, f, l, pb_nb, (pb_t *) noise,
                  mpdu_measure_chandata_blk_nb[PHY_CHANDATA_TYPE_NRJ] +
                  mpdu_measure_chandata_blk_nb[PHY_CHANDATA_TYPE_NRJ_SYMBOL]
                  );
    blk_release (rx_params); // pbproc can erase it.
    sar_launch (sar_ctx);
}

void
START (sar_t **sar_ctx, sta_t **sta, ca_t **ca)
{
    pbproc_t *pbproc_ctx = pbproc_init (&mac_config, mac_store_ctx);
    *ca = (ca_t *) blk_alloc();
    *sar_ctx = sar_init (mac_store_ctx, pbproc_ctx, *ca);
    sar_init_measure_context (*sar_ctx, rxce_ctx);
    sar_init_measurement_cb (*sar_ctx, &rxce_mpdu_measurement_add);
    mac_store_sta_add (mac_store_ctx, 1);
    *sta = mac_store_sta_get (mac_store_ctx, 1);
}

void
END (sar_t *sar_ctx, sta_t *sta, ca_t *ca)
{
    //sar_trace_print (sar_ctx);
    blk_release (sta); // release reference to sta added by mac_store_mfs_get in START.
    dbg_check (mac_store_sta_remove (mac_store_ctx, 1));
    blk_release (ca);
    sar_uninit (sar_ctx);
}

void
frame_process (cyg_addrword_t data)
{

    sar_t *sar_ctx;
    sta_t *sta;
    ca_t *ca;
    int n;
    int tmi_null;
    int tmp_exists;

    test_case_begin (test, "One sound frame");
    START (&sar_ctx, &sta, &ca);
    run_one_reassembly (sar_ctx, 0);
    cyg_thread_delay (3); //rxce can process
    test_begin (test, "check rxce status")
    {
        test_fail_if (sta->rx_tonemaps->default_tmi != TONEMAP_INDEX_INITIAL_START);
        test_fail_if (!sta->rxce.tm_in_build);
    } test_end;
    END (sar_ctx, sta, ca);

    test_case_begin (test, "One 1-PB frame");
    START (&sar_ctx, &sta, &ca);
    run_one_reassembly (sar_ctx, 1);
    cyg_thread_delay (3); //rxce can process
    test_begin (test, "check rxce status")
    {
        test_fail_if (sta->rx_tonemaps->default_tmi != TONEMAP_INDEX_INITIAL_START);
        test_fail_if (!sta->rxce.tm_in_build);
    } test_end;
    END (sar_ctx, sta, ca);

    test_case_begin (test, "One 200-PB frame");
    START (&sar_ctx, &sta, &ca);
    run_one_reassembly (sar_ctx, 200);
    cyg_thread_delay (1); //rxce can process
    test_begin (test, "check rxce status")
    {
        test_fail_if (sta->rx_tonemaps->default_tmi != TONEMAP_INDEX_INITIAL_START);
        test_fail_if (!sta->rxce.tm_in_build);
    } test_end;
    END (sar_ctx, sta, ca);

    test_case_begin (test, "Several sound frames processed one by one");
    START (&sar_ctx, &sta, &ca);
    test_begin (test, "check rxce status")
    {
        for (n=0; n<FRAME_NB_TO_COMPUTE_IN_INITIAL_STEP+10; n++)
        {
            run_one_reassembly (sar_ctx, 0);
            cyg_thread_delay (3); //rxce can process
            tmi_null = sta->rx_tonemaps->default_tmi == TONEMAP_INDEX_INITIAL_START;
            tmp_exists = (int) sta->rxce.tm_in_build;
            test_fail_if (n<FRAME_NB_TO_COMPUTE_IN_INITIAL_STEP && !tmi_null);
            test_fail_if (n<FRAME_NB_TO_COMPUTE_IN_INITIAL_STEP && !tmp_exists);
            test_fail_if (n>=FRAME_NB_TO_COMPUTE_IN_INITIAL_STEP && tmi_null);
            test_fail_if (n==FRAME_NB_TO_COMPUTE_IN_INITIAL_STEP && tmp_exists);
            test_fail_if (n>FRAME_NB_TO_COMPUTE_IN_INITIAL_STEP && !tmp_exists);
        }
    } test_end;
    END (sar_ctx, sta, ca);

    test_case_begin (test, "Several sound frames and rxce late");
    START (&sar_ctx, &sta, &ca);
    test_begin (test, "check rxce status")
    {
        for (n=0; n<FRAME_NB_TO_COMPUTE_IN_INITIAL_STEP+10; n++)
        {
            run_one_reassembly (sar_ctx, 0);
            if (n == FRAME_NB_TO_COMPUTE_IN_INITIAL_STEP -5 )
            {
                cyg_thread_delay (3); //rxce can process
                uint frame_nb = sta->rxce.measurement_computed_nb-1;
                tmi_null = sta->rx_tonemaps->default_tmi == TONEMAP_INDEX_INITIAL_START;
                tmp_exists = (int) sta->rxce.tm_in_build;
                //my_print ("%d, tmi_null=%d, tmp_exists=%d\n",frame_nb, tmi_null, tmp_exists);
                test_fail_if (frame_nb<FRAME_NB_TO_COMPUTE_IN_INITIAL_STEP && !tmi_null);
                test_fail_if (frame_nb<FRAME_NB_TO_COMPUTE_IN_INITIAL_STEP && !tmp_exists);
                test_fail_if (frame_nb>=FRAME_NB_TO_COMPUTE_IN_INITIAL_STEP && tmi_null);
                test_fail_if (frame_nb==FRAME_NB_TO_COMPUTE_IN_INITIAL_STEP && tmp_exists);
                test_fail_if (frame_nb>FRAME_NB_TO_COMPUTE_IN_INITIAL_STEP && !tmp_exists);
            }
            if (n == FRAME_NB_TO_COMPUTE_IN_INITIAL_STEP )
            {
                cyg_thread_delay (3); //rxce can process
                uint frame_nb = sta->rxce.measurement_computed_nb-1;
                tmi_null = sta->rx_tonemaps->default_tmi == TONEMAP_INDEX_INITIAL_START;
                tmp_exists = (int) sta->rxce.tm_in_build;
                //my_print ("%d, tmi_null=%d, tmp_exists=%d\n",frame_nb, tmi_null, tmp_exists);
                test_fail_if (frame_nb<FRAME_NB_TO_COMPUTE_IN_INITIAL_STEP && !tmi_null);
                test_fail_if (frame_nb<FRAME_NB_TO_COMPUTE_IN_INITIAL_STEP && !tmp_exists);
                test_fail_if (frame_nb>=FRAME_NB_TO_COMPUTE_IN_INITIAL_STEP && tmi_null);
                test_fail_if (frame_nb==FRAME_NB_TO_COMPUTE_IN_INITIAL_STEP && tmp_exists);
                test_fail_if (frame_nb>FRAME_NB_TO_COMPUTE_IN_INITIAL_STEP && !tmp_exists);
            }
            if (n == FRAME_NB_TO_COMPUTE_IN_INITIAL_STEP + 5 -1 )
            {
                cyg_thread_delay (3); //rxce can process
                uint frame_nb = sta->rxce.measurement_computed_nb-1;
                tmi_null = sta->rx_tonemaps->default_tmi == TONEMAP_INDEX_INITIAL_START;
                tmp_exists = (int) sta->rxce.tm_in_build;
                //my_print ("%d, tmi_null=%d, tmp_exists=%d\n",frame_nb, tmi_null, tmp_exists);
                test_fail_if (frame_nb<FRAME_NB_TO_COMPUTE_IN_INITIAL_STEP && !tmi_null);
                test_fail_if (frame_nb<FRAME_NB_TO_COMPUTE_IN_INITIAL_STEP && !tmp_exists);
                test_fail_if (frame_nb>=FRAME_NB_TO_COMPUTE_IN_INITIAL_STEP && tmi_null);
                test_fail_if (frame_nb==FRAME_NB_TO_COMPUTE_IN_INITIAL_STEP && tmp_exists);
                test_fail_if (frame_nb>FRAME_NB_TO_COMPUTE_IN_INITIAL_STEP && !tmp_exists);
            }
            cyg_thread_delay (3); //rxce can process
        }
    } test_end;
    END (sar_ctx, sta, ca);

    mac_store_uninit (mac_store_ctx);
    rxce_trace_print (rxce_ctx);
    rxce_trace_uninit (rxce_ctx);
    trace_uninit();
    blk_release (phy);
    test_begin (test, "check alloc/free")
    {
        test_fail_unless (blk_check_memory());
    } test_end;
    test_result (test);
    HAL_PLATFORM_EXIT (test_nb_failed (test) == 0 ? 0 : 1);
}

void
cyg_user_start (int argc, char **argv)
 {
    test_init (test, argc, argv);
    trace_init();
    phy = (phy_t *) blk_alloc();
    mac_ntb_init (phy, &mac_config);

    tonemask_default ((u8 *)(&(mac_config.tonemask_info.tonemask)));
    mac_store_ctx = mac_store_init ();
    sar_t *sar = blk_alloc();
    rxce_ctx = rxce_init (sar, mac_store_ctx, &mac_config);
    cyg_thread_create (RXCE_PRIORITY, &rxce_process,(cyg_addrword_t) rxce_ctx, "rxce",
                       rxce_stack, CYGNUM_HAL_STACK_SIZE_TYPICAL,
                       &rxce_handle, &rxce_thread);
    cyg_thread_resume (rxce_handle);
    cyg_thread_create (FRAME_PRIORITY, &frame_process, 0, "frame_test",
                       frame_stack, CYGNUM_HAL_STACK_SIZE_TYPICAL,
                       &frame_handle, &frame_thread);
    cyg_thread_resume (frame_handle);
    blk_release (sar);

}