comparison 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
comparison
equal deleted inserted replaced
6:a10657f8024e 7:8a386263dd51
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
15 main_loop()
16 {
17 fd_set fds;
18 struct timeval tv;
19 u_char buf[320];
20 unsigned off;
21 int cc, is_active;
22
23 is_active = 0;
24 off = 0;
25 for (;;) {
26 FD_ZERO(&fds);
27 FD_SET(0, &fds);
28 FD_SET(target_fd, &fds);
29 if (is_active) {
30 tv.tv_sec = 0;
31 tv.tv_usec = 100000;
32 cc = select(target_fd+1, &fds, 0, 0, &tv);
33 } else
34 cc = select(target_fd+1, &fds, 0, 0, 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 printf("Rx stream stopped, buffer dribble = %u\n", off);
44 off = 0;
45 continue;
46 }
47 if (FD_ISSET(0, &fds))
48 handle_tty_input();
49 if (FD_ISSET(target_fd, &fds)) {
50 cc = read(target_fd, buf + off, sizeof(buf) - off);
51 if (cc < 0) {
52 perror("serial port read");
53 exit(1);
54 }
55 if (cc == 0) {
56 fprintf(stderr, "read EOF from serial port\n");
57 exit(1);
58 }
59 if (!is_active) {
60 printf("Rx stream started\n");
61 is_active = 1;
62 }
63 off += cc;
64 if (off >= sizeof(buf)) {
65 process_rx_block(buf);
66 off = 0;
67 }
68 }
69 }
70 }