annotate sw/mcsi-rxtx/mainloop.c @ 7:8a386263dd51

fc-mcsi-rxtx skeleton put together
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 28 Oct 2024 01:44:28 +0000
parents sw/mcsi-rx/mainloop.c@de85c3680d7e
children 62579cfff4fc
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
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 main_loop()
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 {
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 fd_set fds;
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 struct timeval tv;
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 u_char buf[320];
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 unsigned off;
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 int cc, is_active;
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 is_active = 0;
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 off = 0;
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 for (;;) {
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 FD_ZERO(&fds);
7
8a386263dd51 fc-mcsi-rxtx skeleton put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 3
diff changeset
27 FD_SET(0, &fds);
3
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 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
44 off = 0;
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 continue;
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 }
7
8a386263dd51 fc-mcsi-rxtx skeleton put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 3
diff changeset
47 if (FD_ISSET(0, &fds))
8a386263dd51 fc-mcsi-rxtx skeleton put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 3
diff changeset
48 handle_tty_input();
8a386263dd51 fc-mcsi-rxtx skeleton put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 3
diff changeset
49 if (FD_ISSET(target_fd, &fds)) {
8a386263dd51 fc-mcsi-rxtx skeleton put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 3
diff changeset
50 cc = read(target_fd, buf + off, sizeof(buf) - off);
8a386263dd51 fc-mcsi-rxtx skeleton put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 3
diff changeset
51 if (cc < 0) {
8a386263dd51 fc-mcsi-rxtx skeleton put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 3
diff changeset
52 perror("serial port read");
8a386263dd51 fc-mcsi-rxtx skeleton put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 3
diff changeset
53 exit(1);
8a386263dd51 fc-mcsi-rxtx skeleton put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 3
diff changeset
54 }
8a386263dd51 fc-mcsi-rxtx skeleton put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 3
diff changeset
55 if (cc == 0) {
8a386263dd51 fc-mcsi-rxtx skeleton put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 3
diff changeset
56 fprintf(stderr, "read EOF from serial port\n");
8a386263dd51 fc-mcsi-rxtx skeleton put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 3
diff changeset
57 exit(1);
8a386263dd51 fc-mcsi-rxtx skeleton put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 3
diff changeset
58 }
8a386263dd51 fc-mcsi-rxtx skeleton put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 3
diff changeset
59 if (!is_active) {
8a386263dd51 fc-mcsi-rxtx skeleton put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 3
diff changeset
60 printf("Rx stream started\n");
8a386263dd51 fc-mcsi-rxtx skeleton put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 3
diff changeset
61 is_active = 1;
8a386263dd51 fc-mcsi-rxtx skeleton put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 3
diff changeset
62 }
8a386263dd51 fc-mcsi-rxtx skeleton put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 3
diff changeset
63 off += cc;
8a386263dd51 fc-mcsi-rxtx skeleton put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 3
diff changeset
64 if (off >= sizeof(buf)) {
8a386263dd51 fc-mcsi-rxtx skeleton put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 3
diff changeset
65 process_rx_block(buf);
8a386263dd51 fc-mcsi-rxtx skeleton put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 3
diff changeset
66 off = 0;
8a386263dd51 fc-mcsi-rxtx skeleton put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 3
diff changeset
67 }
3
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 }
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 }
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 }