blob: edaeb6fa5e28851ec7a1ca03eb45b2e38fe2fdd2 [file] [log] [blame]
Bernhard Reutner-Fischerf588f702007-09-22 20:35:32 +00001/* vi: set sw=4 ts=4: */
2/*
Denis Vlasenko70766952007-09-21 17:42:40 +00003 * bare bones 'talk to modem' program - similar to 'cu -l $device'
4 * inspired by mgetty's microcom
5 *
Denis Vlasenkoffae8452008-02-16 23:40:47 +00006 * Copyright (C) 2008 by Vladimir Dronnikov <dronnikov@gmail.com>
Denis Vlasenkof34e8212007-09-22 20:23:57 +00007 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02008 * Licensed under GPLv2, see file LICENSE in this source tree.
Denis Vlasenko70766952007-09-21 17:42:40 +00009 */
Pere Orga5bc8c002011-04-11 03:29:49 +020010
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: "\nOptions:"
16//usage: "\n -d Wait up to DELAY ms for TTY output before sending every"
17//usage: "\n next byte to it"
18//usage: "\n -t Exit if both stdin and TTY are silent for TIMEOUT ms"
19//usage: "\n -s Set serial line to SPEED"
20//usage: "\n -X Disable special meaning of NUL and Ctrl-X from stdin"
21
Denis Vlasenko9a4e08e2007-09-22 21:37:27 +000022#include "libbb.h"
Denis Vlasenko70766952007-09-21 17:42:40 +000023
Denis Vlasenkodc1c4572008-02-08 17:30:39 +000024// set raw tty mode
Denis Vlasenkoef67c572008-01-27 22:40:39 +000025static void xget1(int fd, struct termios *t, struct termios *oldt)
Denis Vlasenko56244732008-02-17 15:14:04 +000026{
Denis Vlasenkoef67c572008-01-27 22:40:39 +000027 tcgetattr(fd, oldt);
28 *t = *oldt;
29 cfmakeraw(t);
30// t->c_lflag &= ~(ISIG|ICANON|ECHO|IEXTEN);
31// t->c_iflag &= ~(BRKINT|IXON|ICRNL);
32// t->c_oflag &= ~(ONLCR);
33// t->c_cc[VMIN] = 1;
34// t->c_cc[VTIME] = 0;
35}
36
37static int xset1(int fd, struct termios *tio, const char *device)
38{
Denis Vlasenkodc1c4572008-02-08 17:30:39 +000039 int ret = tcsetattr(fd, TCSAFLUSH, tio);
Denis Vlasenkoef67c572008-01-27 22:40:39 +000040
41 if (ret) {
42 bb_perror_msg("can't tcsetattr for %s", device);
43 }
44 return ret;
45}
46
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000047int microcom_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000048int microcom_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenko70766952007-09-21 17:42:40 +000049{
Denis Vlasenko0effc242008-01-28 09:39:30 +000050 int sfd;
Denis Vlasenkodc1c4572008-02-08 17:30:39 +000051 int nfd;
52 struct pollfd pfd[2];
Denis Vlasenko70766952007-09-21 17:42:40 +000053 struct termios tio0, tiosfd, tio;
Denis Vlasenkodc1c4572008-02-08 17:30:39 +000054 char *device_lock_file;
55 enum {
56 OPT_X = 1 << 0, // do not respect Ctrl-X, Ctrl-@
57 OPT_s = 1 << 1, // baudrate
Denis Vlasenko624188a2008-02-19 18:26:40 +000058 OPT_d = 1 << 2, // wait for device response, ms
Denis Vlasenko68e8e962008-02-09 11:39:00 +000059 OPT_t = 1 << 3, // timeout, ms
Denis Vlasenkodc1c4572008-02-08 17:30:39 +000060 };
61 speed_t speed = 9600;
Denis Vlasenkod56e3cc2008-02-09 11:37:21 +000062 int delay = -1;
63 int timeout = -1;
Denis Vlasenko624188a2008-02-19 18:26:40 +000064 unsigned opts;
Denis Vlasenko70766952007-09-21 17:42:40 +000065
Denis Vlasenkoef67c572008-01-27 22:40:39 +000066 // fetch options
Denis Vlasenko624188a2008-02-19 18:26:40 +000067 opt_complementary = "=1:s+:d+:t+"; // exactly one arg, numeric options
Denis Vlasenkoffae8452008-02-16 23:40:47 +000068 opts = getopt32(argv, "Xs:d:t:", &speed, &delay, &timeout);
Denis Vlasenkodc1c4572008-02-08 17:30:39 +000069// argc -= optind;
Denis Vlasenko70766952007-09-21 17:42:40 +000070 argv += optind;
Denis Vlasenkoef67c572008-01-27 22:40:39 +000071
Denis Vlasenko70766952007-09-21 17:42:40 +000072 // try to create lock file in /var/lock
Denis Vlasenkodc1c4572008-02-08 17:30:39 +000073 device_lock_file = (char *)bb_basename(argv[0]);
Denis Vlasenkoef67c572008-01-27 22:40:39 +000074 device_lock_file = xasprintf("/var/lock/LCK..%s", device_lock_file);
Denis Vlasenko70766952007-09-21 17:42:40 +000075 sfd = open(device_lock_file, O_CREAT | O_WRONLY | O_TRUNC | O_EXCL, 0644);
76 if (sfd < 0) {
Denis Vlasenko624188a2008-02-19 18:26:40 +000077 // device already locked -> bail out
Denis Vlasenko70766952007-09-21 17:42:40 +000078 if (errno == EEXIST)
Denys Vlasenko651a2692010-03-23 16:25:17 +010079 bb_perror_msg_and_die("can't create '%s'", device_lock_file);
Denis Vlasenko624188a2008-02-19 18:26:40 +000080 // can't create lock -> don't care
Denis Vlasenkoef67c572008-01-27 22:40:39 +000081 if (ENABLE_FEATURE_CLEAN_UP)
82 free(device_lock_file);
83 device_lock_file = NULL;
Denis Vlasenko624188a2008-02-19 18:26:40 +000084 } else {
85 // %4d to make concurrent mgetty (if any) happy.
86 // Mgetty treats 4-bytes lock files as binary,
Denis Vlasenko70766952007-09-21 17:42:40 +000087 // not text, PID. Making 5+ char file. Brrr...
Denis Vlasenkocd174d12008-02-28 13:14:42 +000088 fdprintf(sfd, "%4d\n", getpid());
Denis Vlasenko70766952007-09-21 17:42:40 +000089 close(sfd);
90 }
91
Denis Vlasenkoef67c572008-01-27 22:40:39 +000092 // setup signals
Denis Vlasenkoffae8452008-02-16 23:40:47 +000093 bb_signals(0
94 + (1 << SIGHUP)
95 + (1 << SIGINT)
96 + (1 << SIGTERM)
97 + (1 << SIGPIPE)
Denis Vlasenko08ea11a2008-09-11 19:51:11 +000098 , record_signo);
Denis Vlasenkoef67c572008-01-27 22:40:39 +000099
100 // error exit code if we fail to open the device
Denis Vlasenko08ea11a2008-09-11 19:51:11 +0000101 bb_got_signal = 1;
Denis Vlasenkoef67c572008-01-27 22:40:39 +0000102
Denis Vlasenkoffae8452008-02-16 23:40:47 +0000103 // open device
104 sfd = open_or_warn(argv[0], O_RDWR | O_NOCTTY | O_NONBLOCK);
105 if (sfd < 0)
106 goto done;
Denys Vlasenkoab19ede2009-11-11 21:05:42 +0100107 fcntl(sfd, F_SETFL, O_RDWR);
Denis Vlasenkoffae8452008-02-16 23:40:47 +0000108
109 // put device to "raw mode"
Denis Vlasenkoef67c572008-01-27 22:40:39 +0000110 xget1(sfd, &tio, &tiosfd);
Denis Vlasenkoef67c572008-01-27 22:40:39 +0000111 // set device speed
112 cfsetspeed(&tio, tty_value_to_baud(speed));
113 if (xset1(sfd, &tio, argv[0]))
Paul Foxa50a9b02008-04-23 15:31:29 +0000114 goto done;
115
116 // put stdin to "raw mode" (if stdin is a TTY),
117 // handle one character at a time
118 if (isatty(STDIN_FILENO)) {
119 xget1(STDIN_FILENO, &tio, &tio0);
120 if (xset1(STDIN_FILENO, &tio, "stdin"))
121 goto done;
122 }
Denis Vlasenko70766952007-09-21 17:42:40 +0000123
124 // main loop: check with poll(), then read/write bytes across
Denis Vlasenko0effc242008-01-28 09:39:30 +0000125 pfd[0].fd = sfd;
Denis Vlasenko70766952007-09-21 17:42:40 +0000126 pfd[0].events = POLLIN;
Denis Vlasenkoef67c572008-01-27 22:40:39 +0000127 pfd[1].fd = STDIN_FILENO;
Denis Vlasenko70766952007-09-21 17:42:40 +0000128 pfd[1].events = POLLIN;
Denis Vlasenkoef67c572008-01-27 22:40:39 +0000129
Denis Vlasenko08ea11a2008-09-11 19:51:11 +0000130 bb_got_signal = 0;
Denis Vlasenkoef67c572008-01-27 22:40:39 +0000131 nfd = 2;
Denys Vlasenkocb23d932010-01-12 12:29:55 +0100132 // Not safe_poll: we want to exit on signal
133 while (!bb_got_signal && poll(pfd, nfd, timeout) > 0) {
Denys Vlasenko88aa5582010-03-02 15:02:45 +0100134 if (nfd > 1 && pfd[1].revents) {
Denis Vlasenko9304d6e2008-02-10 19:05:56 +0000135 char c;
Denis Vlasenkodc1c4572008-02-08 17:30:39 +0000136 // read from stdin -> write to device
137 if (safe_read(STDIN_FILENO, &c, 1) < 1) {
Denis Vlasenkod56e3cc2008-02-09 11:37:21 +0000138 // don't poll stdin anymore if we got EOF/error
Denis Vlasenkodc1c4572008-02-08 17:30:39 +0000139 nfd--;
Denis Vlasenkoffae8452008-02-16 23:40:47 +0000140 goto skip_write;
Denis Vlasenkodc1c4572008-02-08 17:30:39 +0000141 }
142 // do we need special processing?
143 if (!(opts & OPT_X)) {
144 // ^@ sends Break
145 if (VINTR == c) {
146 tcsendbreak(sfd, 0);
Denis Vlasenkoffae8452008-02-16 23:40:47 +0000147 goto skip_write;
Denis Vlasenko70766952007-09-21 17:42:40 +0000148 }
Denis Vlasenkodc1c4572008-02-08 17:30:39 +0000149 // ^X exits
150 if (24 == c)
151 break;
Denis Vlasenko70766952007-09-21 17:42:40 +0000152 }
Denis Vlasenkodc1c4572008-02-08 17:30:39 +0000153 write(sfd, &c, 1);
Denis Vlasenko9304d6e2008-02-10 19:05:56 +0000154 if (delay >= 0)
155 safe_poll(pfd, 1, delay);
Denis Vlasenkoffae8452008-02-16 23:40:47 +0000156skip_write: ;
Denis Vlasenko9304d6e2008-02-10 19:05:56 +0000157 }
Denys Vlasenko88aa5582010-03-02 15:02:45 +0100158 if (pfd[0].revents) {
Denis Vlasenkoffae8452008-02-16 23:40:47 +0000159#define iobuf bb_common_bufsiz1
Denis Vlasenko9304d6e2008-02-10 19:05:56 +0000160 ssize_t len;
161 // read from device -> write to stdout
Denis Vlasenkoffae8452008-02-16 23:40:47 +0000162 len = safe_read(sfd, iobuf, sizeof(iobuf));
Denis Vlasenko9304d6e2008-02-10 19:05:56 +0000163 if (len > 0)
Denis Vlasenkoffae8452008-02-16 23:40:47 +0000164 full_write(STDOUT_FILENO, iobuf, len);
165 else {
166 // EOF/error -> bail out
Denis Vlasenko08ea11a2008-09-11 19:51:11 +0000167 bb_got_signal = SIGHUP;
Denis Vlasenkoffae8452008-02-16 23:40:47 +0000168 break;
169 }
Denis Vlasenko70766952007-09-21 17:42:40 +0000170 }
171 }
Denis Vlasenkodc1c4572008-02-08 17:30:39 +0000172
Denis Vlasenkoffae8452008-02-16 23:40:47 +0000173 // restore device mode
Denis Vlasenkodc1c4572008-02-08 17:30:39 +0000174 tcsetattr(sfd, TCSAFLUSH, &tiosfd);
175
Denis Vlasenkod56e3cc2008-02-09 11:37:21 +0000176 if (isatty(STDIN_FILENO))
Denis Vlasenkodc1c4572008-02-08 17:30:39 +0000177 tcsetattr(STDIN_FILENO, TCSAFLUSH, &tio0);
178
Denis Vlasenkoef67c572008-01-27 22:40:39 +0000179done:
Denis Vlasenkodc1c4572008-02-08 17:30:39 +0000180 if (device_lock_file)
Denis Vlasenko70766952007-09-21 17:42:40 +0000181 unlink(device_lock_file);
Denis Vlasenkodc1c4572008-02-08 17:30:39 +0000182
Denis Vlasenko08ea11a2008-09-11 19:51:11 +0000183 return bb_got_signal;
Denis Vlasenko70766952007-09-21 17:42:40 +0000184}