-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStateMachine.c
153 lines (130 loc) · 3.33 KB
/
StateMachine.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
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <termios.h>
#include <unistd.h>
#define BAUDRATE B38400
#define _POSIX_SOURCE 1 // POSIX compliant source
#define FLAG 0x7E
#define A_SENDER 0x03
#define A_RECEIVER 0x01
#define C_SET 0x03
#define C_UA 0x07
int fd;
#define BUF_SIZE 1024
int state = 0;
unsigned char RECIEVED[5] = {0};
void verifyState(int *state, unsigned char REC) {
switch (*state) {
case 0:
printf("START\n");
if (REC == FLAG) {
*state = 1;
} else {
*state = 0;
}
break;
case 1:
printf("FLAG RCV\n");
if (REC == A_SENDER || REC == A_RECEIVER) {
*state = 2;
} else if (REC == FLAG) {
*state = 1;
} else {
*state = 0;
}
break;
case 2:
printf("A RACV\n");
if (REC == C_SET || REC == C_UA) {
*state = 3;
} else if (REC == FLAG) {
*state = 1;
} else {
*state = 0;
}
break;
case 3:
printf("C REV\n");
if (REC == (A_SENDER ^ C_SET) || REC == (A_RECEIVER ^ C_UA)) {
*state = 4;
} else if (REC == FLAG) {
*state = 1;
} else {
*state = 0;
}
break;
case 4:
printf("BCC OK\n");
if (REC == FLAG) {
*state = 5;
} else {
*state = 0;
}
break;
default:
printf("Invalid state\n");
*state = 0;
break;
}
}
void recievedFrame(unsigned char REC) {
verifyState(&state, REC);
if (state >= 0 && state < 5) { // Ensure the index is within the bounds of the array
RECIEVED[state] = REC;
}
}
int main(int argc, char *argv[]) {
if (argc < 2) {
printf("Incorrect program usage\n"
"Usage: %s <SerialPort>\n"
"Example: %s /dev/ttyS1\n",
argv[0],
argv[0]);
exit(1);
}
const char *serialPortName = argv[1];
fd = open(serialPortName, O_RDWR | O_NOCTTY);
if (fd < 0) {
perror(serialPortName);
exit(-1);
}
struct termios oldtio;
struct termios newtio;
if (tcgetattr(fd, &oldtio) == -1) {
perror("tcgetattr");
exit(-1);
}
memset(&newtio, 0, sizeof(newtio));
newtio.c_cflag = BAUDRATE | CS8 | CLOCAL | CREAD;
newtio.c_iflag = IGNPAR;
newtio.c_oflag = 0;
newtio.c_lflag = 0;
newtio.c_cc[VTIME] = 0;
newtio.c_cc[VMIN] = 1;
tcflush(fd, TCIOFLUSH);
if (tcsetattr(fd, TCSANOW, &newtio) == -1) {
perror("tcsetattr");
exit(-1);
}
printf("New termios structure set\n");
// Read data and process the frame
unsigned char buf[BUF_SIZE] = {0};
int bytes;
for (int i = 0; i < 1024 && state != 5; i++) {
bytes = read(fd, buf, 1);
if (bytes > 0) {
recievedFrame(buf[0]);
}
}
sleep(1);
if (tcsetattr(fd, TCSANOW, &oldtio) == -1) {
perror("tcsetattr");
exit(-1);
}
close(fd);
return 0;
}