2007-09-23 02:05:32 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
2007-09-21 23:12:40 +05:30
|
|
|
* bare bones 'talk to modem' program - similar to 'cu -l $device'
|
|
|
|
* inspired by mgetty's microcom
|
|
|
|
*
|
2008-02-17 05:10:47 +05:30
|
|
|
* Copyright (C) 2008 by Vladimir Dronnikov <dronnikov@gmail.com>
|
2007-09-23 01:53:57 +05:30
|
|
|
*
|
2010-08-16 23:44:46 +05:30
|
|
|
* Licensed under GPLv2, see file LICENSE in this source tree.
|
2007-09-21 23:12:40 +05:30
|
|
|
*/
|
2007-09-23 03:07:27 +05:30
|
|
|
#include "libbb.h"
|
2007-09-21 23:12:40 +05:30
|
|
|
|
2008-02-08 23:00:39 +05:30
|
|
|
// set raw tty mode
|
2008-01-28 04:10:39 +05:30
|
|
|
static void xget1(int fd, struct termios *t, struct termios *oldt)
|
2008-02-17 20:44:04 +05:30
|
|
|
{
|
2008-01-28 04:10:39 +05:30
|
|
|
tcgetattr(fd, oldt);
|
|
|
|
*t = *oldt;
|
|
|
|
cfmakeraw(t);
|
|
|
|
// t->c_lflag &= ~(ISIG|ICANON|ECHO|IEXTEN);
|
|
|
|
// t->c_iflag &= ~(BRKINT|IXON|ICRNL);
|
|
|
|
// t->c_oflag &= ~(ONLCR);
|
|
|
|
// t->c_cc[VMIN] = 1;
|
|
|
|
// t->c_cc[VTIME] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int xset1(int fd, struct termios *tio, const char *device)
|
|
|
|
{
|
2008-02-08 23:00:39 +05:30
|
|
|
int ret = tcsetattr(fd, TCSAFLUSH, tio);
|
2008-01-28 04:10:39 +05:30
|
|
|
|
|
|
|
if (ret) {
|
|
|
|
bb_perror_msg("can't tcsetattr for %s", device);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-10-11 15:35:36 +05:30
|
|
|
int microcom_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2008-07-05 14:48:54 +05:30
|
|
|
int microcom_main(int argc UNUSED_PARAM, char **argv)
|
2007-09-21 23:12:40 +05:30
|
|
|
{
|
2008-01-28 15:09:30 +05:30
|
|
|
int sfd;
|
2008-02-08 23:00:39 +05:30
|
|
|
int nfd;
|
|
|
|
struct pollfd pfd[2];
|
2007-09-21 23:12:40 +05:30
|
|
|
struct termios tio0, tiosfd, tio;
|
2008-02-08 23:00:39 +05:30
|
|
|
char *device_lock_file;
|
|
|
|
enum {
|
|
|
|
OPT_X = 1 << 0, // do not respect Ctrl-X, Ctrl-@
|
|
|
|
OPT_s = 1 << 1, // baudrate
|
2008-02-19 23:56:40 +05:30
|
|
|
OPT_d = 1 << 2, // wait for device response, ms
|
2008-02-09 17:09:00 +05:30
|
|
|
OPT_t = 1 << 3, // timeout, ms
|
2008-02-08 23:00:39 +05:30
|
|
|
};
|
|
|
|
speed_t speed = 9600;
|
2008-02-09 17:07:21 +05:30
|
|
|
int delay = -1;
|
|
|
|
int timeout = -1;
|
2008-02-19 23:56:40 +05:30
|
|
|
unsigned opts;
|
2007-09-21 23:12:40 +05:30
|
|
|
|
2008-01-28 04:10:39 +05:30
|
|
|
// fetch options
|
2008-02-19 23:56:40 +05:30
|
|
|
opt_complementary = "=1:s+:d+:t+"; // exactly one arg, numeric options
|
2008-02-17 05:10:47 +05:30
|
|
|
opts = getopt32(argv, "Xs:d:t:", &speed, &delay, &timeout);
|
2008-02-08 23:00:39 +05:30
|
|
|
// argc -= optind;
|
2007-09-21 23:12:40 +05:30
|
|
|
argv += optind;
|
2008-01-28 04:10:39 +05:30
|
|
|
|
2007-09-21 23:12:40 +05:30
|
|
|
// try to create lock file in /var/lock
|
2008-02-08 23:00:39 +05:30
|
|
|
device_lock_file = (char *)bb_basename(argv[0]);
|
2008-01-28 04:10:39 +05:30
|
|
|
device_lock_file = xasprintf("/var/lock/LCK..%s", device_lock_file);
|
2007-09-21 23:12:40 +05:30
|
|
|
sfd = open(device_lock_file, O_CREAT | O_WRONLY | O_TRUNC | O_EXCL, 0644);
|
|
|
|
if (sfd < 0) {
|
2008-02-19 23:56:40 +05:30
|
|
|
// device already locked -> bail out
|
2007-09-21 23:12:40 +05:30
|
|
|
if (errno == EEXIST)
|
2010-03-23 20:55:17 +05:30
|
|
|
bb_perror_msg_and_die("can't create '%s'", device_lock_file);
|
2008-02-19 23:56:40 +05:30
|
|
|
// can't create lock -> don't care
|
2008-01-28 04:10:39 +05:30
|
|
|
if (ENABLE_FEATURE_CLEAN_UP)
|
|
|
|
free(device_lock_file);
|
|
|
|
device_lock_file = NULL;
|
2008-02-19 23:56:40 +05:30
|
|
|
} else {
|
|
|
|
// %4d to make concurrent mgetty (if any) happy.
|
|
|
|
// Mgetty treats 4-bytes lock files as binary,
|
2007-09-21 23:12:40 +05:30
|
|
|
// not text, PID. Making 5+ char file. Brrr...
|
2008-02-28 18:44:42 +05:30
|
|
|
fdprintf(sfd, "%4d\n", getpid());
|
2007-09-21 23:12:40 +05:30
|
|
|
close(sfd);
|
|
|
|
}
|
|
|
|
|
2008-01-28 04:10:39 +05:30
|
|
|
// setup signals
|
2008-02-17 05:10:47 +05:30
|
|
|
bb_signals(0
|
|
|
|
+ (1 << SIGHUP)
|
|
|
|
+ (1 << SIGINT)
|
|
|
|
+ (1 << SIGTERM)
|
|
|
|
+ (1 << SIGPIPE)
|
2008-09-12 01:21:11 +05:30
|
|
|
, record_signo);
|
2007-09-21 23:12:40 +05:30
|
|
|
|
2008-01-28 04:10:39 +05:30
|
|
|
// error exit code if we fail to open the device
|
2008-09-12 01:21:11 +05:30
|
|
|
bb_got_signal = 1;
|
2007-09-21 23:12:40 +05:30
|
|
|
|
2008-02-17 05:10:47 +05:30
|
|
|
// open device
|
|
|
|
sfd = open_or_warn(argv[0], O_RDWR | O_NOCTTY | O_NONBLOCK);
|
|
|
|
if (sfd < 0)
|
|
|
|
goto done;
|
2009-11-12 01:35:42 +05:30
|
|
|
fcntl(sfd, F_SETFL, O_RDWR);
|
2008-02-17 05:10:47 +05:30
|
|
|
|
|
|
|
// put device to "raw mode"
|
2008-01-28 04:10:39 +05:30
|
|
|
xget1(sfd, &tio, &tiosfd);
|
|
|
|
// set device speed
|
|
|
|
cfsetspeed(&tio, tty_value_to_baud(speed));
|
|
|
|
if (xset1(sfd, &tio, argv[0]))
|
2008-04-23 21:01:29 +05:30
|
|
|
goto done;
|
|
|
|
|
|
|
|
// put stdin to "raw mode" (if stdin is a TTY),
|
|
|
|
// handle one character at a time
|
|
|
|
if (isatty(STDIN_FILENO)) {
|
|
|
|
xget1(STDIN_FILENO, &tio, &tio0);
|
|
|
|
if (xset1(STDIN_FILENO, &tio, "stdin"))
|
|
|
|
goto done;
|
|
|
|
}
|
2007-09-21 23:12:40 +05:30
|
|
|
|
|
|
|
// main loop: check with poll(), then read/write bytes across
|
2008-01-28 15:09:30 +05:30
|
|
|
pfd[0].fd = sfd;
|
2007-09-21 23:12:40 +05:30
|
|
|
pfd[0].events = POLLIN;
|
2008-01-28 04:10:39 +05:30
|
|
|
pfd[1].fd = STDIN_FILENO;
|
2007-09-21 23:12:40 +05:30
|
|
|
pfd[1].events = POLLIN;
|
2008-01-28 04:10:39 +05:30
|
|
|
|
2008-09-12 01:21:11 +05:30
|
|
|
bb_got_signal = 0;
|
2008-01-28 04:10:39 +05:30
|
|
|
nfd = 2;
|
2010-01-12 16:59:55 +05:30
|
|
|
// Not safe_poll: we want to exit on signal
|
|
|
|
while (!bb_got_signal && poll(pfd, nfd, timeout) > 0) {
|
2010-03-02 19:32:45 +05:30
|
|
|
if (nfd > 1 && pfd[1].revents) {
|
2008-02-11 00:35:56 +05:30
|
|
|
char c;
|
2008-02-08 23:00:39 +05:30
|
|
|
// read from stdin -> write to device
|
|
|
|
if (safe_read(STDIN_FILENO, &c, 1) < 1) {
|
2008-02-09 17:07:21 +05:30
|
|
|
// don't poll stdin anymore if we got EOF/error
|
2008-02-08 23:00:39 +05:30
|
|
|
nfd--;
|
2008-02-17 05:10:47 +05:30
|
|
|
goto skip_write;
|
2008-02-08 23:00:39 +05:30
|
|
|
}
|
|
|
|
// do we need special processing?
|
|
|
|
if (!(opts & OPT_X)) {
|
|
|
|
// ^@ sends Break
|
|
|
|
if (VINTR == c) {
|
|
|
|
tcsendbreak(sfd, 0);
|
2008-02-17 05:10:47 +05:30
|
|
|
goto skip_write;
|
2008-01-28 04:10:39 +05:30
|
|
|
}
|
2008-02-08 23:00:39 +05:30
|
|
|
// ^X exits
|
|
|
|
if (24 == c)
|
|
|
|
break;
|
2007-09-21 23:12:40 +05:30
|
|
|
}
|
2008-02-08 23:00:39 +05:30
|
|
|
write(sfd, &c, 1);
|
2008-02-11 00:35:56 +05:30
|
|
|
if (delay >= 0)
|
|
|
|
safe_poll(pfd, 1, delay);
|
2008-02-17 05:10:47 +05:30
|
|
|
skip_write: ;
|
2008-02-11 00:35:56 +05:30
|
|
|
}
|
2010-03-02 19:32:45 +05:30
|
|
|
if (pfd[0].revents) {
|
2008-02-17 05:10:47 +05:30
|
|
|
#define iobuf bb_common_bufsiz1
|
2008-02-11 00:35:56 +05:30
|
|
|
ssize_t len;
|
|
|
|
// read from device -> write to stdout
|
2008-02-17 05:10:47 +05:30
|
|
|
len = safe_read(sfd, iobuf, sizeof(iobuf));
|
2008-02-11 00:35:56 +05:30
|
|
|
if (len > 0)
|
2008-02-17 05:10:47 +05:30
|
|
|
full_write(STDOUT_FILENO, iobuf, len);
|
|
|
|
else {
|
|
|
|
// EOF/error -> bail out
|
2008-09-12 01:21:11 +05:30
|
|
|
bb_got_signal = SIGHUP;
|
2008-02-17 05:10:47 +05:30
|
|
|
break;
|
|
|
|
}
|
2007-09-21 23:12:40 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-17 05:10:47 +05:30
|
|
|
// restore device mode
|
2008-02-08 23:00:39 +05:30
|
|
|
tcsetattr(sfd, TCSAFLUSH, &tiosfd);
|
2008-01-28 04:10:39 +05:30
|
|
|
|
2008-02-09 17:07:21 +05:30
|
|
|
if (isatty(STDIN_FILENO))
|
2008-02-08 23:00:39 +05:30
|
|
|
tcsetattr(STDIN_FILENO, TCSAFLUSH, &tio0);
|
2007-09-21 23:12:40 +05:30
|
|
|
|
2008-02-08 23:00:39 +05:30
|
|
|
done:
|
|
|
|
if (device_lock_file)
|
2007-09-21 23:12:40 +05:30
|
|
|
unlink(device_lock_file);
|
2008-02-08 23:00:39 +05:30
|
|
|
|
2008-09-12 01:21:11 +05:30
|
|
|
return bb_got_signal;
|
2007-09-21 23:12:40 +05:30
|
|
|
}
|