summaryrefslogtreecommitdiff
path: root/mac/ca/src/ca.c
blob: a5785ff882b44ffea7a903edae45685943631e68 (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
/* 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/common/timings.h"

ca_t ca_global;

/**
 * MFS priority comparaison function.
 * \param  left  left hand MFS
 * \param  right  right hand MFS
 * \return  true iff left has a greater priority than right
 */
static bool
ca_mfs_less (heap_node_t *left, heap_node_t *right);

ca_t *
ca_init (phy_t *phy, mac_config_t *config, mac_store_t *store)
{
    dbg_assert (phy);
    dbg_assert (config);
    dbg_assert (store);
    ca_t *ctx = &ca_global;
    ctx->phy = phy;
    ctx->config = config;
    ctx->store = store;
    ctx->access_param.mfs = NULL;
    ctx->access_param.access_date = 0;
    ctx->access_param.duration_tck = 0;
    ctx->access_param.cfp = false;
    ctx->access_param.content = false;
    ctx->vcs_start_date = 0;
    ctx->vcs_length_tck = 0;
    ctx->vcs_eifs = false;
    ctx->anticipation_tck = 0;
    ca_backoff_init (ctx);
    ctx->beacon_periods_head = ctx->beacon_periods_tail = 0;
    heap_init (&ctx->mfs_heap, ca_mfs_less);
    return ctx;
}

void
ca_uninit (ca_t *ctx)
{
    dbg_assert (ctx);
}

void
ca_mfs_init (ca_t *ctx, mfs_tx_t *mfs)
{
    dbg_assert (ctx);
    dbg_assert (mfs);
    heap_node_init (&mfs->link);
    mfs->ca_state = CA_MFS_STATE_UNKNOWN;
}

void
ca_mfs_uninit (ca_t *ctx, mfs_tx_t *mfs)
{
    dbg_assert (ctx);
    dbg_assert (mfs);
    if (mfs->ca_state == CA_MFS_STATE_PRIO_QUEUED)
    {
        heap_remove (&ctx->mfs_heap, &mfs->link);
    }
    mfs->ca_state = CA_MFS_STATE_UNKNOWN;
}

void
ca_mfs_update (ca_t *ctx, mfs_tx_t *mfs)
{
    dbg_assert (ctx);
    dbg_assert (mfs);
    //heap_adjust (&ctx->mfs_heap, &mfs->link);
}

static bool
ca_mfs_less (heap_node_t *left, heap_node_t *right)
{
    mfs_tx_t *lmfs = PARENT_OF (mfs_tx_t, link, left);
    mfs_tx_t *rmfs = PARENT_OF (mfs_tx_t, link, right);
    /* TODO: write a real comparison function. */
    return lmfs->cap < rmfs->cap;
}