summaryrefslogtreecommitdiff
path: root/cesar/ce/rx/bitloading/src/transition.c
blob: 05c326024360be948d28e6494695090b726c477f (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
340
341
342
343
344
345
346
347
348
349
350
/* Cesar project {{{
 *
 * Copyright (C) 2009 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    ce/rx/bitloading/src/transition.c
 * \brief   Transitions of the CE RX Bit Loading FSM.
 * \ingroup ce_rx
 *
 * This file contains the transitions of the FSM of the CE RX Bit Loading.
 */
#include "common/std.h"

#include "ce/rx/bitloading/inc/transition.h"
#include "ce/rx/bitloading/inc/nsr.h"
#include "ce/rx/bitloading/inc/ber.h"
#include "ce/rx/bitloading/inc/initial.h"
#include "ce/rx/bitloading/inc/common.h"
#include "ce/rx/bitloading/bitloading.h"
#include "ce/rx/inc/measure.h"
#include "ce/rx/cp/inc/cp.h"
#include "ce/rx/cp/mme.h"
#include "ce/rx/inc/rx.h"
#include "ce/debug/gpio/gpio.h"
#include "mac/common/tonemap.h"
#include "mac/pbproc/pbproc.h"
#include "cp/sta/mgr/sta_mgr.h"

#include <cyg/kernel/kapi.h>

/**
 * Common handler for initial CE when receiving measure for sound frames.
 * \param  ce_rx  the context of the CE in RX
 * \param  sta  the peer station
 * \param  measure  a measure for sound frames
 * \param  first_sound  if this is the first sound frame, set it to true
 * \return  true if the sound received is the last one
 *
 * This function will initialize (if first sound is true) or add it to the sum
 * of NSR. If this is the last sound, it will compute the mean and them launch
 * an initial CE.
 */
static inline bool
ce_rx_bl__common_initial_sound_measure_received (ce_rx_t *ce_rx, sta_t *sta,
                                                 ce_rx_measure_mbox_t
                                                 *measure, bool first_sound)
{
    /* Check parameters. */
    dbg_assert (measure);
    dbg_assert (measure->rx_params.sound);
    dbg_assert (sta);

    if (first_sound)
        /* Initialize sum of NSR. */
        ce_rx_bl_nsr_sum_init (&sta->ce_rx_bt, measure->chan_data,
                               measure->chan_data_count);
    else
        /* Add it to the sum of NSR. */
        ce_rx_bl_nsr_sum_add (&sta->ce_rx_bt, measure->chan_data,
                              measure->chan_data_count);
    /* Sound complete? */
    if (measure->rx_params.sound_complete)
    {
        ce_debug_gpio_event
            (CE_DEBUG_GPIO_EVENT_CE_RX_BL_MEASURE_SOUND_COMPLETE, true);
        dbg_assert (ce_rx);
        dbg_assert (ce_rx->mac_config);
        /* Compute NSR mean (in every case). */
        ce_rx_bl_nsr_compute_mean (&sta->ce_rx_bt);
        /* Start initial tone map. */
        ce_rx_bl_start_bl (ce_rx, sta, &ce_rx->mac_config->tonemask_info);
        return true;
    }
    ce_debug_gpio_event
        (CE_DEBUG_GPIO_EVENT_CE_RX_BL_MEASURE_SOUND, true);
    return false;
}

/**
 * Common handler to restart CE in RX.
 * \param  ce_rx  the CE RX context
 * \param  sta  the peer station
 */
static void
ce_rx_bl__common__restart_ce_rx (ce_rx_t *ce_rx, sta_t *sta)
{
    /* Check parameter. */
    dbg_assert (sta);

    /* In RX, reset default tone map and remove tone maps. */
    tonemaps_reset (sta->rx_tonemaps);

    /* Reset NSR. */
    ce_rx_bl_nsr_clean (&sta->ce_rx_bt);
    /* Reset high PB error rate counter. */
    sta->ce_rx_bt.high_pb_error_rate_frame_counter = 0;
    /* Reset BER sliding means. */
    ce_rx_bl_ber_sliding_mean_reset (&sta->ce_rx_bt);
}

/**
 * Common handler to restart CE.
 * \param  ce_rx  the CE RX context
 * \param  sta  the peer station
 */
static void
ce_rx_bl__common__restart_ce (ce_rx_t *ce_rx, sta_t *sta)
{
    /* Check parameter. */
    dbg_assert (ce_rx);
    dbg_assert (sta);

    /* Restart CE in RX. */
    ce_rx_bl__common__restart_ce_rx (ce_rx, sta);

    /* Send the MME to the other side. */
    cp_sta_t *cp_sta = cp_sta_mgr_sta_get_assoc
        (ce_rx->cp_ctx, cp_sta_mgr_get_our_avln (ce_rx->cp_ctx), sta->tei);
    dbg_assert (cp_sta);
    /* Send MME to restart CE. */
    ce_rx_mme_restart_initial_ce (ce_rx->cp_ctx, cp_sta, sta->rx_tonemaps);
    /* Clean. */
    slab_release (cp_sta);

    /* In TX, reset default tone map and remove tone maps. */
    /* FIXME: this is a dirty hack: we force the TX to re-estimate the
     * channel, in order to transmit the MME to the other side.
     * In reality, we need to use the SACKI. */
    tonemaps_reset (sta->tx_tonemaps);
}

ce_rx_bl_fsm_branch_t
ce_rx_bl__IDLE__measure_received (ce_rx_t *ce_rx, sta_t *sta,
                                  ce_rx_bitloading_fsm_event_param_t data)
{
    /* Check parameters. */
    dbg_assert (sta);
    dbg_assert (ce_rx);
    dbg_assert (ce_rx->mac_config);

    /* Get measure. */
    ce_rx_measure_mbox_t *measure = data.measure;
    dbg_assert (measure);

    /* Sound? */
    if (measure->rx_params.sound)
    {
        if (ce_rx_bl__common_initial_sound_measure_received (ce_rx, sta,
                                                             measure,
                                                             true))
            return ce_rx_bl_fsm_next_branch (IDLE, measure_received,
                                             sound_complete);
        else
            return ce_rx_bl_fsm_next_branch (IDLE, measure_received,
                                             sound_not_complete);
    }
    else
    {
        ce_debug_gpio_event
            (CE_DEBUG_GPIO_EVENT_CE_RX_BL_MEASURE_DATA_NORMAL, true);
        return ce_rx_bl_fsm_next_branch (IDLE, measure_received, not_sound);
    }
}

ce_rx_bl_fsm_branch_t
ce_rx_bl__INITIAL__measure_received (ce_rx_t *ce_rx, sta_t *sta,
                                     ce_rx_bitloading_fsm_event_param_t data)
{
    /* Check parameters. */
    dbg_assert (sta);
    dbg_assert (ce_rx);
    dbg_assert (ce_rx->mac_config);

    /* Get measure. */
    ce_rx_measure_mbox_t *measure = data.measure;
    dbg_assert (measure);

    /* Sound? */
    if (measure->rx_params.sound)
    {
        if (ce_rx_bl__common_initial_sound_measure_received (ce_rx, sta,
                                                             measure,
                                                             false))
            return ce_rx_bl_fsm_next_branch (INITIAL, measure_received,
                                             sound_complete);
        else
            return ce_rx_bl_fsm_next_branch (INITIAL, measure_received,
                                             sound_not_complete);
    }
    else
    {
        ce_debug_gpio_event
            (CE_DEBUG_GPIO_EVENT_CE_RX_BL_MEASURE_DATA_NORMAL, true);
        return ce_rx_bl_fsm_next_branch (INITIAL, measure_received,
                                         not_sound);
    }
}

ce_rx_bl_fsm_branch_t
ce_rx_bl__TRACKING__measure_received (ce_rx_t *ce_rx, sta_t *sta,
                                      ce_rx_bitloading_fsm_event_param_t
                                      data)
{
    /* Check parameters. */
    dbg_assert (sta);

    /* Get measure. */
    ce_rx_measure_mbox_t *measure = data.measure;
    dbg_assert (measure);

    /* Based on sound? */
    if (measure->rx_params.sound)
    {
        /* Sound complete? */
        if (measure->rx_params.sound_complete)
        {
            ce_debug_gpio_event
                (CE_DEBUG_GPIO_EVENT_CE_RX_BL_MEASURE_SOUND_COMPLETE, true);
            /* Maybe more than one sound complete flag. Let's ignore it. */
            return ce_rx_bl_fsm_next_branch (TRACKING, measure_received,
                                             sound_complete);
        }
        else
        {
            ce_debug_gpio_event
                (CE_DEBUG_GPIO_EVENT_CE_RX_BL_MEASURE_SOUND, true);
            /* Restart CE in RX. */
            CE_RX_TRACE (RESTART_FORCED, sta->tei);
            ce_rx_bl__common__restart_ce_rx (ce_rx, sta);
            /* Initialize sum of NSR. */
            ce_rx_bl_nsr_sum_init (&sta->ce_rx_bt, measure->chan_data,
                                   measure->chan_data_count);
            return ce_rx_bl_fsm_next_branch (TRACKING, measure_received,
                                             sound_not_complete);
        }
    }
    else
    {
        /* Not sound. */
        sta->rx_tonemaps->sound_frame_counter = TONEMAP_SOUND_FRAME_COUNTER;
        /* Measure should only be processed if the TMI used is the one we are
         * expected.
         * FIXME: this will not work with intervals. */
        dbg_assert (sta->rx_tonemaps);
        if (sta->rx_tonemaps->default_tmi == measure->rx_params.tmi_av)
        {
            /* Update BER sliding means. */
            if (measure->total_pb_count != measure->false_pb_count)
                ce_rx_bl_ber_sliding_mean_update
                    (&sta->ce_rx_bt, ce_rx_bl_ber_quantify
                     (measure->ber_sum,
                      measure->total_pb_count - measure->false_pb_count,
                      /* FIXME: see #1462. */
                      520 * 8));
            /* High PB Error rate (not on small frame)? */
            if (TONEMAP_INDEX_IS_NEGOTIATED (measure->rx_params.tmi_av)
                && (measure->total_pb_count >= ce_rx_bl_min_pb_per_frame_)
                && (ce_rx_bl_pb_total_factor_ * measure->false_pb_count
                    > ce_rx_bl_pb_false_factor_ * measure->total_pb_count))
                {
                    /* Increase frame counter. */
                    sta->ce_rx_bt.high_pb_error_rate_frame_counter++;
                    CE_RX_TRACE_VERBOSE
                        (PB_ERR_RATE_HIGH, sta->tei,
                         measure->rx_params.tmi_av,  measure->false_pb_count,
                         measure->total_pb_count,
                         sta->ce_rx_bt.high_pb_error_rate_frame_counter);
                    /* Enough frame in this case and not too soon? */
                    if ((sta->ce_rx_bt.high_pb_error_rate_frame_counter
                        >= ce_rx_bl_min_frame_with_high_pb_err_rate_)
                        && lesseq_mod2p32
                        (sta->ce_rx_bt.next_date_min_for_restart_rtc_date[0],
                         cyg_current_time ()))
                    {
                        ce_debug_gpio_event
                            (CE_DEBUG_GPIO_EVENT_CE_RX_BL_MEASURE_DATA_HIGH_PB_ERROR_RATE, true);
                        CE_RX_TRACE (RESTART_PB_ERR, sta->tei,
                                     sta->ce_rx_bt.high_pb_error_rate_frame_counter);
                        /* Restart CE. */
                        ce_rx_bl__common__restart_ce (ce_rx, sta);

                        return ce_rx_bl_fsm_next_branch (TRACKING,
                                                         measure_received,
                                                         not_sound_pb_err_rate_high);
                    }
                }
            else
            {
                /* Reset frame counter. */
                sta->ce_rx_bt.high_pb_error_rate_frame_counter = 0;
                /* Get tone map. */
                tonemap_t *tm
                    = sta->rx_tonemaps->tm[measure->rx_params.tmi_av];
                /* If this is not negotiated tone map (ROBO)
                   or the tone map is not at the maximum
                   and not too soon. */
                if ((!TONEMAP_INDEX_IS_NEGOTIATED (measure->rx_params.tmi_av)
                    ||
                    ((tm)
                     && (tm->bits_per_symbol
                         < (ce_rx->mac_config->tonemask_info.carrier_nb
                            * CE_BIT_PER_MOD[CE_MOD_COUNT - 1]))))
                    && lesseq_mod2p32
                    (sta->ce_rx_bt.next_date_min_for_restart_rtc_date[1],
                     cyg_current_time ()))
                {
                    /* Compute lower bound. */
                    u64 ber_target;
                    if (!TONEMAP_INDEX_IS_NEGOTIATED
                        (measure->rx_params.tmi_av))
                    {
                        ber_target = ce_rx_bl_ber_pt_robo
                            (ce_rx->mac_config->tonemask_info.carrier_nb);
                    }
                    else
                    {
                        ber_target = tm->ber_target_reached;
                    }

                    u64 lower_bound = ber_target
                        - (ber_target * ce_rx_bl_ber_lower_bound_
                           / CE_RX_BL_BER_LOWER_BOUND_UNIT);
                    /* Get BER sliding means. */
                    s64 *bsm = sta->ce_rx_bt.ber_sliding_mean;
                    if (((u64) bsm[CE_RX_BL_BER_SLIDING_MEAN_FAST]
                         < lower_bound)
                        && ((u64) bsm[CE_RX_BL_BER_SLIDING_MEAN_SLOW]
                            < lower_bound))
                    {
                        ce_debug_gpio_event
                            (CE_DEBUG_GPIO_EVENT_CE_RX_BL_MEASURE_DATA_BER_LOW, true);
                        CE_RX_TRACE (RESTART_BER, sta->tei);
                        /* Restart CE. */
                        ce_rx_bl__common__restart_ce (ce_rx, sta);
                        return ce_rx_bl_fsm_next_branch (TRACKING,
                                                         measure_received,
                                                         not_sound_ber_too_low);
                    }
                }
            }
        }
    }
    ce_debug_gpio_event
        (CE_DEBUG_GPIO_EVENT_CE_RX_BL_MEASURE_DATA_NORMAL, true);
    return ce_rx_bl_fsm_next_branch (TRACKING, measure_received,
                                     not_sound_pb_err_rate_low);
}