| LIRC libraries LinuxInfraredRemoteControl

irrecord.c
1 /***************************************************************************
2 ** irrecord.c **************************************************************
3 ****************************************************************************
4 *
5 * irrecord - library for recording IR-codes for usage with lircd
6 *
7 * Copyright (C) 1998,99 Christoph Bartelmus <lirc@bartelmus.de>
8 *
9 */
10
11 #define _GNU_SOURCE
12
13 #include <unistd.h>
14 #include <poll.h>
15
16 #include "lirc_private.h"
17 #include "irrecord.h"
18
19 /* -------------------------- C file -------------------------------- */
20
21 static const logchannel_t logchannel = LOG_LIB;
22
23 // forwards
24 static lirc_t emulation_readdata(lirc_t timeout);
25
26 // Constants
27 static const struct driver hw_emulation = {
28 .name = "emulation",
29 .device = "/dev/null",
30 .features = LIRC_CAN_REC_MODE2,
31 .send_mode = 0,
32 .rec_mode = LIRC_MODE_MODE2,
33 .code_length = 0,
34 .init_func = NULL,
35 .deinit_func = NULL,
36 .send_func = NULL,
37 .rec_func = NULL,
38 .decode_func = NULL,
39 .drvctl_func = NULL,
40 .readdata = emulation_readdata,
41 .open_func = default_open,
42 .close_func = default_close,
43 .api_version = 2,
44 .driver_version = "0.9.2"
45 };
46
47 static const int IR_CODE_NODE_SIZE = sizeof(struct ir_code_node);
48
49 // Globals
50
51 struct ir_remote remote;
52 unsigned int eps = 30;
53 lirc_t aeps = 100;
54
55 // Static data
56
57 static lirc_t signals[MAX_SIGNALS];
58 static struct ir_remote* emulation_data;
59 static struct ir_ncode* next_code = NULL;
60 static struct ir_ncode* current_code = NULL;
61 static int current_index = 0;
62 static int current_rep = 0;
63
64 static struct lengths* first_space = NULL;
65 static struct lengths* first_pulse = NULL;
66 static struct lengths* first_sum = NULL;
67 static struct lengths* first_gap = NULL;
68 static struct lengths* first_repeat_gap = NULL;
69 static struct lengths* first_signal_length = NULL;
70 static struct lengths* first_headerp = NULL;
71 static struct lengths* first_headers = NULL;
72 static struct lengths* first_1lead = NULL;
73 static struct lengths* first_3lead = NULL;
74 static struct lengths* first_trail = NULL;
75 static struct lengths* first_repeatp = NULL;
76 static struct lengths* first_repeats = NULL;
77
78 static __u32 lengths[MAX_SIGNALS];
79 static __u32 first_length, first_lengths, second_lengths;
80 static unsigned int count, count_spaces, count_signals;
81 static unsigned int count_3repeats, count_5repeats;
82
83
84 // Functions
85
87 void btn_state_set_message(struct button_state* state, const char* fmt, ...)
88 {
89 va_list ap;
90
91 va_start(ap, fmt);
92 vsnprintf(state->message, sizeof(state->message), fmt, ap);
93 va_end(ap);
94 }
95
96
97 static void fprint_copyright(FILE* fout)
98 {
99 fprintf(fout, "\n"
100 "# Please take the time to finish this file as described in\n"
101 "# https://sourceforge.net/p/lirc-remotes/wiki/Checklist/\n"
102 "# and make it available to others by sending it to\n"
103 "# <lirc@bartelmus.de>\n");
104 }
105
106
108 int availabledata(void)
109 {
110 struct pollfd pfd = {
111 .fd = curr_driver->fd, .events = POLLIN, .revents = 0};
112 int ret;
113
114 do {
115 do {
116 ret = curl_poll(&pfd, 1, 0);
117 } while (ret == -1 && errno == EINTR);
118 if (ret == -1) {
119 log_perror_err("availabledata: curl_poll() failed");
120 continue;
121 }
122 } while (ret == -1);
123
124 return pfd.revents & POLLIN ? 1 : 0;
125 }
126
127
129 void flushhw(void)
130 {
131 size_t size = 1;
132 char buffer[PACKET_SIZE];
133
134 switch (curr_driver->rec_mode) {
135 case LIRC_MODE_MODE2:
136 while (availabledata())
137 curr_driver->readdata(0);
138 return;
139 case LIRC_MODE_LIRCCODE:
140 size = curr_driver->code_length / CHAR_BIT;
141 if (curr_driver->code_length % CHAR_BIT)
142 size++;
143 break;
144 }
145 while (read(curr_driver->fd, buffer, size) == size)
146 ;
147 }
148
149
151 int resethw(int started_as_root)
152 {
153 int flags;
154
155 if (started_as_root)
156 if (seteuid(0) == -1)
157 log_error("Cannot reset root uid");
158 if (curr_driver->deinit_func)
159 curr_driver->deinit_func();
160 if (curr_driver->init_func) {
161 if (!curr_driver->init_func()) {
162 drop_sudo_root(seteuid);
163 return 0;
164 }
165 }
166 flags = fcntl(curr_driver->fd, F_GETFL, 0);
167 if (flags == -1 ||
168 fcntl(curr_driver->fd, F_SETFL, flags | O_NONBLOCK) == -1) {
169 if (curr_driver->deinit_func)
170 curr_driver->deinit_func();
171 drop_sudo_root(seteuid);
172 return 0;
173 }
174 drop_sudo_root(seteuid);
175 return 1;
176 }
177
178
179 void gap_state_init(struct gap_state* state)
180 {
181 memset(state, 0, sizeof(struct gap_state));
182 }
183
184
185 void lengths_state_init(struct lengths_state* state)
186 {
187 count = 0;
188 count_spaces = 0;
189 count_3repeats = 0;
190 count_5repeats = 0;
191 count_signals = 0;
192 first_length = 0;
193 first_lengths = 0;
194 second_lengths = 0;
195 memset(state, 0, sizeof(struct lengths_state));
196 state->first_signal = -1;
197 state->retval = 1;
198 }
199
200
201 void toggle_state_init(struct toggle_state* state)
202 {
203 memset(state, 0, sizeof(struct toggle_state));
204 state->retries = 30;
205 state->retval = EXIT_SUCCESS;
206 }
207
208
209 void button_state_init(struct button_state* state, int started_as_root)
210 {
211 memset(state, 0, sizeof(struct button_state));
212 state->started_as_root = started_as_root;
213 state->retval = EXIT_SUCCESS;
214 }
215
216
217 static lirc_t calc_signal(struct lengths* len)
218 {
219 if (len->count == 0)
220 return 0;
221 return (lirc_t)(len->sum / len->count);
222 }
223
224
225 static void set_toggle_bit_mask(struct ir_remote* remote, ir_code xor)
226 {
227 ir_code mask;
228 struct ir_ncode* codes;
229 int bits;
230
231 if (!remote->codes)
232 return;
233
234 bits = bit_count(remote);
235 mask = ((ir_code)1) << (bits - 1);
236 while (mask) {
237 if (mask == xor)
238 break;
239 mask = mask >> 1;
240 }
241 if (mask) {
242 remote->toggle_bit_mask = xor;
243
244 codes = remote->codes;
245 while (codes->name != NULL) {
246 codes->code &= ~xor;
247 codes++;
248 }
249 }
250 /* Sharp, Denon and some others use a toggle_mask */
251 else if (bits == 15 && xor == 0x3ff) {
252 remote->toggle_mask = xor;
253 } else {
254 remote->toggle_bit_mask = xor;
255 }
256 }
257
258
259 void get_pre_data(struct ir_remote* remote)
260 {
261 struct ir_ncode* codes;
262 ir_code mask, last;
263 int count, i;
264 struct ir_code_node* n;
265
266 if (remote->bits == 0)
267 return;
268 mask = (-1);
269 codes = remote->codes;
270 if (codes->name == NULL)
271 return; /* at least 2 codes needed */
272 last = codes->code;
273 codes++;
274 if (codes->name == NULL)
275 return; /* at least 2 codes needed */
276 while (codes->name != NULL) {
277 mask &= ~(last ^ codes->code);
278 last = codes->code;
279 for (n = codes->next; n != NULL; n = n->next) {
280 mask &= ~(last ^ n->code);
281 last = n->code;
282 }
283 codes++;
284 }
285 count = 0;
286 while (mask & 0x8000000000000000LL) {
287 count++;
288 mask = mask << 1;
289 }
290 count -= sizeof(ir_code) * CHAR_BIT - remote->bits;
291
292 /* only "even" numbers should go to pre/post data */
293 if (count % 8 && (remote->bits - count) % 8)
294 count -= count % 8;
295 if (count > 0) {
296 mask = 0;
297 for (i = 0; i < count; i++) {
298 mask = mask << 1;
299 mask |= 1;
300 }
301 remote->bits -= count;
302 mask = mask << (remote->bits);
303 remote->pre_data_bits = count;
304 remote->pre_data = (last & mask) >> (remote->bits);
305
306 codes = remote->codes;
307 while (codes->name != NULL) {
308 codes->code &= ~mask;
309 for (n = codes->next; n != NULL; n = n->next)
310 n->code &= ~mask;
311 codes++;
312 }
313 }
314 }
315
316
317 void get_post_data(struct ir_remote* remote)
318 {
319 struct ir_ncode* codes;
320 ir_code mask, last;
321 int count, i;
322 struct ir_code_node* n;
323
324 if (remote->bits == 0)
325 return;
326
327 mask = (-1);
328 codes = remote->codes;
329 if (codes->name == NULL)
330 return; /* at least 2 codes needed */
331 last = codes->code;
332 codes++;
333 if (codes->name == NULL)
334 return; /* at least 2 codes needed */
335 while (codes->name != NULL) {
336 mask &= ~(last ^ codes->code);
337 last = codes->code;
338 for (n = codes->next; n != NULL; n = n->next) {
339 mask &= ~(last ^ n->code);
340 last = n->code;
341 }
342 codes++;
343 }
344 count = 0;
345 while (mask & 0x1) {
346 count++;
347 mask = mask >> 1;
348 }
349 /* only "even" numbers should go to pre/post data */
350 if (count % 8 && (remote->bits - count) % 8)
351 count -= count % 8;
352 if (count > 0) {
353 mask = 0;
354 for (i = 0; i < count; i++) {
355 mask = mask << 1;
356 mask |= 1;
357 }
358 remote->bits -= count;
359 remote->post_data_bits = count;
360 remote->post_data = last & mask;
361
362 codes = remote->codes;
363 while (codes->name != NULL) {
364 codes->code = codes->code >> count;
365 for (n = codes->next; n != NULL; n = n->next)
366 n->code = n->code >> count;
367 codes++;
368 }
369 }
370 }
371
372
373 void remove_pre_data(struct ir_remote* remote)
374 {
375 struct ir_ncode* codes;
376 struct ir_code_node* n;
377
378 if (remote->pre_data_bits == 0
379 || remote->pre_p != 0
380 || remote->pre_s != 0)
381 return;
382 for (codes = remote->codes; codes->name != NULL; codes++) {
383 codes->code |= remote->pre_data << remote->bits;
384 for (n = codes->next; n != NULL; n = n->next)
385 n->code |= remote->pre_data << remote->bits;
386 }
387 remote->bits += remote->pre_data_bits;
388 remote->pre_data = 0;
389 remote->pre_data_bits = 0;
390 }
391
392
393 void remove_post_data(struct ir_remote* remote)
394 {
395 struct ir_ncode* codes;
396 struct ir_code_node* n;
397
398 if (remote->post_data_bits == 0)
399 return;
400 for (codes = remote->codes; codes->name != NULL; codes++) {
401 codes->code <<= remote->post_data_bits;
402 codes->code |= remote->post_data;
403 for (n = codes->next; n != NULL; n = n->next) {
404 n->code <<= remote->post_data_bits;
405 n->code |= remote->post_data;
406 }
407 }
408 remote->bits += remote->post_data_bits;
409 remote->post_data = 0;
410 remote->post_data_bits = 0;
411 }
412
413
414 void invert_data(struct ir_remote* remote)
415 {
416 struct ir_ncode* codes;
417 ir_code mask;
418 lirc_t p, s;
419 struct ir_code_node* n;
420
421 /* swap one, zero */
422 p = remote->pone;
423 s = remote->sone;
424 remote->pone = remote->pzero;
425 remote->sone = remote->szero;
426 remote->pzero = p;
427 remote->szero = s;
428
429 /* invert pre_data */
430 if (has_pre(remote)) {
431 mask = gen_mask(remote->pre_data_bits);
432 remote->pre_data ^= mask;
433 }
434 /* invert post_data */
435 if (has_post(remote)) {
436 mask = gen_mask(remote->post_data_bits);
437 remote->post_data ^= mask;
438 }
439
440 if (remote->bits == 0)
441 return;
442
443 /* invert codes */
444 mask = gen_mask(remote->bits);
445 for (codes = remote->codes; codes->name != NULL; codes++) {
446 codes->code ^= mask;
447 for (n = codes->next; n != NULL; n = n->next)
448 n->code ^= mask;
449 }
450 }
451
452
453 void remove_trail(struct ir_remote* remote)
454 {
455 int extra_bit;
456
457 if (!is_space_enc(remote))
458 return;
459 if (remote->ptrail == 0)
460 return;
461 if (expect(remote, remote->pone, remote->pzero)
462 || expect(remote, remote->pzero, remote->pone))
463 return;
464 if (!(expect(remote, remote->sone, remote->szero)
465 && expect(remote, remote->szero, remote->sone)))
466 return;
467 if (expect(remote, remote->ptrail, remote->pone))
468 extra_bit = 1;
469 else if (expect(remote, remote->ptrail, remote->pzero))
470 extra_bit = 0;
471 else
472 return;
473
474 remote->post_data_bits++;
475 remote->post_data <<= 1;
476 remote->post_data |= extra_bit;
477 remote->ptrail = 0;
478 }
479
480
481 void for_each_remote(struct ir_remote* remotes, remote_func func)
482 {
483 struct ir_remote* remote;
484
485 remote = remotes;
486 while (remote != NULL) {
487 func(remote);
488 remote = remote->next;
489 }
490 }
491
492
493 static int mywaitfordata(__u32 maxusec)
494 {
495 int ret;
496 struct pollfd pfd = {
497 .fd = curr_driver->fd, .events = POLLIN, .revents = 0};
498
499 do {
500 ret = curl_poll(&pfd, 1, maxusec / 1000);
501 } while (ret == -1 && errno == EINTR);
502
503 if (ret == -1 && errno != EINTR)
504 log_perror_err("mywaitfordata: curl_poll() failed");
505 return (pfd.revents & POLLIN) != 0;
506 }
507
508
509 static lirc_t emulation_readdata(lirc_t timeout)
510 {
511 static lirc_t sum = 0;
512 lirc_t data = 0;
513
514 if (current_code == NULL) {
515 data = 1000000;
516 if (next_code)
517 current_code = next_code;
518 else
519 current_code = emulation_data->codes;
520 current_rep = 0;
521 sum = 0;
522 } else {
523 if (current_code->name == NULL) {
524 log_warn("%s: no data found", emulation_data->name);
525 data = 0;
526 }
527 if (current_index >= current_code->length) {
528 if (next_code) {
529 current_code = next_code;
530 } else {
531 current_rep++;
532 if (current_rep > 2) {
533 current_code++;
534 current_rep = 0;
535 data = 1000000;
536 }
537 }
538 current_index = 0;
539 if (current_code->name == NULL) {
540 current_code = NULL;
541 return emulation_readdata(timeout);
542 }
543 if (data == 0) {
544 if (is_const(emulation_data))
545 data = emulation_data->gap - sum;
546 else
547 data = emulation_data->gap;
548 }
549
550 sum = 0;
551 } else {
552 data = current_code->signals[current_index];
553 if ((current_index % 2) == 0)
554 data |= PULSE_BIT;
555 current_index++;
556 sum += data & PULSE_MASK;
557 }
558 }
559 log_debug("delivering: %c%u\n",
560 data & PULSE_BIT ? 'p':'s', data & PULSE_MASK);
561 return data;
562 }
563
564
565 static struct lengths* new_length(lirc_t length)
566 {
567 struct lengths* l;
568
569 l = malloc(sizeof(struct lengths));
570 if (l == NULL)
571 return NULL;
572 l->count = 1;
573 l->sum = length;
574 l->lower_bound = length / 100 * 100;
575 l->upper_bound = length / 100 * 100 + 99;
576 l->min = l->max = length;
577 l->next = NULL;
578 return l;
579 }
580
581
582 void unlink_length(struct lengths** first, struct lengths* remove)
583 {
584 struct lengths* last;
585 struct lengths* scan;
586
587 if (remove == *first) {
588 *first = remove->next;
589 remove->next = NULL;
590 return;
591 }
592 scan = (*first)->next;
593 last = *first;
594 while (scan) {
595 if (scan == remove) {
596 last->next = remove->next;
597 remove->next = NULL;
598 return;
599 }
600 last = scan;
601 scan = scan->next;
602 }
603 log_error("unlink_length(): report this bug!");
604 }
605
606
607 int add_length(struct lengths** first, lirc_t length)
608 {
609 struct lengths* l;
610 struct lengths* last;
611
612 if (*first == NULL) {
613 *first = new_length(length);
614 if (*first == NULL)
615 return 0;
616 return 1;
617 }
618 l = *first;
619 while (l != NULL) {
620 if (l->lower_bound <= length && length <= l->upper_bound) {
621 l->count++;
622 l->sum += length;
623 l->min = min(l->min, length);
624 l->max = max(l->max, length);
625 return 1;
626 }
627 last = l;
628 l = l->next;
629 }
630 last->next = new_length(length);
631 if (last->next == NULL)
632 return 0;
633 return 1;
634 }
635
636
637 void free_lengths(struct lengths** firstp)
638 {
639 struct lengths* first;
640 struct lengths* next;
641
642 first = *firstp;
643 if (first == NULL)
644 return;
645 while (first != NULL) {
646 next = first->next;
647 free(first);
648 first = next;
649 }
650 *firstp = NULL;
651 }
652
653
654 void free_all_lengths(void)
655 {
656 free_lengths(&first_space);
657 free_lengths(&first_pulse);
658 free_lengths(&first_sum);
659 free_lengths(&first_gap);
660 free_lengths(&first_repeat_gap);
661 free_lengths(&first_signal_length);
662 free_lengths(&first_headerp);
663 free_lengths(&first_headers);
664 free_lengths(&first_1lead);
665 free_lengths(&first_3lead);
666 free_lengths(&first_trail);
667 free_lengths(&first_repeatp);
668 free_lengths(&first_repeats);
669 }
670
671
672 static void merge_lengths(struct lengths* first)
673 {
674 struct lengths* l;
675 struct lengths* inner;
676 struct lengths* last;
677 __u32 new_sum;
678 int new_count;
679
680 l = first;
681 while (l != NULL) {
682 last = l;
683 inner = l->next;
684 while (inner != NULL) {
685 new_sum = l->sum + inner->sum;
686 new_count = l->count + inner->count;
687
688 if ((l->max <= new_sum / new_count + aeps
689 && l->min + aeps >= new_sum / new_count
690 && inner->max <= new_sum / new_count + aeps
691 && inner->min + aeps >= new_sum / new_count)
692 || (l->max <= new_sum / new_count * (100 + eps)
693 && l->min >= new_sum / new_count * (100 - eps)
694 && inner->max <= new_sum / new_count *
695 (100 + eps)
696 && inner->min >= new_sum / new_count *
697 (100 - eps))) {
698 l->sum = new_sum;
699 l->count = new_count;
700 l->upper_bound = max(l->upper_bound,
701 inner->upper_bound);
702 l->lower_bound = min(l->lower_bound,
703 inner->lower_bound);
704 l->min = min(l->min, inner->min);
705 l->max = max(l->max, inner->max);
706
707 last->next = inner->next;
708 free(inner);
709 inner = last;
710 }
711 last = inner;
712 inner = inner->next;
713 }
714 l = l->next;
715 }
716 for (l = first; l != NULL; l = l->next) {
717 log_debug("%d x %u [%u,%u]",
718 l->count, (__u32)calc_signal(l),
719 (__u32)l->min, (__u32)l->max);
720 }
721 }
722
723
729 static struct lengths* get_max_length(struct lengths* first,
730 unsigned int* sump)
731 {
732 unsigned int sum;
733 struct lengths* scan;
734 struct lengths* max_length;
735
736 if (first == NULL)
737 return NULL;
738 max_length = first;
739 sum = first->count;
740
741 if (first->count > 0)
742 log_debug("%u x %u", first->count,
743 (__u32)calc_signal(first));
744 scan = first->next;
745 while (scan) {
746 if (scan->count > max_length->count)
747 max_length = scan;
748 sum += scan->count;
749 log_debug(
750 "%u x %u",
751 scan->count,
752 (__u32)calc_signal(scan));
753 scan = scan->next;
754 }
755 if (sump != NULL)
756 *sump = sum;
757 return max_length;
758 }
759
760
761 int get_trail_length(struct ir_remote* remote, int interactive)
762 {
763 unsigned int sum = 0, max_count;
764 struct lengths* max_length;
765
766 if (is_biphase(remote))
767 return 1;
768
769 max_length = get_max_length(first_trail, &sum);
770 max_count = max_length->count;
771 log_debug(
772 "get_trail_length(): sum: %u, max_count %u",
773 sum, max_count);
774 if (max_count >= sum * TH_TRAIL / 100) {
775 log_debug("Found trail pulse: %lu",
776 (__u32)calc_signal(max_length));
777 remote->ptrail = calc_signal(max_length);
778 return 1;
779 }
780 log_debug("No trail pulse found.");
781 return 1;
782 }
783
784
785 int get_lead_length(struct ir_remote* remote, int interactive)
786 {
787 unsigned int sum = 0, max_count;
788 struct lengths* first_lead;
789 struct lengths* max_length;
790 struct lengths* max2_length;
791 lirc_t a, b, swap;
792
793 if (!is_biphase(remote) || has_header(remote))
794 return 1;
795 if (is_rc6(remote))
796 return 1;
797
798 first_lead = has_header(remote) ? first_3lead : first_1lead;
799 max_length = get_max_length(first_lead, &sum);
800 max_count = max_length->count;
801 log_debug(
802 "get_lead_length(): sum: %u, max_count %u",
803 sum, max_count);
804 if (max_count >= sum * TH_LEAD / 100) {
805 log_debug(
806 "Found lead pulse: %lu",
807 (__u32)calc_signal(max_length));
808 remote->plead = calc_signal(max_length);
809 return 1;
810 }
811 unlink_length(&first_lead, max_length);
812 max2_length = get_max_length(first_lead, &sum);
813 max_length->next = first_lead;
814 first_lead = max_length;
815
816 a = calc_signal(max_length);
817 b = calc_signal(max2_length);
818 if (a > b) {
819 swap = a;
820 a = b;
821 b = swap;
822 }
823 if (abs(2 * a - b) < b * eps / 100 || abs(2 * a - b) < aeps) {
824 log_debug(
825 "Found hidden lead pulse: %lu",
826 (__u32)a);
827 remote->plead = a;
828 return 1;
829 }
830 log_debug("No lead pulse found.");
831 return 1;
832 }
833
834
835 int get_header_length(struct ir_remote* remote, int interactive)
836 {
837 unsigned int sum, max_count;
838 lirc_t headerp, headers;
839 struct lengths* max_plength;
840 struct lengths* max_slength;
841
842 if (first_headerp != NULL) {
843 max_plength = get_max_length(first_headerp, &sum);
844 max_count = max_plength->count;
845 } else {
846 log_debug("No header data.");
847 return 1;
848 }
849 log_debug(
850 "get_header_length(): sum: %u, max_count %u",
851 sum, max_count);
852
853 if (max_count >= sum * TH_HEADER / 100) {
854 max_slength = get_max_length(first_headers, &sum);
855 max_count = max_slength->count;
856 log_debug(
857 "get_header_length(): sum: %u, max_count %u",
858 sum, max_count);
859 if (max_count >= sum * TH_HEADER / 100) {
860 headerp = calc_signal(max_plength);
861 headers = calc_signal(max_slength);
862
863 log_debug(
864 "Found possible header: %lu %lu",
865 (__u32)headerp,
866 (__u32)headers);
867 remote->phead = headerp;
868 remote->shead = headers;
869 if (first_lengths < second_lengths) {
870 log_debug(
871 "Header is not being repeated.");
872 remote->flags |= NO_HEAD_REP;
873 }
874 return 1;
875 }
876 }
877 log_debug("No header found.");
878 return 1;
879 }
880
881
882 int get_repeat_length(struct ir_remote* remote, int interactive)
883 {
884 unsigned int sum = 0, max_count;
885 lirc_t repeatp, repeats, repeat_gap;
886 struct lengths* max_plength;
887 struct lengths* max_slength;
888
889 if (!((count_3repeats > SAMPLES / 2 ? 1 : 0) ^
890 (count_5repeats > SAMPLES / 2 ? 1 : 0))) {
891 if (count_3repeats > SAMPLES / 2
892 || count_5repeats > SAMPLES / 2) {
893 log_warn("Repeat inconsistency.");
894 return 0;
895 }
896 log_debug("No repeat code found.");
897 return 1;
898 }
899
900 max_plength = get_max_length(first_repeatp, &sum);
901 max_count = max_plength->count;
902 log_debug(
903 "get_repeat_length(): sum: %u, max_count %u",
904 sum, max_count);
905 if (max_count >= sum * TH_REPEAT / 100) {
906 max_slength = get_max_length(first_repeats, &sum);
907 max_count = max_slength->count;
908 log_debug(
909 "get_repeat_length(): sum: %u, max_count %u",
910 sum, max_count);
911 if (max_count >= sum * TH_REPEAT / 100) {
912 if (count_5repeats > count_3repeats
913 && !has_header(remote)) {
914 log_warn(
915 "Repeat code has header,"
916 " but no header found!");
917 return 0;
918 }
919 if (count_5repeats > count_3repeats
920 && has_header(remote))
921 remote->flags |= REPEAT_HEADER;
922 repeatp = calc_signal(max_plength);
923 repeats = calc_signal(max_slength);
924
925 log_debug(
926 "Found repeat code: %lu %lu",
927 (__u32)repeatp,
928 (__u32)repeats);
929 remote->prepeat = repeatp;
930 remote->srepeat = repeats;
931 if (!(remote->flags & CONST_LENGTH)) {
932 max_slength = get_max_length(first_repeat_gap,
933 NULL);
934 repeat_gap = calc_signal(max_slength);
935 log_debug(
936 "Found repeat gap: %lu",
937 (__u32)repeat_gap);
938 remote->repeat_gap = repeat_gap;
939 }
940 return 1;
941 }
942 }
943 log_debug("No repeat header found.");
944 return 1;
945 }
946
947
948 void get_scheme(struct ir_remote* remote, int interactive)
949 {
950 unsigned int i, length = 0, sum = 0;
951 struct lengths* maxp;
952 struct lengths* max2p;
953 struct lengths* maxs;
954 struct lengths* max2s;
955
956 for (i = 1; i < MAX_SIGNALS; i++) {
957 if (lengths[i] > lengths[length])
958 length = i;
959 sum += lengths[i];
960 if (lengths[i] > 0)
961 log_debug("%u: %u", i, lengths[i]);
962 }
963 log_debug("get_scheme(): sum: %u length: %u signals: %u"
964 " first_lengths: %u second_lengths: %u\n",
965 sum, length + 1, lengths[length],
966 first_lengths, second_lengths);
967 /* FIXME !!! this heuristic is too bad */
968 if (lengths[length] >= TH_SPACE_ENC * sum / 100) {
969 length++;
970 log_debug(
971 "Space/pulse encoded remote control found.");
972 log_debug("Signal length is %u.", length);
973 /* this is not yet the number of bits */
974 remote->bits = length;
975 set_protocol(remote, SPACE_ENC);
976 return;
977 }
978 maxp = get_max_length(first_pulse, NULL);
979 unlink_length(&first_pulse, maxp);
980 if (first_pulse == NULL)
981 first_pulse = maxp;
982 max2p = get_max_length(first_pulse, NULL);
983 maxp->next = first_pulse;
984 first_pulse = maxp;
985
986 maxs = get_max_length(first_space, NULL);
987 unlink_length(&first_space, maxs);
988 if (first_space == NULL) {
989 first_space = maxs;
990 } else {
991 max2s = get_max_length(first_space, NULL);
992 maxs->next = first_space;
993 first_space = maxs;
994
995 maxs = get_max_length(first_space, NULL);
996
997 if (length > 20
998 && (calc_signal(maxp) < TH_RC6_SIGNAL
999 || calc_signal(max2p) < TH_RC6_SIGNAL)
1000 && (calc_signal(maxs) < TH_RC6_SIGNAL
1001 || calc_signal(max2s) < TH_RC6_SIGNAL)) {
1002 log_debug("RC-6 remote control found.");
1003 set_protocol(remote, RC6);
1004 } else {
1005 log_debug("RC-5 remote control found.");
1006 set_protocol(remote, RC5);
1007 }
1008 return;
1009 }
1010 length++;
1011 log_debug("Suspicious data length: %u.", length);
1012 /* this is not yet the number of bits */
1013 remote->bits = length;
1014 set_protocol(remote, SPACE_ENC);
1015 }
1016
1017
1018 int get_data_length(struct ir_remote* remote, int interactive)
1019 {
1020 unsigned int sum = 0, max_count;
1021 lirc_t p1, p2, s1, s2;
1022 struct lengths* max_plength;
1023 struct lengths* max_slength;
1024 struct lengths* max2_plength;
1025 struct lengths* max2_slength;
1026
1027 max_plength = get_max_length(first_pulse, &sum);
1028 max_count = max_plength->count;
1029 log_debug("get_data_length(): sum: %u, max_count %u",
1030 sum, max_count);
1031
1032 if (max_count >= sum * TH_IS_BIT / 100) {
1033 unlink_length(&first_pulse, max_plength);
1034
1035 max2_plength = get_max_length(first_pulse, NULL);
1036 if (max2_plength != NULL)
1037 if (max2_plength->count < max_count * TH_IS_BIT / 100)
1038 max2_plength = NULL;
1039 log_debug("Pulse candidates: ");
1040 log_debug("%u x %u", max_plength->count,
1041 (__u32)calc_signal(max_plength));
1042 if (max2_plength)
1043 log_debug(", %u x %u",
1044 max2_plength->count,
1045 (__u32)calc_signal(max2_plength));
1046
1047 max_slength = get_max_length(first_space, &sum);
1048 max_count = max_slength->count;
1049 log_debug(
1050 "get_data_length(): sum: %u, max_count %u",
1051 sum, max_count);
1052 if (max_count >= sum * TH_IS_BIT / 100) {
1053 unlink_length(&first_space, max_slength);
1054
1055 max2_slength = get_max_length(first_space, NULL);
1056 if (max2_slength != NULL)
1057 if (max2_slength->count <
1058 max_count * TH_IS_BIT / 100)
1059 max2_slength = NULL;
1060 if (max_count >= sum * TH_IS_BIT / 100) {
1061 log_debug("Space candidates: ");
1062 log_debug(
1063 "%u x %u",
1064 max_slength->count,
1065 (__u32)calc_signal(max_slength));
1066 if (max2_slength) {
1067 log_debug(
1068 "%u x %u",
1069 max2_slength->count,
1070 (__u32)calc_signal(max2_slength));
1071 }
1072 }
1073 remote->eps = eps;
1074 remote->aeps = aeps;
1075 if (is_biphase(remote)) {
1076 if (max2_plength == NULL
1077 || max2_slength == NULL) {
1078 log_notice(
1079 "Unknown encoding found.");
1080 return 0;
1081 }
1082 log_debug(
1083 "Signals are biphase encoded.");
1084 p1 = calc_signal(max_plength);
1085 p2 = calc_signal(max2_plength);
1086 s1 = calc_signal(max_slength);
1087 s2 = calc_signal(max2_slength);
1088
1089 remote->pone =
1090 (min(p1, p2) + max(p1, p2) / 2) / 2;
1091 remote->sone =
1092 (min(s1, s2) + max(s1, s2) / 2) / 2;
1093 remote->pzero = remote->pone;
1094 remote->szero = remote->sone;
1095 } else {
1096 if (max2_plength == NULL
1097 && max2_slength == NULL) {
1098 log_notice(
1099 "No encoding found");
1100 return 0;
1101 }
1102 if (max2_plength && max2_slength) {
1103 log_notice(
1104 "Unknown encoding found.");
1105 return 0;
1106 }
1107 p1 = calc_signal(max_plength);
1108 s1 = calc_signal(max_slength);
1109 if (max2_plength) {
1110 p2 = calc_signal(max2_plength);
1111 log_debug("Signals are pulse encoded.");
1112 remote->pone = max(p1, p2);
1113 remote->sone = s1;
1114 remote->pzero = min(p1, p2);
1115 remote->szero = s1;
1116 if (expect(remote, remote->ptrail, p1)
1117 || expect(remote, remote->ptrail,
1118 p2))
1119 remote->ptrail = 0;
1120 } else {
1121 s2 = calc_signal(max2_slength);
1122 log_debug("Signals are space encoded.");
1123 remote->pone = p1;
1124 remote->sone = max(s1, s2);
1125 remote->pzero = p1;
1126 remote->szero = min(s1, s2);
1127 }
1128 }
1129 if (has_header(remote)
1130 && (!has_repeat(remote)
1131 || remote->flags & NO_HEAD_REP)) {
1132 if (!is_biphase(remote)
1133 && ((expect(remote, remote->phead,
1134 remote->pone)
1135 && expect(remote,
1136 remote->shead,
1137 remote->sone))
1138 || (expect(remote,
1139 remote->phead,
1140 remote->pzero)
1141 && expect(remote,
1142 remote->shead,
1143 remote->szero)))) {
1144 remote->phead = remote->shead = 0;
1145 remote->flags &= ~NO_HEAD_REP;
1146 log_debug(
1147 "Removed header.");
1148 }
1149 if (is_biphase(remote)
1150 && expect(remote,
1151 remote->shead,
1152 remote->sone)) {
1153 remote->plead = remote->phead;
1154 remote->phead = remote->shead = 0;
1155 remote->flags &= ~NO_HEAD_REP;
1156 log_debug(
1157 "Removed header.");
1158 }
1159 }
1160 if (is_biphase(remote)) {
1161 struct lengths* signal_length;
1162 lirc_t data_length;
1163
1164 signal_length =
1165 get_max_length(first_signal_length,
1166 NULL);
1167 data_length =
1168 calc_signal(signal_length) -
1169 remote->plead - remote->phead -
1170 remote->shead +
1171 /* + 1/2 bit */
1172 (remote->pone + remote->sone) / 2;
1173 remote->bits = data_length / (remote->pone +
1174 remote->sone);
1175 if (is_rc6(remote))
1176 remote->bits--;
1177 } else {
1178 remote->bits =
1179 (remote->bits -
1180 (has_header(remote) ? 2 : 0) + 1 -
1181 (remote->ptrail > 0 ? 2 : 0)) / 2;
1182 }
1183 log_debug(
1184 "Signal length is %d",
1185 remote->bits);
1186 free_lengths(&max_plength);
1187 free_lengths(&max_slength);
1188 return 1;
1189 }
1190 free_lengths(&max_plength);
1191 }
1192 log_notice("Could not find data lengths.");
1193 return 0;
1194 }
1195
1196
1197 enum get_gap_status get_gap_length(struct gap_state* state,
1198 struct ir_remote* remote)
1199 {
1200 while (availabledata())
1201 curr_driver->rec_func(NULL);
1202 if (!mywaitfordata(10000000)) {
1203 free_lengths(&(state->gaps));
1204 return STS_GAP_TIMEOUT;
1205 }
1206 gettimeofday(&(state->start), NULL);
1207 while (availabledata())
1208 curr_driver->rec_func(NULL);
1209 gettimeofday(&(state->end), NULL);
1210 if (state->flag) {
1211 state->gap = time_elapsed(&(state->last), &(state->start));
1212 add_length(&(state->gaps), state->gap);
1213 merge_lengths(state->gaps);
1214 state->maxcount = 0;
1215 state->scan = state->gaps;
1216 while (state->scan) {
1217 state->maxcount = max(state->maxcount,
1218 state->scan->count);
1219 if (state->scan->count > SAMPLES) {
1220 remote->gap = calc_signal(state->scan);
1221 free_lengths(&(state->gaps));
1222 return STS_GAP_FOUND;
1223 }
1224 state->scan = state->scan->next;
1225 }
1226 if (state->maxcount > state->lastmaxcount) {
1227 state->lastmaxcount = state->maxcount;
1228 return STS_GAP_GOT_ONE_PRESS;
1229 }
1230 } else {
1231 state->flag = 1;
1232 }
1233 state->last = state->end;
1234 return STS_GAP_AGAIN;
1235 }
1236
1237
1239 int needs_toggle_mask(struct ir_remote* remote)
1240 {
1241 struct ir_ncode* codes;
1242
1243 if (!is_rc6(remote))
1244 return 0;
1245 if (remote->codes) {
1246 codes = remote->codes;
1247 while (codes->name != NULL) {
1248 if (codes->next)
1249 /* asume no toggle bit mask when key
1250 * sequences are used */
1251 return 0;
1252 codes++;
1253 }
1254 }
1255 return 1;
1256 }
1257
1258
1259 /* Compute lengths from four recorded signals. */
1260 static void compute_lengths_4_signals(void)
1261 {
1262 add_length(&first_repeatp, signals[0]);
1263 merge_lengths(first_repeatp);
1264 add_length(&first_repeats, signals[1]);
1265 merge_lengths(first_repeats);
1266 add_length(&first_trail, signals[2]);
1267 merge_lengths(first_trail);
1268 add_length(&first_repeat_gap, signals[3]);
1269 merge_lengths(first_repeat_gap);
1270 }
1271
1272
1273 /* Compute lengths from six recorded signals. */
1274 static void compute_lengths_6_signals(void)
1275 {
1276 add_length(&first_headerp, signals[0]);
1277 merge_lengths(first_headerp);
1278 add_length(&first_headers, signals[1]);
1279 merge_lengths(first_headers);
1280 add_length(&first_repeatp, signals[2]);
1281 merge_lengths(first_repeatp);
1282 add_length(&first_repeats, signals[3]);
1283 merge_lengths(first_repeats);
1284 add_length(&first_trail, signals[4]);
1285 merge_lengths(first_trail);
1286 add_length(&first_repeat_gap, signals[5]);
1287 merge_lengths(first_repeat_gap);
1288 }
1289
1290 /* Compute lengths from more than six recorded signals. */
1291 static void compute_lengths_many_signals(struct lengths_state* state)
1292 {
1293 int i;
1294
1295 add_length(&first_1lead, signals[0]);
1296 merge_lengths(first_1lead);
1297 for (i = 2; i < state->count - 2; i++) {
1298 if (i % 2) {
1299 add_length(&first_space, signals[i]);
1300 merge_lengths(first_space);
1301 } else {
1302 add_length(&first_pulse, signals[i]);
1303 merge_lengths(first_pulse);
1304 }
1305 }
1306 add_length(&first_trail, signals[state->count - 2]);
1307 merge_lengths(first_trail);
1308 lengths[state->count - 2]++;
1309 add_length(&first_signal_length, state->sum - state->data);
1310 merge_lengths(first_signal_length);
1311 if (state->first_signal == 1
1312 || (first_length > 2
1313 && first_length - 2 != state->count - 2)) {
1314 add_length(&first_3lead, signals[2]);
1315 merge_lengths(first_3lead);
1316 add_length(&first_headerp, signals[0]);
1317 merge_lengths(first_headerp);
1318 add_length(&first_headers, signals[1]);
1319 merge_lengths(first_headers);
1320 }
1321 if (state->first_signal == 1) {
1322 first_lengths++;
1323 first_length = state->count - 2;
1324 state->header = signals[0] + signals[1];
1325 } else if (state->first_signal == 0
1326 && first_length - 2 == state->count - 2) {
1327 lengths[state->count - 2]--;
1328 lengths[state->count - 2 + 2]++;
1329 second_lengths++;
1330 }
1331 }
1332
1333
1334 static struct lengths* scan_gap1(struct lengths_state* state,
1335 struct ir_remote* remote,
1336 int* maxcount,
1337 enum lengths_status* again)
1338 {
1339 struct lengths* scan;
1340
1341 for (scan = first_sum; scan; scan = scan->next) {
1342 *maxcount = max(*maxcount, scan->count);
1343 if (scan->count > SAMPLES) {
1344 remote->gap = calc_signal(scan);
1345 remote->flags |= CONST_LENGTH;
1346 state->mode = MODE_HAVE_GAP;
1347 log_debug("Found gap: %u", remote->gap);
1348 *again = STS_LEN_AGAIN_INFO;
1349 break;
1350 }
1351 }
1352 return scan;
1353 }
1354
1355
1356 static struct lengths* scan_gap2(struct lengths_state* state,
1357 struct ir_remote* remote,
1358 int* maxcount,
1359 enum lengths_status* again)
1360 {
1361 struct lengths* scan;
1362
1363 for (scan = first_gap; scan; scan = scan->next) {
1364 *maxcount = max(*maxcount, scan->count);
1365 if (scan->count > SAMPLES) {
1366 remote->gap = calc_signal(scan);
1367 state->mode = MODE_HAVE_GAP;
1368 log_debug("Found gap: %u", remote->gap);
1369 *again = STS_LEN_AGAIN_INFO;
1370 break;
1371 }
1372 }
1373 return scan;
1374 }
1375
1376
1377 enum lengths_status get_lengths(struct lengths_state* state,
1378 struct ir_remote* remote,
1379 int force, int interactive)
1380 {
1381 struct lengths* scan;
1382 int maxcount = 0;
1383 static int lastmaxcount = 0;
1384 enum lengths_status again = STS_LEN_AGAIN;
1385
1386 state->data = curr_driver->readdata(10000000);
1387 if (!state->data) {
1388 state->retval = 0;
1389 return STS_LEN_TIMEOUT;
1390 }
1391 state->count++;
1392 if (state->mode == MODE_GET_GAP) {
1393 state->sum += state->data & PULSE_MASK;
1394 if (state->average == 0 && is_space(state->data)) {
1395 if (state->data > 100000) {
1396 state->sum = 0;
1397 return STS_LEN_AGAIN;
1398 }
1399 state->average = state->data;
1400 state->maxspace = state->data;
1401 } else if (is_space(state->data)) {
1402 if (state->data > MIN_GAP
1403 || state->data > 100 * state->average
1404 /* this MUST be a gap */
1405 || (state->data >= 5000 && count_spaces > 10
1406 && state->data > 5 * state->average)
1407 || (state->data < 5000 && count_spaces > 10
1408 && state->data > 5 * state->maxspace / 2)) {
1409 add_length(&first_sum, state->sum);
1410 merge_lengths(first_sum);
1411 add_length(&first_gap, state->data);
1412 merge_lengths(first_gap);
1413 state->sum = 0;
1414 count_spaces = 0;
1415 state->average = 0;
1416 state->maxspace = 0;
1417
1418 maxcount = 0;
1419 scan = scan_gap1(state,
1420 remote,
1421 &maxcount,
1422 &again);
1423 if (scan == NULL) {
1424 scan = scan_gap2(state,
1425 remote,
1426 &maxcount,
1427 &again);
1428 }
1429 if (scan != NULL) {
1430 state->mode = MODE_HAVE_GAP;
1431 state->sum = 0;
1432 state->count = 0;
1433 state->remaining_gap =
1434 is_const(remote) ?
1435 (remote->gap >
1436 state->data ?
1437 remote->gap - state->data : 0)
1438 : (has_repeat_gap(remote) ?
1439 remote->
1440 repeat_gap : remote->gap);
1441 if (force) {
1442 state->retval = 0;
1443 return STS_LEN_RAW_OK;
1444 }
1445 return STS_LEN_AGAIN_INFO;
1446 }
1447 lastmaxcount = maxcount;
1448 state->keypresses = lastmaxcount;
1449 return again;
1450 }
1451 state->average =
1452 (state->average * count_spaces + state->data)
1453 / (count_spaces + 1);
1454 count_spaces++;
1455 if (state->data > state->maxspace)
1456 state->maxspace = state->data;
1457 }
1458 if (state->count > SAMPLES * MAX_SIGNALS * 2) {
1459 state->retval = 0;
1460 return STS_LEN_NO_GAP_FOUND;
1461 }
1462 state->keypresses = lastmaxcount;
1463 return STS_LEN_AGAIN;
1464 } else if (state->mode == MODE_HAVE_GAP) {
1465 if (state->count <= MAX_SIGNALS) {
1466 signals[state->count - 1] = state->data & PULSE_MASK;
1467 } else {
1468 state->retval = 0;
1469 return STS_LEN_TOO_LONG;
1470 }
1471 if (is_const(remote))
1472 state->remaining_gap =
1473 remote->gap > state->sum ?
1474 remote->gap - state->sum : 0;
1475 else
1476 state->remaining_gap = remote->gap;
1477 state->sum += state->data & PULSE_MASK;
1478
1479 if (state->count > 2
1480 && ((state->data & PULSE_MASK) >=
1481 state->remaining_gap * (100 - eps) / 100
1482 || (state->data & PULSE_MASK) >=
1483 state->remaining_gap - aeps)) {
1484 if (is_space(state->data)) {
1485 /* signal complete */
1486 state->keypresses += 1;
1487 if (state->count == 4) {
1488 count_3repeats++;
1489 compute_lengths_4_signals();
1490 } else if (state->count == 6) {
1491 count_5repeats++;
1492 compute_lengths_6_signals();
1493 } else if (state->count > 6) {
1494 count_signals++;
1495 compute_lengths_many_signals(state);
1496 }
1497 state->count = 0;
1498 state->sum = 0;
1499 }
1500 /* such long pulses may appear with
1501 * crappy hardware (receiver? / remote?)
1502 */
1503 else {
1504 remote->gap = 0;
1505 return STS_LEN_NO_GAP_FOUND;
1506 }
1507
1508 if (count_signals >= SAMPLES) {
1509 get_scheme(remote, interactive);
1510 if (!get_header_length(remote, interactive)
1511 || !get_trail_length(remote, interactive)
1512 || !get_lead_length(remote, interactive)
1513 || !get_repeat_length(remote, interactive)
1514 || !get_data_length(remote, interactive))
1515 state->retval = 0;
1516 return state->retval ==
1517 0 ? STS_LEN_FAIL : STS_LEN_OK;
1518 }
1519 if ((state->data & PULSE_MASK) <=
1520 (state->remaining_gap + state->header) *
1521 (100 + eps) / 100
1522 || (state->data & PULSE_MASK) <=
1523 (state->remaining_gap + state->header) + aeps) {
1524 state->first_signal = 0;
1525 state->header = 0;
1526 } else {
1527 state->first_signal = 1;
1528 }
1529 }
1530 }
1531 return STS_LEN_AGAIN;
1532 }
1533
1534
1535 enum toggle_status get_toggle_bit_mask(struct toggle_state* state,
1536 struct ir_remote* remote)
1537 {
1538 struct decode_ctx_t decode_ctx = { 0 };
1539 int i;
1540 ir_code mask;
1541
1542 if (!state->inited) {
1543 sleep(1);
1544 while (availabledata())
1545 curr_driver->rec_func(NULL);
1546 state->inited = 1;
1547 }
1548 if (state->retries <= 0) {
1549 if (!state->found)
1550 return STS_TGL_NOT_FOUND;
1551 if (state->seq > 0) {
1552 remote->min_repeat = state->repeats / state->seq;
1553 log_debug("min_repeat=%d",
1554 remote->min_repeat);
1555 }
1556 return STS_TGL_FOUND;
1557 }
1558 if (!mywaitfordata(10000000))
1559 return STS_TGL_TIMEOUT;
1560 curr_driver->rec_func(remote);
1561 if (is_rc6(remote) && remote->rc6_mask == 0) {
1562 for (i = 0, mask = 1; i < remote->bits; i++, mask <<= 1) {
1563 remote->rc6_mask = mask;
1564 state->success =
1565 curr_driver->decode_func(remote, &decode_ctx);
1566 if (state->success) {
1567 remote->min_remaining_gap =
1568 decode_ctx.min_remaining_gap;
1569 remote->max_remaining_gap =
1570 decode_ctx.max_remaining_gap;
1571 break;
1572 }
1573 }
1574 if (!state->success)
1575 remote->rc6_mask = 0;
1576 } else {
1577 state->success =
1578 curr_driver->decode_func(remote, &decode_ctx);
1579 if (state->success) {
1580 remote->min_remaining_gap =
1581 decode_ctx.min_remaining_gap;
1582 remote->max_remaining_gap =
1583 decode_ctx.max_remaining_gap;
1584 }
1585 }
1586 if (state->success) {
1587 if (state->flag == 0) {
1588 state->flag = 1;
1589 state->first = decode_ctx.code;
1590 } else if (!decode_ctx.repeat_flag
1591 || decode_ctx.code != state->last) {
1592 state->seq++;
1593 mask = state->first ^ decode_ctx.code;
1594 if (!state->found && mask) {
1595 set_toggle_bit_mask(remote, mask);
1596 state->found = 1;
1597 if (state->seq > 0)
1598 remote->min_repeat =
1599 state->repeats / state->seq;
1600 }
1601 state->retries--;
1602 state->last = decode_ctx.code;
1603 return STS_TGL_GOT_ONE_PRESS;
1604 }
1605 state->repeats++;
1606 state->last = decode_ctx.code;
1607 } else {
1608 state->retries--;
1609 while (availabledata())
1610 curr_driver->rec_func(NULL);
1611 }
1612 return STS_TGL_AGAIN;
1613 }
1614
1615
1617 int analyse_get_lengths(struct lengths_state* lengths_state)
1618 {
1619 enum lengths_status status = STS_LEN_AGAIN;
1620
1621 while (status == STS_LEN_AGAIN) {
1622 status = get_lengths(lengths_state, &remote, 0, 0);
1623 switch (status) {
1624 case STS_LEN_AGAIN_INFO:
1625 status = STS_LEN_AGAIN;
1626 break;
1627 case STS_LEN_AGAIN:
1628 break;
1629 case STS_LEN_OK:
1630 break;
1631 case STS_LEN_FAIL:
1632 log_error("get_lengths() failure");
1633 return 0;
1634 case STS_LEN_RAW_OK:
1635 log_error("raw analyse result?!");
1636 return 0;
1637 case STS_LEN_TIMEOUT:
1638 log_error("analyse timeout?!");
1639 return 0;
1640 case STS_LEN_NO_GAP_FOUND:
1641 log_error("analyse, no gap?!");
1642 return 0;
1643 case STS_LEN_TOO_LONG:
1644 log_error("analyse, signal too long?!");
1645 return 0;
1646 default:
1647 log_error("Cannot read raw data (%d)",
1648 status);
1649 return 0;
1650 }
1651 }
1652 return 1;
1653 }
1654
1655
1657 int analyse_remote(struct ir_remote* raw_data, const struct opts* opts)
1658 {
1659 struct ir_ncode* codes;
1660 struct decode_ctx_t decode_ctx;
1661 struct lengths_state lengths_state;
1662 int code;
1663 int code2;
1664 struct ir_ncode* new_codes;
1665 size_t new_codes_count = 100;
1666 int new_index = 0;
1667 int ret;
1668
1669 if (!is_raw(raw_data)) {
1670 log_error("remote %s not in raw mode, ignoring",
1671 raw_data->name);
1672 return 0;
1673 }
1674 flushhw();
1675 aeps = raw_data->aeps;
1676 eps = raw_data->eps;
1677 emulation_data = raw_data;
1678 next_code = NULL;
1679 current_code = NULL;
1680 current_index = 0;
1681 memset(&remote, 0, sizeof(remote));
1682 lengths_state_init(&lengths_state);
1683 if (!analyse_get_lengths(&lengths_state))
1684 return 0;
1685
1686 if (is_rc6(&remote) && remote.bits >= 5)
1687 /* have to assume something as it's very difficult to
1688 * extract the rc6_mask from the data that we have */
1689 remote.rc6_mask = ((ir_code)0x1ll) << (remote.bits - 5);
1690
1691 remote.name = raw_data->name;
1692 remote.freq = raw_data->freq;
1693
1694 new_codes = malloc(new_codes_count * sizeof(*new_codes));
1695 if (new_codes == NULL) {
1696 log_error("Out of memory");
1697 return 0;
1698 }
1699 memset(new_codes, 0, new_codes_count * sizeof(*new_codes));
1700 codes = raw_data->codes;
1701 while (codes->name != NULL) {
1702 // printf("decoding %s\n", codes->name);
1703 current_code = NULL;
1704 current_index = 0;
1705 next_code = codes;
1706
1707 rec_buffer_init();
1708
1709 ret = receive_decode(&remote, &decode_ctx);
1710 if (!ret) {
1711 log_warn(
1712 "Decoding of %s failed", codes->name);
1713 } else {
1714 if (new_index + 1 >= new_codes_count) {
1715 struct ir_ncode* renew_codes;
1716
1717 new_codes_count *= 2;
1718 renew_codes =
1719 realloc(new_codes,
1720 new_codes_count *
1721 sizeof(*new_codes));
1722 if (renew_codes == NULL) {
1723 log_error("Out of memory");
1724 free(new_codes);
1725 return 0;
1726 }
1727 memset(&new_codes[new_codes_count / 2],
1728 0,
1729 new_codes_count / 2 *
1730 sizeof(*new_codes));
1731 new_codes = renew_codes;
1732 }
1733
1734 rec_buffer_clear();
1735 code = decode_ctx.code;
1736 ret = receive_decode(&remote, &decode_ctx);
1737 code2 = decode_ctx.code;
1738 decode_ctx.code = code;
1739 if (ret && code2 != decode_ctx.code) {
1740 new_codes[new_index].next =
1741 malloc(IR_CODE_NODE_SIZE);
1742 if (new_codes[new_index].next) {
1743 memset(new_codes[new_index].next,
1744 0,
1745 IR_CODE_NODE_SIZE);
1746 new_codes[new_index].next->code =
1747 code2;
1748 }
1749 }
1750 new_codes[new_index].name = codes->name;
1751 new_codes[new_index].code = decode_ctx.code;
1752 new_index++;
1753 }
1754 codes++;
1755 }
1756 new_codes[new_index].name = NULL;
1757 remote.codes = new_codes;
1758 fprint_remotes(stdout, &remote, opts->commandline);
1759 remote.codes = NULL;
1760 free(new_codes);
1761 return 1;
1762 }
1763
1764
1766 int do_analyse(const struct opts* opts, struct main_state* state)
1767 {
1768 FILE* f;
1769 struct ir_remote* r;
1770
1771 memcpy((void*)curr_driver, &hw_emulation, sizeof(struct driver));
1772 f = fopen(opts->filename, "r");
1773 if (f == NULL) {
1774 fprintf(stderr, "Cannot open file: %s\n", opts->filename);
1775 return 0;
1776 }
1777 r = read_config(f, opts->filename);
1778 if (r == NULL) {
1779 fprintf(stderr, "Cannot parse file: %s\n", opts->filename);
1780 return 0;
1781 }
1782 for (; r != NULL; r = r->next) {
1783 if (!is_raw(r)) {
1784 log_error("remote %s not in raw mode, ignoring",
1785 r->name);
1786 continue;
1787 }
1788 analyse_remote(r, opts);
1789 }
1790 return 1;
1791 }
1792
1793
1794 ssize_t raw_read(void* buffer, size_t size, unsigned int timeout_us)
1795 {
1796 if (!mywaitfordata(timeout_us))
1797 return 0;
1798 return read(curr_driver->fd, buffer, size);
1799 }
1800
1801
1802 static int raw_data_ok(struct button_state* btn_state)
1803 {
1804 int r;
1805 int ref;
1806
1807 if (!is_space(btn_state->data)) {
1808 r = 0;
1809 } else if (is_const(&remote)) {
1810 if (remote.gap > btn_state->sum) {
1811 ref = (remote.gap - btn_state->sum);
1812 ref *= (100 - remote.eps);
1813 ref /= 100;
1814 } else {
1815 ref = 0;
1816 }
1817 r = btn_state->data > ref;
1818 } else {
1819 r = btn_state->data > (remote.gap * (100 - remote.eps)) / 100;
1820 }
1821 return r;
1822 }
1823
1824
1825 enum button_status record_buttons(struct button_state* btn_state,
1826 enum button_status last_status,
1827 struct main_state* state,
1828 const struct opts* opts)
1829 {
1830 const char* const MSG_BAD_LENGTH =
1831 "Signal length is %d\n"
1832 "That's weird because the signal length must be odd!\n";
1833 ir_code code2;
1834 int decode_ok;
1835 __u32 timeout;
1836 int retries;
1837 struct ir_remote* my_remote;
1838 FILE* f;
1839 enum button_status sts;
1840
1841 if (btn_state->no_data) {
1842 btn_state->no_data = 0;
1843 return STS_BTN_TIMEOUT;
1844 }
1845 switch (last_status) {
1846 case STS_BTN_INIT:
1847 return STS_BTN_GET_NAME;
1848 case STS_BTN_GET_NAME:
1849 if (strchr(btn_state->buffer, ' ') != NULL) {
1850 btn_state_set_message(
1851 btn_state,
1852 "The name must not contain any whitespace.");
1853 return STS_BTN_SOFT_ERROR;
1854 }
1855 if (strchr(btn_state->buffer, '\t') != NULL) {
1856 btn_state_set_message(
1857 btn_state,
1858 "The name must not contain any whitespace.");
1859 return STS_BTN_SOFT_ERROR;
1860 }
1861 if (strcasecmp(btn_state->buffer, "begin") == 0) {
1862 btn_state_set_message(
1863 btn_state,
1864 "'%s' is not allowed as button name\n",
1865 btn_state->buffer);
1866 return STS_BTN_SOFT_ERROR;
1867 }
1868 if (strcasecmp(btn_state->buffer, "end") == 0) {
1869 btn_state_set_message(
1870 btn_state,
1871 "'%s' is not allowed as button name\n",
1872 btn_state->buffer);
1873 return STS_BTN_SOFT_ERROR;
1874 }
1875 if (strlen(btn_state->buffer) == 0)
1876 return STS_BTN_RECORD_DONE;
1877 if (!opts->disable_namespace
1878 && !is_in_namespace(btn_state->buffer)) {
1879 btn_state_set_message(
1880 btn_state,
1881 "'%s' is not in name space"
1882 " (use --disable-namespace to override)\n",
1883 btn_state->buffer);
1884 return STS_BTN_SOFT_ERROR;
1885 }
1886 return STS_BTN_INIT_DATA;
1887 case STS_BTN_INIT_DATA:
1888 if (opts->force)
1889 flushhw();
1890 else
1891 while (availabledata())
1892 curr_driver->rec_func(NULL);
1893 if (curr_driver->fd == -1)
1894 curr_driver->init_func();
1895 return opts->force ? STS_BTN_GET_RAW_DATA : STS_BTN_GET_DATA;
1896 case STS_BTN_GET_DATA:
1897 for (retries = RETRIES; retries > 0; ) {
1898 if (!mywaitfordata(10000000)) {
1899 btn_state->no_data = 1;
1900 return STS_BTN_TIMEOUT;
1901 }
1902 decode_ok = 0;
1903 last_remote = NULL;
1904 sleep(1);
1905 while (availabledata()) {
1906 curr_driver->rec_func(NULL);
1907 if (curr_driver->decode_func(
1908 &remote,
1909 &(state->decode_ctx))) {
1910 decode_ok = 1;
1911 break;
1912 }
1913 }
1914 if (!decode_ok) {
1915 if (!resethw(btn_state->started_as_root)) {
1916 btn_state_set_message(
1917 btn_state,
1918 "Could not reset hardware.\n");
1919 return STS_BTN_HARD_ERROR;
1920 }
1921 btn_state_set_message(btn_state,
1922 "Cannot decode data\n");
1923 flushhw();
1924 return STS_BTN_SOFT_ERROR;
1925 }
1926 btn_state->ncode.name = btn_state->buffer;
1927 btn_state->ncode.code = state->decode_ctx.code;
1928 curr_driver->rec_func(NULL);
1929 if (!curr_driver->decode_func(&remote,
1930 &(state->decode_ctx))) {
1931 code2 = state->decode_ctx.code;
1932 state->decode_ctx.code = btn_state->ncode.code;
1933 if (state->decode_ctx.code != code2) {
1934 btn_state->ncode.next =
1935 malloc(IR_CODE_NODE_SIZE);
1936 if (btn_state->ncode.next) {
1937 memset(btn_state->ncode.next,
1938 0,
1939 IR_CODE_NODE_SIZE);
1940 btn_state->ncode.next->code =
1941 code2;
1942 }
1943 }
1944 }
1945 break;
1946 }
1947 return STS_BTN_BUTTON_DONE;
1948 case STS_BTN_GET_RAW_DATA:
1949 btn_state->count = 0;
1950 btn_state->sum = 0;
1951 while (btn_state->count < MAX_SIGNALS) {
1952 if (btn_state->count == 0)
1953 timeout = 10000000;
1954 else
1955 timeout = remote.gap * 5;
1956 btn_state->data = curr_driver->readdata(timeout);
1957 if (!btn_state->data) {
1958 if (btn_state->count == 0)
1959 return STS_BTN_TIMEOUT;
1960 btn_state->data = remote.gap;
1961 }
1962 if (btn_state->count == 0) {
1963 if (!is_space(btn_state->data)
1964 || btn_state->data <
1965 remote.gap - remote.gap * remote.eps /
1966 100) {
1967 sleep(3);
1968 flushhw();
1969 btn_state->count = 0;
1970 btn_state_set_message(
1971 btn_state,
1972 "Something went wrong.");
1973 return STS_BTN_SOFT_ERROR;
1974 }
1975 } else {
1976 if (raw_data_ok(btn_state)) {
1977 log_info("Got it.\n");
1978 log_info("Signal length is %d\n",
1979 btn_state->count - 1);
1980 if (btn_state->count % 2) {
1981 btn_state_set_message(
1982 btn_state,
1983 MSG_BAD_LENGTH,
1984 btn_state->count - 1);
1985 sleep(3);
1986 flushhw();
1987 btn_state->count = 0;
1988 return STS_BTN_SOFT_ERROR;
1989 }
1990 btn_state->ncode.name =
1991 btn_state->buffer;
1992 btn_state->ncode.length =
1993 btn_state->count - 1;
1994 btn_state->ncode.signals = signals;
1995 break;
1996 }
1997 signals[btn_state->count - 1] =
1998 btn_state->data & PULSE_MASK;
1999 btn_state->sum +=
2000 btn_state->data & PULSE_MASK;
2001 }
2002 btn_state->count++;
2003 }
2004 if (btn_state->count == MAX_SIGNALS) {
2005 btn_state_set_message(btn_state,
2006 "Signal is too long.\n");
2007 return STS_BTN_SOFT_ERROR;
2008 }
2009 return STS_BTN_BUTTON_DONE;
2010 case STS_BTN_RECORD_DONE:
2011 if (is_raw(&remote))
2012 return STS_BTN_ALL_DONE;
2013 if (!resethw(btn_state->started_as_root)) {
2014 btn_state_set_message(btn_state,
2015 "Could not reset hardware.");
2016 return STS_BTN_HARD_ERROR;
2017 }
2018 return STS_BTN_BUTTONS_DONE;
2019 case STS_BTN_BUTTONS_DONE:
2020 f = fopen(opts->tmpfile, "r");
2021 if (f == NULL) {
2022 btn_state_set_message(btn_state,
2023 "Could not reopen config file");
2024 return STS_BTN_HARD_ERROR;
2025 }
2026 my_remote = read_config(f, opts->filename);
2027 fclose(f);
2028 if (my_remote == NULL) {
2029 btn_state_set_message(
2030 btn_state,
2031 "Internal error: "
2032 "config file contains no valid remote");
2033 return STS_BTN_HARD_ERROR;
2034 }
2035 if (my_remote == (void*)-1) {
2036 btn_state_set_message(
2037 btn_state,
2038 "Internal error: "
2039 "Reading of config file failed");
2040 return STS_BTN_HARD_ERROR;
2041 }
2042 sts = STS_BTN_ALL_DONE;
2043 if (opts->force) {
2044 remote = *my_remote;
2045 return sts;
2046 }
2047 if (!has_toggle_bit_mask(my_remote)) {
2048 if (!opts->using_template
2049 && strcmp(curr_driver->name, "devinput") != 0) {
2050 remote = *(my_remote);
2051 sts = STS_BTN_GET_TOGGLE_BITS;
2052 }
2053 } else {
2054 set_toggle_bit_mask(my_remote,
2055 my_remote->toggle_bit_mask);
2056 if (curr_driver->deinit_func)
2057 curr_driver->deinit_func();
2058 }
2059 if (!opts->update) {
2060 get_pre_data(my_remote);
2061 get_post_data(my_remote);
2062 }
2063 remote = *my_remote;
2064 return sts;
2065 case STS_BTN_BUTTON_DONE:
2066 return STS_BTN_BUTTON_DONE;
2067 case STS_BTN_HARD_ERROR:
2068 return STS_BTN_HARD_ERROR;
2069 default:
2070 btn_state_set_message(btn_state,
2071 "record_buttons(): bad state: %d\n",
2072 last_status);
2073 return STS_BTN_HARD_ERROR;
2074 }
2075 }
2076
2077
2079 void config_file_setup(struct main_state* state, const struct opts* opts)
2080 {
2081 state->fout = fopen(opts->tmpfile, "w");
2082 if (state->fout == NULL) {
2083 log_error("Could not open new config file %s", tmpfile);
2084 log_perror_err("While opening temporary file for write");
2085 return;
2086 }
2087 fprint_copyright(state->fout);
2088 fprint_comment(state->fout, &remote, opts->commandline);
2089 fprint_remote_head(state->fout, &remote);
2090 fprint_remote_signal_head(state->fout, &remote);
2091 }
2092
2093
2094
2096 int config_file_finish(struct main_state* state, const struct opts* opts)
2097 {
2098 state->fout = fopen(opts->filename, "w");
2099 if (state->fout == NULL) {
2100 log_perror_err("While opening \"%s\" for write",
2101 opts->filename);
2102 return 0;
2103 }
2104 fprint_copyright(state->fout);
2105 fprint_remotes(state->fout, &remote, opts->commandline);
2106 return 1;
2107 }
ir_remote::min_remaining_gap
lirc_t min_remaining_gap
Definition: ir_remote_types.h:244
rec_buffer_init
</pre >< h3 > Syntax and semantics of the functions</h3 >< p > Note even if implementing the functions in there is no need to declare them as< code > external C</code >< h4 >< code > init_func</code ></h4 >< code > int the functions< code > rec_buffer_init(void)</code > and/or< code >send_buffer_init(void)</code > needs to be called.</p >< h4 >< code >deinit_func</code ></h4 >< code >int mydeinit_func(void)</code >< p >Function called for suspending the driver. Zero return value indicates failure
last_remote
struct ir_remote * last_remote
Definition: ir_remote.c:58
ir_remote
Definition: ir_remote_types.h:167
ir_remote::bits
int bits
Definition: ir_remote_types.h:171
default_close
int default_close(void)
Definition: driver.c:79
button_state::message
char message[128]
Definition: irrecord.h:239
RC6
#define RC6
Definition: ir_remote_types.h:123
ir_code_node
Definition: ir_remote_types.h:65
toggle_state
Definition: irrecord.h:219
opts
Definition: irrecord.h:152
ir_remote::freq
unsigned int freq
Definition: ir_remote_types.h:220
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::plead
lirc_t plead
Definition: ir_remote_types.h:191
log_debug
#define log_debug(fmt,...)
Definition: lirc_log.h:124
driver::decode_func
int(*const decode_func)(struct ir_remote *remote, struct decode_ctx_t *ctx)
Definition: driver.h:197
ir_ncode::next
struct ir_code_node * next
Definition: ir_remote_types.h:91
ir_remote::name
const char * name
Definition: ir_remote_types.h:168
SPACE_ENC
#define SPACE_ENC
Definition: ir_remote_types.h:125
ir_ncode::signals
lirc_t * signals
Definition: ir_remote_types.h:87
ir_remote::eps
int eps
Definition: ir_remote_types.h:173
lengths_state::keypresses
int keypresses
Definition: irrecord.h:202
gap_state
Definition: irrecord.h:184
log_warn
#define log_warn(fmt,...)
Definition: lirc_log.h:109
button_state
Definition: irrecord.h:235
receive_decode
int receive_decode(struct ir_remote *remote, struct decode_ctx_t *ctx)
Definition: receive.c:1041
ir_code
__u64 ir_code
Definition: ir_remote_types.h:59
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
PACKET_SIZE
#define PACKET_SIZE
Definition: lirc_config.h:98
logchannel_t
logchannel_t
Definition: lirc_log.h:53
driver::code_length
const __u32 code_length
Definition: driver.h:155
ir_ncode::name
char * name
Definition: ir_remote_types.h:78
ir_remote::post_data_bits
int post_data_bits
Definition: ir_remote_types.h:198
ir_remote::toggle_mask
ir_code toggle_mask
Definition: ir_remote_types.h:222
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
driver::rec_func
char *(*const rec_func)(struct ir_remote *remotes)
Definition: driver.h:192
ir_remote::sone
lirc_t sone
Definition: ir_remote_types.h:189
RC5
#define RC5
Definition: ir_remote_types.h:117
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
driver::deinit_func
int(*const deinit_func)(void)
Definition: driver.h:175
lirc_private.h
Main include file for lirc applications.
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
CONST_LENGTH
#define CONST_LENGTH
Definition: ir_remote_types.h:137
NO_HEAD_REP
#define NO_HEAD_REP
Definition: ir_remote_types.h:135
button_state::ncode
struct ir_ncode ncode
Definition: irrecord.h:237
ir_remote::pre_s
lirc_t pre_s
Definition: ir_remote_types.h:200
lengths_state::sum
lirc_t sum
Definition: irrecord.h:210
ir_remote::szero
lirc_t szero
Definition: ir_remote_types.h:190
driver
Definition: driver.h:127
main_state
Definition: irrecord.h:176
ir_ncode
Definition: ir_remote_types.h:76
lengths
Definition: irrecord.h:141
ir_remote::flags
int flags
Definition: ir_remote_types.h:172
ir_remote::aeps
unsigned int aeps
Definition: ir_remote_types.h:174
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
drop_sudo_root
const char * drop_sudo_root(int(*set_some_uid)(uid_t))
Definition: lirc-utils.c:35
ir_remote::max_remaining_gap
lirc_t max_remaining_gap
Definition: ir_remote_types.h:245
driver::name
const char * name
Definition: driver.h:219
default_open
int default_open(const char *path)
Definition: driver.c:64
driver::init_func
int(*const init_func)(void)
Definition: driver.h:169
ir_ncode::code
ir_code code
Definition: ir_remote_types.h:81
driver::rec_mode
__u32 rec_mode
Definition: driver.h:152
lengths_state::count
int count
Definition: irrecord.h:205
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
log_info
#define log_info(fmt,...)
Definition: lirc_log.h:114
ir_remote::toggle_bit_mask
ir_code toggle_bit_mask
Definition: ir_remote_types.h:207
lengths_state
Definition: irrecord.h:198
rec_buffer_clear
int rec_buffer_clear(void)
Definition: receive.c:224
ir_remote::min_repeat
int min_repeat
Definition: ir_remote_types.h:211
read_config
struct ir_remote * read_config(FILE *f, const char *name)
Definition: config_file.c:832
log_notice
#define log_notice(fmt,...)
Definition: lirc_log.h:119
ir_ncode::length
int length
Definition: ir_remote_types.h:84