summaryrefslogtreecommitdiff
path: root/cesar/ce/rx/src/rx.c
blob: 25c748088f35397db87c27adf6ba78aa87776655 (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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/* Cesar project {{{
 *
 * Copyright (C) 2009 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    ce/rx/src/rx.c
 * \brief   Channel Estimation in Receive mode (implementation).
 * \ingroup ce_rx
 */
#include "common/std.h"
#include "ce/rx/bitloading/fsm/fsm.h"
#include "ce/rx/inc/measure.h"
#include "ce/rx/cp/inc/cp.h"
#include "ce/rx/inc/rx.h"
#include "ce/rx/rx.h"
#include "ce/rx/tonemask.h"
#include "mac/common/timings.h"
#include "mac/common/defs.h"
#include "mac/common/store.h"
#include "lib/stats.h"
#include "ce/rx/bitloading/inc/nsr.h"
#include "ce/rx/bitloading/inc/ber.h"
#include "ce/rx/bitloading/inc/common.h"

/**
 * Static context of the CE in RX.
 */
static ce_rx_t ce_rx;

/**
 * Table with the list of functions to call for each flag.
 */
static ce_rx_process_work_t ce_rx_process_work[CE_RX_WORK_FLAG_SIZE] =
{
    ce_rx_process_work_quit,
    ce_rx_process_work_measure,
};

ce_rx_t *
ce_rx_init (mac_store_t *mac_store, sar_t *sar, pbproc_t *pbproc,
            mac_config_t *mac_config)
{
    /* Check parameters. */
    dbg_assert (mac_store);
    dbg_assert (mac_config);

    ce_rx_trace_init (&ce_rx);

    /* Store a pointer to the MAC store. */
    ce_rx.mac_store = mac_store;

    /* Store a pointer to the MAC config. */
    ce_rx.mac_config = mac_config;

    /* We are going to run, disable quit. */
    ce_rx.stop_flag = false;

    /* Initialize an empty release list. */
    tonemap_release_list_init (&ce_rx.tonemap_release_list);

    /* Initialize sub modules. */
    /* Initialize measure. */
    ce_rx_measure_init (&ce_rx, sar, pbproc);
    /* Initialize communication with the CP. */
    ce_rx_cp_init (&ce_rx);
    /* Initialize extra tonemask to mask faulty carriers
     * during tonemap generation. */
    ce_rx_tonemask_init (&ce_rx.tonemask);

    /* Register our configuration variables.
     * FIXME: this is a dirty hack. */
    lib_stats_set_stat_value_notype ("CE_RX_BL_NSR_MARGIN",
                                     &ce_rx_bl_nsr_margin_,
                                     LIB_STATS_ACCESS_READ_WRITE,
                                     LIB_STATS_DEBUG);
    lib_stats_set_stat_value_notype ("CE_RX_BL_BER_MARGIN_1_2",
                                     &ce_rx_bl_ber_margin_[PHY_FEC_RATE_1_2],
                                     LIB_STATS_ACCESS_READ_WRITE,
                                     LIB_STATS_DEBUG);
    lib_stats_set_stat_value_notype ("CE_RX_BL_BER_MARGIN_16_21",
                                     &ce_rx_bl_ber_margin_[PHY_FEC_RATE_16_21],
                                     LIB_STATS_ACCESS_READ_WRITE,
                                     LIB_STATS_DEBUG);
    lib_stats_set_stat_value_notype ("CE_RX_BL_PB_FALSE_FACTOR",
                                     &ce_rx_bl_pb_false_factor_,
                                     LIB_STATS_ACCESS_READ_WRITE,
                                     LIB_STATS_DEBUG);
    lib_stats_set_stat_value_notype ("CE_RX_BL_PB_TOTAL_FACTOR",
                                     &ce_rx_bl_pb_total_factor_,
                                     LIB_STATS_ACCESS_READ_WRITE,
                                     LIB_STATS_DEBUG);
    lib_stats_set_stat_value_notype ("CE_RX_BL_MIN_PB_PER_FRAME",
                                     &ce_rx_bl_min_pb_per_frame_,
                                     LIB_STATS_ACCESS_READ_WRITE,
                                     LIB_STATS_DEBUG);
    lib_stats_set_stat_value_notype ("CE_RX_BL_MIN_FRAME_WITH_HIGH_PB_ERR_RATE",
                                     &ce_rx_bl_min_frame_with_high_pb_err_rate_,
                                     LIB_STATS_ACCESS_READ_WRITE,
                                     LIB_STATS_DEBUG);
    lib_stats_set_stat_value_notype ("CE_RX_BL_BER_LOWER_BOUND",
                                     &ce_rx_bl_ber_lower_bound_,
                                     LIB_STATS_ACCESS_READ_WRITE,
                                     LIB_STATS_DEBUG);
    lib_stats_set_stat_value_notype ("CE_RX_BL_MIN_TIME_BETWEEN_CE_RESTART_PBER_MS",
                                     &ce_rx_bl_min_time_between_ce_restart_ms[0],
                                     LIB_STATS_ACCESS_READ_WRITE,
                                     LIB_STATS_DEBUG);
    lib_stats_set_stat_value_notype ("CE_RX_BL_MIN_TIME_BETWEEN_CE_RESTART_BER_MS",
                                     &ce_rx_bl_min_time_between_ce_restart_ms[1],
                                     LIB_STATS_ACCESS_READ_WRITE,
                                     LIB_STATS_DEBUG);
    lib_stats_set_stat_value_notype ("CE_RX_BL_PBERR_MEAN_INIT",
                                     &ce_rx_bl_pberr_mean_init_,
                                     LIB_STATS_ACCESS_READ_WRITE,
                                     LIB_STATS_DEBUG);

    /* ECos. */
    /* No work to do. */
    cyg_flag_init (&ce_rx.work_flag);

    /* Timer/alarm. */
    ce_rx.real_time_clock_handle = cyg_real_time_clock ();
    cyg_clock_to_counter (ce_rx.real_time_clock_handle,
                          &ce_rx.real_time_counter);
    cyg_alarm_create (ce_rx.real_time_counter,
                      ce_rx_timer_prevent_tone_map_expiration,
                      (cyg_addrword_t) &ce_rx,
                      &ce_rx.alarm_handler, &ce_rx.alarm);

    cyg_resolution_t res = cyg_clock_get_resolution (cyg_real_time_clock ());
    ce_rx.tck_per_rtc = MAC_MS_TO_TCK (1000LL) * res.dividend / res.divisor
        / 1000000000LL;
    cyg_tick_count_t period = MAC_MS_TO_TCK (1000) / ce_rx.tck_per_rtc;
    cyg_alarm_initialize (ce_rx.alarm_handler, period + cyg_current_time (),
                          period);

    /* Create the ECos thread. */
    cyg_thread_create (CE_RX_THREAD_PRIORITY, &ce_rx_thread,
                       (cyg_addrword_t) &ce_rx, CE_RX_THREAD_NAME,
                       ce_rx.thread_stack, CE_RX_THREAD_STACK_SIZE,
                       &ce_rx.thread_handler, &ce_rx.thread);
    cyg_thread_resume (ce_rx.thread_handler);

    TRACE_SHORT (CE_RX_TRACE_, &ce_rx.trace, INIT);

    return &ce_rx;
}

void
ce_rx_thread (cyg_addrword_t data)
{
    /* Check parameters. */
    ce_rx_t *ce_rx = (ce_rx_t *) data;
    dbg_assert (ce_rx);

    /* Prepare a mask for the work we can done. */
    cyg_flag_value_t work_flag_value, work_flag_mask;
    uint i;
    for (work_flag_mask = 0, i = 0; i < CE_RX_WORK_FLAG_SIZE; i++)
    {
        work_flag_mask |= (1 << i);
    }

    /* While we are not asked to terminate. */
    while (!ce_rx->stop_flag)
    {
        /* Something to do? */
        work_flag_value = cyg_flag_wait (&ce_rx->work_flag, work_flag_mask,
                                         CYG_FLAG_WAITMODE_OR
                                         | CYG_FLAG_WAITMODE_CLR);
        for (i = 0; i < CE_RX_WORK_FLAG_SIZE; i++)
        {
            /* This flag (work, quit, etc) enabled? */
            if (work_flag_value & (1 << i))
            {
                /* Clear flag. */
                work_flag_value &= ~(1 << i);
                /* Do work. */
                ce_rx_process_work[i] (ce_rx);
            }
        }
    }
    /* If we reach this part, we have been asked to terminate this thread. */
    cyg_flag_destroy (&ce_rx->work_flag);
}

void
ce_rx_uninit (ce_rx_t *ce_rx)
{
    /* Ask for stop. */
    ce_rx_work_add (ce_rx, CE_RX_WORK_FLAG_QUIT);
}

void
ce_rx_work_add (ce_rx_t *ce_rx, ce_rx_work_flag_t work)
{
    /* Check parameters. */
    dbg_assert (ce_rx);
    dbg_assert (work < CE_RX_WORK_FLAG_SIZE);

    /* Set work. */
    cyg_flag_value_t stop = 1 << work;
    cyg_flag_setbits (&ce_rx->work_flag, stop);
}

void
ce_rx_process_work_quit (ce_rx_t *ce_rx)
{
    /* Check parameters. */
    dbg_assert (ce_rx);
    /* Stop measure. */
    ce_rx_measure_uninit (ce_rx);
    /* Stop communication with the CP. */
    ce_rx_cp_uninit (ce_rx);
    /* Stop CE RX thread. */
    ce_rx->stop_flag = true;
    CE_RX_TRACE (INIT);
}

void
ce_rx_process_work_measure (ce_rx_t *ce_rx)
{
    /* Check parameters. */
    dbg_assert (ce_rx);

    /* Get measure. */
    mbox_node_t *node;
    while ((node = mbox_get (&ce_rx->measure_mbox)))
    {
        dbg_assert (node);
        ce_rx_measure_mbox_t * measure = PARENT_OF (ce_rx_measure_mbox_t,
                                                    mbox_node, node);

        CE_RX_TRACE_VERBOSE (MEASURE_HANDLING, measure->rx_params.tei,
                             measure->rx_params.tmi_av,
                             measure->chan_data_count,
                             measure->total_pb_count,
                             measure->rx_params.sound,
                             measure->rx_params.sound_complete);

        /* Ensure STA is created. */
        mac_store_sta_add (ce_rx->mac_store,
                           measure->rx_params.tei);
        /* Get the STA. */
        sta_t *sta = mac_store_sta_get (ce_rx->mac_store, measure->rx_params.tei);
        /* STA must exists. */
        dbg_assert (sta);

        /* Transform the given measure into FSM event. */
        ce_rx_bl_fsm_event_type_t event = ce_rx_bl_fsm_measure_to_event (
            sta,
            (ce_rx_bitloading_fsm_event_param_t) measure);

        /* Give event to bit loading. */
        ce_rx_bl_fsm_handle_event (ce_rx, sta, event,
            (ce_rx_bitloading_fsm_event_param_t) measure);

        /* Release station. */
        blk_release (sta);

        /* Delete. */
        slab_release (measure);
    }
}

void
ce_rx_timer_prevent_tone_map_expiration (cyg_handle_t alarm_handler,
                                         cyg_addrword_t data)
{
    /* Check required parameter. */
    dbg_assert (data);
    ce_rx_t *ce_rx = (ce_rx_t *) data;

    uint tei;
    /* Go through each STA. */
    for (tei = MAC_TEI_STA_MIN; tei <= MAC_TEI_STA_MAX; tei++)
    {
        /* Get STA. */
        sta_t *sta;
        sta = mac_store_sta_get (ce_rx->mac_store, tei);
        /* If STA exists. */
        if (sta)
        {
            dbg_assert (sta->rx_tonemaps);

            /* Clean tone maps that need to be released. */
            tonemaps_disabled_clean (sta->rx_tonemaps);

            /* Decrement expiration timer. */
            if (sta->rx_tonemaps->refresh_counter_s && !--sta->rx_tonemaps->refresh_counter_s)
            {
                /* Send refresh MME. */
                ce_rx_cp_send_mme_refresh_tmi_list (ce_rx, sta);
            }
            /* Clean. */
            blk_release (sta);
        }
    }

    /* Clean release list. */
    tonemap_release_list_clean (&ce_rx->tonemap_release_list);
}

blk_t *
ce_rx_get_nsr (ce_rx_t *ce_rx, cp_tei_t tei, uint int_index,
               uint int_version, u16 *ber)
{
    /* Check parameters. */
    dbg_assert (ce_rx);
    dbg_assert (MAC_TEI_IS_STA (tei));
    dbg_assert (int_index < TONEMAP_INTERVAL_NB);
    dbg_assert (ber);
    dbg_assert (int_version != 0xFF);

    /* Get STA from its TEI. */
    sta_t *sta = mac_store_sta_get (ce_rx->mac_store, tei);

    if (!sta)
        return NULL;

    blk_t *ret = NULL;
    /* Check if interval version is valid. */
    if (sta->rx_tonemaps->intervals->version == int_version)
    {
        /* FIXME: add support for intervals. */
        ret = ce_rx_bl_get_nsr (&sta->ce_rx_bt);
        /* FIXME: set BER for the moment being. */
        if (ret)
            *ber = 0;
    }

    /* Clean. */
    blk_release (sta);

    return ret;
}