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 internal->new_state = false;
152 internal->handled = false;
153 return false;
154 }
155
156 /* Try to run parent run actions */
157 for (const struct smf_state *tmp_state = ctx->current->parent; tmp_state != NULL;
158 tmp_state = tmp_state->parent) {
159 /* Keep track of where we are in case an ancestor calls smf_set_state() */
160 ctx->executing = tmp_state;
161 /* Execute parent run action */
162 if (tmp_state->run) {
163 tmp_state->run(ctx);
164 /* No need to continue if terminate was set */
165 if (internal->terminate) {
166 return true;
167 }
168
169 /* This state dealt with it. Stop propagating. */
170 if (internal->new_state || internal->handled) {
171 break;
172 }
173 }
174 }
175
176 internal->new_state = false;
177 internal->handled = false;
178
179 /* All done executing the run actions */
180
181 return false;
182 }
183
184 /**
185 * @brief Executes all exit actions from ctx->current to the direct child of topmost
186 *
187 * @param ctx State machine context
188 * @param topmost State we are exiting to. Its exit action is not executed
189 * @return true if the state machine should terminate, else false
190 */
smf_execute_all_exit_actions(struct smf_ctx * const ctx,const struct smf_state * topmost)191 static bool smf_execute_all_exit_actions(struct smf_ctx *const ctx, const struct smf_state *topmost)
192 {
193 struct internal_ctx *const internal = (void *)&ctx->internal;
194
195 for (const struct smf_state *to_execute = ctx->current;
196 to_execute != NULL && to_execute != topmost;
197 to_execute = to_execute->parent) {
198 if (to_execute->exit) {
199 to_execute->exit(ctx);
200
201 /* No need to continue if terminate was set in the exit action */
202 if (internal->terminate) {
203 return true;
204 }
205 }
206 }
207
208 return false;
209 }
210 #endif /* CONFIG_SMF_ANCESTOR_SUPPORT */
211
smf_set_initial(struct smf_ctx * ctx,const struct smf_state * init_state)212 void smf_set_initial(struct smf_ctx *ctx, const struct smf_state *init_state)
213 {
214 struct internal_ctx *const internal = (void *)&ctx->internal;
215
216 #ifdef CONFIG_SMF_INITIAL_TRANSITION
217 /*
218 * The final target will be the deepest leaf state that
219 * the target contains. Set that as the real target.
220 */
221 while (init_state->initial) {
222 init_state = init_state->initial;
223 }
224 #endif
225
226 internal->is_exit = false;
227 internal->terminate = false;
228 internal->handled = false;
229 internal->new_state = false;
230 ctx->current = init_state;
231 ctx->previous = NULL;
232 ctx->terminate_val = 0;
233
234 #ifdef CONFIG_SMF_ANCESTOR_SUPPORT
235 ctx->executing = init_state;
236 const struct smf_state *topmost = get_last_of(init_state);
237
238 /* Execute topmost state entry action, since smf_execute_all_entry_actions()
239 * doesn't
240 */
241 if (topmost->entry) {
242 topmost->entry(ctx);
243 if (internal->terminate) {
244 /* No need to continue if terminate was set */
245 return;
246 }
247 }
248
249 if (smf_execute_all_entry_actions(ctx, init_state, topmost)) {
250 /* No need to continue if terminate was set */
251 return;
252 }
253 #else
254 /* execute entry action if it exists */
255 if (init_state->entry) {
256 init_state->entry(ctx);
257 }
258 #endif
259 }
260
smf_set_state(struct smf_ctx * const ctx,const struct smf_state * new_state)261 void smf_set_state(struct smf_ctx *const ctx, const struct smf_state *new_state)
262 {
263 struct internal_ctx *const internal = (void *)&ctx->internal;
264
265 if (new_state == NULL) {
266 LOG_ERR("new_state cannot be NULL");
267 return;
268 }
269
270 /*
271 * It does not make sense to call smf_set_state in an exit phase of a state
272 * since we are already in a transition; we would always ignore the
273 * intended state to transition into.
274 */
275 if (internal->is_exit) {
276 LOG_ERR("Calling %s from exit action", __func__);
277 return;
278 }
279
280 #ifdef CONFIG_SMF_ANCESTOR_SUPPORT
281 const struct smf_state *topmost;
282
283 if (share_paren(ctx->executing, new_state)) {
284 /* new state is a parent of where we are now*/
285 topmost = new_state;
286 } else if (share_paren(new_state, ctx->executing)) {
287 /* we are a parent of the new state */
288 topmost = ctx->executing;
289 } else {
290 /* not directly related, find LCA */
291 topmost = get_lca_of(ctx->executing, new_state);
292 }
293
294 internal->is_exit = true;
295 internal->new_state = true;
296
297 /* call all exit actions up to (but not including) the topmost */
298 if (smf_execute_all_exit_actions(ctx, topmost)) {
299 /* No need to continue if terminate was set in the exit action */
300 return;
301 }
302
303 /* if self-transition, call the exit action */
304 if ((ctx->executing == new_state) && (new_state->exit)) {
305 new_state->exit(ctx);
306
307 /* No need to continue if terminate was set in the exit action */
308 if (internal->terminate) {
309 return;
310 }
311 }
312
313 internal->is_exit = false;
314
315 /* if self transition, call the entry action */
316 if ((ctx->executing == new_state) && (new_state->entry)) {
317 new_state->entry(ctx);
318
319 /* No need to continue if terminate was set in the entry action */
320 if (internal->terminate) {
321 return;
322 }
323 }
324 #ifdef CONFIG_SMF_INITIAL_TRANSITION
325 /*
326 * The final target will be the deepest leaf state that
327 * the target contains. Set that as the real target.
328 */
329 while (new_state->initial) {
330 new_state = new_state->initial;
331 }
332 #endif
333
334 /* update the state variables */
335 ctx->previous = ctx->current;
336 ctx->current = new_state;
337
338 /* call all entry actions (except those of topmost) */
339 if (smf_execute_all_entry_actions(ctx, new_state, topmost)) {
340 /* No need to continue if terminate was set in the entry action */
341 return;
342 }
343 #else
344 /* Flat state machines have a very simple transition: */
345 if (ctx->current->exit) {
346 internal->is_exit = true;
347 ctx->current->exit(ctx);
348 /* No need to continue if terminate was set in the exit action */
349 if (internal->terminate) {
350 return;
351 }
352 internal->is_exit = false;
353 }
354 /* update the state variables */
355 ctx->previous = ctx->current;
356 ctx->current = new_state;
357
358 if (ctx->current->entry) {
359 ctx->current->entry(ctx);
360 /* No need to continue if terminate was set in the entry action */
361 if (internal->terminate) {
362 return;
363 }
364 }
365 #endif
366 }
367
smf_set_terminate(struct smf_ctx * ctx,int32_t val)368 void smf_set_terminate(struct smf_ctx *ctx, int32_t val)
369 {
370 struct internal_ctx *const internal = (void *)&ctx->internal;
371
372 internal->terminate = true;
373 ctx->terminate_val = val;
374 }
375
smf_set_handled(struct smf_ctx * ctx)376 void smf_set_handled(struct smf_ctx *ctx)
377 {
378 struct internal_ctx *const internal = (void *)&ctx->internal;
379
380 internal->handled = true;
381 }
382
smf_run_state(struct smf_ctx * const ctx)383 int32_t smf_run_state(struct smf_ctx *const ctx)
384 {
385 struct internal_ctx *const internal = (void *)&ctx->internal;
386
387 /* No need to continue if terminate was set */
388 if (internal->terminate) {
389 return ctx->terminate_val;
390 }
391
392 #ifdef CONFIG_SMF_ANCESTOR_SUPPORT
393 ctx->executing = ctx->current;
394 #endif
395
396 if (ctx->current->run) {
397 ctx->current->run(ctx);
398 }
399
400 #ifdef CONFIG_SMF_ANCESTOR_SUPPORT
401 if (smf_execute_ancestor_run_actions(ctx)) {
402 return ctx->terminate_val;
403 }
404 #endif
405 return 0;
406 }
407