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)
121 }
while (ret == -1 && errno == EINTR);
123 logperror(LIRC_ERROR,
"select() failed");
141 case LIRC_MODE_MODE2:
142 while (availabledata())
145 case LIRC_MODE_LIRCCODE:
155 static uid_t getresuid_uid(
void)
157 uid_t ruid, euid, suid;
159 getresuid(&ruid, &euid, &suid);
168 if (getresuid_uid() == 0)
169 if (seteuid(0) == -1)
170 logprintf(LIRC_ERROR,
"Cannot reset root uid");
192 void gap_state_init(
struct gap_state* state)
194 memset(state, 0,
sizeof(
struct gap_state));
209 state->first_signal = -1;
218 state->retval = EXIT_SUCCESS;
225 state->retval = EXIT_SUCCESS;
229 static lirc_t calc_signal(
struct lengths* len)
233 return (lirc_t)(len->sum / len->count);
246 bits = bit_count(remote);
247 mask = ((
ir_code)1) << (bits - 1);
256 codes = remote->codes;
257 while (codes->
name != NULL) {
263 else if (bits == 15 && xor == 0x3ff) {
271 void get_pre_data(
struct ir_remote* remote)
278 if (remote->
bits == 0)
281 codes = remote->codes;
282 if (codes->
name == NULL)
286 if (codes->
name == NULL)
288 while (codes->
name != NULL) {
289 mask &= ~(last ^ codes->
code);
291 for (n = codes->
next; n != NULL; n = n->next) {
292 mask &= ~(last ^ n->code);
298 while (mask & 0x8000000000000000LL) {
302 count -=
sizeof(
ir_code) * CHAR_BIT - remote->
bits;
305 if (count % 8 && (remote->
bits - count) % 8)
309 for (i = 0; i < count; i++) {
313 remote->
bits -= count;
314 mask = mask << (remote->
bits);
318 codes = remote->codes;
319 while (codes->
name != NULL) {
320 codes->
code &= ~mask;
321 for (n = codes->
next; n != NULL; n = n->next)
329 void get_post_data(
struct ir_remote* remote)
336 if (remote->
bits == 0)
340 codes = remote->codes;
341 if (codes->
name == NULL)
345 if (codes->
name == NULL)
347 while (codes->
name != NULL) {
348 mask &= ~(last ^ codes->
code);
350 for (n = codes->
next; n != NULL; n = n->next) {
351 mask &= ~(last ^ n->code);
362 if (count % 8 && (remote->
bits - count) % 8)
366 for (i = 0; i < count; i++) {
370 remote->
bits -= count;
374 codes = remote->codes;
375 while (codes->
name != NULL) {
377 for (n = codes->
next; n != NULL; n = n->next)
378 n->code = n->code >> count;
385 void remove_pre_data(
struct ir_remote* remote)
391 || remote->pre_p != 0
392 || remote->
pre_s != 0)
394 for (codes = remote->codes; codes->
name != NULL; codes++) {
396 for (n = codes->
next; n != NULL; n = n->next)
405 void remove_post_data(
struct ir_remote* remote)
412 for (codes = remote->codes; codes->
name != NULL; codes++) {
415 for (n = codes->
next; n != NULL; n = n->next) {
426 void invert_data(
struct ir_remote* remote)
436 remote->pone = remote->pzero;
442 if (has_pre(remote)) {
447 if (has_post(remote)) {
452 if (remote->
bits == 0)
456 mask = gen_mask(remote->
bits);
457 for (codes = remote->codes; codes->
name != NULL; codes++) {
459 for (n = codes->
next; n != NULL; n = n->next)
465 void remove_trail(
struct ir_remote* remote)
469 if (!is_space_enc(remote))
473 if (expect(remote, remote->pone, remote->pzero)
474 || expect(remote, remote->pzero, remote->pone))
476 if (!(expect(remote, remote->
sone, remote->
szero)
477 && expect(remote, remote->
szero, remote->
sone)))
479 if (expect(remote, remote->
ptrail, remote->pone))
481 else if (expect(remote, remote->
ptrail, remote->pzero))
493 void for_each_remote(
struct ir_remote* remotes, remote_func func)
498 while (remote != NULL) {
500 remote = remote->next;
505 static int mywaitfordata(__u32 maxusec)
508 struct pollfd pfd = {
512 ret = poll(&pfd, 1, maxusec / 1000);
513 }
while (ret == -1 && errno == EINTR);
515 if (ret == -1 && errno != EINTR)
516 logperror(LIRC_ERROR,
"mywaitfordata: poll() failed");
517 return (pfd.revents & POLLIN) != 0;
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;
572 logprintf(LIRC_DEBUG,
"delivering: %c%u\n",
573 data & PULSE_BIT ?
'p':
's', data & PULSE_MASK);
578 static struct lengths* new_length(lirc_t length)
582 l = malloc(
sizeof(
struct lengths));
587 l->lower_bound = length / 100 * 100;
588 l->upper_bound = length / 100 * 100 + 99;
589 l->min = l->max = length;
595 void unlink_length(
struct lengths** first,
struct lengths*
remove)
600 if (
remove == *first) {
601 *first =
remove->next;
605 scan = (*first)->next;
608 if (scan ==
remove) {
609 last->next =
remove->next;
616 logprintf(LIRC_ERROR,
"unlink_length(): report this bug!");
620 int add_length(
struct lengths** first, lirc_t length)
625 if (*first == NULL) {
626 *first = new_length(length);
633 if (l->lower_bound <= length && length <= l->upper_bound) {
636 l->min = min(l->min, length);
637 l->max = max(l->max, length);
643 last->next = new_length(length);
644 if (last->next == NULL)
650 void free_lengths(
struct lengths** firstp)
658 while (first != NULL) {
667 void free_all_lengths(
void)
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);
685 static void merge_lengths(
struct lengths* first)
697 while (inner != NULL) {
698 new_sum = l->sum + inner->sum;
699 new_count = l->count + inner->count;
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 *
709 && inner->min >= new_sum / new_count *
712 l->count = new_count;
713 l->upper_bound = max(l->upper_bound,
715 l->lower_bound = min(l->lower_bound,
717 l->min = min(l->min, inner->min);
718 l->max = max(l->max, inner->max);
720 last->next = inner->next;
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);
754 if (first->count > 0)
755 logprintf(LIRC_DEBUG,
"%u x %u", first->count,
756 (__u32)calc_signal(first));
759 if (scan->count > max_length->count)
762 logprintf(LIRC_DEBUG,
765 (__u32)calc_signal(scan));
774 int get_trail_length(
struct ir_remote* remote,
int interactive)
776 unsigned int sum = 0, max_count;
779 if (is_biphase(remote))
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",
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);
793 logprintf(LIRC_DEBUG,
"No trail pulse found.");
798 int get_lead_length(
struct ir_remote* remote,
int interactive)
800 unsigned int sum = 0, max_count;
806 if (!is_biphase(remote) || has_header(remote))
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",
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);
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;
829 a = calc_signal(max_length);
830 b = calc_signal(max2_length);
836 if (abs(2 * a - b) < b * eps / 100 || abs(2 * a - b) < aeps) {
837 logprintf(LIRC_DEBUG,
838 "Found hidden lead pulse: %lu",
843 logprintf(LIRC_DEBUG,
"No lead pulse found.");
848 int get_header_length(
struct ir_remote* remote,
int interactive)
850 unsigned int sum, max_count;
851 lirc_t headerp, headers;
855 if (first_headerp != NULL) {
856 max_plength = get_max_length(first_headerp, &sum);
857 max_count = max_plength->count;
859 logprintf(LIRC_DEBUG,
"No header data.");
862 logprintf(LIRC_DEBUG,
863 "get_header_length(): sum: %u, max_count %u",
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",
872 if (max_count >= sum * TH_HEADER / 100) {
873 headerp = calc_signal(max_plength);
874 headers = calc_signal(max_slength);
876 logprintf(LIRC_DEBUG,
877 "Found possible header: %lu %lu",
880 remote->phead = headerp;
881 remote->
shead = headers;
882 if (first_lengths < second_lengths) {
883 logprintf(LIRC_DEBUG,
884 "Header is not being repeated.");
890 logprintf(LIRC_DEBUG,
"No header found.");
895 int get_repeat_length(
struct ir_remote* remote,
int interactive)
897 unsigned int sum = 0, max_count;
898 lirc_t repeatp, repeats, repeat_gap;
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.");
909 logprintf(LIRC_DEBUG,
"No repeat code found.");
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",
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",
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!");
932 if (count_5repeats > count_3repeats
933 && has_header(remote))
935 repeatp = calc_signal(max_plength);
936 repeats = calc_signal(max_slength);
938 logprintf(LIRC_DEBUG,
939 "Found repeat code: %lu %lu",
942 remote->prepeat = repeatp;
945 max_slength = get_max_length(first_repeat_gap,
947 repeat_gap = calc_signal(max_slength);
948 logprintf(LIRC_DEBUG,
949 "Found repeat gap: %lu",
956 logprintf(LIRC_DEBUG,
"No repeat header found.");
961 void get_scheme(
struct ir_remote* remote,
int interactive)
963 unsigned int i, length = 0, sum = 0;
969 for (i = 1; i < MAX_SIGNALS; i++) {
974 logprintf(LIRC_DEBUG,
"%u: %u", i,
lengths[i]);
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);
981 if (
lengths[length] >= TH_SPACE_ENC * sum / 100) {
983 logprintf(LIRC_DEBUG,
984 "Space/pulse encoded remote control found.");
985 logprintf(LIRC_DEBUG,
"Signal length is %u.", length);
987 remote->
bits = length;
991 maxp = get_max_length(first_pulse, NULL);
992 unlink_length(&first_pulse, maxp);
993 if (first_pulse == NULL)
995 max2p = get_max_length(first_pulse, NULL);
996 maxp->next = first_pulse;
999 maxs = get_max_length(first_space, NULL);
1000 unlink_length(&first_space, maxs);
1001 if (first_space == NULL) {
1004 max2s = get_max_length(first_space, NULL);
1005 maxs->next = first_space;
1008 maxs = get_max_length(first_space, NULL);
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);
1018 logprintf(LIRC_DEBUG,
"RC-5 remote control found.");
1019 set_protocol(remote,
RC5);
1024 logprintf(LIRC_DEBUG,
"Suspicious data length: %u.", length);
1026 remote->
bits = length;
1031 int get_data_length(
struct ir_remote* remote,
int interactive)
1033 unsigned int sum = 0, max_count;
1034 lirc_t p1, p2, s1, s2;
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",
1045 if (max_count >= sum * TH_IS_BIT / 100) {
1046 unlink_length(&first_pulse, max_plength);
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));
1056 logprintf(LIRC_DEBUG,
", %u x %u",
1057 max2_plength->count,
1058 (__u32)calc_signal(max2_plength));
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",
1065 if (max_count >= sum * TH_IS_BIT / 100) {
1066 unlink_length(&first_space, max_slength);
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,
1078 (__u32)calc_signal(max_slength));
1083 max2_slength->count,
1084 (__u32)calc_signal(max2_slength));
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.");
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);
1104 (min(p1, p2) + max(p1, p2) / 2) / 2;
1106 (min(s1, s2) + max(s1, s2) / 2) / 2;
1107 remote->pzero = remote->pone;
1110 if (max2_plength == NULL
1111 && max2_slength == NULL) {
1112 logprintf(LIRC_NOTICE,
1113 "No encoding found");
1116 if (max2_plength && max2_slength) {
1117 logprintf(LIRC_NOTICE,
1118 "Unknown encoding found.");
1121 p1 = calc_signal(max_plength);
1122 s1 = calc_signal(max_slength);
1124 p2 = calc_signal(max2_plength);
1127 "Signals are pulse encoded.");
1128 remote->pone = max(p1, p2);
1130 remote->pzero = min(p1, p2);
1132 if (expect(remote, remote->
ptrail, p1)
1133 || expect(remote, remote->
ptrail,
1137 s2 = calc_signal(max2_slength);
1140 "Signals are space encoded.");
1142 remote->
sone = max(s1, s2);
1144 remote->
szero = min(s1, s2);
1147 if (has_header(remote)
1148 && (!has_repeat(remote)
1150 if (!is_biphase(remote)
1151 && ((expect(remote, remote->phead,
1162 remote->phead = remote->
shead = 0;
1164 logprintf(LIRC_DEBUG,
1167 if (is_biphase(remote)
1171 remote->
plead = remote->phead;
1172 remote->phead = remote->
shead = 0;
1174 logprintf(LIRC_DEBUG,
1178 if (is_biphase(remote)) {
1179 struct lengths* signal_length;
1183 get_max_length(first_signal_length,
1186 calc_signal(signal_length) -
1187 remote->
plead - remote->phead -
1190 (remote->pone + remote->
sone) / 2;
1191 remote->
bits = data_length / (remote->pone +
1198 (has_header(remote) ? 2 : 0) + 1 -
1199 (remote->
ptrail > 0 ? 2 : 0)) / 2;
1201 logprintf(LIRC_DEBUG,
1202 "Signal length is %d",
1204 free_lengths(&max_plength);
1205 free_lengths(&max_slength);
1208 free_lengths(&max_plength);
1210 logprintf(LIRC_NOTICE,
"Could not find data lengths.");
1215 enum get_gap_status get_gap_length(
struct gap_state* state,
1218 while (availabledata())
1220 if (!mywaitfordata(10000000)) {
1221 free_lengths(&(state->gaps));
1222 return STS_GAP_TIMEOUT;
1224 gettimeofday(&(state->start), NULL);
1225 while (availabledata())
1227 gettimeofday(&(state->end), NULL);
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;
1242 state->scan = state->scan->next;
1244 if (state->maxcount > state->lastmaxcount) {
1245 state->lastmaxcount = state->maxcount;
1246 return STS_GAP_GOT_ONE_PRESS;
1251 state->last = state->end;
1252 return STS_GAP_AGAIN;
1257 int needs_toggle_mask(
struct ir_remote* remote)
1261 if (!is_rc6(remote))
1263 if (remote->codes) {
1264 codes = remote->codes;
1265 while (codes->
name != NULL) {
1278 static void compute_lengths_4_signals(
void)
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);
1292 static void compute_lengths_6_signals(
void)
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);
1309 static void compute_lengths_many_signals(
struct lengths_state* state)
1313 merge_lengths(first_1lead);
1314 for (i = 2; i < state->
count - 2; i++) {
1316 add_length(&first_space,
signals[i]);
1317 merge_lengths(first_space);
1319 add_length(&first_pulse,
signals[i]);
1320 merge_lengths(first_pulse);
1324 merge_lengths(first_trail);
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);
1338 if (state->first_signal == 1) {
1340 first_length = state->
count - 2;
1342 }
else if (state->first_signal == 0
1343 && first_length - 2 == state->
count - 2) {
1354 enum lengths_status* again)
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);
1363 state->mode = MODE_HAVE_GAP;
1364 logprintf(LIRC_DEBUG,
"Found gap: %u", remote->
gap);
1365 *again = STS_LEN_AGAIN_INFO;
1376 enum lengths_status* again)
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;
1394 enum lengths_status get_lengths(
struct lengths_state* state,
1396 int force,
int interactive)
1400 static int lastmaxcount = 0;
1401 enum lengths_status again = STS_LEN_AGAIN;
1406 return STS_LEN_TIMEOUT;
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) {
1414 return STS_LEN_AGAIN;
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
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);
1433 state->maxspace = 0;
1436 scan = scan_gap1(state,
1441 scan = scan_gap2(state,
1447 state->mode = MODE_HAVE_GAP;
1450 state->remaining_gap =
1454 remote->
gap - state->data : 0)
1455 : (has_repeat_gap(remote) ?
1457 repeat_gap : remote->
gap);
1460 return STS_LEN_RAW_OK;
1462 return STS_LEN_AGAIN_INFO;
1464 lastmaxcount = maxcount;
1469 (state->average * count_spaces + state->data)
1470 / (count_spaces + 1);
1472 if (state->data > state->maxspace)
1473 state->maxspace = state->data;
1475 if (state->
count > SAMPLES * MAX_SIGNALS * 2) {
1477 return STS_LEN_NO_GAP_FOUND;
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;
1486 return STS_LEN_TOO_LONG;
1488 if (is_const(remote))
1489 state->remaining_gap =
1490 remote->
gap > state->
sum ?
1491 remote->
gap - state->
sum : 0;
1493 state->remaining_gap = remote->
gap;
1494 state->
sum += state->data & PULSE_MASK;
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)) {
1504 if (state->
count == 4) {
1506 compute_lengths_4_signals();
1507 }
else if (state->
count == 6) {
1509 compute_lengths_6_signals();
1510 }
else if (state->
count > 6) {
1512 compute_lengths_many_signals(state);
1522 return STS_LEN_NO_GAP_FOUND;
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))
1533 return state->retval ==
1534 0 ? STS_LEN_FAIL : STS_LEN_OK;
1536 if ((state->data & PULSE_MASK) <=
1537 (state->remaining_gap + state->header) *
1539 || (state->data & PULSE_MASK) <=
1540 (state->remaining_gap + state->header) + aeps) {
1541 state->first_signal = 0;
1544 state->first_signal = 1;
1548 return STS_LEN_AGAIN;
1552 enum toggle_status get_toggle_bit_mask(
struct toggle_state* state,
1559 if (!state->inited) {
1561 while (availabledata())
1565 if (state->retries <= 0) {
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",
1573 return STS_TGL_FOUND;
1575 if (!mywaitfordata(10000000))
1576 return STS_TGL_TIMEOUT;
1578 if (is_rc6(remote) && remote->
rc6_mask == 0) {
1579 for (i = 0, mask = 1; i < remote->
bits; i++, mask <<= 1) {
1583 if (state->success) {
1591 if (!state->success)
1596 if (state->success) {
1603 if (state->success) {
1604 if (state->flag == 0) {
1606 state->first = decode_ctx.
code;
1608 || decode_ctx.
code != state->last) {
1610 mask = state->first ^ decode_ctx.
code;
1611 if (!state->found && mask) {
1612 set_toggle_bit_mask(remote, mask);
1616 state->repeats / state->seq;
1619 state->last = decode_ctx.
code;
1620 return STS_TGL_GOT_ONE_PRESS;
1623 state->last = decode_ctx.
code;
1626 while (availabledata())
1629 return STS_TGL_AGAIN;
1636 enum lengths_status status = STS_LEN_AGAIN;
1638 while (status == STS_LEN_AGAIN) {
1639 status = get_lengths(lengths_state, &remote, 0, 0);
1641 case STS_LEN_AGAIN_INFO:
1642 status = STS_LEN_AGAIN;
1649 logprintf(LIRC_ERROR,
"get_lengths() failure");
1651 case STS_LEN_RAW_OK:
1652 logprintf(LIRC_ERROR,
"raw analyse result?!");
1654 case STS_LEN_TIMEOUT:
1655 logprintf(LIRC_ERROR,
"analyse timeout?!");
1657 case STS_LEN_NO_GAP_FOUND:
1658 logprintf(LIRC_ERROR,
"analyse, no gap?!");
1660 case STS_LEN_TOO_LONG:
1661 logprintf(LIRC_ERROR,
"analyse, signal too long?!");
1664 logprintf(LIRC_ERROR,
1665 "Cannot read raw data (%d)",
1679 struct lengths_state lengths_state;
1683 size_t new_codes_count = 100;
1687 if (!is_raw(raw_data)) {
1688 logprintf(LIRC_ERROR,
1689 "remote %s not in raw mode, ignoring",
1694 aeps = raw_data->
aeps;
1695 eps = raw_data->
eps;
1696 emulation_data = raw_data;
1698 current_code = NULL;
1700 memset(&remote, 0,
sizeof(remote));
1701 lengths_state_init(&lengths_state);
1702 if (!analyse_get_lengths(&lengths_state))
1705 if (is_rc6(&remote) && remote.
bits >= 5)
1713 new_codes = malloc(new_codes_count *
sizeof(*new_codes));
1714 if (new_codes == NULL) {
1715 logprintf(LIRC_ERROR,
"Out of memory");
1718 memset(new_codes, 0, new_codes_count *
sizeof(*new_codes));
1719 codes = raw_data->codes;
1720 while (codes->
name != NULL) {
1722 current_code = NULL;
1730 logprintf(LIRC_WARNING,
1731 "Decoding of %s failed", codes->
name);
1733 if (new_index + 1 >= new_codes_count) {
1736 new_codes_count *= 2;
1740 sizeof(*new_codes));
1741 if (renew_codes == NULL) {
1742 logprintf(LIRC_ERROR,
1747 memset(&new_codes[new_codes_count / 2],
1749 new_codes_count / 2 *
1750 sizeof(*new_codes));
1751 new_codes = renew_codes;
1755 code = decode_ctx.
code;
1757 code2 = decode_ctx.
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,
1766 new_codes[new_index].
next->code =
1770 new_codes[new_index].
name = codes->
name;
1771 new_codes[new_index].
code = decode_ctx.
code;
1776 new_codes[new_index].
name = NULL;
1777 remote.codes = new_codes;
1778 fprint_remotes(stdout, &remote, opts->commandline);
1779 remote.codes = NULL;
1786 int do_analyse(
const struct opts* opts,
struct main_state* state)
1792 f = fopen(opts->filename,
"r");
1794 fprintf(stderr,
"Cannot open file: %s\n", opts->filename);
1799 fprintf(stderr,
"Cannot parse file: %s\n", opts->filename);
1802 for (; r != NULL; r = r->next) {
1804 logprintf(LIRC_ERROR,
1805 "remote %s not in raw mode, ignoring",
1809 analyse_remote(r, opts);
1815 ssize_t raw_read(
void* buffer,
size_t size,
unsigned int timeout_us)
1817 if (!mywaitfordata(timeout_us))
1828 if (!is_space(btn_state->data)) {
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);
1838 r = btn_state->data > ref;
1840 r = btn_state->data > (remote.
gap * (100 - remote.
eps)) / 100;
1846 enum button_status record_buttons(
struct button_state* btn_state,
1847 enum button_status last_status,
1849 const struct opts* opts)
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";
1860 enum button_status sts;
1862 if (btn_state->no_data) {
1863 btn_state->no_data = 0;
1864 return STS_BTN_TIMEOUT;
1866 switch (last_status) {
1868 return STS_BTN_GET_NAME;
1869 case STS_BTN_GET_NAME:
1870 if (strchr(btn_state->buffer,
' ') != NULL) {
1871 btn_state_set_message(
1873 "The name must not contain any whitespace.");
1874 return STS_BTN_SOFT_ERROR;
1876 if (strchr(btn_state->buffer,
'\t') != NULL) {
1877 btn_state_set_message(
1879 "The name must not contain any whitespace.");
1880 return STS_BTN_SOFT_ERROR;
1882 if (strcasecmp(btn_state->buffer,
"begin") == 0) {
1883 btn_state_set_message(
1885 "'%s' is not allowed as button name\n",
1887 return STS_BTN_SOFT_ERROR;
1889 if (strcasecmp(btn_state->buffer,
"end") == 0) {
1890 btn_state_set_message(
1892 "'%s' is not allowed as button name\n",
1894 return STS_BTN_SOFT_ERROR;
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(
1902 "'%s' is not in name space"
1903 " (use --disable-namespace to override)\n",
1905 return STS_BTN_SOFT_ERROR;
1907 return STS_BTN_INIT_DATA;
1908 case STS_BTN_INIT_DATA:
1912 while (availabledata())
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;
1926 while (availabledata()) {
1930 &(state->decode_ctx))) {
1937 btn_state_set_message(
1939 "Could not reset hardware.\n");
1940 return STS_BTN_HARD_ERROR;
1942 btn_state_set_message(btn_state,
1943 "Cannot decode data\n");
1945 return STS_BTN_SOFT_ERROR;
1947 btn_state->
ncode.
name = btn_state->buffer;
1951 &(state->decode_ctx))) {
1952 code2 = state->decode_ctx.
code;
1954 if (state->decode_ctx.
code != code2) {
1956 malloc(IR_CODE_NODE_SIZE);
1968 return STS_BTN_BUTTON_DONE;
1969 case STS_BTN_GET_RAW_DATA:
1970 btn_state->count = 0;
1972 while (btn_state->count < MAX_SIGNALS) {
1973 if (btn_state->count == 0)
1976 timeout = remote.
gap * 5;
1978 if (!btn_state->data) {
1979 if (btn_state->count == 0)
1980 return STS_BTN_TIMEOUT;
1981 btn_state->data = remote.
gap;
1983 if (btn_state->count == 0) {
1984 if (!is_space(btn_state->data)
1985 || btn_state->data <
1990 btn_state->count = 0;
1991 btn_state_set_message(
1993 "Something went wrong.");
1994 return STS_BTN_SOFT_ERROR;
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(
2006 btn_state->count - 1);
2009 btn_state->count = 0;
2010 return STS_BTN_SOFT_ERROR;
2015 btn_state->count - 1;
2019 signals[btn_state->count - 1] =
2020 btn_state->data & PULSE_MASK;
2022 btn_state->data & PULSE_MASK;
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;
2031 return STS_BTN_BUTTON_DONE;
2032 case STS_BTN_RECORD_DONE:
2033 if (is_raw(&remote))
2034 return STS_BTN_ALL_DONE;
2036 btn_state_set_message(btn_state,
2037 "Could not reset hardware.");
2038 return STS_BTN_HARD_ERROR;
2040 return STS_BTN_BUTTONS_DONE;
2041 case STS_BTN_BUTTONS_DONE:
2042 f = fopen(opts->tmpfile,
"r");
2044 btn_state_set_message(btn_state,
2045 "Could not reopen config file");
2046 return STS_BTN_HARD_ERROR;
2050 if (my_remote == NULL) {
2051 btn_state_set_message(
2054 "config file contains no valid remote");
2055 return STS_BTN_HARD_ERROR;
2057 if (my_remote == (
void*)-1) {
2058 btn_state_set_message(
2061 "Reading of config file failed");
2062 return STS_BTN_HARD_ERROR;
2064 sts = STS_BTN_ALL_DONE;
2066 remote = *my_remote;
2069 if (!has_toggle_bit_mask(my_remote)) {
2070 if (!opts->using_template
2072 remote = *(my_remote);
2073 sts = STS_BTN_GET_TOGGLE_BITS;
2076 set_toggle_bit_mask(my_remote,
2081 if (!opts->update) {
2082 get_pre_data(my_remote);
2083 get_post_data(my_remote);
2085 remote = *my_remote;
2087 case STS_BTN_BUTTON_DONE:
2088 return STS_BTN_BUTTON_DONE;
2089 case STS_BTN_HARD_ERROR:
2090 return STS_BTN_HARD_ERROR;
2092 btn_state_set_message(btn_state,
2093 "record_buttons(): bad state: %d\n",
2095 return STS_BTN_HARD_ERROR;
2101 void config_file_setup(
struct main_state* state,
const struct opts* opts)
2103 state->fout = fopen(opts->tmpfile,
"w");
2104 if (state->fout == NULL) {
2105 logprintf(LIRC_ERROR,
2106 "Could not open new config file %s",
2108 logperror(LIRC_ERROR,
"While opening temporary file for write");
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);
2120 int config_file_finish(
struct main_state* state,
const struct opts* opts)
2122 state->fout = fopen(opts->filename,
"w");
2123 if (state->fout == NULL) {
2125 "While opening \"%s\" for write",
2129 fprint_copyright(state->fout);
2130 fprint_remotes(state->fout, &remote, opts->commandline);
void rec_buffer_init(void)
struct ir_remote * last_remote
int(*const decode_func)(struct ir_remote *remote, struct decode_ctx_t *ctx)
struct ir_code_node * next
int receive_decode(struct ir_remote *remote, struct decode_ctx_t *ctx)
char *(*const rec_func)(struct ir_remote *remotes)
lirc_t(*const readdata)(lirc_t timeout)
int(*const deinit_func)(void)
Main include file for lirc applications.
const char * drop_sudo_root(int(*set_some_uid)(uid_t))
void logperror(loglevel_t prio, const char *fmt,...)
int default_open(const char *path)
int(*const init_func)(void)
const struct driver const * curr_driver
int rec_buffer_clear(void)
struct ir_remote * read_config(FILE *f, const char *name)