• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* microcom.c - Simple serial console.
2  *
3  * Copyright 2017 The Android Open Source Project.
4 
5 USE_MICROCOM(NEWTOY(microcom, "<1>1s#=115200X", TOYFLAG_USR|TOYFLAG_BIN))
6 
7 config MICROCOM
8   bool "microcom"
9   default y
10   help
11     usage: microcom [-s SPEED] [-X] DEVICE
12 
13     Simple serial console.
14 
15     -s	Set baud rate to SPEED (default 115200)
16     -X	Ignore ^@ (send break) and ^] (exit)
17 */
18 
19 #define FOR_microcom
20 #include "toys.h"
21 
GLOBALS(long s;int fd,stok;struct termios old_stdin,old_fd;)22 GLOBALS(
23   long s;
24 
25   int fd, stok;
26   struct termios old_stdin, old_fd;
27 )
28 
29 // TODO: tty_sigreset outputs ansi escape sequences, how to disable?
30 static void restore_states(int i)
31 {
32   if (TT.stok) tcsetattr(0, TCSAFLUSH, &TT.old_stdin);
33   tcsetattr(TT.fd, TCSAFLUSH, &TT.old_fd);
34 }
35 
microcom_main(void)36 void microcom_main(void)
37 {
38   struct termios tio;
39   struct pollfd fds[2];
40   int i;
41 
42   // Open with O_NDELAY, but switch back to blocking for reads.
43   TT.fd = xopen(*toys.optargs, O_RDWR | O_NOCTTY | O_NDELAY);
44   if (-1==(i = fcntl(TT.fd, F_GETFL, 0)) || fcntl(TT.fd, F_SETFL, i&~O_NDELAY)
45       || tcgetattr(TT.fd, &TT.old_fd))
46     perror_exit_raw(*toys.optargs);
47 
48   // Set both input and output to raw mode.
49   memcpy(&tio, &TT.old_fd, sizeof(struct termios));
50   cfmakeraw(&tio);
51   xsetspeed(&tio, TT.s);
52   if (tcsetattr(TT.fd, TCSAFLUSH, &tio)) perror_exit("set speed");
53   if (!set_terminal(0, 1, 0, &TT.old_stdin)) TT.stok++;
54   // ...and arrange to restore things, however we may exit.
55   sigatexit(restore_states);
56 
57   fds[0].fd = TT.fd;
58   fds[1].fd = 0;
59   fds[0].events = fds[1].events = POLLIN;
60 
61   while (poll(fds, 2, -1) > 0) {
62 
63     // Read from connection, write to stdout.
64     if (fds[0].revents) {
65       if (0 < (i = read(TT.fd, toybuf, sizeof(toybuf)))) xwrite(0, toybuf, i);
66       else break;
67     }
68 
69     // Read from stdin, write to connection.
70     if (fds[1].revents) {
71       if (read(0, toybuf, 1) != 1) break;
72       if (!FLAG(X)) {
73         if (!*toybuf) {
74           tcsendbreak(TT.fd, 0);
75           continue;
76         } else if (*toybuf == (']'-'@')) break;
77       }
78       xwrite(TT.fd, toybuf, 1);
79     }
80   }
81 }
82