1 /* $NetBSD: usbhidaction.c,v 1.8 2002/06/11 06:06:21 itojun Exp $ */
5 * Copyright (c) 2000, 2002 The NetBSD Foundation, Inc.
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Lennart Augustsson <lennart@augustsson.net>.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
41 #include <sys/types.h>
42 #include <dev/usb/usbhid.h>
49 static int verbose = 0;
50 static int isdemon = 0;
51 static int reparse = 1;
52 static const char * pidfile = "/var/run/usbaction.pid";
67 static struct command *commands;
72 struct command *parse_conf(const char *, report_desc_t, int, int);
73 void docmd(struct command *, int, const char *, int, char **);
74 void freecommands(struct command *);
77 sighup(int sig __unused)
83 main(int argc, char **argv)
85 const char *conf = NULL;
86 const char *dev = NULL;
87 const char *table = NULL;
88 int fd, fp, ch, n, val, i;
90 int demon, ignore, dieearly;
93 char devnamebuf[PATH_MAX];
100 while ((ch = getopt(argc, argv, "c:def:ip:r:t:v")) != -1) {
121 reportid = atoi(optarg);
138 if (conf == NULL || dev == NULL)
144 snprintf(devnamebuf, sizeof(devnamebuf), "/dev/%s%s",
145 isdigit(dev[0]) ? "uhid" : "", dev);
149 fd = open(dev, O_RDWR);
152 repd = hid_get_report_desc(fd);
154 err(1, "hid_get_report_desc() failed");
156 commands = parse_conf(conf, repd, reportid, ignore);
158 sz = (size_t)hid_report_size(repd, hid_input, -1);
161 printf("report size %zu\n", sz);
163 errx(1, "report too large");
165 (void)signal(SIGHUP, sighup);
168 fp = open(pidfile, O_WRONLY|O_CREAT, S_IRUSR|S_IRGRP|S_IROTH);
170 err(1, "%s", pidfile);
171 if (daemon(0, 0) < 0)
173 snprintf(buf, sizeof(buf), "%ld\n", (long)getpid());
175 if (write(fp, buf, sz1) < 0)
176 err(1, "%s", pidfile);
182 n = read(fd, buf, sz);
184 printf("read %d bytes:", n);
185 for (i = 0; i < n; i++)
186 printf(" %02x", buf[i]);
200 for (cmd = commands; cmd; cmd = cmd->next) {
201 if (cmd->item.report_ID != 0 &&
202 buf[0] != cmd->item.report_ID)
204 if (cmd->item.flags & HIO_VARIABLE)
205 val = hid_get_data(buf, &cmd->item);
207 uint32_t pos = cmd->item.pos;
208 for (i = 0; i < cmd->item.report_count; i++) {
209 val = hid_get_data(buf, &cmd->item);
210 if (val == cmd->value)
212 cmd->item.pos += cmd->item.report_size;
215 val = (i < cmd->item.report_count) ?
218 if (cmd->value != val && cmd->anyvalue == 0)
220 if ((cmd->debounce == 0) ||
221 ((cmd->debounce == 1) && ((cmd->lastseen == -1) ||
222 (cmd->lastseen != val)))) {
223 docmd(cmd, val, dev, argc, argv);
226 if ((cmd->debounce > 1) &&
227 ((cmd->lastused == -1) ||
228 (abs(cmd->lastused - val) >= cmd->debounce))) {
229 docmd(cmd, val, dev, argc, argv);
241 struct command *cmds =
242 parse_conf(conf, repd, reportid, ignore);
244 freecommands(commands);
258 fprintf(stderr, "Usage: %s [-deiv] -c config_file -f hid_dev "
259 "[-p pidfile] [-t tablefile]\n", getprogname());
275 parse_conf(const char *conf, report_desc_t repd, int reportid, int ignore)
280 char buf[SIZE], name[SIZE], value[SIZE], debounce[SIZE], action[SIZE];
281 char usbuf[SIZE], coll[SIZE], *tmp;
282 struct command *cmd, *cmds;
285 int inst, cinst, u, lo, hi, range, t;
287 f = fopen(conf, "r");
292 for (line = 1; ; line++) {
293 if (fgets(buf, sizeof buf, f) == NULL)
295 if (buf[0] == '#' || buf[0] == '\n')
297 p = strchr(buf, '\n');
298 while (p && isspace(peek(f))) {
299 if (fgets(p, sizeof buf - strlen(buf), f) == NULL)
301 p = strchr(buf, '\n');
305 if (sscanf(buf, "%s %s %s %[^\n]",
306 name, value, debounce, action) != 4) {
308 syslog(LOG_WARNING, "config file `%s', line %d"
309 ", syntax error: %s", conf, line, buf);
313 errx(1, "config file `%s', line %d,"
314 ", syntax error: %s", conf, line, buf);
317 tmp = strchr(name, '#');
320 inst = atoi(tmp + 1);
324 cmd = malloc(sizeof *cmd);
326 err(1, "malloc failed");
331 if (strcmp(value, "*") == 0) {
335 if (sscanf(value, "%d", &cmd->value) != 1) {
338 "config file `%s', line %d, "
339 "bad value: %s (should be * or a number)\n",
344 errx(1, "config file `%s', line %d, "
345 "bad value: %s (should be * or a number)\n",
351 if (sscanf(debounce, "%d", &cmd->debounce) != 1) {
354 "config file `%s', line %d, "
355 "bad value: %s (should be a number >= 0)\n",
356 conf, line, debounce);
360 errx(1, "config file `%s', line %d, "
361 "bad value: %s (should be a number >= 0)\n",
362 conf, line, debounce);
368 for (d = hid_start_parse(repd, 1 << hid_input, reportid);
369 hid_get_item(d, &h); ) {
371 printf("kind=%d usage=%x\n", h.kind, h.usage);
372 if (h.flags & HIO_CONST)
376 if (h.usage_minimum != 0 ||
377 h.usage_maximum != 0) {
378 lo = h.usage_minimum;
379 hi = h.usage_maximum;
386 for (u = lo; u <= hi; u++) {
388 snprintf(usbuf, sizeof usbuf,
390 hid_usage_page(HID_PAGE(u)),
391 hid_usage_in_page(u));
393 snprintf(usbuf, sizeof usbuf,
395 hid_usage_page(HID_PAGE(u)),
396 hid_usage_in_page(u));
399 printf("usage %s\n", usbuf);
400 t = strlen(usbuf) - strlen(name);
402 if (strcmp(usbuf + t, name))
404 if (usbuf[t - 1] != '.')
406 } else if (strcmp(usbuf, name))
413 snprintf(coll + strlen(coll),
414 sizeof coll - strlen(coll), ".%s:%s",
415 hid_usage_page(HID_PAGE(h.usage)),
416 hid_usage_in_page(h.usage));
418 case hid_endcollection:
420 *strrchr(coll, '.') = 0;
428 warnx("ignore item '%s'", name);
432 syslog(LOG_WARNING, "config file `%s', line %d, HID "
433 "item not found: `%s'\n", conf, line, name);
437 errx(1, "config file `%s', line %d, HID item "
438 "not found: `%s'\n", conf, line, name);
446 cmd->name = strdup(name);
447 cmd->action = strdup(action);
456 printf("PARSE:%d %s, %d, '%s'\n", cmd->line, name,
457 cmd->value, cmd->action);
464 docmd(struct command *cmd, int value, const char *hid, int argc, char **argv)
466 char cmdbuf[SIZE], *p, *q;
470 for (p = cmd->action, q = cmdbuf; *p && q < &cmdbuf[SIZE-1]; ) {
473 len = &cmdbuf[SIZE-1] - q;
475 n = strtol(p, &p, 10) - 1;
476 if (n >= 0 && n < argc) {
477 strncpy(q, argv[n], len);
480 } else if (*p == 'V') {
482 snprintf(q, len, "%d", value);
484 } else if (*p == 'N') {
486 strncpy(q, cmd->name, len);
488 } else if (*p == 'H') {
490 strncpy(q, hid, len);
502 printf("system '%s'\n", cmdbuf);
504 if (verbose > 1 && r)
505 printf("return code = 0x%x\n", r);
509 freecommands(struct command *cmd)
511 struct command *next;