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