OS  v7.3.3
Documentation
Loading...
Searching...
No Matches
qfsm.h
1
8#ifndef Q_FSM_H
9 #define Q_FSM_H
10
11 #include "qtypes.h"
12 #include "qqueues.h"
13 #include "qstimers.h"
14
15 #ifdef __cplusplus
16 extern "C" {
17 #endif
18
19
20 #ifndef Q_FSM
21 #define Q_FSM ( 1 )
22 #endif
23
24 #ifndef Q_FSM_MAX_NEST_DEPTH
25 #define Q_FSM_MAX_NEST_DEPTH ( 5 )
26 #endif
27
28 #ifndef Q_FSM_MAX_TIMEOUTS
29 #define Q_FSM_MAX_TIMEOUTS ( 3 )
30 #endif
31
32 #ifndef Q_FSM_PS_SIGNALS_MAX
33 #define Q_FSM_PS_SIGNALS_MAX ( 0 )
34 #endif
35
36 #ifndef Q_FSM_PS_SUB_PER_SIGNAL_MAX
37 #define Q_FSM_PS_SUB_PER_SIGNAL_MAX ( 0 )
38 #endif
39
40 #if ( Q_FSM_MAX_NEST_DEPTH < 1 )
41 #error Q_FSM_MAX_NEST_DEPTH must be defined to be greater than or equal to 1.
42 #endif
43
44 #if ( ( Q_FSM_MAX_TIMEOUTS < 1 ) || ( Q_FSM_MAX_TIMEOUTS > 32 ) )
45 #error Q_FSM_MAX_TIMEOUTS must be defined with a value between 1 to 32.
46 #endif
47
57 #define QSM_STATE_TOP ( NULL )
58
60 #define QSM_TSOPT_MASK ( (qSM_TimeoutSpecOptions_t)0x00FFFFFFUL )
63 /*FSM internal signals*/
70 #define QSM_SIGNAL_START ( (qSM_SigId_t)0xFFFFFFFFUL )
76 #define QSM_SIGNAL_EXIT ( (qSM_SigId_t)0xFFFFFFFEUL )
82 #define QSM_SIGNAL_ENTRY ( (qSM_SigId_t)0xFFFFFFFDUL )
86 #define QSM_SIGNAL_NONE ( (qSM_SigId_t)0xFFFFFFFCUL )
91 #define QSM_SIGNAL_TIMEOUT(index) \
92 ( (qSM_SigId_t)0xFFFFFFFBUL - (qSM_SigId_t)( Q_FSM_MAX_TIMEOUTS - 1 ) \
93 + (qSM_SigId_t)(index) ) \
94
100 #define QSM_TSOPT_INDEX(index) \
101 ( ( QSM_TSOPT_MASK & (qSM_TimeoutSpecOptions_t)(index) ) ) \
102
108 #define QSM_TSOPT_SET_ENTRY ( (qSM_TimeoutSpecOptions_t)0x01000000UL )
114 #define QSM_TSOPT_RST_ENTRY ( (qSM_TimeoutSpecOptions_t)0x02000000UL )
120 #define QSM_TSOPT_SET_EXIT ( (qSM_TimeoutSpecOptions_t)0x04000000UL )
126 #define QSM_TSOPT_RST_EXIT ( (qSM_TimeoutSpecOptions_t)0x08000000UL )
132 #define QSM_TSOPT_KEEP_IF_SET ( (qSM_TimeoutSpecOptions_t)0x10000000UL )
138 #define QSM_TSOPT_PERIODIC ( (qSM_TimeoutSpecOptions_t)0x20000000UL )
139
143 #define QSM_SIGNAL_RANGE_MIN ( (qSM_SigId_t)0U )
147 #define QSM_SIGNAL_RANGE_MAX \
148 ( (qSM_SigId_t)( 0xFFFFFFFBUL - (qSM_SigId_t)Q_FSM_MAX_TIMEOUTS ) ) \
149
150 #define QSM_SIGNAL_TM_MAX ( (qSM_SigId_t)0xFFFFFFFBUL )
151 #define QSM_SIGNAL_TM_MIN QSM_SIGNAL_TIMEOUT(0)
152
156 #define QSM_SIGNAL_USER ( QSM_SIGNAL_RANGE_MIN )
157
162
166 typedef struct _qSM_Signal_s { // skipcq: CXX-E2000
168 void *sigData;
169 } qSM_Signal_t;
170
183
194
195 /*fields for the qSM_Handler_t pointer*/
197 /*cppcheck-suppress misra-c2012-20.7 */
198 #define _qSM_HANDLER_FIELDS( pAttrib ) /* skipcq: CXX-E2000 */\
199 void *StartState; \
200 void *NextState; \
201 pAttrib void *machine; \
202 pAttrib void *state; \
203 pAttrib void *Data; \
204 pAttrib void *StateData; \
205 pAttrib void *SignalData; \
206 pAttrib qSM_SigId_t Signal; \
207 pAttrib qSM_Status_t Status; \
208 qSM_TransitionHistoryMode_t TransitionHistory \
209
212 typedef struct _qSM_uPublicData_s { // skipcq: CXX-E2000
213 _qSM_HANDLER_FIELDS( Q_NONE );
214 }
215 _qSM_UnprotectedPublicData_t; // skipcq: CXX-E2000
216 typedef struct _qSM_pPublicData_s { // skipcq: CXX-E2000
217 _qSM_HANDLER_FIELDS( const );
218 }
219 _qSM_ProtectedPublicData_t; // skipcq: CXX-E2000
220 typedef _qSM_UnprotectedPublicData_t* qSM_UnprotectedHandler_t;
221 typedef _qSM_ProtectedPublicData_t* qSM_Handler_t;
224 #ifdef DOXYGEN
232 typedef struct {
234 void *NextState;
235 const void *machine;
236 const void *state;
237 const void *Data;
238 const void *StateData;
239 const void *SignalData;
243 }
245 #endif
246
269
289
308
313
314
319 typedef struct _qSM_TimeoutStateDefinition_s { // skipcq: CXX-E2000
322 }
324
346 typedef struct _qSM_State_s { // skipcq: CXX-E2000
348 struct _qSM_State_Private_s { // skipcq: CXX-E2000
349 struct _qSM_State_s *parent; /*< A pointer to the parent state*/
350 struct _qSM_State_s *lastRunningChild; /*< The last running child state*/
351 struct _qSM_State_s *initState; /*< The initial state of this parent*/
352 qSM_StateCallback_t sCallback; /*< The state callback function*/
353 qSM_TimeoutStateDefinition_t *tdef; /*< The timeout state definition*/
354 void *tTable; /*< A pointer to the transition table.*/
355 void* sData; /*< State data. Storage pointer*/
356 size_t tEntries; /*< Number of entries on <tTable>*/
357 size_t nTm; /*< Number of entries on <tdef>*/
358 }
359 qPrivate;
361 }
363
368 typedef struct _qSM_TimeoutSpec_s { // skipcq: CXX-E2000
370 qUINT32_t isPeriodic; /*< witch one is periodic*/
371 qSTimer_t builtin_timeout[ Q_FSM_MAX_TIMEOUTS ]; /*< the built-in timeouts*/
373 }
375
387 typedef struct _qSM_s { // skipcq: CXX-E2000
389 struct _qSM_Private_s { // skipcq: CXX-E2000
390 qSM_State_t *current; /*< A pointer to the current state.*/
391 qSM_State_t *next; /*< The next state to execute. */
392 qSM_State_t *source; /*< Source state during the last transition*/
393 void *mData; /*< The user storage-pointer*/
394 #if ( Q_QUEUES == 1 )
395 qQueue_t *sQueue; /*< A pointer to the signal queue.*/
396 #endif
397 qSM_TimeoutSpec_t *timeSpec; /*< A pointer to the timeout specification object*/
398 qSM_SurroundingCallback_t surrounding; /*< A callback to handle actions after and before any state. */
399 qSM_State_t top; /*< The top most state*/
400 _qSM_UnprotectedPublicData_t handler; /*< The FSM handler argument*/
401 qSM_Signal_t signalNot; /*< Signal exclusion variable*/
402 void *owner;
403 }
404 qPrivate;
406 }
407 qSM_t;
408
421
437
450 qSM_Signal_t sig );
451
466 qSM_StateCallback_t topFcn,
467 qSM_State_t * const init,
469 void *pData );
470
488 qSM_State_t * const s,
489 qSM_State_t * const parent,
491 qSM_State_t * const init,
492 void *pData );
493
506 qSM_Transition_t * const table,
507 const size_t n );
508
519 qQueue_t *q );
520
547 qSM_SigId_t sig,
548 void *sData,
549 const qBool_t isUrgent );
550
566 qSM_TimeoutSpec_t * const ts );
567
586 const size_t n );
587
601 const qIndex_t xTimeout,
602 const qTime_t t );
603
616 const qIndex_t xTimeout );
617
625 void* qStateMachine_Get_Machine( qSM_t * const m,
626 const qSM_Attribute_t a );
627
635 void* qStateMachine_Get_State( const qSM_State_t * const s,
636 const qSM_Attribute_t a );
637
645 qSM_StateCallback_t sFcn );
646
655
665 qSM_SigId_t s );
666
676 qSM_SigId_t s );
677
680 #ifdef __cplusplus
681 }
682 #endif
683
684#endif
qFloat32_t qTime_t
The typedef that specified an time quantity, usually expressed in seconds.
Definition qclock.h:33
void * qStateMachine_Get_State(const qSM_State_t *const s, const qSM_Attribute_t a)
Get attributes from the provided state object.
Definition qfsm.c:910
qBool_t qStateMachine_Run(qSM_t *const m, qSM_Signal_t sig)
Execute the Finite State Machine (FSM).
Definition qfsm.c:486
qBool_t qStateMachine_Set_MachineSurrounding(qSM_t *const m, qSM_SurroundingCallback_t sFcn)
Set/Change the FSM surrounding callback in run-time.
Definition qfsm.c:954
qUINT32_t qSM_TimeoutSpecOptions_t
A typedef to hold the timeout specification options.
Definition qfsm.h:312
qSM_Attribute_t
This enumeration defines the attributes that can be acquired for a FSM or a state.
Definition qfsm.h:425
qUINT32_t qSM_SigId_t
The type for signal ID.
Definition qfsm.h:161
qBool_t qStateMachine_TimeoutStop(qSM_t *const m, const qIndex_t xTimeout)
Stop the time count for the selected built-in timeout.
Definition qfsm.c:848
qBool_t qStateMachine_InstallSignalQueue(qSM_t *const m, qQueue_t *q)
Install a signal queue to the provided Finite State Machine (FSM).
Definition qfsm.c:607
qBool_t qStateMachine_Set_StateTransitions(qSM_State_t *const s, qSM_Transition_t *const table, const size_t n)
Installs a table with the outgoing transitions for the supplied state.
Definition qfsm.c:592
qBool_t qStateMachine_UnsubscribeFromSignal(const qSM_t *const m, qSM_SigId_t s)
Unsubscribe state machine from a particular signal.
Definition qfsm.c:162
qBool_t qStateMachine_SubscribeToSignal(qSM_t *const m, qSM_SigId_t s)
Subscribe state machine to a particular signal.
Definition qfsm.c:130
qBool_t qStateMachine_TimeoutSet(qSM_t *const m, const qIndex_t xTimeout, const qTime_t t)
Set the time for the selected built-in timeout inside the target FSM.
Definition qfsm.c:833
qBool_t qStateMachine_StateSubscribe(qSM_t *const m, qSM_State_t *const s, qSM_State_t *const parent, qSM_StateCallback_t sFcn, qSM_State_t *const init, void *pData)
This function subscribes the FSM instance to a specific state with an associated callback function.
Definition qfsm.c:565
qBool_t qStateMachine_InstallTimeoutSpec(qSM_t *const m, qSM_TimeoutSpec_t *const ts)
Install the Timeout-specification object to target FSM to allow timed signals within states ( See the...
Definition qfsm.c:799
qSM_Status_t(* qSM_StateCallback_t)(qSM_Handler_t h)
Pointer to a function that represents a state callback.
Definition qfsm.h:268
void(* qSM_SurroundingCallback_t)(qSM_Handler_t h)
Pointer to a function that represents a surrounding callback.
Definition qfsm.h:288
qSM_TransitionHistoryMode_t
This enumeration defines the possible modes to perform a transition to history.
Definition qfsm.h:188
void * qStateMachine_Get_Machine(qSM_t *const m, const qSM_Attribute_t a)
Get attributes from the provided Finite State Machine object.
Definition qfsm.c:877
qBool_t qStateMachine_SendSignal(qSM_t *const m, qSM_SigId_t sig, void *sData, const qBool_t isUrgent)
Sends a signal to a specific state machine or to all its subscribers (if available).
Definition qfsm.c:693
qBool_t qStateMachine_Setup(qSM_t *const m, qSM_StateCallback_t topFcn, qSM_State_t *const init, qSM_SurroundingCallback_t sFcn, void *pData)
Initializes a Finite State Machine (FSM).
Definition qfsm.c:525
qBool_t qStateMachine_Set_StateCallback(qSM_State_t *const state, qSM_StateCallback_t sFcn)
Set/Change the state callback in run-time.
Definition qfsm.c:941
qBool_t(* qSM_SignalAction_t)(qSM_Handler_t h)
Pointer to a function that represents a signal guard/action.
Definition qfsm.h:307
qBool_t qStateMachine_Set_StateTimeouts(qSM_State_t *const s, qSM_TimeoutStateDefinition_t *tdef, const size_t n)
Setup fixed timeouts for the specified state using a lookup-table.
Definition qfsm.c:818
qSM_Status_t
This enumeration defines the built-in state-execution status values that can be used as return value ...
Definition qfsm.h:175
@ qSM_ATTRIB_STATE_CURRENT
Definition qfsm.h:427
@ qSM_ATTRIB_DATA
Definition qfsm.h:431
@ qSM_ATTRIB_TRANSITION_TABLE
Definition qfsm.h:430
@ qSM_ATTRIB_COMPOSITE_LASTSTATE
Definition qfsm.h:433
@ qSM_ATTRIB_SIGNAL_QUEUE
Definition qfsm.h:428
@ qSM_ATTRIB_STATE_TOP
Definition qfsm.h:426
@ qSM_ATTRIB_COMPOSITE_PARENT
Definition qfsm.h:434
@ qSM_ATTRIB_TIMESPEC
Definition qfsm.h:429
@ qSM_ATTRIB_COMPOSITE_INITSTATE
Definition qfsm.h:432
@ qSM_TRANSITION_DEEP_HISTORY
Definition qfsm.h:191
@ qSM_TRANSITION_SHALLOW_HISTORY
Definition qfsm.h:190
@ qSM_TRANSITION_NO_HISTORY
Definition qfsm.h:189
@ qSM_STATUS_NULL
Definition qfsm.h:177
@ qSM_STATUS_BEFORE_ANY
Definition qfsm.h:176
@ qSM_STATUS_EXIT_FAILURE
Definition qfsm.h:178
@ qSM_STATUS_EXIT_SUCCESS
Definition qfsm.h:179
@ qSM_STATUS_SIGNAL_HANDLED
Definition qfsm.h:180
qUINT8_t qBool_t
A type to instantiate an OS boolean variable.
Definition qtypes.h:139
size_t qIndex_t
A type to instantiate an OS index variable. Can store the maximum size of a theoretically possible ob...
Definition qtypes.h:146
uint32_t qUINT32_t
Unsigned integer type with width of exactly 32 bits respectively.
Definition qtypes.h:48
A Queue object.
Definition qqueues.h:53
The callback argument to handle the state-machine dynamics and provide execution information....
Definition qfsm.h:232
void * StartState
Definition qfsm.h:233
const qSM_SigId_t Signal
Definition qfsm.h:240
const void * StateData
Definition qfsm.h:238
const void * state
Definition qfsm.h:236
void * NextState
Definition qfsm.h:234
const void * SignalData
Definition qfsm.h:239
const qSM_Status_t Status
Definition qfsm.h:241
qSM_TransitionHistoryMode_t TransitionHistory
Definition qfsm.h:242
const void * machine
Definition qfsm.h:235
const void * Data
Definition qfsm.h:237
The type to be used as a container variable for a signal.
Definition qfsm.h:166
void * sigData
Definition qfsm.h:168
qSM_SigId_t id
Definition qfsm.h:167
A state object.
Definition qfsm.h:346
A FSM Timeout-specification object.
Definition qfsm.h:368
This structure should be used to define an item for a timeout-specification table.
Definition qfsm.h:319
qSM_TimeoutSpecOptions_t options
Definition qfsm.h:321
qTime_t xTimeout
Definition qfsm.h:320
This structure should be used to define an item for a state transition table.
Definition qfsm.h:413
void * signalData
Definition qfsm.h:418
qSM_SignalAction_t guard
Definition qfsm.h:415
qSM_State_t * nextState
Definition qfsm.h:416
qSM_SigId_t xSignal
Definition qfsm.h:414
qSM_TransitionHistoryMode_t historyMode
Definition qfsm.h:417
A FSM(Finite State Machine) object.
Definition qfsm.h:387
A STimer(Software Timer) object.
Definition qstimers.h:32