summaryrefslogtreecommitdiff
path: root/mac/ca/src/access.c
blob: 1cd69508dc887ca2b24af09e636da28766a9895c (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
/* Cesar project {{{
 *
 * Copyright (C) 2007 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    mac/ca/src/access.c
 * \brief   ACCESS event and VCS related functions.
 * \ingroup mac_ca
 */
#include "common/std.h"

#include "mac/common/timings.h"
#include "mac/ca/ca.h"

#include "hal/phy/phy.h"

#include "mac/ca/inc/context.h"
#include "mac/ca/inc/alloc.h"

/**
 * Choose a MFS TX for the given GLID.
 * \param  ctx  ca context
 * \param  glid  global link identifier
 * \return  MFS or NULL if none match
 */
static mfs_tx_t *
ca_access_choose_mfs_tx (ca_t *ctx, uint glid);

void
ca_access_vcs_restart (ca_t *ctx, u32 start_date, uint length_tck,
                       uint anticipation_tck, bool eifs)
{
    uint bp_i, bp_end_i, alloc_i;
    const ca_beacon_period_t *bp;
    const ca_schedule_t *sched;
    u32 vcs_end_date;
    uint glid;
    bool idle, cfp, hybrid;
    uint minimum_length;
    bool new_backoff_drawn;
    mfs_tx_t *mfs;
    dbg_assert (ctx);
    dbg_assert (length_tck > 0);
    /* Find the beacon period of the start date, will search for TX
     * opportunities in two beacon periods. */
    vcs_end_date = start_date + length_tck;
    bp_i = ca_alloc_find_beacon_period (ctx, start_date);
    if (bp_i != ctx->beacon_periods_tail)
    {
        /* Beacon period found. */
        bp = &ctx->beacon_periods[bp_i];
        sched = &ctx->schedules[bp->schedule_index];
        /* Find the allocation of the start date. */
        alloc_i = ca_alloc_find (sched, start_date - bp->start_date);
        idle = eifs;
    }
    else
    {
        /* Beacon period not found, use first one. */
        bp_i = ctx->beacon_periods_head;
        dbg_assert (bp_i != ctx->beacon_periods_tail);
        bp = &ctx->beacon_periods[bp_i];
        sched = &ctx->schedules[bp->schedule_index];
        alloc_i = 0;
        vcs_end_date = bp->start_date;
        idle = false;
    }
    /* Find a suitable allocation in this beacon period or the next one. */
    bp_end_i = CA_ALLOC_NEXT_BEACON_PERIOD (bp_i);
    if (bp_end_i != ctx->beacon_periods_tail)
        bp_end_i = CA_ALLOC_NEXT_BEACON_PERIOD (bp_i);
    new_backoff_drawn = false;
    do
    {
        /* Test if this allocation can be used. */
        if (alloc_i < sched->allocations_nb
            && sched->allocations[alloc_i].glid != MAC_LID_SPC_HOLE
            && less_mod2p32 (vcs_end_date, bp->start_date
                             + sched->allocations[alloc_i].end_offset_tck))
        {
            glid = sched->allocations[alloc_i].glid;
            mfs = ca_access_choose_mfs_tx (ctx, glid);
            if (mfs)
            {
                /* TODO: test if it have pb to send. */
                cfp = !CA_ALLOC_IS_CSMA (glid);
                hybrid = CA_ALLOC_IS_HYBRID (ctx->config->coexistence_mode,
                                             glid);
                /* TODO: with ACK, depends of the RIFS. */
                /* TODO: need special handling for beacons. */
                minimum_length = MAC_PREAMBLE_TCK
                    + (hybrid ? MAC_FC_10_TCK : 0)
                    + MAC_FC_AV_TCK * ctx->config->fc_symbols_nb
                    + MAC_DX567_TCK + MAC_AIFS_TCK;
                if (!(cfp || idle))
                {
                    /* The backoff should be drawn now. */
                    if (new_backoff_drawn)
                        ca_backoff_cancel (ctx);
                    ca_backoff_new (ctx, mfs->cap);
                    new_backoff_drawn = true;
                    minimum_length += (2 + ctx->backoff.bc) * MAC_SLOT_TCK;
                }
                if (less_mod2p32 (vcs_end_date + minimum_length,
                                  bp->start_date +
                                  sched->allocations[alloc_i].end_offset_tck))
                {
                    /* Fit. */
                    break;
                }
            }
        }
        /* Advance to the next allocation. */
        if (alloc_i == sched->allocations_nb)
        {
            /* Next beacon period. */
            bp_i = CA_ALLOC_NEXT_BEACON_PERIOD (bp_i);
            if (bp_i == bp_end_i)
            {
                /* Give up. */
                break;
            }
            bp = &ctx->beacon_periods[bp_i];
            sched = &ctx->schedules[bp->schedule_index];
            alloc_i = 0;
            vcs_end_date = bp->start_date;
        }
        else
        {
            /* Next allocation. */
            vcs_end_date = bp->start_date
                + sched->allocations[alloc_i].end_offset_tck;
            alloc_i++;
        }
        /* IDLE for EIFS only works for the first allocation.  If we land in
         * another allocation, the synchronisation is done. */
        idle = false;
    } while (1);
    /* TODO: combine with an MME. */
    /* Was a allocation found? */
    if (bp_i != bp_end_i)
    {
        /* Setup ACCESS for this allocation. */
        if (!(cfp || idle))
        {
            phy_access_backoff_start (ctx->phy, vcs_end_date, mfs->cap);
            phy_access_timer_program (ctx->phy, vcs_end_date
                                      + (ctx->backoff.bc + 2) * MAC_SLOT_TCK
                                      - anticipation_tck);
        }
        else
        {
            phy_access_timer_program (ctx->phy, vcs_end_date
                                      - anticipation_tck);
        }
        /* Record parameters if VCS need to be reprogrammed. */
        ctx->vcs_start_date = start_date;
        ctx->vcs_length_tck = length_tck;
        ctx->vcs_eifs = eifs;
        ctx->anticipation_tck = anticipation_tck;
    }
    else
    {
        ctx->vcs_length_tck = 0;
    }
}

void
ca_access_program (ca_t *ctx, u32 date, uint anticipation_tck)
{
    dbg_assert (ctx);
    phy_access_timer_program (ctx->phy, date - anticipation_tck);
}

void
ca_access_grant (ca_t *ctx, mfs_tx_t *mfs, uint duration_tck)
{
    dbg_assert (ctx);
    if (!mfs)
    {
        /* Choose an MFS. */
        ctx->access_param.mfs =
            PARENT_OF (mfs_tx_t, link, heap_get_root (&ctx->mfs_heap));
    }
    else
    {
        /* Use the given one. */
        ctx->access_param.mfs = mfs;
    }
    ctx->access_param.duration_tck = duration_tck;
    ctx->access_param.content = false;
}

const ca_access_param_t *
ca_access_get_param (ca_t *ctx)
{
    dbg_assert (ctx);
    return &ctx->access_param;
}

static mfs_tx_t *
ca_access_choose_mfs_tx (ca_t *ctx, uint glid)
{
    dbg_assert (ctx);
    dbg_assert (glid >= MAC_GLID_MIN);
    /* If GLID, this is a CFP allocation, else choose the MFS with the greater
     * priority. */
    if (glid <= MAC_GLID_MAX)
        return mac_store_mfs_get_tx (ctx->store, false, false, glid, 0);
    else if (CA_ALLOC_IS_CSMA (glid))
        return PARENT_OF (mfs_tx_t, link, heap_get_root (&ctx->mfs_heap));
    else /* \todo support beacon TX. */
        return NULL;
}