Bernhard Reutner-Fischer | f588f70 | 2007-09-22 20:35:32 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
Denis Vlasenko | 7076695 | 2007-09-21 17:42:40 +0000 | [diff] [blame] | 3 | * bare bones 'talk to modem' program - similar to 'cu -l $device' |
| 4 | * inspired by mgetty's microcom |
| 5 | * |
Denis Vlasenko | ffae845 | 2008-02-16 23:40:47 +0000 | [diff] [blame] | 6 | * Copyright (C) 2008 by Vladimir Dronnikov <dronnikov@gmail.com> |
Denis Vlasenko | f34e821 | 2007-09-22 20:23:57 +0000 | [diff] [blame] | 7 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 8 | * Licensed under GPLv2, see file LICENSE in this source tree. |
Denis Vlasenko | 7076695 | 2007-09-21 17:42:40 +0000 | [diff] [blame] | 9 | */ |
Denys Vlasenko | fb4da16 | 2016-11-22 23:14:24 +0100 | [diff] [blame] | 10 | //config:config MICROCOM |
| 11 | //config: bool "microcom" |
| 12 | //config: default y |
| 13 | //config: help |
| 14 | //config: The poor man's minicom utility for chatting with serial port devices. |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 15 | |
Denys Vlasenko | f88e3bf | 2016-11-22 23:54:17 +0100 | [diff] [blame] | 16 | //applet:IF_MICROCOM(APPLET(microcom, BB_DIR_USR_BIN, BB_SUID_DROP)) |
| 17 | |
| 18 | //kbuild:lib-$(CONFIG_MICROCOM) += microcom.o |
| 19 | |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 20 | //usage:#define microcom_trivial_usage |
| 21 | //usage: "[-d DELAY] [-t TIMEOUT] [-s SPEED] [-X] TTY" |
| 22 | //usage:#define microcom_full_usage "\n\n" |
| 23 | //usage: "Copy bytes for stdin to TTY and from TTY to stdout\n" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 24 | //usage: "\n -d Wait up to DELAY ms for TTY output before sending every" |
| 25 | //usage: "\n next byte to it" |
| 26 | //usage: "\n -t Exit if both stdin and TTY are silent for TIMEOUT ms" |
| 27 | //usage: "\n -s Set serial line to SPEED" |
| 28 | //usage: "\n -X Disable special meaning of NUL and Ctrl-X from stdin" |
| 29 | |
Denis Vlasenko | 9a4e08e | 2007-09-22 21:37:27 +0000 | [diff] [blame] | 30 | #include "libbb.h" |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 31 | #include "common_bufsiz.h" |
Denis Vlasenko | 7076695 | 2007-09-21 17:42:40 +0000 | [diff] [blame] | 32 | |
Denis Vlasenko | dc1c457 | 2008-02-08 17:30:39 +0000 | [diff] [blame] | 33 | // set raw tty mode |
Denis Vlasenko | ef67c57 | 2008-01-27 22:40:39 +0000 | [diff] [blame] | 34 | static void xget1(int fd, struct termios *t, struct termios *oldt) |
Denis Vlasenko | 5624473 | 2008-02-17 15:14:04 +0000 | [diff] [blame] | 35 | { |
Denys Vlasenko | 01ccdd1 | 2017-01-11 16:17:59 +0100 | [diff] [blame] | 36 | //TODO: use set_termios_to_raw() |
Denis Vlasenko | ef67c57 | 2008-01-27 22:40:39 +0000 | [diff] [blame] | 37 | tcgetattr(fd, oldt); |
| 38 | *t = *oldt; |
| 39 | cfmakeraw(t); |
| 40 | // t->c_lflag &= ~(ISIG|ICANON|ECHO|IEXTEN); |
| 41 | // t->c_iflag &= ~(BRKINT|IXON|ICRNL); |
| 42 | // t->c_oflag &= ~(ONLCR); |
| 43 | // t->c_cc[VMIN] = 1; |
| 44 | // t->c_cc[VTIME] = 0; |
| 45 | } |
| 46 | |
| 47 | static int xset1(int fd, struct termios *tio, const char *device) |
| 48 | { |
Denis Vlasenko | dc1c457 | 2008-02-08 17:30:39 +0000 | [diff] [blame] | 49 | int ret = tcsetattr(fd, TCSAFLUSH, tio); |
Denis Vlasenko | ef67c57 | 2008-01-27 22:40:39 +0000 | [diff] [blame] | 50 | |
| 51 | if (ret) { |
| 52 | bb_perror_msg("can't tcsetattr for %s", device); |
| 53 | } |
| 54 | return ret; |
| 55 | } |
| 56 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 57 | int microcom_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 58 | int microcom_main(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | 7076695 | 2007-09-21 17:42:40 +0000 | [diff] [blame] | 59 | { |
Denis Vlasenko | 0effc24 | 2008-01-28 09:39:30 +0000 | [diff] [blame] | 60 | int sfd; |
Denis Vlasenko | dc1c457 | 2008-02-08 17:30:39 +0000 | [diff] [blame] | 61 | int nfd; |
| 62 | struct pollfd pfd[2]; |
Denis Vlasenko | 7076695 | 2007-09-21 17:42:40 +0000 | [diff] [blame] | 63 | struct termios tio0, tiosfd, tio; |
Denis Vlasenko | dc1c457 | 2008-02-08 17:30:39 +0000 | [diff] [blame] | 64 | char *device_lock_file; |
| 65 | enum { |
| 66 | OPT_X = 1 << 0, // do not respect Ctrl-X, Ctrl-@ |
| 67 | OPT_s = 1 << 1, // baudrate |
Denis Vlasenko | 624188a | 2008-02-19 18:26:40 +0000 | [diff] [blame] | 68 | OPT_d = 1 << 2, // wait for device response, ms |
Denis Vlasenko | 68e8e96 | 2008-02-09 11:39:00 +0000 | [diff] [blame] | 69 | OPT_t = 1 << 3, // timeout, ms |
Denis Vlasenko | dc1c457 | 2008-02-08 17:30:39 +0000 | [diff] [blame] | 70 | }; |
| 71 | speed_t speed = 9600; |
Denis Vlasenko | d56e3cc | 2008-02-09 11:37:21 +0000 | [diff] [blame] | 72 | int delay = -1; |
| 73 | int timeout = -1; |
Denis Vlasenko | 624188a | 2008-02-19 18:26:40 +0000 | [diff] [blame] | 74 | unsigned opts; |
Denis Vlasenko | 7076695 | 2007-09-21 17:42:40 +0000 | [diff] [blame] | 75 | |
Denis Vlasenko | ef67c57 | 2008-01-27 22:40:39 +0000 | [diff] [blame] | 76 | // fetch options |
Denys Vlasenko | 237bedd | 2016-07-06 21:58:02 +0200 | [diff] [blame] | 77 | opts = getopt32(argv, "Xs:+d:+t:+", &speed, &delay, &timeout); |
Denis Vlasenko | dc1c457 | 2008-02-08 17:30:39 +0000 | [diff] [blame] | 78 | // argc -= optind; |
Denis Vlasenko | 7076695 | 2007-09-21 17:42:40 +0000 | [diff] [blame] | 79 | argv += optind; |
Denis Vlasenko | ef67c57 | 2008-01-27 22:40:39 +0000 | [diff] [blame] | 80 | |
Denis Vlasenko | 7076695 | 2007-09-21 17:42:40 +0000 | [diff] [blame] | 81 | // try to create lock file in /var/lock |
Denis Vlasenko | dc1c457 | 2008-02-08 17:30:39 +0000 | [diff] [blame] | 82 | device_lock_file = (char *)bb_basename(argv[0]); |
Denis Vlasenko | ef67c57 | 2008-01-27 22:40:39 +0000 | [diff] [blame] | 83 | device_lock_file = xasprintf("/var/lock/LCK..%s", device_lock_file); |
Denis Vlasenko | 7076695 | 2007-09-21 17:42:40 +0000 | [diff] [blame] | 84 | sfd = open(device_lock_file, O_CREAT | O_WRONLY | O_TRUNC | O_EXCL, 0644); |
| 85 | if (sfd < 0) { |
Denis Vlasenko | 624188a | 2008-02-19 18:26:40 +0000 | [diff] [blame] | 86 | // device already locked -> bail out |
Denis Vlasenko | 7076695 | 2007-09-21 17:42:40 +0000 | [diff] [blame] | 87 | if (errno == EEXIST) |
Denys Vlasenko | 651a269 | 2010-03-23 16:25:17 +0100 | [diff] [blame] | 88 | bb_perror_msg_and_die("can't create '%s'", device_lock_file); |
Denis Vlasenko | 624188a | 2008-02-19 18:26:40 +0000 | [diff] [blame] | 89 | // can't create lock -> don't care |
Denis Vlasenko | ef67c57 | 2008-01-27 22:40:39 +0000 | [diff] [blame] | 90 | if (ENABLE_FEATURE_CLEAN_UP) |
| 91 | free(device_lock_file); |
| 92 | device_lock_file = NULL; |
Denis Vlasenko | 624188a | 2008-02-19 18:26:40 +0000 | [diff] [blame] | 93 | } else { |
| 94 | // %4d to make concurrent mgetty (if any) happy. |
| 95 | // Mgetty treats 4-bytes lock files as binary, |
Denis Vlasenko | 7076695 | 2007-09-21 17:42:40 +0000 | [diff] [blame] | 96 | // not text, PID. Making 5+ char file. Brrr... |
Denis Vlasenko | cd174d1 | 2008-02-28 13:14:42 +0000 | [diff] [blame] | 97 | fdprintf(sfd, "%4d\n", getpid()); |
Denis Vlasenko | 7076695 | 2007-09-21 17:42:40 +0000 | [diff] [blame] | 98 | close(sfd); |
| 99 | } |
| 100 | |
Denis Vlasenko | ef67c57 | 2008-01-27 22:40:39 +0000 | [diff] [blame] | 101 | // setup signals |
Denis Vlasenko | ffae845 | 2008-02-16 23:40:47 +0000 | [diff] [blame] | 102 | bb_signals(0 |
| 103 | + (1 << SIGHUP) |
| 104 | + (1 << SIGINT) |
| 105 | + (1 << SIGTERM) |
| 106 | + (1 << SIGPIPE) |
Denis Vlasenko | 08ea11a | 2008-09-11 19:51:11 +0000 | [diff] [blame] | 107 | , record_signo); |
Denis Vlasenko | ef67c57 | 2008-01-27 22:40:39 +0000 | [diff] [blame] | 108 | |
| 109 | // error exit code if we fail to open the device |
Denis Vlasenko | 08ea11a | 2008-09-11 19:51:11 +0000 | [diff] [blame] | 110 | bb_got_signal = 1; |
Denis Vlasenko | ef67c57 | 2008-01-27 22:40:39 +0000 | [diff] [blame] | 111 | |
Denis Vlasenko | ffae845 | 2008-02-16 23:40:47 +0000 | [diff] [blame] | 112 | // open device |
| 113 | sfd = open_or_warn(argv[0], O_RDWR | O_NOCTTY | O_NONBLOCK); |
| 114 | if (sfd < 0) |
| 115 | goto done; |
Denys Vlasenko | ab19ede | 2009-11-11 21:05:42 +0100 | [diff] [blame] | 116 | fcntl(sfd, F_SETFL, O_RDWR); |
Denis Vlasenko | ffae845 | 2008-02-16 23:40:47 +0000 | [diff] [blame] | 117 | |
| 118 | // put device to "raw mode" |
Denis Vlasenko | ef67c57 | 2008-01-27 22:40:39 +0000 | [diff] [blame] | 119 | xget1(sfd, &tio, &tiosfd); |
Denis Vlasenko | ef67c57 | 2008-01-27 22:40:39 +0000 | [diff] [blame] | 120 | // set device speed |
| 121 | cfsetspeed(&tio, tty_value_to_baud(speed)); |
| 122 | if (xset1(sfd, &tio, argv[0])) |
Paul Fox | a50a9b0 | 2008-04-23 15:31:29 +0000 | [diff] [blame] | 123 | goto done; |
| 124 | |
| 125 | // put stdin to "raw mode" (if stdin is a TTY), |
| 126 | // handle one character at a time |
| 127 | if (isatty(STDIN_FILENO)) { |
| 128 | xget1(STDIN_FILENO, &tio, &tio0); |
| 129 | if (xset1(STDIN_FILENO, &tio, "stdin")) |
| 130 | goto done; |
| 131 | } |
Denis Vlasenko | 7076695 | 2007-09-21 17:42:40 +0000 | [diff] [blame] | 132 | |
| 133 | // main loop: check with poll(), then read/write bytes across |
Denis Vlasenko | 0effc24 | 2008-01-28 09:39:30 +0000 | [diff] [blame] | 134 | pfd[0].fd = sfd; |
Denis Vlasenko | 7076695 | 2007-09-21 17:42:40 +0000 | [diff] [blame] | 135 | pfd[0].events = POLLIN; |
Denis Vlasenko | ef67c57 | 2008-01-27 22:40:39 +0000 | [diff] [blame] | 136 | pfd[1].fd = STDIN_FILENO; |
Denis Vlasenko | 7076695 | 2007-09-21 17:42:40 +0000 | [diff] [blame] | 137 | pfd[1].events = POLLIN; |
Denis Vlasenko | ef67c57 | 2008-01-27 22:40:39 +0000 | [diff] [blame] | 138 | |
Denis Vlasenko | 08ea11a | 2008-09-11 19:51:11 +0000 | [diff] [blame] | 139 | bb_got_signal = 0; |
Denis Vlasenko | ef67c57 | 2008-01-27 22:40:39 +0000 | [diff] [blame] | 140 | nfd = 2; |
Denys Vlasenko | cb23d93 | 2010-01-12 12:29:55 +0100 | [diff] [blame] | 141 | // Not safe_poll: we want to exit on signal |
| 142 | while (!bb_got_signal && poll(pfd, nfd, timeout) > 0) { |
Denys Vlasenko | 88aa558 | 2010-03-02 15:02:45 +0100 | [diff] [blame] | 143 | if (nfd > 1 && pfd[1].revents) { |
Denis Vlasenko | 9304d6e | 2008-02-10 19:05:56 +0000 | [diff] [blame] | 144 | char c; |
Denis Vlasenko | dc1c457 | 2008-02-08 17:30:39 +0000 | [diff] [blame] | 145 | // read from stdin -> write to device |
| 146 | if (safe_read(STDIN_FILENO, &c, 1) < 1) { |
Denis Vlasenko | d56e3cc | 2008-02-09 11:37:21 +0000 | [diff] [blame] | 147 | // don't poll stdin anymore if we got EOF/error |
Denis Vlasenko | dc1c457 | 2008-02-08 17:30:39 +0000 | [diff] [blame] | 148 | nfd--; |
Denis Vlasenko | ffae845 | 2008-02-16 23:40:47 +0000 | [diff] [blame] | 149 | goto skip_write; |
Denis Vlasenko | dc1c457 | 2008-02-08 17:30:39 +0000 | [diff] [blame] | 150 | } |
| 151 | // do we need special processing? |
| 152 | if (!(opts & OPT_X)) { |
| 153 | // ^@ sends Break |
| 154 | if (VINTR == c) { |
| 155 | tcsendbreak(sfd, 0); |
Denis Vlasenko | ffae845 | 2008-02-16 23:40:47 +0000 | [diff] [blame] | 156 | goto skip_write; |
Denis Vlasenko | 7076695 | 2007-09-21 17:42:40 +0000 | [diff] [blame] | 157 | } |
Denis Vlasenko | dc1c457 | 2008-02-08 17:30:39 +0000 | [diff] [blame] | 158 | // ^X exits |
| 159 | if (24 == c) |
| 160 | break; |
Denis Vlasenko | 7076695 | 2007-09-21 17:42:40 +0000 | [diff] [blame] | 161 | } |
Denis Vlasenko | dc1c457 | 2008-02-08 17:30:39 +0000 | [diff] [blame] | 162 | write(sfd, &c, 1); |
Denis Vlasenko | 9304d6e | 2008-02-10 19:05:56 +0000 | [diff] [blame] | 163 | if (delay >= 0) |
| 164 | safe_poll(pfd, 1, delay); |
Denis Vlasenko | ffae845 | 2008-02-16 23:40:47 +0000 | [diff] [blame] | 165 | skip_write: ; |
Denis Vlasenko | 9304d6e | 2008-02-10 19:05:56 +0000 | [diff] [blame] | 166 | } |
Denys Vlasenko | 88aa558 | 2010-03-02 15:02:45 +0100 | [diff] [blame] | 167 | if (pfd[0].revents) { |
Denis Vlasenko | 9304d6e | 2008-02-10 19:05:56 +0000 | [diff] [blame] | 168 | ssize_t len; |
Denys Vlasenko | 9de2e5a | 2016-04-21 18:38:51 +0200 | [diff] [blame] | 169 | #define iobuf bb_common_bufsiz1 |
| 170 | setup_common_bufsiz(); |
Denis Vlasenko | 9304d6e | 2008-02-10 19:05:56 +0000 | [diff] [blame] | 171 | // read from device -> write to stdout |
Denys Vlasenko | 9de2e5a | 2016-04-21 18:38:51 +0200 | [diff] [blame] | 172 | len = safe_read(sfd, iobuf, COMMON_BUFSIZE); |
Denis Vlasenko | 9304d6e | 2008-02-10 19:05:56 +0000 | [diff] [blame] | 173 | if (len > 0) |
Denis Vlasenko | ffae845 | 2008-02-16 23:40:47 +0000 | [diff] [blame] | 174 | full_write(STDOUT_FILENO, iobuf, len); |
| 175 | else { |
| 176 | // EOF/error -> bail out |
Denis Vlasenko | 08ea11a | 2008-09-11 19:51:11 +0000 | [diff] [blame] | 177 | bb_got_signal = SIGHUP; |
Denis Vlasenko | ffae845 | 2008-02-16 23:40:47 +0000 | [diff] [blame] | 178 | break; |
| 179 | } |
Denis Vlasenko | 7076695 | 2007-09-21 17:42:40 +0000 | [diff] [blame] | 180 | } |
| 181 | } |
Denis Vlasenko | dc1c457 | 2008-02-08 17:30:39 +0000 | [diff] [blame] | 182 | |
Denis Vlasenko | ffae845 | 2008-02-16 23:40:47 +0000 | [diff] [blame] | 183 | // restore device mode |
Denis Vlasenko | dc1c457 | 2008-02-08 17:30:39 +0000 | [diff] [blame] | 184 | tcsetattr(sfd, TCSAFLUSH, &tiosfd); |
| 185 | |
Denis Vlasenko | d56e3cc | 2008-02-09 11:37:21 +0000 | [diff] [blame] | 186 | if (isatty(STDIN_FILENO)) |
Denis Vlasenko | dc1c457 | 2008-02-08 17:30:39 +0000 | [diff] [blame] | 187 | tcsetattr(STDIN_FILENO, TCSAFLUSH, &tio0); |
| 188 | |
Denis Vlasenko | ef67c57 | 2008-01-27 22:40:39 +0000 | [diff] [blame] | 189 | done: |
Denis Vlasenko | dc1c457 | 2008-02-08 17:30:39 +0000 | [diff] [blame] | 190 | if (device_lock_file) |
Denis Vlasenko | 7076695 | 2007-09-21 17:42:40 +0000 | [diff] [blame] | 191 | unlink(device_lock_file); |
Denis Vlasenko | dc1c457 | 2008-02-08 17:30:39 +0000 | [diff] [blame] | 192 | |
Denis Vlasenko | 08ea11a | 2008-09-11 19:51:11 +0000 | [diff] [blame] | 193 | return bb_got_signal; |
Denis Vlasenko | 7076695 | 2007-09-21 17:42:40 +0000 | [diff] [blame] | 194 | } |