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 & PULSE_BIT ? 1 : 0;
114 }
115 
116 static inline int is_space(lirc_t data)
117 {
118  return !is_pulse(data);
119 }
120 
121 static inline int has_repeat(const struct ir_remote* remote)
122 {
123  if (remote->prepeat > 0 && remote->srepeat > 0)
124  return 1;
125  else
126  return 0;
127 }
128 
129 static inline void set_protocol(struct ir_remote* remote, int protocol)
130 {
131  remote->flags &= ~(IR_PROTOCOL_MASK);
132  remote->flags |= protocol;
133 }
134 
135 static inline int is_raw(const struct ir_remote* remote)
136 {
137  if ((remote->flags & IR_PROTOCOL_MASK) == RAW_CODES)
138  return 1;
139  else
140  return 0;
141 }
142 
143 static inline int is_space_enc(const struct ir_remote* remote)
144 {
145  if ((remote->flags & IR_PROTOCOL_MASK) == SPACE_ENC)
146  return 1;
147  else
148  return 0;
149 }
150 
151 static inline int is_space_first(const struct ir_remote* remote)
152 {
153  if ((remote->flags & IR_PROTOCOL_MASK) == SPACE_FIRST)
154  return 1;
155  else
156  return 0;
157 }
158 
159 static inline int is_rc5(const struct ir_remote* remote)
160 {
161  if ((remote->flags & IR_PROTOCOL_MASK) == RC5)
162  return 1;
163  else
164  return 0;
165 }
166 
167 static inline int is_rc6(const struct ir_remote* remote)
168 {
169  if ((remote->flags & IR_PROTOCOL_MASK) == RC6 || remote->rc6_mask)
170  return 1;
171  else
172  return 0;
173 }
174 
175 static inline int is_biphase(const struct ir_remote* remote)
176 {
177  if (is_rc5(remote) || is_rc6(remote))
178  return 1;
179  else
180  return 0;
181 }
182 
183 static inline int is_rcmm(const struct ir_remote* remote)
184 {
185  if ((remote->flags & IR_PROTOCOL_MASK) == RCMM)
186  return 1;
187  else
188  return 0;
189 }
190 
191 static inline int is_grundig(const struct ir_remote* remote)
192 {
193  if ((remote->flags & IR_PROTOCOL_MASK) == GRUNDIG)
194  return 1;
195  else
196  return 0;
197 }
198 
199 static inline int is_bo(const struct ir_remote* remote)
200 {
201  if ((remote->flags & IR_PROTOCOL_MASK) == BO)
202  return 1;
203  else
204  return 0;
205 }
206 
207 static inline int is_serial(const struct ir_remote* remote)
208 {
209  if ((remote->flags & IR_PROTOCOL_MASK) == SERIAL)
210  return 1;
211  else
212  return 0;
213 }
214 
215 static inline int is_xmp(const struct ir_remote* remote)
216 {
217  if ((remote->flags & IR_PROTOCOL_MASK) == XMP)
218  return 1;
219  else
220  return 0;
221 }
222 
223 static inline int is_const(const struct ir_remote* remote)
224 {
225  if (remote->flags & CONST_LENGTH)
226  return 1;
227  else
228  return 0;
229 }
230 
231 static inline int has_repeat_gap(const struct ir_remote* remote)
232 {
233  if (remote->repeat_gap > 0)
234  return 1;
235  else
236  return 0;
237 }
238 
239 static inline int has_pre(const struct ir_remote* remote)
240 {
241  if (remote->pre_data_bits > 0)
242  return 1;
243  else
244  return 0;
245 }
246 
247 static inline int has_post(const struct ir_remote* remote)
248 {
249  if (remote->post_data_bits > 0)
250  return 1;
251  else
252  return 0;
253 }
254 
255 static inline int has_header(const struct ir_remote* remote)
256 {
257  if (remote->phead > 0 && remote->shead > 0)
258  return 1;
259  else
260  return 0;
261 }
262 
263 static inline int has_foot(const struct ir_remote* remote)
264 {
265  if (remote->pfoot > 0 && remote->sfoot > 0)
266  return 1;
267  else
268  return 0;
269 }
270 
271 static inline int has_toggle_bit_mask(const struct ir_remote* remote)
272 {
273  if (remote->toggle_bit_mask > 0)
274  return 1;
275  else
276  return 0;
277 }
278 
279 static inline int has_ignore_mask(const struct ir_remote* remote)
280 {
281  if (remote->ignore_mask > 0)
282  return 1;
283  else
284  return 0;
285 }
286 
287 static inline int has_repeat_mask(struct ir_remote* remote)
288 {
289  if (remote->repeat_mask > 0)
290  return 1;
291  else
292  return 0;
293 }
294 
295 static inline int has_toggle_mask(const struct ir_remote* remote)
296 {
297  if (remote->toggle_mask > 0)
298  return 1;
299  else
300  return 0;
301 }
302 
303 static inline lirc_t min_gap(const struct ir_remote* remote)
304 {
305  if (remote->gap2 != 0 && remote->gap2 < remote->gap)
306  return remote->gap2;
307  else
308  return remote->gap;
309 }
310 
311 static inline lirc_t max_gap(const struct ir_remote* remote)
312 {
313  if (remote->gap2 > remote->gap)
314  return remote->gap2;
315  else
316  return remote->gap;
317 }
318 
319 static inline unsigned int get_duty_cycle(const struct ir_remote* remote)
320 {
321  if (remote->duty_cycle == 0)
322  return 50;
323  else if (remote->duty_cycle < 0)
324  return 1;
325  else if (remote->duty_cycle > 100)
326  return 100;
327  else
328  return remote->duty_cycle;
329 }
330 
331 /* check if delta is inside exdelta +/- exdelta*eps/100 */
332 
333 static inline int expect(const struct ir_remote* remote,
334  lirc_t delta,
335  lirc_t exdelta)
336 {
337  int aeps = curr_driver->resolution > remote->aeps ?
338  curr_driver->resolution : remote->aeps;
339 
340  if (abs(exdelta - delta) <= exdelta * remote->eps / 100
341  || abs(exdelta - delta) <= aeps)
342  return 1;
343  return 0;
344 }
345 
346 static inline int expect_at_least(const struct ir_remote* remote,
347  lirc_t delta,
348  lirc_t exdelta)
349 {
350  int aeps = curr_driver->resolution > remote->aeps ?
351  curr_driver->resolution : remote->aeps;
352 
353  if (delta + exdelta * remote->eps / 100 >= exdelta
354  || delta + aeps >= exdelta)
355  return 1;
356  return 0;
357 }
358 
359 static inline int expect_at_most(const struct ir_remote* remote,
360  lirc_t delta,
361  lirc_t exdelta)
362 {
363  int aeps = curr_driver->resolution > remote->aeps ?
364  curr_driver->resolution : remote->aeps;
365 
366  if (delta <= exdelta + exdelta * remote->eps / 100
367  || delta <= exdelta + aeps)
368  return 1;
369  return 0;
370 }
371 
372 static inline lirc_t upper_limit(const struct ir_remote* remote, lirc_t val)
373 {
374  int aeps = curr_driver->resolution > remote->aeps ?
375  curr_driver->resolution : remote->aeps;
376  lirc_t eps_val = val * (100 + remote->eps) / 100;
377  lirc_t aeps_val = val + aeps;
378 
379  return eps_val > aeps_val ? eps_val : aeps_val;
380 }
381 
382 static inline lirc_t lower_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  if (eps_val <= 0)
390  eps_val = 1;
391  if (aeps_val <= 0)
392  aeps_val = 1;
393 
394  return eps_val < aeps_val ? eps_val : aeps_val;
395 }
396 
397 /* only works if last <= current */
398 static inline unsigned long time_elapsed(const struct timeval* last,
399  const struct timeval* current)
400 {
401  unsigned long secs, diff;
402 
403  secs = current->tv_sec - last->tv_sec;
404 
405  diff = 1000000 * secs + current->tv_usec - last->tv_usec;
406 
407  return diff;
408 }
409 
410 static inline ir_code gen_mask(int bits)
411 {
412  int i;
413  ir_code mask;
414 
415  mask = 0;
416  for (i = 0; i < bits; i++) {
417  mask <<= 1;
418  mask |= 1;
419  }
420  return mask;
421 }
422 
423 static inline ir_code gen_ir_code(const struct ir_remote* remote,
424  ir_code pre,
425  ir_code code,
426  ir_code post)
427 {
428  ir_code all;
429 
430  all = (pre & gen_mask(remote->pre_data_bits));
431  all <<= remote->bits;
432  all |= is_raw(remote) ? code : (code & gen_mask(remote->bits));
433  all <<= remote->post_data_bits;
434  all |= post & gen_mask(remote->post_data_bits);
435 
436  return all;
437 }
438 
446 const struct ir_remote* is_in_remotes(const struct ir_remote* remotes,
447  const struct ir_remote* remote);
448 
450 struct ir_remote* get_ir_remote(const struct ir_remote* remotes,
451  const char* name);
452 
453 void get_frequency_range(const struct ir_remote* remotes,
454  unsigned int* min_freq,
455  unsigned int* max_freq);
456 
457 void get_filter_parameters(const struct ir_remote* remotes,
458  lirc_t* max_gap_lengthp,
459  lirc_t* min_pulse_lengthp,
460  lirc_t* min_space_lengthp,
461  lirc_t* max_pulse_lengthp,
462  lirc_t* max_space_lengthp);
463 
464 int map_code(const struct ir_remote* remote,
465  struct decode_ctx_t* ctx,
466  int pre_bits,
467  ir_code pre,
468  int bits,
469  ir_code code,
470  int post_bits,
471  ir_code post);
472 
473 void map_gap(const struct ir_remote* remote,
474  struct decode_ctx_t* ctx,
475  const struct timeval* start,
476  const struct timeval* last,
477  lirc_t signal_length);
478 
480 struct ir_ncode* get_code_by_name(const struct ir_remote* remote,
481  const char* name);
482 
483 int write_message(char* buffer,
484  size_t size,
485  const char* remote_name,
486  const char* button_name,
487  const char* button_suffix,
488  ir_code code,
489  int reps);
490 
501 char* decode_all(struct ir_remote* remotes);
502 
515 int send_ir_ncode(struct ir_remote* remote, struct ir_ncode* code, int delay);
516 
517 #ifdef __cplusplus
518 }
519 #endif
520 
527 void ir_remote_init(int use_dyncodes);
528 
530 const struct ir_remote* get_decoding(void);
531 
534 #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:713
void get_frequency_range(const struct ir_remote *remotes, unsigned int *min_freq, unsigned int *max_freq)
Definition: ir_remote.c:156
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:193
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:251
struct ir_ncode * repeat_code
Global pointer to the code currently repeating.
Definition: ir_remote.c:63
void ncode_free(struct ir_ncode *ncode)
Dispose an ir_ncode instance obtained from ncode_dup().
Definition: ir_remote.c:104
struct ir_remote * last_remote
TODO.
Definition: ir_remote.c:59
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:823
struct ir_remote * repeat_remote
Global pointer to the remote that contains the code currently repeating.
Definition: ir_remote.c:61
void ir_remote_init(int use_dyncodes)
Initiate: define if dynamic codes should be used.
Definition: ir_remote.c:124
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:239
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:283
struct ir_ncode * ncode_dup(struct ir_ncode *ncode)
Create a malloc'd, deep copy of ncode.
Definition: ir_remote.c:69
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:329
char * decode_all(struct ir_remote *remotes)
Tries to decode current signal trying all known remotes.
Definition: ir_remote.c:733
const struct ir_remote * get_decoding(void)
Return pointer to currently decoded remote.
Definition: ir_remote.c:854
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:397
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:234
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