summaryrefslogtreecommitdiff
path: root/cesar/bsu/aclf/test/utest/src/bpsd.c
blob: 808556ce036a517bec9522e8d688bb1aee5aa216 (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
/* Cesar project {{{
 *
 * Copyright (C) 2010 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    bsu/aclf/test/utest/src/bpsd.c
 * \brief   Beacon Period Start Date accurate test.
 * \ingroup bsu_aclf
 */
#include "common/std.h"
#include "lib/test.h"
#include "common.h"
#include "table_stable.h"
#include "table_increase.h"
#include "table_decrease.h"
#include "result_stable.h"
#include "result_increase.h"
#include "result_decrease.h"

void
bsu_aclf_ac_compute_beacon_period_start_date (bsu_aclf_t *ctx);

void
bsu_aclf_shift_beacon_period_start_date (bsu_aclf_t *ctx);

static u32 zc_test [] = {0, 740000, 1480000};
static u32 zc_test_len = 3;

enum test_pwl_phy_t
{
    TEST_ACLF_PHY_NONE,
    TEST_ACLF_PHY_AC_STABLE,
    TEST_ACLF_PHY_AC_INC,
    TEST_ACLF_PHY_AC_DEC,
    TEST_ACLF_PHY_ZC_TEST,
    TEST_ACLF_PHY_AC_CABLE
};

u32
phy_clock_get_zero_cross_captured_date (phy_t *ctx)
{
    bsu_aclf_test_phy_t *test = (bsu_aclf_test_phy_t *) ctx;
    switch (test->phy_type)
    {
    case TEST_ACLF_PHY_NONE:
        return test->zc;
        break;
    case TEST_ACLF_PHY_AC_STABLE:
        return ac_stable[test->table_index++];
        break;
    case TEST_ACLF_PHY_AC_INC:
        return ac_increase[test->table_index++];
        break;
    case TEST_ACLF_PHY_AC_DEC:
        return ac_decrease[test->table_index++];
        break;
    case TEST_ACLF_PHY_ZC_TEST:
        return zc_test[test->table_index++];
        break;
    default:
        return 0;
        break;
    }
}

void
test_case_aclf_estimate_beacon_period (test_t t)
{
    u32 *tables[3] = {result_ac_stable, result_ac_increase,
        result_ac_decrease};
    enum bsu_aclf_test_aclf_phy_t test_phy_type[3] =
    {BSU_ACLF_TEST_ACLF_PHY_AC_STABLE, BSU_ACLF_TEST_ACLF_PHY_AC_INC,
        BSU_ACLF_TEST_ACLF_PHY_AC_DEC};
    bsu_aclf_test_t test;
    uint nb_iter;
    test_case_begin (t, "Beacon period estimation");
    test_begin (t, "AC Stable, increase, decrease")
    {
        uint i;
        for (i = 0; i < 3; i++)
        {
            bsu_aclf_test_init (&test);
            *((bsu_aclf_frequency_t*) &test.aclf->frequency) =
                BSU_ACLF_FREQ_50HZ;
            *((bsu_aclf_bp_t*) &test.aclf->bp) = BSU_ACLF_BP_50HZ_TCK;
            *((uint*) &test.aclf->zc) = BSU_ACLF_ZC_50HZ_TCK;
            test.aclf->beacon_period = test.aclf->bp;
            test.phy->phy_type = test_phy_type[i];
            for (nb_iter = 0; nb_iter < ac_stable_length; nb_iter ++)
            {
                bsu_aclf_ac_compute_beacon_period_start_date (test.aclf);
                test_fail_if (test.aclf->beacon_period
                              != tables[i][nb_iter],
                              "Estimation is false");
            }
            bsu_aclf_test_uninit (&test);
        }
    }
    test_end;
    test_begin (t, "AC stable, ZC = 740 000 tick")
    {
        bsu_aclf_test_init (&test);
        test.phy->phy_type = TEST_ACLF_PHY_NONE;
        *((uint*) &test.aclf->frequency) = BSU_ACLF_FREQ_50HZ;
        *((uint*) &test.aclf->bp) = BSU_ACLF_BP_50HZ_TCK;
        *((uint*) &test.aclf->zc) = BSU_ACLF_ZC_50HZ_TCK;
        test.aclf->beacon_period = test.aclf->bp;
        for (nb_iter = 0; nb_iter < zc_test_len; nb_iter ++)
        {
            bsu_aclf_ac_compute_beacon_period_start_date (test.aclf);
            test_fail_unless (test.aclf->beacon_period
                              == BSU_ACLF_BP_50HZ_TCK);
        }
        bsu_aclf_test_uninit (&test);
    }
    test_end;
}

void
test_case_aclf_compute_bto (test_t t)
{
    u32 *tables[3] = {result_ac_stable, result_ac_increase,
        result_ac_decrease};
    enum bsu_aclf_test_aclf_phy_t test_phy_type[3] =
    {BSU_ACLF_TEST_ACLF_PHY_AC_STABLE, BSU_ACLF_TEST_ACLF_PHY_AC_INC,
        BSU_ACLF_TEST_ACLF_PHY_AC_DEC};
    bsu_aclf_test_t test_cco, test_sta;
    uint nb_iter;
    test_case_begin (t, "BTO compute");
    test_begin (t, "AC Stable, increase and decrease")
    {
        uint i, j, bto_nb = 4, bpsd_nb = 6;
        s16 btos_cco[bto_nb], btos_sta[bto_nb];
        u32 bpsd_cco[bpsd_nb], bpsd_sta[bpsd_nb], bts;
        int bto;
        for (i = 0; i < 3; i++)
        {
            bsu_aclf_test_init (&test_cco);
            bsu_aclf_test_init (&test_sta);
            test_cco.phy->phy_type = test_phy_type[i];
            *((uint*) &test_cco.aclf->frequency) = BSU_ACLF_FREQ_50HZ;
            *((uint*) &test_cco.aclf->bp) = BSU_ACLF_BP_50HZ_TCK;
            *((uint*) &test_cco.aclf->zc) = BSU_ACLF_ZC_50HZ_TCK;
            *((uint*) &test_sta.aclf->frequency) = BSU_ACLF_FREQ_50HZ;
            *((uint*) &test_sta.aclf->bp) = BSU_ACLF_BP_50HZ_TCK;
            *((uint*) &test_sta.aclf->zc) = BSU_ACLF_ZC_50HZ_TCK;
            test_cco.aclf->beacon_period = test_cco.aclf->bp;
            test_sta.aclf->beacon_period = test_sta.aclf->bp;
            for (nb_iter = 0; nb_iter < ac_stable_length; nb_iter ++)
            {
                test_cco.phy->phy += tables[i][nb_iter];
                test_cco.phy->zc = test_cco.phy->phy - BSU_ACLF_ZC_50HZ_TCK;
                bsu_aclf_ac_compute_beacon_period_start_date (test_cco.aclf);
                bsu_aclf_bto (test_cco.aclf, btos_cco, bto_nb);
                bsu_aclf_beacon_period_start_date (test_cco.aclf, bpsd_cco,
                                                   bpsd_nb);
                /* BTS value is use to simulate the STA beacon reception which
                 * compute the beacon period start date using the BTS and the
                 * BTOs of the central beacon. */
                bts = bpsd_cco[1];
                bsu_aclf_compute_beacon_period_start_date (test_sta.aclf, bts,
                                                           btos_cco, 0, 0);
                bsu_aclf_bto (test_sta.aclf, btos_sta, bto_nb);
                bsu_aclf_beacon_period_start_date (test_sta.aclf, bpsd_sta,
                                                   bpsd_nb);
                test_fail_unless (bpsd_sta[0] == bpsd_cco[1]);
                for (j = 0; j < HPAV_BEACON_BTO_NB; j++)
                {
                    if (btos_sta[j] != HPAV_BEACON_BTO_INVALID)
                        test_fail_unless (bpsd_sta[j+1] == bpsd_cco[j+2]);
                    else
                        test_fail_unless (bpsd_sta[j+1] == bts +
                                          (j+1)*test_sta.aclf->bp);
                }

                for (j = 0; j < HPAV_BEACON_BTO_NB; j++)
                {
                    bto = bpsd_cco[j+2] - bts - (j+1)*test_cco.aclf->bp;
                    if (ABS(bto) >> 15 == 0)
                    {
                        test_fail_unless (bts + (j+1) * test_cco.aclf->bp
                                          + btos_cco[j] == bpsd_cco[2+j]);
                    }
                    else
                        test_fail_unless (btos_cco[j] ==
                                          HPAV_BEACON_BTO_INVALID);
                }
            }
            bsu_aclf_test_uninit (&test_cco);
        }
    }
    test_end;
}

void
test_case_aclf_shift_bpsd (test_t t)
{
    test_case_begin (t, "Shift bpsd");
    test_begin (t, "shift")
    {
        uint i;
        u32 now = phy_date ();
        bsu_aclf_test_t test;
        bsu_aclf_test_init (&test);
        for (i = 0; i < COUNT (test.aclf->bpsd); i++)
            test.aclf->bpsd[i] = i * BSU_ACLF_BP_50HZ_TCK + now;
        test.aclf->beacon_period = BSU_ACLF_BP_50HZ_TCK;
        test.phy->phy = test.aclf->bpsd[1];
        bsu_aclf_shift_beacon_period_start_date (test.aclf);
        test_fail_unless (
            test.aclf->bpsd[1]
            == bsu_aclf_beacon_period_start_date_next (test.aclf));
        for (i = 0; i < BSU_ACLF_BPSD_NB - 1; i++)
            test_fail_unless (test.aclf->bpsd[i] == now + (i+1) *
                              BSU_ACLF_BP_50HZ_TCK);
        bsu_aclf_test_uninit (&test);
    }
    test_end;
}

void
test_suite_aclf__bpsd_accurate (test_t t)
{
    test_suite_begin (t, "Beacon Period Start Date accurate");
    test_case_aclf_estimate_beacon_period (t);
    test_case_aclf_compute_bto (t);
    test_case_aclf_shift_bpsd (t);
}