@@ -13,31 +13,32 @@ inline bool FSM_IsValidState(const TState *const state) {
1313};
1414
1515const TState * FSM_ProcessEventToNextState (
16- TActiveObject * const activeObject ,
17- TEvent event ,
18- uint32_t statesMax ,
19- uint32_t eventsMax ,
20- const TState statesList [statesMax ],
21- const TEventHandler transitionTable [statesMax ][eventsMax ]) {
16+ TActiveObject * const activeObject ,
17+ TEvent event ,
18+ uint32_t statesMax ,
19+ uint32_t eventsMax ,
20+ const TState statesList [statesMax ],
21+ const TEventHandler transitionTable [statesMax ][eventsMax ]) {
2222
23- /* Validate input args */
23+ /* Validate input args */
2424
2525 // Validate Active object
26- if (NULL == activeObject
27- || NULL == statesList
28- || NULL == transitionTable ) return & invalidState ; // Handle null pointers as needed
26+ if (NULL == activeObject
27+ || NULL == statesList
28+ || NULL == transitionTable )
29+ return & invalidState ; // Handle null pointers as needed
2930
3031 const TState * const currState = activeObject -> state ;
31-
32+
3233 // Validate current state
3334 if (currState -> name < 0 || currState -> name >= statesMax ) return & invalidState ;
34-
35+
3536 // Validate current event
3637 if (event .sig < 0 || event .sig >= eventsMax ) return & invalidState ;
3738
3839 // Lookup transition table to find the handler for the current state and event
3940 const TEventHandler eventHandler = transitionTable [currState -> name ][event .sig ];
40-
41+
4142 // Call the handler to get the next state and make side effects
4243 if (eventHandler ) {
4344 const TState * nextState = eventHandler (activeObject , event );
@@ -46,49 +47,48 @@ const TState *FSM_ProcessEventToNextState(
4647 }
4748
4849 // Return empty state if no transition exists
49- return & emptyState ;
50+ return & emptyState ;
5051};
5152
52- bool FSM_TraverseNextState (
53- TActiveObject * const activeObject ,
54- const TState * const nextState ) {
53+ // Execute a state hook and return its status.
54+ bool _executeHook (TStateHook hook , TActiveObject * activeObject ) {
55+ if (hook ) {
56+ return hook (activeObject , NULL );
57+ }
58+ return true; // No hook to execute, so consider it successful
59+ }
5560
56- // Null args checks
57- if (NULL == activeObject
58- || NULL == nextState ) {
61+ bool FSM_TraverseNextState (
62+ TActiveObject * const activeObject ,
63+ const TState * const nextState ) {
64+ // Null args checks
65+ if (!activeObject || !nextState ) {
5966 return false;
60- };
67+ }
6168
6269 // Validate next state
6370 if (!FSM_IsValidState (nextState )) {
6471 return false;
65- };
66-
67- // Call onExit of current state, if any
68- if (NULL != activeObject -> state -> onExit ) {
69- bool isSuccessfulHook = activeObject -> state -> onExit (activeObject , NULL );
70- if (!isSuccessfulHook ) {
71- return false; // Hook failed
72- }
7372 }
7473
75- // Update the state
76- activeObject -> state = (TState * )nextState ;
77-
78- // Call onEnter of next state, if any
79- if (activeObject -> state -> onEnter ) {
80- bool isSuccessfulHook = activeObject -> state -> onEnter (activeObject , NULL );
81- if (!isSuccessfulHook ) {
82- return false; // Hook failed
83- }
74+ const TState * currState = & (* activeObject -> state );
75+
76+ // If the next state is the same as the current state
77+ if (FSM_IsEqualStates (currState , nextState )) {
78+ return _executeHook (currState -> onTraverse , activeObject );
79+ }
80+
81+ // If the next state is different, execute hooks for transitioning
82+ if (!_executeHook (currState -> onExit , activeObject )) {
83+ return false;
8484 }
8585
86- // Finally, call onTraverse of next state, if any
87- if ( activeObject -> state -> onTraverse ) {
88- bool isSuccessfulHook = activeObject -> state -> onTraverse ( activeObject , NULL );
89- if (!isSuccessfulHook ) {
90- return false; // Hook failed
91- }
86+ // Update the state
87+ activeObject -> state = ( TState * ) nextState ;
88+
89+ if (!_executeHook ( activeObject -> state -> onEnter , activeObject ) ||
90+ ! _executeHook ( activeObject -> state -> onTraverse , activeObject )) {
91+ return false;
9292 }
9393
9494 return true;
0 commit comments