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.
11 //usage:#define microcom_trivial_usage
12 //usage: "[-d DELAY] [-t TIMEOUT] [-s SPEED] [-X] TTY"
13 //usage:#define microcom_full_usage "\n\n"
14 //usage: "Copy bytes for stdin to TTY and from TTY to stdout\n"
15 //usage: "\n -d Wait up to DELAY ms for TTY output before sending every"
16 //usage: "\n next byte to it"
17 //usage: "\n -t Exit if both stdin and TTY are silent for TIMEOUT ms"
18 //usage: "\n -s Set serial line to SPEED"
19 //usage: "\n -X Disable special meaning of NUL and Ctrl-X from stdin"
24 static void xget1(int fd, struct termios *t, struct termios *oldt)
29 // t->c_lflag &= ~(ISIG|ICANON|ECHO|IEXTEN);
30 // t->c_iflag &= ~(BRKINT|IXON|ICRNL);
31 // t->c_oflag &= ~(ONLCR);
33 // t->c_cc[VTIME] = 0;
36 static int xset1(int fd, struct termios *tio, const char *device)
38 int ret = tcsetattr(fd, TCSAFLUSH, tio);
41 bb_perror_msg("can't tcsetattr for %s", device);
46 int microcom_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
47 int microcom_main(int argc UNUSED_PARAM, char **argv)
52 struct termios tio0, tiosfd, tio;
53 char *device_lock_file;
55 OPT_X = 1 << 0, // do not respect Ctrl-X, Ctrl-@
56 OPT_s = 1 << 1, // baudrate
57 OPT_d = 1 << 2, // wait for device response, ms
58 OPT_t = 1 << 3, // timeout, ms
66 opt_complementary = "=1:s+:d+:t+"; // exactly one arg, numeric options
67 opts = getopt32(argv, "Xs:d:t:", &speed, &delay, &timeout);
71 // try to create lock file in /var/lock
72 device_lock_file = (char *)bb_basename(argv[0]);
73 device_lock_file = xasprintf("/var/lock/LCK..%s", device_lock_file);
74 sfd = open(device_lock_file, O_CREAT | O_WRONLY | O_TRUNC | O_EXCL, 0644);
76 // device already locked -> bail out
78 bb_perror_msg_and_die("can't create '%s'", device_lock_file);
79 // can't create lock -> don't care
80 if (ENABLE_FEATURE_CLEAN_UP)
81 free(device_lock_file);
82 device_lock_file = NULL;
84 // %4d to make concurrent mgetty (if any) happy.
85 // Mgetty treats 4-bytes lock files as binary,
86 // not text, PID. Making 5+ char file. Brrr...
87 fdprintf(sfd, "%4d\n", getpid());
99 // error exit code if we fail to open the device
103 sfd = open_or_warn(argv[0], O_RDWR | O_NOCTTY | O_NONBLOCK);
106 fcntl(sfd, F_SETFL, O_RDWR);
108 // put device to "raw mode"
109 xget1(sfd, &tio, &tiosfd);
111 cfsetspeed(&tio, tty_value_to_baud(speed));
112 if (xset1(sfd, &tio, argv[0]))
115 // put stdin to "raw mode" (if stdin is a TTY),
116 // handle one character at a time
117 if (isatty(STDIN_FILENO)) {
118 xget1(STDIN_FILENO, &tio, &tio0);
119 if (xset1(STDIN_FILENO, &tio, "stdin"))
123 // main loop: check with poll(), then read/write bytes across
125 pfd[0].events = POLLIN;
126 pfd[1].fd = STDIN_FILENO;
127 pfd[1].events = POLLIN;
131 // Not safe_poll: we want to exit on signal
132 while (!bb_got_signal && poll(pfd, nfd, timeout) > 0) {
133 if (nfd > 1 && pfd[1].revents) {
135 // read from stdin -> write to device
136 if (safe_read(STDIN_FILENO, &c, 1) < 1) {
137 // don't poll stdin anymore if we got EOF/error
141 // do we need special processing?
142 if (!(opts & OPT_X)) {
154 safe_poll(pfd, 1, delay);
157 if (pfd[0].revents) {
158 #define iobuf bb_common_bufsiz1
160 // read from device -> write to stdout
161 len = safe_read(sfd, iobuf, sizeof(iobuf));
163 full_write(STDOUT_FILENO, iobuf, len);
165 // EOF/error -> bail out
166 bb_got_signal = SIGHUP;
172 // restore device mode
173 tcsetattr(sfd, TCSAFLUSH, &tiosfd);
175 if (isatty(STDIN_FILENO))
176 tcsetattr(STDIN_FILENO, TCSAFLUSH, &tio0);
179 if (device_lock_file)
180 unlink(device_lock_file);
182 return bb_got_signal;