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