summaryrefslogtreecommitdiff
path: root/ce/src/rx.c
blob: 28d618799bb9ed9d02b9551ae67d84f2260d3ee7 (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
/* Cesar project {{{
 *
 * Copyright (C) 2007 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    ../../src/rx.c
 * \brief   « brief description »
 * \ingroup « module »
 *
 * « long description »
 */
//#include "../../config_test.h"
#include "common/std.h"
#include "common/defs/ethernet.h"
#include "ce/inc/rx.h"
#include "ce/inc/trace.h"
#include "ce/inc/bitloading.h"
#define RXCE //for static functions of cei.h
#include "ce/inc/cei.h"
#include "cp/interf/interf.h"
#include "ce/test/rx/inc/test_rx.h" // Should be an overrided pbproc...
#if CONFIG_TRACE
#include "mac/common/ntb.h"
#endif

static rxce_t ce_global;

#ifdef EXPIRATION_TEST
void
rxce_init_test_cb (test_cb_t test)
{
    expiration_test = test;
}
#endif

rxce_t *
rxce_init (mac_store_t *mac_store_ctx, mac_config_t *mac_config_ctx)
{
    dbg_assert (mac_store_ctx);
    dbg_assert (mac_config_ctx);
    rxce_t *ctx = &ce_global;
    ctx->mac_store_ctx = mac_store_ctx;
    // Initialize THRESHOLD NOISE for computation
    ctx->mpdu_measure_store_ctx = mpdu_measure_store_init ();
    // Initialize FIFO for cei message to create.
    cei_param_fifo_init();
    // Initialize RXCE with no 'job' to do.
    cyg_semaphore_init (&(ctx->job), 0);
    // Initialize the pointer to the global tonemask
    ctx->mask = mac_config_ctx->tonemask_info.tonemask;

    ctx->pbproc_need_scf = false;
    rxce_trace_init (ctx);
    RXCE_TRACE (INIT, mac_ntb());
    return (ctx);
}

void
pbproc_need_scf_cb (uint tei, uint reason_code)
{
    dbg_assert (&ce_global);
    rxce_t *ctx = &ce_global;
    dbg_assert (&(ctx->job));
    dbg_assert (MAC_TEI_IS_STA(tei));
    dbg_assert ( (reason_code>=0x04 && reason_code<=0x1F)
                 || (reason_code>=0xFC && reason_code<=0xFF) );
    //dbg_assert (!ctx->pbproc_need_scf); // rxce_process didn't respond to a previous request of pbproc.
    ctx->sound_param.stei = tei;
    ctx->sound_param.reason_code = reason_code;
    ctx->pbproc_need_scf = true;
    cyg_semaphore_post (&(ctx->job));
}

bool
sar_mpdu_measurement_cb (void *user, pbproc_rx_params_t *rx_params,
                         uint pb_nb, blk_t **f, blk_t **l, pb_t *noise, uint n)
{
    dbg_assert (user);
    rxce_t *ctx = (rxce_t *) user;
    RXCE_TRACE (SAR_MEASUREMENT_CB, mac_ntb());
    dbg_assert (rx_params);
    bool b;
    pb_measure_blk_t **first = (pb_measure_blk_t **) f;
    pb_measure_blk_t **last = (pb_measure_blk_t **) l;
    // Allocate block if necessary. The SAR will fill blocks after !couldn't be stopped by RXCE!.
    b = mpdu_measure_store_append (ctx->mpdu_measure_store_ctx, rx_params, pb_nb, first, last, (phy_chandata_t *) noise, n);
    // Give 'job' to RXCE that will process the previous frame.
    if (b) cyg_semaphore_post (&(ctx->job));
    else RXCE_TRACE (MEASURE_DROPPED, mac_ntb());
    return (b);
}

/** todo Cf SPEC HPAV     HPAV-FrameControl -- SoundVariantField -- SoundReasonCode */
bool
rxce_scf_compute (rxce_t *ctx)
{
    dbg_assert (ctx);
    bool ret = false;
    sta_t *sta;
    tonemaps_t *tms;
    uint src = ctx->sound_param.reason_code;
    if (src >= 0x04 && src <= 0x1F) //Interval with an invalid tonemap.
    {
        sta = mac_store_sta_get(ctx->mac_store_ctx, ctx->sound_param.stei);
        rxce_job_cei_add (ctx, ctx->sound_param.stei, sta->rx_tonemaps, src - 4, TONEMAP_INDEX_NULL);
        ret = true;
        blk_release (sta);
    }
    if (src == 0xFC )
    {
        //TODO SEND ALL VALID TM
        ret = true;
    }
    if (src == 0xFD )
    {
        sta = mac_store_sta_get(ctx->mac_store_ctx, ctx->sound_param.stei);
        tms = sta->rx_tonemaps;
        if (tms->default_tmi != TONEMAP_INDEX_INITIAL_START)
        {
            ret = true;
        }
        else
        {
            ret = false;
        }
        blk_release (sta);
    }
    if (src == 0xFE)
    {
        ret = false;
        // IF INTERVAL CONCERNED HAS TM ret = true;and cei_param_add
        // ELSE ret = false;
        // todo - it will be necessary to know the date.
    }
    if (src == 0xFF)
    {
        ret = false;
    }
    return (ret);
}

void
rxce_next_measurement_compute (rxce_t *ctx)
{
    dbg_assert (ctx);
    mpdu_measure_t *measure = mpdu_measure_store_get (ctx->mpdu_measure_store_ctx);
    dbg_assert (measure);
    bitloading_modification_t bl =  bitloading_run (ctx->mac_store_ctx, measure);
    mpdu_measure_store_release (ctx->mpdu_measure_store_ctx, measure);
    if (bl.changed_tonemaps ) rxce_job_cei_add (ctx, bl.stei, bl.changed_tonemaps, bl.new_tmi, bl.old_tmi);
}


cyg_tick_count_t
rxce_tonemaps_refresh_management(rxce_t *ctx)
{
    dbg_assert (ctx);
    uint tei;
    cyg_tick_count_t lowest_expiration_date = 0xFFFFFFFFFFFFFFFFll;
    for (tei=MAC_TEI_STA_MIN; tei<=MAC_TEI_STA_MAX; tei++)
    {
        uint date = cyg_current_time();
        sta_t *lsta = mac_store_sta_get (ctx->mac_store_ctx, tei);
        if (lsta)
        {
            if (lsta->rx_tonemaps)
            {
                cyg_tick_count_t expiration_date = lsta->rx_tonemaps->expiration_rtc_date;
                if ( date >= expiration_date )
                {
                    rxce_job_cei_add (ctx, lsta->tei, lsta->rx_tonemaps, TONEMAP_INDEX_NULL, TONEMAP_INDEX_NULL);
                }
                if (lowest_expiration_date > expiration_date)
                {
                    lowest_expiration_date = expiration_date;
                }
            }
            blk_release (lsta);
        }
    }
    return (lowest_expiration_date);
}

void
rxce_job_cei_add (rxce_t *ctx, uint dtei, tonemaps_t *tonemaps, uint new_tmi, uint old_tmi)
{
    dbg_assert (ctx);
    bool b;
    cei_param_t cei_param;
    cei_param.tms = tonemaps;
    cei_param.new_tmi = new_tmi;
    cei_param.old_tmi = old_tmi;
    cei_param.priority = CEI_PRIORITY;
    cei_param.dtei = dtei;
    b = cei_param_add (&cei_param);
    if (b) cyg_semaphore_post (&(ctx->job));
    tonemaps->expiration_rtc_date = cyg_current_time() + S_TO_RTC (RXCE_TONEMAPS_REFRESH_PERIOD_S);
}

void
rxce_process (cyg_addrword_t data)
{
    dbg_assert (data);
    cyg_tick_count_t watchdog = 0;
    mme_t *mme;
    rxce_t *ctx = (rxce_t *) data;
    while (true)
    {
        RXCE_TRACE (PROCESS_WAIT, mac_ntb());
        cyg_semaphore_timed_wait (&(ctx->job), watchdog);
        RXCE_TRACE (PROCESS_TRIGGERED, mac_ntb());
        if (ctx->pbproc_need_scf)  // At first, verify if pbproc waits information about scf.
        {
            // todo : Not sure that this part of code will not be interrupted
            // by pbproc_need_scf_cb and so, the answer could not correspond to
            // the last situation {src,tei} given by pbproc.
            RXCE_TRACE (SCF_PROCESS, mac_ntb());
            bool b = rxce_scf_compute (ctx);
            if (b)
            {
                pbproc_scf (); // a pbproc function.
            }
            //TODO ATOMIC
            ctx->pbproc_need_scf = false;
        }
        else
        {
            if (cei_param_fifo.number > 0 ) // Then Create CEI if waited.
            {
                RXCE_TRACE (CEI_PROCESS, mac_ntb());
                mme = interf_mme_get ();
                dbg_assert (mme);
                cei_param_t *param = cei_param_get ();
                dbg_assert (param);
#ifdef EXPIRATION_TEST
                expiration_test (param->dtei);
#endif
                cei_created_status_t cei_status = cei_create (mme->mm_entry, ctx->mask, param->tms, param->new_tmi, param->old_tmi);
                mme->length = cei_status.length;
                mme->dtei = param->dtei;
                mme->mm_type = cei_status.mm_type;
                interf_mme_send (mme);
#ifdef MAXIMUS_TEST
                sta_t *sta = mac_store_sta_get (ctx->mac_store_ctx, param->dtei);
                cei_decode (sta->tx_tonemaps, cei_status.mm_type, mme->mm_entry, ctx->mask);
#endif
            }
            else
            {
                if (ctx->mpdu_measure_store_ctx->measure_nb) //Then Compute measurement
                {
                    RXCE_TRACE (BITLOADING_PROCESS, mac_ntb());
                    rxce_next_measurement_compute (ctx);
                }
            }
        }
        if (cyg_current_time() > watchdog)
        {
            RXCE_TRACE (REFRESH_PROCESS, mac_ntb());
            watchdog = rxce_tonemaps_refresh_management(ctx);
        }
    }
}