summaryrefslogtreecommitdiff
path: root/cesar/mac/ca/src/alloc.c
blob: 368db7c9cedfdec0fc49edd5775861225ae7ece8 (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
/* Cesar project {{{
 *
 * Copyright (C) 2007 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    mac/ca/src/alloc.c
 * \brief   Beacon period and schedule related functions.
 * \ingroup mac_ca
 */
#include "common/std.h"

#include "mac/ca/inc/context.h"
#include "mac/ca/inc/alloc.h"
#include "mac/ca/inc/access.h"
#include "hal/phy/phy.h"

#include "mac/common/timings.h"

uint
ca_alloc_find_beacon_period (const ca_t *ctx, u32 date)
{
    uint pi, i;
    dbg_assert (ctx);
    pi = ctx->beacon_periods_tail;
    for (i = ctx->beacon_periods_head;
         i != ctx->beacon_periods_tail
         && lesseq_mod2p32 (ctx->beacon_periods[i].start_date, date);
         pi = i, i = CA_ALLOC_NEXT_BEACON_PERIOD (i))
        ;
    return pi;
}

uint
ca_alloc_find (const ca_schedule_t *sched, uint offset_tck)
{
    uint a, b, m;
    /* Dichotomy search. */
    a = 0;
    b = sched->allocations_nb;
    while (a != b)
    {
        m = (a + b) / 2;
        if (offset_tck < sched->allocations[m].end_offset_tck)
            b = m;
        else
            a = m + 1;
    }
    return a;
}

ca_schedule_t *
ca_alloc_get_schedule (ca_t *ctx, uint index)
{
    uint i;
    dbg_assert (ctx);
    dbg_assert (index < CA_SCHEDULE_NB);
    /* Check this schedule is not used. */
    for (i = ctx->beacon_periods_head;
         i != ctx->beacon_periods_tail;
         i = CA_ALLOC_NEXT_BEACON_PERIOD (i))
        dbg_assert (ctx->beacon_periods[i].schedule_index != index);
    return &ctx->schedules[index];
}

void ARCH_ILRAM
ca_alloc_prepare (ca_t *ctx)
{
    uint bp_i;
    ca_beacon_period_t *bp;
    ca_schedule_t *sched;
    uint alloc_i;
    uint aifs_tck = MAC_AIFS_TCK;
    dbg_claim (ctx);
    dbg_assert (ctx->current_beacon_period != ctx->beacon_periods_tail);
    /* Use current schedule. */
    bp_i = ctx->current_beacon_period;
    bp = &ctx->beacon_periods[bp_i];
    sched = &ctx->schedules[bp->schedule_index];
    alloc_i = ctx->current_allocation_index;
    /* Test for beacon period overflow.
     * If the current allocation extends past the next beacon period start
     * date, it must be shrunk to avoid overlapping. */
    u32 beacon_period_start_date = bp->start_date;
    u32 allocation_end_date = beacon_period_start_date
        + sched->allocations[alloc_i].end_offset_tck;
    uint next_bp_i = CA_ALLOC_NEXT_BEACON_PERIOD (bp_i);
    if (next_bp_i != ctx->beacon_periods_tail
        && less_mod2p32 (ctx->beacon_periods[next_bp_i].start_date,
                         allocation_end_date))
    {
        allocation_end_date = ctx->beacon_periods[next_bp_i].start_date;
        /* There should be a B2BIFS between last allocation and a eventual
         * beacon allocation at start of next beacon period. */
        ca_schedule_t *next_sched =
            &ctx->schedules[ctx->beacon_periods[next_bp_i].schedule_index];
        dbg_claim (next_sched->allocations_nb > 0);
        if (next_sched->allocations[0].glid == MAC_LID_SPC_CENTRAL)
            aifs_tck = MAC_B2BIFS_TCK;
    }
    /* Reduce the allocation by the AIFS. */
    allocation_end_date -= aifs_tck;
    /* Store allocation parameters. */
    uint glid = sched->allocations[alloc_i].glid;
    ctx->current_allocation_param.coexistence_mode = sched->coexistence_mode;
    ctx->current_allocation_param.snid = sched->snid;
    ctx->current_allocation_param.hybrid =
        CA_ALLOC_IS_HYBRID (sched->coexistence_mode, glid);
    ctx->current_allocation_param.nek_switch = sched->nek_switch;
    ctx->current_allocation_param.end_date = allocation_end_date;
    ctx->current_allocation_param.beacon_period_start_date =
        beacon_period_start_date;
    ctx->current_allocation_param.glid = glid;
}

void
ca_alloc_update_beacon_periods (ca_t *ctx,
                                ca_beacon_period_t *beacon_periods,
                                uint beacon_periods_nb)
{
    u32 now;
    uint i, j;
    bool current_at_tail;
    dbg_assert (ctx);
    dbg_assert (beacon_periods);
    dbg_assert (beacon_periods_nb > 0
                && beacon_periods_nb < CA_BEACON_PERIOD_NB - 1);
    CA_TRACE (ALLOC_UPDATE_BEACON_PERIODS, phy_date ());
    /* Eliminate expired beacon period from context. */
    for (i = ctx->beacon_periods_head;
         i != ctx->beacon_periods_tail
         && less_mod2p32 (ctx->beacon_periods[i].start_date,
                          beacon_periods[0].start_date);
         i = CA_ALLOC_NEXT_BEACON_PERIOD (i))
        ;
    ctx->beacon_periods_head = i;
    current_at_tail = ctx->current_beacon_period == ctx->beacon_periods_tail;
    /* First beacon period provided should be the current one (actually, it
     * might be the second one...). */
    now = phy_date ();
    dbg_assert (lesseq_mod2p32 (beacon_periods[0].start_date, now)
                && (beacon_periods_nb < 2
                    || less_mod2p32 (now, beacon_periods[1].start_date)));
    /* Update periods. */
    for (j = 0, i = ctx->beacon_periods_head;
         j < beacon_periods_nb;
         j++, i = CA_ALLOC_NEXT_BEACON_PERIOD (i))
    {
        ctx->beacon_periods[i].start_date = beacon_periods[j].start_date;
        ctx->beacon_periods[i].schedule_index =
            beacon_periods[j].schedule_index;
        /* Add some consistency checks for distracted coders. */
        ca_schedule_t *sched =
            &ctx->schedules[beacon_periods[j].schedule_index];
        dbg_assert (sched->coexistence_mode < MAC_COEXISTENCE_NB
                    && sched->snid < 16
                    && sched->nek_switch < 2
                    && sched->allocations_nb > 0
                    && sched->allocations[0].end_offset_tck > 0);
    }
    ctx->beacon_periods_tail = i;
    dbg_assert ((ctx->beacon_periods_tail - ctx->beacon_periods_head +
                CA_BEACON_PERIOD_NB) % CA_BEACON_PERIOD_NB ==
                beacon_periods_nb);
    /* Fix current beacon period index. */
    if (current_at_tail)
        ctx->current_beacon_period = ctx->beacon_periods_tail;
    /* Reschedule. */
    if (ctx->state != CA_STATE_IDLE)
    {
        /* Update current allocation. */
        uint bp_i = ca_alloc_find_beacon_period (ctx, now + MAC_AIFS_TCK);
        dbg_assert (bp_i != ctx->beacon_periods_tail);
        ca_beacon_period_t *bp = &ctx->beacon_periods[bp_i];
        ca_schedule_t *sched = &ctx->schedules[bp->schedule_index];
        uint alloc_i = ca_alloc_find (sched, now + MAC_AIFS_TCK
                                      - bp->start_date);
        ctx->current_beacon_period = bp_i;
        ctx->current_allocation_index = alloc_i;
        ca_alloc_prepare (ctx);
        /* Update schedule. */
        ca_access_update (ctx);
    }
}