24 static lirc_t emulation_readdata(lirc_t timeout);
27 static const struct driver hw_emulation = {
29 .device =
"/dev/null",
30 .features = LIRC_CAN_REC_MODE2,
32 .rec_mode = LIRC_MODE_MODE2,
40 .readdata = emulation_readdata,
44 .driver_version =
"0.9.2" 47 static const int IR_CODE_NODE_SIZE =
sizeof(
struct ir_code_node);
52 unsigned int eps = 30;
57 static lirc_t signals[MAX_SIGNALS];
59 static struct ir_ncode* next_code = NULL;
60 static struct ir_ncode* current_code = NULL;
61 static int current_index = 0;
62 static int current_rep = 0;
64 static struct lengths* first_space = NULL;
65 static struct lengths* first_pulse = NULL;
66 static struct lengths* first_sum = NULL;
67 static struct lengths* first_gap = NULL;
68 static struct lengths* first_repeat_gap = NULL;
69 static struct lengths* first_signal_length = NULL;
70 static struct lengths* first_headerp = NULL;
71 static struct lengths* first_headers = NULL;
72 static struct lengths* first_1lead = NULL;
73 static struct lengths* first_3lead = NULL;
74 static struct lengths* first_trail = NULL;
75 static struct lengths* first_repeatp = NULL;
76 static struct lengths* first_repeats = NULL;
78 static __u32
lengths[MAX_SIGNALS];
79 static __u32 first_length, first_lengths, second_lengths;
80 static unsigned int count, count_spaces, count_signals;
81 static unsigned int count_3repeats, count_5repeats;
87 void btn_state_set_message(
struct button_state* state,
const char* fmt, ...)
97 static void fprint_copyright(FILE* fout)
100 "# Please take the time to finish this file as described in\n" 101 "# https://sourceforge.net/p/lirc-remotes/wiki/Checklist/\n" 102 "# and make it available to others by sending it to\n" 103 "# <lirc@bartelmus.de>\n");
108 int availabledata(
void)
110 struct pollfd pfd = {
116 ret = curl_poll(&pfd, 1, 0);
117 }
while (ret == -1 && errno == EINTR);
124 return pfd.revents & POLLIN ? 1 : 0;
135 case LIRC_MODE_MODE2:
136 while (availabledata())
139 case LIRC_MODE_LIRCCODE:
151 int resethw(
int started_as_root)
156 if (seteuid(0) == -1)
179 void gap_state_init(
struct gap_state* state)
181 memset(state, 0,
sizeof(
struct gap_state));
196 state->first_signal = -1;
205 state->retval = EXIT_SUCCESS;
209 void button_state_init(
struct button_state* state,
int started_as_root)
212 state->started_as_root = started_as_root;
213 state->retval = EXIT_SUCCESS;
217 static lirc_t calc_signal(
struct lengths* len)
221 return (lirc_t)(len->sum / len->count);
234 bits = bit_count(remote);
235 mask = ((
ir_code)1) << (bits - 1);
244 codes = remote->codes;
245 while (codes->
name != NULL) {
251 else if (bits == 15 && xor == 0x3ff) {
259 void get_pre_data(
struct ir_remote* remote)
266 if (remote->
bits == 0)
269 codes = remote->codes;
270 if (codes->
name == NULL)
274 if (codes->
name == NULL)
276 while (codes->
name != NULL) {
277 mask &= ~(last ^ codes->
code);
279 for (n = codes->
next; n != NULL; n = n->next) {
280 mask &= ~(last ^ n->code);
286 while (mask & 0x8000000000000000LL) {
290 count -=
sizeof(
ir_code) * CHAR_BIT - remote->
bits;
293 if (count % 8 && (remote->
bits - count) % 8)
297 for (i = 0; i < count; i++) {
301 remote->
bits -= count;
302 mask = mask << (remote->
bits);
306 codes = remote->codes;
307 while (codes->
name != NULL) {
308 codes->
code &= ~mask;
309 for (n = codes->
next; n != NULL; n = n->next)
317 void get_post_data(
struct ir_remote* remote)
324 if (remote->
bits == 0)
328 codes = remote->codes;
329 if (codes->
name == NULL)
333 if (codes->
name == NULL)
335 while (codes->
name != NULL) {
336 mask &= ~(last ^ codes->
code);
338 for (n = codes->
next; n != NULL; n = n->next) {
339 mask &= ~(last ^ n->code);
350 if (count % 8 && (remote->
bits - count) % 8)
354 for (i = 0; i < count; i++) {
358 remote->
bits -= count;
362 codes = remote->codes;
363 while (codes->
name != NULL) {
365 for (n = codes->
next; n != NULL; n = n->next)
366 n->code = n->code >> count;
373 void remove_pre_data(
struct ir_remote* remote)
379 || remote->pre_p != 0
380 || remote->
pre_s != 0)
382 for (codes = remote->codes; codes->
name != NULL; codes++) {
384 for (n = codes->
next; n != NULL; n = n->next)
393 void remove_post_data(
struct ir_remote* remote)
400 for (codes = remote->codes; codes->
name != NULL; codes++) {
403 for (n = codes->
next; n != NULL; n = n->next) {
414 void invert_data(
struct ir_remote* remote)
424 remote->pone = remote->pzero;
430 if (has_pre(remote)) {
435 if (has_post(remote)) {
440 if (remote->
bits == 0)
444 mask = gen_mask(remote->
bits);
445 for (codes = remote->codes; codes->
name != NULL; codes++) {
447 for (n = codes->
next; n != NULL; n = n->next)
453 void remove_trail(
struct ir_remote* remote)
457 if (!is_space_enc(remote))
461 if (expect(remote, remote->pone, remote->pzero)
462 || expect(remote, remote->pzero, remote->pone))
464 if (!(expect(remote, remote->
sone, remote->
szero)
465 && expect(remote, remote->
szero, remote->
sone)))
467 if (expect(remote, remote->
ptrail, remote->pone))
469 else if (expect(remote, remote->
ptrail, remote->pzero))
481 void for_each_remote(
struct ir_remote* remotes, remote_func func)
486 while (remote != NULL) {
488 remote = remote->next;
493 static int mywaitfordata(__u32 maxusec)
496 struct pollfd pfd = {
500 ret = curl_poll(&pfd, 1, maxusec / 1000);
501 }
while (ret == -1 && errno == EINTR);
503 if (ret == -1 && errno != EINTR)
505 return (pfd.revents & POLLIN) != 0;
509 static lirc_t emulation_readdata(lirc_t timeout)
511 static lirc_t sum = 0;
514 if (current_code == NULL) {
517 current_code = next_code;
519 current_code = emulation_data->codes;
523 if (current_code->
name == NULL) {
527 if (current_index >= current_code->
length) {
529 current_code = next_code;
532 if (current_rep > 2) {
539 if (current_code->
name == NULL) {
541 return emulation_readdata(timeout);
544 if (is_const(emulation_data))
545 data = emulation_data->
gap - sum;
547 data = emulation_data->
gap;
552 data = current_code->
signals[current_index];
553 if ((current_index % 2) == 0)
556 sum += data & PULSE_MASK;
560 data & PULSE_BIT ?
'p':
's', data & PULSE_MASK);
565 static struct lengths* new_length(lirc_t length)
569 l = malloc(
sizeof(
struct lengths));
574 l->lower_bound = length / 100 * 100;
575 l->upper_bound = length / 100 * 100 + 99;
576 l->min = l->max = length;
582 void unlink_length(
struct lengths** first,
struct lengths*
remove)
587 if (
remove == *first) {
588 *first =
remove->next;
592 scan = (*first)->next;
595 if (scan ==
remove) {
596 last->next =
remove->next;
603 log_error(
"unlink_length(): report this bug!");
607 int add_length(
struct lengths** first, lirc_t length)
612 if (*first == NULL) {
613 *first = new_length(length);
620 if (l->lower_bound <= length && length <= l->upper_bound) {
623 l->min = min(l->min, length);
624 l->max = max(l->max, length);
630 last->next = new_length(length);
631 if (last->next == NULL)
637 void free_lengths(
struct lengths** firstp)
645 while (first != NULL) {
654 void free_all_lengths(
void)
656 free_lengths(&first_space);
657 free_lengths(&first_pulse);
658 free_lengths(&first_sum);
659 free_lengths(&first_gap);
660 free_lengths(&first_repeat_gap);
661 free_lengths(&first_signal_length);
662 free_lengths(&first_headerp);
663 free_lengths(&first_headers);
664 free_lengths(&first_1lead);
665 free_lengths(&first_3lead);
666 free_lengths(&first_trail);
667 free_lengths(&first_repeatp);
668 free_lengths(&first_repeats);
672 static void merge_lengths(
struct lengths* first)
684 while (inner != NULL) {
685 new_sum = l->sum + inner->sum;
686 new_count = l->count + inner->count;
688 if ((l->max <= new_sum / new_count + aeps
689 && l->min + aeps >= new_sum / new_count
690 && inner->max <= new_sum / new_count + aeps
691 && inner->min + aeps >= new_sum / new_count)
692 || (l->max <= new_sum / new_count * (100 + eps)
693 && l->min >= new_sum / new_count * (100 - eps)
694 && inner->max <= new_sum / new_count *
696 && inner->min >= new_sum / new_count *
699 l->count = new_count;
700 l->upper_bound = max(l->upper_bound,
702 l->lower_bound = min(l->lower_bound,
704 l->min = min(l->min, inner->min);
705 l->max = max(l->max, inner->max);
707 last->next = inner->next;
716 for (l = first; l != NULL; l = l->next) {
718 l->count, (__u32)calc_signal(l),
719 (__u32)l->min, (__u32)l->max);
741 if (first->count > 0)
743 (__u32)calc_signal(first));
746 if (scan->count > max_length->count)
752 (__u32)calc_signal(scan));
761 int get_trail_length(
struct ir_remote* remote,
int interactive)
763 unsigned int sum = 0, max_count;
766 if (is_biphase(remote))
769 max_length = get_max_length(first_trail, &sum);
770 max_count = max_length->count;
772 "get_trail_length(): sum: %u, max_count %u",
774 if (max_count >= sum * TH_TRAIL / 100) {
776 (__u32)calc_signal(max_length));
777 remote->
ptrail = calc_signal(max_length);
785 int get_lead_length(
struct ir_remote* remote,
int interactive)
787 unsigned int sum = 0, max_count;
793 if (!is_biphase(remote) || has_header(remote))
798 first_lead = has_header(remote) ? first_3lead : first_1lead;
799 max_length = get_max_length(first_lead, &sum);
800 max_count = max_length->count;
802 "get_lead_length(): sum: %u, max_count %u",
804 if (max_count >= sum * TH_LEAD / 100) {
806 "Found lead pulse: %lu",
807 (__u32)calc_signal(max_length));
808 remote->
plead = calc_signal(max_length);
811 unlink_length(&first_lead, max_length);
812 max2_length = get_max_length(first_lead, &sum);
813 max_length->next = first_lead;
814 first_lead = max_length;
816 a = calc_signal(max_length);
817 b = calc_signal(max2_length);
823 if (abs(2 * a - b) < b * eps / 100 || abs(2 * a - b) < aeps) {
825 "Found hidden lead pulse: %lu",
835 int get_header_length(
struct ir_remote* remote,
int interactive)
837 unsigned int sum, max_count;
838 lirc_t headerp, headers;
842 if (first_headerp != NULL) {
843 max_plength = get_max_length(first_headerp, &sum);
844 max_count = max_plength->count;
850 "get_header_length(): sum: %u, max_count %u",
853 if (max_count >= sum * TH_HEADER / 100) {
854 max_slength = get_max_length(first_headers, &sum);
855 max_count = max_slength->count;
857 "get_header_length(): sum: %u, max_count %u",
859 if (max_count >= sum * TH_HEADER / 100) {
860 headerp = calc_signal(max_plength);
861 headers = calc_signal(max_slength);
864 "Found possible header: %lu %lu",
867 remote->phead = headerp;
868 remote->
shead = headers;
869 if (first_lengths < second_lengths) {
871 "Header is not being repeated.");
882 int get_repeat_length(
struct ir_remote* remote,
int interactive)
884 unsigned int sum = 0, max_count;
885 lirc_t repeatp, repeats, repeat_gap;
889 if (!((count_3repeats > SAMPLES / 2 ? 1 : 0) ^
890 (count_5repeats > SAMPLES / 2 ? 1 : 0))) {
891 if (count_3repeats > SAMPLES / 2
892 || count_5repeats > SAMPLES / 2) {
900 max_plength = get_max_length(first_repeatp, &sum);
901 max_count = max_plength->count;
903 "get_repeat_length(): sum: %u, max_count %u",
905 if (max_count >= sum * TH_REPEAT / 100) {
906 max_slength = get_max_length(first_repeats, &sum);
907 max_count = max_slength->count;
909 "get_repeat_length(): sum: %u, max_count %u",
911 if (max_count >= sum * TH_REPEAT / 100) {
912 if (count_5repeats > count_3repeats
913 && !has_header(remote)) {
915 "Repeat code has header," 916 " but no header found!");
919 if (count_5repeats > count_3repeats
920 && has_header(remote))
922 repeatp = calc_signal(max_plength);
923 repeats = calc_signal(max_slength);
926 "Found repeat code: %lu %lu",
929 remote->prepeat = repeatp;
932 max_slength = get_max_length(first_repeat_gap,
934 repeat_gap = calc_signal(max_slength);
936 "Found repeat gap: %lu",
948 void get_scheme(
struct ir_remote* remote,
int interactive)
950 unsigned int i, length = 0, sum = 0;
956 for (i = 1; i < MAX_SIGNALS; i++) {
963 log_debug(
"get_scheme(): sum: %u length: %u signals: %u" 964 " first_lengths: %u second_lengths: %u\n",
965 sum, length + 1,
lengths[length],
966 first_lengths, second_lengths);
968 if (
lengths[length] >= TH_SPACE_ENC * sum / 100) {
971 "Space/pulse encoded remote control found.");
972 log_debug(
"Signal length is %u.", length);
974 remote->
bits = length;
978 maxp = get_max_length(first_pulse, NULL);
979 unlink_length(&first_pulse, maxp);
980 if (first_pulse == NULL)
982 max2p = get_max_length(first_pulse, NULL);
983 maxp->next = first_pulse;
986 maxs = get_max_length(first_space, NULL);
987 unlink_length(&first_space, maxs);
988 if (first_space == NULL) {
991 max2s = get_max_length(first_space, NULL);
992 maxs->next = first_space;
995 maxs = get_max_length(first_space, NULL);
998 && (calc_signal(maxp) < TH_RC6_SIGNAL
999 || calc_signal(max2p) < TH_RC6_SIGNAL)
1000 && (calc_signal(maxs) < TH_RC6_SIGNAL
1001 || calc_signal(max2s) < TH_RC6_SIGNAL)) {
1002 log_debug(
"RC-6 remote control found.");
1003 set_protocol(remote,
RC6);
1005 log_debug(
"RC-5 remote control found.");
1006 set_protocol(remote,
RC5);
1011 log_debug(
"Suspicious data length: %u.", length);
1013 remote->
bits = length;
1018 int get_data_length(
struct ir_remote* remote,
int interactive)
1020 unsigned int sum = 0, max_count;
1021 lirc_t p1, p2, s1, s2;
1027 max_plength = get_max_length(first_pulse, &sum);
1028 max_count = max_plength->count;
1029 log_debug(
"get_data_length(): sum: %u, max_count %u",
1032 if (max_count >= sum * TH_IS_BIT / 100) {
1033 unlink_length(&first_pulse, max_plength);
1035 max2_plength = get_max_length(first_pulse, NULL);
1036 if (max2_plength != NULL)
1037 if (max2_plength->count < max_count * TH_IS_BIT / 100)
1038 max2_plength = NULL;
1040 log_debug(
"%u x %u", max_plength->count,
1041 (__u32)calc_signal(max_plength));
1044 max2_plength->count,
1045 (__u32)calc_signal(max2_plength));
1047 max_slength = get_max_length(first_space, &sum);
1048 max_count = max_slength->count;
1050 "get_data_length(): sum: %u, max_count %u",
1052 if (max_count >= sum * TH_IS_BIT / 100) {
1053 unlink_length(&first_space, max_slength);
1055 max2_slength = get_max_length(first_space, NULL);
1056 if (max2_slength != NULL)
1057 if (max2_slength->count <
1058 max_count * TH_IS_BIT / 100)
1059 max2_slength = NULL;
1060 if (max_count >= sum * TH_IS_BIT / 100) {
1065 (__u32)calc_signal(max_slength));
1069 max2_slength->count,
1070 (__u32)calc_signal(max2_slength));
1074 remote->
aeps = aeps;
1075 if (is_biphase(remote)) {
1076 if (max2_plength == NULL
1077 || max2_slength == NULL) {
1079 "Unknown encoding found.");
1083 "Signals are biphase encoded.");
1084 p1 = calc_signal(max_plength);
1085 p2 = calc_signal(max2_plength);
1086 s1 = calc_signal(max_slength);
1087 s2 = calc_signal(max2_slength);
1090 (min(p1, p2) + max(p1, p2) / 2) / 2;
1092 (min(s1, s2) + max(s1, s2) / 2) / 2;
1093 remote->pzero = remote->pone;
1096 if (max2_plength == NULL
1097 && max2_slength == NULL) {
1099 "No encoding found");
1102 if (max2_plength && max2_slength) {
1104 "Unknown encoding found.");
1107 p1 = calc_signal(max_plength);
1108 s1 = calc_signal(max_slength);
1110 p2 = calc_signal(max2_plength);
1111 log_debug(
"Signals are pulse encoded.");
1112 remote->pone = max(p1, p2);
1114 remote->pzero = min(p1, p2);
1116 if (expect(remote, remote->
ptrail, p1)
1117 || expect(remote, remote->
ptrail,
1121 s2 = calc_signal(max2_slength);
1122 log_debug(
"Signals are space encoded.");
1124 remote->
sone = max(s1, s2);
1126 remote->
szero = min(s1, s2);
1129 if (has_header(remote)
1130 && (!has_repeat(remote)
1132 if (!is_biphase(remote)
1133 && ((expect(remote, remote->phead,
1144 remote->phead = remote->
shead = 0;
1149 if (is_biphase(remote)
1153 remote->
plead = remote->phead;
1154 remote->phead = remote->
shead = 0;
1160 if (is_biphase(remote)) {
1161 struct lengths* signal_length;
1165 get_max_length(first_signal_length,
1168 calc_signal(signal_length) -
1169 remote->
plead - remote->phead -
1172 (remote->pone + remote->
sone) / 2;
1173 remote->
bits = data_length / (remote->pone +
1180 (has_header(remote) ? 2 : 0) + 1 -
1181 (remote->
ptrail > 0 ? 2 : 0)) / 2;
1184 "Signal length is %d",
1186 free_lengths(&max_plength);
1187 free_lengths(&max_slength);
1190 free_lengths(&max_plength);
1197 enum get_gap_status get_gap_length(
struct gap_state* state,
1200 while (availabledata())
1202 if (!mywaitfordata(10000000)) {
1203 free_lengths(&(state->gaps));
1204 return STS_GAP_TIMEOUT;
1206 gettimeofday(&(state->start), NULL);
1207 while (availabledata())
1209 gettimeofday(&(state->end), NULL);
1211 state->gap = time_elapsed(&(state->last), &(state->start));
1212 add_length(&(state->gaps), state->gap);
1213 merge_lengths(state->gaps);
1214 state->maxcount = 0;
1215 state->scan = state->gaps;
1216 while (state->scan) {
1217 state->maxcount = max(state->maxcount,
1218 state->scan->count);
1219 if (state->scan->count > SAMPLES) {
1220 remote->
gap = calc_signal(state->scan);
1221 free_lengths(&(state->gaps));
1222 return STS_GAP_FOUND;
1224 state->scan = state->scan->next;
1226 if (state->maxcount > state->lastmaxcount) {
1227 state->lastmaxcount = state->maxcount;
1228 return STS_GAP_GOT_ONE_PRESS;
1233 state->last = state->end;
1234 return STS_GAP_AGAIN;
1239 int needs_toggle_mask(
struct ir_remote* remote)
1243 if (!is_rc6(remote))
1245 if (remote->codes) {
1246 codes = remote->codes;
1247 while (codes->
name != NULL) {
1260 static void compute_lengths_4_signals(
void)
1262 add_length(&first_repeatp,
signals[0]);
1263 merge_lengths(first_repeatp);
1264 add_length(&first_repeats,
signals[1]);
1265 merge_lengths(first_repeats);
1266 add_length(&first_trail,
signals[2]);
1267 merge_lengths(first_trail);
1268 add_length(&first_repeat_gap,
signals[3]);
1269 merge_lengths(first_repeat_gap);
1274 static void compute_lengths_6_signals(
void)
1276 add_length(&first_headerp,
signals[0]);
1277 merge_lengths(first_headerp);
1278 add_length(&first_headers,
signals[1]);
1279 merge_lengths(first_headers);
1280 add_length(&first_repeatp,
signals[2]);
1281 merge_lengths(first_repeatp);
1282 add_length(&first_repeats,
signals[3]);
1283 merge_lengths(first_repeats);
1284 add_length(&first_trail,
signals[4]);
1285 merge_lengths(first_trail);
1286 add_length(&first_repeat_gap,
signals[5]);
1287 merge_lengths(first_repeat_gap);
1291 static void compute_lengths_many_signals(
struct lengths_state* state)
1295 add_length(&first_1lead,
signals[0]);
1296 merge_lengths(first_1lead);
1297 for (i = 2; i < state->
count - 2; i++) {
1299 add_length(&first_space,
signals[i]);
1300 merge_lengths(first_space);
1302 add_length(&first_pulse,
signals[i]);
1303 merge_lengths(first_pulse);
1307 merge_lengths(first_trail);
1309 add_length(&first_signal_length, state->
sum - state->data);
1310 merge_lengths(first_signal_length);
1311 if (state->first_signal == 1
1312 || (first_length > 2
1313 && first_length - 2 != state->
count - 2)) {
1314 add_length(&first_3lead,
signals[2]);
1315 merge_lengths(first_3lead);
1316 add_length(&first_headerp,
signals[0]);
1317 merge_lengths(first_headerp);
1318 add_length(&first_headers,
signals[1]);
1319 merge_lengths(first_headers);
1321 if (state->first_signal == 1) {
1323 first_length = state->
count - 2;
1325 }
else if (state->first_signal == 0
1326 && first_length - 2 == state->
count - 2) {
1337 enum lengths_status* again)
1341 for (scan = first_sum; scan; scan = scan->next) {
1342 *maxcount = max(*maxcount, scan->count);
1343 if (scan->count > SAMPLES) {
1344 remote->
gap = calc_signal(scan);
1346 state->mode = MODE_HAVE_GAP;
1348 *again = STS_LEN_AGAIN_INFO;
1359 enum lengths_status* again)
1363 for (scan = first_gap; scan; scan = scan->next) {
1364 *maxcount = max(*maxcount, scan->count);
1365 if (scan->count > SAMPLES) {
1366 remote->
gap = calc_signal(scan);
1367 state->mode = MODE_HAVE_GAP;
1369 *again = STS_LEN_AGAIN_INFO;
1377 enum lengths_status get_lengths(
struct lengths_state* state,
1379 int force,
int interactive)
1383 static int lastmaxcount = 0;
1384 enum lengths_status again = STS_LEN_AGAIN;
1389 return STS_LEN_TIMEOUT;
1392 if (state->mode == MODE_GET_GAP) {
1393 state->
sum += state->data & PULSE_MASK;
1394 if (state->average == 0 && is_space(state->data)) {
1395 if (state->data > 100000) {
1397 return STS_LEN_AGAIN;
1399 state->average = state->data;
1400 state->maxspace = state->data;
1401 }
else if (is_space(state->data)) {
1402 if (state->data > MIN_GAP
1403 || state->data > 100 * state->average
1405 || (state->data >= 5000 && count_spaces > 10
1406 && state->data > 5 * state->average)
1407 || (state->data < 5000 && count_spaces > 10
1408 && state->data > 5 * state->maxspace / 2)) {
1409 add_length(&first_sum, state->
sum);
1410 merge_lengths(first_sum);
1411 add_length(&first_gap, state->data);
1412 merge_lengths(first_gap);
1416 state->maxspace = 0;
1419 scan = scan_gap1(state,
1424 scan = scan_gap2(state,
1430 state->mode = MODE_HAVE_GAP;
1433 state->remaining_gap =
1437 remote->
gap - state->data : 0)
1438 : (has_repeat_gap(remote) ?
1440 repeat_gap : remote->
gap);
1443 return STS_LEN_RAW_OK;
1445 return STS_LEN_AGAIN_INFO;
1447 lastmaxcount = maxcount;
1452 (state->average * count_spaces + state->data)
1453 / (count_spaces + 1);
1455 if (state->data > state->maxspace)
1456 state->maxspace = state->data;
1458 if (state->
count > SAMPLES * MAX_SIGNALS * 2) {
1460 return STS_LEN_NO_GAP_FOUND;
1463 return STS_LEN_AGAIN;
1464 }
else if (state->mode == MODE_HAVE_GAP) {
1465 if (state->
count <= MAX_SIGNALS) {
1466 signals[state->
count - 1] = state->data & PULSE_MASK;
1469 return STS_LEN_TOO_LONG;
1471 if (is_const(remote))
1472 state->remaining_gap =
1473 remote->
gap > state->
sum ?
1474 remote->
gap - state->
sum : 0;
1476 state->remaining_gap = remote->
gap;
1477 state->
sum += state->data & PULSE_MASK;
1479 if (state->
count > 2
1480 && ((state->data & PULSE_MASK) >=
1481 state->remaining_gap * (100 - eps) / 100
1482 || (state->data & PULSE_MASK) >=
1483 state->remaining_gap - aeps)) {
1484 if (is_space(state->data)) {
1487 if (state->
count == 4) {
1489 compute_lengths_4_signals();
1490 }
else if (state->
count == 6) {
1492 compute_lengths_6_signals();
1493 }
else if (state->
count > 6) {
1495 compute_lengths_many_signals(state);
1505 return STS_LEN_NO_GAP_FOUND;
1508 if (count_signals >= SAMPLES) {
1509 get_scheme(remote, interactive);
1510 if (!get_header_length(remote, interactive)
1511 || !get_trail_length(remote, interactive)
1512 || !get_lead_length(remote, interactive)
1513 || !get_repeat_length(remote, interactive)
1514 || !get_data_length(remote, interactive))
1516 return state->retval ==
1517 0 ? STS_LEN_FAIL : STS_LEN_OK;
1519 if ((state->data & PULSE_MASK) <=
1520 (state->remaining_gap + state->header) *
1522 || (state->data & PULSE_MASK) <=
1523 (state->remaining_gap + state->header) + aeps) {
1524 state->first_signal = 0;
1527 state->first_signal = 1;
1531 return STS_LEN_AGAIN;
1535 enum toggle_status get_toggle_bit_mask(
struct toggle_state* state,
1542 if (!state->inited) {
1544 while (availabledata())
1548 if (state->retries <= 0) {
1550 return STS_TGL_NOT_FOUND;
1551 if (state->seq > 0) {
1552 remote->
min_repeat = state->repeats / state->seq;
1556 return STS_TGL_FOUND;
1558 if (!mywaitfordata(10000000))
1559 return STS_TGL_TIMEOUT;
1561 if (is_rc6(remote) && remote->
rc6_mask == 0) {
1562 for (i = 0, mask = 1; i < remote->
bits; i++, mask <<= 1) {
1566 if (state->success) {
1574 if (!state->success)
1579 if (state->success) {
1586 if (state->success) {
1587 if (state->flag == 0) {
1589 state->first = decode_ctx.
code;
1591 || decode_ctx.
code != state->last) {
1593 mask = state->first ^ decode_ctx.
code;
1594 if (!state->found && mask) {
1595 set_toggle_bit_mask(remote, mask);
1599 state->repeats / state->seq;
1602 state->last = decode_ctx.
code;
1603 return STS_TGL_GOT_ONE_PRESS;
1606 state->last = decode_ctx.
code;
1609 while (availabledata())
1612 return STS_TGL_AGAIN;
1619 enum lengths_status status = STS_LEN_AGAIN;
1621 while (status == STS_LEN_AGAIN) {
1622 status = get_lengths(lengths_state, &remote, 0, 0);
1624 case STS_LEN_AGAIN_INFO:
1625 status = STS_LEN_AGAIN;
1634 case STS_LEN_RAW_OK:
1637 case STS_LEN_TIMEOUT:
1640 case STS_LEN_NO_GAP_FOUND:
1643 case STS_LEN_TOO_LONG:
1644 log_error(
"analyse, signal too long?!");
1661 struct lengths_state lengths_state;
1665 size_t new_codes_count = 100;
1669 if (!is_raw(raw_data)) {
1670 log_error(
"remote %s not in raw mode, ignoring",
1675 aeps = raw_data->
aeps;
1676 eps = raw_data->
eps;
1677 emulation_data = raw_data;
1679 current_code = NULL;
1681 memset(&remote, 0,
sizeof(remote));
1682 lengths_state_init(&lengths_state);
1683 if (!analyse_get_lengths(&lengths_state))
1686 if (is_rc6(&remote) && remote.
bits >= 5)
1694 new_codes = malloc(new_codes_count *
sizeof(*new_codes));
1695 if (new_codes == NULL) {
1699 memset(new_codes, 0, new_codes_count *
sizeof(*new_codes));
1700 codes = raw_data->codes;
1701 while (codes->
name != NULL) {
1703 current_code = NULL;
1712 "Decoding of %s failed", codes->
name);
1714 if (new_index + 1 >= new_codes_count) {
1717 new_codes_count *= 2;
1721 sizeof(*new_codes));
1722 if (renew_codes == NULL) {
1727 memset(&new_codes[new_codes_count / 2],
1729 new_codes_count / 2 *
1730 sizeof(*new_codes));
1731 new_codes = renew_codes;
1735 code = decode_ctx.
code;
1737 code2 = decode_ctx.
code;
1739 if (ret && code2 != decode_ctx.
code) {
1740 new_codes[new_index].
next =
1741 malloc(IR_CODE_NODE_SIZE);
1742 if (new_codes[new_index].next) {
1743 memset(new_codes[new_index].next,
1746 new_codes[new_index].
next->code =
1750 new_codes[new_index].
name = codes->
name;
1751 new_codes[new_index].
code = decode_ctx.
code;
1756 new_codes[new_index].
name = NULL;
1757 remote.codes = new_codes;
1758 fprint_remotes(stdout, &remote, opts->commandline);
1759 remote.codes = NULL;
1766 int do_analyse(
const struct opts* opts,
struct main_state* state)
1772 f = fopen(opts->filename,
"r");
1774 fprintf(stderr,
"Cannot open file: %s\n", opts->filename);
1779 fprintf(stderr,
"Cannot parse file: %s\n", opts->filename);
1782 for (; r != NULL; r = r->next) {
1784 log_error(
"remote %s not in raw mode, ignoring",
1788 analyse_remote(r, opts);
1794 ssize_t raw_read(
void* buffer,
size_t size,
unsigned int timeout_us)
1796 if (!mywaitfordata(timeout_us))
1807 if (!is_space(btn_state->data)) {
1809 }
else if (is_const(&remote)) {
1810 if (remote.
gap > btn_state->sum) {
1811 ref = (remote.
gap - btn_state->sum);
1812 ref *= (100 - remote.
eps);
1817 r = btn_state->data > ref;
1819 r = btn_state->data > (remote.
gap * (100 - remote.
eps)) / 100;
1825 enum button_status record_buttons(
struct button_state* btn_state,
1826 enum button_status last_status,
1828 const struct opts* opts)
1830 const char*
const MSG_BAD_LENGTH =
1831 "Signal length is %d\n" 1832 "That's weird because the signal length must be odd!\n";
1839 enum button_status sts;
1841 if (btn_state->no_data) {
1842 btn_state->no_data = 0;
1843 return STS_BTN_TIMEOUT;
1845 switch (last_status) {
1847 return STS_BTN_GET_NAME;
1848 case STS_BTN_GET_NAME:
1849 if (strchr(btn_state->buffer,
' ') != NULL) {
1850 btn_state_set_message(
1852 "The name must not contain any whitespace.");
1853 return STS_BTN_SOFT_ERROR;
1855 if (strchr(btn_state->buffer,
'\t') != NULL) {
1856 btn_state_set_message(
1858 "The name must not contain any whitespace.");
1859 return STS_BTN_SOFT_ERROR;
1861 if (strcasecmp(btn_state->buffer,
"begin") == 0) {
1862 btn_state_set_message(
1864 "'%s' is not allowed as button name\n",
1866 return STS_BTN_SOFT_ERROR;
1868 if (strcasecmp(btn_state->buffer,
"end") == 0) {
1869 btn_state_set_message(
1871 "'%s' is not allowed as button name\n",
1873 return STS_BTN_SOFT_ERROR;
1875 if (strlen(btn_state->buffer) == 0)
1876 return STS_BTN_RECORD_DONE;
1877 if (!opts->disable_namespace
1878 && !is_in_namespace(btn_state->buffer)) {
1879 btn_state_set_message(
1881 "'%s' is not in name space" 1882 " (use --disable-namespace to override)\n",
1884 return STS_BTN_SOFT_ERROR;
1886 return STS_BTN_INIT_DATA;
1887 case STS_BTN_INIT_DATA:
1891 while (availabledata())
1895 return opts->force ? STS_BTN_GET_RAW_DATA : STS_BTN_GET_DATA;
1896 case STS_BTN_GET_DATA:
1897 for (retries = RETRIES; retries > 0; ) {
1898 if (!mywaitfordata(10000000)) {
1899 btn_state->no_data = 1;
1900 return STS_BTN_TIMEOUT;
1905 while (availabledata()) {
1909 &(state->decode_ctx))) {
1915 if (!resethw(btn_state->started_as_root)) {
1916 btn_state_set_message(
1918 "Could not reset hardware.\n");
1919 return STS_BTN_HARD_ERROR;
1921 btn_state_set_message(btn_state,
1922 "Cannot decode data\n");
1924 return STS_BTN_SOFT_ERROR;
1926 btn_state->
ncode.
name = btn_state->buffer;
1930 &(state->decode_ctx))) {
1931 code2 = state->decode_ctx.
code;
1933 if (state->decode_ctx.
code != code2) {
1935 malloc(IR_CODE_NODE_SIZE);
1947 return STS_BTN_BUTTON_DONE;
1948 case STS_BTN_GET_RAW_DATA:
1949 btn_state->count = 0;
1951 while (btn_state->count < MAX_SIGNALS) {
1952 if (btn_state->count == 0)
1955 timeout = remote.
gap * 5;
1957 if (!btn_state->data) {
1958 if (btn_state->count == 0)
1959 return STS_BTN_TIMEOUT;
1960 btn_state->data = remote.
gap;
1962 if (btn_state->count == 0) {
1963 if (!is_space(btn_state->data)
1964 || btn_state->data <
1969 btn_state->count = 0;
1970 btn_state_set_message(
1972 "Something went wrong.");
1973 return STS_BTN_SOFT_ERROR;
1976 if (raw_data_ok(btn_state)) {
1979 btn_state->count - 1);
1980 if (btn_state->count % 2) {
1981 btn_state_set_message(
1984 btn_state->count - 1);
1987 btn_state->count = 0;
1988 return STS_BTN_SOFT_ERROR;
1993 btn_state->count - 1;
1997 signals[btn_state->count - 1] =
1998 btn_state->data & PULSE_MASK;
2000 btn_state->data & PULSE_MASK;
2004 if (btn_state->count == MAX_SIGNALS) {
2005 btn_state_set_message(btn_state,
2006 "Signal is too long.\n");
2007 return STS_BTN_SOFT_ERROR;
2009 return STS_BTN_BUTTON_DONE;
2010 case STS_BTN_RECORD_DONE:
2011 if (is_raw(&remote))
2012 return STS_BTN_ALL_DONE;
2013 if (!resethw(btn_state->started_as_root)) {
2014 btn_state_set_message(btn_state,
2015 "Could not reset hardware.");
2016 return STS_BTN_HARD_ERROR;
2018 return STS_BTN_BUTTONS_DONE;
2019 case STS_BTN_BUTTONS_DONE:
2020 f = fopen(opts->tmpfile,
"r");
2022 btn_state_set_message(btn_state,
2023 "Could not reopen config file");
2024 return STS_BTN_HARD_ERROR;
2028 if (my_remote == NULL) {
2029 btn_state_set_message(
2032 "config file contains no valid remote");
2033 return STS_BTN_HARD_ERROR;
2035 if (my_remote == (
void*)-1) {
2036 btn_state_set_message(
2039 "Reading of config file failed");
2040 return STS_BTN_HARD_ERROR;
2042 sts = STS_BTN_ALL_DONE;
2044 remote = *my_remote;
2047 if (!has_toggle_bit_mask(my_remote)) {
2048 if (!opts->using_template
2050 remote = *(my_remote);
2051 sts = STS_BTN_GET_TOGGLE_BITS;
2054 set_toggle_bit_mask(my_remote,
2059 if (!opts->update) {
2060 get_pre_data(my_remote);
2061 get_post_data(my_remote);
2063 remote = *my_remote;
2065 case STS_BTN_BUTTON_DONE:
2066 return STS_BTN_BUTTON_DONE;
2067 case STS_BTN_HARD_ERROR:
2068 return STS_BTN_HARD_ERROR;
2070 btn_state_set_message(btn_state,
2071 "record_buttons(): bad state: %d\n",
2073 return STS_BTN_HARD_ERROR;
2079 void config_file_setup(
struct main_state* state,
const struct opts* opts)
2081 state->fout = fopen(opts->tmpfile,
"w");
2082 if (state->fout == NULL) {
2083 log_error(
"Could not open new config file %s", tmpfile);
2087 fprint_copyright(state->fout);
2088 fprint_comment(state->fout, &remote, opts->commandline);
2089 fprint_remote_head(state->fout, &remote);
2090 fprint_remote_signal_head(state->fout, &remote);
2096 int config_file_finish(
struct main_state* state,
const struct opts* opts)
2098 state->fout = fopen(opts->filename,
"w");
2099 if (state->fout == NULL) {
2104 fprint_copyright(state->fout);
2105 fprint_remotes(state->fout, &remote, opts->commandline);
</pre >< h3 > Syntax and semantics of the functions</h3 >< p > Note even if implementing the functions in there is no need to declare them as< code > external C</code >< h4 >< code > init_func</code ></h4 >< code > int the functions< code > rec_buffer_init(void)</code > and/or< code >send_buffer_init(void)</code > needs to be called.</p >< h4 >< code >deinit_func</code ></h4 >< code >int mydeinit_func(void)</code >< p >Function called for suspending the driver. Zero return value indicates failure
struct ir_remote * last_remote
const struct driver *const curr_driver
#define log_debug(fmt,...)
int(*const decode_func)(struct ir_remote *remote, struct decode_ctx_t *ctx)
struct ir_code_node * next
#define log_warn(fmt,...)
int receive_decode(struct ir_remote *remote, struct decode_ctx_t *ctx)
#define log_error(fmt,...)
char *(*const rec_func)(struct ir_remote *remotes)
#define log_perror_err(fmt,...)
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))
int default_open(const char *path)
int(*const init_func)(void)
#define log_info(fmt,...)
int rec_buffer_clear(void)
struct ir_remote * read_config(FILE *f, const char *name)
#define log_notice(fmt,...)