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 & 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_goldstar(const struct ir_remote* remote)
192 {
193  if ((remote->flags & IR_PROTOCOL_MASK) == GOLDSTAR)
194  return 1;
195  else
196  return 0;
197 }
198 
199 static inline int is_grundig(const struct ir_remote* remote)
200 {
201  if ((remote->flags & IR_PROTOCOL_MASK) == GRUNDIG)
202  return 1;
203  else
204  return 0;
205 }
206 
207 static inline int is_bo(const struct ir_remote* remote)
208 {
209  if ((remote->flags & IR_PROTOCOL_MASK) == BO)
210  return 1;
211  else
212  return 0;
213 }
214 
215 static inline int is_serial(const struct ir_remote* remote)
216 {
217  if ((remote->flags & IR_PROTOCOL_MASK) == SERIAL)
218  return 1;
219  else
220  return 0;
221 }
222 
223 static inline int is_xmp(const struct ir_remote* remote)
224 {
225  if ((remote->flags & IR_PROTOCOL_MASK) == XMP)
226  return 1;
227  else
228  return 0;
229 }
230 
231 static inline int is_const(const struct ir_remote* remote)
232 {
233  if (remote->flags & CONST_LENGTH)
234  return 1;
235  else
236  return 0;
237 }
238 
239 static inline int has_repeat_gap(const struct ir_remote* remote)
240 {
241  if (remote->repeat_gap > 0)
242  return 1;
243  else
244  return 0;
245 }
246 
247 static inline int has_pre(const struct ir_remote* remote)
248 {
249  if (remote->pre_data_bits > 0)
250  return 1;
251  else
252  return 0;
253 }
254 
255 static inline int has_post(const struct ir_remote* remote)
256 {
257  if (remote->post_data_bits > 0)
258  return 1;
259  else
260  return 0;
261 }
262 
263 static inline int has_header(const struct ir_remote* remote)
264 {
265  if (remote->phead > 0 && remote->shead > 0)
266  return 1;
267  else
268  return 0;
269 }
270 
271 static inline int has_foot(const struct ir_remote* remote)
272 {
273  if (remote->pfoot > 0 && remote->sfoot > 0)
274  return 1;
275  else
276  return 0;
277 }
278 
279 static inline int has_toggle_bit_mask(const struct ir_remote* remote)
280 {
281  if (remote->toggle_bit_mask > 0)
282  return 1;
283  else
284  return 0;
285 }
286 
287 static inline int has_ignore_mask(const struct ir_remote* remote)
288 {
289  if (remote->ignore_mask > 0)
290  return 1;
291  else
292  return 0;
293 }
294 
295 static inline int has_repeat_mask(struct ir_remote* remote)
296 {
297  if (remote->repeat_mask > 0)
298  return 1;
299  else
300  return 0;
301 }
302 
303 static inline int has_toggle_mask(const struct ir_remote* remote)
304 {
305  if (remote->toggle_mask > 0)
306  return 1;
307  else
308  return 0;
309 }
310 
311 static inline lirc_t min_gap(const struct ir_remote* remote)
312 {
313  if (remote->gap2 != 0 && remote->gap2 < remote->gap)
314  return remote->gap2;
315  else
316  return remote->gap;
317 }
318 
319 static inline lirc_t max_gap(const struct ir_remote* remote)
320 {
321  if (remote->gap2 > remote->gap)
322  return remote->gap2;
323  else
324  return remote->gap;
325 }
326 
327 static inline unsigned int get_duty_cycle(const struct ir_remote* remote)
328 {
329  if (remote->duty_cycle == 0)
330  return 50;
331  else if (remote->duty_cycle < 0)
332  return 1;
333  else if (remote->duty_cycle > 100)
334  return 100;
335  else
336  return remote->duty_cycle;
337 }
338 
339 /* check if delta is inside exdelta +/- exdelta*eps/100 */
340 
341 static inline int expect(const struct ir_remote* remote,
342  lirc_t delta,
343  lirc_t exdelta)
344 {
345  int aeps = curr_driver->resolution > remote->aeps ?
346  curr_driver->resolution : remote->aeps;
347 
348  if (abs(exdelta - delta) <= exdelta * remote->eps / 100
349  || abs(exdelta - delta) <= aeps)
350  return 1;
351  return 0;
352 }
353 
354 static inline int expect_at_least(const struct ir_remote* remote,
355  lirc_t delta,
356  lirc_t exdelta)
357 {
358  int aeps = curr_driver->resolution > remote->aeps ?
359  curr_driver->resolution : remote->aeps;
360 
361  if (delta + exdelta * remote->eps / 100 >= exdelta
362  || delta + aeps >= exdelta)
363  return 1;
364  return 0;
365 }
366 
367 static inline int expect_at_most(const struct ir_remote* remote,
368  lirc_t delta,
369  lirc_t exdelta)
370 {
371  int aeps = curr_driver->resolution > remote->aeps ?
372  curr_driver->resolution : remote->aeps;
373 
374  if (delta <= exdelta + exdelta * remote->eps / 100
375  || delta <= exdelta + aeps)
376  return 1;
377  return 0;
378 }
379 
380 static inline lirc_t upper_limit(const struct ir_remote* remote, lirc_t val)
381 {
382  int aeps = curr_driver->resolution > remote->aeps ?
383  curr_driver->resolution : remote->aeps;
384  lirc_t eps_val = val * (100 + remote->eps) / 100;
385  lirc_t aeps_val = val + aeps;
386 
387  return eps_val > aeps_val ? eps_val : aeps_val;
388 }
389 
390 static inline lirc_t lower_limit(const struct ir_remote* remote, lirc_t val)
391 {
392  int aeps = curr_driver->resolution > remote->aeps ?
393  curr_driver->resolution : remote->aeps;
394  lirc_t eps_val = val * (100 - remote->eps) / 100;
395  lirc_t aeps_val = val - aeps;
396 
397  if (eps_val <= 0)
398  eps_val = 1;
399  if (aeps_val <= 0)
400  aeps_val = 1;
401 
402  return eps_val < aeps_val ? eps_val : aeps_val;
403 }
404 
405 /* only works if last <= current */
406 static inline unsigned long time_elapsed(const struct timeval* last,
407  const struct timeval* current)
408 {
409  unsigned long secs, diff;
410 
411  secs = current->tv_sec - last->tv_sec;
412 
413  diff = 1000000 * secs + current->tv_usec - last->tv_usec;
414 
415  return diff;
416 }
417 
418 static inline ir_code gen_mask(int bits)
419 {
420  int i;
421  ir_code mask;
422 
423  mask = 0;
424  for (i = 0; i < bits; i++) {
425  mask <<= 1;
426  mask |= 1;
427  }
428  return mask;
429 }
430 
431 static inline ir_code gen_ir_code(const struct ir_remote* remote,
432  ir_code pre,
433  ir_code code,
434  ir_code post)
435 {
436  ir_code all;
437 
438  all = (pre & gen_mask(remote->pre_data_bits));
439  all <<= remote->bits;
440  all |= is_raw(remote) ? code : (code & gen_mask(remote->bits));
441  all <<= remote->post_data_bits;
442  all |= post & gen_mask(remote->post_data_bits);
443 
444  return all;
445 }
446 
454 const struct ir_remote* is_in_remotes(const struct ir_remote* remotes,
455  const struct ir_remote* remote);
456 
458 struct ir_remote* get_ir_remote(const struct ir_remote* remotes,
459  const char* name);
460 
461 void get_frequency_range(const struct ir_remote* remotes,
462  unsigned int* min_freq,
463  unsigned int* max_freq);
464 
465 void get_filter_parameters(const struct ir_remote* remotes,
466  lirc_t* max_gap_lengthp,
467  lirc_t* min_pulse_lengthp,
468  lirc_t* min_space_lengthp,
469  lirc_t* max_pulse_lengthp,
470  lirc_t* max_space_lengthp);
471 
472 int map_code(const struct ir_remote* remote,
473  struct decode_ctx_t* ctx,
474  int pre_bits,
475  ir_code pre,
476  int bits,
477  ir_code code,
478  int post_bits,
479  ir_code post);
480 
481 void map_gap(const struct ir_remote* remote,
482  struct decode_ctx_t* ctx,
483  const struct timeval* start,
484  const struct timeval* last,
485  lirc_t signal_length);
486 
488 struct ir_ncode* get_code_by_name(const struct ir_remote* remote,
489  const char* name);
490 
491 int write_message(char* buffer,
492  size_t size,
493  const char* remote_name,
494  const char* button_name,
495  const char* button_suffix,
496  ir_code code,
497  int reps);
498 
509 char* decode_all(struct ir_remote* remotes);
510 
523 int send_ir_ncode(struct ir_remote* remote, struct ir_ncode* code, int delay);
524 
525 #ifdef __cplusplus
526 }
527 #endif
528 
535 void ir_remote_init(int use_dyncodes);
536 
538 const struct ir_remote* get_decoding(void);
539 
542 #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'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'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
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'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.