summaryrefslogtreecommitdiff
path: root/cesar/mac/pbproc/src/fsm_handle_fc.c
blob: a6fd63d8ccbf796ac444dbbc34e7bf0d07fafa10 (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
/* Cesar project {{{
 *
 * Copyright (C) 2007 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    mac/pbproc/src/fsm_handle_fc.c
 * \brief   FSM handle FC part.
 * \ingroup mac_pbproc
 */
#include "common/std.h"

#include "pbproc.h"
#include "inc/context.h"
#include "inc/fc10.h"
#include "inc/fsm_handle_fc.h"

#include "inc/fsm_rx_data.h"
#include "inc/fsm_rx_beacon.h"
#include "inc/fsm_rx_sound.h"
#include "inc/fsm_top.h"

/**
 * Handle BEACON FC.
 * \param  ctx  pbproc context
 * \param  rx_date  start of preamble date
 * \param  beacon  BEACON FC
 */
static void
pbproc_fhfc_beacon (pbproc_t *ctx, u32 rx_date,
                    const pbproc_fc_beacon_t *beacon);

/**
 * Handle SOF FC.
 * \param  ctx  pbproc context
 * \param  rx_date  start of preamble date
 * \param  sof  SOF FC
 */
static void
pbproc_fhfc_sof (pbproc_t *ctx, u32 rx_date, const pbproc_fc_sof_t *sof);

/**
 * Handle RTS/CTS.
 * \param  ctx  pbproc context
 * \param  rx_date  start of preamble date
 * \param  fc  FC
 */
static void
pbproc_fhfc_rts_cts (pbproc_t *ctx, u32 rx_date,
                     const pbproc_fc_rts_cts_t *fc);

/**
 * Handle SOUND FC.
 * \param  ctx  pbproc context
 * \param  rx_date  start of preamble date
 * \param  sound  SOUND FC
 */
static void
pbproc_fhfc_sound (pbproc_t *ctx, u32 rx_date,
                   const pbproc_fc_sound_t *sound);

/**
 * Ignore the received PPDU.
 * \param  ctx  pbproc context
 * \param  rx_date  start of preamble date
 * \param  length_tck  length to ignore
 */
static void
pbproc_fhfc_ignore (pbproc_t *ctx, u32 rx_date, uint length_tck);

/**
 * Treat the received PPDU as bad.
 * \param  ctx  pbproc context
 * \param  rx_date  start of preamble date
 */
static void
pbproc_fhfc_bad (pbproc_t *ctx, u32 rx_date);

/** Handle FC 1.0.
 * \param  ctx  pbproc context
 * \param  rx_date  start of preamble date
 * \param  fc10  FC 1.0
 * \return  true if it should be considered as a CRC error
 */
static bool
pbproc_fhfc_fc10 (pbproc_t *ctx, u32 rx_date, u32 fc10);

void
pbproc_fhfc_init (pbproc_t *ctx)
{
    dbg_assert (ctx);
}

void ARCH_ILRAM
pbproc_fhfc_handle_fc (pbproc_t *ctx, u32 rx_date, const pbproc_fc_t *fc_av)
{
    dbg_claim (ctx);
    ctx->stats.rx_handle_fc++;
    /* Cancel any pending TX. */
    phy_tx_cancel (ctx->phy);
    /* Hold any access update. */
    ca_access_hold (ctx->ca);
    /* Pending AIFS. */
    pbproc_ftop_aifs_check (ctx);
    /* Look at FC. */
    if (!fc_av)
    {
        /* Handle HP 1.0 detection. */
        bool crc_error = pbproc_fhfc_fc10 (ctx, rx_date,
                                           phy_rx_fc10 (ctx->phy));
        if (crc_error)
        {
            ctx->stats.rx_crc_error++;
            /* Bad CRC. */
            pbproc_fhfc_bad (ctx, rx_date);
        }
    }
    else
    {
        /* Determine PPDU type. */
        switch (fc_av->generic.dt_av)
        {
        case PBPROC_FC_DT_BEACON:
            pbproc_fhfc_beacon (ctx, rx_date, &fc_av->beacon);
            break;
        case PBPROC_FC_DT_SOF:
            pbproc_fhfc_sof (ctx, rx_date, &fc_av->sof);
            break;
        case PBPROC_FC_DT_SACK:
            pbproc_fhfc_ignore (ctx, rx_date, ctx->times.pre_fcs_tck
                                + MAC_CIFS_TCK);
            break;
        case PBPROC_FC_DT_RTS_CTS:
            pbproc_fhfc_rts_cts (ctx, rx_date, &fc_av->rts_cts);
            break;
        case PBPROC_FC_DT_SOUND:
            pbproc_fhfc_sound (ctx, rx_date, &fc_av->sound);
            break;
        default:
            /* Handled as bad CRC. */
            ctx->stats.rx_fc_unknown++;
            pbproc_fhfc_bad (ctx, rx_date);
        }
    }
}

static void ARCH_ILRAM
pbproc_fhfc_beacon (pbproc_t *ctx, u32 rx_date,
                    const pbproc_fc_beacon_t *beacon)
{
    pbproc_frbe__handle (ctx, rx_date, beacon);
}

static void ARCH_ILRAM
pbproc_fhfc_sof (pbproc_t *ctx, u32 rx_date, const pbproc_fc_sof_t *sof)
{
    if (sof->access == false
        && ((ctx->config->tei && sof->snid == ctx->alloc.snid) || sof->mnbf)
        && (sof->dtei == ctx->config->tei || sof->mcf))
    {
        pbproc_frda__handle (ctx, rx_date, sof);
    }
    else
    {
        /* Not for us, count the expected following SACK. */
        pbproc_fhfc_ignore (ctx, rx_date, MAC_FL_TO_TCK (sof->fl_av)
                            + 2 * ctx->times.pre_fcs_tck
                            + MAC_CIFS_TCK);
    }
}

static __attribute__ ((noinline)) void
pbproc_fhfc_rts_cts (pbproc_t *ctx, u32 rx_date,
                     const pbproc_fc_rts_cts_t *fc)
{
    /* First thing to do: unblock the hardware. */
    phy_rx_prepare_short (ctx->phy);
    uint ack_date = rx_date + ctx->times.pre_fcs_tck + MAC_RCG_TCK;
    /* Reply? */
    if (fc->rtsf
        && fc->access == false
        && fc->snid == ctx->alloc.snid
        && fc->dtei == ctx->config->tei)
    {
        ctx->stats.rx_rts++;
        /* Prepare CTS. */
        if (ctx->alloc.hybrid)
            phy_tx_fc10 (ctx->phy, ack_date, 0);
        pbproc_fc_t cts;
        cts.words[0] = BF_FILL (
            PBPROC_FC_RTS_CTS_W0,
            (DT_AV, PBPROC_FC_DT_RTS_CTS),
            (ACCESS, false),
            (SNID, ctx->alloc.snid),
            (STEI, ctx->config->tei),
            (DTEI, fc->stei),
            (LID, fc->lid));
        cts.words[1] = BF_FILL (
            PBPROC_FC_RTS_CTS_W1,
            (CFS, fc->cfs),
            (BDF, ctx->detect.beacon_detected),
            (HP10DF, ctx->detect.hp10_detected),
            (HP11DF, ctx->detect.hp11_detected),
            (RTSF, false),
            (IGF, false),
            (MNBF, fc->mnbf),
            (MCF, fc->mcf),
            (DUR, fc->dur - MAC_TCK_TO_FL (ctx->times.pre_fcs_tck +
                                           MAC_RCG_TCK)),
            (RESERVED0, 0));
        cts.words[2] = BF_FILL (PBPROC_FC_RTS_CTS_W2, (RESERVED1, 0));
        cts.words[3] = BF_FILL (PBPROC_FC_RTS_CTS_W3, (RESERVED2, 0));
        /* Send it. */
        phy_tx_param_short (ctx->phy,
                            PHY_FC_MODE (ctx->alloc.hybrid,
                                         ctx->config->fc_symbols_nb));
        phy_tx_frame (ctx->phy, ack_date, false, true, cts.words);
        /* Activate RX later. */
        ack_date += ctx->times.pre_fcs_tck + MAC_CMG_TCK;
    }
    else
        ctx->stats.rx_nfu++;
    /* Should receive a frame after this one. */
    ca_access_vcs_restart_eifs (ctx->ca, ack_date);
    phy_rx_activate (ctx->phy, false, ack_date, true);
    pbproc_fsm_change_state (ctx, PBPROC_FSM_STATE_IDLE);
}

static void ARCH_ILRAM
pbproc_fhfc_sound (pbproc_t *ctx, u32 rx_date, const pbproc_fc_sound_t *sound)
{
    if (sound->access == false
        && sound->snid == ctx->alloc.snid
        && sound->dtei == ctx->config->tei
        && !sound->saf)
    {
        pbproc_frso__handle (ctx, rx_date, sound);
    }
    else if (sound->saf)
    {
        /* Acknowledge, ignore. */
        pbproc_fhfc_ignore (ctx, rx_date, ctx->times.pre_fcs_tck
                            + MAC_CIFS_TCK);
    }
    else
    {
        /* Not for us, count the expected following SOUND ACK. */
        pbproc_fhfc_ignore (ctx, rx_date, MAC_FL_TO_TCK (sound->fl_av)
                            + 2 * ctx->times.pre_fcs_tck
                            + MAC_CIFS_TCK);
    }
}

static void ARCH_ILRAM
pbproc_fhfc_ignore (pbproc_t *ctx, u32 rx_date, uint length_tck)
{
    dbg_claim (ctx);
    dbg_claim (length_tck <= MAC_FL_TO_TCK (MAC_MAX_FL_MAX_FL));
    ctx->stats.rx_nfu++;
    phy_rx_prepare_short (ctx->phy);
    ca_access_vcs_restart (ctx->ca, rx_date + length_tck);
    pbproc_fsm_change_state (ctx, PBPROC_FSM_STATE_IDLE);
}

static void ARCH_ILRAM
pbproc_fhfc_bad (pbproc_t *ctx, u32 rx_date)
{
    dbg_claim (ctx);
    phy_rx_prepare_short (ctx->phy);
    ca_access_vcs_restart_eifs (ctx->ca, rx_date);
    phy_rx_activate (ctx->phy, false, rx_date + ctx->times.pre_fcs_tck
                     + PHY_RX_ACTIVATE_DELAY_AFTER_SHORT_TCK, true);
    pbproc_fsm_change_state (ctx, PBPROC_FSM_STATE_IDLE);
}

static bool ARCH_ILRAM
pbproc_fhfc_fc10 (pbproc_t *ctx, u32 rx_date, u32 fc10)
{
    dbg_claim (ctx);
    bool crc_error = false;
    uint dt = BF_GET (PBPROC_FC10_GENERIC__DT, fc10);
    if (fc10 == (u32) -1)
        crc_error = true;
    else if (dt == PBPROC_FC10_DT_SOF_NO_RESP
             && (ctx->detect.hp10_detected
                 || ctx->detect.hp11_detected))
    {
        uint tmi = BF_GET (PBPROC_FC10_SOF__TMI, fc10);
        uint fl = BF_GET (PBPROC_FC10_SOF__FL, fc10);
        if (fl > 8 || (tmi == 0 && !(fl & 1)))
            crc_error = true;
        else
        {
            /* For FC 1.0 with no response expected, EOF will be received too
             * late to resynchronise with medium. Use the SOF and lose the
             * opportunity to detect an HP 1.0 station. */
            uint length_tck = MAC_HP10_PREAMBLE_DELTA_TCK
                + MAC_HP10_DELIMITER_TCK + 20 * MAC_HP10_SYMBOL_TCK
                + fl * 20 * MAC_HP10_SYMBOL_TCK + MAC_HP10_EFG_TCK
                + MAC_HP10_DELIMITER_TCK + MAC_HP10_CIFS_TCK;
            pbproc_fhfc_ignore (ctx, rx_date, length_tck);
        }
    }
    else if (dt == PBPROC_FC10_DT_EOF_NO_RESP
             || dt == PBPROC_FC10_DT_EOF_WITH_RESP)
    {
        /* Detect HP 1.0 stations on EOF as it is never sent by an AV
         * station. */
        uint invalid = BF_GET (PBPROC_FC10_EOF__INVALID, fc10);
        uint reserved = BF_GET (PBPROC_FC10_EOF__RESERVED, fc10);
        if (!invalid && reserved == 0)
        {
            ctx->detect.hp10_detected = true;
            ctx->detect.hp10_detect_date = rx_date;
        }
        else if (!invalid && (reserved & 1))
        {
            ctx->detect.hp11_detected = true;
            ctx->detect.hp11_detect_date = rx_date;
        }
        else
            crc_error = true;
        /* Handle medium access. */
        if (!crc_error)
        {
            if (dt == PBPROC_FC10_DT_EOF_NO_RESP)
            {
                /* Too late to start PRP. */
                ctx->stats.rx_nfu++;
                pbproc_fhfc_bad (ctx, rx_date);
            }
            else
            {
                /* Restart after EOF. */
                pbproc_fhfc_ignore (
                    ctx, rx_date, MAC_HP10_PREAMBLE_DELTA_TCK
                    + MAC_HP10_DELIMITER_TCK + MAC_HP10_RIFS_TCK
                    + MAC_HP10_DELIMITER_TCK + MAC_HP10_CIFS_TCK);
            }
        }
    }
    else
        crc_error = true;
    return crc_error;
}