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 case '+': val = +1; break;
90 case '-': val = -1; break;
92 fprintf(stderr, "Unknown -%c argument: %s", option, optarg);
99 void exit_on_escapeseq(const char *buf, int len)
101 static const char escseq[] = "\r~.";
102 static const char *state = escseq+1;
104 for (i = 0; i < len; i++) {
105 if (buf[i] == *state) {
111 if (buf[i] == *state)
117 void usage(const char* argv0)
119 fprintf(stderr, "Usage: %s [options] <device>\n", argv0);
120 fprintf(stderr, "Options:\n");
121 fprintf(stderr, " -d [+|-] create short positive/negative pulse on DTR\n");
122 fprintf(stderr, " -e ignore '~.' escape sequence\n");
123 fprintf(stderr, " -n do not switch the device to raw mode\n");
124 fprintf(stderr, " -r [+|-] create short positive/negative pulse on RTS\n");
125 fprintf(stderr, " -s <baudrate>\n");
126 fprintf(stderr, " -v verbose\n");
129 int main(int argc, char *argv[])
134 int dtr = 0, rts = 0;
139 if ((stdin_tty = isatty(0))) {
140 CHECK(tcgetattr(0, &stdin_tio_backup));
141 atexit(restore_stdin_term);
144 while ((opt = getopt(argc, argv, "nd::er::s:v")) != -1) {
146 case 'd': dtr = dtr_rts_arg(opt); break;
147 case 'e': exit_on_escape = false; break;
148 case 'n': raw = false; break;
149 case 'r': rts = dtr_rts_arg(opt); break;
151 int s = atoi(optarg);
153 #define S(s) case s: speed = B##s; break;
175 fprintf(stderr, "Unknown baud rate %d\n", s);
193 fprintf(stderr, "No device specified\n");
198 signal(SIGINT, sighandler);
199 signal(SIGTERM, sighandler);
200 signal(SIGHUP, sighandler);
202 pid_t pid = dev_lock(dev);
204 fprintf(stderr, "%s is used by PID %d\n", dev, pid);
206 } else if (pid < 0) {
207 perror("dev_lock()");
212 /* O_NONBLOCK is needed to not wait for the CDC signal. See tty_ioctl(4). */
213 if ((fd = open(dev, O_RDWR|O_NOCTTY|O_NONBLOCK)) < 0) {
217 /* Cancel the efect of O_NONBLOCK flag. */
218 int n = fcntl(fd, F_GETFL, 0);
219 fcntl(fd, F_SETFL, n & ~O_NDELAY);
222 CHECK(ioctl(fd, TIOCEXCL, NULL));
224 CHECK(tcgetattr(fd, &tio));
229 CHECK(cfsetospeed(&tio, speed));
230 CHECK(cfsetispeed(&tio, speed));
235 /* tio.c_cflag &= ~HUPCL; */ /* Don't lower DTR/RTS on close */
237 CHECK(ioctl(fd, TIOCMGET, &status));
238 if (dtr == -1) status &= ~TIOCM_DTR;
239 if (dtr == +1) status |= TIOCM_DTR;
240 if (rts == -1) status &= ~TIOCM_RTS;
241 if (rts == +1) status |= TIOCM_RTS;
242 CHECK(ioctl(fd, TIOCMSET, &status));
245 /* Disable flow control */
246 tio.c_cflag &= ~(CRTSCTS);
247 tio.c_iflag &= ~(IXON|IXOFF);
249 CHECK(tcsetattr(fd, TCSANOW, &tio));
250 } else if (speed || dtr || rts) {
251 fprintf(stderr, "Cannot set speed, DTR or RTS on non-terminal %s\n", dev);
255 struct pollfd fds[2] = {
256 { .fd = 0, .events = POLLIN },
257 { .fd = fd, .events = POLLIN },
262 tio = stdin_tio_backup;
265 CHECK(tcsetattr(0, TCSANOW, &tio));
268 VERBOSE("Connected.\r\n");
270 VERBOSE("Use '<Enter>~.' sequence to exit.\r\n");
274 CHECK(poll(fds, 2, -1));
275 if (fds[0].revents & POLLIN) {
276 r1 = CHECK(read(0, buf, sizeof(buf)));
278 VERBOSE("EOF on stdin\r\n");
282 exit_on_escapeseq(buf, r1);
283 r2 = CHECK(write(fd, buf, r1));
285 fprintf(stderr, "Not all data written to %s (%d/%d)\n", dev, r1, r2);
289 if (fds[1].revents & POLLIN) {
290 r1 = CHECK(read(fd, buf, sizeof(buf)));
292 VERBOSE("EOF on %s\r\n", dev);
295 r2 = CHECK(write(1, buf, r1));
297 fprintf(stderr, "Not all data written to stdout (%d/%d)\n", r1, r2);