summaryrefslogtreecommitdiff
path: root/cesar/bsu/aclf/test/utest/src/bpsd.c
blob: 3913c3bdc5074d48d46f80d812c9cb4e6c553ba4 (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
/* 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"

void
bsu_aclf_acl_frequency_detection (bsu_aclf_t *ctx);

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);

void
test_case_aclf_compute_bto (test_t t)
{
    test_case_begin (t, "BTO computation");
    uint f, i;
    float frequency;
    bsu_aclf_test_t ctx;
    test_begin (t, "Test beacon period date and BTO")
    {
        for (f = 425; f < 691; f++)
        {
            bsu_aclf_test_init (&ctx);
            frequency = (float) f / 10;
            ctx.phy->frequency_hz = frequency;
            bsu_aclf_acl_frequency_detection (ctx.aclf);
            bsu_aclf_ac_compute_beacon_period_start_date (ctx.aclf);
            for (i = 1; i < BSU_ACLF_BPSD_NB; i++)
                test_fail_unless (ctx.aclf->bpsd[i] != ctx.aclf->bpsd[i-1]);
            for (i = 0; i < HPAV_BEACON_BTO_NB; i++)
                test_fail_unless (
                    ctx.aclf->bto[i] != HPAV_BEACON_BTO_INVALID);
            bsu_aclf_test_uninit (&ctx);
        }
    }
    test_end;
}

/**
 * Test BPSD computation from PowerLine and beacon reception.
 * \param  t  the test context.
 * \param  frequency  multiplied by 10 to avoid rounded values.
 */
void
test_case_aclf_shift_bpsd_freq (test_t t, uint frequency)
{
    test_within (t);
    uint i;
    u32 ntb_offset_tck = 1000;
    bsu_aclf_test_t ctx;
    bsu_aclf_test_init (&ctx);
    ctx.phy->frequency_hz = (float) frequency / 10;
    bsu_aclf_acl_frequency_detection (ctx.aclf);
    uint beacon_period_cmp_tck = 50.0e6 / ((float) frequency / 10);
    s16 bto[] = {0, 0, 0, 0};
    /* Truncate the beacon period comparison. */
    if (ctx.aclf->frequency == BSU_ACLF_FREQ_50HZ)
    {
        if (beacon_period_cmp_tck < BSU_ACLF_50HZ_CLK_MIN_TCK)
            beacon_period_cmp_tck = BSU_ACLF_50HZ_CLK_MIN_TCK;
        else if (beacon_period_cmp_tck > BSU_ACLF_50HZ_CLK_MAX_TCK)
            beacon_period_cmp_tck = BSU_ACLF_50HZ_CLK_MAX_TCK;
    }
    else
    {
        if (beacon_period_cmp_tck < BSU_ACLF_60HZ_CLK_MIN_TCK)
            beacon_period_cmp_tck = BSU_ACLF_60HZ_CLK_MIN_TCK;
        else if (beacon_period_cmp_tck > BSU_ACLF_60HZ_CLK_MAX_TCK)
            beacon_period_cmp_tck = BSU_ACLF_60HZ_CLK_MAX_TCK;
    }
    bsu_aclf_ac_compute_beacon_period_start_date (ctx.aclf);
    u32 bpsd_date = ctx.aclf->bpsd[1];
    while (!lesseq_mod2p32 (ctx.aclf->bpsd[1], phy_date ()))
        ;
    bsu_aclf_shift_beacon_period_start_date (ctx.aclf);
    test_fail_unless (less_mod2p32 (bpsd_date, ctx.aclf->bpsd[1]));
    for (i = 1; i < BSU_ACLF_BPSD_NB; i++)
    {
        test_fail_unless (
            less_mod2p32 (ctx.aclf->bpsd[i-1], ctx.aclf->bpsd[i]));
        int diff_tck =
            beacon_period_cmp_tck - (ctx.aclf->bpsd[i] - ctx.aclf->bpsd[i-1]);
        test_fail_unless (diff_tck <= 1 && diff_tck >= -1);
    }
    /* Compare BTOs. */
    for (i = 0; i < HPAV_BEACON_BTO_NB; i++)
    {
        bto[i] = beacon_period_cmp_tck -
            ctx.aclf->beacon_period_theo_tck;
        int bto_diff = ctx.aclf->bto[i] - bto[i];
        test_fail_unless (bto_diff >= -1 && bto_diff <= 1);
    }
    /* Compute the next beacon period using a received information
     * from the beacon. */
    u32 bts = ctx.aclf->bpsd[1] + ntb_offset_tck;
    bsu_aclf_compute_beacon_period_start_date (
        ctx.aclf, bts, bto, 0, ntb_offset_tck);
    for (i = 1; i < BSU_ACLF_BPSD_NB - 1; i++)
    {
        u32 diff_tck = ctx.aclf->bpsd[i] - ctx.aclf->bpsd[i-1];
        test_fail_unless (diff_tck == beacon_period_cmp_tck);
    }
    bsu_aclf_test_uninit (&ctx);
}

void
test_case_aclf_shift_bpsd (test_t t)
{
    uint f;
    test_case_begin (t, "Shift bpsd");
    test_begin (t, "shift")
    {
        for (f = 425; f < 691; f++)
            test_case_aclf_shift_bpsd_freq (t, f);
    }
    test_end;
}

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