annotate sw/sniff-rx/initflush.c @ 51:8a3003860cf8

doc/Sniffing-hw-setup: wire assignments for mv-sniffer setup
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 24 Sep 2023 01:15:44 +0000
parents b112c2df6c43
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 * FTDI chip+driver combo (specifically the combination of FTDI chips and
b112c2df6c43 sw: simtrace3-sniff-rx program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 * ftdi_sio driver in Linux, not sure who is the actual culprit) exhibits
b112c2df6c43 sw: simtrace3-sniff-rx program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 * this unpleasant behaviour: even though we request "please flush all
b112c2df6c43 sw: simtrace3-sniff-rx program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 * previous input" when we set our termios params, old accumulated serial
b112c2df6c43 sw: simtrace3-sniff-rx program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 * Rx bytes still remain in some buffer somewhere, and a newly started
b112c2df6c43 sw: simtrace3-sniff-rx program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 * serial application receives this stale garbage. As a workaround,
b112c2df6c43 sw: simtrace3-sniff-rx program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 * we do an additional flush of our own: we put the fd in non-blocking mode
b112c2df6c43 sw: simtrace3-sniff-rx program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 * and keep reading and discarding data until we get EAGAIN or EWOULDBLOCK.
b112c2df6c43 sw: simtrace3-sniff-rx program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 */
b112c2df6c43 sw: simtrace3-sniff-rx program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11
b112c2df6c43 sw: simtrace3-sniff-rx program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 #include <sys/types.h>
b112c2df6c43 sw: simtrace3-sniff-rx program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 #include <sys/errno.h>
b112c2df6c43 sw: simtrace3-sniff-rx program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 #include <stdio.h>
b112c2df6c43 sw: simtrace3-sniff-rx program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 #include <stdlib.h>
b112c2df6c43 sw: simtrace3-sniff-rx program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 #include <unistd.h>
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 extern int target_fd;
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 void
b112c2df6c43 sw: simtrace3-sniff-rx program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 init_serial_flush()
b112c2df6c43 sw: simtrace3-sniff-rx program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 {
b112c2df6c43 sw: simtrace3-sniff-rx program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 u_char buf[512];
b112c2df6c43 sw: simtrace3-sniff-rx program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 int cc;
b112c2df6c43 sw: simtrace3-sniff-rx program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25
b112c2df6c43 sw: simtrace3-sniff-rx program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 set_serial_nonblock(1);
b112c2df6c43 sw: simtrace3-sniff-rx program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 for (;;) {
b112c2df6c43 sw: simtrace3-sniff-rx program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 cc = read(target_fd, buf, sizeof buf);
b112c2df6c43 sw: simtrace3-sniff-rx program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 if (cc <= 0)
b112c2df6c43 sw: simtrace3-sniff-rx program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 break;
b112c2df6c43 sw: simtrace3-sniff-rx program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 }
b112c2df6c43 sw: simtrace3-sniff-rx program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 if (cc == 0) {
b112c2df6c43 sw: simtrace3-sniff-rx program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 fprintf(stderr,
b112c2df6c43 sw: simtrace3-sniff-rx program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 "read EOF from serial port during initial flush\n");
b112c2df6c43 sw: simtrace3-sniff-rx program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 exit(1);
b112c2df6c43 sw: simtrace3-sniff-rx program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 }
b112c2df6c43 sw: simtrace3-sniff-rx program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 if (errno == EAGAIN || errno == EWOULDBLOCK)
b112c2df6c43 sw: simtrace3-sniff-rx program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 return; /* success */
b112c2df6c43 sw: simtrace3-sniff-rx program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 perror("serial port read");
b112c2df6c43 sw: simtrace3-sniff-rx program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 exit(1);
b112c2df6c43 sw: simtrace3-sniff-rx program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 }