summaryrefslogtreecommitdiff
path: root/mac/ca/src/access.c
blob: a312f047f1d6a9765f86f4cd098b5809280f9e66 (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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/* 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"
#include "mac/ca/inc/mfs.h"

const ca_access_alloc_param_t *
ca_access_activate (ca_t *ctx, u32 date, uint anticipation_tck)
{
    dbg_assert (ctx && ctx->state == CA_STATE_IDLE);
    CA_TRACE (ACCESS_ACTIVATE, date);
    /* Change to ACTIVATED state. */
    ctx->state = CA_STATE_ACTIVATED;
    /* Find current allocation. */
    uint bp_i = ca_alloc_find_beacon_period (ctx, date);
    /* The beacon period must be found or we have a real time problem not
     * handled for the moment. */
    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, date - bp->start_date);
    ctx->current_beacon_period = bp_i;
    ctx->current_allocation_index = alloc_i;
    /* Reschedule. */
    bool hybrid = CA_ALLOC_IS_HYBRID (
        sched->coexistence_mode,
        sched->allocations[ctx->current_allocation_index].glid);
    ca_access_vcs_restart (ctx, date, hybrid ? MAC_EIFS_10_TCK
                           : MAC_EIFS_AV_TCK, anticipation_tck, true);
    /* Return current allocation parameters. */
    ctx->current_allocation_param.coexistence_mode = sched->coexistence_mode;
    ctx->current_allocation_param.hybrid = hybrid;
    ctx->current_allocation_param.nek_switch = sched->nek_switch;
    return &ctx->current_allocation_param;
}

void
ca_access_deactivate (ca_t *ctx)
{
    dbg_assert (ctx && ctx->state != CA_STATE_IDLE);
    CA_TRACE (ACCESS_DEACTIVATE);
    /* Cancel programmed timer. */
    phy_access_timer_cancel (ctx->phy);
    /* Change to IDLE state. */
    ctx->state = CA_STATE_IDLE;
}

void
ca_access_hold (ca_t *ctx)
{
    dbg_assert (ctx && ctx->state != CA_STATE_IDLE);
    /* Change to HOLD state. */
    ctx->state = CA_STATE_HOLD;
}

/**
 * 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);

/**
 * Prepare current allocation for access updates.
 * \param  ctx  ca context
 * \param  start_date  start date within the requested allocation
 * \param  sched_p  current schedule
 * \param  allocation_end_date_p  current allocation end date
 * \param  glid_p  current global link identifier
 */
static void
ca_access_prepare_alloc (ca_t *ctx, u32 start_date, ca_schedule_t **sched_p,
                         u32 *allocation_end_date_p, uint *glid_p)
{
    uint bp_i;
    ca_beacon_period_t *bp;
    ca_schedule_t *sched;
    uint alloc_i;
    uint aifs_tck = MAC_AIFS_TCK;
    dbg_assert (ctx);
    dbg_assert (sched_p && allocation_end_date_p && glid_p);
    dbg_assert (ctx->current_beacon_period != ctx->beacon_periods_tail);
    /* Use current schedule one. */
    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 allocation_end_date = bp->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_assert (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;
    /* Return the requested information. */
    *sched_p = sched;
    *allocation_end_date_p = allocation_end_date;
    *glid_p = sched->allocations[alloc_i].glid;
}

/**
 * Setup an AIFS ACCESS for the next allocation.
 * \param  ctx  ca context
 * \param  allocation_end_date  allocation end date (without AIFS)
 */
static void
ca_access_goto_aifs (ca_t *ctx, u32 allocation_end_date)
{
    uint bp_i;
    ca_beacon_period_t *bp;
    ca_schedule_t *sched;
    dbg_assert (ctx);
    /* Get current schedule. */
    bp_i = ctx->current_beacon_period;
    dbg_assert (bp_i != ctx->beacon_periods_tail);
    bp = &ctx->beacon_periods[bp_i];
    sched = &ctx->schedules[bp->schedule_index];
    /* AIFS ACCESS, no MFS. */
    ctx->access_param.mfs = NULL;
    if (ctx->current_allocation_index + 1 == sched->allocations_nb)
    {
        /* Last allocation, go on with the next beacon period (which MUST
         * exist). */
        uint next_bp_i = CA_ALLOC_NEXT_BEACON_PERIOD (bp_i);
        dbg_assert (next_bp_i != ctx->beacon_periods_tail);
        /* Note that we do not care if this is an AIFS or a B2BIFS. */
        ctx->access_param.access_date =
            ctx->beacon_periods[next_bp_i].start_date - MAC_AIFS_TCK;
    }
    else
    {
        /* Next allocation. */
        ctx->access_param.access_date = allocation_end_date;
    }
    /* No anticipation delay for AIFS, cancel the addition done later. */
    ctx->access_param.access_date += ctx->anticipation_tck;
}

void
ca_access_vcs_restart (ca_t *ctx, u32 start_date, uint length_tck,
                       uint anticipation_tck, bool eifs)
{
    ca_schedule_t *sched;
    u32 allocation_end_date;
    uint glid;
    dbg_assert (ctx && ctx->state != CA_STATE_IDLE);
    CA_TRACE (ACCESS_VCS_RESTART, start_date, length_tck, anticipation_tck,
              eifs);
    /* Prepare allocation. */
    ca_access_prepare_alloc (ctx, start_date, &sched, &allocation_end_date,
                             &glid);
    /* Test for allocation overflow or unusable allocation. */
    if (!less_mod2p32 (start_date + length_tck, allocation_end_date)
        || !CA_ALLOC_IS_USABLE (glid))
    {
        ca_access_goto_aifs (ctx, allocation_end_date);
        /* Here, no hope for any frame reception, do not even activate RX. */
        ctx->access_param.cw_start_date = allocation_end_date;
    }
    /* No overflow, ACCESS event. */
    else
    {
        u32 access_date = start_date + length_tck;
        bool csma = CA_ALLOC_IS_CSMA (glid);
        uint cap = 0;
        /* PRP? */
        if (csma && !eifs)
            access_date += 2 * MAC_SLOT_TCK;
        ctx->access_param.cw_start_date = access_date;
        /* Find a suitable MFS. */
        mfs_tx_t *mfs = ca_access_choose_mfs_tx (ctx, glid);
        if (mfs && mfs->ca_state != CA_MFS_STATE_UNKNOWN)
        {
            bool hybrid = CA_ALLOC_IS_HYBRID (sched->coexistence_mode, glid)
                || mfs->beacon;
            cap = mfs->cap;
            if (csma)
            {
                ca_backoff_new (ctx, cap);
                access_date += ctx->backoff.bc * MAC_SLOT_TCK;
            }
            /* Test again for allocation overflow after backoff update. */
            if (!less_mod2p32 (access_date, allocation_end_date))
            {
                ca_access_goto_aifs (ctx, allocation_end_date);
            }
            else
            {
                /* Set Access parameters. */
                ca_beacon_period_t *bp =
                    &ctx->beacon_periods[ctx->current_beacon_period];
                ctx->access_param.mfs = mfs;
                ctx->access_param.access_date = access_date;
                ctx->access_param.beacon_period_start_date = bp->start_date;
                ctx->access_param.duration_tck =
                    allocation_end_date - access_date;
                ctx->access_param.cfp = !csma;
                ctx->access_param.hybrid = hybrid;
            }
        }
        else
        {
            ca_access_goto_aifs (ctx, allocation_end_date);
        }
        /* Reactivate RX or start PRP. */
        if (!eifs)
        {
            if (csma)
            {
                phy_access_backoff_start (ctx->phy, start_date + length_tck,
                                          cap);
            }
            else
            {
                phy_rx_activate (ctx->phy, false,
                                 ctx->access_param.cw_start_date, true);
            }
        }
    }
    /* Setup ACCESS for this allocation. */
    phy_access_timer_program (ctx->phy, ctx->access_param.access_date
                              - anticipation_tck);
    /* Record result for debug. */
    if (ctx->access_param.mfs)
        CA_TRACE (ACCESS_VCS_RESTART_MFS, ctx->access_param.mfs,
                  ctx->access_param.access_date,
                  ctx->access_param.beacon_period_start_date,
                  ctx->access_param.duration_tck, ctx->access_param.cfp,
                  ctx->access_param.hybrid);
    else
        CA_TRACE (ACCESS_VCS_RESTART_AIFS, ctx->access_param.access_date);
    /* 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;
    /* Change state. */
    ctx->state = CA_STATE_ACTIVATED;
}

void
ca_access_program (ca_t *ctx, u32 date, uint anticipation_tck)
{
    dbg_assert (ctx && ctx->state != CA_STATE_IDLE);
    CA_TRACE (ACCESS_PROGRAM, date, anticipation_tck);
    phy_access_timer_program (ctx->phy, date - anticipation_tck);
    ctx->access_param.mfs = NULL;
    /* Change state, should not be updated. */
    ctx->state = CA_STATE_HOLD;
}

void
ca_access_grant (ca_t *ctx, mfs_tx_t *mfs, u32 date, uint duration_tck)
{
    dbg_assert (ctx && ctx->state != CA_STATE_IDLE);
    CA_TRACE (ACCESS_GRANT, mfs, date, duration_tck);
    if (!mfs)
    {
        dbg_assert (!heap_empty (&ctx->mfs_heap)); /* TODO */
        /* Choose an MFS. */
        mfs = PARENT_OF (mfs_tx_t, ca_prio_link, heap_get_root
                         (&ctx->mfs_heap));
        dbg_assert (mfs->ca_state == CA_MFS_STATE_PRIO_QUEUED);
    }
    else
    {
        /* Use the given one. */
    }
    ctx->access_param.mfs = mfs;
    ctx->access_param.access_date = date;
    ctx->access_param.duration_tck = duration_tck;
    ctx->access_param.cfp = true;
    ctx->access_param.hybrid = false; /* TODO */
    /* Change state, should not be updated. */
    ctx->state = CA_STATE_HOLD;
}

void
ca_access_defer (ca_t *ctx, u32 date, uint anticipation_tck)
{
    dbg_assert (ctx && ctx->state != CA_STATE_IDLE);
    CA_TRACE (ACCESS_DEFER, date, anticipation_tck);
    ca_schedule_t *sched;
    u32 allocation_end_date;
    uint glid;
    /* Prepare allocation. */
    ca_access_prepare_alloc (ctx, date, &sched, &allocation_end_date, &glid);
    /* Go directly to AIFS. */
    ca_access_goto_aifs (ctx, allocation_end_date);
    /* Setup ACCESS for this allocation. */
    phy_access_timer_program (ctx->phy, ctx->access_param.access_date
                              - anticipation_tck);
    /* Record parameters if VCS need to be reprogrammed. */
    ctx->anticipation_tck = anticipation_tck;
    /* Change state. */
    ctx->state = CA_STATE_ACTIVATED;
}

const ca_access_alloc_param_t *
ca_access_aifs (ca_t *ctx)
{
    dbg_assert (ctx && ctx->state != CA_STATE_IDLE);
    dbg_assert (ctx->current_beacon_period != ctx->beacon_periods_tail);
    CA_TRACE (ACCESS_AIFS);
    /* Go on with the next allocation, first get current one. */
    uint bp_i = ctx->current_beacon_period;
    ca_beacon_period_t *bp = &ctx->beacon_periods[bp_i];
    ca_schedule_t *sched = &ctx->schedules[bp->schedule_index];
    uint alloc_i = ctx->current_allocation_index;
    u32 access_date;
    /* Last allocation? */
    if (alloc_i + 1 == sched->allocations_nb)
    {
        bp_i = CA_ALLOC_NEXT_BEACON_PERIOD (bp_i);
        dbg_assert (bp_i != ctx->beacon_periods_tail);
        alloc_i = 0;
        access_date = ctx->beacon_periods[bp_i].start_date;
        /* Prepare next beacon period. */
        ca_mfs_next_beacon_period (ctx);
    }
    else
    {
        access_date = bp->start_date
            + sched->allocations[alloc_i].end_offset_tck;
        alloc_i++;
    }
    ctx->current_beacon_period = bp_i;
    ctx->current_allocation_index = alloc_i;
    /* Reschedule. */
    ca_access_vcs_restart (ctx, access_date, 0, ctx->anticipation_tck, false);
    /* Return current allocation parameters. */
    ctx->current_allocation_param.coexistence_mode = sched->coexistence_mode;
    ctx->current_allocation_param.hybrid = CA_ALLOC_IS_HYBRID (
        sched->coexistence_mode,
        sched->allocations[ctx->current_allocation_index].glid);
    ctx->current_allocation_param.nek_switch = sched->nek_switch;
    return &ctx->current_allocation_param;
}

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

void
ca_access_update (ca_t *ctx, u32 date)
{
    dbg_assert (ctx);
    if (ctx->state >= CA_STATE_ACTIVATED)
    {
        if (lesseq_mod2p32 (ctx->vcs_start_date + ctx->vcs_length_tck, date))
        {
            ca_access_vcs_restart (ctx, date, 0, ctx->anticipation_tck,
                                   false);
        }
        else if (less_mod2p32 (date, ctx->vcs_start_date))
        {
            ca_access_vcs_restart (ctx, ctx->vcs_start_date,
                                   ctx->vcs_length_tck, ctx->anticipation_tck,
                                   ctx->vcs_eifs);
        }
        else
        {
            ca_access_vcs_restart (ctx, date, ctx->vcs_length_tck
                                   - (date - ctx->vcs_start_date),
                                   ctx->anticipation_tck, ctx->vcs_eifs);
        }
    }
}

static mfs_tx_t *
ca_access_choose_mfs_tx (ca_t *ctx, uint glid)
{
    mfs_tx_t *mfs;
    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 (!CA_ALLOC_IS_CSMA (glid))
    {
        dbg_assert (glid <= MAC_GLID_MAX
                    || glid == MAC_LID_SPC_CENTRAL
                    || glid == MAC_LID_DISCOVER);
        mfs = mac_store_mfs_get_tx (ctx->store, false, false, glid, 0);
        dbg_assert (!mfs || mfs->ca_state == CA_MFS_STATE_UNKNOWN
                    || mfs->ca_state == CA_MFS_STATE_CFP_QUEUED
                    || mfs->ca_state == CA_MFS_STATE_HELD);
        /* Reference is borrowed from the store. */
        if (mfs)
            blk_release (mfs);
        if (mfs && mfs->ca_state == CA_MFS_STATE_CFP_QUEUED)
            return mfs;
        else
            return NULL;
    }
    else
    {
        if (!heap_empty (&ctx->mfs_heap))
        {
            mfs = PARENT_OF (mfs_tx_t, ca_prio_link,
                             heap_get_root (&ctx->mfs_heap));
            dbg_assert (mfs->ca_state == CA_MFS_STATE_PRIO_QUEUED);
            return mfs;
        }
        else
            return NULL;
    }
}