-
Notifications
You must be signed in to change notification settings - Fork 0
/
ufsm.c
66 lines (61 loc) · 1.88 KB
/
ufsm.c
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
#include <stddef.h>
#include "ufsm.h"
void ufsm_change_state(ufsm_t *ufsm)
{
const state_desc_t *new_desc = &ufsm->desc[ufsm->state];
if ((0 == new_desc->timeout_max) || ((0 == new_desc->state_by_timeout)))
{
ufsm->timeout = 0;
ufsm->last_time = ufsm->get_time();
}
else if (new_desc->timeout_max == new_desc->timeout_min)
{
ufsm->timeout = new_desc->timeout_max;
ufsm->last_time = ufsm->get_time();
}
else
{
ufsm->timeout =
new_desc->timeout_min + ufsm->rand() % (new_desc->timeout_max - new_desc->timeout_min);
ufsm->last_time = ufsm->get_time();
}
if (NULL != new_desc->init)
{
new_desc->init();
}
}
#define UFSM_CHANGE_STATE(ufsm, state_var, new_state) \
{ \
ufsm->log(ufsm->ctx, "State %d %d -> %d", __LINE__, state_var, new_state); \
state_var = new_state; \
ufsm_change_state(ufsm); \
}
void ufsm_state_process(ufsm_t *ufsm)
{
const state_desc_t *desc = &ufsm->desc[ufsm->state];
uint8_t new_state = desc->process();
if (0 != new_state)
{
if (ufsm->max_state == new_state)
{
if (0 != ufsm->timeout)
{
ufsm->last_time = ufsm->get_time();
}
}
else
{
UFSM_CHANGE_STATE(ufsm, ufsm->state, new_state);
}
}
else
{
if (0 != ufsm->timeout)
{
if (ufsm->get_time() - ufsm->last_time > ufsm->timeout)
{
UFSM_CHANGE_STATE(ufsm, ufsm->state, desc->state_by_timeout);
}
}
}
}