forked from foxharp/avrlirc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathavrlirc2udp.c
423 lines (360 loc) · 8.97 KB
/
avrlirc2udp.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
/*
* avrlirc2udp.c
*
* transfer characters from avrlirc device on a serial port to an
* lircd daemon running the udp "driver". the output of the
* arvlirc device matches the expected lircd input pretty much
* exactly. we try to read pairs of bytes using the VMIN parameter
* of the tty driver, but other than that, it's mainly a data copy.
*
* any "out-of-band", i.e., non-IR data is prefixed by a pair of zero
* bytes. currently unused.
*
* one problem is that if we somehow start our reads "halfway" through
* one of the 16 bit data words, we'll forever be out-of-sync. we
* try to correct this by noticing two things: a) if the data
* contains two zero bytes in succession, then they're definitely
* a pair (since this is the out-of-band data prefix), and b) the
* 16 bit values that we read should have alternating high bits:
* 0x8000, 0x0000, 0x8000, etc.
*
**********
*
* Copyright (C) 2007, Paul G. Fox
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the Free
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <syslog.h>
#include <termios.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/ioctl.h>
#include <errno.h>
#define LIRCD_UDP_PORT 8765
extern char *optarg;
extern int optind, opterr, optopt;
char *prog;
int daemonized;
/* debugging can either replace, or augment, normal operation */
#define DEBUG_ONLY 1
#define DEBUG_AND_CONNECT 2
int debug;
void
usage(void)
{
fprintf(stderr,
"usage: %s [options] -t <ttydev> -h <lircd_host> [-p <lircd_port>]\n"
" lircd_port defaults to 8765.\n"
" use '-T' to make a TCP connection rather than UDP.\n"
" use '-d' for debugging (with socket connection).\n"
" use '-D' for debugging (without socket connection).\n"
" use '-f' to keep program in foreground.\n"
" use '-H' for high speed tty (115200 instead of 38400).\n"
" use '-w S' to poll the creation of ttydev at S second intervals.\n"
, prog);
exit(1);
}
void
report(char *s)
{
if (daemonized)
syslog(LOG_NOTICE, "%s", s);
else
fprintf(stderr, "%s\n", s);
}
void
die(char *s)
{
if (daemonized) {
syslog(LOG_ERR, "%s: %m", s);
syslog(LOG_ERR, "exiting");
} else {
perror(s);
}
exit(1);
}
int socket_init(int tcp, char *host, int port)
{
struct sockaddr_in sa[1];
struct hostent *hent;
int s;
hent = gethostbyname(host);
if (!hent) {
fprintf(stderr, "%s: gethostbyname: ", prog);
herror(host);
exit(1);
}
if (tcp) {
if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0)
die("socket");
} else {
if ((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
die("socket");
}
memset((char *) sa, 0, sizeof(*sa));
sa->sin_family = AF_INET;
sa->sin_port = htons(port);
sa->sin_addr = *((struct in_addr *)hent->h_addr);
if (connect(s, (struct sockaddr *)sa, sizeof(*sa)) < 0)
return -1;
return s;
}
/* shared by tty_init()/tty_restore() */
struct termios prev_tios;
int tty_fd;
void
tty_restore(void)
{
tcsetattr(tty_fd, TCSADRAIN, &prev_tios);
}
void
sighandler(int sig)
{
tty_restore();
die("signal");
}
int tty_init(char *term, int wait_term, int speed)
{
int s;
struct termios tios;
int flags;
int logged = 0;
while(1) {
tty_fd = open(term, O_RDWR);
if (tty_fd >= 0 || errno != ENOENT || !wait_term)
break;
if (!logged)
report("waiting for tty creation");
logged = 1;
sleep(wait_term);
}
if (logged)
report("found tty");
if (tty_fd < 0)
die("can't open tty");
if (!isatty(tty_fd))
die("not a tty");
s = tcgetattr(tty_fd, &prev_tios);
if (s < 0)
die("ttopen tcgetattr");
/* set up restore hooks quickly */
atexit(tty_restore);
signal(SIGTERM, sighandler);
signal(SIGHUP, sighandler);
tios = prev_tios;
tios.c_oflag = 0; /* no output flags at all */
tios.c_lflag = 0; /* no line flags at all */
tios.c_cflag &= ~PARENB; /* disable parity, both in and out */
tios.c_cflag |= CSTOPB|CLOCAL|CS8|CREAD; /* two stop bits on transmit */
/* no modem control, 8bit chars, */
/* receiver enabled, */
tios.c_iflag = IGNBRK | IGNPAR; /* ignore break, ignore parity errors */
tios.c_cc[VMIN] = 2;
tios.c_cc[VTIME] = 0;
tios.c_cc[VSUSP] = _POSIX_VDISABLE;
tios.c_cc[VSTART] = _POSIX_VDISABLE;
tios.c_cc[VSTOP] = _POSIX_VDISABLE;
s = cfsetospeed (&tios, speed);
if (s < 0)
die("ttopen cfsetospeed");
s = cfsetispeed (&tios, speed);
if (s < 0)
die("ttopen cfsetispeed");
s = tcsetattr(tty_fd, TCSAFLUSH, &tios);
if (s < 0)
die("ttopen tcsetattr");
/* make sure RTS and DTR lines are high, since device may be
* phantom-powered. no termios/posix way to do this, that i
* know of.
*/
if (ioctl(tty_fd, TIOCMGET, &flags) >= 0) {
flags &= ~TIOCM_RTS;
flags &= ~TIOCM_DTR;
ioctl(tty_fd, TIOCMSET, &flags);
}
return tty_fd;
}
void
process_oob(int from, unsigned char *buf)
{
// nothing here
}
void
data_loop(int from, int tcp, char *host, int port)
{
unsigned char b[2];
int n;
int prevhighbit = -1;
#if OUT_OF_BAND_SOMEDAY
int prevwaszero;
#endif
int highbit;
static int to = -1;
while (1) {
/* leave extra space in buf for extra bytes we may read below */
if ((n = read(from, b, 2)) < 0)
die("read");
/* in my experience, this results from a USB serial
* device being unplugged */
if (n == 0)
return;
if (n == 1) { /* short read -- force it even */
if (read(from, &b[1], 1) <= 0)
die("read");
n++;
}
if (debug) {
int pulse;
pulse = (b[1] << 8) + b[0];
pulse &= 0xffff;
fprintf(stderr, "%s 0x%04x (%d)\n",
(pulse & 0x8000) ? "pulse":"space", pulse, pulse & 0x7fff);
}
#if OUT_OF_BAND_SOMEDAY
if (oob) { // these bytes are out-of-band
process_oob(from, b);
prevhighbit = -1;
prevwaszero = 0;
oob = 0;
continue;
}
//if (debug)
// debug_buffer("", buf, n);
if (b[0] == 0 && b[1] == 0) {
// then we're definitely in phase
oob = 1;
prevwaszero = 0;
continue;
}
oob = 0;
if (prevwaszero && b[0] == 0) {
// then we're definitely out of phase
report("phase correction");
b[0] = b[1];
if (read(from, &b[1], 1) <= 0)
die("read");
process_oob(from, b);
prevhighbit = -1;
prevwaszero = 0;
continue;
}
#endif
highbit = (b[1] & 0x80);
if (highbit == prevhighbit) {
report("phase correction");
b[0] = b[1];
if (read(from, &b[1], 1) <= 0)
die("read");
highbit = (b[1] & 0x80);
}
prevhighbit = highbit;
#if OUT_OF_BAND_SOMEDAY
prevwaszero = (b[1] == 0);
#endif
if (debug != DEBUG_ONLY) { // sending to host
if (to < 0)
to = socket_init(tcp, host, port);
if (to >= 0) {
if (write(to, b, 2) < 0) {
if (errno != ECONNREFUSED)
die("write");
}
}
}
}
}
int
main(int argc, char *argv[])
{
char *p;
char *host = 0, *term = 0;
int port = LIRCD_UDP_PORT;
int foreground = 0;
int wait_term = 0;
int tcp = 0;
int speed = B38400;
int tty;
int c;
prog = argv[0];
p = strrchr(argv[0], '/');
if (p) prog = p + 1;
while ((c = getopt(argc, argv, "HdDTfw:t:h:p:")) != EOF) {
switch (c) {
case 'H':
speed = B115200;
break;
case 'd':
debug = DEBUG_AND_CONNECT;
break;
case 'D':
debug = DEBUG_ONLY;
break;
case 'f':
foreground = 1;
break;
case 't':
term = optarg;
break;
case 'w':
wait_term = atoi(optarg);
if (wait_term == 0)
usage();
break;
case 'h':
host = optarg;
break;
case 'T':
tcp = 1;
break;
case 'p': /* or microseconds */
port = atoi(optarg);
break;
default:
usage();
break;
}
}
if ((debug != DEBUG_ONLY && !host) || !term || optind != argc) {
usage();
}
while (1) {
tty = tty_init(term, wait_term, speed);
if (!daemonized && !foreground && !debug) {
if (daemon(0, 0) < 0)
die("daemon");
daemonized = 1;
}
data_loop(tty, tcp, host, port);
/* we'll only ever return from data_loop() if our read()
* returns 0, which usually means our (USB-based) tty has
* gone away. loop if we were told to wait (-w) for it.
*/
if (!wait_term)
die("end-of-dataloop");
tty_restore();
close(tty);
}
return 0;
}