34 static lirc_t emulation_readdata(lirc_t timeout);
37 static const struct driver hw_emulation = {
39 .device =
"/dev/null",
40 .features = LIRC_CAN_REC_MODE2,
42 .rec_mode = LIRC_MODE_MODE2,
50 .readdata = emulation_readdata,
54 .driver_version =
"0.9.2"
57 static const int IR_CODE_NODE_SIZE =
sizeof(
struct ir_code_node);
62 unsigned int eps = 30;
67 static lirc_t signals[MAX_SIGNALS];
69 static struct ir_ncode* next_code = NULL;
70 static struct ir_ncode* current_code = NULL;
71 static int current_index = 0;
72 static int current_rep = 0;
74 static struct lengths* first_space = NULL;
75 static struct lengths* first_pulse = NULL;
76 static struct lengths* first_sum = NULL;
77 static struct lengths* first_gap = NULL;
78 static struct lengths* first_repeat_gap = NULL;
79 static struct lengths* first_signal_length = NULL;
80 static struct lengths* first_headerp = NULL;
81 static struct lengths* first_headers = NULL;
82 static struct lengths* first_1lead = NULL;
83 static struct lengths* first_3lead = NULL;
84 static struct lengths* first_trail = NULL;
85 static struct lengths* first_repeatp = NULL;
86 static struct lengths* first_repeats = NULL;
88 static uint32_t
lengths[MAX_SIGNALS];
89 static uint32_t first_length, first_lengths, second_lengths;
90 static unsigned int count, count_spaces, count_signals;
91 static unsigned int count_3repeats, count_5repeats;
107 static void fprint_copyright(FILE* fout)
110 "# Please take the time to finish this file as described in\n"
111 "# https://sourceforge.net/p/lirc-remotes/wiki/Checklist/\n"
112 "# and make it available to others by sending it to\n"
113 "# <lirc@bartelmus.de>\n");
120 struct pollfd pfd = {
126 ret = curl_poll(&pfd, 1, 0);
127 }
while (ret == -1 && errno == EINTR);
134 return pfd.revents & POLLIN ? 1 : 0;
145 case LIRC_MODE_MODE2:
149 case LIRC_MODE_LIRCCODE:
166 if (seteuid(0) == -1)
191 memset(state, 0,
sizeof(
struct gap_state));
206 state->first_signal = -1;
215 state->retval = EXIT_SUCCESS;
222 state->started_as_root = started_as_root;
223 state->retval = EXIT_SUCCESS;
227 static lirc_t calc_signal(
struct lengths* len)
231 return (lirc_t)(len->sum / len->count);
244 bits = bit_count(remote);
245 mask = ((
ir_code)1) << (bits - 1);
254 codes = remote->codes;
255 while (codes->
name != NULL) {
261 else if (bits == 15 && xor == 0x3ff) {
276 if (remote->
bits == 0)
279 codes = remote->codes;
280 if (codes->
name == NULL)
284 if (codes->
name == NULL)
286 while (codes->
name != NULL) {
287 mask &= ~(last ^ codes->
code);
289 for (n = codes->
next; n != NULL; n = n->next) {
290 mask &= ~(last ^ n->code);
296 while (mask & 0x8000000000000000LL) {
300 count -=
sizeof(
ir_code) * CHAR_BIT - remote->
bits;
303 if (count % 8 && (remote->
bits - count) % 8)
307 for (i = 0; i < count; i++) {
311 remote->
bits -= count;
312 mask = mask << (remote->
bits);
316 codes = remote->codes;
317 while (codes->
name != NULL) {
318 codes->
code &= ~mask;
319 for (n = codes->
next; n != NULL; n = n->next)
334 if (remote->
bits == 0)
338 codes = remote->codes;
339 if (codes->
name == NULL)
343 if (codes->
name == NULL)
345 while (codes->
name != NULL) {
346 mask &= ~(last ^ codes->
code);
348 for (n = codes->
next; n != NULL; n = n->next) {
349 mask &= ~(last ^ n->code);
360 if (count % 8 && (remote->
bits - count) % 8)
364 for (i = 0; i < count; i++) {
368 remote->
bits -= count;
372 codes = remote->codes;
373 while (codes->
name != NULL) {
375 for (n = codes->
next; n != NULL; n = n->next)
376 n->code = n->code >> count;
389 || remote->pre_p != 0
390 || remote->
pre_s != 0)
392 for (codes = remote->codes; codes->
name != NULL; codes++) {
394 for (n = codes->
next; n != NULL; n = n->next)
410 for (codes = remote->codes; codes->
name != NULL; codes++) {
413 for (n = codes->
next; n != NULL; n = n->next) {
434 remote->pone = remote->pzero;
440 if (has_pre(remote)) {
445 if (has_post(remote)) {
450 if (remote->
bits == 0)
454 mask = gen_mask(remote->
bits);
455 for (codes = remote->codes; codes->
name != NULL; codes++) {
457 for (n = codes->
next; n != NULL; n = n->next)
467 if (!is_space_enc(remote))
471 if (expect(remote, remote->pone, remote->pzero)
472 || expect(remote, remote->pzero, remote->pone))
474 if (!(expect(remote, remote->
sone, remote->
szero)
475 && expect(remote, remote->
szero, remote->
sone)))
477 if (expect(remote, remote->
ptrail, remote->pone))
479 else if (expect(remote, remote->
ptrail, remote->pzero))
496 while (remote != NULL) {
498 remote = remote->next;
503 static int mywaitfordata(uint32_t maxusec)
506 struct pollfd pfd = {
510 ret = curl_poll(&pfd, 1, maxusec / 1000);
511 }
while (ret == -1 && errno == EINTR);
513 if (ret == -1 && errno != EINTR)
515 return (pfd.revents & POLLIN) != 0;
519 static lirc_t emulation_readdata(lirc_t timeout)
521 static lirc_t sum = 0;
524 if (current_code == NULL) {
527 current_code = next_code;
529 current_code = emulation_data->codes;
533 if (current_code->
name == NULL) {
537 if (current_index >= current_code->
length) {
539 current_code = next_code;
542 if (current_rep > 2) {
549 if (current_code->
name == NULL) {
551 return emulation_readdata(timeout);
554 if (is_const(emulation_data))
555 data = emulation_data->
gap - sum;
557 data = emulation_data->
gap;
562 data = current_code->
signals[current_index];
563 if ((current_index % 2) == 0)
566 sum += data & PULSE_MASK;
570 data & PULSE_BIT ?
'p':
's', data & PULSE_MASK);
575 static struct lengths* new_length(lirc_t length)
579 l = malloc(
sizeof(
struct lengths));
584 l->lower_bound = length / 100 * 100;
585 l->upper_bound = length / 100 * 100 + 99;
586 l->min = l->max = length;
592 void unlink_length(
struct lengths** first,
struct lengths*
remove)
597 if (
remove == *first) {
598 *first =
remove->next;
602 scan = (*first)->next;
605 if (scan ==
remove) {
606 last->next =
remove->next;
613 log_error(
"unlink_length(): report this bug!");
617 int add_length(
struct lengths** first, lirc_t length)
622 if (*first == NULL) {
623 *first = new_length(length);
630 if (l->lower_bound <= length && length <= l->upper_bound) {
633 l->min = min(l->min, length);
634 l->max = max(l->max, length);
640 last->next = new_length(length);
641 if (last->next == NULL)
647 void free_lengths(
struct lengths** firstp)
655 while (first != NULL) {
666 free_lengths(&first_space);
667 free_lengths(&first_pulse);
668 free_lengths(&first_sum);
669 free_lengths(&first_gap);
670 free_lengths(&first_repeat_gap);
671 free_lengths(&first_signal_length);
672 free_lengths(&first_headerp);
673 free_lengths(&first_headers);
674 free_lengths(&first_1lead);
675 free_lengths(&first_3lead);
676 free_lengths(&first_trail);
677 free_lengths(&first_repeatp);
678 free_lengths(&first_repeats);
682 static void merge_lengths(
struct lengths* first)
694 while (inner != NULL) {
695 new_sum = l->sum + inner->sum;
696 new_count = l->count + inner->count;
698 if ((l->max <= new_sum / new_count +
aeps
699 && l->min +
aeps >= new_sum / new_count
700 && inner->max <= new_sum / new_count +
aeps
701 && inner->min +
aeps >= new_sum / new_count)
702 || (l->max <= new_sum / new_count * (100 +
eps)
703 && l->min >= new_sum / new_count * (100 -
eps)
704 && inner->max <= new_sum / new_count *
706 && inner->min >= new_sum / new_count *
709 l->count = new_count;
710 l->upper_bound = max(l->upper_bound,
712 l->lower_bound = min(l->lower_bound,
714 l->min = min(l->min, inner->min);
715 l->max = max(l->max, inner->max);
717 last->next = inner->next;
726 for (l = first; l != NULL; l = l->next) {
728 l->count, (uint32_t)calc_signal(l),
729 (uint32_t)l->min, (uint32_t)l->max);
751 if (first->count > 0)
753 (uint32_t)calc_signal(first));
756 if (scan->count > max_length->count)
762 (uint32_t)calc_signal(scan));
771 int get_trail_length(
struct ir_remote* remote,
int interactive)
773 unsigned int sum = 0, max_count;
776 if (is_biphase(remote))
779 max_length = get_max_length(first_trail, &sum);
780 max_count = max_length->count;
782 "get_trail_length(): sum: %u, max_count %u",
784 if (max_count >= sum * TH_TRAIL / 100) {
786 (uint32_t)calc_signal(max_length));
787 remote->
ptrail = calc_signal(max_length);
795 int get_lead_length(
struct ir_remote* remote,
int interactive)
797 unsigned int sum = 0, max_count;
803 if (!is_biphase(remote) || has_header(remote))
808 first_lead = has_header(remote) ? first_3lead : first_1lead;
809 max_length = get_max_length(first_lead, &sum);
810 max_count = max_length->count;
812 "get_lead_length(): sum: %u, max_count %u",
814 if (max_count >= sum * TH_LEAD / 100) {
816 "Found lead pulse: %lu",
817 (uint32_t)calc_signal(max_length));
818 remote->
plead = calc_signal(max_length);
821 unlink_length(&first_lead, max_length);
822 max2_length = get_max_length(first_lead, &sum);
823 max_length->next = first_lead;
824 first_lead = max_length;
826 a = calc_signal(max_length);
827 b = calc_signal(max2_length);
833 if (abs(2 * a - b) < b *
eps / 100 || abs(2 * a - b) <
aeps) {
835 "Found hidden lead pulse: %lu",
845 int get_header_length(
struct ir_remote* remote,
int interactive)
847 unsigned int sum, max_count;
848 lirc_t headerp, headers;
852 if (first_headerp != NULL) {
853 max_plength = get_max_length(first_headerp, &sum);
854 max_count = max_plength->count;
860 "get_header_length(): sum: %u, max_count %u",
863 if (max_count >= sum * TH_HEADER / 100) {
864 max_slength = get_max_length(first_headers, &sum);
865 max_count = max_slength->count;
867 "get_header_length(): sum: %u, max_count %u",
869 if (max_count >= sum * TH_HEADER / 100) {
870 headerp = calc_signal(max_plength);
871 headers = calc_signal(max_slength);
874 "Found possible header: %lu %lu",
877 remote->phead = headerp;
878 remote->
shead = headers;
879 if (first_lengths < second_lengths) {
881 "Header is not being repeated.");
892 int get_repeat_length(
struct ir_remote* remote,
int interactive)
894 unsigned int sum = 0, max_count;
895 lirc_t repeatp, repeats, repeat_gap;
899 if (!((count_3repeats > SAMPLES / 2 ? 1 : 0) ^
900 (count_5repeats > SAMPLES / 2 ? 1 : 0))) {
901 if (count_3repeats > SAMPLES / 2
902 || count_5repeats > SAMPLES / 2) {
910 max_plength = get_max_length(first_repeatp, &sum);
911 max_count = max_plength->count;
913 "get_repeat_length(): sum: %u, max_count %u",
915 if (max_count >= sum * TH_REPEAT / 100) {
916 max_slength = get_max_length(first_repeats, &sum);
917 max_count = max_slength->count;
919 "get_repeat_length(): sum: %u, max_count %u",
921 if (max_count >= sum * TH_REPEAT / 100) {
922 if (count_5repeats > count_3repeats
923 && !has_header(remote)) {
925 "Repeat code has header,"
926 " but no header found!");
929 if (count_5repeats > count_3repeats
930 && has_header(remote))
932 repeatp = calc_signal(max_plength);
933 repeats = calc_signal(max_slength);
936 "Found repeat code: %lu %lu",
939 remote->prepeat = repeatp;
942 max_slength = get_max_length(first_repeat_gap,
944 repeat_gap = calc_signal(max_slength);
946 "Found repeat gap: %lu",
947 (uint32_t)repeat_gap);
958 void get_scheme(
struct ir_remote* remote,
int interactive)
960 unsigned int i, length = 0, sum = 0;
966 for (i = 1; i < MAX_SIGNALS; i++) {
973 log_debug(
"get_scheme(): sum: %u length: %u signals: %u"
974 " first_lengths: %u second_lengths: %u\n",
975 sum, length + 1,
lengths[length],
976 first_lengths, second_lengths);
978 if (
lengths[length] >= TH_SPACE_ENC * sum / 100) {
981 "Space/pulse encoded remote control found.");
982 log_debug(
"Signal length is %u.", length);
984 remote->
bits = length;
988 maxp = get_max_length(first_pulse, NULL);
989 unlink_length(&first_pulse, maxp);
990 if (first_pulse == NULL)
992 max2p = get_max_length(first_pulse, NULL);
993 maxp->next = first_pulse;
996 maxs = get_max_length(first_space, NULL);
997 unlink_length(&first_space, maxs);
998 if (first_space == NULL) {
1001 max2s = get_max_length(first_space, NULL);
1002 maxs->next = first_space;
1005 maxs = get_max_length(first_space, NULL);
1008 && (calc_signal(maxp) < TH_RC6_SIGNAL
1009 || calc_signal(max2p) < TH_RC6_SIGNAL)
1010 && (calc_signal(maxs) < TH_RC6_SIGNAL
1011 || calc_signal(max2s) < TH_RC6_SIGNAL)) {
1012 log_debug(
"RC-6 remote control found.");
1013 set_protocol(remote,
RC6);
1015 log_debug(
"RC-5 remote control found.");
1016 set_protocol(remote,
RC5);
1021 log_debug(
"Suspicious data length: %u.", length);
1023 remote->
bits = length;
1028 int get_data_length(
struct ir_remote* remote,
int interactive)
1030 unsigned int sum = 0, max_count;
1031 lirc_t p1, p2, s1, s2;
1037 max_plength = get_max_length(first_pulse, &sum);
1038 max_count = max_plength->count;
1039 log_debug(
"get_data_length(): sum: %u, max_count %u",
1042 if (max_count >= sum * TH_IS_BIT / 100) {
1043 unlink_length(&first_pulse, max_plength);
1045 max2_plength = get_max_length(first_pulse, NULL);
1046 if (max2_plength != NULL)
1047 if (max2_plength->count < max_count * TH_IS_BIT / 100)
1048 max2_plength = NULL;
1050 log_debug(
"%u x %u", max_plength->count,
1051 (uint32_t)calc_signal(max_plength));
1054 max2_plength->count,
1055 (uint32_t)calc_signal(max2_plength));
1057 max_slength = get_max_length(first_space, &sum);
1058 max_count = max_slength->count;
1060 "get_data_length(): sum: %u, max_count %u",
1062 if (max_count >= sum * TH_IS_BIT / 100) {
1063 unlink_length(&first_space, max_slength);
1065 max2_slength = get_max_length(first_space, NULL);
1066 if (max2_slength != NULL)
1067 if (max2_slength->count <
1068 max_count * TH_IS_BIT / 100)
1069 max2_slength = NULL;
1070 if (max_count >= sum * TH_IS_BIT / 100) {
1075 (uint32_t)calc_signal(max_slength));
1079 max2_slength->count,
1080 (uint32_t)calc_signal(max2_slength));
1085 if (is_biphase(remote)) {
1086 if (max2_plength == NULL
1087 || max2_slength == NULL) {
1089 "Unknown encoding found.");
1093 "Signals are biphase encoded.");
1094 p1 = calc_signal(max_plength);
1095 p2 = calc_signal(max2_plength);
1096 s1 = calc_signal(max_slength);
1097 s2 = calc_signal(max2_slength);
1100 (min(p1, p2) + max(p1, p2) / 2) / 2;
1102 (min(s1, s2) + max(s1, s2) / 2) / 2;
1103 remote->pzero = remote->pone;
1106 if (max2_plength == NULL
1107 && max2_slength == NULL) {
1109 "No encoding found");
1112 if (max2_plength && max2_slength) {
1114 "Unknown encoding found.");
1117 p1 = calc_signal(max_plength);
1118 s1 = calc_signal(max_slength);
1120 p2 = calc_signal(max2_plength);
1121 log_debug(
"Signals are pulse encoded.");
1122 remote->pone = max(p1, p2);
1124 remote->pzero = min(p1, p2);
1126 if (expect(remote, remote->
ptrail, p1)
1127 || expect(remote, remote->
ptrail,
1131 s2 = calc_signal(max2_slength);
1132 log_debug(
"Signals are space encoded.");
1134 remote->
sone = max(s1, s2);
1136 remote->
szero = min(s1, s2);
1139 if (has_header(remote)
1140 && (!has_repeat(remote)
1142 if (!is_biphase(remote)
1143 && ((expect(remote, remote->phead,
1154 remote->phead = remote->
shead = 0;
1159 if (is_biphase(remote)
1163 remote->
plead = remote->phead;
1164 remote->phead = remote->
shead = 0;
1170 if (is_biphase(remote)) {
1171 struct lengths* signal_length;
1175 get_max_length(first_signal_length,
1178 calc_signal(signal_length) -
1179 remote->
plead - remote->phead -
1182 (remote->pone + remote->
sone) / 2;
1183 remote->
bits = data_length / (remote->pone +
1190 (has_header(remote) ? 2 : 0) + 1 -
1191 (remote->
ptrail > 0 ? 2 : 0)) / 2;
1194 "Signal length is %d",
1196 free_lengths(&max_plength);
1197 free_lengths(&max_slength);
1200 free_lengths(&max_plength);
1212 if (!mywaitfordata(10000000)) {
1213 free_lengths(&(state->gaps));
1214 return STS_GAP_TIMEOUT;
1216 gettimeofday(&(state->start), NULL);
1219 gettimeofday(&(state->end), NULL);
1221 state->gap = time_elapsed(&(state->last), &(state->start));
1222 add_length(&(state->gaps), state->gap);
1223 merge_lengths(state->gaps);
1224 state->maxcount = 0;
1225 state->scan = state->gaps;
1226 while (state->scan) {
1227 state->maxcount = max(state->maxcount,
1228 state->scan->count);
1229 if (state->scan->count > SAMPLES) {
1230 remote->
gap = calc_signal(state->scan);
1231 free_lengths(&(state->gaps));
1232 return STS_GAP_FOUND;
1234 state->scan = state->scan->next;
1236 if (state->maxcount > state->lastmaxcount) {
1237 state->lastmaxcount = state->maxcount;
1238 return STS_GAP_GOT_ONE_PRESS;
1243 state->last = state->end;
1244 return STS_GAP_AGAIN;
1253 if (!is_rc6(remote))
1255 if (remote->codes) {
1256 codes = remote->codes;
1257 while (codes->
name != NULL) {
1270 static void compute_lengths_4_signals(
void)
1272 add_length(&first_repeatp,
signals[0]);
1273 merge_lengths(first_repeatp);
1274 add_length(&first_repeats,
signals[1]);
1275 merge_lengths(first_repeats);
1276 add_length(&first_trail,
signals[2]);
1277 merge_lengths(first_trail);
1278 add_length(&first_repeat_gap,
signals[3]);
1279 merge_lengths(first_repeat_gap);
1284 static void compute_lengths_6_signals(
void)
1286 add_length(&first_headerp,
signals[0]);
1287 merge_lengths(first_headerp);
1288 add_length(&first_headers,
signals[1]);
1289 merge_lengths(first_headers);
1290 add_length(&first_repeatp,
signals[2]);
1291 merge_lengths(first_repeatp);
1292 add_length(&first_repeats,
signals[3]);
1293 merge_lengths(first_repeats);
1294 add_length(&first_trail,
signals[4]);
1295 merge_lengths(first_trail);
1296 add_length(&first_repeat_gap,
signals[5]);
1297 merge_lengths(first_repeat_gap);
1301 static void compute_lengths_many_signals(
struct lengths_state* state)
1305 add_length(&first_1lead,
signals[0]);
1306 merge_lengths(first_1lead);
1307 for (i = 2; i < state->
count - 2; i++) {
1309 add_length(&first_space,
signals[i]);
1310 merge_lengths(first_space);
1312 add_length(&first_pulse,
signals[i]);
1313 merge_lengths(first_pulse);
1317 merge_lengths(first_trail);
1319 add_length(&first_signal_length, state->
sum - state->data);
1320 merge_lengths(first_signal_length);
1321 if (state->first_signal == 1
1322 || (first_length > 2
1323 && first_length - 2 != state->
count - 2)) {
1324 add_length(&first_3lead,
signals[2]);
1325 merge_lengths(first_3lead);
1326 add_length(&first_headerp,
signals[0]);
1327 merge_lengths(first_headerp);
1328 add_length(&first_headers,
signals[1]);
1329 merge_lengths(first_headers);
1331 if (state->first_signal == 1) {
1333 first_length = state->
count - 2;
1335 }
else if (state->first_signal == 0
1336 && first_length - 2 == state->
count - 2) {
1351 for (scan = first_sum; scan; scan = scan->next) {
1352 *maxcount = max(*maxcount, scan->count);
1353 if (scan->count > SAMPLES) {
1354 remote->
gap = calc_signal(scan);
1356 state->mode = MODE_HAVE_GAP;
1358 *again = STS_LEN_AGAIN_INFO;
1373 for (scan = first_gap; scan; scan = scan->next) {
1374 *maxcount = max(*maxcount, scan->count);
1375 if (scan->count > SAMPLES) {
1376 remote->
gap = calc_signal(scan);
1377 state->mode = MODE_HAVE_GAP;
1379 *again = STS_LEN_AGAIN_INFO;
1389 int force,
int interactive)
1393 static int lastmaxcount = 0;
1399 return STS_LEN_TIMEOUT;
1402 if (state->mode == MODE_GET_GAP) {
1403 state->
sum += state->data & PULSE_MASK;
1404 if (state->average == 0 && is_space(state->data)) {
1405 if (state->data > 100000) {
1407 return STS_LEN_AGAIN;
1409 state->average = state->data;
1410 state->maxspace = state->data;
1411 }
else if (is_space(state->data)) {
1412 if (state->data > MIN_GAP
1413 || state->data > 100 * state->average
1415 || (state->data >= 5000 && count_spaces > 10
1416 && state->data > 5 * state->average)
1417 || (state->data < 5000 && count_spaces > 10
1418 && state->data > 5 * state->maxspace / 2)) {
1419 add_length(&first_sum, state->
sum);
1420 merge_lengths(first_sum);
1421 add_length(&first_gap, state->data);
1422 merge_lengths(first_gap);
1426 state->maxspace = 0;
1429 scan = scan_gap1(state,
1434 scan = scan_gap2(state,
1440 state->mode = MODE_HAVE_GAP;
1443 state->remaining_gap =
1447 remote->
gap - state->data : 0)
1448 : (has_repeat_gap(remote) ?
1450 repeat_gap : remote->
gap);
1453 return STS_LEN_RAW_OK;
1455 return STS_LEN_AGAIN_INFO;
1457 lastmaxcount = maxcount;
1462 (state->average * count_spaces + state->data)
1463 / (count_spaces + 1);
1465 if (state->data > state->maxspace)
1466 state->maxspace = state->data;
1468 if (state->
count > SAMPLES * MAX_SIGNALS * 2) {
1470 return STS_LEN_NO_GAP_FOUND;
1473 return STS_LEN_AGAIN;
1474 }
else if (state->mode == MODE_HAVE_GAP) {
1475 if (state->
count <= MAX_SIGNALS) {
1476 signals[state->
count - 1] = state->data & PULSE_MASK;
1479 return STS_LEN_TOO_LONG;
1481 if (is_const(remote))
1482 state->remaining_gap =
1483 remote->
gap > state->
sum ?
1484 remote->
gap - state->
sum : 0;
1486 state->remaining_gap = remote->
gap;
1487 state->
sum += state->data & PULSE_MASK;
1489 if (state->
count > 2
1490 && ((state->data & PULSE_MASK) >=
1491 state->remaining_gap * (100 -
eps) / 100
1492 || (state->data & PULSE_MASK) >=
1493 state->remaining_gap -
aeps)) {
1494 if (is_space(state->data)) {
1497 if (state->
count == 4) {
1499 compute_lengths_4_signals();
1500 }
else if (state->
count == 6) {
1502 compute_lengths_6_signals();
1503 }
else if (state->
count > 6) {
1505 compute_lengths_many_signals(state);
1515 return STS_LEN_NO_GAP_FOUND;
1518 if (count_signals >= SAMPLES) {
1519 get_scheme(remote, interactive);
1520 if (!get_header_length(remote, interactive)
1521 || !get_trail_length(remote, interactive)
1522 || !get_lead_length(remote, interactive)
1523 || !get_repeat_length(remote, interactive)
1524 || !get_data_length(remote, interactive))
1526 return state->retval ==
1527 0 ? STS_LEN_FAIL : STS_LEN_OK;
1529 if ((state->data & PULSE_MASK) <=
1530 (state->remaining_gap + state->header) *
1532 || (state->data & PULSE_MASK) <=
1533 (state->remaining_gap + state->header) +
aeps) {
1534 state->first_signal = 0;
1537 state->first_signal = 1;
1541 return STS_LEN_AGAIN;
1552 if (!state->inited) {
1558 if (state->retries <= 0) {
1560 return STS_TGL_NOT_FOUND;
1561 if (state->seq > 0) {
1562 remote->
min_repeat = state->repeats / state->seq;
1566 return STS_TGL_FOUND;
1568 if (!mywaitfordata(10000000))
1569 return STS_TGL_TIMEOUT;
1571 if (is_rc6(remote) && remote->
rc6_mask == 0) {
1572 for (i = 0, mask = 1; i < remote->
bits; i++, mask <<= 1) {
1576 if (state->success) {
1584 if (!state->success)
1589 if (state->success) {
1596 if (state->success) {
1597 if (state->flag == 0) {
1599 state->first = decode_ctx.
code;
1601 || decode_ctx.
code != state->last) {
1603 mask = state->first ^ decode_ctx.
code;
1604 if (!state->found && mask) {
1605 set_toggle_bit_mask(remote, mask);
1609 state->repeats / state->seq;
1612 state->last = decode_ctx.
code;
1613 return STS_TGL_GOT_ONE_PRESS;
1616 state->last = decode_ctx.
code;
1622 return STS_TGL_AGAIN;
1631 while (status == STS_LEN_AGAIN) {
1632 status =
get_lengths(lengths_state, &remote, 0, 0);
1634 case STS_LEN_AGAIN_INFO:
1635 status = STS_LEN_AGAIN;
1644 case STS_LEN_RAW_OK:
1647 case STS_LEN_TIMEOUT:
1650 case STS_LEN_NO_GAP_FOUND:
1653 case STS_LEN_TOO_LONG:
1654 log_error(
"analyse, signal too long?!");
1675 size_t new_codes_count = 100;
1679 if (!is_raw(raw_data)) {
1680 log_error(
"remote %s not in raw mode, ignoring",
1687 emulation_data = raw_data;
1689 current_code = NULL;
1691 memset(&remote, 0,
sizeof(remote));
1696 if (is_rc6(&remote) && remote.
bits >= 5)
1704 new_codes = malloc(new_codes_count *
sizeof(*new_codes));
1705 if (new_codes == NULL) {
1709 memset(new_codes, 0, new_codes_count *
sizeof(*new_codes));
1710 codes = raw_data->codes;
1711 while (codes->
name != NULL) {
1713 current_code = NULL;
1722 "Decoding of %s failed", codes->
name);
1724 if (new_index + 1 >= new_codes_count) {
1727 new_codes_count *= 2;
1731 sizeof(*new_codes));
1732 if (renew_codes == NULL) {
1737 memset(&new_codes[new_codes_count / 2],
1739 new_codes_count / 2 *
1740 sizeof(*new_codes));
1741 new_codes = renew_codes;
1745 code = decode_ctx.
code;
1747 code2 = decode_ctx.
code;
1749 if (ret && code2 != decode_ctx.
code) {
1750 new_codes[new_index].
next =
1751 malloc(IR_CODE_NODE_SIZE);
1752 if (new_codes[new_index].next) {
1753 memset(new_codes[new_index].next,
1756 new_codes[new_index].
next->code =
1760 new_codes[new_index].
name = codes->
name;
1761 new_codes[new_index].
code = decode_ctx.
code;
1766 new_codes[new_index].
name = NULL;
1767 remote.codes = new_codes;
1768 fprint_remotes(stdout, &remote, opts->commandline);
1769 remote.codes = NULL;
1782 f = fopen(opts->filename,
"r");
1784 fprintf(stderr,
"Cannot open file: %s\n", opts->filename);
1789 fprintf(stderr,
"Cannot parse file: %s\n", opts->filename);
1792 for (; r != NULL; r = r->next) {
1794 log_error(
"remote %s not in raw mode, ignoring",
1804 ssize_t
raw_read(
void* buffer,
size_t size,
unsigned int timeout_us)
1806 if (!mywaitfordata(timeout_us))
1817 if (!is_space(btn_state->data)) {
1819 }
else if (is_const(&remote)) {
1820 if (remote.
gap > btn_state->sum) {
1821 ref = (remote.
gap - btn_state->sum);
1822 ref *= (100 - remote.
eps);
1827 r = btn_state->data > ref;
1829 r = btn_state->data > (remote.
gap * (100 - remote.
eps)) / 100;
1840 const char*
const MSG_BAD_LENGTH =
1841 "Signal length is %d\n"
1842 "That's weird because the signal length must be odd!\n";
1851 if (btn_state->no_data) {
1852 btn_state->no_data = 0;
1853 return STS_BTN_TIMEOUT;
1855 switch (last_status) {
1857 return STS_BTN_GET_NAME;
1858 case STS_BTN_GET_NAME:
1859 if (strchr(btn_state->buffer,
' ') != NULL) {
1862 "The name must not contain any whitespace.");
1863 return STS_BTN_SOFT_ERROR;
1865 if (strchr(btn_state->buffer,
'\t') != NULL) {
1868 "The name must not contain any whitespace.");
1869 return STS_BTN_SOFT_ERROR;
1871 if (strcasecmp(btn_state->buffer,
"begin") == 0) {
1874 "'%s' is not allowed as button name\n",
1876 return STS_BTN_SOFT_ERROR;
1878 if (strcasecmp(btn_state->buffer,
"end") == 0) {
1881 "'%s' is not allowed as button name\n",
1883 return STS_BTN_SOFT_ERROR;
1885 if (strlen(btn_state->buffer) == 0)
1886 return STS_BTN_RECORD_DONE;
1887 if (!opts->disable_namespace
1888 && !is_in_namespace(btn_state->buffer)) {
1891 "'%s' is not in name space"
1892 " (use --disable-namespace to override)\n",
1894 return STS_BTN_SOFT_ERROR;
1896 return STS_BTN_INIT_DATA;
1897 case STS_BTN_INIT_DATA:
1905 return opts->force ? STS_BTN_GET_RAW_DATA : STS_BTN_GET_DATA;
1906 case STS_BTN_GET_DATA:
1907 for (retries = RETRIES; retries > 0; ) {
1908 if (!mywaitfordata(10000000)) {
1909 btn_state->no_data = 1;
1910 return STS_BTN_TIMEOUT;
1919 &(state->decode_ctx))) {
1925 if (!
resethw(btn_state->started_as_root)) {
1928 "Could not reset hardware.\n");
1929 return STS_BTN_HARD_ERROR;
1932 "Cannot decode data\n");
1934 return STS_BTN_SOFT_ERROR;
1936 btn_state->
ncode.
name = btn_state->buffer;
1940 &(state->decode_ctx))) {
1941 code2 = state->decode_ctx.
code;
1943 if (state->decode_ctx.
code != code2) {
1945 malloc(IR_CODE_NODE_SIZE);
1957 return STS_BTN_BUTTON_DONE;
1958 case STS_BTN_GET_RAW_DATA:
1959 btn_state->count = 0;
1961 while (btn_state->count < MAX_SIGNALS) {
1962 if (btn_state->count == 0)
1965 timeout = remote.
gap * 5;
1967 if (!btn_state->data) {
1968 if (btn_state->count == 0)
1969 return STS_BTN_TIMEOUT;
1970 btn_state->data = remote.
gap;
1972 if (btn_state->count == 0) {
1973 if (!is_space(btn_state->data)
1974 || btn_state->data <
1979 btn_state->count = 0;
1982 "Something went wrong.");
1983 return STS_BTN_SOFT_ERROR;
1986 if (raw_data_ok(btn_state)) {
1989 btn_state->count - 1);
1990 if (btn_state->count % 2) {
1994 btn_state->count - 1);
1997 btn_state->count = 0;
1998 return STS_BTN_SOFT_ERROR;
2003 btn_state->count - 1;
2007 signals[btn_state->count - 1] =
2008 btn_state->data & PULSE_MASK;
2010 btn_state->data & PULSE_MASK;
2014 if (btn_state->count == MAX_SIGNALS) {
2016 "Signal is too long.\n");
2017 return STS_BTN_SOFT_ERROR;
2019 return STS_BTN_BUTTON_DONE;
2020 case STS_BTN_RECORD_DONE:
2021 if (is_raw(&remote))
2022 return STS_BTN_ALL_DONE;
2023 if (!
resethw(btn_state->started_as_root)) {
2025 "Could not reset hardware.");
2026 return STS_BTN_HARD_ERROR;
2028 return STS_BTN_BUTTONS_DONE;
2029 case STS_BTN_BUTTONS_DONE:
2030 f = fopen(opts->tmpfile,
"r");
2033 "Could not reopen config file");
2034 return STS_BTN_HARD_ERROR;
2038 if (my_remote == NULL) {
2042 "config file contains no valid remote");
2043 return STS_BTN_HARD_ERROR;
2045 if (my_remote == (
void*)-1) {
2049 "Reading of config file failed");
2050 return STS_BTN_HARD_ERROR;
2052 sts = STS_BTN_ALL_DONE;
2054 remote = *my_remote;
2057 if (!has_toggle_bit_mask(my_remote)) {
2058 if (!opts->using_template
2060 remote = *(my_remote);
2061 sts = STS_BTN_GET_TOGGLE_BITS;
2064 set_toggle_bit_mask(my_remote,
2069 if (!opts->update) {
2073 remote = *my_remote;
2075 case STS_BTN_BUTTON_DONE:
2076 return STS_BTN_BUTTON_DONE;
2077 case STS_BTN_HARD_ERROR:
2078 return STS_BTN_HARD_ERROR;
2081 "record_buttons(): bad state: %d\n",
2083 return STS_BTN_HARD_ERROR;
2091 state->fout = fopen(opts->tmpfile,
"w");
2092 if (state->fout == NULL) {
2093 log_error(
"Could not open new config file %s", tmpfile);
2097 fprint_copyright(state->fout);
2098 fprint_comment(state->fout, &remote, opts->commandline);
2099 fprint_remote_head(state->fout, &remote);
2100 fprint_remote_signal_head(state->fout, &remote);
2108 state->fout = fopen(opts->filename,
"w");
2109 if (state->fout == NULL) {
2114 fprint_copyright(state->fout);
2115 fprint_remotes(state->fout, &remote, opts->commandline);
void gap_state_init(struct gap_state *state)
Initiate a pristine gap_state.
lirc_t min_remaining_gap
remember gap for CONST_LENGTH remotes
void rec_buffer_init(void)
Clear internal buffer to pristine state.
struct ir_remote * last_remote
TODO.
One remote as represented in the configuration file.
int bits
bits (length of code)
int default_close(void)
For now, a placeholder.
lengths_status
Return from one attempt to determine lengths in get_lengths().
void free_all_lengths(void)
Free heap data allocated by get_lengths().
#define RC6
IR data follows RC6 protocol.
An ir_code for entering into (singly) linked lists, i.e.
Private state in get_togggle_bit_mask().
Parsed run-time options, reflects long_options and the command line, mostly a const object...
toggle_status
Return from one attempt in get_toggle_bit_mask().
unsigned int freq
modulation frequency
int fd
Set by the driver after init().
const struct driver *const curr_driver
Read-only access to drv for client code.
ir_code post_data
data which the remote sends after actual keycode
lirc_t plead
leading pulse
button_status
Return from one pass in record_buttons().
#define log_debug(fmt,...)
Log a debug message.
int(*const decode_func)(struct ir_remote *remote, struct decode_ctx_t *ctx)
TODO.
void get_pre_data(struct ir_remote *remote)
Test hook: Extract remote->pre_data from remote->bits.
struct ir_code_node * next
Linked list of the subsequent ir_code's, after the first one.
const char * name
name of remote control
#define SPACE_ENC
IR data is space encoded.
lirc_t * signals
(private)
get_gap_status
Return form one attempt to get gap in get_gap().
int eps
eps (relative tolerance)
int keypresses
Number of counted button presses.
int analyse_remote(struct ir_remote *raw_data, const struct opts *opts)
Implement the analyse task, return 1 for ok, 0 for errors.
Private state in get_gap_length().
#define log_warn(fmt,...)
Log a warning message.
void invert_data(struct ir_remote *remote)
Test hook: Invert all data items in remote.
int receive_decode(struct ir_remote *remote, struct decode_ctx_t *ctx)
Decode data from remote.
lirc_t ptrail
trailing pulse
int pre_data_bits
length of pre_data
#define PACKET_SIZE
IR transmission packet size.
logchannel_t
Log channels used to filter messages.
void toggle_state_init(struct toggle_state *state)
Initiate a pristine toggle_state.
char * name
Name of command.
uint32_t rec_mode
Possible values are: LIRC_MODE_RAW, LIRC_MODE_PULSE, LIRC_MODE_MODE2, LIRC_MODE_LIRCCODE.
Library part of irrecord, functions to identify unknown remotes.
int post_data_bits
length of post_data
enum get_gap_status get_gap_length(struct gap_state *state, struct ir_remote *remote)
Try to find out gap length, returning gap_status.
ir_code toggle_mask
Sharp (?) error detection scheme.
#define log_error(fmt,...)
Log an error message.
int needs_toggle_mask(struct ir_remote *remote)
Return true if a given remote needs to compute toggle_mask.
ir_code pre_data
data which the remote sends before actual keycode
char *(*const rec_func)(struct ir_remote *remotes)
Receive data from remote.
ssize_t raw_read(void *buffer, size_t size, unsigned int timeout_us)
Absolute error tolerance (us).
lirc_t aeps
Error tolerance in per cent.
uint32_t gap
time between signals in usecs
void for_each_remote(struct ir_remote *remotes, remote_func func)
Unconditionally apply func(remote) for all items in remotes list.
#define RC5
IR data follows RC5 protocol.
#define log_perror_err(fmt,...)
perror wrapper logging with level LIRC_ERROR.
lirc_t(*const readdata)(lirc_t timeout)
Get length of next pulse/space from hardware.
int(*const deinit_func)(void)
Function called when transmitting/receiving stops.
uint32_t repeat_gap
time between two repeat codes if different from gap
Main include file for lirc applications.
void btn_state_set_message(struct button_state *state, const char *fmt,...)
snprintf-style message formatting into state->message.
#define REPEAT_HEADER
header is also sent before repeat code
void flushhw(void)
Clear the driver input buffers.
int availabledata(void)
Return 1 if there is available after running poll(2), else 0.
lirc_t max_remaining_gap
Estimated max time of trailing gap.
ir_code code
Code part, matched to code defintion.
int analyse_get_lengths(struct lengths_state *lengths_state)
analyse non-interactive get_lengths, returns boolean ok/fail.
unsigned int eps
Shared list of remotes.
#define CONST_LENGTH
signal length+gap is always constant
int config_file_finish(struct main_state *state, const struct opts *opts)
Write the final config file.
#define NO_HEAD_REP
no header for key repeats
lirc_t pre_s
signal between pre_data and keycode
lirc_t sum
Number of bits accounted for in signal.
void remove_trail(struct ir_remote *remote)
Test hook: Move remote->trail into remote->bits.
The data the driver exports i.
void config_file_setup(struct main_state *state, const struct opts *opts)
Write the provisionary config file.
IR Command, corresponding to one (command defining) line of the configuration file.
const uint32_t code_length
Length in bits of the code.
unsigned int aeps
detecting very short pulses is difficult with relative tolerance for some remotes, this is an absolute tolerance to solve this problem usually you can say 0 here.
lirc_t srepeat
indicate repeating
State describing code, pre, post + gap and repeat state.
enum lengths_status get_lengths(struct lengths_state *state, struct ir_remote *remote, int force, int interactive)
Try to find out pre/post etc.
lirc_t min_remaining_gap
Estimated min time of trailing gap.
const char * drop_sudo_root(int(*set_some_uid)(uid_t))
Try to drop possible root privileges, returning new user or "" on error.
lirc_t max_remaining_gap
gap range
void remove_pre_data(struct ir_remote *remote)
Test hook: Move remote->pre_data into remote->bits.
void remove_post_data(struct ir_remote *remote)
Test hook: Move remote->post_data into remote->bits.
enum toggle_status get_toggle_bit_mask(struct toggle_state *state, struct ir_remote *remote)
Try to find out toggle_bit_mask, returning toggle_status.
const char * name
Driver name, as listed by -H help and used as argument to i –driver.
int default_open(const char *path)
Stores path in drv.device if non-null.
int do_analyse(const struct opts *opts, struct main_state *state)
The –analyse wrapper.
int(*const init_func)(void)
Function called for initializing the driver and the hardware.
ir_code code
The first code of the command.
int resethw(int started_as_root)
Reset the hardware.
int count
Number of processed data items.
void get_post_data(struct ir_remote *remote)
Test hook: Extract remote->post_data and post_data_bits from bits.
void button_state_init(struct button_state *state, int started_as_root)
Initiate a pristine button_state.
ir_code rc6_mask
RC-6 doubles signal length of some bits.
void lengths_state_init(struct lengths_state *state)
Initiate a pristine lengths_state.
int repeat_flag
True if code is a repeated one.
#define log_info(fmt,...)
Log an info message.
enum button_status record_buttons(struct button_state *btn_state, enum button_status last_status, struct main_state *state, const struct opts *opts)
Try to record one button, returning button_status.
ir_code toggle_bit_mask
previously only one bit called toggle_bit
State in get_lengths(), private besides commented.
int rec_buffer_clear(void)
Flush the internal fifo and store a single code read from the driver in it.
int min_repeat
code is repeated at least x times code sent once -> min_repeat=0
struct ir_remote * read_config(FILE *f, const char *name)
Parse a lircd.conf config file.
#define log_notice(fmt,...)
Log a notice message.
uint64_t ir_code
Denotes an internal coded representation for an IR transmission.