]> rtime.felk.cvut.cz Git - can-benchmark.git/blob - utils/sterm.c
Add when the graps were generated
[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, format, ##__VA_ARGS__); } while (0)
51
52 bool verbose = false;
53
54 char template[] = "/var/lock/TMPXXXXXX";
55 char lockfile[100];
56 struct termios stdin_tio_backup;
57
58 void rm_file(int status, void *arg)
59 {
60         char *fn = arg;
61         if (fn[0])
62                 unlink(fn);
63         fn[0] = 0;
64 }
65
66 void restore_stdin_term()
67 {
68         tcsetattr(0, TCSANOW, &stdin_tio_backup);
69 }
70
71 void sighandler(int arg)
72 {
73         exit(0); /* Invoke exit handlers */
74 }
75
76 int dtr_rts_arg(const char option)
77 {
78         int val = -1;
79
80         if (optarg) {
81                 switch (optarg[0]) {
82                 case '+': val = +1; break;
83                 case '-': val = -1; break;
84                 default:
85                         fprintf(stderr, "Unknown -%c argument: %s", option, optarg);
86                         exit(1);
87                 }
88         }
89         return val;
90 }
91
92
93 int main(int argc, char *argv[])
94 {
95         int fd;
96         char *dev = NULL;
97         int opt;
98         speed_t speed = 0;
99         int dtr = 0, rts = 0;
100         struct termios tio;
101         bool stdin_tty;
102         bool raw = true;
103
104         if ((stdin_tty = isatty(0))) {
105                 CHECK(tcgetattr(0, &stdin_tio_backup));
106                 atexit(restore_stdin_term);
107         }
108
109         while ((opt = getopt(argc, argv, "nd::r::s:v")) != -1) {
110                 switch (opt) {
111                 case 'd': dtr = dtr_rts_arg(opt); break;
112                 case 'n': raw = false; break;
113                 case 'r': rts = dtr_rts_arg(opt); break;
114                 case 's': {
115                         int s = atoi(optarg);
116                         switch (s) {
117 #define S(s) case s: speed = B##s; break;
118                                 S(0);
119                                 S(50);
120                                 S(75);
121                                 S(110);
122                                 S(134);
123                                 S(150);
124                                 S(200);
125                                 S(300);
126                                 S(600);
127                                 S(1200);
128                                 S(1800);
129                                 S(2400);
130                                 S(4800);
131                                 S(9600);
132                                 S(19200);
133                                 S(38400);
134                                 S(57600);
135                                 S(115200);
136                                 S(230400);
137 #undef S
138                         default:
139                                 fprintf(stderr, "Unknown baud rate %d\n", s);
140                                 exit(1);
141                         }
142                         break;
143                 }
144                 case 'v':
145                         verbose = true;
146                         break;
147                 default: /* '?' */
148                         fprintf(stderr, "Usage: %s [-s baudrate] [-v] <device>\n", argv[0]);
149                         exit(1);
150                 }
151         }
152
153         if (optind < argc)
154                 dev = argv[optind];
155
156         if (!dev) {
157                 fprintf(stderr, "No device specified\n");
158                 exit(1);
159         }
160
161         signal(SIGINT, sighandler);
162         signal(SIGTERM, sighandler);
163         signal(SIGHUP, sighandler);
164
165         if (strncmp(dev, "/dev/", 5) == 0 &&
166             strrchr(dev, '/') == dev + 4 &&
167             dev[5] != 0)
168         { /* Create lock file (to be inter-operable with other programs) */
169                 /* This is racy, but what we can do - see also comments in uucp / cu */
170                 int tmp = CHECK(mkstemp(template));
171                 on_exit(rm_file, template);
172                 char pid[20];
173                 snprintf(pid, sizeof(pid), "%u", getpid());
174                 CHECK(write(tmp, pid, strlen(pid)));
175                 close(tmp);
176                 snprintf(lockfile, sizeof(lockfile), "/var/lock/LCK..%s", dev + 5);
177         retry:
178                 if (link(template, lockfile) == -1) {
179                         tmp = CHECK(open(lockfile, O_RDONLY));
180                         CHECK(read(tmp, pid, sizeof(pid)));
181                         close(tmp);
182                         int p = atoi(pid);
183                         char proc[50];
184                         snprintf(proc, sizeof(proc), "/proc/%d", p);
185                         if (access(proc, F_OK) == 0) {
186                                 fprintf(stderr, "%s is used by PID %d\n", dev, p);
187                                 exit(1);
188                         }
189                         fprintf(stderr, "Stale lock file %s (PID %d) - removing it!\n", lockfile, p);
190                         CHECK(unlink(lockfile));
191                         goto retry;
192                 }
193                 rm_file(0, template);
194                 on_exit(rm_file, lockfile);
195         }
196
197         if ((fd = open(dev, O_RDWR)) < 0) {
198                 perror(dev);
199                 exit(1);
200         }
201
202         if (isatty(fd)) {
203                 CHECK(ioctl(fd, TIOCEXCL, NULL));
204
205                 CHECK(tcgetattr(fd, &tio));
206
207                 cfmakeraw(&tio);
208
209                 if (speed) {
210                         CHECK(cfsetospeed(&tio, speed));
211                         CHECK(cfsetispeed(&tio, speed));
212                 }
213
214                 if (dtr || rts) {
215                         int status;
216                         /* tio.c_cflag &= ~HUPCL; */ /* Don't lower DTR/RTS on close */
217
218                         CHECK(ioctl(fd, TIOCMGET, &status));
219                         if (dtr == -1) status &= ~TIOCM_DTR;
220                         if (dtr == +1) status |=  TIOCM_DTR;
221                         if (rts == -1) status &= ~TIOCM_RTS;
222                         if (rts == +1) status |=  TIOCM_RTS;
223                         CHECK(ioctl(fd, TIOCMSET, &status));
224                 }
225
226                 CHECK(tcsetattr(fd, TCSANOW, &tio));
227         } else if (speed || dtr || rts) {
228                 fprintf(stderr, "Cannot set speed, DTR or RTS on non-terminal %s\n", dev);
229                 exit(1);
230         }
231
232         struct pollfd fds[2] = {
233                 { .fd = 0,  .events = POLLIN },
234                 { .fd = fd, .events = POLLIN },
235         };
236         char buf[4096];
237
238         if (stdin_tty) {
239                 tio = stdin_tio_backup;
240                 if (raw)
241                         cfmakeraw(&tio);
242                 CHECK(tcsetattr(0, TCSANOW, &tio));
243         }
244
245         VERBOSE("Connected.\n");
246         while (1) {
247                 int r1, r2;
248                 CHECK(poll(fds, 2, -1));
249                 if (fds[0].revents & POLLIN) {
250                         r1 = CHECK(read(0, buf, sizeof(buf)));
251                         if (r1 == 0) {
252                                 VERBOSE("EOF on stdin\n");
253                                 break;
254                         }
255                         r2 = CHECK(write(fd, buf, r1));
256                         if (r1 != r2) {
257                                 fprintf(stderr, "Not all data written to %s (%d/%d)\n", dev, r1, r2);
258                                 exit(1);
259                         }
260                 }
261                 if (fds[1].revents & POLLIN) {
262                         r1 = CHECK(read(fd, buf, sizeof(buf)));
263                         if (r1 == 0) {
264                                 VERBOSE("EOF on %s\n", dev);
265                                 break;
266                         }
267                         r2 = CHECK(write(1, buf, r1));
268                         if (r1 != r2) {
269                                 fprintf(stderr, "Not all data written to stdout (%d/%d)\n", r1, r2);
270                                 exit(1);
271                         }
272                 }
273         }
274         return 0;
275 }