22 static lirc_t emulation_readdata(lirc_t timeout);
25 static const struct driver hw_emulation = {
27 .device =
"/dev/null",
28 .features = LIRC_CAN_REC_MODE2,
30 .rec_mode = LIRC_MODE_MODE2,
38 .readdata = emulation_readdata,
42 .driver_version =
"0.9.2"
45 static const int IR_CODE_NODE_SIZE =
sizeof(
struct ir_code_node);
50 unsigned int eps = 30;
55 static lirc_t signals[MAX_SIGNALS];
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;
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;
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;
85 void btn_state_set_message(
struct button_state* state,
const char* fmt, ...)
95 static void fprint_copyright(FILE* fout)
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");
106 int availabledata(
void)
108 struct pollfd pfd = {
114 ret = poll(&pfd, 1, 0);
115 }
while (ret == -1 && errno == EINTR);
117 logperror(LIRC_ERROR,
"availabledata: poll() failed");
122 return pfd.revents & POLLIN ? 1 : 0;
133 case LIRC_MODE_MODE2:
134 while (availabledata())
137 case LIRC_MODE_LIRCCODE:
147 static uid_t getresuid_uid(
void)
149 uid_t ruid, euid, suid;
151 getresuid(&ruid, &euid, &suid);
160 if (getresuid_uid() == 0)
161 if (seteuid(0) == -1)
162 logprintf(LIRC_ERROR,
"Cannot reset root uid");
184 void gap_state_init(
struct gap_state* state)
186 memset(state, 0,
sizeof(
struct gap_state));
201 state->first_signal = -1;
210 state->retval = EXIT_SUCCESS;
217 state->retval = EXIT_SUCCESS;
221 static lirc_t calc_signal(
struct lengths* len)
225 return (lirc_t)(len->sum / len->count);
238 bits = bit_count(remote);
239 mask = ((
ir_code)1) << (bits - 1);
248 codes = remote->codes;
249 while (codes->
name != NULL) {
255 else if (bits == 15 && xor == 0x3ff) {
263 void get_pre_data(
struct ir_remote* remote)
270 if (remote->
bits == 0)
273 codes = remote->codes;
274 if (codes->
name == NULL)
278 if (codes->
name == NULL)
280 while (codes->
name != NULL) {
281 mask &= ~(last ^ codes->
code);
283 for (n = codes->
next; n != NULL; n = n->next) {
284 mask &= ~(last ^ n->code);
290 while (mask & 0x8000000000000000LL) {
294 count -=
sizeof(
ir_code) * CHAR_BIT - remote->
bits;
297 if (count % 8 && (remote->
bits - count) % 8)
301 for (i = 0; i < count; i++) {
305 remote->
bits -= count;
306 mask = mask << (remote->
bits);
310 codes = remote->codes;
311 while (codes->
name != NULL) {
312 codes->
code &= ~mask;
313 for (n = codes->
next; n != NULL; n = n->next)
321 void get_post_data(
struct ir_remote* remote)
328 if (remote->
bits == 0)
332 codes = remote->codes;
333 if (codes->
name == NULL)
337 if (codes->
name == NULL)
339 while (codes->
name != NULL) {
340 mask &= ~(last ^ codes->
code);
342 for (n = codes->
next; n != NULL; n = n->next) {
343 mask &= ~(last ^ n->code);
354 if (count % 8 && (remote->
bits - count) % 8)
358 for (i = 0; i < count; i++) {
362 remote->
bits -= count;
366 codes = remote->codes;
367 while (codes->
name != NULL) {
369 for (n = codes->
next; n != NULL; n = n->next)
370 n->code = n->code >> count;
377 void remove_pre_data(
struct ir_remote* remote)
383 || remote->pre_p != 0
384 || remote->
pre_s != 0)
386 for (codes = remote->codes; codes->
name != NULL; codes++) {
388 for (n = codes->
next; n != NULL; n = n->next)
397 void remove_post_data(
struct ir_remote* remote)
404 for (codes = remote->codes; codes->
name != NULL; codes++) {
407 for (n = codes->
next; n != NULL; n = n->next) {
418 void invert_data(
struct ir_remote* remote)
428 remote->pone = remote->pzero;
434 if (has_pre(remote)) {
439 if (has_post(remote)) {
444 if (remote->
bits == 0)
448 mask = gen_mask(remote->
bits);
449 for (codes = remote->codes; codes->
name != NULL; codes++) {
451 for (n = codes->
next; n != NULL; n = n->next)
457 void remove_trail(
struct ir_remote* remote)
461 if (!is_space_enc(remote))
465 if (expect(remote, remote->pone, remote->pzero)
466 || expect(remote, remote->pzero, remote->pone))
468 if (!(expect(remote, remote->
sone, remote->
szero)
469 && expect(remote, remote->
szero, remote->
sone)))
471 if (expect(remote, remote->
ptrail, remote->pone))
473 else if (expect(remote, remote->
ptrail, remote->pzero))
485 void for_each_remote(
struct ir_remote* remotes, remote_func func)
490 while (remote != NULL) {
492 remote = remote->next;
497 static int mywaitfordata(__u32 maxusec)
499 struct pollfd pfd = {
506 ret = poll(&pfd, 1, maxusec / 1000);
507 }
while (ret == -1 && errno == EINTR);
510 "mywaitfordata: poll() failed");
514 if (pfd.revents & POLLIN)
521 static lirc_t emulation_readdata(lirc_t timeout)
523 static lirc_t sum = 0;
526 if (current_code == NULL) {
529 current_code = next_code;
531 current_code = emulation_data->codes;
535 if (current_code->
name == NULL) {
536 logprintf(LIRC_WARNING,
537 "%s: no data found", emulation_data->
name);
540 if (current_index >= current_code->
length) {
542 current_code = next_code;
545 if (current_rep > 2) {
552 if (current_code->
name == NULL) {
554 return emulation_readdata(timeout);
557 if (is_const(emulation_data))
558 data = emulation_data->
gap - sum;
560 data = emulation_data->
gap;
565 data = current_code->
signals[current_index];
566 if ((current_index % 2) == 0)
569 sum += data & PULSE_MASK;
580 static struct lengths* new_length(lirc_t length)
584 l = malloc(
sizeof(
struct lengths));
589 l->lower_bound = length / 100 * 100;
590 l->upper_bound = length / 100 * 100 + 99;
591 l->min = l->max = length;
597 void unlink_length(
struct lengths** first,
struct lengths*
remove)
602 if (
remove == *first) {
603 *first =
remove->next;
607 scan = (*first)->next;
610 if (scan ==
remove) {
611 last->next =
remove->next;
618 logprintf(LIRC_ERROR,
"unlink_length(): report this bug!");
622 int add_length(
struct lengths** first, lirc_t length)
627 if (*first == NULL) {
628 *first = new_length(length);
635 if (l->lower_bound <= length && length <= l->upper_bound) {
638 l->min = min(l->min, length);
639 l->max = max(l->max, length);
645 last->next = new_length(length);
646 if (last->next == NULL)
652 void free_lengths(
struct lengths** firstp)
660 while (first != NULL) {
669 void free_all_lengths(
void)
671 free_lengths(&first_space);
672 free_lengths(&first_pulse);
673 free_lengths(&first_sum);
674 free_lengths(&first_gap);
675 free_lengths(&first_repeat_gap);
676 free_lengths(&first_signal_length);
677 free_lengths(&first_headerp);
678 free_lengths(&first_headers);
679 free_lengths(&first_1lead);
680 free_lengths(&first_3lead);
681 free_lengths(&first_trail);
682 free_lengths(&first_repeatp);
683 free_lengths(&first_repeats);
687 static void merge_lengths(
struct lengths* first)
699 while (inner != NULL) {
700 new_sum = l->sum + inner->sum;
701 new_count = l->count + inner->count;
703 if ((l->max <= new_sum / new_count + aeps
704 && l->min + aeps >= new_sum / new_count
705 && inner->max <= new_sum / new_count + aeps
706 && inner->min + aeps >= new_sum / new_count)
707 || (l->max <= new_sum / new_count * (100 + eps)
708 && l->min >= new_sum / new_count * (100 - eps)
709 && inner->max <= new_sum / new_count *
711 && inner->min >= new_sum / new_count *
714 l->count = new_count;
715 l->upper_bound = max(l->upper_bound,
717 l->lower_bound = min(l->lower_bound,
719 l->min = min(l->min, inner->min);
720 l->max = max(l->max, inner->max);
722 last->next = inner->next;
731 for (l = first; l != NULL; l = l->next) {
732 logprintf(LIRC_DEBUG,
"%d x %u [%u,%u]",
733 l->count, (__u32)calc_signal(l),
734 (__u32)l->min, (__u32)l->max);
751 if (first->count > 0)
752 logprintf(LIRC_DEBUG,
"%u x %u", first->count,
753 (__u32)calc_signal(first));
756 if (scan->count > max_length->count)
759 logprintf(LIRC_DEBUG,
762 (__u32)calc_signal(scan));
771 int get_trail_length(
struct ir_remote* remote,
int interactive)
773 unsigned int sum = 0, max_count;
776 if (is_biphase(remote))
779 max_length = get_max_length(first_trail, &sum);
780 max_count = max_length->count;
781 logprintf(LIRC_DEBUG,
782 "get_trail_length(): sum: %u, max_count %u",
784 if (max_count >= sum * TH_TRAIL / 100) {
785 logprintf(LIRC_DEBUG,
"Found trail pulse: %lu",
786 (__u32)calc_signal(max_length));
787 remote->
ptrail = calc_signal(max_length);
790 logprintf(LIRC_DEBUG,
"No trail pulse found.");
795 int get_lead_length(
struct ir_remote* remote,
int interactive)
797 unsigned int sum = 0, max_count;
803 if (!is_biphase(remote) || has_header(remote))
808 first_lead = has_header(remote) ? first_3lead : first_1lead;
809 max_length = get_max_length(first_lead, &sum);
810 max_count = max_length->count;
811 logprintf(LIRC_DEBUG,
812 "get_lead_length(): sum: %u, max_count %u",
814 if (max_count >= sum * TH_LEAD / 100) {
815 logprintf(LIRC_DEBUG,
816 "Found lead pulse: %lu",
817 (__u32)calc_signal(max_length));
818 remote->
plead = calc_signal(max_length);
821 unlink_length(&first_lead, max_length);
822 max2_length = get_max_length(first_lead, &sum);
823 max_length->next = first_lead;
824 first_lead = max_length;
826 a = calc_signal(max_length);
827 b = calc_signal(max2_length);
833 if (abs(2 * a - b) < b * eps / 100 || abs(2 * a - b) < aeps) {
834 logprintf(LIRC_DEBUG,
835 "Found hidden lead pulse: %lu",
840 logprintf(LIRC_DEBUG,
"No lead pulse found.");
845 int get_header_length(
struct ir_remote* remote,
int interactive)
847 unsigned int sum, max_count;
848 lirc_t headerp, headers;
852 if (first_headerp != NULL) {
853 max_plength = get_max_length(first_headerp, &sum);
854 max_count = max_plength->count;
856 logprintf(LIRC_DEBUG,
"No header data.");
859 logprintf(LIRC_DEBUG,
860 "get_header_length(): sum: %u, max_count %u",
863 if (max_count >= sum * TH_HEADER / 100) {
864 max_slength = get_max_length(first_headers, &sum);
865 max_count = max_slength->count;
866 logprintf(LIRC_DEBUG,
867 "get_header_length(): sum: %u, max_count %u",
869 if (max_count >= sum * TH_HEADER / 100) {
870 headerp = calc_signal(max_plength);
871 headers = calc_signal(max_slength);
873 logprintf(LIRC_DEBUG,
874 "Found possible header: %lu %lu",
877 remote->phead = headerp;
878 remote->
shead = headers;
879 if (first_lengths < second_lengths) {
880 logprintf(LIRC_DEBUG,
881 "Header is not being repeated.");
887 logprintf(LIRC_DEBUG,
"No header found.");
892 int get_repeat_length(
struct ir_remote* remote,
int interactive)
894 unsigned int sum = 0, max_count;
895 lirc_t repeatp, repeats, repeat_gap;
899 if (!((count_3repeats > SAMPLES / 2 ? 1 : 0) ^
900 (count_5repeats > SAMPLES / 2 ? 1 : 0))) {
901 if (count_3repeats > SAMPLES / 2
902 || count_5repeats > SAMPLES / 2) {
903 logprintf(LIRC_WARNING,
"Repeat inconsistency.");
906 logprintf(LIRC_DEBUG,
"No repeat code found.");
910 max_plength = get_max_length(first_repeatp, &sum);
911 max_count = max_plength->count;
912 logprintf(LIRC_DEBUG,
913 "get_repeat_length(): sum: %u, max_count %u",
915 if (max_count >= sum * TH_REPEAT / 100) {
916 max_slength = get_max_length(first_repeats, &sum);
917 max_count = max_slength->count;
918 logprintf(LIRC_DEBUG,
919 "get_repeat_length(): sum: %u, max_count %u",
921 if (max_count >= sum * TH_REPEAT / 100) {
922 if (count_5repeats > count_3repeats
923 && !has_header(remote)) {
924 logprintf(LIRC_WARNING,
925 "Repeat code has header,"
926 " but no header found!");
929 if (count_5repeats > count_3repeats
930 && has_header(remote))
932 repeatp = calc_signal(max_plength);
933 repeats = calc_signal(max_slength);
935 logprintf(LIRC_DEBUG,
936 "Found repeat code: %lu %lu",
939 remote->prepeat = repeatp;
942 max_slength = get_max_length(first_repeat_gap,
944 repeat_gap = calc_signal(max_slength);
945 logprintf(LIRC_DEBUG,
946 "Found repeat gap: %lu",
953 logprintf(LIRC_DEBUG,
"No repeat header found.");
958 void get_scheme(
struct ir_remote* remote,
int interactive)
960 unsigned int i, length = 0, sum = 0;
966 for (i = 1; i < MAX_SIGNALS; i++) {
971 logprintf(LIRC_DEBUG,
"%u: %u", i,
lengths[i]);
973 logprintf(LIRC_DEBUG,
"get_scheme(): sum: %u length: %u signals: %u"
974 " first_lengths: %u second_lengths: %u\n",
975 sum, length + 1,
lengths[length],
976 first_lengths, second_lengths);
978 if (
lengths[length] >= TH_SPACE_ENC * sum / 100) {
980 logprintf(LIRC_DEBUG,
981 "Space/pulse encoded remote control found.");
982 logprintf(LIRC_DEBUG,
"Signal length is %u.", length);
984 remote->
bits = length;
988 maxp = get_max_length(first_pulse, NULL);
989 unlink_length(&first_pulse, maxp);
990 if (first_pulse == NULL)
992 max2p = get_max_length(first_pulse, NULL);
993 maxp->next = first_pulse;
996 maxs = get_max_length(first_space, NULL);
997 unlink_length(&first_space, maxs);
998 if (first_space == NULL) {
1001 max2s = get_max_length(first_space, NULL);
1002 maxs->next = first_space;
1005 maxs = get_max_length(first_space, NULL);
1008 && (calc_signal(maxp) < TH_RC6_SIGNAL
1009 || calc_signal(max2p) < TH_RC6_SIGNAL)
1010 && (calc_signal(maxs) < TH_RC6_SIGNAL
1011 || calc_signal(max2s) < TH_RC6_SIGNAL)) {
1012 logprintf(LIRC_DEBUG,
"RC-6 remote control found.");
1013 set_protocol(remote,
RC6);
1015 logprintf(LIRC_DEBUG,
"RC-5 remote control found.");
1016 set_protocol(remote,
RC5);
1021 logprintf(LIRC_DEBUG,
"Suspicious data length: %u.", length);
1023 remote->
bits = length;
1028 int get_data_length(
struct ir_remote* remote,
int interactive)
1030 unsigned int sum = 0, max_count;
1031 lirc_t p1, p2, s1, s2;
1037 max_plength = get_max_length(first_pulse, &sum);
1038 max_count = max_plength->count;
1039 logprintf(LIRC_DEBUG,
"get_data_length(): sum: %u, max_count %u",
1042 if (max_count >= sum * TH_IS_BIT / 100) {
1043 unlink_length(&first_pulse, max_plength);
1045 max2_plength = get_max_length(first_pulse, NULL);
1046 if (max2_plength != NULL)
1047 if (max2_plength->count < max_count * TH_IS_BIT / 100)
1048 max2_plength = NULL;
1049 logprintf(LIRC_DEBUG,
"Pulse candidates: ");
1050 logprintf(LIRC_DEBUG,
"%u x %u", max_plength->count,
1051 (__u32)calc_signal(max_plength));
1053 logprintf(LIRC_DEBUG,
", %u x %u",
1054 max2_plength->count,
1055 (__u32)calc_signal(max2_plength));
1057 max_slength = get_max_length(first_space, &sum);
1058 max_count = max_slength->count;
1059 logprintf(LIRC_DEBUG,
1060 "get_data_length(): sum: %u, max_count %u",
1062 if (max_count >= sum * TH_IS_BIT / 100) {
1063 unlink_length(&first_space, max_slength);
1065 max2_slength = get_max_length(first_space, NULL);
1066 if (max2_slength != NULL)
1067 if (max2_slength->count <
1068 max_count * TH_IS_BIT / 100)
1069 max2_slength = NULL;
1070 if (max_count >= sum * TH_IS_BIT / 100) {
1071 logprintf(LIRC_DEBUG,
"Space candidates: ");
1072 logprintf(LIRC_DEBUG,
1075 (__u32)calc_signal(max_slength));
1080 max2_slength->count,
1081 (__u32)calc_signal(max2_slength));
1085 remote->
aeps = aeps;
1086 if (is_biphase(remote)) {
1087 if (max2_plength == NULL
1088 || max2_slength == NULL) {
1089 logprintf(LIRC_NOTICE,
1090 "Unknown encoding found.");
1093 logprintf(LIRC_DEBUG,
1094 "Signals are biphase encoded.");
1095 p1 = calc_signal(max_plength);
1096 p2 = calc_signal(max2_plength);
1097 s1 = calc_signal(max_slength);
1098 s2 = calc_signal(max2_slength);
1101 (min(p1, p2) + max(p1, p2) / 2) / 2;
1103 (min(s1, s2) + max(s1, s2) / 2) / 2;
1104 remote->pzero = remote->pone;
1107 if (max2_plength == NULL
1108 && max2_slength == NULL) {
1109 logprintf(LIRC_NOTICE,
1110 "No encoding found");
1113 if (max2_plength && max2_slength) {
1114 logprintf(LIRC_NOTICE,
1115 "Unknown encoding found.");
1118 p1 = calc_signal(max_plength);
1119 s1 = calc_signal(max_slength);
1121 p2 = calc_signal(max2_plength);
1124 "Signals are pulse encoded.");
1125 remote->pone = max(p1, p2);
1127 remote->pzero = min(p1, p2);
1129 if (expect(remote, remote->
ptrail, p1)
1130 || expect(remote, remote->
ptrail,
1134 s2 = calc_signal(max2_slength);
1137 "Signals are space encoded.");
1139 remote->
sone = max(s1, s2);
1141 remote->
szero = min(s1, s2);
1144 if (has_header(remote)
1145 && (!has_repeat(remote)
1147 if (!is_biphase(remote)
1148 && ((expect(remote, remote->phead,
1159 remote->phead = remote->
shead = 0;
1161 logprintf(LIRC_DEBUG,
1164 if (is_biphase(remote)
1168 remote->
plead = remote->phead;
1169 remote->phead = remote->
shead = 0;
1171 logprintf(LIRC_DEBUG,
1175 if (is_biphase(remote)) {
1176 struct lengths* signal_length;
1180 get_max_length(first_signal_length,
1183 calc_signal(signal_length) -
1184 remote->
plead - remote->phead -
1187 (remote->pone + remote->
sone) / 2;
1188 remote->
bits = data_length / (remote->pone +
1195 (has_header(remote) ? 2 : 0) + 1 -
1196 (remote->
ptrail > 0 ? 2 : 0)) / 2;
1198 logprintf(LIRC_DEBUG,
1199 "Signal length is %d",
1201 free_lengths(&max_plength);
1202 free_lengths(&max_slength);
1205 free_lengths(&max_plength);
1207 logprintf(LIRC_NOTICE,
"Could not find data lengths.");
1212 enum get_gap_status get_gap_length(
struct gap_state* state,
1215 while (availabledata())
1217 if (!mywaitfordata(10000000)) {
1218 free_lengths(&(state->gaps));
1219 return STS_GAP_TIMEOUT;
1221 gettimeofday(&(state->start), NULL);
1222 while (availabledata())
1224 gettimeofday(&(state->end), NULL);
1226 state->gap = time_elapsed(&(state->last), &(state->start));
1227 add_length(&(state->gaps), state->gap);
1228 merge_lengths(state->gaps);
1229 state->maxcount = 0;
1230 state->scan = state->gaps;
1231 while (state->scan) {
1232 state->maxcount = max(state->maxcount,
1233 state->scan->count);
1234 if (state->scan->count > SAMPLES) {
1235 remote->
gap = calc_signal(state->scan);
1236 free_lengths(&(state->gaps));
1237 return STS_GAP_FOUND;
1239 state->scan = state->scan->next;
1241 if (state->maxcount > state->lastmaxcount) {
1242 state->lastmaxcount = state->maxcount;
1243 return STS_GAP_GOT_ONE_PRESS;
1248 state->last = state->end;
1249 return STS_GAP_AGAIN;
1254 int needs_toggle_mask(
struct ir_remote* remote)
1258 if (!is_rc6(remote))
1260 if (remote->codes) {
1261 codes = remote->codes;
1262 while (codes->
name != NULL) {
1275 static void compute_lengths_4_signals(
void)
1277 add_length(&first_repeatp,
signals[0]);
1278 merge_lengths(first_repeatp);
1279 add_length(&first_repeats,
signals[1]);
1280 merge_lengths(first_repeats);
1281 add_length(&first_trail,
signals[2]);
1282 merge_lengths(first_trail);
1283 add_length(&first_repeat_gap,
signals[3]);
1284 merge_lengths(first_repeat_gap);
1289 static void compute_lengths_6_signals(
void)
1291 add_length(&first_headerp,
signals[0]);
1292 merge_lengths(first_headerp);
1293 add_length(&first_headers,
signals[1]);
1294 merge_lengths(first_headers);
1295 add_length(&first_repeatp,
signals[2]);
1296 merge_lengths(first_repeatp);
1297 add_length(&first_repeats,
signals[3]);
1298 merge_lengths(first_repeats);
1299 add_length(&first_trail,
signals[4]);
1300 merge_lengths(first_trail);
1301 add_length(&first_repeat_gap,
signals[5]);
1302 merge_lengths(first_repeat_gap);
1306 static void compute_lengths_many_signals(
struct lengths_state* state)
1310 merge_lengths(first_1lead);
1311 for (i = 2; i < state->count - 2; i++) {
1313 add_length(&first_space,
signals[i]);
1314 merge_lengths(first_space);
1316 add_length(&first_pulse,
signals[i]);
1317 merge_lengths(first_pulse);
1320 add_length(&first_trail,
signals[state->count - 2]);
1321 merge_lengths(first_trail);
1323 add_length(&first_signal_length, state->sum - state->data);
1324 merge_lengths(first_signal_length);
1325 if (state->first_signal == 1
1326 || (first_length > 2
1327 && first_length - 2 != state->count - 2)) {
1328 add_length(&first_3lead,
signals[2]);
1329 merge_lengths(first_3lead);
1330 add_length(&first_headerp,
signals[0]);
1331 merge_lengths(first_headerp);
1332 add_length(&first_headers,
signals[1]);
1333 merge_lengths(first_headers);
1335 if (state->first_signal == 1) {
1337 first_length = state->count - 2;
1339 }
else if (state->first_signal == 0
1340 && first_length - 2 == state->count - 2) {
1342 lengths[state->count - 2 + 2]++;
1351 enum lengths_status* again)
1355 for (scan = first_sum; scan; scan = scan->next) {
1356 *maxcount = max(*maxcount, scan->count);
1357 if (scan->count > SAMPLES) {
1358 remote->
gap = calc_signal(scan);
1360 state->mode = MODE_HAVE_GAP;
1361 logprintf(LIRC_DEBUG,
"Found gap: %u", remote->
gap);
1362 *again = STS_LEN_AGAIN_INFO;
1373 enum lengths_status* again)
1377 for (scan = first_gap; scan; scan = scan->next) {
1378 *maxcount = max(*maxcount, scan->count);
1379 if (scan->count > SAMPLES) {
1380 remote->
gap = calc_signal(scan);
1381 state->mode = MODE_HAVE_GAP;
1382 logprintf(LIRC_DEBUG,
"Found gap: %u", remote->
gap);
1383 *again = STS_LEN_AGAIN_INFO;
1391 enum lengths_status get_lengths(
struct lengths_state* state,
1393 int force,
int interactive)
1397 static int lastmaxcount = 0;
1398 enum lengths_status again = STS_LEN_AGAIN;
1403 return STS_LEN_TIMEOUT;
1406 if (state->mode == MODE_GET_GAP) {
1407 state->sum += state->data & PULSE_MASK;
1408 if (state->average == 0 && is_space(state->data)) {
1409 if (state->data > 100000) {
1411 return STS_LEN_AGAIN;
1413 state->average = state->data;
1414 state->maxspace = state->data;
1415 }
else if (is_space(state->data)) {
1416 if (state->data > MIN_GAP
1417 || state->data > 100 * state->average
1419 || (state->data >= 5000 && count_spaces > 10
1420 && state->data > 5 * state->average)
1421 || (state->data < 5000 && count_spaces > 10
1422 && state->data > 5 * state->maxspace / 2)) {
1423 add_length(&first_sum, state->sum);
1424 merge_lengths(first_sum);
1425 add_length(&first_gap, state->data);
1426 merge_lengths(first_gap);
1430 state->maxspace = 0;
1433 scan = scan_gap1(state,
1438 scan = scan_gap2(state,
1444 state->mode = MODE_HAVE_GAP;
1447 state->remaining_gap =
1451 remote->
gap - state->data : 0)
1452 : (has_repeat_gap(remote) ?
1454 repeat_gap : remote->
gap);
1457 return STS_LEN_RAW_OK;
1459 return STS_LEN_AGAIN_INFO;
1461 lastmaxcount = maxcount;
1466 (state->average * count_spaces + state->data)
1467 / (count_spaces + 1);
1469 if (state->data > state->maxspace)
1470 state->maxspace = state->data;
1472 if (state->count > SAMPLES * MAX_SIGNALS * 2) {
1474 return STS_LEN_NO_GAP_FOUND;
1477 return STS_LEN_AGAIN;
1478 }
else if (state->mode == MODE_HAVE_GAP) {
1479 if (state->count <= MAX_SIGNALS) {
1480 signals[state->count - 1] = state->data & PULSE_MASK;
1483 return STS_LEN_TOO_LONG;
1485 if (is_const(remote))
1486 state->remaining_gap =
1487 remote->
gap > state->sum ? remote->
gap -
1490 state->remaining_gap = remote->
gap;
1491 state->sum += state->data & PULSE_MASK;
1493 if (state->count > 2
1494 && ((state->data & PULSE_MASK) >=
1495 state->remaining_gap * (100 - eps) / 100
1497 PULSE_MASK) >= state->remaining_gap - aeps)) {
1498 if (is_space(state->data)) {
1501 if (state->count == 4) {
1503 compute_lengths_4_signals();
1504 }
else if (state->count == 6) {
1506 compute_lengths_6_signals();
1507 }
else if (state->count > 6) {
1509 compute_lengths_many_signals(state);
1519 return STS_LEN_NO_GAP_FOUND;
1522 if (count_signals >= SAMPLES) {
1523 get_scheme(remote, interactive);
1524 if (!get_header_length(remote, interactive)
1525 || !get_trail_length(remote, interactive)
1526 || !get_lead_length(remote, interactive)
1527 || !get_repeat_length(remote, interactive)
1528 || !get_data_length(remote, interactive))
1530 return state->retval ==
1531 0 ? STS_LEN_FAIL : STS_LEN_OK;
1533 if ((state->data & PULSE_MASK) <=
1534 (state->remaining_gap + state->header) *
1536 || (state->data & PULSE_MASK) <=
1537 (state->remaining_gap + state->header) + aeps) {
1538 state->first_signal = 0;
1541 state->first_signal = 1;
1545 return STS_LEN_AGAIN;
1549 enum toggle_status get_toggle_bit_mask(
struct toggle_state* state,
1556 if (!state->inited) {
1558 while (availabledata())
1562 if (state->retries <= 0) {
1564 return STS_TGL_NOT_FOUND;
1565 if (state->seq > 0) {
1566 remote->
min_repeat = state->repeats / state->seq;
1567 logprintf(LIRC_DEBUG,
"min_repeat=%d",
1570 return STS_TGL_FOUND;
1572 if (!mywaitfordata(10000000))
1573 return STS_TGL_TIMEOUT;
1575 if (is_rc6(remote) && remote->
rc6_mask == 0) {
1576 for (i = 0, mask = 1; i < remote->
bits; i++, mask <<= 1) {
1580 if (state->success) {
1588 if (!state->success)
1593 if (state->success) {
1600 if (state->success) {
1601 if (state->flag == 0) {
1603 state->first = decode_ctx.
code;
1605 || decode_ctx.
code != state->last) {
1607 mask = state->first ^ decode_ctx.
code;
1608 if (!state->found && mask) {
1609 set_toggle_bit_mask(remote, mask);
1613 state->repeats / state->seq;
1616 state->last = decode_ctx.
code;
1617 return STS_TGL_GOT_ONE_PRESS;
1620 state->last = decode_ctx.
code;
1623 while (availabledata())
1626 return STS_TGL_AGAIN;
1633 enum lengths_status status = STS_LEN_AGAIN;
1635 while (status == STS_LEN_AGAIN) {
1636 status = get_lengths(lengths_state, &remote, 0, 0);
1638 case STS_LEN_AGAIN_INFO:
1639 status = STS_LEN_AGAIN;
1646 logprintf(LIRC_ERROR,
"get_lengths() failure");
1648 case STS_LEN_RAW_OK:
1649 logprintf(LIRC_ERROR,
"raw analyse result?!");
1651 case STS_LEN_TIMEOUT:
1652 logprintf(LIRC_ERROR,
"analyse timeout?!");
1654 case STS_LEN_NO_GAP_FOUND:
1655 logprintf(LIRC_ERROR,
"analyse, no gap?!");
1657 case STS_LEN_TOO_LONG:
1658 logprintf(LIRC_ERROR,
"analyse, signal too long?!");
1661 logprintf(LIRC_ERROR,
1662 "Cannot read raw data (%d)",
1676 struct lengths_state lengths_state;
1680 size_t new_codes_count = 100;
1684 if (!is_raw(raw_data)) {
1685 logprintf(LIRC_ERROR,
1686 "remote %s not in raw mode, ignoring",
1691 aeps = raw_data->
aeps;
1692 eps = raw_data->
eps;
1693 emulation_data = raw_data;
1695 current_code = NULL;
1697 memset(&remote, 0,
sizeof(remote));
1698 lengths_state_init(&lengths_state);
1699 if (!analyse_get_lengths(&lengths_state))
1702 if (is_rc6(&remote) && remote.
bits >= 5)
1710 new_codes = malloc(new_codes_count *
sizeof(*new_codes));
1711 if (new_codes == NULL) {
1712 logprintf(LIRC_ERROR,
"Out of memory");
1715 memset(new_codes, 0, new_codes_count *
sizeof(*new_codes));
1716 codes = raw_data->codes;
1717 while (codes->
name != NULL) {
1719 current_code = NULL;
1727 logprintf(LIRC_WARNING,
1728 "Decoding of %s failed", codes->
name);
1730 if (new_index + 1 >= new_codes_count) {
1733 new_codes_count *= 2;
1737 sizeof(*new_codes));
1738 if (renew_codes == NULL) {
1739 logprintf(LIRC_ERROR,
1744 memset(&new_codes[new_codes_count / 2],
1746 new_codes_count / 2 *
1747 sizeof(*new_codes));
1748 new_codes = renew_codes;
1752 code = decode_ctx.
code;
1754 code2 = decode_ctx.
code;
1756 if (ret && code2 != decode_ctx.
code) {
1757 new_codes[new_index].
next =
1758 malloc(IR_CODE_NODE_SIZE);
1759 if (new_codes[new_index].next) {
1760 memset(new_codes[new_index].next,
1763 new_codes[new_index].
next->code =
1767 new_codes[new_index].
name = codes->
name;
1768 new_codes[new_index].
code = decode_ctx.
code;
1773 new_codes[new_index].
name = NULL;
1774 remote.codes = new_codes;
1775 fprint_remotes(stdout, &remote, opts->commandline);
1776 remote.codes = NULL;
1783 int do_analyse(
const struct opts* opts,
struct main_state* state)
1789 f = fopen(opts->filename,
"r");
1791 fprintf(stderr,
"Cannot open file: %s\n", opts->filename);
1796 fprintf(stderr,
"Cannot parse file: %s\n", opts->filename);
1799 for (; r != NULL; r = r->next) {
1801 logprintf(LIRC_ERROR,
1802 "remote %s not in raw mode, ignoring",
1806 analyse_remote(r, opts);
1812 ssize_t raw_read(
void* buffer,
size_t size,
unsigned int timeout_us)
1814 if (!mywaitfordata(timeout_us))
1825 if (!is_space(btn_state->data)) {
1827 }
else if (is_const(&remote)) {
1828 if (remote.
gap > btn_state->sum) {
1829 ref = (remote.
gap - btn_state->sum);
1830 ref *= (100 - remote.
eps);
1835 r = btn_state->data > ref;
1837 r = btn_state->data > (remote.
gap * (100 - remote.
eps)) / 100;
1843 enum button_status record_buttons(
struct button_state* btn_state,
1844 enum button_status last_status,
1846 const struct opts* opts)
1848 const char*
const MSG_BAD_LENGTH =
1849 "Signal length is %d\n"
1850 "That's weird because the signal length must be odd!\n";
1857 enum button_status sts;
1859 if (btn_state->no_data) {
1860 btn_state->no_data = 0;
1861 return STS_BTN_TIMEOUT;
1863 switch (last_status) {
1865 return STS_BTN_GET_NAME;
1866 case STS_BTN_GET_NAME:
1867 if (strchr(btn_state->buffer,
' ') != NULL) {
1868 btn_state_set_message(
1870 "The name must not contain any whitespace.");
1871 return STS_BTN_SOFT_ERROR;
1873 if (strchr(btn_state->buffer,
'\t') != NULL) {
1874 btn_state_set_message(
1876 "The name must not contain any whitespace.");
1877 return STS_BTN_SOFT_ERROR;
1879 if (strcasecmp(btn_state->buffer,
"begin") == 0) {
1880 btn_state_set_message(
1882 "'%s' is not allowed as button name\n",
1884 return STS_BTN_SOFT_ERROR;
1886 if (strcasecmp(btn_state->buffer,
"end") == 0) {
1887 btn_state_set_message(
1889 "'%s' is not allowed as button name\n",
1891 return STS_BTN_SOFT_ERROR;
1893 if (strlen(btn_state->buffer) == 0)
1894 return STS_BTN_RECORD_DONE;
1895 if (!opts->disable_namespace
1896 && !is_in_namespace(btn_state->buffer)) {
1897 btn_state_set_message(
1899 "'%s' is not in name space"
1900 " (use --disable-namespace to override)\n",
1902 return STS_BTN_SOFT_ERROR;
1904 return STS_BTN_INIT_DATA;
1905 case STS_BTN_INIT_DATA:
1909 while (availabledata())
1913 return opts->force ? STS_BTN_GET_RAW_DATA : STS_BTN_GET_DATA;
1914 case STS_BTN_GET_DATA:
1915 for (retries = RETRIES; retries > 0; ) {
1916 if (!mywaitfordata(10000000)) {
1917 btn_state->no_data = 1;
1918 return STS_BTN_TIMEOUT;
1923 while (availabledata()) {
1927 &(state->decode_ctx))) {
1934 btn_state_set_message(
1936 "Try using the -f option.\n");
1937 return STS_BTN_HARD_ERROR;
1940 btn_state_set_message(
1942 "Could not reset hardware.\n");
1943 return STS_BTN_HARD_ERROR;
1945 btn_state_set_message(
1947 "Try again (%d retries left).\n",
1951 return STS_BTN_SOFT_ERROR;
1953 btn_state->
ncode.
name = btn_state->buffer;
1957 &(state->decode_ctx))) {
1958 code2 = state->decode_ctx.
code;
1960 if (state->decode_ctx.
code != code2) {
1962 malloc(IR_CODE_NODE_SIZE);
1974 return STS_BTN_BUTTON_DONE;
1975 case STS_BTN_GET_RAW_DATA:
1976 btn_state->count = 0;
1978 while (btn_state->count < MAX_SIGNALS) {
1979 if (btn_state->count == 0)
1982 timeout = remote.
gap * 5;
1984 if (!btn_state->data) {
1985 if (btn_state->count == 0)
1986 return STS_BTN_TIMEOUT;
1987 btn_state->data = remote.
gap;
1989 if (btn_state->count == 0) {
1990 if (!is_space(btn_state->data)
1991 || btn_state->data <
1996 btn_state->count = 0;
1997 btn_state_set_message(
1999 "Something went wrong.");
2000 return STS_BTN_SOFT_ERROR;
2003 if (raw_data_ok(btn_state)) {
2004 logprintf(LIRC_INFO,
"Got it.\n");
2005 logprintf(LIRC_INFO,
2006 "Signal length is %d\n",
2007 btn_state->count - 1);
2008 if (btn_state->count % 2) {
2009 btn_state_set_message(
2012 btn_state->count - 1);
2015 btn_state->count = 0;
2016 return STS_BTN_SOFT_ERROR;
2021 btn_state->count - 1;
2025 signals[btn_state->count - 1] =
2026 btn_state->data & PULSE_MASK;
2028 btn_state->data & PULSE_MASK;
2032 if (btn_state->count == MAX_SIGNALS) {
2033 btn_state_set_message(btn_state,
2034 "Signal is too long.\n");
2035 return STS_BTN_SOFT_ERROR;
2037 return STS_BTN_BUTTON_DONE;
2038 case STS_BTN_RECORD_DONE:
2039 if (is_raw(&remote))
2040 return STS_BTN_ALL_DONE;
2042 btn_state_set_message(btn_state,
2043 "Could not reset hardware.");
2044 return STS_BTN_HARD_ERROR;
2046 return STS_BTN_BUTTONS_DONE;
2047 case STS_BTN_BUTTONS_DONE:
2048 f = fopen(opts->tmpfile,
"r");
2050 btn_state_set_message(btn_state,
2051 "Could not reopen config file");
2052 return STS_BTN_HARD_ERROR;
2056 if (my_remote == NULL) {
2057 btn_state_set_message(
2060 "config file contains no valid remote");
2061 return STS_BTN_HARD_ERROR;
2063 if (my_remote == (
void*)-1) {
2064 btn_state_set_message(
2067 "Reading of config file failed");
2068 return STS_BTN_HARD_ERROR;
2070 sts = STS_BTN_ALL_DONE;
2072 remote = *my_remote;
2075 if (!has_toggle_bit_mask(my_remote)) {
2076 if (!opts->using_template
2078 remote = *(my_remote);
2079 sts = STS_BTN_GET_TOGGLE_BITS;
2082 set_toggle_bit_mask(my_remote,
2087 if (!opts->update) {
2088 get_pre_data(my_remote);
2089 get_post_data(my_remote);
2091 remote = *my_remote;
2093 case STS_BTN_BUTTON_DONE:
2094 return STS_BTN_BUTTON_DONE;
2095 case STS_BTN_HARD_ERROR:
2096 return STS_BTN_HARD_ERROR;
2098 btn_state_set_message(btn_state,
2099 "record_buttons(): bad state: %d\n",
2101 return STS_BTN_HARD_ERROR;
2107 void config_file_setup(
struct main_state* state,
const struct opts* opts)
2109 state->fout = fopen(opts->tmpfile,
"w");
2110 if (state->fout == NULL) {
2111 logprintf(LIRC_ERROR,
2112 "Could not open new config file %s",
2114 logperror(LIRC_ERROR,
"While opening temporary file for write");
2117 fprint_copyright(state->fout);
2118 fprint_comment(state->fout, &remote, opts->commandline);
2119 fprint_remote_head(state->fout, &remote);
2120 fprint_remote_signal_head(state->fout, &remote);
2126 int config_file_finish(
struct main_state* state,
const struct opts* opts)
2128 state->fout = fopen(opts->filename,
"w");
2129 if (state->fout == NULL) {
2131 "While opening \"%s\" for write",
2135 fprint_copyright(state->fout);
2136 fprint_remotes(state->fout, &remote, opts->commandline);
void rec_buffer_init(void)
struct ir_remote * last_remote
char *(*const rec_func)(struct ir_remote *remotes)
struct ir_code_node * next
int receive_decode(struct ir_remote *remote, struct decode_ctx_t *ctx)
lirc_t(*const readdata)(lirc_t timeout)
Main include file for lirc applications.
int(*const deinit_func)(void)
const char * drop_sudo_root(int(*set_some_uid)(uid_t))
int(*const init_func)(void)
void logperror(loglevel_t prio, const char *fmt,...)
int default_open(const char *path)
int(*const decode_func)(struct ir_remote *remote, struct decode_ctx_t *ctx)
const struct driver const * curr_driver
int rec_buffer_clear(void)
struct ir_remote * read_config(FILE *f, const char *name)