1 /* $Id: receive.c,v 5.43 2010/04/13 16:28:10 lirc Exp $ */
2
3 /****************************************************************************
4 ** receive.c ***************************************************************
5 ****************************************************************************
6 *
7 * functions that decode IR codes
8 *
9 * Copyright (C) 1999 Christoph Bartelmus <lirc@bartelmus.de>
10 *
11 */
12
13 #ifdef HAVE_CONFIG_H
14 # include <config.h>
15 #endif
16
17 #include <limits.h>
18
19 #include "hardware.h"
20 #include "lircd.h"
21 #include "receive.h"
22
23 extern struct hardware hw;
24 extern struct ir_remote *last_remote;
25
26 struct rbuf rec_buffer;
27
lirc_t_max(lirc_t a,lirc_t b)28 inline lirc_t lirc_t_max(lirc_t a, lirc_t b)
29 {
30 return (a > b ? a : b);
31 }
32
set_pending_pulse(lirc_t deltap)33 inline void set_pending_pulse(lirc_t deltap)
34 {
35 LOGPRINTF(5, "pending pulse: %lu", deltap);
36 rec_buffer.pendingp = deltap;
37 }
38
set_pending_space(lirc_t deltas)39 inline void set_pending_space(lirc_t deltas)
40 {
41 LOGPRINTF(5, "pending space: %lu", deltas);
42 rec_buffer.pendings = deltas;
43 }
44
get_next_rec_buffer_internal(lirc_t maxusec)45 static lirc_t get_next_rec_buffer_internal(lirc_t maxusec)
46 {
47 if (rec_buffer.rptr < rec_buffer.wptr) {
48 LOGPRINTF(3, "<%c%lu", rec_buffer.data[rec_buffer.rptr] & PULSE_BIT ? 'p' : 's', (__u32)
49 rec_buffer.data[rec_buffer.rptr] & (PULSE_MASK));
50 rec_buffer.sum += rec_buffer.data[rec_buffer.rptr] & (PULSE_MASK);
51 return (rec_buffer.data[rec_buffer.rptr++]);
52 } else {
53 if (rec_buffer.wptr < RBUF_SIZE) {
54 lirc_t data = 0;
55 unsigned long elapsed = 0;
56
57 if (timerisset(&rec_buffer.last_signal_time)) {
58 struct timeval current;
59
60 gettimeofday(¤t, NULL);
61 elapsed = time_elapsed(&rec_buffer.last_signal_time, ¤t);
62 }
63 if (elapsed < maxusec) {
64 data = hw.readdata(maxusec - elapsed);
65 }
66 if (!data) {
67 LOGPRINTF(3, "timeout: %u", maxusec);
68 return 0;
69 }
70 if (LIRC_IS_TIMEOUT(data)) {
71 LOGPRINTF(1, "timeout received: %lu", (__u32) LIRC_VALUE(data));
72 if (LIRC_VALUE(data) < maxusec) {
73 return get_next_rec_buffer_internal(maxusec - LIRC_VALUE(data));
74 }
75 return 0;
76 }
77
78 rec_buffer.data[rec_buffer.wptr] = data;
79 if (rec_buffer.data[rec_buffer.wptr] == 0)
80 return (0);
81 rec_buffer.sum += rec_buffer.data[rec_buffer.rptr]
82 & (PULSE_MASK);
83 rec_buffer.wptr++;
84 rec_buffer.rptr++;
85 LOGPRINTF(3, "+%c%lu", rec_buffer.data[rec_buffer.rptr - 1] & PULSE_BIT ? 'p' : 's', (__u32)
86 rec_buffer.data[rec_buffer.rptr - 1]
87 & (PULSE_MASK));
88 return (rec_buffer.data[rec_buffer.rptr - 1]);
89 } else {
90 rec_buffer.too_long = 1;
91 return (0);
92 }
93 }
94 return (0);
95 }
96
get_next_rec_buffer(lirc_t maxusec)97 lirc_t get_next_rec_buffer(lirc_t maxusec)
98 {
99 return get_next_rec_buffer_internal(receive_timeout(maxusec));
100 }
101
init_rec_buffer(void)102 void init_rec_buffer(void)
103 {
104 memset(&rec_buffer, 0, sizeof(rec_buffer));
105 }
106
rewind_rec_buffer(void)107 void rewind_rec_buffer(void)
108 {
109 rec_buffer.rptr = 0;
110 rec_buffer.too_long = 0;
111 set_pending_pulse(0);
112 set_pending_space(0);
113 rec_buffer.sum = 0;
114 }
115
clear_rec_buffer(void)116 int clear_rec_buffer(void)
117 {
118 int move, i;
119
120 timerclear(&rec_buffer.last_signal_time);
121 if (hw.rec_mode == LIRC_MODE_LIRCCODE) {
122 unsigned char buffer[sizeof(ir_code)];
123 size_t count;
124
125 count = hw.code_length / CHAR_BIT;
126 if (hw.code_length % CHAR_BIT)
127 count++;
128
129 if (read(hw.fd, buffer, count) != count) {
130 logprintf(LOG_ERR, "reading in mode LIRC_MODE_LIRCCODE failed");
131 return (0);
132 }
133 for (i = 0, rec_buffer.decoded = 0; i < count; i++) {
134 rec_buffer.decoded = (rec_buffer.decoded << CHAR_BIT) + ((ir_code) buffer[i]);
135 }
136 } else {
137 lirc_t data;
138
139 move = rec_buffer.wptr - rec_buffer.rptr;
140 if (move > 0 && rec_buffer.rptr > 0) {
141 memmove(&rec_buffer.data[0], &rec_buffer.data[rec_buffer.rptr],
142 sizeof(rec_buffer.data[0]) * move);
143 rec_buffer.wptr -= rec_buffer.rptr;
144 } else {
145 rec_buffer.wptr = 0;
146 data = hw.readdata(0);
147
148 LOGPRINTF(3, "c%lu", (__u32) data & (PULSE_MASK));
149
150 rec_buffer.data[rec_buffer.wptr] = data;
151 rec_buffer.wptr++;
152 }
153 }
154
155 rewind_rec_buffer();
156 rec_buffer.is_biphase = 0;
157
158 return (1);
159 }
160
unget_rec_buffer(int count)161 inline void unget_rec_buffer(int count)
162 {
163 LOGPRINTF(5, "unget: %d", count);
164 if (count == 1 || count == 2) {
165 rec_buffer.rptr -= count;
166 rec_buffer.sum -= rec_buffer.data[rec_buffer.rptr] & (PULSE_MASK);
167 if (count == 2) {
168 rec_buffer.sum -= rec_buffer.data[rec_buffer.rptr + 1]
169 & (PULSE_MASK);
170 }
171 }
172 }
173
unget_rec_buffer_delta(lirc_t delta)174 inline void unget_rec_buffer_delta(lirc_t delta)
175 {
176 rec_buffer.rptr--;
177 rec_buffer.sum -= delta & (PULSE_MASK);
178 rec_buffer.data[rec_buffer.rptr] = delta;
179 }
180
get_next_pulse(lirc_t maxusec)181 inline lirc_t get_next_pulse(lirc_t maxusec)
182 {
183 lirc_t data;
184
185 data = get_next_rec_buffer(maxusec);
186 if (data == 0)
187 return (0);
188 if (!is_pulse(data)) {
189 LOGPRINTF(2, "pulse expected");
190 return (0);
191 }
192 return (data & (PULSE_MASK));
193 }
194
get_next_space(lirc_t maxusec)195 inline lirc_t get_next_space(lirc_t maxusec)
196 {
197 lirc_t data;
198
199 data = get_next_rec_buffer(maxusec);
200 if (data == 0)
201 return (0);
202 if (!is_space(data)) {
203 LOGPRINTF(2, "space expected");
204 return (0);
205 }
206 return (data);
207 }
208
sync_pending_pulse(struct ir_remote * remote)209 inline int sync_pending_pulse(struct ir_remote *remote)
210 {
211 if (rec_buffer.pendingp > 0) {
212 lirc_t deltap;
213
214 deltap = get_next_pulse(rec_buffer.pendingp);
215 if (deltap == 0)
216 return 0;
217 if (!expect(remote, deltap, rec_buffer.pendingp))
218 return 0;
219 set_pending_pulse(0);
220 }
221 return 1;
222 }
223
sync_pending_space(struct ir_remote * remote)224 inline int sync_pending_space(struct ir_remote *remote)
225 {
226 if (rec_buffer.pendings > 0) {
227 lirc_t deltas;
228
229 deltas = get_next_space(rec_buffer.pendings);
230 if (deltas == 0)
231 return 0;
232 if (!expect(remote, deltas, rec_buffer.pendings))
233 return 0;
234 set_pending_space(0);
235 }
236 return 1;
237 }
238
expectpulse(struct ir_remote * remote,int exdelta)239 int expectpulse(struct ir_remote *remote, int exdelta)
240 {
241 lirc_t deltap;
242 int retval;
243
244 LOGPRINTF(5, "expecting pulse: %lu", exdelta);
245 if (!sync_pending_space(remote))
246 return 0;
247
248 deltap = get_next_pulse(rec_buffer.pendingp + exdelta);
249 if (deltap == 0)
250 return (0);
251 if (rec_buffer.pendingp > 0) {
252 if (rec_buffer.pendingp > deltap)
253 return 0;
254 retval = expect(remote, deltap - rec_buffer.pendingp, exdelta);
255 if (!retval)
256 return (0);
257 set_pending_pulse(0);
258 } else {
259 retval = expect(remote, deltap, exdelta);
260 }
261 return (retval);
262 }
263
expectspace(struct ir_remote * remote,int exdelta)264 int expectspace(struct ir_remote *remote, int exdelta)
265 {
266 lirc_t deltas;
267 int retval;
268
269 LOGPRINTF(5, "expecting space: %lu", exdelta);
270 if (!sync_pending_pulse(remote))
271 return 0;
272
273 deltas = get_next_space(rec_buffer.pendings + exdelta);
274 if (deltas == 0)
275 return (0);
276 if (rec_buffer.pendings > 0) {
277 if (rec_buffer.pendings > deltas)
278 return 0;
279 retval = expect(remote, deltas - rec_buffer.pendings, exdelta);
280 if (!retval)
281 return (0);
282 set_pending_space(0);
283 } else {
284 retval = expect(remote, deltas, exdelta);
285 }
286 return (retval);
287 }
288
expectone(struct ir_remote * remote,int bit)289 inline int expectone(struct ir_remote *remote, int bit)
290 {
291 if (is_biphase(remote)) {
292 int all_bits = bit_count(remote);
293 ir_code mask;
294
295 mask = ((ir_code) 1) << (all_bits - 1 - bit);
296 if (mask & remote->rc6_mask) {
297 if (remote->sone > 0 && !expectspace(remote, 2 * remote->sone)) {
298 unget_rec_buffer(1);
299 return (0);
300 }
301 set_pending_pulse(2 * remote->pone);
302 } else {
303 if (remote->sone > 0 && !expectspace(remote, remote->sone)) {
304 unget_rec_buffer(1);
305 return (0);
306 }
307 set_pending_pulse(remote->pone);
308 }
309 } else if (is_space_first(remote)) {
310 if (remote->sone > 0 && !expectspace(remote, remote->sone)) {
311 unget_rec_buffer(1);
312 return (0);
313 }
314 if (remote->pone > 0 && !expectpulse(remote, remote->pone)) {
315 unget_rec_buffer(2);
316 return (0);
317 }
318 } else {
319 if (remote->pone > 0 && !expectpulse(remote, remote->pone)) {
320 unget_rec_buffer(1);
321 return (0);
322 }
323 if (remote->ptrail > 0) {
324 if (remote->sone > 0 && !expectspace(remote, remote->sone)) {
325 unget_rec_buffer(2);
326 return (0);
327 }
328 } else {
329 set_pending_space(remote->sone);
330 }
331 }
332 return (1);
333 }
334
expectzero(struct ir_remote * remote,int bit)335 inline int expectzero(struct ir_remote *remote, int bit)
336 {
337 if (is_biphase(remote)) {
338 int all_bits = bit_count(remote);
339 ir_code mask;
340
341 mask = ((ir_code) 1) << (all_bits - 1 - bit);
342 if (mask & remote->rc6_mask) {
343 if (!expectpulse(remote, 2 * remote->pzero)) {
344 unget_rec_buffer(1);
345 return (0);
346 }
347 set_pending_space(2 * remote->szero);
348 } else {
349 if (!expectpulse(remote, remote->pzero)) {
350 unget_rec_buffer(1);
351 return (0);
352 }
353 set_pending_space(remote->szero);
354 }
355 } else if (is_space_first(remote)) {
356 if (remote->szero > 0 && !expectspace(remote, remote->szero)) {
357 unget_rec_buffer(1);
358 return (0);
359 }
360 if (remote->pzero > 0 && !expectpulse(remote, remote->pzero)) {
361 unget_rec_buffer(2);
362 return (0);
363 }
364 } else {
365 if (!expectpulse(remote, remote->pzero)) {
366 unget_rec_buffer(1);
367 return (0);
368 }
369 if (remote->ptrail > 0) {
370 if (!expectspace(remote, remote->szero)) {
371 unget_rec_buffer(2);
372 return (0);
373 }
374 } else {
375 set_pending_space(remote->szero);
376 }
377 }
378 return (1);
379 }
380
sync_rec_buffer(struct ir_remote * remote)381 inline lirc_t sync_rec_buffer(struct ir_remote * remote)
382 {
383 int count;
384 lirc_t deltas, deltap;
385
386 count = 0;
387 deltas = get_next_space(1000000);
388 if (deltas == 0)
389 return (0);
390
391 if (last_remote != NULL && !is_rcmm(remote)) {
392 while (!expect_at_least(last_remote, deltas, last_remote->min_remaining_gap)) {
393 deltap = get_next_pulse(1000000);
394 if (deltap == 0)
395 return (0);
396 deltas = get_next_space(1000000);
397 if (deltas == 0)
398 return (0);
399 count++;
400 if (count > REC_SYNC) { /* no sync found,
401 let's try a diffrent remote */
402 return (0);
403 }
404 }
405 if (has_toggle_mask(remote)) {
406 if (!expect_at_most(last_remote, deltas, last_remote->max_remaining_gap)) {
407 remote->toggle_mask_state = 0;
408 remote->toggle_code = NULL;
409 }
410
411 }
412 }
413 rec_buffer.sum = 0;
414 return (deltas);
415 }
416
get_header(struct ir_remote * remote)417 inline int get_header(struct ir_remote *remote)
418 {
419 if (is_rcmm(remote)) {
420 lirc_t deltap, deltas, sum;
421
422 deltap = get_next_pulse(remote->phead);
423 if (deltap == 0) {
424 unget_rec_buffer(1);
425 return (0);
426 }
427 deltas = get_next_space(remote->shead);
428 if (deltas == 0) {
429 unget_rec_buffer(2);
430 return (0);
431 }
432 sum = deltap + deltas;
433 if (expect(remote, sum, remote->phead + remote->shead)) {
434 return (1);
435 }
436 unget_rec_buffer(2);
437 return (0);
438 } else if (is_bo(remote)) {
439 if (expectpulse(remote, remote->pone) && expectspace(remote, remote->sone)
440 && expectpulse(remote, remote->pone) && expectspace(remote, remote->sone)
441 && expectpulse(remote, remote->phead) && expectspace(remote, remote->shead)) {
442 return 1;
443 }
444 return 0;
445 }
446 if (remote->shead == 0) {
447 if (!sync_pending_space(remote))
448 return 0;
449 set_pending_pulse(remote->phead);
450 return 1;
451 }
452 if (!expectpulse(remote, remote->phead)) {
453 unget_rec_buffer(1);
454 return (0);
455 }
456 /* if this flag is set I need a decision now if this is really
457 a header */
458 if (remote->flags & NO_HEAD_REP) {
459 lirc_t deltas;
460
461 deltas = get_next_space(remote->shead);
462 if (deltas != 0) {
463 if (expect(remote, remote->shead, deltas)) {
464 return (1);
465 }
466 unget_rec_buffer(2);
467 return (0);
468 }
469 }
470
471 set_pending_space(remote->shead);
472 return (1);
473 }
474
get_foot(struct ir_remote * remote)475 inline int get_foot(struct ir_remote *remote)
476 {
477 if (!expectspace(remote, remote->sfoot))
478 return (0);
479 if (!expectpulse(remote, remote->pfoot))
480 return (0);
481 return (1);
482 }
483
get_lead(struct ir_remote * remote)484 inline int get_lead(struct ir_remote *remote)
485 {
486 if (remote->plead == 0)
487 return 1;
488 if (!sync_pending_space(remote))
489 return 0;
490 set_pending_pulse(remote->plead);
491 return 1;
492 }
493
get_trail(struct ir_remote * remote)494 inline int get_trail(struct ir_remote *remote)
495 {
496 if (remote->ptrail != 0) {
497 if (!expectpulse(remote, remote->ptrail))
498 return (0);
499 }
500 if (rec_buffer.pendingp > 0) {
501 if (!sync_pending_pulse(remote))
502 return (0);
503 }
504 return (1);
505 }
506
get_gap(struct ir_remote * remote,lirc_t gap)507 inline int get_gap(struct ir_remote *remote, lirc_t gap)
508 {
509 lirc_t data;
510
511 LOGPRINTF(2, "sum: %d", rec_buffer.sum);
512 data = get_next_rec_buffer(gap - gap * remote->eps / 100);
513 if (data == 0)
514 return (1);
515 if (!is_space(data)) {
516 LOGPRINTF(2, "space expected");
517 return (0);
518 }
519 unget_rec_buffer(1);
520 if (!expect_at_least(remote, data, gap)) {
521 LOGPRINTF(1, "end of signal not found");
522 return (0);
523 }
524 return (1);
525 }
526
get_repeat(struct ir_remote * remote)527 inline int get_repeat(struct ir_remote *remote)
528 {
529 if (!get_lead(remote))
530 return (0);
531 if (is_biphase(remote)) {
532 if (!expectspace(remote, remote->srepeat))
533 return (0);
534 if (!expectpulse(remote, remote->prepeat))
535 return (0);
536 } else {
537 if (!expectpulse(remote, remote->prepeat))
538 return (0);
539 set_pending_space(remote->srepeat);
540 }
541 if (!get_trail(remote))
542 return (0);
543 if (!get_gap
544 (remote,
545 is_const(remote) ? (min_gap(remote) >
546 rec_buffer.sum ? min_gap(remote) -
547 rec_buffer.sum : 0) : (has_repeat_gap(remote) ? remote->repeat_gap : min_gap(remote))
548 ))
549 return (0);
550 return (1);
551 }
552
get_data(struct ir_remote * remote,int bits,int done)553 ir_code get_data(struct ir_remote * remote, int bits, int done)
554 {
555 ir_code code;
556 int i;
557
558 code = 0;
559
560 if (is_rcmm(remote)) {
561 lirc_t deltap, deltas, sum;
562
563 if (bits % 2 || done % 2) {
564 logprintf(LOG_ERR, "invalid bit number.");
565 return ((ir_code) - 1);
566 }
567 if (!sync_pending_space(remote))
568 return 0;
569 for (i = 0; i < bits; i += 2) {
570 code <<= 2;
571 deltap = get_next_pulse(remote->pzero + remote->pone + remote->ptwo + remote->pthree);
572 deltas = get_next_space(remote->szero + remote->sone + remote->stwo + remote->sthree);
573 if (deltap == 0 || deltas == 0) {
574 logprintf(LOG_ERR, "failed on bit %d", done + i + 1);
575 return ((ir_code) - 1);
576 }
577 sum = deltap + deltas;
578 LOGPRINTF(3, "rcmm: sum %ld", (__u32) sum);
579 if (expect(remote, sum, remote->pzero + remote->szero)) {
580 code |= 0;
581 LOGPRINTF(2, "00");
582 } else if (expect(remote, sum, remote->pone + remote->sone)) {
583 code |= 1;
584 LOGPRINTF(2, "01");
585 } else if (expect(remote, sum, remote->ptwo + remote->stwo)) {
586 code |= 2;
587 LOGPRINTF(2, "10");
588 } else if (expect(remote, sum, remote->pthree + remote->sthree)) {
589 code |= 3;
590 LOGPRINTF(2, "11");
591 } else {
592 LOGPRINTF(2, "no match for %d+%d=%d", deltap, deltas, sum);
593 return ((ir_code) - 1);
594 }
595 }
596 return (code);
597 } else if (is_grundig(remote)) {
598 lirc_t deltap, deltas, sum;
599 int state, laststate;
600
601 if (bits % 2 || done % 2) {
602 logprintf(LOG_ERR, "invalid bit number.");
603 return ((ir_code) - 1);
604 }
605 if (!sync_pending_pulse(remote))
606 return ((ir_code) - 1);
607 for (laststate = state = -1, i = 0; i < bits;) {
608 deltas = get_next_space(remote->szero + remote->sone + remote->stwo + remote->sthree);
609 deltap = get_next_pulse(remote->pzero + remote->pone + remote->ptwo + remote->pthree);
610 if (deltas == 0 || deltap == 0) {
611 logprintf(LOG_ERR, "failed on bit %d", done + i + 1);
612 return ((ir_code) - 1);
613 }
614 sum = deltas + deltap;
615 LOGPRINTF(3, "grundig: sum %ld", (__u32) sum);
616 if (expect(remote, sum, remote->szero + remote->pzero)) {
617 state = 0;
618 LOGPRINTF(2, "2T");
619 } else if (expect(remote, sum, remote->sone + remote->pone)) {
620 state = 1;
621 LOGPRINTF(2, "3T");
622 } else if (expect(remote, sum, remote->stwo + remote->ptwo)) {
623 state = 2;
624 LOGPRINTF(2, "4T");
625 } else if (expect(remote, sum, remote->sthree + remote->pthree)) {
626 state = 3;
627 LOGPRINTF(2, "6T");
628 } else {
629 LOGPRINTF(2, "no match for %d+%d=%d", deltas, deltap, sum);
630 return ((ir_code) - 1);
631 }
632 if (state == 3) { /* 6T */
633 i += 2;
634 code <<= 2;
635 state = -1;
636 code |= 0;
637 } else if (laststate == 2 && state == 0) { /* 4T2T */
638 i += 2;
639 code <<= 2;
640 state = -1;
641 code |= 1;
642 } else if (laststate == 1 && state == 1) { /* 3T3T */
643 i += 2;
644 code <<= 2;
645 state = -1;
646 code |= 2;
647 } else if (laststate == 0 && state == 2) { /* 2T4T */
648 i += 2;
649 code <<= 2;
650 state = -1;
651 code |= 3;
652 } else if (laststate == -1) {
653 /* 1st bit */
654 } else {
655 logprintf(LOG_ERR, "invalid state %d:%d", laststate, state);
656 return ((ir_code) - 1);
657 }
658 laststate = state;
659 }
660 return (code);
661 } else if (is_serial(remote)) {
662 int received;
663 int space, start_bit, stop_bit, parity_bit;
664 int parity;
665 lirc_t delta, origdelta, pending, expecting, gap_delta;
666 lirc_t base, stop;
667 lirc_t max_space, max_pulse;
668
669 base = 1000000 / remote->baud;
670
671 /* start bit */
672 set_pending_pulse(base);
673
674 received = 0;
675 space = (rec_buffer.pendingp == 0); /* expecting space ? */
676 start_bit = 0;
677 stop_bit = 0;
678 parity_bit = 0;
679 delta = origdelta = 0;
680 stop = base * remote->stop_bits / 2;
681 parity = 0;
682 gap_delta = 0;
683
684 max_space = remote->sone * remote->bits_in_byte + stop;
685 max_pulse = remote->pzero * (1 + remote->bits_in_byte);
686 if (remote->parity != IR_PARITY_NONE) {
687 parity_bit = 1;
688 max_space += remote->sone;
689 max_pulse += remote->pzero;
690 bits += bits / remote->bits_in_byte;
691 }
692
693 while (received < bits || stop_bit) {
694 if (delta == 0) {
695 delta = space ? get_next_space(max_space) : get_next_pulse(max_pulse);
696 if (delta == 0 && space && received + remote->bits_in_byte + parity_bit >= bits) {
697 /* open end */
698 delta = max_space;
699 }
700 origdelta = delta;
701 }
702 if (delta == 0) {
703 LOGPRINTF(1, "failed before bit %d", received + 1);
704 return ((ir_code) - 1);
705 }
706 pending = (space ? rec_buffer.pendings : rec_buffer.pendingp);
707 if (expect(remote, delta, pending)) {
708 delta = 0;
709 } else if (delta > pending) {
710 delta -= pending;
711 } else {
712 LOGPRINTF(1, "failed before bit %d", received + 1);
713 return ((ir_code) - 1);
714 }
715 if (pending > 0) {
716 if (stop_bit) {
717 LOGPRINTF(5, "delta: %lu", delta);
718 gap_delta = delta;
719 delta = 0;
720 set_pending_pulse(base);
721 set_pending_space(0);
722 stop_bit = 0;
723 space = 0;
724 LOGPRINTF(3, "stop bit found");
725 } else {
726 LOGPRINTF(3, "pending bit found");
727 set_pending_pulse(0);
728 set_pending_space(0);
729 if (delta == 0) {
730 space = (space ? 0 : 1);
731 }
732 }
733 continue;
734 }
735 expecting = (space ? remote->sone : remote->pzero);
736 if (delta > expecting || expect(remote, delta, expecting)) {
737 delta -= (expecting > delta ? delta : expecting);
738 received++;
739 code <<= 1;
740 code |= space;
741 parity ^= space;
742 LOGPRINTF(2, "adding %d", space);
743 if (received % (remote->bits_in_byte + parity_bit) == 0) {
744 ir_code temp;
745
746 if ((remote->parity == IR_PARITY_EVEN && parity)
747 || (remote->parity == IR_PARITY_ODD && !parity)) {
748 LOGPRINTF(1, "parity error after %d bits", received + 1);
749 return ((ir_code) - 1);
750 }
751 parity = 0;
752
753 /* parity bit is filtered out */
754 temp = code >> (remote->bits_in_byte + parity_bit);
755 code =
756 temp << remote->bits_in_byte | reverse(code >> parity_bit,
757 remote->bits_in_byte);
758
759 if (space && delta == 0) {
760 LOGPRINTF(1, "failed at stop bit after %d bits", received + 1);
761 return ((ir_code) - 1);
762 }
763 LOGPRINTF(3, "awaiting stop bit");
764 set_pending_space(stop);
765 stop_bit = 1;
766 }
767 } else {
768 if (delta == origdelta) {
769 LOGPRINTF(1, "framing error after %d bits", received + 1);
770 return ((ir_code) - 1);
771 }
772 delta = 0;
773 }
774 if (delta == 0) {
775 space = (space ? 0 : 1);
776 }
777 }
778 if (gap_delta)
779 unget_rec_buffer_delta(gap_delta);
780 set_pending_pulse(0);
781 set_pending_space(0);
782 return code;
783 } else if (is_bo(remote)) {
784 int lastbit = 1;
785 lirc_t deltap, deltas;
786 lirc_t pzero, szero;
787 lirc_t pone, sone;
788
789 for (i = 0; i < bits; i++) {
790 code <<= 1;
791 deltap = get_next_pulse(remote->pzero + remote->pone + remote->ptwo + remote->pthree);
792 deltas = get_next_space(remote->szero + remote->sone + remote->stwo + remote->sthree);
793 if (deltap == 0 || deltas == 0) {
794 logprintf(LOG_ERR, "failed on bit %d", done + i + 1);
795 return ((ir_code) - 1);
796 }
797 if (lastbit == 1) {
798 pzero = remote->pone;
799 szero = remote->sone;
800 pone = remote->ptwo;
801 sone = remote->stwo;
802 } else {
803 pzero = remote->ptwo;
804 szero = remote->stwo;
805 pone = remote->pthree;
806 sone = remote->sthree;
807 }
808 LOGPRINTF(5, "%lu %lu %lu %lu", pzero, szero, pone, sone);
809 if (expect(remote, deltap, pzero)) {
810 if (expect(remote, deltas, szero)) {
811 code |= 0;
812 lastbit = 0;
813 LOGPRINTF(2, "0");
814 continue;
815 }
816 }
817
818 if (expect(remote, deltap, pone)) {
819 if (expect(remote, deltas, sone)) {
820 code |= 1;
821 lastbit = 1;
822 LOGPRINTF(2, "1");
823 continue;
824 }
825 }
826 logprintf(LOG_ERR, "failed on bit %d", done + i + 1);
827 return ((ir_code) - 1);
828 }
829 return code;
830 } else if (is_xmp(remote)) {
831 lirc_t deltap, deltas, sum;
832 ir_code n;
833
834 if (bits % 4 || done % 4) {
835 logprintf(LOG_ERR, "invalid bit number.");
836 return ((ir_code) - 1);
837 }
838 if (!sync_pending_space(remote))
839 return 0;
840 for (i = 0; i < bits; i += 4) {
841 code <<= 4;
842 deltap = get_next_pulse(remote->pzero);
843 deltas = get_next_space(remote->szero + 16 * remote->sone);
844 if (deltap == 0 || deltas == 0) {
845 logprintf(LOG_ERR, "failed on bit %d", done + i + 1);
846 return ((ir_code) - 1);
847 }
848 sum = deltap + deltas;
849
850 sum -= remote->pzero + remote->szero;
851 n = (sum + remote->sone / 2) / remote->sone;
852 if (n >= 16) {
853 logprintf(LOG_ERR, "failed on bit %d", done + i + 1);
854 return ((ir_code) - 1);
855 }
856 LOGPRINTF(1, "%d: %lx", i, n);
857 code |= n;
858
859 }
860 return code;
861 }
862
863 for (i = 0; i < bits; i++) {
864 code = code << 1;
865 if (is_goldstar(remote)) {
866 if ((done + i) % 2) {
867 LOGPRINTF(2, "$1");
868 remote->pone = remote->ptwo;
869 remote->sone = remote->stwo;
870 } else {
871 LOGPRINTF(2, "$2");
872 remote->pone = remote->pthree;
873 remote->sone = remote->sthree;
874 }
875 }
876
877 if (expectone(remote, done + i)) {
878 LOGPRINTF(2, "1");
879 code |= 1;
880 } else if (expectzero(remote, done + i)) {
881 LOGPRINTF(2, "0");
882 code |= 0;
883 } else {
884 LOGPRINTF(1, "failed on bit %d", done + i + 1);
885 return ((ir_code) - 1);
886 }
887 }
888 return (code);
889 }
890
get_pre(struct ir_remote * remote)891 ir_code get_pre(struct ir_remote * remote)
892 {
893 ir_code pre;
894
895 pre = get_data(remote, remote->pre_data_bits, 0);
896
897 if (pre == (ir_code) - 1) {
898 LOGPRINTF(1, "failed on pre_data");
899 return ((ir_code) - 1);
900 }
901 if (remote->pre_p > 0 && remote->pre_s > 0) {
902 if (!expectpulse(remote, remote->pre_p))
903 return ((ir_code) - 1);
904 set_pending_space(remote->pre_s);
905 }
906 return (pre);
907 }
908
get_post(struct ir_remote * remote)909 ir_code get_post(struct ir_remote * remote)
910 {
911 ir_code post;
912
913 if (remote->post_p > 0 && remote->post_s > 0) {
914 if (!expectpulse(remote, remote->post_p))
915 return ((ir_code) - 1);
916 set_pending_space(remote->post_s);
917 }
918
919 post = get_data(remote, remote->post_data_bits, remote->pre_data_bits + remote->bits);
920
921 if (post == (ir_code) - 1) {
922 LOGPRINTF(1, "failed on post_data");
923 return ((ir_code) - 1);
924 }
925 return (post);
926 }
927
receive_decode(struct ir_remote * remote,ir_code * prep,ir_code * codep,ir_code * postp,int * repeat_flagp,lirc_t * min_remaining_gapp,lirc_t * max_remaining_gapp)928 int receive_decode(struct ir_remote *remote, ir_code * prep, ir_code * codep, ir_code * postp, int *repeat_flagp,
929 lirc_t * min_remaining_gapp, lirc_t * max_remaining_gapp)
930 {
931 ir_code pre, code, post;
932 lirc_t sync;
933 int header;
934 struct timeval current;
935
936 sync = 0; /* make compiler happy */
937 code = pre = post = 0;
938 header = 0;
939
940 if (hw.rec_mode == LIRC_MODE_MODE2 || hw.rec_mode == LIRC_MODE_PULSE || hw.rec_mode == LIRC_MODE_RAW) {
941 rewind_rec_buffer();
942 rec_buffer.is_biphase = is_biphase(remote) ? 1 : 0;
943
944 /* we should get a long space first */
945 if (!(sync = sync_rec_buffer(remote))) {
946 LOGPRINTF(1, "failed on sync");
947 return (0);
948 }
949
950 LOGPRINTF(1, "sync");
951
952 if (has_repeat(remote) && last_remote == remote) {
953 if (remote->flags & REPEAT_HEADER && has_header(remote)) {
954 if (!get_header(remote)) {
955 LOGPRINTF(1, "failed on repeat header");
956 return (0);
957 }
958 LOGPRINTF(1, "repeat header");
959 }
960 if (get_repeat(remote)) {
961 if (remote->last_code == NULL) {
962 logprintf(LOG_NOTICE, "repeat code without last_code received");
963 return (0);
964 }
965
966 *prep = remote->pre_data;
967 *codep = remote->last_code->code;
968 *postp = remote->post_data;
969 *repeat_flagp = 1;
970
971 *min_remaining_gapp =
972 is_const(remote) ? (min_gap(remote) >
973 rec_buffer.sum ? min_gap(remote) -
974 rec_buffer.sum : 0) : (has_repeat_gap(remote) ? remote->
975 repeat_gap : min_gap(remote));
976 *max_remaining_gapp =
977 is_const(remote) ? (max_gap(remote) >
978 rec_buffer.sum ? max_gap(remote) -
979 rec_buffer.sum : 0) : (has_repeat_gap(remote) ? remote->
980 repeat_gap : max_gap(remote));
981 return (1);
982 } else {
983 LOGPRINTF(1, "no repeat");
984 rewind_rec_buffer();
985 sync_rec_buffer(remote);
986 }
987
988 }
989
990 if (has_header(remote)) {
991 header = 1;
992 if (!get_header(remote)) {
993 header = 0;
994 if (!(remote->flags & NO_HEAD_REP && expect_at_most(remote, sync, max_gap(remote)))) {
995 LOGPRINTF(1, "failed on header");
996 return (0);
997 }
998 }
999 LOGPRINTF(1, "header");
1000 }
1001 }
1002
1003 if (is_raw(remote)) {
1004 struct ir_ncode *codes, *found;
1005 int i;
1006
1007 if (hw.rec_mode == LIRC_MODE_LIRCCODE)
1008 return (0);
1009
1010 codes = remote->codes;
1011 found = NULL;
1012 while (codes->name != NULL && found == NULL) {
1013 found = codes;
1014 for (i = 0; i < codes->length;) {
1015 if (!expectpulse(remote, codes->signals[i++])) {
1016 found = NULL;
1017 rewind_rec_buffer();
1018 sync_rec_buffer(remote);
1019 break;
1020 }
1021 if (i < codes->length && !expectspace(remote, codes->signals[i++])) {
1022 found = NULL;
1023 rewind_rec_buffer();
1024 sync_rec_buffer(remote);
1025 break;
1026 }
1027 }
1028 codes++;
1029 if (found != NULL) {
1030 if (!get_gap
1031 (remote, is_const(remote) ? min_gap(remote) - rec_buffer.sum : min_gap(remote)))
1032 found = NULL;
1033 }
1034 }
1035 if (found == NULL)
1036 return (0);
1037 code = found->code;
1038 } else {
1039 if (hw.rec_mode == LIRC_MODE_LIRCCODE) {
1040 lirc_t sum;
1041 ir_code decoded = rec_buffer.decoded;
1042
1043 LOGPRINTF(1, "decoded: %llx", decoded);
1044 if (hw.rec_mode == LIRC_MODE_LIRCCODE && hw.code_length != bit_count(remote)) {
1045 return (0);
1046 }
1047
1048 post = decoded & gen_mask(remote->post_data_bits);
1049 decoded >>= remote->post_data_bits;
1050 code = decoded & gen_mask(remote->bits);
1051 pre = decoded >> remote->bits;
1052
1053 gettimeofday(¤t, NULL);
1054 sum = remote->phead + remote->shead +
1055 lirc_t_max(remote->pone + remote->sone,
1056 remote->pzero + remote->szero) * bit_count(remote) + remote->plead +
1057 remote->ptrail + remote->pfoot + remote->sfoot + remote->pre_p + remote->pre_s +
1058 remote->post_p + remote->post_s;
1059
1060 rec_buffer.sum = sum >= remote->gap ? remote->gap - 1 : sum;
1061 sync = time_elapsed(&remote->last_send, ¤t) - rec_buffer.sum;
1062 } else {
1063 if (!get_lead(remote)) {
1064 LOGPRINTF(1, "failed on leading pulse");
1065 return (0);
1066 }
1067
1068 if (has_pre(remote)) {
1069 pre = get_pre(remote);
1070 if (pre == (ir_code) - 1) {
1071 LOGPRINTF(1, "failed on pre");
1072 return (0);
1073 }
1074 LOGPRINTF(1, "pre: %llx", pre);
1075 }
1076
1077 code = get_data(remote, remote->bits, remote->pre_data_bits);
1078 if (code == (ir_code) - 1) {
1079 LOGPRINTF(1, "failed on code");
1080 return (0);
1081 }
1082 LOGPRINTF(1, "code: %llx", code);
1083
1084 if (has_post(remote)) {
1085 post = get_post(remote);
1086 if (post == (ir_code) - 1) {
1087 LOGPRINTF(1, "failed on post");
1088 return (0);
1089 }
1090 LOGPRINTF(1, "post: %llx", post);
1091 }
1092 if (!get_trail(remote)) {
1093 LOGPRINTF(1, "failed on trailing pulse");
1094 return (0);
1095 }
1096 if (has_foot(remote)) {
1097 if (!get_foot(remote)) {
1098 LOGPRINTF(1, "failed on foot");
1099 return (0);
1100 }
1101 }
1102 if (header == 1 && is_const(remote) && (remote->flags & NO_HEAD_REP)) {
1103 rec_buffer.sum -= remote->phead + remote->shead;
1104 }
1105 if (is_rcmm(remote)) {
1106 if (!get_gap(remote, 1000))
1107 return (0);
1108 } else if (is_const(remote)) {
1109 if (!get_gap
1110 (remote, min_gap(remote) > rec_buffer.sum ? min_gap(remote) - rec_buffer.sum : 0))
1111 return (0);
1112 } else {
1113 if (!get_gap(remote, min_gap(remote)))
1114 return (0);
1115 }
1116 } /* end of mode specific code */
1117 }
1118 *prep = pre;
1119 *codep = code;
1120 *postp = post;
1121 if ((!has_repeat(remote) || remote->reps < remote->min_code_repeat)
1122 && expect_at_most(remote, sync, remote->max_remaining_gap))
1123 *repeat_flagp = 1;
1124 else
1125 *repeat_flagp = 0;
1126 if (hw.rec_mode == LIRC_MODE_LIRCCODE) {
1127 /* Most TV cards don't pass each signal to the
1128 driver. This heuristic should fix repeat in such
1129 cases. */
1130 if (time_elapsed(&remote->last_send, ¤t) < 325000) {
1131 *repeat_flagp = 1;
1132 }
1133 }
1134 if (is_const(remote)) {
1135 *min_remaining_gapp = min_gap(remote) > rec_buffer.sum ? min_gap(remote) - rec_buffer.sum : 0;
1136 *max_remaining_gapp = max_gap(remote) > rec_buffer.sum ? max_gap(remote) - rec_buffer.sum : 0;
1137 } else {
1138 *min_remaining_gapp = min_gap(remote);
1139 *max_remaining_gapp = max_gap(remote);
1140 }
1141 return (1);
1142 }
1143