#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
enum eventTypes
{
Event_dummy,
};
struct eventPayload
{
char data;
const char *expectedState;
};
static void entryAction(
void *stateData,
struct event *
event );
static void exitAction(
void *stateData,
struct event *
event );
static void transAction(
void *oldStateData,
struct event *
event,
void *newStateData );
static bool guard(
void *condition,
struct event *
event );
static struct state s1, s2, s3, s4, s5, s6, s9, s10, s11, sE;
s1 =
{
{ Event_dummy, (
void *)(intptr_t)
'd', &
guard, &transAction, &s3 },
},
.numTransitions = 1,
.parentState = &s9,
},
s2 =
{
.data = "2",
.entryAction = &entryAction,
.exitAction = &exitAction,
{ Event_dummy, (
void *)(intptr_t)
'b', &
guard, &transAction, &s1 },
},
},
s3 =
{
.data = "3",
.entryAction = &entryAction,
.exitAction = &exitAction,
{ Event_dummy, (
void *)(intptr_t)
'e', &
guard, &transAction, &s11 },
},
.numTransitions = 1,
.parentState = &s10,
},
s4 =
{
.data = "4",
.entryAction = &entryAction,
.exitAction = &exitAction,
{ Event_dummy, (
void *)(intptr_t)
'h', &
guard, &transAction, &s5 },
{ Event_dummy, (
void *)(intptr_t)
'j', &
guard, &transAction, &s9 },
},
.numTransitions = 2,
.parentState = &s11,
},
s5 =
{
.data = "5",
.entryAction = &entryAction,
.exitAction = &exitAction,
{ Event_dummy, NULL, NULL, &transAction, &s10 },
},
.numTransitions = 1,
.parentState = &s11,
},
s6 =
{
.data = "6",
.entryAction = &entryAction,
.exitAction = &exitAction,
},
s9 =
{
.data = "9",
.entryAction = &entryAction,
.exitAction = &exitAction,
.entryState = &s4,
{ Event_dummy, (
void *)(intptr_t)
'a', &
guard, &transAction, &s3 },
},
.numTransitions = 1,
},
s10 =
{
.data = "10",
.entryAction = &entryAction,
.exitAction = &exitAction,
.entryState = &s9,
{ Event_dummy, (
void *)(intptr_t)
'f', &
guard, &transAction, &s2 },
{ Event_dummy, (
void *)(intptr_t)
'i', &
guard, &transAction, &s6 },
},
.numTransitions = 2,
.parentState = &s9,
},
s11 =
{
.data = "11",
.entryAction = &entryAction,
.exitAction = &exitAction,
.entryState = &s5,
{ Event_dummy, (
void *)(intptr_t)
'g', &
guard, &transAction, &s2 },
},
.numTransitions = 1,
.parentState = &s10,
},
sE =
{
.data = "ERROR",
.entryAction = &entryAction,
};
int main()
{
struct event events[] = {
{ Event_dummy, &(struct eventPayload){ 'd', "3" } },
{ Event_dummy, &(struct eventPayload){ 'e', "5" } },
{ Event_dummy, &(struct eventPayload){ '*', "4" } },
{ Event_dummy, &(struct eventPayload){ 'j', "4" } },
{ Event_dummy, &(struct eventPayload){ 'g', "2" } },
{ Event_dummy, &(struct eventPayload){ 'b', "1" } },
{ Event_dummy, &(struct eventPayload){ 'd', "3" } },
{ Event_dummy, &(struct eventPayload){ 'e', "5" } },
{ Event_dummy, &(struct eventPayload){ 'k', "4" } },
{ Event_dummy, &(struct eventPayload){ 'h', "5" } },
{ Event_dummy, &(struct eventPayload){ '*', "4" } },
{ Event_dummy, &(struct eventPayload){ 'f', "2" } },
{ Event_dummy, &(struct eventPayload){ 'b', "1" } },
{ Event_dummy, &(struct eventPayload){ 'a', "3" } },
{ Event_dummy, &(struct eventPayload){ 'f', "2" } },
{ Event_dummy, &(struct eventPayload){ 'b', "1" } },
{ Event_dummy, &(struct eventPayload){ 'd', "3" } },
{ Event_dummy, &(struct eventPayload){ 'i', "6" } },
};
int res;
size_t i;
for ( i = 0; i < sizeof( events ) / sizeof( events[ 0 ] ) - 1; ++i )
{
{
if ( i == 0 )
{
fputs( "Internal error. This should not happen.\n", stderr );
exit( 4 );
}
if ( !strcmp( ( (struct eventPayload *)events[ i ].data
)->expectedState, ((struct eventPayload *)events[ i - 1 ]
.data )->expectedState ) )
puts( "State changed back to itself" );
else
{
fputs( "State unexpectedly changed back to itself", stderr );
exit( 5 );
}
}
{
fprintf( stderr, "Unexpected return value from stateM_handleEvent:"
" %d\n", res );
exit( 2 );
}
}
{
fprintf( stderr, "Unexpected return value from stateM_handleEvent:"
" %d\n", res );
exit( 3 );
}
puts( "A final state was reached (as expected)" );
return 0;
}
static void entryAction(
void *stateData,
struct event *
event )
{
const char *stateName = (const char *)stateData;
printf( "Entering %s\n", stateName );
}
static void exitAction(
void *stateData,
struct event *
event )
{
const char *stateName = (const char *)stateData;
printf( "Exiting %s\n", stateName );
}
static void transAction(
void *oldStateData,
struct event *
event,
void *newStateData )
{
struct eventPayload *eventData = (
struct eventPayload *)event->
data;
printf( "Event '%c'\n", eventData->data );
if ( strcmp( ( (const char *)newStateData ), eventData->expectedState ) )
{
fprintf( stderr, "Unexpected state transition (to %s)\n",
(const char *)newStateData );
exit( 1 );
}
}
static bool guard( void *condition, struct event *event )
{
struct eventPayload *eventData = (
struct eventPayload *)event->
data;
return (intptr_t )condition == (intptr_t)eventData->data;
}