LIRC libraries LinuxInfraredRemoteControl

- Main Page
- Related Pages
- Modules
- Data Structures
- Files
- Examples

- File List
- Globals

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