diff --git a/src/stateMachine.c b/src/stateMachine.c index 25a8fc8..44d09e2 100644 --- a/src/stateMachine.c +++ b/src/stateMachine.c @@ -82,20 +82,40 @@ int stateM_handleEvent( struct stateMachine *fsm, while ( nextState->entryState ) nextState = nextState->entryState; - /* Run exit action only if the current state is left (only if it does + /* Run exit actions only if the current state is left (only if it does * not return to itself): */ - if ( nextState != fsm->currentState && fsm->currentState->exitAction ) - fsm->currentState->exitAction( fsm->currentState->data, event ); + if(nextState != fsm->currentState) + { + if ( fsm->currentState->exitAction ) + fsm->currentState->exitAction( fsm->currentState->data, event ); + + /* Call the current state's parent state exit action if it has one + * and if new parent state is different than the current states parent + * state */ + if ( fsm->currentState->parentState && fsm->currentState->parentState->entryAction && + (nextState->parentState != fsm->currentState->parentState) ) + fsm->currentState->parentState->exitAction( fsm->currentState->parentState->data, event ); + } /* Run transition action (if any): */ if ( transition->action ) transition->action( fsm->currentState->data, event, nextState-> data ); - /* Call the new state's entry action if it has any (only if state does + /* Call the new state's entry actions if it has any (only if state does * not return to itself): */ - if ( nextState != fsm->currentState && nextState->entryAction ) - nextState->entryAction( nextState->data, event ); + if(nextState != fsm->currentState) + { + /* Call the new state's parent state entry action if it has one + * and if its parent state is different than the current states parent + * state */ + if ( nextState->parentState && nextState->parentState->entryAction && + (nextState->parentState != fsm->currentState->parentState) ) + nextState->parentState->entryAction( nextState->parentState->data, event ); + + if ( nextState->entryAction ) + nextState->entryAction( nextState->data, event ); + } fsm->previousState = fsm->currentState; fsm->currentState = nextState;