-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathfsm.c
144 lines (115 loc) · 2.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
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
/*
* It's a part of smartfsm, by Shanjin Yang.
* Email: sjyangv0@gmail.com
*
*/
#include "fsm.h"
#include <string.h>
FSM *fsm_obj;
STATE_DIS *state_obj;
STATE_DIS *state_obj_copy;
FSM fsm_default =
{
0, NULL, "state_default",
};
int fsm_init()
{
state_obj = (STATE_DIS *)malloc(sizeof(STATE_DIS));
if (state_obj == NULL) {
printf("malloc error: %s %d\n", __FILE__, __LINE__);
return 0;
}
fsm_obj = (FSM *)malloc(sizeof(FSM));
if (fsm_obj == NULL) {
printf("malloc error: %s %d\n", __FILE__, __LINE__);
return 0;
}
return 0;
}
int state_init(int state, FUNC_PTR func, uchar *name)
{
fsm_init();
if ((state_obj == NULL) || ( fsm_obj == NULL))
ERROR_PRINTF;
fsm_obj->state = state;
fsm_obj->func = func;
fsm_obj->name = name;
state_obj->state = state;
state_obj->func = func;
state_obj->name = name;
state_obj_copy = state_obj;
list_init(&(state_obj->node));
return 0;
}
int state_add(int state, FUNC_PTR func, uchar *name)
{
/*Just make a symbol for state name*/
int token = (int)*name;
STATE_OBJ_MALLOC_AND_INSERT(token, state, func, name);
state_obj = (STATE_DIS *)(&(state_obj->node))->next;
return 0;
}
int state_remove(int state)
{
STATE_DIS *tmp = state_obj_copy;
while (!is_list_empty(&(tmp->node))) {
if (tmp->state == state) {
list_delete(&(tmp->node));
tmp->state = 10000;
tmp->func = NULL;
tmp->name = "";
return 0;
}
tmp = (STATE_DIS *)(&(tmp->node))->next;
}
if (tmp->state == state) {
list_delete(&(tmp->node));
tmp->state = 10000;
tmp->func = NULL;
tmp->name = "";
return 0;
}
printf("State no fund\n");
return 0;
}
int state_tran(int state, void *message)
{
STATE_DIS *tmp = state_obj_copy;
memset(fsm_obj->message, 0, MSG_LEN);
while (!is_list_empty(&(tmp->node))) {
if (tmp->state == state) {
fsm_obj->func = tmp->func;
fsm_obj->name = tmp->name;
fsm_obj->state = state;
if (message != NULL)
memcpy(fsm_obj->message, message, strlen((char *)message));
return 0;
}
tmp = (STATE_DIS *)(&(tmp->node))->next;
}
if (tmp->state == state) {
fsm_obj->name = tmp->name;
fsm_obj->state = state;
fsm_obj->func = tmp->func;
if (message != NULL)
memcpy(fsm_obj->message, message, strlen((char *)message));
return 0;
}
state_default(fsm_default.state, fsm_default.func, fsm_default.name);
printf("state is no found, and fsm will transfer to default, It is name: %s\n", fsm_obj->name);
return 0;
}
void fsm_while(FSM *obj)
{
while (1) {
printf("state name = %s\n", obj->name);
obj->func(obj->message);
}
}
int state_default(int state, FUNC_PTR func, uchar *name)
{
fsm_obj->state = state;
fsm_obj->func = func;
fsm_obj->name = name;
return 0;
}