summaryrefslogtreecommitdiff
path: root/cesar/mac/ca/src/alloc.c
blob: b7c40a39b1f746b47f470ad858e8cb9c750620aa (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
/* 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"
#include "common/defs/homeplugAV.h"

ca_beacon_period_t *
ca_alloc_find_beacon_period (ca_t *ctx, u32 date)
{
    ca_beacon_period_t *bp, *prev_bp, *last_bp;
    dbg_assert (ctx);
    last_bp = ctx->beacon_periods + ctx->beacon_periods_nb;
    prev_bp = last_bp;
    for (bp = ctx->beacon_periods;
         bp != last_bp && lesseq_mod2p32 (bp->start_date, date);
         prev_bp = bp, bp++)
        ;
    return prev_bp == last_bp ? NULL : prev_bp;
}

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 = 0; i != ctx->beacon_periods_nb; i++)
        dbg_assert (ctx->beacon_periods[i].schedule_index != index);
    return &ctx->schedules[index];
}

void ARCH_ILRAM
ca_alloc_prepare (ca_t *ctx)
{
    ca_beacon_period_t *bp;
    ca_schedule_t *sched;
    uint alloc_i;
    bool merge = false;
    dbg_claim (ctx);
    dbg_claim (ctx->current_beacon_period);
    /* Use current schedule. */
    bp = ctx->current_beacon_period;
    sched = &ctx->schedules[bp->schedule_index];
    alloc_i = ctx->current_allocation_index;
    u32 beacon_period_start_date = bp->start_date;
    uint glid = sched->allocations[alloc_i].glid;
    u32 aifs_date = beacon_period_start_date
        + sched->allocations[alloc_i].end_offset_tck;
    u32 allocation_end_date = aifs_date - MAC_AIFS_TCK;
    if (MAC_LID_IS_BEACON (glid))
        allocation_end_date += MAC_AIFS_TCK - MAC_B2BIFS_TCK;
    /* Look at next beacon period. */
    ca_beacon_period_t *next_bp = bp + 1;
    if (next_bp != ctx->beacon_periods + ctx->beacon_periods_nb)
    {
        ca_schedule_t *next_sched = &ctx->schedules[next_bp->schedule_index];
        dbg_claim (next_sched->allocations_nb > 0);
        uint next_glid = next_sched->allocations[0].glid;
        /* Move AIFS date to beacon start. */
        if (alloc_i == sched->allocations_nb - 1)
        {
            aifs_date = next_bp->start_date;
            /* Test for CSMA allocation merging. */
            if (CA_ALLOC_IS_CSMA (glid)
                && CA_ALLOC_IS_CSMA (next_glid)
                && (CA_ALLOC_IS_HYBRID (sched->coexistence_mode, glid)
                    == CA_ALLOC_IS_HYBRID (next_sched->coexistence_mode,
                                           next_glid)))
                merge = true;
        }
        /* Test for beacon period overflow.
         * If the current allocation extends past the next beacon period start
         * date, it must be shrunk to avoid overlapping.
         * There should be a B2BIFS between last allocation and a eventual
         * beacon allocation at start of next beacon period. */
        u32 shrink_date = next_bp->start_date - MAC_AIFS_TCK;
        if (MAC_LID_IS_BEACON (next_glid))
            shrink_date += MAC_AIFS_TCK - MAC_B2BIFS_TCK;
        if (less_mod2p32 (shrink_date, allocation_end_date))
            allocation_end_date = shrink_date;
        /* Add margin in case of merge. */
        if (merge)
        {
            allocation_end_date += CA_ACCESS_MERGE_MARGIN_TCK;
            /* If this is not true, be prepared for complications. */
            dbg_assert (next_sched->allocations[0].end_offset_tck
                        >= CA_ACCESS_MERGE_MARGIN_TCK);
        }
    }
    /* Store allocation parameters. */
    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.merge = merge;
    ctx->current_allocation_param.nek_switch = sched->nek_switch;
    ctx->current_allocation_param.end_date = allocation_end_date;
    ctx->current_allocation_param.aifs_date = aifs_date;
    ctx->current_allocation_param.beacon_period_start_date =
        beacon_period_start_date;
    ctx->current_allocation_param.glid = glid;
    CA_TRACE (ALLOC_PREPARE, beacon_period_start_date, allocation_end_date,
              glid);
}

void
ca_alloc_update_beacon_periods (ca_t *ctx,
                                ca_beacon_period_t *beacon_periods,
                                uint beacon_periods_nb)
{
    u32 now;
    uint i;
    dbg_assert (ctx);
    dbg_assert (beacon_periods);
    dbg_assert (beacon_periods_nb > 0
                && beacon_periods_nb < CA_BEACON_PERIOD_NB);
    CA_TRACE (ALLOC_UPDATE_BEACON_PERIODS, phy_date ());
    uint flags = arch_isr_lock ();
    /* First beacon period provided should be the current one. */
    now = phy_date ();
    dbg_assert (ctx->state == CA_STATE_IDLE
                || (beacon_periods[0].start_date
                    == ctx->current_beacon_period->start_date));
    /* Update periods. */
    for (i = 0; i < beacon_periods_nb; i++)
    {
        ctx->beacon_periods[i] = beacon_periods[i];
        /* Add some consistency checks for distracted coders. */
        ca_schedule_t *sched =
            &ctx->schedules[beacon_periods[i].schedule_index];
        dbg_assert (sched->coexistence_mode < MAC_COEXISTENCE_NB
                    && sched->snid < HPAV_SNID_NB
                    && sched->nek_switch < MAC_NEK_INDEX_NB
                    && sched->allocations_nb > 0
                    && sched->allocations[0].end_offset_tck > 0);
    }
    ctx->beacon_periods_nb = beacon_periods_nb;
    /* Reschedule. */
    if (ctx->state != CA_STATE_IDLE)
    {
        /* Update current allocation. */
        ca_beacon_period_t *bp = ctx->beacon_periods;
        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;
        ctx->current_allocation_index = alloc_i;
        ca_alloc_prepare (ctx);
        arch_isr_unlock (flags);
        /* Update schedule. */
        ca_access_update (ctx);
    }
    else
        arch_isr_unlock (flags);
}