diff --git a/examples/fsm/fsm_test.c b/examples/fsm/fsm_test.c index cb6504c..7ae46b0 100644 --- a/examples/fsm/fsm_test.c +++ b/examples/fsm/fsm_test.c @@ -71,13 +71,19 @@ int entry_action_rx(const struct fsm_T *fsm, void *ptr) struct fsm_T fsm; +const int STATE_OUT = N_STATES; + int main() { int i; + int nState; + fsm_init(&fsm, actions, entry_actions, FSM_NO_ACTIONS, N_STATES, ST_INIT); for(i = 0; i < 10; i++) + while(nState != STATE_OUT) { fsm_step(&fsm, 0); + nState = fsm_get_state(&fsm); } return 0; } diff --git a/src/fsm/fsm.c b/src/fsm/fsm.c index 6b7990c..b05d0d7 100644 --- a/src/fsm/fsm.c +++ b/src/fsm/fsm.c @@ -19,6 +19,9 @@ #include "fsm.h" +#define OUT_OF_RANGE_STATE(state,min,max)\ + ((state=max)) + void fsm_init( struct fsm_T *fsm, const fsm_action_T *actions, @@ -58,10 +61,10 @@ int fsm_step(struct fsm_T *fsm, void *ptr) cur_state = fsm->state; next_state = cur_state; - - if((cur_state < 0) || (((unsigned int)cur_state) >= fsm->n_states)) + + if (OUT_OF_RANGE_STATE(cur_state,0,fsm->n_states)) { - return_val = FSM_INVALID_STATE; + return_val = FSM_INVALID_STATE; } else { @@ -74,8 +77,9 @@ int fsm_step(struct fsm_T *fsm, void *ptr) } else { + /* Why not test entry_action result before action ? */ next_state = action(fsm, ptr); - if((next_state < 0) || (((unsigned int)next_state) >= fsm->n_states)) + if (OUT_OF_RANGE_STATE(next_state,0,fsm->n_states)) { return_val = FSM_INVALID_NEXT_STATE; next_state = cur_state;