-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleFSM.c
88 lines (76 loc) · 1.94 KB
/
SimpleFSM.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
#include <stdio.h>
int entry_state(void);
int foo_state(void);
int bar_state(void);
int exit_state(void);
/* array and enum below must be in sync! */
int (* state[])(void) = { entry_state, foo_state, bar_state, exit_state};
enum state_codes { entry, foo, bar, end};
enum ret_codes { ok, fail, repeat};
struct transition {
enum state_codes src_state;
enum ret_codes ret_code;
enum state_codes dst_state;
};
/* transitions from end state aren't needed */
struct transition state_transitions[] = {
{entry, ok, foo},
{entry, fail, end},
{foo, ok, bar},
{foo, fail, end},
{foo, repeat, foo},
{bar, ok, end},
{bar, fail, end},
{bar, repeat, foo}
};
#define EXIT_STATE end
#define ENTRY_STATE entry
#define EXIT_SUCCESS 0
int entry_state(void) {
printf("entry_state\r\n");
return 0;
}
int foo_state(void) {
printf("foo_state\r\n");
static int count = 0;
count++;
if(count < 5) {
return 2;
}
else {
return 0;
}
}
int bar_state(void) {
printf("bar_state\r\n");
return 0;
}
int exit_state(void) {
printf("exit_state\r\n");
return 0;
}
int lookup_transitions(enum state_codes cur_state,enum ret_codes rc) {
enum state_codes ret = EXIT_STATE;
int transition_len = sizeof(state_transitions)/sizeof(state_transitions[0]);
for(int i = 0; i < transition_len; i++) {
if(cur_state == state_transitions[i].src_state && rc == state_transitions[i].ret_code) {
ret = state_transitions[i].dst_state;
}
}
return ret;
}
int main(int argc, char *argv[]) {
enum state_codes cur_state = ENTRY_STATE;
enum ret_codes rc;
int (* state_fun)(void);
for (;;) {
state_fun = state[cur_state];
rc = state_fun();
if (EXIT_STATE == cur_state)
{
break;
}
cur_state = lookup_transitions(cur_state, rc);
}
return EXIT_SUCCESS;
}