Zephyr API Documentation  3.5.0
A Scalable Open Source RTOS
3.5.0
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
7/* State Machine Framework */
8
9#ifndef ZEPHYR_INCLUDE_SMF_H_
10#define ZEPHYR_INCLUDE_SMF_H_
11
12#ifdef CONFIG_SMF_ANCESTOR_SUPPORT
21#define SMF_CREATE_STATE(_entry, _run, _exit, _parent) \
22{ \
23 .entry = _entry, \
24 .run = _run, \
25 .exit = _exit, \
26 .parent = _parent \
27}
28
29#else
30
38#define SMF_CREATE_STATE(_entry, _run, _exit) \
39{ \
40 .entry = _entry, \
41 .run = _run, \
42 .exit = _exit \
43}
44
45#endif /* CONFIG_SMF_ANCESTOR_SUPPORT */
46
53#define SMF_CTX(o) ((struct smf_ctx *)o)
54
55#ifdef __cplusplus
56extern "C" {
57#endif
58
59#include <zephyr/kernel.h>
60
66typedef void (*state_execution)(void *obj);
67
69struct smf_state {
89 const struct smf_state *parent;
90};
91
93struct smf_ctx {
95 const struct smf_state *current;
97 const struct smf_state *previous;
110};
111
118void smf_set_initial(struct smf_ctx *ctx, const struct smf_state *init_state);
119
128void smf_set_state(struct smf_ctx *ctx, const struct smf_state *new_state);
129
137void smf_set_terminate(struct smf_ctx *ctx, int32_t val);
138
149
150#ifdef __cplusplus
151}
152#endif
153
154#endif /* ZEPHYR_INCLUDE_SMF_H_ */
Public kernel APIs.
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_execution)(void *obj)
Function pointer that implements a portion of a state.
Definition: smf.h:66
__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:93
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:104
const struct smf_state * previous
Previous state the state machine executed.
Definition: smf.h:97
const struct smf_state * current
Current state the state machine is executing.
Definition: smf.h:95
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:109
General state that can be used in multiple state machines.
Definition: smf.h:69
const state_execution exit
Optional method that will be run when this state exists.
Definition: smf.h:78
const state_execution entry
Optional method that will be run when this state is entered.
Definition: smf.h:71
const state_execution run
Optional method that will be run repeatedly during state machine loop.
Definition: smf.h:76
const struct smf_state * parent
Optional parent state that contains common entry/run/exit implementation among various child states.
Definition: smf.h:89