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;
1401 if (is_timeout(state->data)) {
1402 return STS_LEN_AGAIN;
1405 if (state->mode == MODE_GET_GAP) {
1406 if (state->
sum != 0 || is_pulse(state->data)) {
1407 state->
sum += state->data & PULSE_MASK;
1409 return STS_LEN_AGAIN;
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==1 && is_space(state->data)) {
1484 return STS_LEN_AGAIN;
1486 if (state->
count <= MAX_SIGNALS) {
1487 signals[state->
count - 1] = state->data & PULSE_MASK;
1490 return STS_LEN_TOO_LONG;
1492 if (is_const(remote))
1493 state->remaining_gap =
1494 remote->
gap > state->
sum ?
1495 remote->
gap - state->
sum : 0;
1497 state->remaining_gap = remote->
gap;
1498 state->
sum += state->data & PULSE_MASK;
1500 if (state->
count > 2
1501 && ((state->data & PULSE_MASK) >=
1502 state->remaining_gap * (100 -
eps) / 100
1503 || (state->data & PULSE_MASK) >=
1504 state->remaining_gap -
aeps)) {
1505 if (is_space(state->data)) {
1508 if (state->
count == 4) {
1510 compute_lengths_4_signals();
1511 }
else if (state->
count == 6) {
1513 compute_lengths_6_signals();
1514 }
else if (state->
count > 6) {
1516 compute_lengths_many_signals(state);
1524 else if(is_pulse(state->data)) {
1526 return STS_LEN_NO_GAP_FOUND;
1529 if (count_signals >= SAMPLES) {
1530 get_scheme(remote, interactive);
1531 if (!get_header_length(remote, interactive)
1532 || !get_trail_length(remote, interactive)
1533 || !get_lead_length(remote, interactive)
1534 || !get_repeat_length(remote, interactive)
1535 || !get_data_length(remote, interactive))
1537 return state->retval ==
1538 0 ? STS_LEN_FAIL : STS_LEN_OK;
1540 if ((state->data & PULSE_MASK) <=
1541 (state->remaining_gap + state->header) *
1543 || (state->data & PULSE_MASK) <=
1544 (state->remaining_gap + state->header) +
aeps) {
1545 state->first_signal = 0;
1548 state->first_signal = 1;
1552 return STS_LEN_AGAIN;
1563 if (!state->inited) {
1569 if (state->retries <= 0) {
1571 return STS_TGL_NOT_FOUND;
1572 if (state->seq > 0) {
1573 remote->
min_repeat = state->repeats / state->seq;
1577 return STS_TGL_FOUND;
1579 if (!mywaitfordata(10000000))
1580 return STS_TGL_TIMEOUT;
1582 if (is_rc6(remote) && remote->
rc6_mask == 0) {
1583 for (i = 0, mask = 1; i < remote->
bits; i++, mask <<= 1) {
1587 if (state->success) {
1595 if (!state->success)
1600 if (state->success) {
1607 if (state->success) {
1608 if (state->flag == 0) {
1610 state->first = decode_ctx.
code;
1612 || decode_ctx.
code != state->last) {
1614 mask = state->first ^ decode_ctx.
code;
1615 if (!state->found && mask) {
1616 set_toggle_bit_mask(remote, mask);
1620 state->repeats / state->seq;
1623 state->last = decode_ctx.
code;
1624 return STS_TGL_GOT_ONE_PRESS;
1627 state->last = decode_ctx.
code;
1633 return STS_TGL_AGAIN;
1642 while (status == STS_LEN_AGAIN) {
1643 status =
get_lengths(lengths_state, &remote, 0, 0);
1645 case STS_LEN_AGAIN_INFO:
1646 status = STS_LEN_AGAIN;
1655 case STS_LEN_RAW_OK:
1658 case STS_LEN_TIMEOUT:
1661 case STS_LEN_NO_GAP_FOUND:
1664 case STS_LEN_TOO_LONG:
1665 log_error(
"analyse, signal too long?!");
1686 size_t new_codes_count = 100;
1690 if (!is_raw(raw_data)) {
1691 log_error(
"remote %s not in raw mode, ignoring",
1698 emulation_data = raw_data;
1700 current_code = NULL;
1702 memset(&remote, 0,
sizeof(remote));
1707 if (is_rc6(&remote) && remote.
bits >= 5)
1715 new_codes = malloc(new_codes_count *
sizeof(*new_codes));
1716 if (new_codes == NULL) {
1720 memset(new_codes, 0, new_codes_count *
sizeof(*new_codes));
1721 codes = raw_data->codes;
1722 while (codes->
name != NULL) {
1724 current_code = NULL;
1733 "Decoding of %s failed", codes->
name);
1735 if (new_index + 1 >= new_codes_count) {
1738 new_codes_count *= 2;
1742 sizeof(*new_codes));
1743 if (renew_codes == NULL) {
1748 memset(&new_codes[new_codes_count / 2],
1750 new_codes_count / 2 *
1751 sizeof(*new_codes));
1752 new_codes = renew_codes;
1756 code = decode_ctx.
code;
1758 code2 = decode_ctx.
code;
1760 if (ret && code2 != decode_ctx.
code) {
1761 new_codes[new_index].
next =
1762 malloc(IR_CODE_NODE_SIZE);
1763 if (new_codes[new_index].
next) {
1764 memset(new_codes[new_index].next,
1767 new_codes[new_index].
next->code =
1771 new_codes[new_index].
name = codes->
name;
1772 new_codes[new_index].
code = decode_ctx.
code;
1777 new_codes[new_index].
name = NULL;
1778 remote.codes = new_codes;
1779 fprint_remotes(stdout, &remote, opts->commandline);
1780 remote.codes = NULL;
1793 f = fopen(opts->filename,
"r");
1795 fprintf(stderr,
"Cannot open file: %s\n", opts->filename);
1800 fprintf(stderr,
"Cannot parse file: %s\n", opts->filename);
1803 for (; r != NULL; r = r->next) {
1805 log_error(
"remote %s not in raw mode, ignoring",
1815 ssize_t
raw_read(
void* buffer,
size_t size,
unsigned int timeout_us)
1817 if (!mywaitfordata(timeout_us))
1828 if (is_pulse(btn_state->data)) {
1830 }
else if (is_space(btn_state->data)) {
1831 if (is_const(&remote)) {
1832 if (remote.
gap > btn_state->sum) {
1833 ref = (remote.
gap - btn_state->sum);
1834 ref *= (100 - remote.
eps);
1839 r = btn_state->data > ref;
1841 r = btn_state->data > (remote.
gap * (100 - remote.
eps)) / 100;
1853 const char*
const MSG_BAD_LENGTH =
1854 "Signal length is %d\n" 1855 "That's weird because the signal length must be odd!\n";
1864 if (btn_state->no_data) {
1865 btn_state->no_data = 0;
1866 return STS_BTN_TIMEOUT;
1868 switch (last_status) {
1870 return STS_BTN_GET_NAME;
1871 case STS_BTN_GET_NAME:
1872 if (strchr(btn_state->buffer,
' ') != NULL) {
1875 "The name must not contain any whitespace.");
1876 return STS_BTN_SOFT_ERROR;
1878 if (strchr(btn_state->buffer,
'\t') != NULL) {
1881 "The name must not contain any whitespace.");
1882 return STS_BTN_SOFT_ERROR;
1884 if (strcasecmp(btn_state->buffer,
"begin") == 0) {
1887 "'%s' is not allowed as button name\n",
1889 return STS_BTN_SOFT_ERROR;
1891 if (strcasecmp(btn_state->buffer,
"end") == 0) {
1894 "'%s' is not allowed as button name\n",
1896 return STS_BTN_SOFT_ERROR;
1898 if (strlen(btn_state->buffer) == 0)
1899 return STS_BTN_RECORD_DONE;
1900 if (!opts->disable_namespace
1901 && !is_in_namespace(btn_state->buffer)) {
1904 "'%s' is not in name space" 1905 " (use --disable-namespace to override)\n",
1907 return STS_BTN_SOFT_ERROR;
1909 return STS_BTN_INIT_DATA;
1910 case STS_BTN_INIT_DATA:
1918 return opts->force ? STS_BTN_GET_RAW_DATA : STS_BTN_GET_DATA;
1919 case STS_BTN_GET_DATA:
1920 for (retries = RETRIES; retries > 0; ) {
1921 if (!mywaitfordata(10000000)) {
1922 btn_state->no_data = 1;
1923 return STS_BTN_TIMEOUT;
1932 &(state->decode_ctx))) {
1938 if (!
resethw(btn_state->started_as_root)) {
1941 "Could not reset hardware.\n");
1942 return STS_BTN_HARD_ERROR;
1945 "Cannot decode data\n");
1947 return STS_BTN_SOFT_ERROR;
1949 btn_state->
ncode.
name = btn_state->buffer;
1953 &(state->decode_ctx))) {
1954 code2 = state->decode_ctx.
code;
1956 if (state->decode_ctx.
code != code2) {
1958 malloc(IR_CODE_NODE_SIZE);
1970 return STS_BTN_BUTTON_DONE;
1971 case STS_BTN_GET_RAW_DATA:
1972 btn_state->count = 0;
1974 while (btn_state->count < MAX_SIGNALS) {
1975 if (btn_state->count == 0)
1978 timeout = remote.
gap * 5;
1980 if (!btn_state->data) {
1981 if (btn_state->count == 0)
1982 return STS_BTN_TIMEOUT;
1983 btn_state->data = remote.
gap;
1985 if (btn_state->count == 0) {
1986 if (is_pulse(btn_state->data)
1987 || btn_state->data <
1992 btn_state->count = 0;
1995 "Something went wrong.");
1996 return STS_BTN_SOFT_ERROR;
1999 if (raw_data_ok(btn_state)) {
2002 btn_state->count - 1);
2003 if (btn_state->count % 2) {
2007 btn_state->count - 1);
2010 btn_state->count = 0;
2011 return STS_BTN_SOFT_ERROR;
2016 btn_state->count - 1;
2020 signals[btn_state->count - 1] =
2021 btn_state->data & PULSE_MASK;
2023 btn_state->data & PULSE_MASK;
2027 if (btn_state->count == MAX_SIGNALS) {
2029 "Signal is too long.\n");
2030 return STS_BTN_SOFT_ERROR;
2032 return STS_BTN_BUTTON_DONE;
2033 case STS_BTN_RECORD_DONE:
2034 if (is_raw(&remote))
2035 return STS_BTN_ALL_DONE;
2036 if (!
resethw(btn_state->started_as_root)) {
2038 "Could not reset hardware.");
2039 return STS_BTN_HARD_ERROR;
2041 return STS_BTN_BUTTONS_DONE;
2042 case STS_BTN_BUTTONS_DONE:
2043 f = fopen(opts->tmpfile,
"r");
2046 "Could not reopen config file");
2047 return STS_BTN_HARD_ERROR;
2051 if (my_remote == NULL) {
2055 "config file contains no valid remote");
2056 return STS_BTN_HARD_ERROR;
2058 if (my_remote == (
void*)-1) {
2062 "Reading of config file failed");
2063 return STS_BTN_HARD_ERROR;
2065 sts = STS_BTN_ALL_DONE;
2067 remote = *my_remote;
2070 if (!has_toggle_bit_mask(my_remote)) {
2071 if (!opts->using_template
2073 remote = *(my_remote);
2074 sts = STS_BTN_GET_TOGGLE_BITS;
2077 set_toggle_bit_mask(my_remote,
2082 if (!opts->update) {
2086 remote = *my_remote;
2088 case STS_BTN_BUTTON_DONE:
2089 return STS_BTN_BUTTON_DONE;
2090 case STS_BTN_HARD_ERROR:
2091 return STS_BTN_HARD_ERROR;
2094 "record_buttons(): bad state: %d\n",
2096 return STS_BTN_HARD_ERROR;
2104 state->fout = fopen(opts->tmpfile,
"w");
2105 if (state->fout == NULL) {
2106 log_error(
"Could not open new config file %s", tmpfile);
2110 fprint_copyright(state->fout);
2111 fprint_comment(state->fout, &remote, opts->commandline);
2112 fprint_remote_head(state->fout, &remote);
2113 fprint_remote_signal_head(state->fout, &remote);
2121 state->fout = fopen(opts->filename,
"w");
2122 if (state->fout == NULL) {
2127 fprint_copyright(state->fout);
2128 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.