Little fix.
[irreco.git] / lirc-0.8.4a / daemons / ir_remote.c
blob41933bf37cf2f5eb629231f4bbd09395965a8f50
1 /* $Id: ir_remote.c,v 5.38 2008/06/03 17:21:29 lirc Exp $ */
3 /****************************************************************************
4 ** ir_remote.c *************************************************************
5 ****************************************************************************
7 * ir_remote.c - sends and decodes the signals from IR remotes
8 *
9 * Copyright (C) 1996,97 Ralph Metzler (rjkm@thp.uni-koeln.de)
10 * Copyright (C) 1998 Christoph Bartelmus (lirc@bartelmus.de)
14 #ifdef HAVE_CONFIG_H
15 # include <config.h>
16 #endif
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <fcntl.h>
21 #include <limits.h>
23 #include <sys/ioctl.h>
25 #include "drivers/lirc.h"
27 #include "lircd.h"
28 #include "ir_remote.h"
29 #include "hardware.h"
30 #include "release.h"
32 struct ir_remote *decoding=NULL;
34 struct ir_remote *last_remote=NULL;
35 struct ir_remote *repeat_remote=NULL;
36 struct ir_ncode *repeat_code;
38 extern struct hardware hw;
40 static int match_ir_code(struct ir_remote *remote, ir_code a, ir_code b)
42 return ((remote->ignore_mask|a) == (remote->ignore_mask|b) ||
43 (remote->ignore_mask|a) ==
44 (remote->ignore_mask|(b^remote->toggle_bit_mask)));
47 void get_frequency_range(struct ir_remote *remotes,
48 unsigned int *min_freq,unsigned int *max_freq)
50 struct ir_remote *scan;
52 /* use remotes carefully, it may be changed on SIGHUP */
53 scan=remotes;
54 if(scan==NULL)
56 *min_freq=DEFAULT_FREQ;
57 *max_freq=DEFAULT_FREQ;
59 else
61 *min_freq=scan->freq;
62 *max_freq=scan->freq;
63 scan=scan->next;
65 while(scan)
67 if(scan->freq!=0)
69 if(scan->freq>*max_freq)
71 *max_freq=scan->freq;
73 else if(scan->freq<*min_freq)
75 *min_freq=scan->freq;
78 scan=scan->next;
82 struct ir_remote *is_in_remotes(struct ir_remote *remotes,
83 struct ir_remote *remote)
85 while(remotes != NULL)
87 if(remotes == remote)
89 return remote;
91 remotes = remotes->next;
93 return NULL;
96 struct ir_remote *get_ir_remote(struct ir_remote *remotes,char *name)
98 struct ir_remote *all;
100 /* use remotes carefully, it may be changed on SIGHUP */
101 all=remotes;
102 while(all)
104 if(strcasecmp(all->name,name)==0)
106 return(all);
108 all=all->next;
110 return(NULL);
113 int map_code(struct ir_remote *remote,
114 ir_code *prep,ir_code *codep,ir_code *postp,
115 int pre_bits,ir_code pre,
116 int bits,ir_code code,
117 int post_bits,ir_code post)
119 ir_code all;
121 if(pre_bits+bits+post_bits!=
122 remote->pre_data_bits+remote->bits+remote->post_data_bits)
124 return(0);
126 all=(pre&gen_mask(pre_bits));
127 all<<=bits;
128 all|=(code&gen_mask(bits));
129 all<<=post_bits;
130 all|=(post&gen_mask(post_bits));
132 *postp=(all&gen_mask(remote->post_data_bits));
133 all>>=remote->post_data_bits;
134 *codep=(all&gen_mask(remote->bits));
135 all>>=remote->bits;
136 *prep=(all&gen_mask(remote->pre_data_bits));
138 LOGPRINTF(1,"pre: %llx", (unsigned long long) *prep);
139 LOGPRINTF(1,"code: %llx", (unsigned long long) *codep);
140 LOGPRINTF(1,"post: %llx", (unsigned long long) *postp);
141 LOGPRINTF(1, "code: %016llx\n", code);
143 return(1);
146 void map_gap(struct ir_remote *remote,
147 struct timeval *start, struct timeval *last,
148 lirc_t signal_length,
149 int *repeat_flagp,
150 lirc_t *min_remaining_gapp,
151 lirc_t *max_remaining_gapp)
153 // Time gap (us) between a keypress on the remote control and
154 // the next one.
155 lirc_t gap;
157 // Check the time gap between the last keypress and this one.
158 if (start->tv_sec - last->tv_sec >= 2) {
159 // Gap of 2 or more seconds: this is not a repeated keypress.
160 *repeat_flagp = 0;
161 gap = 0;
162 } else {
163 // Calculate the time gap in microseconds.
164 gap = time_elapsed(last, start);
165 if(expect_at_most(remote, gap, remote->max_remaining_gap))
167 // The gap is shorter than a standard gap
168 // (with relative or aboslute tolerance): this
169 // is a repeated keypress.
170 *repeat_flagp = 1;
172 else
174 // Standard gap: this is a new keypress.
175 *repeat_flagp = 0;
179 // Calculate extimated time gap remaining for the next code.
180 if (is_const(remote)) {
181 // The sum (signal_length + gap) is always constant
182 // so the gap is shorter when the code is longer.
183 if (min_gap(remote) > signal_length) {
184 *min_remaining_gapp = min_gap(remote) - signal_length;
185 *max_remaining_gapp = max_gap(remote) - signal_length;
186 } else {
187 *min_remaining_gapp = 0;
188 if(max_gap(remote) > signal_length)
190 *max_remaining_gapp = max_gap(remote) - signal_length;
192 else
194 *max_remaining_gapp = 0;
197 } else {
198 // The gap after the signal is always constant.
199 // This is the case of Kanam Accent serial remote.
200 *min_remaining_gapp = min_gap(remote);
201 *max_remaining_gapp = max_gap(remote);
204 LOGPRINTF(1, "repeat_flagp: %d", *repeat_flagp);
205 LOGPRINTF(1, "is_const(remote): %d", is_const(remote));
206 LOGPRINTF(1, "remote->gap range: %lu %lu",
207 (unsigned long) min_gap(remote),
208 (unsigned long) max_gap(remote));
209 LOGPRINTF(1, "remote->remaining_gap: %lu %lu",
210 (unsigned long) remote->min_remaining_gap,
211 (unsigned long) remote->max_remaining_gap);
212 LOGPRINTF(1, "signal length: %lu",
213 (unsigned long) signal_length);
214 LOGPRINTF(1, "gap: %lu",
215 (unsigned long) gap);
216 LOGPRINTF(1, "extim. remaining_gap: %lu %lu",
217 (unsigned long) *min_remaining_gapp,
218 (unsigned long) *max_remaining_gapp);
222 struct ir_ncode *get_code_by_name(struct ir_remote *remote,char *name)
224 struct ir_ncode *all;
226 all=remote->codes;
227 while(all->name!=NULL)
229 if(strcasecmp(all->name,name)==0)
231 return(all);
233 all++;
235 return(0);
238 struct ir_ncode *get_code(struct ir_remote *remote,
239 ir_code pre,ir_code code,ir_code post,
240 ir_code *toggle_bit_mask_statep)
242 ir_code pre_mask,code_mask,post_mask,toggle_bit_mask_state,all;
243 int found_code, have_code;
244 struct ir_ncode *codes,*found;
246 pre_mask=code_mask=post_mask=0;
248 if(has_toggle_bit_mask(remote))
250 pre_mask = remote->toggle_bit_mask >>
251 (remote->bits + remote->post_data_bits);
252 post_mask = remote->toggle_bit_mask &
253 gen_mask(remote->post_data_bits);
255 if(has_ignore_mask(remote))
257 pre_mask |= remote->ignore_mask >>
258 (remote->bits + remote->post_data_bits);
259 post_mask |= remote->ignore_mask &
260 gen_mask(remote->post_data_bits);
262 if(has_toggle_mask(remote) && remote->toggle_mask_state%2)
264 ir_code *affected,mask,mask_bit;
265 int bit,current_bit;
267 affected=&post;
268 mask=remote->toggle_mask;
269 for(bit=current_bit=0;bit<bit_count(remote);bit++,current_bit++)
271 if(bit==remote->post_data_bits)
273 affected=&code;
274 current_bit=0;
276 if(bit==remote->post_data_bits+remote->bits)
278 affected=&pre;
279 current_bit=0;
281 mask_bit=mask&1;
282 (*affected)^=(mask_bit<<current_bit);
283 mask>>=1;
286 if(has_pre(remote))
288 if((pre|pre_mask)!=(remote->pre_data|pre_mask))
290 LOGPRINTF(1,"bad pre data");
291 # ifdef LONG_IR_CODE
292 LOGPRINTF(2,"%llx %llx",pre,remote->pre_data);
293 # else
294 LOGPRINTF(2,"%lx %lx",pre,remote->pre_data);
295 # endif
296 return(0);
298 LOGPRINTF(1,"pre");
301 if(has_post(remote))
303 if((post|post_mask)!=(remote->post_data|post_mask))
305 LOGPRINTF(1,"bad post data");
306 # ifdef LONG_IR_CODE
307 LOGPRINTF(2,"%llx %llx",post,remote->post_data);
308 # else
309 LOGPRINTF(2,"%lx %lx",post,remote->post_data);
310 # endif
311 return(0);
313 LOGPRINTF(1,"post");
316 all = gen_ir_code(remote, pre, code, post);
318 toggle_bit_mask_state = all&remote->toggle_bit_mask;
320 found=NULL;
321 found_code=0;
322 have_code=0;
323 codes=remote->codes;
324 if(codes!=NULL)
326 while(codes->name!=NULL)
328 ir_code next_all;
330 next_all = gen_ir_code(remote, remote->pre_data,
331 get_ir_code(codes, codes->current),
332 remote->post_data);
333 if(match_ir_code(remote, next_all, all))
335 found_code=1;
336 if(codes->next!=NULL)
338 if(codes->current==NULL)
340 codes->current=codes->next;
342 else
344 codes->current=
345 codes->current->next;
348 if(!have_code)
350 found=codes;
351 if(codes->current==NULL)
353 have_code=1;
357 else
359 /* find longest matching sequence */
360 struct ir_code_node *search;
362 search = codes->next;
363 if(search == NULL ||
364 (codes->next != NULL && codes->current == NULL))
366 codes->current=NULL;
368 else
370 int sequence_match = 0;
372 while(search != codes->current->next)
374 struct ir_code_node *prev, *next;
375 int flag = 1;
377 prev = NULL; /* means codes->code */
378 next = search;
379 while(next != codes->current)
381 if(get_ir_code(codes, prev) != get_ir_code(codes, next))
383 flag = 0;
384 break;
386 prev = get_next_ir_code_node(codes, prev);
387 next = get_next_ir_code_node(codes, next);
389 if(flag == 1)
391 next_all = gen_ir_code(remote, remote->pre_data,
392 get_ir_code(codes, prev),
393 remote->post_data);
394 if(match_ir_code(remote, next_all, all))
396 codes->current = get_next_ir_code_node(codes, prev);
397 sequence_match = 1;
398 break;
401 search = search->next;
403 if(!sequence_match) codes->current = NULL;
406 codes++;
409 # ifdef DYNCODES
410 if(!found_code)
412 if(remote->dyncodes[remote->dyncode].code!=code)
414 remote->dyncode++;
415 remote->dyncode%=2;
417 remote->dyncodes[remote->dyncode].code=code;
418 found=&(remote->dyncodes[remote->dyncode]);
419 found_code=1;
421 # endif
422 if(found_code && found!=NULL && has_toggle_mask(remote))
424 if(!(remote->toggle_mask_state%2))
426 remote->toggle_code=found;
427 LOGPRINTF(1,"toggle_mask_start");
429 else
431 if(found!=remote->toggle_code)
433 remote->toggle_code=NULL;
434 return(NULL);
436 remote->toggle_code=NULL;
439 *toggle_bit_mask_statep=toggle_bit_mask_state;
440 return(found);
443 unsigned long long set_code(struct ir_remote *remote,struct ir_ncode *found,
444 ir_code toggle_bit_mask_state,int repeat_flag,
445 lirc_t min_remaining_gap, lirc_t max_remaining_gap)
447 unsigned long long code;
448 struct timeval current;
450 LOGPRINTF(1,"found: %s",found->name);
452 gettimeofday(&current,NULL);
453 if(remote==last_remote &&
454 (found==remote->last_code || (found->next!=NULL && found->current!=NULL)) &&
455 repeat_flag &&
456 time_elapsed(&remote->last_send,&current)<1000000 &&
457 (!has_toggle_bit_mask(remote) || toggle_bit_mask_state==remote->toggle_bit_mask_state))
459 if(has_toggle_mask(remote))
461 remote->toggle_mask_state++;
462 if(remote->toggle_mask_state==4)
464 remote->reps++;
465 remote->toggle_mask_state=2;
468 else if(found->current==NULL)
470 remote->reps++;
473 else
475 if(found->next!=NULL && found->current==NULL)
477 remote->reps=1;
479 else
481 remote->reps=0;
483 if(has_toggle_mask(remote))
485 remote->toggle_mask_state=1;
486 remote->toggle_code=found;
488 if(has_toggle_bit_mask(remote))
490 remote->toggle_bit_mask_state=toggle_bit_mask_state;
493 last_remote=remote;
494 if(found->current==NULL) remote->last_code=found;
495 remote->last_send=current;
496 remote->min_remaining_gap=min_remaining_gap;
497 remote->max_remaining_gap=max_remaining_gap;
499 code=0;
500 if(has_pre(remote))
502 code|=remote->pre_data;
503 code=code<<remote->bits;
505 code|=found->code;
506 if(has_post(remote))
508 code=code<<remote->post_data_bits;
509 code|=remote->post_data;
511 if(remote->flags&COMPAT_REVERSE)
513 /* actually this is wrong: pre, code and post should
514 be rotated separately but we have to stay
515 compatible with older software
517 code=reverse(code,bit_count(remote));
519 return(code);
522 int write_message(char *buffer, size_t size, const char *remote_name,
523 const char *button_name, const char *button_suffix,
524 ir_code code, int reps)
526 int len;
528 #ifdef __GLIBC__
529 /* It seems you can't print 64-bit longs on glibc */
531 len=snprintf(buffer, size,"%08lx%08lx %02x %s%s %s\n",
532 (unsigned long) (code>>32),
533 (unsigned long) (code&0xFFFFFFFF),
534 reps,
535 button_name, button_suffix,
536 remote_name);
537 #else
538 len=snprintf(buffer, size, "%016llx %02x %s%s %s\n",
539 code,
540 reps,
541 button_name, button_suffix,
542 remote_name);
543 #endif
544 return len;
547 char *decode_all(struct ir_remote *remotes)
549 struct ir_remote *remote;
550 static char message[PACKET_SIZE+1];
551 ir_code pre,code,post;
552 struct ir_ncode *ncode;
553 int repeat_flag;
554 ir_code toggle_bit_mask_state;
555 lirc_t min_remaining_gap, max_remaining_gap;
556 struct ir_remote *scan;
557 struct ir_ncode *scan_ncode;
559 /* use remotes carefully, it may be changed on SIGHUP */
560 decoding=remote=remotes;
561 while(remote)
563 LOGPRINTF(1,"trying \"%s\" remote",remote->name);
565 if(hw.decode_func(remote,&pre,&code,&post,&repeat_flag,
566 &min_remaining_gap, &max_remaining_gap) &&
567 (ncode=get_code(remote,pre,code,post,&toggle_bit_mask_state)))
569 int len;
571 code=set_code(remote,ncode,toggle_bit_mask_state,
572 repeat_flag,
573 min_remaining_gap,
574 max_remaining_gap);
575 if((has_toggle_mask(remote) &&
576 remote->toggle_mask_state%2) ||
577 ncode->current!=NULL)
579 decoding=NULL;
580 return(NULL);
583 for(scan = decoding; scan != NULL; scan = scan->next)
585 for( scan_ncode = scan->codes; scan_ncode->name != NULL; scan_ncode++)
587 scan_ncode->current = NULL;
590 register_button_press
591 (remote, remote->last_code,
592 code, remote->reps-(ncode->next ? 1:0));
594 len = write_message(message, PACKET_SIZE+1,
595 remote->name,
596 remote->last_code->name, "", code,
597 remote->reps-(ncode->next ? 1:0));
598 decoding=NULL;
599 if(len>=PACKET_SIZE+1)
601 logprintf(LOG_ERR,"message buffer overflow");
602 return(NULL);
604 else
606 return(message);
609 else
611 LOGPRINTF(1,"failed \"%s\" remote",remote->name);
613 remote->toggle_mask_state=0;
614 remote=remote->next;
616 decoding=NULL;
617 last_remote=NULL;
618 LOGPRINTF(1,"decoding failed for all remotes");
619 return(NULL);
622 int send_ir_ncode(struct ir_remote *remote,struct ir_ncode *code)
624 int ret;
626 ret = hw.send_func(remote, code);
628 if(ret)
630 gettimeofday(&remote->last_send, NULL);
631 remote->last_code = code;
634 return ret;