summaryrefslogtreecommitdiff
path: root/cesar/ce/rx/cp/src/mme.c
blob: af3b14365f2992cc0f42dae46bfd8f878bd1b8f3 (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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/* Cesar project {{{
 *
 * Copyright (C) 2009 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    ce/rx/cp/src/mme.c
 * \brief   Generation of MME for the CE in RX (declaration part).
 * \ingroup ce_rx_cp
 *
 * Sending the MME for the CE in RX from the CP context can be quite
 * complicated.
 */
#include "common/std.h"
#include "lib/bitstream.h"
#include "cp/inc/context.h"
#include "cp/msg/msg.h"
#include "mac/common/timings.h"
#include "ce/common/mme.h"
#include "ce/rx/cp/mme.h"
#include "ce/rx/cp/inc/mme.h"
#include "ce/rx/cp/inc/cp.h"

u16
ce_rx_cp_mme_tone_map_size_with_rle (tonemap_t *tm, tonemask_info_t *tonemask)
{
    /* Check parameters. */
    dbg_assert (tm);
    dbg_assert (tonemask);

    u8 modulation;
    s8 prev_modulation = -1;
    u8 count = 0;
    uint size = 0;
    MAC_COMMON_TONEMAP_READ_BEGIN (tm, tonemask->tonemask, modulation)
    {
        /* Initialisation of first loop. */
        if (prev_modulation == -1)
            prev_modulation = modulation;
        if ((prev_modulation == modulation) && (count < 74))
        {
            count++;
        }
        else
        {
            /* New modulation is different, increase size. */
            size += ce_rx_cp_mme_rle_size (count);
            /* Reset counter. */
            count = 1;
            /* Update previous modulation. */
            prev_modulation = modulation;
        }
    }
    MAC_COMMON_TONEMAP_READ_END;
    /* Commit last count. */
    size += ce_rx_cp_mme_rle_size (count);

    /* Return computed size. */
    return size;
}

u16
ce_rx_cp_mme_tone_map_size_with_update (tonemap_t *old_tm, tonemap_t *new_tm,
                                        tonemask_info_t *tonemask)
{
    /* Check parameters. */
    dbg_assert (old_tm);
    dbg_assert (new_tm);
    dbg_assert (tonemask);
    u16 size = 0;
    uint i, j;
    u8 new_mod, old_mod;
    /* Current tone mask word. */
    u32 tonemask_word;
    const u32* tonemask_word_ptr = tonemask->tonemask;
    /* Access to tone maps. */
    bitstream_t bt_old_tm;
    bitstream_read_init (&bt_old_tm, old_tm->tmdma_desc_head->data,
                         BLK_SIZE);
    /* Set-up the callback. */
    bitstream_init_buffer_cb (&bt_old_tm,
                              ce_tonemap_buffer_exhausted,
                              old_tm->tmdma_desc_head->next->data);
    bitstream_t bt_new_tm;
    bitstream_read_init (&bt_new_tm, new_tm->tmdma_desc_head->data,
                         BLK_SIZE);
    /* Set-up the callback. */
    bitstream_init_buffer_cb (&bt_new_tm,
                              ce_tonemap_buffer_exhausted,
                              new_tm->tmdma_desc_head->next->data);

    /* Go through each group of 32 tones. */
    for (i = 0; i < PHY_CARRIER_NB / 32; i++)
    {
        /* Get current tone mask word. */
        tonemask_word = *(tonemask_word_ptr++);
        /* Go through each tone of this group of 32 tones. */
        for (j = 32; j; j--)
        {
            old_mod = bitstream_read (&bt_old_tm, 4);
            new_mod = bitstream_read (&bt_new_tm, 4);
            /* Get the mask for the tone of the current group of 32
               tones. */
            if ((tonemask_word & 0x1) == 0)
            {
                /* Same modulation? */
                if (old_mod != new_mod)
                {
                    size += 16;
                }
            }
            /* Next tone in the current group of 32 tones. */
            tonemask_word >>= 1;
        }
    }
    /* A group of less than 32 tones? */
    if (PHY_CARRIER_NB % 32)
    {
        /* Get current tone mask word. */
        tonemask_word = *(tonemask_word_ptr++);
        /* Go through each tone of this group of 32 tones. */
        for (j = PHY_CARRIER_NB % 32; j; j--)
        {
            old_mod = bitstream_read (&bt_old_tm, 4);
            new_mod = bitstream_read (&bt_new_tm, 4);
            /* Get the mask for the tone of the current group of 32
               tones. */
            if ((tonemask_word & 0x1) == 0)
            {
                /* Same modulation? */
                if (old_mod != new_mod)
                {
                    size += 16;
                }
            }
            /* Next tone in the current group of 32 tones. */
            tonemask_word >>= 1;
        }
    }

    /* Return computed size. */
    return size;
}

void
ce_rx_mme_send_cm_chan_est_ind (cp_t *ctx, cp_sta_t *sta, tonemaps_t *tms,
                                bool initial_ce, u8 new_tmi, u8 default_tmi,
                                u32 tmi_list, tonemap_intervals_t *int_list)
{
    /* Check parameters. */
    dbg_assert (ctx);
    dbg_assert (sta);
    dbg_assert (tms);
    dbg_assert ((new_tmi >> 5) == 0x000);
    dbg_assert ((default_tmi >> 5) == 0x000);
    dbg_assert (int_list);

    /* Get peer. */
    cp_mme_peer_t peer;
    cp_sta_get_peer (sta, &peer);

    /* Build MME. */
    cp_mme_tx_t *mme = cp_msg_mme_init (ctx, &peer, CM_CHAN_EST_IND);
    dbg_assert (mme);

    bitstream_write (&mme->bitstream, tms->max_fl_av, 16);
    bitstream_write (&mme->bitstream,
                     tms->rifs_av_one_sym_tck / MAC_TCK_PER_FL, 8);
    bitstream_write (&mme->bitstream,
                     tms->rifs_av_two_sym_tck / MAC_TCK_PER_FL, 8);
    bitstream_write (&mme->bitstream,
                     tms->rifs_av_g2_sym_tck / MAC_TCK_PER_FL, 8);
    if (initial_ce)
        bitstream_write (&mme->bitstream, 0, 8);
    else
        bitstream_write (&mme->bitstream, 1, 8);
    bitstream_write (&mme->bitstream, tms->max_tm, 8);
    bitstream_write (&mme->bitstream, default_tmi, 8);
    bitstream_write (&mme->bitstream, tms->scl_cp, 8);
    bitstream_write (&mme->bitstream, tms->scl_cfp, 8);

    /* TMI AV valid list. */
    u8 tm_count = 0;
    uint i;
    for (i = NEGOCIATED_TONEMAP_INDEX_FIRST; i < TONEMAP_INDEX_NB ; i++)
    {
        if (tmi_list & (1 << i))
            tm_count++;
    }
    dbg_assert (tm_count || default_tmi < PHY_MOD_ROBO_NB);
    bitstream_write (&mme->bitstream, tm_count, 8);
    for (i = NEGOCIATED_TONEMAP_INDEX_FIRST; i < TONEMAP_INDEX_NB; i++)
        if (tmi_list & (1 << i))
        {
            bitstream_write (&mme->bitstream, i, 8);
            tm_count--;
        }
    dbg_assert (tm_count == 0);

    /* Intervals. */
    bitstream_write (&mme->bitstream, int_list->intervals_nb, 8);
    for (i = 0; i < int_list->intervals_nb; i++)
    {
        bitstream_write (&mme->bitstream,
                         int_list->interval[i].end_offset_atu, 16);
        bitstream_write (&mme->bitstream, int_list->interval[i].tmi, 8);
    }

    /* New TMI? */
    bitstream_write (&mme->bitstream, new_tmi, 8);
    if (new_tmi)
    {
        /* Get tone map. */
        tonemap_t *tm = tms->tm[new_tmi];
        dbg_assert (tm);
        bitstream_write (&mme->bitstream, tm->cpf, 8);
        bitstream_write (&mme->bitstream, tm->fecrate, 8);
        bitstream_write (&mme->bitstream, tm->gil, 8);
        /* Always use RLE. */
        bitstream_write (&mme->bitstream, 1, 8);
        /* Copy position in the bitstream. */
        uint cbd_len_pos = bitstream_written_bits (&mme->bitstream);
        /* CBD length. */
        bitstream_write (&mme->bitstream, 0, 16);

        /* Write tone map in RLE. */
        u8 modulation;
        s8 prev_modulation = -1;
        u16 cbd_length = 0;
        u8 count = 0;
        MAC_COMMON_TONEMAP_READ_BEGIN
            (tm, ctx->mac_config->tonemask_info.tonemask, modulation)
        {
            /* Initialisation of first loop. */
            if (prev_modulation == -1)
                prev_modulation = modulation;
            /* Same modulation and not over max RLE? */
            if ((prev_modulation == modulation) && (count < 74))
            {
                count++;
            }
            else
            {
                /* We need to write the modulation. */
                if (count < 3)
                {
                    /* Write modulation required times. */
                    uint k;
                    for (k = 0; k < count; k++)
                        bitstream_write (&mme->bitstream, prev_modulation,
                                         4);
                }
                else
                {
                    /* Use RLE. */
                    /* Sanity check. */
                    dbg_assert (count <= 74);
                    /* Write modulation. */
                    bitstream_write (&mme->bitstream, prev_modulation,
                                     4);
                    /* Write one or two nibbles. */
                    bitstream_write (&mme->bitstream,
                                     ce_rx_cp_mme_get_rle (count),
                                     count < 11 ? 4 : 8);
                }
                /* Increase size. */
                cbd_length += ce_rx_cp_mme_rle_size (count);
                /* Reset counter. */
                count = 1;
                /* Update previous modulation. */
                prev_modulation = modulation;
            }
        }
        MAC_COMMON_TONEMAP_READ_END;
        /* Let's write last modulation. */
        if (count < 3)
        {
            /* Not in RLE. */
            /* Write modulation required times. */
            uint k;
            for (k = 0; k < count; k++)
                bitstream_write (&mme->bitstream, modulation,
                                 4);
        }
        else
        {
            /* Use RLE. */
            /* Sanity check. */
            dbg_assert (count <= 74);
            /* Write modulation. */
            bitstream_write (&mme->bitstream, prev_modulation,
                             4);
            /* Write one or two nibbles. */
            bitstream_write (&mme->bitstream,
                             ce_rx_cp_mme_get_rle (count),
                             count < 11 ? 4 : 8);
        }
        /* Increase size. */
        cbd_length += ce_rx_cp_mme_rle_size (count);
        /* Write CBD length. */
        dbg_assert (cbd_length % 4 == 0);
        bitstream_direct_write (mme->p_mme, cbd_len_pos, cbd_length / 4, 16);
    }
    bitstream_write_finalise (&mme->bitstream);

    /* Send. */
    cp_msg_mme_send (ctx, mme);

    /* Restart expiration timer. */
    tms->expiration_s = TONEMAPS_LIFE_DURATION_S - 1;
}

void
ce_rx_cp_mme_send_tone_map (cp_t *ctx, cp_sta_t *sta, u32 tmi_list,
                            u8 default_tmi, tonemap_intervals_t *int_list,
                            u8 new_tmi, u8 old_tmi, tonemaps_t *tms,
                            bool initial_ce)
{
    /* Check parameters. */
    dbg_assert (ctx);
    dbg_assert (sta);
    dbg_assert (tmi_list);
    dbg_assert ((default_tmi >> 5) == 0x000);
    dbg_assert (int_list);
    dbg_assert ((new_tmi >> 5) == 0x000);
    dbg_assert ((old_tmi >> 5) == 0x000);
    dbg_assert (tms);

    /* Not based on old tone map? */
    if (!old_tmi)
    {
        /* Use CM_CHAN_EST.IND. */
        ce_rx_mme_send_cm_chan_est_ind (ctx, sta, tms, initial_ce,
                                        new_tmi, default_tmi, tmi_list,
                                        int_list);
    }
    else
    {
        /* Compute and use best one. */
        if (ce_rx_cp_mme_tone_map_size_with_rle (tms->tm[new_tmi],
                                                 &ctx->mac_config->tonemask_info)
            > ce_rx_cp_mme_tone_map_size_with_update (tms->tm[old_tmi],
                                                      tms->tm[new_tmi],
                                                      &ctx->mac_config->tonemask_info))
        {
            /* Use CM_TM_UPDATE.IND. */
            /* TODO. */
        }
        else
        {
            /* Use CM_CHAN_EST.IND. */
            ce_rx_mme_send_cm_chan_est_ind (ctx, sta, tms, initial_ce,
                                            new_tmi, default_tmi, tmi_list,
                                            int_list);
        }
    }
    /* Set refresh counter when there is some TMI enabled. */
    if (tmi_list)
        tms->refresh_counter_s = CE_RX_REFRESH_TONE_MAP_S;
    else
        tms->refresh_counter_s = 0;
}

void
ce_rx_mme_refresh_tone_map_list (cp_t *ctx, cp_sta_t *sta, u32 tmi_list, u8
                                 default_tmi, tonemap_intervals_t *int_list,
                                 tonemaps_t *tms)
{
    /* FIXME: use CM_TM_UPDATE.IND is better. */
    ce_rx_mme_send_cm_chan_est_ind (ctx, sta, tms, false, 0, default_tmi,
                                    tmi_list, int_list);
    /* Set refresh counter when there is some TMI enabled. */
    if (tmi_list)
        tms->refresh_counter_s = CE_RX_REFRESH_TONE_MAP_S;
    else
        tms->refresh_counter_s = 0;
}