-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfsm_func_ptr_entry_exit_origin.c
121 lines (102 loc) · 2.09 KB
/
fsm_func_ptr_entry_exit_origin.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <stdio.h>
#include <unistd.h>
// gcc fsm_func_ptr_entry_exit_origin.c -o fsm
struct FSM_T;
struct STATE_T
{
void (*_onEntry)( void* pPrivateData );
void (*_onDo)( struct FSM_T* pFsm );
void (*_onExit)( void* pPrivateData );
};
struct FSM_T
{
struct STATE_T* pCurrentState;
struct STATE_T* pNewState;
void* pPrivateData;
};
struct STATE_T statePong;
void fsmExecute( struct FSM_T* pFsm )
{
if( pFsm->pCurrentState != pFsm->pNewState )
{
pFsm->pNewState->_onEntry( pFsm->pPrivateData );
pFsm->pCurrentState = pFsm->pNewState;
}
pFsm->pCurrentState->_onDo( pFsm );
if( pFsm->pCurrentState != pFsm->pNewState )
pFsm->pCurrentState->_onExit( pFsm->pPrivateData );
}
void PingEntry( void* pPrivateData )
{
*(int*)pPrivateData = 3;
printf( "Function \"%s\"\n", __func__ );
}
void Ping( struct FSM_T* pFsm )
{
int* pCounter = pFsm->pPrivateData;
printf( "Function \"%s\"\n", __func__ );
if( *pCounter == 0 )
{
pFsm->pNewState = &statePong;
return;
}
(*pCounter)--;
}
void PingExit( void* pPrivateData )
{
printf( "Function \"%s\"\n", __func__ );
}
struct STATE_T statePing =
{
._onEntry = PingEntry,
._onDo = Ping,
._onExit = PingExit
};
void PongEntry( void* pPrivateData )
{
*(int*)pPrivateData = 3;
printf( "Function \"%s\"\n", __func__ );
}
void Pong( struct FSM_T* pFsm )
{
int* pCounter = pFsm->pPrivateData;
printf( "Function \"%s\"\n", __func__ );
if( *pCounter == 0 )
{
pFsm->pNewState = &statePing;
return;
}
(*pCounter)--;
}
void PongExit( void* pPrivateData )
{
printf( "Function \"%s\"\n", __func__ );
}
struct STATE_T statePong =
{
._onEntry = PongEntry,
._onDo = Pong,
._onExit = PongExit
};
int main( void )
{
int counter;
int i;
struct FSM_T myFsm =
{
.pNewState = &statePing,
.pCurrentState = NULL,
.pPrivateData = &counter
};
printf( "Test of %s\n", __FILE__ );
for( i = 0; i < 10; i++ )
{
do
{
fsmExecute( &myFsm );
sleep( 1 );
}
while( counter > 0 );
}
return 0;
}