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