-
Notifications
You must be signed in to change notification settings - Fork 3
/
fsm.c
228 lines (188 loc) · 5.69 KB
/
fsm.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
//
// fsm.c
// FSM sample code
//
// Created by Minsuk Lee, 2014.11.1.
// Copyright (c) 2014. Minsuk Lee All rights reserved.
// see LICENSE
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <sys/errno.h>
#define TEST_FSM
#define NUM_STATE 3
#define NUM_EVENT 8
enum pakcet_type { F_CON, F_ACK, F_FIN, F_DATA }; // Packet Type
enum proto_state { wait_CON, CON_sent, CONNECTED }; // States
// Events
enum proto_event { RCV_CON, RCV_FIN, RCV_ACK, RCV_DATA, CONNECT, CLOSE, SEND, TIMEOUT };
char *pkt_name[] = { "F_CON", "F_ACK", "F_FIN", "F_DATA" };
char *st_name[] = { "wait_CON", "CON_sent", "CONNECTED" };
char *ev_name[] = { "RCV_CON", "RCV_FIN", "RCV_ACK", "RCV_DATA",
"CONNECT", "CLOSE", "SEND", "TIMEOUT" };
struct state_action { // Protocol FSM Structure
void (* action)(void *p);
enum proto_state next_state;
};
struct p_event { // Event Structure
enum proto_event event;
void *data;
int size;
};
#define MAX_DATA_SIZE (500)
struct packet { // 504 Byte Packet to & from Simulator
unsigned short type; // enum packet_type
unsigned short size;
char data[MAX_DATA_SIZE];
};
enum proto_state c_state = wait_CON; // Initial State
volatile int timedout = 0;
static void timer_handler(int signum)
{
printf("Timedout\n");
timedout = 1;
}
static void timer_init(void)
{
struct sigaction sa;
memset (&sa, 0, sizeof (sa));
sa.sa_handler = &timer_handler;
sigaction(SIGALRM, &sa, NULL);
}
void set_timer(int sec)
{
struct itimerval timer;
timedout = 0;
timer.it_value.tv_sec = sec;
timer.it_value.tv_usec = 0;
timer.it_interval.tv_sec = 0; // Non Periodic timer
timer.it_interval.tv_usec = 0;
setitimer (ITIMER_REAL, &timer, NULL);
}
void send_packet(int flag, void *p, int size)
{
#ifdef TEST_FSM
printf("SEND %s\n", pkt_name[flag]);
#else
struct packet buf;
buf.type = flag;
if (size) {
buf.size = size;
memcpy(buf.data, p, (size > MAX_DATA_SIZE) ? MAX_DATA_SIZE : size);
}
//---> sendto() to SIMULATOR
#endif
}
static void report_connect(void *p)
{
printf("Connected\n");
set_timer(0); // Stop Timer
// Report connection made to upper layer
}
static void passive_con(void *p)
{
send_packet(F_ACK, NULL, 0);
report_connect(NULL);
}
static void active_con(void *p)
{
send_packet(F_CON, NULL, 0);
set_timer(3);
}
static void close_con(void *p)
{
printf("Connection Closed\n");
send_packet(F_FIN, NULL, 0);
// Report Connected Closed to upper layer
}
static void send_data(void *p)
{
printf("Send Data to peer size:%d\n", ((struct p_event*)p)->size);
send_packet(F_DATA, ((struct p_event *)p)->data, ((struct p_event *)p)->size);
}
static void report_data(void *p)
{
printf("Data Arrived size:%d\n", ((struct p_event*)p)->size);
// Queue received data for upper layer user
}
struct state_action p_FSM[NUM_STATE][NUM_EVENT] = {
// for each event:
// RCV_CON, RCV_FIN, RCV_ACK, RCV_DATA,
// CONNECT, CLOSE, SEND, TIMEOUT
// - wait_CON state
{{ passive_con, CONNECTED }, { NULL, wait_CON }, { NULL, wait_CON }, { NULL, wait_CON },
{ active_con, CON_sent }, { NULL, wait_CON }, { NULL, wait_CON }, { NULL, wait_CON }},
// - CON_sent state
{{ passive_con, CONNECTED }, { close_con, wait_CON }, { report_connect, CONNECTED }, { NULL, CON_sent },
{ NULL, CON_sent }, { close_con, wait_CON }, { NULL, CON_sent }, { close_con, wait_CON }},
// - CONNECTED state
{{ NULL, CONNECTED }, { close_con, wait_CON }, { NULL, CONNECTED }, { report_data, CONNECTED },
{ NULL, CONNECTED }, { close_con, wait_CON }, { send_data, CONNECTED }, { NULL, CONNECTED }},
};
struct p_event *get_event(void)
{
static struct p_event event; // not thread-safe
#ifdef TEST_FSM
char test_event[10];
int i;
event.data = NULL;
event.size = 0;
loop:
while (scanf("%s", test_event) <= 0) {
// scanf() returns error on signal (timer)
if ((errno == EINTR) && timedout) { // ALARM signal Catched !!
timedout = 0;
i = TIMEOUT;
goto got_it;
}
}
for (i = 0; i < NUM_EVENT; i++)
if (!strcasecmp(test_event, ev_name[i]))
goto got_it;
if (!strcasecmp(test_event, "quit"))
return NULL;
fprintf(stderr, "%s : BAD EVENT NAME\n", test_event);
goto loop;
got_it:
event.event = i;
// No data
#else
// Check if there is user command
// Check Packet arrival by event_wait()
// if then, decode header to make event
// Check if timer is timed-out
#endif
return &event;
}
void
Protocol_Loop(void)
{
struct p_event *eventp;
timer_init();
while (1) {
printf("Current Stat = %s\n", st_name[c_state]);
/* Step 0: Get Input Event */
if((eventp = get_event()) == NULL)
break;
/* Step 1: Do Action */
if (p_FSM[c_state][eventp->event].action)
p_FSM[c_state][eventp->event].action(eventp);
else
printf("No Action for this event\n");
/* Step 2: Set Next State */
c_state = p_FSM[c_state][eventp->event].next_state;
}
}
int
main(int argc, char *argv[])
{
// INITIALIZE USER THREAD
// SIMULATOR_INITIALIZE
printf("Entering protocol loop...\n");
printf("type 'CONNECT', 'CLOSE', 'SEND', or 'QUIT'\n");
Protocol_Loop();
// SIMULATOR_CLOSE
return 0;
}