summaryrefslogtreecommitdiff
path: root/cesar/mac/pbproc/src/pbproc.c
blob: 72b3fd680a9dd53922985eb295ed58073269ad24 (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
/* Cesar project {{{
 *
 * Copyright (C) 2007 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    mac/pbproc/src/pbproc.c
 * \brief   General PB Processing functions.
 * \ingroup pbproc
 */
#include "common/std.h"
#include "mac/pbproc/inc/context.h"

#include "mac/common/timings.h"

#include "hal/arch/arch.h"

#include "mac/pbproc/inc/fsm_top.h"
#include "mac/pbproc/inc/fsm_handle_fc.h"
#include "mac/pbproc/inc/fsm_rx_data.h"
#include "mac/pbproc/inc/fsm_rx_beacon.h"
#include "mac/pbproc/inc/fsm_tx_rts_cts.h"
#include "mac/pbproc/inc/fsm_tx_data.h"

static pbproc_t pbproc_global;

static void
pbproc_times_init (mac_config_t *config, pbproc_times_t times[2])
{
    uint pre_fcs_tck;
    dbg_assert_ptr (config);
    dbg_assert (1 <= config->fc_symbols_nb && config->fc_symbols_nb <= 2);
    /* AV only timings. */
    pre_fcs_tck = MAC_PREAMBLE_TCK + config->fc_symbols_nb * MAC_FC_AV_TCK;
    times[0].eifs_tck = MAC_EIFS_AV_TCK;
    times[0].pre_fcs_tck = pre_fcs_tck;
    times[0].sack_tck = pre_fcs_tck;
    times[0].rts_rcg_tck = pre_fcs_tck + MAC_RCG_TCK;
    times[0].rts_rcg_cts_cmg_tck = pre_fcs_tck + MAC_RCG_TCK + pre_fcs_tck
        + MAC_CMG_TCK;
    times[0].max_fl_tck = MAC_FL_TO_TCK (MAC_MAX_FL_MAX_FL);
    /* Hybrid timings. */
    pre_fcs_tck = MAC_PREAMBLE_HYBRID_TCK + MAC_FC_10_TCK
        + config->fc_symbols_nb * MAC_FC_AV_TCK;
    times[1].eifs_tck = MAC_EIFS_10_TCK;
    times[1].pre_fcs_tck = pre_fcs_tck;
    times[1].sack_tck = pre_fcs_tck;
    times[1].rts_rcg_tck = pre_fcs_tck + MAC_RCG_TCK;
    times[1].rts_rcg_cts_cmg_tck = pre_fcs_tck + MAC_RCG_TCK + pre_fcs_tck
        + MAC_CMG_TCK;
    times[1].max_fl_tck = MAC_FL_TO_TCK (MAC_MAX_FL_MIN_FL);
}

pbproc_t *
pbproc_init (mac_config_t *config, mac_store_t *store)
{
    pbproc_t *ctx;
    dbg_assert (config);
    dbg_assert (store);
    ctx = &pbproc_global;
    /* Initialise context. */
    ctx->config = config;
    ctx->store = store;
    ctx->phy =
        phy_init (ctx, (phy_rx_fc_cb_t) pbproc_fsm_handle_rx_fc_event,
                  (phy_access_cb_t) pbproc_fsm_handle_access_event,
                  (phy_access_conf_cb_t) pbproc_fsm_handle_access_conf_event,
                  (phy_pbdma_cb_t) pbproc_fsm_handle_pbdma_event,
                  (phy_tx_false_alarm_cb_t) pbproc_fsm_handle_tx_false_alarm_event,
                  (phy_deferred_cb_t) pbproc_fsm_handle_deferred);
    phy_set_tonemask (ctx->phy, config->tonemask_info.tonemask,
                      config->tonemask_info.carrier_nb);
    ctx->ca = ca_init (ctx->phy, config, store);
    pbproc_trace_init (ctx);
    ctx->user_data = NULL;
    ctx->rx_cb = NULL;
    ctx->rx_beacon_cb = NULL;
    ctx->access.mfs = NULL;
    pbproc_prep_mpdu_init (ctx);
    /* Initialise precomputed times. */
    pbproc_times_init (config, ctx->times);
    ctx->symbol_tck[PHY_GIL_417] = MAC_DX417_TCK;
    ctx->symbol_tck[PHY_GIL_567] = MAC_DX567_TCK;
    ctx->symbol_tck[PHY_GIL_3534] = MAC_DX3534_TCK;
    /* Reset stats. */
    ctx->stats.rts_fail = 0;
    ctx->stats.rts_bad_cts = 0;
    ctx->stats.prp_lost = 0;
    ctx->stats.cw_lost = 0;
    ctx->stats.data_fail = 0;
    /* RX PB pool. */
    ctx->rx_pool_head = ctx->rx_pool_tail = NULL;
    ctx->rx_pool_size = 0;
    /* Initialise FSM. */
    pbproc_fsm_init (ctx);
    pbproc_ftop_init (ctx);
    pbproc_fhfc_init (ctx);
    pbproc_frda_init (ctx);
    pbproc_frbe_init (ctx);
    //pbproc_ftrc_init (ctx);
    pbproc_ftda_init (ctx);
    /* All done! */
    PBPROC_TRACE (INIT);
    return ctx;
}

void
pbproc_init_cb (pbproc_t *ctx, void *user_data, pbproc_rx_cb_t rx_cb,
                pbproc_rx_beacon_cb_t rx_beacon_cb)
{
    dbg_assert (ctx);
    dbg_assert (rx_cb);
    dbg_assert (rx_beacon_cb);
    ctx->user_data = user_data;
    ctx->rx_cb = rx_cb;
    ctx->rx_beacon_cb = rx_beacon_cb;
}

void
pbproc_uninit (pbproc_t *ctx)
{
    dbg_assert (ctx);
    PBPROC_TRACE (UNINIT);
    if (ctx->rx_pool_head)
        blk_release_desc_range (&ctx->rx_pool_head->blk,
                                &ctx->rx_pool_tail->blk);
    pbproc_trace_uninit (ctx);
    ca_uninit (ctx->ca);
    phy_uninit (ctx->phy);
}

phy_t *
pbproc_get_phy (pbproc_t *ctx)
{
    return ctx->phy;
}

ca_t *
pbproc_get_ca (pbproc_t *ctx)
{
    return ctx->ca;
}

void
pbproc_activate (pbproc_t *ctx, bool flag)
{
    PBPROC_TRACE (ACTIVATE, flag);
    if (flag)
    {
        /* Activate. */
        ctx->alloc = *ca_access_activate (ctx->ca, phy_date (ctx->phy),
                                          PBPROC_ANTICIP_TCK);
        phy_rx_param (ctx->phy, PHY_FC_MODE (ctx->alloc.hybrid,
                                             ctx->config->fc_symbols_nb));
        phy_rx_activate (ctx->phy, true, 0, true);
    }
    else
    {
        /* Deactivate. */
        dbg_assert (ctx->fsm.current_state == PBPROC_FSM_STATE_IDLE);
        phy_rx_activate (ctx->phy, true, 0, false);
        ca_access_deactivate (ctx->ca);
    }
}

void
pbproc_rx_segment_refill (pbproc_t *ctx, pb_t *first, pb_t *last, uint nb)
{
    dbg_assert_ptr (ctx);
    dbg_assert_ptr (last);
    dbg_assert (nb > 0);
    if (DEBUG)
    {
        uint i;
        pb_t *p, *plast;
        for (i = 0, p = first; i < nb - 1; i++, p = p->next)
        {
            dbg_assert_ptr (p);
            plast = p;
        }
        dbg_assert (p == last);
    }
    PBPROC_TRACE (RX_SEG_REFILL, nb);
    /* Now, the real "work", ISR locked. */
    arch_isr_lock ();
    if (!ctx->rx_pool_head)
    {
        dbg_assert (ctx->rx_pool_size == 0);
        ctx->rx_pool_head = first;
    }
    else
    {
        dbg_assert (ctx->rx_pool_size != 0);
        ctx->rx_pool_tail->next = first;
    }
    ctx->rx_pool_tail = last;
    ctx->rx_pool_size += nb;
    arch_isr_unlock ();
}