| 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 /* check if delta is inside exdelta +/- exdelta*eps/100 */
328
329 static inline int expect(const struct ir_remote* remote,
330 lirc_t delta,
331 lirc_t exdelta)
332 {
333 int aeps = curr_driver->resolution > remote->aeps ?
334 curr_driver->resolution : remote->aeps;
335
336 if (abs(exdelta - delta) <= exdelta * remote->eps / 100
337 || abs(exdelta - delta) <= aeps)
338 return 1;
339 return 0;
340 }
341
342 static inline int expect_at_least(const struct ir_remote* remote,
343 lirc_t delta,
344 lirc_t exdelta)
345 {
346 int aeps = curr_driver->resolution > remote->aeps ?
347 curr_driver->resolution : remote->aeps;
348
349 if (delta + exdelta * remote->eps / 100 >= exdelta
350 || delta + aeps >= exdelta)
351 return 1;
352 return 0;
353 }
354
355 static inline int expect_at_most(const struct ir_remote* remote,
356 lirc_t delta,
357 lirc_t exdelta)
358 {
359 int aeps = curr_driver->resolution > remote->aeps ?
360 curr_driver->resolution : remote->aeps;
361
362 if (delta <= exdelta + exdelta * remote->eps / 100
363 || delta <= exdelta + aeps)
364 return 1;
365 return 0;
366 }
367
368 static inline lirc_t upper_limit(const struct ir_remote* remote, lirc_t val)
369 {
370 int aeps = curr_driver->resolution > remote->aeps ?
371 curr_driver->resolution : remote->aeps;
372 lirc_t eps_val = val * (100 + remote->eps) / 100;
373 lirc_t aeps_val = val + aeps;
374
375 return eps_val > aeps_val ? eps_val : aeps_val;
376 }
377
378 static inline lirc_t lower_limit(const struct ir_remote* remote, lirc_t val)
379 {
380 int aeps = curr_driver->resolution > remote->aeps ?
381 curr_driver->resolution : remote->aeps;
382 lirc_t eps_val = val * (100 - remote->eps) / 100;
383 lirc_t aeps_val = val - aeps;
384
385 if (eps_val <= 0)
386 eps_val = 1;
387 if (aeps_val <= 0)
388 aeps_val = 1;
389
390 return eps_val < aeps_val ? eps_val : aeps_val;
391 }
392
393 /* only works if last <= current */
394 static inline unsigned long time_elapsed(const struct timeval* last,
395 const struct timeval* current)
396 {
397 unsigned long secs, diff;
398
399 secs = current->tv_sec - last->tv_sec;
400
401 diff = 1000000 * secs + current->tv_usec - last->tv_usec;
402
403 return diff;
404 }
405
406 static inline ir_code gen_mask(int bits)
407 {
408 int i;
409 ir_code mask;
410
411 mask = 0;
412 for (i = 0; i < bits; i++) {
413 mask <<= 1;
414 mask |= 1;
415 }
416 return mask;
417 }
418
419 static inline ir_code gen_ir_code(const struct ir_remote* remote,
420 ir_code pre,
421 ir_code code,
422 ir_code post)
423 {
424 ir_code all;
425
426 all = (pre & gen_mask(remote->pre_data_bits));
427 all <<= remote->bits;
428 all |= is_raw(remote) ? code : (code & gen_mask(remote->bits));
429 all <<= remote->post_data_bits;
430 all |= post & gen_mask(remote->post_data_bits);
431
432 return all;
433 }
434
442 const struct ir_remote* is_in_remotes(const struct ir_remote* remotes,
443 const struct ir_remote* remote);
444
446 struct ir_remote* get_ir_remote(const struct ir_remote* remotes,
447 const char* name);
448
449 void get_frequency_range(const struct ir_remote* remotes,
450 unsigned int* min_freq,
451 unsigned int* max_freq);
452
453 void get_filter_parameters(const struct ir_remote* remotes,
454 lirc_t* max_gap_lengthp,
455 lirc_t* min_pulse_lengthp,
456 lirc_t* min_space_lengthp,
457 lirc_t* max_pulse_lengthp,
458 lirc_t* max_space_lengthp);
459
460 int map_code(const struct ir_remote* remote,
461 struct decode_ctx_t* ctx,
462 int pre_bits,
463 ir_code pre,
464 int bits,
465 ir_code code,
466 int post_bits,
467 ir_code post);
468
469 void map_gap(const struct ir_remote* remote,
470 struct decode_ctx_t* ctx,
471 const struct timeval* start,
472 const struct timeval* last,
473 lirc_t signal_length);
474
476 struct ir_ncode* get_code_by_name(const struct ir_remote* remote,
477 const char* name);
478
479 int write_message(char* buffer,
480 size_t size,
481 const char* remote_name,
482 const char* button_name,
483 const char* button_suffix,
484 ir_code code,
485 int reps);
486
497 char* decode_all(struct ir_remote* remotes);
498
511 int send_ir_ncode(struct ir_remote* remote, struct ir_ncode* code, int delay);
512
513 #ifdef __cplusplus
514 }
515 #endif
516
523 void ir_remote_init(int use_dyncodes);
524
526 const struct ir_remote* get_decoding(void);
527
530 #endif
last_remote
struct ir_remote * last_remote
Definition: ir_remote.c:58
repeat_code
struct ir_ncode * repeat_code
Definition: ir_remote.c:62
ir_remote
Definition: ir_remote_types.h:167
ir_remote::bits
int bits
Definition: ir_remote_types.h:171
RC6
#define RC6
Definition: ir_remote_types.h:123
ir_code_node
Definition: ir_remote_types.h:65
GOLDSTAR
#define GOLDSTAR
Definition: ir_remote_types.h:127
ir_remote_init
void ir_remote_init(int use_dyncodes)
Definition: ir_remote.c:123
curr_driver
const struct driver *const curr_driver
Definition: driver.c:28
ir_remote::repeat_mask
ir_code repeat_mask
Definition: ir_remote_types.h:234
get_filter_parameters
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:192
ir_ncode::next
struct ir_code_node * next
Definition: ir_remote_types.h:91
ir_remote::name
const char * name
Definition: ir_remote_types.h:168
SPACE_ENC
#define SPACE_ENC
Definition: ir_remote_types.h:125
get_code_by_name
struct ir_ncode * get_code_by_name(const struct ir_remote *remote, const char *name)
Definition: ir_remote.c:396
driver::resolution
unsigned int resolution
Definition: driver.h:225
driver.h
Interface to the userspace drivers.
ir_remote::eps
int eps
Definition: ir_remote_types.h:173
get_ir_remote
struct ir_remote * get_ir_remote(const struct ir_remote *remotes, const char *name)
Definition: ir_remote.c:250
ir_code
__u64 ir_code
Definition: ir_remote_types.h:59
map_code
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:282
ir_remote::sfoot
lirc_t sfoot
Definition: ir_remote_types.h:193
decode_all
char * decode_all(struct ir_remote *remotes)
Definition: ir_remote.c:731
ir_remote::pre_data_bits
int pre_data_bits
Definition: ir_remote_types.h:196
ir_ncode::name
char * name
Definition: ir_remote_types.h:78
ir_remote::post_data_bits
int post_data_bits
Definition: ir_remote_types.h:198
RCMM
#define RCMM
Definition: ir_remote_types.h:124
ir_remote::toggle_mask
ir_code toggle_mask
Definition: ir_remote_types.h:222
ir_remote_types.h
Describes and decodes the signals from IR remotes.
write_message
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)
Definition: ir_remote.c:712
RC5
#define RC5
Definition: ir_remote_types.h:117
ir_remote::shead
lirc_t shead
Definition: ir_remote_types.h:186
is_in_remotes
const struct ir_remote * is_in_remotes(const struct ir_remote *remotes, const struct ir_remote *remote)
Definition: ir_remote.c:238
SPACE_FIRST
#define SPACE_FIRST
Definition: ir_remote_types.h:126
ir_remote::repeat_gap
__u32 repeat_gap
Definition: ir_remote_types.h:205
ir_remote::gap
__u32 gap
Definition: ir_remote_types.h:203
get_decoding
const struct ir_remote * get_decoding(void)
Definition: ir_remote.c:852
CONST_LENGTH
#define CONST_LENGTH
Definition: ir_remote_types.h:137
GRUNDIG
#define GRUNDIG
Definition: ir_remote_types.h:128
ncode_free
void ncode_free(struct ir_ncode *ncode)
Definition: ir_remote.c:103
ir_ncode
Definition: ir_remote_types.h:76
ir_remote::flags
int flags
Definition: ir_remote_types.h:172
ir_remote::aeps
unsigned int aeps
Definition: ir_remote_types.h:174
ir_remote::srepeat
lirc_t srepeat
Definition: ir_remote_types.h:194
decode_ctx_t
Definition: ir_remote_types.h:154
map_gap
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:328
SERIAL
#define SERIAL
Definition: ir_remote_types.h:130
ncode_dup
struct ir_ncode * ncode_dup(struct ir_ncode *ncode)
Definition: ir_remote.c:68
get_frequency_range
void get_frequency_range(const struct ir_remote *remotes, unsigned int *min_freq, unsigned int *max_freq)
Definition: ir_remote.c:155
BO
#define BO
Definition: ir_remote_types.h:129
ir_ncode::code
ir_code code
Definition: ir_remote_types.h:81
ir_remote::rc6_mask
ir_code rc6_mask
Definition: ir_remote_types.h:223
repeat_remote
struct ir_remote * repeat_remote
Definition: ir_remote.c:60
ir_remote::toggle_bit_mask
ir_code toggle_bit_mask
Definition: ir_remote_types.h:207
ir_remote::gap2
__u32 gap2
Definition: ir_remote_types.h:204
send_ir_ncode
int send_ir_ncode(struct ir_remote *remote, struct ir_ncode *code, int delay)
Definition: ir_remote.c:821
RAW_CODES
#define RAW_CODES
Definition: ir_remote_types.h:116
ir_remote::ignore_mask
ir_code ignore_mask
Definition: ir_remote_types.h:233
XMP
#define XMP
Definition: ir_remote_types.h:131