OS  v1.8.0
Documentation
Loading...
Searching...
No Matches
fsm.hpp
1#ifndef QOS_CPP_FSM
2#define QOS_CPP_FSM
3
4#include "include/types.hpp"
5#include "include/timer.hpp"
6#include "include/queue.hpp"
7
8namespace qOS {
9
10
15
16 class stateMachine;
17
21 namespace sm {
22
26
27 class state;
28
29 using signalIDType = uint32_t;
30
41 SIGNAL_START = 0xFFFFFFFFUL,
47 SIGNAL_EXIT = 0xFFFFFFFEUL,
53 SIGNAL_ENTRY = 0xFFFFFFFDUL,
57 SIGNAL_NONE = 0xFFFFFFFCUL,
59 TM_MAX = 0xFFFFFFFBUL,
60 TM_MIN = TM_MAX - ( Q_FSM_MAX_TIMEOUTS - 1 ),
62 #if ( 0 == Q_FSM_MAX_TIMEOUTS )
63 #warning "FSM has no defined internal timeouts"
64 #elif ( 1 == Q_FSM_MAX_TIMEOUTS )
65 SIGNAL_TIMEOUT0 = 0xFFFFFFFBUL,
66 #elif ( 2 == Q_FSM_MAX_TIMEOUTS )
67 SIGNAL_TIMEOUT0= 0xFFFFFFFBUL,
68 SIGNAL_TIMEOUT1 = 0xFFFFFFFAUL,
69 #elif ( 3 == Q_FSM_MAX_TIMEOUTS )
73 SIGNAL_TIMEOUT0 = 0xFFFFFFFBUL,
77 SIGNAL_TIMEOUT1 = 0xFFFFFFFAUL,
81 SIGNAL_TIMEOUT2 = 0xFFFFFFF9UL,
82 #elif ( 4 == Q_FSM_MAX_TIMEOUTS )
83 SIGNAL_TIMEOUT0 = 0xFFFFFFFBUL,
84 SIGNAL_TIMEOUT1 = 0xFFFFFFFAUL,
85 SIGNAL_TIMEOUT2 = 0xFFFFFFF9UL,
86 SIGNAL_TIMEOUT3 = 0xFFFFFFF8UL,
87 #elif ( 5 == Q_FSM_MAX_TIMEOUTS )
88 SIGNAL_TIMEOUT0 = 0xFFFFFFFBUL,
89 SIGNAL_TIMEOUT1 = 0xFFFFFFFAUL,
90 SIGNAL_TIMEOUT2 = 0xFFFFFFF9UL,
91 SIGNAL_TIMEOUT3 = 0xFFFFFFF8UL,
92 SIGNAL_TIMEOUT4 = 0xFFFFFFF7UL,
93 #elif ( 6 == Q_FSM_MAX_TIMEOUTS )
94 SIGNAL_TIMEOUT0 = 0xFFFFFFFBUL,
95 SIGNAL_TIMEOUT1 = 0xFFFFFFFAUL,
96 SIGNAL_TIMEOUT2 = 0xFFFFFFF9UL,
97 SIGNAL_TIMEOUT3 = 0xFFFFFFF8UL,
98 SIGNAL_TIMEOUT4 = 0xFFFFFFF7UL,
99 SIGNAL_TIMEOUT5 = 0xFFFFFFF6UL,
100 #elif ( 7 == Q_FSM_MAX_TIMEOUTS )
101 SIGNAL_TIMEOUT0 = 0xFFFFFFFBUL,
102 SIGNAL_TIMEOUT1 = 0xFFFFFFFAUL,
103 SIGNAL_TIMEOUT2 = 0xFFFFFFF9UL,
104 SIGNAL_TIMEOUT3 = 0xFFFFFFF8UL,
105 SIGNAL_TIMEOUT4 = 0xFFFFFFF7UL,
106 SIGNAL_TIMEOUT5 = 0xFFFFFFF6UL,
107 SIGNAL_TIMEOUT6 = 0xFFFFFFF5UL,
108 #elif ( 8 == Q_FSM_MAX_TIMEOUTS )
109 SIGNAL_TIMEOUT0 = 0xFFFFFFFBUL,
110 SIGNAL_TIMEOUT1 = 0xFFFFFFFAUL,
111 SIGNAL_TIMEOUT2 = 0xFFFFFFF9UL,
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 )
118 SIGNAL_TIMEOUT0 = 0xFFFFFFFBUL,
119 SIGNAL_TIMEOUT1 = 0xFFFFFFFAUL,
120 SIGNAL_TIMEOUT2 = 0xFFFFFFF9UL,
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 )
128 SIGNAL_TIMEOUT0 = 0xFFFFFFFBUL,
129 SIGNAL_TIMEOUT1 = 0xFFFFFFFAUL,
130 SIGNAL_TIMEOUT2 = 0xFFFFFFF9UL,
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"
140 #endif
142 MIN_SIGNAL = 0x0UL,
143 MAX_SIGNAL = TM_MIN - 1
145 };
146
147
148
155 {
156 return static_cast<signalID>( signalID::TM_MAX - static_cast<signalID>( Q_FSM_MAX_TIMEOUTS - 1 ) + static_cast<signalID>( iTm ) );
157 }
158
164 constexpr signalID SIGNAL_USER( uint32_t s )
165 {
166 /*cstat -MISRAC++2008-5-0-3*/
167 return ( s < signalID::MAX_SIGNAL ) ? static_cast<signalID>( s ) : signalID::MAX_SIGNAL;
168 /*cstat +MISRAC++2008-5-0-3*/
169 }
170
174 struct signal_t {
176 void *data{ nullptr };
177 };
178
182 template <size_t numberOfSignals>
183 struct signalQueue {
185 signal_t qStack[ numberOfSignals ]; // skipcq: CXX-W2066
186 };
187
188
193 enum status : int16_t {
199 BEFORE_ANY = -32767,
204 ABSENT = -32766,
209 FAILURE = -32765,
213 SUCCESS = -32764,
220 };
221
244
245 #ifdef DOXYGEN
252 class handler_t final {
253 protected:
255 state *StartState{ nullptr };
256 state *NextState{ nullptr };
257 stateMachine* Machine{ nullptr };
258 state *State{ nullptr };
259 historyMode TransitionHistory{ historyMode::NO_HISTORY };
260 status Status{ SUCCESS };
262 _Handler( _Handler const& ) = delete; /* not copyable*/
263 void operator=( _Handler const& ) = delete; /* not assignable*/
265 public:
267 _Handler() = default;
269 void *SignalData{ nullptr };
270 void *Data{ nullptr };
271 void *StateData{ nullptr };
278 inline void nextState( state &s, historyMode m = historyMode::NO_HISTORY ) noexcept
279 {
280 NextState = &s;
281 TransitionHistory = m;
282 }
283
292 inline void startState( state &s ) noexcept
293 {
294 StartState = &s;
295 }
296
307 bool timeoutSet( const index_t i, const qOS::duration_t t ) noexcept;
318 bool timeoutStop( const index_t i ) noexcept;
323 inline state& thisState( void ) noexcept
324 {
325 return *State;
326 }
327
332 inline stateMachine& thisMachine( void ) noexcept
333 {
334 return *Machine;
335 }
336
340 inline signalID signal( void ) const noexcept
341 {
342 return Signal;
343 }
344
349 inline status lastStatus( void ) const noexcept
350 {
351 return Status;
352 }
353 friend class qOS::sm::state;
354 };
355 #endif
356
358 class stateHandler : private nonCopyable {
359 protected:
360 state *StartState{ nullptr };
361 state *NextState{ nullptr };
362 stateMachine* Machine{ nullptr };
363 state *State{ nullptr };
364 historyMode TransitionHistory{ historyMode::NO_HISTORY };
365 status Status{ SUCCESS };
366 signalID Signal{ signalID::SIGNAL_NONE };
367 stateHandler() = default;
368 public:
369 virtual ~stateHandler() {}
370 void *SignalData{ nullptr };
371 void *Data{ nullptr };
372 void *StateData{ nullptr };
373 inline void nextState( state &s, historyMode m = historyMode::NO_HISTORY ) noexcept
374 {
375 NextState = &s;
376 TransitionHistory = m;
377 }
378 inline void startState( state &s ) noexcept
379 {
380 StartState = &s;
381 }
382 inline state& thisState( void ) noexcept
383 {
384 return *State;
385 }
386 inline const state& thisState( void ) const noexcept
387 {
388 return *State;
389 }
390 inline stateMachine& thisMachine( void ) noexcept
391 {
392 return *Machine;
393 }
394 inline const stateMachine& thisMachine( void ) const noexcept
395 {
396 return *Machine;
397 }
398 inline signalID signal( void ) const noexcept
399 {
400 return Signal;
401 }
402 inline status lastStatus( void ) const noexcept
403 {
404 return Status;
405 }
406 friend class qOS::sm::state;
407 };
408 using handler_t = stateHandler&;
410
433
452 using surroundingCallback_t = void (*)( handler_t );
453
471 using signalAction_t = bool (*)( handler_t );
472
476 using timeoutSpecOption_t = uint32_t;
477
486
491 struct transition final {
494 signalAction_t guard{ nullptr };
495 state *nextState{ nullptr };
497 void *signalData{ nullptr };
499 transition() = default;
500 /*cstat -MISRAC++2008-7-1-2*/
501 transition( signalID iSignal, signalAction_t sGuard, state &next, historyMode mHistory = historyMode::NO_HISTORY, void *sigData = nullptr ) :
502 xSignal(iSignal),
503 guard(sGuard),
504 nextState(&next),
505 history(mHistory),
506 signalData(sigData) {}
507 transition( signalID iSignal, state &next, historyMode mHistory = historyMode::NO_HISTORY, void *sigData = nullptr ) :
508 xSignal(iSignal),
509 guard(nullptr),
510 nextState(&next),
511 history(mHistory),
512 signalData(sigData) {}
513 transition( signalIDType iSignal, signalAction_t sGuard, state &next, uint8_t mHistory = 0U, void *sigData = nullptr ) :
514 xSignal( static_cast<signalID>( iSignal ) ),
515 guard(sGuard),
516 nextState(&next),
517 history( static_cast<historyMode>(mHistory) ),
518 signalData(sigData) {}
519 /*cstat +MISRAC++2008-7-1-2*/
520 transition( signalIDType iSignal, state &next, uint8_t mHistory = 0U, void *sigData = nullptr ) :
521 xSignal( static_cast<signalID>( iSignal ) ),
522 guard(nullptr),
523 nextState(&next),
524 history( static_cast<historyMode>(mHistory) ),
525 signalData(sigData) {}
526 };
527
548 class state {
549 private:
550 state *parent{ nullptr };
551 state *lastRunningChild{ nullptr };
552 state *initState{ nullptr };
553 stateCallback_t sCallback{ nullptr};
554 timeoutStateDefinition *tdef{ nullptr };
555 transition *tTable{ nullptr };
556 void *sData{ nullptr };
557 size_t tEntries{ 0U };
558 size_t nTm{ 0U };
559 stateHandler *pHandler{ nullptr };
560 void sweepTransitionTable( stateHandler &h ) const noexcept;
561 state( state const& ) = delete;
562 void operator=( state const& ) = delete;
563 bool subscribe( sm::state *s, const sm::stateCallback_t &sFcn, sm::state *init ) noexcept;
564 void topSelf( const sm::stateCallback_t &topFcn, sm::state *init ) noexcept;
565 protected:
567 public:
568 state() = default;
570 virtual ~state() {}
583 inline bool add( sm::state &s, const sm::stateCallback_t &sFcn, sm::state &init ) noexcept
584 {
585 return subscribe( &s, sFcn, &init );
586 }
587
595 inline bool add( sm::state &s, const sm::stateCallback_t &sFcn ) noexcept
596 {
597 return subscribe( &s, sFcn, nullptr );
598 }
599
609 bool install( transition *table, size_t n ) noexcept;
620 template <size_t numberOfTransitions>
621 bool install( transition (&table)[ numberOfTransitions ] ) noexcept // skipcq : CXX-W2066
622 {
623 return install( table, numberOfTransitions );
624 }
625
643 inline bool install( transition *table, size_t nTable, timeoutStateDefinition *def, size_t nDef )
644 {
645 int ret = 1;
646 /*cstat -MISRAC++2008-5-0-21 -CERT-EXP46-C */
647 ret &= install( table, nTable ); // skipcq: CXX-W2065
648 ret &= install( def, nDef ); // skipcq: CXX-W2065
649 /*cstat +MISRAC++2008-5-0-21 +CERT-EXP46-C */
650 return ( 0 != ret );
651 }
652
670 template <size_t numberOfTransitions, size_t numberOfTimeoutDefs>
671 bool install( transition (&table)[ numberOfTransitions ], timeoutStateDefinition (&def)[ numberOfTimeoutDefs ] ) noexcept // skipcq : CXX-W2066
672 {
673 return install( table, numberOfTransitions, def, numberOfTimeoutDefs );
674 }
675
689 bool install( timeoutStateDefinition *def, size_t n ) noexcept;
704 template <size_t numberOfTimeoutDefs>
705 bool install( timeoutStateDefinition (&def)[ numberOfTimeoutDefs ] ) noexcept // skipcq : CXX-W2066
706 {
707 return install( def, numberOfTimeoutDefs );
708 }
709
713 void* getData( void ) noexcept;
718 void setData( void *pData ) noexcept;
729 void setCallback( const stateCallback_t sFcn ) noexcept;
730 friend class qOS::stateMachine;
731 };
732
738 uint32_t isPeriodic{ 0U };
739 timer timeout[ Q_FSM_MAX_TIMEOUTS ]; // skipcq: CXX-W2066
740 friend class qOS::stateMachine;
741 };
742
744 enum psReqStatus{
745 PS_SIGNAL_NOT_FOUND,
746 PS_SUBSCRIBER_NOT_FOUND,
747 PS_SUBSCRIBER_FOUND,
748 PS_SIGNAL_SLOTS_FULL,
749 PS_SUBSCRIBER_SLOTS_FULL
750 };
751
752 struct psIndex_t {
753 psReqStatus status;
754 size_t sig_slot;
755 size_t sub_slot;
756 };
758
802 {
803 return ( 0x00FFFFFFUL & static_cast<timeoutSpecOption_t>( i ) );
804 }
805
812 {
813 return ( 0x00FFFFFFUL & static_cast<timeoutSpecOption_t>( i ) );
814 }
815
816 }
817
828 class stateMachine : protected sm::stateHandler {
829 private:
830 sm::state *current{ nullptr };
831 sm::state *next{ nullptr };
832 sm::state *source{ nullptr };
833 queue *sQueue{ nullptr };
834 sm::timeoutSpec *timeSpec{ nullptr };
835 sm::surroundingCallback_t surrounding{ nullptr };
836 sm::state top;
837 sm::signal_t signalNot;
838 void *owner{ nullptr };
839 void *mData{ nullptr };
840 bool internalSignalSend( sm::signalID sig, void *sData, bool isUrgent ) noexcept;
841 void timeoutCheckSignals( void ) noexcept;
842 void timeoutPerformSpecifiedActions( sm::state * const s, sm::signalID sig ) noexcept;
843 void transition( sm::state *target, sm::historyMode mHistory ) noexcept;
844 uint8_t levelsToLCA( sm::state *target ) const noexcept;
845 void exitUpToLCA( uint8_t lca ) noexcept;
846 void prepareHandler( sm::signal_t sig, sm::state *s ) noexcept;
847 sm::status invokeStateActivities( sm::state * const s ) noexcept;
848 sm::state* stateOnExit( sm::state *s ) noexcept;
849 void stateOnEntry( sm::state *s ) noexcept;
850 sm::state* stateOnStart( sm::state *s ) noexcept;
851 sm::status stateOnSignal( sm::state *s, sm::signal_t sig ) noexcept;
852 void tracePathAndRetraceEntry( sm::state **trace ) noexcept;
853 void traceOnStart( sm::state **entryPath ) noexcept;
854 sm::signal_t checkForSignals( sm::signal_t sig ) noexcept;
855 stateMachine( stateMachine const& ) = delete;
856 void operator=( stateMachine const& ) = delete;
857 bool smSetup( sm::stateCallback_t topFcn, sm::state *init, const sm::surroundingCallback_t sFcn, void* pData ) noexcept;
858
859 /*cstat -MISRAC++2008-8-5-2*/
860 static sm::signalID psSignals[ Q_FSM_PS_SIGNALS_MAX ]; // skipcq: CXX-W2009, CXX-W2066
861 /*cstat +MISRAC++2008-8-5-2*/
862 static stateMachine* psSubs[ Q_FSM_PS_SIGNALS_MAX ][ Q_FSM_PS_SUB_PER_SIGNAL_MAX ]; // skipcq: CXX-W2009, CXX-W2066
863 static const sm::timeoutSpecOption_t OPT_INDEX_MASK;
864 public:
865 stateMachine() = default;
867 virtual ~stateMachine() {}
881 inline bool setup( const sm::stateCallback_t &topFcn, sm::state &init, sm::surroundingCallback_t sFcn = nullptr, void* pData = nullptr) noexcept
882 {
883 return smSetup( topFcn, &init, sFcn, pData );
884 }
885
896 inline bool add( sm::state &s, const sm::stateCallback_t &sFcn, sm::state &init ) noexcept
897 {
898 return top.subscribe( &s, sFcn, &init );
899 }
900
909 inline bool add( sm::state &s, const sm::stateCallback_t &sFcn ) noexcept
910 {
911 return top.subscribe( &s, sFcn, nullptr );
912 }
913
924 bool install( queue& q ) noexcept;
925
932 template <size_t numberOfSignals>
934 {
935 sq.q.setup( sq.qStack, sizeof(sm::signal_t), numberOfSignals );
936 return install( sq.q );
937 }
938
957 bool sendSignal( sm::signalID sig, void *sData = nullptr, bool isUrgent = false ) noexcept;
958 bool sendSignal( sm::signalIDType sig, void *sData = nullptr, bool isUrgent = false ) noexcept
959 {
960 return sendSignal( static_cast<sm::signalID>( sig ), sData, isUrgent );
961 }
962
974 bool install( sm::timeoutSpec &ts ) noexcept;
986 bool timeoutSet( const index_t xTimeout, const qOS::duration_t t ) noexcept;
997 bool timeoutStop( const index_t xTimeout ) noexcept;
1002 const sm::state& getTop( void ) const noexcept;
1007 sm::state * const & getCurrent( void ) const noexcept;
1013 queue * const & getQueue( void ) const noexcept;
1019 sm::timeoutSpec * const & getTimeSpec( void ) const noexcept;
1024 void * const & getData( void ) const noexcept;
1029 void setSurrounding( const sm::surroundingCallback_t &sFcn ) noexcept;
1040 bool run( sm::signal_t sig ) noexcept;
1045 bool isInitialized( void ) const {
1046 return ( nullptr != top.initState );
1047 }
1048
1052 explicit operator bool() const noexcept {
1053 return isInitialized();
1054 }
1055 friend class core;
1056 };
1057
1058 namespace sm {
1062
1071 private:
1072 sm::signalID psSignals[ Q_FSM_PS_SIGNALS_MAX ] = { sm::signalID::SIGNAL_START }; // skipcq: CXX-W2009, CXX-W2066
1073 stateMachine* psSubs[ Q_FSM_PS_SIGNALS_MAX ][ Q_FSM_PS_SUB_PER_SIGNAL_MAX ]; // skipcq: CXX-W2009, CXX-W2066
1074 sm::psIndex_t getSubscriptionStatus( stateMachine &m, sm::signalID s ) const noexcept;
1075 void unsubscribeAll( void ) noexcept;
1076 public:
1079 virtual ~signalPublisher() {}
1089 {
1090 return subscribeToSignal( m, static_cast<sm::signalID>( s ) );
1091 }
1092
1100 {
1101 return unsubscribeFromSignal( m, static_cast<sm::signalID>( s ) );
1102 }
1103
1125 bool sendSignal( sm::signalID sig, void *sData = nullptr, bool isUrgent = false ) noexcept;
1126 bool sendSignal( sm::signalIDType sig, void *sData = nullptr, bool isUrgent = false ) noexcept
1127 {
1128 return sendSignal( static_cast<sm::signalID>( sig ), sData, isUrgent );
1129 }
1130 };
1131
1132 }
1133}
1134
1135
1136
1137#endif /*QOS_CPP_FSM*/
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
friend class qOS::sm::state
Definition fsm.hpp:353
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
bool subscribeToSignal(stateMachine &m, sm::signalIDType s) noexcept
Definition fsm.hpp:1088
bool unsubscribeFromSignal(stateMachine &m, sm::signalIDType s) noexcept
Definition fsm.hpp:1099
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:548
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:583
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:671
bool install(transition(&table)[numberOfTransitions]) noexcept
Install a table with the outgoing transitions for this state.
Definition fsm.hpp:621
bool install(timeoutStateDefinition(&def)[numberOfTimeoutDefs]) noexcept
Install fixed timeouts for the specified state using a lookup-table.
Definition fsm.hpp:705
bool add(sm::state &s, const sm::stateCallback_t &sFcn) noexcept
Add the specified state as a child state.
Definition fsm.hpp:595
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:643
state()=default
void setData(void *pData) noexcept
Set the state data or storage-pointer.
A FSM Timeout-specification object.
Definition fsm.hpp:737
A FSM(Finite State Machine) object.
Definition fsm.hpp:828
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).
bool isInitialized(void) const
Check if the state machine instance has been initialized.
Definition fsm.hpp:1045
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:909
bool timeoutStop(const index_t xTimeout) noexcept
Stop the time count for the selected built-in timeout.
stateMachine()=default
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:896
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:933
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:881
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.
friend class core
Definition fsm.hpp:1055
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:432
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:801
bool(*)(handler_t) signalAction_t
Pointer to a function that represents a signal guard/action.
Definition fsm.hpp:471
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:811
uint32_t signalIDType
Definition fsm.hpp:29
void(*)(handler_t) surroundingCallback_t
Pointer to a function that represents a surrounding callback.
Definition fsm.hpp:452
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:476
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
Finite State Machine interfaces.
Definition fsm.hpp:21
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:482
timeoutSpecOption_t options
Definition fsm.hpp:484
qOS::duration_t xTimeout
Definition fsm.hpp:483
This structure should be used to define an item for a state transition table.
Definition fsm.hpp:491
transition(signalID iSignal, signalAction_t sGuard, state &next, historyMode mHistory=historyMode::NO_HISTORY, void *sigData=nullptr)
Definition fsm.hpp:501
transition(signalIDType iSignal, state &next, uint8_t mHistory=0U, void *sigData=nullptr)
Definition fsm.hpp:520
transition(signalID iSignal, state &next, historyMode mHistory=historyMode::NO_HISTORY, void *sigData=nullptr)
Definition fsm.hpp:507
transition(signalIDType iSignal, signalAction_t sGuard, state &next, uint8_t mHistory=0U, void *sigData=nullptr)
Definition fsm.hpp:513