annotate frbl/test/ttypassthru.c @ 370:844f884e0171

pirelli/vibrator-ctrl: additional observations
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 29 May 2021 18:23:34 +0000
parents cefa700d1b8f
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
323
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * This module implements the pass-thru operation mode, in which
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 * the Unix host tty is cross-connected directly to the target
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 * running some code we have just loaded.
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 */
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include <sys/types.h>
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <sys/ioctl.h>
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <sys/errno.h>
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 #include <termios.h>
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 #include <stdio.h>
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 #include <stdlib.h>
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 #include <unistd.h>
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 #include <strings.h>
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 extern int errno;
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 extern int target_fd;
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 static struct termios saved_termios, my_termios;
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 static void
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 safe_output(buf, cc)
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 u_char *buf;
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 {
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 int i, c;
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 for (i = 0; i < cc; i++) {
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 c = buf[i];
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 if (c == '\r' || c == '\n' || c == '\t' || c == '\b') {
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 putchar(c);
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 continue;
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 }
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 if (c & 0x80) {
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 putchar('M');
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 putchar('-');
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 c &= 0x7F;
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 }
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 if (c < 0x20) {
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 putchar('^');
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 putchar(c + '@');
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 } else if (c == 0x7F) {
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 putchar('^');
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 putchar('?');
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 } else
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 putchar(c);
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 }
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 fflush(stdout);
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 }
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 static void
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 loop()
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 {
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 char buf[BUFSIZ];
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 fd_set fds, fds1;
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 register int i, cc, max;
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 FD_ZERO(&fds);
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 FD_SET(0, &fds);
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 FD_SET(target_fd, &fds);
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 max = target_fd + 1;
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 for (;;) {
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 bcopy(&fds, &fds1, sizeof(fd_set));
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 i = select(max, &fds1, NULL, NULL, NULL);
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 if (i < 0) {
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 if (errno == EINTR)
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 continue;
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 tcsetattr(0, TCSAFLUSH, &saved_termios);
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 perror("select");
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 exit(1);
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 }
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 if (FD_ISSET(0, &fds1)) {
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 cc = read(0, buf, sizeof buf);
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 if (cc <= 0)
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 return;
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 if (cc == 1 && buf[0] == 0x1C)
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
77 return;
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78 write(target_fd, buf, cc);
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
79 }
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
80 if (FD_ISSET(target_fd, &fds1)) {
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
81 cc = read(target_fd, buf, sizeof buf);
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
82 if (cc <= 0) {
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
83 tcsetattr(0, TCSAFLUSH, &saved_termios);
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
84 fprintf(stderr, "EOF/error on target tty\n");
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
85 exit(1);
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
86 }
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
87 safe_output(buf, cc);
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
88 }
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
89 }
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
90 }
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
91
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
92 tty_passthru()
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
93 {
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
94 static int zero = 0;
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
95
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
96 ioctl(target_fd, FIONBIO, &zero);
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
97
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
98 tcgetattr(0, &saved_termios);
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
99 bcopy(&saved_termios, &my_termios, sizeof(struct termios));
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
100 cfmakeraw(&my_termios);
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
101 my_termios.c_cc[VMIN] = 1;
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
102 my_termios.c_cc[VTIME] = 0;
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
103 tcsetattr(0, TCSAFLUSH, &my_termios);
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
104
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
105 printf("Entering tty pass-thru; type ^\\ to exit\r\n\n");
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
106 loop();
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
107 tcsetattr(0, TCSAFLUSH, &saved_termios);
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
108 return 0;
cefa700d1b8f frbl: beginning of frbl2test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
109 }