Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions examples/fsm/fsm_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
12 changes: 8 additions & 4 deletions src/fsm/fsm.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@

#include "fsm.h"

#define OUT_OF_RANGE_STATE(state,min,max)\
((state<min)||(((unsigned int)state)>=max))

void fsm_init(
struct fsm_T *fsm,
const fsm_action_T *actions,
Expand Down Expand Up @@ -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
{
Expand All @@ -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;
Expand Down