FsmFramework.h
Go to the documentation of this file.
#ifndef __STATEMACHINE_H_
#define __STATEMACHINE_H_
#include <stdint.h>
#include <stdbool.h>
#ifndef SW_USE_ONENTRY
#define SW_USE_ONENTRY 1
#endif
#ifndef SW_USE_ONDO
#define SW_USE_ONDO 0
#endif
#ifndef SW_USE_ONEXIT
#define SW_USE_ONEXIT 1
#endif
#ifndef SW_USE_TRANSITIONFUNCTION
#define SW_USE_TRANSITIONFUNCTION 1
#endif
#define IS_IN(statemachine, state) (statemachine.s32ActualState == state)
typedef struct stateTableEvents sStateTable_t;
typedef sStateTable_t* psStateTable_t;
typedef enum specialStates
{
FAULT_STATE = -1, // Used in sStateTransitions_t as nextStateId for not allowed events.
FINAL_STATE = -2 // Used in sStateTransitions_t as nextStateId for the final state.
} eSpecialStates_t;
typedef enum specialEvents
{
NO_EVENT = -1, // Can be used, if there is no event, but the state machine should be called.
TIME_EVENT = -2 // Should be used for timer events, especially elapsing timers.
} eSpecialEvents_t;
typedef void (*vCallback_t)(void);
typedef _Bool (*bCallbackGuard_t)(void);
typedef struct stateTransition
{
int32_t s32EventId; // Associated event id for this transition.
bCallbackGuard_t bGuardCb; // Guard function, returns either true or false
vCallback_t vTransitionCb; // Transition function callback.
int32_t s32NextStateId; // ID of next state.
} sStateTransitions_t;
typedef sStateTransitions_t* psStateTransitions_t;
typedef struct stateTableEvents
{
int32_t s32StateId; // ID of this state.
vCallback_t vOnEntry; // onEntry function callback.
vCallback_t vOnDo; // While in the state function callback.
vCallback_t vOnExit; // onExit function callback.
psStateTransitions_t psStateEvents; // Event table for this state.
psStateTable_t psNestedStateTable; // If this state machine has a nested state machine
} sStateTableEvents_t;
typedef sStateTableEvents_t* psStateTableEvents_t;
typedef struct stateMachine
{
uint32_t u32StateMachineId; // ID of the state machine, set by init.
int32_t s32ActualState; // Holds the actual state.
int32_t s32NextState; // Holds the next state after a transition.
uint32_t u32StateCount; // Number of states in this state machine.
uint32_t u32EventCount; // Number of events in this state machine.
_Bool bStateChanged; // Holds if the fsm changed its state after last transition.
psStateTable_t psStateTable; // Table with all states and transitions.
} sStateMachine_t;
typedef sStateMachine_t* psStateMachine_t;
_Bool bFsmframeworkInitialize(psStateMachine_t psStateMachine, psStateTable_t psStateTable, \
int32_t s32StartState, uint32_t u32StateCount, \
uint32_t u32EventCount, vCallback_t vInitCallback);
_Bool bFsmframeworkDispatchEvent(psStateMachine_t psStateMachine, int32_t s32Event);
int32_t s32FsmframeworkGetState(psStateMachine_t psStateMachine);
_Bool bFsmframeworkIsStateChanged(psStateMachine_t psStateMachine);
#endif // __STATEMACHINE_H_
stateTransition::s32EventId
int32_t s32EventId
Definition: FsmFramework.h:112
bFsmframeworkDispatchEvent
_Bool bFsmframeworkDispatchEvent(psStateMachine_t psStateMachine, int32_t s32Event)
Dispatch Event.
Definition: FsmFramework.c:47
eSpecialEvents_t
enum specialEvents eSpecialEvents_t
Special events that can occur while the state machine is running.
specialStates
specialStates
Special states to use with nextStateId in sStateTransitions_t.
Definition: FsmFramework.h:75
NO_EVENT
@ NO_EVENT
Definition: FsmFramework.h:85
stateMachine::u32StateMachineId
uint32_t u32StateMachineId
Definition: FsmFramework.h:146
IS_IN
#define IS_IN(statemachine, state)
MACRO checking if statemachine is in state.
Definition: FsmFramework.h:65
ES2_V4_app.h
global header file for project
stateMachine
All nesseccary data for a state machine.
Definition: FsmFramework.h:145
stateTableEvents::s32StateId
int32_t s32StateId
Definition: FsmFramework.h:129
s32FsmframeworkGetState
int32_t s32FsmframeworkGetState(psStateMachine_t psStateMachine)
Get the actual state of a statemachine.
Definition: FsmFramework.c:134
psStateMachine_t
sStateMachine_t * psStateMachine_t
Definition: FsmFramework.h:158
FINAL_STATE
@ FINAL_STATE
Definition: FsmFramework.h:77
psStateTableEvents_t
sStateTableEvents_t * psStateTableEvents_t
Definition: FsmFramework.h:136
specialEvents
specialEvents
Special events that can occur while the state machine is running.
Definition: FsmFramework.h:84
eSpecialStates_t
enum specialStates eSpecialStates_t
Special states to use with nextStateId in sStateTransitions_t.
FAULT_STATE
@ FAULT_STATE
Definition: FsmFramework.h:76
stateTableEvents::psNestedStateTable
psStateTable_t psNestedStateTable
Definition: FsmFramework.h:134
stateMachine::s32ActualState
int32_t s32ActualState
Definition: FsmFramework.h:148
stateTransition::s32NextStateId
int32_t s32NextStateId
Definition: FsmFramework.h:115
vCallback_t
void(* vCallback_t)(void)
Callback typedef for better readable code.
Definition: FsmFramework.h:92
stateMachine::u32EventCount
uint32_t u32EventCount
Definition: FsmFramework.h:152
stateTableEvents::vOnDo
vCallback_t vOnDo
Definition: FsmFramework.h:131
stateTableEvents::psStateEvents
psStateTransitions_t psStateEvents
Definition: FsmFramework.h:133
stateTableEvents
State table holds states and corresponding event tables.
Definition: FsmFramework.h:128
stateTransition::vTransitionCb
vCallback_t vTransitionCb
Definition: FsmFramework.h:114
stateMachine::u32StateCount
uint32_t u32StateCount
Definition: FsmFramework.h:151
u32StateMachineCount
static uint32_t u32StateMachineCount
Definition: FsmFramework.c:4
stateTransition
Event table holds events that can generate a state transition.
Definition: FsmFramework.h:111
TIME_EVENT
@ TIME_EVENT
Definition: FsmFramework.h:86
stateTableEvents::vOnExit
vCallback_t vOnExit
Definition: FsmFramework.h:132
stateMachine::bStateChanged
_Bool bStateChanged
Definition: FsmFramework.h:154
sStateMachine_t
struct stateMachine sStateMachine_t
All nesseccary data for a state machine.
psStateTable_t
sStateTable_t * psStateTable_t
Definition: FsmFramework.h:69
sStateTableEvents_t
struct stateTableEvents sStateTableEvents_t
State table holds states and corresponding event tables.
bFsmframeworkIsStateChanged
_Bool bFsmframeworkIsStateChanged(psStateMachine_t psStateMachine)
Get the statechanged flag. 1 if last event changed the state, 0 if last event did not changed the sta...
Definition: FsmFramework.c:139
sStateTransitions_t
struct stateTransition sStateTransitions_t
Event table holds events that can generate a state transition.
stateMachine::psStateTable
psStateTable_t psStateTable
Definition: FsmFramework.h:156
bFsmframeworkInitialize
_Bool bFsmframeworkInitialize(psStateMachine_t psStateMachine, psStateTable_t psStateTable, int32_t s32StartState, uint32_t u32StateCount, uint32_t u32EventCount, vCallback_t vInitCallback)
Initialization of the State Machine.
Definition: FsmFramework.c:6
stateTransition::bGuardCb
bCallbackGuard_t bGuardCb
Definition: FsmFramework.h:113
stateTableEvents::vOnEntry
vCallback_t vOnEntry
Definition: FsmFramework.h:130
psStateTransitions_t
sStateTransitions_t * psStateTransitions_t
Definition: FsmFramework.h:117
stateMachine::s32NextState
int32_t s32NextState
Definition: FsmFramework.h:149
bCallbackGuard_t
_Bool(* bCallbackGuard_t)(void)
Callback for guard functions, that return either true or false.
Definition: FsmFramework.h:97