LIRC libraries: /home/mk/tmp/lirc/lib/receive.c (e229353 2016-10-20 17:36:59 +0200) Source File

| LIRC libraries LinuxInfraredRemoteControl

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