]> rtime.felk.cvut.cz Git - can-benchmark.git/blob - utils/sterm.c
Add simple serial line terminal
[can-benchmark.git] / utils / sterm.c
1 /*
2  * Simple terminal
3  *
4  * This is a minimalist terminal program like minicom or cu. The only
5  * thing it does is creating a bidirectional connection between
6  * stdin/stdout and a device (e.g. serial terminal). It can also set
7  * serial line baudrate and manipulate DTR/RTS modem lines.
8  *
9  * Copyright 2014 Michal Sojka <sojkam1@fel.cvut.cz>
10  *
11  * This program is free software: you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License as
13  * published by the Free Software Foundation, either version 3 of the
14  * License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful, but
17  * WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program. If not, see
23  * <http://www.gnu.org/licenses/>.
24  */
25
26 #define _BSD_SOURCE
27 #include <sys/ioctl.h>
28 #include <unistd.h>
29 #include <fcntl.h>
30 #include <termios.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <getopt.h>
34 #include <poll.h>
35 #include <stdbool.h>
36 #include <string.h>
37 #include <signal.h>
38
39 #define STRINGIFY(val) #val
40 #define TOSTRING(val) STRINGIFY(val)
41 #define CHECK(cmd) ({ int ret = (cmd); if (ret == -1) { perror(#cmd " line " TOSTRING(__LINE__)); exit(1); }; ret; })
42 #define CHECKPTR(cmd) ({ void *ptr = (cmd); if (ptr == (void*)-1) { perror(#cmd " line " TOSTRING(__LINE__)); exit(1); }; ptr; })
43
44 #define VERBOSE(format, ...) do { if (verbose) fprintf(stderr, format, ##__VA_ARGS__); } while (0)
45
46 bool verbose = false;
47
48 char template[] = "/var/lock/TMPXXXXXX";
49 char lockfile[100];
50 struct termios stdin_tio_backup;
51
52 void rm_file(int status, void *arg)
53 {
54         char *fn = arg;
55         if (fn[0])
56                 unlink(fn);
57         fn[0] = 0;
58 }
59
60 void restore_stdin_term()
61 {
62         tcsetattr(0, TCSANOW, &stdin_tio_backup);
63 }
64
65 void sighandler(int arg)
66 {
67         exit(0);
68 }
69
70
71 int main(int argc, char *argv[])
72 {
73         int fd;
74         char *dev = NULL;
75         int opt;
76         speed_t speed = 0;
77         int ret;
78         int dtr = 0, rts = 0;
79         struct termios tio;
80         bool stdin_tty;
81         bool raw = true;
82
83         if ((stdin_tty = isatty(0))) {
84                 CHECK(tcgetattr(0, &stdin_tio_backup));
85                 atexit(restore_stdin_term);
86         }
87
88         while ((opt = getopt(argc, argv, "ndrs:v")) != -1) {
89                 switch (opt) {
90                 case 'd':
91                         dtr = 1;
92                         break;
93                 case 'n':
94                         raw = false;
95                 case 'r':
96                         rts = 1;
97                         break;
98                 case 's': {
99                         int s = atoi(optarg);
100                         switch (s) {
101 #define S(s) case s: speed = B##s; break;
102                                 S(0);
103                                 S(50);
104                                 S(75);
105                                 S(110);
106                                 S(134);
107                                 S(150);
108                                 S(200);
109                                 S(300);
110                                 S(600);
111                                 S(1200);
112                                 S(1800);
113                                 S(2400);
114                                 S(4800);
115                                 S(9600);
116                                 S(19200);
117                                 S(38400);
118                                 S(57600);
119                                 S(115200);
120                                 S(230400);
121 #undef S
122                         default:
123                                 fprintf(stderr, "Unknown baud rate %d\n", s);
124                                 exit(1);
125                         }
126                         break;
127                 }
128                 case 'v':
129                         verbose = true;
130                         break;
131                 default: /* '?' */
132                         fprintf(stderr, "Usage: %s [-s baudrate] [-v] <device>\n", argv[0]);
133                         exit(1);
134                 }
135         }
136
137         if (optind < argc)
138                 dev = argv[optind];
139
140         if (!dev) {
141                 fprintf(stderr, "No device specified\n");
142                 exit(1);
143         }
144
145         signal(SIGINT, sighandler);
146         signal(SIGTERM, sighandler);
147         signal(SIGHUP, sighandler);
148
149         if (strncmp(dev, "/dev/", 5) == 0 &&
150             strrchr(dev, '/') == dev + 4 &&
151             dev[5] != 0)
152         { /* Create lock file (to be inter-operable with other programs) */
153                 /* This is racy, but what we can do - see also comments in uucp / cu */
154                 int tmp = CHECK(mkstemp(template));
155                 on_exit(rm_file, template);
156                 char pid[20];
157                 snprintf(pid, sizeof(pid), "%u", getpid());
158                 CHECK(write(tmp, pid, strlen(pid)));
159                 close(tmp);
160                 snprintf(lockfile, sizeof(lockfile), "/var/lock/LCK..%s", dev + 5);
161                 if (link(template, lockfile) == -1) {
162                         perror(lockfile);
163                         exit(1);
164                 }
165                 rm_file(0, template);
166                 on_exit(rm_file, lockfile);
167         }
168
169         if ((fd = open(dev, O_RDWR)) < 0) {
170                 perror(dev);
171                 exit(1);
172         }
173
174         if (isatty(fd)) {
175                 CHECK(ioctl(fd, TIOCEXCL, NULL));
176
177                 CHECK(tcgetattr(fd, &tio));
178
179                 cfmakeraw(&tio);
180
181                 if (speed) {
182                         CHECK(cfsetospeed(&tio, speed));
183                         CHECK(cfsetispeed(&tio, speed));
184                 }
185
186                 if (dtr || rts) {
187                         int status;
188                         tio.c_cflag &= ~HUPCL;
189
190                         CHECK(ioctl(fd, TIOCMGET, &status));
191                         if (dtr == +1) status &= ~TIOCM_DTR;
192                         if (dtr == -1) status |=  TIOCM_DTR;
193                         if (rts == +1) status &= ~TIOCM_RTS;
194                         if (rts == -1) status |=  TIOCM_RTS;
195                         CHECK(ioctl(fd, TIOCMSET, &status));
196                 }
197
198                 CHECK(tcsetattr(fd, TCSANOW, &tio));
199         } else if (speed || dtr || rts) {
200                 fprintf(stderr, "Cannot set speed, DTR or RTS on non-terminal %s\n", dev);
201                 exit(1);
202         }
203
204         struct pollfd fds[2] = {
205                 { .fd = 0,  .events = POLLIN },
206                 { .fd = fd, .events = POLLIN },
207         };
208         char buf[4096];
209
210         if (stdin_tty) {
211                 tio = stdin_tio_backup;
212                 if (raw)
213                         cfmakeraw(&tio);
214                 CHECK(tcsetattr(0, TCSANOW, &tio));
215         }
216
217         VERBOSE("Connected.\n");
218         while (1) {
219                 int r1, r2;
220                 ret = CHECK(poll(fds, 2, -1));
221                 if (fds[0].revents & POLLIN) {
222                         r1 = CHECK(read(0, buf, sizeof(buf)));
223                         if (r1 == 0) {
224                                 VERBOSE("EOF on stdin\n");
225                                 break;
226                         }
227                         r2 = CHECK(write(fd, buf, r1));
228                         if (r1 != r2) {
229                                 fprintf(stderr, "Not all data written to %s (%d/%d)\n", dev, r1, r2);
230                                 exit(1);
231                         }
232                 }
233                 if (fds[1].revents & POLLIN) {
234                         r1 = CHECK(read(fd, buf, sizeof(buf)));
235                         if (r1 == 0) {
236                                 VERBOSE("EOF on %s\n", dev);
237                                 break;
238                         }
239                         r2 = CHECK(write(1, buf, r1));
240                         if (r1 != r2) {
241                                 fprintf(stderr, "Not all data written to stdout (%d/%d)\n", r1, r2);
242                                 exit(1);
243                         }
244                 }
245         }
246         return 0;
247 }