FreeCalypso > hg > fc-sim-sniff
comparison sw/sniff-rx/mainloop.c @ 22:b112c2df6c43
sw: simtrace3-sniff-rx program written, compiles
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Tue, 22 Aug 2023 06:16:44 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
21:c4346cdc9641 | 22:b112c2df6c43 |
---|---|
1 /* | |
2 * This module holds our main loop code, factored out into a separate | |
3 * function that is called from main() after initialization. | |
4 */ | |
5 | |
6 #include <sys/types.h> | |
7 #include <sys/time.h> | |
8 #include <sys/errno.h> | |
9 #include <stdio.h> | |
10 #include <stdlib.h> | |
11 #include <unistd.h> | |
12 | |
13 extern int target_fd; | |
14 extern FILE *main_outf; | |
15 | |
16 struct timeval curtime; | |
17 | |
18 main_loop() | |
19 { | |
20 fd_set fds; | |
21 struct timeval tv; | |
22 u_char buf[512]; | |
23 int cc, i, is_active; | |
24 | |
25 for (is_active = 0; ; ) { | |
26 FD_ZERO(&fds); | |
27 FD_SET(target_fd, &fds); | |
28 if (is_active) { | |
29 tv.tv_sec = 1; | |
30 tv.tv_usec = 0; | |
31 cc = select(target_fd+1, &fds, 0, 0, &tv); | |
32 } else | |
33 cc = select(target_fd+1, &fds, 0, 0, 0); | |
34 gettimeofday(&curtime, 0); | |
35 if (cc < 0) { | |
36 if (errno == EINTR) | |
37 continue; | |
38 perror("select"); | |
39 exit(1); | |
40 } | |
41 if (cc == 0) { | |
42 is_active = 0; | |
43 fflush(main_outf); | |
44 flush_pending_byte(); | |
45 continue; | |
46 } | |
47 cc = read(target_fd, buf, sizeof buf); | |
48 if (cc < 0) { | |
49 perror("serial port read"); | |
50 exit(1); | |
51 } | |
52 if (cc == 0) { | |
53 fprintf(stderr, "read EOF from serial port\n"); | |
54 exit(1); | |
55 } | |
56 current_date_time(); | |
57 for (i = 0; i < cc; i++) | |
58 rx_byte_in(buf[i]); | |
59 is_active = 1; | |
60 } | |
61 } |