Zephyr Project API 4.3.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
smf.h
Go to the documentation of this file.
1/*
2 * Copyright 2021 The Chromium OS Authors
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
12
13#ifndef ZEPHYR_INCLUDE_SMF_H_
14#define ZEPHYR_INCLUDE_SMF_H_
15
16#include <zephyr/sys/util.h>
17
25
35/* clang-format off */
36#define SMF_CREATE_STATE(_entry, _run, _exit, _parent, _initial) \
37{ \
38 .entry = _entry, \
39 .run = _run, \
40 .exit = _exit, \
41 IF_ENABLED(CONFIG_SMF_ANCESTOR_SUPPORT, (.parent = _parent,)) \
42 IF_ENABLED(CONFIG_SMF_INITIAL_TRANSITION, (.initial = _initial,)) \
43}
44/* clang-format on */
45
52#define SMF_CTX(o) ((struct smf_ctx *)o)
53
54#ifdef __cplusplus
55extern "C" {
56#endif
57
58#include <zephyr/kernel.h>
59
67
77
79#define SMF_ERR_NULL_TRANSITION 1
80#define SMF_ERR_TRANSITION_IN_EXIT 2
81
82#if defined(CONFIG_SMF_INSTRUMENTATION) || defined(__DOXYGEN__)
83/* Forward declarations for hook typedefs */
84struct smf_ctx;
85struct smf_state;
86
95typedef void (*smf_transition_hook)(struct smf_ctx *ctx, const struct smf_state *source,
96 const struct smf_state *dest);
97
105typedef void (*smf_action_hook)(struct smf_ctx *ctx, const struct smf_state *state,
106 enum smf_action_type action_type);
107
114typedef void (*smf_error_hook)(struct smf_ctx *ctx, int error_code);
115
126#endif /* CONFIG_SMF_INSTRUMENTATION */
127
134typedef void (*state_method)(void *obj);
135
143typedef enum smf_state_result (*state_execution)(void *obj);
144
146struct smf_state {
149
155
158#ifdef CONFIG_SMF_ANCESTOR_SUPPORT
169 const struct smf_state *parent;
170
171#ifdef CONFIG_SMF_INITIAL_TRANSITION
175 const struct smf_state *initial;
176#endif /* CONFIG_SMF_INITIAL_TRANSITION */
177#endif /* CONFIG_SMF_ANCESTOR_SUPPORT */
178};
179
181struct smf_ctx {
183 const struct smf_state *current;
185 const struct smf_state *previous;
186
187#ifdef CONFIG_SMF_ANCESTOR_SUPPORT
189 const struct smf_state *executing;
190#endif /* CONFIG_SMF_ANCESTOR_SUPPORT */
203
204#ifdef CONFIG_SMF_INSTRUMENTATION
206 const struct smf_hooks *hooks;
207#endif /* CONFIG_SMF_INSTRUMENTATION */
208};
209
216void smf_set_initial(struct smf_ctx *ctx, const struct smf_state *init_state);
217
226void smf_set_state(struct smf_ctx *ctx, const struct smf_state *new_state);
227
235void smf_set_terminate(struct smf_ctx *ctx, int32_t val);
236
237#ifdef CONFIG_SMF_INSTRUMENTATION
250void smf_set_hooks(struct smf_ctx *ctx, const struct smf_hooks *hooks);
251#endif /* CONFIG_SMF_INSTRUMENTATION */
252
263
273static inline const struct smf_state *smf_get_current_leaf_state(const struct smf_ctx *const ctx)
274{
275 return ctx->current;
276}
277
284static inline const struct smf_state *
286{
287#ifdef CONFIG_SMF_ANCESTOR_SUPPORT
288 return ctx->executing;
289#else
290 return ctx->current;
291#endif /* CONFIG_SMF_ANCESTOR_SUPPORT */
292}
293
294#ifdef __cplusplus
295}
296#endif
297
301
302#endif /* ZEPHYR_INCLUDE_SMF_H_ */
smf_state_result
enum for the return value of a state_execution function
Definition smf.h:63
enum smf_state_result(* state_execution)(void *obj)
Function pointer that implements a the run action of a state.
Definition smf.h:143
void smf_set_state(struct smf_ctx *ctx, const struct smf_state *new_state)
Changes a state machines state.
void smf_set_initial(struct smf_ctx *ctx, const struct smf_state *init_state)
Initializes the state machine and sets its initial state.
int32_t smf_run_state(struct smf_ctx *ctx)
Runs one iteration of a state machine (including any parent states)
void(* smf_error_hook)(struct smf_ctx *ctx, int error_code)
Called when an invalid operation is detected.
Definition smf.h:114
void smf_set_terminate(struct smf_ctx *ctx, int32_t val)
Terminate a state machine.
void(* smf_action_hook)(struct smf_ctx *ctx, const struct smf_state *state, enum smf_action_type action_type)
Called before a state action (entry/run/exit) is invoked.
Definition smf.h:105
static const struct smf_state * smf_get_current_leaf_state(const struct smf_ctx *const ctx)
Get the current leaf state.
Definition smf.h:273
void(* state_method)(void *obj)
Function pointer that implements a entry and exit actions of a state.
Definition smf.h:134
smf_action_type
Enum identifying which action type is being executed.
Definition smf.h:72
static const struct smf_state * smf_get_current_executing_state(const struct smf_ctx *const ctx)
Get the state that is currently executing.
Definition smf.h:285
void(* smf_transition_hook)(struct smf_ctx *ctx, const struct smf_state *source, const struct smf_state *dest)
Called after the current state pointer is updated, before entry actions of the new state execute.
Definition smf.h:95
@ SMF_EVENT_HANDLED
Definition smf.h:64
@ SMF_EVENT_PROPAGATE
Definition smf.h:65
@ SMF_ACTION_RUN
Run action.
Definition smf.h:74
@ SMF_ACTION_EXIT
Exit action.
Definition smf.h:75
@ SMF_ACTION_ENTRY
Entry action.
Definition smf.h:73
Public kernel APIs.
state
Definition parser_state.h:29
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
__INT32_TYPE__ int32_t
Definition stdint.h:74
Defines the current context of the state machine.
Definition smf.h:181
int32_t terminate_val
This value is set by the set_terminate function and should terminate the state machine when its set t...
Definition smf.h:197
const struct smf_state * previous
Previous state the state machine executed.
Definition smf.h:185
const struct smf_state * current
Current state the state machine is executing.
Definition smf.h:183
uint32_t internal
The state machine casts this to a "struct internal_ctx" and it's used to track state machine context.
Definition smf.h:202
Collection of optional instrumentation hooks.
Definition smf.h:121
smf_error_hook on_error
Hook called on error.
Definition smf.h:124
smf_transition_hook on_transition
Hook called on transition.
Definition smf.h:122
smf_action_hook on_action
Hook called on entry/run/exit actions.
Definition smf.h:123
General state that can be used in multiple state machines.
Definition smf.h:146
const state_method exit
Optional method that will be run when this state exists.
Definition smf.h:157
const state_method entry
Optional method that will be run when this state is entered.
Definition smf.h:148
const state_execution run
Optional method that will be run repeatedly during state machine loop.
Definition smf.h:154
Misc utilities.