]> rtime.felk.cvut.cz Git - can-benchmark.git/blob - utils/sterm.c
a3e9786ab82fd0e3e273b0e79f81f31e7183e7f7
[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                 if (link(template, lockfile) == -1) {
178                         perror(lockfile);
179                         exit(1);
180                 }
181                 rm_file(0, template);
182                 on_exit(rm_file, lockfile);
183         }
184
185         if ((fd = open(dev, O_RDWR)) < 0) {
186                 perror(dev);
187                 exit(1);
188         }
189
190         if (isatty(fd)) {
191                 CHECK(ioctl(fd, TIOCEXCL, NULL));
192
193                 CHECK(tcgetattr(fd, &tio));
194
195                 cfmakeraw(&tio);
196
197                 if (speed) {
198                         CHECK(cfsetospeed(&tio, speed));
199                         CHECK(cfsetispeed(&tio, speed));
200                 }
201
202                 if (dtr || rts) {
203                         int status;
204                         /* tio.c_cflag &= ~HUPCL; */ /* Don't lower DTR/RTS on close */
205
206                         CHECK(ioctl(fd, TIOCMGET, &status));
207                         if (dtr == -1) status &= ~TIOCM_DTR;
208                         if (dtr == +1) status |=  TIOCM_DTR;
209                         if (rts == -1) status &= ~TIOCM_RTS;
210                         if (rts == +1) status |=  TIOCM_RTS;
211                         CHECK(ioctl(fd, TIOCMSET, &status));
212                 }
213
214                 CHECK(tcsetattr(fd, TCSANOW, &tio));
215         } else if (speed || dtr || rts) {
216                 fprintf(stderr, "Cannot set speed, DTR or RTS on non-terminal %s\n", dev);
217                 exit(1);
218         }
219
220         struct pollfd fds[2] = {
221                 { .fd = 0,  .events = POLLIN },
222                 { .fd = fd, .events = POLLIN },
223         };
224         char buf[4096];
225
226         if (stdin_tty) {
227                 tio = stdin_tio_backup;
228                 if (raw)
229                         cfmakeraw(&tio);
230                 CHECK(tcsetattr(0, TCSANOW, &tio));
231         }
232
233         VERBOSE("Connected.\n");
234         while (1) {
235                 int r1, r2;
236                 CHECK(poll(fds, 2, -1));
237                 if (fds[0].revents & POLLIN) {
238                         r1 = CHECK(read(0, buf, sizeof(buf)));
239                         if (r1 == 0) {
240                                 VERBOSE("EOF on stdin\n");
241                                 break;
242                         }
243                         r2 = CHECK(write(fd, buf, r1));
244                         if (r1 != r2) {
245                                 fprintf(stderr, "Not all data written to %s (%d/%d)\n", dev, r1, r2);
246                                 exit(1);
247                         }
248                 }
249                 if (fds[1].revents & POLLIN) {
250                         r1 = CHECK(read(fd, buf, sizeof(buf)));
251                         if (r1 == 0) {
252                                 VERBOSE("EOF on %s\n", dev);
253                                 break;
254                         }
255                         r2 = CHECK(write(1, buf, r1));
256                         if (r1 != r2) {
257                                 fprintf(stderr, "Not all data written to stdout (%d/%d)\n", r1, r2);
258                                 exit(1);
259                         }
260                 }
261         }
262         return 0;
263 }