-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhack-pi.c
executable file
·630 lines (565 loc) · 14 KB
/
hack-pi.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
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
/*
* RTTY signal generator using GPIO interface of Raspberry Pi.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <ctype.h>
#include <fcntl.h>
#include <termios.h>
#include <sys/time.h>
#include <unistd.h>
#include <wiringPi.h>
int g_stop;
#define BAUD_DELAY_45 22000 /* 22ms = 45 baud, 60WPM */
#define BAUD_DELAY_50 20000 /* 20ms = 50 baud, 66WPM */
#define BAUD_DELAY_74 13470 /* 13ms = 74 baud, 100WPM */
#define COLUMN_MAX 76
#define CHAR_A 0
#define CHAR_Z 25
#define CHAR_NULL 26
#define CHAR_LF 27
#define CHAR_SPACE 28
#define CHAR_CR 29
#define CHAR_SHIFT_UP 30
#define CHAR_SHIFT_DOWN 31
#define CHAR_OPEN 32
#define CHAR_CLOSED 33
#define CHAR_0 15
#define CHAR_1 16
#define CHAR_2 22
#define CHAR_3 4
#define CHAR_4 17
#define CHAR_5 19
#define CHAR_6 24
#define CHAR_7 20
#define CHAR_8 8
#define CHAR_9 14
#define CHAR_DASH 0
#define CHAR_QUESTION 1
#define CHAR_COLON 2
#define CHAR_DOLLAR 3
#define CHAR_BELL 6
#define CHAR_APOSTROPHE 9
#define CHAR_LPHAREN 10
#define CHAR_RPHAREN 11
#define CHAR_PERIOD 12
#define CHAR_COMMA 13
#define CHAR_SEMICOLON 21
#define CHAR_SOLIDUS 23
#define CHAR_QUOTE 25
typedef struct rtty_conf
{
char *filename;
int bits;
int bit_delay;
int wpm;
int format;
int shift;
int column;
int test_count;
} rtty_conf;
void encode_bit(rtty_conf *ctx, int c);
void encode_to_baudot(rtty_conf *ctx, char c);
void encode_bits(rtty_conf *ctx, char *bits, int len);
int ascii_2_baudot(char c, char *baudot, int *shift);
void print_char(rtty_conf *ctx, char c);
void initialize_tty(rtty_conf *ctx);
void pause_print(rtty_conf *ctx, int count);
void print_line(rtty_conf *ctx, char *line);
void print_file(rtty_conf *ctx, char *name);
int set_raw(int fd, struct termios *old_mode);
void keyboard_io(rtty_conf *ctx);
void interrupt_function(int sig)
{
if (sig == SIGINT)
{
g_stop = 1;
}
}
void rtty_conf_init(rtty_conf *ctx)
{
ctx->bits = 8;
switch (ctx->wpm)
{
case 60:
ctx->bit_delay = BAUD_DELAY_45;
break;
case 66:
ctx->bit_delay = BAUD_DELAY_50;
break;
case 100:
ctx->bit_delay = BAUD_DELAY_74;
break;
default:
ctx->bit_delay = BAUD_DELAY_45;
break;
}
wiringPiSetup ();
pinMode (0, OUTPUT);
}
void
Usage(void) {
fprintf(stderr, "usage: rtty-alsa [options] number ...\n"
" Valid options with their default values are:\n"
" TTY options:\n"
" --input-file IN_FILE\n"
" --test-data\n"
" --keyboard\n"
" --wpm 60 | 66 | 100\n"
);
exit(1);
}
void
getvalue(int *arg, int *index, int argc,
char **argv, int min, int max)
{
if (*index >= argc-1)
Usage();
*arg = atoi(argv[1+*index]);
if (*arg < min || *arg > max) {
fprintf(stderr, "Value for %s should be in the range %d..%d\n",
argv[*index]+2, min, max);
exit(1);
}
++*index;
}
void test_generator(rtty_conf *ctx)
{
int i = 0;
char line[] =
"the quick brown fox jumped over the lazy dog's back 1234567890\n"
"ryryryryryryryryryryryryryryryryryryryryryryryryryryryryryryry";
initialize_tty(ctx);
while (ctx->test_count-- > 0) {
for (i=0; line[i]; i++) {
print_char(ctx, line[i]);
}
print_char(ctx, '\n');
}
initialize_tty(ctx);
}
int main(int argc, char **argv)
{
int i;
int test_data = 0;
int keyboard = 0;
rtty_conf ctx = {0};
for(i = 1; i < argc; i++)
{
if (argv[i][0] != '-' || argv[i][1] != '-')
break;
if (!strcmp(argv[i], "--keyboard")) {
keyboard = 1;
}
else if (!strcmp(argv[i], "--test-data")) {
test_data = 1;
if ((i+1) < argc && atoi(argv[i+1]) > 1) {
ctx.test_count = atoi(argv[i+1]);
}
}
else if (!strcmp(argv[i], "--wpm")) {
getvalue(&ctx.wpm, &i, argc, argv,
10, 10000);
}
else if (!strcmp(argv[i], "--input-file")) {
i++;
if (i >= argc)
Usage();
ctx.filename = argv[i];
}
else {
Usage();
}
}
rtty_conf_init(&ctx);
signal(SIGINT, interrupt_function);
if (test_data) {
test_generator(&ctx);
}
else {
pause_print(&ctx, 10);
initialize_tty(&ctx);
if (keyboard)
{
keyboard_io(&ctx);
}
else if (ctx.filename) {
print_file(&ctx, ctx.filename);
}
else {
while (i < argc) {
print_line(&ctx, argv[i]);
}
}
initialize_tty(&ctx);
pause_print(&ctx, 10);
}
return 0;
}
void delay_usec(int usec)
{
struct timeval to = {0};
to.tv_usec = usec;
select(0, 0, 0, 0, &to);
}
void keyboard_io(rtty_conf *ctx)
{
fd_set rmask;
char c;
int n;
struct termios old;
static int N_count = 0;
static char prev_char = 0;
set_raw(0, &old);
do
{
FD_ZERO(&rmask);
FD_SET(0, &rmask);
n = select(1, &rmask, NULL, NULL, NULL);
if (n > 0) {
read(0, &c, 1);
if (c == '\r' || c == '\n' ||
(ctx->column && ((ctx->column % COLUMN_MAX) == 0)))
{
encode_to_baudot(ctx, CHAR_CR);
encode_to_baudot(ctx, CHAR_LF);
printf("\r\n");
ctx->column = 0;
N_count = 0;
}
else
{
print_char(ctx, c);
}
fflush(stdout);
if (c == 'N' && c == prev_char)
{
if (ctx->column <= 4)
{
N_count++;
}
else
{
N_count = 0;
}
}
prev_char = c;
}
} while (N_count < 3);
tcsetattr(0, TCSANOW, &old);
}
void print_file(rtty_conf *ctx, char *name)
{
FILE *fp;
char *line;
fp = fopen(name, "r");
if (!fp) {
perror("fopen");
return;
}
line = (char *) malloc(1024);
if (!line) {
perror("malloc");
fclose(fp);
return;
}
fgets(line, 1022, fp);
while (!feof(fp)) {
print_line(ctx, line);
fgets(line, 1022, fp);
}
fclose(fp);
free(line);
}
void print_line(rtty_conf *ctx, char *line)
{
char *cp;
if (!line) return;
for (cp=line; *cp; cp++) {
if (isspace(*cp) || isalnum(*cp) || ispunct(*cp)) {
print_char(ctx, *cp);
}
}
}
void initialize_tty(rtty_conf *ctx)
{
encode_to_baudot(ctx, CHAR_NULL);
encode_to_baudot(ctx, CHAR_NULL);
encode_to_baudot(ctx, CHAR_SHIFT_DOWN);
encode_to_baudot(ctx, CHAR_CR);
encode_to_baudot(ctx, CHAR_LF);
}
void pause_print(rtty_conf *ctx, int count)
{
int i;
for (i=0; i<count; i++) {
encode_to_baudot(ctx, CHAR_CLOSED);
}
}
void print_char(rtty_conf *ctx, char c)
{
char baudot[16];
char *bp;
int cnt;
if (g_stop)
{
initialize_tty(ctx);
encode_to_baudot(ctx, CHAR_CLOSED);
exit(0);
}
cnt = ascii_2_baudot(c, baudot, &ctx->shift);
bp = baudot;
while (cnt > 0) {
encode_to_baudot(ctx, *bp);
bp++;
cnt--;
}
if (isspace(c) || isalnum(c) || ispunct(c))
{
ctx->column++;
if (c == '\n' || c == '\r')
{
ctx->column = 0;
printf("\r\n");
}
else
{
putchar((char) toupper((int) c));
}
fflush(stdout);
}
if (ctx->column >= COLUMN_MAX) {
encode_to_baudot(ctx, CHAR_CR);
encode_to_baudot(ctx, CHAR_LF);
encode_to_baudot(ctx, CHAR_CR);
printf("\r\n");
ctx->column = 0;
}
}
int
ascii_2_baudot(char c, char *baudot, int *shift)
{
int i;
char *p = baudot;
/* Convert punction characters first */
switch (c) {
case '-':
*p++ = CHAR_DASH;
break;
case '?':
*p++ = CHAR_QUESTION;
break;
case ':':
*p++ = CHAR_COLON;
break;
case '$':
*p++ = CHAR_DOLLAR;
break;
case 7:
*p++ = CHAR_BELL;
break;
case '\'':
case '`':
*p++ = CHAR_APOSTROPHE;
break;
case '(':
*p++ = CHAR_LPHAREN;
break;
case ')':
*p++ = CHAR_RPHAREN;
break;
case '.':
*p++ = CHAR_PERIOD;
break;
case ',':
*p++ = CHAR_COMMA;
break;
case ';':
*p++ = CHAR_SEMICOLON;
break;
case '/':
*p++ = CHAR_SOLIDUS;
break;
case '"':
*p++ = CHAR_QUOTE;
break;
}
/* p has advanced one byte if we have found a character to convert */
if (p != baudot) {
/*
* Prefix the converted character with shift up if not
* already shifted
*/
if (!*shift) {
p[0] = p[-1];
p[-1] = CHAR_SHIFT_UP;
p++;
*shift = 1;
}
}
else {
/* The current character has not been mapped */
i = (int) c;
if (c == ' ') {
*p++ = CHAR_SPACE;
}
else if (c == '\n') {
*p++ = CHAR_CR;
*p++ = CHAR_LF;
}
else if (isdigit(c)) {
if (!*shift) {
*shift = 1;
*p++ = CHAR_SHIFT_UP;
}
i = (int) (c - '0');
switch(i) {
case 0:
*p++ = CHAR_0;
break;
case 1:
*p++ = CHAR_1;
break;
case 2:
*p++ = CHAR_2;
break;
case 3:
*p++ = CHAR_3;
break;
case 4:
*p++ = CHAR_4;
break;
case 5:
*p++ = CHAR_5;
break;
case 6:
*p++ = CHAR_6;
break;
case 7:
*p++ = CHAR_7;
break;
case 8:
*p++ = CHAR_8;
break;
case 9:
*p++ = CHAR_9;
break;
}
}
else if (isalpha(c)) {
if (*shift) {
*shift = 0;
*p++ = CHAR_SHIFT_DOWN;
}
*p++ = (char) (toupper(c) - 'A');
}
else if (i>=CHAR_NULL && i<=CHAR_CLOSED) {
*p++ = i;
}
else {
/*
* There is no reasonable mapping for this character
*/
*p++ = CHAR_NULL;
}
}
return p - baudot;
}
void
encode_to_baudot(rtty_conf *ctx, char c)
{
static char baudot_bits[][8] = {
{0, 1, 1, 0, 0, 0, 1, 1}, /* A */
{0, 1, 0, 0, 1, 1, 1, 1}, /* B */
{0, 0, 1, 1, 1, 0, 1, 1}, /* C */
{0, 1, 0, 0, 1, 0, 1, 1}, /* D */
{0, 1, 0, 0, 0, 0, 1, 1}, /* E / 3 */
{0, 1, 0, 1, 1, 0, 1, 1}, /* F */
{0, 0, 1, 0, 1, 1, 1, 1}, /* G */
{0, 0, 0, 1, 0, 1, 1, 1}, /* H */
{0, 0, 1, 1, 0, 0, 1, 1}, /* I / 8 */
{0, 1, 1, 0, 1, 0, 1, 1}, /* J */
{0, 1, 1, 1, 1, 0, 1, 1}, /* K */
{0, 0, 1, 0, 0, 1, 1, 1}, /* L */
{0, 0, 0, 1, 1, 1, 1, 1}, /* M / . */
{0, 0, 0, 1, 1, 0, 1, 1}, /* N */
{0, 0, 0, 0, 1, 1, 1, 1}, /* O / 9 */
{0, 0, 1, 1, 0, 1, 1, 1}, /* P / 0 */
{0, 1, 1, 1, 0, 1, 1, 1}, /* Q / 1 */
{0, 0, 1, 0, 1, 0, 1, 1}, /* R / 4 */
{0, 1, 0, 1, 0, 0, 1, 1}, /* S */
{0, 0, 0, 0, 0, 1, 1, 1}, /* T / 5 */
{0, 1, 1, 1, 0, 0, 1, 1}, /* U / 7 */
{0, 0, 1, 1, 1, 1, 1, 1}, /* V */
{0, 1, 1, 0, 0, 1, 1, 1}, /* W / 2 */
{0, 1, 0, 1, 1, 1, 1, 1}, /* X / / */
{0, 1, 0, 1, 0, 1, 1, 1}, /* Y / 6 */
{0, 1, 0, 0, 0, 1, 1, 1}, /* Z */
{0, 0, 0, 0, 0, 0, 1, 1}, /* NULL */
{0, 0, 1, 0, 0, 0, 1, 1}, /* LF */
{0, 0, 0, 1, 0, 0, 1, 1}, /* SPACE */
{0, 0, 0, 0, 1, 0, 1, 1}, /* CR */
{0, 1, 1, 0, 1, 1, 1, 1}, /* SHIFT_UP */
{0, 1, 1, 1, 1, 1, 1, 1}, /* SHIFT_DOWN */
{0, 0, 0, 0, 0, 0, 0, 0}, /* Open */
{1, 1, 1, 1, 1, 1, 1, 1} /* closed */
};
int i = (int) c;
if (i>=0 && i<(sizeof(baudot_bits)/8)) {
encode_bits(ctx, baudot_bits[i], 8);
}
}
void encode_bits(rtty_conf *ctx, char *bits, int len)
{
int i;
for (i=0; i < len; i++) {
encode_bit(ctx, bits[i]);
}
}
void
encode_bit(rtty_conf *ctx, int c)
{
switch(c) {
case 0:
digitalWrite(0, HIGH);
delay_usec(ctx->bit_delay);
break;
case 1:
digitalWrite(0, LOW);
delay_usec(ctx->bit_delay);
break;
}
}
/*
* Put terminal into raw mode.
* The "definitive" set raw code? I don't know about that
* but it works.
*
* Until proven otherwise this is the way to put a pty into raw mode.
* This has been tested by streaming binary data into and from
* a pty after being set into raw mode with this code, and it
* was completely unmodified by the pty. Earlier versions of
* set_raw() could not claim this fact.
*/
int set_raw(int fd, struct termios *old_mode)
{
struct termios mode;
if (!isatty(fd)) {
return (-1);
}
tcgetattr(fd, &mode);
if (old_mode) {
tcgetattr(fd, old_mode);
}
mode.c_iflag = 0;
mode.c_oflag &= ~OPOST;
mode.c_lflag &= ~(IEXTEN | ISIG | ICANON | ECHO | XCASE);
mode.c_cflag &= ~(CSIZE | PARENB);
mode.c_cflag |= CS8;
mode.c_cc[VMIN] = 1;
mode.c_cc[VTIME] = 1;
tcsetattr(fd, TCSANOW, &mode);
return 0;
}