]> rtime.felk.cvut.cz Git - sojka/sterm.git/blob - sterm.c
Refactor pulse to a function
[sojka/sterm.git] / sterm.c
1 /*
2  * Simple serial terminal
3  *
4  * Copyright 2014, 2015 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 <sys/types.h>
35 #include <unistd.h>
36 #include <fcntl.h>
37 #include <termios.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <getopt.h>
41 #include <poll.h>
42 #include <stdbool.h>
43 #include <string.h>
44 #include <signal.h>
45 #include <lockdev.h>
46
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; })
51
52 #define VERBOSE(format, ...) do { if (verbose) fprintf(stderr, "sterm: " format, ##__VA_ARGS__); } while (0)
53
54 bool verbose = false;
55 bool exit_on_escape = true;
56
57 struct termios stdin_tio_backup;
58 char *dev = NULL;
59
60 void rm_file(int status, void *arg)
61 {
62         char *fn = arg;
63         if (fn[0])
64                 unlink(fn);
65         fn[0] = 0;
66 }
67
68 void restore_stdin_term()
69 {
70         tcsetattr(0, TCSANOW, &stdin_tio_backup);
71 }
72
73 void unlock()
74 {
75         dev_unlock(dev, getpid());
76 }
77
78 void sighandler(int arg)
79 {
80         exit(0); /* Invoke exit handlers */
81 }
82
83 int dtr_rts_arg(const char option)
84 {
85         int val = -1;
86
87         if (optarg) {
88                 char *end;
89                 val = strtol(optarg, &end, 10);
90                 if (end == optarg) {
91                         /* Not a number */
92                         switch (optarg[0]) {
93                         case '+': val = +1; break;
94                         case '-': val = -1; break;
95                         default:
96                                 fprintf(stderr, "Unknown -%c argument: %s", option, optarg);
97                                 exit(1);
98                         }
99                 }
100         }
101         return val;
102 }
103
104 void exit_on_escapeseq(const char *buf, int len)
105 {
106         static const char escseq[] = "\r~.";
107         static const char *state = escseq+1;
108         int i;
109         for (i = 0; i < len; i++) {
110                 if (buf[i] == *state) {
111                         state++;
112                         if (*state == 0)
113                                 exit(0);
114                 } else {
115                         state = escseq;
116                         if (buf[i] == *state)
117                                 state++;
118                 }
119         }
120 }
121
122 void usage(const char* argv0)
123 {
124         fprintf(stderr, "Usage: %s [options] <device>\n", argv0);
125         fprintf(stderr,
126                 "Options:\n"
127                 "  -d[PULSE] make pulse on DTR\n"
128                 "  -e        ignore '~.' escape sequence\n"
129                 "  -n        do not switch the device to raw mode\n"
130                 "  -r[PULSE] make pulse on RTS\n"
131                 "  -s <baudrate>\n"
132                 "\n"
133                 "PULSE is a number specifying the pulse. Absolute value defines the\n"
134                 "length of the pulse in milliseconds, sign determines the polarity of\n"
135                 "the pulse. Alternatively, PULSE can be either '+' or '-', which\n"
136                 "corresponds to +1 or -1.\n"
137                 );
138 }
139
140 void pulse(int fd, int dtr, int rts)
141 {
142         int status, ms = 0;
143         /* tio.c_cflag &= ~HUPCL; */ /* Don't lower DTR/RTS on close */
144
145         CHECK(ioctl(fd, TIOCMGET, &status));
146         if (dtr > 0) { status &= ~TIOCM_DTR; ms = +dtr; }
147         if (dtr < 0) { status |=  TIOCM_DTR; ms = -dtr; }
148         if (rts > 0) { status &= ~TIOCM_RTS; ms = +rts; }
149         if (rts < 0) { status |=  TIOCM_RTS; ms = -rts; }
150         CHECK(ioctl(fd, TIOCMSET, &status));
151
152         usleep(ms*1000);
153
154         if (dtr < 0) { status &= ~TIOCM_DTR; }
155         if (dtr > 0) { status |=  TIOCM_DTR; }
156         if (rts < 0) { status &= ~TIOCM_RTS; }
157         if (rts > 0) { status |=  TIOCM_RTS; }
158         CHECK(ioctl(fd, TIOCMSET, &status));
159 }
160
161 int main(int argc, char *argv[])
162 {
163         int fd;
164         int opt;
165         speed_t speed = 0;
166         int dtr = 0, rts = 0;
167         struct termios tio;
168         bool stdin_tty;
169         bool raw = true;
170
171         if ((stdin_tty = isatty(0))) {
172                 CHECK(tcgetattr(0, &stdin_tio_backup));
173                 atexit(restore_stdin_term);
174         }
175
176         while ((opt = getopt(argc, argv, "nd::er::s:v")) != -1) {
177                 switch (opt) {
178                 case 'd': dtr = dtr_rts_arg(opt); break;
179                 case 'e': exit_on_escape = false; break;
180                 case 'n': raw = false; break;
181                 case 'r': rts = dtr_rts_arg(opt); break;
182                 case 's': {
183                         int s = atoi(optarg);
184                         switch (s) {
185 #define S(s) case s: speed = B##s; break;
186                                 S(0);
187                                 S(50);
188                                 S(75);
189                                 S(110);
190                                 S(134);
191                                 S(150);
192                                 S(200);
193                                 S(300);
194                                 S(600);
195                                 S(1200);
196                                 S(1800);
197                                 S(2400);
198                                 S(4800);
199                                 S(9600);
200                                 S(19200);
201                                 S(38400);
202                                 S(57600);
203                                 S(115200);
204                                 S(230400);
205 #undef S
206                         default:
207                                 fprintf(stderr, "Unknown baud rate %d\n", s);
208                                 exit(1);
209                         }
210                         break;
211                 }
212                 case 'v':
213                         verbose = true;
214                         break;
215                 default: /* '?' */
216                         usage(argv[0]);
217                         exit(1);
218                 }
219         }
220
221         if (optind < argc)
222                 dev = argv[optind];
223
224         if (!dev) {
225                 fprintf(stderr, "No device specified\n");
226                 usage(argv[0]);
227                 exit(1);
228         }
229
230         signal(SIGINT, sighandler);
231         signal(SIGTERM, sighandler);
232         signal(SIGHUP, sighandler);
233
234         pid_t pid = dev_lock(dev);
235         if (pid > 0) {
236                 fprintf(stderr, "%s is used by PID %d\n", dev, pid);
237                 exit(1);
238         } else if (pid < 0) {
239                 perror("dev_lock()");
240                 exit(1);
241         }
242         atexit(unlock);
243
244         /* O_NONBLOCK is needed to not wait for the CDC signal. See tty_ioctl(4). */
245         if ((fd = open(dev, O_RDWR|O_NOCTTY|O_NONBLOCK)) < 0) {
246                 perror(dev);
247                 exit(1);
248         }
249         /* Cancel the efect of O_NONBLOCK flag. */
250         int n = fcntl(fd, F_GETFL, 0);
251         fcntl(fd, F_SETFL, n & ~O_NDELAY);
252
253         if (isatty(fd)) {
254                 CHECK(ioctl(fd, TIOCEXCL, NULL));
255
256                 CHECK(tcgetattr(fd, &tio));
257
258                 cfmakeraw(&tio);
259
260                 if (speed) {
261                         CHECK(cfsetospeed(&tio, speed));
262                         CHECK(cfsetispeed(&tio, speed));
263                 }
264
265                 if (dtr || rts)
266                         pulse(fd, dtr, rts);
267
268                  /* Disable flow control */
269                 tio.c_cflag &= ~(CRTSCTS);
270                 tio.c_iflag &= ~(IXON|IXOFF);
271
272                 CHECK(tcsetattr(fd, TCSANOW, &tio));
273         } else if (speed || dtr || rts) {
274                 fprintf(stderr, "Cannot set speed, DTR or RTS on non-terminal %s\n", dev);
275                 exit(1);
276         }
277
278         struct pollfd fds[2] = {
279                 { .fd = 0,  .events = POLLIN },
280                 { .fd = fd, .events = POLLIN },
281         };
282         char buf[4096];
283
284         if (stdin_tty) {
285                 tio = stdin_tio_backup;
286                 if (raw)
287                         cfmakeraw(&tio);
288                 CHECK(tcsetattr(0, TCSANOW, &tio));
289         }
290
291         VERBOSE("Connected.\r\n");
292         if (exit_on_escape)
293                 VERBOSE("Use '<Enter>~.' sequence to exit.\r\n");
294
295         while (1) {
296                 int r1, r2;
297                 CHECK(poll(fds, 2, -1));
298                 if (fds[0].revents & POLLIN) {
299                         r1 = CHECK(read(0, buf, sizeof(buf)));
300                         if (r1 == 0) {
301                                 VERBOSE("EOF on stdin\r\n");
302                                 break;
303                         }
304                         if (exit_on_escape)
305                                 exit_on_escapeseq(buf, r1);
306                         r2 = CHECK(write(fd, buf, r1));
307                         if (r1 != r2) {
308                                 fprintf(stderr, "Not all data written to %s (%d/%d)\n", dev, r1, r2);
309                                 exit(1);
310                         }
311                 }
312                 if (fds[1].revents & POLLIN) {
313                         r1 = CHECK(read(fd, buf, sizeof(buf)));
314                         if (r1 == 0) {
315                                 VERBOSE("EOF on %s\r\n", dev);
316                                 break;
317                         }
318                         r2 = CHECK(write(1, buf, r1));
319                         if (r1 != r2) {
320                                 fprintf(stderr, "Not all data written to stdout (%d/%d)\n", r1, r2);
321                                 exit(1);
322                         }
323                 }
324         }
325         return 0;
326 }