30 #include <sys/ioctl.h>
32 #include "include/media/lirc.h"
33 #include "lirc/ir_remote.h"
34 #include "lirc/driver.h"
35 #include "lirc/release.h"
36 #include "lirc/lirc_log.h"
40 "__EOF",
LIRC_EOF, 1, NULL, NULL, NULL, 0
44 static const char*
const PACKET_EOF =
"0000000008000000 00 __EOF lirc\n";
47 static struct ir_remote lirc_internal_remote = {
"lirc" };
57 static int dyncodes = 0;
70 if (new_ncode == NULL)
72 memcpy(new_ncode, ncode,
sizeof(
struct ir_ncode));
73 new_ncode->
name = ncode->
name == NULL ? NULL : strdup(ncode->
name);
75 signal_size = ncode->
length *
sizeof(lirc_t);
76 new_ncode->
signals = (lirc_t*)malloc(signal_size);
83 node_ptr = &(new_ncode->
next);
84 for (node = ncode->
next; node != NULL; node = node->next) {
88 node_ptr = &(new_node->next);
104 while (node != NULL) {
118 dyncodes = use_dyncodes;
122 static lirc_t time_left(
struct timeval* current,
123 struct timeval* last,
126 unsigned long secs, diff;
128 secs = current->tv_sec - last->tv_sec;
129 diff = 1000000 * secs + current->tv_usec - last->tv_usec;
130 return (lirc_t)(diff < gap ? gap - diff : 0);
149 unsigned int* min_freq,
150 unsigned int* max_freq)
160 *min_freq = scan->
freq;
161 *max_freq = scan->
freq;
165 if (scan->
freq != 0) {
166 if (scan->
freq > *max_freq)
167 *max_freq = scan->
freq;
168 else if (scan->
freq < *min_freq)
169 *min_freq = scan->
freq;
186 lirc_t* max_gap_lengthp,
187 lirc_t* min_pulse_lengthp,
188 lirc_t* min_space_lengthp,
189 lirc_t* max_pulse_lengthp,
190 lirc_t* max_space_lengthp)
194 lirc_t min_pulse_length = 0, min_space_length = 0;
195 lirc_t max_pulse_length = 0, max_space_length = 0;
201 if (val > max_gap_length)
202 max_gap_length = val;
203 val = lower_limit(scan, scan->min_pulse_length);
204 if (min_pulse_length == 0 || val < min_pulse_length)
205 min_pulse_length = val;
206 val = lower_limit(scan, scan->min_space_length);
207 if (min_space_length == 0 || val > min_space_length)
208 min_space_length = val;
209 val = upper_limit(scan, scan->max_pulse_length);
210 if (val > max_pulse_length)
211 max_pulse_length = val;
212 val = upper_limit(scan, scan->max_space_length);
213 if (val > max_space_length)
214 max_space_length = val;
218 *min_pulse_lengthp = min_pulse_length;
219 *min_space_lengthp = min_space_length;
220 *max_pulse_lengthp = max_pulse_length;
221 *max_space_lengthp = max_space_length;
234 while (remotes != NULL) {
235 if (remotes == remote)
237 remotes = remotes->next;
250 if (strcmp(name,
"lirc") == 0)
251 return &lirc_internal_remote;
253 if (strcasecmp(all->
name, name) == 0)
290 all = (pre & gen_mask(pre_bits));
292 all |= (code & gen_mask(bits));
294 all |= (post & gen_mask(post_bits));
298 ctx->
code = (all & gen_mask(remote->
bits));
299 all >>= remote->
bits;
323 const struct timeval* start,
324 const struct timeval* last,
325 lirc_t signal_length)
332 if (start->tv_sec - last->tv_sec >= 2) {
338 gap = time_elapsed(last, start);
351 if (is_const(remote)) {
354 if (min_gap(remote) > signal_length) {
361 if (max_gap(remote) > signal_length)
375 LOGPRINTF(1,
"is_const(remote): %d", is_const(remote));
376 LOGPRINTF(1,
"remote->gap range: %lu %lu", (__u32)min_gap(
377 remote), (__u32)max_gap(remote));
378 LOGPRINTF(1,
"remote->remaining_gap: %lu %lu",
381 LOGPRINTF(1,
"signal length: %lu", (__u32)signal_length);
383 LOGPRINTF(1,
"extim. remaining_gap: %lu %lu",
397 if (strcmp(remote->
name,
"lirc") == 0)
398 return strcmp(name,
"__EOF") == 0 ? &NCODE_EOF : 0;
399 while (all->
name != NULL) {
400 if (strcasecmp(all->
name, name) == 0)
409 void find_longest_match(
struct ir_remote* remote,
421 int sequence_match = 0;
423 search = codes->
next;
425 || (codes->
next != NULL && codes->
current == NULL)) {
429 while (search != codes->
current->next) {
432 while (next != codes->
current) {
433 if (get_ir_code(codes, prev)
434 != get_ir_code(codes, next)) {
438 prev = get_next_ir_code_node(codes, prev);
439 next = get_next_ir_code_node(codes, next);
442 *next_all = gen_ir_code(remote,
444 get_ir_code(codes, prev),
446 if (match_ir_code(remote, *next_all, all)) {
448 get_next_ir_code_node(codes, prev);
456 search = search->next;
468 ir_code* toggle_bit_mask_statep)
470 ir_code pre_mask, code_mask, post_mask, toggle_bit_mask_state, all;
471 int found_code, have_code;
475 pre_mask = code_mask = post_mask = 0;
477 if (has_toggle_bit_mask(remote)) {
483 if (has_ignore_mask(remote)) {
489 if (has_toggle_mask(remote) && remote->toggle_mask_state % 2) {
493 int bit, current_bit;
497 for (bit = current_bit = 0; bit < bit_count(remote);
498 bit++, current_bit++) {
508 (*affected) ^= (mask_bit << current_bit);
512 if (has_pre(remote)) {
513 if ((pre | pre_mask) != (remote->
pre_data | pre_mask)) {
521 if (has_post(remote)) {
522 if ((post | post_mask) != (remote->
post_data | post_mask)) {
530 all = gen_ir_code(remote, pre, code, post);
532 if (*repeat_flag && has_repeat_mask(remote))
540 codes = remote->codes;
542 while (codes->
name != NULL) {
545 next_all = gen_ir_code(remote,
550 if (match_ir_code(remote, next_all, all) ||
552 has_repeat_mask(remote) &&
553 match_ir_code(remote,
557 if (codes->
next != NULL) {
570 find_longest_match(remote,
581 if (!found_code && dyncodes) {
590 if (found_code && found != NULL && has_toggle_mask(remote)) {
591 if (!(remote->toggle_mask_state % 2)) {
602 *toggle_bit_mask_statep = toggle_bit_mask_state;
607 static __u64 set_code(
struct ir_remote* remote,
612 struct timeval current;
613 static struct ir_remote* last_decoded = NULL;
617 gettimeofday(¤t, NULL);
618 LOGPRINTF(1,
"%lx %lx %lx %d %d %d %d %d %d %d",
619 remote, last_remote, last_decoded,
620 remote == last_decoded,
625 (!has_toggle_bit_mask(remote)
627 toggle_bit_mask_state ==
629 ->toggle_bit_mask_state));
634 "repeat indicated although release was detected before");
638 if (remote == last_decoded &&
640 || (found->
next != NULL && found->
current != NULL))
642 && time_elapsed(&remote->
last_send, ¤t) < 1000000
643 && (!has_toggle_bit_mask(remote)
644 || toggle_bit_mask_state == remote->toggle_bit_mask_state)) {
645 if (has_toggle_mask(remote)) {
646 remote->toggle_mask_state++;
647 if (remote->toggle_mask_state == 4) {
649 remote->toggle_mask_state = 2;
651 }
else if (found->
current == NULL) {
659 if (has_toggle_mask(remote)) {
660 remote->toggle_mask_state = 1;
663 if (has_toggle_bit_mask(remote))
664 remote->toggle_bit_mask_state = toggle_bit_mask_state;
666 last_remote = remote;
667 last_decoded = remote;
675 if (has_pre(remote)) {
680 if (has_post(remote)) {
689 ctx->
code = reverse(ctx->
code, bit_count(remote));
707 const char* remote_name,
708 const char* button_name,
709 const char* button_suffix,
716 len = snprintf(buffer, size,
"%016llx %02x %s%s %s\n",
717 (
unsigned long long)code, reps, button_name,
718 button_suffix, remote_name);
735 decoding = remote = remotes;
739 ncode = get_code(remote,
742 &toggle_bit_mask_state);
747 if (ncode == &NCODE_EOF) {
748 logprintf(LIRC_DEBUG,
749 "decode all: returning EOF");
751 PACKET_EOF,
sizeof(message));
754 ctx.
code = set_code(remote,
756 toggle_bit_mask_state,
758 if ((has_toggle_mask(remote)
759 && remote->toggle_mask_state % 2)
765 for (scan = decoding;
768 for (scan_ncode = scan->codes;
769 scan_ncode->
name != NULL;
775 reps = remote->reps - (ncode->
next ? 1 : 0);
777 if (reps <= remote->suppress_repeat) {
783 register_button_press(remote,
795 logprintf(LIRC_ERROR,
796 "message buffer overflow");
806 remote->toggle_mask_state = 0;
807 remote = remote->next;
811 LOGPRINTF(1,
"decoding failed for all remotes");
823 struct timeval current;
826 gettimeofday(¤t, NULL);
827 usecs = time_left(¤t,
831 if (repeat_remote == NULL || remote !=
849 return (
const struct ir_remote*)&decoding;
struct ir_remote * last_remote
struct ir_ncode * repeat_code
int(*const send_func)(struct ir_remote *remote, struct ir_ncode *code)
void ir_remote_init(int use_dyncodes)
struct ir_ncode * toggle_code
void get_filter_parameters(const struct ir_remote *remotes, lirc_t *max_gap_lengthp, lirc_t *min_pulse_lengthp, lirc_t *min_space_lengthp, lirc_t *max_pulse_lengthp, lirc_t *max_space_lengthp)
struct ir_code_node * next
struct ir_ncode * get_code_by_name(const struct ir_remote *remote, const char *name)
struct ir_ncode * last_code
struct ir_remote * get_ir_remote(const struct ir_remote *remotes, const char *name)
int map_code(const struct ir_remote *remote, struct decode_ctx_t *ctx, int pre_bits, ir_code pre, int bits, ir_code code, int post_bits, ir_code post)
char * decode_all(struct ir_remote *remotes)
struct ir_code_node * current
int write_message(char *buffer, size_t size, const char *remote_name, const char *button_name, const char *button_suffix, ir_code code, int reps)
const struct ir_remote * is_in_remotes(const struct ir_remote *remotes, const struct ir_remote *remote)
const struct ir_remote * get_decoding(void)
#define LOGPRINTF(level, fmt, args...)
void ncode_free(struct ir_ncode *ncode)
void map_gap(const struct ir_remote *remote, struct decode_ctx_t *ctx, const struct timeval *start, const struct timeval *last, lirc_t signal_length)
struct ir_ncode * ncode_dup(struct ir_ncode *ncode)
void get_frequency_range(const struct ir_remote *remotes, unsigned int *min_freq, unsigned int *max_freq)
int(*const decode_func)(struct ir_remote *remote, struct decode_ctx_t *ctx)
const struct driver const * curr_driver
struct ir_ncode dyncodes[2]
struct ir_remote * repeat_remote
int send_ir_ncode(struct ir_remote *remote, struct ir_ncode *code, int delay)