2007-09-22 20:35:32 +00:00
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
2007-09-21 17:42:40 +00:00
|
|
|
* bare bones 'talk to modem' program - similar to 'cu -l $device'
|
|
|
|
* inspired by mgetty's microcom
|
|
|
|
*
|
2007-09-22 20:23:57 +00:00
|
|
|
* Copyright (C) 2007 by Vladimir Dronnikov <dronnikov@gmail.ru>
|
|
|
|
*
|
|
|
|
* Licensed under GPLv2, see file LICENSE in this tarball for details.
|
2007-09-21 17:42:40 +00:00
|
|
|
*/
|
2007-09-22 21:37:27 +00:00
|
|
|
#include "libbb.h"
|
2007-09-21 17:42:40 +00:00
|
|
|
|
2008-01-27 22:40:39 +00:00
|
|
|
/* All known arches use small ints for signals */
|
|
|
|
static volatile smallint signalled;
|
|
|
|
|
|
|
|
static void signal_handler(int signo)
|
|
|
|
{
|
|
|
|
signalled = signo;
|
|
|
|
}
|
|
|
|
|
2008-02-08 17:30:39 +00:00
|
|
|
// set raw tty mode
|
2008-01-27 22:40:39 +00:00
|
|
|
static void xget1(int fd, struct termios *t, struct termios *oldt)
|
|
|
|
{
|
|
|
|
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 17:30:39 +00:00
|
|
|
int ret = tcsetattr(fd, TCSAFLUSH, tio);
|
2008-01-27 22:40:39 +00:00
|
|
|
|
|
|
|
if (ret) {
|
|
|
|
bb_perror_msg("can't tcsetattr for %s", device);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-10-11 10:05:36 +00:00
|
|
|
int microcom_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2007-09-21 17:42:40 +00:00
|
|
|
int microcom_main(int argc, char **argv)
|
|
|
|
{
|
2008-01-28 09:39:30 +00:00
|
|
|
int sfd;
|
2008-02-08 17:30:39 +00:00
|
|
|
int nfd;
|
|
|
|
struct pollfd pfd[2];
|
2007-09-21 17:42:40 +00:00
|
|
|
struct termios tio0, tiosfd, tio;
|
2008-02-08 17:30:39 +00:00
|
|
|
char *device_lock_file;
|
|
|
|
enum {
|
|
|
|
OPT_X = 1 << 0, // do not respect Ctrl-X, Ctrl-@
|
|
|
|
OPT_s = 1 << 1, // baudrate
|
2008-02-09 11:37:21 +00:00
|
|
|
OPT_d = 1 << 2 // wait for device response, msecs
|
|
|
|
OPT_t = 1 << 3 // timeout, ms
|
2008-02-08 17:30:39 +00:00
|
|
|
};
|
|
|
|
speed_t speed = 9600;
|
2008-02-09 11:37:21 +00:00
|
|
|
int delay = -1;
|
|
|
|
int timeout = -1;
|
2007-09-21 17:42:40 +00:00
|
|
|
|
2008-01-27 22:40:39 +00:00
|
|
|
// fetch options
|
2008-02-08 17:30:39 +00:00
|
|
|
char *opt_s;
|
|
|
|
char *opt_t;
|
|
|
|
unsigned opts;
|
|
|
|
opt_complementary = "=1"; /* exactly one arg should be there */
|
2008-02-09 11:37:21 +00:00
|
|
|
opts = getopt32(argv, "Xs:d:t:", &opt_s, &opt_d, &opt_t);
|
|
|
|
|
2008-02-08 17:30:39 +00:00
|
|
|
// apply options
|
|
|
|
if (opts & OPT_s)
|
|
|
|
speed = xatoi_u(opt_s);
|
2008-02-09 11:37:21 +00:00
|
|
|
if (opts & OPT_d)
|
|
|
|
delay = xatoi_u(opt_d);
|
2008-02-08 17:30:39 +00:00
|
|
|
if (opts & OPT_t)
|
|
|
|
timeout = xatoi_u(opt_t);
|
2008-02-09 11:37:21 +00:00
|
|
|
|
2008-02-08 17:30:39 +00:00
|
|
|
// argc -= optind;
|
2007-09-21 17:42:40 +00:00
|
|
|
argv += optind;
|
2008-01-27 22:40:39 +00:00
|
|
|
|
2007-09-21 17:42:40 +00:00
|
|
|
// try to create lock file in /var/lock
|
2008-02-08 17:30:39 +00:00
|
|
|
device_lock_file = (char *)bb_basename(argv[0]);
|
2008-01-27 22:40:39 +00:00
|
|
|
device_lock_file = xasprintf("/var/lock/LCK..%s", device_lock_file);
|
2007-09-21 17:42:40 +00:00
|
|
|
sfd = open(device_lock_file, O_CREAT | O_WRONLY | O_TRUNC | O_EXCL, 0644);
|
|
|
|
if (sfd < 0) {
|
|
|
|
if (errno == EEXIST)
|
2008-02-08 17:30:39 +00:00
|
|
|
bb_perror_msg_and_die("can't create %s", device_lock_file);
|
2008-01-27 22:40:39 +00:00
|
|
|
if (ENABLE_FEATURE_CLEAN_UP)
|
|
|
|
free(device_lock_file);
|
|
|
|
device_lock_file = NULL;
|
2008-02-08 17:30:39 +00:00
|
|
|
}
|
|
|
|
if (sfd > 0) {
|
2007-09-21 17:42:40 +00:00
|
|
|
// %4d to make mgetty happy. It treats 4-bytes lock files as binary,
|
|
|
|
// not text, PID. Making 5+ char file. Brrr...
|
2008-01-27 22:40:39 +00:00
|
|
|
char *s = xasprintf("%4d\n", getpid());
|
2007-09-21 17:42:40 +00:00
|
|
|
write(sfd, s, strlen(s));
|
|
|
|
if (ENABLE_FEATURE_CLEAN_UP)
|
2008-01-27 22:40:39 +00:00
|
|
|
free(s);
|
2007-09-21 17:42:40 +00:00
|
|
|
close(sfd);
|
|
|
|
}
|
|
|
|
|
2008-01-27 22:40:39 +00:00
|
|
|
// setup signals
|
|
|
|
sig_catch(SIGHUP, signal_handler);
|
|
|
|
sig_catch(SIGINT, signal_handler);
|
|
|
|
sig_catch(SIGTERM, signal_handler);
|
|
|
|
sig_catch(SIGPIPE, signal_handler);
|
2007-09-21 17:42:40 +00:00
|
|
|
|
2008-01-27 22:40:39 +00:00
|
|
|
// error exit code if we fail to open the device
|
|
|
|
signalled = 1;
|
2007-09-21 17:42:40 +00:00
|
|
|
|
2008-01-27 22:40:39 +00:00
|
|
|
// open device
|
|
|
|
sfd = open_or_warn(argv[0], O_RDWR | O_NOCTTY | O_NONBLOCK);
|
|
|
|
if (sfd < 0)
|
2008-02-08 17:30:39 +00:00
|
|
|
goto done;
|
2008-01-27 22:40:39 +00:00
|
|
|
fcntl(sfd, F_SETFL, O_RDWR | O_NOCTTY);
|
2007-09-21 17:42:40 +00:00
|
|
|
|
2008-02-09 11:37:21 +00:00
|
|
|
// put stdin to "raw mode" (if stdin is a TTY),
|
|
|
|
// handle one character at a time
|
2008-02-08 17:30:39 +00:00
|
|
|
if (isatty(STDIN_FILENO)) {
|
2008-01-27 22:40:39 +00:00
|
|
|
xget1(STDIN_FILENO, &tio, &tio0);
|
|
|
|
if (xset1(STDIN_FILENO, &tio, "stdin"))
|
2008-02-08 17:30:39 +00:00
|
|
|
goto done;
|
2008-01-27 22:40:39 +00:00
|
|
|
}
|
2007-09-21 17:42:40 +00:00
|
|
|
|
2008-01-27 22:40:39 +00:00
|
|
|
// same thing for modem
|
|
|
|
xget1(sfd, &tio, &tiosfd);
|
|
|
|
// order device to hang up at exit
|
|
|
|
tio.c_cflag |= (CREAD|HUPCL);
|
2008-02-08 17:30:39 +00:00
|
|
|
// if (!istty)
|
|
|
|
// tio.c_iflag |= (IGNCR);
|
2008-01-27 22:40:39 +00:00
|
|
|
// set device speed
|
|
|
|
cfsetspeed(&tio, tty_value_to_baud(speed));
|
|
|
|
if (xset1(sfd, &tio, argv[0]))
|
2008-02-08 17:30:39 +00:00
|
|
|
goto restore0_and_done;
|
2007-09-21 17:42:40 +00:00
|
|
|
|
|
|
|
// main loop: check with poll(), then read/write bytes across
|
2008-01-28 09:39:30 +00:00
|
|
|
pfd[0].fd = sfd;
|
2007-09-21 17:42:40 +00:00
|
|
|
pfd[0].events = POLLIN;
|
2008-01-27 22:40:39 +00:00
|
|
|
pfd[1].fd = STDIN_FILENO;
|
2007-09-21 17:42:40 +00:00
|
|
|
pfd[1].events = POLLIN;
|
2008-01-27 22:40:39 +00:00
|
|
|
|
|
|
|
signalled = 0;
|
|
|
|
nfd = 2;
|
2008-02-08 17:30:39 +00:00
|
|
|
while (!signalled && safe_poll(pfd, nfd, timeout) > 0) {
|
|
|
|
char c;
|
2008-02-09 11:37:21 +00:00
|
|
|
if (pfd[0].revents) {
|
|
|
|
serial_ready:
|
2008-02-08 17:30:39 +00:00
|
|
|
// read from device -> write to stdout
|
|
|
|
if (safe_read(sfd, &c, 1) > 0)
|
|
|
|
write(STDOUT_FILENO, &c, 1);
|
2008-02-09 11:37:21 +00:00
|
|
|
// else { EOF/error - what to do? }
|
2008-02-08 17:30:39 +00:00
|
|
|
}
|
2008-02-09 11:37:21 +00:00
|
|
|
if (pfd[1].revents) {
|
|
|
|
pfd[1].revents = 0;
|
2008-02-08 17:30:39 +00:00
|
|
|
// read from stdin -> write to device
|
|
|
|
if (safe_read(STDIN_FILENO, &c, 1) < 1) {
|
2008-02-09 11:37:21 +00:00
|
|
|
// don't poll stdin anymore if we got EOF/error
|
2008-02-08 17:30:39 +00:00
|
|
|
nfd--;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// do we need special processing?
|
|
|
|
if (!(opts & OPT_X)) {
|
|
|
|
// ^@ sends Break
|
|
|
|
if (VINTR == c) {
|
|
|
|
tcsendbreak(sfd, 0);
|
2008-01-27 22:40:39 +00:00
|
|
|
continue;
|
|
|
|
}
|
2008-02-08 17:30:39 +00:00
|
|
|
// ^X exits
|
|
|
|
if (24 == c)
|
|
|
|
break;
|
2007-09-21 17:42:40 +00:00
|
|
|
}
|
2008-02-08 17:30:39 +00:00
|
|
|
write(sfd, &c, 1);
|
2008-02-09 11:37:21 +00:00
|
|
|
if (delay >= 0 && safe_poll(pfd, 1, delay) > 0)
|
|
|
|
goto serial_ready;
|
2007-09-21 17:42:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-08 17:30:39 +00:00
|
|
|
tcsetattr(sfd, TCSAFLUSH, &tiosfd);
|
2008-01-27 22:40:39 +00:00
|
|
|
|
2008-02-08 17:30:39 +00:00
|
|
|
restore0_and_done:
|
2008-02-09 11:37:21 +00:00
|
|
|
if (isatty(STDIN_FILENO))
|
2008-02-08 17:30:39 +00:00
|
|
|
tcsetattr(STDIN_FILENO, TCSAFLUSH, &tio0);
|
2007-09-21 17:42:40 +00:00
|
|
|
|
2008-02-08 17:30:39 +00:00
|
|
|
done:
|
|
|
|
if (device_lock_file)
|
2007-09-21 17:42:40 +00:00
|
|
|
unlink(device_lock_file);
|
2008-02-08 17:30:39 +00:00
|
|
|
|
2008-01-27 22:40:39 +00:00
|
|
|
return signalled;
|
2007-09-21 17:42:40 +00:00
|
|
|
}
|