forked from zephyrproject-rtos/zephyr
/
smf.c
325 lines (270 loc) · 7.35 KB
/
smf.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
* Copyright 2021 The Chromium OS Authors
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/smf.h>
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(smf);
/*
* Private structure (to this file) used to track state machine context.
* The structure is not used directly, but instead to cast the "internal"
* member of the smf_ctx structure.
*/
struct internal_ctx {
bool new_state : 1;
bool terminate : 1;
bool exit : 1;
bool handled : 1;
};
static bool share_paren(const struct smf_state *test_state,
const struct smf_state *target_state)
{
for (const struct smf_state *state = test_state;
state != NULL;
state = state->parent) {
if (target_state == state) {
return true;
}
}
return false;
}
static bool last_state_share_paren(struct smf_ctx *const ctx,
const struct smf_state *state)
{
/* Get parent state of previous state */
if (!ctx->previous) {
return false;
}
return share_paren(ctx->previous->parent, state);
}
static const struct smf_state *get_child_of(const struct smf_state *states,
const struct smf_state *parent)
{
for (const struct smf_state *tmp = states; ; tmp = tmp->parent) {
if (tmp->parent == parent) {
return tmp;
}
if (tmp->parent == NULL) {
return NULL;
}
}
return NULL;
}
static const struct smf_state *get_last_of(const struct smf_state *states)
{
return get_child_of(states, NULL);
}
/**
* @brief Execute all ancestor entry actions
*
* @param ctx State machine context
* @param target The entry actions of this target's ancestors are executed
* @return true if the state machine should terminate, else false
*/
__unused static bool smf_execute_ancestor_entry_actions(
struct smf_ctx *const ctx, const struct smf_state *target)
{
struct internal_ctx * const internal = (void *) &ctx->internal;
for (const struct smf_state *to_execute = get_last_of(target);
to_execute != NULL && to_execute != target;
to_execute = get_child_of(target, to_execute)) {
/* Execute parent state's entry */
if (!last_state_share_paren(ctx, to_execute) && to_execute->entry) {
to_execute->entry(ctx);
/* No need to continue if terminate was set */
if (internal->terminate) {
return true;
}
}
}
return false;
}
/**
* @brief Execute all ancestor run actions
*
* @param ctx State machine context
* @param target The run actions of this target's ancestors are executed
* @return true if the state machine should terminate, else false
*/
__unused static bool smf_execute_ancestor_run_actions(struct smf_ctx *ctx)
{
struct internal_ctx * const internal = (void *) &ctx->internal;
/* Execute all run actions in reverse order */
/* Return if the current state switched states */
if (internal->new_state) {
internal->new_state = false;
return false;
}
/* Return if the current state terminated */
if (internal->terminate) {
return true;
}
if (internal->handled) {
/* Event was handled by this state. Stop propagating */
internal->handled = false;
return false;
}
/* Try to run parent run actions */
for (const struct smf_state *tmp_state = ctx->current->parent;
tmp_state != NULL;
tmp_state = tmp_state->parent) {
/* Execute parent run action */
if (tmp_state->run) {
tmp_state->run(ctx);
/* No need to continue if terminate was set */
if (internal->terminate) {
return true;
}
if (internal->new_state) {
break;
}
if (internal->handled) {
/* Event was handled by this state. Stop propagating */
internal->handled = false;
break;
}
}
}
internal->new_state = false;
/* All done executing the run actions */
return false;
}
/**
* @brief Execute all ancestor exit actions
*
* @param ctx State machine context
* @param target The exit actions of this target's ancestors are executed
* @return true if the state machine should terminate, else false
*/
__unused static bool smf_execute_ancestor_exit_actions(
struct smf_ctx *const ctx, const struct smf_state *target)
{
struct internal_ctx * const internal = (void *) &ctx->internal;
/* Execute all parent exit actions in reverse order */
for (const struct smf_state *tmp_state = ctx->current->parent;
tmp_state != NULL;
tmp_state = tmp_state->parent) {
if (!share_paren(target->parent, tmp_state) && tmp_state->exit) {
tmp_state->exit(ctx);
/* No need to continue if terminate was set */
if (internal->terminate) {
return true;
}
}
}
return false;
}
void smf_set_initial(struct smf_ctx *ctx, const struct smf_state *init_state)
{
struct internal_ctx * const internal = (void *) &ctx->internal;
#ifdef CONFIG_SMF_INITIAL_TRANSITION
/*
* The final target will be the deepest leaf state that
* the target contains. Set that as the real target.
*/
while (init_state->initial) {
init_state = init_state->initial;
}
#endif
internal->exit = false;
internal->terminate = false;
ctx->current = init_state;
ctx->previous = NULL;
ctx->terminate_val = 0;
if (IS_ENABLED(CONFIG_SMF_ANCESTOR_SUPPORT)) {
internal->new_state = false;
if (smf_execute_ancestor_entry_actions(ctx, init_state)) {
return;
}
}
/* Now execute the initial state's entry action */
if (init_state->entry) {
init_state->entry(ctx);
}
}
void smf_set_state(struct smf_ctx *const ctx, const struct smf_state *target)
{
struct internal_ctx * const internal = (void *) &ctx->internal;
/*
* It does not make sense to call set_state in an exit phase of a state
* since we are already in a transition; we would always ignore the
* intended state to transition into.
*/
if (internal->exit) {
LOG_WRN("Calling %s from exit action", __func__);
return;
}
internal->exit = true;
/* Execute the current states exit action */
if (ctx->current->exit) {
ctx->current->exit(ctx);
/*
* No need to continue if terminate was set in the
* exit action
*/
if (internal->terminate) {
return;
}
}
if (IS_ENABLED(CONFIG_SMF_ANCESTOR_SUPPORT)) {
internal->new_state = true;
if (smf_execute_ancestor_exit_actions(ctx, target)) {
return;
}
}
internal->exit = false;
#ifdef CONFIG_SMF_INITIAL_TRANSITION
/*
* The final target will be the deepest leaf state that
* the target contains. Set that as the real target.
*/
while (target->initial) {
target = target->initial;
}
#endif
/* update the state variables */
ctx->previous = ctx->current;
ctx->current = target;
if (IS_ENABLED(CONFIG_SMF_ANCESTOR_SUPPORT)) {
if (smf_execute_ancestor_entry_actions(ctx, target)) {
return;
}
}
/* Now execute the target entry action */
if (ctx->current->entry) {
ctx->current->entry(ctx);
/*
* If terminate was set, it will be handled in the
* smf_run_state function
*/
}
}
void smf_set_terminate(struct smf_ctx *ctx, int32_t val)
{
struct internal_ctx * const internal = (void *) &ctx->internal;
internal->terminate = true;
ctx->terminate_val = val;
}
void smf_set_handled(struct smf_ctx *ctx)
{
struct internal_ctx *const internal = (void *)&ctx->internal;
internal->handled = true;
}
int32_t smf_run_state(struct smf_ctx *const ctx)
{
struct internal_ctx * const internal = (void *) &ctx->internal;
/* No need to continue if terminate was set */
if (internal->terminate) {
return ctx->terminate_val;
}
if (ctx->current->run) {
ctx->current->run(ctx);
}
if (IS_ENABLED(CONFIG_SMF_ANCESTOR_SUPPORT)) {
if (smf_execute_ancestor_run_actions(ctx)) {
return ctx->terminate_val;
}
}
return 0;
}