VSF Documented
vsf_fsm.h
Go to the documentation of this file.
1/*****************************************************************************
2 * Copyright(C)2009-2022 by VSF Team *
3 * *
4 * Licensed under the Apache License, Version 2.0 (the "License"); *
5 * you may not use this file except in compliance with the License. *
6 * You may obtain a copy of the License at *
7 * *
8 * http://www.apache.org/licenses/LICENSE-2.0 *
9 * *
10 * Unless required by applicable law or agreed to in writing, software *
11 * distributed under the License is distributed on an "AS IS" BASIS, *
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
13 * See the License for the specific language governing permissions and *
14 * limitations under the License. *
15 * *
16 ****************************************************************************/
17
18/****************************************************************************
19* Copyright 2017 Gorgon Meducer (Email:embedded_zhuoran@hotmail.com) *
20* *
21* Licensed under the Apache License, Version 2.0 (the "License"); *
22* you may not use this file except in compliance with the License. *
23* You may obtain a copy of the License at *
24* *
25* http://www.apache.org/licenses/LICENSE-2.0 *
26* *
27* Unless required by applicable law or agreed to in writing, software *
28* distributed under the License is distributed on an "AS IS" BASIS, *
29* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
30* See the License for the specific language governing permissions and *
31* limitations under the License. *
32* *
33****************************************************************************/
34
35#ifndef __SIMPLE_FSM_H__
36#define __SIMPLE_FSM_H__
37
38/*============================ INCLUDES ======================================*/
40
42/*============================ MACROS ========================================*/
43
44#if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L
45# undef VSF_KERNEL_CFG_EDA_SUPPORT_SIMPLE_FSM
46# define VSF_KERNEL_CFG_EDA_SUPPORT_SIMPLE_FSM DISABLED
47#endif
48
49#if VSF_KERNEL_CFG_EDA_SUBCALL_HAS_RETURN_VALUE == ENABLED \
50 && VSF_USE_KERNEL == ENABLED \
51 && VSF_KERNEL_CFG_EDA_SUPPORT_SIMPLE_FSM == ENABLED \
52 && VSF_KERNEL_CFG_EDA_SUPPORT_TASK == ENABLED
53
54#ifdef __cplusplus
55extern "C" {
56#endif
57
58
59#ifndef vsf_this
60# define vsf_this (*vsf_pthis)
61#endif
62
63
64#if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L
65# error simple_fsm require at least ANSI-C99 support
66#endif
67
68/*============================ MACROFIED FUNCTIONS ===========================*/
69#define def_states(...) \
70 enum { \
71 START = 0, \
72 __VA_ARGS__ \
73 };
74
75#ifndef def_params
76# define def_params(...) __VA_ARGS__
77#endif
78
79#define vsf_args(...) ,__VA_ARGS__
80
81#define vsf_fsm(__name) vsf_task(__name)
82
83#define __def_fsm(__fsm_type, ...) \
84 def_vsf_task(__fsm_type, \
85 __VA_ARGS__);
86
87#define def_fsm(__name, ...) \
88 __def_fsm(__name, __VA_ARGS__)
89
90#define def_simple_fsm(__name, ...) \
91 __declare_fsm(__name); \
92 __def_fsm(__name, __VA_ARGS__)
93
94#define end_def_simple_fsm(...)
95
96/*
97#define __extern_simple_fsm(__fsm_type, ...) \
98 declare_class(__fsm_type) \
99 extern_class(__fsm_type) \
100 uint_fast8_t fsm_state; \
101 __VA_ARGS__ \
102 end_extern_class(__fsm_type)
103
104#define extern_simple_fsm(__name, ...) \
105 __extern_simple_fsm(fsm(__name), __VA_ARGS__)
106*/
107
108#define __declare_fsm(__name) \
109 declare_vsf_task(__name)
110
111#define declare_vsf_fsm(__name) __declare_fsm(__name)
112
122#define __extern_fsm_initialiser(__name, ...) \
123 vsf_fsm(__name) *__name##_init(vsf_fsm(__name) *fsm_ptr __VA_ARGS__); \
124 typedef vsf_fsm(__name) *__name##_init_fn(vsf_fsm(__name) *fsm_ptr __VA_ARGS__);
125
126#define extern_vsf_fsm_initialiser(__name, ...) \
127 __extern_fsm_initialiser(__name, __VA_ARGS__)
128
129
139#define __extern_fsm_implementation_ex(__name,__type) \
140 fsm_rt_t __name(fsm(__type) *vsf_pthis, vsf_evt_t evt); \
141 typedef fsm_rt_t __name##_fn( vsf_fsm(__type) *vsf_pthis, vsf_evt_t evt );
142
143#define declare_vsf_fsm_implementation_ex(__name, __type) \
144 __extern_fsm_implementation_ex(__name, __type)
145
146#define extern_vsf_fsm_implementation_ex(__name,__type) \
147 __extern_fsm_implementation_ex(__name, __type)
148
149#define extern_vsf_fsm_implementation(__name) \
150 __extern_fsm_implementation_ex(__name, __name)
151
152#define declare_vsf_fsm_implementation(__name) \
153 __extern_fsm_implementation_ex(__name, __name)
155
156#define call_vsf_fsm(__name, __fsm ) \
157 vsf_task_call_task(__name, __fsm)
158
159#define ____state(__state, ...) \
160 case __state: \
161 __state_entry_##__state:{ \
162 __VA_ARGS__; \
163 };
164
165#define vsf_state(__state, ...) break; ____state(__state, __VA_ARGS__)
166
167#define on_start(...) {__VA_ARGS__;}
168
169
170#define reset_vsf_fsm() do { vsf_this.fsm_state = 0; } while(0);
171#define vsf_fsm_cpl() do {reset_vsf_fsm(); return fsm_rt_cpl;} while(0);
172#define vsf_fsm_report(__ERROR) do {reset_vsf_fsm(); return (fsm_rt_t)(__ERROR); } while(0);
173#define vsf_fsm_wait_for_obj() return fsm_rt_wait_for_obj;
174#define vsf_fsm_on_going() return fsm_rt_on_going;
175
177//#define fsm_continue() break
178
179
180#define update_state_to(__state) \
181 { vsf_this.fsm_state = (__state); goto __state_entry_##__state;}
182
183#define transfer_to(__state) \
184 { vsf_this.fsm_state = (__state); vsf_fsm_on_going() }
185
186
187#define __fsm_initialiser(__name, ...) \
188 vsf_fsm(__name) *__name##_init(vsf_fsm(__name) *vsf_pthis __VA_ARGS__) \
189 { \
190 VSF_KERNEL_ASSERT (NULL != vsf_pthis); \
191 vsf_this.fsm_state = 0;
192
193#define vsf_fsm_initialiser(__name, ...) \
194 __fsm_initialiser(__name, __VA_ARGS__)
195
196
197#define abort_vsf_fsm_init() return NULL;
198
199#define vsf_fsm_init_body(...) \
200 __VA_ARGS__ \
201 return &vsf_this; \
202 }
203
204
205#define init_vsf_fsm(__name, __fsm, ...) \
206 __name##_init(__fsm __VA_ARGS__)
207
208#define init_simple_fsm(__name, __fsm, ...) \
209 init_vsf_fsm(__fsm __VA_ARGS__)
210
211#define start_vsf_fsm(__name, __fsm, __pri, ...) \
212 init_vsf_task(__name, (__fsm), (__pri), __VA_ARGS__)
213
214#define start_simple_fsm(__name, __fsm, __pri, ...) \
215 start_vsf_fsm(__name, (__fsm), (__pri), __VA_ARGS__)
216
217#define __implement_fsm_ex(__name, __type) \
218 implement_vsf_task(__name) \
219 { \
220 vsf_task_begin(); \
221 if (NULL == vsf_pthis) { \
222 return fsm_rt_err; \
223 }
224
225#define __body(...) \
226 switch (vsf_this.fsm_state) { \
227 case 0: \
228 vsf_this.fsm_state++; \
229 __VA_ARGS__ \
230 break; \
231 default: \
232 return fsm_rt_err; \
233 } \
234 \
235 vsf_task_end(); \
236 }
237
238#define vsf_fsm_body(...) __body(__VA_ARGS__)
239
240#define vsf_fsm_begin() \
241 switch (vsf_this.fsm_state) { \
242 case 0: \
243 vsf_this.fsm_state++;
244
245#define vsf_fsm_end() \
246 break; \
247 default: \
248 return fsm_rt_err; \
249 } \
250 }vsf_task_end()
251
252
272#define implement_fsm_ex(__name, __type) \
273 __implement_fsm_ex(__name, __type)
274
275#define __implement_fsm(__name) \
276 implement_fsm_ex(__name, __name)
277
278#define implement_vsf_fsm(__name, ...) __implement_fsm(__name)
279
280#define __privilege_state(__state, ...) \
281 break;do { \
282 do { \
283 ____state(__state, __VA_ARGS__) \
284 } while(0); /* add extra while(0) to catch the fsm_continue()*/ \
285 if (vsf_this.fsm_state != (__state)) { \
286 break; \
287 } \
288 } while(1);
289
290#define privilege_state(__state, ...) \
291 __privilege_state(__state, __VA_ARGS__)
292
293
294#define privilege_group(...) { __VA_ARGS__;}
295
296#define privilege_body(...) \
297 do { \
298 switch (vsf_this.fsm_state) { \
299 case 0: \
300 vsf_this.fsm_state++; \
301 __VA_ARGS__ \
302 } \
303 while(1); \
304 \
305 return fsm_rt_on_going; \
306 }
307
308
309/*============================ TYPES =========================================*/
310
311#ifndef __FSM_RT_TYPE__
312#define __FSM_RT_TYPE__
315typedef enum {
321} fsm_rt_t;
323
324#endif
325
326/*============================ GLOBAL VARIABLES ==============================*/
327/*============================ LOCAL VARIABLES ===============================*/
328/*============================ PROTOTYPES ====================================*/
329
330#ifdef __cplusplus
331}
332#endif
333
334#endif
335#endif
fsm_rt_t
Definition vsf_fsm.h:315
@ fsm_rt_asyn
fsm asynchronose complete, you can check it later.
Definition vsf_fsm.h:320
@ fsm_rt_err
fsm error, error code can be get from other interface
Definition vsf_fsm.h:316
@ fsm_rt_wait_for_obj
fsm wait for object
Definition vsf_fsm.h:319
@ fsm_rt_cpl
fsm complete
Definition vsf_fsm.h:317
@ fsm_rt_on_going
fsm on-going
Definition vsf_fsm.h:318