OS  v1.7.5
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 stateMachine& thisMachine( void ) noexcept
387 {
388 return *Machine;
389 }
390 inline signalID signal( void ) const noexcept
391 {
392 return Signal;
393 }
394 inline status lastStatus( void ) const noexcept
395 {
396 return Status;
397 }
398 friend class qOS::sm::state;
399 };
400 using handler_t = stateHandler&;
402
425
444 using surroundingCallback_t = void (*)( handler_t );
445
463 using signalAction_t = bool (*)( handler_t );
464
468 using timeoutSpecOption_t = uint32_t;
469
478
483 struct transition final {
486 signalAction_t guard{ nullptr };
487 state *nextState{ nullptr };
489 void *signalData{ nullptr };
491 transition() = default;
492 /*cstat -MISRAC++2008-7-1-2*/
493 transition( signalID iSignal, signalAction_t sGuard, state &next, historyMode mHistory = historyMode::NO_HISTORY, void *sigData = nullptr ) :
494 xSignal(iSignal),
495 guard(sGuard),
496 nextState(&next),
497 history(mHistory),
498 signalData(sigData) {}
499 transition( signalID iSignal, state &next, historyMode mHistory = historyMode::NO_HISTORY, void *sigData = nullptr ) :
500 xSignal(iSignal),
501 guard(nullptr),
502 nextState(&next),
503 history(mHistory),
504 signalData(sigData) {}
505 transition( signalIDType iSignal, signalAction_t sGuard, state &next, uint8_t mHistory = 0U, void *sigData = nullptr ) :
506 xSignal( static_cast<signalID>( iSignal ) ),
507 guard(sGuard),
508 nextState(&next),
509 history( static_cast<historyMode>(mHistory) ),
510 signalData(sigData) {}
511 /*cstat +MISRAC++2008-7-1-2*/
512 transition( signalIDType iSignal, state &next, uint8_t mHistory = 0U, void *sigData = nullptr ) :
513 xSignal( static_cast<signalID>( iSignal ) ),
514 guard(nullptr),
515 nextState(&next),
516 history( static_cast<historyMode>(mHistory) ),
517 signalData(sigData) {}
518 };
519
540 class state {
541 private:
542 state *parent{ nullptr };
543 state *lastRunningChild{ nullptr };
544 state *initState{ nullptr };
545 stateCallback_t sCallback{ nullptr};
546 timeoutStateDefinition *tdef{ nullptr };
547 transition *tTable{ nullptr };
548 void *sData{ nullptr };
549 size_t tEntries{ 0U };
550 size_t nTm{ 0U };
551 stateHandler *pHandler{ nullptr };
552 void sweepTransitionTable( stateHandler &h ) const noexcept;
553 state( state const& ) = delete;
554 void operator=( state const& ) = delete;
555 bool subscribe( sm::state *s, const sm::stateCallback_t &sFcn, sm::state *init ) noexcept;
556 void topSelf( const sm::stateCallback_t &topFcn, sm::state *init ) noexcept;
557 protected:
559 public:
560 state() = default;
562 virtual ~state() {}
575 inline bool add( sm::state &s, const sm::stateCallback_t &sFcn, sm::state &init ) noexcept
576 {
577 return subscribe( &s, sFcn, &init );
578 }
579
587 inline bool add( sm::state &s, const sm::stateCallback_t &sFcn ) noexcept
588 {
589 return subscribe( &s, sFcn, nullptr );
590 }
591
601 bool install( transition *table, size_t n ) noexcept;
612 template <size_t numberOfTransitions>
613 bool install( transition (&table)[ numberOfTransitions ] ) noexcept // skipcq : CXX-W2066
614 {
615 return install( table, numberOfTransitions );
616 }
617
635 inline bool install( transition *table, size_t nTable, timeoutStateDefinition *def, size_t nDef )
636 {
637 int ret = 1;
638 /*cstat -MISRAC++2008-5-0-21 -CERT-EXP46-C */
639 ret &= install( table, nTable ); // skipcq: CXX-W2065
640 ret &= install( def, nDef ); // skipcq: CXX-W2065
641 /*cstat +MISRAC++2008-5-0-21 +CERT-EXP46-C */
642 return ( 0 != ret );
643 }
644
662 template <size_t numberOfTransitions, size_t numberOfTimeoutDefs>
663 bool install( transition (&table)[ numberOfTransitions ], timeoutStateDefinition (&def)[ numberOfTimeoutDefs ] ) noexcept // skipcq : CXX-W2066
664 {
665 return install( table, numberOfTransitions, def, numberOfTimeoutDefs );
666 }
667
681 bool install( timeoutStateDefinition *def, size_t n ) noexcept;
696 template <size_t numberOfTimeoutDefs>
697 bool install( timeoutStateDefinition (&def)[ numberOfTimeoutDefs ] ) noexcept // skipcq : CXX-W2066
698 {
699 return install( def, numberOfTimeoutDefs );
700 }
701
705 void* getData( void ) noexcept;
710 void setData( void *pData ) noexcept;
721 void setCallback( const stateCallback_t sFcn ) noexcept;
722 friend class qOS::stateMachine;
723 };
724
730 uint32_t isPeriodic{ 0U };
731 timer timeout[ Q_FSM_MAX_TIMEOUTS ]; // skipcq: CXX-W2066
732 friend class qOS::stateMachine;
733 };
734
736 enum psReqStatus{
737 PS_SIGNAL_NOT_FOUND,
738 PS_SUBSCRIBER_NOT_FOUND,
739 PS_SUBSCRIBER_FOUND,
740 PS_SIGNAL_SLOTS_FULL,
741 PS_SUBSCRIBER_SLOTS_FULL
742 };
743
744 struct psIndex_t {
745 psReqStatus status;
746 size_t sig_slot;
747 size_t sub_slot;
748 };
750
794 {
795 return ( 0x00FFFFFFUL & static_cast<timeoutSpecOption_t>( i ) );
796 }
797
804 {
805 return ( 0x00FFFFFFUL & static_cast<timeoutSpecOption_t>( i ) );
806 }
807
808 }
809
820 class stateMachine : protected sm::stateHandler {
821 private:
822 sm::state *current{ nullptr };
823 sm::state *next{ nullptr };
824 sm::state *source{ nullptr };
825 queue *sQueue{ nullptr };
826 sm::timeoutSpec *timeSpec{ nullptr };
827 sm::surroundingCallback_t surrounding{ nullptr };
828 sm::state top;
829 sm::signal_t signalNot;
830 void *owner{ nullptr };
831 void *mData{ nullptr };
832 bool internalSignalSend( sm::signalID sig, void *sData, bool isUrgent ) noexcept;
833 void timeoutCheckSignals( void ) noexcept;
834 void timeoutPerformSpecifiedActions( sm::state * const s, sm::signalID sig ) noexcept;
835 void transition( sm::state *target, sm::historyMode mHistory ) noexcept;
836 uint8_t levelsToLCA( sm::state *target ) const noexcept;
837 void exitUpToLCA( uint8_t lca ) noexcept;
838 void prepareHandler( sm::signal_t sig, sm::state *s ) noexcept;
839 sm::status invokeStateActivities( sm::state * const s ) noexcept;
840 sm::state* stateOnExit( sm::state *s ) noexcept;
841 void stateOnEntry( sm::state *s ) noexcept;
842 sm::state* stateOnStart( sm::state *s ) noexcept;
843 sm::status stateOnSignal( sm::state *s, sm::signal_t sig ) noexcept;
844 void tracePathAndRetraceEntry( sm::state **trace ) noexcept;
845 void traceOnStart( sm::state **entryPath ) noexcept;
846 sm::signal_t checkForSignals( sm::signal_t sig ) noexcept;
847 stateMachine( stateMachine const& ) = delete;
848 void operator=( stateMachine const& ) = delete;
849 bool smSetup( sm::stateCallback_t topFcn, sm::state *init, const sm::surroundingCallback_t sFcn, void* pData ) noexcept;
850
851 /*cstat -MISRAC++2008-8-5-2*/
852 static sm::signalID psSignals[ Q_FSM_PS_SIGNALS_MAX ]; // skipcq: CXX-W2009, CXX-W2066
853 /*cstat +MISRAC++2008-8-5-2*/
854 static stateMachine* psSubs[ Q_FSM_PS_SIGNALS_MAX ][ Q_FSM_PS_SUB_PER_SIGNAL_MAX ]; // skipcq: CXX-W2009, CXX-W2066
855 static const sm::timeoutSpecOption_t OPT_INDEX_MASK;
856 public:
857 stateMachine() = default;
859 virtual ~stateMachine() {}
873 inline bool setup( const sm::stateCallback_t &topFcn, sm::state &init, sm::surroundingCallback_t sFcn = nullptr, void* pData = nullptr) noexcept
874 {
875 return smSetup( topFcn, &init, sFcn, pData );
876 }
877
888 inline bool add( sm::state &s, const sm::stateCallback_t &sFcn, sm::state &init ) noexcept
889 {
890 return top.subscribe( &s, sFcn, &init );
891 }
892
901 inline bool add( sm::state &s, const sm::stateCallback_t &sFcn ) noexcept
902 {
903 return top.subscribe( &s, sFcn, nullptr );
904 }
905
916 bool install( queue& q ) noexcept;
917
924 template <size_t numberOfSignals>
926 {
927 sq.q.setup( sq.qStack, sizeof(sm::signal_t), numberOfSignals );
928 return install( sq.q );
929 }
930
949 bool sendSignal( sm::signalID sig, void *sData = nullptr, bool isUrgent = false ) noexcept;
950 bool sendSignal( sm::signalIDType sig, void *sData = nullptr, bool isUrgent = false ) noexcept
951 {
952 return sendSignal( static_cast<sm::signalID>( sig ), sData, isUrgent );
953 }
954
966 bool install( sm::timeoutSpec &ts ) noexcept;
978 bool timeoutSet( const index_t xTimeout, const qOS::duration_t t ) noexcept;
989 bool timeoutStop( const index_t xTimeout ) noexcept;
994 const sm::state& getTop( void ) const noexcept;
999 sm::state * const & getCurrent( void ) const noexcept;
1005 queue * const & getQueue( void ) const noexcept;
1011 sm::timeoutSpec * const & getTimeSpec( void ) const noexcept;
1016 void * const & getData( void ) const noexcept;
1021 void setSurrounding( const sm::surroundingCallback_t &sFcn ) noexcept;
1032 bool run( sm::signal_t sig ) noexcept;
1037 bool isInitialized( void ) const {
1038 return ( nullptr != top.initState );
1039 }
1040
1044 explicit operator bool() const noexcept {
1045 return isInitialized();
1046 }
1047 friend class core;
1048 };
1049
1050 namespace sm {
1054
1063 private:
1064 sm::signalID psSignals[ Q_FSM_PS_SIGNALS_MAX ] = { sm::signalID::SIGNAL_START }; // skipcq: CXX-W2009, CXX-W2066
1065 stateMachine* psSubs[ Q_FSM_PS_SIGNALS_MAX ][ Q_FSM_PS_SUB_PER_SIGNAL_MAX ]; // skipcq: CXX-W2009, CXX-W2066
1066 sm::psIndex_t getSubscriptionStatus( stateMachine &m, sm::signalID s ) const noexcept;
1067 void unsubscribeAll( void ) noexcept;
1068 public:
1071 virtual ~signalPublisher() {}
1081 {
1082 return subscribeToSignal( m, static_cast<sm::signalID>( s ) );
1083 }
1084
1092 {
1093 return unsubscribeFromSignal( m, static_cast<sm::signalID>( s ) );
1094 }
1095
1117 bool sendSignal( sm::signalID sig, void *sData = nullptr, bool isUrgent = false ) noexcept;
1118 bool sendSignal( sm::signalIDType sig, void *sData = nullptr, bool isUrgent = false ) noexcept
1119 {
1120 return sendSignal( static_cast<sm::signalID>( sig ), sData, isUrgent );
1121 }
1122 };
1123
1124 }
1125}
1126
1127
1128
1129#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:1080
bool unsubscribeFromSignal(stateMachine &m, sm::signalIDType s) noexcept
Definition fsm.hpp:1091
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:540
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:575
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:663
bool install(transition(&table)[numberOfTransitions]) noexcept
Install a table with the outgoing transitions for this state.
Definition fsm.hpp:613
bool install(timeoutStateDefinition(&def)[numberOfTimeoutDefs]) noexcept
Install fixed timeouts for the specified state using a lookup-table.
Definition fsm.hpp:697
bool add(sm::state &s, const sm::stateCallback_t &sFcn) noexcept
Add the specified state as a child state.
Definition fsm.hpp:587
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:635
state()=default
void setData(void *pData) noexcept
Set the state data or storage-pointer.
A FSM Timeout-specification object.
Definition fsm.hpp:729
A FSM(Finite State Machine) object.
Definition fsm.hpp:820
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:1037
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:901
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:888
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:925
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:873
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:1047
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:424
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:793
bool(*)(handler_t) signalAction_t
Pointer to a function that represents a signal guard/action.
Definition fsm.hpp:463
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:803
uint32_t signalIDType
Definition fsm.hpp:29
void(*)(handler_t) surroundingCallback_t
Pointer to a function that represents a surrounding callback.
Definition fsm.hpp:444
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:468
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:474
timeoutSpecOption_t options
Definition fsm.hpp:476
qOS::duration_t xTimeout
Definition fsm.hpp:475
This structure should be used to define an item for a state transition table.
Definition fsm.hpp:483
transition(signalID iSignal, signalAction_t sGuard, state &next, historyMode mHistory=historyMode::NO_HISTORY, void *sigData=nullptr)
Definition fsm.hpp:493
transition(signalIDType iSignal, state &next, uint8_t mHistory=0U, void *sigData=nullptr)
Definition fsm.hpp:512
transition(signalID iSignal, state &next, historyMode mHistory=historyMode::NO_HISTORY, void *sigData=nullptr)
Definition fsm.hpp:499
transition(signalIDType iSignal, signalAction_t sGuard, state &next, uint8_t mHistory=0U, void *sigData=nullptr)
Definition fsm.hpp:505