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