1 /*      $Id: ir_remote.h,v 5.44 2010/04/11 18:50:38 lirc Exp $      */
2 
3 /****************************************************************************
4  ** ir_remote.h *************************************************************
5  ****************************************************************************
6  *
7  * ir_remote.h - describes and decodes the signals from IR remotes
8  *
9  * Copyright (C) 1996,97 Ralph Metzler <rjkm@thp.uni-koeln.de>
10  * Copyright (C) 1998 Christoph Bartelmus <lirc@bartelmus.de>
11  *
12  */
13 
14 #ifndef IR_REMOTE_H
15 #define IR_REMOTE_H
16 
17 #include <sys/types.h>
18 #include <sys/time.h>
19 #include <unistd.h>
20 #include <string.h>
21 #include <math.h>
22 #include <stdlib.h>
23 
24 #include "drivers/lirc.h"
25 #include "hardware.h"
26 
27 #include "ir_remote_types.h"
28 
29 extern struct hardware hw;
30 
get_ir_code(struct ir_ncode * ncode,struct ir_code_node * node)31 static inline ir_code get_ir_code(struct ir_ncode *ncode, struct ir_code_node *node)
32 {
33 	if (ncode->next && node != NULL)
34 		return node->code;
35 	return ncode->code;
36 }
37 
get_next_ir_code_node(struct ir_ncode * ncode,struct ir_code_node * node)38 static inline struct ir_code_node *get_next_ir_code_node(struct ir_ncode *ncode, struct ir_code_node *node)
39 {
40 	if (node == NULL)
41 		return ncode->next;
42 	return node->next;
43 }
44 
bit_count(struct ir_remote * remote)45 static inline int bit_count(struct ir_remote *remote)
46 {
47 	return remote->pre_data_bits + remote->bits + remote->post_data_bits;
48 }
49 
bits_set(ir_code data)50 static inline int bits_set(ir_code data)
51 {
52 	int ret = 0;
53 	while (data) {
54 		if (data & 1)
55 			ret++;
56 		data >>= 1;
57 	}
58 	return ret;
59 }
60 
reverse(ir_code data,int bits)61 static inline ir_code reverse(ir_code data, int bits)
62 {
63 	int i;
64 	ir_code c;
65 
66 	c = 0;
67 	for (i = 0; i < bits; i++) {
68 		c |= (ir_code) (((data & (((ir_code) 1) << i)) ? 1 : 0))
69 		    << (bits - 1 - i);
70 	}
71 	return (c);
72 }
73 
is_pulse(lirc_t data)74 static inline int is_pulse(lirc_t data)
75 {
76 	return (data & PULSE_BIT ? 1 : 0);
77 }
78 
is_space(lirc_t data)79 static inline int is_space(lirc_t data)
80 {
81 	return (!is_pulse(data));
82 }
83 
has_repeat(struct ir_remote * remote)84 static inline int has_repeat(struct ir_remote *remote)
85 {
86 	if (remote->prepeat > 0 && remote->srepeat > 0)
87 		return (1);
88 	else
89 		return (0);
90 }
91 
set_protocol(struct ir_remote * remote,int protocol)92 static inline void set_protocol(struct ir_remote *remote, int protocol)
93 {
94 	remote->flags &= ~(IR_PROTOCOL_MASK);
95 	remote->flags |= protocol;
96 }
97 
is_raw(struct ir_remote * remote)98 static inline int is_raw(struct ir_remote *remote)
99 {
100 	if ((remote->flags & IR_PROTOCOL_MASK) == RAW_CODES)
101 		return (1);
102 	else
103 		return (0);
104 }
105 
is_space_enc(struct ir_remote * remote)106 static inline int is_space_enc(struct ir_remote *remote)
107 {
108 	if ((remote->flags & IR_PROTOCOL_MASK) == SPACE_ENC)
109 		return (1);
110 	else
111 		return (0);
112 }
113 
is_space_first(struct ir_remote * remote)114 static inline int is_space_first(struct ir_remote *remote)
115 {
116 	if ((remote->flags & IR_PROTOCOL_MASK) == SPACE_FIRST)
117 		return (1);
118 	else
119 		return (0);
120 }
121 
is_rc5(struct ir_remote * remote)122 static inline int is_rc5(struct ir_remote *remote)
123 {
124 	if ((remote->flags & IR_PROTOCOL_MASK) == RC5)
125 		return (1);
126 	else
127 		return (0);
128 }
129 
is_rc6(struct ir_remote * remote)130 static inline int is_rc6(struct ir_remote *remote)
131 {
132 	if ((remote->flags & IR_PROTOCOL_MASK) == RC6 || remote->rc6_mask)
133 		return (1);
134 	else
135 		return (0);
136 }
137 
is_biphase(struct ir_remote * remote)138 static inline int is_biphase(struct ir_remote *remote)
139 {
140 	if (is_rc5(remote) || is_rc6(remote))
141 		return (1);
142 	else
143 		return (0);
144 }
145 
is_rcmm(struct ir_remote * remote)146 static inline int is_rcmm(struct ir_remote *remote)
147 {
148 	if ((remote->flags & IR_PROTOCOL_MASK) == RCMM)
149 		return (1);
150 	else
151 		return (0);
152 }
153 
is_goldstar(struct ir_remote * remote)154 static inline int is_goldstar(struct ir_remote *remote)
155 {
156 	if ((remote->flags & IR_PROTOCOL_MASK) == GOLDSTAR)
157 		return (1);
158 	else
159 		return (0);
160 }
161 
is_grundig(struct ir_remote * remote)162 static inline int is_grundig(struct ir_remote *remote)
163 {
164 	if ((remote->flags & IR_PROTOCOL_MASK) == GRUNDIG)
165 		return (1);
166 	else
167 		return (0);
168 }
169 
is_bo(struct ir_remote * remote)170 static inline int is_bo(struct ir_remote *remote)
171 {
172 	if ((remote->flags & IR_PROTOCOL_MASK) == BO)
173 		return (1);
174 	else
175 		return (0);
176 }
177 
is_serial(struct ir_remote * remote)178 static inline int is_serial(struct ir_remote *remote)
179 {
180 	if ((remote->flags & IR_PROTOCOL_MASK) == SERIAL)
181 		return (1);
182 	else
183 		return (0);
184 }
185 
is_xmp(struct ir_remote * remote)186 static inline int is_xmp(struct ir_remote *remote)
187 {
188 	if ((remote->flags & IR_PROTOCOL_MASK) == XMP)
189 		return (1);
190 	else
191 		return (0);
192 }
193 
is_const(struct ir_remote * remote)194 static inline int is_const(struct ir_remote *remote)
195 {
196 	if (remote->flags & CONST_LENGTH)
197 		return (1);
198 	else
199 		return (0);
200 }
201 
has_repeat_gap(struct ir_remote * remote)202 static inline int has_repeat_gap(struct ir_remote *remote)
203 {
204 	if (remote->repeat_gap > 0)
205 		return (1);
206 	else
207 		return (0);
208 }
209 
has_pre(struct ir_remote * remote)210 static inline int has_pre(struct ir_remote *remote)
211 {
212 	if (remote->pre_data_bits > 0)
213 		return (1);
214 	else
215 		return (0);
216 }
217 
has_post(struct ir_remote * remote)218 static inline int has_post(struct ir_remote *remote)
219 {
220 	if (remote->post_data_bits > 0)
221 		return (1);
222 	else
223 		return (0);
224 }
225 
has_header(struct ir_remote * remote)226 static inline int has_header(struct ir_remote *remote)
227 {
228 	if (remote->phead > 0 && remote->shead > 0)
229 		return (1);
230 	else
231 		return (0);
232 }
233 
has_foot(struct ir_remote * remote)234 static inline int has_foot(struct ir_remote *remote)
235 {
236 	if (remote->pfoot > 0 && remote->sfoot > 0)
237 		return (1);
238 	else
239 		return (0);
240 }
241 
has_toggle_bit_mask(struct ir_remote * remote)242 static inline int has_toggle_bit_mask(struct ir_remote *remote)
243 {
244 	if (remote->toggle_bit_mask > 0)
245 		return (1);
246 	else
247 		return (0);
248 }
249 
has_ignore_mask(struct ir_remote * remote)250 static inline int has_ignore_mask(struct ir_remote *remote)
251 {
252 	if (remote->ignore_mask > 0)
253 		return (1);
254 	else
255 		return (0);
256 }
257 
has_toggle_mask(struct ir_remote * remote)258 static inline int has_toggle_mask(struct ir_remote *remote)
259 {
260 	if (remote->toggle_mask > 0)
261 		return (1);
262 	else
263 		return (0);
264 }
265 
min_gap(struct ir_remote * remote)266 static inline lirc_t min_gap(struct ir_remote *remote)
267 {
268 	if (remote->gap2 != 0 && remote->gap2 < remote->gap) {
269 		return remote->gap2;
270 	} else {
271 		return remote->gap;
272 	}
273 }
274 
max_gap(struct ir_remote * remote)275 static inline lirc_t max_gap(struct ir_remote *remote)
276 {
277 	if (remote->gap2 > remote->gap) {
278 		return remote->gap2;
279 	} else {
280 		return remote->gap;
281 	}
282 }
283 
284 /* check if delta is inside exdelta +/- exdelta*eps/100 */
285 
expect(struct ir_remote * remote,lirc_t delta,lirc_t exdelta)286 static inline int expect(struct ir_remote *remote, lirc_t delta, lirc_t exdelta)
287 {
288 	int aeps = hw.resolution > remote->aeps ? hw.resolution : remote->aeps;
289 
290 	if (abs(exdelta - delta) <= exdelta * remote->eps / 100 || abs(exdelta - delta) <= aeps)
291 		return 1;
292 	return 0;
293 }
294 
expect_at_least(struct ir_remote * remote,lirc_t delta,lirc_t exdelta)295 static inline int expect_at_least(struct ir_remote *remote, lirc_t delta, lirc_t exdelta)
296 {
297 	int aeps = hw.resolution > remote->aeps ? hw.resolution : remote->aeps;
298 
299 	if (delta + exdelta * remote->eps / 100 >= exdelta || delta + aeps >= exdelta) {
300 		return 1;
301 	}
302 	return 0;
303 }
304 
expect_at_most(struct ir_remote * remote,lirc_t delta,lirc_t exdelta)305 static inline int expect_at_most(struct ir_remote *remote, lirc_t delta, lirc_t exdelta)
306 {
307 	int aeps = hw.resolution > remote->aeps ? hw.resolution : remote->aeps;
308 
309 	if (delta <= exdelta + exdelta * remote->eps / 100 || delta <= exdelta + aeps) {
310 		return 1;
311 	}
312 	return 0;
313 }
314 
upper_limit(struct ir_remote * remote,lirc_t val)315 static inline lirc_t upper_limit(struct ir_remote *remote, lirc_t val)
316 {
317 	int aeps = hw.resolution > remote->aeps ? hw.resolution : remote->aeps;
318 	lirc_t eps_val = val * (100 + remote->eps) / 100;
319 	lirc_t aeps_val = val + aeps;
320 	return eps_val > aeps_val ? eps_val : aeps_val;
321 }
322 
lower_limit(struct ir_remote * remote,lirc_t val)323 static inline lirc_t lower_limit(struct ir_remote *remote, lirc_t val)
324 {
325 	int aeps = hw.resolution > remote->aeps ? hw.resolution : remote->aeps;
326 	lirc_t eps_val = val * (100 - remote->eps) / 100;
327 	lirc_t aeps_val = val - aeps;
328 
329 	if (eps_val <= 0)
330 		eps_val = 1;
331 	if (aeps_val <= 0)
332 		aeps_val = 1;
333 
334 	return eps_val < aeps_val ? eps_val : aeps_val;
335 }
336 
337 /* only works if last <= current */
time_elapsed(struct timeval * last,struct timeval * current)338 static inline unsigned long time_elapsed(struct timeval *last, struct timeval *current)
339 {
340 	unsigned long secs, diff;
341 
342 	secs = current->tv_sec - last->tv_sec;
343 
344 	diff = 1000000 * secs + current->tv_usec - last->tv_usec;
345 
346 	return (diff);
347 }
348 
gen_mask(int bits)349 static inline ir_code gen_mask(int bits)
350 {
351 	int i;
352 	ir_code mask;
353 
354 	mask = 0;
355 	for (i = 0; i < bits; i++) {
356 		mask <<= 1;
357 		mask |= 1;
358 	}
359 	return (mask);
360 }
361 
gen_ir_code(struct ir_remote * remote,ir_code pre,ir_code code,ir_code post)362 static inline ir_code gen_ir_code(struct ir_remote *remote, ir_code pre, ir_code code, ir_code post)
363 {
364 	ir_code all;
365 
366 	all = (pre & gen_mask(remote->pre_data_bits));
367 	all <<= remote->bits;
368 	all |= is_raw(remote) ? code : (code & gen_mask(remote->bits));
369 	all <<= remote->post_data_bits;
370 	all |= post & gen_mask(remote->post_data_bits);
371 
372 	return all;
373 }
374 
375 void get_frequency_range(struct ir_remote *remotes, unsigned int *min_freq, unsigned int *max_freq);
376 void get_filter_parameters(struct ir_remote *remotes, lirc_t * max_gap_lengthp, lirc_t * min_pulse_lengthp,
377 			   lirc_t * min_space_lengthp, lirc_t * max_pulse_lengthp, lirc_t * max_space_lengthp);
378 struct ir_remote *is_in_remotes(struct ir_remote *remotes, struct ir_remote *remote);
379 struct ir_remote *get_ir_remote(struct ir_remote *remotes, char *name);
380 int map_code(struct ir_remote *remote, ir_code * prep, ir_code * codep, ir_code * postp, int pre_bits, ir_code pre,
381 	     int bits, ir_code code, int post_bits, ir_code post);
382 void map_gap(struct ir_remote *remote, struct timeval *start, struct timeval *last, lirc_t signal_length,
383 	     int *repeat_flagp, lirc_t * min_remaining_gapp, lirc_t * max_remaining_gapp);
384 struct ir_ncode *get_code_by_name(struct ir_remote *remote, char *name);
385 struct ir_ncode *get_code(struct ir_remote *remote, ir_code pre, ir_code code, ir_code post,
386 			  ir_code * toggle_bit_mask_state);
387 __u64 set_code(struct ir_remote *remote, struct ir_ncode *found, ir_code toggle_bit_mask_state, int repeat_flag,
388 	       lirc_t min_remaining_gap, lirc_t max_remaining_gap);
389 int write_message(char *buffer, size_t size, const char *remote_name, const char *button_name,
390 		  const char *button_suffix, ir_code code, int reps);
391 char *decode_all(struct ir_remote *remotes);
392 int send_ir_ncode(struct ir_remote *remote, struct ir_ncode *code);
393 
394 #endif
395