2 * Simple serial terminal
4 * Copyright 2014, 2015 Michal Sojka <sojkam1@fel.cvut.cz>
6 * This program is free software: you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation, either version 3 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see
18 * <http://www.gnu.org/licenses/>.
22 * This is a minimalist terminal program like minicom or cu. The only
23 * thing it does is creating a bidirectional connection between
24 * stdin/stdout and a device (e.g. serial terminal). It can also set
25 * serial line baudrate and manipulate DTR/RTS modem lines.
27 * The -d and -r option create short pulse on DTR/RTS. The lines are
28 * always raised when the device is opened and those options lower the
29 * lines immediately after opening.
33 #include <sys/ioctl.h>
34 #include <sys/types.h>
47 #define STRINGIFY(val) #val
48 #define TOSTRING(val) STRINGIFY(val)
49 #define CHECK(cmd) ({ int ret = (cmd); if (ret == -1) { perror(#cmd " line " TOSTRING(__LINE__)); exit(1); }; ret; })
50 #define CHECKPTR(cmd) ({ void *ptr = (cmd); if (ptr == (void*)-1) { perror(#cmd " line " TOSTRING(__LINE__)); exit(1); }; ptr; })
52 #define VERBOSE(format, ...) do { if (verbose) fprintf(stderr, "sterm: " format, ##__VA_ARGS__); } while (0)
55 bool exit_on_escape = true;
57 struct termios stdin_tio_backup;
60 void rm_file(int status, void *arg)
68 void restore_stdin_term()
70 tcsetattr(0, TCSANOW, &stdin_tio_backup);
75 dev_unlock(dev, getpid());
78 void sighandler(int arg)
80 exit(0); /* Invoke exit handlers */
83 int dtr_rts_arg(const char option)
89 val = strtol(optarg, &end, 10);
93 case '+': val = +1; break;
94 case '-': val = -1; break;
96 fprintf(stderr, "Unknown -%c argument: %s", option, optarg);
104 void exit_on_escapeseq(const char *buf, int len)
106 static const char escseq[] = "\r~.";
107 static const char *state = escseq+1;
109 for (i = 0; i < len; i++) {
110 if (buf[i] == *state) {
116 if (buf[i] == *state)
122 void usage(const char* argv0)
124 fprintf(stderr, "Usage: %s [options] <device>\n", argv0);
127 " -d[PULSE] make pulse on DTR\n"
128 " -e ignore '~.' escape sequence\n"
129 " -n do not switch the device to raw mode\n"
130 " -r[PULSE] make pulse on RTS\n"
133 "PULSE is a number specifying the pulse. Absolute value defines the\n"
134 "length of the pulse in milliseconds, sign determines the polarity of\n"
135 "the pulse. Alternatively, PULSE can be either '+' or '-', which\n"
136 "corresponds to +1 or -1.\n"
140 void pulse(int fd, int dtr, int rts)
143 /* tio.c_cflag &= ~HUPCL; */ /* Don't lower DTR/RTS on close */
145 CHECK(ioctl(fd, TIOCMGET, &status));
146 if (dtr > 0) { status &= ~TIOCM_DTR; ms = +dtr; }
147 if (dtr < 0) { status |= TIOCM_DTR; ms = -dtr; }
148 if (rts > 0) { status &= ~TIOCM_RTS; ms = +rts; }
149 if (rts < 0) { status |= TIOCM_RTS; ms = -rts; }
150 CHECK(ioctl(fd, TIOCMSET, &status));
154 if (dtr < 0) { status &= ~TIOCM_DTR; }
155 if (dtr > 0) { status |= TIOCM_DTR; }
156 if (rts < 0) { status &= ~TIOCM_RTS; }
157 if (rts > 0) { status |= TIOCM_RTS; }
158 CHECK(ioctl(fd, TIOCMSET, &status));
161 int main(int argc, char *argv[])
166 int dtr = 0, rts = 0;
171 if ((stdin_tty = isatty(0))) {
172 CHECK(tcgetattr(0, &stdin_tio_backup));
173 atexit(restore_stdin_term);
176 while ((opt = getopt(argc, argv, "nd::er::s:v")) != -1) {
178 case 'd': dtr = dtr_rts_arg(opt); break;
179 case 'e': exit_on_escape = false; break;
180 case 'n': raw = false; break;
181 case 'r': rts = dtr_rts_arg(opt); break;
183 int s = atoi(optarg);
185 #define S(s) case s: speed = B##s; break;
207 fprintf(stderr, "Unknown baud rate %d\n", s);
225 fprintf(stderr, "No device specified\n");
230 signal(SIGINT, sighandler);
231 signal(SIGTERM, sighandler);
232 signal(SIGHUP, sighandler);
234 pid_t pid = dev_lock(dev);
236 fprintf(stderr, "%s is used by PID %d\n", dev, pid);
238 } else if (pid < 0) {
239 perror("dev_lock()");
244 /* O_NONBLOCK is needed to not wait for the CDC signal. See tty_ioctl(4). */
245 if ((fd = open(dev, O_RDWR|O_NOCTTY|O_NONBLOCK)) < 0) {
249 /* Cancel the efect of O_NONBLOCK flag. */
250 int n = fcntl(fd, F_GETFL, 0);
251 fcntl(fd, F_SETFL, n & ~O_NDELAY);
254 CHECK(ioctl(fd, TIOCEXCL, NULL));
256 CHECK(tcgetattr(fd, &tio));
261 CHECK(cfsetospeed(&tio, speed));
262 CHECK(cfsetispeed(&tio, speed));
268 /* Disable flow control */
269 tio.c_cflag &= ~(CRTSCTS);
270 tio.c_iflag &= ~(IXON|IXOFF);
272 CHECK(tcsetattr(fd, TCSANOW, &tio));
273 } else if (speed || dtr || rts) {
274 fprintf(stderr, "Cannot set speed, DTR or RTS on non-terminal %s\n", dev);
278 struct pollfd fds[2] = {
279 { .fd = 0, .events = POLLIN },
280 { .fd = fd, .events = POLLIN },
285 tio = stdin_tio_backup;
288 CHECK(tcsetattr(0, TCSANOW, &tio));
291 VERBOSE("Connected.\r\n");
293 VERBOSE("Use '<Enter>~.' sequence to exit.\r\n");
297 CHECK(poll(fds, 2, -1));
298 if (fds[0].revents & POLLIN) {
299 r1 = CHECK(read(0, buf, sizeof(buf)));
301 VERBOSE("EOF on stdin\r\n");
305 exit_on_escapeseq(buf, r1);
306 r2 = CHECK(write(fd, buf, r1));
308 fprintf(stderr, "Not all data written to %s (%d/%d)\n", dev, r1, r2);
312 if (fds[1].revents & POLLIN) {
313 r1 = CHECK(read(fd, buf, sizeof(buf)));
315 VERBOSE("EOF on %s\r\n", dev);
318 r2 = CHECK(write(1, buf, r1));
320 fprintf(stderr, "Not all data written to stdout (%d/%d)\n", r1, r2);