1 /* vi: set sw=4 ts=4: */
3 * bare bones 'talk to modem' program - similar to 'cu -l $device'
4 * inspired by mgetty's microcom
6 * Copyright (C) 2008 by Vladimir Dronnikov <dronnikov@gmail.com>
8 * Licensed under GPLv2, see file LICENSE in this source tree.
10 //config:config MICROCOM
11 //config: bool "microcom (5.6 kb)"
14 //config: The poor man's minicom utility for chatting with serial port devices.
16 //applet:IF_MICROCOM(APPLET(microcom, BB_DIR_USR_BIN, BB_SUID_DROP))
18 //kbuild:lib-$(CONFIG_MICROCOM) += microcom.o
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"
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"
31 #include "common_bufsiz.h"
34 static void xget1(int fd, struct termios *t, struct termios *oldt)
36 //TODO: use set_termios_to_raw()
40 // t->c_lflag &= ~(ISIG|ICANON|ECHO|IEXTEN);
41 // t->c_iflag &= ~(BRKINT|IXON|ICRNL);
42 // t->c_oflag &= ~(ONLCR);
44 // t->c_cc[VTIME] = 0;
47 static int xset1(int fd, struct termios *tio, const char *device)
49 int ret = tcsetattr(fd, TCSAFLUSH, tio);
52 bb_perror_msg("can't tcsetattr for %s", device);
57 int microcom_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
58 int microcom_main(int argc UNUSED_PARAM, char **argv)
63 struct termios tio0, tiosfd, tio;
64 char *device_lock_file;
66 OPT_X = 1 << 0, // do not respect Ctrl-X, Ctrl-@
67 OPT_s = 1 << 1, // baudrate
68 OPT_d = 1 << 2, // wait for device response, ms
69 OPT_t = 1 << 3, // timeout, ms
77 opt_complementary = "=1";
78 opts = getopt32(argv, "Xs:+d:+t:+", &speed, &delay, &timeout);
82 // try to create lock file in /var/lock
83 device_lock_file = (char *)bb_basename(argv[0]);
84 device_lock_file = xasprintf("/var/lock/LCK..%s", device_lock_file);
85 sfd = open(device_lock_file, O_CREAT | O_WRONLY | O_TRUNC | O_EXCL, 0644);
87 // device already locked -> bail out
89 bb_perror_msg_and_die("can't create '%s'", device_lock_file);
90 // can't create lock -> don't care
91 if (ENABLE_FEATURE_CLEAN_UP)
92 free(device_lock_file);
93 device_lock_file = NULL;
95 // %4d to make concurrent mgetty (if any) happy.
96 // Mgetty treats 4-bytes lock files as binary,
97 // not text, PID. Making 5+ char file. Brrr...
98 fdprintf(sfd, "%4d\n", getpid());
110 // error exit code if we fail to open the device
114 sfd = open_or_warn(argv[0], O_RDWR | O_NOCTTY | O_NONBLOCK);
117 fcntl(sfd, F_SETFL, O_RDWR);
119 // put device to "raw mode"
120 xget1(sfd, &tio, &tiosfd);
122 cfsetspeed(&tio, tty_value_to_baud(speed));
123 if (xset1(sfd, &tio, argv[0]))
126 // put stdin to "raw mode" (if stdin is a TTY),
127 // handle one character at a time
128 if (isatty(STDIN_FILENO)) {
129 xget1(STDIN_FILENO, &tio, &tio0);
130 if (xset1(STDIN_FILENO, &tio, "stdin"))
134 // main loop: check with poll(), then read/write bytes across
136 pfd[0].events = POLLIN;
137 pfd[1].fd = STDIN_FILENO;
138 pfd[1].events = POLLIN;
142 // Not safe_poll: we want to exit on signal
143 while (!bb_got_signal && poll(pfd, nfd, timeout) > 0) {
144 if (nfd > 1 && pfd[1].revents) {
146 // read from stdin -> write to device
147 if (safe_read(STDIN_FILENO, &c, 1) < 1) {
148 // don't poll stdin anymore if we got EOF/error
152 // do we need special processing?
153 if (!(opts & OPT_X)) {
165 safe_poll(pfd, 1, delay);
168 if (pfd[0].revents) {
170 #define iobuf bb_common_bufsiz1
171 setup_common_bufsiz();
172 // read from device -> write to stdout
173 len = safe_read(sfd, iobuf, COMMON_BUFSIZE);
175 full_write(STDOUT_FILENO, iobuf, len);
177 // EOF/error -> bail out
178 bb_got_signal = SIGHUP;
184 // restore device mode
185 tcsetattr(sfd, TCSAFLUSH, &tiosfd);
187 if (isatty(STDIN_FILENO))
188 tcsetattr(STDIN_FILENO, TCSAFLUSH, &tio0);
191 if (device_lock_file)
192 unlink(device_lock_file);
194 return bb_got_signal;