1 /* $NetBSD: usbhidaction.c,v 1.8 2002/06/11 06:06:21 itojun Exp $ */
4 * SPDX-License-Identifier: BSD-2-Clause
6 * Copyright (c) 2000, 2002 The NetBSD Foundation, Inc.
9 * This code is derived from software contributed to The NetBSD Foundation
10 * by Lennart Augustsson <lennart@augustsson.net>.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
42 #include <sys/types.h>
43 #include <dev/usb/usbhid.h>
50 static int verbose = 0;
51 static int isdemon = 0;
52 static int reparse = 1;
53 static const char * pidfile = "/var/run/usbaction.pid";
68 static struct command *commands;
73 struct command *parse_conf(const char *, report_desc_t, int, int);
74 void docmd(struct command *, int, const char *, int, char **);
75 void freecommands(struct command *);
78 sighup(int sig __unused)
84 main(int argc, char **argv)
86 const char *conf = NULL;
87 const char *dev = NULL;
88 const char *table = NULL;
89 int fd, fp, ch, n, val, i;
91 int demon, ignore, dieearly;
94 char devnamebuf[PATH_MAX];
101 while ((ch = getopt(argc, argv, "c:def:ip:r:t:v")) != -1) {
122 reportid = atoi(optarg);
139 if (conf == NULL || dev == NULL)
145 snprintf(devnamebuf, sizeof(devnamebuf), "/dev/%s%s",
146 isdigit(dev[0]) ? "uhid" : "", dev);
150 fd = open(dev, O_RDWR);
153 repd = hid_get_report_desc(fd);
155 err(1, "hid_get_report_desc() failed");
157 commands = parse_conf(conf, repd, reportid, ignore);
159 sz = (size_t)hid_report_size(repd, hid_input, -1);
162 printf("report size %zu\n", sz);
164 errx(1, "report too large");
166 (void)signal(SIGHUP, sighup);
169 fp = open(pidfile, O_WRONLY|O_CREAT, S_IRUSR|S_IRGRP|S_IROTH);
171 err(1, "%s", pidfile);
172 if (daemon(0, 0) < 0)
174 snprintf(buf, sizeof(buf), "%ld\n", (long)getpid());
176 if (write(fp, buf, sz1) < 0)
177 err(1, "%s", pidfile);
183 n = read(fd, buf, sz);
185 printf("read %d bytes:", n);
186 for (i = 0; i < n; i++)
187 printf(" %02x", buf[i]);
201 for (cmd = commands; cmd; cmd = cmd->next) {
202 if (cmd->item.report_ID != 0 &&
203 buf[0] != cmd->item.report_ID)
205 if (cmd->item.flags & HIO_VARIABLE)
206 val = hid_get_data(buf, &cmd->item);
208 uint32_t pos = cmd->item.pos;
209 for (i = 0; i < cmd->item.report_count; i++) {
210 val = hid_get_data(buf, &cmd->item);
211 if (val == cmd->value)
213 cmd->item.pos += cmd->item.report_size;
216 val = (i < cmd->item.report_count) ?
219 if (cmd->value != val && cmd->anyvalue == 0)
221 if ((cmd->debounce == 0) ||
222 ((cmd->debounce == 1) && ((cmd->lastseen == -1) ||
223 (cmd->lastseen != val)))) {
224 docmd(cmd, val, dev, argc, argv);
227 if ((cmd->debounce > 1) &&
228 ((cmd->lastused == -1) ||
229 (abs(cmd->lastused - val) >= cmd->debounce))) {
230 docmd(cmd, val, dev, argc, argv);
242 struct command *cmds =
243 parse_conf(conf, repd, reportid, ignore);
245 freecommands(commands);
259 fprintf(stderr, "Usage: %s [-deiv] -c config_file -f hid_dev "
260 "[-p pidfile] [-t tablefile]\n", getprogname());
276 parse_conf(const char *conf, report_desc_t repd, int reportid, int ignore)
281 char buf[SIZE], name[SIZE], value[SIZE], debounce[SIZE], action[SIZE];
282 char usbuf[SIZE], coll[SIZE], *tmp;
283 struct command *cmd, *cmds;
286 int inst, cinst, u, lo, hi, range, t;
288 f = fopen(conf, "r");
293 for (line = 1; ; line++) {
294 if (fgets(buf, sizeof buf, f) == NULL)
296 if (buf[0] == '#' || buf[0] == '\n')
298 p = strchr(buf, '\n');
299 while (p && isspace(peek(f))) {
300 if (fgets(p, sizeof buf - strlen(buf), f) == NULL)
302 p = strchr(buf, '\n');
306 if (sscanf(buf, "%s %s %s %[^\n]",
307 name, value, debounce, action) != 4) {
309 syslog(LOG_WARNING, "config file `%s', line %d"
310 ", syntax error: %s", conf, line, buf);
314 errx(1, "config file `%s', line %d,"
315 ", syntax error: %s", conf, line, buf);
318 tmp = strchr(name, '#');
321 inst = atoi(tmp + 1);
325 cmd = malloc(sizeof *cmd);
327 err(1, "malloc failed");
332 if (strcmp(value, "*") == 0) {
336 if (sscanf(value, "%d", &cmd->value) != 1) {
339 "config file `%s', line %d, "
340 "bad value: %s (should be * or a number)\n",
345 errx(1, "config file `%s', line %d, "
346 "bad value: %s (should be * or a number)\n",
352 if (sscanf(debounce, "%d", &cmd->debounce) != 1) {
355 "config file `%s', line %d, "
356 "bad value: %s (should be a number >= 0)\n",
357 conf, line, debounce);
361 errx(1, "config file `%s', line %d, "
362 "bad value: %s (should be a number >= 0)\n",
363 conf, line, debounce);
369 for (d = hid_start_parse(repd, 1 << hid_input, reportid);
370 hid_get_item(d, &h); ) {
372 printf("kind=%d usage=%x\n", h.kind, h.usage);
373 if (h.flags & HIO_CONST)
377 if (h.usage_minimum != 0 ||
378 h.usage_maximum != 0) {
379 lo = h.usage_minimum;
380 hi = h.usage_maximum;
387 for (u = lo; u <= hi; u++) {
389 snprintf(usbuf, sizeof usbuf,
391 hid_usage_page(HID_PAGE(u)),
392 hid_usage_in_page(u));
394 snprintf(usbuf, sizeof usbuf,
396 hid_usage_page(HID_PAGE(u)),
397 hid_usage_in_page(u));
400 printf("usage %s\n", usbuf);
401 t = strlen(usbuf) - strlen(name);
403 if (strcmp(usbuf + t, name))
405 if (usbuf[t - 1] != '.')
407 } else if (strcmp(usbuf, name))
414 snprintf(coll + strlen(coll),
415 sizeof coll - strlen(coll), ".%s:%s",
416 hid_usage_page(HID_PAGE(h.usage)),
417 hid_usage_in_page(h.usage));
419 case hid_endcollection:
421 *strrchr(coll, '.') = 0;
429 warnx("ignore item '%s'", name);
433 syslog(LOG_WARNING, "config file `%s', line %d, HID "
434 "item not found: `%s'\n", conf, line, name);
438 errx(1, "config file `%s', line %d, HID item "
439 "not found: `%s'\n", conf, line, name);
447 cmd->name = strdup(name);
448 cmd->action = strdup(action);
457 printf("PARSE:%d %s, %d, '%s'\n", cmd->line, name,
458 cmd->value, cmd->action);
465 docmd(struct command *cmd, int value, const char *hid, int argc, char **argv)
467 char cmdbuf[SIZE], *p, *q;
471 for (p = cmd->action, q = cmdbuf; *p && q < &cmdbuf[SIZE-1]; ) {
474 len = &cmdbuf[SIZE-1] - q;
476 n = strtol(p, &p, 10) - 1;
477 if (n >= 0 && n < argc) {
478 strncpy(q, argv[n], len);
481 } else if (*p == 'V') {
483 snprintf(q, len, "%d", value);
485 } else if (*p == 'N') {
487 strncpy(q, cmd->name, len);
489 } else if (*p == 'H') {
491 strncpy(q, hid, len);
503 printf("system '%s'\n", cmdbuf);
505 if (verbose > 1 && r)
506 printf("return code = 0x%x\n", r);
510 freecommands(struct command *cmd)
512 struct command *next;