Debian Codegraph @

utils/common/ir-encode.c

raw
1 /*
2 * ir-encode.c - encodes IR scancodes in different protocols
3 *
4 * Copyright (C) 2016 Sean Young <[email protected]>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, version 2 of the License.
9
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16 /*
17 * TODO: XMP protocol and MCE keyboard
18 */
19
20 #include <stdbool.h>
21 #include <stdint.h>
22 #include <stdlib.h>
23 #include <ctype.h>
24 #include <stdio.h>
25
26 #include <linux/lirc.h>
27
28 #include "ir-encode.h"
29
30 #define NS_TO_US(x) (((x)+500)/1000)
31
32 static const int nec_unit = 562500;
33
34 static void nec_add_byte(unsigned *buf, int *n, unsigned bits)
35 {
36 int i;
37 for (i=0; i<8; i++) {
38 buf[(*n)++] = NS_TO_US(nec_unit);
39 if (bits & (1 << i))
40 buf[(*n)++] = NS_TO_US(nec_unit * 3);
41 else
42 buf[(*n)++] = NS_TO_US(nec_unit);
43 }
44 }
45
46 static int nec_encode(enum rc_proto proto, unsigned scancode, unsigned *buf)
47 {
48 int n = 0;
49
50 buf[n++] = NS_TO_US(nec_unit * 16);
51 buf[n++] = NS_TO_US(nec_unit * 8);
52
53 switch (proto) {
54 default:
55 return 0;
56 case RC_PROTO_NEC:
57 nec_add_byte(buf, &n, scancode >> 8);
58 nec_add_byte(buf, &n, ~(scancode >> 8));
59 nec_add_byte(buf, &n, scancode);
60 nec_add_byte(buf, &n, ~scancode);
61 break;
62 case RC_PROTO_NECX:
63 nec_add_byte(buf, &n, scancode >> 16);
64 nec_add_byte(buf, &n, scancode >> 8);
65 nec_add_byte(buf, &n, scancode);
66 nec_add_byte(buf, &n, ~scancode);
67 break;
68 case RC_PROTO_NEC32:
69 nec_add_byte(buf, &n, scancode >> 16);
70 nec_add_byte(buf, &n, scancode >> 24);
71 nec_add_byte(buf, &n, scancode);
72 nec_add_byte(buf, &n, scancode >> 8);
73 break;
74 }
75
76 buf[n++] = NS_TO_US(nec_unit);
77
78 return n;
79 }
80
81 static int jvc_encode(enum rc_proto proto, unsigned scancode, unsigned *buf)
82 {
83 const int jvc_unit = 525000;
84 int i;
85
86 /* swap bytes so address comes first */
87 scancode = ((scancode << 8) & 0xff00) | ((scancode >> 8) & 0x00ff);
88
89 *buf++ = NS_TO_US(jvc_unit * 16);
90 *buf++ = NS_TO_US(jvc_unit * 8);
91
92 for (i=0; i<16; i++) {
93 *buf++ = NS_TO_US(jvc_unit);
94
95 if (scancode & 1)
96 *buf++ = NS_TO_US(jvc_unit * 3);
97 else
98 *buf++ = NS_TO_US(jvc_unit);
99
100 scancode >>= 1;
101 }
102
103 *buf = NS_TO_US(jvc_unit);
104
105 return 35;
106 }
107
108 static const int sanyo_unit = 562500;
109
110 static void sanyo_add_bits(unsigned **buf, int bits, int count)
111 {
112 int i;
113 for (i=0; i<count; i++) {
114 *(*buf)++ = NS_TO_US(sanyo_unit);
115
116 if (bits & (1 << i))
117 *(*buf)++ = NS_TO_US(sanyo_unit * 3);
118 else
119 *(*buf)++ = NS_TO_US(sanyo_unit);
120 }
121 }
122
123 static int sanyo_encode(enum rc_proto proto, unsigned scancode, unsigned *buf)
124 {
125 *buf++ = NS_TO_US(sanyo_unit * 16);
126 *buf++ = NS_TO_US(sanyo_unit * 8);
127
128 sanyo_add_bits(&buf, scancode >> 8, 13);
129 sanyo_add_bits(&buf, ~(scancode >> 8), 13);
130 sanyo_add_bits(&buf, scancode, 8);
131 sanyo_add_bits(&buf, ~scancode, 8);
132
133 *buf = NS_TO_US(sanyo_unit);
134
135 return 87;
136 }
137
138 static const int sharp_unit = 40000;
139
140 static void sharp_add_bits(unsigned **buf, int bits, int count)
141 {
142 int i;
143 for (i=0; i<count; i++) {
144 *(*buf)++ = NS_TO_US(sharp_unit * 8);
145
146 if (bits & (1 << i))
147 *(*buf)++ = NS_TO_US(sharp_unit * 42);
148 else
149 *(*buf)++ = NS_TO_US(sharp_unit * 17);
150 }
151 }
152
153 static int sharp_encode(enum rc_proto proto, unsigned scancode, unsigned *buf)
154 {
155 sharp_add_bits(&buf, scancode >> 8, 5);
156 sharp_add_bits(&buf, scancode, 8);
157 sharp_add_bits(&buf, 1, 2);
158
159 *buf++ = NS_TO_US(sharp_unit * 8);
160 *buf++ = NS_TO_US(sharp_unit * 1000);
161
162 sharp_add_bits(&buf, scancode >> 8, 5);
163 sharp_add_bits(&buf, ~scancode, 8);
164 sharp_add_bits(&buf, ~1, 2);
165 *buf++ = NS_TO_US(sharp_unit * 8);
166
167 return (13 + 2) * 4 + 3;
168 }
169
170 static const int sony_unit = 600000;
171
172 static void sony_add_bits(unsigned *buf, int *n, int bits, int count)
173 {
174 int i;
175 for (i=0; i<count; i++) {
176 if (bits & (1 << i))
177 buf[(*n)++] = NS_TO_US(sony_unit * 2);
178 else
179 buf[(*n)++] = NS_TO_US(sony_unit);
180
181 buf[(*n)++] = NS_TO_US(sony_unit);
182 }
183 }
184
185 static int sony_encode(enum rc_proto proto, unsigned scancode, unsigned *buf)
186 {
187 int n = 0;
188
189 buf[n++] = NS_TO_US(sony_unit * 4);
190 buf[n++] = NS_TO_US(sony_unit);
191
192 switch (proto) {
193 case RC_PROTO_SONY12:
194 sony_add_bits(buf, &n, scancode, 7);
195 sony_add_bits(buf, &n, scancode >> 16, 5);
196 break;
197 case RC_PROTO_SONY15:
198 sony_add_bits(buf, &n, scancode, 7);
199 sony_add_bits(buf, &n, scancode >> 16, 8);
200 break;
201 case RC_PROTO_SONY20:
202 sony_add_bits(buf, &n, scancode, 7);
203 sony_add_bits(buf, &n, scancode >> 16, 5);
204 sony_add_bits(buf, &n, scancode >> 8, 8);
205 break;
206 default:
207 return 0;
208 }
209
210 /* ignore last space */
211 return n - 1;
212 }
213
214 static const unsigned int rc5_unit = 888888;
215
216 static void rc5_advance_space(unsigned *buf, unsigned *n, unsigned length)
217 {
218 if (*n % 2)
219 buf[*n] += length;
220 else
221 buf[++(*n)] = length;
222 }
223
224 static void rc5_advance_pulse(unsigned *buf, unsigned *n, unsigned length)
225 {
226 if (*n % 2)
227 buf[++(*n)] = length;
228 else
229 buf[*n] += length;
230 }
231
232 static void rc5_add_bits(unsigned *buf, unsigned *n, int bits, int count)
233 {
234 while (count--) {
235 if (bits & (1 << count)) {
236 rc5_advance_space(buf, n, NS_TO_US(rc5_unit));
237 rc5_advance_pulse(buf, n, NS_TO_US(rc5_unit));
238 } else {
239 rc5_advance_pulse(buf, n, NS_TO_US(rc5_unit));
240 rc5_advance_space(buf, n, NS_TO_US(rc5_unit));
241 }
242 }
243 }
244
245 static int rc5_encode(enum rc_proto proto, unsigned scancode, unsigned *buf)
246 {
247 unsigned n = 0;
248
249 buf[n] = NS_TO_US(rc5_unit);
250
251 switch (proto) {
252 default:
253 return 0;
254 case RC_PROTO_RC5:
255 rc5_add_bits(buf, &n, !(scancode & 0x40), 1);
256 rc5_add_bits(buf, &n, 0, 1);
257 rc5_add_bits(buf, &n, scancode >> 8, 5);
258 rc5_add_bits(buf, &n, scancode, 6);
259 break;
260 case RC_PROTO_RC5_SZ:
261 rc5_add_bits(buf, &n, !!(scancode & 0x2000), 1);
262 rc5_add_bits(buf, &n, 0, 1);
263 rc5_add_bits(buf, &n, scancode >> 6, 6);
264 rc5_add_bits(buf, &n, scancode, 6);
265 break;
266 case RC_PROTO_RC5X_20:
267 rc5_add_bits(buf, &n, !(scancode & 0x4000), 1);
268 rc5_add_bits(buf, &n, 0, 1);
269 rc5_add_bits(buf, &n, scancode >> 16, 5);
270 rc5_advance_space(buf, &n, NS_TO_US(rc5_unit * 4));
271 rc5_add_bits(buf, &n, scancode >> 8, 6);
272 rc5_add_bits(buf, &n, scancode, 6);
273 break;
274 }
275
276 /* drop any trailing pulse */
277 return (n % 2) ? n : n + 1;
278 }
279
280 static const unsigned int rc6_unit = 444444;
281
282 static void rc6_advance_space(unsigned *buf, unsigned *n, unsigned length)
283 {
284 if (*n % 2)
285 buf[*n] += length;
286 else
287 buf[++(*n)] = length;
288 }
289
290 static void rc6_advance_pulse(unsigned *buf, unsigned *n, unsigned length)
291 {
292 if (*n % 2)
293 buf[++(*n)] = length;
294 else
295 buf[*n] += length;
296 }
297
298 static void rc6_add_bits(unsigned *buf, unsigned *n,
299 unsigned bits, unsigned count, unsigned length)
300 {
301 while (count--) {
302 if (bits & (1 << count)) {
303 rc6_advance_pulse(buf, n, length);
304 rc6_advance_space(buf, n, length);
305 } else {
306 rc6_advance_space(buf, n, length);
307 rc6_advance_pulse(buf, n, length);
308 }
309 }
310 }
311
312 static int rc6_encode(enum rc_proto proto, unsigned scancode, unsigned *buf)
313 {
314 unsigned n = 0;
315 buf[n++] = NS_TO_US(rc6_unit * 6);
316 buf[n++] = NS_TO_US(rc6_unit * 2);
317 buf[n] = 0;
318
319 switch (proto) {
320 default:
321 return 0;
322 case RC_PROTO_RC6_0:
323 rc6_add_bits(buf, &n, 8, 4, NS_TO_US(rc6_unit));
324 rc6_add_bits(buf, &n, 0, 1, NS_TO_US(rc6_unit * 2));
325 rc6_add_bits(buf, &n, scancode, 16, NS_TO_US(rc6_unit));
326 break;
327 case RC_PROTO_RC6_6A_20:
328 rc6_add_bits(buf, &n, 14, 4, NS_TO_US(rc6_unit));
329 rc6_add_bits(buf, &n, 0, 1, NS_TO_US(rc6_unit * 2));
330 rc6_add_bits(buf, &n, scancode, 20, NS_TO_US(rc6_unit));
331 break;
332 case RC_PROTO_RC6_6A_24:
333 rc6_add_bits(buf, &n, 14, 4, NS_TO_US(rc6_unit));
334 rc6_add_bits(buf, &n, 0, 1, NS_TO_US(rc6_unit * 2));
335 rc6_add_bits(buf, &n, scancode, 24, NS_TO_US(rc6_unit));
336 break;
337 case RC_PROTO_RC6_6A_32:
338 case RC_PROTO_RC6_MCE:
339 rc6_add_bits(buf, &n, 14, 4, NS_TO_US(rc6_unit));
340 rc6_add_bits(buf, &n, 0, 1, NS_TO_US(rc6_unit * 2));
341 rc6_add_bits(buf, &n, scancode, 32, NS_TO_US(rc6_unit));
342 break;
343 }
344
345 /* drop any trailing pulse */
346 return (n % 2) ? n : n + 1;
347 }
348
349 static int xbox_dvd_encode(enum rc_proto proto, unsigned scancode, unsigned *buf)
350 {
351 int len = 0;
352
353 buf[len++] = 4000;
354 buf[len++] = 3900;
355
356 scancode &= 0xfff;
357 scancode |= (~scancode << 12) & 0xfff000;
358
359 for (int i=23; i >=0; i--) {
360 buf[len++] = 550;
361
362 if (scancode & (1 << i))
363 buf[len++] = 1900;
364 else
365 buf[len++] = 900;
366 }
367
368 buf[len++]= 550;
369
370 return len;
371 }
372
373 static const struct {
374 char name[10];
375 unsigned scancode_mask;
376 unsigned max_edges;
377 unsigned carrier;
378 int (*encode)(enum rc_proto proto, unsigned scancode, unsigned *buf);
379 } protocols[] = {
380 [RC_PROTO_UNKNOWN] = { "unknown" },
381 [RC_PROTO_OTHER] = { "other" },
382 [RC_PROTO_RC5] = { "rc5", 0x1f7f, 25, 36000, rc5_encode },
383 [RC_PROTO_RC5X_20] = { "rc5x_20", 0x1f7f3f, 40, 36000, rc5_encode },
384 [RC_PROTO_RC5_SZ] = { "rc5_sz", 0x2fff, 27, 36000, rc5_encode },
385 [RC_PROTO_SONY12] = { "sony12", 0x1f007f, 25, 40000, sony_encode },
386 [RC_PROTO_SONY15] = { "sony15", 0xff007f, 31, 40000, sony_encode },
387 [RC_PROTO_SONY20] = { "sony20", 0x1fff7f, 41, 40000, sony_encode },
388 [RC_PROTO_JVC] = { "jvc", 0xffff, 35, 38000, jvc_encode },
389 [RC_PROTO_NEC] = { "nec", 0xffff, 67, 38000, nec_encode },
390 [RC_PROTO_NECX] = { "necx", 0xffffff, 67, 38000, nec_encode },
391 [RC_PROTO_NEC32] = { "nec32", 0xffffffff, 67, 38000, nec_encode },
392 [RC_PROTO_SANYO] = { "sanyo", 0x1fffff, 87, 38000, sanyo_encode },
393 [RC_PROTO_RC6_0] = { "rc6_0", 0xffff, 43, 36000, rc6_encode },
394 [RC_PROTO_RC6_6A_20] = { "rc6_6a_20", 0xfffff, 52, 36000, rc6_encode },
395 [RC_PROTO_RC6_6A_24] = { "rc6_6a_24", 0xffffff, 60, 36000, rc6_encode },
396 [RC_PROTO_RC6_6A_32] = { "rc6_6a_32", 0xffffffff, 76, 36000, rc6_encode },
397 [RC_PROTO_RC6_MCE] = { "rc6_mce", 0xffff7fff, 76, 36000, rc6_encode },
398 [RC_PROTO_SHARP] = { "sharp", 0x1fff, 63, 38000, sharp_encode },
399 [RC_PROTO_MCIR2_KBD] = { "mcir2-kbd" },
400 [RC_PROTO_MCIR2_MSE] = { "mcir2-mse" },
401 [RC_PROTO_XMP] = { "xmp" },
402 [RC_PROTO_CEC] = { "cec" },
403 [RC_PROTO_IMON] = { "imon", 0x7fffffff },
404 [RC_PROTO_RCMM12] = { "rc-mm-12", 0x0fff },
405 [RC_PROTO_RCMM24] = { "rc-mm-24", 0xffffff },
406 [RC_PROTO_RCMM32] = { "rc-mm-32", 0xffffffff },
407 [RC_PROTO_XBOX_DVD] = { "xbox-dvd", 0xfff, 68, 38000, xbox_dvd_encode },
408 };
409
410 static bool str_like(const char *a, const char *b)
411 {
412 while (*a && *b) {
413 while (*a == ' ' || *a == '-' || *a == '_')
414 a++;
415 while (*b == ' ' || *b == '-' || *b == '_')
416 b++;
417
418 if (*a >= 0x7f || *b >= 0x7f)
419 return false;
420
421 if (tolower(*a) != tolower(*b))
422 return false;
423
424 a++; b++;
425 }
426
427 return !*a && !*b;
428 }
429
430 bool protocol_match(const char *name, enum rc_proto *proto)
431 {
432 enum rc_proto p;
433
434 for (p=0; p<ARRAY_SIZE(protocols); p++) {
435 if (str_like(protocols[p].name, name)) {
436 *proto = p;
437 return true;
438 }
439 }
440
441 return false;
442 }
443
444 unsigned protocol_carrier(enum rc_proto proto)
445 {
446 return protocols[proto].carrier;
447 }
448
449 unsigned protocol_max_size(enum rc_proto proto)
450 {
451 return protocols[proto].max_edges;
452 }
453
454 unsigned protocol_scancode_mask(enum rc_proto proto)
455 {
456 return protocols[proto].scancode_mask;
457 }
458
459 void protocol_scancode_valid(enum rc_proto *p, unsigned *s)
460 {
461 enum rc_proto p2 = *p;
462 unsigned s2 = *s;
463
464 // rc6_mce is rc6_6a_32 with vendor code 0x800f and
465 if (*p == RC_PROTO_RC6_MCE && (*s & 0xffff0000) != 0x800f0000) {
466 p2 = RC_PROTO_RC6_6A_32;
467 } else if (*p == RC_PROTO_RC6_6A_32 && (*s & 0xffff0000) == 0x800f0000) {
468 p2 = RC_PROTO_RC6_MCE;
469 } else if (*p == RC_PROTO_NEC || *p == RC_PROTO_NECX || *p == RC_PROTO_NEC32) {
470 // nec scancodes may repeat the address and command
471 // in inverted form; the inverted values are not in the
472 // scancode.
473
474 // can 24 bit scancode be represented as 16 bit scancode
475 if (*s > 0x0000ffff && *s <= 0x00ffffff) {
476 if ((((*s >> 16) ^ ~(*s >> 8)) & 0xff) != 0) {
477 // is it necx
478 p2 = RC_PROTO_NECX;
479 } else {
480 // or regular nec
481 s2 = ((*s >> 8) & 0xff00) | (*s & 0x00ff);
482 p2 = RC_PROTO_NEC;
483 }
484 // can 32 bit scancode be represented as 24 or 16 bit scancode
485 } else if (*s > 0x00ffffff) {
486 if (((((*s >> 24) ^ ~(*s >> 16)) & 0xff) == 0) &&
487 ((((*s >> 8) ^ ~(*s >> 0)) & 0xff) == 0)) {
488 // is it nec
489 s2 = ((*s >> 16) & 0xff00) |
490 ((*s >> 8) & 0x00ff);
491 p2 = RC_PROTO_NEC;
492 } else if (((((*s >> 24) ^ ~(*s >> 16)) & 0xff) != 0) &&
493 ((((*s >> 8) ^ ~(*s >> 0)) & 0xff) == 0)) {
494 // is it nec-x
495 s2 = (*s >> 8) & 0xffffff;
496 p2 = RC_PROTO_NECX;
497 } else {
498 // or it has to be nec32
499 p2 = RC_PROTO_NEC32;
500 }
501 }
502 }
503
504 s2 &= protocols[p2].scancode_mask;
505
506 if (*p != p2 || *s != s2) {
507 fprintf(stderr,
508 "warning: `%s:0x%x' will be decoded as `%s:0x%x'\n",
509 protocol_name(*p), *s, protocol_name(p2), s2);
510
511 *p = p2;
512 *s = s2;
513 }
514 }
515
516 bool protocol_encoder_available(enum rc_proto proto)
517 {
518 return protocols[proto].encode != NULL;
519 }
520
521 unsigned protocol_encode(enum rc_proto proto, unsigned scancode, unsigned *buf)
522 {
523 if (!protocols[proto].encode)
524 return 0;
525
526 return protocols[proto].encode(proto, scancode, buf);
527 }
528
529 const char* protocol_name(enum rc_proto proto)
530 {
531 if (proto >= ARRAY_SIZE(protocols) || !protocols[proto].name[0])
532 return NULL;
533
534 return protocols[proto].name;
535 }