summaryrefslogtreecommitdiff
path: root/cesar/cp2/fsm/src/fsm.c
blob: 4c673e2bf457daca03f5c098c2f1c1e1b972e561 (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
/* Cesar project {{{
 *
 * Copyright (C) 2008 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    cp2/fsm/src/fsm.c
 * \brief   FSM engine.
 * \ingroup cp2_fsm
 */
#include "common/std.h"

#include "cp2/inc/context.h"

#include "fsm.h"
#include "inc/tables.h"

void
cp_fsm_init (cp_t *ctx)
{
    uint i;
    dbg_assert (ctx);
    for (i = 0; i < CP_FSM_INITIAL_NB; i++)
        ctx->fsm.active_states[i] = cp_fsm_initials_table[i];
    for (i = 0; i < CP_FSM_INITIAL_NB; i++)
        if (cp_fsm_enter_table[i])
            cp_fsm_enter_table[i] (ctx);
    ctx->fsm.handled_event = NULL;
    ctx->fsm.head = NULL;
    ctx->fsm.tail = NULL;
    cp_fsm_event_init (ctx);
}

void
cp_fsm_uninit (cp_t *ctx)
{
    dbg_assert (ctx);
    dbg_assert (!ctx->fsm.handled_event);
    cp_fsm_event_t *p, *pnext;
    for (p = ctx->fsm.head; p; p = pnext)
    {
        pnext = p->next;
        slab_release (p);
    }
}

/**
 * Execute callbacks attached to enter and leave action of states.
 * \param  ctx  control plane context
 * \param  i  active state index
 * \param  to  state entered
 */
static void
cp_fsm_leave_enter (cp_t *ctx, uint i, cp_fsm_state_t to)
{
    dbg_assert (ctx);
    dbg_assert (i < CP_FSM_INITIAL_NB);
    dbg_assert (to < CP_FSM_STATE_NB);
    cp_fsm_state_t from = ctx->fsm.active_states[i];
    dbg_assert (from < CP_FSM_STATE_NB);
    if (cp_fsm_leave_table[from])
        cp_fsm_leave_table[from] (ctx);
    ctx->fsm.active_states[i] = to;
    if (cp_fsm_enter_table[to])
        cp_fsm_enter_table[to] (ctx);
}

/**
 * Handle event for one active state.
 * \param  ctx  control plane context
 * \param  e  event to handle
 * \param  i  active state index
 */
static void
cp_fsm_handled_event (cp_t *ctx, cp_fsm_event_t *e, uint i)
{
    dbg_assert (ctx);
    dbg_assert (e);
    dbg_assert (i < CP_FSM_INITIAL_NB);
    cp_fsm_transition_t t =
        cp_fsm_transition_table[ctx->fsm.active_states[i]][e->type];
    if (t)
    {
        /* Transition with action. */
        ctx->fsm.handled_event = e;
        ctx->fsm.handled_active_state = i;
        e->handler (ctx, e, t);
        /* If state has not been set, this means that there is one and
         * only one branch for this transition.  Check this and change
         * state. */
        if (ctx->fsm.handled_event)
        {
            cp_fsm_state_t to =
                cp_fsm_only_branch_table[ctx->fsm.active_states[i]][e->type];
            dbg_assert_print (to != CP_FSM_STATE_NB,
                              "no branch selected");
            dbg_assert (to < CP_FSM_STATE_NB);
            if (to != CP_FSM_STATE_NB) /* Be clement for errors. */
                cp_fsm_leave_enter (ctx, i, to);
            ctx->fsm.handled_event = NULL;
        }
    }
    else
    {
        /* Transition without action. */
        cp_fsm_state_t to =
            cp_fsm_only_branch_table[ctx->fsm.active_states[i]][e->type];
        if (to != CP_FSM_STATE_NB)
        {
            dbg_assert (to < CP_FSM_STATE_NB);
            cp_fsm_leave_enter (ctx, i, to);
            ctx->fsm.handled_event = NULL;
        }
    }
}

void
cp_fsm_process (cp_t *ctx)
{
    uint i;
    dbg_assert (ctx);
    dbg_assert (!ctx->fsm.handled_event);
    if (ctx->fsm.head)
    {
        /* Extract event from queue. */
        cp_fsm_event_t *e = ctx->fsm.head;
        ctx->fsm.head = e->next;
        if (!e->next)
            dbg_invalid_ptr (ctx->fsm.tail);
        dbg_assert (e->type < CP_FSM_EVENT_TYPE_NB);
        dbg_assert (e->handler);
        /* Handle event. */
        for (i = 0; i < CP_FSM_INITIAL_NB; i++)
            cp_fsm_handled_event (ctx, e, i);
        /* Release event. */
        slab_release (e);
    }
}

void
cp_fsm_post (cp_t *ctx, cp_fsm_event_t *event)
{
    dbg_assert (ctx);
    dbg_assert_ptr (event);
    dbg_assert (event->next == NULL);
    dbg_assert (event->type < CP_FSM_EVENT_TYPE_NB);
    dbg_assert (event->handler);
    /* Add event to event queue, do not add reference as it is transfered to
     * us. */
    if (ctx->fsm.head)
    {
        dbg_assert_ptr (ctx->fsm.tail);
        ctx->fsm.tail->next = event;
    }
    else
        ctx->fsm.head = event;
    ctx->fsm.tail = event;
}

void
cp_fsm_branch_ (cp_t *ctx, cp_fsm_branch_t branch)
{
    dbg_assert (ctx);
    uint i = ctx->fsm.handled_active_state;
    dbg_assert (i < CP_FSM_INITIAL_NB);
    dbg_assert (ctx->fsm.active_states[i] < CP_FSM_STATE_NB);
    dbg_assert_print (ctx->fsm.handled_event,
                      "not in transition or duplicated branch");
    /* Check there is actually several branches. */
    dbg_assert_print (cp_fsm_only_branch_table[ctx->fsm.active_states[i]]
                      [ctx->fsm.handled_event->type] == CP_FSM_STATE_NB,
                      "no branch");
    /* Check this correspond to the current transition. */
    cp_fsm_state_t state = ((uint) branch >> 16) & 0xff;
    cp_fsm_event_type_t event_type = ((uint) branch >> 8) & 0xff;
    dbg_assert_print (state == ctx->fsm.active_states[i]
                      && event_type == ctx->fsm.handled_event->type,
                      "bad branch");
    /* Select this branch. */
    cp_fsm_state_t to = ((uint) branch) & 0xff;
    cp_fsm_leave_enter (ctx, i, to);
    ctx->fsm.handled_event = NULL;
}