4#include "include/types.hpp"
5#include "include/timer.hpp"
6#include "include/queue.hpp"
59 TM_MAX = 0xFFFFFFFBUL,
63 #warning "FSM has no defined internal timeouts"
64 #elif ( 1 == Q_FSM_MAX_TIMEOUTS )
66 #elif ( 2 == Q_FSM_MAX_TIMEOUTS )
69 #elif ( 3 == Q_FSM_MAX_TIMEOUTS )
82 #elif ( 4 == Q_FSM_MAX_TIMEOUTS )
86 SIGNAL_TIMEOUT3 = 0xFFFFFFF8UL,
87 #elif ( 5 == Q_FSM_MAX_TIMEOUTS )
91 SIGNAL_TIMEOUT3 = 0xFFFFFFF8UL,
92 SIGNAL_TIMEOUT4 = 0xFFFFFFF7UL,
93 #elif ( 6 == Q_FSM_MAX_TIMEOUTS )
97 SIGNAL_TIMEOUT3 = 0xFFFFFFF8UL,
98 SIGNAL_TIMEOUT4 = 0xFFFFFFF7UL,
99 SIGNAL_TIMEOUT5 = 0xFFFFFFF6UL,
100 #elif ( 7 == Q_FSM_MAX_TIMEOUTS )
104 SIGNAL_TIMEOUT3 = 0xFFFFFFF8UL,
105 SIGNAL_TIMEOUT4 = 0xFFFFFFF7UL,
106 SIGNAL_TIMEOUT5 = 0xFFFFFFF6UL,
107 SIGNAL_TIMEOUT6 = 0xFFFFFFF5UL,
108 #elif ( 8 == Q_FSM_MAX_TIMEOUTS )
112 SIGNAL_TIMEOUT3 = 0xFFFFFFF8UL,
113 SIGNAL_TIMEOUT4 = 0xFFFFFFF7UL,
114 SIGNAL_TIMEOUT5 = 0xFFFFFFF6UL,
115 SIGNAL_TIMEOUT6 = 0xFFFFFFF5UL,
116 SIGNAL_TIMEOUT7 = 0xFFFFFFF4UL,
117 #elif ( 9 == Q_FSM_MAX_TIMEOUTS )
121 SIGNAL_TIMEOUT3 = 0xFFFFFFF8UL,
122 SIGNAL_TIMEOUT4 = 0xFFFFFFF7UL,
123 SIGNAL_TIMEOUT5 = 0xFFFFFFF6UL,
124 SIGNAL_TIMEOUT6 = 0xFFFFFFF5UL,
125 SIGNAL_TIMEOUT7 = 0xFFFFFFF4UL,
126 SIGNAL_TIMEOUT8 = 0xFFFFFFF3UL,
127 #elif ( 10 == Q_FSM_MAX_TIMEOUTS )
131 SIGNAL_TIMEOUT3 = 0xFFFFFFF8UL,
132 SIGNAL_TIMEOUT4 = 0xFFFFFFF7UL,
133 SIGNAL_TIMEOUT5 = 0xFFFFFFF6UL,
134 SIGNAL_TIMEOUT6 = 0xFFFFFFF5UL,
135 SIGNAL_TIMEOUT7 = 0xFFFFFFF4UL,
136 SIGNAL_TIMEOUT8 = 0xFFFFFFF3UL,
137 SIGNAL_TIMEOUT9 = 0xFFFFFFF2UL,
138 #elif ( Q_FSM_MAX_TIMEOUTS > 10 )
139 #error "Max FSM allowed timeout should not be greater that 10"
143 MAX_SIGNAL = TM_MIN - 1
167 return ( s < signalID::MAX_SIGNAL ) ?
static_cast<signalID>( s ) : signalID::MAX_SIGNAL;
182 template <
size_t numberOfSignals>
255 state *StartState{
nullptr };
256 state *NextState{
nullptr };
258 state *State{
nullptr };
262 _Handler( _Handler
const& ) =
delete;
263 void operator=( _Handler
const& ) =
delete;
267 _Handler() =
default;
281 TransitionHistory = m;
360 state *StartState{
nullptr };
361 state *NextState{
nullptr };
362 stateMachine* Machine{
nullptr };
363 state *State{
nullptr };
364 historyMode TransitionHistory{ historyMode::NO_HISTORY };
366 signalID Signal{ signalID::SIGNAL_NONE };
367 stateHandler( stateHandler
const& ) =
delete;
368 void operator=( stateHandler
const& ) =
delete;
370 stateHandler() =
default;
371 virtual ~stateHandler() {}
372 void *SignalData{
nullptr };
373 void *Data{
nullptr };
374 void *StateData{
nullptr };
375 inline void nextState( state &s, historyMode m = historyMode::NO_HISTORY )
noexcept
378 TransitionHistory = m;
380 inline void startState( state &s )
noexcept
384 inline state& thisState(
void )
noexcept
388 inline stateMachine& thisMachine(
void )
noexcept
392 inline signalID signal(
void )
const noexcept
396 inline status lastStatus(
void )
const noexcept
402 using handler_t = stateHandler&;
489 state *nextState{
nullptr };
491 void *signalData{
nullptr };
500 signalData(sigData) {}
506 signalData(sigData) {}
508 xSignal( static_cast<
signalID>( iSignal ) ),
512 signalData(sigData) {}
515 xSignal( static_cast<
signalID>( iSignal ) ),
519 signalData(sigData) {}
544 state *parent{
nullptr };
545 state *lastRunningChild{
nullptr };
546 state *initState{
nullptr };
550 void *sData{
nullptr };
551 size_t tEntries{ 0U };
553 stateHandler *pHandler{
nullptr };
554 void sweepTransitionTable( stateHandler &h )
const noexcept;
556 void operator=(
state const& ) =
delete;
579 return subscribe( &s, sFcn, &init );
591 return subscribe( &s, sFcn,
nullptr );
614 template <
size_t numberOfTransitions>
617 return install( table, numberOfTransitions );
641 ret &=
install( table, nTable );
664 template <
size_t numberOfTransitions,
size_t numberOfTimeoutDefs>
667 return install( table, numberOfTransitions, def, numberOfTimeoutDefs );
698 template <
size_t numberOfTimeoutDefs>
701 return install( def, numberOfTimeoutDefs );
732 uint32_t isPeriodic{ 0U };
740 PS_SUBSCRIBER_NOT_FOUND,
742 PS_SIGNAL_SLOTS_FULL,
743 PS_SUBSCRIBER_SLOTS_FULL
827 queue *sQueue{
nullptr };
832 void *owner{
nullptr };
833 void *mData{
nullptr };
834 bool internalSignalSend(
sm::signalID sig,
void *sData,
bool isUrgent )
noexcept;
835 void timeoutCheckSignals(
void )
noexcept;
838 uint8_t levelsToLCA(
sm::state *target )
const noexcept;
839 void exitUpToLCA( uint8_t lca )
noexcept;
843 void stateOnEntry(
sm::state *s )
noexcept;
846 void tracePathAndRetraceEntry(
sm::state **trace )
noexcept;
847 void traceOnStart(
sm::state **entryPath )
noexcept;
877 return smSetup( topFcn, &init, sFcn, pData );
892 return top.subscribe( &s, sFcn, &init );
905 return top.subscribe( &s, sFcn,
nullptr );
926 template <
size_t numberOfSignals>
929 sq.q.setup( sq.qStack,
sizeof(
sm::signal_t), numberOfSignals );
952 bool sendSignal( sm::signalIDType sig,
void *sData =
nullptr,
bool isUrgent = false ) noexcept
1055 void unsubscribeAll(
void )
noexcept;
The class to interface the OS.
Definition kernel.hpp:83
A Queue object.
Definition queue.hpp:44
The state callback argument to handle the state-machine dynamics and provide execution information....
Definition fsm.hpp:252
void nextState(state &s, historyMode m=historyMode::NO_HISTORY) noexcept
Produce a transition to the desired state.
Definition fsm.hpp:278
stateMachine & thisMachine(void) noexcept
Gets a reference to the state machine in which this state is contained.
Definition fsm.hpp:332
signalID signal(void) const noexcept
Get the Signal ID currently being evaluated for this state.
Definition fsm.hpp:340
state & thisState(void) noexcept
Get a reference to state being evaluated.
Definition fsm.hpp:323
void * Data
Definition fsm.hpp:270
bool timeoutSet(const index_t i, const qOS::duration_t t) noexcept
Set the time for the selected built-in timeout inside the target FSM.
status lastStatus(void) const noexcept
The last state return status.
Definition fsm.hpp:349
void * SignalData
Definition fsm.hpp:269
void startState(state &s) noexcept
Set the nested initial state ( sub-state ). This The application writer should change this field to s...
Definition fsm.hpp:292
bool timeoutStop(const index_t i) noexcept
Stop the time count for the selected built-in timeout.
void * StateData
Definition fsm.hpp:271
An object to subscribe FSM(Finite State Machine) objects to specific user-defined signals.
Definition fsm.hpp:1050
bool subscribeToSignal(stateMachine &m, sm::signalIDType s) noexcept
Definition fsm.hpp:1068
bool unsubscribeFromSignal(stateMachine &m, sm::signalIDType s) noexcept
Definition fsm.hpp:1079
bool sendSignal(sm::signalID sig, void *sData=nullptr, bool isUrgent=false) noexcept
Sends a signal to all its subscribers.
bool subscribeToSignal(stateMachine &m, sm::signalID s) noexcept
Subscribe state machine to a particular signal.
bool unsubscribeFromSignal(stateMachine &m, sm::signalID s) noexcept
Unsubscribe state machine from a particular signal.
A state object.
Definition fsm.hpp:542
virtual sm::status activities(sm::handler_t h)
bool install(transition *table, size_t n) noexcept
Install a table with the outgoing transitions for this state.
transition * getTransitionTable(void) noexcept
Retrieve a pointer to the state transition table.
bool install(timeoutStateDefinition *def, size_t n) noexcept
Install fixed timeouts for the specified state using a lookup-table.
bool add(sm::state &s, const sm::stateCallback_t &sFcn, sm::state &init) noexcept
Add the specified state as a child state.
Definition fsm.hpp:577
bool install(transition(&table)[numberOfTransitions], timeoutStateDefinition(&def)[numberOfTimeoutDefs]) noexcept
Install both, a table with the outgoing transitions for this state and fixed timeouts for the specifi...
Definition fsm.hpp:665
bool install(transition(&table)[numberOfTransitions]) noexcept
Install a table with the outgoing transitions for this state.
Definition fsm.hpp:615
bool install(timeoutStateDefinition(&def)[numberOfTimeoutDefs]) noexcept
Install fixed timeouts for the specified state using a lookup-table.
Definition fsm.hpp:699
bool add(sm::state &s, const sm::stateCallback_t &sFcn) noexcept
Add the specified state as a child state.
Definition fsm.hpp:589
void * getData(void) noexcept
Retrieve the state data or storage-pointer.
void setCallback(const stateCallback_t sFcn) noexcept
Set/Change the state callback.
bool install(transition *table, size_t nTable, timeoutStateDefinition *def, size_t nDef)
Install both, a table with the outgoing transitions for this state and fixed timeouts for the specifi...
Definition fsm.hpp:637
void setData(void *pData) noexcept
Set the state data or storage-pointer.
A FSM Timeout-specification object.
Definition fsm.hpp:731
A FSM(Finite State Machine) object.
Definition fsm.hpp:822
sm::timeoutSpec *const & getTimeSpec(void) const noexcept
Get a pointer to the installed timeout specification if available.
sm::state *const & getCurrent(void) const noexcept
Retrieve a reference to the state currently being evaluated.
const sm::state & getTop(void) const noexcept
Retrieve a reference to the Top state of.
bool install(queue &q) noexcept
Install a signal queue to the provided Finite State Machine (FSM).
queue *const & getQueue(void) const noexcept
Get a pointer to the installed queue if available.
bool add(sm::state &s, const sm::stateCallback_t &sFcn) noexcept
This function adds the specified state to the stateMachine "Top" state.
Definition fsm.hpp:903
bool timeoutStop(const index_t xTimeout) noexcept
Stop the time count for the selected built-in timeout.
bool add(sm::state &s, const sm::stateCallback_t &sFcn, sm::state &init) noexcept
Add the specified state to the stateMachine "Top" state.
Definition fsm.hpp:890
bool sendSignal(sm::signalID sig, void *sData=nullptr, bool isUrgent=false) noexcept
Sends a signal to a state machine.
bool install(sm::timeoutSpec &ts) noexcept
Install the Timeout-specification object to target FSM and allow timed signals within states.
bool timeoutSet(const index_t xTimeout, const qOS::duration_t t) noexcept
Set the time for the selected built-in timeout inside the target FSM.
bool install(sm::signalQueue< numberOfSignals > &sq) noexcept
Install a signal queue to the provided Finite State Machine (FSM).
Definition fsm.hpp:927
void setSurrounding(const sm::surroundingCallback_t &sFcn) noexcept
Set the state-machine surrounding callback.
bool setup(const sm::stateCallback_t &topFcn, sm::state &init, sm::surroundingCallback_t sFcn=nullptr, void *pData=nullptr) noexcept
Initializes a Finite State Machine (FSM).
Definition fsm.hpp:875
bool run(sm::signal_t sig) noexcept
Execute the Finite State Machine (FSM).
void *const & getData(void) const noexcept
Retrieve the state machine data or storage-pointer.
A non-blocking Timer object.
Definition timer.hpp:26
timeCount_t duration_t
The typedef that specified an time quantity, usually expressed in milliseconds.
Definition clock.hpp:18
#define Q_FSM_PS_SIGNALS_MAX
Finite State Machine : number of signals to subscribe.
Definition config.h:82
#define Q_FSM_PS_SUB_PER_SIGNAL_MAX
Finite State Machine : subscribers per signal.
Definition config.h:88
#define Q_FSM_MAX_TIMEOUTS
Finite State Machine : number of timeouts.
Definition config.h:76
base_t state
The intrinsic type of co::position to hold a coroutine progress.
Definition coroutine.hpp:21
historyMode
This enumeration defines the possible modes to perform a transition to history.
Definition fsm.hpp:226
const timeoutSpecOption_t TIMEOUT_RST_ENTRY
This timeout-specification option its used to specify that the engine should reset the timeout when t...
const timeoutSpecOption_t TIMEOUT_PERIODIC
This timeout-specification option its used setup the timeout in periodic mode.
status(*)(handler_t) stateCallback_t
Pointer to a function that represents a state callback.
Definition fsm.hpp:426
const timeoutSpecOption_t TIMEOUT_KEEP_IF_SET
This timeout-specification option its used to specify that the engine should set the timeout only if ...
constexpr timeoutSpecOption_t TIMEOUT_INDEX(index_t i)
Timeout-specification option. Should be used only to specify which timeout signal to use.
Definition fsm.hpp:795
bool(*)(handler_t) signalAction_t
Pointer to a function that represents a signal guard/action.
Definition fsm.hpp:465
constexpr timeoutSpecOption_t TIMEOUT_USE_SIGNAL(index_t i)
Timeout-specification option. Should be used only to specify which timeout signal to use.
Definition fsm.hpp:805
uint32_t signalIDType
Definition fsm.hpp:29
void(*)(handler_t) surroundingCallback_t
Pointer to a function that represents a surrounding callback.
Definition fsm.hpp:446
const timeoutSpecOption_t TIMEOUT_SET_ENTRY
This timeout-specification option its used to specify that the engine should set the timeout when the...
status
This enumeration defines the built-in state-execution status values that can be used as return value ...
Definition fsm.hpp:193
uint32_t timeoutSpecOption_t
A typedef to hold the timeout specification options.
Definition fsm.hpp:470
const timeoutSpecOption_t TIMEOUT_SET_EXIT
This timeout-specification option its used to specify that the engine should set the timeout when the...
constexpr signalID SIGNAL_USER(uint32_t s)
Function to define a user-defined signal.
Definition fsm.hpp:164
constexpr signalID SIGNAL_TIMEOUT(index_t iTm)
Built-in signal to indicate that a timeout expiration event occurs.
Definition fsm.hpp:154
const timeoutSpecOption_t TIMEOUT_RST_EXIT
This timeout-specification option its used to specify that the engine should reset the timeout when t...
signalID
The type for signal ID.
Definition fsm.hpp:34
@ SHALLOW_HISTORY
History will be kept to allow the return to only the top-most sub-state of the most recent state conf...
Definition fsm.hpp:237
@ DEEP_HISTORY
History will be kept to allow full state configuration of the most recent visit to the containing reg...
Definition fsm.hpp:242
@ NO_HISTORY
History is not preserved. Composite states will start according to their default transition.
Definition fsm.hpp:231
@ SUCCESS
To indicate that the status was successful.
Definition fsm.hpp:213
@ FAILURE
To indicate that the state had a failure or abnormal execution. It can be used to handle exceptions.
Definition fsm.hpp:209
@ BEFORE_ANY
(Only available in the surrounding callback) Indicates an execution of the surrounding callback befor...
Definition fsm.hpp:199
@ ABSENT
Not to be used at the application level. This value is reserved for use.
Definition fsm.hpp:204
@ SIGNAL_HANDLED
To indicate that the state handled the signal and therefore it is not necessary to continue propagati...
Definition fsm.hpp:219
@ SIGNAL_ENTRY
Built-in signal to indicate if the current state has just entered from another state.
Definition fsm.hpp:53
@ SIGNAL_START
Built-in signal that can be used to set a nested initial-transition (aka default transition) by using...
Definition fsm.hpp:41
@ SIGNAL_TIMEOUT2
Built-in signal to indicate that the timeout at index 2 expired.
Definition fsm.hpp:81
@ SIGNAL_TIMEOUT0
Built-in signal to indicate that the timeout at index 0 expired.
Definition fsm.hpp:73
@ SIGNAL_NONE
Built-in signal to indicate that there is not signal available.
Definition fsm.hpp:57
@ SIGNAL_EXIT
Built-in signal to indicate if the current state has just exit to another state.
Definition fsm.hpp:47
@ SIGNAL_TIMEOUT1
Built-in signal to indicate that the timeout at index 1 expired.
Definition fsm.hpp:77
STD_TYPE_SIZE_T index_t
A type to instantiate an OS index variable. Can store the maximum size of a theoretically possible ob...
Definition types.hpp:94
OS/Kernel interfaces.
Definition bytebuffer.hpp:7
The type to be used as a container variable for a signal.
Definition fsm.hpp:174
void * data
Definition fsm.hpp:176
The type of a FSM signal-queue.
Definition fsm.hpp:183
signal_t qStack[numberOfSignals]
Definition fsm.hpp:185
queue q
Definition fsm.hpp:184
This type should be used to define an item for a timeout-specification table.
Definition fsm.hpp:476
timeoutSpecOption_t options
Definition fsm.hpp:478
qOS::duration_t xTimeout
Definition fsm.hpp:477
This structure should be used to define an item for a state transition table.
Definition fsm.hpp:485
transition(signalID iSignal, signalAction_t sGuard, state &next, historyMode mHistory=historyMode::NO_HISTORY, void *sigData=nullptr)
Definition fsm.hpp:495
transition(signalIDType iSignal, state &next, uint8_t mHistory=0U, void *sigData=nullptr)
Definition fsm.hpp:514
transition(signalID iSignal, state &next, historyMode mHistory=historyMode::NO_HISTORY, void *sigData=nullptr)
Definition fsm.hpp:501
transition(signalIDType iSignal, signalAction_t sGuard, state &next, uint8_t mHistory=0U, void *sigData=nullptr)
Definition fsm.hpp:507