annotate sw/mcsi-rx/mainloop.c @ 3:de85c3680d7e

sw: fc-mcsi-rx program put together
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 11 Oct 2024 23:54:39 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * This module holds our main loop code, factored out into a separate
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 * function that is called from main() after initialization.
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 */
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 #include <sys/types.h>
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include <sys/time.h>
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <sys/errno.h>
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <stdio.h>
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 #include <stdlib.h>
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 #include <unistd.h>
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 extern int target_fd;
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 extern FILE *out_binfile;
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 main_loop()
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 {
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 fd_set fds;
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 struct timeval tv;
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 u_char buf[320];
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 unsigned off;
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 int cc, is_active;
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 is_active = 0;
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 off = 0;
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 for (;;) {
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 FD_ZERO(&fds);
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 FD_SET(target_fd, &fds);
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 if (is_active) {
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 tv.tv_sec = 0;
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 tv.tv_usec = 100000;
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 cc = select(target_fd+1, &fds, 0, 0, &tv);
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 } else
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 cc = select(target_fd+1, &fds, 0, 0, 0);
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 if (cc < 0) {
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 if (errno == EINTR)
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 continue;
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 perror("select");
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 exit(1);
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 }
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 if (cc == 0) {
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 is_active = 0;
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 fflush(out_binfile);
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 printf("Rx stream stopped, buffer dribble = %u\n", off);
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 off = 0;
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 continue;
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 }
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 cc = read(target_fd, buf + off, sizeof(buf) - off);
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 if (cc < 0) {
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 perror("serial port read");
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 exit(1);
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 }
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 if (cc == 0) {
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 fprintf(stderr, "read EOF from serial port\n");
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 exit(1);
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 }
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 if (!is_active) {
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 printf("Rx stream started\n");
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 is_active = 1;
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 }
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 off += cc;
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 if (off >= sizeof(buf)) {
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 process_rx_block(buf);
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 off = 0;
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 }
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 }
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 }