-
Notifications
You must be signed in to change notification settings - Fork 0
/
fsm.c
99 lines (85 loc) · 1.66 KB
/
fsm.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
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
#define G_LOG_USE_STRUCTURED
#include <stdio.h>
#include <glib.h>
#include "fsm.h"
/*********************** FSM support *********************/
void doFSM(struct fsm *fsm,int event,void *data)
{
int (*handler)(int,int,void *);
int state;
struct fsmtable *fsmEntry;
nextEvent:
fsmEntry = fsm->table;
state = fsm->state;
if(fsm->debugFlag)
{
g_debug("\tFSM %s ",fsm->name);
if(fsm->stateNames != NULL)
{
g_debug("state %s ",(fsm->stateNames)[state]);
}
else
{
g_debug("state %d ",state);
}
if(fsm->eventNames != NULL)
{
if(event < 0)
g_debug("event = FSMNoEvent ");
else
g_debug("event %s \n",(fsm->eventNames)[event]);
}
else
{
g_debug("event %d \n",event);
}
}
while(fsmEntry->state != -1)
{
if( (fsmEntry->state == state) && (fsmEntry->event == event))
{
handler = fsmEntry->handler;
if(handler == NULL)
{
state = fsmEntry->nextState;
}
else
{
state = (handler)(state,event,data);
if(state == -1)
state = fsmEntry->nextState;
}
fsm->state = state;
if(fsm->nextEvent != -1)
{
event = fsm->nextEvent;
fsm->nextEvent = -1;
goto nextEvent;
}
return;
}
fsmEntry++;
}
if(fsm->debugFlag)
{
if(fsm->stateNames == NULL)
{
g_debug("No entry in FSM table %s for state %d ",fsm->name,state);
}
else
{
g_debug("No entry in FSM table %s for state %s",fsm->name,(fsm->stateNames)[state]);
}
if(fsm->eventNames == NULL)
{
g_debug("event = %d",event);
}
else
{
if(event < 0)
g_debug("event = FSMNoEvent ");
else
g_debug("event = %s",(fsm->eventNames)[event]);
}
}
}