Annotation of src/usr.bin/usbhidaction/usbhidaction.c, Revision 1.4
1.4 ! deraadt 1: /* $OpenBSD: usbhidaction.c,v 1.3 2004/04/03 21:01:25 jmc Exp $ */
1.1 nate 2: /* $NetBSD: usbhidaction.c,v 1.7 2002/01/18 14:38:59 augustss Exp $ */
3:
4: /*
5: * Copyright (c) 2000, 2002 The NetBSD Foundation, Inc.
6: * All rights reserved.
7: *
8: * This code is derived from software contributed to The NetBSD Foundation
9: * by Lennart Augustsson <lennart@augustsson.net>.
10: *
11: * Redistribution and use in source and binary forms, with or without
12: * modification, are permitted provided that the following conditions
13: * are met:
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.
19: * 3. All advertising materials mentioning features or use of this software
20: * must display the following acknowledgement:
21: * This product includes software developed by the NetBSD
22: * Foundation, Inc. and its contributors.
23: * 4. Neither the name of The NetBSD Foundation nor the names of its
24: * contributors may be used to endorse or promote products derived
25: * from this software without specific prior written permission.
26: *
27: * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28: * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29: * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37: * POSSIBILITY OF SUCH DAMAGE.
38: */
39:
40: #include <stdio.h>
41: #include <stdlib.h>
42: #include <string.h>
43: #include <ctype.h>
44: #include <err.h>
45: #include <fcntl.h>
46: #include <limits.h>
47: #include <unistd.h>
48: #include <sys/types.h>
49: #include <sys/ioctl.h>
50: #include <dev/usb/usb.h>
51: #include <dev/usb/usbhid.h>
52: #include <usbhid.h>
53: #include <util.h>
54: #include <syslog.h>
55: #include <signal.h>
56:
57: int verbose = 0;
58: int isdemon = 0;
59: int reparse = 1;
60:
61: struct command {
62: struct command *next;
63: int line;
64:
65: struct hid_item item;
66: int value;
67: char anyvalue;
68: char *name;
69: char *action;
70: };
71: struct command *commands;
72:
73: #define SIZE 4000
74:
75: void usage(void);
76: struct command *parse_conf(const char *, report_desc_t, int, int);
77: void docmd(struct command *, int, const char *, int, char **);
78: void freecommands(struct command *);
79:
80: static void
81: sighup(int sig)
82: {
83: reparse = 1;
84: }
85:
86: int
87: main(int argc, char **argv)
88: {
89: const char *conf = NULL;
90: const char *dev = NULL;
91: int fd, ch, sz, n, val, i;
92: int demon, ignore;
93: report_desc_t repd;
94: char buf[100];
95: char devnamebuf[PATH_MAX];
96: struct command *cmd;
97: int reportid;
98:
99: demon = 1;
100: ignore = 0;
101: while ((ch = getopt(argc, argv, "c:df:iv")) != -1) {
102: switch(ch) {
103: case 'c':
104: conf = optarg;
105: break;
106: case 'd':
107: demon ^= 1;
108: break;
109: case 'i':
110: ignore++;
111: break;
112: case 'f':
113: dev = optarg;
114: break;
115: case 'v':
116: demon = 0;
117: verbose++;
118: break;
119: case '?':
120: default:
121: usage();
122: }
123: }
124: argc -= optind;
125: argv += optind;
126:
127: if (conf == NULL || dev == NULL)
128: usage();
129:
1.4 ! deraadt 130: if (hid_start(NULL) == -1)
! 131: errx(1, "hid_init");
1.1 nate 132:
133: if (dev[0] != '/') {
134: snprintf(devnamebuf, sizeof(devnamebuf), "/dev/%s%s",
1.4 ! deraadt 135: isdigit(dev[0]) ? "uhid" : "", dev);
1.1 nate 136: dev = devnamebuf;
137: }
138:
139: fd = open(dev, O_RDWR);
140: if (fd < 0)
141: err(1, "%s", dev);
142: if (ioctl(fd, USB_GET_REPORT_ID, &reportid) < 0)
143: reportid = -1;
144: repd = hid_get_report_desc(fd);
145: if (repd == NULL)
1.2 jsyn 146: err(1, "hid_get_report_desc() failed");
1.1 nate 147:
148: commands = parse_conf(conf, repd, reportid, ignore);
149:
150: sz = hid_report_size(repd, hid_input, reportid);
151:
152: if (verbose)
153: printf("report size %d\n", sz);
154: if (sz > sizeof buf)
155: errx(1, "report too large");
156:
157: (void)signal(SIGHUP, sighup);
158:
159: if (demon) {
160: if (daemon(0, 0) < 0)
161: err(1, "daemon()");
162: pidfile(NULL);
163: isdemon = 1;
164: }
165:
166: for(;;) {
167: n = read(fd, buf, sz);
168: if (verbose > 2) {
169: printf("read %d bytes:", n);
170: for (i = 0; i < n; i++)
171: printf(" %02x", buf[i]);
172: printf("\n");
173: }
174: if (n < 0) {
175: if (verbose)
176: err(1, "read");
177: else
178: exit(1);
179: }
180: #if 0
181: if (n != sz) {
182: err(2, "read size");
183: }
184: #endif
185: for (cmd = commands; cmd; cmd = cmd->next) {
186: val = hid_get_data(buf, &cmd->item);
187: if (cmd->value == val || cmd->anyvalue)
188: docmd(cmd, val, dev, argc, argv);
189: }
190: if (reparse) {
191: struct command *cmds =
192: parse_conf(conf, repd, reportid, ignore);
193: if (cmds) {
194: freecommands(commands);
195: commands = cmds;
196: }
197: reparse = 0;
198: }
199: }
200:
201: exit(0);
202: }
203:
204: void
205: usage(void)
206: {
207: extern char *__progname;
208:
1.3 jmc 209: fprintf(stderr, "Usage: %s [-div] -c config-file -f device "
210: "arg ...\n", __progname);
1.1 nate 211: exit(1);
212: }
213:
214: static int
215: peek(FILE *f)
216: {
217: int c;
218:
219: c = getc(f);
220: if (c != EOF)
221: ungetc(c, f);
222: return c;
223: }
224:
225: struct command *
226: parse_conf(const char *conf, report_desc_t repd, int reportid, int ignore)
227: {
228: FILE *f;
229: char *p;
230: int line;
231: char buf[SIZE], name[SIZE], value[SIZE], action[SIZE];
232: char usage[SIZE], coll[SIZE];
233: struct command *cmd, *cmds;
234: struct hid_data *d;
235: struct hid_item h;
236: int u, lo, hi, range;
237:
238: f = fopen(conf, "r");
239: if (f == NULL)
240: err(1, "%s", conf);
241:
242: cmds = NULL;
243: for (line = 1; ; line++) {
244: if (fgets(buf, sizeof buf, f) == NULL)
245: break;
246: if (buf[0] == '#' || buf[0] == '\n')
247: continue;
248: p = strchr(buf, '\n');
249: while (p && isspace(peek(f))) {
250: if (fgets(p, sizeof buf - strlen(buf), f) == NULL)
251: break;
252: p = strchr(buf, '\n');
253: }
254: if (p)
255: *p = 0;
256: if (sscanf(buf, "%s %s %[^\n]", name, value, action) != 3) {
257: if (isdemon) {
258: syslog(LOG_WARNING, "config file `%s', line %d"
1.4 ! deraadt 259: ", syntax error: %s", conf, line, buf);
1.1 nate 260: freecommands(cmds);
261: return (NULL);
262: } else {
263: errx(1, "config file `%s', line %d,"
1.4 ! deraadt 264: ", syntax error: %s", conf, line, buf);
1.1 nate 265: }
266: }
267:
268: cmd = malloc(sizeof *cmd);
269: if (cmd == NULL)
270: err(1, "malloc failed");
271: cmd->next = cmds;
272: cmds = cmd;
273: cmd->line = line;
274:
275: if (strcmp(value, "*") == 0) {
276: cmd->anyvalue = 1;
277: } else {
278: cmd->anyvalue = 0;
279: if (sscanf(value, "%d", &cmd->value) != 1) {
280: if (isdemon) {
281: syslog(LOG_WARNING,
1.4 ! deraadt 282: "config file `%s', line %d, "
! 283: "bad value: %s\n",
! 284: conf, line, value);
1.1 nate 285: freecommands(cmds);
286: return (NULL);
287: } else {
288: errx(1, "config file `%s', line %d, "
1.4 ! deraadt 289: "bad value: %s\n",
! 290: conf, line, value);
1.1 nate 291: }
292: }
293: }
294:
295: coll[0] = 0;
296: for (d = hid_start_parse(repd, 1 << hid_input, reportid);
1.4 ! deraadt 297: hid_get_item(d, &h); ) {
1.1 nate 298: if (verbose > 2)
299: printf("kind=%d usage=%x\n", h.kind, h.usage);
300: if (h.flags & HIO_CONST)
301: continue;
302: switch (h.kind) {
303: case hid_input:
304: if (h.usage_minimum != 0 ||
305: h.usage_maximum != 0) {
306: lo = h.usage_minimum;
307: hi = h.usage_maximum;
308: range = 1;
309: } else {
310: lo = h.usage;
311: hi = h.usage;
312: range = 0;
313: }
314: for (u = lo; u <= hi; u++) {
315: snprintf(usage, sizeof usage, "%s:%s",
1.4 ! deraadt 316: hid_usage_page(HID_PAGE(u)),
1.1 nate 317: hid_usage_in_page(u));
318: if (verbose > 2)
319: printf("usage %s\n", usage);
320: if (!strcasecmp(usage, name))
321: goto foundhid;
322: if (coll[0]) {
323: snprintf(usage, sizeof usage,
324: "%s.%s:%s", coll+1,
1.4 ! deraadt 325: hid_usage_page(HID_PAGE(u)),
1.1 nate 326: hid_usage_in_page(u));
327: if (verbose > 2)
328: printf("usage %s\n",
1.4 ! deraadt 329: usage);
1.1 nate 330: if (!strcasecmp(usage, name))
331: goto foundhid;
332: }
333: }
334: break;
335: case hid_collection:
336: snprintf(coll + strlen(coll),
337: sizeof coll - strlen(coll), ".%s:%s",
1.4 ! deraadt 338: hid_usage_page(HID_PAGE(h.usage)),
1.1 nate 339: hid_usage_in_page(h.usage));
340: break;
341: case hid_endcollection:
342: if (coll[0])
343: *strrchr(coll, '.') = 0;
344: break;
345: default:
346: break;
347: }
348: }
349: if (ignore) {
350: if (verbose)
1.2 jsyn 351: warnx("ignore item '%s'", name);
1.1 nate 352: continue;
353: }
354: if (isdemon) {
355: syslog(LOG_WARNING, "config file `%s', line %d, HID "
1.4 ! deraadt 356: "item not found: `%s'\n", conf, line, name);
1.1 nate 357: freecommands(cmds);
358: return (NULL);
359: } else {
360: errx(1, "config file `%s', line %d, HID item "
1.4 ! deraadt 361: "not found: `%s'\n", conf, line, name);
1.1 nate 362: }
363:
364: foundhid:
365: hid_end_parse(d);
366: cmd->item = h;
367: cmd->name = strdup(name);
368: cmd->action = strdup(action);
369: if (range) {
370: if (cmd->value == 1)
371: cmd->value = u - lo;
372: else
373: cmd->value = -1;
374: }
375:
376: if (verbose)
377: printf("PARSE:%d %s, %d, '%s'\n", cmd->line, name,
1.4 ! deraadt 378: cmd->value, cmd->action);
1.1 nate 379: }
380: fclose(f);
381: return (cmds);
382: }
383:
384: void
385: docmd(struct command *cmd, int value, const char *hid, int argc, char **argv)
386: {
387: char cmdbuf[SIZE], *p, *q;
388: size_t len;
389: int n, r;
390:
391: for (p = cmd->action, q = cmdbuf; *p && q < &cmdbuf[SIZE-1]; ) {
392: if (*p == '$') {
393: p++;
394: len = &cmdbuf[SIZE-1] - q;
395: if (isdigit(*p)) {
396: n = strtol(p, &p, 10) - 1;
397: if (n >= 0 && n < argc) {
398: strncpy(q, argv[n], len);
399: q += strlen(q);
400: }
401: } else if (*p == 'V') {
402: p++;
403: snprintf(q, len, "%d", value);
404: q += strlen(q);
405: } else if (*p == 'N') {
406: p++;
407: strncpy(q, cmd->name, len);
408: q += strlen(q);
409: } else if (*p == 'H') {
410: p++;
411: strncpy(q, hid, len);
412: q += strlen(q);
413: } else if (*p) {
414: *q++ = *p++;
415: }
416: } else {
417: *q++ = *p++;
418: }
419: }
420: *q = 0;
421:
422: if (verbose)
423: printf("system '%s'\n", cmdbuf);
424: r = system(cmdbuf);
425: if (verbose > 1 && r)
426: printf("return code = 0x%x\n", r);
427: }
428:
429: void
430: freecommands(struct command *cmd)
431: {
432: struct command *next;
433:
434: while (cmd) {
435: next = cmd->next;
436: free(cmd);
437: cmd = next;
438: }
439: }