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"
39 lirc_t data[RBUF_SIZE];
48 struct timeval last_signal_time;
57 static struct rbuf rec_buffer;
58 static int update_mode = 0;
67 static lirc_t readdata(lirc_t timeout)
72 rec_buffer.at_eof = data &
LIRC_EOF ? 1 : 0;
73 if (rec_buffer.at_eof)
74 logprintf(LIRC_DEBUG,
"receive: Got EOF");
79 static lirc_t lirc_t_max(lirc_t a, lirc_t b)
84 static void set_pending_pulse(lirc_t deltap)
86 LOGPRINTF(5,
"pending pulse: %lu", deltap);
87 rec_buffer.pendingp = deltap;
90 static void set_pending_space(lirc_t deltas)
92 LOGPRINTF(5,
"pending space: %lu", deltas);
93 rec_buffer.pendings = deltas;
97 static void log_input(lirc_t data)
99 fprintf(rec_buffer.input_log,
"%s %u\n",
100 data & PULSE_BIT ?
"pulse" :
"space", data & PULSE_MASK);
101 fflush(rec_buffer.input_log);
105 static lirc_t get_next_rec_buffer_internal(lirc_t maxusec)
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++];
113 if (rec_buffer.wptr < RBUF_SIZE) {
115 unsigned long elapsed = 0;
117 if (timerisset(&rec_buffer.last_signal_time)) {
118 struct timeval current;
120 gettimeofday(¤t, NULL);
121 elapsed = time_elapsed(&rec_buffer.last_signal_time, ¤t);
123 if (elapsed < maxusec)
124 data = readdata(maxusec - elapsed);
130 logprintf(LIRC_DEBUG,
"Receive: returning EOF");
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));
140 rec_buffer.data[rec_buffer.wptr] = data;
141 if (rec_buffer.input_log != NULL)
143 if (rec_buffer.data[rec_buffer.wptr] == 0)
145 rec_buffer.sum += rec_buffer.data[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]
152 return rec_buffer.data[rec_buffer.rptr - 1];
154 rec_buffer.too_long = 1;
161 struct pollfd pfd = {
167 ret = poll(&pfd, 1, maxusec * 1000);
168 }
while (ret == -1 && errno == EINTR);
175 if (pfd.revents & POLLIN)
184 if (rec_buffer.input_log != NULL)
185 fclose(rec_buffer.input_log);
186 rec_buffer.input_log = f;
190 static lirc_t get_next_rec_buffer(lirc_t maxusec)
192 return get_next_rec_buffer_internal(receive_timeout(maxusec));
197 memset(&rec_buffer, 0,
sizeof(rec_buffer));
203 rec_buffer.too_long = 0;
204 set_pending_pulse(0);
205 set_pending_space(0);
207 rec_buffer.at_eof = 0;
219 timerclear(&rec_buffer.last_signal_time);
221 unsigned char buffer[
sizeof(
ir_code)];
229 logprintf(LIRC_ERROR,
"reading in mode LIRC_MODE_LIRCCODE failed");
232 for (i = 0, rec_buffer.decoded = 0; i < count; i++)
233 rec_buffer.decoded = (rec_buffer.decoded << CHAR_BIT) + ((
ir_code)buffer[i]);
237 move = rec_buffer.wptr - rec_buffer.rptr;
238 if (move > 0 && rec_buffer.rptr > 0) {
239 memmove(&rec_buffer.data[0], &rec_buffer.data[rec_buffer.rptr],
240 sizeof(rec_buffer.data[0]) * move);
241 rec_buffer.wptr -= rec_buffer.rptr;
246 LOGPRINTF(3,
"c%lu", (__u32)data & (PULSE_MASK));
248 rec_buffer.data[rec_buffer.wptr] = data;
254 rec_buffer.is_biphase = 0;
259 static void unget_rec_buffer(
int count)
262 if (count == 1 || count == 2) {
263 rec_buffer.rptr -= count;
264 rec_buffer.sum -= rec_buffer.data[rec_buffer.rptr] & (PULSE_MASK);
266 rec_buffer.sum -= rec_buffer.data[rec_buffer.rptr + 1]
271 static void unget_rec_buffer_delta(lirc_t delta)
274 rec_buffer.sum -= delta & (PULSE_MASK);
275 rec_buffer.data[rec_buffer.rptr] = delta;
278 static lirc_t get_next_pulse(lirc_t maxusec)
282 data = get_next_rec_buffer(maxusec);
285 if (!is_pulse(data)) {
289 return data & (PULSE_MASK);
292 static lirc_t get_next_space(lirc_t maxusec)
296 data = get_next_rec_buffer(maxusec);
299 if (!is_space(data)) {
306 static int sync_pending_pulse(
struct ir_remote* remote)
308 if (rec_buffer.pendingp > 0) {
311 deltap = get_next_pulse(rec_buffer.pendingp);
314 if (!expect(remote, deltap, rec_buffer.pendingp))
316 set_pending_pulse(0);
321 static int sync_pending_space(
struct ir_remote* remote)
323 if (rec_buffer.pendings > 0) {
326 deltas = get_next_space(rec_buffer.pendings);
329 if (!expect(remote, deltas, rec_buffer.pendings))
331 set_pending_space(0);
336 static int expectpulse(
struct ir_remote* remote,
int exdelta)
341 LOGPRINTF(5,
"expecting pulse: %lu", exdelta);
342 if (!sync_pending_space(remote))
345 deltap = get_next_pulse(rec_buffer.pendingp + exdelta);
348 if (rec_buffer.pendingp > 0) {
349 if (rec_buffer.pendingp > deltap)
351 retval = expect(remote, deltap - rec_buffer.pendingp, exdelta);
354 set_pending_pulse(0);
356 retval = expect(remote, deltap, exdelta);
361 static int expectspace(
struct ir_remote* remote,
int exdelta)
366 LOGPRINTF(5,
"expecting space: %lu", exdelta);
367 if (!sync_pending_pulse(remote))
370 deltas = get_next_space(rec_buffer.pendings + exdelta);
373 if (rec_buffer.pendings > 0) {
374 if (rec_buffer.pendings > deltas)
376 retval = expect(remote, deltas - rec_buffer.pendings, exdelta);
379 set_pending_space(0);
381 retval = expect(remote, deltas, exdelta);
386 static int expectone(
struct ir_remote* remote,
int bit)
388 if (is_biphase(remote)) {
389 int all_bits = bit_count(remote);
392 mask = ((
ir_code)1) << (all_bits - 1 - bit);
394 if (remote->
sone > 0 && !expectspace(remote, 2 * remote->
sone)) {
398 set_pending_pulse(2 * remote->pone);
400 if (remote->
sone > 0 && !expectspace(remote, remote->
sone)) {
404 set_pending_pulse(remote->pone);
406 }
else if (is_space_first(remote)) {
407 if (remote->
sone > 0 && !expectspace(remote, remote->
sone)) {
411 if (remote->pone > 0 && !expectpulse(remote, remote->pone)) {
416 if (remote->pone > 0 && !expectpulse(remote, remote->pone)) {
421 if (remote->
sone > 0 && !expectspace(remote, remote->
sone)) {
426 set_pending_space(remote->
sone);
432 static int expectzero(
struct ir_remote* remote,
int bit)
434 if (is_biphase(remote)) {
435 int all_bits = bit_count(remote);
438 mask = ((
ir_code)1) << (all_bits - 1 - bit);
440 if (!expectpulse(remote, 2 * remote->pzero)) {
444 set_pending_space(2 * remote->
szero);
446 if (!expectpulse(remote, remote->pzero)) {
450 set_pending_space(remote->
szero);
452 }
else if (is_space_first(remote)) {
453 if (remote->
szero > 0 && !expectspace(remote, remote->
szero)) {
457 if (remote->pzero > 0 && !expectpulse(remote, remote->pzero)) {
462 if (!expectpulse(remote, remote->pzero)) {
467 if (!expectspace(remote, remote->
szero)) {
472 set_pending_space(remote->
szero);
478 static lirc_t sync_rec_buffer(
struct ir_remote* remote)
481 lirc_t deltas, deltap;
484 deltas = get_next_space(1000000);
490 deltap = get_next_pulse(1000000);
493 deltas = get_next_space(1000000);
497 if (count > REC_SYNC)
501 if (has_toggle_mask(remote)) {
503 remote->toggle_mask_state = 0;
512 static int get_header(
struct ir_remote* remote)
514 if (is_rcmm(remote)) {
515 lirc_t deltap, deltas, sum;
517 deltap = get_next_pulse(remote->phead);
522 deltas = get_next_space(remote->
shead);
527 sum = deltap + deltas;
528 if (expect(remote, sum, remote->phead + remote->
shead))
532 }
else if (is_bo(remote)) {
533 if (expectpulse(remote, remote->pone) && expectspace(remote, remote->
sone)
534 && expectpulse(remote, remote->pone) && expectspace(remote, remote->
sone)
535 && expectpulse(remote, remote->phead) && expectspace(remote, remote->
shead))
539 if (remote->
shead == 0) {
540 if (!sync_pending_space(remote))
542 set_pending_pulse(remote->phead);
545 if (!expectpulse(remote, remote->phead)) {
554 deltas = get_next_space(remote->
shead);
556 if (expect(remote, remote->
shead, deltas))
563 set_pending_space(remote->
shead);
567 static int get_foot(
struct ir_remote* remote)
569 if (!expectspace(remote, remote->
sfoot))
571 if (!expectpulse(remote, remote->pfoot))
576 static int get_lead(
struct ir_remote* remote)
578 if (remote->
plead == 0)
580 if (!sync_pending_space(remote))
582 set_pending_pulse(remote->
plead);
586 static int get_trail(
struct ir_remote* remote)
589 if (!expectpulse(remote, remote->
ptrail))
591 if (rec_buffer.pendingp > 0)
592 if (!sync_pending_pulse(remote))
597 static int get_gap(
struct ir_remote* remote, lirc_t gap)
602 data = get_next_rec_buffer(gap - gap * remote->
eps / 100);
605 if (!is_space(data)) {
610 if (!expect_at_least(remote, data, gap)) {
617 static int get_repeat(
struct ir_remote* remote)
619 if (!get_lead(remote))
621 if (is_biphase(remote)) {
622 if (!expectspace(remote, remote->
srepeat))
624 if (!expectpulse(remote, remote->prepeat))
627 if (!expectpulse(remote, remote->prepeat))
629 set_pending_space(remote->
srepeat);
631 if (!get_trail(remote))
635 is_const(remote) ? (min_gap(remote) >
637 min_gap(remote) - rec_buffer.sum : 0) :
638 (has_repeat_gap(remote) ? remote->
repeat_gap : min_gap(remote))
651 if (is_rcmm(remote)) {
652 lirc_t deltap, deltas, sum;
654 if (bits % 2 || done % 2) {
655 logprintf(LIRC_ERROR,
"invalid bit number.");
658 if (!sync_pending_space(remote))
660 for (i = 0; i < bits; i += 2) {
662 deltap = get_next_pulse(remote->pzero + remote->pone + remote->ptwo + remote->pthree);
664 if (deltap == 0 || deltas == 0) {
665 logprintf(LIRC_ERROR,
"failed on bit %d", done + i + 1);
668 sum = deltap + deltas;
669 LOGPRINTF(3,
"rcmm: sum %ld", (__u32)sum);
670 if (expect(remote, sum, remote->pzero + remote->
szero)) {
673 }
else if (expect(remote, sum, remote->pone + remote->
sone)) {
676 }
else if (expect(remote, sum, remote->ptwo + remote->
stwo)) {
679 }
else if (expect(remote, sum, remote->pthree + remote->
sthree)) {
683 LOGPRINTF(2,
"no match for %d+%d=%d", deltap, deltas, sum);
688 }
else if (is_grundig(remote)) {
689 lirc_t deltap, deltas, sum;
690 int state, laststate;
692 if (bits % 2 || done % 2) {
693 logprintf(LIRC_ERROR,
"invalid bit number.");
696 if (!sync_pending_pulse(remote))
698 for (laststate = state = -1, i = 0; i < bits; ) {
700 deltap = get_next_pulse(remote->pzero + remote->pone + remote->ptwo + remote->pthree);
701 if (deltas == 0 || deltap == 0) {
702 logprintf(LIRC_ERROR,
"failed on bit %d", done + i + 1);
705 sum = deltas + deltap;
706 LOGPRINTF(3,
"grundig: sum %ld", (__u32)sum);
707 if (expect(remote, sum, remote->
szero + remote->pzero)) {
710 }
else if (expect(remote, sum, remote->
sone + remote->pone)) {
713 }
else if (expect(remote, sum, remote->
stwo + remote->ptwo)) {
716 }
else if (expect(remote, sum, remote->
sthree + remote->pthree)) {
720 LOGPRINTF(2,
"no match for %d+%d=%d", deltas, deltap, sum);
728 }
else if (laststate == 2 && state == 0) {
733 }
else if (laststate == 1 && state == 1) {
738 }
else if (laststate == 0 && state == 2) {
743 }
else if (laststate == -1) {
746 logprintf(LIRC_ERROR,
"invalid state %d:%d", laststate, state);
752 }
else if (is_serial(remote)) {
754 int space, stop_bit, parity_bit;
756 lirc_t delta, origdelta, pending, expecting, gap_delta;
758 lirc_t max_space, max_pulse;
760 base = 1000000 / remote->
baud;
763 set_pending_pulse(base);
766 space = (rec_buffer.pendingp == 0);
769 delta = origdelta = 0;
776 if (remote->
parity != IR_PARITY_NONE) {
778 max_space += remote->
sone;
779 max_pulse += remote->pzero;
783 while (received < bits || stop_bit) {
785 delta = space ? get_next_space(max_space) : get_next_pulse(max_pulse);
786 if (delta == 0 && space && received + remote->
bits_in_byte + parity_bit >= bits)
792 LOGPRINTF(1,
"failed before bit %d", received + 1);
795 pending = (space ? rec_buffer.pendings : rec_buffer.pendingp);
796 if (expect(remote, delta, pending)) {
798 }
else if (delta > pending) {
801 LOGPRINTF(1,
"failed before bit %d", received + 1);
809 set_pending_pulse(base);
810 set_pending_space(0);
816 set_pending_pulse(0);
817 set_pending_space(0);
819 space = (space ? 0 : 1);
823 expecting = (space ? remote->
sone : remote->pzero);
824 if (delta > expecting || expect(remote, delta, expecting)) {
825 delta -= (expecting > delta ? delta : expecting);
831 if (received % (remote->
bits_in_byte + parity_bit) == 0) {
834 if ((remote->
parity == IR_PARITY_EVEN && parity)
835 || (remote->
parity == IR_PARITY_ODD && !parity)) {
836 LOGPRINTF(1,
"parity error after %d bits", received + 1);
844 temp << remote->
bits_in_byte | reverse(code >> parity_bit,
847 if (space && delta == 0) {
848 LOGPRINTF(1,
"failed at stop bit after %d bits", received + 1);
852 set_pending_space(stop);
856 if (delta == origdelta) {
857 LOGPRINTF(1,
"framing error after %d bits", received + 1);
863 space = (space ? 0 : 1);
866 unget_rec_buffer_delta(gap_delta);
867 set_pending_pulse(0);
868 set_pending_space(0);
870 }
else if (is_bo(remote)) {
872 lirc_t deltap, deltas;
876 for (i = 0; i < bits; i++) {
878 deltap = get_next_pulse(remote->pzero + remote->pone + remote->ptwo + remote->pthree);
880 if (deltap == 0 || deltas == 0) {
881 logprintf(LIRC_ERROR,
"failed on bit %d", done + i + 1);
885 pzero = remote->pone;
886 szero = remote->
sone;
890 pzero = remote->ptwo;
891 szero = remote->
stwo;
892 pone = remote->pthree;
895 LOGPRINTF(5,
"%lu %lu %lu %lu", pzero, szero, pone, sone);
896 if (expect(remote, deltap, pzero)) {
897 if (expect(remote, deltas, szero)) {
905 if (expect(remote, deltap, pone)) {
906 if (expect(remote, deltas, sone)) {
913 logprintf(LIRC_ERROR,
"failed on bit %d", done + i + 1);
917 }
else if (is_xmp(remote)) {
918 lirc_t deltap, deltas, sum;
921 if (bits % 4 || done % 4) {
922 logprintf(LIRC_ERROR,
"invalid bit number.");
925 if (!sync_pending_space(remote))
927 for (i = 0; i < bits; i += 4) {
929 deltap = get_next_pulse(remote->pzero);
930 deltas = get_next_space(remote->
szero + 16 * remote->
sone);
931 if (deltap == 0 || deltas == 0) {
932 logprintf(LIRC_ERROR,
"failed on bit %d", done + i + 1);
935 sum = deltap + deltas;
937 sum -= remote->pzero + remote->
szero;
938 n = (sum + remote->
sone / 2) / remote->
sone;
940 logprintf(LIRC_ERROR,
"failed on bit %d", done + i + 1);
949 for (i = 0; i < bits; i++) {
951 if (is_goldstar(remote)) {
952 if ((done + i) % 2) {
954 remote->pone = remote->ptwo;
958 remote->pone = remote->pthree;
963 if (expectone(remote, done + i)) {
966 }
else if (expectzero(remote, done + i)) {
970 LOGPRINTF(1,
"failed on bit %d", done + i + 1);
987 LOGPRINTF(1,
"Failed on pre_data: cannot get it");
998 remote_pre = remote->
pre_data & ~toggle_mask;
999 match_pre = pre & ~toggle_mask;
1000 if (remote->
pre_data != 0 && remote_pre != match_pre) {
1001 LOGPRINTF(1,
"Failed on pre_data: bad data: %x", pre);
1005 if (remote->pre_p > 0 && remote->
pre_s > 0) {
1006 if (!expectpulse(remote, remote->pre_p))
1008 set_pending_space(remote->
pre_s);
1017 if (remote->post_p > 0 && remote->
post_s > 0) {
1018 if (!expectpulse(remote, remote->post_p))
1020 set_pending_space(remote->
post_s);
1036 struct timeval current;
1043 if (rec_buffer.at_eof && rec_buffer.wptr - rec_buffer.rptr <= 1) {
1044 logprintf(LIRC_DEBUG,
"Decode: found EOF");
1046 rec_buffer.at_eof = 0;
1053 rec_buffer.is_biphase = is_biphase(remote) ? 1 : 0;
1056 sync = sync_rec_buffer(remote);
1063 if (has_repeat(remote) &&
last_remote == remote) {
1065 if (!get_header(remote)) {
1066 LOGPRINTF(1,
"failed on repeat header");
1071 if (get_repeat(remote)) {
1073 logprintf(LIRC_NOTICE,
"repeat code without last_code received");
1083 is_const(remote) ? (min_gap(remote) >
1084 rec_buffer.sum ? min_gap(remote) -
1085 rec_buffer.sum : 0) : (has_repeat_gap(remote) ? remote->
1086 repeat_gap : min_gap(remote));
1088 is_const(remote) ? (max_gap(remote) >
1089 rec_buffer.sum ? max_gap(remote) -
1090 rec_buffer.sum : 0) : (has_repeat_gap(remote) ? remote->
1091 repeat_gap : max_gap(remote));
1096 sync_rec_buffer(remote);
1099 if (has_header(remote)) {
1101 if (!get_header(remote)) {
1103 if (!(remote->
flags &
NO_HEAD_REP && expect_at_most(remote, sync, max_gap(remote)))) {
1112 if (is_raw(remote)) {
1120 codes = remote->codes;
1122 while (codes->
name != NULL && found == NULL) {
1124 for (i = 0; i < codes->
length; ) {
1125 if (!expectpulse(remote, codes->
signals[i++])) {
1128 sync_rec_buffer(remote);
1131 if (i < codes->
length && !expectspace(remote, codes->
signals[i++])) {
1134 sync_rec_buffer(remote);
1139 if (found != NULL) {
1141 (remote, is_const(remote) ?
1142 min_gap(remote) - rec_buffer.sum :
1153 ir_code decoded = rec_buffer.decoded;
1162 ctx->
code = decoded & gen_mask(remote->
bits);
1163 ctx->
pre = decoded >> remote->
bits;
1165 gettimeofday(¤t, NULL);
1166 sum = remote->phead + remote->
shead +
1167 lirc_t_max(remote->pone + remote->
sone,
1168 remote->pzero + remote->
szero) * bit_count(remote) + remote->
plead +
1169 remote->
ptrail + remote->pfoot + remote->
sfoot + remote->pre_p + remote->
pre_s +
1170 remote->post_p + remote->
post_s;
1172 rec_buffer.sum = sum >= remote->
gap ? remote->
gap - 1 : sum;
1173 sync = time_elapsed(&remote->
last_send, ¤t) - rec_buffer.sum;
1175 if (!get_lead(remote)) {
1176 LOGPRINTF(1,
"failed on leading pulse");
1180 if (has_pre(remote)) {
1181 ctx->
pre = get_pre(remote);
1196 if (has_post(remote)) {
1197 ctx->
post = get_post(remote);
1204 if (!get_trail(remote)) {
1205 LOGPRINTF(1,
"failed on trailing pulse");
1208 if (has_foot(remote)) {
1209 if (!get_foot(remote)) {
1215 rec_buffer.sum -= remote->phead + remote->
shead;
1216 if (is_rcmm(remote)) {
1217 if (!get_gap(remote, 1000))
1219 }
else if (is_const(remote)) {
1220 if (!get_gap(remote, min_gap(remote) > rec_buffer.sum ?
1221 min_gap(remote) - rec_buffer.sum :
1225 if (!get_gap(remote, min_gap(remote)))
1239 if (time_elapsed(&remote->
last_send, ¤t) < 325000)
1242 if (is_const(remote)) {
1243 ctx->
min_remaining_gap = min_gap(remote) > rec_buffer.sum ? min_gap(remote) - rec_buffer.sum : 0;
1244 ctx->
max_remaining_gap = max_gap(remote) > rec_buffer.sum ? max_gap(remote) - rec_buffer.sum : 0;
void rec_buffer_init(void)
struct ir_remote * last_remote
void rec_buffer_set_logfile(FILE *f)
int waitfordata(__u32 maxusec)
struct ir_ncode * toggle_code
void rec_buffer_rewind(void)
struct ir_ncode * last_code
int receive_decode(struct ir_remote *remote, struct decode_ctx_t *ctx)
void rec_buffer_reset_wptr(void)
lirc_t(*const readdata)(lirc_t timeout)
#define LOGPRINTF(level, fmt, args...)
void logperror(loglevel_t prio, const char *fmt,...)
void rec_set_update_mode(int mode)
unsigned int min_code_repeat
const struct driver const * curr_driver
int rec_buffer_clear(void)
unsigned int bits_in_byte