| 1 |
/* |
| 2 |
* ir-ctl.c - Program to send and receive IR using lirc interface |
| 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 |
#include <stdio.h> |
| 17 |
#include <unistd.h> |
| 18 |
#include <stdlib.h> |
| 19 |
#include <stdbool.h> |
| 20 |
#include <stdint.h> |
| 21 |
#include <sys/types.h> |
| 22 |
#include <sys/stat.h> |
| 23 |
#include <sys/ioctl.h> |
| 24 |
#include <errno.h> |
| 25 |
#include <fcntl.h> |
| 26 |
#include <argp.h> |
| 27 |
#include <sysexits.h> |
| 28 |
|
| 29 |
#include <linux/lirc.h> |
| 30 |
|
| 31 |
#include "ir-encode.h" |
| 32 |
#include "keymap.h" |
| 33 |
#include "bpf_encoder.h" |
| 34 |
|
| 35 |
#ifdef ENABLE_NLS |
| 36 |
# define _(string) gettext(string) |
| 37 |
# include "gettext.h" |
| 38 |
# include <locale.h> |
| 39 |
# include <langinfo.h> |
| 40 |
# include <iconv.h> |
| 41 |
#else |
| 42 |
# include <string.h> |
| 43 |
# define _(string) string |
| 44 |
#endif |
| 45 |
|
| 46 |
# define N_(string) string |
| 47 |
|
| 48 |
/* taken from glibc unistd.h */ |
| 49 |
#ifndef TEMP_FAILURE_RETRY |
| 50 |
#define TEMP_FAILURE_RETRY(expression) \ |
| 51 |
({ long int __result; \ |
| 52 |
do __result = (long int) (expression); \ |
| 53 |
while (__result == -1L && errno == EINTR); \ |
| 54 |
__result; }) |
| 55 |
#endif |
| 56 |
|
| 57 |
/* See drivers/media/rc/lirc_dev.c line 22 */ |
| 58 |
#define LIRCBUF_SIZE 1024 |
| 59 |
#define IR_DEFAULT_TIMEOUT 125000 |
| 60 |
#define UNSET UINT32_MAX |
| 61 |
|
| 62 |
const char *argp_program_version = "IR ctl version " V4L_UTILS_VERSION; |
| 63 |
const char *argp_program_bug_address = "Sean Young <[email protected]>"; |
| 64 |
|
| 65 |
enum send_ty { |
| 66 |
SEND_RAW, |
| 67 |
SEND_SCANCODE, |
| 68 |
SEND_KEYCODE, |
| 69 |
SEND_GAP, |
| 70 |
}; |
| 71 |
|
| 72 |
/* |
| 73 |
* Since this program drives the lirc interface, use the same terminology |
| 74 |
*/ |
| 75 |
struct send { |
| 76 |
struct send *next; |
| 77 |
const char *fname; |
| 78 |
enum send_ty ty; |
| 79 |
union { |
| 80 |
struct { |
| 81 |
unsigned carrier; |
| 82 |
unsigned len; |
| 83 |
unsigned buf[LIRCBUF_SIZE]; |
| 84 |
}; |
| 85 |
struct { |
| 86 |
unsigned scancode; |
| 87 |
unsigned protocol; |
| 88 |
}; |
| 89 |
char keycode[1]; |
| 90 |
unsigned gap; |
| 91 |
}; |
| 92 |
}; |
| 93 |
|
| 94 |
struct arguments { |
| 95 |
char *device; |
| 96 |
bool features; |
| 97 |
bool receive; |
| 98 |
bool verbose; |
| 99 |
bool mode2; |
| 100 |
struct keymap *keymap; |
| 101 |
struct send *send; |
| 102 |
bool oneshot; |
| 103 |
int wideband; |
| 104 |
unsigned carrier_low, carrier_high; |
| 105 |
unsigned timeout; |
| 106 |
unsigned gap; |
| 107 |
int carrier_reports; |
| 108 |
unsigned carrier; |
| 109 |
unsigned duty; |
| 110 |
unsigned emitters; |
| 111 |
bool work_to_do; |
| 112 |
}; |
| 113 |
|
| 114 |
static const struct argp_option options[] = { |
| 115 |
{ "device", 'd', N_("DEV"), 0, N_("lirc device to use") }, |
| 116 |
{ "features", 'f', 0, 0, N_("list lirc device features") }, |
| 117 |
{ "receive", 'r', 0, 0, N_("receive IR to stdout") }, |
| 118 |
{ "send", 's', N_("FILE"), 0, N_("send IR pulse and space file") }, |
| 119 |
{ "scancode", 'S', N_("SCANCODE"), 0, N_("send IR scancode in protocol specified") }, |
| 120 |
{ "keycode", 'K', N_("KEYCODE"), 0, N_("send IR keycode from keymap") }, |
| 121 |
{ "verbose", 'v', 0, 0, N_("verbose output") }, |
| 122 |
{ .doc = N_("Receiving options:") }, |
| 123 |
{ "one-shot", '1', 0, 0, N_("end receiving after first message") }, |
| 124 |
{ "mode2", 2, 0, 0, N_("output in mode2 format") }, |
| 125 |
{ "wideband", 'w', 0, 0, N_("use wideband receiver aka learning mode") }, |
| 126 |
{ "narrowband", 'n', 0, 0, N_("use narrowband receiver, disable learning mode") }, |
| 127 |
{ "carrier-range", 'R', N_("RANGE"), 0, N_("set receiver carrier range") }, |
| 128 |
{ "measure-carrier", 'm', 0, 0, N_("report carrier frequency") }, |
| 129 |
{ "no-measure-carrier", 'M', 0, 0, N_("disable reporting carrier frequency") }, |
| 130 |
{ "timeout", 't', N_("TIMEOUT"), 0, N_("set receiving timeout") }, |
| 131 |
{ .doc = N_("Sending options:") }, |
| 132 |
{ "keymap", 'k', N_("KEYMAP"), 0, N_("use keymap to send key from") }, |
| 133 |
{ "carrier", 'c', N_("CARRIER"), 0, N_("set send carrier") }, |
| 134 |
{ "duty-cycle", 'D', N_("DUTY"), 0, N_("set send duty cycle") }, |
| 135 |
{ "emitters", 'e', N_("EMITTERS"), 0, N_("set send emitters") }, |
| 136 |
{ "gap", 'g', N_("GAP"), 0, N_("set gap between files or scancodes") }, |
| 137 |
{ } |
| 138 |
}; |
| 139 |
|
| 140 |
static const char args_doc[] = N_( |
| 141 |
"--features\n" |
| 142 |
"--receive\n" |
| 143 |
"--send [file to send]\n" |
| 144 |
"--scancode [scancode to send]\n" |
| 145 |
"--keycode [keycode to send]\n" |
| 146 |
"[to set lirc option]"); |
| 147 |
|
| 148 |
static const char doc[] = N_( |
| 149 |
"\nReceive IR, send IR and list features of lirc device\n" |
| 150 |
"You will need permission on /dev/lirc for the program to work\n" |
| 151 |
"\nOn the options below, the arguments are:\n" |
| 152 |
" DEV - the /dev/lirc* device to use\n" |
| 153 |
" FILE - a text file containing pulses and spaces\n" |
| 154 |
" CARRIER - the carrier frequency to use for sending\n" |
| 155 |
" DUTY - the duty cycle to use for sending\n" |
| 156 |
" EMITTERS - comma separated list of emitters to use for sending, e.g. 1,2\n" |
| 157 |
" GAP - gap between sending in microseconds\n" |
| 158 |
" RANGE - set range of accepted carrier frequencies, e.g. 20000-40000\n" |
| 159 |
" TIMEOUT - set length of space before receiving stops in microseconds\n" |
| 160 |
" KEYCODE - key code in keymap\n" |
| 161 |
" SCANCODE - protocol:scancode, e.g. nec:0xa814\n" |
| 162 |
" KEYMAP - a rc keymap file from which to send keys\n\n" |
| 163 |
"Note that most lirc setting have global state, i.e. the device will remain\n" |
| 164 |
"in this state until set otherwise."); |
| 165 |
|
| 166 |
static bool strtoint(const char *p, const char *unit, unsigned *ret) |
| 167 |
{ |
| 168 |
char *end; |
| 169 |
long arg = strtol(p, &end, 10); |
| 170 |
if (end == NULL || (end[0] != 0 && strcasecmp(end, unit) != 0)) |
| 171 |
return false; |
| 172 |
|
| 173 |
if (arg < 0 || arg >= 0xffffff) |
| 174 |
return false; |
| 175 |
|
| 176 |
*ret = arg; |
| 177 |
return true; |
| 178 |
} |
| 179 |
|
| 180 |
static bool strtoscancode(const char *p, unsigned *ret) |
| 181 |
{ |
| 182 |
char *end; |
| 183 |
long long arg = strtoll(p, &end, 0); |
| 184 |
if (end == NULL || end[0] != 0) |
| 185 |
return false; |
| 186 |
|
| 187 |
if (arg < 0 || arg > 0xffffffff) |
| 188 |
return false; |
| 189 |
|
| 190 |
*ret = arg; |
| 191 |
return true; |
| 192 |
} |
| 193 |
|
| 194 |
static unsigned parse_emitters(char *p) |
| 195 |
{ |
| 196 |
unsigned emit = 0; |
| 197 |
static const char *sep = " ,;:"; |
| 198 |
char *saveptr, *q; |
| 199 |
|
| 200 |
q = strtok_r(p, sep, &saveptr); |
| 201 |
while (q) { |
| 202 |
char *endptr; |
| 203 |
long e = strtol(q, &endptr, 10); |
| 204 |
if ((endptr && *endptr) || e <= 0 || e > 32) |
| 205 |
return 0; |
| 206 |
|
| 207 |
emit |= 1 << (e - 1); |
| 208 |
q = strtok_r(NULL, sep, &saveptr); |
| 209 |
} |
| 210 |
|
| 211 |
return emit; |
| 212 |
} |
| 213 |
|
| 214 |
static struct send *read_file_pulse_space(struct arguments *args, const char *fname, FILE *input) |
| 215 |
{ |
| 216 |
bool expect_pulse = true; |
| 217 |
int lineno = 0, lastspace = 0; |
| 218 |
char *line = NULL; |
| 219 |
size_t line_size; |
| 220 |
int len = 0; |
| 221 |
static const char whitespace[] = " \n\r\t"; |
| 222 |
struct send *f; |
| 223 |
|
| 224 |
f = malloc(sizeof(*f)); |
| 225 |
if (f == NULL) { |
| 226 |
fprintf(stderr, _("Failed to allocate memory\n")); |
| 227 |
return NULL; |
| 228 |
} |
| 229 |
f->ty = SEND_RAW; |
| 230 |
f->carrier = UNSET; |
| 231 |
f->fname = fname; |
| 232 |
|
| 233 |
while (getline(&line, &line_size, input) > 0) { |
| 234 |
char *p, *saveptr; |
| 235 |
lineno++; |
| 236 |
char *keyword = strtok_r(line, whitespace, &saveptr); |
| 237 |
|
| 238 |
if (keyword == NULL || *keyword == 0 || *keyword == '#' || |
| 239 |
(keyword[0] == '/' && keyword[1] == '/')) |
| 240 |
continue; |
| 241 |
|
| 242 |
p = strtok_r(NULL, whitespace, &saveptr); |
| 243 |
if (p == NULL) { |
| 244 |
fprintf(stderr, _("warning: %s:%d: missing argument\n"), fname, lineno); |
| 245 |
continue; |
| 246 |
} |
| 247 |
|
| 248 |
if (strcmp(keyword, "scancode") == 0) { |
| 249 |
enum rc_proto proto; |
| 250 |
unsigned scancode, carrier; |
| 251 |
char *scancodestr; |
| 252 |
|
| 253 |
if (!expect_pulse) { |
| 254 |
f->buf[len++] = args->gap; |
| 255 |
expect_pulse = true; |
| 256 |
} |
| 257 |
|
| 258 |
scancodestr = strchr(p, ':'); |
| 259 |
if (!scancodestr) { |
| 260 |
fprintf(stderr, _("error: %s:%d: scancode argument '%s' should be in protocol:scancode format\n"), fname, lineno, p); |
| 261 |
fclose(input); |
| 262 |
free(f); |
| 263 |
return NULL; |
| 264 |
} |
| 265 |
|
| 266 |
*scancodestr++ = 0; |
| 267 |
|
| 268 |
if (!protocol_match(p, &proto)) { |
| 269 |
fprintf(stderr, _("error: %s:%d: protocol '%s' not found\n"), fname, lineno, p); |
| 270 |
fclose(input); |
| 271 |
free(f); |
| 272 |
return NULL; |
| 273 |
} |
| 274 |
|
| 275 |
if (!strtoscancode(scancodestr, &scancode)) { |
| 276 |
fprintf(stderr, _("error: %s:%d: invalid scancode '%s'\n"), fname, lineno, scancodestr); |
| 277 |
fclose(input); |
| 278 |
free(f); |
| 279 |
return NULL; |
| 280 |
} |
| 281 |
|
| 282 |
if (!protocol_encoder_available(proto)) { |
| 283 |
fprintf(stderr, _("error: %s:%d: no encoder available for `%s'\n"), fname, lineno, protocol_name(proto)); |
| 284 |
fclose(input); |
| 285 |
free(f); |
| 286 |
return NULL; |
| 287 |
} |
| 288 |
|
| 289 |
protocol_scancode_valid(&proto, &scancode); |
| 290 |
|
| 291 |
if (len + protocol_max_size(proto) >= LIRCBUF_SIZE) { |
| 292 |
fprintf(stderr, _("error: %s:%d: too much IR for one transmit\n"), fname, lineno); |
| 293 |
fclose(input); |
| 294 |
free(f); |
| 295 |
return NULL; |
| 296 |
} |
| 297 |
|
| 298 |
carrier = protocol_carrier(proto); |
| 299 |
if (f->carrier && f->carrier != carrier) |
| 300 |
fprintf(stderr, _("error: %s:%d: carrier already specified\n"), fname, lineno); |
| 301 |
else |
| 302 |
f->carrier = carrier; |
| 303 |
|
| 304 |
len += protocol_encode(proto, scancode, f->buf + len); |
| 305 |
expect_pulse = false; |
| 306 |
continue; |
| 307 |
} |
| 308 |
|
| 309 |
unsigned int arg; |
| 310 |
if (!strtoint(p, "", &arg)) { |
| 311 |
fprintf(stderr, _("warning: %s:%d: invalid argument '%s'\n"), fname, lineno, p); |
| 312 |
continue; |
| 313 |
} |
| 314 |
|
| 315 |
p = strtok_r(NULL, whitespace, &saveptr); |
| 316 |
if (p && p[0] != '#' && !(p[0] == '/' && p[1] == '/')) { |
| 317 |
fprintf(stderr, _("warning: %s:%d: '%s' unexpected\n"), fname, lineno, p); |
| 318 |
continue; |
| 319 |
} |
| 320 |
|
| 321 |
if (!strcmp(keyword, "space") || !strcmp(keyword, "timeout")) { |
| 322 |
if (arg == 0) { |
| 323 |
fprintf(stderr, _("warning: %s:%d: invalid argument to space '%d'\n"), fname, lineno, arg); |
| 324 |
continue; |
| 325 |
} |
| 326 |
if (expect_pulse) { |
| 327 |
if (len == 0) { |
| 328 |
fprintf(stderr, _("warning: %s:%d: leading space ignored\n"), |
| 329 |
fname, lineno); |
| 330 |
} else { |
| 331 |
f->buf[len-1] += arg; |
| 332 |
} |
| 333 |
} else { |
| 334 |
f->buf[len++] = arg; |
| 335 |
} |
| 336 |
lastspace = lineno; |
| 337 |
expect_pulse = true; |
| 338 |
} else if (!strcmp(keyword, "pulse")) { |
| 339 |
if (arg == 0) { |
| 340 |
fprintf(stderr, _("warning: %s:%d: invalid argument to pulse '%d'\n"), fname, lineno, arg); |
| 341 |
continue; |
| 342 |
} |
| 343 |
if (!expect_pulse) |
| 344 |
f->buf[len-1] += arg; |
| 345 |
else |
| 346 |
f->buf[len++] = arg; |
| 347 |
expect_pulse = false; |
| 348 |
} else if (!strcmp(keyword, "carrier")) { |
| 349 |
if (f->carrier != UNSET && f->carrier != arg) { |
| 350 |
fprintf(stderr, _("warning: %s:%d: carrier already specified\n"), fname, lineno); |
| 351 |
} else { |
| 352 |
f->carrier = arg; |
| 353 |
} |
| 354 |
} else { |
| 355 |
fprintf(stderr, _("warning: %s:%d: unknown keyword '%s' ignored\n"), fname, lineno, keyword); |
| 356 |
continue; |
| 357 |
} |
| 358 |
|
| 359 |
if (len >= LIRCBUF_SIZE) { |
| 360 |
fprintf(stderr, _("warning: %s:%d: IR cannot exceed %u edges\n"), fname, lineno, LIRCBUF_SIZE); |
| 361 |
break; |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
free(line); |
| 366 |
fclose(input); |
| 367 |
|
| 368 |
if (len == 0) { |
| 369 |
fprintf(stderr, _("%s: no pulses or spaces found\n"), fname); |
| 370 |
free(f); |
| 371 |
return NULL; |
| 372 |
} |
| 373 |
|
| 374 |
if ((len % 2) == 0) { |
| 375 |
fprintf(stderr, _("warning: %s:%d: trailing space ignored\n"), |
| 376 |
fname, lastspace); |
| 377 |
len--; |
| 378 |
} |
| 379 |
|
| 380 |
f->len = len; |
| 381 |
|
| 382 |
return f; |
| 383 |
} |
| 384 |
|
| 385 |
static struct send *read_file_raw(struct arguments *args, const char *fname, FILE *input) |
| 386 |
{ |
| 387 |
int lineno = 0, lastspace = 0; |
| 388 |
char *line = NULL; |
| 389 |
size_t line_size; |
| 390 |
int len = 0; |
| 391 |
static const char whitespace[] = " \n\r\t,"; |
| 392 |
struct send *f; |
| 393 |
|
| 394 |
f = malloc(sizeof(*f)); |
| 395 |
if (f == NULL) { |
| 396 |
fprintf(stderr, _("Failed to allocate memory\n")); |
| 397 |
fclose(input); |
| 398 |
return NULL; |
| 399 |
} |
| 400 |
f->ty = SEND_RAW; |
| 401 |
f->carrier = UNSET; |
| 402 |
f->fname = fname; |
| 403 |
|
| 404 |
while (getline(&line, &line_size, input) > 0) { |
| 405 |
long int value; |
| 406 |
char *p, *saveptr; |
| 407 |
lineno++; |
| 408 |
char *keyword = strtok_r(line, whitespace, &saveptr); |
| 409 |
|
| 410 |
for (;;) { |
| 411 |
if (keyword == NULL || *keyword == 0 || *keyword == '#' || |
| 412 |
(keyword[0] == '/' && keyword[1] == '/')) |
| 413 |
break; |
| 414 |
|
| 415 |
errno = 0; |
| 416 |
value = strtol(keyword, &p, 10); |
| 417 |
if (errno || *p) { |
| 418 |
fprintf(stderr, _("%s:%d: error: expected integer, got `%s'\n"), |
| 419 |
fname, lineno, keyword); |
| 420 |
fclose(input); |
| 421 |
free(f); |
| 422 |
return NULL; |
| 423 |
} |
| 424 |
|
| 425 |
if (len % 2) { |
| 426 |
if (keyword[0] == '+') { |
| 427 |
fprintf(stderr, _("%s:%d: error: pulse found where space expected `%s'\n"), fname, lineno, keyword); |
| 428 |
free(f); |
| 429 |
return NULL; |
| 430 |
} |
| 431 |
if (keyword[0] == '-') |
| 432 |
value = -value; |
| 433 |
} else { |
| 434 |
if (keyword[0] == '-') { |
| 435 |
fprintf(stderr, _("%s:%d: error: space found where pulse expected `%s'\n"), fname, lineno, keyword); |
| 436 |
fclose(input); |
| 437 |
free(f); |
| 438 |
return NULL; |
| 439 |
} |
| 440 |
lastspace = lineno; |
| 441 |
} |
| 442 |
|
| 443 |
if (value <= 0 || value >= LIRC_VALUE_MASK) { |
| 444 |
fprintf(stderr, _("%s:%d: error: value `%s' out of range\n"), fname, lineno, keyword); |
| 445 |
fclose(input); |
| 446 |
free(f); |
| 447 |
return NULL; |
| 448 |
} |
| 449 |
|
| 450 |
f->buf[len++] = value; |
| 451 |
|
| 452 |
if (len >= LIRCBUF_SIZE) { |
| 453 |
fprintf(stderr, _("warning: %s:%d: IR cannot exceed %u edges\n"), fname, lineno, LIRCBUF_SIZE); |
| 454 |
break; |
| 455 |
} |
| 456 |
|
| 457 |
keyword = strtok_r(NULL, whitespace, &saveptr); |
| 458 |
} |
| 459 |
} |
| 460 |
|
| 461 |
free(line); |
| 462 |
fclose(input); |
| 463 |
|
| 464 |
if (len == 0) { |
| 465 |
fprintf(stderr, _("%s: no pulses or spaces found\n"), fname); |
| 466 |
free(f); |
| 467 |
return NULL; |
| 468 |
} |
| 469 |
|
| 470 |
if ((len % 2) == 0) { |
| 471 |
fprintf(stderr, _("warning: %s:%d: trailing space ignored\n"), |
| 472 |
fname, lastspace); |
| 473 |
len--; |
| 474 |
} |
| 475 |
|
| 476 |
f->len = len; |
| 477 |
|
| 478 |
return f; |
| 479 |
} |
| 480 |
|
| 481 |
static struct send *read_file(struct arguments *args, const char *fname) |
| 482 |
{ |
| 483 |
FILE *input = fopen(fname, "r"); |
| 484 |
char *line = NULL; |
| 485 |
size_t line_size; |
| 486 |
|
| 487 |
if (!input) { |
| 488 |
fprintf(stderr, _("%s: could not open: %m\n"), fname); |
| 489 |
return NULL; |
| 490 |
} |
| 491 |
|
| 492 |
while (getline(&line, &line_size, input) > 0) { |
| 493 |
int start = 0; |
| 494 |
|
| 495 |
while (isspace(line[start])) |
| 496 |
start++; |
| 497 |
|
| 498 |
switch (line[start]) { |
| 499 |
case '/': |
| 500 |
if (line[start+1] != '/') |
| 501 |
break; |
| 502 |
case 0: |
| 503 |
case '#': |
| 504 |
continue; |
| 505 |
case '+': |
| 506 |
case '-': |
| 507 |
case '0' ... '9': |
| 508 |
rewind(input); |
| 509 |
return read_file_raw(args, fname, input); |
| 510 |
default: |
| 511 |
rewind(input); |
| 512 |
return read_file_pulse_space(args, fname, input); |
| 513 |
} |
| 514 |
} |
| 515 |
|
| 516 |
free(line); |
| 517 |
fclose(input); |
| 518 |
|
| 519 |
fprintf(stderr, _("%s: file is empty\n"), fname); |
| 520 |
|
| 521 |
return NULL; |
| 522 |
} |
| 523 |
|
| 524 |
static struct send *read_scancode(const char *name) |
| 525 |
{ |
| 526 |
enum rc_proto proto; |
| 527 |
struct send *f; |
| 528 |
unsigned scancode; |
| 529 |
char *pstr; |
| 530 |
char *p = strchr(name, ':'); |
| 531 |
|
| 532 |
if (!p) { |
| 533 |
fprintf(stderr, _("error: scancode '%s' must be in protocol:scancode format\n"), name); |
| 534 |
return NULL; |
| 535 |
} |
| 536 |
|
| 537 |
pstr = strndup(name, p - name); |
| 538 |
|
| 539 |
if (!protocol_match(pstr, &proto)) { |
| 540 |
fprintf(stderr, _("error: protocol '%s' not found\n"), pstr); |
| 541 |
free(pstr); |
| 542 |
return NULL; |
| 543 |
} |
| 544 |
free(pstr); |
| 545 |
|
| 546 |
if (!strtoscancode(p + 1, &scancode)) { |
| 547 |
fprintf(stderr, _("error: invalid scancode '%s'\n"), p + 1); |
| 548 |
return NULL; |
| 549 |
} |
| 550 |
|
| 551 |
protocol_scancode_valid(&proto, &scancode); |
| 552 |
|
| 553 |
f = malloc(sizeof(*f)); |
| 554 |
if (f == NULL) { |
| 555 |
fprintf(stderr, _("Failed to allocate memory\n")); |
| 556 |
return NULL; |
| 557 |
} |
| 558 |
|
| 559 |
f->ty = SEND_SCANCODE; |
| 560 |
f->scancode = scancode; |
| 561 |
f->protocol = proto; |
| 562 |
|
| 563 |
return f; |
| 564 |
} |
| 565 |
|
| 566 |
static void add_to_send_list(struct arguments *arguments, struct send *send) |
| 567 |
{ |
| 568 |
send->next = NULL; |
| 569 |
|
| 570 |
if (arguments->send == NULL) |
| 571 |
arguments->send = send; |
| 572 |
else { |
| 573 |
// introduce gap |
| 574 |
struct send *gap = malloc(sizeof(*gap)); |
| 575 |
gap->ty = SEND_GAP; |
| 576 |
gap->fname= NULL; |
| 577 |
gap->gap = arguments->gap; |
| 578 |
gap->next = send; |
| 579 |
|
| 580 |
struct send *p = arguments->send; |
| 581 |
while (p->next) p = p->next; |
| 582 |
p->next = gap; |
| 583 |
} |
| 584 |
} |
| 585 |
|
| 586 |
static error_t parse_opt(int k, char *arg, struct argp_state *state) |
| 587 |
{ |
| 588 |
struct arguments *arguments = state->input; |
| 589 |
struct keymap *map; |
| 590 |
struct send *s; |
| 591 |
|
| 592 |
switch (k) { |
| 593 |
case 'f': |
| 594 |
if (arguments->receive || arguments->send) |
| 595 |
argp_error(state, _("features can not be combined with receive or send option")); |
| 596 |
arguments->features = true; |
| 597 |
break; |
| 598 |
// receiving |
| 599 |
case 'r': |
| 600 |
if (arguments->features || arguments->send) |
| 601 |
argp_error(state, _("receive can not be combined with features or send option")); |
| 602 |
|
| 603 |
arguments->receive = true; |
| 604 |
break; |
| 605 |
case '1': |
| 606 |
arguments->oneshot = true; |
| 607 |
break; |
| 608 |
case 2: |
| 609 |
arguments->mode2 = true; |
| 610 |
break; |
| 611 |
case 'v': |
| 612 |
arguments->verbose = true; |
| 613 |
break; |
| 614 |
case 'm': |
| 615 |
if (arguments->carrier_reports == 2) |
| 616 |
argp_error(state, _("cannot enable and disable carrier reports")); |
| 617 |
|
| 618 |
arguments->carrier_reports = 1; |
| 619 |
break; |
| 620 |
case 'M': |
| 621 |
if (arguments->carrier_reports == 1) |
| 622 |
argp_error(state, _("cannot enable and disable carrier reports")); |
| 623 |
|
| 624 |
arguments->carrier_reports = 2; |
| 625 |
break; |
| 626 |
case 'n': |
| 627 |
if (arguments->wideband) |
| 628 |
argp_error(state, _("cannot use narrowband and wideband receiver at once")); |
| 629 |
|
| 630 |
arguments->wideband = 2; |
| 631 |
break; |
| 632 |
case 'w': |
| 633 |
if (arguments->wideband) |
| 634 |
argp_error(state, _("cannot use narrowband and wideband receiver at once")); |
| 635 |
|
| 636 |
arguments->wideband = 1; |
| 637 |
break; |
| 638 |
case 'R': { |
| 639 |
long low, high; |
| 640 |
char *end; |
| 641 |
|
| 642 |
low = strtol(arg, &end, 10); |
| 643 |
if (end == NULL || end[0] != '-') |
| 644 |
argp_error(state, _("cannot parse carrier range `%s'"), arg); |
| 645 |
high = strtol(end + 1, &end, 10); |
| 646 |
if (end[0] != 0 || low <= 0 || low >= high || high > 1000000) |
| 647 |
argp_error(state, _("cannot parse carrier range `%s'"), arg); |
| 648 |
|
| 649 |
arguments->carrier_low = low; |
| 650 |
arguments->carrier_high = high; |
| 651 |
break; |
| 652 |
} |
| 653 |
case 't': |
| 654 |
if (!strtoint(arg, "µs", &arguments->timeout)) |
| 655 |
argp_error(state, _("cannot parse timeout `%s'"), arg); |
| 656 |
break; |
| 657 |
|
| 658 |
// sending |
| 659 |
case 'd': |
| 660 |
arguments->device = arg; |
| 661 |
break; |
| 662 |
case 'c': |
| 663 |
if (!strtoint(arg, "Hz", &arguments->carrier)) |
| 664 |
argp_error(state, _("cannot parse carrier `%s'"), arg); |
| 665 |
break; |
| 666 |
case 'e': |
| 667 |
arguments->emitters = parse_emitters(arg); |
| 668 |
if (arguments->emitters == 0) |
| 669 |
argp_error(state, _("cannot parse emitters `%s'"), arg); |
| 670 |
break; |
| 671 |
case 'g': |
| 672 |
if (!strtoint(arg, "", &arguments->gap) || arguments->gap == 0) |
| 673 |
argp_error(state, _("cannot parse gap `%s'"), arg); |
| 674 |
break; |
| 675 |
case 'D': |
| 676 |
if (!strtoint(arg, "%", &arguments->duty) || |
| 677 |
arguments->duty == 0 || arguments->duty >= 100) |
| 678 |
argp_error(state, _("invalid duty cycle `%s'"), arg); |
| 679 |
break; |
| 680 |
case 's': |
| 681 |
if (arguments->receive || arguments->features) |
| 682 |
argp_error(state, _("send can not be combined with receive or features option")); |
| 683 |
s = read_file(arguments, arg); |
| 684 |
if (s == NULL) |
| 685 |
exit(EX_DATAERR); |
| 686 |
|
| 687 |
add_to_send_list(arguments, s); |
| 688 |
break; |
| 689 |
case 'S': |
| 690 |
if (arguments->receive || arguments->features) |
| 691 |
argp_error(state, _("send can not be combined with receive or features option")); |
| 692 |
s = read_scancode(arg); |
| 693 |
if (s == NULL) |
| 694 |
exit(EX_DATAERR); |
| 695 |
|
| 696 |
add_to_send_list(arguments, s); |
| 697 |
break; |
| 698 |
|
| 699 |
case 'K': |
| 700 |
if (arguments->receive || arguments->features) |
| 701 |
argp_error(state, _("key send can not be combined with receive or features option")); |
| 702 |
s = malloc(sizeof(*s) + strlen(arg)); |
| 703 |
if (s == NULL) |
| 704 |
exit(EX_DATAERR); |
| 705 |
strcpy(s->keycode, arg); |
| 706 |
s->ty = SEND_KEYCODE; |
| 707 |
|
| 708 |
add_to_send_list(arguments, s); |
| 709 |
break; |
| 710 |
|
| 711 |
case 'k': |
| 712 |
if (parse_keymap(arg, &map, arguments->verbose)) |
| 713 |
exit(EX_DATAERR); |
| 714 |
if (arguments->keymap == NULL) |
| 715 |
arguments->keymap = map; |
| 716 |
else { |
| 717 |
// add to end of list of keymaps |
| 718 |
struct keymap *p = arguments->keymap; |
| 719 |
while (p->next) p = p->next; |
| 720 |
p->next = map; |
| 721 |
} |
| 722 |
break; |
| 723 |
|
| 724 |
case ARGP_KEY_END: |
| 725 |
if (!arguments->work_to_do) |
| 726 |
argp_usage(state); |
| 727 |
|
| 728 |
break; |
| 729 |
default: |
| 730 |
return ARGP_ERR_UNKNOWN; |
| 731 |
} |
| 732 |
|
| 733 |
if (k != '1' && k != 'd' && k != 'v' && k != 'k' && k != 2) |
| 734 |
arguments->work_to_do = true; |
| 735 |
|
| 736 |
return 0; |
| 737 |
} |
| 738 |
|
| 739 |
static struct send* convert_keycode(struct keymap *map, const char *keycode) |
| 740 |
{ |
| 741 |
struct send *s = NULL; |
| 742 |
int count = 0; |
| 743 |
|
| 744 |
for (;map; map = map->next) { |
| 745 |
struct scancode_entry *se; |
| 746 |
struct raw_entry *re; |
| 747 |
|
| 748 |
for (re = map->raw; re; re = re->next) { |
| 749 |
if (strcmp(re->keycode, keycode)) |
| 750 |
continue; |
| 751 |
|
| 752 |
count++; |
| 753 |
|
| 754 |
if (!s) { |
| 755 |
s = malloc(sizeof(*s) + re->raw_length * sizeof(int)); |
| 756 |
s->len = re->raw_length; |
| 757 |
memcpy(s->buf, re->raw, s->len * sizeof(int)); |
| 758 |
s->ty = SEND_RAW; |
| 759 |
s->carrier = keymap_param(map, "carrier", 0); |
| 760 |
s->next = NULL; |
| 761 |
} |
| 762 |
} |
| 763 |
|
| 764 |
for (se = map->scancode; se; se = se->next) { |
| 765 |
int buf[LIRCBUF_SIZE], length; |
| 766 |
const char *proto_str; |
| 767 |
enum rc_proto proto; |
| 768 |
|
| 769 |
if (strcmp(se->keycode, keycode)) |
| 770 |
continue; |
| 771 |
|
| 772 |
count++; |
| 773 |
|
| 774 |
if (s) |
| 775 |
continue; |
| 776 |
|
| 777 |
proto_str = map->variant ?: map->protocol; |
| 778 |
|
| 779 |
if (protocol_match(proto_str, &proto)) { |
| 780 |
s = malloc(sizeof(*s)); |
| 781 |
s->protocol = proto; |
| 782 |
s->scancode = se->scancode; |
| 783 |
s->ty = SEND_SCANCODE; |
| 784 |
s->next = NULL; |
| 785 |
} else if (encode_bpf_protocol(map, se->scancode, |
| 786 |
buf, &length)) { |
| 787 |
s = malloc(sizeof(*s) + sizeof(int) * length); |
| 788 |
s->len = length; |
| 789 |
memcpy(s->buf, buf, length * sizeof(int)); |
| 790 |
s->ty = SEND_RAW; |
| 791 |
s->carrier = keymap_param(map, "carrier", 0); |
| 792 |
s->next = NULL; |
| 793 |
} else { |
| 794 |
fprintf(stderr, _("error: protocol '%s' not supported\n"), proto_str); |
| 795 |
return NULL; |
| 796 |
} |
| 797 |
} |
| 798 |
} |
| 799 |
|
| 800 |
if (!s) { |
| 801 |
fprintf(stderr, _("error: keycode `%s' not found in keymap\n"), keycode); |
| 802 |
return NULL; |
| 803 |
} |
| 804 |
|
| 805 |
if (count > 1) |
| 806 |
fprintf(stderr, _("warning: keycode `%s' has %d definitions in keymaps, using first\n"), keycode, count); |
| 807 |
|
| 808 |
return s; |
| 809 |
} |
| 810 |
|
| 811 |
static const struct argp argp = { |
| 812 |
.options = options, |
| 813 |
.parser = parse_opt, |
| 814 |
.args_doc = args_doc, |
| 815 |
.doc = doc |
| 816 |
}; |
| 817 |
|
| 818 |
static int open_lirc(const char *fname, unsigned *features) |
| 819 |
{ |
| 820 |
int fd; |
| 821 |
|
| 822 |
fd = TEMP_FAILURE_RETRY(open(fname, O_RDWR | O_CLOEXEC)); |
| 823 |
if (fd == -1) { |
| 824 |
fprintf(stderr, _("%s: cannot open: %m\n"), fname); |
| 825 |
return -1; |
| 826 |
} |
| 827 |
|
| 828 |
struct stat st; |
| 829 |
int rc = TEMP_FAILURE_RETRY(fstat(fd, &st)); |
| 830 |
if (rc) { |
| 831 |
fprintf(stderr, _("%s: cannot stat: %m\n"), fname); |
| 832 |
close(fd); |
| 833 |
return -1; |
| 834 |
} |
| 835 |
|
| 836 |
if ((st.st_mode & S_IFMT) != S_IFCHR) { |
| 837 |
fprintf(stderr, _("%s: not character device\n"), fname); |
| 838 |
close(fd); |
| 839 |
return -1; |
| 840 |
} |
| 841 |
|
| 842 |
rc = ioctl(fd, LIRC_GET_FEATURES, features); |
| 843 |
if (rc) { |
| 844 |
fprintf(stderr, _("%s: failed to get lirc features: %m\n"), fname); |
| 845 |
close(fd); |
| 846 |
return -1; |
| 847 |
} |
| 848 |
|
| 849 |
return fd; |
| 850 |
} |
| 851 |
|
| 852 |
static void lirc_set_send_carrier(int fd, const char *devname, unsigned features, unsigned carrier) |
| 853 |
{ |
| 854 |
if (features & LIRC_CAN_SET_SEND_CARRIER) { |
| 855 |
int rc = ioctl(fd, LIRC_SET_SEND_CARRIER, &carrier); |
| 856 |
if (rc < 0) |
| 857 |
fprintf(stderr, _("warning: %s: failed to set carrier: %m\n"), devname); |
| 858 |
if (rc != 0) |
| 859 |
fprintf(stderr, _("warning: %s: set send carrier returned %d, should return 0\n"), devname, rc); |
| 860 |
} else |
| 861 |
fprintf(stderr, _("warning: %s: does not support setting send carrier\n"), devname); |
| 862 |
} |
| 863 |
|
| 864 |
static int lirc_options(struct arguments *args, int fd, unsigned features) |
| 865 |
{ |
| 866 |
const char *dev = args->device; |
| 867 |
int rc; |
| 868 |
|
| 869 |
if (args->timeout != UNSET) { |
| 870 |
if (features & LIRC_CAN_SET_REC_TIMEOUT) { |
| 871 |
rc = ioctl(fd, LIRC_SET_REC_TIMEOUT, &args->timeout); |
| 872 |
if (rc) |
| 873 |
fprintf(stderr, _("%s: failed to set receiving timeout\n"), dev); |
| 874 |
} else |
| 875 |
fprintf(stderr, _("%s: device does not support setting timeout\n"), dev); |
| 876 |
} |
| 877 |
|
| 878 |
if (args->wideband) { |
| 879 |
unsigned on = args->wideband == 1; |
| 880 |
if (features & LIRC_CAN_USE_WIDEBAND_RECEIVER) { |
| 881 |
rc = ioctl(fd, LIRC_SET_WIDEBAND_RECEIVER, &on); |
| 882 |
if (rc) |
| 883 |
fprintf(stderr, _("%s: failed to set wideband receiver %s\n"), dev, on ? _("on") : _("off")); |
| 884 |
} else |
| 885 |
fprintf(stderr, _("%s: device does not have wideband receiver\n"), dev); |
| 886 |
} |
| 887 |
|
| 888 |
if (args->carrier_reports) { |
| 889 |
unsigned on = args->carrier_reports == 1; |
| 890 |
if (features & LIRC_CAN_MEASURE_CARRIER) { |
| 891 |
rc = ioctl(fd, LIRC_SET_MEASURE_CARRIER_MODE, &on); |
| 892 |
if (rc) |
| 893 |
fprintf(stderr, _("%s: failed to set carrier reports %s\n"), dev, on ? _("on") : _("off")); |
| 894 |
} else |
| 895 |
fprintf(stderr, _("%s: device cannot measure carrier\n"), dev); |
| 896 |
} |
| 897 |
|
| 898 |
if (features & LIRC_CAN_REC_MODE2) { |
| 899 |
unsigned on = 1; |
| 900 |
rc = ioctl(fd, LIRC_SET_REC_TIMEOUT_REPORTS, &on); |
| 901 |
if (rc) |
| 902 |
fprintf(stderr, _("%s: failed to enable timeout reports: %m\n"), dev); |
| 903 |
} |
| 904 |
|
| 905 |
if (args->carrier_low) { |
| 906 |
if (features & LIRC_CAN_SET_REC_CARRIER_RANGE) { |
| 907 |
rc = ioctl(fd, LIRC_SET_REC_CARRIER_RANGE, &args->carrier_low); |
| 908 |
if (rc) |
| 909 |
fprintf(stderr, _("%s: failed to set low carrier range: %m\n"), dev); |
| 910 |
rc = ioctl(fd, LIRC_SET_REC_CARRIER, &args->carrier_high); |
| 911 |
if (rc) |
| 912 |
fprintf(stderr, _("%s: failed to set high carrier range: %m\n"), dev); |
| 913 |
} else |
| 914 |
fprintf(stderr, _("%s: device does not support setting receiver carrier range\n"), dev); |
| 915 |
} |
| 916 |
|
| 917 |
if (args->carrier != UNSET) |
| 918 |
lirc_set_send_carrier(fd, dev, features, args->carrier); |
| 919 |
|
| 920 |
if (args->duty) { |
| 921 |
if (features & LIRC_CAN_SET_SEND_DUTY_CYCLE) { |
| 922 |
rc = ioctl(fd, LIRC_SET_SEND_DUTY_CYCLE, &args->duty); |
| 923 |
if (rc) |
| 924 |
fprintf(stderr, _("warning: %s: failed to set duty cycle: %m\n"), dev); |
| 925 |
} else |
| 926 |
fprintf(stderr, _("warning: %s: does not support setting send duty cycle\n"), dev); |
| 927 |
} |
| 928 |
|
| 929 |
if (args->emitters) { |
| 930 |
if (features & LIRC_CAN_SET_TRANSMITTER_MASK) { |
| 931 |
rc = ioctl(fd, LIRC_SET_TRANSMITTER_MASK, &args->emitters); |
| 932 |
if (rc > 0) |
| 933 |
fprintf(stderr, _("warning: %s: failed to set send transmitters: only %d available\n"), dev, rc); |
| 934 |
else if (rc < 0) |
| 935 |
fprintf(stderr, _("warning: %s: failed to set send transmitters: %m\n"), dev); |
| 936 |
} else |
| 937 |
fprintf(stderr, _("warning: %s: does not support setting send transmitters\n"), dev); |
| 938 |
} |
| 939 |
|
| 940 |
|
| 941 |
return 0; |
| 942 |
} |
| 943 |
|
| 944 |
static void lirc_features(struct arguments *args, int fd, unsigned features) |
| 945 |
{ |
| 946 |
const char *dev = args->device; |
| 947 |
unsigned resolution = 0, mode = LIRC_MODE_SCANCODE; |
| 948 |
int rc; |
| 949 |
|
| 950 |
if (features & LIRC_CAN_GET_REC_RESOLUTION) { |
| 951 |
rc = ioctl(fd, LIRC_GET_REC_RESOLUTION, &resolution); |
| 952 |
if (rc == 0 && resolution == 0) |
| 953 |
fprintf(stderr, _("warning: %s: device returned resolution of 0\n"), dev); |
| 954 |
else if (rc) |
| 955 |
fprintf(stderr, _("warning: %s: unexpected error while retrieving resolution: %m\n"), dev); |
| 956 |
} |
| 957 |
|
| 958 |
printf(_("Receive features %s:\n"), dev); |
| 959 |
if (features & LIRC_CAN_REC_SCANCODE) { |
| 960 |
printf(_(" - Device can receive scancodes\n")); |
| 961 |
} else if (features & LIRC_CAN_REC_MODE2) { |
| 962 |
printf(_(" - Device can receive raw IR\n")); |
| 963 |
if (ioctl(fd, LIRC_SET_REC_MODE, &mode) == 0) |
| 964 |
printf(_(" - Can report decoded scancodes and protocol\n")); |
| 965 |
if (resolution) |
| 966 |
printf(_(" - Resolution %u microseconds\n"), resolution); |
| 967 |
if (features & LIRC_CAN_SET_REC_CARRIER) |
| 968 |
printf(_(" - Set receive carrier\n")); |
| 969 |
if (features & LIRC_CAN_USE_WIDEBAND_RECEIVER) |
| 970 |
printf(_(" - Use wideband receiver\n")); |
| 971 |
if (features & LIRC_CAN_MEASURE_CARRIER) |
| 972 |
printf(_(" - Can measure carrier\n")); |
| 973 |
|
| 974 |
// This ioctl is only supported from kernel 4.18 onwards |
| 975 |
unsigned timeout; |
| 976 |
int rc = ioctl(fd, LIRC_GET_REC_TIMEOUT, &timeout); |
| 977 |
if (rc == 0) { |
| 978 |
if (timeout == 0) |
| 979 |
printf(_(" - Receiving timeout not set\n")); |
| 980 |
else |
| 981 |
printf(_(" - Receiving timeout %u microseconds\n"), timeout); |
| 982 |
} |
| 983 |
|
| 984 |
if (features & LIRC_CAN_SET_REC_TIMEOUT) { |
| 985 |
unsigned min_timeout, max_timeout; |
| 986 |
|
| 987 |
rc = ioctl(fd, LIRC_GET_MIN_TIMEOUT, &min_timeout); |
| 988 |
if (rc) { |
| 989 |
fprintf(stderr, _("warning: %s: device supports setting receiving timeout but LIRC_GET_MIN_TIMEOUT returns: %m\n"), dev); |
| 990 |
min_timeout = 0; |
| 991 |
} else if (min_timeout == 0) |
| 992 |
fprintf(stderr, _("warning: %s: device supports setting receiving timeout but min timeout is 0\n"), dev); |
| 993 |
rc = ioctl(fd, LIRC_GET_MAX_TIMEOUT, &max_timeout); |
| 994 |
if (rc) { |
| 995 |
fprintf(stderr, _("warning: %s: device supports setting receiving timeout but LIRC_GET_MAX_TIMEOUT returns: %m\n"), dev); |
| 996 |
max_timeout = 0; |
| 997 |
} else if (max_timeout == 0) { |
| 998 |
fprintf(stderr, _("warning: %s: device supports setting receiving timeout but max timeout is 0\n"), dev); |
| 999 |
} |
| 1000 |
|
| 1001 |
if (min_timeout || max_timeout) |
| 1002 |
printf(_(" - Can set receiving timeout min %u microseconds, max %u microseconds\n"), min_timeout, max_timeout); |
| 1003 |
} |
| 1004 |
} else if (features & LIRC_CAN_REC_LIRCCODE) { |
| 1005 |
printf(_(" - Device can receive using device dependent LIRCCODE mode (not supported)\n")); |
| 1006 |
} else { |
| 1007 |
printf(_(" - Device cannot receive\n")); |
| 1008 |
} |
| 1009 |
|
| 1010 |
printf(_("Send features %s:\n"), dev); |
| 1011 |
if (features & LIRC_CAN_SEND_PULSE) { |
| 1012 |
printf(_(" - Device can send raw IR\n")); |
| 1013 |
if (ioctl(fd, LIRC_SET_SEND_MODE, &mode) == 0) |
| 1014 |
printf(_(" - IR scancode encoder\n")); |
| 1015 |
if (features & LIRC_CAN_SET_SEND_CARRIER) |
| 1016 |
printf(_(" - Set carrier\n")); |
| 1017 |
if (features & LIRC_CAN_SET_SEND_DUTY_CYCLE) |
| 1018 |
printf(_(" - Set duty cycle\n")); |
| 1019 |
if (features & LIRC_CAN_SET_TRANSMITTER_MASK) { |
| 1020 |
unsigned mask = ~0; |
| 1021 |
rc = ioctl(fd, LIRC_SET_TRANSMITTER_MASK, &mask); |
| 1022 |
if (rc == 0) |
| 1023 |
fprintf(stderr, _("warning: %s: device supports setting transmitter mask but returns 0 as number of transmitters\n"), dev); |
| 1024 |
else if (rc < 0) |
| 1025 |
fprintf(stderr, _("warning: %s: device supports setting transmitter mask but returns: %m\n"), dev); |
| 1026 |
else |
| 1027 |
printf(_(" - Set transmitter (%d available)\n"), rc); |
| 1028 |
} |
| 1029 |
} else if (features & LIRC_CAN_SEND_LIRCCODE) { |
| 1030 |
printf(_(" - Device can send using device dependent LIRCCODE mode (not supported)\n")); |
| 1031 |
} else { |
| 1032 |
printf(_(" - Device cannot send\n")); |
| 1033 |
} |
| 1034 |
} |
| 1035 |
|
| 1036 |
static int lirc_send(struct arguments *args, int fd, unsigned features, struct send *f) |
| 1037 |
{ |
| 1038 |
const char *dev = args->device; |
| 1039 |
int rc, mode; |
| 1040 |
ssize_t ret; |
| 1041 |
|
| 1042 |
if (!(features & LIRC_CAN_SEND_PULSE)) { |
| 1043 |
fprintf(stderr, _("%s: device cannot send\n"), dev); |
| 1044 |
return EX_UNAVAILABLE; |
| 1045 |
} |
| 1046 |
|
| 1047 |
if (f->ty == SEND_SCANCODE) { |
| 1048 |
if (args->verbose) |
| 1049 |
printf("Sending to kernel encoder protocol:%s scancode:0x%x\n", |
| 1050 |
protocol_name(f->protocol), f->scancode); |
| 1051 |
|
| 1052 |
mode = LIRC_MODE_SCANCODE; |
| 1053 |
rc = ioctl(fd, LIRC_SET_SEND_MODE, &mode); |
| 1054 |
if (rc == 0) { |
| 1055 |
struct lirc_scancode sc = { |
| 1056 |
.scancode = f->scancode, |
| 1057 |
.rc_proto = f->protocol, |
| 1058 |
.flags = 0 |
| 1059 |
}; |
| 1060 |
ret = TEMP_FAILURE_RETRY(write(fd, &sc, sizeof sc)); |
| 1061 |
if (ret > 0) |
| 1062 |
return 0; |
| 1063 |
} |
| 1064 |
} |
| 1065 |
|
| 1066 |
mode = LIRC_MODE_PULSE; |
| 1067 |
rc = ioctl(fd, LIRC_SET_SEND_MODE, &mode); |
| 1068 |
if (rc) { |
| 1069 |
fprintf(stderr, _("%s: cannot set send mode\n"), dev); |
| 1070 |
return EX_UNAVAILABLE; |
| 1071 |
} |
| 1072 |
|
| 1073 |
if (f->ty == SEND_SCANCODE) { |
| 1074 |
// encode scancode |
| 1075 |
enum rc_proto proto = f->protocol; |
| 1076 |
if (!protocol_encoder_available(proto)) { |
| 1077 |
fprintf(stderr, _("%s: no encoder available for `%s'\n"), |
| 1078 |
dev, protocol_name(proto)); |
| 1079 |
return EX_UNAVAILABLE; |
| 1080 |
} |
| 1081 |
f->len = protocol_encode(f->protocol, f->scancode, f->buf); |
| 1082 |
f->carrier = protocol_carrier(proto); |
| 1083 |
} |
| 1084 |
|
| 1085 |
if (args->carrier != UNSET) { |
| 1086 |
if (f->carrier != UNSET) |
| 1087 |
fprintf(stderr, _("warning: carrier specified but overwritten on command line\n")); |
| 1088 |
lirc_set_send_carrier(fd, dev, features, args->carrier); |
| 1089 |
} else if (f->carrier != UNSET) |
| 1090 |
lirc_set_send_carrier(fd, dev, features, f->carrier); |
| 1091 |
|
| 1092 |
size_t size = f->len * sizeof(unsigned); |
| 1093 |
if (args->verbose) { |
| 1094 |
int i; |
| 1095 |
printf("Sending:"); |
| 1096 |
for (i=0; i<f->len; i++) |
| 1097 |
printf("%s%u", i & 1 ? " -" : " +", f->buf[i]); |
| 1098 |
putchar('\n'); |
| 1099 |
} |
| 1100 |
ret = TEMP_FAILURE_RETRY(write(fd, f->buf, size)); |
| 1101 |
if (ret < 0) { |
| 1102 |
fprintf(stderr, _("%s: failed to send: %m\n"), dev); |
| 1103 |
return EX_IOERR; |
| 1104 |
} |
| 1105 |
|
| 1106 |
if (size < ret) { |
| 1107 |
fprintf(stderr, _("warning: %s: sent %zd out %zd edges\n"), |
| 1108 |
dev, |
| 1109 |
ret / sizeof(unsigned), |
| 1110 |
size / sizeof(unsigned)); |
| 1111 |
return EX_IOERR; |
| 1112 |
} |
| 1113 |
if (args->verbose) |
| 1114 |
printf("Successfully sent\n"); |
| 1115 |
|
| 1116 |
return 0; |
| 1117 |
} |
| 1118 |
|
| 1119 |
int lirc_receive(struct arguments *args, int fd, unsigned features) |
| 1120 |
{ |
| 1121 |
char *dev = args->device; |
| 1122 |
int rc = EX_IOERR; |
| 1123 |
int mode = LIRC_MODE_MODE2; |
| 1124 |
|
| 1125 |
if (!(features & LIRC_CAN_REC_MODE2)) { |
| 1126 |
fprintf(stderr, _("%s: device cannot receive raw ir\n"), dev); |
| 1127 |
return EX_UNAVAILABLE; |
| 1128 |
} |
| 1129 |
|
| 1130 |
// kernel v4.8 and v4.9 return ENOTTY |
| 1131 |
if (ioctl(fd, LIRC_SET_REC_MODE, &mode) && errno != ENOTTY) { |
| 1132 |
fprintf(stderr, _("%s: failed to set receive mode: %m\n"), dev); |
| 1133 |
return EX_IOERR; |
| 1134 |
} |
| 1135 |
|
| 1136 |
unsigned buf[LIRCBUF_SIZE]; |
| 1137 |
|
| 1138 |
bool keep_reading = true; |
| 1139 |
bool leading_space = true; |
| 1140 |
unsigned carrier = 0; |
| 1141 |
|
| 1142 |
while (keep_reading) { |
| 1143 |
ssize_t ret = TEMP_FAILURE_RETRY(read(fd, buf, sizeof(buf))); |
| 1144 |
if (ret < 0) { |
| 1145 |
fprintf(stderr, _("%s: failed read: %m\n"), dev); |
| 1146 |
goto err; |
| 1147 |
} |
| 1148 |
|
| 1149 |
if (ret == 0 || ret % sizeof(unsigned)) { |
| 1150 |
fprintf(stderr, _("%s: read returned %zd bytes\n"), |
| 1151 |
dev, ret); |
| 1152 |
goto err; |
| 1153 |
} |
| 1154 |
|
| 1155 |
for (int i=0; i<ret / sizeof(unsigned); i++) { |
| 1156 |
unsigned val = buf[i] & LIRC_VALUE_MASK; |
| 1157 |
unsigned msg = buf[i] & LIRC_MODE2_MASK; |
| 1158 |
|
| 1159 |
// FIXME: the kernel often send us a space after |
| 1160 |
// the IR receiver comes out of idle mode. This |
| 1161 |
// is meaningless, maybe fix the kernel? |
| 1162 |
if (leading_space && msg == LIRC_MODE2_SPACE) |
| 1163 |
continue; |
| 1164 |
|
| 1165 |
leading_space = false; |
| 1166 |
if (args->oneshot && |
| 1167 |
(msg == LIRC_MODE2_TIMEOUT || |
| 1168 |
(msg == LIRC_MODE2_SPACE && val > 19000))) { |
| 1169 |
keep_reading = false; |
| 1170 |
break; |
| 1171 |
} |
| 1172 |
|
| 1173 |
if (args->mode2) { |
| 1174 |
switch (msg) { |
| 1175 |
case LIRC_MODE2_TIMEOUT: |
| 1176 |
printf("timeout %u\n", val); |
| 1177 |
leading_space = true; |
| 1178 |
break; |
| 1179 |
case LIRC_MODE2_PULSE: |
| 1180 |
printf("pulse %u\n", val); |
| 1181 |
break; |
| 1182 |
case LIRC_MODE2_SPACE: |
| 1183 |
printf("space %u\n", val); |
| 1184 |
break; |
| 1185 |
case LIRC_MODE2_FREQUENCY: |
| 1186 |
printf("carrier %u\n", val); |
| 1187 |
break; |
| 1188 |
case LIRC_MODE2_OVERFLOW: |
| 1189 |
printf("overflow\n"); |
| 1190 |
leading_space = true; |
| 1191 |
break; |
| 1192 |
} |
| 1193 |
} else { |
| 1194 |
switch (msg) { |
| 1195 |
case LIRC_MODE2_TIMEOUT: |
| 1196 |
if (carrier) |
| 1197 |
printf("-%u # carrier %uHz\n", val, carrier); |
| 1198 |
else |
| 1199 |
printf("-%u\n", val); |
| 1200 |
leading_space = true; |
| 1201 |
carrier = 0; |
| 1202 |
break; |
| 1203 |
case LIRC_MODE2_PULSE: |
| 1204 |
printf("+%u ", val); |
| 1205 |
break; |
| 1206 |
case LIRC_MODE2_SPACE: |
| 1207 |
printf("-%u ", val); |
| 1208 |
break; |
| 1209 |
case LIRC_MODE2_FREQUENCY: |
| 1210 |
carrier = val; |
| 1211 |
break; |
| 1212 |
case LIRC_MODE2_OVERFLOW: |
| 1213 |
if (carrier) |
| 1214 |
printf("# carrier %uHz, overflow\n", carrier); |
| 1215 |
else |
| 1216 |
printf("# overflow\n"); |
| 1217 |
leading_space = true; |
| 1218 |
carrier = 0; |
| 1219 |
break; |
| 1220 |
} |
| 1221 |
} |
| 1222 |
|
| 1223 |
fflush(stdout); |
| 1224 |
} |
| 1225 |
} |
| 1226 |
|
| 1227 |
rc = 0; |
| 1228 |
err: |
| 1229 |
return rc; |
| 1230 |
} |
| 1231 |
|
| 1232 |
int main(int argc, char *argv[]) |
| 1233 |
{ |
| 1234 |
struct arguments args = { |
| 1235 |
.gap = IR_DEFAULT_TIMEOUT, |
| 1236 |
.carrier = UNSET, |
| 1237 |
.timeout = UNSET, |
| 1238 |
}; |
| 1239 |
|
| 1240 |
#ifdef ENABLE_NLS |
| 1241 |
setlocale (LC_ALL, ""); |
| 1242 |
bindtextdomain (PACKAGE, LOCALEDIR); |
| 1243 |
textdomain (PACKAGE); |
| 1244 |
#endif |
| 1245 |
|
| 1246 |
argp_parse(&argp, argc, argv, 0, 0, &args); |
| 1247 |
|
| 1248 |
if (args.device == NULL) |
| 1249 |
args.device = "/dev/lirc0"; |
| 1250 |
|
| 1251 |
int rc, fd; |
| 1252 |
unsigned features; |
| 1253 |
|
| 1254 |
fd = open_lirc(args.device, &features); |
| 1255 |
if (fd < 0) |
| 1256 |
exit(EX_NOINPUT); |
| 1257 |
|
| 1258 |
rc = lirc_options(&args, fd, features); |
| 1259 |
if (rc) |
| 1260 |
exit(EX_IOERR); |
| 1261 |
|
| 1262 |
struct send *s = args.send; |
| 1263 |
while (s) { |
| 1264 |
struct send *next = s->next; |
| 1265 |
if (s->ty == SEND_GAP) { |
| 1266 |
usleep(s->gap); |
| 1267 |
} else { |
| 1268 |
if (s->ty == SEND_KEYCODE) { |
| 1269 |
struct send *k; |
| 1270 |
|
| 1271 |
if (!args.keymap) { |
| 1272 |
fprintf(stderr, _("error: no keymap specified\n")); |
| 1273 |
exit(EX_DATAERR); |
| 1274 |
} |
| 1275 |
|
| 1276 |
k = convert_keycode(args.keymap, s->keycode); |
| 1277 |
if (!k) |
| 1278 |
exit(EX_DATAERR); |
| 1279 |
|
| 1280 |
free(s); |
| 1281 |
s = k; |
| 1282 |
} |
| 1283 |
|
| 1284 |
rc = lirc_send(&args, fd, features, s); |
| 1285 |
if (rc) { |
| 1286 |
close(fd); |
| 1287 |
exit(rc); |
| 1288 |
} |
| 1289 |
} |
| 1290 |
|
| 1291 |
free(s); |
| 1292 |
s = next; |
| 1293 |
} |
| 1294 |
|
| 1295 |
if (args.receive) { |
| 1296 |
rc = lirc_receive(&args, fd, features); |
| 1297 |
if (rc) { |
| 1298 |
close(fd); |
| 1299 |
exit(rc); |
| 1300 |
} |
| 1301 |
} |
| 1302 |
|
| 1303 |
if (args.features) |
| 1304 |
lirc_features(&args, fd, features); |
| 1305 |
|
| 1306 |
free_keymap(args.keymap); |
| 1307 |
close(fd); |
| 1308 |
|
| 1309 |
return 0; |
| 1310 |
} |