Lines Matching full:state

3 State Machine Framework
11 The State Machine Framework (SMF) is an application agnostic framework that
12 provides an easy way for developers to integrate state machines into their
16 State Creation
19 A state is represented by three functions, where one function implements the
23 defined structure that has the state machine context, :c:struct:`smf_ctx`, as
31 The :c:struct:`smf_ctx` member must be first because the state machine
38 By default, a state can have no ancestor states, resulting in a flat state
39 machine. But to enable the creation of a hierarchical state machine, the
42 By default, the hierarchical state machines do not support initial transitions
46 The following macro can be used for easy state creation:
48 * :c:macro:`SMF_CREATE_STATE` Create a state
50 State Machine Creation
53 A state machine is created by defining a table of states that's indexed by an
76 from parent state S0 to child state S2::
80 /* Forward declaration of state table */
89 To set the initial state, the :c:func:`smf_set_initial` function should be
92 To transition from one state to another, the :c:func:`smf_set_state`
97 not be passed a parent state as the parent state does not know which
98 child state to transition to. Transitioning to a parent state is OK
99 if an initial transition to a child state is defined. A well-formed
102 .. note:: While the state machine is running, :c:func:`smf_set_state` should
107 State Machine Execution
110 To run the state machine, the :c:func:`smf_run_state` function should be
118 states. It is not required to call :c:func:`smf_set_handled` if the state
121 State Machine Termination
124 To terminate the state machine, the :c:func:`smf_set_terminate` function
129 UML State Machines
132 SMF follows UML hierarchical state machine rules for transitions i.e., the
142 of the source state, rather than after the exit actions are performed.
144 from a superstate to a child state is treated as a local transition.
149 from the entry action of a 'terminate' state. Orthogonal regions are modelled
152 State Machine Examples
155 Flat State Machine Example
158 This example turns the following state diagram into code using the SMF, where
159 the initial state is S0.
162 :caption: Flat state machine diagram
181 /* Forward declaration of state table */
192 /* Other state specific data add here */
195 /* State S0 */
209 /* State S1 */
219 /* State S2 */
229 /* Populate state table */
232 /* State S1 does not have an entry action */
234 /* State S2 does not have an exit action */
242 /* Set initial state */
245 /* Run the state machine */
247 /* State machine terminates if a non-zero value is returned */
250 /* handle return code and terminate state machine */
257 Hierarchical State Machine Example
260 This example turns the following state diagram into code using the SMF, where
261 S0 and S1 share a parent state and S0 is the initial state.
265 :caption: Hierarchical state machine diagram
289 /* Forward declaration of state table */
300 /* Other state specific data add here */
303 /* Parent State */
313 /* State S0 */
319 /* State S1 */
325 /* State S2 */
331 /* Populate state table */
333 /* Parent state does not have a run action */
338 /* State S2 do not have entry or exit actions and no parent */
346 /* Set initial state */
349 /* Run the state machine */
351 /* State machine terminates if a non-zero value is returned */
354 /* handle return code and terminate state machine */
361 When designing hierarchical state machines, the following should be considered:
369 state. For example, the s1_exit function is called before the parent_exit
374 Event Driven State Machine Example
377 Events are not explicitly part of the State Machine Framework but an event driven
378 state machine can be implemented using Zephyr :ref:`events`.
381 :caption: Event driven state machine diagram
410 /* Forward declaration of state table */
425 /* Other state specific data add here */
428 /* State S0 */
444 /* State S1 */
460 /* Populate state table */
504 /* Set initial state */
507 /* Run the state machine */
513 /* State machine terminates if a non-zero value is returned */
516 /* handle return code and terminate state machine */
522 State Machine Example With Initial Transitions And Transition To Self
525 :zephyr_file:`tests/lib/smf/src/test_lib_self_transition_smf.c` defines a state
527 state. The statechart for this test is below.
531 :caption: Test state machine for UML State Transitions