30 #include <sys/ioctl.h> 32 #ifdef HAVE_KERNEL_LIRC_H 33 #include <linux/lirc.h> 35 #include "include/media/lirc.h" 38 #include "lirc/ir_remote.h" 39 #include "lirc/driver.h" 40 #include "lirc/release.h" 41 #include "lirc/lirc_log.h" 47 "__EOF",
LIRC_EOF, 1, NULL, NULL, NULL, 0
51 static const char*
const PACKET_EOF =
"0000000008000000 00 __EOF lirc\n";
54 static struct ir_remote lirc_internal_remote = {
"lirc" };
64 static int dyncodes = 0;
77 if (new_ncode == NULL)
79 memcpy(new_ncode, ncode,
sizeof(
struct ir_ncode));
80 new_ncode->
name = ncode->
name == NULL ? NULL : strdup(ncode->
name);
82 signal_size = ncode->
length *
sizeof(lirc_t);
83 new_ncode->
signals = (lirc_t*)malloc(signal_size);
90 node_ptr = &(new_ncode->
next);
91 for (node = ncode->
next; node != NULL; node = node->next) {
95 node_ptr = &(new_node->next);
111 while (node != NULL) {
125 dyncodes = use_dyncodes;
129 static lirc_t time_left(
struct timeval* current,
130 struct timeval* last,
133 unsigned long secs, diff;
135 secs = current->tv_sec - last->tv_sec;
136 diff = 1000000 * secs + current->tv_usec - last->tv_usec;
137 return (lirc_t)(diff < gap ? gap - diff : 0);
156 unsigned int* min_freq,
157 unsigned int* max_freq)
167 *min_freq = scan->
freq;
168 *max_freq = scan->
freq;
172 if (scan->
freq != 0) {
173 if (scan->
freq > *max_freq)
174 *max_freq = scan->
freq;
175 else if (scan->
freq < *min_freq)
176 *min_freq = scan->
freq;
193 lirc_t* max_gap_lengthp,
194 lirc_t* min_pulse_lengthp,
195 lirc_t* min_space_lengthp,
196 lirc_t* max_pulse_lengthp,
197 lirc_t* max_space_lengthp)
201 lirc_t min_pulse_length = 0, min_space_length = 0;
202 lirc_t max_pulse_length = 0, max_space_length = 0;
208 if (val > max_gap_length)
209 max_gap_length = val;
210 val = lower_limit(scan, scan->min_pulse_length);
211 if (min_pulse_length == 0 || val < min_pulse_length)
212 min_pulse_length = val;
213 val = lower_limit(scan, scan->min_space_length);
214 if (min_space_length == 0 || val > min_space_length)
215 min_space_length = val;
216 val = upper_limit(scan, scan->max_pulse_length);
217 if (val > max_pulse_length)
218 max_pulse_length = val;
219 val = upper_limit(scan, scan->max_space_length);
220 if (val > max_space_length)
221 max_space_length = val;
225 *min_pulse_lengthp = min_pulse_length;
226 *min_space_lengthp = min_space_length;
227 *max_pulse_lengthp = max_pulse_length;
228 *max_space_lengthp = max_space_length;
241 while (remotes != NULL) {
242 if (remotes == remote)
244 remotes = remotes->next;
257 if (strcmp(name,
"lirc") == 0)
258 return &lirc_internal_remote;
260 if (strcasecmp(all->
name, name) == 0)
297 all = (pre & gen_mask(pre_bits));
299 all |= (code & gen_mask(bits));
301 all |= (post & gen_mask(post_bits));
305 ctx->
code = (all & gen_mask(remote->
bits));
306 all >>= remote->
bits;
330 const struct timeval* start,
331 const struct timeval* last,
332 lirc_t signal_length)
339 if (start->tv_sec - last->tv_sec >= 2) {
345 gap = time_elapsed(last, start);
358 if (is_const(remote)) {
361 if (min_gap(remote) > signal_length) {
368 if (max_gap(remote) > signal_length)
382 log_trace(
"is_const(remote): %d", is_const(remote));
383 log_trace(
"remote->gap range: %lu %lu", (__u32)min_gap(
384 remote), (__u32)max_gap(remote));
385 log_trace(
"remote->remaining_gap: %lu %lu",
388 log_trace(
"signal length: %lu", (__u32)signal_length);
390 log_trace(
"extim. remaining_gap: %lu %lu",
404 if (strcmp(remote->
name,
"lirc") == 0)
405 return strcmp(name,
"__EOF") == 0 ? &NCODE_EOF : 0;
406 while (all->
name != NULL) {
407 if (strcasecmp(all->
name, name) == 0)
416 void find_longest_match(
struct ir_remote* remote,
428 int sequence_match = 0;
430 search = codes->
next;
432 || (codes->
next != NULL && codes->
current == NULL)) {
436 while (search != codes->
current->next) {
439 while (next != codes->
current) {
440 if (get_ir_code(codes, prev)
441 != get_ir_code(codes, next)) {
445 prev = get_next_ir_code_node(codes, prev);
446 next = get_next_ir_code_node(codes, next);
449 *next_all = gen_ir_code(remote,
451 get_ir_code(codes, prev),
453 if (match_ir_code(remote, *next_all, all)) {
455 get_next_ir_code_node(codes, prev);
463 search = search->next;
475 ir_code* toggle_bit_mask_statep)
477 ir_code pre_mask, code_mask, post_mask, toggle_bit_mask_state, all;
478 int found_code, have_code;
482 pre_mask = code_mask = post_mask = 0;
484 if (has_toggle_bit_mask(remote)) {
490 if (has_ignore_mask(remote)) {
496 if (has_toggle_mask(remote) && remote->toggle_mask_state % 2) {
500 int bit, current_bit;
504 for (bit = current_bit = 0; bit < bit_count(remote);
505 bit++, current_bit++) {
515 (*affected) ^= (mask_bit << current_bit);
519 if (has_pre(remote)) {
520 if ((pre | pre_mask) != (remote->
pre_data | pre_mask)) {
528 if (has_post(remote)) {
529 if ((post | post_mask) != (remote->
post_data | post_mask)) {
537 all = gen_ir_code(remote, pre,
code, post);
539 if (*repeat_flag && has_repeat_mask(remote))
547 codes = remote->codes;
549 while (codes->
name != NULL) {
552 next_all = gen_ir_code(remote,
557 if (match_ir_code(remote, next_all, all) ||
559 has_repeat_mask(remote) &&
560 match_ir_code(remote,
564 if (codes->
next != NULL) {
577 find_longest_match(remote,
588 if (!found_code && dyncodes) {
597 if (found_code && found != NULL && has_toggle_mask(remote)) {
598 if (!(remote->toggle_mask_state % 2)) {
609 *toggle_bit_mask_statep = toggle_bit_mask_state;
614 static __u64 set_code(
struct ir_remote* remote,
619 struct timeval current;
620 static struct ir_remote* last_decoded = NULL;
624 gettimeofday(¤t, NULL);
625 log_trace(
"%lx %lx %lx %d %d %d %d %d %d %d",
626 remote, last_remote, last_decoded,
627 remote == last_decoded,
632 (!has_toggle_bit_mask(remote)
634 toggle_bit_mask_state ==
636 ->toggle_bit_mask_state));
641 "repeat indicated although release was detected before");
645 if (remote == last_decoded &&
647 || (found->
next != NULL && found->
current != NULL))
649 && time_elapsed(&remote->
last_send, ¤t) < 1000000
650 && (!has_toggle_bit_mask(remote)
651 || toggle_bit_mask_state == remote->toggle_bit_mask_state)) {
652 if (has_toggle_mask(remote)) {
653 remote->toggle_mask_state++;
654 if (remote->toggle_mask_state == 4) {
656 remote->toggle_mask_state = 2;
658 }
else if (found->
current == NULL) {
666 if (has_toggle_mask(remote)) {
667 remote->toggle_mask_state = 1;
670 if (has_toggle_bit_mask(remote))
671 remote->toggle_bit_mask_state = toggle_bit_mask_state;
673 last_remote = remote;
674 last_decoded = remote;
682 if (has_pre(remote)) {
687 if (has_post(remote)) {
696 ctx->
code = reverse(ctx->
code, bit_count(remote));
714 const char* remote_name,
715 const char* button_name,
716 const char* button_suffix,
723 len = snprintf(buffer, size,
"%016llx %02x %s%s %s\n",
724 (
unsigned long long)code, reps, button_name,
725 button_suffix, remote_name);
742 decoding = remote = remotes;
746 ncode = get_code(remote,
749 &toggle_bit_mask_state);
754 if (ncode == &NCODE_EOF) {
757 PACKET_EOF,
sizeof(message));
760 ctx.
code = set_code(remote,
762 toggle_bit_mask_state,
764 if ((has_toggle_mask(remote)
765 && remote->toggle_mask_state % 2)
771 for (scan = decoding;
774 for (scan_ncode = scan->codes;
775 scan_ncode->
name != NULL;
781 reps = remote->reps - (ncode->
next ? 1 : 0);
783 if (reps <= remote->suppress_repeat) {
789 register_button_press(remote,
811 remote->toggle_mask_state = 0;
812 remote = remote->next;
816 log_trace(
"decoding failed for all remotes");
828 struct timeval current;
831 gettimeofday(¤t, NULL);
832 usecs = time_left(¤t,
836 if (repeat_remote == NULL || remote !=
854 return (
const struct ir_remote*)&decoding;
struct ir_remote * last_remote
struct ir_ncode * repeat_code
void ir_remote_init(int use_dyncodes)
const struct driver *const curr_driver
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)
#define log_debug(fmt,...)
int(*const decode_func)(struct ir_remote *remote, struct decode_ctx_t *ctx)
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)
int(*const send_func)(struct ir_remote *remote, struct ir_ncode *code)
struct ir_code_node * current
#define log_error(fmt,...)
#define log_trace1(fmt,...)
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)
#define log_trace(fmt,...)
const struct ir_remote * is_in_remotes(const struct ir_remote *remotes, const struct ir_remote *remote)
const struct ir_remote * get_decoding(void)
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)
struct ir_ncode dyncodes[2]
struct ir_remote * repeat_remote
int send_ir_ncode(struct ir_remote *remote, struct ir_ncode *code, int delay)