#ifndef FINITESTATEMACHINE #define FINITESTATEMACHINE #include #include #include #include typedef struct statemachine_s StateMachine, *pStateMachine; typedef struct EvtEvent_s EvtEvent, *pEvtEvent; typedef void (*StateFunc)(pStateMachine self, pEvtEvent event); enum eventtype { eStateEvent, eTimerEvent, eMessageEvent, eCommandEvent, eTimeoutEvent }; typedef struct EvtState_s { } EvtState; typedef struct EvtTimer_s { int timerValue; } EvtTimer; typedef struct EvtMessage_s { pAsyncTxn cmd; } EvtMessage; typedef struct EvtCommand_s { const void* cmd_text; } EvtCommand; typedef struct EvtTimeout_s { } EvtTimeout; struct EvtEvent_s { enum eventtype event_type; union { EvtState sta; EvtTimer tmr; EvtMessage msg; EvtCommand cmd; EvtTimeout tmo; } event; }; struct statemachine_s { void* context; char* name; bool debug; StateFunc myState; int mySubState; StateFunc myPrevState; const char* (*state_name)(StateFunc func); const char* (*event_name)(pEvtEvent event, char* text, int length); }; char* fsm_textify(const char* pInp, int inp_len, char* pOut, int out_len); int fsm_msg_callback(pAsyncTxn pCmd); int fsm_tmr_callback(void* ctx, int mode); int fsm_cmd_execute(pStateMachine self, const void* cmd); void fsm_change_state(pStateMachine self, StateFunc func); void fsm_unhandled_event(pStateMachine self, pEvtEvent event); #endif /* FINITESTATEMACHINE */