21 static lirc_t emulation_readdata(lirc_t timeout);
24 static const struct driver hw_emulation = {
26 .device =
"/dev/null",
27 .features = LIRC_CAN_REC_MODE2,
29 .rec_mode = LIRC_MODE_MODE2,
37 .readdata = emulation_readdata,
41 .driver_version =
"0.9.2"
44 static const int IR_CODE_NODE_SIZE =
sizeof(
struct ir_code_node);
49 unsigned int eps = 30;
54 static lirc_t signals[MAX_SIGNALS];
56 static struct ir_ncode* next_code = NULL;
57 static struct ir_ncode* current_code = NULL;
58 static int current_index = 0;
59 static int current_rep = 0;
61 static struct lengths* first_space = NULL;
62 static struct lengths* first_pulse = NULL;
63 static struct lengths* first_sum = NULL;
64 static struct lengths* first_gap = NULL;
65 static struct lengths* first_repeat_gap = NULL;
66 static struct lengths* first_signal_length = NULL;
67 static struct lengths* first_headerp = NULL;
68 static struct lengths* first_headers = NULL;
69 static struct lengths* first_1lead = NULL;
70 static struct lengths* first_3lead = NULL;
71 static struct lengths* first_trail = NULL;
72 static struct lengths* first_repeatp = NULL;
73 static struct lengths* first_repeats = NULL;
75 static __u32
lengths[MAX_SIGNALS];
76 static __u32 first_length, first_lengths, second_lengths;
77 static unsigned int count, count_spaces, count_signals;
78 static unsigned int count_3repeats, count_5repeats;
84 void btn_state_set_message(
struct button_state* state,
const char* fmt, ...)
94 static void fprint_copyright(FILE* fout)
97 "# Please take the time to finish this file as described in\n"
98 "# https://sourceforge.net/p/lirc-remotes/wiki/Checklist/\n"
99 "# and make it available to others by sending it to\n"
100 "# <lirc@bartelmus.de>\n");
105 int availabledata(
void)
120 }
while (ret == -1 && errno == EINTR);
122 logperror(LIRC_ERROR,
"select() failed");
140 case LIRC_MODE_MODE2:
141 while (availabledata())
144 case LIRC_MODE_LIRCCODE:
154 static uid_t getresuid_uid(
void)
156 uid_t ruid, euid, suid;
158 getresuid(&ruid, &euid, &suid);
167 if (getresuid_uid() == 0)
168 if (seteuid(0) == -1)
169 logprintf(LIRC_ERROR,
"Cannot reset root uid");
191 void gap_state_init(
struct gap_state* state)
193 memset(state, 0,
sizeof(
struct gap_state));
208 state->first_signal = -1;
217 state->retval = EXIT_SUCCESS;
224 state->retval = EXIT_SUCCESS;
228 static lirc_t calc_signal(
struct lengths* len)
232 return (lirc_t)(len->sum / len->count);
245 bits = bit_count(remote);
246 mask = ((
ir_code)1) << (bits - 1);
255 codes = remote->codes;
256 while (codes->
name != NULL) {
262 else if (bits == 15 && xor == 0x3ff) {
270 void get_pre_data(
struct ir_remote* remote)
277 if (remote->
bits == 0)
280 codes = remote->codes;
281 if (codes->
name == NULL)
285 if (codes->
name == NULL)
287 while (codes->
name != NULL) {
288 mask &= ~(last ^ codes->
code);
290 for (n = codes->
next; n != NULL; n = n->next) {
291 mask &= ~(last ^ n->code);
297 while (mask & 0x8000000000000000LL) {
301 count -=
sizeof(
ir_code) * CHAR_BIT - remote->
bits;
304 if (count % 8 && (remote->
bits - count) % 8)
308 for (i = 0; i < count; i++) {
312 remote->
bits -= count;
313 mask = mask << (remote->
bits);
317 codes = remote->codes;
318 while (codes->
name != NULL) {
319 codes->
code &= ~mask;
320 for (n = codes->
next; n != NULL; n = n->next)
328 void get_post_data(
struct ir_remote* remote)
335 if (remote->
bits == 0)
339 codes = remote->codes;
340 if (codes->
name == NULL)
344 if (codes->
name == NULL)
346 while (codes->
name != NULL) {
347 mask &= ~(last ^ codes->
code);
349 for (n = codes->
next; n != NULL; n = n->next) {
350 mask &= ~(last ^ n->code);
361 if (count % 8 && (remote->
bits - count) % 8)
365 for (i = 0; i < count; i++) {
369 remote->
bits -= count;
373 codes = remote->codes;
374 while (codes->
name != NULL) {
376 for (n = codes->
next; n != NULL; n = n->next)
377 n->code = n->code >> count;
384 void remove_pre_data(
struct ir_remote* remote)
390 || remote->pre_p != 0
391 || remote->
pre_s != 0)
393 for (codes = remote->codes; codes->
name != NULL; codes++) {
395 for (n = codes->
next; n != NULL; n = n->next)
404 void remove_post_data(
struct ir_remote* remote)
411 for (codes = remote->codes; codes->
name != NULL; codes++) {
414 for (n = codes->
next; n != NULL; n = n->next) {
425 void invert_data(
struct ir_remote* remote)
435 remote->pone = remote->pzero;
441 if (has_pre(remote)) {
446 if (has_post(remote)) {
451 if (remote->
bits == 0)
455 mask = gen_mask(remote->
bits);
456 for (codes = remote->codes; codes->
name != NULL; codes++) {
458 for (n = codes->
next; n != NULL; n = n->next)
464 void remove_trail(
struct ir_remote* remote)
468 if (!is_space_enc(remote))
472 if (expect(remote, remote->pone, remote->pzero)
473 || expect(remote, remote->pzero, remote->pone))
475 if (!(expect(remote, remote->
sone, remote->
szero)
476 && expect(remote, remote->
szero, remote->
sone)))
478 if (expect(remote, remote->
ptrail, remote->pone))
480 else if (expect(remote, remote->
ptrail, remote->pzero))
492 void for_each_remote(
struct ir_remote* remotes, remote_func func)
497 while (remote != NULL) {
499 remote = remote->next;
504 static int mywaitfordata(__u32 maxusec)
516 tv.tv_sec = maxusec / 1000000;
517 tv.tv_usec = maxusec % 1000000;
519 &fds, NULL, NULL, &tv);
524 &fds, NULL, NULL, NULL);
526 }
while (ret == -1 && errno == EINTR);
528 logperror(LIRC_ERROR,
"select() failed");
540 static lirc_t emulation_readdata(lirc_t timeout)
542 static lirc_t sum = 0;
545 if (current_code == NULL) {
548 current_code = next_code;
550 current_code = emulation_data->codes;
554 if (current_code->
name == NULL) {
555 logprintf(LIRC_WARNING,
556 "%s: no data found", emulation_data->
name);
559 if (current_index >= current_code->
length) {
561 current_code = next_code;
564 if (current_rep > 2) {
571 if (current_code->
name == NULL) {
573 return emulation_readdata(timeout);
576 if (is_const(emulation_data))
577 data = emulation_data->
gap - sum;
579 data = emulation_data->
gap;
584 data = current_code->
signals[current_index];
585 if ((current_index % 2) == 0)
588 sum += data & PULSE_MASK;
599 static struct lengths* new_length(lirc_t length)
603 l = malloc(
sizeof(
struct lengths));
608 l->lower_bound = length / 100 * 100;
609 l->upper_bound = length / 100 * 100 + 99;
610 l->min = l->max = length;
616 void unlink_length(
struct lengths** first,
struct lengths*
remove)
621 if (
remove == *first) {
622 *first =
remove->next;
626 scan = (*first)->next;
629 if (scan ==
remove) {
630 last->next =
remove->next;
637 logprintf(LIRC_ERROR,
"unlink_length(): report this bug!");
641 int add_length(
struct lengths** first, lirc_t length)
646 if (*first == NULL) {
647 *first = new_length(length);
654 if (l->lower_bound <= length && length <= l->upper_bound) {
657 l->min = min(l->min, length);
658 l->max = max(l->max, length);
664 last->next = new_length(length);
665 if (last->next == NULL)
671 void free_lengths(
struct lengths** firstp)
679 while (first != NULL) {
688 void free_all_lengths(
void)
690 free_lengths(&first_space);
691 free_lengths(&first_pulse);
692 free_lengths(&first_sum);
693 free_lengths(&first_gap);
694 free_lengths(&first_repeat_gap);
695 free_lengths(&first_signal_length);
696 free_lengths(&first_headerp);
697 free_lengths(&first_headers);
698 free_lengths(&first_1lead);
699 free_lengths(&first_3lead);
700 free_lengths(&first_trail);
701 free_lengths(&first_repeatp);
702 free_lengths(&first_repeats);
706 static void merge_lengths(
struct lengths* first)
718 while (inner != NULL) {
719 new_sum = l->sum + inner->sum;
720 new_count = l->count + inner->count;
722 if ((l->max <= new_sum / new_count + aeps
723 && l->min + aeps >= new_sum / new_count
724 && inner->max <= new_sum / new_count + aeps
725 && inner->min + aeps >= new_sum / new_count)
726 || (l->max <= new_sum / new_count * (100 + eps)
727 && l->min >= new_sum / new_count * (100 - eps)
728 && inner->max <= new_sum / new_count *
730 && inner->min >= new_sum / new_count *
733 l->count = new_count;
734 l->upper_bound = max(l->upper_bound,
736 l->lower_bound = min(l->lower_bound,
738 l->min = min(l->min, inner->min);
739 l->max = max(l->max, inner->max);
741 last->next = inner->next;
750 for (l = first; l != NULL; l = l->next) {
751 logprintf(LIRC_DEBUG,
"%d x %u [%u,%u]",
752 l->count, (__u32)calc_signal(l),
753 (__u32)l->min, (__u32)l->max);
770 if (first->count > 0)
771 logprintf(LIRC_DEBUG,
"%u x %u", first->count,
772 (__u32)calc_signal(first));
775 if (scan->count > max_length->count)
778 logprintf(LIRC_DEBUG,
781 (__u32)calc_signal(scan));
790 int get_trail_length(
struct ir_remote* remote,
int interactive)
792 unsigned int sum = 0, max_count;
795 if (is_biphase(remote))
798 max_length = get_max_length(first_trail, &sum);
799 max_count = max_length->count;
800 logprintf(LIRC_DEBUG,
801 "get_trail_length(): sum: %u, max_count %u",
803 if (max_count >= sum * TH_TRAIL / 100) {
804 logprintf(LIRC_DEBUG,
"Found trail pulse: %lu",
805 (__u32)calc_signal(max_length));
806 remote->
ptrail = calc_signal(max_length);
809 logprintf(LIRC_DEBUG,
"No trail pulse found.");
814 int get_lead_length(
struct ir_remote* remote,
int interactive)
816 unsigned int sum = 0, max_count;
822 if (!is_biphase(remote) || has_header(remote))
827 first_lead = has_header(remote) ? first_3lead : first_1lead;
828 max_length = get_max_length(first_lead, &sum);
829 max_count = max_length->count;
830 logprintf(LIRC_DEBUG,
831 "get_lead_length(): sum: %u, max_count %u",
833 if (max_count >= sum * TH_LEAD / 100) {
834 logprintf(LIRC_DEBUG,
835 "Found lead pulse: %lu",
836 (__u32)calc_signal(max_length));
837 remote->
plead = calc_signal(max_length);
840 unlink_length(&first_lead, max_length);
841 max2_length = get_max_length(first_lead, &sum);
842 max_length->next = first_lead;
843 first_lead = max_length;
845 a = calc_signal(max_length);
846 b = calc_signal(max2_length);
852 if (abs(2 * a - b) < b * eps / 100 || abs(2 * a - b) < aeps) {
853 logprintf(LIRC_DEBUG,
854 "Found hidden lead pulse: %lu",
859 logprintf(LIRC_DEBUG,
"No lead pulse found.");
864 int get_header_length(
struct ir_remote* remote,
int interactive)
866 unsigned int sum, max_count;
867 lirc_t headerp, headers;
871 if (first_headerp != NULL) {
872 max_plength = get_max_length(first_headerp, &sum);
873 max_count = max_plength->count;
875 logprintf(LIRC_DEBUG,
"No header data.");
878 logprintf(LIRC_DEBUG,
879 "get_header_length(): sum: %u, max_count %u",
882 if (max_count >= sum * TH_HEADER / 100) {
883 max_slength = get_max_length(first_headers, &sum);
884 max_count = max_slength->count;
885 logprintf(LIRC_DEBUG,
886 "get_header_length(): sum: %u, max_count %u",
888 if (max_count >= sum * TH_HEADER / 100) {
889 headerp = calc_signal(max_plength);
890 headers = calc_signal(max_slength);
892 logprintf(LIRC_DEBUG,
893 "Found possible header: %lu %lu",
896 remote->phead = headerp;
897 remote->
shead = headers;
898 if (first_lengths < second_lengths) {
899 logprintf(LIRC_DEBUG,
900 "Header is not being repeated.");
906 logprintf(LIRC_DEBUG,
"No header found.");
911 int get_repeat_length(
struct ir_remote* remote,
int interactive)
913 unsigned int sum = 0, max_count;
914 lirc_t repeatp, repeats, repeat_gap;
918 if (!((count_3repeats > SAMPLES / 2 ? 1 : 0) ^
919 (count_5repeats > SAMPLES / 2 ? 1 : 0))) {
920 if (count_3repeats > SAMPLES / 2
921 || count_5repeats > SAMPLES / 2) {
922 logprintf(LIRC_WARNING,
"Repeat inconsistency.");
925 logprintf(LIRC_DEBUG,
"No repeat code found.");
929 max_plength = get_max_length(first_repeatp, &sum);
930 max_count = max_plength->count;
931 logprintf(LIRC_DEBUG,
932 "get_repeat_length(): sum: %u, max_count %u",
934 if (max_count >= sum * TH_REPEAT / 100) {
935 max_slength = get_max_length(first_repeats, &sum);
936 max_count = max_slength->count;
937 logprintf(LIRC_DEBUG,
938 "get_repeat_length(): sum: %u, max_count %u",
940 if (max_count >= sum * TH_REPEAT / 100) {
941 if (count_5repeats > count_3repeats
942 && !has_header(remote)) {
943 logprintf(LIRC_WARNING,
944 "Repeat code has header,"
945 " but no header found!");
948 if (count_5repeats > count_3repeats
949 && has_header(remote))
951 repeatp = calc_signal(max_plength);
952 repeats = calc_signal(max_slength);
954 logprintf(LIRC_DEBUG,
955 "Found repeat code: %lu %lu",
958 remote->prepeat = repeatp;
961 max_slength = get_max_length(first_repeat_gap,
963 repeat_gap = calc_signal(max_slength);
964 logprintf(LIRC_DEBUG,
965 "Found repeat gap: %lu",
972 logprintf(LIRC_DEBUG,
"No repeat header found.");
977 void get_scheme(
struct ir_remote* remote,
int interactive)
979 unsigned int i, length = 0, sum = 0;
985 for (i = 1; i < MAX_SIGNALS; i++) {
990 logprintf(LIRC_DEBUG,
"%u: %u", i,
lengths[i]);
992 logprintf(LIRC_DEBUG,
"get_scheme(): sum: %u length: %u signals: %u"
993 " first_lengths: %u second_lengths: %u\n",
994 sum, length + 1,
lengths[length],
995 first_lengths, second_lengths);
997 if (
lengths[length] >= TH_SPACE_ENC * sum / 100) {
999 logprintf(LIRC_DEBUG,
1000 "Space/pulse encoded remote control found.");
1001 logprintf(LIRC_DEBUG,
"Signal length is %u.", length);
1003 remote->
bits = length;
1007 maxp = get_max_length(first_pulse, NULL);
1008 unlink_length(&first_pulse, maxp);
1009 if (first_pulse == NULL)
1011 max2p = get_max_length(first_pulse, NULL);
1012 maxp->next = first_pulse;
1015 maxs = get_max_length(first_space, NULL);
1016 unlink_length(&first_space, maxs);
1017 if (first_space == NULL) {
1020 max2s = get_max_length(first_space, NULL);
1021 maxs->next = first_space;
1024 maxs = get_max_length(first_space, NULL);
1027 && (calc_signal(maxp) < TH_RC6_SIGNAL
1028 || calc_signal(max2p) < TH_RC6_SIGNAL)
1029 && (calc_signal(maxs) < TH_RC6_SIGNAL
1030 || calc_signal(max2s) < TH_RC6_SIGNAL)) {
1031 logprintf(LIRC_DEBUG,
"RC-6 remote control found.");
1032 set_protocol(remote,
RC6);
1034 logprintf(LIRC_DEBUG,
"RC-5 remote control found.");
1035 set_protocol(remote,
RC5);
1040 logprintf(LIRC_DEBUG,
"Suspicious data length: %u.", length);
1042 remote->
bits = length;
1047 int get_data_length(
struct ir_remote* remote,
int interactive)
1049 unsigned int sum = 0, max_count;
1050 lirc_t p1, p2, s1, s2;
1056 max_plength = get_max_length(first_pulse, &sum);
1057 max_count = max_plength->count;
1058 logprintf(LIRC_DEBUG,
"get_data_length(): sum: %u, max_count %u",
1061 if (max_count >= sum * TH_IS_BIT / 100) {
1062 unlink_length(&first_pulse, max_plength);
1064 max2_plength = get_max_length(first_pulse, NULL);
1065 if (max2_plength != NULL)
1066 if (max2_plength->count < max_count * TH_IS_BIT / 100)
1067 max2_plength = NULL;
1068 logprintf(LIRC_DEBUG,
"Pulse candidates: ");
1069 logprintf(LIRC_DEBUG,
"%u x %u", max_plength->count,
1070 (__u32)calc_signal(max_plength));
1072 logprintf(LIRC_DEBUG,
", %u x %u",
1073 max2_plength->count,
1074 (__u32)calc_signal(max2_plength));
1076 max_slength = get_max_length(first_space, &sum);
1077 max_count = max_slength->count;
1078 logprintf(LIRC_DEBUG,
1079 "get_data_length(): sum: %u, max_count %u",
1081 if (max_count >= sum * TH_IS_BIT / 100) {
1082 unlink_length(&first_space, max_slength);
1084 max2_slength = get_max_length(first_space, NULL);
1085 if (max2_slength != NULL)
1086 if (max2_slength->count <
1087 max_count * TH_IS_BIT / 100)
1088 max2_slength = NULL;
1089 if (max_count >= sum * TH_IS_BIT / 100) {
1090 logprintf(LIRC_DEBUG,
"Space candidates: ");
1091 logprintf(LIRC_DEBUG,
1094 (__u32)calc_signal(max_slength));
1099 max2_slength->count,
1100 (__u32)calc_signal(max2_slength));
1104 remote->
aeps = aeps;
1105 if (is_biphase(remote)) {
1106 if (max2_plength == NULL
1107 || max2_slength == NULL) {
1108 logprintf(LIRC_NOTICE,
1109 "Unknown encoding found.");
1112 logprintf(LIRC_DEBUG,
1113 "Signals are biphase encoded.");
1114 p1 = calc_signal(max_plength);
1115 p2 = calc_signal(max2_plength);
1116 s1 = calc_signal(max_slength);
1117 s2 = calc_signal(max2_slength);
1120 (min(p1, p2) + max(p1, p2) / 2) / 2;
1122 (min(s1, s2) + max(s1, s2) / 2) / 2;
1123 remote->pzero = remote->pone;
1126 if (max2_plength == NULL
1127 && max2_slength == NULL) {
1128 logprintf(LIRC_NOTICE,
1129 "No encoding found");
1132 if (max2_plength && max2_slength) {
1133 logprintf(LIRC_NOTICE,
1134 "Unknown encoding found.");
1137 p1 = calc_signal(max_plength);
1138 s1 = calc_signal(max_slength);
1140 p2 = calc_signal(max2_plength);
1143 "Signals are pulse encoded.");
1144 remote->pone = max(p1, p2);
1146 remote->pzero = min(p1, p2);
1148 if (expect(remote, remote->
ptrail, p1)
1149 || expect(remote, remote->
ptrail,
1153 s2 = calc_signal(max2_slength);
1156 "Signals are space encoded.");
1158 remote->
sone = max(s1, s2);
1160 remote->
szero = min(s1, s2);
1163 if (has_header(remote)
1164 && (!has_repeat(remote)
1166 if (!is_biphase(remote)
1167 && ((expect(remote, remote->phead,
1178 remote->phead = remote->
shead = 0;
1180 logprintf(LIRC_DEBUG,
1183 if (is_biphase(remote)
1187 remote->
plead = remote->phead;
1188 remote->phead = remote->
shead = 0;
1190 logprintf(LIRC_DEBUG,
1194 if (is_biphase(remote)) {
1195 struct lengths* signal_length;
1199 get_max_length(first_signal_length,
1202 calc_signal(signal_length) -
1203 remote->
plead - remote->phead -
1206 (remote->pone + remote->
sone) / 2;
1207 remote->
bits = data_length / (remote->pone +
1214 (has_header(remote) ? 2 : 0) + 1 -
1215 (remote->
ptrail > 0 ? 2 : 0)) / 2;
1217 logprintf(LIRC_DEBUG,
1218 "Signal length is %d",
1220 free_lengths(&max_plength);
1221 free_lengths(&max_slength);
1224 free_lengths(&max_plength);
1226 logprintf(LIRC_NOTICE,
"Could not find data lengths.");
1231 enum get_gap_status get_gap_length(
struct gap_state* state,
1234 while (availabledata())
1236 if (!mywaitfordata(10000000)) {
1237 free_lengths(&(state->gaps));
1238 return STS_GAP_TIMEOUT;
1240 gettimeofday(&(state->start), NULL);
1241 while (availabledata())
1243 gettimeofday(&(state->end), NULL);
1245 state->gap = time_elapsed(&(state->last), &(state->start));
1246 add_length(&(state->gaps), state->gap);
1247 merge_lengths(state->gaps);
1248 state->maxcount = 0;
1249 state->scan = state->gaps;
1250 while (state->scan) {
1251 state->maxcount = max(state->maxcount,
1252 state->scan->count);
1253 if (state->scan->count > SAMPLES) {
1254 remote->
gap = calc_signal(state->scan);
1255 free_lengths(&(state->gaps));
1256 return STS_GAP_FOUND;
1258 state->scan = state->scan->next;
1260 if (state->maxcount > state->lastmaxcount) {
1261 state->lastmaxcount = state->maxcount;
1262 return STS_GAP_GOT_ONE_PRESS;
1267 state->last = state->end;
1268 return STS_GAP_AGAIN;
1273 int needs_toggle_mask(
struct ir_remote* remote)
1277 if (!is_rc6(remote))
1279 if (remote->codes) {
1280 codes = remote->codes;
1281 while (codes->
name != NULL) {
1294 static void compute_lengths_4_signals(
void)
1296 add_length(&first_repeatp,
signals[0]);
1297 merge_lengths(first_repeatp);
1298 add_length(&first_repeats,
signals[1]);
1299 merge_lengths(first_repeats);
1300 add_length(&first_trail,
signals[2]);
1301 merge_lengths(first_trail);
1302 add_length(&first_repeat_gap,
signals[3]);
1303 merge_lengths(first_repeat_gap);
1308 static void compute_lengths_6_signals(
void)
1310 add_length(&first_headerp,
signals[0]);
1311 merge_lengths(first_headerp);
1312 add_length(&first_headers,
signals[1]);
1313 merge_lengths(first_headers);
1314 add_length(&first_repeatp,
signals[2]);
1315 merge_lengths(first_repeatp);
1316 add_length(&first_repeats,
signals[3]);
1317 merge_lengths(first_repeats);
1318 add_length(&first_trail,
signals[4]);
1319 merge_lengths(first_trail);
1320 add_length(&first_repeat_gap,
signals[5]);
1321 merge_lengths(first_repeat_gap);
1325 static void compute_lengths_many_signals(
struct lengths_state* state)
1329 merge_lengths(first_1lead);
1330 for (i = 2; i < state->count - 2; i++) {
1332 add_length(&first_space,
signals[i]);
1333 merge_lengths(first_space);
1335 add_length(&first_pulse,
signals[i]);
1336 merge_lengths(first_pulse);
1339 add_length(&first_trail,
signals[state->count - 2]);
1340 merge_lengths(first_trail);
1342 add_length(&first_signal_length, state->sum - state->data);
1343 merge_lengths(first_signal_length);
1344 if (state->first_signal == 1
1345 || (first_length > 2
1346 && first_length - 2 != state->count - 2)) {
1347 add_length(&first_3lead,
signals[2]);
1348 merge_lengths(first_3lead);
1349 add_length(&first_headerp,
signals[0]);
1350 merge_lengths(first_headerp);
1351 add_length(&first_headers,
signals[1]);
1352 merge_lengths(first_headers);
1354 if (state->first_signal == 1) {
1356 first_length = state->count - 2;
1358 }
else if (state->first_signal == 0
1359 && first_length - 2 == state->count - 2) {
1361 lengths[state->count - 2 + 2]++;
1370 enum lengths_status* again)
1374 for (scan = first_sum; scan; scan = scan->next) {
1375 *maxcount = max(*maxcount, scan->count);
1376 if (scan->count > SAMPLES) {
1377 remote->
gap = calc_signal(scan);
1379 state->mode = MODE_HAVE_GAP;
1380 logprintf(LIRC_DEBUG,
"Found gap: %u", remote->
gap);
1381 *again = STS_LEN_AGAIN_INFO;
1392 enum lengths_status* again)
1396 for (scan = first_gap; scan; scan = scan->next) {
1397 *maxcount = max(*maxcount, scan->count);
1398 if (scan->count > SAMPLES) {
1399 remote->
gap = calc_signal(scan);
1400 state->mode = MODE_HAVE_GAP;
1401 logprintf(LIRC_DEBUG,
"Found gap: %u", remote->
gap);
1402 *again = STS_LEN_AGAIN_INFO;
1410 enum lengths_status get_lengths(
struct lengths_state* state,
1412 int force,
int interactive)
1416 static int lastmaxcount = 0;
1417 enum lengths_status again = STS_LEN_AGAIN;
1422 return STS_LEN_TIMEOUT;
1425 if (state->mode == MODE_GET_GAP) {
1426 state->sum += state->data & PULSE_MASK;
1427 if (state->average == 0 && is_space(state->data)) {
1428 if (state->data > 100000) {
1430 return STS_LEN_AGAIN;
1432 state->average = state->data;
1433 state->maxspace = state->data;
1434 }
else if (is_space(state->data)) {
1435 if (state->data > MIN_GAP
1436 || state->data > 100 * state->average
1438 || (state->data >= 5000 && count_spaces > 10
1439 && state->data > 5 * state->average)
1440 || (state->data < 5000 && count_spaces > 10
1441 && state->data > 5 * state->maxspace / 2)) {
1442 add_length(&first_sum, state->sum);
1443 merge_lengths(first_sum);
1444 add_length(&first_gap, state->data);
1445 merge_lengths(first_gap);
1449 state->maxspace = 0;
1452 scan = scan_gap1(state,
1457 scan = scan_gap2(state,
1463 state->mode = MODE_HAVE_GAP;
1466 state->remaining_gap =
1470 remote->
gap - state->data : 0)
1471 : (has_repeat_gap(remote) ?
1473 repeat_gap : remote->
gap);
1476 return STS_LEN_RAW_OK;
1478 return STS_LEN_AGAIN_INFO;
1480 lastmaxcount = maxcount;
1485 (state->average * count_spaces + state->data)
1486 / (count_spaces + 1);
1488 if (state->data > state->maxspace)
1489 state->maxspace = state->data;
1491 if (state->count > SAMPLES * MAX_SIGNALS * 2) {
1493 return STS_LEN_NO_GAP_FOUND;
1496 return STS_LEN_AGAIN;
1497 }
else if (state->mode == MODE_HAVE_GAP) {
1498 if (state->count <= MAX_SIGNALS) {
1499 signals[state->count - 1] = state->data & PULSE_MASK;
1502 return STS_LEN_TOO_LONG;
1504 if (is_const(remote))
1505 state->remaining_gap =
1506 remote->
gap > state->sum ? remote->
gap -
1509 state->remaining_gap = remote->
gap;
1510 state->sum += state->data & PULSE_MASK;
1512 if (state->count > 2
1513 && ((state->data & PULSE_MASK) >=
1514 state->remaining_gap * (100 - eps) / 100
1516 PULSE_MASK) >= state->remaining_gap - aeps)) {
1517 if (is_space(state->data)) {
1520 if (state->count == 4) {
1522 compute_lengths_4_signals();
1523 }
else if (state->count == 6) {
1525 compute_lengths_6_signals();
1526 }
else if (state->count > 6) {
1528 compute_lengths_many_signals(state);
1538 return STS_LEN_NO_GAP_FOUND;
1541 if (count_signals >= SAMPLES) {
1542 get_scheme(remote, interactive);
1543 if (!get_header_length(remote, interactive)
1544 || !get_trail_length(remote, interactive)
1545 || !get_lead_length(remote, interactive)
1546 || !get_repeat_length(remote, interactive)
1547 || !get_data_length(remote, interactive))
1549 return state->retval ==
1550 0 ? STS_LEN_FAIL : STS_LEN_OK;
1552 if ((state->data & PULSE_MASK) <=
1553 (state->remaining_gap + state->header) *
1555 || (state->data & PULSE_MASK) <=
1556 (state->remaining_gap + state->header) + aeps) {
1557 state->first_signal = 0;
1560 state->first_signal = 1;
1564 return STS_LEN_AGAIN;
1568 enum toggle_status get_toggle_bit_mask(
struct toggle_state* state,
1575 if (!state->inited) {
1577 while (availabledata())
1581 if (state->retries <= 0) {
1583 return STS_TGL_NOT_FOUND;
1584 if (state->seq > 0) {
1585 remote->
min_repeat = state->repeats / state->seq;
1586 logprintf(LIRC_DEBUG,
"min_repeat=%d",
1589 return STS_TGL_FOUND;
1591 if (!mywaitfordata(10000000))
1592 return STS_TGL_TIMEOUT;
1594 if (is_rc6(remote) && remote->
rc6_mask == 0) {
1595 for (i = 0, mask = 1; i < remote->
bits; i++, mask <<= 1) {
1599 if (state->success) {
1607 if (!state->success)
1612 if (state->success) {
1619 if (state->success) {
1620 if (state->flag == 0) {
1622 state->first = decode_ctx.
code;
1624 || decode_ctx.
code != state->last) {
1626 mask = state->first ^ decode_ctx.
code;
1627 if (!state->found && mask) {
1628 set_toggle_bit_mask(remote, mask);
1632 state->repeats / state->seq;
1635 state->last = decode_ctx.
code;
1636 return STS_TGL_GOT_ONE_PRESS;
1639 state->last = decode_ctx.
code;
1642 while (availabledata())
1645 return STS_TGL_AGAIN;
1652 enum lengths_status status = STS_LEN_AGAIN;
1654 while (status == STS_LEN_AGAIN) {
1655 status = get_lengths(lengths_state, &remote, 0, 0);
1657 case STS_LEN_AGAIN_INFO:
1658 status = STS_LEN_AGAIN;
1665 logprintf(LIRC_ERROR,
"get_lengths() failure");
1667 case STS_LEN_RAW_OK:
1668 logprintf(LIRC_ERROR,
"raw analyse result?!");
1670 case STS_LEN_TIMEOUT:
1671 logprintf(LIRC_ERROR,
"analyse timeout?!");
1673 case STS_LEN_NO_GAP_FOUND:
1674 logprintf(LIRC_ERROR,
"analyse, no gap?!");
1676 case STS_LEN_TOO_LONG:
1677 logprintf(LIRC_ERROR,
"analyse, signal too long?!");
1680 logprintf(LIRC_ERROR,
1681 "Cannot read raw data (%d)",
1695 struct lengths_state lengths_state;
1699 size_t new_codes_count = 100;
1703 if (!is_raw(raw_data)) {
1704 logprintf(LIRC_ERROR,
1705 "remote %s not in raw mode, ignoring",
1710 aeps = raw_data->
aeps;
1711 eps = raw_data->
eps;
1712 emulation_data = raw_data;
1714 current_code = NULL;
1716 memset(&remote, 0,
sizeof(remote));
1717 lengths_state_init(&lengths_state);
1718 if (!analyse_get_lengths(&lengths_state))
1721 if (is_rc6(&remote) && remote.
bits >= 5)
1729 new_codes = malloc(new_codes_count *
sizeof(*new_codes));
1730 if (new_codes == NULL) {
1731 logprintf(LIRC_ERROR,
"Out of memory");
1734 memset(new_codes, 0, new_codes_count *
sizeof(*new_codes));
1735 codes = raw_data->codes;
1736 while (codes->
name != NULL) {
1738 current_code = NULL;
1746 logprintf(LIRC_WARNING,
1747 "Decoding of %s failed", codes->
name);
1749 if (new_index + 1 >= new_codes_count) {
1752 new_codes_count *= 2;
1756 sizeof(*new_codes));
1757 if (renew_codes == NULL) {
1758 logprintf(LIRC_ERROR,
1763 memset(&new_codes[new_codes_count / 2],
1765 new_codes_count / 2 *
1766 sizeof(*new_codes));
1767 new_codes = renew_codes;
1771 code = decode_ctx.
code;
1773 code2 = decode_ctx.
code;
1775 if (ret && code2 != decode_ctx.
code) {
1776 new_codes[new_index].
next =
1777 malloc(IR_CODE_NODE_SIZE);
1778 if (new_codes[new_index].next) {
1779 memset(new_codes[new_index].next,
1782 new_codes[new_index].
next->code =
1786 new_codes[new_index].
name = codes->
name;
1787 new_codes[new_index].
code = decode_ctx.
code;
1792 new_codes[new_index].
name = NULL;
1793 remote.codes = new_codes;
1794 fprint_remotes(stdout, &remote, opts->commandline);
1795 remote.codes = NULL;
1802 int do_analyse(
const struct opts* opts,
struct main_state* state)
1808 f = fopen(opts->filename,
"r");
1810 fprintf(stderr,
"Cannot open file: %s\n", opts->filename);
1815 fprintf(stderr,
"Cannot parse file: %s\n", opts->filename);
1818 for (; r != NULL; r = r->next) {
1820 logprintf(LIRC_ERROR,
1821 "remote %s not in raw mode, ignoring",
1825 analyse_remote(r, opts);
1831 ssize_t raw_read(
void* buffer,
size_t size,
unsigned int timeout_us)
1833 if (!mywaitfordata(timeout_us))
1843 r = is_space(btn_state->data) && (is_const(&remote) ?
1845 (remote.
gap > btn_state->sum ? (
1851 : btn_state->data > remote.
gap *
1852 (100 - remote.
eps) / 100);
1857 enum button_status record_buttons(
struct button_state* btn_state,
1858 enum button_status last_status,
1860 const struct opts* opts)
1862 const char*
const MSG_BAD_LENGTH =
1863 "Signal length is %d\n"
1864 "That's weird because the signal length must be odd!\n";
1871 enum button_status sts;
1873 if (btn_state->no_data) {
1874 btn_state->no_data = 0;
1875 return STS_BTN_TIMEOUT;
1877 switch (last_status) {
1879 return STS_BTN_GET_NAME;
1880 case STS_BTN_GET_NAME:
1881 if (strchr(btn_state->buffer,
' ') != NULL) {
1882 btn_state_set_message(
1884 "The name must not contain any whitespace.");
1885 return STS_BTN_SOFT_ERROR;
1887 if (strchr(btn_state->buffer,
'\t') != NULL) {
1888 btn_state_set_message(
1890 "The name must not contain any whitespace.");
1891 return STS_BTN_SOFT_ERROR;
1893 if (strcasecmp(btn_state->buffer,
"begin") == 0) {
1894 btn_state_set_message(
1896 "'%s' is not allowed as button name\n",
1898 return STS_BTN_SOFT_ERROR;
1900 if (strcasecmp(btn_state->buffer,
"end") == 0) {
1901 btn_state_set_message(
1903 "'%s' is not allowed as button name\n",
1905 return STS_BTN_SOFT_ERROR;
1907 if (strlen(btn_state->buffer) == 0)
1908 return STS_BTN_RECORD_DONE;
1909 if (!opts->disable_namespace
1910 && !is_in_namespace(btn_state->buffer)) {
1911 btn_state_set_message(
1913 "'%s' is not in name space"
1914 " (use --disable-namespace to override)\n",
1916 return STS_BTN_SOFT_ERROR;
1918 return STS_BTN_INIT_DATA;
1919 case STS_BTN_INIT_DATA:
1923 while (availabledata())
1927 return opts->force ? STS_BTN_GET_RAW_DATA : STS_BTN_GET_DATA;
1928 case STS_BTN_GET_DATA:
1929 for (retries = RETRIES; retries > 0; ) {
1930 if (!mywaitfordata(10000000)) {
1931 btn_state->no_data = 1;
1932 return STS_BTN_TIMEOUT;
1937 while (availabledata()) {
1941 &(state->decode_ctx))) {
1948 btn_state_set_message(
1950 "Try using the -f option.\n");
1951 return STS_BTN_HARD_ERROR;
1954 btn_state_set_message(
1956 "Could not reset hardware.\n");
1957 return STS_BTN_HARD_ERROR;
1959 btn_state_set_message(
1961 "Try again (%d retries left).\n",
1965 return STS_BTN_SOFT_ERROR;
1967 btn_state->
ncode.
name = btn_state->buffer;
1971 &(state->decode_ctx))) {
1972 code2 = state->decode_ctx.
code;
1974 if (state->decode_ctx.
code != code2) {
1976 malloc(IR_CODE_NODE_SIZE);
1988 return STS_BTN_BUTTON_DONE;
1989 case STS_BTN_GET_RAW_DATA:
1990 btn_state->count = 0;
1992 while (btn_state->count < MAX_SIGNALS) {
1993 if (btn_state->count == 0)
1996 timeout = remote.
gap * 5;
1998 if (!btn_state->data) {
1999 if (btn_state->count == 0)
2000 return STS_BTN_TIMEOUT;
2001 btn_state->data = remote.
gap;
2003 if (btn_state->count == 0) {
2004 if (!is_space(btn_state->data)
2005 || btn_state->data <
2010 btn_state->count = 0;
2011 btn_state_set_message(
2013 "Something went wrong.");
2014 return STS_BTN_SOFT_ERROR;
2017 if (raw_data_ok(btn_state)) {
2018 logprintf(LIRC_INFO,
"Got it.\n");
2019 logprintf(LIRC_INFO,
2020 "Signal length is %d\n",
2021 btn_state->count - 1);
2022 if (btn_state->count % 2) {
2023 btn_state_set_message(
2026 btn_state->count - 1);
2029 btn_state->count = 0;
2030 return STS_BTN_SOFT_ERROR;
2035 btn_state->count - 1;
2039 signals[btn_state->count - 1] =
2040 btn_state->data & PULSE_MASK;
2042 btn_state->data & PULSE_MASK;
2046 if (btn_state->count == MAX_SIGNALS) {
2047 btn_state_set_message(btn_state,
2048 "Signal is too long.\n");
2049 return STS_BTN_SOFT_ERROR;
2051 return STS_BTN_BUTTON_DONE;
2052 case STS_BTN_RECORD_DONE:
2053 if (is_raw(&remote))
2054 return STS_BTN_ALL_DONE;
2056 btn_state_set_message(btn_state,
2057 "Could not reset hardware.");
2058 return STS_BTN_HARD_ERROR;
2060 return STS_BTN_BUTTONS_DONE;
2061 case STS_BTN_BUTTONS_DONE:
2062 f = fopen(opts->tmpfile,
"r");
2064 btn_state_set_message(btn_state,
2065 "Could not reopen config file");
2066 return STS_BTN_HARD_ERROR;
2070 if (my_remote == NULL) {
2071 btn_state_set_message(
2074 "config file contains no valid remote");
2075 return STS_BTN_HARD_ERROR;
2077 if (my_remote == (
void*)-1) {
2078 btn_state_set_message(
2081 "Reading of config file failed");
2082 return STS_BTN_HARD_ERROR;
2084 sts = STS_BTN_ALL_DONE;
2085 if (!has_toggle_bit_mask(my_remote)) {
2086 if (!opts->using_template
2088 remote = *(my_remote);
2089 sts = STS_BTN_GET_TOGGLE_BITS;
2092 set_toggle_bit_mask(my_remote,
2097 if (!opts->using_template) {
2098 get_pre_data(my_remote);
2099 get_post_data(my_remote);
2101 remote = *my_remote;
2103 case STS_BTN_BUTTON_DONE:
2104 return STS_BTN_BUTTON_DONE;
2105 case STS_BTN_HARD_ERROR:
2106 return STS_BTN_HARD_ERROR;
2108 btn_state_set_message(btn_state,
2109 "record_buttons(): bad state: %d\n",
2111 return STS_BTN_HARD_ERROR;
2117 void config_file_setup(
struct main_state* state,
const struct opts* opts)
2119 state->fout = fopen(opts->tmpfile,
"w");
2120 if (state->fout == NULL) {
2121 logprintf(LIRC_ERROR,
2122 "Could not open new config file %s",
2124 logperror(LIRC_ERROR,
"While opening temporary file for write");
2127 fprint_copyright(state->fout);
2128 fprint_comment(state->fout, &remote, opts->commandline);
2129 fprint_remote_head(state->fout, &remote);
2130 fprint_remote_signal_head(state->fout, &remote);
2136 int config_file_finish(
struct main_state* state,
const struct opts* opts)
2138 state->fout = fopen(opts->filename,
"w");
2139 if (state->fout == NULL) {
2141 "While opening \"%s\" for write",
2145 fprint_copyright(state->fout);
2146 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)