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