summaryrefslogtreecommitdiff
path: root/cesar/mac/sar/test/functional/src/sar_tx.c
blob: b8cb3995cdaf22ee2a304c3c30a8cd3c8a243cc7 (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
/* Cesar project {{{
 *
 * Copyright (C) 2009 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    src/sar_tx.c
 * \brief   « brief description »
 * \ingroup « module »
 *
 * « long description »
 */
#include "common/std.h"
#include "lib/test.h"
#include "lib/bitstream.h"

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

#include "hal/arch/arch.h"

#include "mac/common/ntb.h"
#include "mac/common/store.h"

#include "mac/pbproc/pbproc.h"
#include "mac/sar/sar.h"

/* Variables needed by the test. */
#ifdef __sparc__
static u8 skbuffer[2048] __attribute__ ((section(".private")));
#else
static u8 skbuffer[2048];
#endif
static sar_t *sar_ctx = NULL;
static mac_store_t *mac_store = NULL;

static uint count = 0;
cyg_sem_t semaphore;

/* Thread data for the test. */
cyg_thread  my_test_thread;
cyg_handle_t my_test_thread_handle;
u8 my_test_thread_stack [CYGNUM_HAL_STACK_SIZE_TYPICAL];

/* Functions. */
bool ce_measurements (void *user, pbproc_rx_params_t *rx_params, uint pb_nb,
        blk_t **first, blk_t **last, pb_t *chandata, uint nb_chandata, uint
        *blk_offset)
{
    if (nb_chandata)
        blk_release_desc_range_nb ((blk_t *) chandata, nb_chandata);
    return false;
}

void
segmentation_done (void *ctx, u8 *buffer, void *user_data)
{
    count ++;

    if (count == 2)
        cyg_semaphore_post (&semaphore);
}

void
prepare_test (test_t test)
{
    u8 *buffer = NULL;
    mfs_tx_t *mfs = NULL;
    bool added = false;

    /* Prepare the buffer. */
    buffer = ARCH_CPU_TO_UNCACHEABLE (skbuffer);
    memset (buffer, 0xFF, 2048);

    /* Add the MFS. */
    mfs = mac_store_mfs_add_tx (mac_store, false, false, 1, 1, &added);
    dbg_assert (mfs);

    if (added)
        sar_mfs_add (sar_ctx, (mfs_t *)mfs);

    /* Add the first frame. */
    sar_msdu_add (sar_ctx, buffer, 1434, 0x0, mfs, NULL, 123);
    /* Add the second frame. */
    sar_msdu_add (sar_ctx, buffer, 1334, 0x0, mfs, NULL, 123);

    /* Release the reference on the MFS. */
    blk_release (mfs);
}

void
verify_test (test_t test)
{
    mfs_tx_t *mfs = NULL;
    pb_t *pb_first = NULL;
    pb_t *pb_last = NULL;
    bool added = false;
    sar_mf_t sar_mf;
    pb_t *pb_list[6 + 1];
    pb_t *pb_current = NULL;
    uint i = 0;
    u32 crc;

    mfs = mac_store_mfs_add_tx (mac_store, false, false, 1, 1, &added);
    dbg_assert (mfs);

    test_case_begin (test, "SAR TX");

    test_begin (test, "Verify PBs")
    {
        test_fail_unless (mfs->common.release == false);
        test_fail_unless (mfs->head != NULL);
        test_fail_unless (mfs->tail!= NULL);
        test_fail_unless (mfs->head != mfs->tail);
        test_fail_unless (mfs->tail->header.ssn + 1 == mfs->seg_nb);
        test_fail_unless (mfs->seg_nb == 6);

        pb_first = mfs->head;
        pb_last = mfs->tail;
        pb_last->next = NULL;
        mfs->head = NULL;
        mfs->tail = NULL;

        for (i = 0, pb_current = pb_first;
             pb_current;
             i++, pb_current = pb_current->next)
        {
            pb_list[i] = pb_current;
            arch_load_cache ((u32*) &(pb_current->header), 1);
            arch_load_cache ((u32*) (pb_current->data), BLK_SIZE / 4);
            test_fail_unless (pb_current->header.ssn == i);
        }

        /* Test the header mfbf and mfbo. */
        test_fail_unless (pb_list[0]->header.mfbf == true);
        test_fail_unless (pb_list[0]->header.mfbo == 0);
        test_fail_unless (pb_list[2]->header.mfbf == true);
        test_fail_unless (pb_list[2]->header.mfbo == 416);

        /* Verify the headers. */
        sar_mf_header (pb_list[0], 0, &sar_mf);
        test_fail_unless (sar_mf.type == SAR_MF_TYPE_DATA);
        test_fail_unless (sar_mf.length == 1434);

        sar_mf_header (pb_list[2], 416, &sar_mf);
        test_fail_unless (sar_mf.type == SAR_MF_TYPE_DATA);
        test_fail_unless (sar_mf.length == 1334);

        /* Verify the CRC. */
        crc = bitstream_direct_read (pb_list[2]->data, 412 * 8, 32);
        test_fail_unless (crc == 0xcbc4fcd7);

        crc = bitstream_direct_read (pb_list[5]->data, 216 * 8, 32);
        test_fail_unless (crc == 0x6bbcffe7);

        sar_mfs_remove (sar_ctx, (mfs_t *) mfs);
        blk_release_desc_range_nb ((blk_t *) pb_first, 6);
    }
    test_end;

    blk_release (mfs);
}

void
test_thread_process (cyg_addrword_t data)
{
    test_t test;
    mac_config_t mac_config;
    uint ca;
    uint phy;
    uint pbproc;

    test_init (test, 0, NULL);

    /* Initialise the test. */
    mac_ntb_init ((phy_t *) &phy, &mac_config);
    mac_store = mac_store_init ();
    sar_ctx = sar_init (mac_store, (pbproc_t *) &pbproc, (ca_t *) &ca);
    sar_activate (sar_ctx, true);

    sar_init_data_context (sar_ctx, sar_ctx);
    sar_init_mme_context (sar_ctx, sar_ctx);
    sar_init_measurement_cb (sar_ctx, ce_measurements);
    sar_init_segmentation_data_cb (sar_ctx, segmentation_done);
    sar_init_segmentation_mme_cb (sar_ctx, segmentation_done);

    prepare_test (test);

    /* Wait the bridgedma ends its job. */
    cyg_semaphore_wait (&semaphore);
    verify_test (test);

    test_result (test);

    sar_uninit (sar_ctx);
    pbproc_uninit ((pbproc_t *) &pbproc);

    dbg_check (mac_store_sta_remove (mac_store, 0x1));
    mac_store_uninit (mac_store);
    mac_ntb_uninit();

    cyg_semaphore_destroy (&semaphore);

#ifndef __sparc__
    HAL_PLATFORM_EXIT (test_nb_failed (test) == 0 ? 0 : 1);
#endif
}

int
cyg_user_start (void)
{
    cyg_semaphore_init(&semaphore, 0);

    // Create the thread.
    cyg_thread_create( 10,
                      &test_thread_process,
                      (cyg_addrword_t) 0,
                      "TEST_THREAD",
                      my_test_thread_stack,
                      CYGNUM_HAL_STACK_SIZE_TYPICAL,
                      &my_test_thread_handle,
                      &my_test_thread);
    cyg_thread_resume (my_test_thread_handle);

    return 0;
}