LIRC libraries
Linux Infrared Remote Control
ir_remote.h
Go to the documentation of this file.
1 /****************************************************************************
2 ** ir_remote.h *************************************************************
3 ****************************************************************************
4 *
5 * ir_remote.h - describes and decodes the signals from IR remotes
6 *
7 * Copyright (C) 1996,97 Ralph Metzler <rjkm@thp.uni-koeln.de>
8 * Copyright (C) 1998 Christoph Bartelmus <lirc@bartelmus.de>
9 *
10 */
21 #ifndef IR_REMOTE_H
22 #define IR_REMOTE_H
23 
24 #include <sys/types.h>
25 #include <sys/time.h>
26 #include <unistd.h>
27 #include <string.h>
28 #include <math.h>
29 #include <stdlib.h>
30 
31 #include "driver.h"
32 
33 #include "ir_remote_types.h"
34 
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38 
39 
41 struct ir_ncode* ncode_dup(struct ir_ncode* ncode);
42 
44 void ncode_free(struct ir_ncode* ncode);
45 
46 
50 extern struct ir_remote* last_remote;
51 
52 
57 extern struct ir_remote* repeat_remote;
58 
62 extern struct ir_ncode* repeat_code;
63 
64 
65 static inline ir_code get_ir_code(const struct ir_ncode* ncode,
66  const struct ir_code_node* node)
67 {
68  if (ncode->next && node != NULL)
69  return node->code;
70  return ncode->code;
71 }
72 
73 static inline struct ir_code_node*
74 get_next_ir_code_node(const struct ir_ncode* ncode,
75  const struct ir_code_node* node)
76 {
77  if (node == NULL)
78  return ncode->next;
79  return node->next;
80 }
81 
82 static inline int bit_count(const struct ir_remote* remote)
83 {
84  return remote->pre_data_bits + remote->bits + remote->post_data_bits;
85 }
86 
87 static inline int bits_set(ir_code data)
88 {
89  int ret = 0;
90 
91  while (data) {
92  if (data & 1)
93  ret++;
94  data >>= 1;
95  }
96  return ret;
97 }
98 
99 static inline ir_code reverse(ir_code data, int bits)
100 {
101  int i;
102  ir_code c;
103 
104  c = 0;
105  for (i = 0; i < bits; i++)
106  c |= (ir_code)(((data & (((ir_code)1) << i)) ? 1 : 0))
107  << (bits - 1 - i);
108  return c;
109 }
110 
111 static inline int is_pulse(lirc_t data)
112 {
113  return ((data & LIRC_MODE2_MASK)==LIRC_MODE2_PULSE) ? 1 : 0;
114 }
115 
116 static inline int is_space(lirc_t data)
117 {
118  return ((data & LIRC_MODE2_MASK)==LIRC_MODE2_SPACE) ? 1 : 0;
119 }
120 
121 static inline int is_timeout(lirc_t data)
122 {
123  return ((data & LIRC_MODE2_MASK)==LIRC_MODE2_TIMEOUT) ? 1 : 0;
124 }
125 
126 static inline int is_overflow(lirc_t data)
127 {
128  return ((data & LIRC_MODE2_MASK)==LIRC_MODE2_OVERFLOW) ? 1 : 0;
129 }
130 
131 static inline int has_repeat(const struct ir_remote* remote)
132 {
133  if (remote->prepeat > 0 && remote->srepeat > 0)
134  return 1;
135  else
136  return 0;
137 }
138 
139 static inline void set_protocol(struct ir_remote* remote, int protocol)
140 {
141  remote->flags &= ~(IR_PROTOCOL_MASK);
142  remote->flags |= protocol;
143 }
144 
145 static inline int is_raw(const struct ir_remote* remote)
146 {
147  if ((remote->flags & IR_PROTOCOL_MASK) == RAW_CODES)
148  return 1;
149  else
150  return 0;
151 }
152 
153 static inline int is_space_enc(const struct ir_remote* remote)
154 {
155  if ((remote->flags & IR_PROTOCOL_MASK) == SPACE_ENC)
156  return 1;
157  else
158  return 0;
159 }
160 
161 static inline int is_space_first(const struct ir_remote* remote)
162 {
163  if ((remote->flags & IR_PROTOCOL_MASK) == SPACE_FIRST)
164  return 1;
165  else
166  return 0;
167 }
168 
169 static inline int is_rc5(const struct ir_remote* remote)
170 {
171  if ((remote->flags & IR_PROTOCOL_MASK) == RC5)
172  return 1;
173  else
174  return 0;
175 }
176 
177 static inline int is_rc6(const struct ir_remote* remote)
178 {
179  if ((remote->flags & IR_PROTOCOL_MASK) == RC6 || remote->rc6_mask)
180  return 1;
181  else
182  return 0;
183 }
184 
185 static inline int is_biphase(const struct ir_remote* remote)
186 {
187  if (is_rc5(remote) || is_rc6(remote))
188  return 1;
189  else
190  return 0;
191 }
192 
193 static inline int is_rcmm(const struct ir_remote* remote)
194 {
195  if ((remote->flags & IR_PROTOCOL_MASK) == RCMM)
196  return 1;
197  else
198  return 0;
199 }
200 
201 static inline int is_grundig(const struct ir_remote* remote)
202 {
203  if ((remote->flags & IR_PROTOCOL_MASK) == GRUNDIG)
204  return 1;
205  else
206  return 0;
207 }
208 
209 static inline int is_bo(const struct ir_remote* remote)
210 {
211  if ((remote->flags & IR_PROTOCOL_MASK) == BO)
212  return 1;
213  else
214  return 0;
215 }
216 
217 static inline int is_serial(const struct ir_remote* remote)
218 {
219  if ((remote->flags & IR_PROTOCOL_MASK) == SERIAL)
220  return 1;
221  else
222  return 0;
223 }
224 
225 static inline int is_xmp(const struct ir_remote* remote)
226 {
227  if ((remote->flags & IR_PROTOCOL_MASK) == XMP)
228  return 1;
229  else
230  return 0;
231 }
232 
233 static inline int is_const(const struct ir_remote* remote)
234 {
235  if (remote->flags & CONST_LENGTH)
236  return 1;
237  else
238  return 0;
239 }
240 
241 static inline int has_repeat_gap(const struct ir_remote* remote)
242 {
243  if (remote->repeat_gap > 0)
244  return 1;
245  else
246  return 0;
247 }
248 
249 static inline int has_pre(const struct ir_remote* remote)
250 {
251  if (remote->pre_data_bits > 0)
252  return 1;
253  else
254  return 0;
255 }
256 
257 static inline int has_post(const struct ir_remote* remote)
258 {
259  if (remote->post_data_bits > 0)
260  return 1;
261  else
262  return 0;
263 }
264 
265 static inline int has_header(const struct ir_remote* remote)
266 {
267  if (remote->phead > 0 && remote->shead > 0)
268  return 1;
269  else
270  return 0;
271 }
272 
273 static inline int has_foot(const struct ir_remote* remote)
274 {
275  if (remote->pfoot > 0 && remote->sfoot > 0)
276  return 1;
277  else
278  return 0;
279 }
280 
281 static inline int has_toggle_bit_mask(const struct ir_remote* remote)
282 {
283  if (remote->toggle_bit_mask > 0)
284  return 1;
285  else
286  return 0;
287 }
288 
289 static inline int has_ignore_mask(const struct ir_remote* remote)
290 {
291  if (remote->ignore_mask > 0)
292  return 1;
293  else
294  return 0;
295 }
296 
297 static inline int has_repeat_mask(struct ir_remote* remote)
298 {
299  if (remote->repeat_mask > 0)
300  return 1;
301  else
302  return 0;
303 }
304 
305 static inline int has_toggle_mask(const struct ir_remote* remote)
306 {
307  if (remote->toggle_mask > 0)
308  return 1;
309  else
310  return 0;
311 }
312 
313 static inline lirc_t min_gap(const struct ir_remote* remote)
314 {
315  if (remote->gap2 != 0 && remote->gap2 < remote->gap)
316  return remote->gap2;
317  else
318  return remote->gap;
319 }
320 
321 static inline lirc_t max_gap(const struct ir_remote* remote)
322 {
323  if (remote->gap2 > remote->gap)
324  return remote->gap2;
325  else
326  return remote->gap;
327 }
328 
329 static inline unsigned int get_duty_cycle(const struct ir_remote* remote)
330 {
331  if (remote->duty_cycle == 0)
332  return 50;
333  else if (remote->duty_cycle < 0)
334  return 1;
335  else if (remote->duty_cycle > 100)
336  return 100;
337  else
338  return remote->duty_cycle;
339 }
340 
341 /* check if delta is inside exdelta +/- exdelta*eps/100 */
342 
343 static inline int expect(const struct ir_remote* remote,
344  lirc_t delta,
345  lirc_t exdelta)
346 {
347  int aeps = curr_driver->resolution > remote->aeps ?
348  curr_driver->resolution : remote->aeps;
349 
350  if (abs(exdelta - delta) <= exdelta * remote->eps / 100
351  || abs(exdelta - delta) <= aeps)
352  return 1;
353  return 0;
354 }
355 
356 static inline int expect_at_least(const struct ir_remote* remote,
357  lirc_t delta,
358  lirc_t exdelta)
359 {
360  int aeps = curr_driver->resolution > remote->aeps ?
361  curr_driver->resolution : remote->aeps;
362 
363  if (delta + exdelta * remote->eps / 100 >= exdelta
364  || delta + aeps >= exdelta)
365  return 1;
366  return 0;
367 }
368 
369 static inline int expect_at_most(const struct ir_remote* remote,
370  lirc_t delta,
371  lirc_t exdelta)
372 {
373  int aeps = curr_driver->resolution > remote->aeps ?
374  curr_driver->resolution : remote->aeps;
375 
376  if (delta <= exdelta + exdelta * remote->eps / 100
377  || delta <= exdelta + aeps)
378  return 1;
379  return 0;
380 }
381 
382 static inline lirc_t upper_limit(const struct ir_remote* remote, lirc_t val)
383 {
384  int aeps = curr_driver->resolution > remote->aeps ?
385  curr_driver->resolution : remote->aeps;
386  lirc_t eps_val = val * (100 + remote->eps) / 100;
387  lirc_t aeps_val = val + aeps;
388 
389  return eps_val > aeps_val ? eps_val : aeps_val;
390 }
391 
392 static inline lirc_t lower_limit(const struct ir_remote* remote, lirc_t val)
393 {
394  int aeps = curr_driver->resolution > remote->aeps ?
395  curr_driver->resolution : remote->aeps;
396  lirc_t eps_val = val * (100 - remote->eps) / 100;
397  lirc_t aeps_val = val - aeps;
398 
399  if (eps_val <= 0)
400  eps_val = 1;
401  if (aeps_val <= 0)
402  aeps_val = 1;
403 
404  return eps_val < aeps_val ? eps_val : aeps_val;
405 }
406 
407 /* only works if last <= current */
408 static inline unsigned long time_elapsed(const struct timeval* last,
409  const struct timeval* current)
410 {
411  unsigned long secs, diff;
412 
413  secs = current->tv_sec - last->tv_sec;
414 
415  diff = 1000000 * secs + current->tv_usec - last->tv_usec;
416 
417  return diff;
418 }
419 
420 static inline ir_code gen_mask(int bits)
421 {
422  int i;
423  ir_code mask;
424 
425  mask = 0;
426  for (i = 0; i < bits; i++) {
427  mask <<= 1;
428  mask |= 1;
429  }
430  return mask;
431 }
432 
433 static inline ir_code gen_ir_code(const struct ir_remote* remote,
434  ir_code pre,
435  ir_code code,
436  ir_code post)
437 {
438  ir_code all;
439 
440  all = (pre & gen_mask(remote->pre_data_bits));
441  all <<= remote->bits;
442  all |= is_raw(remote) ? code : (code & gen_mask(remote->bits));
443  all <<= remote->post_data_bits;
444  all |= post & gen_mask(remote->post_data_bits);
445 
446  return all;
447 }
448 
456 const struct ir_remote* is_in_remotes(const struct ir_remote* remotes,
457  const struct ir_remote* remote);
458 
460 struct ir_remote* get_ir_remote(const struct ir_remote* remotes,
461  const char* name);
462 
463 void get_frequency_range(const struct ir_remote* remotes,
464  unsigned int* min_freq,
465  unsigned int* max_freq);
466 
467 void get_filter_parameters(const struct ir_remote* remotes,
468  lirc_t* max_gap_lengthp,
469  lirc_t* min_pulse_lengthp,
470  lirc_t* min_space_lengthp,
471  lirc_t* max_pulse_lengthp,
472  lirc_t* max_space_lengthp);
473 
474 int map_code(const struct ir_remote* remote,
475  struct decode_ctx_t* ctx,
476  int pre_bits,
477  ir_code pre,
478  int bits,
479  ir_code code,
480  int post_bits,
481  ir_code post);
482 
483 void map_gap(const struct ir_remote* remote,
484  struct decode_ctx_t* ctx,
485  const struct timeval* start,
486  const struct timeval* last,
487  lirc_t signal_length);
488 
490 struct ir_ncode* get_code_by_name(const struct ir_remote* remote,
491  const char* name);
492 
493 int write_message(char* buffer,
494  size_t size,
495  const char* remote_name,
496  const char* button_name,
497  const char* button_suffix,
498  ir_code code,
499  int reps);
500 
511 char* decode_all(struct ir_remote* remotes);
512 
525 int send_ir_ncode(struct ir_remote* remote, struct ir_ncode* code, int delay);
526 
527 #ifdef __cplusplus
528 }
529 #endif
530 
537 void ir_remote_init(int use_dyncodes);
538 
540 const struct ir_remote* get_decoding(void);
541 
544 #endif
const struct driver *const curr_driver
Read-only access to drv for client code.
Definition: driver.c:34
Interface to the userspace drivers.
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)
Formats the arguments into a readable string.
Definition: ir_remote.c:709
void get_frequency_range(const struct ir_remote *remotes, unsigned int *min_freq, unsigned int *max_freq)
Definition: ir_remote.c:152
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)
Definition: ir_remote.c:189
struct ir_remote * get_ir_remote(const struct ir_remote *remotes, const char *name)
Return ir_remote with given name in remotes list, or NULL if not found.
Definition: ir_remote.c:247
struct ir_ncode * repeat_code
Global pointer to the code currently repeating.
Definition: ir_remote.c:59
void ncode_free(struct ir_ncode *ncode)
Dispose an ir_ncode instance obtained from ncode_dup().
Definition: ir_remote.c:100
struct ir_remote * last_remote
TODO.
Definition: ir_remote.c:55
int send_ir_ncode(struct ir_remote *remote, struct ir_ncode *code, int delay)
Transmits the actual code in the second argument by calling the current hardware driver.
Definition: ir_remote.c:819
struct ir_remote * repeat_remote
Global pointer to the remote that contains the code currently repeating.
Definition: ir_remote.c:57
void ir_remote_init(int use_dyncodes)
Initiate: define if dynamic codes should be used.
Definition: ir_remote.c:120
const struct ir_remote * is_in_remotes(const struct ir_remote *remotes, const struct ir_remote *remote)
Test if a given remote is in a list of remotes.
Definition: ir_remote.c:235
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)
Definition: ir_remote.c:279
struct ir_ncode * ncode_dup(struct ir_ncode *ncode)
Create a malloc'd, deep copy of ncode.
Definition: ir_remote.c:65
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)
Definition: ir_remote.c:325
char * decode_all(struct ir_remote *remotes)
Tries to decode current signal trying all known remotes.
Definition: ir_remote.c:729
const struct ir_remote * get_decoding(void)
Return pointer to currently decoded remote.
Definition: ir_remote.c:850
struct ir_ncode * get_code_by_name(const struct ir_remote *remote, const char *name)
Return code with given name in remote's list of codes or NULL.
Definition: ir_remote.c:393
Describes and decodes the signals from IR remotes.
#define RAW_CODES
for internal use only
#define XMP
XMP protocol.
#define RC6
IR data follows RC6 protocol.
#define GRUNDIG
encoding found on Grundig remote
#define BO
encoding found on Bang & Olufsen remote
uint64_t ir_code
Denotes an internal coded representation for an IR transmission.
#define SPACE_FIRST
bits are encoded as space+pulse
#define SPACE_ENC
IR data is space encoded.
#define RC5
IR data follows RC5 protocol.
#define SERIAL
serial protocol
#define CONST_LENGTH
signal length+gap is always constant
#define RCMM
IR data follows RC-MM protocol.
unsigned int eps
Shared list of remotes.
Definition: irrecord.c:62
lirc_t aeps
Error tolerance in per cent.
Definition: irrecord.c:63
State describing code, pre, post + gap and repeat state.
unsigned int resolution
The resolution in microseconds of the recorded durations when reading signals.
Definition: driver.h:230
An ir_code for entering into (singly) linked lists, i.e.
IR Command, corresponding to one (command defining) line of the configuration file.
struct ir_code_node * next
Linked list of the subsequent ir_code's, after the first one.
ir_code code
The first code of the command.
char * name
Name of command.
One remote as represented in the configuration file.
uint32_t repeat_gap
time between two repeat codes if different from gap
unsigned int aeps
detecting very short pulses is difficult with relative tolerance for some remotes,...
uint32_t gap2
time between signals in usecs
lirc_t sfoot
foot
ir_code rc6_mask
RC-6 doubles signal length of some bits.
unsigned int duty_cycle
0<duty cycle<=100 default: 50
ir_code repeat_mask
mask defines which bits are inverted for repeats
lirc_t srepeat
indicate repeating
int bits
bits (length of code)
int post_data_bits
length of post_data
ir_code ignore_mask
mask defines which bits can be ignored when matching a code
lirc_t shead
header
ir_code toggle_mask
Sharp (?) error detection scheme.
int flags
flags
uint32_t gap
time between signals in usecs
int eps
eps (relative tolerance)
const char * name
name of remote control
ir_code toggle_bit_mask
previously only one bit called toggle_bit
int pre_data_bits
length of pre_data