2 * Simple serial terminal
4 * Copyright 2014, 2015, 2016, 2017, 2019, 2020, 2021 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 #define _DEFAULT_SOURCE
35 #include <sys/ioctl.h>
36 #include <sys/types.h>
55 #define STRINGIFY(val) #val
56 #define TOSTRING(val) STRINGIFY(val)
57 #define CHECK(cmd) ({ int ret = (cmd); if (ret == -1) { perror(#cmd " line " TOSTRING(__LINE__)); exit(1); }; ret; })
58 #define CHECKPTR(cmd) ({ void *ptr = (cmd); if (ptr == (void*)-1) { perror(#cmd " line " TOSTRING(__LINE__)); exit(1); }; ptr; })
59 #define CHECKNULL(cmd) ({ void *ptr = (cmd); if (ptr == NULL) { perror(#cmd " line " TOSTRING(__LINE__)); exit(1); }; ptr; })
61 #define VERBOSE(format, ...) do { if (verbose) fprintf(stderr, "sterm: " format, ##__VA_ARGS__); } while (0)
63 #define MAX(X, Y) (((X) > (Y)) ? (X) : (Y))
66 bool exit_on_escape = true;
68 struct termios stdin_tio_backup;
71 void rm_file(int status, void *arg)
79 void restore_stdin_term()
81 tcsetattr(0, TCSANOW, &stdin_tio_backup);
87 dev_unlock(dev, getpid());
91 void sighandler(int arg)
93 exit(0); /* Invoke exit handlers */
96 int dtr_rts_arg(const char option, const char *optarg)
102 val = strtol(optarg, &end, 10);
106 case '+': val = +1; break;
107 case '-': val = -1; break;
109 fprintf(stderr, "Unknown -%c argument: %s\n", option, optarg);
117 // See DSR and CPR at
118 // https://en.wikipedia.org/wiki/ANSI_escape_code#Terminal_output_sequences
119 bool is_cpr_control_seq(char c)
121 static enum state { CSI_ESC, CSI_BRACKET, PAR_N, PAR_M, FIN_R } state;
124 case CSI_ESC: state = (c == 0x1b) ? CSI_BRACKET : CSI_ESC; break;
125 case CSI_BRACKET: state = (c == '[') ? PAR_N : CSI_ESC; break;
126 case PAR_N: state = (c == ';') ? PAR_M : (c >= '0' && c <= '9' ? PAR_N : CSI_ESC); break;
127 case PAR_M: state = (c == 'R') ? FIN_R : (c >= '0' && c <= '9' ? PAR_M : CSI_ESC); break;
130 if (state == FIN_R) {
135 return state != CSI_ESC;
138 void exit_on_escapeseq(const char *buf, int len)
140 static const char escseq[] = "\r~.";
141 static const char *state = escseq+1;
143 for (i = 0; i < len; i++) {
144 if (is_cpr_control_seq(buf[i]))
146 if (buf[i] == *state) {
152 if (buf[i] == *state)
158 void print_usage_and_exit(const char* argv0, int exit_code)
160 fprintf(exit_code == 0 ? stdout : stderr, "Usage: %s [options] <device>\n", argv0);
161 fprintf(exit_code == 0 ? stdout : stderr,
163 " -b <duration> send break signal\n"
164 " -c enter command mode\n"
165 " -d[PULSE] make pulse on DTR\n"
166 " -e ignore '~.' escape sequence\n"
167 " -h, --help print help and exit\n"
168 " -n do not switch stdin TTY to raw mode\n"
169 " -r[PULSE] make pulse on RTS\n"
171 " -t <ms> minimum delay between two transmitted characters\n"
173 " -V, --version print version and exit\n"
175 "PULSE is a number specifying the pulse. Absolute value defines the\n"
176 "length of the pulse in milliseconds, sign determines the polarity of\n"
177 "the pulse. Alternatively, PULSE can be either '+' or '-', which\n"
178 "corresponds to '+1' or '-1'.\n"
183 void pulse(int fd, int dtr, int rts)
186 /* tio.c_cflag &= ~HUPCL; */ /* Don't lower DTR/RTS on close */
188 CHECK(ioctl(fd, TIOCMGET, &status));
189 if (dtr > 0) { status &= ~TIOCM_DTR; ms = +dtr; }
190 if (dtr < 0) { status |= TIOCM_DTR; ms = -dtr; }
191 if (rts > 0) { status &= ~TIOCM_RTS; ms = +rts; }
192 if (rts < 0) { status |= TIOCM_RTS; ms = -rts; }
193 CHECK(ioctl(fd, TIOCMSET, &status));
197 if (dtr < 0) { status &= ~TIOCM_DTR; }
198 if (dtr > 0) { status |= TIOCM_DTR; }
199 if (rts < 0) { status &= ~TIOCM_RTS; }
200 if (rts > 0) { status |= TIOCM_RTS; }
201 CHECK(ioctl(fd, TIOCMSET, &status));
204 void handle_commands(int fd)
212 if (fgets(command, sizeof(command), stdin) == NULL) {
214 perror("Command read");
217 if (sscanf(command, "dtr %ms", &p1) == 1)
218 pulse(fd, dtr_rts_arg('d', p1), 0);
219 else if (sscanf(command, "rts %ms", &p1) == 1)
220 pulse(fd, 0, dtr_rts_arg('r', p1));
221 else if (sscanf(command, "break %d", &num) == 1)
222 CHECK(tcsendbreak(fd, num));
223 else if (strcmp(command, "go\n") == 0)
225 else if (strcmp(command, "exit\n") == 0)
228 fprintf(stderr, "Unknown command: %s\n", command);
236 static int64_t now_us()
239 CHECK(clock_gettime(CLOCK_MONOTONIC, &ts));
240 return ts.tv_sec * 1000000 + ts.tv_nsec / 1000;
243 int main(int argc, char *argv[])
248 int dtr = 0, rts = 0;
256 if ((stdin_tty = isatty(0))) {
257 CHECK(tcgetattr(0, &stdin_tio_backup));
258 atexit(restore_stdin_term);
261 static struct option long_options[] = {
262 {"help", no_argument, 0, 'h' },
263 {"version", no_argument, 0, 'V' },
267 while ((opt = getopt_long(argc, argv, "b:cnd::er::s:vVt:h",
268 long_options, NULL)) != -1) {
270 case 'b': break_dur = atoi(optarg); break;
271 case 'c': cmd = true; break;
272 case 'd': dtr = dtr_rts_arg(opt, optarg); break;
273 case 'e': exit_on_escape = false; break;
274 case 'n': raw = false; break;
275 case 'r': rts = dtr_rts_arg(opt, optarg); break;
277 int s = atoi(optarg);
279 #define S(s) case s: speed = B##s; break;
313 fprintf(stderr, "Unknown baud rate %d\n", s);
319 tx_delay_ms = atoi(optarg);
325 printf("sterm version %s\n", strlen(STERM_VERSION) > 0 ? STERM_VERSION : "unknown");
330 print_usage_and_exit(argv[0], opt == 'h' ? 0 : 1);
338 fprintf(stderr, "No device specified\n");
339 print_usage_and_exit(argv[0], 1);
342 signal(SIGINT, sighandler);
343 signal(SIGTERM, sighandler);
344 signal(SIGHUP, sighandler);
347 pid_t pid = dev_lock(dev);
349 fprintf(stderr, "%s is used by PID %d\n", dev, pid);
351 } else if (pid < 0) {
353 asprintf(&msg, "dev_lock('%s')", dev); /* No free() because we exit() immediately */
357 fprintf(stderr, "%s: Error\n", msg);
363 /* O_NONBLOCK is needed to not wait for the CDC signal. See tty_ioctl(4). */
364 if ((fd = open(dev, O_RDWR|O_NOCTTY|O_NONBLOCK)) < 0) {
368 /* Cancel the efect of O_NONBLOCK flag. */
369 int n = fcntl(fd, F_GETFL, 0);
370 fcntl(fd, F_SETFL, n & ~O_NDELAY);
375 CHECK(ioctl(fd, TIOCEXCL, NULL));
377 CHECK(tcgetattr(fd, &tio));
382 CHECK(cfsetospeed(&tio, speed));
383 CHECK(cfsetispeed(&tio, speed));
390 CHECK(tcsendbreak(fd, break_dur));
392 /* Disable flow control */
393 tio.c_cflag &= ~(CRTSCTS);
394 tio.c_iflag &= ~(IXON|IXOFF);
396 CHECK(tcsetattr(fd, TCSANOW, &tio));
397 } else if (speed || dtr || rts) {
398 fprintf(stderr, "Cannot set speed, DTR or RTS on non-terminal %s\n", dev);
402 VERBOSE("Connected.\r\n");
409 struct pollfd fds[2] = {
410 [STDIN] = { .fd = STDIN_FILENO, .events = POLLIN },
411 [DEV] = { .fd = fd, .events = POLLIN },
415 tio = stdin_tio_backup;
418 CHECK(tcsetattr(0, TCSANOW, &tio));
422 VERBOSE("Use '<Enter>~.' sequence to exit.\r\n");
425 int buf_len = 0, buf_idx = 0;
426 int64_t last_tx_us = 0;
428 int timeout = (tx_delay_ms == 0 || buf_len == 0)
430 : MAX(0, tx_delay_ms - (now_us() - last_tx_us) / 1000);
432 CHECK(poll(fds, 2, timeout));
433 if (fds[STDIN].revents & POLLIN && buf_len == 0) {
434 buf_len = CHECK(read(STDIN_FILENO, buf2dev, sizeof(buf2dev)));
436 VERBOSE("EOF on stdin\r\n");
441 exit_on_escapeseq(buf2dev, buf_len);
445 bool short_write = false;
446 if (tx_delay_ms == 0) {
447 wlen = CHECK(write(fd, buf2dev, buf_len));
448 short_write = wlen != buf_len;
451 int64_t now = now_us();
452 if (now - last_tx_us >= tx_delay_ms * 1000) {
453 wlen = CHECK(write(fd, &buf2dev[buf_idx], 1));
454 short_write = wlen != 1;
459 fprintf(stderr, "Not all data written to %s (%d/%d)\n", dev, buf_len, wlen);
463 if (buf_idx >= buf_len)
466 if (fds[STDIN].revents & POLLHUP) {
467 VERBOSE("HUP on stdin\r\n");
470 if (fds[DEV].revents & POLLIN) {
473 rlen = CHECK(read(fd, buf, sizeof(buf)));
475 VERBOSE("EOF on %s\r\n", dev);
478 wlen = CHECK(write(STDOUT_FILENO, buf, rlen));
480 fprintf(stderr, "Not all data written to stdout (%d/%d)\n", wlen, rlen);