-
Notifications
You must be signed in to change notification settings - Fork 0
/
xbee.c
292 lines (220 loc) · 8.11 KB
/
xbee.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
//
// Created by filip on 4/27/17.
//
#include "lib.h"
int set_interface_attribs(int fd, int speed, int parity) {
struct termios tty;
memset(&tty, 0, sizeof tty);
if (tcgetattr(fd, &tty) != 0) {
perror("error from tcgetattr");
return -1;
}
cfsetospeed(&tty, speed);
cfsetispeed(&tty, speed);
tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8; // 8-bit chars
// disable IGNBRK for mismatched speed tests; otherwise receive break
// as \000 chars
tty.c_iflag &= ~IGNBRK; // disable break processing
tty.c_lflag = 0; // no signaling chars, npeni echo,
// no canonical processing
tty.c_oflag = 0; // no remapping, no delays
tty.c_cc[VMIN] = 0; // read doesn't block
tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout
tty.c_iflag &= ~(IXON | IXOFF | IXANY); // shut off xon/xoff ctrl
tty.c_cflag |= (CLOCAL | CREAD);// ignore modem controls,
// enable reading
tty.c_cflag &= ~(PARENB | PARODD); // hut off parity
tty.c_cflag |= parity;
tty.c_cflag &= ~CSTOPB;
tty.c_cflag &= ~CRTSCTS;
if (tcsetattr(fd, TCSANOW, &tty) != 0) {
perror("error from tcsetattr");
return -1;
}
return 0;
}
void set_blocking(int fd, int should_block) {
struct termios tty;
memset(&tty, 0, sizeof tty);
if (tcgetattr(fd, &tty) != 0) {
perror("error from tggetattr");
return;
}
tty.c_cc[VMIN] = should_block ? 1 : 0;
tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout
if (tcsetattr(fd, TCSANOW, &tty) != 0)
perror("error setting term attributes");
}
int myWrite(int fd, msg *m) {
int count = 0;
for (int i = 0; i < sizeof(msg); i++) {
int n = write(fd, (char *)m + i, 1);
if (n < 0) {
return -1;
}
count++;
}
return count;
}
int myRead(int fd, msg *m) {
int count = 0;
for (int i = 0; i < sizeof(msg); i++) {
int n = read(fd, (char*)m + i, 1);
if (n < 0) {
return -1;
}
count++;
}
return count;
}
int main(int argc, char **argv) {
char *portname = "/dev/tty";
char buffer[BUFLEN];
memset(buffer, 0, BUFLEN);
sprintf(buffer, "%s%s", portname, argv[1]);
int fd = open(buffer, O_RDWR | O_NOCTTY | O_SYNC);
if (fd < 0) {
perror("error opening");
return 0;
}
fd_set read_fds;
fd_set tmp_fds;
int fdmax;
FD_ZERO(&read_fds);
FD_ZERO(&tmp_fds);
FD_SET(0, &read_fds);
FD_SET(fd, &read_fds);
fdmax = fd;
set_interface_attribs(fd, B115200, 0);
set_blocking(fd, 0);
msg recv;
msg send;
unsigned char seq = 0;
unsigned char expected_seq;
while (true) {
tmp_fds = read_fds;
if (select(fdmax + 1, &tmp_fds, NULL, NULL, NULL) == -1) {
perror("ERROR in select\n");
exit(1);
}
for (int i = 0; i <= fdmax; i++) {
if (FD_ISSET(i, &tmp_fds)) {
/*
* Daca i == 0 atunci se citeste de la stdin.
*/
if (i == 0) {
memset(&send, 0, sizeof(msg));
fgets(send.buffer, BUFLEN, stdin);
send.buffer[strlen(send.buffer) - 1] = '\0';
/*
* Transorm primele 4 litere (lower), deoarece vreau sa
* functioneze comanda 'quit' sa nu fie 'case sensitive'.
*/
if (tolower(send.buffer[0]) == 'q'
&& tolower(send.buffer[1]) == 'u'
&& tolower(send.buffer[2]) == 'i'
&& tolower(send.buffer[3]) == 't') {
return 0;
}
if (tolower(send.buffer[0]) == 't'
&& tolower(send.buffer[1]) == 'e'
&& tolower(send.buffer[2]) == 's'
&& tolower(send.buffer[3]) == 't') {
send.type = TEST;
if (myWrite(fd, &send) < 0) {
printf("Eroare la trimitere. Se inchide conexiunea.\n");
return 0;
}
/*
* TODO VLAD
*
* Aici primesti mesaje generate random si numeri
* cate au CRC-ul bun. In functie de cate CRC bune ai
* afisezi un mesaj care iti zice cam cat de buna
* e conexiunea.
*/
continue;
}
send.type = DATA;
/*
* TODO NICA & NICA
*
* criptare inainte de clacul CRC
*/
send.CRC = crc16_ccitt(&send, BUFLEN + sizeof(char));
/*
* Pachetul cu mesajul citit de la tastatura este format, si
* va fi trimis pana voi primi ACK de la celalat capat.
*/
do {
if (myWrite(fd, &send) < 0) {
printf("Eroare la trimitere. Se inchide conexiunea.\n");
return 0;
}
memset(&recv, 0, sizeof(msg));
sleep(1);
if (myRead(fd, &recv) < 0) {
printf("Eroare la citire. Se inchide conexiunea.\n");
return 0;
}
unsigned short CRC = crc16_ccitt(&recv, BUFLEN + sizeof(char));
if (CRC == recv.CRC
&& recv.type == ACK
&& strncmp(recv.buffer, "ACK", 3) == 0) {
break;
}
} while (true);
}
/*
* Daca i == fd, atunci primesc un mesaj.
*/
if (i == fd) {
boolean succes = false;
do {
memset(&recv, 0, sizeof(msg));
if (myRead(fd, &recv) < 0) {
printf("Eroare la citire. Se inchide conexiunea.\n");
return 0;
}
unsigned short CRC = crc16_ccitt(&recv, BUFLEN + sizeof(char));
memset(&send, 0, sizeof(msg));
if(CRC == recv.CRC && recv.type != DATA){
break;
}
if (CRC == recv.CRC) {
send.type = ACK;
sprintf(send.buffer, "%s", "ACK");
succes = true;
} else {
send.type = NAK;
sprintf(send.buffer, "%s", "NAK");
}
send.CRC = crc16_ccitt(&send, BUFLEN + sizeof(char));
if (myWrite(fd, &send) < 0) {
printf("Eroare la trimitere. Se inchide conexiunea.\n");
return 0;
}
if(succes) {
break;
}
} while (!succes);
if (recv.type == TEST) {
/*
* TODO LUCA
*
* Aici trimiti mai multe mesaje pe care le generezi
* random.
*/
}
if (recv.type == DATA) {
/*
* TODO NICA & NICA
* decriptare
*/
printf("Am primit mesajul: %s\n", recv.buffer);
}
}
}
}
}
}