summaryrefslogtreecommitdiff
path: root/cesar/mac/ca/src/ca.c
blob: b0e30b29c341cfad4f2d03edaf8799472dfc9ad1 (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
/* Cesar project {{{
 *
 * Copyright (C) 2007 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    mac/ca/src/ca.c
 * \brief   Channel Access main implementation file.
 * \ingroup mac_ca
 */
#include "common/std.h"

#include "mac/ca/inc/context.h"
#include "mac/ca/inc/access.h"
#include "mac/common/timings.h"

#include "hal/phy/phy.h"

ca_t ARCH_DLRAM_BSS ca_global;

ca_t *
ca_init (phy_t *phy, mac_config_t *config, mac_store_t *store,
         uint anticipation_tck)
{
    int cap;
    dbg_assert (phy);
    dbg_assert (config);
    dbg_assert (store);
    ca_t *ctx = &ca_global;
    ctx->state = CA_STATE_IDLE;
    ctx->phy = phy;
    ctx->config = config;
    ctx->store = store;
    ca_trace_init (ctx);
    ctx->anticipation_tck = anticipation_tck;
    ctx->access.mfs = NULL;
    ctx->access.unusable = true;
    ctx->access.param.access_date = 0;
    ctx->access.param.cw_start_date = 0;
    ctx->access.param.duration_tck = 0;
    ctx->access.param.aifs = false;
    ctx->access.param.prp = false;
    ctx->access.param.cfp = false;
    ca_backoff_init (ctx, config->seed);
    ctx->beacon_periods_nb = 0;
    ctx->current_beacon_period = NULL;
    ctx->current_allocation_index = 0;
    for (cap = 0; cap < MAC_CAP_NB; cap++)
        list_init (&ctx->prio[cap]);
    list_init (&ctx->held);
    CA_TRACE (INIT);
    return ctx;
}

void
ca_uninit (ca_t *ctx)
{
    int cap;
    dbg_assert (ctx && ctx->state == CA_STATE_IDLE);
    for (cap = 0; cap < MAC_CAP_NB; cap++)
        dbg_assert (list_empty (&ctx->prio[cap]));
    CA_TRACE (UNINIT);
    ca_trace_uninit (ctx);
}

void
ca_mfs_add (ca_t *ctx, mfs_tx_t *mfs)
{
    dbg_assert (ctx);
    dbg_assert (mfs);
    CA_TRACE (MFS_ADD, mfs);
    ca_mfs_update (ctx, mfs);
}

void
ca_mfs_remove (ca_t *ctx, mfs_tx_t *mfs)
{
    dbg_assert (ctx);
    dbg_assert (mfs);
    CA_TRACE (MFS_REMOVE, mfs);
    /* Lock. */
    uint flags = arch_isr_lock ();
    /* Remove the MFS. */
    switch (mfs->ca_state)
    {
    case CA_MFS_STATE_PRIO_QUEUED:
        list_remove (&ctx->prio[mfs->cap], &mfs->ca_link);
        break;
    case CA_MFS_STATE_HELD:
        list_remove (&ctx->held, &mfs->ca_link);
        break;
    default:
        ;
    }
    mfs->ca_state = CA_MFS_STATE_REMOVED;
    /* Unlock. */
    arch_isr_unlock (flags);
    /* The current ACCESS may have changed. */
    ca_access_update (ctx);
}

static void
ca_mfs_update_common (ca_t *ctx, mfs_tx_t *mfs, bool locked)
{
    ca_mfs_state_t new_state;
    dbg_assert (ctx);
    dbg_assert_ptr (mfs);
    dbg_assert (mfs->ca_state != CA_MFS_STATE_REMOVED);
    CA_TRACE (MFS_UPDATE, mfs);
    /* Lock. */
    uint flags = 0;
    if (!locked) flags = arch_isr_lock ();
    /* Ignore held MFS. */
    if (mfs->ca_state != CA_MFS_STATE_HELD)
    {
        /* Compute the new MFS state... */
        if (mfs->seg_nb + mfs->pending_seg_nb != 0
            || mfs == ctx->access.mfs)
        {
            if (mfs->cfp)
                new_state = CA_MFS_STATE_CFP_QUEUED;
            else
                new_state = CA_MFS_STATE_PRIO_QUEUED;
        }
        else
            new_state = CA_MFS_STATE_UNKNOWN;
        /* ...and execute the corresponding transition. */
        if (mfs->ca_state == CA_MFS_STATE_PRIO_QUEUED)
        {
            if (new_state != CA_MFS_STATE_PRIO_QUEUED || locked)
                list_remove (&ctx->prio[mfs->cap], &mfs->ca_link);
            if (new_state == CA_MFS_STATE_PRIO_QUEUED && locked)
                list_push (&ctx->prio[mfs->cap], &mfs->ca_link);
        }
        else if (new_state == CA_MFS_STATE_PRIO_QUEUED)
        {
            list_push (&ctx->prio[mfs->cap], &mfs->ca_link);
        }
        /* Done. */
        mfs->ca_state = new_state;
        /* Unlock. */
        if (!locked) arch_isr_unlock (flags);
        /* The current ACCESS may have changed. */
        if (!locked)
            ca_access_update (ctx);
    }
    else
    {
        /* Unlock. */
        if (!locked) arch_isr_unlock (flags);
    }
}

void
ca_mfs_update (ca_t *ctx, mfs_tx_t *mfs)
{
    ca_mfs_update_common (ctx, mfs, false);
}

void
ca_mfs_update_locked (ca_t *ctx, mfs_tx_t *mfs)
{
    ca_mfs_update_common (ctx, mfs, true);
}

static void
ca_mfs_hold_common (ca_t *ctx, mfs_tx_t *mfs, bool locked)
{
    dbg_assert (ctx);
    dbg_assert_ptr (mfs);
    dbg_assert (mfs->ca_state != CA_MFS_STATE_REMOVED);
    CA_TRACE (MFS_HOLD, mfs);
    /* Lock. */
    uint flags = 0;
    if (!locked) flags = arch_isr_lock ();
    /* Do nothing if held yet. */
    if (mfs->ca_state != CA_MFS_STATE_HELD)
    {
        /* Remove from priority queue. */
        if (mfs->ca_state == CA_MFS_STATE_PRIO_QUEUED)
            list_remove (&ctx->prio[mfs->cap], &mfs->ca_link);
        /* Add to hold list. */
        mfs->ca_state = CA_MFS_STATE_HELD;
        list_push (&ctx->held, &mfs->ca_link);
        /* Unlock. */
        if (!locked) arch_isr_unlock (flags);
        /* The current ACCESS may have changed. */
        if (!locked)
            ca_access_update (ctx);
    }
    else
    {
        /* Unlock. */
        if (!locked) arch_isr_unlock (flags);
    }
}

void
ca_mfs_hold (ca_t *ctx, mfs_tx_t *mfs)
{
    ca_mfs_hold_common (ctx, mfs, false);
}

void
ca_mfs_hold_locked (ca_t *ctx, mfs_tx_t *mfs)
{
    ca_mfs_hold_common (ctx, mfs, true);
}

void
ca_mfs_next_beacon_period (ca_t *ctx)
{
    dbg_assert (ctx);
    /* Unhold MFS. */
    while (!list_empty (&ctx->held))
    {
        mfs_tx_t *mfs = PARENT_OF (mfs_tx_t, ca_link, list_pop (&ctx->held));
        mfs->ca_state = CA_MFS_STATE_UNKNOWN;
        ca_mfs_update_locked (ctx, mfs);
    }
}