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 tarball for details.
12 /* All known arches use small ints for signals */
13 static volatile smallint signalled;
15 static void signal_handler(int signo)
21 static void xget1(int fd, struct termios *t, struct termios *oldt)
26 // t->c_lflag &= ~(ISIG|ICANON|ECHO|IEXTEN);
27 // t->c_iflag &= ~(BRKINT|IXON|ICRNL);
28 // t->c_oflag &= ~(ONLCR);
30 // t->c_cc[VTIME] = 0;
33 static int xset1(int fd, struct termios *tio, const char *device)
35 int ret = tcsetattr(fd, TCSAFLUSH, tio);
38 bb_perror_msg("can't tcsetattr for %s", device);
43 int microcom_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
44 int microcom_main(int argc, char **argv)
49 struct termios tio0, tiosfd, tio;
50 char *device_lock_file;
52 OPT_X = 1 << 0, // do not respect Ctrl-X, Ctrl-@
53 OPT_s = 1 << 1, // baudrate
54 OPT_d = 1 << 2, // wait for device response, ms
55 OPT_t = 1 << 3, // timeout, ms
63 opt_complementary = "=1:s+:d+:t+"; // exactly one arg, numeric options
64 opts = getopt32(argv, "Xs:d:t:", &speed, &delay, &timeout);
68 // try to create lock file in /var/lock
69 device_lock_file = (char *)bb_basename(argv[0]);
70 device_lock_file = xasprintf("/var/lock/LCK..%s", device_lock_file);
71 sfd = open(device_lock_file, O_CREAT | O_WRONLY | O_TRUNC | O_EXCL, 0644);
73 // device already locked -> bail out
75 bb_perror_msg_and_die("can't create %s", device_lock_file);
76 // can't create lock -> don't care
77 if (ENABLE_FEATURE_CLEAN_UP)
78 free(device_lock_file);
79 device_lock_file = NULL;
81 // %4d to make concurrent mgetty (if any) happy.
82 // Mgetty treats 4-bytes lock files as binary,
83 // not text, PID. Making 5+ char file. Brrr...
84 fdprintf(sfd, "%4d\n", getpid());
96 // error exit code if we fail to open the device
99 // put stdin to "raw mode" (if stdin is a TTY),
100 // handle one character at a time
101 if (isatty(STDIN_FILENO)) {
102 xget1(STDIN_FILENO, &tio, &tio0);
103 if (xset1(STDIN_FILENO, &tio, "stdin"))
108 sfd = open_or_warn(argv[0], O_RDWR | O_NOCTTY | O_NONBLOCK);
111 fcntl(sfd, F_SETFL, 0);
113 // put device to "raw mode"
114 xget1(sfd, &tio, &tiosfd);
115 // tio.c_cflag |= (CREAD|HUPCL); // we just bail out on any device error
117 cfsetspeed(&tio, tty_value_to_baud(speed));
118 if (xset1(sfd, &tio, argv[0]))
119 goto restore0_and_done;
121 // main loop: check with poll(), then read/write bytes across
123 pfd[0].events = POLLIN;
124 pfd[1].fd = STDIN_FILENO;
125 pfd[1].events = POLLIN;
129 while (!signalled && safe_poll(pfd, nfd, timeout) > 0) {
130 if (nfd > 1 && pfd[1].revents) {
132 // read from stdin -> write to device
133 if (safe_read(STDIN_FILENO, &c, 1) < 1) {
134 // don't poll stdin anymore if we got EOF/error
138 // do we need special processing?
139 if (!(opts & OPT_X)) {
151 safe_poll(pfd, 1, delay);
154 if (pfd[0].revents) {
155 #define iobuf bb_common_bufsiz1
157 // read from device -> write to stdout
158 len = safe_read(sfd, iobuf, sizeof(iobuf));
160 full_write(STDOUT_FILENO, iobuf, len);
162 // EOF/error -> bail out
169 // restore device mode
170 tcsetattr(sfd, TCSAFLUSH, &tiosfd);
173 if (isatty(STDIN_FILENO))
174 tcsetattr(STDIN_FILENO, TCSAFLUSH, &tio0);
177 if (device_lock_file)
178 unlink(device_lock_file);