forked from redocCheng/state_machine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
post_state.c
298 lines (253 loc) · 8.01 KB
/
post_state.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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
* Copyright (c) 2019, redoc
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2021-01-12 redoc the first version
*/
#define LOG_TAG "state.post"
#define LOG_LVL LOG_LVL_DBG
#include "state.h"
#include <ulog.h>
/* post state graph
*
*
* +-----------------------------------------------------------------+
* | root--------+ |
* | | |
* | | |
* | +----------+ fail +----------+ pass +----------+ |
* | | postfail | <-------- | post | --------> | postpass | |
* | +----------+ +----------+ +----------+ |
* | ^ \post |
* | post| \break |
* | break| \on |
* | off| \----> +-----------+ |
* | +------------- | postbreak | |
* | +-----------+ |
* | |
* +-----------------------------------------------------------------+
*
*/
enum event_post_type
{
EVENT_POST_NULL,
EVENT_POST_START,
EVENT_POST_BREAKON,
EVENT_POST_BREAKOFF,
EVENT_POST_ANSWER,
EVENT_POST_NUMS,
};
static struct state state_root, state_post, state_postpass, state_postfail,
state_postbreak;
static void print_msg_err( void *state_data, struct event *event );
static void print_msg_enter( void *state_data, struct event *event );
static void print_msg_exit( void *state_data, struct event *event );
static void state_post_enter( void *state_data, struct event *event );
static bool guard_post_pass( void *condition, struct event *event );
static bool guard_post_fail( void *condition, struct event *event );
static void action_post_break( void *oldstate_data, struct event *event,
void *state_new_data );
static void action_post_pass( void *oldstate_data, struct event *event,
void *state_new_data );
static void action_post_fail( void *oldstate_data, struct event *event,
void *state_new_data );
static struct state state_root = {
.state_parent = NULL,
.state_entry = NULL,
.transitions = (struct transition[]){
/* event_type, condition, guard, action, next, state */
{ EVENT_POST_START, NULL, NULL, NULL, &state_post },
},
.transition_nums = 1,
.data = "ROOT",
.action_entry = &print_msg_enter,
.action_exti = &print_msg_exit,
};
static struct state state_post = {
.state_parent = NULL,
.state_entry = NULL,
.transitions = (struct transition[]){
{ EVENT_POST_BREAKON, NULL, NULL, &action_post_break, &state_postbreak },
{ EVENT_POST_ANSWER, (void*)1, &guard_post_fail, &action_post_fail, &state_postfail },
{ EVENT_POST_ANSWER, (void*)2, &guard_post_pass, &action_post_pass, &state_postpass },
},
.transition_nums = 3,
.data = "POST",
.action_entry = &state_post_enter,
.action_exti = &print_msg_exit,
};
static struct state state_postpass = {
.state_parent = NULL,
.state_entry = NULL,
.data = "POSTPASS",
.action_entry = &print_msg_enter,
.action_exti = &print_msg_exit,
};
static struct state state_postfail = {
.state_parent = NULL,
.state_entry = NULL,
.data = "POSTFAIL",
.action_entry = &print_msg_enter,
.action_exti = &print_msg_exit,
};
static struct state state_postbreak = {
.state_parent = NULL,
.state_entry = NULL,
.transitions = (struct transition[]){
{ EVENT_POST_BREAKOFF, NULL, NULL, NULL, &state_post },
},
.transition_nums = 1,
.data = "POSTBREAK",
.action_entry = &print_msg_enter,
.action_exti = &print_msg_exit,
};
static struct state state_error = {
.data = "ERROR",
.action_entry = &print_msg_err,
};
/* post process start */
static void state_post_enter( void *state_data, struct event *event )
{
print_msg_enter(state_data, event);
log_i("post start...");
}
static void action_post_break( void *oldstate_data, struct event *event,
void *state_new_data )
{
log_i("post break,display break...");
}
static bool guard_post_pass( void *condition, struct event *event )
{
if(event->type != EVENT_POST_ANSWER)
{
return false;
}
return ((int)event->data == (int)condition);
}
static void action_post_pass( void *oldstate_data, struct event *event,
void *state_new_data )
{
log_i("post pass,display pass...");
}
static bool guard_post_fail( void *condition, struct event *event )
{
if(event->type != EVENT_POST_ANSWER)
{
return false;
}
return ((int)event->data == (int)condition);
}
static void action_post_fail( void *oldstate_data, struct event *event,
void *state_new_data )
{
log_i("post fail,display fail...");
}
/* post process end */
static void print_msg_err( void *state_data, struct event *event )
{
log_e( "entered error state!" );
}
static void print_msg_enter( void *state_data, struct event *event )
{
log_i( "entering %s state", (char *)state_data );
}
static void print_msg_exit( void *state_data, struct event *event )
{
log_i( "Eexiting %s state", (char *)state_data );
}
static rt_mq_t mq_event_post;
static struct state_machine m_post;
int state_post_event_set(enum event_post_type event, void* data)
{
struct event e;
RT_ASSERT(event < EVENT_POST_NUMS);
e.type = event;
e.data = data;
return rt_mq_send(mq_event_post, &e, sizeof(struct event));
}
static void state_process(void *parameter)
{
struct event e;
statem_init( &m_post, &state_root, &state_error );
while(1)
{
if(RT_EOK == rt_mq_recv(mq_event_post, &e, sizeof(struct event), 20))
{
statem_handle_event( &m_post, &(struct event){ e.type, e.data } );
}
}
}
static int state_post_init(void)
{
rt_thread_t tid = RT_NULL;
mq_event_post = rt_mq_create("event_post",sizeof(struct event), 16, RT_IPC_FLAG_FIFO);
tid = rt_thread_create("state_post", state_process, RT_NULL, 1024, 10, 100);
if (tid == RT_NULL)
{
rt_kprintf("state post initialize failed! thread create failed!\r\n");
return -RT_ENOMEM;
}
rt_thread_startup(tid);
return RT_EOK;
}
INIT_APP_EXPORT(state_post_init);
#ifdef FINSH_USING_MSH
static void post_event_set(uint8_t argc, char **argv)
{
struct event e;
if(argc < 2)
{
rt_kprintf("state post event set <event> <data>\n");
}
else
{
const char *operator = argv[1];
if (argc == 3)
{
e.data = (void*)atoi(argv[2]);
}
else
{
e.data = RT_NULL;
}
if (!rt_strcmp(operator, "start"))
{
e.type = EVENT_POST_START;
}
else if (!rt_strcmp(operator, "breakon"))
{
e.type = EVENT_POST_BREAKON;
}
else if (!rt_strcmp(operator, "breakoff"))
{
e.type = EVENT_POST_BREAKOFF;
}
else if (!rt_strcmp(operator, "answer"))
{
e.type = EVENT_POST_ANSWER;
}
else
{
rt_kprintf("state key set:%s\n",argv[1]);
return;
}
state_post_event_set(e.type, e.data);
}
}
MSH_CMD_EXPORT(post_event_set, state post event set <event> <data>.);
static void post_current_get(uint8_t argc, char **argv)
{
if(!m_post.state_current->data)
{
rt_kprintf("post current state is NULL\n");
}
else
{
rt_kprintf("post current state is %s\n",m_post.state_current->data);
}
}
MSH_CMD_EXPORT(post_current_get, get current state.);
#endif /* FINSH_USING_MSH */