comparison 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
comparison
equal deleted inserted replaced
2:a4918a161d2e 3:de85c3680d7e
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 *out_binfile;
15
16 main_loop()
17 {
18 fd_set fds;
19 struct timeval tv;
20 u_char buf[320];
21 unsigned off;
22 int cc, is_active;
23
24 is_active = 0;
25 off = 0;
26 for (;;) {
27 FD_ZERO(&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 fflush(out_binfile);
44 printf("Rx stream stopped, buffer dribble = %u\n", off);
45 off = 0;
46 continue;
47 }
48 cc = read(target_fd, buf + off, sizeof(buf) - off);
49 if (cc < 0) {
50 perror("serial port read");
51 exit(1);
52 }
53 if (cc == 0) {
54 fprintf(stderr, "read EOF from serial port\n");
55 exit(1);
56 }
57 if (!is_active) {
58 printf("Rx stream started\n");
59 is_active = 1;
60 }
61 off += cc;
62 if (off >= sizeof(buf)) {
63 process_rx_block(buf);
64 off = 0;
65 }
66 }
67 }