Skip to content

File syn_fsm.c

FileList > src > syntropic > util > syn_fsm.c

Go to the source code of this file

Finite state machine implementation.

  • #include "../log/syn_log.h"
  • #include "syn_assert.h"
  • #include "syn_fsm.h"
  • #include <string.h>

Public Functions

Type Name
bool syn_fsm_dispatch (SYN_FSM * fsm, SYN_FSM_Event event)
Dispatch an event.
void syn_fsm_init (SYN_FSM * fsm, const SYN_FSM_Transition * transitions, SYN_FSM_State initial, const char * tag)
Initialize the FSM.
void syn_fsm_set_context (SYN_FSM * fsm, void * ctx)
Set the user context pointer passed to guards and actions.
void syn_fsm_set_state (SYN_FSM * fsm, SYN_FSM_State state)
Force-set the FSM to a specific state.
void syn_fsm_set_state_descs (SYN_FSM * fsm, const SYN_FSM_StateDesc * descs)
Set optional state descriptors (entry/exit actions).
void syn_fsm_set_state_names (SYN_FSM * fsm, const char *const * names)
Set optional state name strings for debug/logging.

Public Static Functions

Type Name
const SYN_FSM_StateDesc * find_state_desc (const SYN_FSM * fsm, SYN_FSM_State state)
Find the state descriptor for a given state.
void fire_enter (const SYN_FSM * fsm, SYN_FSM_State state)
Invoke the on_enter callback for a state.
void fire_exit (const SYN_FSM * fsm, SYN_FSM_State state)
Invoke the on_exit callback for a state.

Public Functions Documentation

function syn_fsm_dispatch

Dispatch an event.

bool syn_fsm_dispatch (
    SYN_FSM * fsm,
    SYN_FSM_Event event
) 

Scans the transition table for a matching (current_state, event) pair. If a guard exists and returns false, the transition is skipped and the next matching row is tried. On a successful match: * Exit action for the current state (if state_descs set) * Transition action * State change * Entry action for the new state (if state_descs set)

Parameters:

  • fsm FSM instance.
  • event Event to dispatch.

Returns:

true if a transition was taken, false if no match found.


function syn_fsm_init

Initialize the FSM.

void syn_fsm_init (
    SYN_FSM * fsm,
    const SYN_FSM_Transition * transitions,
    SYN_FSM_State initial,
    const char * tag
) 

Parameters:

  • fsm FSM instance.
  • transitions Transition table (terminated by SYN_FSM_END).
  • initial Initial state.
  • tag Log tag for transition logging (e.g., "motor").

function syn_fsm_set_context

Set the user context pointer passed to guards and actions.

void syn_fsm_set_context (
    SYN_FSM * fsm,
    void * ctx
) 

Parameters:

  • fsm FSM instance.
  • ctx User context.

function syn_fsm_set_state

Force-set the FSM to a specific state.

void syn_fsm_set_state (
    SYN_FSM * fsm,
    SYN_FSM_State state
) 

Fires exit action for the old state and entry action for the new state (if state_descs are set). No transition action is called.

Parameters:

  • fsm FSM instance.
  • state New state.

function syn_fsm_set_state_descs

Set optional state descriptors (entry/exit actions).

void syn_fsm_set_state_descs (
    SYN_FSM * fsm,
    const SYN_FSM_StateDesc * descs
) 

Parameters:

  • fsm FSM instance.
  • descs State descriptor table (terminated by SYN_FSM_STATE_END).

function syn_fsm_set_state_names

Set optional state name strings for debug/logging.

void syn_fsm_set_state_names (
    SYN_FSM * fsm,
    const char *const * names
) 

Parameters:

  • fsm FSM instance.
  • names Array of state name strings, indexed by state value. Must cover all state values used in the transition table.

Public Static Functions Documentation

function find_state_desc

Find the state descriptor for a given state.

static const SYN_FSM_StateDesc * find_state_desc (
    const SYN_FSM * fsm,
    SYN_FSM_State state
) 

Parameters:

  • fsm FSM instance.
  • state State to look up.

Returns:

Descriptor, or NULL if not found.


function fire_enter

Invoke the on_enter callback for a state.

static void fire_enter (
    const SYN_FSM * fsm,
    SYN_FSM_State state
) 

Parameters:

  • fsm FSM instance.
  • state State being entered.

function fire_exit

Invoke the on_exit callback for a state.

static void fire_exit (
    const SYN_FSM * fsm,
    SYN_FSM_State state
) 

Parameters:

  • fsm FSM instance.
  • state State being exited.


The documentation for this class was generated from the following file src/syntropic/util/syn_fsm.c