Zephyr API Documentation 4.1.99
A Scalable Open Source RTOS
 4.1.99
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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
13#ifndef ZEPHYR_INCLUDE_SMF_H_
14#define ZEPHYR_INCLUDE_SMF_H_
15
16#include <zephyr/sys/util.h>
17
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
74typedef void (*state_method)(void *obj);
75
83typedef enum smf_state_result (*state_execution)(void *obj);
84
86struct smf_state {
89
95
98#ifdef CONFIG_SMF_ANCESTOR_SUPPORT
109 const struct smf_state *parent;
110
111#ifdef CONFIG_SMF_INITIAL_TRANSITION
115 const struct smf_state *initial;
116#endif /* CONFIG_SMF_INITIAL_TRANSITION */
117#endif /* CONFIG_SMF_ANCESTOR_SUPPORT */
118};
119
121struct smf_ctx {
123 const struct smf_state *current;
125 const struct smf_state *previous;
126
127#ifdef CONFIG_SMF_ANCESTOR_SUPPORT
129 const struct smf_state *executing;
130#endif /* CONFIG_SMF_ANCESTOR_SUPPORT */
143};
144
151void smf_set_initial(struct smf_ctx *ctx, const struct smf_state *init_state);
152
161void smf_set_state(struct smf_ctx *ctx, const struct smf_state *new_state);
162
170void smf_set_terminate(struct smf_ctx *ctx, int32_t val);
171
182
183#ifdef __cplusplus
184}
185#endif
186
191#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:83
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_set_terminate(struct smf_ctx *ctx, int32_t val)
Terminate a state machine.
void(* state_method)(void *obj)
Function pointer that implements a entry and exit actions of a state.
Definition smf.h:74
@ SMF_EVENT_HANDLED
Definition smf.h:64
@ SMF_EVENT_PROPAGATE
Definition smf.h:65
Public kernel APIs.
__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:121
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:137
const struct smf_state * previous
Previous state the state machine executed.
Definition smf.h:125
const struct smf_state * current
Current state the state machine is executing.
Definition smf.h:123
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:142
General state that can be used in multiple state machines.
Definition smf.h:86
const state_method exit
Optional method that will be run when this state exists.
Definition smf.h:97
const state_method entry
Optional method that will be run when this state is entered.
Definition smf.h:88
const state_execution run
Optional method that will be run repeatedly during state machine loop.
Definition smf.h:94
Misc utilities.