-
Notifications
You must be signed in to change notification settings - Fork 0
/
irtoy.c
486 lines (453 loc) · 11.7 KB
/
irtoy.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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <fcntl.h>
#include <termios.h>
#include <ctype.h>
#include <sys/stat.h>
#include <unistd.h>
#include "irtoy.h"
#include "error.h"
#include "dict.h"
int irtoy_gap = 8; /* min gap between packets */
int irtoy_jitter = 3; /* acceptable jitter */
IRPacket *
new_irpacket (void)
{
IRPacket *k;
k = malloc (sizeof *k);
k->n_pulses_allocated = 8;
k->n_pulses = 0;
k->pulses = calloc (k->n_pulses_allocated, sizeof *k->pulses);
return k;
}
void
free_irpacket (IRPacket * k)
{
if (k->pulses)
free (k->pulses);
free (k);
}
bool
irpacket_complete (IRPacket * k)
{
/* XXX Complete the packet */
return false;
}
void
irpacket_pulse (IRPacket * k, IRPulse pulse)
{
/* Add a pulse to a packet */
if (k->n_pulses_allocated == k->n_pulses)
{
k->n_pulses_allocated *= 2;
k->pulses =
realloc (k->pulses, k->n_pulses_allocated * sizeof *k->pulses);
}
k->pulses[k->n_pulses++] = pulse;
}
void
irpacket_printf (FILE * out, IRPacket * k)
{
/* Print it out */
int i;
bool expected_value = true;
if (!k)
{
fprintf (out, "NULL");
return;
}
fprintf (out, " { ");
for (i = 0; i < k->n_pulses; i++)
{
#if 0
if (k->pulses[i].value != expected_value)
fatal (0, "Unexpected pulse value: pulse %d (of %d) has value %d",
i, k->n_pulses, k->pulses[i].value);
#endif
fprintf (out, "%d ", k->pulses[i].width);
expected_value = !expected_value;
}
fprintf (out, "} ");
}
void
irpacket_decode (IRPacket * k)
{
#define IMUL(x, f) (((int)(512 * (f)) * x ) / 512)
int i;
int packet_len = 0;
int min_width = 0;
int max_bits = 0;
int min_bits = 0;
int min_res = 2; /* minimum resolution of +/- 2 */
const double proportional_error = 1.1;
for (i = 0; i < k->n_pulses; i++)
{
packet_len += k->pulses[i].width;
if (k->pulses[i].width < min_width || min_width == 0)
min_width = k->pulses[i].width;
}
printf("Decode packet: "); irpacket_printf(stdout, k);
printf("\n"); irpacket_render(stdout, k);
printf("\nMinimum pulse width = %d, total packet time = %d\n",
min_width, packet_len);
/* Minimum number of bits: there *must* be at least as many bits as
there were pulses. Also, the minimum size of a pulse might set a
lower bound: a pulse can be at most one bit wide, give or take. */
if (IMUL(min_width, 0.9) > 0)
min_bits = packet_len / (IMUL(min_width, 0.9));
else
min_bits = packet_len;
printf("First guess at min bits: %d (by min pulse width)\n", min_bits);
if (min_bits < k->n_pulses)
{
printf("Number of bits must be at least number of pulses, so %d\n",
k->n_pulses);
min_bits = k->n_pulses;
}
/* Maximum bits: our minimum resolution is +/- 3. If bits are any
smaller than that, we stand no chance of recognising them. */
max_bits = packet_len / (2 * min_res);
printf("Can only fit %d bits (of min size %d) in %d time.\n",
max_bits, 2*min_res, packet_len);
printf("Packet has somewhere between %d and %d bits.\n", min_bits, max_bits);
#undef IMUL
}
void
irpacket_render (FILE * out, IRPacket * k)
{
int term_width = 78;
int total_width;
int i, c, x;
char *cols;
if ((cols = getenv("COLUMNS")))
{
term_width = atoi(cols);
}
total_width = 0;
for (i = 0; i < k->n_pulses; i++)
total_width += k->pulses[i].width;
c = 0;
x = 0;
for (i = 0; i < k->n_pulses; i++)
{
x += k->pulses[i].width;
while (c < 1.0 * x * term_width / total_width)
{
c++;
if (k->pulses[i].value)
fputc ('|', out);
else
fputc ('_', out);
}
}
}
IRPacket *
irpacket_scanf (FILE * in)
{
/* Read it from IN */
IRPacket *k = new_irpacket ();
char buffer[BUFSIZ];
IRPulse pulse;
pulse.value = 0;
if (!fscanf (in, "%s", buffer) || feof (in))
return NULL;
if (strcmp (buffer, "{"))
fatal (0, "Malformed packet: expected '{', got '%s'", buffer);
while (!feof (in))
{
fscanf (in, "%s", buffer);
if (strcmp (buffer, "}"))
{
int width = atoi (buffer);
if (width < 0 || width > 0xffff)
fatal (0, "Malformed packet: expected short int or '}', got '%s'",
buffer);
pulse.width = width;
pulse.value = !pulse.value;
irpacket_pulse (k, pulse);
}
else
{
irpacket_complete (k);
break;
}
}
return k;
}
/* Do two packets match? */
bool
irpacket_match (IRPacket * a, IRPacket * b, int jitter)
{
int i;
int a_total = 0, b_total = 0;
if (a->n_pulses != b->n_pulses)
return false;
for (i = 0; i < a->n_pulses; i++)
{
a_total += a->pulses[i].width;
b_total += b->pulses[i].width;
if (abs (a->pulses[i].width - b->pulses[i].width) <= jitter)
continue;
else
return false;
}
/* The total packet time should also fit into the allowed jitter:
* it's not cumulative.
*/
if (abs (a_total - b_total) <= jitter)
return true;
else
return false;
}
/* ------------------------------------------------------------
* Dict/Symbol handling
* XXX note: just about everything here is O(n). That shouldn't matter
* though.
*/
IRDict *
new_irdict (void)
{
IRDict *d = malloc (sizeof *d);
d->first = NULL;
d->by_name = dict_new (NULL);
return d;
}
/* Find a symbol (any symbol) for this name.
*/
IRPacket *
irdict_lookup_name (IRDict * d, const char *name)
{
IRSymbol *s;
s = dict_get (d->by_name, name);
if (s)
return s->packet;
else
return NULL;
}
/* Find a symbol name for this packet */
const char *
irdict_lookup_packet (IRDict * d, IRPacket * k)
{
IRSymbol *s;
for (s = d->first; s; s = s->next)
if (irpacket_match (s->packet, k, irtoy_jitter))
return s->name;
return NULL;
}
/* Insert a symbol in the dictionary */
void
irdict_insert (IRDict * d, const char *name, IRPacket * k)
{
IRSymbol *s = malloc (sizeof *s);
s->next = d->first;
s->name = name;
s->packet = k;
d->first = s;
if (!dict_has_key (d->by_name, name))
dict_insert (d->by_name, name, s);
//XXX
irpacket_decode(k);
}
/* ------------------------------------------------------------
* IR State handling
*/
IRState *
new_irstate (void)
{
IRState *ir;
ir = malloc (sizeof *ir);
ir->value = false;
ir->packet = NULL;
ir->last_width = -1;
ir->fd = -1;
ir->buf_valid = false;
ir->timed_out = false;
return ir;
}
IRPacket *
irstate_pulse (IRState * ir, unsigned short width)
{
IRPacket *k;
IRPulse p;
bool timed_out = ir->timed_out;
ir->timed_out = false;
/* Previous signal value was IR->VALUE. This pulse marks the end of
that phase with a pulse of width WIDTH. */
if (width == 0xffff)
{
/* Dummy end pulse */
if (ir->packet)
irpacket_complete (ir->packet);
k = ir->packet;
ir->packet = NULL;
ir->value = false; /* idle */
return k;
}
/* Real pulse, flip current state */
ir->value = !ir->value;
if (ir->value == false && width > irtoy_gap * ir->last_width)
{
/* Gap between packets */
/* Previous packet was ended on a timeout. This is the gap
between the previous packet and a new packet. Drop it on the
floor. */
if (timed_out)
return NULL;
if (!ir->packet)
fatal (0, "Got gap without a current packet");
irpacket_complete (ir->packet);
k = ir->packet;
ir->packet = NULL;
ir->value = false;
return k;
}
/* Got an actual non-terminal pulse */
if (!ir->packet)
ir->packet = new_irpacket ();
p.value = ir->value;
p.width = width;
irpacket_pulse (ir->packet, p);
ir->last_width = width;
return NULL;
}
IRPacket *
irstate_timeout (IRState * ir)
{
IRPacket *k = NULL;
if (ir->packet)
irpacket_complete (ir->packet);
k = ir->packet;
ir->packet = NULL;
ir->timed_out = true;
if (ir->buf_valid && ir->buf == 0xff)
{
/* Timed out with a single 0xff in the byte buffer. Suspicious.
* This probably means we're out of sync and have actually
* received a 0xff 0xff pair, but mistakenly interpreted the
* second 0xff as the first byte of a word beginning with 0xff..
* So let's drop that on the floor.
*/
ir->buf_valid = false;
}
return k;
}
int
irstate_rxbytes (IRState * ir, int n_bytes, unsigned char *bytes,
IRPacket ** out_packets)
{
int i;
int n_out_packets = 0;
for (i = 0; i < n_bytes; i++)
{
if (ir->buf_valid)
{
unsigned short width;
IRPacket *k;
width = ir->buf << 8; /* prior byte was high byte */
width |= bytes[i];
k = irstate_pulse (ir, width);
ir->buf_valid = false;
if (k)
out_packets[n_out_packets++] = k;
}
else
{
ir->buf_valid = true;
ir->buf = bytes[i];
}
}
return n_out_packets;
}
void
irstate_open (IRState * ir, const char *dev)
{
struct termios t_opt;
struct stat st;
int i;
if (strchr (dev, '*'))
{
/* Wildcard in device name; try 0-9 in place of the '*'. */
int i = 0;
char buffer[BUFSIZ];
char *cp;
strcpy (buffer, dev);
cp = strchr (buffer, '*');
for (i = 0; i <= 9; i++)
{
*cp = '0' + i;
/* Open TTY in blocking mode. We'll set it non-blocking later. */
ir->fd = open (buffer, O_RDWR | O_NOCTTY);
if (ir->fd != -1)
break;
}
}
else
{
/* Open TTY in blocking mode. We'll set it non-blocking later. */
ir->fd = open (dev, O_RDWR | O_NOCTTY);
}
if (ir->fd == -1)
fatal (0, "Cannot open device '%s'", dev);
fstat (ir->fd, &st);
if (S_ISCHR (st.st_mode))
{
char c, c2, c3;
int open_tries = 5;
/* Largely lifted from IRToyRecPlay.
* IRToyRecPlay also sets the terminal speed, but I don't think
* we should have to worry about that.
*/
tcgetattr (ir->fd, &t_opt);
cfsetispeed (&t_opt, 921600);
cfsetospeed (&t_opt, 921600);
t_opt.c_cflag |= (CLOCAL | CREAD);
t_opt.c_cflag &= ~PARENB;
t_opt.c_cflag &= ~CSTOPB;
t_opt.c_cflag &= ~CSIZE;
t_opt.c_cflag |= CS8;
t_opt.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
t_opt.c_iflag &= ~(IXON | IXOFF | IXANY);
t_opt.c_iflag &= ~(ICRNL | INLCR);
t_opt.c_oflag &= ~(OCRNL | ONLCR);
t_opt.c_oflag &= ~OPOST;
t_opt.c_cc[VMIN] = 0;
t_opt.c_cc[VTIME] = 10;
tcflush (ir->fd, TCIOFLUSH);
tcsetattr (ir->fd, TCSANOW, &t_opt);
fprintf (stdout, "Initialising IRToy");
do
{
for (i = 0; i < 5; i++)
{
c = '\0';
fprintf (stdout, ".");
fflush (stdout);
write (ir->fd, &c, 1);
}
sleep (1 + 5 - open_tries);
fprintf (stdout, "\nSetting sampling mode\n");
c = 'S';
write (ir->fd, &c, 1);
/* Read protocol version */
if (read (ir->fd, &c, 1)
&& read (ir->fd, &c2, 1) && read (ir->fd, &c3, 1))
{
fprintf (stdout, "Protocol version '%c%c%c'\n", c, c2, c3);
if (c == 'S' && isdigit (c2) && isdigit (c3))
break;
else
{
if (--open_tries == 0)
fatal (0, "Couldn't set sampling mode");
fprintf (stdout, "Read bogus response, retrying...\n");
}
}
else
fatal (0, "Couldn't read protocol version\n");
}
while (open_tries);
}
fcntl (ir->fd, F_SETFL, fcntl (ir->fd, F_GETFL) | O_NONBLOCK);
}