2 * Simple serial terminal
4 * Copyright 2014, 2015, 2016, 2017 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
34 #include <sys/ioctl.h>
35 #include <sys/types.h>
48 #define STRINGIFY(val) #val
49 #define TOSTRING(val) STRINGIFY(val)
50 #define CHECK(cmd) ({ int ret = (cmd); if (ret == -1) { perror(#cmd " line " TOSTRING(__LINE__)); exit(1); }; ret; })
51 #define CHECKPTR(cmd) ({ void *ptr = (cmd); if (ptr == (void*)-1) { perror(#cmd " line " TOSTRING(__LINE__)); exit(1); }; ptr; })
52 #define CHECKNULL(cmd) ({ void *ptr = (cmd); if (ptr == NULL) { perror(#cmd " line " TOSTRING(__LINE__)); exit(1); }; ptr; })
54 #define VERBOSE(format, ...) do { if (verbose) fprintf(stderr, "sterm: " format, ##__VA_ARGS__); } while (0)
57 bool exit_on_escape = true;
59 struct termios stdin_tio_backup;
62 void rm_file(int status, void *arg)
70 void restore_stdin_term()
72 tcsetattr(0, TCSANOW, &stdin_tio_backup);
77 dev_unlock(dev, getpid());
80 void sighandler(int arg)
82 exit(0); /* Invoke exit handlers */
85 int dtr_rts_arg(const char option, const char *optarg)
91 val = strtol(optarg, &end, 10);
95 case '+': val = +1; break;
96 case '-': val = -1; break;
98 fprintf(stderr, "Unknown -%c argument: %s\n", option, optarg);
106 void exit_on_escapeseq(const char *buf, int len)
108 static const char escseq[] = "\r~.";
109 static const char *state = escseq+1;
111 for (i = 0; i < len; i++) {
112 if (buf[i] == *state) {
118 if (buf[i] == *state)
124 void usage(const char* argv0)
126 fprintf(stderr, "Usage: %s [options] <device>\n", argv0);
129 " -c enter command mode\n"
130 " -d[PULSE] make pulse on DTR\n"
131 " -e ignore '~.' escape sequence\n"
132 " -n do not switch the device to raw mode\n"
133 " -r[PULSE] make pulse on RTS\n"
137 "PULSE is a number specifying the pulse. Absolute value defines the\n"
138 "length of the pulse in milliseconds, sign determines the polarity of\n"
139 "the pulse. Alternatively, PULSE can be either '+' or '-', which\n"
140 "corresponds to +1 or -1.\n"
144 void pulse(int fd, int dtr, int rts)
147 /* tio.c_cflag &= ~HUPCL; */ /* Don't lower DTR/RTS on close */
149 CHECK(ioctl(fd, TIOCMGET, &status));
150 if (dtr > 0) { status &= ~TIOCM_DTR; ms = +dtr; }
151 if (dtr < 0) { status |= TIOCM_DTR; ms = -dtr; }
152 if (rts > 0) { status &= ~TIOCM_RTS; ms = +rts; }
153 if (rts < 0) { status |= TIOCM_RTS; ms = -rts; }
154 CHECK(ioctl(fd, TIOCMSET, &status));
158 if (dtr < 0) { status &= ~TIOCM_DTR; }
159 if (dtr > 0) { status |= TIOCM_DTR; }
160 if (rts < 0) { status &= ~TIOCM_RTS; }
161 if (rts > 0) { status |= TIOCM_RTS; }
162 CHECK(ioctl(fd, TIOCMSET, &status));
165 void handle_commands(int fd)
172 if (fgets(command, sizeof(command), stdin) == NULL) {
174 perror("Command read");
177 if (sscanf(command, "dtr %ms", &p1) == 1)
178 pulse(fd, dtr_rts_arg('d', p1), 0);
179 else if (sscanf(command, "rts %ms", &p1) == 1)
180 pulse(fd, 0, dtr_rts_arg('r', p1));
181 else if (strcmp(command, "go\n") == 0)
184 fprintf(stderr, "Unknown command: %s\n", command);
192 int main(int argc, char *argv[])
197 int dtr = 0, rts = 0;
203 if ((stdin_tty = isatty(0))) {
204 CHECK(tcgetattr(0, &stdin_tio_backup));
205 atexit(restore_stdin_term);
208 while ((opt = getopt(argc, argv, "cnd::er::s:v")) != -1) {
210 case 'c': cmd = true; break;
211 case 'd': dtr = dtr_rts_arg(opt, optarg); break;
212 case 'e': exit_on_escape = false; break;
213 case 'n': raw = false; break;
214 case 'r': rts = dtr_rts_arg(opt, optarg); break;
216 int s = atoi(optarg);
218 #define S(s) case s: speed = B##s; break;
240 fprintf(stderr, "Unknown baud rate %d\n", s);
258 fprintf(stderr, "No device specified\n");
263 signal(SIGINT, sighandler);
264 signal(SIGTERM, sighandler);
265 signal(SIGHUP, sighandler);
267 pid_t pid = dev_lock(dev);
269 fprintf(stderr, "%s is used by PID %d\n", dev, pid);
271 } else if (pid < 0) {
272 perror("dev_lock()");
277 /* O_NONBLOCK is needed to not wait for the CDC signal. See tty_ioctl(4). */
278 if ((fd = open(dev, O_RDWR|O_NOCTTY|O_NONBLOCK)) < 0) {
282 /* Cancel the efect of O_NONBLOCK flag. */
283 int n = fcntl(fd, F_GETFL, 0);
284 fcntl(fd, F_SETFL, n & ~O_NDELAY);
287 CHECK(ioctl(fd, TIOCEXCL, NULL));
289 CHECK(tcgetattr(fd, &tio));
294 CHECK(cfsetospeed(&tio, speed));
295 CHECK(cfsetispeed(&tio, speed));
301 /* Disable flow control */
302 tio.c_cflag &= ~(CRTSCTS);
303 tio.c_iflag &= ~(IXON|IXOFF);
305 CHECK(tcsetattr(fd, TCSANOW, &tio));
306 } else if (speed || dtr || rts) {
307 fprintf(stderr, "Cannot set speed, DTR or RTS on non-terminal %s\n", dev);
311 VERBOSE("Connected.\r\n");
316 struct pollfd fds[2] = {
317 { .fd = 0, .events = POLLIN },
318 { .fd = fd, .events = POLLIN },
323 tio = stdin_tio_backup;
326 CHECK(tcsetattr(0, TCSANOW, &tio));
330 VERBOSE("Use '<Enter>~.' sequence to exit.\r\n");
334 CHECK(poll(fds, 2, -1));
335 if (fds[0].revents & POLLIN) {
336 r1 = CHECK(read(0, buf, sizeof(buf)));
338 VERBOSE("EOF on stdin\r\n");
342 exit_on_escapeseq(buf, r1);
343 r2 = CHECK(write(fd, buf, r1));
345 fprintf(stderr, "Not all data written to %s (%d/%d)\n", dev, r1, r2);
349 if (fds[1].revents & POLLIN) {
350 r1 = CHECK(read(fd, buf, sizeof(buf)));
352 VERBOSE("EOF on %s\r\n", dev);
355 r2 = CHECK(write(1, buf, r1));
357 fprintf(stderr, "Not all data written to stdout (%d/%d)\n", r1, r2);