]> rtime.felk.cvut.cz Git - can-benchmark.git/blob - utils/sterm.c
More messages in verbose mode
[can-benchmark.git] / utils / sterm.c
1 /*
2  * Simple serial terminal
3  *
4  * Copyright 2014 Michal Sojka <sojkam1@fel.cvut.cz>
5  *
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.
10  *
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.
15  *
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/>.
19  */
20
21 /*
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.
26  *
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.
30  */
31
32 #define _BSD_SOURCE
33 #include <sys/ioctl.h>
34 #include <unistd.h>
35 #include <fcntl.h>
36 #include <termios.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <getopt.h>
40 #include <poll.h>
41 #include <stdbool.h>
42 #include <string.h>
43 #include <signal.h>
44
45 #define STRINGIFY(val) #val
46 #define TOSTRING(val) STRINGIFY(val)
47 #define CHECK(cmd) ({ int ret = (cmd); if (ret == -1) { perror(#cmd " line " TOSTRING(__LINE__)); exit(1); }; ret; })
48 #define CHECKPTR(cmd) ({ void *ptr = (cmd); if (ptr == (void*)-1) { perror(#cmd " line " TOSTRING(__LINE__)); exit(1); }; ptr; })
49
50 #define VERBOSE(format, ...) do { if (verbose) fprintf(stderr, "sterm: " format, ##__VA_ARGS__); } while (0)
51
52 bool verbose = false;
53 bool exit_on_escape = true;
54
55 char template[] = "/var/lock/TMPXXXXXX";
56 char lockfile[100];
57 struct termios stdin_tio_backup;
58
59 void rm_file(int status, void *arg)
60 {
61         char *fn = arg;
62         if (fn[0])
63                 unlink(fn);
64         fn[0] = 0;
65 }
66
67 void restore_stdin_term()
68 {
69         tcsetattr(0, TCSANOW, &stdin_tio_backup);
70 }
71
72 void sighandler(int arg)
73 {
74         exit(0); /* Invoke exit handlers */
75 }
76
77 int dtr_rts_arg(const char option)
78 {
79         int val = -1;
80
81         if (optarg) {
82                 switch (optarg[0]) {
83                 case '+': val = +1; break;
84                 case '-': val = -1; break;
85                 default:
86                         fprintf(stderr, "Unknown -%c argument: %s", option, optarg);
87                         exit(1);
88                 }
89         }
90         return val;
91 }
92
93 void exit_on_escapeseq(const char *buf, int len)
94 {
95         static const char escseq[] = "\r~.";
96         static const char *state = escseq+1;
97         int i;
98
99         for (i = 0; i < len; i++) {
100                 if (buf[i] == *state) {
101                         state++;
102                         if (*state == 0)
103                                 exit(0);
104                 } else
105                         state = escseq;
106         }
107 }
108
109 void usage(const char* argv0)
110 {
111         fprintf(stderr, "Usage: %s [options] <device>\n", argv0);
112         fprintf(stderr, "Options:\n");
113         fprintf(stderr, "  -d [+|-] create short positive/negative pulse on DTR\n");
114         fprintf(stderr, "  -e       ignore '~.' escape sequence\n");
115         fprintf(stderr, "  -n       do not switch the device to raw mode\n");
116         fprintf(stderr, "  -r [+|-] create short positive/negative pulse on RTS\n");
117         fprintf(stderr, "  -s <baudrate>\n");
118         fprintf(stderr, "  -v       verbose\n");
119 }
120
121 int main(int argc, char *argv[])
122 {
123         int fd;
124         char *dev = NULL;
125         int opt;
126         speed_t speed = 0;
127         int dtr = 0, rts = 0;
128         struct termios tio;
129         bool stdin_tty;
130         bool raw = true;
131
132         if ((stdin_tty = isatty(0))) {
133                 CHECK(tcgetattr(0, &stdin_tio_backup));
134                 atexit(restore_stdin_term);
135         }
136
137         while ((opt = getopt(argc, argv, "nd::er::s:v")) != -1) {
138                 switch (opt) {
139                 case 'd': dtr = dtr_rts_arg(opt); break;
140                 case 'e': exit_on_escape = false; break;
141                 case 'n': raw = false; break;
142                 case 'r': rts = dtr_rts_arg(opt); break;
143                 case 's': {
144                         int s = atoi(optarg);
145                         switch (s) {
146 #define S(s) case s: speed = B##s; break;
147                                 S(0);
148                                 S(50);
149                                 S(75);
150                                 S(110);
151                                 S(134);
152                                 S(150);
153                                 S(200);
154                                 S(300);
155                                 S(600);
156                                 S(1200);
157                                 S(1800);
158                                 S(2400);
159                                 S(4800);
160                                 S(9600);
161                                 S(19200);
162                                 S(38400);
163                                 S(57600);
164                                 S(115200);
165                                 S(230400);
166 #undef S
167                         default:
168                                 fprintf(stderr, "Unknown baud rate %d\n", s);
169                                 exit(1);
170                         }
171                         break;
172                 }
173                 case 'v':
174                         verbose = true;
175                         break;
176                 default: /* '?' */
177                         usage(argv[0]);
178                         exit(1);
179                 }
180         }
181
182         if (optind < argc)
183                 dev = argv[optind];
184
185         if (!dev) {
186                 fprintf(stderr, "No device specified\n");
187                 usage(argv[0]);
188                 exit(1);
189         }
190
191         signal(SIGINT, sighandler);
192         signal(SIGTERM, sighandler);
193         signal(SIGHUP, sighandler);
194
195         if (strncmp(dev, "/dev/", 5) == 0 &&
196             strrchr(dev, '/') == dev + 4 &&
197             dev[5] != 0)
198         { /* Create lock file (to be inter-operable with other programs) */
199                 /* This is racy, but what we can do - see also comments in uucp / cu */
200                 int tmp = CHECK(mkstemp(template));
201                 on_exit(rm_file, template);
202                 char pid[20];
203                 snprintf(pid, sizeof(pid), "%u", getpid());
204                 CHECK(write(tmp, pid, strlen(pid)));
205                 close(tmp);
206                 snprintf(lockfile, sizeof(lockfile), "/var/lock/LCK..%s", dev + 5);
207         retry:
208                 if (link(template, lockfile) == -1) {
209                         tmp = CHECK(open(lockfile, O_RDONLY));
210                         CHECK(read(tmp, pid, sizeof(pid)));
211                         close(tmp);
212                         int p = atoi(pid);
213                         char proc[50];
214                         snprintf(proc, sizeof(proc), "/proc/%d", p);
215                         if (access(proc, F_OK) == 0) {
216                                 fprintf(stderr, "%s is used by PID %d\n", dev, p);
217                                 exit(1);
218                         }
219                         fprintf(stderr, "Stale lock file %s (PID %d) - removing it!\n", lockfile, p);
220                         CHECK(unlink(lockfile));
221                         goto retry;
222                 }
223                 rm_file(0, template);
224                 on_exit(rm_file, lockfile);
225         }
226
227         if ((fd = open(dev, O_RDWR)) < 0) {
228                 perror(dev);
229                 exit(1);
230         }
231
232         if (isatty(fd)) {
233                 CHECK(ioctl(fd, TIOCEXCL, NULL));
234
235                 CHECK(tcgetattr(fd, &tio));
236
237                 cfmakeraw(&tio);
238
239                 if (speed) {
240                         CHECK(cfsetospeed(&tio, speed));
241                         CHECK(cfsetispeed(&tio, speed));
242                 }
243
244                 if (dtr || rts) {
245                         int status;
246                         /* tio.c_cflag &= ~HUPCL; */ /* Don't lower DTR/RTS on close */
247
248                         CHECK(ioctl(fd, TIOCMGET, &status));
249                         if (dtr == -1) status &= ~TIOCM_DTR;
250                         if (dtr == +1) status |=  TIOCM_DTR;
251                         if (rts == -1) status &= ~TIOCM_RTS;
252                         if (rts == +1) status |=  TIOCM_RTS;
253                         CHECK(ioctl(fd, TIOCMSET, &status));
254                 }
255
256                 CHECK(tcsetattr(fd, TCSANOW, &tio));
257         } else if (speed || dtr || rts) {
258                 fprintf(stderr, "Cannot set speed, DTR or RTS on non-terminal %s\n", dev);
259                 exit(1);
260         }
261
262         struct pollfd fds[2] = {
263                 { .fd = 0,  .events = POLLIN },
264                 { .fd = fd, .events = POLLIN },
265         };
266         char buf[4096];
267
268         if (stdin_tty) {
269                 tio = stdin_tio_backup;
270                 if (raw)
271                         cfmakeraw(&tio);
272                 CHECK(tcsetattr(0, TCSANOW, &tio));
273         }
274
275         VERBOSE("Connected.\r\n");
276         if (exit_on_escape)
277                 VERBOSE("Use '<Enter>~.' sequence to exit.\r\n");
278
279         while (1) {
280                 int r1, r2;
281                 CHECK(poll(fds, 2, -1));
282                 if (fds[0].revents & POLLIN) {
283                         r1 = CHECK(read(0, buf, sizeof(buf)));
284                         if (r1 == 0) {
285                                 VERBOSE("EOF on stdin\r\n");
286                                 break;
287                         }
288                         if (exit_on_escape)
289                                 exit_on_escapeseq(buf, r1);
290                         r2 = CHECK(write(fd, buf, r1));
291                         if (r1 != r2) {
292                                 fprintf(stderr, "Not all data written to %s (%d/%d)\n", dev, r1, r2);
293                                 exit(1);
294                         }
295                 }
296                 if (fds[1].revents & POLLIN) {
297                         r1 = CHECK(read(fd, buf, sizeof(buf)));
298                         if (r1 == 0) {
299                                 VERBOSE("EOF on %s\r\n", dev);
300                                 break;
301                         }
302                         r2 = CHECK(write(1, buf, r1));
303                         if (r1 != r2) {
304                                 fprintf(stderr, "Not all data written to stdout (%d/%d)\n", r1, r2);
305                                 exit(1);
306                         }
307                 }
308         }
309         return 0;
310 }