LIRC libraries
LinuxInfraredRemoteControl
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 has_repeat(const struct ir_remote* remote)
127 {
128  if (remote->prepeat > 0 && remote->srepeat > 0)
129  return 1;
130  else
131  return 0;
132 }
133 
134 static inline void set_protocol(struct ir_remote* remote, int protocol)
135 {
136  remote->flags &= ~(IR_PROTOCOL_MASK);
137  remote->flags |= protocol;
138 }
139 
140 static inline int is_raw(const struct ir_remote* remote)
141 {
142  if ((remote->flags & IR_PROTOCOL_MASK) == RAW_CODES)
143  return 1;
144  else
145  return 0;
146 }
147 
148 static inline int is_space_enc(const struct ir_remote* remote)
149 {
150  if ((remote->flags & IR_PROTOCOL_MASK) == SPACE_ENC)
151  return 1;
152  else
153  return 0;
154 }
155 
156 static inline int is_space_first(const struct ir_remote* remote)
157 {
158  if ((remote->flags & IR_PROTOCOL_MASK) == SPACE_FIRST)
159  return 1;
160  else
161  return 0;
162 }
163 
164 static inline int is_rc5(const struct ir_remote* remote)
165 {
166  if ((remote->flags & IR_PROTOCOL_MASK) == RC5)
167  return 1;
168  else
169  return 0;
170 }
171 
172 static inline int is_rc6(const struct ir_remote* remote)
173 {
174  if ((remote->flags & IR_PROTOCOL_MASK) == RC6 || remote->rc6_mask)
175  return 1;
176  else
177  return 0;
178 }
179 
180 static inline int is_biphase(const struct ir_remote* remote)
181 {
182  if (is_rc5(remote) || is_rc6(remote))
183  return 1;
184  else
185  return 0;
186 }
187 
188 static inline int is_rcmm(const struct ir_remote* remote)
189 {
190  if ((remote->flags & IR_PROTOCOL_MASK) == RCMM)
191  return 1;
192  else
193  return 0;
194 }
195 
196 static inline int is_goldstar(const struct ir_remote* remote)
197 {
198  if ((remote->flags & IR_PROTOCOL_MASK) == GOLDSTAR)
199  return 1;
200  else
201  return 0;
202 }
203 
204 static inline int is_grundig(const struct ir_remote* remote)
205 {
206  if ((remote->flags & IR_PROTOCOL_MASK) == GRUNDIG)
207  return 1;
208  else
209  return 0;
210 }
211 
212 static inline int is_bo(const struct ir_remote* remote)
213 {
214  if ((remote->flags & IR_PROTOCOL_MASK) == BO)
215  return 1;
216  else
217  return 0;
218 }
219 
220 static inline int is_serial(const struct ir_remote* remote)
221 {
222  if ((remote->flags & IR_PROTOCOL_MASK) == SERIAL)
223  return 1;
224  else
225  return 0;
226 }
227 
228 static inline int is_xmp(const struct ir_remote* remote)
229 {
230  if ((remote->flags & IR_PROTOCOL_MASK) == XMP)
231  return 1;
232  else
233  return 0;
234 }
235 
236 static inline int is_const(const struct ir_remote* remote)
237 {
238  if (remote->flags & CONST_LENGTH)
239  return 1;
240  else
241  return 0;
242 }
243 
244 static inline int has_repeat_gap(const struct ir_remote* remote)
245 {
246  if (remote->repeat_gap > 0)
247  return 1;
248  else
249  return 0;
250 }
251 
252 static inline int has_pre(const struct ir_remote* remote)
253 {
254  if (remote->pre_data_bits > 0)
255  return 1;
256  else
257  return 0;
258 }
259 
260 static inline int has_post(const struct ir_remote* remote)
261 {
262  if (remote->post_data_bits > 0)
263  return 1;
264  else
265  return 0;
266 }
267 
268 static inline int has_header(const struct ir_remote* remote)
269 {
270  if (remote->phead > 0 && remote->shead > 0)
271  return 1;
272  else
273  return 0;
274 }
275 
276 static inline int has_foot(const struct ir_remote* remote)
277 {
278  if (remote->pfoot > 0 && remote->sfoot > 0)
279  return 1;
280  else
281  return 0;
282 }
283 
284 static inline int has_toggle_bit_mask(const struct ir_remote* remote)
285 {
286  if (remote->toggle_bit_mask > 0)
287  return 1;
288  else
289  return 0;
290 }
291 
292 static inline int has_ignore_mask(const struct ir_remote* remote)
293 {
294  if (remote->ignore_mask > 0)
295  return 1;
296  else
297  return 0;
298 }
299 
300 static inline int has_repeat_mask(struct ir_remote* remote)
301 {
302  if (remote->repeat_mask > 0)
303  return 1;
304  else
305  return 0;
306 }
307 
308 static inline int has_toggle_mask(const struct ir_remote* remote)
309 {
310  if (remote->toggle_mask > 0)
311  return 1;
312  else
313  return 0;
314 }
315 
316 static inline lirc_t min_gap(const struct ir_remote* remote)
317 {
318  if (remote->gap2 != 0 && remote->gap2 < remote->gap)
319  return remote->gap2;
320  else
321  return remote->gap;
322 }
323 
324 static inline lirc_t max_gap(const struct ir_remote* remote)
325 {
326  if (remote->gap2 > remote->gap)
327  return remote->gap2;
328  else
329  return remote->gap;
330 }
331 
332 static inline unsigned int get_duty_cycle(const struct ir_remote* remote)
333 {
334  if (remote->duty_cycle == 0)
335  return 50;
336  else if (remote->duty_cycle < 0)
337  return 1;
338  else if (remote->duty_cycle > 100)
339  return 100;
340  else
341  return remote->duty_cycle;
342 }
343 
344 /* check if delta is inside exdelta +/- exdelta*eps/100 */
345 
346 static inline int expect(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 (abs(exdelta - delta) <= exdelta * remote->eps / 100
354  || abs(exdelta - delta) <= aeps)
355  return 1;
356  return 0;
357 }
358 
359 static inline int expect_at_least(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 * remote->eps / 100 >= exdelta
367  || delta + aeps >= exdelta)
368  return 1;
369  return 0;
370 }
371 
372 static inline int expect_at_most(const struct ir_remote* remote,
373  lirc_t delta,
374  lirc_t exdelta)
375 {
376  int aeps = curr_driver->resolution > remote->aeps ?
377  curr_driver->resolution : remote->aeps;
378 
379  if (delta <= exdelta + exdelta * remote->eps / 100
380  || delta <= exdelta + aeps)
381  return 1;
382  return 0;
383 }
384 
385 static inline lirc_t upper_limit(const struct ir_remote* remote, lirc_t val)
386 {
387  int aeps = curr_driver->resolution > remote->aeps ?
388  curr_driver->resolution : remote->aeps;
389  lirc_t eps_val = val * (100 + remote->eps) / 100;
390  lirc_t aeps_val = val + aeps;
391 
392  return eps_val > aeps_val ? eps_val : aeps_val;
393 }
394 
395 static inline lirc_t lower_limit(const struct ir_remote* remote, lirc_t val)
396 {
397  int aeps = curr_driver->resolution > remote->aeps ?
398  curr_driver->resolution : remote->aeps;
399  lirc_t eps_val = val * (100 - remote->eps) / 100;
400  lirc_t aeps_val = val - aeps;
401 
402  if (eps_val <= 0)
403  eps_val = 1;
404  if (aeps_val <= 0)
405  aeps_val = 1;
406 
407  return eps_val < aeps_val ? eps_val : aeps_val;
408 }
409 
410 /* only works if last <= current */
411 static inline unsigned long time_elapsed(const struct timeval* last,
412  const struct timeval* current)
413 {
414  unsigned long secs, diff;
415 
416  secs = current->tv_sec - last->tv_sec;
417 
418  diff = 1000000 * secs + current->tv_usec - last->tv_usec;
419 
420  return diff;
421 }
422 
423 static inline ir_code gen_mask(int bits)
424 {
425  int i;
426  ir_code mask;
427 
428  mask = 0;
429  for (i = 0; i < bits; i++) {
430  mask <<= 1;
431  mask |= 1;
432  }
433  return mask;
434 }
435 
436 static inline ir_code gen_ir_code(const struct ir_remote* remote,
437  ir_code pre,
438  ir_code code,
439  ir_code post)
440 {
441  ir_code all;
442 
443  all = (pre & gen_mask(remote->pre_data_bits));
444  all <<= remote->bits;
445  all |= is_raw(remote) ? code : (code & gen_mask(remote->bits));
446  all <<= remote->post_data_bits;
447  all |= post & gen_mask(remote->post_data_bits);
448 
449  return all;
450 }
451 
459 const struct ir_remote* is_in_remotes(const struct ir_remote* remotes,
460  const struct ir_remote* remote);
461 
463 struct ir_remote* get_ir_remote(const struct ir_remote* remotes,
464  const char* name);
465 
466 void get_frequency_range(const struct ir_remote* remotes,
467  unsigned int* min_freq,
468  unsigned int* max_freq);
469 
470 void get_filter_parameters(const struct ir_remote* remotes,
471  lirc_t* max_gap_lengthp,
472  lirc_t* min_pulse_lengthp,
473  lirc_t* min_space_lengthp,
474  lirc_t* max_pulse_lengthp,
475  lirc_t* max_space_lengthp);
476 
477 int map_code(const struct ir_remote* remote,
478  struct decode_ctx_t* ctx,
479  int pre_bits,
480  ir_code pre,
481  int bits,
482  ir_code code,
483  int post_bits,
484  ir_code post);
485 
486 void map_gap(const struct ir_remote* remote,
487  struct decode_ctx_t* ctx,
488  const struct timeval* start,
489  const struct timeval* last,
490  lirc_t signal_length);
491 
493 struct ir_ncode* get_code_by_name(const struct ir_remote* remote,
494  const char* name);
495 
496 int write_message(char* buffer,
497  size_t size,
498  const char* remote_name,
499  const char* button_name,
500  const char* button_suffix,
501  ir_code code,
502  int reps);
503 
514 char* decode_all(struct ir_remote* remotes);
515 
528 int send_ir_ncode(struct ir_remote* remote, struct ir_ncode* code, int delay);
529 
530 #ifdef __cplusplus
531 }
532 #endif
533 
540 void ir_remote_init(int use_dyncodes);
541 
543 const struct ir_remote* get_decoding(void);
544 
547 #endif
struct ir_remote * last_remote
TODO.
Definition: ir_remote.c:59
struct ir_ncode * repeat_code
Global pointer to the code currently repeating.
Definition: ir_remote.c:63
One remote as represented in the configuration file.
int bits
bits (length of code)
#define RC6
IR data follows RC6 protocol.
An ir_code for entering into (singly) linked lists, i.e.
#define GOLDSTAR
encoding found on Goldstar remote
void ir_remote_init(int use_dyncodes)
Initiate: define if dynamic codes should be used.
Definition: ir_remote.c:124
const struct driver *const curr_driver
Read-only access to drv for client code.
Definition: driver.c:34
ir_code repeat_mask
mask defines which bits are inverted for repeats
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_code_node * next
Linked list of the subsequent ir_code&#39;s, after the first one.
const char * name
name of remote control
#define SPACE_ENC
IR data is space encoded.
struct ir_ncode * get_code_by_name(const struct ir_remote *remote, const char *name)
Return code with given name in remote&#39;s list of codes or NULL.
Definition: ir_remote.c:397
unsigned int resolution
The resolution in microseconds of the recorded durations when reading signals.
Definition: driver.h:234
Interface to the userspace drivers.
int eps
eps (relative tolerance)
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
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
lirc_t sfoot
foot
char * decode_all(struct ir_remote *remotes)
Tries to decode current signal trying all known remotes.
Definition: ir_remote.c:733
int pre_data_bits
length of pre_data
char * name
Name of command.
unsigned int duty_cycle
0<duty cycle<=100 default: 50
int post_data_bits
length of post_data
#define RCMM
IR data follows RC-MM protocol.
ir_code toggle_mask
Sharp (?) error detection scheme.
Describes and decodes the signals from IR remotes.
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
lirc_t aeps
Error tolerance in per cent.
Definition: irrecord.c:63
uint32_t gap
time between signals in usecs
#define RC5
IR data follows RC5 protocol.
uint32_t repeat_gap
time between two repeat codes if different from gap
lirc_t shead
header
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
#define SPACE_FIRST
bits are encoded as space+pulse
const struct ir_remote * get_decoding(void)
Return pointer to currently decoded remote.
Definition: ir_remote.c:854
unsigned int eps
Shared list of remotes.
Definition: irrecord.c:62
#define CONST_LENGTH
signal length+gap is always constant
uint32_t gap2
time between signals in usecs
#define GRUNDIG
encoding found on Grundig remote
void ncode_free(struct ir_ncode *ncode)
Dispose an ir_ncode instance obtained from ncode_dup().
Definition: ir_remote.c:104
IR Command, corresponding to one (command defining) line of the configuration file.
int flags
flags
unsigned int aeps
detecting very short pulses is difficult with relative tolerance for some remotes, this is an absolute tolerance to solve this problem usually you can say 0 here.
lirc_t srepeat
indicate repeating
State describing code, pre, post + gap and repeat state.
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
#define SERIAL
serial protocol
struct ir_ncode * ncode_dup(struct ir_ncode *ncode)
Create a malloc&#39;d, deep copy of ncode.
Definition: ir_remote.c:69
void get_frequency_range(const struct ir_remote *remotes, unsigned int *min_freq, unsigned int *max_freq)
Definition: ir_remote.c:156
#define BO
encoding found on Bang & Olufsen remote
ir_code code
The first code of the command.
ir_code rc6_mask
RC-6 doubles signal length of some bits.
struct ir_remote * repeat_remote
Global pointer to the remote that contains the code currently repeating.
Definition: ir_remote.c:61
ir_code toggle_bit_mask
previously only one bit called toggle_bit
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
#define RAW_CODES
for internal use only
ir_code ignore_mask
mask defines which bits can be ignored when matching a code
uint64_t ir_code
Denotes an internal coded representation for an IR transmission.
#define XMP
XMP protocol.