1 /*
2 * Copyright 2021 The Chromium OS Authors
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/smf.h>
8
9 #include <zephyr/logging/log.h>
10 LOG_MODULE_REGISTER(smf);
11
12 /**
13 * @brief Private structure (to this file) used to track state machine context.
14 * The structure is not used directly, but instead to cast the "internal"
15 * member of the smf_ctx structure.
16 */
17 struct internal_ctx {
18 bool new_state: 1;
19 bool terminate: 1;
20 bool is_exit: 1;
21 bool handled: 1;
22 };
23
24 #ifdef CONFIG_SMF_ANCESTOR_SUPPORT
share_paren(const struct smf_state * test_state,const struct smf_state * target_state)25 static bool share_paren(const struct smf_state *test_state, const struct smf_state *target_state)
26 {
27 for (const struct smf_state *state = test_state; state != NULL; state = state->parent) {
28 if (target_state == state) {
29 return true;
30 }
31 }
32
33 return false;
34 }
35
get_child_of(const struct smf_state * states,const struct smf_state * parent)36 static const struct smf_state *get_child_of(const struct smf_state *states,
37 const struct smf_state *parent)
38 {
39 const struct smf_state *tmp = states;
40
41 while (true) {
42 if (tmp->parent == parent) {
43 return tmp;
44 }
45
46 if (tmp->parent == NULL) {
47 return NULL;
48 }
49
50 tmp = tmp->parent;
51 }
52 }
53
get_last_of(const struct smf_state * states)54 static const struct smf_state *get_last_of(const struct smf_state *states)
55 {
56 return get_child_of(states, NULL);
57 }
58
59 /**
60 * @brief Find the Least Common Ancestor (LCA) of two states
61 *
62 * @param source transition source
63 * @param dest transition destination
64 * @return LCA state, or NULL if states have no LCA.
65 */
get_lca_of(const struct smf_state * source,const struct smf_state * dest)66 static const struct smf_state *get_lca_of(const struct smf_state *source,
67 const struct smf_state *dest)
68 {
69 for (const struct smf_state *ancestor = source->parent; ancestor != NULL;
70 ancestor = ancestor->parent) {
71 if (ancestor == dest) {
72 return ancestor->parent;
73 } else if (share_paren(dest, ancestor)) {
74 return ancestor;
75 }
76 }
77
78 return NULL;
79 }
80
81 /**
82 * @brief Executes all entry actions from the direct child of topmost to the new state
83 *
84 * @param ctx State machine context
85 * @param new_state State we are transitioning to
86 * @param topmost State we are entering from. Its entry action is not executed
87 * @return true if the state machine should terminate, else false
88 */
smf_execute_all_entry_actions(struct smf_ctx * const ctx,const struct smf_state * new_state,const struct smf_state * topmost)89 static bool smf_execute_all_entry_actions(struct smf_ctx *const ctx,
90 const struct smf_state *new_state,
91 const struct smf_state *topmost)
92 {
93 struct internal_ctx *const internal = (void *)&ctx->internal;
94
95 if (new_state == topmost) {
96 /* There are no child states, so do nothing */
97 return false;
98 }
99
100 for (const struct smf_state *to_execute = get_child_of(new_state, topmost);
101 to_execute != NULL && to_execute != new_state;
102 to_execute = get_child_of(new_state, to_execute)) {
103 /* Keep track of the executing entry action in case it calls
104 * smf_set_state()
105 */
106 ctx->executing = to_execute;
107 /* Execute every entry action EXCEPT that of the topmost state */
108 if (to_execute->entry) {
109 to_execute->entry(ctx);
110
111 /* No need to continue if terminate was set */
112 if (internal->terminate) {
113 return true;
114 }
115 }
116 }
117
118 /* and execute the new state entry action */
119 ctx->executing = new_state;
120 if (new_state->entry) {
121 new_state->entry(ctx);
122
123 /* No need to continue if terminate was set */
124 if (internal->terminate) {
125 return true;
126 }
127 }
128
129 return false;
130 }
131
132 /**
133 * @brief Execute all ancestor run actions
134 *
135 * @param ctx State machine context
136 * @param target The run actions of this target's ancestors are executed
137 * @return true if the state machine should terminate, else false
138 */
smf_execute_ancestor_run_actions(struct smf_ctx * ctx)139 static bool smf_execute_ancestor_run_actions(struct smf_ctx *ctx)
140 {
141 struct internal_ctx *const internal = (void *)&ctx->internal;
142 /* Execute all run actions in reverse order */
143
144 /* Return if the current state terminated */
145 if (internal->terminate) {
146 return true;
147 }
148
149 /* The child state either transitioned or handled it. Either way, stop propagating. */
150 if (internal->new_state || internal->handled) {
151 return false;
152 }
153
154 /* Try to run parent run actions */
155 for (const struct smf_state *tmp_state = ctx->current->parent; tmp_state != NULL;
156 tmp_state = tmp_state->parent) {
157 /* Keep track of where we are in case an ancestor calls smf_set_state() */
158 ctx->executing = tmp_state;
159 /* Execute parent run action */
160 if (tmp_state->run) {
161 tmp_state->run(ctx);
162 /* No need to continue if terminate was set */
163 if (internal->terminate) {
164 return true;
165 }
166
167 /* This state dealt with it. Stop propagating. */
168 if (internal->new_state || internal->handled) {
169 break;
170 }
171 }
172 }
173
174 /* All done executing the run actions */
175
176 return false;
177 }
178
179 /**
180 * @brief Executes all exit actions from ctx->current to the direct child of topmost
181 *
182 * @param ctx State machine context
183 * @param topmost State we are exiting to. Its exit action is not executed
184 * @return true if the state machine should terminate, else false
185 */
smf_execute_all_exit_actions(struct smf_ctx * const ctx,const struct smf_state * topmost)186 static bool smf_execute_all_exit_actions(struct smf_ctx *const ctx, const struct smf_state *topmost)
187 {
188 struct internal_ctx *const internal = (void *)&ctx->internal;
189
190 for (const struct smf_state *to_execute = ctx->current;
191 to_execute != NULL && to_execute != topmost;
192 to_execute = to_execute->parent) {
193 if (to_execute->exit) {
194 to_execute->exit(ctx);
195
196 /* No need to continue if terminate was set in the exit action */
197 if (internal->terminate) {
198 return true;
199 }
200 }
201 }
202
203 return false;
204 }
205 #endif /* CONFIG_SMF_ANCESTOR_SUPPORT */
206
207 /**
208 * @brief Reset the internal state of the state machine back to default values.
209 * Should be called on entry to smf_set_initial() and smf_set_state().
210 *
211 * @param ctx State machine context.
212 */
smf_clear_internal_state(struct smf_ctx * ctx)213 static void smf_clear_internal_state(struct smf_ctx *ctx)
214 {
215 struct internal_ctx *const internal = (void *)&ctx->internal;
216
217 internal->is_exit = false;
218 internal->terminate = false;
219 internal->handled = false;
220 internal->new_state = false;
221 }
222
smf_set_initial(struct smf_ctx * ctx,const struct smf_state * init_state)223 void smf_set_initial(struct smf_ctx *ctx, const struct smf_state *init_state)
224 {
225 #ifdef CONFIG_SMF_INITIAL_TRANSITION
226 /*
227 * The final target will be the deepest leaf state that
228 * the target contains. Set that as the real target.
229 */
230 while (init_state->initial) {
231 init_state = init_state->initial;
232 }
233 #endif
234
235 smf_clear_internal_state(ctx);
236 ctx->current = init_state;
237 ctx->previous = NULL;
238 ctx->terminate_val = 0;
239
240 #ifdef CONFIG_SMF_ANCESTOR_SUPPORT
241 struct internal_ctx *const internal = (void *)&ctx->internal;
242
243 ctx->executing = init_state;
244 const struct smf_state *topmost = get_last_of(init_state);
245
246 /* Execute topmost state entry action, since smf_execute_all_entry_actions()
247 * doesn't
248 */
249 if (topmost->entry) {
250 topmost->entry(ctx);
251 if (internal->terminate) {
252 /* No need to continue if terminate was set */
253 return;
254 }
255 }
256
257 if (smf_execute_all_entry_actions(ctx, init_state, topmost)) {
258 /* No need to continue if terminate was set */
259 return;
260 }
261 #else
262 /* execute entry action if it exists */
263 if (init_state->entry) {
264 init_state->entry(ctx);
265 }
266 #endif
267 }
268
smf_set_state(struct smf_ctx * const ctx,const struct smf_state * new_state)269 void smf_set_state(struct smf_ctx *const ctx, const struct smf_state *new_state)
270 {
271 struct internal_ctx *const internal = (void *)&ctx->internal;
272
273 if (new_state == NULL) {
274 LOG_ERR("new_state cannot be NULL");
275 return;
276 }
277
278 /*
279 * It does not make sense to call smf_set_state in an exit phase of a state
280 * since we are already in a transition; we would always ignore the
281 * intended state to transition into.
282 */
283 if (internal->is_exit) {
284 LOG_ERR("Calling %s from exit action", __func__);
285 return;
286 }
287
288 #ifdef CONFIG_SMF_ANCESTOR_SUPPORT
289 const struct smf_state *topmost;
290
291 if (share_paren(ctx->executing, new_state)) {
292 /* new state is a parent of where we are now*/
293 topmost = new_state;
294 } else if (share_paren(new_state, ctx->executing)) {
295 /* we are a parent of the new state */
296 topmost = ctx->executing;
297 } else {
298 /* not directly related, find LCA */
299 topmost = get_lca_of(ctx->executing, new_state);
300 }
301
302 internal->is_exit = true;
303 internal->new_state = true;
304
305 /* call all exit actions up to (but not including) the topmost */
306 if (smf_execute_all_exit_actions(ctx, topmost)) {
307 /* No need to continue if terminate was set in the exit action */
308 return;
309 }
310
311 /* if self-transition, call the exit action */
312 if ((ctx->executing == new_state) && (new_state->exit)) {
313 new_state->exit(ctx);
314
315 /* No need to continue if terminate was set in the exit action */
316 if (internal->terminate) {
317 return;
318 }
319 }
320
321 internal->is_exit = false;
322
323 /* if self transition, call the entry action */
324 if ((ctx->executing == new_state) && (new_state->entry)) {
325 new_state->entry(ctx);
326
327 /* No need to continue if terminate was set in the entry action */
328 if (internal->terminate) {
329 return;
330 }
331 }
332 #ifdef CONFIG_SMF_INITIAL_TRANSITION
333 /*
334 * The final target will be the deepest leaf state that
335 * the target contains. Set that as the real target.
336 */
337 while (new_state->initial) {
338 new_state = new_state->initial;
339 }
340 #endif
341
342 /* update the state variables */
343 ctx->previous = ctx->current;
344 ctx->current = new_state;
345
346 /* call all entry actions (except those of topmost) */
347 if (smf_execute_all_entry_actions(ctx, new_state, topmost)) {
348 /* No need to continue if terminate was set in the entry action */
349 return;
350 }
351 #else
352 /* Flat state machines have a very simple transition: */
353 if (ctx->current->exit) {
354 internal->is_exit = true;
355 ctx->current->exit(ctx);
356 /* No need to continue if terminate was set in the exit action */
357 if (internal->terminate) {
358 return;
359 }
360 internal->is_exit = false;
361 }
362 /* update the state variables */
363 ctx->previous = ctx->current;
364 ctx->current = new_state;
365
366 if (ctx->current->entry) {
367 ctx->current->entry(ctx);
368 /* No need to continue if terminate was set in the entry action */
369 if (internal->terminate) {
370 return;
371 }
372 }
373 #endif
374 }
375
smf_set_terminate(struct smf_ctx * ctx,int32_t val)376 void smf_set_terminate(struct smf_ctx *ctx, int32_t val)
377 {
378 struct internal_ctx *const internal = (void *)&ctx->internal;
379
380 internal->terminate = true;
381 ctx->terminate_val = val;
382 }
383
smf_set_handled(struct smf_ctx * ctx)384 void smf_set_handled(struct smf_ctx *ctx)
385 {
386 struct internal_ctx *const internal = (void *)&ctx->internal;
387
388 internal->handled = true;
389 }
390
smf_run_state(struct smf_ctx * const ctx)391 int32_t smf_run_state(struct smf_ctx *const ctx)
392 {
393 struct internal_ctx *const internal = (void *)&ctx->internal;
394
395 /* No need to continue if terminate was set */
396 if (internal->terminate) {
397 return ctx->terminate_val;
398 }
399
400 /* Executing a states run function could cause a transition, so clear the
401 * internal state to ensure that the transition is handled correctly.
402 */
403 smf_clear_internal_state(ctx);
404
405 #ifdef CONFIG_SMF_ANCESTOR_SUPPORT
406 ctx->executing = ctx->current;
407 #endif
408
409 if (ctx->current->run) {
410 ctx->current->run(ctx);
411 }
412
413 #ifdef CONFIG_SMF_ANCESTOR_SUPPORT
414 if (smf_execute_ancestor_run_actions(ctx)) {
415 return ctx->terminate_val;
416 }
417 #endif
418 return 0;
419 }
420