-
Notifications
You must be signed in to change notification settings - Fork 0
/
scat.c
586 lines (510 loc) · 11.7 KB
/
scat.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
#define _DEFAULT_SOURCE
#define _BSD_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <termio.h>
#include <string.h>
#include <errno.h>
#define PROGNAME "scat"
#define PARITY_NONE 0
#define PARITY_ODD 1
#define PARITY_EVEN 2
#define FLOW_NONE 0
#define FLOW_HARD 1
#define FLOW_SOFT 2
int raw = 0;
void usage(FILE *fp)
{
fprintf(fp, "Usage: %s [options] [device]\n", PROGNAME);
fprintf(fp, " -l <device>\n");
fprintf(fp, " -s <speed> (default: 115200)\n");
fprintf(fp, " -[45678]: datasize (default: 8)\n");
fprintf(fp, " -[NEO]: parity (default: N)\n");
fprintf(fp, " -[12]: stopbit (default: 1)\n");
fprintf(fp, " -[HX]: flow contro (default: not specified)\n");
fprintf(fp, " -x: hex dump mode\n");
fprintf(fp, " -r: raw output mode (display characters AS IS)\n");
fprintf(fp, " -t: Make TTY raw mode\n");
fprintf(fp, " -T <N>: timeout after EOF in sec (default: 0.5)\n");
exit(0);
}
int speed_t_to_baud(speed_t speed)
{
switch (speed) {
case B50: return 50;
case B75: return 75;
case B110: return 110;
case B134: return 134;
case B150: return 150;
case B200: return 200;
case B300: return 300;
case B600: return 600;
case B1200: return 1200;
case B1800: return 1800;
case B2400: return 2400;
case B4800: return 4800;
case B9600: return 9600;
case B19200: return 19200;
case B38400: return 38400;
#ifdef B57600
case B57600: return 57600;
#endif
#ifdef B115200
case B115200: return 115200;
#endif
#ifdef B230400
case B230400: return 230400;
#endif
#ifdef B460800
case B460800: return 460800;
#endif
#ifdef B500000
case B500000: return 500000;
#endif
#ifdef B576000
case B576000: return 576000;
#endif
#ifdef B921600
case B921600: return 921600;
#endif
#ifdef B1000000
case B1000000: return 1000000;
#endif
#ifdef B1152000
case B1152000: return 1152000;
#endif
#ifdef B1500000
case B1500000: return 1500000;
#endif
#ifdef B2000000
case B2000000: return 2000000;
#endif
#ifdef B2500000
case B2500000: return 2500000;
#endif
#ifdef B3000000
case B3000000: return 3000000;
#endif
#ifdef B3500000
case B3500000: return 3500000;
#endif
#ifdef B4000000
case B4000000: return 4000000;
#endif
default:
return speed;
}
}
speed_t baud_to_speed_t(int baud)
{
switch (baud) {
case 50: return B50;
case 75: return B75;
case 110: return B110;
case 134: return B134;
case 150: return B150;
case 200: return B200;
case 300: return B300;
case 600: return B600;
case 1200: return B1200;
case 1800: return B1800;
case 2400: return B2400;
case 4800: return B4800;
case 9600: return B9600;
case 19200: return B19200;
case 38400: return B38400;
#ifdef B57600
case 57600: return B57600;
#endif
#ifdef B115200
case 115200: return B115200;
#endif
#ifdef B230400
case 230400: return B230400;
#endif
#ifdef B460800
case 460800: return B460800;
#endif
#ifdef B500000
case 500000: return B500000;
#endif
#ifdef B576000
case 576000: return B576000;
#endif
#ifdef B921600
case 921600: return B921600;
#endif
#ifdef B1000000
case 1000000: return B1000000;
#endif
#ifdef B1152000
case 1152000: return B1152000;
#endif
#ifdef B1500000
case 1500000: return B1500000;
#endif
#ifdef B2000000
case 2000000: return B2000000;
#endif
#ifdef B2500000
case 2500000: return B2500000;
#endif
#ifdef B3000000
case 3000000: return B3000000;
#endif
#ifdef B3500000
case 3500000: return B3500000;
#endif
#ifdef B4000000
case 4000000: return B4000000;
#endif
default:
fprintf(stderr, "Unsupported speed %d.\n", baud);
return 0;
}
}
int open_serial(const char *fname, mode_t mode, int com_speed, int com_datasize, int com_parity, int com_stopbit, int com_flow)
{
struct termios newtio;
int fd;
fd = open(fname, mode);
if (fd < 0) {
char fname_dev[1024];
snprintf(fname_dev, sizeof(fname_dev), "/dev/%s", fname);
fd = open(fname_dev, mode);
}
if (fd < 0) {
fprintf(stderr, "open: %s\n", strerror(errno));
return -1;
}
if (!isatty(fd)) {
fprintf(stderr, "%s: is not a tty\n", fname);
return -1;
}
memset(&newtio, 0, sizeof(newtio)); // clear struct for new port settings
newtio.c_cflag = CREAD;
newtio.c_iflag = 0;
newtio.c_oflag = 0;
newtio.c_lflag = 0;
newtio.c_cc[VTIME] = 0; // inter-character timer unused
newtio.c_cc[VMIN] = 1; // blocking read until 1 character arrives
speed_t speed = baud_to_speed_t(com_speed);
cfsetospeed(&newtio, speed);
cfsetispeed(&newtio, speed);
switch (com_datasize) {
case 5: newtio.c_cflag |= CS5; break;
case 6: newtio.c_cflag |= CS6; break;
case 7: newtio.c_cflag |= CS7; break;
case 8: newtio.c_cflag |= CS8; break;
default:
fprintf(stderr, "Unsupported character size.\n");
goto err;
}
switch (com_parity) {
case PARITY_NONE: break;
case PARITY_ODD : newtio.c_cflag |= PARENB | PARODD; break;
case PARITY_EVEN: newtio.c_cflag |= PARENB; break;
default:
fprintf(stderr, "Unsupported parity.\n");
goto err;
}
switch (com_stopbit) {
default:
fprintf(stderr, "Unsupported stopbits.\n");
goto err;
case 1:
break;
case 2:
newtio.c_cflag |= CSTOPB;
break;
}
switch (com_flow) {
case FLOW_HARD: // hard
newtio.c_cflag |= CRTSCTS;
break;
case FLOW_SOFT: // soft
newtio.c_iflag |= IXON | IXOFF;
break;
case FLOW_NONE:
default:
break;
}
tcflush(fd, TCIFLUSH);
if (tcsetattr(fd, TCSANOW, &newtio) < 0) {
perror("tcsetattr");
}
return fd;
err:
return -1;
}
void fprint_com_status(FILE *fp, int fd)
{
struct termios tio;
if (tcgetattr(fd, &tio) == 0) {
fprintf(fp, "%u, ", speed_t_to_baud(cfgetispeed(&tio)));
// datasize
switch (tio.c_cflag & CSIZE) {
case CS5: fputc('5', fp); break;
case CS6: fputc('6', fp); break;
case CS7: fputc('7', fp); break;
case CS8: fputc('8', fp); break;
default: break;
}
if (tio.c_cflag & PARENB) {
fputc(tio.c_cflag & PARODD ? 'O' : 'E', fp);
} else {
fputc('N', fp);
}
fputc(tio.c_cflag & CSTOPB ? '2' : '1', fp);
if (tio.c_cflag & CRTSCTS) {
fputc('H', fp);
}
if (tio.c_iflag & (IXON | IXOFF)) {
fputc('X', fp);
}
fprintf(fp, "\n");
}
}
void hexdump(void *buf_, size_t len, unsigned long start_addr)
{
const int fold_size = 16;
const int flag_print_chars = 1;
const unsigned char *buf = (const unsigned char *)buf_;
unsigned long i, j;
unsigned long offset;
unsigned long count;
offset = start_addr % fold_size;
count = (offset + len + fold_size - 1) / fold_size;
for (i = 0; i < count; i++) {
printf("%08lx: ", start_addr);
start_addr = (start_addr / fold_size) * fold_size;
for (j = 0; j < fold_size; j++) {
unsigned long idx = i * fold_size + j - offset;
if (j % 8 == 0) printf(" ");
if (idx < len) {
printf("%02x ", buf[idx]);
} else {
printf(" ");
}
}
if (flag_print_chars) {
printf(" |");
for (j = 0; j < fold_size; j++) {
unsigned long idx = i * fold_size + j - offset;
//if (j % 8 == 0) printf(" ");
if (idx < len) {
printf("%c", buf[idx] >= 0x20 && buf[idx] < 0x7fUL ? buf[idx] : '.');
} else {
printf(" ");
}
}
printf("|");
}
printf("\n");
start_addr += fold_size;
}
}
void escape_unprintable(const char *buf, size_t len)
{
while (len-- > 0) {
int c = *(unsigned char *)buf++;
if (raw || c == '\r' || c == '\n' || c == '\t' || (c >= 0x20 && c < 0x7f)) {
putchar(c);
} else {
printf("<%02x>", c);
}
}
fflush(stdout);
}
ssize_t write_to_device(int fd, const char *src, size_t len, int non_canon)
{
const char *last_p, *p;
int wrote = 0;
int r;
FILE *fp = fdopen(fd, "a");
if (fp == NULL) return -1;
for (p = src; p < src + len; p++) {
int c = *(unsigned char *)p;
{
static int state = 1;
switch (state) {
case 0:
break;
case 1:
if (c == '~') {
state = 2;
continue;
}
break;
case 2:
switch (c) {
case '.':
fprintf(stderr, "Quit\r\n");
return -1;
case '?':
fprintf(stderr, "Escape commands:\n");
fprintf(stderr, " ~. - Terminate the session\n");
fprintf(stderr, " ~r - Toggle raw mode\n");
fprintf(stderr, " ~? - This help\n");
fprintf(stderr, " ~~ - Send the escape character\n");
state = 1;
continue;
case 'r':
raw = 1 - raw;
fprintf(stderr, "Toggle raw mode (now: %s)\r\n", raw ? "true" : "false");
state = 1;
continue;
default:
fputc('~', fp);
break;
}
}
state = 0;
if (c == '\r' || c == '\n') state = 1;
}
if (c == '\n') fputc('\r', fp);
fputc(c, fp);
fflush(fp);
}
return len;
}
int main(int argc, char *argv[])
{
int ch;
const char *fname = NULL;
int speed = 115200, cs = 8, parity = 0, stop = 1, flow = 0;
int dump = 0;
int non_canon = 0;;
double timeout = 0.5;
int verbose = 0;
while ((ch = getopt(argc, argv, "l:s:rT:NEOneoHXS56789xthv")) != EOF) {
switch (ch) {
case 'l':
fname = optarg;
break;
case 's':
speed = strtoul(optarg, NULL, 0);
break;
case 'r':
raw = 1;
break;
case 'T':
timeout = strtod(optarg, NULL);
break;
case 'N': parity = PARITY_NONE; break;
case 'E': parity = PARITY_EVEN; break;
case 'O': parity = PARITY_ODD; break;
case 'H': flow = FLOW_HARD; break;
case 'X': case 'S': flow = FLOW_SOFT; break;
case '1': stop = 1; break;
case '2': stop = 2; break;
case '5': cs = 5; break;
case '6': cs = 6; break;
case '7': cs = 7; break;
case '8': cs = 8; break;
case '9': cs = 9; break;
case 'x': dump++; break;
case 't': non_canon = 1; break;
case 'h': usage(stdout); break;
case 'v': verbose = 1; break;
}
}
argc -= optind;
argv += optind;
if (fname == NULL) fname = argv[0];
if (fname == NULL) {
fprintf(stderr, PROGNAME ": No device specified\n");
return -1;
}
int fd = open_serial(fname, O_RDWR, speed, cs, parity, stop, flow);
if (fd < 0) return -1;
if (verbose) {
fprintf(stderr, "* COM setting ");
fprint_com_status(stderr, fd);
}
int fdin = STDIN_FILENO;
// Configure STDIN tty
struct termios *backup = NULL;
if (non_canon && isatty(fdin)) {
static struct termios old, p;
tcgetattr(fdin, &old);
backup = &old;
tcgetattr(fdin, &p);
cfmakeraw(&p);
/*
p.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
p.c_oflag &= ~OPOST;
p.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
p.c_cflag &= ~(CSIZE | PARENB);
p.c_cflag |= CS8;
*/
tcsetattr(fdin, TCSAFLUSH, &p);
}
while (1) {
char buf[4096];
struct timeval tv;
fd_set readfds;
tv.tv_sec = timeout;
tv.tv_usec = (timeout - tv.tv_sec) * 1000 * 1000;
FD_ZERO(&readfds);
if (fdin >= 0) FD_SET(fdin, &readfds);
if (fd >= 0) FD_SET(fd, &readfds);
int ret = select(FD_SETSIZE, &readfds, NULL, NULL, &tv);
if (ret == -1) {
if (errno == EAGAIN) continue;
int err = errno;
if (backup) tcsetattr(fdin, TCSAFLUSH, backup);
fprintf(stderr, "select: %s\n", strerror(err));
return -1;
}
if (ret == 0) {
// timed out
if (fdin < 0 || fd < 0) {
if (backup) tcsetattr(fdin, TCSAFLUSH, backup);
return 0;
}
continue;
}
if (FD_ISSET(fd, &readfds)) {
ssize_t len = read(fd, buf, sizeof(buf));
if (len < 0) {
int err = errno;
if (backup) tcsetattr(fdin, TCSAFLUSH, backup);
fprintf(stderr, "read: %s\n", strerror(err));
return -1;
}
if (len == 0) {
if (backup) tcsetattr(fdin, TCSAFLUSH, backup);
return 0;
}
if (dump) {
static unsigned long pos = 0;
hexdump(buf, len, dump == 1 ? 0 : pos);
pos += len;
} else {
escape_unprintable(buf, len);
}
}
if (FD_ISSET(fdin, &readfds)) {
ssize_t len = read(fdin, buf, sizeof(buf));
if (len < 0) {
int err = errno;
if (backup) tcsetattr(fdin, TCSAFLUSH, backup);
fprintf(stderr, "read: %s\n", strerror(err));
return -1;
}
if (len == 0) {
close(fdin);
fdin = -1;
continue;
}
ssize_t ret = write_to_device(fd, buf, len, non_canon);
if (ret < 0) {
if (backup) tcsetattr(fdin, TCSAFLUSH, backup);
return -1;
}
}
}
}